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