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.
31 #include <account-private.h>
34 #include <pkgmgr-info.h>
38 #define EAS_CMDLINE "/usr/bin/eas-engine"
39 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
40 #define ACTIVESYNC_APPID "activesync-ui"
42 static sqlite3* g_hAccountDB = NULL;
43 static int g_refCntDB = 0;
44 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static char *_account_get_text(const char *text_data);
47 static int _account_gslist_free(GSList* list);
48 static int _account_glist_free(GList* list);
49 static char *_account_query_table_column_text(account_stmt pStmt, int pos);
50 static int _account_insert_custom(account_s *account, int account_id);
51 static int _account_update_custom(account_s *account, int account_id);
52 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data );
53 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
55 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
60 GSList** appid_list = (GSList**)user_data;
62 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
63 item = _account_get_text(appid);
64 *appid_list = g_slist_append(*appid_list, item);
69 static inline int __read_proc(const char *path, char *buf, int size)
73 if (buf == NULL || path == NULL) {
74 ACCOUNT_ERROR("path and buffer is mandatory\n");
78 fd = open(path, O_RDONLY);
80 ACCOUNT_ERROR("fd open error(%d)\n", fd);
84 ret = read(fd, buf, size - 1);
86 ACCOUNT_ERROR("fd read error(%d)\n", fd);
97 char *_account_get_proc_cmdline_bypid(int pid)
102 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
103 ret = __read_proc(buf, buf, sizeof(buf));
105 ACCOUNT_INFO("No proc directory (%d)\n", pid);
113 static char* _account_get_current_appid()
116 char appid[128]={0,};
117 pkgmgrinfo_appinfo_h ahandle=NULL;
118 char* appid_ret = NULL;
121 ACCOUNT_INFO("pid (%d)\n", pid, ret);
122 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
123 ACCOUNT_INFO("appid (%s) ret(%d)!!!!!!\n", appid, ret);
125 /* SLP platform core exception */
126 if(strlen(appid) == 0){
127 char* cmdline = NULL;
128 cmdline = _account_get_proc_cmdline_bypid(pid);
129 ACCOUNT_INFO("cmdline (%s)!!!!!!\n", cmdline);
130 if(!strcmp(cmdline, EAS_CMDLINE)) {
131 appid_ret = _account_get_text(ACTIVESYNC_APPID);
134 ACCOUNT_ERROR("No app id\n");
139 appid_ret = _account_get_text(appid);
141 pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
145 static int _account_check_account_type_with_appid_group(const char* appid, char** verified_appid)
147 int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
149 pkgmgrinfo_appinfo_h ahandle=NULL;
150 pkgmgrinfo_pkginfo_h phandle=NULL;
152 GSList* appid_list = NULL;
156 ACCOUNT_INFO("input param is null\n");
157 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
161 ACCOUNT_INFO("output param is null\n");
162 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
165 if(!strcmp(appid, "com.samsung.setting")){
166 ACCOUNT_DEBUG("Setting exception\n");
167 *verified_appid = _account_get_text("com.samsung.setting");
168 return ACCOUNT_ERROR_NONE;
171 /* Get app id family which is stored in account database */
172 ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
173 ACCOUNT_INFO("ahandle (%p), ret(%x)\n", ahandle, ret);
174 ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
175 ACCOUNT_INFO("package_id (%s), ret(%x)\n", package_id, ret);
176 ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
177 ACCOUNT_INFO("phandle (%p), ret(%x)\n", package_id, ret);
179 ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); //==> pkgÇڵ鿡 ¼ÓÇÑ ui-appid ¸ðµÎ ãÀ½
180 ACCOUNT_INFO("ret(%x)\n", ret);
182 /* Compare current app id with the stored app id family */
183 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
184 char* tmp = (char*)iter->data;
186 if(account_type_query_app_id_exist(tmp) == ACCOUNT_ERROR_NONE) {
187 ACCOUNT_INFO("permission verified appid(%s), current appid(%s)\n", tmp, appid);
188 *verified_appid = _account_get_text(tmp);
189 error_code = ACCOUNT_ERROR_NONE;
193 ACCOUNT_DEBUG("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
199 g_slist_free(appid_list);
200 pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
201 pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
206 static const char *_account_db_err_msg()
208 assert(NULL != g_hAccountDB);
209 return sqlite3_errmsg(g_hAccountDB);
212 static void _account_insert_delete_update_notification_send(char *noti_name)
215 ACCOUNT_ERROR("Noti Name is NULL!!!!!!\n");
219 ACCOUNT_DEBUG("Sending notification with value %s\n", noti_name);
221 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) == 0) {
222 ACCOUNT_VERBOSE("Vconf MSG Str set SUCCESS !!!!!!\n");;
224 ACCOUNT_ERROR("Vconf MSG Str set FAILED !!!!!!\n");;
228 static int _account_get_record_count(char* query)
232 account_stmt pStmt = NULL;
234 assert(NULL != query);
235 assert(NULL != g_hAccountDB);
236 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
238 rc = sqlite3_step(pStmt);
239 if (SQLITE_ROW != rc) {
240 ACCOUNT_ERROR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
241 sqlite3_finalize(pStmt);
242 return ACCOUNT_ERROR_DB_FAILED;
245 ncount = sqlite3_column_int(pStmt, 0);
247 ACCOUNT_VERBOSE("count : %d, End", ncount);
248 sqlite3_finalize(pStmt);
253 static int _account_execute_query(char *query)
256 char* pszErrorMsg = NULL;
258 assert(NULL != query);
259 assert(NULL != g_hAccountDB);
261 ACCOUNT_INFO("query : %s", query);
263 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
264 if (SQLITE_OK != rc) {
265 ACCOUNT_FATAL("sqlite3_exec(%s) failed(%s).", query, pszErrorMsg);
266 sqlite3_free(pszErrorMsg);
272 static int _account_begin_transaction(void)
276 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
278 if (ret != SQLITE_OK) {
279 ACCOUNT_FATAL("_account_svc_begin_transaction fail :: %d", ret);
280 return ACCOUNT_ERROR_DB_FAILED;
283 return ACCOUNT_ERROR_NONE;
286 static int _account_end_transaction(bool is_success)
290 if (is_success == true) {
291 ret = _account_execute_query("COMMIT TRANSACTION");
293 ret = _account_execute_query("ROLLBACK TRANSACTION");
296 if (ret != SQLITE_OK) {
297 ACCOUNT_FATAL("_account_svc_end_transaction fail :: %d", ret);
298 return ACCOUNT_ERROR_DB_FAILED;
301 return ACCOUNT_ERROR_NONE;
304 static bool _account_check_add_more_account(const char* app_id)
306 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
309 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
310 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
312 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
314 ACCOUNT_DEBUG("app id (%s)\n", app_id);
316 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
317 rc = _account_get_record_count(query);
319 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
321 /* multiple account support case */
323 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
327 /* multiple account not support case */
328 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
329 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
330 rc = _account_get_record_count(query);
333 ACCOUNT_DEBUG("app id (%s) supports single account. and there is no account of the app id\n", app_id);
340 static int _account_create_all_tables(void)
343 int error_code = ACCOUNT_ERROR_NONE;
344 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
347 ACCOUNT_MEMSET(query, 0, sizeof(query));
349 ACCOUNT_VERBOSE("_account_create_all_tables begin");
351 /*Create the account table*/
352 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
353 rc = _account_get_record_count(query);
355 ACCOUNT_MEMSET(query, 0, sizeof(query));
356 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_SCHEMA, ACCOUNT_TABLE);
357 ACCOUNT_INFO("Create %s table", ACCOUNT_TABLE);
358 rc = _account_execute_query(query);
359 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
362 /*Create capability table*/
363 ACCOUNT_MEMSET(query, 0, sizeof(query));
364 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
365 rc = _account_get_record_count(query);
367 ACCOUNT_MEMSET(query, 0, sizeof(query));
368 ACCOUNT_SNPRINTF(query, sizeof(query), CAPABILITY_SCHEMA, CAPABILITY_TABLE);
369 ACCOUNT_INFO("Create %s table", CAPABILITY_TABLE);
370 rc = _account_execute_query(query);
371 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
374 /* Create account custom table */
375 ACCOUNT_MEMSET(query, 0, sizeof(query));
376 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
377 rc = _account_get_record_count(query);
379 ACCOUNT_MEMSET(query, 0, sizeof(query));
380 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_CUSTOM_SCHEMA, ACCOUNT_CUSTOM_TABLE);
381 ACCOUNT_INFO("Create %s table", ACCOUNT_CUSTOM_TABLE);
382 rc = _account_execute_query(query);
383 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
386 /* Create account type table */
387 ACCOUNT_MEMSET(query, 0, sizeof(query));
388 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
389 rc = _account_get_record_count(query);
391 ACCOUNT_MEMSET(query, 0, sizeof(query));
392 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_TYPE_SCHEMA, ACCOUNT_TYPE_TABLE);
393 ACCOUNT_INFO("Create %s table", ACCOUNT_TYPE_TABLE);
394 rc = _account_execute_query(query);
395 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
398 /* Create label table */
399 ACCOUNT_MEMSET(query, 0, sizeof(query));
400 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
401 rc = _account_get_record_count(query);
403 ACCOUNT_MEMSET(query, 0, sizeof(query));
404 ACCOUNT_SNPRINTF(query, sizeof(query), LABEL_SCHEMA, LABEL_TABLE);
405 ACCOUNT_INFO("Create %s table", LABEL_TABLE);
406 rc = _account_execute_query(query);
407 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
410 /* Create account feature table */
411 ACCOUNT_MEMSET(query, 0, sizeof(query));
412 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
413 rc = _account_get_record_count(query);
415 ACCOUNT_MEMSET(query, 0, sizeof(query));
416 ACCOUNT_SNPRINTF(query, sizeof(query), PROVIDER_FEATURE_SCHEMA, PROVIDER_FEATURE_TABLE);
417 ACCOUNT_INFO("Create %s table", PROVIDER_FEATURE_TABLE);
418 rc = _account_execute_query(query);
419 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
425 static bool _account_check_is_all_table_exists()
428 char query[ACCOUNT_SQL_LEN_MAX] = {0,};
429 ACCOUNT_MEMSET(query, 0, sizeof(query));
431 ACCOUNT_VERBOSE("_account_check_is_all_table_exists");
433 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
434 ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
435 rc = _account_get_record_count(query);
437 ACCOUNT_DEBUG("Table count = %d\n", rc);
439 if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
440 ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
444 ACCOUNT_VERBOSE("END of _account_check_is_all_table_exists\n");
449 static int _account_db_open(void)
452 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
454 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
457 rc = db_util_open(ACCOUNT_DB_NAME, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
458 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
461 ACCOUNT_INFO("_account_db_open: The database connected. refcnt=%d ", g_refCntDB);
464 ACCOUNT_INFO("The database already connected. refcnt=%d ", g_refCntDB);
467 return ACCOUNT_ERROR_NONE;
470 static int _account_db_close(void)
476 if (g_refCntDB > 0) {
478 ACCOUNT_INFO("_account_svc_db_close: The database disconnected. refcnt=%d ", g_refCntDB);
480 if (g_refCntDB == 0) {
481 rc = db_util_close(g_hAccountDB);
482 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
484 ACCOUNT_INFO( "_account_svc_db_close: The database disconnected really. ");
486 ret = ACCOUNT_ERROR_NONE;
488 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
489 ret = ACCOUNT_ERROR_DB_FAILED;
495 static int _account_connect(void)
497 int error_code = ACCOUNT_ERROR_NONE;
499 pthread_mutex_lock(&account_mutex);
501 ACCOUNT_VERBOSE("db path = %s\n", ACCOUNT_DB_NAME);
503 error_code = _account_db_open();
504 if (ACCOUNT_ERROR_NONE != error_code) {
505 ACCOUNT_ERROR("The database isn't connected.\n");
506 pthread_mutex_unlock(&account_mutex);
507 return ACCOUNT_ERROR_DB_NOT_OPENED;
510 if (FALSE == _account_check_is_all_table_exists())
511 error_code = _account_create_all_tables();
513 pthread_mutex_unlock(&account_mutex);
514 return ACCOUNT_ERROR_NONE;
517 ACCOUNT_API int account_connect (void)
519 return _account_connect();
522 static int _account_disconnect(void)
524 int error_code = ACCOUNT_ERROR_NONE;
526 pthread_mutex_lock(&account_mutex);
527 ACCOUNT_INFO("db path = %s have been closed!!!\n", ACCOUNT_DB_NAME);
529 error_code = _account_db_close();
530 pthread_mutex_unlock(&account_mutex);
535 ACCOUNT_API int account_disconnect (void)
537 return _account_disconnect();
540 static int _account_free_capability_items(account_capability_s *data)
542 _ACCOUNT_FREE(data->type);
543 _ACCOUNT_FREE(data->package_name);
544 _ACCOUNT_FREE(data->user_name);
546 return ACCOUNT_ERROR_NONE;
549 static int _account_custom_item_free(account_custom_s *data)
551 _ACCOUNT_FREE(data->app_id);
552 _ACCOUNT_FREE(data->key);
553 _ACCOUNT_FREE(data->value);
555 return ACCOUNT_ERROR_NONE;
558 static int _account_custom_gslist_free(GSList* list)
560 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
564 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
565 account_custom_s *custom_data = (account_custom_s*)iter->data;
566 _account_custom_item_free(custom_data);
567 _ACCOUNT_FREE(custom_data);
573 return ACCOUNT_ERROR_NONE;
576 static int _account_free_account_items(account_s *data)
578 _ACCOUNT_FREE(data->user_name);
579 _ACCOUNT_FREE(data->email_address);
580 _ACCOUNT_FREE(data->display_name);
581 _ACCOUNT_FREE(data->icon_path);
582 _ACCOUNT_FREE(data->source);
583 _ACCOUNT_FREE(data->package_name);
584 _ACCOUNT_FREE(data->domain_name);
585 _ACCOUNT_FREE(data->access_token);
588 for(i=0;i<USER_TXT_CNT;i++)
589 _ACCOUNT_FREE(data->user_data_txt[i]);
591 _account_gslist_free(data->capablity_list);
592 _account_glist_free(data->account_list);
593 _account_custom_gslist_free(data->custom_list);
595 return ACCOUNT_ERROR_NONE;
598 static int _account_gslist_free(GSList* list)
600 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
604 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
605 account_capability_s *cap_data = (account_capability_s*)iter->data;
606 _account_free_capability_items(cap_data);
607 _ACCOUNT_FREE(cap_data);
613 return ACCOUNT_ERROR_NONE;
616 static int _account_glist_free(GList* list)
618 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
622 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
623 account_s *account_record = (account_s*)iter->data;
624 _account_free_account_items(account_record);
625 _ACCOUNT_FREE(account_record);
631 return ACCOUNT_ERROR_NONE;
634 static gboolean _account_check_duplicated(account_s *data)
637 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
640 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
642 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and package_name='%s'"
643 , ACCOUNT_TABLE, data->user_name, data->package_name);
645 count = _account_get_record_count(query);
647 ACCOUNT_INFO("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
648 count, data->user_name, data->domain_name );
656 static int _account_get_next_sequence(char *pszName)
659 account_stmt pStmt = NULL;
661 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
663 ACCOUNT_VERBOSE( "[Enter] pszName:%s\n", pszName);
665 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
666 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
667 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
669 rc = sqlite3_step(pStmt);
670 max_seq = sqlite3_column_int(pStmt, 0);
673 ACCOUNT_VERBOSE( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
675 /*Finalize Statement*/
676 rc = sqlite3_finalize(pStmt);
682 static account_stmt _account_prepare_query(char *query)
685 account_stmt pStmt = NULL;
687 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
689 ACCOUNT_INFO( "prepare query : %s", query);
691 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
692 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
697 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
699 assert(NULL != pStmt);
701 return sqlite3_bind_int(pStmt, pos, num);
704 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
706 assert(NULL != pStmt);
709 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
711 return sqlite3_bind_null(pStmt, pos);
714 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
716 ACCOUNT_VERBOSE( "_account_convert_account_to_sql");
719 /*Caution : Keep insert query orders.*/
722 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
724 /* 2. email address*/
725 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
728 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
731 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
734 _account_query_bind_text(hstmt, count++, (char*)account->source);
737 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
740 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
743 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
746 _account_query_bind_int(hstmt, count++, account->auth_type);
749 _account_query_bind_int(hstmt, count++, account->secret);
751 /* 11. sync_support */
752 _account_query_bind_int(hstmt, count++, account->sync_support);
757 for(i=0; i< USER_TXT_CNT; i++)
758 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
760 /* 13. user integer */
761 for(i=0; i< USER_INT_CNT; i++)
762 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
767 static void _account_query_finalize(account_stmt pStmt)
772 ACCOUNT_FATAL( "pStmt is NULL");
776 rc = sqlite3_finalize(pStmt);
777 if (rc != SQLITE_OK) {
778 ACCOUNT_FATAL( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
781 ACCOUNT_VERBOSE( "sqlite3_finalize finish");
784 static int _account_query_step(account_stmt pStmt)
786 assert(NULL != pStmt);
787 return sqlite3_step(pStmt);
790 static int _account_execute_insert_query(account_s *account)
793 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
794 int error_code = ACCOUNT_ERROR_NONE;
795 account_stmt hstmt = NULL;
797 /* check whether app id exist in account type db */
799 if (!account->user_name && !account->display_name && !account->email_address) {
800 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
801 return ACCOUNT_ERROR_INVALID_PARAMETER;
804 /* Check account own icon existence */
805 if(!account->icon_path) {
806 char* temptxt = NULL;
807 account_type_h account_type = NULL;
808 error_code = account_type_create(&account_type);
809 error_code = account_type_query_by_app_id((const char*)account->package_name, &account_type);
810 error_code = account_type_get_icon_path(account_type, &temptxt);
811 if (error_code != ACCOUNT_ERROR_NONE) {
812 ACCOUNT_ERROR("account_type_get_icon_path: Failed \n");
813 } else if (temptxt && strlen(temptxt)) {
814 ACCOUNT_DEBUG("icon path (%s) app_id(%s) \n", temptxt, account->package_name);
815 account->icon_path = _account_get_text(temptxt);
817 ACCOUNT_DEBUG("account_get_icon_path: returned NULL \n");
819 _ACCOUNT_FREE(temptxt);
820 error_code = account_type_destroy(account_type);
823 /* End of Check account own icon existence */
825 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
826 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , "
827 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
828 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values "
829 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?)", ACCOUNT_TABLE);
831 hstmt = _account_prepare_query(query);
832 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
834 _account_convert_account_to_sql(account, hstmt, query);
836 rc = _account_query_step(hstmt);
837 if (rc != SQLITE_DONE) {
838 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
839 error_code = ACCOUNT_ERROR_DB_FAILED;
842 _account_query_finalize(hstmt);
848 static int _account_insert_capability(account_s *account, int account_id)
851 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
852 account_stmt hstmt = NULL;
854 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
856 if (g_slist_length( account->capablity_list)==0) {
857 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
858 return ACCOUNT_ERROR_NONE;
861 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
863 rc = _account_get_record_count(query);
866 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
867 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
874 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
878 account_capability_s* cap_data = NULL;
879 cap_data = (account_capability_s*)iter->data;
881 ACCOUNT_VERBOSE("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
883 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
884 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
885 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
886 hstmt = _account_prepare_query(query);
888 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
890 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
891 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
892 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
893 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
894 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
895 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
896 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
897 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
898 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
899 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
901 rc = _account_query_step(hstmt);
903 if (rc != SQLITE_DONE) {
904 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
908 _account_query_finalize(hstmt);
913 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
915 return ACCOUNT_ERROR_NONE;
918 static int _account_update_capability(account_s *account, int account_id)
921 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
922 account_stmt hstmt = NULL;
924 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
926 if (g_slist_length( account->capablity_list)==0) {
927 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
928 return ACCOUNT_ERROR_NONE;
931 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
933 rc = _account_get_record_count(query);
936 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
937 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
940 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
942 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
943 hstmt = _account_prepare_query(query);
945 _account_query_bind_int(hstmt, count++, (int)account_id);
946 rc = _account_query_step(hstmt);
948 if (rc != SQLITE_DONE) {
949 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
950 return ACCOUNT_ERROR_DB_FAILED;
952 _account_query_finalize(hstmt);
957 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
960 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
961 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
962 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
964 hstmt = _account_prepare_query(query);
966 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
968 account_capability_s* cap_data = NULL;
969 cap_data = (account_capability_s*)iter->data;
971 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
972 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
973 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
974 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
975 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
976 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
977 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
978 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
979 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
980 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
982 rc = _account_query_step(hstmt);
984 if (rc != SQLITE_DONE) {
985 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
989 _account_query_finalize(hstmt);
994 ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
996 return ACCOUNT_ERROR_NONE;
999 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
1002 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1003 account_stmt hstmt = NULL;
1005 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1007 if (g_slist_length( account->capablity_list)==0) {
1008 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
1009 return ACCOUNT_ERROR_NONE;
1012 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
1014 rc = _account_get_record_count(query);
1017 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1018 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1021 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1023 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1024 hstmt = _account_prepare_query(query);
1026 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1027 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1028 rc = _account_query_step(hstmt);
1029 if (rc != SQLITE_DONE) {
1030 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1031 return ACCOUNT_ERROR_DB_FAILED;
1034 _account_query_finalize(hstmt);
1039 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1042 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1043 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1044 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1046 hstmt = _account_prepare_query(query);
1048 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1050 account_capability_s* cap_data = NULL;
1051 cap_data = (account_capability_s*)iter->data;
1053 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1054 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1055 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1056 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1057 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1058 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1059 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1060 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1061 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1062 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1064 rc = _account_query_step(hstmt);
1066 if (rc != SQLITE_DONE) {
1067 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1071 _account_query_finalize(hstmt);
1076 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
1078 return ACCOUNT_ERROR_NONE;
1081 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1083 assert(NULL != pStmt);
1085 return sqlite3_column_int(pStmt, pos);
1088 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
1090 assert(NULL != pStmt);
1092 return (char *)sqlite3_column_text(pStmt, pos);
1095 static void _account_db_data_to_text(const char *textbuf, char **output)
1097 if (textbuf && strlen(textbuf)>0) {
1102 *output = strdup(textbuf);
1106 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1108 char *textbuf = NULL;
1110 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1112 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1113 _account_db_data_to_text(textbuf, &(account_record->user_name));
1115 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1116 _account_db_data_to_text(textbuf, &(account_record->email_address));
1118 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1119 _account_db_data_to_text(textbuf, &(account_record->display_name));
1121 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1122 _account_db_data_to_text(textbuf, &(account_record->icon_path));
1124 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1125 _account_db_data_to_text(textbuf, &(account_record->source));
1127 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1128 _account_db_data_to_text(textbuf, &(account_record->package_name));
1130 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1131 _account_db_data_to_text(textbuf, &(account_record->access_token));
1133 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1134 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1136 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1138 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1140 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1142 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1143 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1145 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1146 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1148 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1149 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1151 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1152 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1154 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1155 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1157 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1158 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1159 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1160 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1161 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1163 ACCOUNT_VERBOSE("END _account_convert_column_to_account");
1166 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1168 char *textbuf = NULL;
1170 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1172 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1173 _account_db_data_to_text(textbuf, &(capability_record->type));
1175 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1177 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1178 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1180 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1181 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1183 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1185 ACCOUNT_VERBOSE("END _account_convert_column_to_capability");
1188 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1190 char *textbuf = NULL;
1192 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1194 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1195 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1197 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1198 _account_db_data_to_text(textbuf, &(custom_record->key));
1200 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1201 _account_db_data_to_text(textbuf, &(custom_record->value));
1203 ACCOUNT_VERBOSE("END _account_convert_column_to_custom");
1206 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1208 account_s *data = (account_s*)user_data;
1210 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1212 if (cap_data == NULL)
1214 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1216 cap_data->type = _account_get_text(capability_type);
1217 cap_data->value = capability_value;
1219 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1221 ACCOUNT_VERBOSE("_account_get_capability_text_cb :: %s\n", capability_type);
1227 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1229 account_s *data = (account_s*)user_data;
1231 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1233 if (custom_data == NULL) {
1234 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1237 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1239 custom_data->account_id = data->id;
1240 custom_data->app_id = _account_get_text(data->package_name);
1241 custom_data->key = _account_get_text(key);
1242 custom_data->value = _account_get_text(value);
1244 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1250 static char *_account_get_text(const char *text_data)
1252 char *text_value = NULL;
1254 if (text_data != NULL) {
1255 text_value = strdup(text_data);
1256 ACCOUNT_VERBOSE("text_value = %s", text_value);
1261 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
1263 int error_code = ACCOUNT_ERROR_NONE;
1264 account_stmt hstmt = NULL;
1265 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1267 account_s *old_account = NULL;
1269 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1270 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1271 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1272 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1274 old_account = (account_s*)calloc(1, sizeof(account_s));
1276 ACCOUNT_FATAL("Memory alloc fail\n");
1277 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1280 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1282 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
1283 hstmt = _account_prepare_query(query);
1285 rc = _account_query_step(hstmt);
1286 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1288 while (rc == SQLITE_ROW) {
1289 _account_convert_column_to_account(hstmt, old_account);
1290 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
1291 rc = _account_query_step(hstmt);
1294 _account_query_finalize(hstmt);
1298 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1299 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1302 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1303 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1306 new_account->id = old_account->id;
1309 if(!new_account->user_name) {
1310 if(old_account->user_name)
1311 new_account->user_name = _account_get_text(old_account->user_name);
1315 if(!new_account->display_name) {
1316 if(old_account->display_name)
1317 new_account->display_name = _account_get_text(old_account->display_name);
1321 if(!new_account->email_address) {
1322 if(old_account->email_address)
1323 new_account->email_address = _account_get_text(old_account->email_address);
1327 if(!new_account->domain_name) {
1328 if(old_account->domain_name)
1329 new_account->domain_name = _account_get_text(old_account->domain_name);
1333 if(!new_account->icon_path) {
1334 if(old_account->icon_path)
1335 new_account->icon_path = _account_get_text(old_account->icon_path);
1339 if(!new_account->source) {
1340 if(old_account->source)
1341 new_account->source = _account_get_text(old_account->source);
1344 _ACCOUNT_FREE(new_account->package_name);
1345 new_account->package_name = _account_get_text(old_account->package_name);
1348 if(!new_account->access_token) {
1349 if(old_account->access_token)
1350 new_account->access_token = _account_get_text(old_account->access_token);
1355 for(i=0;i<USER_TXT_CNT;i++) {
1356 if(!new_account->user_data_txt[i]) {
1357 if(old_account->user_data_txt[i])
1358 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1363 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1364 new_account->auth_type = old_account->auth_type;
1368 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1369 new_account->secret = old_account->secret;
1373 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1374 new_account->sync_support = old_account->sync_support;
1378 for(i=0;i<USER_INT_CNT;i++) {
1379 if(new_account->user_data_int[i] == 0) {
1380 new_account->user_data_int[i] = old_account->user_data_int[i];
1386 // user custom table
1391 if (hstmt != NULL) {
1392 _account_query_finalize(hstmt);
1397 _account_free_account_items(old_account);
1398 _ACCOUNT_FREE(old_account);
1401 return ACCOUNT_ERROR_NONE;
1406 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
1408 int rc = 0, binding_count = 0, count = 0;
1409 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1410 int error_code = ACCOUNT_ERROR_NONE;
1411 account_stmt hstmt = NULL;
1413 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1414 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1416 char* current_appid = NULL;
1417 char* verified_appid = NULL;
1419 current_appid = _account_get_current_appid();
1420 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
1422 _ACCOUNT_FREE(current_appid);
1423 _ACCOUNT_FREE(verified_appid);
1425 if(error_code != ACCOUNT_ERROR_NONE){
1426 ACCOUNT_ERROR("No permission to update\n");
1427 return ACCOUNT_ERROR_PERMISSION_DENIED;
1430 _account_compare_old_record_by_user_name(account, user_name, package_name);
1432 if (!account->package_name) {
1433 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1434 return ACCOUNT_ERROR_INVALID_PARAMETER;
1437 if (!account->user_name && !account->display_name && !account->email_address) {
1438 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1439 return ACCOUNT_ERROR_INVALID_PARAMETER;
1442 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1443 , ACCOUNT_TABLE, user_name, package_name);
1445 count = _account_get_record_count(query);
1447 ACCOUNT_INFO("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1448 count, user_name, package_name);
1449 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1452 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1453 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1454 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1455 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1456 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1458 hstmt = _account_prepare_query(query);
1459 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1461 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1463 _account_query_bind_text(hstmt, binding_count++, user_name);
1464 _account_query_bind_text(hstmt, binding_count++, package_name);
1465 rc = _account_query_step(hstmt);
1466 if (rc != SQLITE_DONE) {
1467 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1469 _account_query_finalize(hstmt);
1472 /*update capability*/
1473 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1476 error_code = _account_update_custom(account, account->id);
1481 ACCOUNT_API int account_insert_to_db(account_h account, int *account_id)
1483 int error_code = ACCOUNT_ERROR_NONE;
1484 int ret_transaction = 0;
1486 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1487 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1488 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1490 account_s *data = (account_s*)account;
1492 pthread_mutex_lock(&account_mutex);
1494 /* transaction control required*/
1495 ret_transaction = _account_begin_transaction();
1497 if (ret_transaction != ACCOUNT_ERROR_NONE) {
1498 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
1499 pthread_mutex_unlock(&account_mutex);
1500 return ret_transaction;
1503 if (_account_check_duplicated(data)) {
1504 ret_transaction = _account_end_transaction(FALSE);
1505 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
1507 pthread_mutex_unlock(&account_mutex);
1508 return ACCOUNT_ERROR_DUPLICATED;
1510 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1513 appid = _account_get_current_appid();
1516 // API caller cannot be recognized
1517 ACCOUNT_ERROR("APP ID not detectable!\n");
1518 ret_transaction = _account_end_transaction(FALSE);
1519 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1520 pthread_mutex_unlock(&account_mutex);
1521 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1524 char* verified_appid = NULL;
1525 error_code = _account_check_account_type_with_appid_group(appid, &verified_appid);
1526 if(error_code != ACCOUNT_ERROR_NONE){
1527 ret_transaction = _account_end_transaction(FALSE);
1528 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1529 _ACCOUNT_FREE(verified_appid);
1530 _ACCOUNT_FREE(appid);
1531 pthread_mutex_unlock(&account_mutex);
1535 _ACCOUNT_FREE(appid);
1538 if(!_account_check_add_more_account(verified_appid)) {
1539 ret_transaction = _account_end_transaction(FALSE);
1540 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1541 pthread_mutex_unlock(&account_mutex);
1542 _ACCOUNT_FREE(verified_appid);
1543 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1546 _ACCOUNT_FREE(data->package_name);
1547 data->package_name = _account_get_text(verified_appid);
1548 _ACCOUNT_FREE(verified_appid);
1551 error_code = _account_execute_insert_query(data);
1553 if (error_code != ACCOUNT_ERROR_NONE) {
1554 ret_transaction = _account_end_transaction(FALSE);
1555 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1557 pthread_mutex_unlock(&account_mutex);
1562 ACCOUNT_VERBOSE( "_account_execute_insert_query, insert error_code : %d", error_code);
1564 error_code = _account_insert_capability(data, *account_id);
1565 if (error_code != ACCOUNT_ERROR_NONE) {
1566 ret_transaction = _account_end_transaction(FALSE);
1567 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1569 pthread_mutex_unlock(&account_mutex);
1573 error_code = _account_insert_custom(data, *account_id);
1574 if (error_code != ACCOUNT_ERROR_NONE) {
1575 ret_transaction = _account_end_transaction(FALSE);
1576 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1578 pthread_mutex_unlock(&account_mutex);
1582 pthread_mutex_unlock(&account_mutex);
1583 _account_end_transaction(TRUE);
1586 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1587 _account_insert_delete_update_notification_send(buf);
1589 return ACCOUNT_ERROR_NONE;
1593 ACCOUNT_API int account_create(account_h *account)
1596 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1597 return ACCOUNT_ERROR_INVALID_PARAMETER;
1600 account_s *data = (account_s*)malloc(sizeof(account_s));
1603 ACCOUNT_FATAL("Memory Allocation Failed");
1604 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1606 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1608 ACCOUNT_VERBOSE("create handle=%p\n", *account);
1610 /*Setting account as visible by default*/
1611 data->secret = ACCOUNT_SECRECY_VISIBLE;
1613 /*Setting account as not supporting sync by default*/
1614 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
1616 *account = (account_h)data;
1618 return ACCOUNT_ERROR_NONE;
1621 ACCOUNT_API int account_set_user_name(account_h account, const char *user_name)
1624 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1625 return ACCOUNT_ERROR_INVALID_PARAMETER;
1629 ACCOUNT_ERROR("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1630 return ACCOUNT_ERROR_INVALID_PARAMETER;
1633 account_s *data = (account_s*)account;
1635 _ACCOUNT_FREE(data->user_name);
1636 data->user_name = _account_get_text(user_name);
1638 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1640 return ACCOUNT_ERROR_NONE;
1643 ACCOUNT_API int account_set_display_name(account_h account, const char *display_name)
1646 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1647 return ACCOUNT_ERROR_INVALID_PARAMETER;
1650 if (!display_name) {
1651 ACCOUNT_ERROR("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1652 return ACCOUNT_ERROR_INVALID_PARAMETER;
1655 account_s *data = (account_s*)account;
1657 _ACCOUNT_FREE(data->display_name);
1658 data->display_name = _account_get_text(display_name);
1660 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1662 return ACCOUNT_ERROR_NONE;
1665 ACCOUNT_API int account_set_email_address(account_h account, const char *email_address)
1668 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1669 return ACCOUNT_ERROR_INVALID_PARAMETER;
1672 if (!email_address) {
1673 ACCOUNT_ERROR("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1674 return ACCOUNT_ERROR_INVALID_PARAMETER;
1677 account_s *data = (account_s*)account;
1679 _ACCOUNT_FREE(data->email_address);
1680 data->email_address = _account_get_text(email_address);
1682 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1684 return ACCOUNT_ERROR_NONE;
1687 ACCOUNT_API int account_set_icon_path(account_h account, const char *icon_path)
1690 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1691 return ACCOUNT_ERROR_INVALID_PARAMETER;
1695 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1696 return ACCOUNT_ERROR_INVALID_PARAMETER;
1699 account_s *data = (account_s*)account;
1701 _ACCOUNT_FREE(data->icon_path);
1702 data->icon_path = _account_get_text(icon_path);
1704 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1706 return ACCOUNT_ERROR_NONE;
1709 ACCOUNT_API int account_set_source(account_h account, const char *source)
1712 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1713 return ACCOUNT_ERROR_INVALID_PARAMETER;
1717 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1718 return ACCOUNT_ERROR_INVALID_PARAMETER;
1720 account_s *data = (account_s*)account;
1722 _ACCOUNT_FREE(data->source);
1723 data->source = _account_get_text(source);
1725 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1727 return ACCOUNT_ERROR_NONE;
1730 ACCOUNT_API int account_set_package_name(account_h account, const char *package_name)
1733 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1734 return ACCOUNT_ERROR_INVALID_PARAMETER;
1737 if (!package_name) {
1738 ACCOUNT_ERROR("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1739 return ACCOUNT_ERROR_INVALID_PARAMETER;
1742 account_s *data = (account_s*)account;
1744 _ACCOUNT_FREE(data->package_name);
1745 data->package_name = _account_get_text(package_name);
1747 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1749 return ACCOUNT_ERROR_NONE;
1752 ACCOUNT_API int account_set_domain_name(account_h account, const char *domain_name)
1755 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1756 return ACCOUNT_ERROR_INVALID_PARAMETER;
1760 ACCOUNT_ERROR("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1761 return ACCOUNT_ERROR_INVALID_PARAMETER;
1763 account_s *data = (account_s*)account;
1765 _ACCOUNT_FREE(data->domain_name);
1766 data->domain_name = _account_get_text(domain_name);
1768 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1770 return ACCOUNT_ERROR_NONE;
1773 ACCOUNT_API int account_set_access_token(account_h account, const char *access_token)
1776 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1777 return ACCOUNT_ERROR_INVALID_PARAMETER;
1780 if (!access_token) {
1781 ACCOUNT_ERROR("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1782 return ACCOUNT_ERROR_INVALID_PARAMETER;
1785 account_s *data = (account_s*)account;
1787 _ACCOUNT_FREE(data->access_token);
1788 data->access_token = _account_get_text(access_token);
1790 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1792 return ACCOUNT_ERROR_NONE;
1795 ACCOUNT_API int account_set_user_text(account_h account, int index, const char *user_txt)
1798 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1799 return ACCOUNT_ERROR_INVALID_PARAMETER;
1803 ACCOUNT_ERROR("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1804 return ACCOUNT_ERROR_INVALID_PARAMETER;
1806 if (index >= USER_TXT_CNT || index < 0) {
1807 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1808 return ACCOUNT_ERROR_INVALID_PARAMETER;
1811 account_s *data = (account_s*)account;
1813 _ACCOUNT_FREE(data->user_data_txt[index]);
1814 data->user_data_txt[index] = _account_get_text(user_txt);
1816 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1818 return ACCOUNT_ERROR_NONE;
1821 ACCOUNT_API int account_set_custom(account_h account, const char* key, const char* value)
1824 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1825 return ACCOUNT_ERROR_INVALID_PARAMETER;
1829 ACCOUNT_ERROR("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__);
1830 return ACCOUNT_ERROR_INVALID_PARAMETER;
1834 ACCOUNT_ERROR("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__);
1835 return ACCOUNT_ERROR_INVALID_PARAMETER;
1838 account_s *data = (account_s*)account;
1841 bool b_is_new = TRUE;
1843 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1845 account_custom_s* custom_data = NULL;
1846 custom_data = (account_custom_s*)iter->data;
1847 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
1849 if(!strcmp(custom_data->key, key)) {
1850 _ACCOUNT_FREE(custom_data->value);
1851 custom_data->value = _account_get_text(value);
1857 account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1859 if (custom_data == NULL) {
1860 ACCOUNT_FATAL("(%s)-(%d) MALLOC FAIL\n", __FUNCTION__, __LINE__);
1861 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1863 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1864 custom_data->account_id = data->id;
1865 custom_data->app_id = _account_get_text(data->package_name);
1866 custom_data->key = _account_get_text(key);
1867 custom_data->value = _account_get_text(value);
1868 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1871 return ACCOUNT_ERROR_NONE;
1874 ACCOUNT_API int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1876 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1878 if ( (auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
1879 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1880 return ACCOUNT_ERROR_INVALID_PARAMETER;
1883 account_s *data = (account_s*)account;
1885 data->auth_type = (int)auth_type;
1887 return ACCOUNT_ERROR_NONE;
1890 ACCOUNT_API int account_set_secret(account_h account, const account_secrecy_state_e secret)
1892 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1894 if ( (secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
1895 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1896 return ACCOUNT_ERROR_INVALID_PARAMETER;
1899 account_s *data = (account_s*)account;
1901 data->secret = (int)secret;
1903 return ACCOUNT_ERROR_NONE;
1906 ACCOUNT_API int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
1908 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1910 if ( (sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) {
1911 ACCOUNT_ERROR("(%s)-(%d) sync_support is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1912 return ACCOUNT_ERROR_INVALID_PARAMETER;
1915 account_s *data = (account_s*)account;
1917 data->sync_support= (int)sync_support;
1919 return ACCOUNT_ERROR_NONE;
1922 ACCOUNT_API int account_set_user_int(account_h account, int index, const int user_int)
1925 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1926 return ACCOUNT_ERROR_INVALID_PARAMETER;
1929 if (index >= USER_INT_CNT ||index < 0) {
1930 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1931 return ACCOUNT_ERROR_INVALID_PARAMETER;
1934 account_s *data = (account_s*)account;
1936 data->user_data_int[index] = user_int;
1938 return ACCOUNT_ERROR_NONE;
1941 ACCOUNT_API int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value)
1943 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null"));
1944 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null"));
1946 if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
1947 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1948 return ACCOUNT_ERROR_INVALID_PARAMETER;
1951 account_s *data = (account_s*)account;
1953 GSList *iter = NULL;
1954 bool b_is_new = TRUE;
1956 for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1957 account_capability_s *cap_data = NULL;
1958 cap_data = (account_capability_s*)iter->data;
1960 if(!strcmp(cap_data->type, capability_type)) {
1961 cap_data->value = capability_value;
1968 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1970 if (cap_data == NULL)
1971 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1972 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1974 cap_data->type = _account_get_text(capability_type);
1975 cap_data->value = capability_value;
1976 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1979 return ACCOUNT_ERROR_NONE;
1982 ACCOUNT_API int account_get_user_name(account_h account, char **user_name)
1985 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1986 return ACCOUNT_ERROR_INVALID_PARAMETER;
1990 ACCOUNT_ERROR("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
1991 return ACCOUNT_ERROR_INVALID_PARAMETER;
1994 account_s *data = (account_s*)account;
1996 (*user_name) = NULL;
1997 *user_name = _account_get_text(data->user_name);
1999 return ACCOUNT_ERROR_NONE;
2002 ACCOUNT_API int account_get_display_name(account_h account, char **display_name)
2005 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2006 return ACCOUNT_ERROR_INVALID_PARAMETER;
2009 if (!display_name) {
2010 ACCOUNT_ERROR("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
2011 return ACCOUNT_ERROR_INVALID_PARAMETER;
2014 account_s *data = (account_s*)account;
2016 (*display_name) = NULL;
2018 *display_name = _account_get_text(data->display_name);
2020 return ACCOUNT_ERROR_NONE;
2023 ACCOUNT_API int account_get_email_address(account_h account,char **email_address)
2026 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2027 return ACCOUNT_ERROR_INVALID_PARAMETER;
2030 if (!email_address) {
2031 ACCOUNT_ERROR("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
2032 return ACCOUNT_ERROR_INVALID_PARAMETER;
2035 account_s *data = (account_s*)account;
2037 (*email_address) = NULL;
2039 *email_address = _account_get_text(data->email_address);
2041 return ACCOUNT_ERROR_NONE;
2044 ACCOUNT_API int account_get_icon_path(account_h account, char **icon_path)
2047 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2048 return ACCOUNT_ERROR_INVALID_PARAMETER;
2052 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
2053 return ACCOUNT_ERROR_INVALID_PARAMETER;
2056 account_s *data = (account_s*)account;
2058 (*icon_path) = NULL;
2060 *icon_path = _account_get_text(data->icon_path);
2062 return ACCOUNT_ERROR_NONE;
2065 ACCOUNT_API int account_get_source(account_h account, char **source)
2068 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2069 return ACCOUNT_ERROR_INVALID_PARAMETER;
2073 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
2074 return ACCOUNT_ERROR_INVALID_PARAMETER;
2077 account_s *data = (account_s*)account;
2081 *source = _account_get_text(data->source);
2083 return ACCOUNT_ERROR_NONE;
2086 ACCOUNT_API int account_get_package_name(account_h account, char **package_name)
2089 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2090 return ACCOUNT_ERROR_INVALID_PARAMETER;
2093 if (!package_name) {
2094 ACCOUNT_ERROR("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
2095 return ACCOUNT_ERROR_INVALID_PARAMETER;
2098 account_s *data = (account_s*)account;
2100 (*package_name) = NULL;
2102 *package_name = _account_get_text(data->package_name);
2104 return ACCOUNT_ERROR_NONE;
2107 ACCOUNT_API int account_get_domain_name(account_h account, char **domain_name)
2110 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2111 return ACCOUNT_ERROR_INVALID_PARAMETER;
2115 ACCOUNT_ERROR("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
2116 return ACCOUNT_ERROR_INVALID_PARAMETER;
2119 account_s *data = (account_s*)account;
2121 (*domain_name) = NULL;
2123 *domain_name = _account_get_text(data->domain_name);
2125 return ACCOUNT_ERROR_NONE;
2128 ACCOUNT_API int account_get_access_token(account_h account, char **access_token)
2131 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2132 return ACCOUNT_ERROR_INVALID_PARAMETER;
2135 if (!access_token) {
2136 ACCOUNT_ERROR("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
2137 return ACCOUNT_ERROR_INVALID_PARAMETER;
2140 account_s *data = (account_s*)account;
2142 (*access_token) = NULL;
2144 *access_token = _account_get_text(data->access_token);
2146 return ACCOUNT_ERROR_NONE;
2149 ACCOUNT_API int account_get_user_text(account_h account, int user_text_index, char **text)
2152 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2153 return ACCOUNT_ERROR_INVALID_PARAMETER;
2157 ACCOUNT_ERROR("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
2158 return ACCOUNT_ERROR_INVALID_PARAMETER;
2160 ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2162 account_s *data = (account_s*)account;
2166 *text = _account_get_text(data->user_data_txt[user_text_index]);
2168 return ACCOUNT_ERROR_NONE;
2171 ACCOUNT_API int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
2174 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2175 return ACCOUNT_ERROR_INVALID_PARAMETER;
2178 ACCOUNT_ERROR("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
2179 return ACCOUNT_ERROR_INVALID_PARAMETER;
2182 account_s* data = (account_s*)account;
2184 *auth_type = data->auth_type;
2186 return ACCOUNT_ERROR_NONE;
2189 ACCOUNT_API int account_get_secret(account_h account, account_secrecy_state_e *secret)
2192 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2193 return ACCOUNT_ERROR_INVALID_PARAMETER;
2196 ACCOUNT_ERROR("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
2197 return ACCOUNT_ERROR_INVALID_PARAMETER;
2200 account_s* data = (account_s*)account;
2202 *secret = data->secret;
2204 return ACCOUNT_ERROR_NONE;
2207 ACCOUNT_API int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
2210 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2211 return ACCOUNT_ERROR_INVALID_PARAMETER;
2213 if (!sync_support) {
2214 ACCOUNT_ERROR("(%s)-(%d) sync_support is NULL.\n", __FUNCTION__, __LINE__);
2215 return ACCOUNT_ERROR_INVALID_PARAMETER;
2218 account_s* data = (account_s*)account;
2220 *sync_support = data->sync_support;
2222 return ACCOUNT_ERROR_NONE;
2225 ACCOUNT_API int account_get_account_id(account_h account, int *account_id)
2228 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2229 return ACCOUNT_ERROR_INVALID_PARAMETER;
2232 ACCOUNT_ERROR("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
2233 return ACCOUNT_ERROR_INVALID_PARAMETER;
2236 account_s *data = (account_s*)account;
2238 *account_id = data->id;
2240 return ACCOUNT_ERROR_NONE;
2243 ACCOUNT_API int account_get_user_int(account_h account, int user_int_index, int *integer)
2246 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2247 return ACCOUNT_ERROR_INVALID_PARAMETER;
2250 ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2253 ACCOUNT_ERROR("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
2254 return ACCOUNT_ERROR_INVALID_PARAMETER;
2257 account_s *data = (account_s*)account;
2259 *integer = data->user_data_int[user_int_index];
2261 return ACCOUNT_ERROR_NONE;
2264 ACCOUNT_API int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value)
2266 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2267 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL"));
2268 ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL"));
2271 account_s *data = (account_s*)account;
2273 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2274 account_capability_s *cap_data = NULL;
2276 cap_data = (account_capability_s*)iter->data;
2278 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2280 if(!strcmp(capability_type, cap_data->type)) {
2281 *capability_value = cap_data->value;
2282 return ACCOUNT_ERROR_NONE;
2286 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2289 ACCOUNT_API int account_get_capability_all(account_h account, capability_cb cb_func, void *user_data)
2291 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2292 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2295 account_s *data = (account_s*)account;
2297 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2298 account_capability_s *cap_data = NULL;
2300 cap_data = (account_capability_s*)iter->data;
2302 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2304 //cb_func(cap_data->type, cap_data->value, user_data);
2305 if(cb_func(cap_data->type, cap_data->value, user_data)!=TRUE){
2306 ACCOUNT_VERBOSE("account_get_capability :: cb_func returns false, it is stopped\n");
2307 return ACCOUNT_ERROR_NONE;
2311 return ACCOUNT_ERROR_NONE;
2314 ACCOUNT_API int account_get_custom(account_h account, const char* key, char** value)
2316 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2317 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST"));
2318 ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL"));
2321 account_s *data = (account_s*)account;
2323 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2324 account_custom_s *custom_data = NULL;
2326 custom_data = (account_custom_s*)iter->data;
2328 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2330 if(!strcmp(key, custom_data->key)) {
2332 *value = _account_get_text(custom_data->value);
2333 return ACCOUNT_ERROR_NONE;
2337 ACCOUNT_INFO("key is not found %s", key);
2339 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2342 ACCOUNT_API int account_get_custom_all(account_h account, account_custom_cb cb_func, void* user_data)
2344 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2345 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2348 account_s *data = (account_s*)account;
2350 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2351 bool cb_ret = FALSE;
2352 account_custom_s *custom_data = NULL;
2354 custom_data = (account_custom_s*)iter->data;
2356 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2358 cb_ret = cb_func(custom_data->key, custom_data->value, user_data);
2360 ACCOUNT_INFO("account_get_custom_all callback func ret = %d", cb_ret);
2365 return ACCOUNT_ERROR_NONE;
2368 ACCOUNT_API int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
2370 int error_code = ACCOUNT_ERROR_NONE;
2371 account_stmt hstmt = NULL;
2372 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2375 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2376 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2377 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2379 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2381 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2382 hstmt = _account_prepare_query(query);
2384 rc = _account_query_step(hstmt);
2385 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2387 account_capability_s* capability_record = NULL;
2389 while (rc == SQLITE_ROW) {
2390 bool cb_ret = FALSE;
2391 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2393 if (capability_record == NULL) {
2394 ACCOUNT_FATAL("malloc Failed");
2398 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2400 _account_convert_column_to_capability(hstmt, capability_record);
2402 cb_ret = cb_func(capability_record->type, capability_record->value, user_data);
2404 _account_free_capability_items(capability_record);
2405 _ACCOUNT_FREE(capability_record);
2407 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2409 rc = _account_query_step(hstmt);
2412 _account_query_finalize(hstmt);
2415 error_code = ACCOUNT_ERROR_NONE;
2418 if (hstmt != NULL) {
2419 _account_query_finalize(hstmt);
2423 pthread_mutex_unlock(&account_mutex);
2427 static int _account_compare_old_record(account_s *new_account, int account_id)
2429 int error_code = ACCOUNT_ERROR_NONE;
2430 account_stmt hstmt = NULL;
2431 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2433 account_s *old_account = NULL;
2435 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2436 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2437 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2439 old_account = (account_s*)calloc(1, sizeof(account_s));
2441 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2443 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2444 hstmt = _account_prepare_query(query);
2446 rc = _account_query_step(hstmt);
2447 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2449 while (rc == SQLITE_ROW) {
2450 _account_convert_column_to_account(hstmt, old_account);
2451 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
2452 rc = _account_query_step(hstmt);
2455 _account_query_finalize(hstmt);
2459 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2460 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2463 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2464 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2468 new_account->id = old_account->id;
2471 if(!new_account->user_name) {
2472 if(old_account->user_name)
2473 new_account->user_name = _account_get_text(old_account->user_name);
2477 if(!new_account->display_name) {
2478 if(old_account->display_name)
2479 new_account->display_name = _account_get_text(old_account->display_name);
2483 if(!new_account->email_address) {
2484 if(old_account->email_address)
2485 new_account->email_address = _account_get_text(old_account->email_address);
2489 if(!new_account->domain_name) {
2490 if(old_account->domain_name)
2491 new_account->domain_name = _account_get_text(old_account->domain_name);
2495 if(!new_account->icon_path) {
2496 if(old_account->icon_path)
2497 new_account->icon_path = _account_get_text(old_account->icon_path);
2501 if(!new_account->source) {
2502 if(old_account->source)
2503 new_account->source = _account_get_text(old_account->source);
2506 _ACCOUNT_FREE(new_account->package_name);
2507 new_account->package_name = _account_get_text(old_account->package_name);
2510 if(!new_account->access_token) {
2511 if(old_account->access_token)
2512 new_account->access_token = _account_get_text(old_account->access_token);
2517 for(i=0;i<USER_TXT_CNT;i++) {
2518 if(!new_account->user_data_txt[i]) {
2519 if(old_account->user_data_txt[i])
2520 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2525 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2526 new_account->auth_type = old_account->auth_type;
2530 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2531 new_account->secret = old_account->secret;
2535 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2536 new_account->sync_support = old_account->sync_support;
2540 for(i=0;i<USER_INT_CNT;i++) {
2541 if(new_account->user_data_int[i] == 0) {
2542 new_account->user_data_int[i] = old_account->user_data_int[i];
2548 // user custom table
2553 if (hstmt != NULL) {
2554 _account_query_finalize(hstmt);
2559 _account_free_account_items(old_account);
2560 _ACCOUNT_FREE(old_account);
2563 return ACCOUNT_ERROR_NONE;
2566 static int _account_update_account(account_s *account, int account_id)
2568 int rc = 0, binding_count =0;
2569 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2570 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2571 account_stmt hstmt = NULL;
2573 if (!account->package_name) {
2574 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2575 return ACCOUNT_ERROR_INVALID_PARAMETER;
2578 char* current_appid = NULL;
2579 char* verified_appid = NULL;
2581 current_appid = _account_get_current_appid();
2582 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
2584 _ACCOUNT_FREE(current_appid);
2585 _ACCOUNT_FREE(verified_appid);
2587 if(error_code != ACCOUNT_ERROR_NONE){
2588 ACCOUNT_ERROR("No permission to update\n");
2589 return ACCOUNT_ERROR_PERMISSION_DENIED;
2592 _account_compare_old_record(account, account_id);
2594 if (!account->user_name && !account->display_name && !account->email_address) {
2595 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2596 return ACCOUNT_ERROR_INVALID_PARAMETER;
2599 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2601 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2603 count = _account_get_record_count(query);
2605 ACCOUNT_WARNING(" Account record not found, count = %d\n", count);
2606 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2609 /* transaction control required*/
2610 ret_transaction = _account_begin_transaction();
2612 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2613 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2614 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2615 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2616 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2618 hstmt = _account_prepare_query(query);
2619 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE)));
2621 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2622 _account_query_bind_int(hstmt, binding_count++, account_id);
2624 rc = _account_query_step(hstmt);
2625 if (rc != SQLITE_DONE) {
2626 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2629 _account_query_finalize(hstmt);
2632 /*update capability*/
2633 error_code = _account_update_capability(account, account_id);
2634 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2635 ret_transaction = _account_end_transaction(FALSE);
2636 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2641 error_code = _account_update_custom(account, account_id);
2642 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2643 ret_transaction = _account_end_transaction(FALSE);
2644 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2648 ret_transaction = _account_end_transaction(TRUE);
2649 ACCOUNT_DEBUG("update capability success, trying to commit(%x) !!!\n", ret_transaction);
2654 ACCOUNT_API int account_update_to_db_by_id(const account_h account, int account_id)
2656 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2657 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2658 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2659 int error_code = ACCOUNT_ERROR_NONE;
2660 account_s* data = (account_s*)account;
2662 pthread_mutex_lock(&account_mutex);
2664 error_code = _account_update_account(data, account_id);
2666 if(error_code != ACCOUNT_ERROR_NONE) {
2667 pthread_mutex_unlock(&account_mutex);
2671 pthread_mutex_unlock(&account_mutex);
2674 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2675 _account_insert_delete_update_notification_send(buf);
2677 return ACCOUNT_ERROR_NONE;
2680 ACCOUNT_API int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
2682 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2683 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2684 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2685 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2687 int error_code = ACCOUNT_ERROR_NONE;
2688 account_s *data = (account_s*)account;
2690 pthread_mutex_lock(&account_mutex);
2692 error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
2694 pthread_mutex_unlock(&account_mutex);
2697 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2698 _account_insert_delete_update_notification_send(buf);
2703 ACCOUNT_API int account_foreach_account_from_db(account_cb callback, void *user_data)
2705 int error_code = ACCOUNT_ERROR_NONE;
2706 account_stmt hstmt = NULL;
2707 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2709 GList *account_list = NULL;
2711 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
2712 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2714 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2716 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2717 hstmt = _account_prepare_query(query);
2719 rc = _account_query_step(hstmt);
2721 account_s *account_record = NULL;
2723 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2726 while(rc == SQLITE_ROW) {
2727 account_record = (account_s*) malloc(sizeof(account_s));
2729 if (account_record == NULL) {
2730 ACCOUNT_FATAL("malloc Failed");
2734 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2735 _account_convert_column_to_account(hstmt, account_record);
2736 account_list = g_list_append(account_list, account_record);
2737 rc = _account_query_step(hstmt);
2741 _account_query_finalize(hstmt);
2746 for (iter = account_list; iter != NULL; iter = g_list_next(iter)) {
2747 account_s *account = NULL;
2748 account = (account_s*)iter->data;
2749 account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2750 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2751 callback((account_h)account, user_data);
2755 error_code = ACCOUNT_ERROR_NONE;
2758 if (hstmt != NULL) {
2759 _account_query_finalize(hstmt);
2763 _account_glist_free(account_list);
2764 account_list = NULL;
2770 ACCOUNT_API int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status)
2772 int error_code = ACCOUNT_ERROR_NONE;
2773 account_stmt hstmt = NULL;
2774 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2778 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2779 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2780 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2781 ACCOUNT_ERROR("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2782 return ACCOUNT_ERROR_INVALID_PARAMETER;
2785 pthread_mutex_lock(&account_mutex);
2787 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2789 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2791 rc = _account_get_record_count(query);
2794 ACCOUNT_ERROR( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2795 pthread_mutex_unlock(&account_mutex);
2796 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2799 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2801 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2802 hstmt = _account_prepare_query(query);
2804 _account_query_bind_int(hstmt, count, (int)sync_status);
2806 rc = _account_query_step(hstmt);
2808 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2809 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2811 _account_query_finalize(hstmt);
2814 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2815 _account_insert_delete_update_notification_send(buf);
2818 error_code = ACCOUNT_ERROR_NONE;
2821 if (hstmt != NULL) {
2822 _account_query_finalize(hstmt);
2826 pthread_mutex_unlock(&account_mutex);
2830 ACCOUNT_API int account_query_account_by_account_id(int account_db_id, account_h *account)
2832 int error_code = ACCOUNT_ERROR_NONE;
2833 account_stmt hstmt = NULL;
2834 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2837 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2838 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2839 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2841 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2843 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2844 hstmt = _account_prepare_query(query);
2846 rc = _account_query_step(hstmt);
2847 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2849 account_s *account_record = (account_s *)(*account);
2851 while (rc == SQLITE_ROW) {
2852 _account_convert_column_to_account(hstmt, account_record);
2853 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
2854 rc = _account_query_step(hstmt);
2857 _account_query_finalize(hstmt);
2858 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2859 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2862 error_code = ACCOUNT_ERROR_NONE;
2865 if (hstmt != NULL) {
2866 _account_query_finalize(hstmt);
2870 pthread_mutex_unlock(&account_mutex);
2874 ACCOUNT_API int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
2876 int error_code = ACCOUNT_ERROR_NONE;
2877 account_stmt hstmt = NULL;
2878 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2881 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2882 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2884 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2886 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2888 hstmt = _account_prepare_query(query);
2890 int binding_count = 1;
2891 _account_query_bind_text(hstmt, binding_count++, user_name);
2893 rc = _account_query_step(hstmt);
2895 account_s *account_head = NULL;
2897 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2901 account_head = (account_s*) malloc(sizeof(account_s));
2902 if (account_head == NULL) {
2903 ACCOUNT_FATAL("malloc Failed");
2904 if (hstmt != NULL) {
2905 _account_query_finalize(hstmt);
2908 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2910 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2912 while (rc == SQLITE_ROW) {
2913 account_s* account_record = NULL;
2915 account_record = (account_s*) malloc(sizeof(account_s));
2917 if (account_record == NULL) {
2918 ACCOUNT_FATAL("malloc Failed");
2921 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2923 _account_convert_column_to_account(hstmt, account_record);
2925 account_head->account_list = g_list_append(account_head->account_list, account_record);
2927 rc = _account_query_step(hstmt);
2929 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2932 _account_query_finalize(hstmt);
2938 tmp = g_list_length(account_head->account_list);
2939 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2941 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2943 account = (account_h)iter->data;
2945 account_s *testaccount = (account_s*)account;
2947 ACCOUNT_VERBOSE("id = %d", testaccount->id);
2948 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
2949 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
2950 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
2951 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
2952 ACCOUNT_VERBOSE("source = %s", testaccount->source);
2953 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
2954 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
2955 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
2956 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
2957 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
2958 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
2960 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2961 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2963 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2965 //cb_func(account, user_data);
2966 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("cb_func returns false, it is stopped.\n"));
2970 error_code = ACCOUNT_ERROR_NONE;
2973 if (hstmt != NULL) {
2974 _account_query_finalize(hstmt);
2978 if (account_head->account_list) {
2979 _account_glist_free(account_head->account_list);
2980 account_head->account_list = NULL;
2981 _account_free_account_items(account_head);
2983 _ACCOUNT_FREE(account_head);
2986 pthread_mutex_unlock(&account_mutex);
2990 ACCOUNT_API int account_query_account_by_capability(account_cb cb_func, const char* capability_type, account_capability_state_e capability_value, void* user_data)
2992 int error_code = ACCOUNT_ERROR_NONE;
2993 account_stmt hstmt = NULL;
2994 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2997 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
2999 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
3000 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
3001 return ACCOUNT_ERROR_INVALID_PARAMETER;
3004 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3005 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3007 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3009 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3011 hstmt = _account_prepare_query(query);
3013 int binding_count = 1;
3014 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3015 _account_query_bind_int(hstmt, binding_count++, (int)capability_value);
3017 rc = _account_query_step(hstmt);
3019 account_s* account_head = NULL;
3021 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3025 account_head = (account_s*) malloc(sizeof(account_s));
3026 if (account_head == NULL) {
3027 ACCOUNT_FATAL("malloc Failed");
3028 if (hstmt != NULL) {
3029 _account_query_finalize(hstmt);
3032 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3034 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3036 while (rc == SQLITE_ROW) {
3037 account_s* account_record = NULL;
3039 account_record = (account_s*) malloc(sizeof(account_s));
3041 if (account_record == NULL) {
3042 ACCOUNT_FATAL("malloc Failed");
3045 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3047 _account_convert_column_to_account(hstmt, account_record);
3049 account_head->account_list = g_list_append(account_head->account_list, account_record);
3051 rc = _account_query_step(hstmt);
3053 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3056 _account_query_finalize(hstmt);
3062 tmp = g_list_length(account_head->account_list);
3063 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3065 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3066 account_h account = NULL;
3067 account = (account_h)iter->data;
3068 account_s* testaccount = (account_s*)account;
3070 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3071 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3073 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3075 //cb_func(account, user_data);
3076 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("cb_func returns false, it is stopped.\n"));
3080 error_code = ACCOUNT_ERROR_NONE;
3083 if (hstmt != NULL) {
3084 _account_query_finalize(hstmt);
3088 if (account_head->account_list) {
3089 _account_glist_free(account_head->account_list);
3090 account_head->account_list = NULL;
3091 _account_free_account_items(account_head);
3093 _ACCOUNT_FREE(account_head);
3096 pthread_mutex_unlock(&account_mutex);
3100 ACCOUNT_API int account_query_account_by_capability_type(account_cb cb_func, const char* capability_type, void* user_data)
3102 int error_code = ACCOUNT_ERROR_NONE;
3103 account_stmt hstmt = NULL;
3104 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3107 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3108 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3109 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3111 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3113 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3115 hstmt = _account_prepare_query(query);
3117 int binding_count = 1;
3118 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3120 rc = _account_query_step(hstmt);
3122 account_s* account_head = NULL;
3124 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3128 account_head = (account_s*) malloc(sizeof(account_s));
3129 if (account_head == NULL) {
3130 ACCOUNT_FATAL("malloc Failed");
3131 if (hstmt != NULL) {
3132 _account_query_finalize(hstmt);
3135 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3137 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3139 while (rc == SQLITE_ROW) {
3140 account_s* account_record = NULL;
3142 account_record = (account_s*) malloc(sizeof(account_s));
3144 if (account_record == NULL) {
3145 ACCOUNT_FATAL("malloc Failed");
3148 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3150 _account_convert_column_to_account(hstmt, account_record);
3152 account_head->account_list = g_list_append(account_head->account_list, account_record);
3154 rc = _account_query_step(hstmt);
3156 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3159 _account_query_finalize(hstmt);
3165 tmp = g_list_length(account_head->account_list);
3166 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3168 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3169 account_h account = NULL;
3170 account = (account_h)iter->data;
3171 account_s* testaccount = (account_s*)account;
3173 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3174 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3176 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3178 //cb_func(account, user_data);
3179 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n"));
3183 error_code = ACCOUNT_ERROR_NONE;
3186 if (hstmt != NULL) {
3187 _account_query_finalize(hstmt);
3191 if (account_head->account_list) {
3192 _account_glist_free(account_head->account_list);
3193 account_head->account_list = NULL;
3194 _account_free_account_items(account_head);
3196 _ACCOUNT_FREE(account_head);
3199 pthread_mutex_unlock(&account_mutex);
3203 ACCOUNT_API int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
3205 int error_code = ACCOUNT_ERROR_NONE;
3206 account_stmt hstmt = NULL;
3207 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3210 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
3211 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3212 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3214 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3216 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3218 hstmt = _account_prepare_query(query);
3220 int binding_count = 1;
3221 _account_query_bind_text(hstmt, binding_count++, package_name);
3223 rc = _account_query_step(hstmt);
3225 account_s* account_head = NULL;
3227 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3231 account_head = (account_s*) malloc(sizeof(account_s));
3232 if (account_head == NULL) {
3233 ACCOUNT_FATAL("malloc Failed");
3234 if (hstmt != NULL) {
3235 _account_query_finalize(hstmt);
3238 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3240 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3242 while (rc == SQLITE_ROW) {
3243 account_s* account_record = NULL;
3245 account_record = (account_s*) malloc(sizeof(account_s));
3247 if (account_record == NULL) {
3248 ACCOUNT_FATAL("malloc Failed");
3251 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3253 _account_convert_column_to_account(hstmt, account_record);
3255 account_head->account_list = g_list_append(account_head->account_list, account_record);
3257 rc = _account_query_step(hstmt);
3259 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3262 _account_query_finalize(hstmt);
3267 tmp = g_list_length(account_head->account_list);
3268 ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
3270 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3271 account_h account = NULL;
3272 account = (account_h)iter->data;
3274 account_s* testaccount = (account_s*)account;
3276 ACCOUNT_VERBOSE("id = %d", testaccount->id);
3277 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
3278 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
3279 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
3280 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
3281 ACCOUNT_VERBOSE("source = %s", testaccount->source);
3282 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
3283 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
3284 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
3285 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
3286 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
3287 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
3289 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3290 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3292 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3294 //cb_func(account, user_data);
3295 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n"));
3299 error_code = ACCOUNT_ERROR_NONE;
3302 if (hstmt != NULL) {
3303 _account_query_finalize(hstmt);
3307 if (account_head->account_list) {
3308 _account_glist_free(account_head->account_list);
3309 account_head->account_list = NULL;
3310 _account_free_account_items(account_head);
3312 _ACCOUNT_FREE(account_head);
3315 pthread_mutex_unlock(&account_mutex);
3319 ACCOUNT_API int account_delete(int account_id)
3321 int error_code = ACCOUNT_ERROR_NONE;
3322 account_stmt hstmt = NULL;
3323 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3325 int ret_transaction = 0;
3326 bool is_success = FALSE;
3328 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3331 account_h account = NULL;
3332 /* Check requested ID to delete */
3333 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3335 count = _account_get_record_count(query);
3337 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3338 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3341 /* Check permission of requested appid */
3342 account_create(&account);
3343 account_query_account_by_account_id(account_id, &account);
3345 char* current_appid = NULL;
3346 char* verified_appid = NULL;
3348 current_appid = _account_get_current_appid();
3349 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
3351 _ACCOUNT_FREE(current_appid);
3352 _ACCOUNT_FREE(verified_appid);
3354 if(error_code != ACCOUNT_ERROR_NONE){
3355 ACCOUNT_ERROR("No permission to delete\n");
3356 return ACCOUNT_ERROR_PERMISSION_DENIED;
3359 account_destroy(account);
3361 /* transaction control required*/
3362 ret_transaction = _account_begin_transaction();
3364 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3365 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3366 pthread_mutex_unlock(&account_mutex);
3367 return ret_transaction;
3370 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3371 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3373 hstmt = _account_prepare_query(query);
3374 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3375 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3377 rc = _account_query_step(hstmt);
3378 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3380 _account_query_finalize(hstmt);
3383 ACCOUNT_MEMSET(query, 0, sizeof(query));
3385 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3387 hstmt = _account_prepare_query(query);
3388 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3389 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3391 rc = _account_query_step(hstmt);
3392 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3394 _account_query_finalize(hstmt);
3397 /* delete custom data */
3398 ACCOUNT_MEMSET(query, 0, sizeof(query));
3400 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3402 hstmt = _account_prepare_query(query);
3403 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3404 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3406 rc = _account_query_step(hstmt);
3407 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3409 _account_query_finalize(hstmt);
3414 if (hstmt != NULL) {
3415 _account_query_finalize(hstmt);
3419 ret_transaction = _account_end_transaction(is_success);
3421 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3422 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3424 if (is_success == true) {
3426 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3427 _account_insert_delete_update_notification_send(buf);
3431 pthread_mutex_unlock(&account_mutex);
3437 ACCOUNT_API int account_delete_from_db_by_id(int account_db_id)
3439 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
3441 return account_delete(account_db_id);
3444 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3446 int error_code = ACCOUNT_ERROR_NONE;
3447 account_stmt hstmt = NULL;
3448 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3450 int binding_count = 1;
3452 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3453 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3454 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3455 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3457 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3459 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3460 hstmt = _account_prepare_query(query);
3462 _account_query_bind_text(hstmt, binding_count++, username);
3463 _account_query_bind_text(hstmt, binding_count++, package_name);
3465 rc = _account_query_step(hstmt);
3466 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3468 account_s *account_record = (account_s *)(*account);
3470 while (rc == SQLITE_ROW) {
3471 _account_convert_column_to_account(hstmt, account_record);
3472 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
3473 rc = _account_query_step(hstmt);
3476 _account_query_finalize(hstmt);
3477 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3478 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3481 error_code = ACCOUNT_ERROR_NONE;
3484 if (hstmt != NULL) {
3485 _account_query_finalize(hstmt);
3489 pthread_mutex_unlock(&account_mutex);
3493 ACCOUNT_API int account_delete_from_db_by_user_name(char *user_name, char *package_name)
3495 int error_code = ACCOUNT_ERROR_NONE;
3496 account_stmt hstmt = NULL;
3497 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3499 int ret_transaction = 0;
3500 bool is_success = FALSE;
3501 account_h account = NULL;
3502 int binding_count = 1;
3503 int account_id = -1;
3505 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3506 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3507 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3509 rc = account_create(&account);
3510 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3512 rc = account_get_account_id(account, &account_id);
3514 rc = account_destroy(account);
3516 /* transaction control required*/
3517 ret_transaction = _account_begin_transaction();
3519 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3520 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3521 pthread_mutex_unlock(&account_mutex);
3522 return ret_transaction;
3525 /* delete custom data */
3526 ACCOUNT_MEMSET(query, 0, sizeof(query));
3527 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3529 hstmt = _account_prepare_query(query);
3531 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3532 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3534 _account_query_bind_int(hstmt, binding_count++, account_id);
3536 rc = _account_query_step(hstmt);
3537 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3539 _account_query_finalize(hstmt);
3542 /* delete capability */
3543 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3545 hstmt = _account_prepare_query(query);
3547 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3548 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3551 _account_query_bind_text(hstmt, binding_count++, user_name);
3552 _account_query_bind_text(hstmt, binding_count++, package_name);
3554 rc = _account_query_step(hstmt);
3555 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3557 _account_query_finalize(hstmt);
3560 ACCOUNT_MEMSET(query, 0, sizeof(query));
3562 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3564 hstmt = _account_prepare_query(query);
3565 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3566 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3569 _account_query_bind_text(hstmt, binding_count++, user_name);
3570 _account_query_bind_text(hstmt, binding_count++, package_name);
3572 rc = _account_query_step(hstmt);
3573 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. user_name=%s, package_name=%s, rc=%d\n", user_name, package_name, rc));
3575 _account_query_finalize(hstmt);
3581 if (hstmt != NULL) {
3582 _account_query_finalize(hstmt);
3586 ret_transaction = _account_end_transaction(is_success);
3588 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3589 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3591 if (is_success == true) {
3593 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3594 _account_insert_delete_update_notification_send(buf);
3598 pthread_mutex_unlock(&account_mutex);
3603 static bool _account_delete_from_db_by_package_name_cb(account_h account, void *user_data)
3605 GSList **account_id_list = (GSList**)user_data;
3607 char id_buf[10] ={0,};
3608 char* tmp_ptr = NULL;
3610 account_get_account_id(account, &account_id);
3611 ACCOUNT_SNPRINTF(id_buf, sizeof(id_buf), "%d", account_id);
3613 tmp_ptr = strdup(id_buf);
3615 *account_id_list = g_slist_append(*account_id_list, tmp_ptr);
3620 ACCOUNT_API int account_delete_from_db_by_package_name(const char *package_name)
3622 int error_code = ACCOUNT_ERROR_NONE;
3623 account_stmt hstmt = NULL;
3624 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3626 int ret_transaction = 0;
3627 bool is_success = FALSE;
3628 int binding_count = 1;
3629 GSList *account_id_list = NULL;
3631 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3632 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3634 account_query_account_by_package_name(_account_delete_from_db_by_package_name_cb, package_name, (void*)&account_id_list);
3636 /* transaction control required*/
3637 ret_transaction = _account_begin_transaction();
3639 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3640 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3641 pthread_mutex_unlock(&account_mutex);
3642 return ret_transaction;
3645 /* delete custom table */
3646 ACCOUNT_MEMSET(query, 0, sizeof(query));
3647 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3649 hstmt = _account_prepare_query(query);
3651 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3652 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3655 _account_query_bind_text(hstmt, binding_count++, package_name);
3657 rc = _account_query_step(hstmt);
3658 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3660 _account_query_finalize(hstmt);
3663 /* delete capability table */
3664 ACCOUNT_MEMSET(query, 0, sizeof(query));
3665 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3667 hstmt = _account_prepare_query(query);
3669 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3670 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3673 _account_query_bind_text(hstmt, binding_count++, package_name);
3675 rc = _account_query_step(hstmt);
3676 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3678 _account_query_finalize(hstmt);
3681 /* delete account table */
3682 ACCOUNT_MEMSET(query, 0, sizeof(query));
3684 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3686 hstmt = _account_prepare_query(query);
3687 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3688 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3691 _account_query_bind_text(hstmt, binding_count++, package_name);
3693 rc = _account_query_step(hstmt);
3694 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));
3696 _account_query_finalize(hstmt);
3702 if (hstmt != NULL) {
3703 _account_query_finalize(hstmt);
3707 ret_transaction = _account_end_transaction(is_success);
3709 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3710 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3712 if (is_success == true) {
3714 for (iter = account_id_list; iter != NULL; iter = g_slist_next(iter)) {
3715 char* p_tmpid = NULL;
3716 p_tmpid = (char*)iter->data;
3718 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
3719 ACCOUNT_VERBOSE("%s", buf);
3720 _account_insert_delete_update_notification_send(buf);
3721 _ACCOUNT_FREE(p_tmpid);
3723 g_slist_free(account_id_list);
3727 pthread_mutex_unlock(&account_mutex);
3732 ACCOUNT_API int account_get_total_count_from_db(int *count)
3735 ACCOUNT_ERROR("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3736 return ACCOUNT_ERROR_INVALID_PARAMETER;
3738 char query[1024] = {0, };
3739 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3740 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3742 *count = _account_get_record_count(query);
3745 account_stmt pStmt = NULL;
3747 assert(NULL != g_hAccountDB);
3748 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3750 rc = sqlite3_step(pStmt);
3751 if (SQLITE_ROW != rc) {
3752 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3753 sqlite3_finalize(pStmt);
3754 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3757 ncount = sqlite3_column_int(pStmt, 0);
3761 ACCOUNT_VERBOSE("Number of account : %d", ncount);
3762 sqlite3_finalize(pStmt);
3765 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3766 return ACCOUNT_ERROR_DB_FAILED;
3769 return ACCOUNT_ERROR_NONE;
3772 ACCOUNT_API int account_destroy(account_h account)
3774 account_s *data = (account_s*)account;
3776 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3778 ACCOUNT_INFO("destroy handle=%p\n", data);
3780 _account_free_account_items(data);
3781 _ACCOUNT_FREE(data);
3783 return ACCOUNT_ERROR_NONE;
3786 static int _account_type_free_label_items(label_s *data)
3788 _ACCOUNT_FREE(data->app_id);
3789 _ACCOUNT_FREE(data->label);
3790 _ACCOUNT_FREE(data->locale);
3792 return ACCOUNT_ERROR_NONE;
3795 static int _account_type_free_feature_items(provider_feature_s *data)
3797 _ACCOUNT_FREE(data->app_id);
3798 _ACCOUNT_FREE(data->key);
3800 return ACCOUNT_ERROR_NONE;
3803 static int _account_type_gslist_free(GSList* list)
3805 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
3809 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
3810 label_s *label_data = (label_s*)iter->data;
3811 _account_type_free_label_items(label_data);
3812 _ACCOUNT_FREE(label_data);
3818 return ACCOUNT_ERROR_NONE;
3821 static int _account_type_item_free(account_type_s *data)
3823 _ACCOUNT_FREE(data->app_id);
3824 _ACCOUNT_FREE(data->service_provider_id);
3825 _ACCOUNT_FREE(data->icon_path);
3826 _ACCOUNT_FREE(data->small_icon_path);
3828 return ACCOUNT_ERROR_NONE;
3831 static int _account_type_glist_free(GList* list)
3833 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
3837 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
3838 account_type_s *account_type_record = (account_type_s*)iter->data;
3839 _account_type_item_free(account_type_record);
3840 _ACCOUNT_FREE(account_type_record);
3846 return ACCOUNT_ERROR_NONE;
3849 static int _account_type_free_account_type_items(account_type_s *data)
3851 _account_type_item_free(data);
3853 _account_type_gslist_free(data->label_list);
3854 _account_type_glist_free(data->account_type_list);
3856 return ACCOUNT_ERROR_NONE;
3859 ACCOUNT_API int account_type_create(account_type_h *account_type)
3861 if (!account_type) {
3862 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3863 return ACCOUNT_ERROR_INVALID_PARAMETER;
3866 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3869 ACCOUNT_ERROR("Memory Allocation Failed");
3870 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3873 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3875 ACCOUNT_VERBOSE("create handle=%p\n", *account_type);
3877 *account_type = (account_type_h)data;
3879 return ACCOUNT_ERROR_NONE;
3882 ACCOUNT_API int account_type_destroy(account_type_h account_type)
3884 account_type_s *data = (account_type_s*)account_type;
3886 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
3888 ACCOUNT_VERBOSE("destroy handle=%p\n", data);
3890 _account_type_free_account_type_items(data);
3891 _ACCOUNT_FREE(data);
3893 return ACCOUNT_ERROR_NONE;
3896 //app_id mandatory field
3897 ACCOUNT_API int account_type_set_app_id(account_type_h account_type, const char *app_id)
3899 if (!account_type) {
3900 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3901 return ACCOUNT_ERROR_INVALID_PARAMETER;
3905 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
3906 return ACCOUNT_ERROR_INVALID_PARAMETER;
3909 account_type_s *data = (account_type_s*)account_type;
3911 _ACCOUNT_FREE(data->app_id);
3912 data->app_id = _account_get_text(app_id);
3914 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->app_id, app_id);
3915 return ACCOUNT_ERROR_NONE;
3918 //service_provider_id mandatory field
3919 ACCOUNT_API int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
3921 if (!account_type) {
3922 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3923 return ACCOUNT_ERROR_INVALID_PARAMETER;
3926 if (!service_provider_id) {
3927 ACCOUNT_ERROR("(%s)-(%d) service_provider_id is NULL.\n", __FUNCTION__, __LINE__);
3928 return ACCOUNT_ERROR_INVALID_PARAMETER;
3931 account_type_s *data = (account_type_s*)account_type;
3933 _ACCOUNT_FREE(data->service_provider_id);
3934 data->service_provider_id = _account_get_text(service_provider_id);
3936 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->service_provider_id, service_provider_id);
3937 return ACCOUNT_ERROR_NONE;
3940 ACCOUNT_API int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
3942 if (!account_type) {
3943 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3944 return ACCOUNT_ERROR_INVALID_PARAMETER;
3948 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
3949 return ACCOUNT_ERROR_INVALID_PARAMETER;
3952 account_type_s *data = (account_type_s*)account_type;
3954 _ACCOUNT_FREE(data->icon_path);
3955 data->icon_path = _account_get_text(icon_path);
3957 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
3958 return ACCOUNT_ERROR_NONE;
3961 ACCOUNT_API int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
3963 if (!account_type) {
3964 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3965 return ACCOUNT_ERROR_INVALID_PARAMETER;
3968 if (!small_icon_path) {
3969 ACCOUNT_ERROR("(%s)-(%d) small_icon_path is NULL.\n", __FUNCTION__, __LINE__);
3970 return ACCOUNT_ERROR_INVALID_PARAMETER;
3973 account_type_s *data = (account_type_s*)account_type;
3975 _ACCOUNT_FREE(data->small_icon_path);
3976 data->small_icon_path = _account_get_text(small_icon_path);
3978 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->small_icon_path, small_icon_path);
3979 return ACCOUNT_ERROR_NONE;
3982 ACCOUNT_API int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
3984 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
3986 account_type_s *data = (account_type_s*)account_type;
3988 data->multiple_account_support = multiple_account_support;
3990 return ACCOUNT_ERROR_NONE;
3994 ACCOUNT_API int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
3996 if (!account_type) {
3997 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3998 return ACCOUNT_ERROR_INVALID_PARAMETER;
4001 if(!label || !locale) {
4002 ACCOUNT_ERROR("(%s)-(%d) label(%p) or locale(%p) is NULL.\n", __FUNCTION__, __LINE__, label, locale);
4003 return ACCOUNT_ERROR_INVALID_PARAMETER;
4006 account_type_s *data = (account_type_s*)account_type;
4007 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4009 if (label_data == NULL) {
4010 ACCOUNT_FATAL("(%s)-(%d) Malloc fail. label_data is NULL.\n", __FUNCTION__, __LINE__);
4011 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4013 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4015 label_data->label = _account_get_text(label);
4016 label_data->locale = _account_get_text(locale);
4018 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4020 return ACCOUNT_ERROR_NONE;
4023 ACCOUNT_API int account_type_get_app_id(account_type_h account_type, char **app_id)
4025 if (!account_type) {
4026 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4027 return ACCOUNT_ERROR_INVALID_PARAMETER;
4031 ACCOUNT_ERROR("(%s)-(%d) app id is NULL.\n", __FUNCTION__, __LINE__);
4032 return ACCOUNT_ERROR_INVALID_PARAMETER;
4035 account_type_s *data = (account_type_s*)account_type;
4038 *app_id = _account_get_text(data->app_id);
4040 return ACCOUNT_ERROR_NONE;
4043 ACCOUNT_API int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4045 if (!account_type) {
4046 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4047 return ACCOUNT_ERROR_INVALID_PARAMETER;
4050 if (!service_provider_id) {
4051 ACCOUNT_ERROR("(%s)-(%d) service provider id is NULL.\n", __FUNCTION__, __LINE__);
4052 return ACCOUNT_ERROR_INVALID_PARAMETER;
4055 account_type_s *data = (account_type_s*)account_type;
4057 (*service_provider_id) = NULL;
4058 *service_provider_id = _account_get_text(data->service_provider_id);
4060 return ACCOUNT_ERROR_NONE;
4063 ACCOUNT_API int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4065 if (!account_type) {
4066 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4067 return ACCOUNT_ERROR_INVALID_PARAMETER;
4071 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4072 return ACCOUNT_ERROR_INVALID_PARAMETER;
4075 account_type_s *data = (account_type_s*)account_type;
4077 (*icon_path) = NULL;
4078 *icon_path = _account_get_text(data->icon_path);
4080 return ACCOUNT_ERROR_NONE;
4083 ACCOUNT_API int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4085 if (!account_type) {
4086 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4087 return ACCOUNT_ERROR_INVALID_PARAMETER;
4090 if (!small_icon_path) {
4091 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4092 return ACCOUNT_ERROR_INVALID_PARAMETER;
4095 account_type_s *data = (account_type_s*)account_type;
4097 (*small_icon_path) = NULL;
4098 *small_icon_path = _account_get_text(data->small_icon_path);
4100 return ACCOUNT_ERROR_NONE;
4103 ACCOUNT_API int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4105 if (!account_type) {
4106 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4107 return ACCOUNT_ERROR_INVALID_PARAMETER;
4109 if (!multiple_account_support) {
4110 ACCOUNT_ERROR("(%s)-(%d) multiple_account_support is NULL.\n", __FUNCTION__, __LINE__);
4111 return ACCOUNT_ERROR_INVALID_PARAMETER;
4114 account_type_s *data = (account_type_s*)account_type;
4116 *multiple_account_support = data->multiple_account_support;
4118 return ACCOUNT_ERROR_NONE;
4121 ACCOUNT_API int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4123 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4124 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4127 account_type_s *data = (account_type_s*)account_type;
4129 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4130 label_s *label_data = NULL;
4132 label_data = (label_s*)iter->data;
4134 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4138 if(!strcmp(locale, label_data->locale)) {
4139 *label = _account_get_text(label_data->label);
4140 return ACCOUNT_ERROR_NONE;
4144 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4147 ACCOUNT_API int account_type_get_label(account_type_h account_type, account_label_cb cb_func, void *user_data)
4149 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4150 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4153 account_type_s *data = (account_type_s*)account_type;
4155 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4156 label_s *label_data = NULL;
4158 label_data = (label_s*)iter->data;
4160 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4162 //cb_func(label_data->app_id, label_data->label, label_data->locale, user_data);
4163 if(cb_func(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4164 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4165 return ACCOUNT_ERROR_NONE;
4169 return ACCOUNT_ERROR_NONE;
4172 static gboolean _account_type_check_duplicated(account_type_s *data)
4174 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4177 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4179 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4180 , ACCOUNT_TYPE_TABLE, data->app_id);
4182 count = _account_get_record_count(query);
4184 ACCOUNT_VERBOSE("_account_type_check_duplicated : duplicated %d account type(s) exist!, app_id=%s, service_provider_id=%s\n",
4185 count, data->app_id, data->service_provider_id);
4192 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4196 /*Caution : Keep insert query orders.*/
4199 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4201 /* 2. service provider id*/
4202 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4205 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4207 /* 4. small icon path*/
4208 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4210 /* 5. multiple accont support*/
4211 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4217 static int _account_type_execute_insert_query(account_type_s *account_type)
4220 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4221 int error_code = ACCOUNT_ERROR_NONE;
4222 account_stmt hstmt = NULL;
4224 /* check mandatory field */
4225 // app id & service provider id
4226 if (!account_type->app_id) {
4227 ACCOUNT_ERROR("App id is mandetory field, it can not be NULL!!!!\n");
4228 return ACCOUNT_ERROR_INVALID_PARAMETER;
4231 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4232 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4233 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4235 hstmt = _account_prepare_query(query);
4236 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4238 _account_type_convert_account_to_sql(account_type, hstmt, query);
4240 rc = _account_query_step(hstmt);
4241 if (rc != SQLITE_DONE) {
4242 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4243 error_code = ACCOUNT_ERROR_DB_FAILED;
4246 _account_query_finalize(hstmt);
4252 static int _account_type_insert_label(account_type_s *account_type)
4255 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4256 account_stmt hstmt = NULL;
4258 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4260 if (g_slist_length( account_type->label_list)==0) {
4261 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4262 return ACCOUNT_ERROR_NONE;
4265 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4267 rc = _account_get_record_count(query);
4270 ACCOUNT_ERROR( "_account_insert_label : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
4271 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4277 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4280 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4281 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4282 "(?, ?, ?) ", LABEL_TABLE);
4284 hstmt = _account_prepare_query(query);
4286 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4288 label_s* label_data = NULL;
4289 label_data = (label_s*)iter->data;
4290 ACCOUNT_VERBOSE( "label_data->appid = %s, label_data->label = %s, label_data->locale \n", label_data->app_id, label_data->label, label_data->locale);
4292 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4293 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4294 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4295 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4296 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4297 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4299 rc = _account_query_step(hstmt);
4301 if (rc != SQLITE_DONE) {
4302 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4306 _account_query_finalize(hstmt);
4311 ACCOUNT_VERBOSE( "_account_type_insert_label() DONE\n");
4313 return ACCOUNT_ERROR_NONE;
4316 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4318 char *textbuf = NULL;
4320 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4321 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4323 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4324 _account_db_data_to_text(textbuf, &(feature_record->key));
4326 ACCOUNT_VERBOSE("END _account_type_convert_column_to_provider_feature");
4329 ACCOUNT_API int account_type_query_provider_feature_by_app_id(provider_feature_cb cb_func, const char* app_id, void *user_data )
4331 int error_code = ACCOUNT_ERROR_NONE;
4332 account_stmt hstmt = NULL;
4333 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4334 int rc = 0, binding_count = 1;
4336 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4337 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4338 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4340 ACCOUNT_VERBOSE("app_id = %s", app_id);
4342 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4344 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4345 hstmt = _account_prepare_query(query);
4347 _account_query_bind_text(hstmt, binding_count++, app_id);
4349 rc = _account_query_step(hstmt);
4350 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4352 provider_feature_s* feature_record = NULL;
4354 while (rc == SQLITE_ROW) {
4355 bool cb_ret = FALSE;
4356 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4358 if (feature_record == NULL) {
4359 ACCOUNT_FATAL("malloc Failed");
4363 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4365 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4367 cb_ret = cb_func(feature_record->app_id, feature_record->key, user_data);
4369 _account_type_free_feature_items(feature_record);
4370 _ACCOUNT_FREE(feature_record);
4372 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4374 rc = _account_query_step(hstmt);
4377 _account_query_finalize(hstmt);
4380 error_code = ACCOUNT_ERROR_NONE;
4383 if (hstmt != NULL) {
4384 _account_query_finalize(hstmt);
4388 pthread_mutex_unlock(&account_mutex);
4392 ACCOUNT_API bool account_type_query_supported_feature(const char* app_id, const char* capability)
4394 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4395 int record_count = 0;
4397 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4398 ACCOUNT_RETURN_VAL((capability != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CAPABILITY"));
4400 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4402 record_count = _account_get_record_count(query);
4404 if (record_count <= 0) {
4405 ACCOUNT_WARNING( "related capability type item is not existed rc=%d , %s", record_count, _account_db_err_msg());
4414 ACCOUNT_API int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb cb_func, void* user_data)
4416 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4417 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4420 account_type_s *data = (account_type_s*)account_type;
4422 for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4423 provider_feature_s *feature_data = NULL;
4425 feature_data = (provider_feature_s*)iter->data;
4427 ACCOUNT_VERBOSE("appid = %s, key = %s", feature_data->key, feature_data->app_id);
4429 //cb_func(feature_data->app_id, feature_data->key, user_data);
4430 if(cb_func(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
4431 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4432 return ACCOUNT_ERROR_NONE;
4436 return ACCOUNT_ERROR_NONE;
4439 ACCOUNT_API int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4441 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4442 ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4444 account_type_s *data = (account_type_s*)account_type;
4446 GSList *iter = NULL;
4447 bool b_is_new = TRUE;
4449 for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4450 provider_feature_s *feature_data = NULL;
4451 feature_data = (provider_feature_s*)iter->data;
4453 if(!strcmp(feature_data->key, provider_feature)) {
4460 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4462 if (feature_data == NULL)
4463 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4464 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4466 feature_data->key = _account_get_text(provider_feature);
4467 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4470 return ACCOUNT_ERROR_NONE;
4473 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4476 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4477 account_stmt hstmt = NULL;
4479 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4480 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4482 if (g_slist_length( account_type->provider_feature_list)==0) {
4483 ACCOUNT_ERROR( "no capability\n");
4484 return ACCOUNT_ERROR_NONE;
4487 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4489 rc = _account_get_record_count(query);
4492 ACCOUNT_WARNING( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4493 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4500 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4503 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4504 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4505 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4507 hstmt = _account_prepare_query(query);
4509 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4511 provider_feature_s* feature_data = NULL;
4512 feature_data = (provider_feature_s*)iter->data;
4513 ACCOUNT_VERBOSE("key = %s, app_id = %s \n", feature_data->key, app_id);
4515 ret = _account_query_bind_text(hstmt, count++, app_id);
4516 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4517 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4518 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4520 rc = _account_query_step(hstmt);
4522 if (rc != SQLITE_DONE) {
4523 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4527 _account_query_finalize(hstmt);
4532 ACCOUNT_VERBOSE( "_account_type_insert_provider_feature() DONE\n");
4534 return ACCOUNT_ERROR_NONE;
4537 ACCOUNT_API int account_type_insert_to_db(account_type_h account_type, int* account_type_id)
4539 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4541 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4542 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4543 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4545 account_type_s *data = (account_type_s*)account_type;
4547 pthread_mutex_lock(&account_mutex);
4550 /* transaction control required*/
4551 ret_transaction = _account_begin_transaction();
4553 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4554 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4555 pthread_mutex_unlock(&account_mutex);
4556 return ret_transaction;
4559 if (_account_type_check_duplicated(data)) {
4560 ret_transaction = _account_end_transaction(FALSE);
4561 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4562 *account_type_id = -1;
4563 pthread_mutex_unlock(&account_mutex);
4564 return ACCOUNT_ERROR_DUPLICATED;
4566 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4568 error_code = _account_type_execute_insert_query(data);
4570 if (error_code != ACCOUNT_ERROR_NONE){
4571 error_code = ACCOUNT_ERROR_DUPLICATED;
4572 ret_transaction = _account_end_transaction(FALSE);
4573 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4574 *account_type_id = -1;
4575 pthread_mutex_unlock(&account_mutex);
4580 ACCOUNT_INFO( "_account_type_execute_insert_query, insert error_code : %d", error_code);
4582 error_code = _account_type_insert_provider_feature(data, data->app_id);
4583 if(error_code != ACCOUNT_ERROR_NONE) {
4584 ret_transaction = _account_end_transaction(FALSE);
4585 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4586 pthread_mutex_unlock(&account_mutex);
4589 error_code = _account_type_insert_label(data);
4590 if(error_code != ACCOUNT_ERROR_NONE) {
4591 ret_transaction = _account_end_transaction(FALSE);
4592 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4593 pthread_mutex_unlock(&account_mutex);
4597 ret_transaction = _account_end_transaction(TRUE);
4598 ACCOUNT_DEBUG("Insert success(%x), commit insert query(%x)!!!!\n", error_code, ret_transaction);
4600 pthread_mutex_unlock(&account_mutex);
4602 return ACCOUNT_ERROR_NONE;
4605 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4608 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4609 account_stmt hstmt = NULL;
4611 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4613 if (g_slist_length( account_type->provider_feature_list)==0) {
4614 ACCOUNT_ERROR( "no feature\n");
4615 return ACCOUNT_ERROR_NONE;
4618 ACCOUNT_DEBUG( "app id", app_id);
4620 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4622 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4623 hstmt = _account_prepare_query(query);
4625 _account_query_bind_text(hstmt, count++, app_id);
4626 rc = _account_query_step(hstmt);
4628 if (rc != SQLITE_DONE) {
4629 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4630 return ACCOUNT_ERROR_DB_FAILED;
4632 _account_query_finalize(hstmt);
4637 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4640 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4641 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4642 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4644 hstmt = _account_prepare_query(query);
4646 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4648 provider_feature_s* feature_data = NULL;
4649 feature_data = (provider_feature_s*)iter->data;
4651 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4652 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4653 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4654 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4656 rc = _account_query_step(hstmt);
4658 if (rc != SQLITE_DONE) {
4659 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4662 _account_query_finalize(hstmt);
4666 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4668 return ACCOUNT_ERROR_NONE;
4671 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4674 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4675 account_stmt hstmt = NULL;
4677 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4679 if (g_slist_length( account_type->label_list)==0) {
4680 ACCOUNT_ERROR( "_account_type_update_label, no label\n");
4681 return ACCOUNT_ERROR_NONE;
4684 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4686 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4687 hstmt = _account_prepare_query(query);
4689 _account_query_bind_text(hstmt, count++, app_id);
4690 rc = _account_query_step(hstmt);
4692 if (rc != SQLITE_DONE) {
4693 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4694 return ACCOUNT_ERROR_DB_FAILED;
4696 _account_query_finalize(hstmt);
4701 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4704 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4705 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4706 "(?, ?, ?) ", LABEL_TABLE);
4708 hstmt = _account_prepare_query(query);
4710 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4712 label_s* label_data = NULL;
4713 label_data = (label_s*)iter->data;
4715 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4716 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4717 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4718 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4719 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
4720 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4722 rc = _account_query_step(hstmt);
4724 if (rc != SQLITE_DONE) {
4725 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4728 _account_query_finalize(hstmt);
4732 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4734 return ACCOUNT_ERROR_NONE;
4738 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
4740 int rc = 0, binding_count =1;
4741 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4742 int error_code = ACCOUNT_ERROR_NONE;
4743 account_stmt hstmt = NULL;
4745 if (!account_type->app_id) {
4746 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
4747 return ACCOUNT_ERROR_INVALID_PARAMETER;
4750 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4751 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
4752 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
4754 hstmt = _account_prepare_query(query);
4755 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
4757 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
4758 _account_query_bind_text(hstmt, binding_count++, app_id);
4760 rc = _account_query_step(hstmt);
4761 if (rc != SQLITE_DONE) {
4762 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4765 _account_query_finalize(hstmt);
4769 error_code = _account_type_update_label(account_type, app_id);
4770 /* update provider feature */
4771 error_code = _account_type_update_provider_feature(account_type, app_id);
4776 ACCOUNT_API int account_type_update_to_db_by_app_id(account_type_h account_type, const char* app_id)
4778 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
4779 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4780 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4782 int error_code = ACCOUNT_ERROR_NONE;
4783 account_type_s* data = (account_type_s*)account_type;
4785 pthread_mutex_lock(&account_mutex);
4787 error_code = _account_type_update_account(data, app_id);
4789 pthread_mutex_unlock(&account_mutex);
4794 ACCOUNT_API int account_type_delete_by_app_id(const char* app_id)
4796 int error_code = ACCOUNT_ERROR_NONE;
4797 account_stmt hstmt = NULL;
4798 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4799 int rc = 0, count = -1;
4800 int ret_transaction = 0;
4801 int binding_count = 1;
4802 bool is_success = FALSE;
4804 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4805 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
4807 /* Check requested ID to delete */
4808 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
4810 count = _account_get_record_count(query);
4812 ACCOUNT_ERROR("app id(%s) is not exist. count(%d)\n", app_id, count);
4813 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4816 /* transaction control required*/
4817 ret_transaction = _account_begin_transaction();
4819 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4820 ACCOUNT_FATAL("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4821 pthread_mutex_unlock(&account_mutex);
4822 return ret_transaction;
4825 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
4827 hstmt = _account_prepare_query(query);
4828 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4829 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4831 _account_query_bind_text(hstmt, binding_count++, app_id);
4833 rc = _account_query_step(hstmt);
4834 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4836 _account_query_finalize(hstmt);
4840 ACCOUNT_MEMSET(query, 0, sizeof(query));
4842 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
4844 hstmt = _account_prepare_query(query);
4845 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4846 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4848 _account_query_bind_text(hstmt, binding_count++, app_id);
4850 rc = _account_query_step(hstmt);
4851 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4853 _account_query_finalize(hstmt);
4859 ACCOUNT_MEMSET(query, 0, sizeof(query));
4861 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
4863 hstmt = _account_prepare_query(query);
4864 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4865 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4867 _account_query_bind_text(hstmt, binding_count++, app_id);
4869 rc = _account_query_step(hstmt);
4870 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4872 _account_query_finalize(hstmt);
4878 if (hstmt != NULL) {
4879 _account_query_finalize(hstmt);
4883 ret_transaction = _account_end_transaction(is_success);
4885 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4886 ACCOUNT_FATAL("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4889 pthread_mutex_unlock(&account_mutex);
4894 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
4896 char *textbuf = NULL;
4898 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
4900 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
4901 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
4903 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
4904 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
4906 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
4907 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
4909 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
4910 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
4912 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
4914 ACCOUNT_VERBOSE("END _account_type_convert_column_to_account_type");
4917 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
4919 char *textbuf = NULL;
4921 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
4922 _account_db_data_to_text(textbuf, &(label_record->app_id));
4924 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
4925 _account_db_data_to_text(textbuf, &(label_record->label));
4927 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
4928 _account_db_data_to_text(textbuf, &(label_record->locale));
4930 ACCOUNT_VERBOSE("END _account_type_convert_column_to_label");
4933 ACCOUNT_API int account_type_query_label_by_app_id(account_label_cb cb_func, const char* app_id, void *user_data )
4935 int error_code = ACCOUNT_ERROR_NONE;
4936 account_stmt hstmt = NULL;
4937 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4938 int rc = 0, binding_count = 1;
4940 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4941 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4942 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4944 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4946 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
4947 hstmt = _account_prepare_query(query);
4949 _account_query_bind_text(hstmt, binding_count++, app_id);
4951 rc = _account_query_step(hstmt);
4952 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4954 label_s* label_record = NULL;
4956 while (rc == SQLITE_ROW) {
4957 bool cb_ret = FALSE;
4958 label_record = (label_s*) malloc(sizeof(label_s));
4960 if (label_record == NULL) {
4961 ACCOUNT_FATAL("malloc Failed");
4965 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4967 _account_type_convert_column_to_label(hstmt, label_record);
4969 cb_ret = cb_func(label_record->app_id, label_record->label , label_record->locale, user_data);
4971 _account_type_free_label_items(label_record);
4972 _ACCOUNT_FREE(label_record);
4974 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4976 rc = _account_query_step(hstmt);
4979 _account_query_finalize(hstmt);
4982 error_code = ACCOUNT_ERROR_NONE;
4985 if (hstmt != NULL) {
4986 _account_query_finalize(hstmt);
4990 pthread_mutex_unlock(&account_mutex);
4994 int _account_type_label_get_app_id(label_h label, char **app_id)
4997 ACCOUNT_ERROR("(%s)-(%d) label handle is NULL.\n", __FUNCTION__, __LINE__);
4998 return ACCOUNT_ERROR_INVALID_PARAMETER;
5002 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
5003 return ACCOUNT_ERROR_INVALID_PARAMETER;
5006 label_s *data = (label_s*)label;
5010 *app_id = _account_get_text(data->app_id);
5012 return ACCOUNT_ERROR_NONE;
5015 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
5017 account_type_s *data = (account_type_s*)user_data;
5019 label_s *label_data = (label_s*)malloc(sizeof(label_s));
5021 if (label_data == NULL) {
5022 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
5025 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
5027 label_data->app_id = _account_get_text(app_id);
5028 label_data->label = _account_get_text(label);
5029 label_data->locale = _account_get_text(locale);
5031 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5033 ACCOUNT_VERBOSE("_account_get_label_text_cb :: appid=%s, label=%s\n", label_data->app_id, label_data->label);
5038 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5040 account_type_s *data = (account_type_s*)user_data;
5042 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5044 if (feature_data == NULL) {
5045 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5048 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5050 feature_data->app_id = _account_get_text(app_id);
5051 feature_data->key = _account_get_text(key);
5053 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5055 ACCOUNT_VERBOSE("appid=%s, key=%s\n", feature_data->app_id, feature_data->key);
5060 ACCOUNT_API int account_type_query_by_app_id(const char* app_id, account_type_h *account_type)
5062 int error_code = ACCOUNT_ERROR_NONE;
5063 account_stmt hstmt = NULL;
5064 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5065 int rc = 0, binding_count = 1;
5067 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5068 ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL"));
5069 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5071 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5073 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5074 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5075 hstmt = _account_prepare_query(query);
5077 _account_query_bind_text(hstmt, binding_count++, app_id);
5079 rc = _account_query_step(hstmt);
5080 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5082 account_type_s *account_type_record = (account_type_s *)(*account_type);
5084 while (rc == SQLITE_ROW) {
5085 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5086 ACCOUNT_DEBUG("get account info by id %p\n", account_type_record);
5087 rc = _account_query_step(hstmt);
5090 _account_query_finalize(hstmt);
5091 account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)account_type_record);
5092 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)account_type_record);
5095 error_code = ACCOUNT_ERROR_NONE;
5098 if (hstmt != NULL) {
5099 _account_query_finalize(hstmt);
5103 pthread_mutex_unlock(&account_mutex);
5107 ACCOUNT_API int account_type_query_app_id_exist(const char* app_id)
5109 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5112 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5113 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5115 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5117 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5119 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5120 rc = _account_get_record_count(query);
5123 ACCOUNT_DEBUG("(%s) not exist in account type db");
5124 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5127 return ACCOUNT_ERROR_NONE;
5130 ACCOUNT_API int account_type_query_by_provider_feature(account_type_cb cb_func, const char* key, void* user_data)
5132 int error_code = ACCOUNT_ERROR_NONE;
5133 account_stmt hstmt = NULL;
5134 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5136 GList *account_type_list = NULL;
5138 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
5139 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
5140 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5142 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5144 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5146 hstmt = _account_prepare_query(query);
5148 int binding_count = 1;
5149 _account_query_bind_text(hstmt, binding_count++, (char*)key);
5151 rc = _account_query_step(hstmt);
5153 account_type_s *account_type_record = NULL;
5155 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5158 while(rc == SQLITE_ROW) {
5159 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5161 if (account_type_record == NULL) {
5162 ACCOUNT_FATAL("malloc Failed");
5166 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5167 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5168 account_type_list = g_list_append(account_type_list, account_type_record);
5169 rc = _account_query_step(hstmt);
5173 _account_query_finalize(hstmt);
5178 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5179 account_type_s *account_type = NULL;
5180 account_type = (account_type_s*)iter->data;
5181 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5182 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5183 //cb_func((account_type_h)account_type, user_data);
5184 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"));
5188 error_code = ACCOUNT_ERROR_NONE;
5191 if (hstmt != NULL) {
5192 _account_query_finalize(hstmt);
5195 if (account_type_list) {
5196 _account_type_glist_free(account_type_list);
5197 account_type_list = NULL;
5204 ACCOUNT_API int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data)
5206 int error_code = ACCOUNT_ERROR_NONE;
5207 account_stmt hstmt = NULL;
5208 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5210 GList *account_type_list = NULL;
5212 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
5213 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5215 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5217 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5218 hstmt = _account_prepare_query(query);
5220 rc = _account_query_step(hstmt);
5222 account_type_s *account_type_record = NULL;
5224 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5227 while(rc == SQLITE_ROW) {
5228 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5230 if (account_type_record == NULL) {
5231 ACCOUNT_FATAL("malloc Failed");
5235 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5236 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5237 account_type_list = g_list_append(account_type_list, account_type_record);
5238 rc = _account_query_step(hstmt);
5242 _account_query_finalize(hstmt);
5247 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5248 account_type_s *account_type = NULL;
5249 account_type = (account_type_s*)iter->data;
5250 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5251 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5252 callback((account_type_h)account_type, user_data);
5256 error_code = ACCOUNT_ERROR_NONE;
5259 if (hstmt != NULL) {
5260 _account_query_finalize(hstmt);
5263 if (account_type_list) {
5264 _account_type_glist_free(account_type_list);
5265 account_type_list = NULL;
5271 // output parameter label must be free
5272 ACCOUNT_API int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label)
5274 int error_code = ACCOUNT_ERROR_NONE;
5275 account_stmt hstmt = NULL;
5276 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5277 int rc = 0, binding_count = 1;
5279 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5280 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5281 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5283 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5285 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, locale);
5286 hstmt = _account_prepare_query(query);
5288 _account_query_bind_text(hstmt, binding_count++, app_id);
5290 rc = _account_query_step(hstmt);
5291 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5293 label_s* label_record = NULL;
5295 while (rc == SQLITE_ROW) {
5296 label_record = (label_s*) malloc(sizeof(label_s));
5298 if (label_record == NULL) {
5299 ACCOUNT_FATAL("malloc Failed");
5303 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5305 _account_type_convert_column_to_label(hstmt,label_record);
5307 *label = _account_get_text(label_record->label);
5309 _account_type_free_label_items(label_record);
5310 _ACCOUNT_FREE(label_record);
5312 rc = _account_query_step(hstmt);
5315 _account_query_finalize(hstmt);
5318 error_code = ACCOUNT_ERROR_NONE;
5321 if (hstmt != NULL) {
5322 _account_query_finalize(hstmt);
5326 pthread_mutex_unlock(&account_mutex);
5330 static int _account_insert_custom(account_s *account, int account_id)
5333 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5334 account_stmt hstmt = NULL;
5336 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5338 if (g_slist_length( account->custom_list)==0) {
5339 ACCOUNT_ERROR( "_account_insert_custom, no custom data\n");
5340 return ACCOUNT_ERROR_NONE;
5343 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5345 rc = _account_get_record_count(query);
5348 ACCOUNT_ERROR( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5349 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5356 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5359 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5360 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5361 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5363 hstmt = _account_prepare_query(query);
5365 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5367 account_custom_s* custom_data = NULL;
5368 custom_data = (account_custom_s*)iter->data;
5369 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
5371 ret = _account_query_bind_int(hstmt, count++, account_id);
5372 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5373 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5374 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5375 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5376 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5377 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5378 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5380 rc = _account_query_step(hstmt);
5382 if (rc != SQLITE_DONE) {
5383 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5387 _account_query_finalize(hstmt);
5392 ACCOUNT_VERBOSE( "_account_insert_custom() DONE\n");
5394 return ACCOUNT_ERROR_NONE;
5397 static int _account_update_custom(account_s *account, int account_id)
5400 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5401 account_stmt hstmt = NULL;
5403 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5405 ACCOUNT_INFO( "account handle=%p, account_id=%d", account, account_id);
5407 if (g_slist_length( account->custom_list)==0) {
5408 ACCOUNT_ERROR( "_account_update_custom, no custom data\n");
5409 return ACCOUNT_ERROR_NONE;
5412 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5414 rc = _account_get_record_count(query);
5417 ACCOUNT_ERROR( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5418 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5421 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5423 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5424 hstmt = _account_prepare_query(query);
5426 _account_query_bind_int(hstmt, count++, (int)account_id);
5427 rc = _account_query_step(hstmt);
5429 if (rc != SQLITE_DONE) {
5430 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5431 return ACCOUNT_ERROR_DB_FAILED;
5433 _account_query_finalize(hstmt);
5438 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5441 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5442 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5443 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5445 hstmt = _account_prepare_query(query);
5447 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5449 account_custom_s* custom_data = NULL;
5450 custom_data = (account_custom_s*)iter->data;
5452 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5453 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5454 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5455 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5456 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5457 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5458 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5459 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5461 rc = _account_query_step(hstmt);
5463 if (rc != SQLITE_DONE) {
5464 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5468 _account_query_finalize(hstmt);
5473 ACCOUNT_VERBOSE( "_account_update_custom() DONE\n");
5475 return ACCOUNT_ERROR_NONE;
5478 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data )
5480 int error_code = ACCOUNT_ERROR_NONE;
5481 account_stmt hstmt = NULL;
5482 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5485 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5486 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5487 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5489 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5491 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5492 hstmt = _account_prepare_query(query);
5494 rc = _account_query_step(hstmt);
5495 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5497 account_custom_s* custom_record = NULL;
5499 while (rc == SQLITE_ROW) {
5500 bool cb_ret = FALSE;
5501 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5503 if (custom_record == NULL) {
5504 ACCOUNT_FATAL("malloc Failed");
5508 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5510 _account_convert_column_to_custom(hstmt, custom_record);
5512 cb_ret = cb_func(custom_record->key, custom_record->value, user_data);
5514 _account_custom_item_free(custom_record);
5515 _ACCOUNT_FREE(custom_record);
5517 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5519 rc = _account_query_step(hstmt);
5522 _account_query_finalize(hstmt);
5525 error_code = ACCOUNT_ERROR_NONE;
5528 if (hstmt != NULL) {
5529 _account_query_finalize(hstmt);
5533 pthread_mutex_unlock(&account_mutex);
5537 static void _account_subscribe_vconf_callback(keynode_t *key, void *user_data)
5539 account_subscribe_s* tmp = (account_subscribe_s*)user_data;
5540 char *msg = NULL, *vconf_key = NULL;
5541 char event_msg[256] ={0, };
5542 int account_id = -1;
5544 ACCOUNT_DEBUG("START\n");
5547 ACCOUNT_ERROR("No subscribtion msg !!!!!\n");
5552 ACCOUNT_ERROR("user data required\n");
5556 if(!memcmp(vconf_keynode_get_name(key), VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR)))
5558 vconf_key = vconf_keynode_get_str(key);
5559 msg = strdup(vconf_key);
5561 char* event_type = NULL;
5565 event_type = strtok_r(msg, ":", &ptr);
5566 id = strtok_r(NULL, ":", &ptr);
5568 ACCOUNT_DEBUG("msg(%s), event_type(%s), id(%s)\n", msg, event_type, id);
5570 ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type);
5572 account_id = atoi(id);
5574 if(tmp->account_subscription_callback)
5575 tmp->account_subscription_callback(event_msg, account_id, tmp->user_data);
5580 ACCOUNT_DEBUG("END\n");
5583 ACCOUNT_API int account_subscribe_create(account_subscribe_h* account_subscribe)
5585 if (!account_subscribe) {
5586 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
5587 return ACCOUNT_ERROR_INVALID_PARAMETER;
5590 account_subscribe_s *data = (account_subscribe_s*)calloc(1,sizeof(account_subscribe_s));
5593 ACCOUNT_FATAL("OUT OF MEMORY\n");
5594 return ACCOUNT_ERROR_OUT_OF_MEMORY;
5597 ACCOUNT_VERBOSE("create handle=%p\n", *data);
5599 *account_subscribe = (account_subscribe_h)data;
5601 return ACCOUNT_ERROR_NONE;
5604 ACCOUNT_API int account_subscribe_notification(account_subscribe_h account_subscribe, account_event_cb cb_func, void* user_data)
5606 ACCOUNT_DEBUG("START\n");
5608 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5610 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5612 tmp->account_subscription_callback = cb_func;
5613 tmp->user_data = user_data;
5615 if (vconf_notify_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5616 (vconf_callback_fn)_account_subscribe_vconf_callback,
5618 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5619 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5622 ACCOUNT_DEBUG("Vconf Subscription Success!!!!\n");
5623 return ACCOUNT_ERROR_NONE;
5626 ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_subscribe)
5628 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5630 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5634 if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5635 (vconf_callback_fn)_account_subscribe_vconf_callback) != 0) {
5636 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5637 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5640 ACCOUNT_DEBUG("Vconf Unsubscription Success!!!!\n");
5641 return ACCOUNT_ERROR_NONE;