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