4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Wonyoung Lee <wy1115.lee@samsung.com>, Sungchan Kim <sungchan81.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/types.h>
33 #include <account-private.h>
37 #include <pkgmgr-info.h>
40 #include <tzplatform_config.h>
42 #define EAS_CMDLINE tzplatform_mkpath(TZ_SYS_BIN, "eas-engine")
43 #define EMAIL_SERVICE_CMDLINE tzplatform_mkpath(TZ_SYS_BIN, "email-service")
44 #define ACTIVESYNC_APPID "activesync-ui"
46 static sqlite3* g_hAccountDB = NULL;
47 static int g_refCntDB = 0;
48 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
50 static char *_account_get_text(const char *text_data);
51 static int _account_gslist_free(GSList* list);
52 static int _account_glist_free(GList* list);
53 static char *_account_query_table_column_text(account_stmt pStmt, int pos);
54 static int _account_insert_custom(account_s *account, int account_id);
55 static int _account_update_custom(account_s *account, int account_id);
56 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data );
57 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
59 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
64 GSList** appid_list = (GSList**)user_data;
66 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
67 item = _account_get_text(appid);
68 *appid_list = g_slist_append(*appid_list, item);
73 static inline int __read_proc(const char *path, char *buf, int size)
77 if (buf == NULL || path == NULL) {
78 ACCOUNT_ERROR("path and buffer is mandatory\n");
82 fd = open(path, O_RDONLY);
84 ACCOUNT_ERROR("fd open error(%d)\n", fd);
88 ret = read(fd, buf, size - 1);
90 ACCOUNT_ERROR("fd read error(%d)\n", fd);
101 char *_account_get_proc_cmdline_bypid(int pid)
106 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
107 ret = __read_proc(buf, buf, sizeof(buf));
109 ACCOUNT_INFO("No proc directory (%d)\n", pid);
117 static char* _account_get_current_appid()
120 char appid[128]={0,};
121 pkgmgrinfo_appinfo_h ahandle=NULL;
122 char* appid_ret = NULL;
125 ACCOUNT_INFO("pid (%d)\n", pid, ret);
126 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
127 ACCOUNT_INFO("appid (%s) ret(%d)!!!!!!\n", appid, ret);
129 /* SLP platform core exception */
130 if(strlen(appid) == 0){
131 char* cmdline = NULL;
132 cmdline = _account_get_proc_cmdline_bypid(pid);
133 ACCOUNT_INFO("cmdline (%s)!!!!!!\n", cmdline);
134 if(!strcmp(cmdline, EAS_CMDLINE)) {
135 appid_ret = _account_get_text(ACTIVESYNC_APPID);
138 ACCOUNT_ERROR("No app id\n");
143 appid_ret = _account_get_text(appid);
145 pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
149 static int _account_check_account_type_with_appid_group(const char* appid, char** verified_appid)
151 int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
153 pkgmgrinfo_appinfo_h ahandle=NULL;
154 pkgmgrinfo_pkginfo_h phandle=NULL;
156 GSList* appid_list = NULL;
160 ACCOUNT_INFO("input param is null\n");
161 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
165 ACCOUNT_INFO("output param is null\n");
166 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
169 if(!strcmp(appid, "com.samsung.setting")){
170 ACCOUNT_DEBUG("Setting exception\n");
171 *verified_appid = _account_get_text("com.samsung.setting");
172 return ACCOUNT_ERROR_NONE;
175 /* Get app id family which is stored in account database */
176 ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
177 ACCOUNT_INFO("ahandle (%p), ret(%x)\n", ahandle, ret);
178 ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
179 ACCOUNT_INFO("package_id (%s), ret(%x)\n", package_id, ret);
180 ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
181 ACCOUNT_INFO("phandle (%p), ret(%x)\n", package_id, ret);
183 ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); //==> pkgÇڵ鿡 ¼ÓÇÑ ui-appid ¸ðµÎ ãÀ½
184 ACCOUNT_INFO("ret(%x)\n", ret);
186 /* Compare current app id with the stored app id family */
187 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
188 char* tmp = (char*)iter->data;
190 if(account_type_query_app_id_exist(tmp) == ACCOUNT_ERROR_NONE) {
191 ACCOUNT_INFO("permission verified appid(%s), current appid(%s)\n", tmp, appid);
192 *verified_appid = _account_get_text(tmp);
193 error_code = ACCOUNT_ERROR_NONE;
197 ACCOUNT_DEBUG("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
203 g_slist_free(appid_list);
204 pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
205 pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
210 static const char *_account_db_err_msg()
212 assert(NULL != g_hAccountDB);
213 return sqlite3_errmsg(g_hAccountDB);
216 static void _account_insert_delete_update_notification_send(char *noti_name)
219 ACCOUNT_ERROR("Noti Name is NULL!!!!!!\n");
223 ACCOUNT_DEBUG("Sending notification with value %s\n", noti_name);
225 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) == 0) {
226 ACCOUNT_VERBOSE("Vconf MSG Str set SUCCESS !!!!!!\n");;
228 ACCOUNT_ERROR("Vconf MSG Str set FAILED !!!!!!\n");;
232 static int _account_get_record_count(char* query)
236 account_stmt pStmt = NULL;
238 assert(NULL != query);
239 assert(NULL != g_hAccountDB);
240 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
242 rc = sqlite3_step(pStmt);
243 if (SQLITE_ROW != rc) {
244 ACCOUNT_ERROR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
245 sqlite3_finalize(pStmt);
246 return ACCOUNT_ERROR_DB_FAILED;
249 ncount = sqlite3_column_int(pStmt, 0);
251 ACCOUNT_VERBOSE("count : %d, End", ncount);
252 sqlite3_finalize(pStmt);
257 static int _account_execute_query(char *query)
260 char* pszErrorMsg = NULL;
262 assert(NULL != query);
263 assert(NULL != g_hAccountDB);
265 ACCOUNT_INFO("query : %s", query);
267 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
268 if (SQLITE_OK != rc) {
269 ACCOUNT_FATAL("sqlite3_exec(%s) failed(%s).", query, pszErrorMsg);
270 sqlite3_free(pszErrorMsg);
276 static int _account_begin_transaction(void)
280 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
282 if (ret != SQLITE_OK) {
283 ACCOUNT_FATAL("_account_svc_begin_transaction fail :: %d", ret);
284 return ACCOUNT_ERROR_DB_FAILED;
287 return ACCOUNT_ERROR_NONE;
290 static int _account_end_transaction(bool is_success)
294 if (is_success == true) {
295 ret = _account_execute_query("COMMIT TRANSACTION");
297 ret = _account_execute_query("ROLLBACK TRANSACTION");
300 if (ret != SQLITE_OK) {
301 ACCOUNT_FATAL("_account_svc_end_transaction fail :: %d", ret);
302 return ACCOUNT_ERROR_DB_FAILED;
305 return ACCOUNT_ERROR_NONE;
308 static bool _account_check_add_more_account(const char* app_id)
310 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
313 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
314 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
316 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
318 ACCOUNT_DEBUG("app id (%s)\n", app_id);
320 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
321 rc = _account_get_record_count(query);
323 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
325 /* multiple account support case */
327 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
331 /* multiple account not support case */
332 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
333 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
334 rc = _account_get_record_count(query);
337 ACCOUNT_DEBUG("app id (%s) supports single account. and there is no account of the app id\n", app_id);
344 static int _account_create_all_tables(void)
347 int error_code = ACCOUNT_ERROR_NONE;
348 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
351 ACCOUNT_MEMSET(query, 0, sizeof(query));
353 ACCOUNT_VERBOSE("_account_create_all_tables begin");
355 /*Create the account table*/
356 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
357 rc = _account_get_record_count(query);
359 ACCOUNT_MEMSET(query, 0, sizeof(query));
360 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_SCHEMA, ACCOUNT_TABLE);
361 ACCOUNT_INFO("Create %s table", ACCOUNT_TABLE);
362 rc = _account_execute_query(query);
363 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
366 /*Create capability table*/
367 ACCOUNT_MEMSET(query, 0, sizeof(query));
368 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
369 rc = _account_get_record_count(query);
371 ACCOUNT_MEMSET(query, 0, sizeof(query));
372 ACCOUNT_SNPRINTF(query, sizeof(query), CAPABILITY_SCHEMA, CAPABILITY_TABLE);
373 ACCOUNT_INFO("Create %s table", CAPABILITY_TABLE);
374 rc = _account_execute_query(query);
375 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
378 /* Create account custom table */
379 ACCOUNT_MEMSET(query, 0, sizeof(query));
380 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
381 rc = _account_get_record_count(query);
383 ACCOUNT_MEMSET(query, 0, sizeof(query));
384 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_CUSTOM_SCHEMA, ACCOUNT_CUSTOM_TABLE);
385 ACCOUNT_INFO("Create %s table", ACCOUNT_CUSTOM_TABLE);
386 rc = _account_execute_query(query);
387 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
390 /* Create account type table */
391 ACCOUNT_MEMSET(query, 0, sizeof(query));
392 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
393 rc = _account_get_record_count(query);
395 ACCOUNT_MEMSET(query, 0, sizeof(query));
396 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_TYPE_SCHEMA, ACCOUNT_TYPE_TABLE);
397 ACCOUNT_INFO("Create %s table", ACCOUNT_TYPE_TABLE);
398 rc = _account_execute_query(query);
399 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
402 /* Create label table */
403 ACCOUNT_MEMSET(query, 0, sizeof(query));
404 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
405 rc = _account_get_record_count(query);
407 ACCOUNT_MEMSET(query, 0, sizeof(query));
408 ACCOUNT_SNPRINTF(query, sizeof(query), LABEL_SCHEMA, LABEL_TABLE);
409 ACCOUNT_INFO("Create %s table", LABEL_TABLE);
410 rc = _account_execute_query(query);
411 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
414 /* Create account feature table */
415 ACCOUNT_MEMSET(query, 0, sizeof(query));
416 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
417 rc = _account_get_record_count(query);
419 ACCOUNT_MEMSET(query, 0, sizeof(query));
420 ACCOUNT_SNPRINTF(query, sizeof(query), PROVIDER_FEATURE_SCHEMA, PROVIDER_FEATURE_TABLE);
421 ACCOUNT_INFO("Create %s table", PROVIDER_FEATURE_TABLE);
422 rc = _account_execute_query(query);
423 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
429 static bool _account_check_is_all_table_exists()
432 char query[ACCOUNT_SQL_LEN_MAX] = {0,};
433 ACCOUNT_MEMSET(query, 0, sizeof(query));
435 ACCOUNT_VERBOSE("_account_check_is_all_table_exists");
437 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
438 ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
439 rc = _account_get_record_count(query);
441 ACCOUNT_DEBUG("Table count = %d\n", rc);
443 if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
444 ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
448 ACCOUNT_VERBOSE("END of _account_check_is_all_table_exists\n");
453 static int _account_db_open(void)
456 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
458 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
461 rc = db_util_open(ACCOUNT_DB_NAME, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
462 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
465 ACCOUNT_INFO("_account_db_open: The database connected. refcnt=%d ", g_refCntDB);
468 ACCOUNT_INFO("The database already connected. refcnt=%d ", g_refCntDB);
471 return ACCOUNT_ERROR_NONE;
474 static int _account_db_close(void)
480 if (g_refCntDB > 0) {
482 ACCOUNT_INFO("_account_svc_db_close: The database disconnected. refcnt=%d ", g_refCntDB);
484 if (g_refCntDB == 0) {
485 rc = db_util_close(g_hAccountDB);
486 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
488 ACCOUNT_INFO( "_account_svc_db_close: The database disconnected really. ");
490 ret = ACCOUNT_ERROR_NONE;
492 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
493 ret = ACCOUNT_ERROR_DB_FAILED;
499 static int _account_connect(void)
501 int error_code = ACCOUNT_ERROR_NONE;
502 const char *ACCT_DIR_PATH = tzplatform_mkpath(TZ_USER_SHARE, "account");
505 error_code = mkdir(ACCT_DIR_PATH, 755);
506 pid = tzplatform_getgid(TZ_SYS_USER_GROUP);
507 error_code = chown(ACCT_DIR_PATH, -1, pid);
509 pthread_mutex_lock(&account_mutex);
511 ACCOUNT_VERBOSE("db path = %s\n", ACCOUNT_DB_NAME);
513 error_code = _account_db_open();
514 if (ACCOUNT_ERROR_NONE != error_code) {
515 ACCOUNT_ERROR("The database isn't connected.\n");
516 pthread_mutex_unlock(&account_mutex);
517 return ACCOUNT_ERROR_DB_NOT_OPENED;
520 if (FALSE == _account_check_is_all_table_exists())
521 error_code = _account_create_all_tables();
523 pthread_mutex_unlock(&account_mutex);
524 return ACCOUNT_ERROR_NONE;
527 ACCOUNT_API int account_connect (void)
529 return _account_connect();
532 static int _account_disconnect(void)
534 int error_code = ACCOUNT_ERROR_NONE;
536 pthread_mutex_lock(&account_mutex);
537 ACCOUNT_INFO("db path = %s have been closed!!!\n", ACCOUNT_DB_NAME);
539 error_code = _account_db_close();
540 pthread_mutex_unlock(&account_mutex);
545 ACCOUNT_API int account_disconnect (void)
547 return _account_disconnect();
550 static int _account_free_capability_items(account_capability_s *data)
552 _ACCOUNT_FREE(data->type);
553 _ACCOUNT_FREE(data->package_name);
554 _ACCOUNT_FREE(data->user_name);
556 return ACCOUNT_ERROR_NONE;
559 static int _account_custom_item_free(account_custom_s *data)
561 _ACCOUNT_FREE(data->app_id);
562 _ACCOUNT_FREE(data->key);
563 _ACCOUNT_FREE(data->value);
565 return ACCOUNT_ERROR_NONE;
568 static int _account_custom_gslist_free(GSList* list)
570 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
574 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
575 account_custom_s *custom_data = (account_custom_s*)iter->data;
576 _account_custom_item_free(custom_data);
577 _ACCOUNT_FREE(custom_data);
583 return ACCOUNT_ERROR_NONE;
586 static int _account_free_account_items(account_s *data)
588 _ACCOUNT_FREE(data->user_name);
589 _ACCOUNT_FREE(data->email_address);
590 _ACCOUNT_FREE(data->display_name);
591 _ACCOUNT_FREE(data->icon_path);
592 _ACCOUNT_FREE(data->source);
593 _ACCOUNT_FREE(data->package_name);
594 _ACCOUNT_FREE(data->domain_name);
595 _ACCOUNT_FREE(data->access_token);
598 for(i=0;i<USER_TXT_CNT;i++)
599 _ACCOUNT_FREE(data->user_data_txt[i]);
601 _account_gslist_free(data->capablity_list);
602 _account_glist_free(data->account_list);
603 _account_custom_gslist_free(data->custom_list);
605 return ACCOUNT_ERROR_NONE;
608 static int _account_gslist_free(GSList* list)
610 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
614 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
615 account_capability_s *cap_data = (account_capability_s*)iter->data;
616 _account_free_capability_items(cap_data);
617 _ACCOUNT_FREE(cap_data);
623 return ACCOUNT_ERROR_NONE;
626 static int _account_glist_free(GList* list)
628 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
632 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
633 account_s *account_record = (account_s*)iter->data;
634 _account_free_account_items(account_record);
635 _ACCOUNT_FREE(account_record);
641 return ACCOUNT_ERROR_NONE;
644 static gboolean _account_check_duplicated(account_s *data)
647 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
650 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
652 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and package_name='%s'"
653 , ACCOUNT_TABLE, data->user_name, data->package_name);
655 count = _account_get_record_count(query);
657 ACCOUNT_INFO("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
658 count, data->user_name, data->domain_name );
666 static int _account_get_next_sequence(char *pszName)
669 account_stmt pStmt = NULL;
671 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
673 ACCOUNT_VERBOSE( "[Enter] pszName:%s\n", pszName);
675 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
676 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
677 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
679 rc = sqlite3_step(pStmt);
680 max_seq = sqlite3_column_int(pStmt, 0);
683 ACCOUNT_VERBOSE( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
685 /*Finalize Statement*/
686 rc = sqlite3_finalize(pStmt);
692 static account_stmt _account_prepare_query(char *query)
695 account_stmt pStmt = NULL;
697 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
699 ACCOUNT_INFO( "prepare query : %s", query);
701 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
702 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
707 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
709 assert(NULL != pStmt);
711 return sqlite3_bind_int(pStmt, pos, num);
714 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
716 assert(NULL != pStmt);
719 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
721 return sqlite3_bind_null(pStmt, pos);
724 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
726 ACCOUNT_VERBOSE( "_account_convert_account_to_sql");
729 /*Caution : Keep insert query orders.*/
732 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
734 /* 2. email address*/
735 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
738 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
741 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
744 _account_query_bind_text(hstmt, count++, (char*)account->source);
747 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
750 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
753 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
756 _account_query_bind_int(hstmt, count++, account->auth_type);
759 _account_query_bind_int(hstmt, count++, account->secret);
761 /* 11. sync_support */
762 _account_query_bind_int(hstmt, count++, account->sync_support);
767 for(i=0; i< USER_TXT_CNT; i++)
768 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
770 /* 13. user integer */
771 for(i=0; i< USER_INT_CNT; i++)
772 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
777 static void _account_query_finalize(account_stmt pStmt)
782 ACCOUNT_FATAL( "pStmt is NULL");
786 rc = sqlite3_finalize(pStmt);
787 if (rc != SQLITE_OK) {
788 ACCOUNT_FATAL( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
791 ACCOUNT_VERBOSE( "sqlite3_finalize finish");
794 static int _account_query_step(account_stmt pStmt)
796 assert(NULL != pStmt);
797 return sqlite3_step(pStmt);
800 static int _account_execute_insert_query(account_s *account)
803 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
804 int error_code = ACCOUNT_ERROR_NONE;
805 account_stmt hstmt = NULL;
807 /* check whether app id exist in account type db */
809 if (!account->user_name && !account->display_name && !account->email_address) {
810 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
811 return ACCOUNT_ERROR_INVALID_PARAMETER;
814 /* Check account own icon existence */
815 if(!account->icon_path) {
816 char* temptxt = NULL;
817 account_type_h account_type = NULL;
818 error_code = account_type_create(&account_type);
819 error_code = account_type_query_by_app_id((const char*)account->package_name, &account_type);
820 error_code = account_type_get_icon_path(account_type, &temptxt);
821 if (error_code != ACCOUNT_ERROR_NONE) {
822 ACCOUNT_ERROR("account_type_get_icon_path: Failed \n");
823 } else if (temptxt && strlen(temptxt)) {
824 ACCOUNT_DEBUG("icon path (%s) app_id(%s) \n", temptxt, account->package_name);
825 account->icon_path = _account_get_text(temptxt);
827 ACCOUNT_DEBUG("account_get_icon_path: returned NULL \n");
829 _ACCOUNT_FREE(temptxt);
830 error_code = account_type_destroy(account_type);
833 /* End of Check account own icon existence */
835 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
836 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , "
837 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
838 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values "
839 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?)", ACCOUNT_TABLE);
841 hstmt = _account_prepare_query(query);
842 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
844 _account_convert_account_to_sql(account, hstmt, query);
846 rc = _account_query_step(hstmt);
847 if (rc != SQLITE_DONE) {
848 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
849 error_code = ACCOUNT_ERROR_DB_FAILED;
852 _account_query_finalize(hstmt);
858 static int _account_insert_capability(account_s *account, int account_id)
861 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
862 account_stmt hstmt = NULL;
864 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
866 if (g_slist_length( account->capablity_list)==0) {
867 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
868 return ACCOUNT_ERROR_NONE;
871 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
873 rc = _account_get_record_count(query);
876 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
877 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
884 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
888 account_capability_s* cap_data = NULL;
889 cap_data = (account_capability_s*)iter->data;
891 ACCOUNT_VERBOSE("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
893 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
894 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
895 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
896 hstmt = _account_prepare_query(query);
898 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
900 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
901 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
902 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
903 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
904 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
905 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
906 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
907 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
908 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
909 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
911 rc = _account_query_step(hstmt);
913 if (rc != SQLITE_DONE) {
914 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
918 _account_query_finalize(hstmt);
923 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
925 return ACCOUNT_ERROR_NONE;
928 static int _account_update_capability(account_s *account, int account_id)
931 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
932 account_stmt hstmt = NULL;
934 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
936 if (g_slist_length( account->capablity_list)==0) {
937 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
938 return ACCOUNT_ERROR_NONE;
941 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
943 rc = _account_get_record_count(query);
946 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
947 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
950 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
952 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
953 hstmt = _account_prepare_query(query);
955 _account_query_bind_int(hstmt, count++, (int)account_id);
956 rc = _account_query_step(hstmt);
958 if (rc != SQLITE_DONE) {
959 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
960 return ACCOUNT_ERROR_DB_FAILED;
962 _account_query_finalize(hstmt);
967 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
970 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
971 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
972 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
974 hstmt = _account_prepare_query(query);
976 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
978 account_capability_s* cap_data = NULL;
979 cap_data = (account_capability_s*)iter->data;
981 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
982 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
983 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
984 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
985 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
986 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
987 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
988 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
989 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
990 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
992 rc = _account_query_step(hstmt);
994 if (rc != SQLITE_DONE) {
995 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
999 _account_query_finalize(hstmt);
1004 ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
1006 return ACCOUNT_ERROR_NONE;
1009 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
1012 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1013 account_stmt hstmt = NULL;
1015 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1017 if (g_slist_length( account->capablity_list)==0) {
1018 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
1019 return ACCOUNT_ERROR_NONE;
1022 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
1024 rc = _account_get_record_count(query);
1027 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1028 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1031 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1033 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1034 hstmt = _account_prepare_query(query);
1036 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1037 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1038 rc = _account_query_step(hstmt);
1039 if (rc != SQLITE_DONE) {
1040 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1041 return ACCOUNT_ERROR_DB_FAILED;
1044 _account_query_finalize(hstmt);
1049 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1052 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1053 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1054 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1056 hstmt = _account_prepare_query(query);
1058 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1060 account_capability_s* cap_data = NULL;
1061 cap_data = (account_capability_s*)iter->data;
1063 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1064 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1065 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1066 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1067 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1068 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1069 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1070 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1071 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1072 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1074 rc = _account_query_step(hstmt);
1076 if (rc != SQLITE_DONE) {
1077 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1081 _account_query_finalize(hstmt);
1086 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
1088 return ACCOUNT_ERROR_NONE;
1091 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1093 assert(NULL != pStmt);
1095 return sqlite3_column_int(pStmt, pos);
1098 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
1100 assert(NULL != pStmt);
1102 return (char *)sqlite3_column_text(pStmt, pos);
1105 static void _account_db_data_to_text(const char *textbuf, char **output)
1107 if (textbuf && strlen(textbuf)>0) {
1112 *output = strdup(textbuf);
1116 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1118 char *textbuf = NULL;
1120 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1122 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1123 _account_db_data_to_text(textbuf, &(account_record->user_name));
1125 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1126 _account_db_data_to_text(textbuf, &(account_record->email_address));
1128 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1129 _account_db_data_to_text(textbuf, &(account_record->display_name));
1131 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1132 _account_db_data_to_text(textbuf, &(account_record->icon_path));
1134 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1135 _account_db_data_to_text(textbuf, &(account_record->source));
1137 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1138 _account_db_data_to_text(textbuf, &(account_record->package_name));
1140 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1141 _account_db_data_to_text(textbuf, &(account_record->access_token));
1143 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1144 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1146 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1148 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1150 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1152 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1153 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1155 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1156 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1158 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1159 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1161 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1162 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1164 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1165 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1167 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1168 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1169 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1170 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1171 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1173 ACCOUNT_VERBOSE("END _account_convert_column_to_account");
1176 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1178 char *textbuf = NULL;
1180 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1182 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1183 _account_db_data_to_text(textbuf, &(capability_record->type));
1185 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1187 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1188 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1190 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1191 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1193 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1195 ACCOUNT_VERBOSE("END _account_convert_column_to_capability");
1198 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1200 char *textbuf = NULL;
1202 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1204 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1205 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1207 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1208 _account_db_data_to_text(textbuf, &(custom_record->key));
1210 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1211 _account_db_data_to_text(textbuf, &(custom_record->value));
1213 ACCOUNT_VERBOSE("END _account_convert_column_to_custom");
1216 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1218 account_s *data = (account_s*)user_data;
1220 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1222 if (cap_data == NULL)
1224 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1226 cap_data->type = _account_get_text(capability_type);
1227 cap_data->value = capability_value;
1229 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1231 ACCOUNT_VERBOSE("_account_get_capability_text_cb :: %s\n", capability_type);
1237 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1239 account_s *data = (account_s*)user_data;
1241 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1243 if (custom_data == NULL) {
1244 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1247 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1249 custom_data->account_id = data->id;
1250 custom_data->app_id = _account_get_text(data->package_name);
1251 custom_data->key = _account_get_text(key);
1252 custom_data->value = _account_get_text(value);
1254 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1260 static char *_account_get_text(const char *text_data)
1262 char *text_value = NULL;
1264 if (text_data != NULL) {
1265 text_value = strdup(text_data);
1266 ACCOUNT_VERBOSE("text_value = %s", text_value);
1271 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
1273 int error_code = ACCOUNT_ERROR_NONE;
1274 account_stmt hstmt = NULL;
1275 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1277 account_s *old_account = NULL;
1279 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1280 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1281 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1282 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1284 old_account = (account_s*)calloc(1, sizeof(account_s));
1286 ACCOUNT_FATAL("Memory alloc fail\n");
1287 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1290 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1292 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
1293 hstmt = _account_prepare_query(query);
1295 rc = _account_query_step(hstmt);
1296 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1298 while (rc == SQLITE_ROW) {
1299 _account_convert_column_to_account(hstmt, old_account);
1300 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
1301 rc = _account_query_step(hstmt);
1304 _account_query_finalize(hstmt);
1308 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1309 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1312 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1313 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1316 new_account->id = old_account->id;
1319 if(!new_account->user_name) {
1320 if(old_account->user_name)
1321 new_account->user_name = _account_get_text(old_account->user_name);
1325 if(!new_account->display_name) {
1326 if(old_account->display_name)
1327 new_account->display_name = _account_get_text(old_account->display_name);
1331 if(!new_account->email_address) {
1332 if(old_account->email_address)
1333 new_account->email_address = _account_get_text(old_account->email_address);
1337 if(!new_account->domain_name) {
1338 if(old_account->domain_name)
1339 new_account->domain_name = _account_get_text(old_account->domain_name);
1343 if(!new_account->icon_path) {
1344 if(old_account->icon_path)
1345 new_account->icon_path = _account_get_text(old_account->icon_path);
1349 if(!new_account->source) {
1350 if(old_account->source)
1351 new_account->source = _account_get_text(old_account->source);
1354 _ACCOUNT_FREE(new_account->package_name);
1355 new_account->package_name = _account_get_text(old_account->package_name);
1358 if(!new_account->access_token) {
1359 if(old_account->access_token)
1360 new_account->access_token = _account_get_text(old_account->access_token);
1365 for(i=0;i<USER_TXT_CNT;i++) {
1366 if(!new_account->user_data_txt[i]) {
1367 if(old_account->user_data_txt[i])
1368 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1373 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1374 new_account->auth_type = old_account->auth_type;
1378 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1379 new_account->secret = old_account->secret;
1383 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1384 new_account->sync_support = old_account->sync_support;
1388 for(i=0;i<USER_INT_CNT;i++) {
1389 if(new_account->user_data_int[i] == 0) {
1390 new_account->user_data_int[i] = old_account->user_data_int[i];
1396 // user custom table
1401 if (hstmt != NULL) {
1402 _account_query_finalize(hstmt);
1407 _account_free_account_items(old_account);
1408 _ACCOUNT_FREE(old_account);
1411 return ACCOUNT_ERROR_NONE;
1416 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
1418 int rc = 0, binding_count = 0, count = 0;
1419 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1420 int error_code = ACCOUNT_ERROR_NONE;
1421 account_stmt hstmt = NULL;
1423 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1424 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1426 char* current_appid = NULL;
1427 char* verified_appid = NULL;
1429 current_appid = _account_get_current_appid();
1430 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
1432 _ACCOUNT_FREE(current_appid);
1433 _ACCOUNT_FREE(verified_appid);
1435 if(error_code != ACCOUNT_ERROR_NONE){
1436 ACCOUNT_ERROR("No permission to update\n");
1437 return ACCOUNT_ERROR_PERMISSION_DENIED;
1440 _account_compare_old_record_by_user_name(account, user_name, package_name);
1442 if (!account->package_name) {
1443 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1444 return ACCOUNT_ERROR_INVALID_PARAMETER;
1447 if (!account->user_name && !account->display_name && !account->email_address) {
1448 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1449 return ACCOUNT_ERROR_INVALID_PARAMETER;
1452 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1453 , ACCOUNT_TABLE, user_name, package_name);
1455 count = _account_get_record_count(query);
1457 ACCOUNT_INFO("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1458 count, user_name, package_name);
1459 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1462 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1463 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1464 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1465 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1466 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1468 hstmt = _account_prepare_query(query);
1469 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1471 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1473 _account_query_bind_text(hstmt, binding_count++, user_name);
1474 _account_query_bind_text(hstmt, binding_count++, package_name);
1475 rc = _account_query_step(hstmt);
1476 if (rc != SQLITE_DONE) {
1477 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1479 _account_query_finalize(hstmt);
1482 /*update capability*/
1483 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1486 error_code = _account_update_custom(account, account->id);
1491 ACCOUNT_API int account_insert_to_db(account_h account, int *account_id)
1493 int error_code = ACCOUNT_ERROR_NONE;
1494 int ret_transaction = 0;
1496 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1497 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1498 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1500 account_s *data = (account_s*)account;
1502 pthread_mutex_lock(&account_mutex);
1504 /* transaction control required*/
1505 ret_transaction = _account_begin_transaction();
1507 if (ret_transaction != ACCOUNT_ERROR_NONE) {
1508 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
1509 pthread_mutex_unlock(&account_mutex);
1510 return ret_transaction;
1513 if (_account_check_duplicated(data)) {
1514 ret_transaction = _account_end_transaction(FALSE);
1515 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
1517 pthread_mutex_unlock(&account_mutex);
1518 return ACCOUNT_ERROR_DUPLICATED;
1520 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1523 appid = _account_get_current_appid();
1526 // API caller cannot be recognized
1527 ACCOUNT_ERROR("APP ID not detectable!\n");
1528 ret_transaction = _account_end_transaction(FALSE);
1529 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1530 pthread_mutex_unlock(&account_mutex);
1531 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1534 char* verified_appid = NULL;
1535 error_code = _account_check_account_type_with_appid_group(appid, &verified_appid);
1536 if(error_code != ACCOUNT_ERROR_NONE){
1537 ret_transaction = _account_end_transaction(FALSE);
1538 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1539 _ACCOUNT_FREE(verified_appid);
1540 _ACCOUNT_FREE(appid);
1541 pthread_mutex_unlock(&account_mutex);
1545 _ACCOUNT_FREE(appid);
1548 if(!_account_check_add_more_account(verified_appid)) {
1549 ret_transaction = _account_end_transaction(FALSE);
1550 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1551 pthread_mutex_unlock(&account_mutex);
1552 _ACCOUNT_FREE(verified_appid);
1553 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1556 _ACCOUNT_FREE(data->package_name);
1557 data->package_name = _account_get_text(verified_appid);
1558 _ACCOUNT_FREE(verified_appid);
1561 error_code = _account_execute_insert_query(data);
1563 if (error_code != ACCOUNT_ERROR_NONE) {
1564 ret_transaction = _account_end_transaction(FALSE);
1565 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1567 pthread_mutex_unlock(&account_mutex);
1572 ACCOUNT_VERBOSE( "_account_execute_insert_query, insert error_code : %d", error_code);
1574 error_code = _account_insert_capability(data, *account_id);
1575 if (error_code != ACCOUNT_ERROR_NONE) {
1576 ret_transaction = _account_end_transaction(FALSE);
1577 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1579 pthread_mutex_unlock(&account_mutex);
1583 error_code = _account_insert_custom(data, *account_id);
1584 if (error_code != ACCOUNT_ERROR_NONE) {
1585 ret_transaction = _account_end_transaction(FALSE);
1586 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1588 pthread_mutex_unlock(&account_mutex);
1592 pthread_mutex_unlock(&account_mutex);
1593 _account_end_transaction(TRUE);
1596 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1597 _account_insert_delete_update_notification_send(buf);
1599 return ACCOUNT_ERROR_NONE;
1603 ACCOUNT_API int account_create(account_h *account)
1606 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1607 return ACCOUNT_ERROR_INVALID_PARAMETER;
1610 account_s *data = (account_s*)malloc(sizeof(account_s));
1613 ACCOUNT_FATAL("Memory Allocation Failed");
1614 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1616 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1618 ACCOUNT_VERBOSE("create handle=%p\n", *account);
1620 /*Setting account as visible by default*/
1621 data->secret = ACCOUNT_SECRECY_VISIBLE;
1623 /*Setting account as not supporting sync by default*/
1624 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
1626 *account = (account_h)data;
1628 return ACCOUNT_ERROR_NONE;
1631 ACCOUNT_API int account_set_user_name(account_h account, const char *user_name)
1634 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1635 return ACCOUNT_ERROR_INVALID_PARAMETER;
1639 ACCOUNT_ERROR("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1640 return ACCOUNT_ERROR_INVALID_PARAMETER;
1643 account_s *data = (account_s*)account;
1645 _ACCOUNT_FREE(data->user_name);
1646 data->user_name = _account_get_text(user_name);
1648 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1650 return ACCOUNT_ERROR_NONE;
1653 ACCOUNT_API int account_set_display_name(account_h account, const char *display_name)
1656 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1657 return ACCOUNT_ERROR_INVALID_PARAMETER;
1660 if (!display_name) {
1661 ACCOUNT_ERROR("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1662 return ACCOUNT_ERROR_INVALID_PARAMETER;
1665 account_s *data = (account_s*)account;
1667 _ACCOUNT_FREE(data->display_name);
1668 data->display_name = _account_get_text(display_name);
1670 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1672 return ACCOUNT_ERROR_NONE;
1675 ACCOUNT_API int account_set_email_address(account_h account, const char *email_address)
1678 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1679 return ACCOUNT_ERROR_INVALID_PARAMETER;
1682 if (!email_address) {
1683 ACCOUNT_ERROR("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1684 return ACCOUNT_ERROR_INVALID_PARAMETER;
1687 account_s *data = (account_s*)account;
1689 _ACCOUNT_FREE(data->email_address);
1690 data->email_address = _account_get_text(email_address);
1692 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1694 return ACCOUNT_ERROR_NONE;
1697 ACCOUNT_API int account_set_icon_path(account_h account, const char *icon_path)
1700 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1701 return ACCOUNT_ERROR_INVALID_PARAMETER;
1705 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1706 return ACCOUNT_ERROR_INVALID_PARAMETER;
1709 account_s *data = (account_s*)account;
1711 _ACCOUNT_FREE(data->icon_path);
1712 data->icon_path = _account_get_text(icon_path);
1714 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1716 return ACCOUNT_ERROR_NONE;
1719 ACCOUNT_API int account_set_source(account_h account, const char *source)
1722 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1723 return ACCOUNT_ERROR_INVALID_PARAMETER;
1727 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1728 return ACCOUNT_ERROR_INVALID_PARAMETER;
1730 account_s *data = (account_s*)account;
1732 _ACCOUNT_FREE(data->source);
1733 data->source = _account_get_text(source);
1735 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1737 return ACCOUNT_ERROR_NONE;
1740 ACCOUNT_API int account_set_package_name(account_h account, const char *package_name)
1743 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1744 return ACCOUNT_ERROR_INVALID_PARAMETER;
1747 if (!package_name) {
1748 ACCOUNT_ERROR("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1749 return ACCOUNT_ERROR_INVALID_PARAMETER;
1752 account_s *data = (account_s*)account;
1754 _ACCOUNT_FREE(data->package_name);
1755 data->package_name = _account_get_text(package_name);
1757 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1759 return ACCOUNT_ERROR_NONE;
1762 ACCOUNT_API int account_set_domain_name(account_h account, const char *domain_name)
1765 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1766 return ACCOUNT_ERROR_INVALID_PARAMETER;
1770 ACCOUNT_ERROR("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1771 return ACCOUNT_ERROR_INVALID_PARAMETER;
1773 account_s *data = (account_s*)account;
1775 _ACCOUNT_FREE(data->domain_name);
1776 data->domain_name = _account_get_text(domain_name);
1778 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1780 return ACCOUNT_ERROR_NONE;
1783 ACCOUNT_API int account_set_access_token(account_h account, const char *access_token)
1786 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1787 return ACCOUNT_ERROR_INVALID_PARAMETER;
1790 if (!access_token) {
1791 ACCOUNT_ERROR("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1792 return ACCOUNT_ERROR_INVALID_PARAMETER;
1795 account_s *data = (account_s*)account;
1797 _ACCOUNT_FREE(data->access_token);
1798 data->access_token = _account_get_text(access_token);
1800 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1802 return ACCOUNT_ERROR_NONE;
1805 ACCOUNT_API int account_set_user_text(account_h account, int index, const char *user_txt)
1808 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1809 return ACCOUNT_ERROR_INVALID_PARAMETER;
1813 ACCOUNT_ERROR("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1814 return ACCOUNT_ERROR_INVALID_PARAMETER;
1816 if (index >= USER_TXT_CNT || index < 0) {
1817 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1818 return ACCOUNT_ERROR_INVALID_PARAMETER;
1821 account_s *data = (account_s*)account;
1823 _ACCOUNT_FREE(data->user_data_txt[index]);
1824 data->user_data_txt[index] = _account_get_text(user_txt);
1826 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1828 return ACCOUNT_ERROR_NONE;
1831 ACCOUNT_API int account_set_custom(account_h account, const char* key, const char* value)
1834 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1835 return ACCOUNT_ERROR_INVALID_PARAMETER;
1839 ACCOUNT_ERROR("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__);
1840 return ACCOUNT_ERROR_INVALID_PARAMETER;
1844 ACCOUNT_ERROR("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__);
1845 return ACCOUNT_ERROR_INVALID_PARAMETER;
1848 account_s *data = (account_s*)account;
1851 bool b_is_new = TRUE;
1853 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1855 account_custom_s* custom_data = NULL;
1856 custom_data = (account_custom_s*)iter->data;
1857 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
1859 if(!strcmp(custom_data->key, key)) {
1860 _ACCOUNT_FREE(custom_data->value);
1861 custom_data->value = _account_get_text(value);
1867 account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1869 if (custom_data == NULL) {
1870 ACCOUNT_FATAL("(%s)-(%d) MALLOC FAIL\n", __FUNCTION__, __LINE__);
1871 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1873 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1874 custom_data->account_id = data->id;
1875 custom_data->app_id = _account_get_text(data->package_name);
1876 custom_data->key = _account_get_text(key);
1877 custom_data->value = _account_get_text(value);
1878 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1881 return ACCOUNT_ERROR_NONE;
1884 ACCOUNT_API int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1886 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1888 if ( (auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
1889 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1890 return ACCOUNT_ERROR_INVALID_PARAMETER;
1893 account_s *data = (account_s*)account;
1895 data->auth_type = (int)auth_type;
1897 return ACCOUNT_ERROR_NONE;
1900 ACCOUNT_API int account_set_secret(account_h account, const account_secrecy_state_e secret)
1902 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1904 if ( (secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
1905 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1906 return ACCOUNT_ERROR_INVALID_PARAMETER;
1909 account_s *data = (account_s*)account;
1911 data->secret = (int)secret;
1913 return ACCOUNT_ERROR_NONE;
1916 ACCOUNT_API int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
1918 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1920 if ( (sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) {
1921 ACCOUNT_ERROR("(%s)-(%d) sync_support is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1922 return ACCOUNT_ERROR_INVALID_PARAMETER;
1925 account_s *data = (account_s*)account;
1927 data->sync_support= (int)sync_support;
1929 return ACCOUNT_ERROR_NONE;
1932 ACCOUNT_API int account_set_user_int(account_h account, int index, const int user_int)
1935 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1936 return ACCOUNT_ERROR_INVALID_PARAMETER;
1939 if (index >= USER_INT_CNT ||index < 0) {
1940 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1941 return ACCOUNT_ERROR_INVALID_PARAMETER;
1944 account_s *data = (account_s*)account;
1946 data->user_data_int[index] = user_int;
1948 return ACCOUNT_ERROR_NONE;
1951 ACCOUNT_API int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value)
1953 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null"));
1954 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null"));
1956 if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
1957 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1958 return ACCOUNT_ERROR_INVALID_PARAMETER;
1961 account_s *data = (account_s*)account;
1963 GSList *iter = NULL;
1964 bool b_is_new = TRUE;
1966 for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1967 account_capability_s *cap_data = NULL;
1968 cap_data = (account_capability_s*)iter->data;
1970 if(!strcmp(cap_data->type, capability_type)) {
1971 cap_data->value = capability_value;
1978 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1980 if (cap_data == NULL)
1981 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1982 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1984 cap_data->type = _account_get_text(capability_type);
1985 cap_data->value = capability_value;
1986 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1989 return ACCOUNT_ERROR_NONE;
1992 ACCOUNT_API int account_get_user_name(account_h account, char **user_name)
1995 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1996 return ACCOUNT_ERROR_INVALID_PARAMETER;
2000 ACCOUNT_ERROR("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
2001 return ACCOUNT_ERROR_INVALID_PARAMETER;
2004 account_s *data = (account_s*)account;
2006 (*user_name) = NULL;
2007 *user_name = _account_get_text(data->user_name);
2009 return ACCOUNT_ERROR_NONE;
2012 ACCOUNT_API int account_get_display_name(account_h account, char **display_name)
2015 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2016 return ACCOUNT_ERROR_INVALID_PARAMETER;
2019 if (!display_name) {
2020 ACCOUNT_ERROR("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
2021 return ACCOUNT_ERROR_INVALID_PARAMETER;
2024 account_s *data = (account_s*)account;
2026 (*display_name) = NULL;
2028 *display_name = _account_get_text(data->display_name);
2030 return ACCOUNT_ERROR_NONE;
2033 ACCOUNT_API int account_get_email_address(account_h account,char **email_address)
2036 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2037 return ACCOUNT_ERROR_INVALID_PARAMETER;
2040 if (!email_address) {
2041 ACCOUNT_ERROR("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
2042 return ACCOUNT_ERROR_INVALID_PARAMETER;
2045 account_s *data = (account_s*)account;
2047 (*email_address) = NULL;
2049 *email_address = _account_get_text(data->email_address);
2051 return ACCOUNT_ERROR_NONE;
2054 ACCOUNT_API int account_get_icon_path(account_h account, char **icon_path)
2057 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2058 return ACCOUNT_ERROR_INVALID_PARAMETER;
2062 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
2063 return ACCOUNT_ERROR_INVALID_PARAMETER;
2066 account_s *data = (account_s*)account;
2068 (*icon_path) = NULL;
2070 *icon_path = _account_get_text(data->icon_path);
2072 return ACCOUNT_ERROR_NONE;
2075 ACCOUNT_API int account_get_source(account_h account, char **source)
2078 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2079 return ACCOUNT_ERROR_INVALID_PARAMETER;
2083 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
2084 return ACCOUNT_ERROR_INVALID_PARAMETER;
2087 account_s *data = (account_s*)account;
2091 *source = _account_get_text(data->source);
2093 return ACCOUNT_ERROR_NONE;
2096 ACCOUNT_API int account_get_package_name(account_h account, char **package_name)
2099 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2100 return ACCOUNT_ERROR_INVALID_PARAMETER;
2103 if (!package_name) {
2104 ACCOUNT_ERROR("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
2105 return ACCOUNT_ERROR_INVALID_PARAMETER;
2108 account_s *data = (account_s*)account;
2110 (*package_name) = NULL;
2112 *package_name = _account_get_text(data->package_name);
2114 return ACCOUNT_ERROR_NONE;
2117 ACCOUNT_API int account_get_domain_name(account_h account, char **domain_name)
2120 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2121 return ACCOUNT_ERROR_INVALID_PARAMETER;
2125 ACCOUNT_ERROR("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
2126 return ACCOUNT_ERROR_INVALID_PARAMETER;
2129 account_s *data = (account_s*)account;
2131 (*domain_name) = NULL;
2133 *domain_name = _account_get_text(data->domain_name);
2135 return ACCOUNT_ERROR_NONE;
2138 ACCOUNT_API int account_get_access_token(account_h account, char **access_token)
2141 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2142 return ACCOUNT_ERROR_INVALID_PARAMETER;
2145 if (!access_token) {
2146 ACCOUNT_ERROR("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
2147 return ACCOUNT_ERROR_INVALID_PARAMETER;
2150 account_s *data = (account_s*)account;
2152 (*access_token) = NULL;
2154 *access_token = _account_get_text(data->access_token);
2156 return ACCOUNT_ERROR_NONE;
2159 ACCOUNT_API int account_get_user_text(account_h account, int user_text_index, char **text)
2162 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2163 return ACCOUNT_ERROR_INVALID_PARAMETER;
2167 ACCOUNT_ERROR("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
2168 return ACCOUNT_ERROR_INVALID_PARAMETER;
2170 ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2172 account_s *data = (account_s*)account;
2176 *text = _account_get_text(data->user_data_txt[user_text_index]);
2178 return ACCOUNT_ERROR_NONE;
2181 ACCOUNT_API int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
2184 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2185 return ACCOUNT_ERROR_INVALID_PARAMETER;
2188 ACCOUNT_ERROR("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
2189 return ACCOUNT_ERROR_INVALID_PARAMETER;
2192 account_s* data = (account_s*)account;
2194 *auth_type = data->auth_type;
2196 return ACCOUNT_ERROR_NONE;
2199 ACCOUNT_API int account_get_secret(account_h account, account_secrecy_state_e *secret)
2202 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2203 return ACCOUNT_ERROR_INVALID_PARAMETER;
2206 ACCOUNT_ERROR("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
2207 return ACCOUNT_ERROR_INVALID_PARAMETER;
2210 account_s* data = (account_s*)account;
2212 *secret = data->secret;
2214 return ACCOUNT_ERROR_NONE;
2217 ACCOUNT_API int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
2220 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2221 return ACCOUNT_ERROR_INVALID_PARAMETER;
2223 if (!sync_support) {
2224 ACCOUNT_ERROR("(%s)-(%d) sync_support is NULL.\n", __FUNCTION__, __LINE__);
2225 return ACCOUNT_ERROR_INVALID_PARAMETER;
2228 account_s* data = (account_s*)account;
2230 *sync_support = data->sync_support;
2232 return ACCOUNT_ERROR_NONE;
2235 ACCOUNT_API int account_get_account_id(account_h account, int *account_id)
2238 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2239 return ACCOUNT_ERROR_INVALID_PARAMETER;
2242 ACCOUNT_ERROR("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
2243 return ACCOUNT_ERROR_INVALID_PARAMETER;
2246 account_s *data = (account_s*)account;
2248 *account_id = data->id;
2250 return ACCOUNT_ERROR_NONE;
2253 ACCOUNT_API int account_get_user_int(account_h account, int user_int_index, int *integer)
2256 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2257 return ACCOUNT_ERROR_INVALID_PARAMETER;
2260 ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2263 ACCOUNT_ERROR("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
2264 return ACCOUNT_ERROR_INVALID_PARAMETER;
2267 account_s *data = (account_s*)account;
2269 *integer = data->user_data_int[user_int_index];
2271 return ACCOUNT_ERROR_NONE;
2274 ACCOUNT_API int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value)
2276 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2277 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL"));
2278 ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL"));
2281 account_s *data = (account_s*)account;
2283 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2284 account_capability_s *cap_data = NULL;
2286 cap_data = (account_capability_s*)iter->data;
2288 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2290 if(!strcmp(capability_type, cap_data->type)) {
2291 *capability_value = cap_data->value;
2292 return ACCOUNT_ERROR_NONE;
2296 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2299 ACCOUNT_API int account_get_capability_all(account_h account, capability_cb cb_func, void *user_data)
2301 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2302 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2305 account_s *data = (account_s*)account;
2307 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2308 account_capability_s *cap_data = NULL;
2310 cap_data = (account_capability_s*)iter->data;
2312 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2314 //cb_func(cap_data->type, cap_data->value, user_data);
2315 if(cb_func(cap_data->type, cap_data->value, user_data)!=TRUE){
2316 ACCOUNT_VERBOSE("account_get_capability :: cb_func returns false, it is stopped\n");
2317 return ACCOUNT_ERROR_NONE;
2321 return ACCOUNT_ERROR_NONE;
2324 ACCOUNT_API int account_get_custom(account_h account, const char* key, char** value)
2326 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2327 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST"));
2328 ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL"));
2331 account_s *data = (account_s*)account;
2333 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2334 account_custom_s *custom_data = NULL;
2336 custom_data = (account_custom_s*)iter->data;
2338 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2340 if(!strcmp(key, custom_data->key)) {
2342 *value = _account_get_text(custom_data->value);
2343 return ACCOUNT_ERROR_NONE;
2347 ACCOUNT_INFO("key is not found %s", key);
2349 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2352 ACCOUNT_API int account_get_custom_all(account_h account, account_custom_cb cb_func, void* user_data)
2354 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2355 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2358 account_s *data = (account_s*)account;
2360 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2361 bool cb_ret = FALSE;
2362 account_custom_s *custom_data = NULL;
2364 custom_data = (account_custom_s*)iter->data;
2366 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2368 cb_ret = cb_func(custom_data->key, custom_data->value, user_data);
2370 ACCOUNT_INFO("account_get_custom_all callback func ret = %d", cb_ret);
2375 return ACCOUNT_ERROR_NONE;
2378 ACCOUNT_API int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
2380 int error_code = ACCOUNT_ERROR_NONE;
2381 account_stmt hstmt = NULL;
2382 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2385 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2386 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2387 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2389 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2391 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2392 hstmt = _account_prepare_query(query);
2394 rc = _account_query_step(hstmt);
2395 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2397 account_capability_s* capability_record = NULL;
2399 while (rc == SQLITE_ROW) {
2400 bool cb_ret = FALSE;
2401 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2403 if (capability_record == NULL) {
2404 ACCOUNT_FATAL("malloc Failed");
2408 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2410 _account_convert_column_to_capability(hstmt, capability_record);
2412 cb_ret = cb_func(capability_record->type, capability_record->value, user_data);
2414 _account_free_capability_items(capability_record);
2415 _ACCOUNT_FREE(capability_record);
2417 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2419 rc = _account_query_step(hstmt);
2422 _account_query_finalize(hstmt);
2425 error_code = ACCOUNT_ERROR_NONE;
2428 if (hstmt != NULL) {
2429 _account_query_finalize(hstmt);
2433 pthread_mutex_unlock(&account_mutex);
2437 static int _account_compare_old_record(account_s *new_account, int account_id)
2439 int error_code = ACCOUNT_ERROR_NONE;
2440 account_stmt hstmt = NULL;
2441 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2443 account_s *old_account = NULL;
2445 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2446 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2447 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2449 old_account = (account_s*)calloc(1, sizeof(account_s));
2451 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2453 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2454 hstmt = _account_prepare_query(query);
2456 rc = _account_query_step(hstmt);
2457 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2459 while (rc == SQLITE_ROW) {
2460 _account_convert_column_to_account(hstmt, old_account);
2461 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
2462 rc = _account_query_step(hstmt);
2465 _account_query_finalize(hstmt);
2469 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2470 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2473 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2474 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2478 new_account->id = old_account->id;
2481 if(!new_account->user_name) {
2482 if(old_account->user_name)
2483 new_account->user_name = _account_get_text(old_account->user_name);
2487 if(!new_account->display_name) {
2488 if(old_account->display_name)
2489 new_account->display_name = _account_get_text(old_account->display_name);
2493 if(!new_account->email_address) {
2494 if(old_account->email_address)
2495 new_account->email_address = _account_get_text(old_account->email_address);
2499 if(!new_account->domain_name) {
2500 if(old_account->domain_name)
2501 new_account->domain_name = _account_get_text(old_account->domain_name);
2505 if(!new_account->icon_path) {
2506 if(old_account->icon_path)
2507 new_account->icon_path = _account_get_text(old_account->icon_path);
2511 if(!new_account->source) {
2512 if(old_account->source)
2513 new_account->source = _account_get_text(old_account->source);
2516 _ACCOUNT_FREE(new_account->package_name);
2517 new_account->package_name = _account_get_text(old_account->package_name);
2520 if(!new_account->access_token) {
2521 if(old_account->access_token)
2522 new_account->access_token = _account_get_text(old_account->access_token);
2527 for(i=0;i<USER_TXT_CNT;i++) {
2528 if(!new_account->user_data_txt[i]) {
2529 if(old_account->user_data_txt[i])
2530 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2535 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2536 new_account->auth_type = old_account->auth_type;
2540 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2541 new_account->secret = old_account->secret;
2545 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2546 new_account->sync_support = old_account->sync_support;
2550 for(i=0;i<USER_INT_CNT;i++) {
2551 if(new_account->user_data_int[i] == 0) {
2552 new_account->user_data_int[i] = old_account->user_data_int[i];
2558 // user custom table
2563 if (hstmt != NULL) {
2564 _account_query_finalize(hstmt);
2569 _account_free_account_items(old_account);
2570 _ACCOUNT_FREE(old_account);
2573 return ACCOUNT_ERROR_NONE;
2576 static int _account_update_account(account_s *account, int account_id)
2578 int rc = 0, binding_count =0;
2579 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2580 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2581 account_stmt hstmt = NULL;
2583 if (!account->package_name) {
2584 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2585 return ACCOUNT_ERROR_INVALID_PARAMETER;
2588 char* current_appid = NULL;
2589 char* verified_appid = NULL;
2591 current_appid = _account_get_current_appid();
2592 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
2594 _ACCOUNT_FREE(current_appid);
2595 _ACCOUNT_FREE(verified_appid);
2597 if(error_code != ACCOUNT_ERROR_NONE){
2598 ACCOUNT_ERROR("No permission to update\n");
2599 return ACCOUNT_ERROR_PERMISSION_DENIED;
2602 _account_compare_old_record(account, account_id);
2604 if (!account->user_name && !account->display_name && !account->email_address) {
2605 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2606 return ACCOUNT_ERROR_INVALID_PARAMETER;
2609 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2611 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2613 count = _account_get_record_count(query);
2615 ACCOUNT_WARNING(" Account record not found, count = %d\n", count);
2616 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2619 /* transaction control required*/
2620 ret_transaction = _account_begin_transaction();
2622 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2623 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2624 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2625 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2626 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2628 hstmt = _account_prepare_query(query);
2629 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)));
2631 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2632 _account_query_bind_int(hstmt, binding_count++, account_id);
2634 rc = _account_query_step(hstmt);
2635 if (rc != SQLITE_DONE) {
2636 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2639 _account_query_finalize(hstmt);
2642 /*update capability*/
2643 error_code = _account_update_capability(account, account_id);
2644 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2645 ret_transaction = _account_end_transaction(FALSE);
2646 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2651 error_code = _account_update_custom(account, account_id);
2652 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2653 ret_transaction = _account_end_transaction(FALSE);
2654 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2658 ret_transaction = _account_end_transaction(TRUE);
2659 ACCOUNT_DEBUG("update capability success, trying to commit(%x) !!!\n", ret_transaction);
2664 ACCOUNT_API int account_update_to_db_by_id(const account_h account, int account_id)
2666 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2667 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2668 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2669 int error_code = ACCOUNT_ERROR_NONE;
2670 account_s* data = (account_s*)account;
2672 pthread_mutex_lock(&account_mutex);
2674 error_code = _account_update_account(data, account_id);
2676 if(error_code != ACCOUNT_ERROR_NONE) {
2677 pthread_mutex_unlock(&account_mutex);
2681 pthread_mutex_unlock(&account_mutex);
2684 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2685 _account_insert_delete_update_notification_send(buf);
2687 return ACCOUNT_ERROR_NONE;
2690 ACCOUNT_API int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
2692 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2693 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2694 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2695 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2697 int error_code = ACCOUNT_ERROR_NONE;
2698 account_s *data = (account_s*)account;
2700 pthread_mutex_lock(&account_mutex);
2702 error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
2704 pthread_mutex_unlock(&account_mutex);
2707 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2708 _account_insert_delete_update_notification_send(buf);
2713 ACCOUNT_API int account_foreach_account_from_db(account_cb callback, void *user_data)
2715 int error_code = ACCOUNT_ERROR_NONE;
2716 account_stmt hstmt = NULL;
2717 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2719 GList *account_list = NULL;
2721 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
2722 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2724 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2726 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2727 hstmt = _account_prepare_query(query);
2729 rc = _account_query_step(hstmt);
2731 account_s *account_record = NULL;
2733 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2736 while(rc == SQLITE_ROW) {
2737 account_record = (account_s*) malloc(sizeof(account_s));
2739 if (account_record == NULL) {
2740 ACCOUNT_FATAL("malloc Failed");
2744 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2745 _account_convert_column_to_account(hstmt, account_record);
2746 account_list = g_list_append(account_list, account_record);
2747 rc = _account_query_step(hstmt);
2751 _account_query_finalize(hstmt);
2756 for (iter = account_list; iter != NULL; iter = g_list_next(iter)) {
2757 account_s *account = NULL;
2758 account = (account_s*)iter->data;
2759 account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2760 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2761 callback((account_h)account, user_data);
2765 error_code = ACCOUNT_ERROR_NONE;
2768 if (hstmt != NULL) {
2769 _account_query_finalize(hstmt);
2773 _account_glist_free(account_list);
2774 account_list = NULL;
2780 ACCOUNT_API int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status)
2782 int error_code = ACCOUNT_ERROR_NONE;
2783 account_stmt hstmt = NULL;
2784 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2788 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2789 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2790 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2791 ACCOUNT_ERROR("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2792 return ACCOUNT_ERROR_INVALID_PARAMETER;
2795 pthread_mutex_lock(&account_mutex);
2797 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2799 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2801 rc = _account_get_record_count(query);
2804 ACCOUNT_ERROR( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2805 pthread_mutex_unlock(&account_mutex);
2806 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2809 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2811 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2812 hstmt = _account_prepare_query(query);
2814 _account_query_bind_int(hstmt, count, (int)sync_status);
2816 rc = _account_query_step(hstmt);
2818 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2819 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2821 _account_query_finalize(hstmt);
2824 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2825 _account_insert_delete_update_notification_send(buf);
2828 error_code = ACCOUNT_ERROR_NONE;
2831 if (hstmt != NULL) {
2832 _account_query_finalize(hstmt);
2836 pthread_mutex_unlock(&account_mutex);
2840 ACCOUNT_API int account_query_account_by_account_id(int account_db_id, account_h *account)
2842 int error_code = ACCOUNT_ERROR_NONE;
2843 account_stmt hstmt = NULL;
2844 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2847 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2848 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2849 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2851 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2853 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2854 hstmt = _account_prepare_query(query);
2856 rc = _account_query_step(hstmt);
2857 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2859 account_s *account_record = (account_s *)(*account);
2861 while (rc == SQLITE_ROW) {
2862 _account_convert_column_to_account(hstmt, account_record);
2863 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
2864 rc = _account_query_step(hstmt);
2867 _account_query_finalize(hstmt);
2868 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2869 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2872 error_code = ACCOUNT_ERROR_NONE;
2875 if (hstmt != NULL) {
2876 _account_query_finalize(hstmt);
2880 pthread_mutex_unlock(&account_mutex);
2884 ACCOUNT_API int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
2886 int error_code = ACCOUNT_ERROR_NONE;
2887 account_stmt hstmt = NULL;
2888 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2891 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2892 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2894 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2896 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2898 hstmt = _account_prepare_query(query);
2900 int binding_count = 1;
2901 _account_query_bind_text(hstmt, binding_count++, user_name);
2903 rc = _account_query_step(hstmt);
2905 account_s *account_head = NULL;
2907 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2911 account_head = (account_s*) malloc(sizeof(account_s));
2912 if (account_head == NULL) {
2913 ACCOUNT_FATAL("malloc Failed");
2914 if (hstmt != NULL) {
2915 _account_query_finalize(hstmt);
2918 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2920 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2922 while (rc == SQLITE_ROW) {
2923 account_s* account_record = NULL;
2925 account_record = (account_s*) malloc(sizeof(account_s));
2927 if (account_record == NULL) {
2928 ACCOUNT_FATAL("malloc Failed");
2931 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2933 _account_convert_column_to_account(hstmt, account_record);
2935 account_head->account_list = g_list_append(account_head->account_list, account_record);
2937 rc = _account_query_step(hstmt);
2939 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2942 _account_query_finalize(hstmt);
2948 tmp = g_list_length(account_head->account_list);
2949 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2951 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2953 account = (account_h)iter->data;
2955 account_s *testaccount = (account_s*)account;
2957 ACCOUNT_VERBOSE("id = %d", testaccount->id);
2958 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
2959 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
2960 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
2961 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
2962 ACCOUNT_VERBOSE("source = %s", testaccount->source);
2963 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
2964 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
2965 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
2966 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
2967 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
2968 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
2970 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2971 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2973 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2975 //cb_func(account, user_data);
2976 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("cb_func returns false, it is stopped.\n"));
2980 error_code = ACCOUNT_ERROR_NONE;
2983 if (hstmt != NULL) {
2984 _account_query_finalize(hstmt);
2988 if (account_head->account_list) {
2989 _account_glist_free(account_head->account_list);
2990 account_head->account_list = NULL;
2991 _account_free_account_items(account_head);
2993 _ACCOUNT_FREE(account_head);
2996 pthread_mutex_unlock(&account_mutex);
3000 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)
3002 int error_code = ACCOUNT_ERROR_NONE;
3003 account_stmt hstmt = NULL;
3004 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3007 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3009 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
3010 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
3011 return ACCOUNT_ERROR_INVALID_PARAMETER;
3014 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3015 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3017 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3019 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3021 hstmt = _account_prepare_query(query);
3023 int binding_count = 1;
3024 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3025 _account_query_bind_int(hstmt, binding_count++, (int)capability_value);
3027 rc = _account_query_step(hstmt);
3029 account_s* account_head = NULL;
3031 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3035 account_head = (account_s*) malloc(sizeof(account_s));
3036 if (account_head == NULL) {
3037 ACCOUNT_FATAL("malloc Failed");
3038 if (hstmt != NULL) {
3039 _account_query_finalize(hstmt);
3042 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3044 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3046 while (rc == SQLITE_ROW) {
3047 account_s* account_record = NULL;
3049 account_record = (account_s*) malloc(sizeof(account_s));
3051 if (account_record == NULL) {
3052 ACCOUNT_FATAL("malloc Failed");
3055 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3057 _account_convert_column_to_account(hstmt, account_record);
3059 account_head->account_list = g_list_append(account_head->account_list, account_record);
3061 rc = _account_query_step(hstmt);
3063 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3066 _account_query_finalize(hstmt);
3072 tmp = g_list_length(account_head->account_list);
3073 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3075 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3076 account_h account = NULL;
3077 account = (account_h)iter->data;
3078 account_s* testaccount = (account_s*)account;
3080 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3081 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3083 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3085 //cb_func(account, user_data);
3086 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("cb_func returns false, it is stopped.\n"));
3090 error_code = ACCOUNT_ERROR_NONE;
3093 if (hstmt != NULL) {
3094 _account_query_finalize(hstmt);
3098 if (account_head->account_list) {
3099 _account_glist_free(account_head->account_list);
3100 account_head->account_list = NULL;
3101 _account_free_account_items(account_head);
3103 _ACCOUNT_FREE(account_head);
3106 pthread_mutex_unlock(&account_mutex);
3110 ACCOUNT_API int account_query_account_by_capability_type(account_cb cb_func, const char* capability_type, void* user_data)
3112 int error_code = ACCOUNT_ERROR_NONE;
3113 account_stmt hstmt = NULL;
3114 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3117 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3118 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3119 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3121 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3123 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3125 hstmt = _account_prepare_query(query);
3127 int binding_count = 1;
3128 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3130 rc = _account_query_step(hstmt);
3132 account_s* account_head = NULL;
3134 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3138 account_head = (account_s*) malloc(sizeof(account_s));
3139 if (account_head == NULL) {
3140 ACCOUNT_FATAL("malloc Failed");
3141 if (hstmt != NULL) {
3142 _account_query_finalize(hstmt);
3145 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3147 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3149 while (rc == SQLITE_ROW) {
3150 account_s* account_record = NULL;
3152 account_record = (account_s*) malloc(sizeof(account_s));
3154 if (account_record == NULL) {
3155 ACCOUNT_FATAL("malloc Failed");
3158 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3160 _account_convert_column_to_account(hstmt, account_record);
3162 account_head->account_list = g_list_append(account_head->account_list, account_record);
3164 rc = _account_query_step(hstmt);
3166 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3169 _account_query_finalize(hstmt);
3175 tmp = g_list_length(account_head->account_list);
3176 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3178 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3179 account_h account = NULL;
3180 account = (account_h)iter->data;
3181 account_s* testaccount = (account_s*)account;
3183 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3184 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3186 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3188 //cb_func(account, user_data);
3189 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n"));
3193 error_code = ACCOUNT_ERROR_NONE;
3196 if (hstmt != NULL) {
3197 _account_query_finalize(hstmt);
3201 if (account_head->account_list) {
3202 _account_glist_free(account_head->account_list);
3203 account_head->account_list = NULL;
3204 _account_free_account_items(account_head);
3206 _ACCOUNT_FREE(account_head);
3209 pthread_mutex_unlock(&account_mutex);
3213 ACCOUNT_API int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
3215 int error_code = ACCOUNT_ERROR_NONE;
3216 account_stmt hstmt = NULL;
3217 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3220 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
3221 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3222 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3224 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3226 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3228 hstmt = _account_prepare_query(query);
3230 int binding_count = 1;
3231 _account_query_bind_text(hstmt, binding_count++, package_name);
3233 rc = _account_query_step(hstmt);
3235 account_s* account_head = NULL;
3237 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3241 account_head = (account_s*) malloc(sizeof(account_s));
3242 if (account_head == NULL) {
3243 ACCOUNT_FATAL("malloc Failed");
3244 if (hstmt != NULL) {
3245 _account_query_finalize(hstmt);
3248 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3250 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3252 while (rc == SQLITE_ROW) {
3253 account_s* account_record = NULL;
3255 account_record = (account_s*) malloc(sizeof(account_s));
3257 if (account_record == NULL) {
3258 ACCOUNT_FATAL("malloc Failed");
3261 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3263 _account_convert_column_to_account(hstmt, account_record);
3265 account_head->account_list = g_list_append(account_head->account_list, account_record);
3267 rc = _account_query_step(hstmt);
3269 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3272 _account_query_finalize(hstmt);
3277 tmp = g_list_length(account_head->account_list);
3278 ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
3280 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3281 account_h account = NULL;
3282 account = (account_h)iter->data;
3284 account_s* testaccount = (account_s*)account;
3286 ACCOUNT_VERBOSE("id = %d", testaccount->id);
3287 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
3288 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
3289 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
3290 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
3291 ACCOUNT_VERBOSE("source = %s", testaccount->source);
3292 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
3293 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
3294 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
3295 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
3296 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
3297 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
3299 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3300 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3302 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3304 //cb_func(account, user_data);
3305 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n"));
3309 error_code = ACCOUNT_ERROR_NONE;
3312 if (hstmt != NULL) {
3313 _account_query_finalize(hstmt);
3317 if (account_head->account_list) {
3318 _account_glist_free(account_head->account_list);
3319 account_head->account_list = NULL;
3320 _account_free_account_items(account_head);
3322 _ACCOUNT_FREE(account_head);
3325 pthread_mutex_unlock(&account_mutex);
3329 ACCOUNT_API int account_delete(int account_id)
3331 int error_code = ACCOUNT_ERROR_NONE;
3332 account_stmt hstmt = NULL;
3333 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3335 int ret_transaction = 0;
3336 bool is_success = FALSE;
3338 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3341 account_h account = NULL;
3342 /* Check requested ID to delete */
3343 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3345 count = _account_get_record_count(query);
3347 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3348 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3351 /* Check permission of requested appid */
3352 account_create(&account);
3353 account_query_account_by_account_id(account_id, &account);
3355 char* current_appid = NULL;
3356 char* verified_appid = NULL;
3358 current_appid = _account_get_current_appid();
3359 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
3361 _ACCOUNT_FREE(current_appid);
3362 _ACCOUNT_FREE(verified_appid);
3364 if(error_code != ACCOUNT_ERROR_NONE){
3365 ACCOUNT_ERROR("No permission to delete\n");
3366 return ACCOUNT_ERROR_PERMISSION_DENIED;
3369 account_destroy(account);
3371 /* transaction control required*/
3372 ret_transaction = _account_begin_transaction();
3374 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3375 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3376 pthread_mutex_unlock(&account_mutex);
3377 return ret_transaction;
3380 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3381 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3383 hstmt = _account_prepare_query(query);
3384 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3385 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3387 rc = _account_query_step(hstmt);
3388 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3390 _account_query_finalize(hstmt);
3393 ACCOUNT_MEMSET(query, 0, sizeof(query));
3395 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3397 hstmt = _account_prepare_query(query);
3398 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3399 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3401 rc = _account_query_step(hstmt);
3402 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3404 _account_query_finalize(hstmt);
3407 /* delete custom data */
3408 ACCOUNT_MEMSET(query, 0, sizeof(query));
3410 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
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()));
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));
3419 _account_query_finalize(hstmt);
3424 if (hstmt != NULL) {
3425 _account_query_finalize(hstmt);
3429 ret_transaction = _account_end_transaction(is_success);
3431 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3432 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3434 if (is_success == true) {
3436 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3437 _account_insert_delete_update_notification_send(buf);
3441 pthread_mutex_unlock(&account_mutex);
3447 ACCOUNT_API int account_delete_from_db_by_id(int account_db_id)
3449 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
3451 return account_delete(account_db_id);
3454 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3456 int error_code = ACCOUNT_ERROR_NONE;
3457 account_stmt hstmt = NULL;
3458 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3460 int binding_count = 1;
3462 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3463 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3464 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3465 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3467 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3469 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3470 hstmt = _account_prepare_query(query);
3472 _account_query_bind_text(hstmt, binding_count++, username);
3473 _account_query_bind_text(hstmt, binding_count++, package_name);
3475 rc = _account_query_step(hstmt);
3476 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3478 account_s *account_record = (account_s *)(*account);
3480 while (rc == SQLITE_ROW) {
3481 _account_convert_column_to_account(hstmt, account_record);
3482 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
3483 rc = _account_query_step(hstmt);
3486 _account_query_finalize(hstmt);
3487 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3488 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3491 error_code = ACCOUNT_ERROR_NONE;
3494 if (hstmt != NULL) {
3495 _account_query_finalize(hstmt);
3499 pthread_mutex_unlock(&account_mutex);
3503 ACCOUNT_API int account_delete_from_db_by_user_name(char *user_name, char *package_name)
3505 int error_code = ACCOUNT_ERROR_NONE;
3506 account_stmt hstmt = NULL;
3507 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3509 int ret_transaction = 0;
3510 bool is_success = FALSE;
3511 account_h account = NULL;
3512 int binding_count = 1;
3513 int account_id = -1;
3515 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3516 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3517 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3519 rc = account_create(&account);
3520 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3522 rc = account_get_account_id(account, &account_id);
3524 rc = account_destroy(account);
3526 /* transaction control required*/
3527 ret_transaction = _account_begin_transaction();
3529 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3530 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3531 pthread_mutex_unlock(&account_mutex);
3532 return ret_transaction;
3535 /* delete custom data */
3536 ACCOUNT_MEMSET(query, 0, sizeof(query));
3537 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3539 hstmt = _account_prepare_query(query);
3541 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3542 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3544 _account_query_bind_int(hstmt, binding_count++, account_id);
3546 rc = _account_query_step(hstmt);
3547 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3549 _account_query_finalize(hstmt);
3552 /* delete capability */
3553 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3555 hstmt = _account_prepare_query(query);
3557 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3558 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3561 _account_query_bind_text(hstmt, binding_count++, user_name);
3562 _account_query_bind_text(hstmt, binding_count++, package_name);
3564 rc = _account_query_step(hstmt);
3565 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3567 _account_query_finalize(hstmt);
3570 ACCOUNT_MEMSET(query, 0, sizeof(query));
3572 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3574 hstmt = _account_prepare_query(query);
3575 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3576 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3579 _account_query_bind_text(hstmt, binding_count++, user_name);
3580 _account_query_bind_text(hstmt, binding_count++, package_name);
3582 rc = _account_query_step(hstmt);
3583 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));
3585 _account_query_finalize(hstmt);
3591 if (hstmt != NULL) {
3592 _account_query_finalize(hstmt);
3596 ret_transaction = _account_end_transaction(is_success);
3598 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3599 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3601 if (is_success == true) {
3603 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3604 _account_insert_delete_update_notification_send(buf);
3608 pthread_mutex_unlock(&account_mutex);
3613 ACCOUNT_API int account_delete_from_db_by_package_name(char *package_name)
3615 int error_code = ACCOUNT_ERROR_NONE;
3616 account_stmt hstmt = NULL;
3617 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3619 int ret_transaction = 0;
3620 bool is_success = FALSE;
3621 int binding_count = 1;
3623 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3624 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3626 /* transaction control required*/
3627 ret_transaction = _account_begin_transaction();
3629 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3630 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3631 pthread_mutex_unlock(&account_mutex);
3632 return ret_transaction;
3635 /* delete custom table */
3636 ACCOUNT_MEMSET(query, 0, sizeof(query));
3637 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3639 hstmt = _account_prepare_query(query);
3641 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3642 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3645 _account_query_bind_text(hstmt, binding_count++, package_name);
3647 rc = _account_query_step(hstmt);
3648 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3650 _account_query_finalize(hstmt);
3653 /* delete capability table */
3654 ACCOUNT_MEMSET(query, 0, sizeof(query));
3655 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3657 hstmt = _account_prepare_query(query);
3659 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3660 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3663 _account_query_bind_text(hstmt, binding_count++, package_name);
3665 rc = _account_query_step(hstmt);
3666 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3668 _account_query_finalize(hstmt);
3671 /* delete account table */
3672 ACCOUNT_MEMSET(query, 0, sizeof(query));
3674 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3676 hstmt = _account_prepare_query(query);
3677 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3678 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3681 _account_query_bind_text(hstmt, binding_count++, package_name);
3683 rc = _account_query_step(hstmt);
3684 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));
3686 _account_query_finalize(hstmt);
3692 if (hstmt != NULL) {
3693 _account_query_finalize(hstmt);
3697 ret_transaction = _account_end_transaction(is_success);
3699 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3700 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3702 if (is_success == true) {
3704 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, -1);
3705 _account_insert_delete_update_notification_send(buf);
3709 pthread_mutex_unlock(&account_mutex);
3714 ACCOUNT_API int account_get_total_count_from_db(int *count)
3717 ACCOUNT_ERROR("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3718 return ACCOUNT_ERROR_INVALID_PARAMETER;
3720 char query[1024] = {0, };
3721 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3722 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3724 *count = _account_get_record_count(query);
3727 account_stmt pStmt = NULL;
3729 assert(NULL != g_hAccountDB);
3730 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3732 rc = sqlite3_step(pStmt);
3733 if (SQLITE_ROW != rc) {
3734 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3735 sqlite3_finalize(pStmt);
3736 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3739 ncount = sqlite3_column_int(pStmt, 0);
3743 ACCOUNT_VERBOSE("Number of account : %d", ncount);
3744 sqlite3_finalize(pStmt);
3747 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3748 return ACCOUNT_ERROR_DB_FAILED;
3751 return ACCOUNT_ERROR_NONE;
3754 ACCOUNT_API int account_destroy(account_h account)
3756 account_s *data = (account_s*)account;
3758 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3760 ACCOUNT_INFO("destroy handle=%p\n", data);
3762 _account_free_account_items(data);
3763 _ACCOUNT_FREE(data);
3765 return ACCOUNT_ERROR_NONE;
3768 static int _account_type_free_label_items(label_s *data)
3770 _ACCOUNT_FREE(data->app_id);
3771 _ACCOUNT_FREE(data->label);
3772 _ACCOUNT_FREE(data->locale);
3774 return ACCOUNT_ERROR_NONE;
3777 static int _account_type_free_feature_items(provider_feature_s *data)
3779 _ACCOUNT_FREE(data->app_id);
3780 _ACCOUNT_FREE(data->key);
3782 return ACCOUNT_ERROR_NONE;
3785 static int _account_type_gslist_free(GSList* list)
3787 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
3791 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
3792 label_s *label_data = (label_s*)iter->data;
3793 _account_type_free_label_items(label_data);
3794 _ACCOUNT_FREE(label_data);
3800 return ACCOUNT_ERROR_NONE;
3803 static int _account_type_item_free(account_type_s *data)
3805 _ACCOUNT_FREE(data->app_id);
3806 _ACCOUNT_FREE(data->service_provider_id);
3807 _ACCOUNT_FREE(data->icon_path);
3808 _ACCOUNT_FREE(data->small_icon_path);
3810 return ACCOUNT_ERROR_NONE;
3813 static int _account_type_glist_free(GList* list)
3815 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
3819 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
3820 account_type_s *account_type_record = (account_type_s*)iter->data;
3821 _account_type_item_free(account_type_record);
3822 _ACCOUNT_FREE(account_type_record);
3828 return ACCOUNT_ERROR_NONE;
3831 static int _account_type_free_account_type_items(account_type_s *data)
3833 _account_type_item_free(data);
3835 _account_type_gslist_free(data->label_list);
3836 _account_type_glist_free(data->account_type_list);
3838 return ACCOUNT_ERROR_NONE;
3841 ACCOUNT_API int account_type_create(account_type_h *account_type)
3843 if (!account_type) {
3844 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3845 return ACCOUNT_ERROR_INVALID_PARAMETER;
3848 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3851 ACCOUNT_ERROR("Memory Allocation Failed");
3852 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3855 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3857 ACCOUNT_VERBOSE("create handle=%p\n", *account_type);
3859 *account_type = (account_type_h)data;
3861 return ACCOUNT_ERROR_NONE;
3864 ACCOUNT_API int account_type_destroy(account_type_h account_type)
3866 account_type_s *data = (account_type_s*)account_type;
3868 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
3870 ACCOUNT_VERBOSE("destroy handle=%p\n", data);
3872 _account_type_free_account_type_items(data);
3873 _ACCOUNT_FREE(data);
3875 return ACCOUNT_ERROR_NONE;
3878 //app_id mandatory field
3879 ACCOUNT_API int account_type_set_app_id(account_type_h account_type, const char *app_id)
3881 if (!account_type) {
3882 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3883 return ACCOUNT_ERROR_INVALID_PARAMETER;
3887 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
3888 return ACCOUNT_ERROR_INVALID_PARAMETER;
3891 account_type_s *data = (account_type_s*)account_type;
3893 _ACCOUNT_FREE(data->app_id);
3894 data->app_id = _account_get_text(app_id);
3896 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->app_id, app_id);
3897 return ACCOUNT_ERROR_NONE;
3900 //service_provider_id mandatory field
3901 ACCOUNT_API int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
3903 if (!account_type) {
3904 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3905 return ACCOUNT_ERROR_INVALID_PARAMETER;
3908 if (!service_provider_id) {
3909 ACCOUNT_ERROR("(%s)-(%d) service_provider_id is NULL.\n", __FUNCTION__, __LINE__);
3910 return ACCOUNT_ERROR_INVALID_PARAMETER;
3913 account_type_s *data = (account_type_s*)account_type;
3915 _ACCOUNT_FREE(data->service_provider_id);
3916 data->service_provider_id = _account_get_text(service_provider_id);
3918 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->service_provider_id, service_provider_id);
3919 return ACCOUNT_ERROR_NONE;
3922 ACCOUNT_API int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
3924 if (!account_type) {
3925 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3926 return ACCOUNT_ERROR_INVALID_PARAMETER;
3930 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
3931 return ACCOUNT_ERROR_INVALID_PARAMETER;
3934 account_type_s *data = (account_type_s*)account_type;
3936 _ACCOUNT_FREE(data->icon_path);
3937 data->icon_path = _account_get_text(icon_path);
3939 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
3940 return ACCOUNT_ERROR_NONE;
3943 ACCOUNT_API int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
3945 if (!account_type) {
3946 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3947 return ACCOUNT_ERROR_INVALID_PARAMETER;
3950 if (!small_icon_path) {
3951 ACCOUNT_ERROR("(%s)-(%d) small_icon_path is NULL.\n", __FUNCTION__, __LINE__);
3952 return ACCOUNT_ERROR_INVALID_PARAMETER;
3955 account_type_s *data = (account_type_s*)account_type;
3957 _ACCOUNT_FREE(data->small_icon_path);
3958 data->small_icon_path = _account_get_text(small_icon_path);
3960 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->small_icon_path, small_icon_path);
3961 return ACCOUNT_ERROR_NONE;
3964 ACCOUNT_API int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
3966 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
3968 account_type_s *data = (account_type_s*)account_type;
3970 data->multiple_account_support = multiple_account_support;
3972 return ACCOUNT_ERROR_NONE;
3976 ACCOUNT_API int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
3978 if (!account_type) {
3979 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3980 return ACCOUNT_ERROR_INVALID_PARAMETER;
3983 if(!label || !locale) {
3984 ACCOUNT_ERROR("(%s)-(%d) label(%p) or locale(%p) is NULL.\n", __FUNCTION__, __LINE__, label, locale);
3985 return ACCOUNT_ERROR_INVALID_PARAMETER;
3988 account_type_s *data = (account_type_s*)account_type;
3989 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3991 if (label_data == NULL) {
3992 ACCOUNT_FATAL("(%s)-(%d) Malloc fail. label_data is NULL.\n", __FUNCTION__, __LINE__);
3993 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3995 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3997 label_data->label = _account_get_text(label);
3998 label_data->locale = _account_get_text(locale);
4000 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4002 return ACCOUNT_ERROR_NONE;
4005 ACCOUNT_API int account_type_get_app_id(account_type_h account_type, char **app_id)
4007 if (!account_type) {
4008 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4009 return ACCOUNT_ERROR_INVALID_PARAMETER;
4013 ACCOUNT_ERROR("(%s)-(%d) app id is NULL.\n", __FUNCTION__, __LINE__);
4014 return ACCOUNT_ERROR_INVALID_PARAMETER;
4017 account_type_s *data = (account_type_s*)account_type;
4020 *app_id = _account_get_text(data->app_id);
4022 return ACCOUNT_ERROR_NONE;
4025 ACCOUNT_API int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4027 if (!account_type) {
4028 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4029 return ACCOUNT_ERROR_INVALID_PARAMETER;
4032 if (!service_provider_id) {
4033 ACCOUNT_ERROR("(%s)-(%d) service provider id is NULL.\n", __FUNCTION__, __LINE__);
4034 return ACCOUNT_ERROR_INVALID_PARAMETER;
4037 account_type_s *data = (account_type_s*)account_type;
4039 (*service_provider_id) = NULL;
4040 *service_provider_id = _account_get_text(data->service_provider_id);
4042 return ACCOUNT_ERROR_NONE;
4045 ACCOUNT_API int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4047 if (!account_type) {
4048 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4049 return ACCOUNT_ERROR_INVALID_PARAMETER;
4053 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4054 return ACCOUNT_ERROR_INVALID_PARAMETER;
4057 account_type_s *data = (account_type_s*)account_type;
4059 (*icon_path) = NULL;
4060 *icon_path = _account_get_text(data->icon_path);
4062 return ACCOUNT_ERROR_NONE;
4065 ACCOUNT_API int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4067 if (!account_type) {
4068 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4069 return ACCOUNT_ERROR_INVALID_PARAMETER;
4072 if (!small_icon_path) {
4073 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4074 return ACCOUNT_ERROR_INVALID_PARAMETER;
4077 account_type_s *data = (account_type_s*)account_type;
4079 (*small_icon_path) = NULL;
4080 *small_icon_path = _account_get_text(data->small_icon_path);
4082 return ACCOUNT_ERROR_NONE;
4085 ACCOUNT_API int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4087 if (!account_type) {
4088 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4089 return ACCOUNT_ERROR_INVALID_PARAMETER;
4091 if (!multiple_account_support) {
4092 ACCOUNT_ERROR("(%s)-(%d) multiple_account_support is NULL.\n", __FUNCTION__, __LINE__);
4093 return ACCOUNT_ERROR_INVALID_PARAMETER;
4096 account_type_s *data = (account_type_s*)account_type;
4098 *multiple_account_support = data->multiple_account_support;
4100 return ACCOUNT_ERROR_NONE;
4103 ACCOUNT_API int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4105 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4106 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4109 account_type_s *data = (account_type_s*)account_type;
4111 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4112 label_s *label_data = NULL;
4114 label_data = (label_s*)iter->data;
4116 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4120 if(!strcmp(locale, label_data->locale)) {
4121 *label = _account_get_text(label_data->label);
4122 return ACCOUNT_ERROR_NONE;
4126 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4129 ACCOUNT_API int account_type_get_label(account_type_h account_type, account_label_cb cb_func, void *user_data)
4131 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4132 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4135 account_type_s *data = (account_type_s*)account_type;
4137 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4138 label_s *label_data = NULL;
4140 label_data = (label_s*)iter->data;
4142 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4144 //cb_func(label_data->app_id, label_data->label, label_data->locale, user_data);
4145 if(cb_func(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4146 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4147 return ACCOUNT_ERROR_NONE;
4151 return ACCOUNT_ERROR_NONE;
4154 static gboolean _account_type_check_duplicated(account_type_s *data)
4156 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4159 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4161 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4162 , ACCOUNT_TYPE_TABLE, data->app_id);
4164 count = _account_get_record_count(query);
4166 ACCOUNT_VERBOSE("_account_type_check_duplicated : duplicated %d account type(s) exist!, app_id=%s, service_provider_id=%s\n",
4167 count, data->app_id, data->service_provider_id);
4174 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4178 /*Caution : Keep insert query orders.*/
4181 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4183 /* 2. service provider id*/
4184 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4187 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4189 /* 4. small icon path*/
4190 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4192 /* 5. multiple accont support*/
4193 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4199 static int _account_type_execute_insert_query(account_type_s *account_type)
4202 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4203 int error_code = ACCOUNT_ERROR_NONE;
4204 account_stmt hstmt = NULL;
4206 /* check mandatory field */
4207 // app id & service provider id
4208 if (!account_type->app_id) {
4209 ACCOUNT_ERROR("App id is mandetory field, it can not be NULL!!!!\n");
4210 return ACCOUNT_ERROR_INVALID_PARAMETER;
4213 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4214 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4215 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4217 hstmt = _account_prepare_query(query);
4218 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4220 _account_type_convert_account_to_sql(account_type, hstmt, query);
4222 rc = _account_query_step(hstmt);
4223 if (rc != SQLITE_DONE) {
4224 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4225 error_code = ACCOUNT_ERROR_DB_FAILED;
4228 _account_query_finalize(hstmt);
4234 static int _account_type_insert_label(account_type_s *account_type)
4237 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4238 account_stmt hstmt = NULL;
4240 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4242 if (g_slist_length( account_type->label_list)==0) {
4243 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4244 return ACCOUNT_ERROR_NONE;
4247 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4249 rc = _account_get_record_count(query);
4252 ACCOUNT_ERROR( "_account_insert_label : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
4253 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4259 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4262 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4263 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4264 "(?, ?, ?) ", LABEL_TABLE);
4266 hstmt = _account_prepare_query(query);
4268 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4270 label_s* label_data = NULL;
4271 label_data = (label_s*)iter->data;
4272 ACCOUNT_VERBOSE( "label_data->appid = %s, label_data->label = %s, label_data->locale \n", label_data->app_id, label_data->label, label_data->locale);
4274 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4275 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4276 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4277 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4278 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4279 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4281 rc = _account_query_step(hstmt);
4283 if (rc != SQLITE_DONE) {
4284 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4288 _account_query_finalize(hstmt);
4293 ACCOUNT_VERBOSE( "_account_type_insert_label() DONE\n");
4295 return ACCOUNT_ERROR_NONE;
4298 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4300 char *textbuf = NULL;
4302 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4303 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4305 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4306 _account_db_data_to_text(textbuf, &(feature_record->key));
4308 ACCOUNT_VERBOSE("END _account_type_convert_column_to_provider_feature");
4311 ACCOUNT_API int account_type_query_provider_feature_by_app_id(provider_feature_cb cb_func, const char* app_id, void *user_data )
4313 int error_code = ACCOUNT_ERROR_NONE;
4314 account_stmt hstmt = NULL;
4315 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4316 int rc = 0, binding_count = 1;
4318 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4319 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4320 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4322 ACCOUNT_VERBOSE("app_id = %s", app_id);
4324 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4326 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4327 hstmt = _account_prepare_query(query);
4329 _account_query_bind_text(hstmt, binding_count++, app_id);
4331 rc = _account_query_step(hstmt);
4332 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4334 provider_feature_s* feature_record = NULL;
4336 while (rc == SQLITE_ROW) {
4337 bool cb_ret = FALSE;
4338 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4340 if (feature_record == NULL) {
4341 ACCOUNT_FATAL("malloc Failed");
4345 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4347 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4349 cb_ret = cb_func(feature_record->app_id, feature_record->key, user_data);
4351 _account_type_free_feature_items(feature_record);
4352 _ACCOUNT_FREE(feature_record);
4354 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4356 rc = _account_query_step(hstmt);
4359 _account_query_finalize(hstmt);
4362 error_code = ACCOUNT_ERROR_NONE;
4365 if (hstmt != NULL) {
4366 _account_query_finalize(hstmt);
4370 pthread_mutex_unlock(&account_mutex);
4374 ACCOUNT_API bool account_type_query_supported_feature(const char* app_id, const char* capability)
4376 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4377 int record_count = 0;
4379 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4380 ACCOUNT_RETURN_VAL((capability != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CAPABILITY"));
4382 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4384 record_count = _account_get_record_count(query);
4386 if (record_count <= 0) {
4387 ACCOUNT_WARNING( "related capability type item is not existed rc=%d , %s", record_count, _account_db_err_msg());
4396 ACCOUNT_API int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb cb_func, void* user_data)
4398 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4399 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4402 account_type_s *data = (account_type_s*)account_type;
4404 for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4405 provider_feature_s *feature_data = NULL;
4407 feature_data = (provider_feature_s*)iter->data;
4409 ACCOUNT_VERBOSE("appid = %s, key = %s", feature_data->key, feature_data->app_id);
4411 //cb_func(feature_data->app_id, feature_data->key, user_data);
4412 if(cb_func(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
4413 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4414 return ACCOUNT_ERROR_NONE;
4418 return ACCOUNT_ERROR_NONE;
4421 ACCOUNT_API int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4423 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4424 ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4426 account_type_s *data = (account_type_s*)account_type;
4428 GSList *iter = NULL;
4429 bool b_is_new = TRUE;
4431 for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4432 provider_feature_s *feature_data = NULL;
4433 feature_data = (provider_feature_s*)iter->data;
4435 if(!strcmp(feature_data->key, provider_feature)) {
4442 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4444 if (feature_data == NULL)
4445 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4446 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4448 feature_data->key = _account_get_text(provider_feature);
4449 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4452 return ACCOUNT_ERROR_NONE;
4455 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4458 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4459 account_stmt hstmt = NULL;
4461 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4462 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4464 if (g_slist_length( account_type->provider_feature_list)==0) {
4465 ACCOUNT_ERROR( "no capability\n");
4466 return ACCOUNT_ERROR_NONE;
4469 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4471 rc = _account_get_record_count(query);
4474 ACCOUNT_WARNING( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4475 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4482 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4485 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4486 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4487 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4489 hstmt = _account_prepare_query(query);
4491 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4493 provider_feature_s* feature_data = NULL;
4494 feature_data = (provider_feature_s*)iter->data;
4495 ACCOUNT_VERBOSE("key = %s, app_id = %s \n", feature_data->key, app_id);
4497 ret = _account_query_bind_text(hstmt, count++, app_id);
4498 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4499 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4500 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4502 rc = _account_query_step(hstmt);
4504 if (rc != SQLITE_DONE) {
4505 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4509 _account_query_finalize(hstmt);
4514 ACCOUNT_VERBOSE( "_account_type_insert_provider_feature() DONE\n");
4516 return ACCOUNT_ERROR_NONE;
4519 ACCOUNT_API int account_type_insert_to_db(account_type_h account_type, int* account_type_id)
4521 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4523 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4524 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4525 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4527 account_type_s *data = (account_type_s*)account_type;
4529 pthread_mutex_lock(&account_mutex);
4532 /* transaction control required*/
4533 ret_transaction = _account_begin_transaction();
4535 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4536 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4537 pthread_mutex_unlock(&account_mutex);
4538 return ret_transaction;
4541 if (_account_type_check_duplicated(data)) {
4542 ret_transaction = _account_end_transaction(FALSE);
4543 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4544 *account_type_id = -1;
4545 pthread_mutex_unlock(&account_mutex);
4546 return ACCOUNT_ERROR_DUPLICATED;
4548 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4550 error_code = _account_type_execute_insert_query(data);
4552 if (error_code != ACCOUNT_ERROR_NONE){
4553 error_code = ACCOUNT_ERROR_DUPLICATED;
4554 ret_transaction = _account_end_transaction(FALSE);
4555 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4556 *account_type_id = -1;
4557 pthread_mutex_unlock(&account_mutex);
4562 ACCOUNT_INFO( "_account_type_execute_insert_query, insert error_code : %d", error_code);
4564 error_code = _account_type_insert_provider_feature(data, data->app_id);
4565 if(error_code != ACCOUNT_ERROR_NONE) {
4566 ret_transaction = _account_end_transaction(FALSE);
4567 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4568 pthread_mutex_unlock(&account_mutex);
4571 error_code = _account_type_insert_label(data);
4572 if(error_code != ACCOUNT_ERROR_NONE) {
4573 ret_transaction = _account_end_transaction(FALSE);
4574 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4575 pthread_mutex_unlock(&account_mutex);
4579 ret_transaction = _account_end_transaction(TRUE);
4580 ACCOUNT_DEBUG("Insert success(%x), commit insert query(%x)!!!!\n", error_code, ret_transaction);
4582 pthread_mutex_unlock(&account_mutex);
4584 return ACCOUNT_ERROR_NONE;
4587 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4590 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4591 account_stmt hstmt = NULL;
4593 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4595 if (g_slist_length( account_type->provider_feature_list)==0) {
4596 ACCOUNT_ERROR( "no feature\n");
4597 return ACCOUNT_ERROR_NONE;
4600 ACCOUNT_DEBUG( "app id", app_id);
4602 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4604 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4605 hstmt = _account_prepare_query(query);
4607 _account_query_bind_text(hstmt, count++, app_id);
4608 rc = _account_query_step(hstmt);
4610 if (rc != SQLITE_DONE) {
4611 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4612 return ACCOUNT_ERROR_DB_FAILED;
4614 _account_query_finalize(hstmt);
4619 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4622 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4623 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4624 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4626 hstmt = _account_prepare_query(query);
4628 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4630 provider_feature_s* feature_data = NULL;
4631 feature_data = (provider_feature_s*)iter->data;
4633 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4634 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4635 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4636 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4638 rc = _account_query_step(hstmt);
4640 if (rc != SQLITE_DONE) {
4641 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4644 _account_query_finalize(hstmt);
4648 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4650 return ACCOUNT_ERROR_NONE;
4653 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4656 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4657 account_stmt hstmt = NULL;
4659 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4661 if (g_slist_length( account_type->label_list)==0) {
4662 ACCOUNT_ERROR( "_account_type_update_label, no label\n");
4663 return ACCOUNT_ERROR_NONE;
4666 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4668 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4669 hstmt = _account_prepare_query(query);
4671 _account_query_bind_text(hstmt, count++, app_id);
4672 rc = _account_query_step(hstmt);
4674 if (rc != SQLITE_DONE) {
4675 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4676 return ACCOUNT_ERROR_DB_FAILED;
4678 _account_query_finalize(hstmt);
4683 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4686 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4687 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4688 "(?, ?, ?) ", LABEL_TABLE);
4690 hstmt = _account_prepare_query(query);
4692 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4694 label_s* label_data = NULL;
4695 label_data = (label_s*)iter->data;
4697 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4698 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4699 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4700 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4701 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
4702 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4704 rc = _account_query_step(hstmt);
4706 if (rc != SQLITE_DONE) {
4707 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4710 _account_query_finalize(hstmt);
4714 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4716 return ACCOUNT_ERROR_NONE;
4720 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
4722 int rc = 0, binding_count =1;
4723 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4724 int error_code = ACCOUNT_ERROR_NONE;
4725 account_stmt hstmt = NULL;
4727 if (!account_type->app_id) {
4728 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
4729 return ACCOUNT_ERROR_INVALID_PARAMETER;
4732 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4733 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
4734 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
4736 hstmt = _account_prepare_query(query);
4737 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
4739 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
4740 _account_query_bind_text(hstmt, binding_count++, app_id);
4742 rc = _account_query_step(hstmt);
4743 if (rc != SQLITE_DONE) {
4744 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4747 _account_query_finalize(hstmt);
4751 error_code = _account_type_update_label(account_type, app_id);
4752 /* update provider feature */
4753 error_code = _account_type_update_provider_feature(account_type, app_id);
4758 ACCOUNT_API int account_type_update_to_db_by_app_id(account_type_h account_type, const char* app_id)
4760 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
4761 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4762 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4764 int error_code = ACCOUNT_ERROR_NONE;
4765 account_type_s* data = (account_type_s*)account_type;
4767 pthread_mutex_lock(&account_mutex);
4769 error_code = _account_type_update_account(data, app_id);
4771 pthread_mutex_unlock(&account_mutex);
4776 ACCOUNT_API int account_type_delete_by_app_id(const char* app_id)
4778 int error_code = ACCOUNT_ERROR_NONE;
4779 account_stmt hstmt = NULL;
4780 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4781 int rc = 0, count = -1;
4782 int ret_transaction = 0;
4783 int binding_count = 1;
4784 bool is_success = FALSE;
4786 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4787 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
4789 /* Check requested ID to delete */
4790 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
4792 count = _account_get_record_count(query);
4794 ACCOUNT_ERROR("app id(%s) is not exist. count(%d)\n", app_id, count);
4795 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4798 /* transaction control required*/
4799 ret_transaction = _account_begin_transaction();
4801 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4802 ACCOUNT_FATAL("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4803 pthread_mutex_unlock(&account_mutex);
4804 return ret_transaction;
4807 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
4809 hstmt = _account_prepare_query(query);
4810 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4811 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4813 _account_query_bind_text(hstmt, binding_count++, app_id);
4815 rc = _account_query_step(hstmt);
4816 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4818 _account_query_finalize(hstmt);
4822 ACCOUNT_MEMSET(query, 0, sizeof(query));
4824 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
4826 hstmt = _account_prepare_query(query);
4827 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4828 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4830 _account_query_bind_text(hstmt, binding_count++, app_id);
4832 rc = _account_query_step(hstmt);
4833 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4835 _account_query_finalize(hstmt);
4841 ACCOUNT_MEMSET(query, 0, sizeof(query));
4843 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
4845 hstmt = _account_prepare_query(query);
4846 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4847 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4849 _account_query_bind_text(hstmt, binding_count++, app_id);
4851 rc = _account_query_step(hstmt);
4852 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4854 _account_query_finalize(hstmt);
4860 if (hstmt != NULL) {
4861 _account_query_finalize(hstmt);
4865 ret_transaction = _account_end_transaction(is_success);
4867 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4868 ACCOUNT_FATAL("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4871 pthread_mutex_unlock(&account_mutex);
4876 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
4878 char *textbuf = NULL;
4880 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
4882 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
4883 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
4885 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
4886 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
4888 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
4889 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
4891 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
4892 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
4894 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
4896 ACCOUNT_VERBOSE("END _account_type_convert_column_to_account_type");
4899 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
4901 char *textbuf = NULL;
4903 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
4904 _account_db_data_to_text(textbuf, &(label_record->app_id));
4906 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
4907 _account_db_data_to_text(textbuf, &(label_record->label));
4909 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
4910 _account_db_data_to_text(textbuf, &(label_record->locale));
4912 ACCOUNT_VERBOSE("END _account_type_convert_column_to_label");
4915 ACCOUNT_API int account_type_query_label_by_app_id(account_label_cb cb_func, const char* app_id, void *user_data )
4917 int error_code = ACCOUNT_ERROR_NONE;
4918 account_stmt hstmt = NULL;
4919 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4920 int rc = 0, binding_count = 1;
4922 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4923 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4924 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4926 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4928 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
4929 hstmt = _account_prepare_query(query);
4931 _account_query_bind_text(hstmt, binding_count++, app_id);
4933 rc = _account_query_step(hstmt);
4934 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4936 label_s* label_record = NULL;
4938 while (rc == SQLITE_ROW) {
4939 bool cb_ret = FALSE;
4940 label_record = (label_s*) malloc(sizeof(label_s));
4942 if (label_record == NULL) {
4943 ACCOUNT_FATAL("malloc Failed");
4947 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4949 _account_type_convert_column_to_label(hstmt, label_record);
4951 cb_ret = cb_func(label_record->app_id, label_record->label , label_record->locale, user_data);
4953 _account_type_free_label_items(label_record);
4954 _ACCOUNT_FREE(label_record);
4956 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4958 rc = _account_query_step(hstmt);
4961 _account_query_finalize(hstmt);
4964 error_code = ACCOUNT_ERROR_NONE;
4967 if (hstmt != NULL) {
4968 _account_query_finalize(hstmt);
4972 pthread_mutex_unlock(&account_mutex);
4976 int _account_type_label_get_app_id(label_h label, char **app_id)
4979 ACCOUNT_ERROR("(%s)-(%d) label handle is NULL.\n", __FUNCTION__, __LINE__);
4980 return ACCOUNT_ERROR_INVALID_PARAMETER;
4984 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4985 return ACCOUNT_ERROR_INVALID_PARAMETER;
4988 label_s *data = (label_s*)label;
4992 *app_id = _account_get_text(data->app_id);
4994 return ACCOUNT_ERROR_NONE;
4997 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
4999 account_type_s *data = (account_type_s*)user_data;
5001 label_s *label_data = (label_s*)malloc(sizeof(label_s));
5003 if (label_data == NULL) {
5004 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
5007 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
5009 label_data->app_id = _account_get_text(app_id);
5010 label_data->label = _account_get_text(label);
5011 label_data->locale = _account_get_text(locale);
5013 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5015 ACCOUNT_VERBOSE("_account_get_label_text_cb :: appid=%s, label=%s\n", label_data->app_id, label_data->label);
5020 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5022 account_type_s *data = (account_type_s*)user_data;
5024 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5026 if (feature_data == NULL) {
5027 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5030 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5032 feature_data->app_id = _account_get_text(app_id);
5033 feature_data->key = _account_get_text(key);
5035 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5037 ACCOUNT_VERBOSE("appid=%s, key=%s\n", feature_data->app_id, feature_data->key);
5042 ACCOUNT_API int account_type_query_by_app_id(const char* app_id, account_type_h *account_type)
5044 int error_code = ACCOUNT_ERROR_NONE;
5045 account_stmt hstmt = NULL;
5046 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5047 int rc = 0, binding_count = 1;
5049 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5050 ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL"));
5051 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5053 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5055 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5056 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5057 hstmt = _account_prepare_query(query);
5059 _account_query_bind_text(hstmt, binding_count++, app_id);
5061 rc = _account_query_step(hstmt);
5062 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5064 account_type_s *account_type_record = (account_type_s *)(*account_type);
5066 while (rc == SQLITE_ROW) {
5067 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5068 ACCOUNT_DEBUG("get account info by id %p\n", account_type_record);
5069 rc = _account_query_step(hstmt);
5072 _account_query_finalize(hstmt);
5073 account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)account_type_record);
5074 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)account_type_record);
5077 error_code = ACCOUNT_ERROR_NONE;
5080 if (hstmt != NULL) {
5081 _account_query_finalize(hstmt);
5085 pthread_mutex_unlock(&account_mutex);
5089 ACCOUNT_API int account_type_query_app_id_exist(const char* app_id)
5091 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5094 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5095 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5097 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5099 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5101 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5102 rc = _account_get_record_count(query);
5105 ACCOUNT_DEBUG("(%s) not exist in account type db");
5106 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5109 return ACCOUNT_ERROR_NONE;
5112 ACCOUNT_API int account_type_query_by_provider_feature(account_type_cb cb_func, const char* key, void* user_data)
5114 int error_code = ACCOUNT_ERROR_NONE;
5115 account_stmt hstmt = NULL;
5116 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5118 GList *account_type_list = NULL;
5120 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
5121 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
5122 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5124 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5126 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5128 hstmt = _account_prepare_query(query);
5130 int binding_count = 1;
5131 _account_query_bind_text(hstmt, binding_count++, (char*)key);
5133 rc = _account_query_step(hstmt);
5135 account_type_s *account_type_record = NULL;
5137 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5140 while(rc == SQLITE_ROW) {
5141 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5143 if (account_type_record == NULL) {
5144 ACCOUNT_FATAL("malloc Failed");
5148 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5149 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5150 account_type_list = g_list_append(account_type_list, account_type_record);
5151 rc = _account_query_step(hstmt);
5155 _account_query_finalize(hstmt);
5160 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5161 account_type_s *account_type = NULL;
5162 account_type = (account_type_s*)iter->data;
5163 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5164 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5165 //cb_func((account_type_h)account_type, user_data);
5166 ACCOUNT_CATCH_ERROR(cb_func((account_type_h)account_type, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5170 error_code = ACCOUNT_ERROR_NONE;
5173 if (hstmt != NULL) {
5174 _account_query_finalize(hstmt);
5177 if (account_type_list) {
5178 _account_type_glist_free(account_type_list);
5179 account_type_list = NULL;
5186 ACCOUNT_API int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data)
5188 int error_code = ACCOUNT_ERROR_NONE;
5189 account_stmt hstmt = NULL;
5190 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5192 GList *account_type_list = NULL;
5194 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
5195 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5197 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5199 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5200 hstmt = _account_prepare_query(query);
5202 rc = _account_query_step(hstmt);
5204 account_type_s *account_type_record = NULL;
5206 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5209 while(rc == SQLITE_ROW) {
5210 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5212 if (account_type_record == NULL) {
5213 ACCOUNT_FATAL("malloc Failed");
5217 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5218 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5219 account_type_list = g_list_append(account_type_list, account_type_record);
5220 rc = _account_query_step(hstmt);
5224 _account_query_finalize(hstmt);
5229 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5230 account_type_s *account_type = NULL;
5231 account_type = (account_type_s*)iter->data;
5232 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5233 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5234 callback((account_type_h)account_type, user_data);
5238 error_code = ACCOUNT_ERROR_NONE;
5241 if (hstmt != NULL) {
5242 _account_query_finalize(hstmt);
5245 if (account_type_list) {
5246 _account_type_glist_free(account_type_list);
5247 account_type_list = NULL;
5253 // output parameter label must be free
5254 ACCOUNT_API int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label)
5256 int error_code = ACCOUNT_ERROR_NONE;
5257 account_stmt hstmt = NULL;
5258 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5259 int rc = 0, binding_count = 1;
5261 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5262 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5263 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5265 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5267 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, locale);
5268 hstmt = _account_prepare_query(query);
5270 _account_query_bind_text(hstmt, binding_count++, app_id);
5272 rc = _account_query_step(hstmt);
5273 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5275 label_s* label_record = NULL;
5277 while (rc == SQLITE_ROW) {
5278 label_record = (label_s*) malloc(sizeof(label_s));
5280 if (label_record == NULL) {
5281 ACCOUNT_FATAL("malloc Failed");
5285 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5287 _account_type_convert_column_to_label(hstmt,label_record);
5289 *label = _account_get_text(label_record->label);
5291 _account_type_free_label_items(label_record);
5292 _ACCOUNT_FREE(label_record);
5294 rc = _account_query_step(hstmt);
5297 _account_query_finalize(hstmt);
5300 error_code = ACCOUNT_ERROR_NONE;
5303 if (hstmt != NULL) {
5304 _account_query_finalize(hstmt);
5308 pthread_mutex_unlock(&account_mutex);
5312 static int _account_insert_custom(account_s *account, int account_id)
5315 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5316 account_stmt hstmt = NULL;
5318 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5320 if (g_slist_length( account->custom_list)==0) {
5321 ACCOUNT_ERROR( "_account_insert_custom, no custom data\n");
5322 return ACCOUNT_ERROR_NONE;
5325 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5327 rc = _account_get_record_count(query);
5330 ACCOUNT_ERROR( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5331 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5338 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5341 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5342 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5343 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5345 hstmt = _account_prepare_query(query);
5347 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5349 account_custom_s* custom_data = NULL;
5350 custom_data = (account_custom_s*)iter->data;
5351 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
5353 ret = _account_query_bind_int(hstmt, count++, account_id);
5354 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5355 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5356 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5357 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5358 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5359 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5360 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5362 rc = _account_query_step(hstmt);
5364 if (rc != SQLITE_DONE) {
5365 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5369 _account_query_finalize(hstmt);
5374 ACCOUNT_VERBOSE( "_account_insert_custom() DONE\n");
5376 return ACCOUNT_ERROR_NONE;
5379 static int _account_update_custom(account_s *account, int account_id)
5382 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5383 account_stmt hstmt = NULL;
5385 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5387 ACCOUNT_INFO( "account handle=%p, account_id=%d", account, account_id);
5389 if (g_slist_length( account->custom_list)==0) {
5390 ACCOUNT_ERROR( "_account_update_custom, no custom data\n");
5391 return ACCOUNT_ERROR_NONE;
5394 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5396 rc = _account_get_record_count(query);
5399 ACCOUNT_ERROR( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5400 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5403 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5405 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5406 hstmt = _account_prepare_query(query);
5408 _account_query_bind_int(hstmt, count++, (int)account_id);
5409 rc = _account_query_step(hstmt);
5411 if (rc != SQLITE_DONE) {
5412 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5413 return ACCOUNT_ERROR_DB_FAILED;
5415 _account_query_finalize(hstmt);
5420 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5423 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5424 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5425 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5427 hstmt = _account_prepare_query(query);
5429 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5431 account_custom_s* custom_data = NULL;
5432 custom_data = (account_custom_s*)iter->data;
5434 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5435 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5436 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5437 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5438 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5439 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5440 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5441 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5443 rc = _account_query_step(hstmt);
5445 if (rc != SQLITE_DONE) {
5446 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5450 _account_query_finalize(hstmt);
5455 ACCOUNT_VERBOSE( "_account_update_custom() DONE\n");
5457 return ACCOUNT_ERROR_NONE;
5460 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data )
5462 int error_code = ACCOUNT_ERROR_NONE;
5463 account_stmt hstmt = NULL;
5464 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5467 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5468 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5469 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5471 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5473 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5474 hstmt = _account_prepare_query(query);
5476 rc = _account_query_step(hstmt);
5477 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5479 account_custom_s* custom_record = NULL;
5481 while (rc == SQLITE_ROW) {
5482 bool cb_ret = FALSE;
5483 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5485 if (custom_record == NULL) {
5486 ACCOUNT_FATAL("malloc Failed");
5490 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5492 _account_convert_column_to_custom(hstmt, custom_record);
5494 cb_ret = cb_func(custom_record->key, custom_record->value, user_data);
5496 _account_custom_item_free(custom_record);
5497 _ACCOUNT_FREE(custom_record);
5499 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5501 rc = _account_query_step(hstmt);
5504 _account_query_finalize(hstmt);
5507 error_code = ACCOUNT_ERROR_NONE;
5510 if (hstmt != NULL) {
5511 _account_query_finalize(hstmt);
5515 pthread_mutex_unlock(&account_mutex);
5519 static void _account_subscribe_vconf_callback(keynode_t *key, void *user_data)
5521 account_subscribe_s* tmp = (account_subscribe_s*)user_data;
5522 char *msg = NULL, *vconf_key = NULL;
5523 char event_msg[256] ={0, };
5524 int account_id = -1;
5526 ACCOUNT_DEBUG("START\n");
5529 ACCOUNT_ERROR("No subscribtion msg !!!!!\n");
5534 ACCOUNT_ERROR("user data required\n");
5538 if(!memcmp(vconf_keynode_get_name(key), VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR)))
5540 vconf_key = vconf_keynode_get_str(key);
5541 msg = strdup(vconf_key);
5543 char* event_type = NULL;
5547 event_type = strtok_r(msg, ":", &ptr);
5548 id = strtok_r(NULL, ":", &ptr);
5550 ACCOUNT_DEBUG("msg(%s), event_type(%s), id(%s)\n", msg, event_type, id);
5552 ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type);
5554 account_id = atoi(id);
5556 if(tmp->account_subscription_callback)
5557 tmp->account_subscription_callback(event_msg, account_id, tmp->user_data);
5562 ACCOUNT_DEBUG("END\n");
5565 ACCOUNT_API int account_subscribe_create(account_subscribe_h* account_subscribe)
5567 if (!account_subscribe) {
5568 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
5569 return ACCOUNT_ERROR_INVALID_PARAMETER;
5572 account_subscribe_s *data = (account_subscribe_s*)calloc(1,sizeof(account_subscribe_s));
5575 ACCOUNT_FATAL("OUT OF MEMORY\n");
5576 return ACCOUNT_ERROR_OUT_OF_MEMORY;
5579 ACCOUNT_VERBOSE("create handle=%p\n", *data);
5581 *account_subscribe = (account_subscribe_h)data;
5583 return ACCOUNT_ERROR_NONE;
5586 ACCOUNT_API int account_subscribe_notification(account_subscribe_h account_subscribe, account_event_cb cb_func, void* user_data)
5588 ACCOUNT_DEBUG("START\n");
5590 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5592 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5594 tmp->account_subscription_callback = cb_func;
5595 tmp->user_data = user_data;
5597 if (vconf_notify_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5598 (vconf_callback_fn)_account_subscribe_vconf_callback,
5600 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5601 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5604 ACCOUNT_DEBUG("Vconf Subscription Success!!!!\n");
5605 return ACCOUNT_ERROR_NONE;
5608 ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_subscribe)
5610 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5612 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5616 if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5617 (vconf_callback_fn)_account_subscribe_vconf_callback) != 0) {
5618 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5619 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5622 ACCOUNT_DEBUG("Vconf Unsubscription Success!!!!\n");
5623 return ACCOUNT_ERROR_NONE;