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