Add multi-user support.
[platform/core/pim/libaccounts-svc.git] / src / account.c
1 /*
2  *  account
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Wonyoung Lee <wy1115.lee@samsung.com>, Sungchan Kim <sungchan81.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <account.h>
29 #include <glib.h>
30 #include <db-util.h>
31 #include <pthread.h>
32 #include <assert.h>
33 #include <account-private.h>
34 #include <vconf.h>
35 #include <app.h>
36
37 #include <pkgmgr-info.h>
38 #include <aul.h>
39 #include <unistd.h>
40 #include <tzplatform_config.h>
41
42 #define EAS_CMDLINE tzplatform_mkpath(TZ_SYS_BIN, "eas-engine")
43 #define EMAIL_SERVICE_CMDLINE tzplatform_mkpath(TZ_SYS_BIN, "email-service")
44 #define ACTIVESYNC_APPID "activesync-ui"
45
46 static sqlite3* g_hAccountDB = NULL;
47 static int              g_refCntDB = 0;
48 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
49
50 static char *_account_get_text(const char *text_data);
51 static int _account_gslist_free(GSList* list);
52 static int _account_glist_free(GList* list);
53 static char *_account_query_table_column_text(account_stmt pStmt, int pos);
54 static int _account_insert_custom(account_s *account, int account_id);
55 static int _account_update_custom(account_s *account, int account_id);
56 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data );
57 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
58
59 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
60 {
61         int ret = -1;
62         char* appid = NULL;
63         char* item = NULL;
64         GSList** appid_list = (GSList**)user_data;
65
66         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
67         item = _account_get_text(appid);
68         *appid_list = g_slist_append(*appid_list, item);
69
70         return 0;
71 }
72
73 static inline int __read_proc(const char *path, char *buf, int size)
74 {
75         int fd = 0, ret = 0;
76
77         if (buf == NULL || path == NULL) {
78                 ACCOUNT_ERROR("path and buffer is mandatory\n");
79                 return -1;
80         }
81
82         fd = open(path, O_RDONLY);
83         if (fd < 0) {
84                 ACCOUNT_ERROR("fd open error(%d)\n", fd);
85                 return -1;
86         }
87
88         ret = read(fd, buf, size - 1);
89         if (ret <= 0) {
90                 ACCOUNT_ERROR("fd read error(%d)\n", fd);
91                 close(fd);
92                 return -1;
93         } else
94                 buf[ret] = 0;
95
96         close(fd);
97
98         return ret;
99 }
100
101 char *_account_get_proc_cmdline_bypid(int pid)
102 {
103         char buf[128];
104         int ret = 0;
105
106         ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
107         ret = __read_proc(buf, buf, sizeof(buf));
108         if (ret <= 0) {
109                 ACCOUNT_INFO("No proc directory (%d)\n", pid);
110                 return NULL;
111         }
112
113         return strdup(buf);
114 }
115
116
117 static char* _account_get_current_appid()
118 {
119         int pid=0, ret=-1;
120         char appid[128]={0,};
121         pkgmgrinfo_appinfo_h ahandle=NULL;
122         char* appid_ret = NULL;
123
124         pid = getpid();
125         ACCOUNT_INFO("pid (%d)\n", pid, ret);
126         ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
127         ACCOUNT_INFO("appid (%s) ret(%d)!!!!!!\n", appid, ret);
128
129         /* SLP platform core exception */
130         if(strlen(appid) == 0){
131                 char* cmdline = NULL;
132                 cmdline = _account_get_proc_cmdline_bypid(pid);
133                 ACCOUNT_INFO("cmdline (%s)!!!!!!\n", cmdline);
134                 if(!strcmp(cmdline, EAS_CMDLINE)) {
135                         appid_ret = _account_get_text(ACTIVESYNC_APPID);
136                         return appid_ret;
137                 } else {
138                         ACCOUNT_ERROR("No app id\n");
139                         return NULL;
140                 }
141         }
142
143         appid_ret = _account_get_text(appid);
144
145         pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
146         return appid_ret;
147 }
148
149 static int _account_check_account_type_with_appid_group(const char* appid, char** verified_appid)
150 {
151         int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
152         int ret=-1;
153         pkgmgrinfo_appinfo_h ahandle=NULL;
154         pkgmgrinfo_pkginfo_h phandle=NULL;
155         char* package_id;
156         GSList* appid_list = NULL;
157         GSList* iter = NULL;
158
159         if(!appid){
160                 ACCOUNT_INFO("input param is null\n");
161                 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
162         }
163
164         if(!verified_appid){
165                 ACCOUNT_INFO("output param is null\n");
166                 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
167         }
168
169         if(!strcmp(appid, "com.samsung.setting")){
170                 ACCOUNT_DEBUG("Setting exception\n");
171                 *verified_appid = _account_get_text("com.samsung.setting");
172                 return ACCOUNT_ERROR_NONE;
173         }
174
175         /* Get app id family which is stored in account database */
176         ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
177         ACCOUNT_INFO("ahandle (%p), ret(%x)\n", ahandle, ret);
178         ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
179         ACCOUNT_INFO("package_id (%s), ret(%x)\n", package_id, ret);
180         ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
181         ACCOUNT_INFO("phandle (%p), ret(%x)\n", package_id, ret);
182
183         ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); //==> pkgÇڵ鿡 ¼ÓÇÑ ui-appid ¸ðµÎ Ã£À½
184         ACCOUNT_INFO("ret(%x)\n", ret);
185
186         /* Compare current app id with the stored app id family */
187         for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
188                 char* tmp = (char*)iter->data;
189                 if(tmp) {
190                         if(account_type_query_app_id_exist(tmp) == ACCOUNT_ERROR_NONE) {
191                                 ACCOUNT_INFO("permission verified appid(%s), current appid(%s)\n", tmp, appid);
192                                 *verified_appid = _account_get_text(tmp);
193                                 error_code = ACCOUNT_ERROR_NONE;
194                                 _ACCOUNT_FREE(tmp);
195                                 break;
196                         } else {
197                                 ACCOUNT_DEBUG("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
198                         }
199                 }
200                 _ACCOUNT_FREE(tmp);
201         }
202
203         g_slist_free(appid_list);
204         pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
205         pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
206
207         return error_code;
208 }
209
210 static const char *_account_db_err_msg()
211 {
212         assert(NULL != g_hAccountDB);
213         return sqlite3_errmsg(g_hAccountDB);
214 }
215
216 static void _account_insert_delete_update_notification_send(char *noti_name)
217 {
218         if (!noti_name) {
219                 ACCOUNT_ERROR("Noti Name is NULL!!!!!!\n");
220                 return;
221         }
222
223         ACCOUNT_DEBUG("Sending notification with value %s\n", noti_name);
224
225         if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) == 0) {
226                 ACCOUNT_VERBOSE("Vconf MSG Str set SUCCESS !!!!!!\n");;
227         } else {
228                 ACCOUNT_ERROR("Vconf MSG Str set FAILED !!!!!!\n");;
229         }
230 }
231
232 static int _account_get_record_count(char* query)
233 {
234         int rc = -1;
235         int ncount = 0;
236         account_stmt pStmt = NULL;
237
238         assert(NULL != query);
239         assert(NULL != g_hAccountDB);
240         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
241
242         rc = sqlite3_step(pStmt);
243         if (SQLITE_ROW != rc) {
244                 ACCOUNT_ERROR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
245                 sqlite3_finalize(pStmt);
246                 return ACCOUNT_ERROR_DB_FAILED;
247         }
248
249         ncount = sqlite3_column_int(pStmt, 0);
250
251         ACCOUNT_VERBOSE("count : %d, End", ncount);
252         sqlite3_finalize(pStmt);
253
254         return ncount;
255 }
256
257 static int _account_execute_query(char *query)
258 {
259         int rc = -1;
260         char* pszErrorMsg = NULL;
261
262         assert(NULL != query);
263         assert(NULL != g_hAccountDB);
264
265         ACCOUNT_INFO("query : %s", query);
266
267         rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
268         if (SQLITE_OK != rc) {
269                 ACCOUNT_FATAL("sqlite3_exec(%s) failed(%s).", query, pszErrorMsg);
270                 sqlite3_free(pszErrorMsg);
271         }
272
273         return rc;
274 }
275
276 static int _account_begin_transaction(void)
277 {
278         int ret = -1;
279
280         ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
281
282         if (ret != SQLITE_OK) {
283                 ACCOUNT_FATAL("_account_svc_begin_transaction fail :: %d", ret);
284                 return ACCOUNT_ERROR_DB_FAILED;
285         }
286
287    return ACCOUNT_ERROR_NONE;
288 }
289
290 static int _account_end_transaction(bool is_success)
291 {
292         int ret = -1;
293
294         if (is_success == true) {
295                 ret = _account_execute_query("COMMIT TRANSACTION");
296         } else {
297                 ret = _account_execute_query("ROLLBACK TRANSACTION");
298         }
299
300         if (ret != SQLITE_OK) {
301                 ACCOUNT_FATAL("_account_svc_end_transaction fail :: %d", ret);
302                 return ACCOUNT_ERROR_DB_FAILED;
303         }
304
305    return ACCOUNT_ERROR_NONE;
306 }
307
308 static bool _account_check_add_more_account(const char* app_id)
309 {
310         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
311         int                     rc = 0;
312
313         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
314         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
315
316         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
317
318         ACCOUNT_DEBUG("app id (%s)\n", app_id);
319
320         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
321         rc = _account_get_record_count(query);
322
323         ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
324
325         /* multiple account support case */
326         if(rc > 0) {
327                 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
328                 return TRUE;
329         }
330
331         /* multiple account not support case */
332         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
333         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
334         rc = _account_get_record_count(query);
335
336         if(rc <= 0) {
337                 ACCOUNT_DEBUG("app id (%s) supports single account. and there is no account of the app id\n", app_id);
338                 return TRUE;
339         }
340
341         return FALSE;
342 }
343
344 static int _account_create_all_tables(void)
345 {
346         int rc = -1;
347         int error_code = ACCOUNT_ERROR_NONE;
348         char    query[ACCOUNT_SQL_LEN_MAX] = {0, };
349
350
351         ACCOUNT_MEMSET(query, 0, sizeof(query));
352
353         ACCOUNT_VERBOSE("_account_create_all_tables begin");
354
355         /*Create the account table*/
356         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
357         rc = _account_get_record_count(query);
358         if (rc <= 0) {
359                 ACCOUNT_MEMSET(query, 0, sizeof(query));
360                 ACCOUNT_SNPRINTF(query, sizeof(query),  ACCOUNT_SCHEMA, ACCOUNT_TABLE);
361                 ACCOUNT_INFO("Create %s table", ACCOUNT_TABLE);
362                 rc = _account_execute_query(query);
363                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
364         }
365
366         /*Create capability table*/
367         ACCOUNT_MEMSET(query, 0, sizeof(query));
368         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
369         rc = _account_get_record_count(query);
370         if (rc <= 0) {
371                 ACCOUNT_MEMSET(query, 0, sizeof(query));
372                 ACCOUNT_SNPRINTF(query, sizeof(query),  CAPABILITY_SCHEMA, CAPABILITY_TABLE);
373                 ACCOUNT_INFO("Create %s table", CAPABILITY_TABLE);
374                 rc = _account_execute_query(query);
375                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
376         }
377
378         /* Create account custom table */
379         ACCOUNT_MEMSET(query, 0, sizeof(query));
380         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
381         rc = _account_get_record_count(query);
382         if (rc <= 0) {
383                 ACCOUNT_MEMSET(query, 0, sizeof(query));
384                 ACCOUNT_SNPRINTF(query, sizeof(query),  ACCOUNT_CUSTOM_SCHEMA, ACCOUNT_CUSTOM_TABLE);
385                 ACCOUNT_INFO("Create %s table", ACCOUNT_CUSTOM_TABLE);
386                 rc = _account_execute_query(query);
387                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
388         }
389
390         /* Create account type table */
391         ACCOUNT_MEMSET(query, 0, sizeof(query));
392         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
393         rc = _account_get_record_count(query);
394         if (rc <= 0) {
395                 ACCOUNT_MEMSET(query, 0, sizeof(query));
396                 ACCOUNT_SNPRINTF(query, sizeof(query),  ACCOUNT_TYPE_SCHEMA, ACCOUNT_TYPE_TABLE);
397                 ACCOUNT_INFO("Create %s table", ACCOUNT_TYPE_TABLE);
398                 rc = _account_execute_query(query);
399                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
400         }
401
402         /* Create label table */
403         ACCOUNT_MEMSET(query, 0, sizeof(query));
404         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
405         rc = _account_get_record_count(query);
406         if (rc <= 0) {
407                 ACCOUNT_MEMSET(query, 0, sizeof(query));
408                 ACCOUNT_SNPRINTF(query, sizeof(query),  LABEL_SCHEMA, LABEL_TABLE);
409                 ACCOUNT_INFO("Create %s table", LABEL_TABLE);
410                 rc = _account_execute_query(query);
411                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
412         }
413
414         /* Create account feature table */
415         ACCOUNT_MEMSET(query, 0, sizeof(query));
416         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
417         rc = _account_get_record_count(query);
418         if (rc <= 0) {
419                 ACCOUNT_MEMSET(query, 0, sizeof(query));
420                 ACCOUNT_SNPRINTF(query, sizeof(query),  PROVIDER_FEATURE_SCHEMA, PROVIDER_FEATURE_TABLE);
421                 ACCOUNT_INFO("Create %s table", PROVIDER_FEATURE_TABLE);
422                 rc = _account_execute_query(query);
423                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
424         }
425
426         return error_code;
427 }
428
429 static bool _account_check_is_all_table_exists()
430 {
431         int     rc = 0;
432         char    query[ACCOUNT_SQL_LEN_MAX] = {0,};
433         ACCOUNT_MEMSET(query, 0, sizeof(query));
434
435         ACCOUNT_VERBOSE("_account_check_is_all_table_exists");
436
437         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
438                         ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
439         rc = _account_get_record_count(query);
440
441         ACCOUNT_DEBUG("Table count = %d\n", rc);
442
443         if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
444                 ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
445                 return FALSE;
446         }
447
448         ACCOUNT_VERBOSE("END of _account_check_is_all_table_exists\n");
449
450         return TRUE;
451 }
452
453 static int _account_db_open(void)
454 {
455         int     rc = 0;
456         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
457
458         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
459
460         if (!g_hAccountDB) {
461                 rc = db_util_open(ACCOUNT_DB_NAME, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
462                 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
463
464                 g_refCntDB++;
465                 ACCOUNT_INFO("_account_db_open: The database connected. refcnt=%d ", g_refCntDB);
466         } else {
467                 g_refCntDB++;
468                 ACCOUNT_INFO("The database already connected. refcnt=%d ", g_refCntDB);
469         }
470
471         return ACCOUNT_ERROR_NONE;
472 }
473
474 static int _account_db_close(void)
475 {
476         int     rc = 0;
477         int ret = -1;
478
479         if (g_hAccountDB) {
480                 if (g_refCntDB > 0) {
481                         g_refCntDB--;
482                         ACCOUNT_INFO("_account_svc_db_close: The database disconnected. refcnt=%d ", g_refCntDB);
483                 }
484                 if (g_refCntDB == 0) {
485                         rc = db_util_close(g_hAccountDB);
486                         ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
487                         g_hAccountDB = NULL;
488                         ACCOUNT_INFO( "_account_svc_db_close: The database disconnected really. ");
489                 }
490                 ret = ACCOUNT_ERROR_NONE;
491         } else {
492                 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
493                 ret = ACCOUNT_ERROR_DB_FAILED;
494         }
495
496         return ret;
497 }
498
499 static int _account_connect(void)
500 {
501         int error_code = ACCOUNT_ERROR_NONE;
502         const char *ACCT_DIR_PATH = tzplatform_mkpath(TZ_USER_SHARE, "account");
503         pid_t pid;
504
505         error_code = mkdir(ACCT_DIR_PATH, 755);
506         pid = tzplatform_getgid(TZ_SYS_USER_GROUP);
507         error_code = chown(ACCT_DIR_PATH, -1, pid);
508
509         pthread_mutex_lock(&account_mutex);
510
511         ACCOUNT_VERBOSE("db path = %s\n", ACCOUNT_DB_NAME);
512
513         error_code = _account_db_open();
514         if (ACCOUNT_ERROR_NONE != error_code) {
515                 ACCOUNT_ERROR("The database isn't connected.\n");
516                 pthread_mutex_unlock(&account_mutex);
517                 return ACCOUNT_ERROR_DB_NOT_OPENED;
518         }
519
520         if (FALSE == _account_check_is_all_table_exists())
521                 error_code = _account_create_all_tables();
522
523         pthread_mutex_unlock(&account_mutex);
524         return ACCOUNT_ERROR_NONE;
525 }
526
527 ACCOUNT_API int account_connect (void)
528 {
529         return _account_connect();
530 }
531
532 static int _account_disconnect(void)
533 {
534         int error_code = ACCOUNT_ERROR_NONE;
535
536         pthread_mutex_lock(&account_mutex);
537         ACCOUNT_INFO("db path = %s have been closed!!!\n", ACCOUNT_DB_NAME);
538
539         error_code = _account_db_close();
540         pthread_mutex_unlock(&account_mutex);
541
542         return error_code;
543 }
544
545 ACCOUNT_API int account_disconnect (void)
546 {
547         return _account_disconnect();
548 }
549
550 static int _account_free_capability_items(account_capability_s *data)
551 {
552         _ACCOUNT_FREE(data->type);
553         _ACCOUNT_FREE(data->package_name);
554         _ACCOUNT_FREE(data->user_name);
555
556         return ACCOUNT_ERROR_NONE;
557 }
558
559 static int _account_custom_item_free(account_custom_s *data)
560 {
561         _ACCOUNT_FREE(data->app_id);
562         _ACCOUNT_FREE(data->key);
563         _ACCOUNT_FREE(data->value);
564
565         return ACCOUNT_ERROR_NONE;
566 }
567
568 static int _account_custom_gslist_free(GSList* list)
569 {
570         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
571
572         GSList* iter;
573
574         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
575                 account_custom_s *custom_data = (account_custom_s*)iter->data;
576                 _account_custom_item_free(custom_data);
577                 _ACCOUNT_FREE(custom_data);
578         }
579
580         g_slist_free(list);
581         list = NULL;
582
583         return ACCOUNT_ERROR_NONE;
584 }
585
586 static int _account_free_account_items(account_s *data)
587 {
588         _ACCOUNT_FREE(data->user_name);
589         _ACCOUNT_FREE(data->email_address);
590         _ACCOUNT_FREE(data->display_name);
591         _ACCOUNT_FREE(data->icon_path);
592         _ACCOUNT_FREE(data->source);
593         _ACCOUNT_FREE(data->package_name);
594         _ACCOUNT_FREE(data->domain_name);
595         _ACCOUNT_FREE(data->access_token);
596
597         int i;
598         for(i=0;i<USER_TXT_CNT;i++)
599                 _ACCOUNT_FREE(data->user_data_txt[i]);
600
601         _account_gslist_free(data->capablity_list);
602         _account_glist_free(data->account_list);
603         _account_custom_gslist_free(data->custom_list);
604
605         return ACCOUNT_ERROR_NONE;
606 }
607
608 static int _account_gslist_free(GSList* list)
609 {
610         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
611
612         GSList* iter;
613
614         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
615                 account_capability_s *cap_data = (account_capability_s*)iter->data;
616                 _account_free_capability_items(cap_data);
617                 _ACCOUNT_FREE(cap_data);
618         }
619
620         g_slist_free(list);
621         list = NULL;
622
623         return ACCOUNT_ERROR_NONE;
624 }
625
626 static int _account_glist_free(GList* list)
627 {
628         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
629
630         GList* iter;
631
632         for (iter = list; iter != NULL; iter = g_list_next(iter)) {
633                 account_s *account_record = (account_s*)iter->data;
634                 _account_free_account_items(account_record);
635                 _ACCOUNT_FREE(account_record);
636         }
637
638         g_list_free(list);
639         list = NULL;
640
641         return ACCOUNT_ERROR_NONE;
642 }
643
644 static gboolean _account_check_duplicated(account_s *data)
645 {
646 #if 0
647         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
648         int count = 0;
649
650         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
651
652         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and package_name='%s'"
653                         , ACCOUNT_TABLE, data->user_name, data->package_name);
654
655         count = _account_get_record_count(query);
656         if (count > 0) {
657                 ACCOUNT_INFO("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
658                         count, data->user_name, data->domain_name );
659                 return TRUE;
660         }
661 #endif
662
663         return FALSE;
664 }
665
666 static int _account_get_next_sequence(char *pszName)
667 {
668         int                     rc = 0;
669         account_stmt    pStmt = NULL;
670         int                     max_seq = 0;
671         char                    szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
672
673         ACCOUNT_VERBOSE( "[Enter] pszName:%s\n", pszName);
674
675         ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
676         ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery),  "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
677         rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
678
679         rc = sqlite3_step(pStmt);
680         max_seq = sqlite3_column_int(pStmt, 0);
681         max_seq++;
682
683         ACCOUNT_VERBOSE( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
684
685         /*Finalize Statement*/
686         rc = sqlite3_finalize(pStmt);
687         pStmt = NULL;
688
689         return max_seq;
690 }
691
692 static account_stmt _account_prepare_query(char *query)
693 {
694         int                     rc = -1;
695         account_stmt    pStmt = NULL;
696
697         ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
698
699         ACCOUNT_INFO( "prepare query : %s", query);
700
701         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
702         ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
703
704         return pStmt;
705 }
706
707 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
708 {
709         assert(NULL != pStmt);
710         assert(pos > -1);
711         return sqlite3_bind_int(pStmt, pos, num);
712 }
713
714 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
715 {
716         assert(NULL != pStmt);
717
718         if(str)
719                 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
720         else
721                 return sqlite3_bind_null(pStmt, pos);
722 }
723
724 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
725 {
726         ACCOUNT_VERBOSE( "_account_convert_account_to_sql");
727         int count = 1;
728
729         /*Caution : Keep insert query orders.*/
730
731         /* 1. user name*/
732         _account_query_bind_text(hstmt, count++, (char*)account->user_name);
733
734         /* 2. email address*/
735         _account_query_bind_text(hstmt, count++, (char*)account->email_address);
736
737         /* 3. display name*/
738         _account_query_bind_text(hstmt, count++, (char*)account->display_name);
739
740         /* 4. icon path*/
741         _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
742
743         /* 5. source*/
744         _account_query_bind_text(hstmt, count++, (char*)account->source);
745
746         /* 6. package name*/
747         _account_query_bind_text(hstmt, count++, (char*)account->package_name);
748
749         /* 7. access token*/
750         _account_query_bind_text(hstmt, count++, (char*)account->access_token);
751
752         /* 8. domain name*/
753         _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
754
755         /* 9. auth type*/
756         _account_query_bind_int(hstmt, count++, account->auth_type);
757
758         /* 10. secret */
759         _account_query_bind_int(hstmt, count++, account->secret);
760
761         /* 11. sync_support */
762         _account_query_bind_int(hstmt, count++, account->sync_support);
763
764         int i;
765
766         /* 12. user text*/
767         for(i=0; i< USER_TXT_CNT; i++)
768                 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
769
770         /* 13. user integer     */
771         for(i=0; i< USER_INT_CNT; i++)
772                 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
773
774         return count;
775 }
776
777 static void _account_query_finalize(account_stmt pStmt)
778 {
779         int rc = -1;
780
781         if (!pStmt) {
782                 ACCOUNT_FATAL( "pStmt is NULL");
783                 return;
784         }
785
786         rc = sqlite3_finalize(pStmt);
787         if (rc != SQLITE_OK) {
788                 ACCOUNT_FATAL( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
789         }
790
791         ACCOUNT_VERBOSE( "sqlite3_finalize finish");
792 }
793
794 static int _account_query_step(account_stmt pStmt)
795 {
796         assert(NULL != pStmt);
797         return sqlite3_step(pStmt);
798 }
799
800 static int _account_execute_insert_query(account_s *account)
801 {
802         int                             rc = 0;
803         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
804         int                             error_code = ACCOUNT_ERROR_NONE;
805         account_stmt    hstmt = NULL;
806
807         /* check whether app id exist in account type db */
808
809         if (!account->user_name && !account->display_name && !account->email_address) {
810                 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
811                 return ACCOUNT_ERROR_INVALID_PARAMETER;
812         }
813
814         /* Check account own icon existence */
815         if(!account->icon_path) {
816                 char* temptxt = NULL;
817                 account_type_h account_type = NULL;
818                 error_code = account_type_create(&account_type);
819                 error_code = account_type_query_by_app_id((const char*)account->package_name, &account_type);
820                 error_code = account_type_get_icon_path(account_type, &temptxt);
821                 if (error_code != ACCOUNT_ERROR_NONE) {
822                                 ACCOUNT_ERROR("account_type_get_icon_path: Failed \n");
823                 } else if (temptxt && strlen(temptxt)) {
824                         ACCOUNT_DEBUG("icon path (%s) app_id(%s) \n", temptxt, account->package_name);
825                         account->icon_path = _account_get_text(temptxt);
826                 } else {
827                         ACCOUNT_DEBUG("account_get_icon_path: returned NULL \n");
828                 }
829                 _ACCOUNT_FREE(temptxt);
830                 error_code = account_type_destroy(account_type);
831         }
832
833         /* End of Check account own icon existence */
834
835         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
836         ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , "
837                         "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
838                         "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values "
839                         "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?)",  ACCOUNT_TABLE);
840
841         hstmt = _account_prepare_query(query);
842         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
843
844         _account_convert_account_to_sql(account, hstmt, query);
845
846         rc = _account_query_step(hstmt);
847         if (rc != SQLITE_DONE) {
848                 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
849                 error_code = ACCOUNT_ERROR_DB_FAILED;
850         }
851
852         _account_query_finalize(hstmt);
853         hstmt = NULL;
854
855         return error_code;
856 }
857
858 static int _account_insert_capability(account_s *account, int account_id)
859 {
860         int                     rc, count = 1;
861         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
862         account_stmt    hstmt = NULL;
863
864         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
865
866         if (g_slist_length( account->capablity_list)==0) {
867                 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
868                 return ACCOUNT_ERROR_NONE;
869         }
870
871         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
872
873         rc = _account_get_record_count(query);
874
875         if (rc <= 0) {
876                 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
877                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
878         }
879
880         /* insert query*/
881
882         GSList *iter;
883
884         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
885                 int rc, ret;
886                 count = 1;
887
888                 account_capability_s* cap_data = NULL;
889                 cap_data = (account_capability_s*)iter->data;
890
891                 ACCOUNT_VERBOSE("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
892
893                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
894                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
895                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
896                 hstmt = _account_prepare_query(query);
897
898                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
899
900                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
901                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
902                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
903                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
904                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
905                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
906                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
907                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
908                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
909                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
910
911                 rc = _account_query_step(hstmt);
912
913                 if (rc != SQLITE_DONE) {
914                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
915                         break;
916                 }
917
918                 _account_query_finalize(hstmt);
919                 hstmt = NULL;
920
921         }
922
923         ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
924
925         return ACCOUNT_ERROR_NONE;
926 }
927
928 static int _account_update_capability(account_s *account, int account_id)
929 {
930         int                     rc, count = 1;
931         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
932         account_stmt    hstmt = NULL;
933
934         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
935
936         if (g_slist_length( account->capablity_list)==0) {
937                 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
938                 return ACCOUNT_ERROR_NONE;
939         }
940
941         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
942
943         rc = _account_get_record_count(query);
944
945         if (rc <= 0) {
946                 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
947                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
948         }
949
950         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
951
952         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
953         hstmt = _account_prepare_query(query);
954         count = 1;
955         _account_query_bind_int(hstmt, count++, (int)account_id);
956         rc = _account_query_step(hstmt);
957
958         if (rc != SQLITE_DONE) {
959                 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
960                 return ACCOUNT_ERROR_DB_FAILED;
961         }
962         _account_query_finalize(hstmt);
963         hstmt = NULL;
964
965         GSList *iter;
966
967         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
968                 int rc, ret;
969                 count = 1;
970                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
971                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
972                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
973
974                 hstmt = _account_prepare_query(query);
975
976                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
977
978                 account_capability_s* cap_data = NULL;
979                 cap_data = (account_capability_s*)iter->data;
980
981                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
982                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
983                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
984                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
985                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
986                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
987                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
988                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
989                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
990                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
991
992                 rc = _account_query_step(hstmt);
993
994                 if (rc != SQLITE_DONE) {
995                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
996                         break;
997                 }
998
999                 _account_query_finalize(hstmt);
1000                 hstmt = NULL;
1001
1002         }
1003
1004         ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
1005
1006         return ACCOUNT_ERROR_NONE;
1007 }
1008
1009 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
1010 {
1011         int                     rc, count = 1;
1012         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1013         account_stmt    hstmt = NULL;
1014
1015         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1016
1017         if (g_slist_length( account->capablity_list)==0) {
1018                 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
1019                 return ACCOUNT_ERROR_NONE;
1020         }
1021
1022         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
1023
1024         rc = _account_get_record_count(query);
1025
1026         if (rc <= 0) {
1027                 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1028                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1029         }
1030
1031         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1032
1033         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1034         hstmt = _account_prepare_query(query);
1035         count = 1;
1036         _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1037         _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1038         rc = _account_query_step(hstmt);
1039         if (rc != SQLITE_DONE) {
1040                 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1041                 return ACCOUNT_ERROR_DB_FAILED;
1042         }
1043
1044         _account_query_finalize(hstmt);
1045         hstmt = NULL;
1046
1047         GSList* iter;
1048
1049         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1050                 int rc, ret;
1051                 count = 1;
1052                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1053                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1054                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1055
1056                 hstmt = _account_prepare_query(query);
1057
1058                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1059
1060                 account_capability_s* cap_data = NULL;
1061                 cap_data = (account_capability_s*)iter->data;
1062
1063                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1064                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1065                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1066                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1067                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1068                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1069                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1070                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1071                 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1072                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1073
1074                 rc = _account_query_step(hstmt);
1075
1076                 if (rc != SQLITE_DONE) {
1077                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1078                         break;
1079                 }
1080
1081                 _account_query_finalize(hstmt);
1082                 hstmt = NULL;
1083
1084         }
1085
1086         ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
1087
1088         return ACCOUNT_ERROR_NONE;
1089 }
1090
1091 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1092 {
1093         assert(NULL != pStmt);
1094         assert(pos > -1);
1095         return sqlite3_column_int(pStmt, pos);
1096 }
1097
1098 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
1099 {
1100         assert(NULL != pStmt);
1101         assert(pos > -1);
1102         return (char *)sqlite3_column_text(pStmt, pos);
1103 }
1104
1105 static void _account_db_data_to_text(const char *textbuf, char **output)
1106 {
1107         if (textbuf && strlen(textbuf)>0) {
1108                 if (*output) {
1109                         free(*output);
1110                         *output = NULL;
1111                 }
1112                 *output = strdup(textbuf);
1113         }
1114 }
1115
1116 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1117 {
1118         char    *textbuf = NULL;
1119
1120         account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1121
1122         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1123         _account_db_data_to_text(textbuf, &(account_record->user_name));
1124
1125         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1126         _account_db_data_to_text(textbuf, &(account_record->email_address));
1127
1128         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1129         _account_db_data_to_text(textbuf, &(account_record->display_name));
1130
1131         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1132         _account_db_data_to_text(textbuf, &(account_record->icon_path));
1133
1134         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1135         _account_db_data_to_text(textbuf, &(account_record->source));
1136
1137         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1138         _account_db_data_to_text(textbuf, &(account_record->package_name));
1139
1140         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1141         _account_db_data_to_text(textbuf, &(account_record->access_token));
1142
1143         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1144         _account_db_data_to_text(textbuf, &(account_record->domain_name));
1145
1146         account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1147
1148         account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1149
1150         account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1151
1152         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1153         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1154
1155         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1156         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1157
1158         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1159         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1160
1161         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1162         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1163
1164         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1165         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1166
1167         account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1168         account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1169         account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1170         account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1171         account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1172
1173         ACCOUNT_VERBOSE("END _account_convert_column_to_account");
1174 }
1175
1176 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1177 {
1178         char *textbuf = NULL;
1179
1180         capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1181
1182         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1183         _account_db_data_to_text(textbuf, &(capability_record->type));
1184
1185         capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1186
1187         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1188         _account_db_data_to_text(textbuf, &(capability_record->package_name));
1189
1190         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1191         _account_db_data_to_text(textbuf, &(capability_record->user_name));
1192
1193         capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1194
1195         ACCOUNT_VERBOSE("END _account_convert_column_to_capability");
1196 }
1197
1198 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1199 {
1200         char *textbuf = NULL;
1201
1202         custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1203
1204         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1205         _account_db_data_to_text(textbuf, &(custom_record->app_id));
1206
1207         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1208         _account_db_data_to_text(textbuf, &(custom_record->key));
1209
1210         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1211         _account_db_data_to_text(textbuf, &(custom_record->value));
1212
1213         ACCOUNT_VERBOSE("END _account_convert_column_to_custom");
1214 }
1215
1216 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1217 {
1218         account_s *data = (account_s*)user_data;
1219
1220         account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1221
1222         if (cap_data == NULL)
1223                 return FALSE;
1224         ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1225
1226         cap_data->type = _account_get_text(capability_type);
1227         cap_data->value = capability_value;
1228
1229         data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1230
1231         ACCOUNT_VERBOSE("_account_get_capability_text_cb :: %s\n", capability_type);
1232
1233         return TRUE;
1234 }
1235
1236
1237 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1238 {
1239         account_s *data = (account_s*)user_data;
1240
1241         account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1242
1243         if (custom_data == NULL) {
1244                 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1245                 return FALSE;
1246         }
1247         ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1248
1249         custom_data->account_id = data->id;
1250         custom_data->app_id = _account_get_text(data->package_name);
1251         custom_data->key = _account_get_text(key);
1252         custom_data->value = _account_get_text(value);
1253
1254         data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1255
1256         return TRUE;
1257 }
1258
1259
1260 static char *_account_get_text(const char *text_data)
1261 {
1262         char *text_value = NULL;
1263
1264         if (text_data != NULL) {
1265                 text_value = strdup(text_data);
1266                 ACCOUNT_VERBOSE("text_value = %s", text_value);
1267         }
1268         return text_value;
1269 }
1270
1271 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
1272 {
1273         int                             error_code = ACCOUNT_ERROR_NONE;
1274         account_stmt    hstmt = NULL;
1275         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1276         int                             rc = 0;
1277         account_s *old_account = NULL;
1278
1279         ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1280         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1281         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1282         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1283
1284         old_account = (account_s*)calloc(1, sizeof(account_s));
1285         if(!old_account) {
1286                 ACCOUNT_FATAL("Memory alloc fail\n");
1287                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1288         }
1289
1290         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1291
1292         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
1293         hstmt = _account_prepare_query(query);
1294
1295         rc = _account_query_step(hstmt);
1296         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1297
1298         while (rc == SQLITE_ROW) {
1299                 _account_convert_column_to_account(hstmt, old_account);
1300                 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
1301                 rc = _account_query_step(hstmt);
1302         }
1303
1304         _account_query_finalize(hstmt);
1305         hstmt = NULL;
1306
1307         // get capability
1308         error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1309         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1310
1311         // get custom text
1312         error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1313         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1314
1315         // compare
1316         new_account->id = old_account->id;
1317
1318         //user name
1319         if(!new_account->user_name) {
1320                 if(old_account->user_name)
1321                         new_account->user_name = _account_get_text(old_account->user_name);
1322         }
1323
1324         // display name
1325         if(!new_account->display_name) {
1326                 if(old_account->display_name)
1327                         new_account->display_name = _account_get_text(old_account->display_name);
1328         }
1329
1330         // email address
1331         if(!new_account->email_address) {
1332                 if(old_account->email_address)
1333                         new_account->email_address = _account_get_text(old_account->email_address);
1334         }
1335
1336         // domain name
1337         if(!new_account->domain_name) {
1338                 if(old_account->domain_name)
1339                         new_account->domain_name = _account_get_text(old_account->domain_name);
1340         }
1341
1342         // icon path
1343         if(!new_account->icon_path) {
1344                 if(old_account->icon_path)
1345                         new_account->icon_path = _account_get_text(old_account->icon_path);
1346         }
1347
1348         // source
1349         if(!new_account->source) {
1350                 if(old_account->source)
1351                         new_account->source = _account_get_text(old_account->source);
1352         }
1353
1354         _ACCOUNT_FREE(new_account->package_name);
1355         new_account->package_name = _account_get_text(old_account->package_name);
1356
1357         // access token
1358         if(!new_account->access_token) {
1359                 if(old_account->access_token)
1360                         new_account->access_token = _account_get_text(old_account->access_token);
1361         }
1362
1363         // user text
1364         int i;
1365         for(i=0;i<USER_TXT_CNT;i++) {
1366                 if(!new_account->user_data_txt[i]) {
1367                         if(old_account->user_data_txt[i])
1368                                 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1369                 }
1370         }
1371
1372         // auth type
1373         if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1374                 new_account->auth_type = old_account->auth_type;
1375         }
1376
1377         //secret
1378         if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1379                 new_account->secret = old_account->secret;
1380         }
1381
1382         // sync support
1383         if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1384                 new_account->sync_support = old_account->sync_support;
1385         }
1386
1387         // user int
1388         for(i=0;i<USER_INT_CNT;i++) {
1389                 if(new_account->user_data_int[i] == 0) {
1390                                 new_account->user_data_int[i] = old_account->user_data_int[i];
1391                 }
1392         }
1393
1394         // capability
1395
1396         // user custom table
1397
1398
1399
1400         CATCH:
1401                 if (hstmt != NULL) {
1402                         _account_query_finalize(hstmt);
1403                         hstmt = NULL;
1404                 }
1405
1406                 if (old_account) {
1407                         _account_free_account_items(old_account);
1408                         _ACCOUNT_FREE(old_account);
1409                 }
1410
1411         return ACCOUNT_ERROR_NONE;
1412 }
1413
1414
1415
1416 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
1417 {
1418         int                             rc = 0, binding_count = 0, count = 0;
1419         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1420         int                             error_code = ACCOUNT_ERROR_NONE;
1421         account_stmt    hstmt = NULL;
1422
1423         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1424         ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1425
1426         char* current_appid = NULL;
1427         char* verified_appid = NULL;
1428
1429         current_appid = _account_get_current_appid();
1430         error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
1431
1432         _ACCOUNT_FREE(current_appid);
1433         _ACCOUNT_FREE(verified_appid);
1434
1435         if(error_code != ACCOUNT_ERROR_NONE){
1436                 ACCOUNT_ERROR("No permission to update\n");
1437                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1438         }
1439
1440         _account_compare_old_record_by_user_name(account, user_name, package_name);
1441
1442         if (!account->package_name) {
1443                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1444                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1445         }
1446
1447         if (!account->user_name && !account->display_name && !account->email_address) {
1448                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1449                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1450         }
1451
1452         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1453                         , ACCOUNT_TABLE, user_name, package_name);
1454
1455         count = _account_get_record_count(query);
1456         if (count <= 0) {
1457                 ACCOUNT_INFO("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1458                         count, user_name, package_name);
1459                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1460         }
1461
1462         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1463         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1464                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1465                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1466                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1467
1468         hstmt = _account_prepare_query(query);
1469         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1470
1471         binding_count = _account_convert_account_to_sql(account, hstmt, query);
1472
1473         _account_query_bind_text(hstmt, binding_count++, user_name);
1474         _account_query_bind_text(hstmt, binding_count++, package_name);
1475         rc = _account_query_step(hstmt);
1476         if (rc != SQLITE_DONE) {
1477                 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1478         }
1479         _account_query_finalize(hstmt);
1480         hstmt = NULL;
1481
1482         /*update capability*/
1483         error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1484
1485         /* update custom */
1486         error_code = _account_update_custom(account, account->id);
1487
1488         return error_code;
1489 }
1490
1491 ACCOUNT_API int account_insert_to_db(account_h account, int *account_id)
1492 {
1493         int             error_code = ACCOUNT_ERROR_NONE;
1494         int     ret_transaction = 0;
1495
1496         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1497         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1498         ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1499
1500         account_s *data = (account_s*)account;
1501
1502         pthread_mutex_lock(&account_mutex);
1503
1504         /* transaction control required*/
1505         ret_transaction = _account_begin_transaction();
1506
1507         if (ret_transaction != ACCOUNT_ERROR_NONE) {
1508                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
1509                 pthread_mutex_unlock(&account_mutex);
1510                 return ret_transaction;
1511         }
1512
1513         if (_account_check_duplicated(data)) {
1514                 ret_transaction = _account_end_transaction(FALSE);
1515                 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
1516                 *account_id = -1;
1517                 pthread_mutex_unlock(&account_mutex);
1518                 return ACCOUNT_ERROR_DUPLICATED;
1519         } else {
1520                 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1521
1522                 char* appid = NULL;
1523                 appid = _account_get_current_appid();
1524
1525                 if(!appid){
1526                         // API caller cannot be recognized
1527                         ACCOUNT_ERROR("APP ID not detectable!\n");
1528                         ret_transaction = _account_end_transaction(FALSE);
1529                         ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1530                         pthread_mutex_unlock(&account_mutex);
1531                         return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1532                 }
1533
1534                 char* verified_appid = NULL;
1535                 error_code  = _account_check_account_type_with_appid_group(appid, &verified_appid);
1536                 if(error_code != ACCOUNT_ERROR_NONE){
1537                         ret_transaction = _account_end_transaction(FALSE);
1538                         ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1539                         _ACCOUNT_FREE(verified_appid);
1540                         _ACCOUNT_FREE(appid);
1541                         pthread_mutex_unlock(&account_mutex);
1542                         return error_code;
1543                 }
1544
1545                 _ACCOUNT_FREE(appid);
1546
1547                 if(verified_appid){
1548                         if(!_account_check_add_more_account(verified_appid)) {
1549                                 ret_transaction = _account_end_transaction(FALSE);
1550                                 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1551                                 pthread_mutex_unlock(&account_mutex);
1552                                 _ACCOUNT_FREE(verified_appid);
1553                                 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1554                         }
1555
1556                         _ACCOUNT_FREE(data->package_name);
1557                         data->package_name = _account_get_text(verified_appid);
1558                         _ACCOUNT_FREE(verified_appid);
1559                 }
1560
1561                 error_code = _account_execute_insert_query(data);
1562
1563                 if (error_code != ACCOUNT_ERROR_NONE) {
1564                         ret_transaction = _account_end_transaction(FALSE);
1565                         ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1566                         *account_id = -1;
1567                         pthread_mutex_unlock(&account_mutex);
1568                         return error_code;
1569                 }
1570         }
1571
1572         ACCOUNT_VERBOSE( "_account_execute_insert_query, insert error_code : %d", error_code);
1573
1574         error_code = _account_insert_capability(data, *account_id);
1575         if (error_code != ACCOUNT_ERROR_NONE) {
1576                 ret_transaction = _account_end_transaction(FALSE);
1577                 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1578                 *account_id = -1;
1579                 pthread_mutex_unlock(&account_mutex);
1580                 return error_code;
1581         }
1582
1583         error_code = _account_insert_custom(data, *account_id);
1584         if (error_code != ACCOUNT_ERROR_NONE) {
1585                 ret_transaction = _account_end_transaction(FALSE);
1586                 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1587                 *account_id = -1;
1588                 pthread_mutex_unlock(&account_mutex);
1589                 return error_code;
1590         }
1591
1592         pthread_mutex_unlock(&account_mutex);
1593         _account_end_transaction(TRUE);
1594
1595         char buf[64]={0,};
1596         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1597         _account_insert_delete_update_notification_send(buf);
1598
1599         return ACCOUNT_ERROR_NONE;
1600
1601 }
1602
1603 ACCOUNT_API int account_create(account_h *account)
1604 {
1605         if (!account) {
1606                 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1607                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1608         }
1609
1610         account_s *data = (account_s*)malloc(sizeof(account_s));
1611
1612         if (data == NULL) {
1613                 ACCOUNT_FATAL("Memory Allocation Failed");
1614                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1615         }
1616         ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1617
1618         ACCOUNT_VERBOSE("create handle=%p\n", *account);
1619
1620         /*Setting account as visible by default*/
1621         data->secret = ACCOUNT_SECRECY_VISIBLE;
1622
1623         /*Setting account as not supporting sync by default*/
1624         data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
1625
1626         *account = (account_h)data;
1627
1628         return ACCOUNT_ERROR_NONE;
1629 }
1630
1631 ACCOUNT_API int account_set_user_name(account_h account, const char *user_name)
1632 {
1633         if (!account) {
1634                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1635                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1636         }
1637
1638         if (!user_name) {
1639                 ACCOUNT_ERROR("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1640                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1641         }
1642
1643         account_s *data = (account_s*)account;
1644
1645         _ACCOUNT_FREE(data->user_name);
1646         data->user_name = _account_get_text(user_name);
1647
1648         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1649
1650         return ACCOUNT_ERROR_NONE;
1651 }
1652
1653 ACCOUNT_API int account_set_display_name(account_h account, const char *display_name)
1654 {
1655         if (!account) {
1656                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1657                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1658         }
1659
1660         if (!display_name) {
1661                 ACCOUNT_ERROR("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1662                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1663         }
1664
1665         account_s *data = (account_s*)account;
1666
1667         _ACCOUNT_FREE(data->display_name);
1668         data->display_name = _account_get_text(display_name);
1669
1670         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1671
1672         return ACCOUNT_ERROR_NONE;
1673 }
1674
1675 ACCOUNT_API int account_set_email_address(account_h account, const char *email_address)
1676 {
1677         if (!account) {
1678                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1679                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1680         }
1681
1682         if (!email_address) {
1683                 ACCOUNT_ERROR("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1684                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1685         }
1686
1687         account_s *data = (account_s*)account;
1688
1689         _ACCOUNT_FREE(data->email_address);
1690         data->email_address = _account_get_text(email_address);
1691
1692         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1693
1694         return ACCOUNT_ERROR_NONE;
1695 }
1696
1697 ACCOUNT_API int account_set_icon_path(account_h account, const char *icon_path)
1698 {
1699         if (!account) {
1700                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1701                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1702         }
1703
1704         if (!icon_path) {
1705                 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1706                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1707         }
1708
1709         account_s *data = (account_s*)account;
1710
1711         _ACCOUNT_FREE(data->icon_path);
1712         data->icon_path = _account_get_text(icon_path);
1713
1714         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1715
1716         return ACCOUNT_ERROR_NONE;
1717 }
1718
1719 ACCOUNT_API int account_set_source(account_h account, const char *source)
1720 {
1721         if (!account) {
1722                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1723                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1724         }
1725
1726         if (!source) {
1727                 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1728                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1729         }
1730         account_s *data = (account_s*)account;
1731
1732         _ACCOUNT_FREE(data->source);
1733         data->source = _account_get_text(source);
1734
1735         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1736
1737         return ACCOUNT_ERROR_NONE;
1738 }
1739
1740 ACCOUNT_API int account_set_package_name(account_h account, const char *package_name)
1741 {
1742         if (!account) {
1743                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1744                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1745         }
1746
1747         if (!package_name) {
1748                 ACCOUNT_ERROR("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1749                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1750         }
1751
1752         account_s *data = (account_s*)account;
1753
1754         _ACCOUNT_FREE(data->package_name);
1755         data->package_name = _account_get_text(package_name);
1756
1757         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1758
1759         return ACCOUNT_ERROR_NONE;
1760 }
1761
1762 ACCOUNT_API int account_set_domain_name(account_h account, const char *domain_name)
1763 {
1764         if (!account) {
1765                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1766                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1767         }
1768
1769         if (!domain_name) {
1770                 ACCOUNT_ERROR("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1771                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1772         }
1773         account_s *data = (account_s*)account;
1774
1775         _ACCOUNT_FREE(data->domain_name);
1776         data->domain_name = _account_get_text(domain_name);
1777
1778         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1779
1780         return ACCOUNT_ERROR_NONE;
1781 }
1782
1783 ACCOUNT_API int account_set_access_token(account_h account, const char *access_token)
1784 {
1785         if (!account) {
1786                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1787                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1788         }
1789
1790         if (!access_token) {
1791                 ACCOUNT_ERROR("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1792                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1793         }
1794
1795         account_s *data = (account_s*)account;
1796
1797         _ACCOUNT_FREE(data->access_token);
1798         data->access_token = _account_get_text(access_token);
1799
1800         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1801
1802         return ACCOUNT_ERROR_NONE;
1803 }
1804
1805 ACCOUNT_API int account_set_user_text(account_h account, int index, const char *user_txt)
1806 {
1807         if (!account) {
1808                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1809                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1810         }
1811
1812         if (!user_txt) {
1813                 ACCOUNT_ERROR("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1814                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1815         }
1816         if (index >= USER_TXT_CNT || index < 0) {
1817                 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1818                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1819         }
1820
1821         account_s *data = (account_s*)account;
1822
1823         _ACCOUNT_FREE(data->user_data_txt[index]);
1824         data->user_data_txt[index] = _account_get_text(user_txt);
1825
1826         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1827
1828         return ACCOUNT_ERROR_NONE;
1829 }
1830
1831 ACCOUNT_API int account_set_custom(account_h account, const char* key, const char* value)
1832 {
1833         if (!account) {
1834                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1835                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1836         }
1837
1838         if (!key) {
1839                 ACCOUNT_ERROR("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__);
1840                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1841         }
1842
1843         if (!value) {
1844                 ACCOUNT_ERROR("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__);
1845                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1846         }
1847
1848         account_s *data = (account_s*)account;
1849
1850         GSList *iter;
1851         bool b_is_new = TRUE;
1852
1853         for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1854
1855                 account_custom_s* custom_data = NULL;
1856                 custom_data = (account_custom_s*)iter->data;
1857                 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
1858
1859                 if(!strcmp(custom_data->key, key)) {
1860                         _ACCOUNT_FREE(custom_data->value);
1861                         custom_data->value = _account_get_text(value);
1862                         b_is_new = FALSE;
1863                 }
1864         }
1865
1866         if(b_is_new) {
1867                 account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1868
1869                 if (custom_data == NULL) {
1870                         ACCOUNT_FATAL("(%s)-(%d) MALLOC FAIL\n", __FUNCTION__, __LINE__);
1871                         return ACCOUNT_ERROR_OUT_OF_MEMORY;
1872                 }
1873                 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1874                 custom_data->account_id = data->id;
1875                 custom_data->app_id = _account_get_text(data->package_name);
1876                 custom_data->key = _account_get_text(key);
1877                 custom_data->value = _account_get_text(value);
1878                 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1879         }
1880
1881         return ACCOUNT_ERROR_NONE;
1882 }
1883
1884 ACCOUNT_API int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1885 {
1886         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",  __FUNCTION__, __LINE__));
1887
1888         if ( (auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
1889                 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1890                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1891         }
1892
1893         account_s *data = (account_s*)account;
1894
1895         data->auth_type = (int)auth_type;
1896
1897         return ACCOUNT_ERROR_NONE;
1898 }
1899
1900 ACCOUNT_API int account_set_secret(account_h account, const account_secrecy_state_e secret)
1901 {
1902         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",      __FUNCTION__, __LINE__));
1903
1904         if ( (secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
1905                 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1906                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1907         }
1908
1909         account_s *data = (account_s*)account;
1910
1911         data->secret = (int)secret;
1912
1913         return ACCOUNT_ERROR_NONE;
1914 }
1915
1916 ACCOUNT_API int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
1917 {
1918         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",      __FUNCTION__, __LINE__));
1919
1920         if ( (sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) {
1921                 ACCOUNT_ERROR("(%s)-(%d) sync_support is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1922                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1923         }
1924
1925         account_s *data = (account_s*)account;
1926
1927         data->sync_support= (int)sync_support;
1928
1929         return ACCOUNT_ERROR_NONE;
1930 }
1931
1932 ACCOUNT_API int account_set_user_int(account_h account, int index, const int user_int)
1933 {
1934         if (!account) {
1935                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1936                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1937         }
1938
1939         if (index >= USER_INT_CNT ||index < 0) {
1940                 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1941                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1942         }
1943
1944         account_s *data = (account_s*)account;
1945
1946         data->user_data_int[index] = user_int;
1947
1948         return ACCOUNT_ERROR_NONE;
1949 }
1950
1951 ACCOUNT_API int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value)
1952 {
1953         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null"));
1954         ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null"));
1955
1956         if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
1957                 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1958                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1959         }
1960
1961         account_s *data = (account_s*)account;
1962
1963         GSList *iter = NULL;
1964         bool b_is_new = TRUE;
1965
1966         for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1967                 account_capability_s *cap_data = NULL;
1968                 cap_data = (account_capability_s*)iter->data;
1969
1970                 if(!strcmp(cap_data->type, capability_type)) {
1971                         cap_data->value = capability_value;
1972                         b_is_new = FALSE;
1973                         break;
1974                 }
1975         }
1976
1977         if(b_is_new) {
1978                 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1979
1980                 if (cap_data == NULL)
1981                         return ACCOUNT_ERROR_OUT_OF_MEMORY;
1982                 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1983
1984                 cap_data->type = _account_get_text(capability_type);
1985                 cap_data->value = capability_value;
1986                 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1987         }
1988
1989         return ACCOUNT_ERROR_NONE;
1990 }
1991
1992 ACCOUNT_API int account_get_user_name(account_h account, char **user_name)
1993 {
1994         if (!account) {
1995                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1996                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1997         }
1998
1999         if (!user_name) {
2000                 ACCOUNT_ERROR("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
2001                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2002         }
2003
2004         account_s *data = (account_s*)account;
2005
2006         (*user_name) = NULL;
2007         *user_name = _account_get_text(data->user_name);
2008
2009         return ACCOUNT_ERROR_NONE;
2010 }
2011
2012 ACCOUNT_API int account_get_display_name(account_h account, char **display_name)
2013 {
2014         if (!account) {
2015                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2016                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2017         }
2018
2019         if (!display_name) {
2020                 ACCOUNT_ERROR("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
2021                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2022         }
2023
2024         account_s *data = (account_s*)account;
2025
2026         (*display_name) = NULL;
2027
2028         *display_name = _account_get_text(data->display_name);
2029
2030         return ACCOUNT_ERROR_NONE;
2031 }
2032
2033 ACCOUNT_API int account_get_email_address(account_h account,char **email_address)
2034 {
2035         if (!account) {
2036                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2037                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2038         }
2039
2040         if (!email_address) {
2041                 ACCOUNT_ERROR("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
2042                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2043         }
2044
2045         account_s *data = (account_s*)account;
2046
2047         (*email_address) = NULL;
2048
2049         *email_address = _account_get_text(data->email_address);
2050
2051         return ACCOUNT_ERROR_NONE;
2052 }
2053
2054 ACCOUNT_API int  account_get_icon_path(account_h account, char **icon_path)
2055 {
2056         if (!account) {
2057                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2058                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2059         }
2060
2061         if (!icon_path) {
2062                 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
2063                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2064         }
2065
2066         account_s *data = (account_s*)account;
2067
2068         (*icon_path) = NULL;
2069
2070         *icon_path = _account_get_text(data->icon_path);
2071
2072         return ACCOUNT_ERROR_NONE;
2073 }
2074
2075 ACCOUNT_API int account_get_source(account_h account, char **source)
2076 {
2077         if (!account) {
2078                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2079                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2080         }
2081
2082         if (!source) {
2083                 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
2084                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2085         }
2086
2087         account_s *data = (account_s*)account;
2088
2089         (*source) = NULL;
2090
2091         *source = _account_get_text(data->source);
2092
2093         return ACCOUNT_ERROR_NONE;
2094 }
2095
2096 ACCOUNT_API int account_get_package_name(account_h account, char **package_name)
2097 {
2098         if (!account) {
2099                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2100                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2101         }
2102
2103         if (!package_name) {
2104                 ACCOUNT_ERROR("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
2105                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2106         }
2107
2108         account_s *data = (account_s*)account;
2109
2110         (*package_name) = NULL;
2111
2112         *package_name = _account_get_text(data->package_name);
2113
2114         return ACCOUNT_ERROR_NONE;
2115 }
2116
2117 ACCOUNT_API int account_get_domain_name(account_h account, char **domain_name)
2118 {
2119         if (!account) {
2120                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2121                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2122         }
2123
2124         if (!domain_name) {
2125                 ACCOUNT_ERROR("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
2126                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2127         }
2128
2129         account_s *data = (account_s*)account;
2130
2131         (*domain_name) = NULL;
2132
2133         *domain_name = _account_get_text(data->domain_name);
2134
2135         return ACCOUNT_ERROR_NONE;
2136 }
2137
2138 ACCOUNT_API int account_get_access_token(account_h account, char **access_token)
2139 {
2140         if (!account) {
2141                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2142                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2143         }
2144
2145         if (!access_token) {
2146                 ACCOUNT_ERROR("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
2147                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2148         }
2149
2150         account_s *data = (account_s*)account;
2151
2152         (*access_token) = NULL;
2153
2154         *access_token = _account_get_text(data->access_token);
2155
2156         return ACCOUNT_ERROR_NONE;
2157 }
2158
2159 ACCOUNT_API int account_get_user_text(account_h account, int user_text_index, char **text)
2160 {
2161         if (!account) {
2162                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2163                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2164         }
2165
2166         if (!text) {
2167                 ACCOUNT_ERROR("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
2168                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2169         }
2170         ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2171
2172         account_s *data = (account_s*)account;
2173
2174         (*text) = NULL;
2175
2176         *text = _account_get_text(data->user_data_txt[user_text_index]);
2177
2178         return ACCOUNT_ERROR_NONE;
2179 }
2180
2181 ACCOUNT_API int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
2182 {
2183         if (!account) {
2184                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2185                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2186         }
2187         if (!auth_type) {
2188                 ACCOUNT_ERROR("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
2189                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2190         }
2191
2192         account_s* data = (account_s*)account;
2193
2194         *auth_type = data->auth_type;
2195
2196         return ACCOUNT_ERROR_NONE;
2197 }
2198
2199 ACCOUNT_API int account_get_secret(account_h account, account_secrecy_state_e *secret)
2200 {
2201         if (!account) {
2202                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2203                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2204         }
2205         if (!secret) {
2206                 ACCOUNT_ERROR("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
2207                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2208         }
2209
2210         account_s* data = (account_s*)account;
2211
2212         *secret = data->secret;
2213
2214         return ACCOUNT_ERROR_NONE;
2215 }
2216
2217 ACCOUNT_API int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
2218 {
2219         if (!account) {
2220                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2221                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2222         }
2223         if (!sync_support) {
2224                 ACCOUNT_ERROR("(%s)-(%d) sync_support is NULL.\n", __FUNCTION__, __LINE__);
2225                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2226         }
2227
2228         account_s* data = (account_s*)account;
2229
2230         *sync_support = data->sync_support;
2231
2232         return ACCOUNT_ERROR_NONE;
2233 }
2234
2235 ACCOUNT_API int account_get_account_id(account_h account, int *account_id)
2236 {
2237         if (!account) {
2238                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2239                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2240         }
2241         if (!account_id) {
2242                 ACCOUNT_ERROR("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
2243                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2244         }
2245
2246         account_s *data = (account_s*)account;
2247
2248         *account_id = data->id;
2249
2250         return ACCOUNT_ERROR_NONE;
2251 }
2252
2253 ACCOUNT_API int account_get_user_int(account_h account, int user_int_index, int *integer)
2254 {
2255         if (!account) {
2256                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2257                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2258         }
2259
2260         ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2261
2262         if (!integer) {
2263                 ACCOUNT_ERROR("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
2264                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2265         }
2266
2267         account_s *data = (account_s*)account;
2268
2269         *integer = data->user_data_int[user_int_index];
2270
2271         return ACCOUNT_ERROR_NONE;
2272 }
2273
2274 ACCOUNT_API int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value)
2275 {
2276         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2277         ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL"));
2278         ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL"));
2279
2280         GSList *iter;
2281         account_s *data = (account_s*)account;
2282
2283         for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2284                 account_capability_s *cap_data = NULL;
2285
2286                 cap_data = (account_capability_s*)iter->data;
2287
2288                 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2289
2290                 if(!strcmp(capability_type, cap_data->type)) {
2291                         *capability_value = cap_data->value;
2292                         return ACCOUNT_ERROR_NONE;
2293                 }
2294         }
2295
2296         return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2297 }
2298
2299 ACCOUNT_API int account_get_capability_all(account_h account, capability_cb cb_func, void *user_data)
2300 {
2301         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2302         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2303
2304         GSList *iter;
2305         account_s *data = (account_s*)account;
2306
2307         for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2308                 account_capability_s *cap_data = NULL;
2309
2310                 cap_data = (account_capability_s*)iter->data;
2311
2312                 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2313
2314                 //cb_func(cap_data->type, cap_data->value, user_data);
2315                 if(cb_func(cap_data->type, cap_data->value, user_data)!=TRUE){
2316                         ACCOUNT_VERBOSE("account_get_capability ::  cb_func returns false, it is stopped\n");
2317                         return ACCOUNT_ERROR_NONE;
2318                 }
2319         }
2320
2321         return ACCOUNT_ERROR_NONE;
2322 }
2323
2324 ACCOUNT_API int account_get_custom(account_h account, const char* key, char** value)
2325 {
2326         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2327         ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST"));
2328         ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL"));
2329
2330         GSList *iter;
2331         account_s *data = (account_s*)account;
2332
2333         for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2334                 account_custom_s *custom_data = NULL;
2335
2336                 custom_data = (account_custom_s*)iter->data;
2337
2338                 ACCOUNT_VERBOSE("account_get_custom ::  key = %s, value = %s", custom_data->key, custom_data->value);
2339
2340                 if(!strcmp(key, custom_data->key)) {
2341                         (*value) = NULL;
2342                         *value = _account_get_text(custom_data->value);
2343                         return ACCOUNT_ERROR_NONE;
2344                 }
2345         }
2346
2347         ACCOUNT_INFO("key is not found %s", key);
2348
2349         return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2350 }
2351
2352 ACCOUNT_API int account_get_custom_all(account_h account, account_custom_cb cb_func, void* user_data)
2353 {
2354         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2355         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2356
2357         GSList *iter;
2358         account_s *data = (account_s*)account;
2359
2360         for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2361                 bool cb_ret = FALSE;
2362                 account_custom_s *custom_data = NULL;
2363
2364                 custom_data = (account_custom_s*)iter->data;
2365
2366                 ACCOUNT_VERBOSE("account_get_custom ::  key = %s, value = %s", custom_data->key, custom_data->value);
2367
2368                 cb_ret = cb_func(custom_data->key, custom_data->value, user_data);
2369                 if(!cb_ret) {
2370                         ACCOUNT_INFO("account_get_custom_all callback func ret = %d", cb_ret);
2371                         break;
2372         }
2373         }
2374
2375         return ACCOUNT_ERROR_NONE;
2376 }
2377
2378 ACCOUNT_API int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
2379 {
2380         int                     error_code = ACCOUNT_ERROR_NONE;
2381         account_stmt    hstmt = NULL;
2382         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2383         int                     rc = 0;
2384
2385         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2386         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2387         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2388
2389         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2390
2391         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2392         hstmt = _account_prepare_query(query);
2393
2394         rc = _account_query_step(hstmt);
2395         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2396
2397         account_capability_s* capability_record = NULL;
2398
2399         while (rc == SQLITE_ROW) {
2400                 bool cb_ret = FALSE;
2401                 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2402
2403                 if (capability_record == NULL) {
2404                         ACCOUNT_FATAL("malloc Failed");
2405                         break;
2406                 }
2407
2408                 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2409
2410                 _account_convert_column_to_capability(hstmt, capability_record);
2411
2412                 cb_ret = cb_func(capability_record->type, capability_record->value, user_data);
2413
2414                 _account_free_capability_items(capability_record);
2415                 _ACCOUNT_FREE(capability_record);
2416
2417                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2418
2419                 rc = _account_query_step(hstmt);
2420         }
2421
2422         _account_query_finalize(hstmt);
2423         hstmt = NULL;
2424
2425         error_code = ACCOUNT_ERROR_NONE;
2426
2427 CATCH:
2428         if (hstmt != NULL) {
2429                 _account_query_finalize(hstmt);
2430                 hstmt = NULL;
2431         }
2432
2433         pthread_mutex_unlock(&account_mutex);
2434         return error_code;
2435 }
2436
2437 static int _account_compare_old_record(account_s *new_account, int account_id)
2438 {
2439         int                             error_code = ACCOUNT_ERROR_NONE;
2440         account_stmt    hstmt = NULL;
2441         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2442         int                             rc = 0;
2443         account_s *old_account = NULL;
2444
2445         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2446         ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2447         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2448
2449         old_account = (account_s*)calloc(1, sizeof(account_s));
2450
2451         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2452
2453         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2454         hstmt = _account_prepare_query(query);
2455
2456         rc = _account_query_step(hstmt);
2457         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2458
2459         while (rc == SQLITE_ROW) {
2460                 _account_convert_column_to_account(hstmt, old_account);
2461                 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
2462                 rc = _account_query_step(hstmt);
2463         }
2464
2465         _account_query_finalize(hstmt);
2466         hstmt = NULL;
2467
2468         // get capability
2469         error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2470         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2471
2472         // get custom text
2473         error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2474         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2475
2476         // compare
2477
2478         new_account->id = old_account->id;
2479
2480         //user name
2481         if(!new_account->user_name) {
2482                 if(old_account->user_name)
2483                         new_account->user_name = _account_get_text(old_account->user_name);
2484         }
2485
2486         // display name
2487         if(!new_account->display_name) {
2488                 if(old_account->display_name)
2489                         new_account->display_name = _account_get_text(old_account->display_name);
2490         }
2491
2492         // email address
2493         if(!new_account->email_address) {
2494                 if(old_account->email_address)
2495                         new_account->email_address = _account_get_text(old_account->email_address);
2496         }
2497
2498         // domain name
2499         if(!new_account->domain_name) {
2500                 if(old_account->domain_name)
2501                         new_account->domain_name = _account_get_text(old_account->domain_name);
2502         }
2503
2504         // icon path
2505         if(!new_account->icon_path) {
2506                 if(old_account->icon_path)
2507                         new_account->icon_path = _account_get_text(old_account->icon_path);
2508         }
2509
2510         // source
2511         if(!new_account->source) {
2512                 if(old_account->source)
2513                         new_account->source = _account_get_text(old_account->source);
2514         }
2515
2516         _ACCOUNT_FREE(new_account->package_name);
2517         new_account->package_name = _account_get_text(old_account->package_name);
2518
2519         // access token
2520         if(!new_account->access_token) {
2521                 if(old_account->access_token)
2522                         new_account->access_token = _account_get_text(old_account->access_token);
2523         }
2524
2525         // user text
2526         int i;
2527         for(i=0;i<USER_TXT_CNT;i++) {
2528                 if(!new_account->user_data_txt[i]) {
2529                         if(old_account->user_data_txt[i])
2530                                 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2531                 }
2532         }
2533
2534         // auth type
2535         if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2536                 new_account->auth_type = old_account->auth_type;
2537         }
2538
2539         //secret
2540         if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2541                 new_account->secret = old_account->secret;
2542         }
2543
2544         // sync support
2545         if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2546                 new_account->sync_support = old_account->sync_support;
2547         }
2548
2549         // user int
2550         for(i=0;i<USER_INT_CNT;i++) {
2551                 if(new_account->user_data_int[i] == 0) {
2552                                 new_account->user_data_int[i] = old_account->user_data_int[i];
2553                 }
2554         }
2555
2556         // capability
2557
2558         // user custom table
2559
2560
2561
2562         CATCH:
2563                 if (hstmt != NULL) {
2564                         _account_query_finalize(hstmt);
2565                         hstmt = NULL;
2566                 }
2567
2568                 if (old_account) {
2569                         _account_free_account_items(old_account);
2570                         _ACCOUNT_FREE(old_account);
2571                 }
2572
2573         return ACCOUNT_ERROR_NONE;
2574 }
2575
2576 static int _account_update_account(account_s *account, int account_id)
2577 {
2578         int                             rc = 0, binding_count =0;
2579         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2580         int                             error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2581         account_stmt    hstmt = NULL;
2582
2583         if (!account->package_name) {
2584                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2585                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2586         }
2587
2588         char* current_appid = NULL;
2589         char* verified_appid = NULL;
2590
2591         current_appid = _account_get_current_appid();
2592         error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
2593
2594         _ACCOUNT_FREE(current_appid);
2595         _ACCOUNT_FREE(verified_appid);
2596
2597         if(error_code != ACCOUNT_ERROR_NONE){
2598                 ACCOUNT_ERROR("No permission to update\n");
2599                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2600         }
2601
2602         _account_compare_old_record(account, account_id);
2603
2604         if (!account->user_name && !account->display_name && !account->email_address) {
2605                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2606                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2607         }
2608
2609         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2610
2611         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2612
2613         count = _account_get_record_count(query);
2614         if (count <= 0) {
2615                 ACCOUNT_WARNING(" Account record not found, count = %d\n", count);
2616                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2617         }
2618
2619         /* transaction control required*/
2620         ret_transaction = _account_begin_transaction();
2621
2622         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2623         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2624                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2625                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2626                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2627
2628         hstmt = _account_prepare_query(query);
2629         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE)));
2630
2631         binding_count = _account_convert_account_to_sql(account, hstmt, query);
2632         _account_query_bind_int(hstmt, binding_count++, account_id);
2633
2634         rc = _account_query_step(hstmt);
2635         if (rc != SQLITE_DONE) {
2636                 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2637         }
2638
2639         _account_query_finalize(hstmt);
2640         hstmt = NULL;
2641
2642         /*update capability*/
2643         error_code = _account_update_capability(account, account_id);
2644         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2645                 ret_transaction = _account_end_transaction(FALSE);
2646                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2647                 return error_code;
2648         }
2649
2650         /* update custom */
2651         error_code = _account_update_custom(account, account_id);
2652         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2653                 ret_transaction = _account_end_transaction(FALSE);
2654                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2655                 return error_code;
2656         }
2657
2658         ret_transaction = _account_end_transaction(TRUE);
2659         ACCOUNT_DEBUG("update capability success, trying to commit(%x) !!!\n", ret_transaction);
2660
2661         return error_code;
2662 }
2663
2664 ACCOUNT_API int account_update_to_db_by_id(const account_h account, int account_id)
2665 {
2666         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2667         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2668         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2669         int     error_code = ACCOUNT_ERROR_NONE;
2670         account_s* data = (account_s*)account;
2671
2672         pthread_mutex_lock(&account_mutex);
2673
2674         error_code = _account_update_account(data, account_id);
2675
2676         if(error_code != ACCOUNT_ERROR_NONE) {
2677                 pthread_mutex_unlock(&account_mutex);
2678                 return error_code;
2679         }
2680
2681         pthread_mutex_unlock(&account_mutex);
2682
2683         char buf[64]={0,};
2684         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2685         _account_insert_delete_update_notification_send(buf);
2686
2687         return ACCOUNT_ERROR_NONE;
2688 }
2689
2690 ACCOUNT_API int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
2691 {
2692         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2693         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2694         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2695         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2696
2697         int     error_code = ACCOUNT_ERROR_NONE;
2698         account_s *data = (account_s*)account;
2699
2700         pthread_mutex_lock(&account_mutex);
2701
2702         error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
2703
2704         pthread_mutex_unlock(&account_mutex);
2705
2706         char buf[64]={0,};
2707         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2708         _account_insert_delete_update_notification_send(buf);
2709
2710         return error_code;
2711 }
2712
2713 ACCOUNT_API int account_foreach_account_from_db(account_cb callback, void *user_data)
2714 {
2715         int                     error_code = ACCOUNT_ERROR_NONE;
2716         account_stmt    hstmt = NULL;
2717         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2718         int                     rc = 0;
2719         GList                   *account_list = NULL;
2720
2721         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
2722         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2723
2724         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2725
2726         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2727         hstmt = _account_prepare_query(query);
2728
2729         rc = _account_query_step(hstmt);
2730
2731         account_s *account_record = NULL;
2732
2733         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2734
2735         int k=0;
2736         while(rc == SQLITE_ROW) {
2737                 account_record = (account_s*) malloc(sizeof(account_s));
2738
2739                 if (account_record == NULL) {
2740                         ACCOUNT_FATAL("malloc Failed");
2741                         break;
2742                 }
2743
2744                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2745                 _account_convert_column_to_account(hstmt, account_record);
2746                 account_list = g_list_append(account_list, account_record);
2747                 rc = _account_query_step(hstmt);
2748                 k++;
2749         }
2750
2751         _account_query_finalize(hstmt);
2752         hstmt = NULL;
2753
2754         GList* iter;
2755         k = 0;
2756         for (iter = account_list; iter != NULL; iter = g_list_next(iter)) {
2757                 account_s *account = NULL;
2758                 account = (account_s*)iter->data;
2759                 account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2760                 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2761                 callback((account_h)account, user_data);
2762                 k++;
2763         }
2764
2765         error_code = ACCOUNT_ERROR_NONE;
2766
2767 CATCH:
2768         if (hstmt != NULL) {
2769                 _account_query_finalize(hstmt);
2770                 hstmt = NULL;
2771         }
2772         if (account_list) {
2773                 _account_glist_free(account_list);
2774                 account_list = NULL;
2775         }
2776
2777         return error_code;
2778 }
2779
2780 ACCOUNT_API int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status)
2781 {
2782         int                             error_code = ACCOUNT_ERROR_NONE;
2783         account_stmt    hstmt = NULL;
2784         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2785         int                             rc = 0;
2786         int count =1;
2787
2788         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2789         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2790         if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2791                 ACCOUNT_ERROR("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2792                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2793         }
2794
2795         pthread_mutex_lock(&account_mutex);
2796
2797         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2798
2799         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2800
2801         rc = _account_get_record_count(query);
2802
2803         if (rc <= 0) {
2804                 ACCOUNT_ERROR( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2805                 pthread_mutex_unlock(&account_mutex);
2806                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2807         }
2808
2809         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2810
2811         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2812         hstmt = _account_prepare_query(query);
2813
2814         _account_query_bind_int(hstmt, count, (int)sync_status);
2815
2816         rc = _account_query_step(hstmt);
2817
2818         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2819                                 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2820
2821         _account_query_finalize(hstmt);
2822
2823         char buf[64]={0,};
2824         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2825         _account_insert_delete_update_notification_send(buf);
2826
2827         hstmt = NULL;
2828         error_code = ACCOUNT_ERROR_NONE;
2829
2830 CATCH:
2831         if (hstmt != NULL) {
2832                 _account_query_finalize(hstmt);
2833                 hstmt = NULL;
2834         }
2835
2836         pthread_mutex_unlock(&account_mutex);
2837         return error_code;
2838 }
2839
2840 ACCOUNT_API int account_query_account_by_account_id(int account_db_id, account_h *account)
2841 {
2842         int                             error_code = ACCOUNT_ERROR_NONE;
2843         account_stmt    hstmt = NULL;
2844         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2845         int                             rc = 0;
2846
2847         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2848         ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2849         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2850
2851         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2852
2853         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2854         hstmt = _account_prepare_query(query);
2855
2856         rc = _account_query_step(hstmt);
2857         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2858
2859         account_s *account_record = (account_s *)(*account);
2860
2861         while (rc == SQLITE_ROW) {
2862                 _account_convert_column_to_account(hstmt, account_record);
2863                 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
2864                 rc = _account_query_step(hstmt);
2865         }
2866
2867         _account_query_finalize(hstmt);
2868         account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2869         _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2870
2871         hstmt = NULL;
2872         error_code = ACCOUNT_ERROR_NONE;
2873
2874 CATCH:
2875         if (hstmt != NULL) {
2876                 _account_query_finalize(hstmt);
2877                 hstmt = NULL;
2878         }
2879
2880         pthread_mutex_unlock(&account_mutex);
2881         return error_code;
2882 }
2883
2884 ACCOUNT_API int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
2885 {
2886         int                             error_code = ACCOUNT_ERROR_NONE;
2887         account_stmt    hstmt = NULL;
2888         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2889         int                             rc = 0;
2890
2891         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2892         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2893
2894         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2895
2896         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2897
2898         hstmt = _account_prepare_query(query);
2899
2900         int binding_count = 1;
2901         _account_query_bind_text(hstmt, binding_count++, user_name);
2902
2903         rc = _account_query_step(hstmt);
2904
2905         account_s *account_head = NULL;
2906
2907         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2908
2909         int tmp = 0;
2910
2911         account_head = (account_s*) malloc(sizeof(account_s));
2912         if (account_head == NULL) {
2913                 ACCOUNT_FATAL("malloc Failed");
2914                 if (hstmt != NULL) {
2915                         _account_query_finalize(hstmt);
2916                         hstmt = NULL;
2917                 }
2918                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2919         }
2920         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2921
2922         while (rc == SQLITE_ROW) {
2923                 account_s* account_record = NULL;
2924
2925                 account_record = (account_s*) malloc(sizeof(account_s));
2926
2927                 if (account_record == NULL) {
2928                         ACCOUNT_FATAL("malloc Failed");
2929                         break;
2930                 }
2931                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2932
2933                 _account_convert_column_to_account(hstmt, account_record);
2934
2935                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2936
2937                 rc = _account_query_step(hstmt);
2938                 tmp++;
2939                 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2940         }
2941
2942         _account_query_finalize(hstmt);
2943         hstmt = NULL;
2944
2945         GList *iter;
2946
2947
2948         tmp = g_list_length(account_head->account_list);
2949         ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2950
2951         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2952                 account_h account;
2953                 account = (account_h)iter->data;
2954
2955                 account_s *testaccount = (account_s*)account;
2956
2957                 ACCOUNT_VERBOSE("id = %d", testaccount->id);
2958                 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
2959                 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
2960                 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
2961                 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
2962                 ACCOUNT_VERBOSE("source = %s", testaccount->source);
2963                 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
2964                 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
2965                 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
2966                 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
2967                 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
2968                 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
2969
2970                 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2971                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2972
2973                 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2974
2975                 //cb_func(account, user_data);
2976                 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("cb_func returns false, it is stopped.\n"));
2977
2978         }
2979
2980         error_code = ACCOUNT_ERROR_NONE;
2981
2982 CATCH:
2983         if (hstmt != NULL) {
2984                 _account_query_finalize(hstmt);
2985                 hstmt = NULL;
2986         }
2987         if (account_head) {
2988                 if (account_head->account_list) {
2989                         _account_glist_free(account_head->account_list);
2990                         account_head->account_list = NULL;
2991                         _account_free_account_items(account_head);
2992                 }
2993                 _ACCOUNT_FREE(account_head);
2994         }
2995
2996         pthread_mutex_unlock(&account_mutex);
2997         return error_code;
2998 }
2999
3000 ACCOUNT_API int account_query_account_by_capability(account_cb cb_func, const char* capability_type, account_capability_state_e capability_value, void* user_data)
3001 {
3002         int                     error_code = ACCOUNT_ERROR_NONE;
3003         account_stmt    hstmt = NULL;
3004         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3005         int                     rc = 0;
3006
3007         ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3008
3009         if ((capability_value  < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
3010                 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
3011                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3012         }
3013
3014         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3015         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3016
3017         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3018
3019         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3020
3021         hstmt = _account_prepare_query(query);
3022
3023         int binding_count = 1;
3024         _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3025         _account_query_bind_int(hstmt, binding_count++, (int)capability_value);
3026
3027         rc = _account_query_step(hstmt);
3028
3029         account_s* account_head = NULL;
3030
3031         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3032
3033         int tmp = 0;
3034
3035         account_head = (account_s*) malloc(sizeof(account_s));
3036         if (account_head == NULL) {
3037                 ACCOUNT_FATAL("malloc Failed");
3038                 if (hstmt != NULL) {
3039                         _account_query_finalize(hstmt);
3040                         hstmt = NULL;
3041                 }
3042                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3043         }
3044         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3045
3046         while (rc == SQLITE_ROW) {
3047                 account_s* account_record = NULL;
3048
3049                 account_record = (account_s*) malloc(sizeof(account_s));
3050
3051                 if (account_record == NULL) {
3052                         ACCOUNT_FATAL("malloc Failed");
3053                         break;
3054                 }
3055                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3056
3057                 _account_convert_column_to_account(hstmt, account_record);
3058
3059                 account_head->account_list = g_list_append(account_head->account_list, account_record);
3060
3061                 rc = _account_query_step(hstmt);
3062                 tmp++;
3063                 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3064         }
3065
3066         _account_query_finalize(hstmt);
3067         hstmt = NULL;
3068
3069         GList *iter;
3070
3071
3072         tmp = g_list_length(account_head->account_list);
3073         ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3074
3075         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3076                 account_h account = NULL;
3077                 account = (account_h)iter->data;
3078                 account_s* testaccount = (account_s*)account;
3079
3080                 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3081                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3082
3083                 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3084
3085                 //cb_func(account, user_data);
3086                 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("cb_func returns false, it is stopped.\n"));
3087         }
3088
3089
3090         error_code = ACCOUNT_ERROR_NONE;
3091
3092 CATCH:
3093         if (hstmt != NULL) {
3094                 _account_query_finalize(hstmt);
3095                 hstmt = NULL;
3096         }
3097         if (account_head) {
3098                 if (account_head->account_list) {
3099                         _account_glist_free(account_head->account_list);
3100                         account_head->account_list = NULL;
3101                         _account_free_account_items(account_head);
3102                 }
3103                 _ACCOUNT_FREE(account_head);
3104         }
3105
3106         pthread_mutex_unlock(&account_mutex);
3107         return error_code;
3108 }
3109
3110 ACCOUNT_API int account_query_account_by_capability_type(account_cb cb_func, const char* capability_type, void* user_data)
3111 {
3112         int                     error_code = ACCOUNT_ERROR_NONE;
3113         account_stmt    hstmt = NULL;
3114         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3115         int                     rc = 0;
3116
3117         ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3118         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3119         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3120
3121         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3122
3123         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3124
3125         hstmt = _account_prepare_query(query);
3126
3127         int binding_count = 1;
3128         _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3129
3130         rc = _account_query_step(hstmt);
3131
3132         account_s* account_head = NULL;
3133
3134         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3135
3136         int tmp = 0;
3137
3138         account_head = (account_s*) malloc(sizeof(account_s));
3139         if (account_head == NULL) {
3140                 ACCOUNT_FATAL("malloc Failed");
3141                 if (hstmt != NULL) {
3142                         _account_query_finalize(hstmt);
3143                         hstmt = NULL;
3144                 }
3145                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3146         }
3147         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3148
3149         while (rc == SQLITE_ROW) {
3150                 account_s* account_record = NULL;
3151
3152                 account_record = (account_s*) malloc(sizeof(account_s));
3153
3154                 if (account_record == NULL) {
3155                         ACCOUNT_FATAL("malloc Failed");
3156                         break;
3157                 }
3158                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3159
3160                 _account_convert_column_to_account(hstmt, account_record);
3161
3162                 account_head->account_list = g_list_append(account_head->account_list, account_record);
3163
3164                 rc = _account_query_step(hstmt);
3165                 tmp++;
3166                 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3167         }
3168
3169         _account_query_finalize(hstmt);
3170         hstmt = NULL;
3171
3172         GList *iter;
3173
3174
3175         tmp = g_list_length(account_head->account_list);
3176         ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3177
3178         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3179                 account_h account = NULL;
3180                 account = (account_h)iter->data;
3181                 account_s* testaccount = (account_s*)account;
3182
3183                 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3184                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3185
3186                 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3187
3188                 //cb_func(account, user_data);
3189                 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n"));
3190
3191         }
3192
3193         error_code = ACCOUNT_ERROR_NONE;
3194
3195 CATCH:
3196         if (hstmt != NULL) {
3197                 _account_query_finalize(hstmt);
3198                 hstmt = NULL;
3199         }
3200         if (account_head) {
3201                 if (account_head->account_list) {
3202                         _account_glist_free(account_head->account_list);
3203                         account_head->account_list = NULL;
3204                         _account_free_account_items(account_head);
3205                 }
3206                 _ACCOUNT_FREE(account_head);
3207         }
3208
3209         pthread_mutex_unlock(&account_mutex);
3210         return error_code;
3211 }
3212
3213 ACCOUNT_API int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
3214 {
3215         int                     error_code = ACCOUNT_ERROR_NONE;
3216         account_stmt    hstmt = NULL;
3217         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3218         int                     rc = 0;
3219
3220         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
3221         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3222         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3223
3224         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3225
3226         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3227
3228         hstmt = _account_prepare_query(query);
3229
3230         int binding_count = 1;
3231         _account_query_bind_text(hstmt, binding_count++, package_name);
3232
3233         rc = _account_query_step(hstmt);
3234
3235         account_s* account_head = NULL;
3236
3237         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3238
3239         int tmp = 0;
3240
3241         account_head = (account_s*) malloc(sizeof(account_s));
3242         if (account_head == NULL) {
3243                 ACCOUNT_FATAL("malloc Failed");
3244                 if (hstmt != NULL) {
3245                         _account_query_finalize(hstmt);
3246                         hstmt = NULL;
3247                 }
3248                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3249         }
3250         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3251
3252         while (rc == SQLITE_ROW) {
3253                 account_s* account_record = NULL;
3254
3255                 account_record = (account_s*) malloc(sizeof(account_s));
3256
3257                 if (account_record == NULL) {
3258                         ACCOUNT_FATAL("malloc Failed");
3259                         break;
3260                 }
3261                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3262
3263                 _account_convert_column_to_account(hstmt, account_record);
3264
3265                 account_head->account_list = g_list_append(account_head->account_list, account_record);
3266
3267                 rc = _account_query_step(hstmt);
3268                 tmp++;
3269                 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3270         }
3271
3272         _account_query_finalize(hstmt);
3273         hstmt = NULL;
3274
3275         GList *iter;
3276
3277         tmp = g_list_length(account_head->account_list);
3278         ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
3279
3280         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3281                 account_h account = NULL;
3282                 account = (account_h)iter->data;
3283
3284                 account_s* testaccount = (account_s*)account;
3285
3286                 ACCOUNT_VERBOSE("id = %d", testaccount->id);
3287                 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
3288                 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
3289                 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
3290                 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
3291                 ACCOUNT_VERBOSE("source = %s", testaccount->source);
3292                 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
3293                 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
3294                 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
3295                 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
3296                 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
3297                 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
3298
3299                 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3300                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3301
3302                 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3303
3304                 //cb_func(account, user_data);
3305                 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n"));
3306
3307         }
3308
3309         error_code = ACCOUNT_ERROR_NONE;
3310
3311 CATCH:
3312         if (hstmt != NULL) {
3313                 _account_query_finalize(hstmt);
3314                 hstmt = NULL;
3315         }
3316         if (account_head) {
3317                 if (account_head->account_list) {
3318                         _account_glist_free(account_head->account_list);
3319                         account_head->account_list = NULL;
3320                         _account_free_account_items(account_head);
3321                 }
3322                 _ACCOUNT_FREE(account_head);
3323         }
3324
3325         pthread_mutex_unlock(&account_mutex);
3326         return error_code;
3327 }
3328
3329 ACCOUNT_API int account_delete(int account_id)
3330 {
3331         int                             error_code = ACCOUNT_ERROR_NONE;
3332         account_stmt    hstmt = NULL;
3333         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3334         int                             rc = 0;
3335         int                             ret_transaction = 0;
3336         bool                    is_success = FALSE;
3337
3338         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3339
3340         int count = -1;
3341         account_h               account = NULL;
3342         /* Check requested ID to delete */
3343         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3344
3345         count = _account_get_record_count(query);
3346         if (count <= 0) {
3347                 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3348                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3349         }
3350
3351         /* Check permission of requested appid */
3352         account_create(&account);
3353         account_query_account_by_account_id(account_id, &account);
3354
3355         char* current_appid = NULL;
3356         char* verified_appid = NULL;
3357
3358         current_appid = _account_get_current_appid();
3359         error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
3360
3361         _ACCOUNT_FREE(current_appid);
3362         _ACCOUNT_FREE(verified_appid);
3363
3364         if(error_code != ACCOUNT_ERROR_NONE){
3365                 ACCOUNT_ERROR("No permission to delete\n");
3366                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3367         }
3368
3369         account_destroy(account);
3370
3371         /* transaction control required*/
3372         ret_transaction = _account_begin_transaction();
3373
3374         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3375                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3376                 pthread_mutex_unlock(&account_mutex);
3377                 return ret_transaction;
3378         }
3379
3380         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3381         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3382
3383         hstmt = _account_prepare_query(query);
3384         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3385                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3386
3387         rc = _account_query_step(hstmt);
3388         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3389
3390         _account_query_finalize(hstmt);
3391         hstmt = NULL;
3392
3393         ACCOUNT_MEMSET(query, 0, sizeof(query));
3394
3395         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3396
3397         hstmt = _account_prepare_query(query);
3398         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3399                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3400
3401         rc = _account_query_step(hstmt);
3402         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3403
3404         _account_query_finalize(hstmt);
3405         hstmt = NULL;
3406
3407         /* delete custom data */
3408         ACCOUNT_MEMSET(query, 0, sizeof(query));
3409
3410         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3411
3412         hstmt = _account_prepare_query(query);
3413         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3414                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3415
3416         rc = _account_query_step(hstmt);
3417         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3418
3419         _account_query_finalize(hstmt);
3420
3421         is_success = TRUE;
3422
3423 CATCH:
3424         if (hstmt != NULL) {
3425                 _account_query_finalize(hstmt);
3426                 hstmt = NULL;
3427         }
3428
3429         ret_transaction = _account_end_transaction(is_success);
3430
3431         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3432                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3433         } else {
3434                 if (is_success == true) {
3435                         char buf[64]={0,};
3436                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3437                         _account_insert_delete_update_notification_send(buf);
3438                 }
3439         }
3440
3441         pthread_mutex_unlock(&account_mutex);
3442
3443         return error_code;
3444
3445 }
3446
3447 ACCOUNT_API int account_delete_from_db_by_id(int account_db_id)
3448 {
3449         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
3450
3451         return account_delete(account_db_id);
3452 }
3453
3454 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3455 {
3456         int                             error_code = ACCOUNT_ERROR_NONE;
3457         account_stmt    hstmt = NULL;
3458         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3459         int                             rc = 0;
3460         int                             binding_count = 1;
3461
3462         ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3463         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3464         ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3465         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3466
3467         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3468
3469         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3470         hstmt = _account_prepare_query(query);
3471
3472         _account_query_bind_text(hstmt, binding_count++, username);
3473         _account_query_bind_text(hstmt, binding_count++, package_name);
3474
3475         rc = _account_query_step(hstmt);
3476         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3477
3478         account_s *account_record = (account_s *)(*account);
3479
3480         while (rc == SQLITE_ROW) {
3481                 _account_convert_column_to_account(hstmt, account_record);
3482                 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
3483                 rc = _account_query_step(hstmt);
3484         }
3485
3486         _account_query_finalize(hstmt);
3487         account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3488         _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3489
3490         hstmt = NULL;
3491         error_code = ACCOUNT_ERROR_NONE;
3492
3493 CATCH:
3494         if (hstmt != NULL) {
3495                 _account_query_finalize(hstmt);
3496                 hstmt = NULL;
3497         }
3498
3499         pthread_mutex_unlock(&account_mutex);
3500         return error_code;
3501 }
3502
3503 ACCOUNT_API int account_delete_from_db_by_user_name(char *user_name, char *package_name)
3504 {
3505         int                     error_code = ACCOUNT_ERROR_NONE;
3506         account_stmt    hstmt = NULL;
3507         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3508         int                     rc = 0;
3509         int                     ret_transaction = 0;
3510         bool                    is_success = FALSE;
3511         account_h               account = NULL;
3512         int                     binding_count = 1;
3513         int                             account_id = -1;
3514
3515         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3516         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3517         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3518
3519         rc = account_create(&account);
3520         rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3521
3522         rc = account_get_account_id(account, &account_id);
3523
3524         rc = account_destroy(account);
3525
3526         /* transaction control required*/
3527         ret_transaction = _account_begin_transaction();
3528
3529         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3530                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3531                 pthread_mutex_unlock(&account_mutex);
3532                 return ret_transaction;
3533         }
3534
3535         /* delete custom data */
3536         ACCOUNT_MEMSET(query, 0, sizeof(query));
3537         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3538
3539         hstmt = _account_prepare_query(query);
3540
3541         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3542                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3543
3544         _account_query_bind_int(hstmt, binding_count++, account_id);
3545
3546         rc = _account_query_step(hstmt);
3547         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3548
3549         _account_query_finalize(hstmt);
3550         hstmt = NULL;
3551
3552         /* delete capability */
3553         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3554
3555         hstmt = _account_prepare_query(query);
3556
3557         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3558                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3559
3560         binding_count = 1;
3561         _account_query_bind_text(hstmt, binding_count++, user_name);
3562         _account_query_bind_text(hstmt, binding_count++, package_name);
3563
3564         rc = _account_query_step(hstmt);
3565         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3566
3567         _account_query_finalize(hstmt);
3568         hstmt = NULL;
3569
3570         ACCOUNT_MEMSET(query, 0, sizeof(query));
3571
3572         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3573
3574         hstmt = _account_prepare_query(query);
3575         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3576                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3577
3578         binding_count = 1;
3579         _account_query_bind_text(hstmt, binding_count++, user_name);
3580         _account_query_bind_text(hstmt, binding_count++, package_name);
3581
3582         rc = _account_query_step(hstmt);
3583         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. user_name=%s, package_name=%s, rc=%d\n", user_name, package_name, rc));
3584
3585         _account_query_finalize(hstmt);
3586         is_success = TRUE;
3587
3588         hstmt = NULL;
3589
3590 CATCH:
3591         if (hstmt != NULL) {
3592                 _account_query_finalize(hstmt);
3593                 hstmt = NULL;
3594         }
3595
3596         ret_transaction = _account_end_transaction(is_success);
3597
3598         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3599                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3600         } else {
3601                 if (is_success == true) {
3602                         char buf[64]={0,};
3603                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3604                         _account_insert_delete_update_notification_send(buf);
3605                 }
3606         }
3607
3608         pthread_mutex_unlock(&account_mutex);
3609
3610         return error_code;
3611 }
3612
3613 ACCOUNT_API int account_delete_from_db_by_package_name(char *package_name)
3614 {
3615         int                     error_code = ACCOUNT_ERROR_NONE;
3616         account_stmt    hstmt = NULL;
3617         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3618         int                     rc = 0;
3619         int                     ret_transaction = 0;
3620         bool                    is_success = FALSE;
3621         int                     binding_count = 1;
3622
3623         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3624         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3625
3626         /* transaction control required*/
3627         ret_transaction = _account_begin_transaction();
3628
3629         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3630                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3631                 pthread_mutex_unlock(&account_mutex);
3632                 return ret_transaction;
3633         }
3634
3635         /* delete custom table  */
3636         ACCOUNT_MEMSET(query, 0, sizeof(query));
3637         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3638
3639         hstmt = _account_prepare_query(query);
3640
3641         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3642                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3643
3644         binding_count = 1;
3645         _account_query_bind_text(hstmt, binding_count++, package_name);
3646
3647         rc = _account_query_step(hstmt);
3648         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3649
3650         _account_query_finalize(hstmt);
3651         hstmt = NULL;
3652
3653         /* delete capability table */
3654         ACCOUNT_MEMSET(query, 0, sizeof(query));
3655         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3656
3657         hstmt = _account_prepare_query(query);
3658
3659         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3660                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3661
3662         binding_count = 1;
3663         _account_query_bind_text(hstmt, binding_count++, package_name);
3664
3665         rc = _account_query_step(hstmt);
3666         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3667
3668         _account_query_finalize(hstmt);
3669         hstmt = NULL;
3670
3671         /* delete account table */
3672         ACCOUNT_MEMSET(query, 0, sizeof(query));
3673
3674         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3675
3676         hstmt = _account_prepare_query(query);
3677         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3678                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3679
3680         binding_count = 1;
3681         _account_query_bind_text(hstmt, binding_count++, package_name);
3682
3683         rc = _account_query_step(hstmt);
3684         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));
3685
3686         _account_query_finalize(hstmt);
3687         is_success = TRUE;
3688
3689         hstmt = NULL;
3690
3691 CATCH:
3692         if (hstmt != NULL) {
3693                 _account_query_finalize(hstmt);
3694                 hstmt = NULL;
3695         }
3696
3697         ret_transaction = _account_end_transaction(is_success);
3698
3699         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3700                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3701         } else {
3702                 if (is_success == true) {
3703                         char buf[64]={0,};
3704                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, -1);
3705                         _account_insert_delete_update_notification_send(buf);
3706                 }
3707         }
3708
3709         pthread_mutex_unlock(&account_mutex);
3710
3711         return error_code;
3712 }
3713
3714 ACCOUNT_API int account_get_total_count_from_db(int *count)
3715 {
3716         if (!count) {
3717                 ACCOUNT_ERROR("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3718                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3719         }
3720         char query[1024] = {0, };
3721         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3722         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3723
3724         *count = _account_get_record_count(query);
3725         int rc = -1;
3726         int ncount = 0;
3727         account_stmt pStmt = NULL;
3728
3729         assert(NULL != g_hAccountDB);
3730         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3731
3732         rc = sqlite3_step(pStmt);
3733         if (SQLITE_ROW != rc) {
3734                 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3735                 sqlite3_finalize(pStmt);
3736                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3737         }
3738
3739         ncount = sqlite3_column_int(pStmt, 0);
3740
3741         *count = ncount;
3742
3743         ACCOUNT_VERBOSE("Number of account : %d", ncount);
3744         sqlite3_finalize(pStmt);
3745
3746         if (ncount < 0) {
3747                 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3748                 return ACCOUNT_ERROR_DB_FAILED;
3749         }
3750
3751         return ACCOUNT_ERROR_NONE;
3752 }
3753
3754 ACCOUNT_API int account_destroy(account_h account)
3755 {
3756         account_s *data = (account_s*)account;
3757
3758         ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3759
3760         ACCOUNT_INFO("destroy handle=%p\n", data);
3761
3762         _account_free_account_items(data);
3763         _ACCOUNT_FREE(data);
3764
3765         return ACCOUNT_ERROR_NONE;
3766 }
3767
3768 static int _account_type_free_label_items(label_s *data)
3769 {
3770         _ACCOUNT_FREE(data->app_id);
3771         _ACCOUNT_FREE(data->label);
3772         _ACCOUNT_FREE(data->locale);
3773
3774         return ACCOUNT_ERROR_NONE;
3775 }
3776
3777 static int _account_type_free_feature_items(provider_feature_s *data)
3778 {
3779         _ACCOUNT_FREE(data->app_id);
3780         _ACCOUNT_FREE(data->key);
3781
3782         return ACCOUNT_ERROR_NONE;
3783 }
3784
3785 static int _account_type_gslist_free(GSList* list)
3786 {
3787         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
3788
3789         GSList* iter;
3790
3791         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
3792                 label_s *label_data = (label_s*)iter->data;
3793                 _account_type_free_label_items(label_data);
3794                 _ACCOUNT_FREE(label_data);
3795         }
3796
3797         g_slist_free(list);
3798         list = NULL;
3799
3800         return ACCOUNT_ERROR_NONE;
3801 }
3802
3803 static int _account_type_item_free(account_type_s *data)
3804 {
3805         _ACCOUNT_FREE(data->app_id);
3806         _ACCOUNT_FREE(data->service_provider_id);
3807         _ACCOUNT_FREE(data->icon_path);
3808         _ACCOUNT_FREE(data->small_icon_path);
3809
3810         return ACCOUNT_ERROR_NONE;
3811 }
3812
3813 static int _account_type_glist_free(GList* list)
3814 {
3815         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
3816
3817         GList* iter;
3818
3819         for (iter = list; iter != NULL; iter = g_list_next(iter)) {
3820                 account_type_s *account_type_record = (account_type_s*)iter->data;
3821                 _account_type_item_free(account_type_record);
3822                 _ACCOUNT_FREE(account_type_record);
3823         }
3824
3825         g_list_free(list);
3826         list = NULL;
3827
3828         return ACCOUNT_ERROR_NONE;
3829 }
3830
3831 static int _account_type_free_account_type_items(account_type_s *data)
3832 {
3833         _account_type_item_free(data);
3834
3835         _account_type_gslist_free(data->label_list);
3836         _account_type_glist_free(data->account_type_list);
3837
3838         return ACCOUNT_ERROR_NONE;
3839 }
3840
3841 ACCOUNT_API int account_type_create(account_type_h *account_type)
3842 {
3843         if (!account_type) {
3844                 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3845                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3846         }
3847
3848         account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3849
3850         if (data == NULL) {
3851                 ACCOUNT_ERROR("Memory Allocation Failed");
3852                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3853         }
3854
3855         ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3856
3857         ACCOUNT_VERBOSE("create handle=%p\n", *account_type);
3858
3859         *account_type = (account_type_h)data;
3860
3861         return ACCOUNT_ERROR_NONE;
3862 }
3863
3864 ACCOUNT_API int account_type_destroy(account_type_h account_type)
3865 {
3866         account_type_s *data = (account_type_s*)account_type;
3867
3868         ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
3869
3870         ACCOUNT_VERBOSE("destroy handle=%p\n", data);
3871
3872         _account_type_free_account_type_items(data);
3873         _ACCOUNT_FREE(data);
3874
3875         return ACCOUNT_ERROR_NONE;
3876 }
3877
3878 //app_id mandatory field
3879 ACCOUNT_API int account_type_set_app_id(account_type_h account_type, const char *app_id)
3880 {
3881         if (!account_type) {
3882                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3883                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3884         }
3885
3886         if (!app_id) {
3887                 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
3888                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3889         }
3890
3891         account_type_s *data = (account_type_s*)account_type;
3892
3893         _ACCOUNT_FREE(data->app_id);
3894         data->app_id = _account_get_text(app_id);
3895
3896         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->app_id, app_id);
3897         return ACCOUNT_ERROR_NONE;
3898 }
3899
3900 //service_provider_id mandatory field
3901 ACCOUNT_API int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
3902 {
3903         if (!account_type) {
3904                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3905                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3906         }
3907
3908         if (!service_provider_id) {
3909                 ACCOUNT_ERROR("(%s)-(%d) service_provider_id is NULL.\n", __FUNCTION__, __LINE__);
3910                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3911         }
3912
3913         account_type_s *data = (account_type_s*)account_type;
3914
3915         _ACCOUNT_FREE(data->service_provider_id);
3916         data->service_provider_id = _account_get_text(service_provider_id);
3917
3918         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->service_provider_id, service_provider_id);
3919         return ACCOUNT_ERROR_NONE;
3920 }
3921
3922 ACCOUNT_API int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
3923 {
3924         if (!account_type) {
3925                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3926                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3927         }
3928
3929         if (!icon_path) {
3930                 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
3931                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3932         }
3933
3934         account_type_s *data = (account_type_s*)account_type;
3935
3936         _ACCOUNT_FREE(data->icon_path);
3937         data->icon_path = _account_get_text(icon_path);
3938
3939         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
3940         return ACCOUNT_ERROR_NONE;
3941 }
3942
3943 ACCOUNT_API int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
3944 {
3945         if (!account_type) {
3946                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3947                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3948         }
3949
3950         if (!small_icon_path) {
3951                 ACCOUNT_ERROR("(%s)-(%d) small_icon_path is NULL.\n", __FUNCTION__, __LINE__);
3952                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3953         }
3954
3955         account_type_s *data = (account_type_s*)account_type;
3956
3957         _ACCOUNT_FREE(data->small_icon_path);
3958         data->small_icon_path = _account_get_text(small_icon_path);
3959
3960         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->small_icon_path, small_icon_path);
3961         return ACCOUNT_ERROR_NONE;
3962 }
3963
3964 ACCOUNT_API int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
3965 {
3966         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",  __FUNCTION__, __LINE__));
3967
3968         account_type_s *data = (account_type_s*)account_type;
3969
3970         data->multiple_account_support = multiple_account_support;
3971
3972         return ACCOUNT_ERROR_NONE;
3973 }
3974
3975 // unset?
3976 ACCOUNT_API int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
3977 {
3978         if (!account_type) {
3979                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3980                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3981         }
3982
3983         if(!label || !locale) {
3984                 ACCOUNT_ERROR("(%s)-(%d) label(%p) or locale(%p) is NULL.\n", __FUNCTION__, __LINE__, label, locale);
3985                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3986         }
3987
3988         account_type_s *data = (account_type_s*)account_type;
3989         label_s *label_data = (label_s*)malloc(sizeof(label_s));
3990
3991         if (label_data == NULL) {
3992                 ACCOUNT_FATAL("(%s)-(%d) Malloc fail. label_data is NULL.\n", __FUNCTION__, __LINE__);
3993                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3994         }
3995         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3996
3997         label_data->label = _account_get_text(label);
3998         label_data->locale = _account_get_text(locale);
3999
4000         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4001
4002         return ACCOUNT_ERROR_NONE;
4003 }
4004
4005 ACCOUNT_API int account_type_get_app_id(account_type_h account_type, char **app_id)
4006 {
4007         if (!account_type) {
4008                 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4009                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4010         }
4011
4012         if (!app_id) {
4013                 ACCOUNT_ERROR("(%s)-(%d) app id is NULL.\n", __FUNCTION__, __LINE__);
4014                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4015         }
4016
4017         account_type_s *data = (account_type_s*)account_type;
4018
4019         (*app_id) = NULL;
4020         *app_id = _account_get_text(data->app_id);
4021
4022         return ACCOUNT_ERROR_NONE;
4023 }
4024
4025 ACCOUNT_API int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4026 {
4027         if (!account_type) {
4028                 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4029                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4030         }
4031
4032         if (!service_provider_id) {
4033                 ACCOUNT_ERROR("(%s)-(%d) service provider id is NULL.\n", __FUNCTION__, __LINE__);
4034                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4035         }
4036
4037         account_type_s *data = (account_type_s*)account_type;
4038
4039         (*service_provider_id) = NULL;
4040         *service_provider_id = _account_get_text(data->service_provider_id);
4041
4042         return ACCOUNT_ERROR_NONE;
4043 }
4044
4045 ACCOUNT_API int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4046 {
4047         if (!account_type) {
4048                 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4049                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4050         }
4051
4052         if (!icon_path) {
4053                 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4054                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4055         }
4056
4057         account_type_s *data = (account_type_s*)account_type;
4058
4059         (*icon_path) = NULL;
4060         *icon_path = _account_get_text(data->icon_path);
4061
4062         return ACCOUNT_ERROR_NONE;
4063 }
4064
4065 ACCOUNT_API int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4066 {
4067         if (!account_type) {
4068                 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4069                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4070         }
4071
4072         if (!small_icon_path) {
4073                 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4074                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4075         }
4076
4077         account_type_s *data = (account_type_s*)account_type;
4078
4079         (*small_icon_path) = NULL;
4080         *small_icon_path = _account_get_text(data->small_icon_path);
4081
4082         return ACCOUNT_ERROR_NONE;
4083 }
4084
4085 ACCOUNT_API int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4086 {
4087         if (!account_type) {
4088                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4089                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4090         }
4091         if (!multiple_account_support) {
4092                 ACCOUNT_ERROR("(%s)-(%d) multiple_account_support is NULL.\n", __FUNCTION__, __LINE__);
4093                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4094         }
4095
4096         account_type_s *data = (account_type_s*)account_type;
4097
4098         *multiple_account_support = data->multiple_account_support;
4099
4100         return ACCOUNT_ERROR_NONE;
4101 }
4102
4103 ACCOUNT_API int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4104 {
4105         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4106         ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4107
4108         GSList *iter;
4109         account_type_s *data = (account_type_s*)account_type;
4110
4111         for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4112                 label_s *label_data = NULL;
4113
4114                 label_data = (label_s*)iter->data;
4115
4116                 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4117
4118                 *label = NULL;
4119
4120                 if(!strcmp(locale, label_data->locale)) {
4121                         *label = _account_get_text(label_data->label);
4122                         return ACCOUNT_ERROR_NONE;
4123                 }
4124         }
4125
4126         return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4127 }
4128
4129 ACCOUNT_API int account_type_get_label(account_type_h account_type, account_label_cb cb_func, void *user_data)
4130 {
4131         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4132         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4133
4134         GSList *iter;
4135         account_type_s *data = (account_type_s*)account_type;
4136
4137         for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4138                 label_s *label_data = NULL;
4139
4140                 label_data = (label_s*)iter->data;
4141
4142                 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4143
4144                 //cb_func(label_data->app_id, label_data->label, label_data->locale, user_data);
4145                 if(cb_func(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4146                         ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4147                         return ACCOUNT_ERROR_NONE;
4148                 }
4149         }
4150
4151         return ACCOUNT_ERROR_NONE;
4152 }
4153
4154 static gboolean _account_type_check_duplicated(account_type_s *data)
4155 {
4156         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4157         int count = 0;
4158
4159         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4160
4161         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4162                         , ACCOUNT_TYPE_TABLE, data->app_id);
4163
4164         count = _account_get_record_count(query);
4165         if (count > 0) {
4166                 ACCOUNT_VERBOSE("_account_type_check_duplicated : duplicated %d account type(s) exist!, app_id=%s, service_provider_id=%s\n",
4167                         count, data->app_id, data->service_provider_id);
4168                 return TRUE;
4169         }
4170
4171         return FALSE;
4172 }
4173
4174 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4175 {
4176         int count = 1;
4177
4178         /*Caution : Keep insert query orders.*/
4179
4180         /* 1. app id*/
4181         _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4182
4183         /* 2. service provider id*/
4184         _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4185
4186         /* 3. icon path*/
4187         _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4188
4189         /* 4. small icon path*/
4190         _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4191
4192         /* 5. multiple accont support*/
4193         _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4194
4195         return count;
4196 }
4197
4198
4199 static int _account_type_execute_insert_query(account_type_s *account_type)
4200 {
4201         int                             rc = 0;
4202         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4203         int                             error_code = ACCOUNT_ERROR_NONE;
4204         account_stmt    hstmt = NULL;
4205
4206         /* check mandatory field */
4207         // app id & service provider id
4208         if (!account_type->app_id) {
4209                 ACCOUNT_ERROR("App id is mandetory field, it can not be NULL!!!!\n");
4210                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4211         }
4212
4213         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4214         ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4215                         "(?, ?, ?, ?, ?)",      ACCOUNT_TYPE_TABLE);
4216
4217         hstmt = _account_prepare_query(query);
4218         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4219
4220         _account_type_convert_account_to_sql(account_type, hstmt, query);
4221
4222         rc = _account_query_step(hstmt);
4223         if (rc != SQLITE_DONE) {
4224                 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4225                 error_code = ACCOUNT_ERROR_DB_FAILED;
4226         }
4227
4228         _account_query_finalize(hstmt);
4229         hstmt = NULL;
4230
4231         return error_code;
4232 }
4233
4234 static int _account_type_insert_label(account_type_s *account_type)
4235 {
4236         int                     rc, count = 1;
4237         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4238         account_stmt    hstmt = NULL;
4239
4240         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4241
4242         if (g_slist_length( account_type->label_list)==0) {
4243                 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4244                 return ACCOUNT_ERROR_NONE;
4245         }
4246
4247         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4248
4249         rc = _account_get_record_count(query);
4250
4251         if (rc <= 0) {
4252                 ACCOUNT_ERROR( "_account_insert_label : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
4253                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4254         }
4255
4256         /* insert query*/
4257         GSList *iter;
4258
4259         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4260                 int rc, ret;
4261                 count = 1;
4262                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4263                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4264                                 "(?, ?, ?) ", LABEL_TABLE);
4265
4266                 hstmt = _account_prepare_query(query);
4267
4268                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4269
4270                 label_s* label_data = NULL;
4271                 label_data = (label_s*)iter->data;
4272                 ACCOUNT_VERBOSE( "label_data->appid = %s, label_data->label = %s, label_data->locale \n", label_data->app_id, label_data->label, label_data->locale);
4273
4274                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4275                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4276                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4277                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4278                 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4279                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4280
4281                 rc = _account_query_step(hstmt);
4282
4283                 if (rc != SQLITE_DONE) {
4284                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4285                         break;
4286                 }
4287
4288                 _account_query_finalize(hstmt);
4289                 hstmt = NULL;
4290
4291         }
4292
4293         ACCOUNT_VERBOSE( "_account_type_insert_label() DONE\n");
4294
4295         return ACCOUNT_ERROR_NONE;
4296 }
4297
4298 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4299 {
4300         char *textbuf = NULL;
4301
4302         textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4303         _account_db_data_to_text(textbuf, &(feature_record->app_id));
4304
4305         textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4306         _account_db_data_to_text(textbuf, &(feature_record->key));
4307
4308         ACCOUNT_VERBOSE("END _account_type_convert_column_to_provider_feature");
4309 }
4310
4311 ACCOUNT_API int account_type_query_provider_feature_by_app_id(provider_feature_cb cb_func, const char* app_id, void *user_data )
4312 {
4313         int                     error_code = ACCOUNT_ERROR_NONE;
4314         account_stmt    hstmt = NULL;
4315         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4316         int                     rc = 0, binding_count = 1;
4317
4318         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4319         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4320         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4321
4322         ACCOUNT_VERBOSE("app_id = %s", app_id);
4323
4324         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4325
4326         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4327         hstmt = _account_prepare_query(query);
4328
4329         _account_query_bind_text(hstmt, binding_count++, app_id);
4330
4331         rc = _account_query_step(hstmt);
4332         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4333
4334         provider_feature_s* feature_record = NULL;
4335
4336         while (rc == SQLITE_ROW) {
4337                 bool cb_ret = FALSE;
4338                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4339
4340                 if (feature_record == NULL) {
4341                         ACCOUNT_FATAL("malloc Failed");
4342                         break;
4343                 }
4344
4345                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4346
4347                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4348
4349                 cb_ret = cb_func(feature_record->app_id, feature_record->key, user_data);
4350
4351                 _account_type_free_feature_items(feature_record);
4352                 _ACCOUNT_FREE(feature_record);
4353
4354                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4355
4356                 rc = _account_query_step(hstmt);
4357         }
4358
4359         _account_query_finalize(hstmt);
4360         hstmt = NULL;
4361
4362         error_code = ACCOUNT_ERROR_NONE;
4363
4364 CATCH:
4365         if (hstmt != NULL) {
4366                 _account_query_finalize(hstmt);
4367                 hstmt = NULL;
4368         }
4369
4370         pthread_mutex_unlock(&account_mutex);
4371         return error_code;
4372 }
4373
4374 ACCOUNT_API bool account_type_query_supported_feature(const char* app_id, const char* capability)
4375 {
4376         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4377         int                     record_count = 0;
4378
4379         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4380         ACCOUNT_RETURN_VAL((capability != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CAPABILITY"));
4381
4382         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4383
4384         record_count = _account_get_record_count(query);
4385
4386         if (record_count <= 0) {
4387                 ACCOUNT_WARNING( "related capability type item is not existed rc=%d , %s", record_count, _account_db_err_msg());
4388                 return FALSE;
4389         }
4390
4391         return TRUE;
4392
4393 }
4394
4395
4396 ACCOUNT_API int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb cb_func, void* user_data)
4397 {
4398         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4399         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4400
4401         GSList *iter;
4402         account_type_s *data = (account_type_s*)account_type;
4403
4404         for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4405                 provider_feature_s *feature_data = NULL;
4406
4407                 feature_data = (provider_feature_s*)iter->data;
4408
4409                 ACCOUNT_VERBOSE("appid = %s, key = %s", feature_data->key, feature_data->app_id);
4410
4411                 //cb_func(feature_data->app_id, feature_data->key, user_data);
4412                 if(cb_func(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
4413                         ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4414                         return ACCOUNT_ERROR_NONE;
4415                 }
4416         }
4417
4418         return ACCOUNT_ERROR_NONE;
4419 }
4420
4421 ACCOUNT_API int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4422 {
4423         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4424         ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4425
4426         account_type_s *data = (account_type_s*)account_type;
4427
4428         GSList *iter = NULL;
4429         bool b_is_new = TRUE;
4430
4431         for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4432                 provider_feature_s *feature_data = NULL;
4433                 feature_data = (provider_feature_s*)iter->data;
4434
4435                 if(!strcmp(feature_data->key, provider_feature)) {
4436                         b_is_new = FALSE;
4437                         break;
4438                 }
4439         }
4440
4441         if(b_is_new) {
4442                 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4443
4444                 if (feature_data == NULL)
4445                         return ACCOUNT_ERROR_OUT_OF_MEMORY;
4446                 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4447
4448                 feature_data->key = _account_get_text(provider_feature);
4449                 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4450         }
4451
4452         return ACCOUNT_ERROR_NONE;
4453 }
4454
4455 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4456 {
4457         int                     rc, count = 1;
4458         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4459         account_stmt    hstmt = NULL;
4460
4461         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4462         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4463
4464         if (g_slist_length( account_type->provider_feature_list)==0) {
4465                 ACCOUNT_ERROR( "no capability\n");
4466                 return ACCOUNT_ERROR_NONE;
4467         }
4468
4469         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4470
4471         rc = _account_get_record_count(query);
4472
4473         if (rc <= 0) {
4474                 ACCOUNT_WARNING( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4475                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4476         }
4477
4478         /* insert query*/
4479
4480         GSList *iter;
4481
4482         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4483                 int rc, ret;
4484                 count = 1;
4485                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4486                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4487                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4488
4489                 hstmt = _account_prepare_query(query);
4490
4491                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4492
4493                 provider_feature_s* feature_data = NULL;
4494                 feature_data = (provider_feature_s*)iter->data;
4495                 ACCOUNT_VERBOSE("key = %s, app_id = %s \n", feature_data->key, app_id);
4496
4497                 ret = _account_query_bind_text(hstmt, count++, app_id);
4498                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4499                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4500                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4501
4502                 rc = _account_query_step(hstmt);
4503
4504                 if (rc != SQLITE_DONE) {
4505                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4506                         break;
4507                 }
4508
4509                 _account_query_finalize(hstmt);
4510                 hstmt = NULL;
4511
4512         }
4513
4514         ACCOUNT_VERBOSE( "_account_type_insert_provider_feature() DONE\n");
4515
4516         return ACCOUNT_ERROR_NONE;
4517 }
4518
4519 ACCOUNT_API int account_type_insert_to_db(account_type_h account_type, int* account_type_id)
4520 {
4521         int             error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4522
4523         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4524         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4525         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4526
4527         account_type_s *data = (account_type_s*)account_type;
4528
4529         pthread_mutex_lock(&account_mutex);
4530
4531
4532         /* transaction control required*/
4533         ret_transaction = _account_begin_transaction();
4534
4535         if (ret_transaction != ACCOUNT_ERROR_NONE) {
4536                 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4537                 pthread_mutex_unlock(&account_mutex);
4538                 return ret_transaction;
4539         }
4540
4541         if (_account_type_check_duplicated(data)) {
4542                 ret_transaction = _account_end_transaction(FALSE);
4543                 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4544                 *account_type_id = -1;
4545                 pthread_mutex_unlock(&account_mutex);
4546                 return ACCOUNT_ERROR_DUPLICATED;
4547         } else {
4548                 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4549
4550                 error_code = _account_type_execute_insert_query(data);
4551
4552                 if (error_code != ACCOUNT_ERROR_NONE){
4553                         error_code = ACCOUNT_ERROR_DUPLICATED;
4554                         ret_transaction = _account_end_transaction(FALSE);
4555                         ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4556                         *account_type_id = -1;
4557                         pthread_mutex_unlock(&account_mutex);
4558                         return error_code;
4559                 }
4560         }
4561
4562         ACCOUNT_INFO( "_account_type_execute_insert_query, insert error_code : %d", error_code);
4563
4564         error_code = _account_type_insert_provider_feature(data, data->app_id);
4565         if(error_code != ACCOUNT_ERROR_NONE) {
4566                 ret_transaction = _account_end_transaction(FALSE);
4567                 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4568                 pthread_mutex_unlock(&account_mutex);
4569                 return error_code;
4570         }
4571         error_code = _account_type_insert_label(data);
4572         if(error_code != ACCOUNT_ERROR_NONE) {
4573                 ret_transaction = _account_end_transaction(FALSE);
4574                 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4575                 pthread_mutex_unlock(&account_mutex);
4576                 return error_code;
4577         }
4578
4579         ret_transaction = _account_end_transaction(TRUE);
4580         ACCOUNT_DEBUG("Insert success(%x), commit insert query(%x)!!!!\n", error_code, ret_transaction);
4581
4582         pthread_mutex_unlock(&account_mutex);
4583
4584         return ACCOUNT_ERROR_NONE;
4585 }
4586
4587 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4588 {
4589         int                     rc, count = 1;
4590         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4591         account_stmt    hstmt = NULL;
4592
4593         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4594
4595         if (g_slist_length( account_type->provider_feature_list)==0) {
4596                 ACCOUNT_ERROR( "no feature\n");
4597                 return ACCOUNT_ERROR_NONE;
4598         }
4599
4600         ACCOUNT_DEBUG( "app id", app_id);
4601
4602         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4603
4604         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4605         hstmt = _account_prepare_query(query);
4606         count = 1;
4607         _account_query_bind_text(hstmt, count++, app_id);
4608         rc = _account_query_step(hstmt);
4609
4610         if (rc != SQLITE_DONE) {
4611                 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4612                 return ACCOUNT_ERROR_DB_FAILED;
4613         }
4614         _account_query_finalize(hstmt);
4615         hstmt = NULL;
4616
4617         GSList *iter;
4618
4619         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4620                 int rc, ret;
4621                 count = 1;
4622                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4623                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4624                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4625
4626                 hstmt = _account_prepare_query(query);
4627
4628                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4629
4630                 provider_feature_s* feature_data = NULL;
4631                 feature_data = (provider_feature_s*)iter->data;
4632
4633                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4634                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4635                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4636                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4637
4638                 rc = _account_query_step(hstmt);
4639
4640                 if (rc != SQLITE_DONE) {
4641                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4642                         break;
4643                 }
4644                 _account_query_finalize(hstmt);
4645                 hstmt = NULL;
4646         }
4647
4648         ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4649
4650         return ACCOUNT_ERROR_NONE;
4651 }
4652
4653 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4654 {
4655         int                     rc, count = 1;
4656         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4657         account_stmt    hstmt = NULL;
4658
4659         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4660
4661         if (g_slist_length( account_type->label_list)==0) {
4662                 ACCOUNT_ERROR( "_account_type_update_label, no label\n");
4663                 return ACCOUNT_ERROR_NONE;
4664         }
4665
4666         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4667
4668         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4669         hstmt = _account_prepare_query(query);
4670         count = 1;
4671         _account_query_bind_text(hstmt, count++, app_id);
4672         rc = _account_query_step(hstmt);
4673
4674         if (rc != SQLITE_DONE) {
4675                 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4676                 return ACCOUNT_ERROR_DB_FAILED;
4677         }
4678         _account_query_finalize(hstmt);
4679         hstmt = NULL;
4680
4681         GSList *iter;
4682
4683         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4684                 int rc, ret;
4685                 count = 1;
4686                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4687                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4688                                 "(?, ?, ?) ", LABEL_TABLE);
4689
4690                 hstmt = _account_prepare_query(query);
4691
4692                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4693
4694                 label_s* label_data = NULL;
4695                 label_data = (label_s*)iter->data;
4696
4697                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4698                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4699                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4700                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4701                 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
4702                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4703
4704                 rc = _account_query_step(hstmt);
4705
4706                 if (rc != SQLITE_DONE) {
4707                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4708                         break;
4709                 }
4710                 _account_query_finalize(hstmt);
4711                 hstmt = NULL;
4712         }
4713
4714         ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4715
4716         return ACCOUNT_ERROR_NONE;
4717 }
4718
4719
4720 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
4721 {
4722         int                             rc = 0, binding_count =1;
4723         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4724         int                             error_code = ACCOUNT_ERROR_NONE;
4725         account_stmt    hstmt = NULL;
4726
4727         if (!account_type->app_id) {
4728                 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
4729                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4730         }
4731
4732         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4733         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
4734                         "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
4735
4736         hstmt = _account_prepare_query(query);
4737         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
4738
4739         binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
4740         _account_query_bind_text(hstmt, binding_count++, app_id);
4741
4742         rc = _account_query_step(hstmt);
4743         if (rc != SQLITE_DONE) {
4744                 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4745         }
4746
4747         _account_query_finalize(hstmt);
4748         hstmt = NULL;
4749
4750         /*update label*/
4751         error_code = _account_type_update_label(account_type, app_id);
4752         /* update provider feature */
4753         error_code = _account_type_update_provider_feature(account_type, app_id);
4754
4755         return error_code;
4756 }
4757
4758 ACCOUNT_API int account_type_update_to_db_by_app_id(account_type_h account_type, const char* app_id)
4759 {
4760         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
4761         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4762         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4763
4764         int     error_code = ACCOUNT_ERROR_NONE;
4765         account_type_s* data = (account_type_s*)account_type;
4766
4767         pthread_mutex_lock(&account_mutex);
4768
4769         error_code = _account_type_update_account(data, app_id);
4770
4771         pthread_mutex_unlock(&account_mutex);
4772
4773         return error_code;
4774 }
4775
4776 ACCOUNT_API int account_type_delete_by_app_id(const char* app_id)
4777 {
4778         int                     error_code = ACCOUNT_ERROR_NONE;
4779         account_stmt    hstmt = NULL;
4780         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4781         int                     rc = 0, count = -1;
4782         int                     ret_transaction = 0;
4783         int                             binding_count = 1;
4784         bool                    is_success = FALSE;
4785
4786         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4787         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
4788
4789         /* Check requested ID to delete */
4790         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
4791
4792         count = _account_get_record_count(query);
4793         if (count <= 0) {
4794                 ACCOUNT_ERROR("app id(%s) is not exist. count(%d)\n", app_id, count);
4795                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4796         }
4797
4798         /* transaction control required*/
4799         ret_transaction = _account_begin_transaction();
4800
4801         if (ret_transaction != ACCOUNT_ERROR_NONE) {
4802                 ACCOUNT_FATAL("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4803                 pthread_mutex_unlock(&account_mutex);
4804                 return ret_transaction;
4805         }
4806
4807         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
4808
4809         hstmt = _account_prepare_query(query);
4810         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4811                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4812
4813         _account_query_bind_text(hstmt, binding_count++, app_id);
4814
4815         rc = _account_query_step(hstmt);
4816         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4817
4818         _account_query_finalize(hstmt);
4819         hstmt = NULL;
4820
4821         binding_count = 1;
4822         ACCOUNT_MEMSET(query, 0, sizeof(query));
4823
4824         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
4825
4826         hstmt = _account_prepare_query(query);
4827         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4828                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4829
4830         _account_query_bind_text(hstmt, binding_count++, app_id);
4831
4832         rc = _account_query_step(hstmt);
4833         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4834
4835         _account_query_finalize(hstmt);
4836         is_success = TRUE;
4837
4838         hstmt = NULL;
4839
4840         binding_count = 1;
4841         ACCOUNT_MEMSET(query, 0, sizeof(query));
4842
4843         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
4844
4845         hstmt = _account_prepare_query(query);
4846         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4847                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4848
4849         _account_query_bind_text(hstmt, binding_count++, app_id);
4850
4851         rc = _account_query_step(hstmt);
4852         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4853
4854         _account_query_finalize(hstmt);
4855         is_success = TRUE;
4856
4857         hstmt = NULL;
4858
4859         CATCH:
4860         if (hstmt != NULL) {
4861                 _account_query_finalize(hstmt);
4862                 hstmt = NULL;
4863         }
4864
4865         ret_transaction = _account_end_transaction(is_success);
4866
4867         if (ret_transaction != ACCOUNT_ERROR_NONE) {
4868                 ACCOUNT_FATAL("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4869         }
4870
4871         pthread_mutex_unlock(&account_mutex);
4872
4873         return error_code;
4874 }
4875
4876 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
4877 {
4878         char    *textbuf = NULL;
4879
4880         account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
4881
4882         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
4883         _account_db_data_to_text(textbuf, &(account_type_record->app_id));
4884
4885         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
4886         _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
4887
4888         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
4889         _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
4890
4891         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
4892         _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
4893
4894         account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
4895
4896         ACCOUNT_VERBOSE("END _account_type_convert_column_to_account_type");
4897 }
4898
4899 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
4900 {
4901         char *textbuf = NULL;
4902
4903         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
4904         _account_db_data_to_text(textbuf, &(label_record->app_id));
4905
4906         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
4907         _account_db_data_to_text(textbuf, &(label_record->label));
4908
4909         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
4910         _account_db_data_to_text(textbuf, &(label_record->locale));
4911
4912         ACCOUNT_VERBOSE("END _account_type_convert_column_to_label");
4913 }
4914
4915 ACCOUNT_API int account_type_query_label_by_app_id(account_label_cb cb_func, const char* app_id, void *user_data )
4916 {
4917         int                     error_code = ACCOUNT_ERROR_NONE;
4918         account_stmt    hstmt = NULL;
4919         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4920         int                     rc = 0, binding_count = 1;
4921
4922         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4923         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4924         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4925
4926         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4927
4928         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
4929         hstmt = _account_prepare_query(query);
4930
4931         _account_query_bind_text(hstmt, binding_count++, app_id);
4932
4933         rc = _account_query_step(hstmt);
4934         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4935
4936         label_s* label_record = NULL;
4937
4938         while (rc == SQLITE_ROW) {
4939                 bool cb_ret = FALSE;
4940                 label_record = (label_s*) malloc(sizeof(label_s));
4941
4942                 if (label_record == NULL) {
4943                         ACCOUNT_FATAL("malloc Failed");
4944                         break;
4945                 }
4946
4947                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4948
4949                 _account_type_convert_column_to_label(hstmt, label_record);
4950
4951                 cb_ret = cb_func(label_record->app_id, label_record->label , label_record->locale, user_data);
4952
4953                 _account_type_free_label_items(label_record);
4954                 _ACCOUNT_FREE(label_record);
4955
4956                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4957
4958                 rc = _account_query_step(hstmt);
4959         }
4960
4961         _account_query_finalize(hstmt);
4962         hstmt = NULL;
4963
4964         error_code = ACCOUNT_ERROR_NONE;
4965
4966 CATCH:
4967         if (hstmt != NULL) {
4968                 _account_query_finalize(hstmt);
4969                 hstmt = NULL;
4970         }
4971
4972         pthread_mutex_unlock(&account_mutex);
4973         return error_code;
4974 }
4975
4976 int _account_type_label_get_app_id(label_h label, char **app_id)
4977 {
4978         if (!label) {
4979                 ACCOUNT_ERROR("(%s)-(%d) label handle is NULL.\n", __FUNCTION__, __LINE__);
4980                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4981         }
4982
4983         if (!app_id) {
4984                 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4985                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4986         }
4987
4988         label_s *data = (label_s*)label;
4989
4990         (*app_id) = NULL;
4991
4992         *app_id = _account_get_text(data->app_id);
4993
4994         return ACCOUNT_ERROR_NONE;
4995 }
4996
4997 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
4998 {
4999         account_type_s *data = (account_type_s*)user_data;
5000
5001         label_s *label_data = (label_s*)malloc(sizeof(label_s));
5002
5003         if (label_data == NULL) {
5004                 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
5005                 return FALSE;
5006         }
5007         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
5008
5009         label_data->app_id = _account_get_text(app_id);
5010         label_data->label = _account_get_text(label);
5011         label_data->locale = _account_get_text(locale);
5012
5013         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5014
5015         ACCOUNT_VERBOSE("_account_get_label_text_cb :: appid=%s, label=%s\n", label_data->app_id, label_data->label);
5016
5017         return TRUE;
5018 }
5019
5020 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5021 {
5022         account_type_s *data = (account_type_s*)user_data;
5023
5024         provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5025
5026         if (feature_data == NULL) {
5027                 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5028                 return FALSE;
5029         }
5030         ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5031
5032         feature_data->app_id = _account_get_text(app_id);
5033         feature_data->key = _account_get_text(key);
5034
5035         data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5036
5037         ACCOUNT_VERBOSE("appid=%s, key=%s\n", feature_data->app_id, feature_data->key);
5038
5039         return TRUE;
5040 }
5041
5042 ACCOUNT_API int account_type_query_by_app_id(const char* app_id, account_type_h *account_type)
5043 {
5044         int                     error_code = ACCOUNT_ERROR_NONE;
5045         account_stmt    hstmt = NULL;
5046         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5047         int                     rc = 0, binding_count = 1;
5048
5049         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5050         ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL"));
5051         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5052
5053         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5054
5055         ACCOUNT_DEBUG("app id (%s)\n", app_id);
5056         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5057         hstmt = _account_prepare_query(query);
5058
5059         _account_query_bind_text(hstmt, binding_count++, app_id);
5060
5061         rc = _account_query_step(hstmt);
5062         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5063
5064         account_type_s *account_type_record = (account_type_s *)(*account_type);
5065
5066         while (rc == SQLITE_ROW) {
5067                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5068                 ACCOUNT_DEBUG("get account info by id %p\n", account_type_record);
5069                 rc = _account_query_step(hstmt);
5070         }
5071
5072         _account_query_finalize(hstmt);
5073         account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)account_type_record);
5074         account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)account_type_record);
5075
5076         hstmt = NULL;
5077         error_code = ACCOUNT_ERROR_NONE;
5078
5079         CATCH:
5080         if (hstmt != NULL) {
5081                 _account_query_finalize(hstmt);
5082                 hstmt = NULL;
5083         }
5084
5085         pthread_mutex_unlock(&account_mutex);
5086         return error_code;
5087 }
5088
5089 ACCOUNT_API int account_type_query_app_id_exist(const char* app_id)
5090 {
5091         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5092         int                     rc = 0;
5093
5094         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5095         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5096
5097         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5098
5099         ACCOUNT_DEBUG("app id (%s)\n", app_id);
5100
5101         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5102         rc = _account_get_record_count(query);
5103
5104         if (rc <= 0) {
5105                 ACCOUNT_DEBUG("(%s) not exist in account type db");
5106                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5107         }
5108
5109         return ACCOUNT_ERROR_NONE;
5110 }
5111
5112 ACCOUNT_API int account_type_query_by_provider_feature(account_type_cb cb_func, const char* key, void* user_data)
5113 {
5114         int                     error_code = ACCOUNT_ERROR_NONE;
5115         account_stmt    hstmt = NULL;
5116         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5117         int                     rc = 0;
5118         GList                   *account_type_list = NULL;
5119
5120         ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
5121         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
5122         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5123
5124         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5125
5126         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5127
5128         hstmt = _account_prepare_query(query);
5129
5130         int binding_count = 1;
5131         _account_query_bind_text(hstmt, binding_count++, (char*)key);
5132
5133         rc = _account_query_step(hstmt);
5134
5135         account_type_s *account_type_record = NULL;
5136
5137         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5138
5139         int k=0;
5140         while(rc == SQLITE_ROW) {
5141                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5142
5143                 if (account_type_record == NULL) {
5144                         ACCOUNT_FATAL("malloc Failed");
5145                         break;
5146                 }
5147
5148                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5149                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5150                 account_type_list = g_list_append(account_type_list, account_type_record);
5151                 rc = _account_query_step(hstmt);
5152                 k++;
5153         }
5154
5155         _account_query_finalize(hstmt);
5156         hstmt = NULL;
5157
5158         GList* iter;
5159         k = 0;
5160         for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5161                 account_type_s *account_type = NULL;
5162                 account_type = (account_type_s*)iter->data;
5163                 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5164                 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5165                 //cb_func((account_type_h)account_type, user_data);
5166                 ACCOUNT_CATCH_ERROR(cb_func((account_type_h)account_type, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5167                 k++;
5168         }
5169
5170         error_code = ACCOUNT_ERROR_NONE;
5171
5172 CATCH:
5173         if (hstmt != NULL) {
5174                 _account_query_finalize(hstmt);
5175                 hstmt = NULL;
5176         }
5177         if (account_type_list) {
5178                 _account_type_glist_free(account_type_list);
5179                 account_type_list = NULL;
5180         }
5181
5182         return error_code;
5183 }
5184
5185
5186 ACCOUNT_API int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data)
5187 {
5188         int                     error_code = ACCOUNT_ERROR_NONE;
5189         account_stmt    hstmt = NULL;
5190         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5191         int                     rc = 0;
5192         GList                   *account_type_list = NULL;
5193
5194         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
5195         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5196
5197         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5198
5199         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5200         hstmt = _account_prepare_query(query);
5201
5202         rc = _account_query_step(hstmt);
5203
5204         account_type_s *account_type_record = NULL;
5205
5206         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5207
5208         int k=0;
5209         while(rc == SQLITE_ROW) {
5210                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5211
5212                 if (account_type_record == NULL) {
5213                         ACCOUNT_FATAL("malloc Failed");
5214                         break;
5215                 }
5216
5217                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5218                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5219                 account_type_list = g_list_append(account_type_list, account_type_record);
5220                 rc = _account_query_step(hstmt);
5221                 k++;
5222         }
5223
5224         _account_query_finalize(hstmt);
5225         hstmt = NULL;
5226
5227         GList* iter;
5228         k = 0;
5229         for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5230                 account_type_s *account_type = NULL;
5231                 account_type = (account_type_s*)iter->data;
5232                 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5233                 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5234                 callback((account_type_h)account_type, user_data);
5235                 k++;
5236         }
5237
5238         error_code = ACCOUNT_ERROR_NONE;
5239
5240 CATCH:
5241         if (hstmt != NULL) {
5242                 _account_query_finalize(hstmt);
5243                 hstmt = NULL;
5244         }
5245         if (account_type_list) {
5246                 _account_type_glist_free(account_type_list);
5247                 account_type_list = NULL;
5248         }
5249
5250         return error_code;
5251 }
5252
5253 // output parameter label must be free
5254 ACCOUNT_API int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label)
5255 {
5256         int                     error_code = ACCOUNT_ERROR_NONE;
5257         account_stmt    hstmt = NULL;
5258         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5259         int                     rc = 0, binding_count = 1;
5260
5261         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5262         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5263         ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5264
5265         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5266
5267         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, locale);
5268         hstmt = _account_prepare_query(query);
5269
5270         _account_query_bind_text(hstmt, binding_count++, app_id);
5271
5272         rc = _account_query_step(hstmt);
5273         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5274
5275         label_s* label_record = NULL;
5276
5277         while (rc == SQLITE_ROW) {
5278                 label_record = (label_s*) malloc(sizeof(label_s));
5279
5280                 if (label_record == NULL) {
5281                         ACCOUNT_FATAL("malloc Failed");
5282                         break;
5283                 }
5284
5285                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5286
5287                 _account_type_convert_column_to_label(hstmt,label_record);
5288
5289                 *label = _account_get_text(label_record->label);
5290
5291                 _account_type_free_label_items(label_record);
5292                 _ACCOUNT_FREE(label_record);
5293
5294                 rc = _account_query_step(hstmt);
5295         }
5296
5297         _account_query_finalize(hstmt);
5298         hstmt = NULL;
5299
5300         error_code = ACCOUNT_ERROR_NONE;
5301
5302         CATCH:
5303         if (hstmt != NULL) {
5304                 _account_query_finalize(hstmt);
5305                 hstmt = NULL;
5306         }
5307
5308         pthread_mutex_unlock(&account_mutex);
5309         return error_code;
5310 }
5311
5312 static int _account_insert_custom(account_s *account, int account_id)
5313 {
5314         int                     rc, count = 1;
5315         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5316         account_stmt    hstmt = NULL;
5317
5318         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5319
5320         if (g_slist_length( account->custom_list)==0) {
5321                 ACCOUNT_ERROR( "_account_insert_custom, no custom data\n");
5322                 return ACCOUNT_ERROR_NONE;
5323         }
5324
5325         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5326
5327         rc = _account_get_record_count(query);
5328
5329         if (rc <= 0) {
5330                 ACCOUNT_ERROR( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5331                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5332         }
5333
5334         /* insert query*/
5335
5336         GSList *iter;
5337
5338         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5339                 int rc, ret;
5340                 count = 1;
5341                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5342                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5343                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5344
5345                 hstmt = _account_prepare_query(query);
5346
5347                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5348
5349                 account_custom_s* custom_data = NULL;
5350                 custom_data = (account_custom_s*)iter->data;
5351                 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
5352
5353                 ret = _account_query_bind_int(hstmt, count++, account_id);
5354                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5355                 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5356                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5357                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5358                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5359                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5360                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5361
5362                 rc = _account_query_step(hstmt);
5363
5364                 if (rc != SQLITE_DONE) {
5365                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5366                         break;
5367                 }
5368
5369                 _account_query_finalize(hstmt);
5370                 hstmt = NULL;
5371
5372         }
5373
5374         ACCOUNT_VERBOSE( "_account_insert_custom() DONE\n");
5375
5376         return ACCOUNT_ERROR_NONE;
5377 }
5378
5379 static int _account_update_custom(account_s *account, int account_id)
5380 {
5381         int                     rc, count = 1;
5382         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5383         account_stmt    hstmt = NULL;
5384
5385         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5386
5387         ACCOUNT_INFO( "account handle=%p, account_id=%d", account, account_id);
5388
5389         if (g_slist_length( account->custom_list)==0) {
5390                 ACCOUNT_ERROR( "_account_update_custom, no custom data\n");
5391                 return ACCOUNT_ERROR_NONE;
5392         }
5393
5394         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5395
5396         rc = _account_get_record_count(query);
5397
5398         if (rc <= 0) {
5399                 ACCOUNT_ERROR( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5400                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5401         }
5402
5403         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5404
5405         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5406         hstmt = _account_prepare_query(query);
5407         count = 1;
5408         _account_query_bind_int(hstmt, count++, (int)account_id);
5409         rc = _account_query_step(hstmt);
5410
5411         if (rc != SQLITE_DONE) {
5412                 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5413                 return ACCOUNT_ERROR_DB_FAILED;
5414         }
5415         _account_query_finalize(hstmt);
5416         hstmt = NULL;
5417
5418         GSList *iter;
5419
5420         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5421                 int rc, ret;
5422                 count = 1;
5423                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5424                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5425                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5426
5427                 hstmt = _account_prepare_query(query);
5428
5429                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5430
5431                 account_custom_s* custom_data = NULL;
5432                 custom_data = (account_custom_s*)iter->data;
5433
5434                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5435                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5436                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5437                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5438                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5439                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5440                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5441                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5442
5443                 rc = _account_query_step(hstmt);
5444
5445                 if (rc != SQLITE_DONE) {
5446                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5447                         break;
5448                 }
5449
5450                 _account_query_finalize(hstmt);
5451                 hstmt = NULL;
5452
5453         }
5454
5455         ACCOUNT_VERBOSE( "_account_update_custom() DONE\n");
5456
5457         return ACCOUNT_ERROR_NONE;
5458 }
5459
5460 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data )
5461 {
5462         int                     error_code = ACCOUNT_ERROR_NONE;
5463         account_stmt    hstmt = NULL;
5464         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5465         int                     rc = 0;
5466
5467         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5468         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5469         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5470
5471         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5472
5473         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5474         hstmt = _account_prepare_query(query);
5475
5476         rc = _account_query_step(hstmt);
5477         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5478
5479         account_custom_s* custom_record = NULL;
5480
5481         while (rc == SQLITE_ROW) {
5482                 bool cb_ret = FALSE;
5483                 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5484
5485                 if (custom_record == NULL) {
5486                         ACCOUNT_FATAL("malloc Failed");
5487                         break;
5488                 }
5489
5490                 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5491
5492                 _account_convert_column_to_custom(hstmt, custom_record);
5493
5494                 cb_ret = cb_func(custom_record->key, custom_record->value, user_data);
5495
5496                 _account_custom_item_free(custom_record);
5497                 _ACCOUNT_FREE(custom_record);
5498
5499                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5500
5501                 rc = _account_query_step(hstmt);
5502         }
5503
5504         _account_query_finalize(hstmt);
5505         hstmt = NULL;
5506
5507         error_code = ACCOUNT_ERROR_NONE;
5508
5509 CATCH:
5510         if (hstmt != NULL) {
5511                 _account_query_finalize(hstmt);
5512                 hstmt = NULL;
5513         }
5514
5515         pthread_mutex_unlock(&account_mutex);
5516         return error_code;
5517 }
5518
5519 static void _account_subscribe_vconf_callback(keynode_t *key, void *user_data)
5520 {
5521         account_subscribe_s* tmp = (account_subscribe_s*)user_data;
5522         char *msg = NULL, *vconf_key = NULL;
5523         char event_msg[256] ={0, };
5524         int account_id = -1;
5525
5526         ACCOUNT_DEBUG("START\n");
5527
5528         if(!key) {
5529                 ACCOUNT_ERROR("No subscribtion msg !!!!!\n");
5530                 return;
5531         }
5532
5533         if(!tmp) {
5534                 ACCOUNT_ERROR("user data required\n");
5535                 return;
5536         }
5537
5538         if(!memcmp(vconf_keynode_get_name(key), VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR)))
5539         {
5540                 vconf_key = vconf_keynode_get_str(key);
5541                 msg = strdup(vconf_key);
5542
5543                 char* event_type = NULL;
5544                 char* id = NULL;
5545                 char* ptr = NULL;
5546
5547                 event_type = strtok_r(msg, ":", &ptr);
5548                 id = strtok_r(NULL, ":", &ptr);
5549
5550                 ACCOUNT_DEBUG("msg(%s), event_type(%s), id(%s)\n", msg, event_type, id);
5551
5552                 ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type);
5553
5554                 account_id = atoi(id);
5555
5556                 if(tmp->account_subscription_callback)
5557                         tmp->account_subscription_callback(event_msg, account_id, tmp->user_data);
5558         }
5559
5560         _ACCOUNT_FREE(msg);
5561
5562         ACCOUNT_DEBUG("END\n");
5563 }
5564
5565 ACCOUNT_API int account_subscribe_create(account_subscribe_h* account_subscribe)
5566 {
5567         if (!account_subscribe) {
5568                 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
5569                 return ACCOUNT_ERROR_INVALID_PARAMETER;
5570         }
5571
5572         account_subscribe_s *data = (account_subscribe_s*)calloc(1,sizeof(account_subscribe_s));
5573
5574         if(!data) {
5575                 ACCOUNT_FATAL("OUT OF MEMORY\n");
5576                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
5577         }
5578
5579         ACCOUNT_VERBOSE("create handle=%p\n", *data);
5580
5581         *account_subscribe = (account_subscribe_h)data;
5582
5583         return ACCOUNT_ERROR_NONE;
5584 }
5585
5586 ACCOUNT_API int account_subscribe_notification(account_subscribe_h account_subscribe, account_event_cb cb_func, void* user_data)
5587 {
5588         ACCOUNT_DEBUG("START\n");
5589
5590         ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n",  __FUNCTION__, __LINE__));
5591
5592         account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5593
5594         tmp->account_subscription_callback = cb_func;
5595         tmp->user_data = user_data;
5596
5597         if (vconf_notify_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5598                                 (vconf_callback_fn)_account_subscribe_vconf_callback,
5599                                 (void*)tmp) != 0) {
5600                 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5601                 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5602         }
5603
5604         ACCOUNT_DEBUG("Vconf Subscription Success!!!!\n");
5605         return ACCOUNT_ERROR_NONE;
5606 }
5607
5608 ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_subscribe)
5609 {
5610         ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n",  __FUNCTION__, __LINE__));
5611
5612         account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5613
5614         _ACCOUNT_FREE(tmp);
5615
5616         if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5617            (vconf_callback_fn)_account_subscribe_vconf_callback) != 0) {
5618                 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5619                 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5620         }
5621
5622         ACCOUNT_DEBUG("Vconf Unsubscription Success!!!!\n");
5623         return ACCOUNT_ERROR_NONE;
5624 }
5625