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