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