tizen 2.4 release
[framework/account/libaccounts-svc.git] / src / account_offline.c
1 /*
2  *
3  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <db-util.h>
24 #include <pthread.h>
25 #include <vasum.h>
26 #include <vconf.h>
27
28 #include <account-private.h>
29 #include <account_free.h>
30 #include <dbg.h>
31 #include "account_private_client.h"
32 #include "account_internal.h"
33
34 #ifdef TIZEN_PROFILE_MOBILE
35 #include "mobile/account.h"
36 #else
37 #include "wearable/account.h"
38 #endif
39
40 #define ACCOUNT_DB_OPEN_READONLY 0
41 #define ACCOUNT_DB_OPEN_READWRITE 1
42
43 typedef sqlite3_stmt* account_stmt;
44
45 static sqlite3* g_hAccountDB = NULL;
46 static int              g_refCntDB = 0;
47 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
48
49 static const char *_account_db_err_msg()
50 {
51         return sqlite3_errmsg(g_hAccountDB);
52 }
53
54 static int _account_db_err_code()
55 {
56         return sqlite3_errcode(g_hAccountDB);
57 }
58
59 static int _account_db_open(int mode)
60 {
61         int  rc = 0;
62         int ret = -1;
63         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
64
65         char *client_db_path = NULL;
66
67         _INFO( "start to get DB path");
68
69         ret = vsm_canonicalize_path("/opt/usr/dbspace/.account.db", &client_db_path);
70         if (ret <= 0){
71                 _ERR( "vsm_canonicalize_path fail ret = %d", ret);
72                 _ACCOUNT_FREE(client_db_path);
73                 return ACCOUNT_ERROR_PERMISSION_DENIED;
74         }
75
76         _INFO( "account_db_path canonicalized = %s", client_db_path);
77
78         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
79
80         if (!g_hAccountDB) {
81                 if(mode == ACCOUNT_DB_OPEN_READWRITE)
82                         rc = db_util_open(client_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
83                 else if(mode == ACCOUNT_DB_OPEN_READONLY)
84                         rc = db_util_open_with_options(client_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
85                 else {
86                         _ACCOUNT_FREE(client_db_path);
87                         return ACCOUNT_ERROR_DB_NOT_OPENED;
88                 }
89                 _ACCOUNT_FREE(client_db_path);
90
91                 if( _account_db_err_code() == SQLITE_PERM ){
92                         ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
93                         return ACCOUNT_ERROR_PERMISSION_DENIED;
94                 }
95
96                 ACCOUNT_RETURN_VAL((rc != SQLITE_PERM), {}, ACCOUNT_ERROR_PERMISSION_DENIED, ("Account permission denied rc : %d", rc));
97                 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
98                 g_refCntDB++;
99         } else {
100                 g_refCntDB++;
101                 _ACCOUNT_FREE(client_db_path);
102         }
103
104         ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("busy handler fail. rc : %d", rc));
105
106         return ACCOUNT_ERROR_NONE;
107 }
108
109 static int _account_db_close(void)
110 {
111         int rc = 0;
112         int ret = -1;
113
114         if (g_hAccountDB) {
115                 if (g_refCntDB > 0) {
116                         g_refCntDB--;
117                 }
118                 if (g_refCntDB == 0) {
119                         rc = db_util_close(g_hAccountDB);
120                         if(  rc == SQLITE_PERM ){
121                                 ACCOUNT_ERROR( "Access failed(SQLITE_PERM)");
122                                 return ACCOUNT_ERROR_PERMISSION_DENIED;
123                         } else if ( rc == SQLITE_BUSY ){
124                                 ACCOUNT_ERROR( "database busy");
125                                 return ACCOUNT_ERROR_DATABASE_BUSY;
126                         }
127                         ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
128                         g_hAccountDB = NULL;
129                 }
130                 ret = ACCOUNT_ERROR_NONE;
131         } else {
132                 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
133                 ret = ACCOUNT_ERROR_DB_FAILED;
134         }
135
136         return ret;
137 }
138
139 static int _account_execute_query(const char *query)
140 {
141         int rc = -1;
142         char* pszErrorMsg = NULL;
143
144         if(!query){
145                 ACCOUNT_ERROR("NULL query\n");
146                 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
147         }
148
149         if(!g_hAccountDB){
150                 ACCOUNT_ERROR("DB is not opened\n");
151                 return ACCOUNT_ERROR_DB_NOT_OPENED;
152         }
153
154         rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
155         if (SQLITE_OK != rc) {
156                 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
157                 sqlite3_free(pszErrorMsg);
158         }
159
160         return rc;
161 }
162
163 static int _account_begin_transaction(void)
164 {
165         ACCOUNT_DEBUG("_account_begin_transaction start");
166         int ret = -1;
167
168         ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
169
170         if (ret == SQLITE_BUSY){
171                 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
172                 return ACCOUNT_ERROR_DATABASE_BUSY;
173         } else if(ret != SQLITE_OK) {
174                 ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret);
175                 return ACCOUNT_ERROR_DB_FAILED;
176         }
177
178         ACCOUNT_DEBUG("_account_begin_transaction end");
179         return ACCOUNT_ERROR_NONE;
180 }
181
182 static int _account_end_transaction(bool is_success)
183 {
184         ACCOUNT_DEBUG("_account_end_transaction start");
185
186         int ret = -1;
187
188         if (is_success == true) {
189                 ret = _account_execute_query("COMMIT TRANSACTION");
190                 ACCOUNT_DEBUG("_account_end_transaction COMMIT");
191         } else {
192                 ret = _account_execute_query("ROLLBACK TRANSACTION");
193                 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
194         }
195
196         if(ret == SQLITE_PERM){
197                 ACCOUNT_ERROR("Account permission denied :: %d", ret);
198                 return ACCOUNT_ERROR_PERMISSION_DENIED;
199         }
200
201         if (ret == SQLITE_BUSY){
202                 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
203                 return ACCOUNT_ERROR_DATABASE_BUSY;
204         }
205
206         if (ret != SQLITE_OK) {
207                 ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret);
208                 return ACCOUNT_ERROR_DB_FAILED;
209         }
210
211         ACCOUNT_DEBUG("_account_end_transaction end");
212         return ACCOUNT_ERROR_NONE;
213 }
214
215 static int _account_get_record_count(char* query)
216 {
217         _INFO("_account_get_record_count");
218
219         int rc = -1;
220         int ncount = 0;
221         account_stmt pStmt = NULL;
222
223         if(!query){
224                 _ERR("NULL query\n");
225                 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
226         }
227
228         if(!g_hAccountDB){
229                 _ERR("DB is not opened\n");
230                 return ACCOUNT_ERROR_DB_NOT_OPENED;
231         }
232
233         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
234
235         if (SQLITE_BUSY == rc){
236                 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
237                 sqlite3_finalize(pStmt);
238                 return ACCOUNT_ERROR_DATABASE_BUSY;
239         } else if (SQLITE_OK != rc) {
240                 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
241                 sqlite3_finalize(pStmt);
242                 return ACCOUNT_ERROR_DB_FAILED;
243         }
244
245         rc = sqlite3_step(pStmt);
246         if (SQLITE_BUSY == rc) {
247                 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
248                 sqlite3_finalize(pStmt);
249                 return ACCOUNT_ERROR_DATABASE_BUSY;
250         } else if (SQLITE_ROW != rc) {
251                 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
252                 sqlite3_finalize(pStmt);
253                 return ACCOUNT_ERROR_DB_FAILED;
254         }
255
256         ncount = sqlite3_column_int(pStmt, 0);
257
258         _INFO("account record count [%d]", ncount);
259         sqlite3_finalize(pStmt);
260
261         return ncount;
262 }
263
264 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
265 {
266         if(!pStmt){
267                 ACCOUNT_ERROR("statement is null");
268                 return -1;
269         }
270
271         if(pos < 0){
272                 ACCOUNT_ERROR("invalid pos");
273                 return -1;
274         }
275
276         return sqlite3_bind_int(pStmt, pos, num);
277 }
278
279 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
280 {
281         _INFO("_account_query_bind_text");
282
283         if(!pStmt)
284         {
285                 _ERR("statement is null");
286                 return -1;
287         }
288
289         if(str)
290         {
291                 _INFO("sqlite3_bind_text");
292                 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
293         }
294         else
295         {
296                 _INFO("sqlite3_bind_null");
297                 return sqlite3_bind_null(pStmt, pos);
298         }
299 }
300
301 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
302 {
303         _INFO("");
304
305         int count = 1;
306
307         /*Caution : Keep insert query orders.*/
308
309         /* 1. app id*/
310         _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
311
312         /* 2. service provider id*/
313         _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
314
315         /* 3. icon path*/
316         _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
317
318         /* 4. small icon path*/
319         _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
320
321         /* 5. multiple accont support*/
322         _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
323
324         _INFO("");
325
326         return count;
327 }
328
329 static gboolean _account_type_check_duplicated(account_type_s *data)
330 {
331         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
332         int count = 0;
333
334         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
335
336         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
337                         , ACCOUNT_TYPE_TABLE, data->app_id);
338
339         count = _account_get_record_count(query);
340         if (count > 0) {
341                 return TRUE;
342         }
343
344         return FALSE;
345 }
346
347 static int _account_query_finalize(account_stmt pStmt)
348 {
349         int rc = -1;
350
351         if (!pStmt) {
352                 ACCOUNT_ERROR( "pStmt is NULL");
353                 return ACCOUNT_ERROR_INVALID_PARAMETER;
354         }
355
356         rc = sqlite3_finalize(pStmt);
357         if (rc == SQLITE_BUSY){
358                 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
359                 return ACCOUNT_ERROR_DATABASE_BUSY;
360         } else if (rc != SQLITE_OK) {
361                 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
362                 return ACCOUNT_ERROR_DB_FAILED;
363         }
364
365         return ACCOUNT_ERROR_NONE;
366 }
367
368 static int _account_query_step(account_stmt pStmt)
369 {
370         if(!pStmt){
371                 ACCOUNT_ERROR( "pStmt is NULL");
372                 return -1;
373         }
374
375         return sqlite3_step(pStmt);
376 }
377
378 static account_stmt _account_prepare_query(char *query)
379 {
380         int                     rc = -1;
381         account_stmt    pStmt = NULL;
382
383         ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
384
385         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
386
387         ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
388
389         return pStmt;
390 }
391
392 static int _account_get_next_sequence(const char *pszName)
393 {
394         int                     rc = 0;
395         account_stmt    pStmt = NULL;
396         int                     max_seq = 0;
397         char                    szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
398
399         ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
400         ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery),  "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
401         rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
402         if (SQLITE_OK != rc) {
403                 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
404                 sqlite3_finalize(pStmt);
405                 return ACCOUNT_ERROR_DB_FAILED;
406         }
407
408         rc = sqlite3_step(pStmt);
409         max_seq = sqlite3_column_int(pStmt, 0);
410         max_seq++;
411
412         /*Finalize Statement*/
413         rc = sqlite3_finalize(pStmt);
414         pStmt = NULL;
415
416         return max_seq;
417 }
418
419 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
420 {
421         int                     rc, count = 1;
422         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
423         account_stmt    hstmt = NULL;
424
425         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
426         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
427
428         if (g_slist_length( account_type->provider_feature_list)==0) {
429                 ACCOUNT_ERROR( "no capability\n");
430                 return ACCOUNT_ERROR_NONE;
431         }
432
433         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
434
435         rc = _account_get_record_count(query);
436
437         if( _account_db_err_code() == SQLITE_PERM ){
438                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
439                 return ACCOUNT_ERROR_PERMISSION_DENIED;
440         }
441
442         if (rc <= 0) {
443                 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
444                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
445         }
446
447         /* insert query*/
448
449         GSList *iter;
450
451         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
452                 int ret;
453                 count = 1;
454                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
455                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
456                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
457
458                 hstmt = _account_prepare_query(query);
459
460                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
461
462                 provider_feature_s* feature_data = NULL;
463                 feature_data = (provider_feature_s*)iter->data;
464
465                 ret = _account_query_bind_text(hstmt, count++, app_id);
466                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
467                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
468                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
469
470                 rc = _account_query_step(hstmt);
471
472                 if (rc != SQLITE_DONE) {
473                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
474                         break;
475                 }
476
477                 rc = _account_query_finalize(hstmt);
478                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
479                 hstmt = NULL;
480
481         }
482
483         return ACCOUNT_ERROR_NONE;
484 }
485
486 static int _account_type_insert_label(account_type_s *account_type)
487 {
488         int                     rc, count = 1;
489         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
490         account_stmt    hstmt = NULL;
491
492         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
493
494         if (g_slist_length( account_type->label_list)==0) {
495                 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
496                 return ACCOUNT_ERROR_NONE;
497         }
498
499         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
500
501         rc = _account_get_record_count(query);
502
503         if( _account_db_err_code() == SQLITE_PERM ){
504                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
505                 return ACCOUNT_ERROR_PERMISSION_DENIED;
506         }
507
508         if (rc <= 0) {
509                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
510         }
511
512         /* insert query*/
513         GSList *iter;
514
515         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
516                 int ret;
517                 count = 1;
518                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
519                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
520                                 "(?, ?, ?) ", LABEL_TABLE);
521
522                 hstmt = _account_prepare_query(query);
523
524                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
525
526                 label_s* label_data = NULL;
527                 label_data = (label_s*)iter->data;
528
529                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
530                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
531                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
532                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
533                 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
534                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
535
536                 rc = _account_query_step(hstmt);
537
538                 if (rc != SQLITE_DONE) {
539                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
540                         break;
541                 }
542
543                 rc = _account_query_finalize(hstmt);
544                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
545                 hstmt = NULL;
546
547         }
548
549         return ACCOUNT_ERROR_NONE;
550 }
551
552 static int _account_type_execute_insert_query(account_type_s *account_type)
553 {
554         _INFO("");
555
556         int                             rc = 0;
557         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
558         int                             error_code = ACCOUNT_ERROR_NONE;
559         account_stmt    hstmt = NULL;
560
561         /* check mandatory field */
562         // app id & service provider id
563         if (!account_type->app_id) {
564                 return ACCOUNT_ERROR_INVALID_PARAMETER;
565         }
566
567         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
568         ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
569                         "(?, ?, ?, ?, ?)",      ACCOUNT_TYPE_TABLE);
570
571         _INFO("");
572         hstmt = _account_prepare_query(query);
573         _INFO("");
574
575         if( _account_db_err_code() == SQLITE_PERM ){
576                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
577                 return ACCOUNT_ERROR_PERMISSION_DENIED;
578         } else if( _account_db_err_code() == SQLITE_BUSY ){
579                 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
580                 return ACCOUNT_ERROR_DATABASE_BUSY;
581         }
582
583         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
584
585         _INFO("");
586         _account_type_convert_account_to_sql(account_type, hstmt, query);
587         _INFO("");
588
589         rc = _account_query_step(hstmt);
590         if (rc == SQLITE_BUSY) {
591                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
592                 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
593         } else if (rc != SQLITE_DONE) {
594                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
595                 error_code = ACCOUNT_ERROR_DB_FAILED;
596         }
597
598         _INFO("");
599         rc = _account_query_finalize(hstmt);
600         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
601         hstmt = NULL;
602
603         _INFO("");
604         return error_code;
605 }
606
607 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
608 {
609         _INFO("");
610
611         int             error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
612
613         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
614         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
615         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
616
617         account_type_s *data = (account_type_s*)account_type;
618
619         pthread_mutex_lock(&account_mutex);
620
621
622         /* transaction control required*/
623         ret_transaction = _account_begin_transaction();
624
625         _INFO("");
626
627         if( _account_db_err_code() == SQLITE_PERM ){
628                 pthread_mutex_unlock(&account_mutex);
629                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
630                 return ACCOUNT_ERROR_PERMISSION_DENIED;
631         }
632
633         _INFO("");
634         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
635                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
636                 pthread_mutex_unlock(&account_mutex);
637                 return ACCOUNT_ERROR_DATABASE_BUSY;
638         } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
639                 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
640                 pthread_mutex_unlock(&account_mutex);
641                 return ret_transaction;
642         }
643
644         _INFO("");
645         if (_account_type_check_duplicated(data)) {
646                 _INFO("");
647                 ret_transaction = _account_end_transaction(FALSE);
648                 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
649                 *account_type_id = -1;
650                 pthread_mutex_unlock(&account_mutex);
651                 return ACCOUNT_ERROR_DUPLICATED;
652         } else {
653                 _INFO("");
654                 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
655
656                 error_code = _account_type_execute_insert_query(data);
657
658                 if (error_code != ACCOUNT_ERROR_NONE){
659                         error_code = ACCOUNT_ERROR_DUPLICATED;
660                         ret_transaction = _account_end_transaction(FALSE);
661                         ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
662                         *account_type_id = -1;
663                         pthread_mutex_unlock(&account_mutex);
664                         return error_code;
665                 }
666         }
667
668         _INFO("");
669         error_code = _account_type_insert_provider_feature(data, data->app_id);
670         if(error_code != ACCOUNT_ERROR_NONE) {
671                 _INFO("");
672                 ret_transaction = _account_end_transaction(FALSE);
673                 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
674                 pthread_mutex_unlock(&account_mutex);
675                 return error_code;
676         }
677         _INFO("");
678         error_code = _account_type_insert_label(data);
679         if(error_code != ACCOUNT_ERROR_NONE) {
680                 _INFO("");
681                 ret_transaction = _account_end_transaction(FALSE);
682                 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
683                 pthread_mutex_unlock(&account_mutex);
684                 return error_code;
685         }
686
687         ret_transaction = _account_end_transaction(TRUE);
688         _INFO("");
689         pthread_mutex_unlock(&account_mutex);
690
691         _INFO("");
692         return ACCOUNT_ERROR_NONE;
693 }
694
695 int _account_type_delete_by_app_id(const char* app_id)
696 {
697         int                     error_code = ACCOUNT_ERROR_NONE;
698         account_stmt    hstmt = NULL;
699         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
700         int                     rc = 0, count = -1;
701         int                     ret_transaction = 0;
702         int                             binding_count = 1;
703         bool                    is_success = FALSE;
704
705         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
706         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
707
708         /* Check requested ID to delete */
709         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
710
711         count = _account_get_record_count(query);
712
713         if( _account_db_err_code() == SQLITE_PERM ){
714                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
715                 return ACCOUNT_ERROR_PERMISSION_DENIED;
716         }
717
718         if (count <= 0) {
719                 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
720                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
721         }
722
723         /* transaction control required*/
724         ret_transaction = _account_begin_transaction();
725
726         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
727                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
728                 pthread_mutex_unlock(&account_mutex);
729                 return ACCOUNT_ERROR_DATABASE_BUSY;
730         }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
731                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
732                 pthread_mutex_unlock(&account_mutex);
733                 return ret_transaction;
734         }
735
736         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
737
738         hstmt = _account_prepare_query(query);
739
740         if( _account_db_err_code() == SQLITE_PERM ){
741                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
742                 pthread_mutex_unlock(&account_mutex);
743                 return ACCOUNT_ERROR_PERMISSION_DENIED;
744         }
745
746         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
747                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
748
749         _account_query_bind_text(hstmt, binding_count++, app_id);
750
751         rc = _account_query_step(hstmt);
752         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
753
754         rc = _account_query_finalize(hstmt);
755         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
756         hstmt = NULL;
757
758         binding_count = 1;
759         ACCOUNT_MEMSET(query, 0, sizeof(query));
760
761         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
762
763         hstmt = _account_prepare_query(query);
764         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
765                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
766
767         _account_query_bind_text(hstmt, binding_count++, app_id);
768
769         rc = _account_query_step(hstmt);
770         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
771
772         rc = _account_query_finalize(hstmt);
773         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
774         is_success = TRUE;
775
776         hstmt = NULL;
777
778         binding_count = 1;
779         ACCOUNT_MEMSET(query, 0, sizeof(query));
780
781         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
782
783         hstmt = _account_prepare_query(query);
784         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
785                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
786
787         _account_query_bind_text(hstmt, binding_count++, app_id);
788
789         rc = _account_query_step(hstmt);
790         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
791
792         rc = _account_query_finalize(hstmt);
793         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
794         is_success = TRUE;
795
796         hstmt = NULL;
797
798         CATCH:
799         if (hstmt != NULL) {
800                 rc = _account_query_finalize(hstmt);
801                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
802                 hstmt = NULL;
803         }
804
805         ret_transaction = _account_end_transaction(is_success);
806
807         if (ret_transaction != ACCOUNT_ERROR_NONE) {
808                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
809         }
810
811         pthread_mutex_unlock(&account_mutex);
812
813         return error_code;
814 }
815
816 ACCOUNT_INTERNAL_API int account_type_insert_to_db_offline(account_type_h account_type, int* account_type_id)
817 {
818         _INFO("account_type_insert_to_db starting");
819
820         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
821         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
822
823         int db_id = -1;
824
825         _INFO("account_manager_account_type_add start");
826
827         guint pid = getpid();
828         _INFO("client Id = [%u]", pid);
829
830         int return_code = _account_db_open(1);
831         if (return_code != ACCOUNT_ERROR_NONE)
832         {
833                 _ERR("_account_db_open() error, ret = %d", return_code);
834
835                 goto RETURN;
836         }
837
838         int uid = getuid();
839         if (uid != 0)
840         {
841                 _ERR("current daemon is not root user, uid=%d", uid);
842                 goto RETURN;
843         }
844
845         _INFO("before _account_type_insert_to_db");
846         return_code = _account_type_insert_to_db((account_type_s*)account_type, &db_id);
847         _INFO("after _account_type_insert_to_db");
848         if (return_code != ACCOUNT_ERROR_NONE)
849         {
850                 _ERR("_account_type_insert_to_db error");
851                 goto RETURN;
852         }
853
854         *account_type_id = db_id;
855
856         account_type_s* account_type_data = (account_type_s*)account_type;
857         account_type_data->id = db_id;
858
859 RETURN:
860         _INFO("account_manager_account_type_add end");
861
862         if( g_hAccountDB == NULL )
863                 return return_code;
864
865         return_code = _account_db_close();
866         if (return_code != ACCOUNT_ERROR_NONE)
867         {
868                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
869                 return_code = ACCOUNT_ERROR_DB_FAILED;
870         }
871
872         return return_code;
873 }
874
875 ACCOUNT_INTERNAL_API int account_type_delete_by_app_id_offline(const char* app_id)
876 {
877         _INFO("account_type_delete_by_app_id starting");
878
879         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
880
881         guint pid = getpid();
882
883         _INFO("client Id = [%u]", pid);
884
885         int return_code = _account_db_open(1);
886         if (return_code != ACCOUNT_ERROR_NONE)
887         {
888                 _ERR("_account_db_open() error, ret = %d", return_code);
889
890                 goto RETURN;
891         }
892
893         int uid = getuid();
894         if (uid != 0)
895         {
896                 _ERR("current daemon is not root user, uid=%d", uid);
897                 goto RETURN;
898         }
899
900         _INFO("before _account_type_delete_by_app_id");
901         return_code = _account_type_delete_by_app_id(app_id);
902         _INFO("after _account_type_delete_by_app_id=[%d]", return_code);
903
904         if (return_code != ACCOUNT_ERROR_NONE)
905         {
906                 _ERR("_account_type_delete_by_app_id error");
907                 goto RETURN;
908         }
909
910 RETURN:
911         _INFO("account_type_delete_by_app_id_offline end");
912
913         if( g_hAccountDB == NULL )
914                 return return_code;
915
916         return_code = _account_db_close();
917         if (return_code != ACCOUNT_ERROR_NONE)
918         {
919                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
920                 return_code = ACCOUNT_ERROR_DB_FAILED;
921         }
922
923         return return_code;
924 }
925
926 static void _account_db_data_to_text(const char *textbuf, char **output)
927 {
928         if (textbuf && strlen(textbuf)>0) {
929                 if (*output) {
930                         free(*output);
931                         *output = NULL;
932                 }
933                 *output = strdup(textbuf);
934         }
935 }
936 static int _account_query_table_column_int(account_stmt pStmt, int pos)
937 {
938         if(!pStmt){
939                 ACCOUNT_ERROR("statement is null");
940                 return -1;
941         }
942
943         if(pos < 0){
944                 ACCOUNT_ERROR("invalid pos");
945                 return -1;
946         }
947
948         return sqlite3_column_int(pStmt, pos);
949 }
950
951 static const char *_account_query_table_column_text(account_stmt pStmt, int pos)
952 {
953         if(!pStmt){
954                 ACCOUNT_ERROR("statement is null");
955                 return NULL;
956         }
957
958         if(pos < 0){
959                 ACCOUNT_ERROR("invalid pos");
960                 return NULL;
961         }
962
963         return (const char*)sqlite3_column_text(pStmt, pos);
964 }
965
966 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
967 {
968         const char *textbuf = NULL;
969
970         account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
971         ACCOUNT_DEBUG("account_record->id =[%d]", account_record->id);
972
973         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
974         _account_db_data_to_text(textbuf, &(account_record->user_name));
975
976         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
977         _account_db_data_to_text(textbuf, &(account_record->email_address));
978
979         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
980         _account_db_data_to_text(textbuf, &(account_record->display_name));
981
982         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
983         _account_db_data_to_text(textbuf, &(account_record->icon_path));
984
985         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
986         _account_db_data_to_text(textbuf, &(account_record->source));
987
988         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
989         _account_db_data_to_text(textbuf, &(account_record->package_name));
990
991         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
992         _account_db_data_to_text(textbuf, &(account_record->access_token));
993
994         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
995         _account_db_data_to_text(textbuf, &(account_record->domain_name));
996
997         account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
998
999         account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1000
1001         account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1002
1003         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1004         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1005
1006         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1007         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1008
1009         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1010         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1011
1012         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1013         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1014
1015         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1016         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1017
1018         account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1019         account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1020         account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1021         account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1022         account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1023 }
1024
1025 GList* _account_query_account_by_package_name(const char* package_name, int *error_code)
1026 {
1027         _INFO("_account_query_account_by_package_name");
1028
1029         *error_code = ACCOUNT_ERROR_NONE;
1030         account_stmt    hstmt = NULL;
1031         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1032         int                     rc = 0;
1033
1034         ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
1035         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
1036
1037         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1038
1039         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
1040
1041         hstmt = _account_prepare_query(query);
1042
1043         if( _account_db_err_code() == SQLITE_PERM ){
1044                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1045                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1046                 return NULL;
1047         }
1048
1049         int binding_count = 1;
1050         _account_query_bind_text(hstmt, binding_count++, package_name);
1051
1052         rc = _account_query_step(hstmt);
1053
1054         account_s* account_head = NULL;
1055
1056         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
1057
1058         int tmp = 0;
1059
1060         account_head = (account_s*) malloc(sizeof(account_s));
1061         if (account_head == NULL) {
1062                 ACCOUNT_FATAL("malloc Failed");
1063                 if (hstmt != NULL) {
1064                         rc = _account_query_finalize(hstmt);
1065                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1066                         hstmt = NULL;
1067                 }
1068                 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
1069                 return NULL;
1070         }
1071         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
1072
1073         while (rc == SQLITE_ROW) {
1074                 account_s* account_record = NULL;
1075
1076                 account_record = (account_s*) malloc(sizeof(account_s));
1077
1078                 if (account_record == NULL) {
1079                         ACCOUNT_FATAL("malloc Failed");
1080                         break;
1081                 }
1082                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1083
1084                 _account_convert_column_to_account(hstmt, account_record);
1085
1086                 _INFO("Adding account_list");
1087                 account_head->account_list = g_list_append(account_head->account_list, account_record);
1088
1089                 rc = _account_query_step(hstmt);
1090                 tmp++;
1091         }
1092
1093         rc = _account_query_finalize(hstmt);
1094         ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1095         hstmt = NULL;
1096 /*
1097         GList *iter;
1098
1099         tmp = g_list_length(account_head->account_list);
1100
1101         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
1102                 account_s* testaccount = (account_s*)iter->data;
1103
1104                 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
1105                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
1106         }
1107 */
1108         *error_code = ACCOUNT_ERROR_NONE;
1109
1110 CATCH:
1111         if (hstmt != NULL)
1112         {
1113                 rc = _account_query_finalize(hstmt);
1114                 if (rc != ACCOUNT_ERROR_NONE) {
1115                         *error_code = rc;
1116                         _ERR("finalize error");
1117                 }
1118                 hstmt = NULL;
1119         }
1120
1121         pthread_mutex_unlock(&account_mutex);
1122
1123         if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
1124                 _account_glist_account_free(account_head->account_list);
1125                 _ACCOUNT_FREE(account_head);
1126                 account_head = NULL;
1127         }
1128
1129         if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
1130         {
1131                 _INFO("Returning account_list");
1132 //              _remove_sensitive_info_from_non_owning_account_list(getpid(), account_head->account_list);
1133                 GList* result = account_head->account_list;
1134                 _ACCOUNT_FREE(account_head);
1135                 return result;
1136         }
1137         return NULL;
1138 }
1139
1140 static void _account_insert_delete_update_notification_send(char *noti_name, int pid)
1141 {
1142         vsm_context_h ctx;
1143         vsm_zone_h effective_zone, real_zone;
1144
1145         if (!noti_name) {
1146                 _ERR("Noti Name is NULL!!!!!!\n");
1147                 return;
1148         }
1149
1150         //Tizen zone [[
1151         ctx = vsm_create_context();
1152
1153         if(ctx == NULL) {
1154                 _ERR( "Failed to initialize domain control vsm context.");
1155                 return;
1156         }
1157
1158         effective_zone = vsm_lookup_zone_by_pid(ctx, pid);
1159         if(effective_zone == NULL) {
1160                 _ERR( "Failed vsm_lookup_zone_by_pid.");
1161                 return;
1162         }
1163
1164         _INFO( "before set vsm_join_zone()");
1165         real_zone = vsm_join_zone(effective_zone);
1166         _INFO( "after set vsm_join_zone()");
1167         //]]
1168
1169         _INFO("noti_type = %s", noti_name);
1170
1171         if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
1172                 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
1173         }
1174
1175         _INFO( "before recover vsm_join_zone()");
1176         vsm_join_zone(real_zone);
1177         _INFO( "after recover vsm_join_zone()");
1178
1179         _INFO( "before vsm_cleanup_context()");
1180         vsm_cleanup_context(ctx);
1181         _INFO( "after vsm_cleanup_context()");
1182 }
1183
1184 int _account_delete_from_db_by_package_name_offline(const char *package_name)
1185 {
1186         int                     error_code = ACCOUNT_ERROR_NONE;
1187         account_stmt    hstmt = NULL;
1188         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1189         int                     rc = 0;
1190         int                     ret_transaction = 0;
1191         bool                    is_success = FALSE;
1192         int                     binding_count = 1;
1193         GSList                  *account_id_list = NULL;
1194         int                             ret = -1;
1195
1196         // It only needs list of ids, does not need to query sensitive info. So sending 0
1197         GList* account_list_temp = _account_query_account_by_package_name(package_name, &ret);
1198         if (account_list_temp == NULL)
1199         {
1200                 _ERR("_account_query_account_by_package_name returned NULL");
1201                 return ACCOUNT_ERROR_DB_FAILED;
1202         }
1203
1204         if( _account_db_err_code() == SQLITE_PERM ){
1205                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1206                 _account_glist_account_free(account_list_temp);
1207                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1208         }
1209
1210         if(ret != ACCOUNT_ERROR_NONE){
1211                 _account_glist_account_free(account_list_temp);
1212                 return ret;
1213         }
1214
1215         account_list_temp = g_list_first(account_list_temp);
1216         _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
1217
1218         GList* iter = NULL;
1219         for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
1220         {
1221                 _INFO("iterating account_list_temp");
1222                 account_s *account = NULL;
1223                 _INFO("Before iter->data");
1224                 account = (account_s*)iter->data;
1225                 _INFO("After iter->data");
1226                 if (account != NULL)
1227                 {
1228                         char id[256] = {0, };
1229
1230                         ACCOUNT_MEMSET(id, 0, 256);
1231
1232                         ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
1233
1234                         _INFO("Adding account id [%s]", id);
1235                         account_id_list = g_slist_append(account_id_list, g_strdup(id));
1236                 }
1237         }
1238
1239         _account_glist_account_free(account_list_temp);
1240         /* transaction control required*/
1241         ret_transaction = _account_begin_transaction();
1242
1243         if( _account_db_err_code() == SQLITE_PERM ){
1244                 pthread_mutex_unlock(&account_mutex);
1245                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1246                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1247         }
1248
1249         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
1250                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
1251                 pthread_mutex_unlock(&account_mutex);
1252                 return ACCOUNT_ERROR_DATABASE_BUSY;
1253         }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
1254                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
1255                 pthread_mutex_unlock(&account_mutex);
1256                 return ret_transaction;
1257         }
1258
1259         /* delete custom table  */
1260         ACCOUNT_MEMSET(query, 0, sizeof(query));
1261         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
1262
1263         hstmt = _account_prepare_query(query);
1264
1265         if( _account_db_err_code() == SQLITE_PERM ){
1266                 _account_end_transaction(FALSE);
1267                 pthread_mutex_unlock(&account_mutex);
1268                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1269                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1270         }
1271
1272         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
1273                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
1274
1275         binding_count = 1;
1276         _account_query_bind_text(hstmt, binding_count++, package_name);
1277
1278         rc = _account_query_step(hstmt);
1279         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1280
1281         rc = _account_query_finalize(hstmt);
1282         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1283         hstmt = NULL;
1284
1285         /* delete capability table */
1286         ACCOUNT_MEMSET(query, 0, sizeof(query));
1287         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
1288
1289         hstmt = _account_prepare_query(query);
1290
1291         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
1292                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
1293
1294         binding_count = 1;
1295         _account_query_bind_text(hstmt, binding_count++, package_name);
1296
1297         rc = _account_query_step(hstmt);
1298         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1299
1300         rc = _account_query_finalize(hstmt);
1301         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1302         hstmt = NULL;
1303
1304         /* delete account table */
1305         ACCOUNT_MEMSET(query, 0, sizeof(query));
1306
1307         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
1308
1309         hstmt = _account_prepare_query(query);
1310         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
1311                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
1312
1313         binding_count = 1;
1314         _account_query_bind_text(hstmt, binding_count++, package_name);
1315
1316         rc = _account_query_step(hstmt);
1317         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. package_name=%s, rc=%d\n", package_name, rc));
1318
1319         rc = _account_query_finalize(hstmt);
1320         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1321         is_success = TRUE;
1322
1323         hstmt = NULL;
1324
1325 CATCH:
1326         if (hstmt != NULL) {
1327                 rc = _account_query_finalize(hstmt);
1328                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1329                 hstmt = NULL;
1330         }
1331
1332         ret_transaction = _account_end_transaction(is_success);
1333
1334         if (ret_transaction != ACCOUNT_ERROR_NONE) {
1335                 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
1336         } else {
1337                 if (is_success == true) {
1338                         GSList* gs_iter = NULL;
1339                         for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
1340                                 char* p_tmpid = NULL;
1341                                 p_tmpid = (char*)gs_iter->data;
1342                                 char buf[64]={0,};
1343                                 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
1344                                 ACCOUNT_SLOGD("%s", buf);
1345                                 _account_insert_delete_update_notification_send(buf, getpid());
1346                                 _ACCOUNT_FREE(p_tmpid);
1347                         }
1348                         g_slist_free(account_id_list);
1349                 }
1350         }
1351
1352         pthread_mutex_unlock(&account_mutex);
1353
1354         _INFO("_account_delete_from_db_by_package_name_offline end");
1355         return error_code;
1356 }
1357
1358 ACCOUNT_INTERNAL_API int account_delete_from_db_by_package_name_offline(const char *package_name)
1359 {
1360         _INFO("_account_delete_from_db_by_package_name_offline");
1361
1362         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
1363
1364         int return_code = _account_db_open(1);
1365         if (return_code != ACCOUNT_ERROR_NONE)
1366         {
1367                 _ERR("_account_db_open() error, ret = %d", return_code);
1368
1369                 goto RETURN;
1370         }
1371
1372         int uid = getuid();
1373         if (uid != 0)
1374         {
1375                 _ERR("current process user is not root, uid=%d", uid);
1376                 return_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1377                 goto RETURN;
1378         }
1379
1380         _INFO("before _account_delete_from_db_by_package_name_offline");
1381         return_code = _account_delete_from_db_by_package_name_offline(package_name);
1382         _INFO("after _account_delete_from_db_by_package_name_offline=[%d]", return_code);
1383
1384         if (return_code != ACCOUNT_ERROR_NONE)
1385         {
1386                 _ERR("_account_delete_from_db_by_package_name_offline error");
1387                 goto RETURN;
1388         }
1389
1390 RETURN:
1391         _INFO("account_delete_from_db_by_package_name_offline end");
1392
1393         if( g_hAccountDB == NULL )
1394                 return return_code;
1395
1396         return_code = _account_db_close();
1397         if (return_code != ACCOUNT_ERROR_NONE)
1398         {
1399                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1400                 return_code = ACCOUNT_ERROR_DB_FAILED;
1401         }
1402
1403         return return_code;
1404 }