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>
35 #define ACCOUNT_VERIFY
37 #include <pkgmgr-info.h>
41 #define EAS_CMDLINE "/usr/bin/eas-engine"
42 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
43 #define ACTIVESYNC_APPID "activesync-ui"
46 #define EMAIL_APPID "email-setting-efl"
48 #define EMAIL_APPID "vxqbrefica"
53 static sqlite3* g_hAccountDB = NULL;
54 static int g_refCntDB = 0;
55 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
57 static char *_account_get_text(const char *text_data);
58 static int _account_gslist_free(GSList* list);
59 static int _account_glist_free(GList* list);
60 static char *_account_query_table_column_text(account_stmt pStmt, int pos);
61 static int _account_insert_custom(account_s *account, int account_id);
62 static int _account_update_custom(account_s *account, int account_id);
63 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data );
64 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
67 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
72 GSList** appid_list = (GSList**)user_data;
74 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
75 item = _account_get_text(appid);
76 *appid_list = g_slist_append(*appid_list, item);
81 static inline int __read_proc(const char *path, char *buf, int size)
85 if (buf == NULL || path == NULL) {
86 ACCOUNT_ERROR("path and buffer is mandatory\n");
90 fd = open(path, O_RDONLY);
92 ACCOUNT_ERROR("fd open error(%d)\n", fd);
96 ret = read(fd, buf, size - 1);
98 ACCOUNT_ERROR("fd read error(%d)\n", fd);
109 char *_account_get_proc_cmdline_bypid(int pid)
114 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
115 ret = __read_proc(buf, buf, sizeof(buf));
117 ACCOUNT_INFO("No proc directory (%d)\n", pid);
125 static char* _account_get_current_appid()
128 char appid[128]={0,};
129 pkgmgrinfo_appinfo_h ahandle=NULL;
130 char* appid_ret = NULL;
133 ACCOUNT_INFO("pid (%d)\n", pid, ret);
134 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
135 ACCOUNT_INFO("appid (%s) ret(%d)!!!!!!\n", appid, ret);
137 /* SLP platform core exception */
138 if(strlen(appid) == 0){
139 char* cmdline = NULL;
140 cmdline = _account_get_proc_cmdline_bypid(pid);
141 ACCOUNT_INFO("cmdline (%s)!!!!!!\n", cmdline);
142 if(!strcmp(cmdline, EAS_CMDLINE)) {
143 appid_ret = _account_get_text(ACTIVESYNC_APPID);
145 } else if (!strcmp(cmdline, EMAIL_SERVICE_CMDLINE)) {
146 appid_ret = _account_get_text(EMAIL_APPID);
149 ACCOUNT_ERROR("No app id\n");
154 appid_ret = _account_get_text(appid);
156 pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
160 static bool _account_verify_permission(const char* appid)
164 pkgmgrinfo_appinfo_h ahandle=NULL;
165 pkgmgrinfo_pkginfo_h phandle=NULL;
167 GSList* appid_list = NULL;
170 char* current_appid = _account_get_current_appid();
172 if(current_appid == NULL) {
173 // assuming current process is slp core daemon
174 ACCOUNT_DEBUG("current app id is null\n");
178 /* Get app id family which is stored in account database */
179 ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
180 ACCOUNT_INFO("ahandle (%p), ret(%x)\n", ahandle, ret);
181 ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
182 ACCOUNT_INFO("package_id (%s), ret(%x)\n", package_id, ret);
183 ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
184 ACCOUNT_INFO("phandle (%p), ret(%x)\n", package_id, ret);
186 ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); //==> pkgÇڵ鿡 ¼ÓÇÑ ui-appid ¸ðµÎ ãÀ½
187 ACCOUNT_INFO("ret(%x)\n", ret);
189 /* Compare current app id with the stored app id family */
190 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
191 char* tmp = (char*)iter->data;
192 if(tmp && !strcmp(tmp, current_appid)) {
193 ACCOUNT_INFO("permission verified appid(%s), current appid(%s)\n", tmp, current_appid);
196 ACCOUNT_DEBUG("owner group app id(%s), current appid(%s)\n", tmp, current_appid);
200 g_slist_free(appid_list);
201 pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
202 pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
204 _ACCOUNT_FREE(current_appid);
211 static const char *_account_db_err_msg()
213 assert(NULL != g_hAccountDB);
214 return sqlite3_errmsg(g_hAccountDB);
217 static void _account_insert_delete_update_notification_send(char *noti_name)
220 ACCOUNT_ERROR("Noti Name is NULL!!!!!!\n");
224 ACCOUNT_DEBUG("Sending notification with value %s\n", noti_name);
226 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) == 0) {
227 ACCOUNT_VERBOSE("Vconf MSG Str set SUCCESS !!!!!!\n");;
229 ACCOUNT_ERROR("Vconf MSG Str set FAILED !!!!!!\n");;
233 static int _account_get_record_count(char* query)
237 account_stmt pStmt = NULL;
239 assert(NULL != query);
240 assert(NULL != g_hAccountDB);
241 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
243 rc = sqlite3_step(pStmt);
244 if (SQLITE_ROW != rc) {
245 ACCOUNT_ERROR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
246 sqlite3_finalize(pStmt);
247 return ACCOUNT_ERROR_DB_FAILED;
250 ncount = sqlite3_column_int(pStmt, 0);
252 ACCOUNT_VERBOSE("count : %d, End", ncount);
253 sqlite3_finalize(pStmt);
258 static int _account_execute_query(char *query)
261 char* pszErrorMsg = NULL;
263 assert(NULL != query);
264 assert(NULL != g_hAccountDB);
266 ACCOUNT_INFO("query : %s", query);
268 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
269 if (SQLITE_OK != rc) {
270 ACCOUNT_FATAL("sqlite3_exec(%s) failed(%s).", query, pszErrorMsg);
271 sqlite3_free(pszErrorMsg);
277 static int _account_begin_transaction(void)
281 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
283 if (ret != SQLITE_OK) {
284 ACCOUNT_FATAL("_account_svc_begin_transaction fail :: %d", ret);
285 return ACCOUNT_ERROR_DB_FAILED;
288 return ACCOUNT_ERROR_NONE;
291 static int _account_end_transaction(bool is_success)
295 if (is_success == true) {
296 ret = _account_execute_query("COMMIT TRANSACTION");
298 ret = _account_execute_query("ROLLBACK TRANSACTION");
301 if (ret != SQLITE_OK) {
302 ACCOUNT_FATAL("_account_svc_end_transaction fail :: %d", ret);
303 return ACCOUNT_ERROR_DB_FAILED;
306 return ACCOUNT_ERROR_NONE;
309 static bool _account_check_add_more_account(const char* app_id)
311 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
314 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
315 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
317 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
319 ACCOUNT_DEBUG("app id (%s)\n", app_id);
321 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
322 rc = _account_get_record_count(query);
324 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
326 /* multiple account support case */
328 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
332 /* multiple account not support case */
333 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
334 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
335 rc = _account_get_record_count(query);
338 ACCOUNT_DEBUG("app id (%s) supports single account. and there is no account of the app id\n", app_id);
345 static int _account_create_all_tables(void)
348 int error_code = ACCOUNT_ERROR_NONE;
349 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
352 ACCOUNT_MEMSET(query, 0, sizeof(query));
354 ACCOUNT_VERBOSE("_account_create_all_tables begin");
356 /*Create the account table*/
357 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
358 rc = _account_get_record_count(query);
360 ACCOUNT_MEMSET(query, 0, sizeof(query));
361 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_SCHEMA, ACCOUNT_TABLE);
362 ACCOUNT_INFO("Create %s table", ACCOUNT_TABLE);
363 rc = _account_execute_query(query);
364 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
367 /*Create capability table*/
368 ACCOUNT_MEMSET(query, 0, sizeof(query));
369 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
370 rc = _account_get_record_count(query);
372 ACCOUNT_MEMSET(query, 0, sizeof(query));
373 ACCOUNT_SNPRINTF(query, sizeof(query), CAPABILITY_SCHEMA, CAPABILITY_TABLE);
374 ACCOUNT_INFO("Create %s table", CAPABILITY_TABLE);
375 rc = _account_execute_query(query);
376 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
379 /* Create account custom table */
380 ACCOUNT_MEMSET(query, 0, sizeof(query));
381 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
382 rc = _account_get_record_count(query);
384 ACCOUNT_MEMSET(query, 0, sizeof(query));
385 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_CUSTOM_SCHEMA, ACCOUNT_CUSTOM_TABLE);
386 ACCOUNT_INFO("Create %s table", ACCOUNT_CUSTOM_TABLE);
387 rc = _account_execute_query(query);
388 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
391 /* Create account type table */
392 ACCOUNT_MEMSET(query, 0, sizeof(query));
393 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
394 rc = _account_get_record_count(query);
396 ACCOUNT_MEMSET(query, 0, sizeof(query));
397 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_TYPE_SCHEMA, ACCOUNT_TYPE_TABLE);
398 ACCOUNT_INFO("Create %s table", ACCOUNT_TYPE_TABLE);
399 rc = _account_execute_query(query);
400 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
403 /* Create label table */
404 ACCOUNT_MEMSET(query, 0, sizeof(query));
405 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
406 rc = _account_get_record_count(query);
408 ACCOUNT_MEMSET(query, 0, sizeof(query));
409 ACCOUNT_SNPRINTF(query, sizeof(query), LABEL_SCHEMA, LABEL_TABLE);
410 ACCOUNT_INFO("Create %s table", LABEL_TABLE);
411 rc = _account_execute_query(query);
412 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
415 /* Create account feature table */
416 ACCOUNT_MEMSET(query, 0, sizeof(query));
417 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
418 rc = _account_get_record_count(query);
420 ACCOUNT_MEMSET(query, 0, sizeof(query));
421 ACCOUNT_SNPRINTF(query, sizeof(query), PROVIDER_FEATURE_SCHEMA, PROVIDER_FEATURE_TABLE);
422 ACCOUNT_INFO("Create %s table", PROVIDER_FEATURE_TABLE);
423 rc = _account_execute_query(query);
424 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
430 static bool _account_check_is_all_table_exists()
433 char query[ACCOUNT_SQL_LEN_MAX] = {0,};
434 ACCOUNT_MEMSET(query, 0, sizeof(query));
436 ACCOUNT_VERBOSE("_account_check_is_all_table_exists");
438 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
439 ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
440 rc = _account_get_record_count(query);
442 ACCOUNT_DEBUG("Table count = %d\n", rc);
444 if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
445 ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
449 ACCOUNT_VERBOSE("END of _account_check_is_all_table_exists\n");
454 static int _account_db_open(void)
457 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
459 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
462 rc = db_util_open(ACCOUNT_DB_NAME, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
463 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
466 ACCOUNT_INFO("_account_db_open: The database connected. refcnt=%d ", g_refCntDB);
469 ACCOUNT_INFO("The database already connected. refcnt=%d ", g_refCntDB);
472 return ACCOUNT_ERROR_NONE;
475 static int _account_db_close(void)
481 if (g_refCntDB > 0) {
483 ACCOUNT_INFO("_account_svc_db_close: The database disconnected. refcnt=%d ", g_refCntDB);
485 if (g_refCntDB == 0) {
486 rc = db_util_close(g_hAccountDB);
487 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
489 ACCOUNT_INFO( "_account_svc_db_close: The database disconnected really. ");
491 ret = ACCOUNT_ERROR_NONE;
493 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
494 ret = ACCOUNT_ERROR_DB_FAILED;
500 static int _account_connect(void)
502 int error_code = ACCOUNT_ERROR_NONE;
504 pthread_mutex_lock(&account_mutex);
506 ACCOUNT_VERBOSE("db path = %s\n", ACCOUNT_DB_NAME);
508 error_code = _account_db_open();
509 if (ACCOUNT_ERROR_NONE != error_code) {
510 ACCOUNT_ERROR("The database isn't connected.\n");
511 pthread_mutex_unlock(&account_mutex);
512 return ACCOUNT_ERROR_DB_NOT_OPENED;
515 if (FALSE == _account_check_is_all_table_exists())
516 error_code = _account_create_all_tables();
518 pthread_mutex_unlock(&account_mutex);
519 return ACCOUNT_ERROR_NONE;
522 ACCOUNT_API int account_connect (void)
524 return _account_connect();
527 static int _account_disconnect(void)
529 int error_code = ACCOUNT_ERROR_NONE;
531 pthread_mutex_lock(&account_mutex);
532 ACCOUNT_INFO("db path = %s have been closed!!!\n", ACCOUNT_DB_NAME);
534 error_code = _account_db_close();
535 pthread_mutex_unlock(&account_mutex);
540 ACCOUNT_API int account_disconnect (void)
542 return _account_disconnect();
545 static int _account_free_capability_items(account_capability_s *data)
547 _ACCOUNT_FREE(data->type);
548 _ACCOUNT_FREE(data->package_name);
549 _ACCOUNT_FREE(data->user_name);
551 return ACCOUNT_ERROR_NONE;
554 static int _account_custom_item_free(account_custom_s *data)
556 _ACCOUNT_FREE(data->app_id);
557 _ACCOUNT_FREE(data->key);
558 _ACCOUNT_FREE(data->value);
560 return ACCOUNT_ERROR_NONE;
563 static int _account_custom_gslist_free(GSList* list)
565 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
569 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
570 account_custom_s *custom_data = (account_custom_s*)iter->data;
571 _account_custom_item_free(custom_data);
572 _ACCOUNT_FREE(custom_data);
578 return ACCOUNT_ERROR_NONE;
581 static int _account_free_account_items(account_s *data)
583 _ACCOUNT_FREE(data->user_name);
584 _ACCOUNT_FREE(data->email_address);
585 _ACCOUNT_FREE(data->display_name);
586 _ACCOUNT_FREE(data->icon_path);
587 _ACCOUNT_FREE(data->source);
588 _ACCOUNT_FREE(data->package_name);
589 _ACCOUNT_FREE(data->domain_name);
590 _ACCOUNT_FREE(data->access_token);
593 for(i=0;i<USER_TXT_CNT;i++)
594 _ACCOUNT_FREE(data->user_data_txt[i]);
596 _account_gslist_free(data->capablity_list);
597 _account_glist_free(data->account_list);
598 _account_custom_gslist_free(data->custom_list);
600 return ACCOUNT_ERROR_NONE;
603 static int _account_gslist_free(GSList* list)
605 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
609 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
610 account_capability_s *cap_data = (account_capability_s*)iter->data;
611 _account_free_capability_items(cap_data);
612 _ACCOUNT_FREE(cap_data);
618 return ACCOUNT_ERROR_NONE;
621 static int _account_glist_free(GList* list)
623 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
627 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
628 account_s *account_record = (account_s*)iter->data;
629 _account_free_account_items(account_record);
630 _ACCOUNT_FREE(account_record);
636 return ACCOUNT_ERROR_NONE;
639 static gboolean _account_check_duplicated(account_s *data)
641 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
644 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
646 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and package_name='%s'"
647 , ACCOUNT_TABLE, data->user_name, data->package_name);
649 count = _account_get_record_count(query);
651 ACCOUNT_INFO("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
652 count, data->user_name, data->domain_name );
659 static int _account_get_next_sequence(char *pszName)
662 account_stmt pStmt = NULL;
664 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
666 ACCOUNT_VERBOSE( "[Enter] pszName:%s\n", pszName);
668 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
669 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
670 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
672 rc = sqlite3_step(pStmt);
673 max_seq = sqlite3_column_int(pStmt, 0);
676 ACCOUNT_VERBOSE( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
678 /*Finalize Statement*/
679 rc = sqlite3_finalize(pStmt);
685 static account_stmt _account_prepare_query(char *query)
688 account_stmt pStmt = NULL;
690 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
692 ACCOUNT_INFO( "prepare query : %s", query);
694 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
695 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
700 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
702 assert(NULL != pStmt);
704 return sqlite3_bind_int(pStmt, pos, num);
707 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
709 assert(NULL != pStmt);
712 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
714 return sqlite3_bind_null(pStmt, pos);
717 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
719 ACCOUNT_VERBOSE( "_account_convert_account_to_sql");
722 /*Caution : Keep insert query orders.*/
725 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
727 /* 2. email address*/
728 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
731 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
734 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
737 _account_query_bind_text(hstmt, count++, (char*)account->source);
740 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
743 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
746 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
749 _account_query_bind_int(hstmt, count++, account->auth_type);
752 _account_query_bind_int(hstmt, count++, account->secret);
754 /* 11. sync_support */
755 _account_query_bind_int(hstmt, count++, account->sync_support);
760 for(i=0; i< USER_TXT_CNT; i++)
761 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
763 /* 13. user integer */
764 for(i=0; i< USER_INT_CNT; i++)
765 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
770 static void _account_query_finalize(account_stmt pStmt)
775 ACCOUNT_FATAL( "pStmt is NULL");
779 rc = sqlite3_finalize(pStmt);
780 if (rc != SQLITE_OK) {
781 ACCOUNT_FATAL( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
784 ACCOUNT_VERBOSE( "sqlite3_finalize finish");
787 static int _account_query_step(account_stmt pStmt)
789 assert(NULL != pStmt);
790 return sqlite3_step(pStmt);
795 static int _do_account_owner_existance_check()
797 /* TODO check owner*/
798 return ACCOUNT_ERROR_NONE;
801 static int _account_execute_insert_query(account_s *account)
804 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
805 int error_code = ACCOUNT_ERROR_NONE;
806 account_stmt hstmt = NULL;
808 /* check whether app id exist in account type db */
810 if (!account->user_name && !account->display_name && !account->email_address) {
811 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
812 return ACCOUNT_ERROR_INVALID_PARAMETER;
815 /* Check account own icon existence */
816 if(!account->icon_path) {
817 char* temptxt = NULL;
818 account_type_h account_type = NULL;
819 error_code = account_type_create(&account_type);
820 error_code = account_type_query_by_app_id((const char*)account->package_name, &account_type);
821 error_code = account_type_get_icon_path(account_type, &temptxt);
822 if (error_code != ACCOUNT_ERROR_NONE) {
823 ACCOUNT_ERROR("account_type_get_icon_path: Failed \n");
824 } else if (temptxt && strlen(temptxt)) {
825 ACCOUNT_DEBUG("icon path (%s) app_id(%s) \n", temptxt, account->package_name);
826 account->icon_path = _account_get_text(temptxt);
828 ACCOUNT_DEBUG("account_get_icon_path: returned NULL \n");
830 _ACCOUNT_FREE(temptxt);
831 error_code = account_type_destroy(account_type);
834 /* End of Check account own icon existence */
836 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
837 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , "
838 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
839 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values "
840 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?)", ACCOUNT_TABLE);
842 hstmt = _account_prepare_query(query);
843 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
845 _account_convert_account_to_sql(account, hstmt, query);
847 rc = _account_query_step(hstmt);
848 if (rc != SQLITE_DONE) {
849 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
850 error_code = ACCOUNT_ERROR_DB_FAILED;
853 _account_query_finalize(hstmt);
859 static int _account_insert_capability(account_s *account, int account_id)
862 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
863 account_stmt hstmt = NULL;
865 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
867 if (g_slist_length( account->capablity_list)==0) {
868 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
869 return ACCOUNT_ERROR_NONE;
872 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
874 rc = _account_get_record_count(query);
877 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
878 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
885 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
889 account_capability_s* cap_data = NULL;
890 cap_data = (account_capability_s*)iter->data;
892 ACCOUNT_VERBOSE("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
894 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
895 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
896 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
897 hstmt = _account_prepare_query(query);
899 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
901 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
902 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
903 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
904 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
905 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
906 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
907 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
908 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
909 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
910 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
912 rc = _account_query_step(hstmt);
914 if (rc != SQLITE_DONE) {
915 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
919 _account_query_finalize(hstmt);
924 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
926 return ACCOUNT_ERROR_NONE;
929 static int _account_update_capability(account_s *account, int account_id)
932 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
933 account_stmt hstmt = NULL;
935 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
937 if (g_slist_length( account->capablity_list)==0) {
938 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
939 return ACCOUNT_ERROR_NONE;
942 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
944 rc = _account_get_record_count(query);
947 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
948 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
951 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
953 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
954 hstmt = _account_prepare_query(query);
956 _account_query_bind_int(hstmt, count++, (int)account_id);
957 rc = _account_query_step(hstmt);
959 if (rc != SQLITE_DONE) {
960 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
961 return ACCOUNT_ERROR_DB_FAILED;
963 _account_query_finalize(hstmt);
968 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
971 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
972 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
973 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
975 hstmt = _account_prepare_query(query);
977 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
979 account_capability_s* cap_data = NULL;
980 cap_data = (account_capability_s*)iter->data;
982 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
983 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
984 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
985 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
986 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
987 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
988 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
989 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
990 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
991 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
993 rc = _account_query_step(hstmt);
995 if (rc != SQLITE_DONE) {
996 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1000 _account_query_finalize(hstmt);
1005 ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
1007 return ACCOUNT_ERROR_NONE;
1010 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
1013 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1014 account_stmt hstmt = NULL;
1016 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1018 if (g_slist_length( account->capablity_list)==0) {
1019 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
1020 return ACCOUNT_ERROR_NONE;
1023 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
1025 rc = _account_get_record_count(query);
1028 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1029 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1032 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1034 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1035 hstmt = _account_prepare_query(query);
1037 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1038 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1039 rc = _account_query_step(hstmt);
1040 if (rc != SQLITE_DONE) {
1041 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1042 return ACCOUNT_ERROR_DB_FAILED;
1045 _account_query_finalize(hstmt);
1050 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1053 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1054 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1055 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1057 hstmt = _account_prepare_query(query);
1059 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1061 account_capability_s* cap_data = NULL;
1062 cap_data = (account_capability_s*)iter->data;
1064 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1065 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1066 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1067 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1068 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1069 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1070 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1071 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1072 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1073 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1075 rc = _account_query_step(hstmt);
1077 if (rc != SQLITE_DONE) {
1078 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1082 _account_query_finalize(hstmt);
1087 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
1089 return ACCOUNT_ERROR_NONE;
1092 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1094 assert(NULL != pStmt);
1096 return sqlite3_column_int(pStmt, pos);
1099 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
1101 assert(NULL != pStmt);
1103 return (char *)sqlite3_column_text(pStmt, pos);
1106 static void _account_db_data_to_text(const char *textbuf, char **output)
1108 if (textbuf && strlen(textbuf)>0) {
1113 *output = strdup(textbuf);
1117 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1119 char *textbuf = NULL;
1121 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1123 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1124 _account_db_data_to_text(textbuf, &(account_record->user_name));
1126 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1127 _account_db_data_to_text(textbuf, &(account_record->email_address));
1129 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1130 _account_db_data_to_text(textbuf, &(account_record->display_name));
1132 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1133 _account_db_data_to_text(textbuf, &(account_record->icon_path));
1135 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1136 _account_db_data_to_text(textbuf, &(account_record->source));
1138 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1139 _account_db_data_to_text(textbuf, &(account_record->package_name));
1141 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1142 _account_db_data_to_text(textbuf, &(account_record->access_token));
1144 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1145 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1147 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1149 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1151 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1153 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1154 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1156 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1157 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1159 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1160 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1162 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1163 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1165 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1166 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1168 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1169 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1170 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1171 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1172 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1174 ACCOUNT_VERBOSE("END _account_convert_column_to_account");
1177 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1179 char *textbuf = NULL;
1181 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1183 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1184 _account_db_data_to_text(textbuf, &(capability_record->type));
1186 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1188 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1189 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1191 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1192 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1194 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1196 ACCOUNT_VERBOSE("END _account_convert_column_to_capability");
1199 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1201 char *textbuf = NULL;
1203 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1205 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1206 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1208 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1209 _account_db_data_to_text(textbuf, &(custom_record->key));
1211 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1212 _account_db_data_to_text(textbuf, &(custom_record->value));
1214 ACCOUNT_VERBOSE("END _account_convert_column_to_custom");
1217 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1219 account_s *data = (account_s*)user_data;
1221 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1223 if (cap_data == NULL)
1225 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1227 cap_data->type = _account_get_text(capability_type);
1228 cap_data->value = capability_value;
1230 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1232 ACCOUNT_VERBOSE("_account_get_capability_text_cb :: %s\n", capability_type);
1238 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1240 account_s *data = (account_s*)user_data;
1242 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1244 if (custom_data == NULL) {
1245 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1248 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1250 custom_data->account_id = data->id;
1251 custom_data->app_id = _account_get_text(data->package_name);
1252 custom_data->key = _account_get_text(key);
1253 custom_data->value = _account_get_text(value);
1255 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1261 static char *_account_get_text(const char *text_data)
1263 char *text_value = NULL;
1265 if (text_data != NULL) {
1266 text_value = strdup(text_data);
1267 ACCOUNT_VERBOSE("text_value = %s", text_value);
1272 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
1274 int error_code = ACCOUNT_ERROR_NONE;
1275 account_stmt hstmt = NULL;
1276 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1278 account_s *old_account = NULL;
1280 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1281 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1282 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1283 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1285 old_account = (account_s*)calloc(1, sizeof(account_s));
1287 ACCOUNT_FATAL("Memory alloc fail\n");
1288 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1291 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1293 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
1294 hstmt = _account_prepare_query(query);
1296 rc = _account_query_step(hstmt);
1297 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1299 while (rc == SQLITE_ROW) {
1300 _account_convert_column_to_account(hstmt, old_account);
1301 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
1302 rc = _account_query_step(hstmt);
1305 _account_query_finalize(hstmt);
1309 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1310 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1313 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1314 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1317 new_account->id = old_account->id;
1320 if(!new_account->user_name) {
1321 if(old_account->user_name)
1322 new_account->user_name = _account_get_text(old_account->user_name);
1326 if(!new_account->display_name) {
1327 if(old_account->display_name)
1328 new_account->display_name = _account_get_text(old_account->display_name);
1332 if(!new_account->email_address) {
1333 if(old_account->email_address)
1334 new_account->email_address = _account_get_text(old_account->email_address);
1338 if(!new_account->domain_name) {
1339 if(old_account->domain_name)
1340 new_account->domain_name = _account_get_text(old_account->domain_name);
1344 if(!new_account->icon_path) {
1345 if(old_account->icon_path)
1346 new_account->icon_path = _account_get_text(old_account->icon_path);
1350 if(!new_account->source) {
1351 if(old_account->source)
1352 new_account->source = _account_get_text(old_account->source);
1355 #ifndef ACCOUNT_VERIFY
1357 if(!new_account->package_name) {
1358 if(old_account->package_name)
1359 new_account->package_name = _account_get_text(old_account->package_name);
1364 if(!new_account->access_token) {
1365 if(old_account->access_token)
1366 new_account->access_token = _account_get_text(old_account->access_token);
1371 for(i=0;i<USER_TXT_CNT;i++) {
1372 if(!new_account->user_data_txt[i]) {
1373 if(old_account->user_data_txt[i])
1374 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1379 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1380 new_account->auth_type = old_account->auth_type;
1384 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1385 new_account->secret = old_account->secret;
1389 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1390 new_account->sync_support = old_account->sync_support;
1394 for(i=0;i<USER_INT_CNT;i++) {
1395 if(new_account->user_data_int[i] == 0) {
1396 new_account->user_data_int[i] = old_account->user_data_int[i];
1402 // user custom table
1407 if (hstmt != NULL) {
1408 _account_query_finalize(hstmt);
1413 _account_free_account_items(old_account);
1414 _ACCOUNT_FREE(old_account);
1417 return ACCOUNT_ERROR_NONE;
1422 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
1424 int rc = 0, binding_count = 0, count = 0;
1425 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1426 int error_code = ACCOUNT_ERROR_NONE;
1427 account_stmt hstmt = NULL;
1429 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1430 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1432 _account_compare_old_record_by_user_name(account, user_name, package_name);
1434 if (!account->user_name && !account->display_name && !account->email_address) {
1435 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1436 return ACCOUNT_ERROR_INVALID_PARAMETER;
1440 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1441 , ACCOUNT_TABLE, user_name, package_name);
1443 count = _account_get_record_count(query);
1445 ACCOUNT_INFO("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1446 count, user_name, package_name);
1447 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1450 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1451 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1452 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1453 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1454 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1456 hstmt = _account_prepare_query(query);
1457 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1459 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1461 _account_query_bind_text(hstmt, binding_count++, user_name);
1462 _account_query_bind_text(hstmt, binding_count++, package_name);
1463 rc = _account_query_step(hstmt);
1464 if (rc != SQLITE_DONE) {
1465 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1467 _account_query_finalize(hstmt);
1470 /*update capability*/
1471 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1474 error_code = _account_update_custom(account, account->id);
1479 ACCOUNT_API int account_insert_to_db(account_h account, int *account_id)
1481 int error_code = ACCOUNT_ERROR_NONE;
1482 int ret_transaction = 0;
1484 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1485 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1486 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1488 account_s *data = (account_s*)account;
1490 pthread_mutex_lock(&account_mutex);
1492 /* transaction control required*/
1493 ret_transaction = _account_begin_transaction();
1495 if (ret_transaction != ACCOUNT_ERROR_NONE) {
1496 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
1497 pthread_mutex_unlock(&account_mutex);
1498 return ret_transaction;
1501 if (_account_check_duplicated(data)) {
1502 ret_transaction = _account_end_transaction(FALSE);
1503 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
1505 pthread_mutex_unlock(&account_mutex);
1506 return ACCOUNT_ERROR_DUPLICATED;
1508 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1510 #ifdef ACCOUNT_VERIFY
1512 appid = _account_get_current_appid();
1515 //replace appid to account->package_name
1516 _ACCOUNT_FREE(data->package_name);
1517 data->package_name = _account_get_text(appid);
1518 _ACCOUNT_FREE(appid);
1520 // API caller cannot be recognized
1521 ACCOUNT_ERROR("APP ID not detectable!\n");
1522 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1525 if (!account->package_name) {
1526 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1527 return ACCOUNT_ERROR_INVALID_PARAMETER;
1531 if(account_type_query_app_id_exist(data->package_name) != ACCOUNT_ERROR_NONE) {
1532 ret_transaction = _account_end_transaction(FALSE);
1533 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1534 pthread_mutex_unlock(&account_mutex);
1535 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1538 if(!_account_check_add_more_account(data->package_name)) {
1539 ret_transaction = _account_end_transaction(FALSE);
1540 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1541 pthread_mutex_unlock(&account_mutex);
1542 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1545 error_code = _account_execute_insert_query(data);
1547 if (error_code != ACCOUNT_ERROR_NONE) {
1548 ret_transaction = _account_end_transaction(FALSE);
1549 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1551 pthread_mutex_unlock(&account_mutex);
1556 ACCOUNT_VERBOSE( "_account_execute_insert_query, insert error_code : %d", error_code);
1558 error_code = _account_insert_capability(data, *account_id);
1559 if (error_code != ACCOUNT_ERROR_NONE) {
1560 ret_transaction = _account_end_transaction(FALSE);
1561 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1563 pthread_mutex_unlock(&account_mutex);
1567 error_code = _account_insert_custom(data, *account_id);
1568 if (error_code != ACCOUNT_ERROR_NONE) {
1569 ret_transaction = _account_end_transaction(FALSE);
1570 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1572 pthread_mutex_unlock(&account_mutex);
1576 pthread_mutex_unlock(&account_mutex);
1579 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1580 _account_insert_delete_update_notification_send(buf);
1582 _account_end_transaction(TRUE);
1584 return ACCOUNT_ERROR_NONE;
1588 ACCOUNT_API int account_create(account_h *account)
1591 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1592 return ACCOUNT_ERROR_INVALID_PARAMETER;
1595 account_s *data = (account_s*)malloc(sizeof(account_s));
1598 ACCOUNT_FATAL("Memory Allocation Failed");
1599 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1601 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1603 ACCOUNT_VERBOSE("create handle=%p\n", *account);
1605 /*Setting account as visible by default*/
1606 data->secret = ACCOUNT_SECRECY_VISIBLE;
1608 /*Setting account as not supporting sync by default*/
1609 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
1611 *account = (account_h)data;
1613 return ACCOUNT_ERROR_NONE;
1616 ACCOUNT_API int account_set_user_name(account_h account, const char *user_name)
1619 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1620 return ACCOUNT_ERROR_INVALID_PARAMETER;
1624 ACCOUNT_ERROR("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1625 return ACCOUNT_ERROR_INVALID_PARAMETER;
1628 account_s *data = (account_s*)account;
1630 _ACCOUNT_FREE(data->user_name);
1631 data->user_name = _account_get_text(user_name);
1633 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1635 return ACCOUNT_ERROR_NONE;
1638 ACCOUNT_API int account_set_display_name(account_h account, const char *display_name)
1641 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1642 return ACCOUNT_ERROR_INVALID_PARAMETER;
1645 if (!display_name) {
1646 ACCOUNT_ERROR("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1647 return ACCOUNT_ERROR_INVALID_PARAMETER;
1650 account_s *data = (account_s*)account;
1652 _ACCOUNT_FREE(data->display_name);
1653 data->display_name = _account_get_text(display_name);
1655 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1657 return ACCOUNT_ERROR_NONE;
1660 ACCOUNT_API int account_set_email_address(account_h account, const char *email_address)
1663 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1664 return ACCOUNT_ERROR_INVALID_PARAMETER;
1667 if (!email_address) {
1668 ACCOUNT_ERROR("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1669 return ACCOUNT_ERROR_INVALID_PARAMETER;
1672 account_s *data = (account_s*)account;
1674 _ACCOUNT_FREE(data->email_address);
1675 data->email_address = _account_get_text(email_address);
1677 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1679 return ACCOUNT_ERROR_NONE;
1682 ACCOUNT_API int account_set_icon_path(account_h account, const char *icon_path)
1685 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1686 return ACCOUNT_ERROR_INVALID_PARAMETER;
1690 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1691 return ACCOUNT_ERROR_INVALID_PARAMETER;
1694 account_s *data = (account_s*)account;
1696 _ACCOUNT_FREE(data->icon_path);
1697 data->icon_path = _account_get_text(icon_path);
1699 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1701 return ACCOUNT_ERROR_NONE;
1704 ACCOUNT_API int account_set_source(account_h account, const char *source)
1707 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1708 return ACCOUNT_ERROR_INVALID_PARAMETER;
1712 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1713 return ACCOUNT_ERROR_INVALID_PARAMETER;
1715 account_s *data = (account_s*)account;
1717 _ACCOUNT_FREE(data->source);
1718 data->source = _account_get_text(source);
1720 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1722 return ACCOUNT_ERROR_NONE;
1725 ACCOUNT_API int account_set_package_name(account_h account, const char *package_name)
1728 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1729 return ACCOUNT_ERROR_INVALID_PARAMETER;
1732 if (!package_name) {
1733 ACCOUNT_ERROR("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1734 return ACCOUNT_ERROR_INVALID_PARAMETER;
1737 account_s *data = (account_s*)account;
1739 _ACCOUNT_FREE(data->package_name);
1740 data->package_name = _account_get_text(package_name);
1742 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1744 return ACCOUNT_ERROR_NONE;
1747 ACCOUNT_API int account_set_domain_name(account_h account, const char *domain_name)
1750 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1751 return ACCOUNT_ERROR_INVALID_PARAMETER;
1755 ACCOUNT_ERROR("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1756 return ACCOUNT_ERROR_INVALID_PARAMETER;
1758 account_s *data = (account_s*)account;
1760 _ACCOUNT_FREE(data->domain_name);
1761 data->domain_name = _account_get_text(domain_name);
1763 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1765 return ACCOUNT_ERROR_NONE;
1768 ACCOUNT_API int account_set_access_token(account_h account, const char *access_token)
1771 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1772 return ACCOUNT_ERROR_INVALID_PARAMETER;
1775 if (!access_token) {
1776 ACCOUNT_ERROR("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1777 return ACCOUNT_ERROR_INVALID_PARAMETER;
1780 account_s *data = (account_s*)account;
1782 _ACCOUNT_FREE(data->access_token);
1783 data->access_token = _account_get_text(access_token);
1785 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1787 return ACCOUNT_ERROR_NONE;
1790 ACCOUNT_API int account_set_user_text(account_h account, int index, const char *user_txt)
1793 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1794 return ACCOUNT_ERROR_INVALID_PARAMETER;
1798 ACCOUNT_ERROR("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1799 return ACCOUNT_ERROR_INVALID_PARAMETER;
1801 if (index >= USER_TXT_CNT || index < 0) {
1802 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1803 return ACCOUNT_ERROR_INVALID_PARAMETER;
1806 account_s *data = (account_s*)account;
1808 _ACCOUNT_FREE(data->user_data_txt[index]);
1809 data->user_data_txt[index] = _account_get_text(user_txt);
1811 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1813 return ACCOUNT_ERROR_NONE;
1816 ACCOUNT_API int account_set_custom(account_h account, const char* key, const char* value)
1819 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1820 return ACCOUNT_ERROR_INVALID_PARAMETER;
1824 ACCOUNT_ERROR("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__);
1825 return ACCOUNT_ERROR_INVALID_PARAMETER;
1829 ACCOUNT_ERROR("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__);
1830 return ACCOUNT_ERROR_INVALID_PARAMETER;
1833 account_s *data = (account_s*)account;
1836 bool b_is_new = TRUE;
1838 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1840 account_custom_s* custom_data = NULL;
1841 custom_data = (account_custom_s*)iter->data;
1842 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
1844 if(!strcmp(custom_data->key, key)) {
1845 _ACCOUNT_FREE(custom_data->value);
1846 custom_data->value = _account_get_text(value);
1852 account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1854 if (custom_data == NULL) {
1855 ACCOUNT_FATAL("(%s)-(%d) MALLOC FAIL\n", __FUNCTION__, __LINE__);
1856 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1858 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1859 custom_data->account_id = data->id;
1860 custom_data->app_id = _account_get_text(data->package_name);
1861 custom_data->key = _account_get_text(key);
1862 custom_data->value = _account_get_text(value);
1863 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1866 return ACCOUNT_ERROR_NONE;
1869 ACCOUNT_API int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1871 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1873 if ( (auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
1874 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1875 return ACCOUNT_ERROR_INVALID_PARAMETER;
1878 account_s *data = (account_s*)account;
1880 data->auth_type = (int)auth_type;
1882 return ACCOUNT_ERROR_NONE;
1885 ACCOUNT_API int account_set_secret(account_h account, const account_secrecy_state_e secret)
1887 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1889 if ( (secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
1890 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1891 return ACCOUNT_ERROR_INVALID_PARAMETER;
1894 account_s *data = (account_s*)account;
1896 data->secret = (int)secret;
1898 return ACCOUNT_ERROR_NONE;
1901 ACCOUNT_API int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
1903 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1905 if ( (sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) {
1906 ACCOUNT_ERROR("(%s)-(%d) sync_support is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1907 return ACCOUNT_ERROR_INVALID_PARAMETER;
1910 account_s *data = (account_s*)account;
1912 data->sync_support= (int)sync_support;
1914 return ACCOUNT_ERROR_NONE;
1917 ACCOUNT_API int account_set_user_int(account_h account, int index, const int user_int)
1920 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1921 return ACCOUNT_ERROR_INVALID_PARAMETER;
1924 if (index >= USER_INT_CNT ||index < 0) {
1925 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1926 return ACCOUNT_ERROR_INVALID_PARAMETER;
1929 account_s *data = (account_s*)account;
1931 data->user_data_int[index] = user_int;
1933 return ACCOUNT_ERROR_NONE;
1936 ACCOUNT_API int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value)
1938 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null"));
1939 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null"));
1941 if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
1942 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1943 return ACCOUNT_ERROR_INVALID_PARAMETER;
1946 account_s *data = (account_s*)account;
1948 GSList *iter = NULL;
1949 bool b_is_new = TRUE;
1951 for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1952 account_capability_s *cap_data = NULL;
1953 cap_data = (account_capability_s*)iter->data;
1955 if(!strcmp(cap_data->type, capability_type)) {
1956 cap_data->value = capability_value;
1963 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1965 if (cap_data == NULL)
1966 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1967 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1969 cap_data->type = _account_get_text(capability_type);
1970 cap_data->value = capability_value;
1971 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1974 return ACCOUNT_ERROR_NONE;
1977 ACCOUNT_API int account_get_user_name(account_h account, char **user_name)
1980 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1981 return ACCOUNT_ERROR_INVALID_PARAMETER;
1985 ACCOUNT_ERROR("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
1986 return ACCOUNT_ERROR_INVALID_PARAMETER;
1989 account_s *data = (account_s*)account;
1991 (*user_name) = NULL;
1992 *user_name = _account_get_text(data->user_name);
1994 return ACCOUNT_ERROR_NONE;
1997 ACCOUNT_API int account_get_display_name(account_h account, char **display_name)
2000 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2001 return ACCOUNT_ERROR_INVALID_PARAMETER;
2004 if (!display_name) {
2005 ACCOUNT_ERROR("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
2006 return ACCOUNT_ERROR_INVALID_PARAMETER;
2009 account_s *data = (account_s*)account;
2011 (*display_name) = NULL;
2013 *display_name = _account_get_text(data->display_name);
2015 return ACCOUNT_ERROR_NONE;
2018 ACCOUNT_API int account_get_email_address(account_h account,char **email_address)
2021 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2022 return ACCOUNT_ERROR_INVALID_PARAMETER;
2025 if (!email_address) {
2026 ACCOUNT_ERROR("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
2027 return ACCOUNT_ERROR_INVALID_PARAMETER;
2030 account_s *data = (account_s*)account;
2032 (*email_address) = NULL;
2034 *email_address = _account_get_text(data->email_address);
2036 return ACCOUNT_ERROR_NONE;
2039 ACCOUNT_API int account_get_icon_path(account_h account, char **icon_path)
2042 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2043 return ACCOUNT_ERROR_INVALID_PARAMETER;
2047 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
2048 return ACCOUNT_ERROR_INVALID_PARAMETER;
2051 account_s *data = (account_s*)account;
2053 (*icon_path) = NULL;
2055 *icon_path = _account_get_text(data->icon_path);
2057 return ACCOUNT_ERROR_NONE;
2060 ACCOUNT_API int account_get_source(account_h account, char **source)
2063 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2064 return ACCOUNT_ERROR_INVALID_PARAMETER;
2068 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
2069 return ACCOUNT_ERROR_INVALID_PARAMETER;
2072 account_s *data = (account_s*)account;
2076 *source = _account_get_text(data->source);
2078 return ACCOUNT_ERROR_NONE;
2081 ACCOUNT_API int account_get_package_name(account_h account, char **package_name)
2084 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2085 return ACCOUNT_ERROR_INVALID_PARAMETER;
2088 if (!package_name) {
2089 ACCOUNT_ERROR("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
2090 return ACCOUNT_ERROR_INVALID_PARAMETER;
2093 account_s *data = (account_s*)account;
2095 (*package_name) = NULL;
2097 *package_name = _account_get_text(data->package_name);
2099 return ACCOUNT_ERROR_NONE;
2102 ACCOUNT_API int account_get_domain_name(account_h account, char **domain_name)
2105 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2106 return ACCOUNT_ERROR_INVALID_PARAMETER;
2110 ACCOUNT_ERROR("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
2111 return ACCOUNT_ERROR_INVALID_PARAMETER;
2114 account_s *data = (account_s*)account;
2116 (*domain_name) = NULL;
2118 *domain_name = _account_get_text(data->domain_name);
2120 return ACCOUNT_ERROR_NONE;
2123 ACCOUNT_API int account_get_access_token(account_h account, char **access_token)
2126 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2127 return ACCOUNT_ERROR_INVALID_PARAMETER;
2130 if (!access_token) {
2131 ACCOUNT_ERROR("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
2132 return ACCOUNT_ERROR_INVALID_PARAMETER;
2135 account_s *data = (account_s*)account;
2137 (*access_token) = NULL;
2139 *access_token = _account_get_text(data->access_token);
2141 return ACCOUNT_ERROR_NONE;
2144 ACCOUNT_API int account_get_user_text(account_h account, int user_text_index, char **text)
2147 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2148 return ACCOUNT_ERROR_INVALID_PARAMETER;
2152 ACCOUNT_ERROR("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
2153 return ACCOUNT_ERROR_INVALID_PARAMETER;
2155 ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2157 account_s *data = (account_s*)account;
2161 *text = _account_get_text(data->user_data_txt[user_text_index]);
2163 return ACCOUNT_ERROR_NONE;
2166 ACCOUNT_API int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
2169 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2170 return ACCOUNT_ERROR_INVALID_PARAMETER;
2173 ACCOUNT_ERROR("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
2174 return ACCOUNT_ERROR_INVALID_PARAMETER;
2177 account_s* data = (account_s*)account;
2179 *auth_type = data->auth_type;
2181 return ACCOUNT_ERROR_NONE;
2184 ACCOUNT_API int account_get_secret(account_h account, account_secrecy_state_e *secret)
2187 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2188 return ACCOUNT_ERROR_INVALID_PARAMETER;
2191 ACCOUNT_ERROR("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
2192 return ACCOUNT_ERROR_INVALID_PARAMETER;
2195 account_s* data = (account_s*)account;
2197 *secret = data->secret;
2199 return ACCOUNT_ERROR_NONE;
2202 ACCOUNT_API int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
2205 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2206 return ACCOUNT_ERROR_INVALID_PARAMETER;
2208 if (!sync_support) {
2209 ACCOUNT_ERROR("(%s)-(%d) sync_support is NULL.\n", __FUNCTION__, __LINE__);
2210 return ACCOUNT_ERROR_INVALID_PARAMETER;
2213 account_s* data = (account_s*)account;
2215 *sync_support = data->sync_support;
2217 return ACCOUNT_ERROR_NONE;
2220 ACCOUNT_API int account_get_account_id(account_h account, int *account_id)
2223 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2224 return ACCOUNT_ERROR_INVALID_PARAMETER;
2227 ACCOUNT_ERROR("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
2228 return ACCOUNT_ERROR_INVALID_PARAMETER;
2231 account_s *data = (account_s*)account;
2233 *account_id = data->id;
2235 return ACCOUNT_ERROR_NONE;
2238 ACCOUNT_API int account_get_user_int(account_h account, int user_int_index, int *integer)
2241 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2242 return ACCOUNT_ERROR_INVALID_PARAMETER;
2245 ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2248 ACCOUNT_ERROR("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
2249 return ACCOUNT_ERROR_INVALID_PARAMETER;
2252 account_s *data = (account_s*)account;
2254 *integer = data->user_data_int[user_int_index];
2256 return ACCOUNT_ERROR_NONE;
2259 ACCOUNT_API int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value)
2261 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2262 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL"));
2263 ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL"));
2266 account_s *data = (account_s*)account;
2268 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2269 account_capability_s *cap_data = NULL;
2271 cap_data = (account_capability_s*)iter->data;
2273 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2275 if(!strcmp(capability_type, cap_data->type)) {
2276 *capability_value = cap_data->value;
2277 return ACCOUNT_ERROR_NONE;
2281 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2284 ACCOUNT_API int account_get_capability_all(account_h account, capability_cb cb_func, void *user_data)
2286 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2287 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2290 account_s *data = (account_s*)account;
2292 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2293 account_capability_s *cap_data = NULL;
2295 cap_data = (account_capability_s*)iter->data;
2297 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2299 cb_func(cap_data->type, cap_data->value, user_data);
2302 return ACCOUNT_ERROR_NONE;
2305 ACCOUNT_API int account_get_custom(account_h account, const char* key, char** value)
2307 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2308 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST"));
2309 ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL"));
2312 account_s *data = (account_s*)account;
2314 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2315 account_custom_s *custom_data = NULL;
2317 custom_data = (account_custom_s*)iter->data;
2319 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2321 if(!strcmp(key, custom_data->key)) {
2323 *value = _account_get_text(custom_data->value);
2324 return ACCOUNT_ERROR_NONE;
2328 ACCOUNT_INFO("key is not found %s", key);
2330 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2333 ACCOUNT_API int account_get_custom_all(account_h account, account_custom_cb cb_func, void* user_data)
2335 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2336 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2339 account_s *data = (account_s*)account;
2341 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2342 bool cb_ret = FALSE;
2343 account_custom_s *custom_data = NULL;
2345 custom_data = (account_custom_s*)iter->data;
2347 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2349 cb_ret = cb_func(custom_data->key, custom_data->value, user_data);
2351 ACCOUNT_INFO("account_get_custom_all callback func ret = %d", cb_ret);
2356 return ACCOUNT_ERROR_NONE;
2359 ACCOUNT_API int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
2361 int error_code = ACCOUNT_ERROR_NONE;
2362 account_stmt hstmt = NULL;
2363 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2365 int ret = ACCOUNT_ERROR_NONE;
2367 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2368 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2369 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2371 ret = _do_account_owner_existance_check();
2372 if (ret != ACCOUNT_ERROR_NONE) {
2373 ACCOUNT_ERROR("_do_account_owner_existance_check Failed !!!\n");
2376 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2378 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2379 hstmt = _account_prepare_query(query);
2381 rc = _account_query_step(hstmt);
2382 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2384 account_capability_s* capability_record = NULL;
2386 while (rc == SQLITE_ROW) {
2387 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2389 if (capability_record == NULL) {
2390 ACCOUNT_FATAL("malloc Failed");
2394 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2396 _account_convert_column_to_capability(hstmt, capability_record);
2398 cb_func(capability_record->type, capability_record->value, user_data);
2400 _account_free_capability_items(capability_record);
2401 _ACCOUNT_FREE(capability_record);
2403 rc = _account_query_step(hstmt);
2406 _account_query_finalize(hstmt);
2409 error_code = ACCOUNT_ERROR_NONE;
2412 if (hstmt != NULL) {
2413 _account_query_finalize(hstmt);
2417 pthread_mutex_unlock(&account_mutex);
2421 static int _account_compare_old_record(account_s *new_account, int account_id)
2423 int error_code = ACCOUNT_ERROR_NONE;
2424 account_stmt hstmt = NULL;
2425 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2427 account_s *old_account = NULL;
2429 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2430 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2431 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2433 old_account = (account_s*)calloc(1, sizeof(account_s));
2435 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2437 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2438 hstmt = _account_prepare_query(query);
2440 rc = _account_query_step(hstmt);
2441 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2443 while (rc == SQLITE_ROW) {
2444 _account_convert_column_to_account(hstmt, old_account);
2445 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
2446 rc = _account_query_step(hstmt);
2449 _account_query_finalize(hstmt);
2453 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2454 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2457 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2458 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2462 new_account->id = old_account->id;
2465 if(!new_account->user_name) {
2466 if(old_account->user_name)
2467 new_account->user_name = _account_get_text(old_account->user_name);
2471 if(!new_account->display_name) {
2472 if(old_account->display_name)
2473 new_account->display_name = _account_get_text(old_account->display_name);
2477 if(!new_account->email_address) {
2478 if(old_account->email_address)
2479 new_account->email_address = _account_get_text(old_account->email_address);
2483 if(!new_account->domain_name) {
2484 if(old_account->domain_name)
2485 new_account->domain_name = _account_get_text(old_account->domain_name);
2489 if(!new_account->icon_path) {
2490 if(old_account->icon_path)
2491 new_account->icon_path = _account_get_text(old_account->icon_path);
2495 if(!new_account->source) {
2496 if(old_account->source)
2497 new_account->source = _account_get_text(old_account->source);
2500 #ifndef ACCOUNT_VERIFY
2502 if(!new_account->package_name) {
2503 if(old_account->package_name)
2504 new_account->package_name = _account_get_text(old_account->package_name);
2509 if(!new_account->access_token) {
2510 if(old_account->access_token)
2511 new_account->access_token = _account_get_text(old_account->access_token);
2516 for(i=0;i<USER_TXT_CNT;i++) {
2517 if(!new_account->user_data_txt[i]) {
2518 if(old_account->user_data_txt[i])
2519 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2524 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2525 new_account->auth_type = old_account->auth_type;
2529 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2530 new_account->secret = old_account->secret;
2534 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2535 new_account->sync_support = old_account->sync_support;
2539 for(i=0;i<USER_INT_CNT;i++) {
2540 if(new_account->user_data_int[i] == 0) {
2541 new_account->user_data_int[i] = old_account->user_data_int[i];
2547 // user custom table
2552 if (hstmt != NULL) {
2553 _account_query_finalize(hstmt);
2558 _account_free_account_items(old_account);
2559 _ACCOUNT_FREE(old_account);
2562 return ACCOUNT_ERROR_NONE;
2565 static int _account_update_account(account_s *account, int account_id)
2567 int rc = 0, binding_count =0;
2568 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2569 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2570 account_stmt hstmt = NULL;
2572 #ifdef ACCOUNT_VERIFY
2575 _ACCOUNT_FREE(account->package_name);
2576 appid = _account_get_current_appid();
2577 account->package_name = _account_get_text(appid);
2578 _ACCOUNT_FREE(appid);
2580 /* Check permission of requested appid */
2581 if(!_account_verify_permission(account->package_name)) {
2582 ACCOUNT_ERROR("No permission to update\n");
2583 return ACCOUNT_ERROR_PERMISSION_DENIED;
2587 _account_compare_old_record(account, account_id);
2589 if (!account->package_name) {
2590 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2591 return ACCOUNT_ERROR_INVALID_PARAMETER;
2595 if(account_type_query_app_id_exist(account->package_name) != ACCOUNT_ERROR_NONE) {
2596 ACCOUNT_ERROR("App id is not registered in account type DB!!!!\n");
2597 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
2600 if (!account->user_name && !account->display_name && !account->email_address) {
2601 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2602 return ACCOUNT_ERROR_INVALID_PARAMETER;
2605 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2607 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2609 count = _account_get_record_count(query);
2611 ACCOUNT_WARNING(" Account record not found, count = %d\n", count);
2612 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2615 /* transaction control required*/
2616 ret_transaction = _account_begin_transaction();
2618 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2619 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2620 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2621 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2622 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2624 hstmt = _account_prepare_query(query);
2625 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE)));
2627 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2628 _account_query_bind_int(hstmt, binding_count++, account_id);
2630 rc = _account_query_step(hstmt);
2631 if (rc != SQLITE_DONE) {
2632 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2635 _account_query_finalize(hstmt);
2638 /*update capability*/
2639 error_code = _account_update_capability(account, account_id);
2640 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2641 ret_transaction = _account_end_transaction(FALSE);
2642 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2647 error_code = _account_update_custom(account, account_id);
2648 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2649 ret_transaction = _account_end_transaction(FALSE);
2650 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2654 ret_transaction = _account_end_transaction(TRUE);
2655 ACCOUNT_DEBUG("update capability success, trying to commit(%x) !!!\n", ret_transaction);
2660 ACCOUNT_API int account_update_to_db_by_id(const account_h account, int account_id)
2662 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2663 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2664 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2665 int error_code = ACCOUNT_ERROR_NONE;
2666 account_s* data = (account_s*)account;
2668 pthread_mutex_lock(&account_mutex);
2670 error_code = _account_update_account(data, account_id);
2672 if(error_code != ACCOUNT_ERROR_NONE) {
2673 pthread_mutex_unlock(&account_mutex);
2677 pthread_mutex_unlock(&account_mutex);
2680 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2681 _account_insert_delete_update_notification_send(buf);
2683 return ACCOUNT_ERROR_NONE;
2686 ACCOUNT_API int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
2688 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2689 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2690 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2691 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2693 int error_code = ACCOUNT_ERROR_NONE;
2694 account_s *data = (account_s*)account;
2696 pthread_mutex_lock(&account_mutex);
2698 error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
2700 pthread_mutex_unlock(&account_mutex);
2703 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2704 _account_insert_delete_update_notification_send(buf);
2709 ACCOUNT_API int account_foreach_account_from_db(account_cb callback, void *user_data)
2711 int error_code = ACCOUNT_ERROR_NONE;
2712 account_stmt hstmt = NULL;
2713 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2715 GList *account_list = NULL;
2716 int ret = ACCOUNT_ERROR_NONE;
2718 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
2719 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2721 ret = _do_account_owner_existance_check();
2722 if (ret != ACCOUNT_ERROR_NONE) {
2723 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2726 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2728 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2729 hstmt = _account_prepare_query(query);
2731 rc = _account_query_step(hstmt);
2733 account_s *account_record = NULL;
2735 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2738 while(rc == SQLITE_ROW) {
2739 account_record = (account_s*) malloc(sizeof(account_s));
2741 if (account_record == NULL) {
2742 ACCOUNT_FATAL("malloc Failed");
2746 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2747 _account_convert_column_to_account(hstmt, account_record);
2748 account_list = g_list_append(account_list, account_record);
2749 rc = _account_query_step(hstmt);
2753 _account_query_finalize(hstmt);
2758 for (iter = account_list; iter != NULL; iter = g_list_next(iter)) {
2759 account_s *account = NULL;
2760 account = (account_s*)iter->data;
2761 account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2762 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2763 callback((account_h)account, user_data);
2767 error_code = ACCOUNT_ERROR_NONE;
2770 if (hstmt != NULL) {
2771 _account_query_finalize(hstmt);
2775 _account_glist_free(account_list);
2776 account_list = NULL;
2782 ACCOUNT_API int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status)
2784 int error_code = ACCOUNT_ERROR_NONE;
2785 account_stmt hstmt = NULL;
2786 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2790 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2791 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2792 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2793 ACCOUNT_ERROR("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2794 return ACCOUNT_ERROR_INVALID_PARAMETER;
2797 pthread_mutex_lock(&account_mutex);
2799 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2801 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2803 rc = _account_get_record_count(query);
2806 ACCOUNT_ERROR( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2807 pthread_mutex_unlock(&account_mutex);
2808 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2811 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2813 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2814 hstmt = _account_prepare_query(query);
2816 _account_query_bind_int(hstmt, count, (int)sync_status);
2818 rc = _account_query_step(hstmt);
2820 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2821 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2823 _account_query_finalize(hstmt);
2826 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2827 _account_insert_delete_update_notification_send(buf);
2830 error_code = ACCOUNT_ERROR_NONE;
2833 if (hstmt != NULL) {
2834 _account_query_finalize(hstmt);
2838 pthread_mutex_unlock(&account_mutex);
2842 ACCOUNT_API int account_query_account_by_account_id(int account_db_id, account_h *account)
2844 int error_code = ACCOUNT_ERROR_NONE;
2845 account_stmt hstmt = NULL;
2846 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2848 int ret = ACCOUNT_ERROR_NONE;
2850 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2851 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2852 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2854 ret = _do_account_owner_existance_check();
2855 if (ret != ACCOUNT_ERROR_NONE) {
2856 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2859 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2861 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2862 hstmt = _account_prepare_query(query);
2864 rc = _account_query_step(hstmt);
2865 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2867 account_s *account_record = (account_s *)(*account);
2869 while (rc == SQLITE_ROW) {
2870 _account_convert_column_to_account(hstmt, account_record);
2871 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
2872 rc = _account_query_step(hstmt);
2875 _account_query_finalize(hstmt);
2876 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2877 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2880 error_code = ACCOUNT_ERROR_NONE;
2883 if (hstmt != NULL) {
2884 _account_query_finalize(hstmt);
2888 pthread_mutex_unlock(&account_mutex);
2892 ACCOUNT_API int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
2894 int error_code = ACCOUNT_ERROR_NONE;
2895 account_stmt hstmt = NULL;
2896 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2899 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2900 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2903 int ret = ACCOUNT_ERROR_NONE;
2906 ret = _do_account_owner_existance_check();
2907 if (ret != ACCOUNT_ERROR_NONE) {
2908 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2911 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2913 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2915 hstmt = _account_prepare_query(query);
2917 int binding_count = 1;
2918 _account_query_bind_text(hstmt, binding_count++, user_name);
2920 rc = _account_query_step(hstmt);
2922 account_s *account_head = NULL;
2924 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2928 account_head = (account_s*) malloc(sizeof(account_s));
2929 if (account_head == NULL) {
2930 ACCOUNT_FATAL("malloc Failed");
2931 if (hstmt != NULL) {
2932 _account_query_finalize(hstmt);
2935 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2937 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2939 while (rc == SQLITE_ROW) {
2940 account_s* account_record = NULL;
2942 account_record = (account_s*) malloc(sizeof(account_s));
2944 if (account_record == NULL) {
2945 ACCOUNT_FATAL("malloc Failed");
2948 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2950 _account_convert_column_to_account(hstmt, account_record);
2952 account_head->account_list = g_list_append(account_head->account_list, account_record);
2954 rc = _account_query_step(hstmt);
2956 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2959 _account_query_finalize(hstmt);
2965 tmp = g_list_length(account_head->account_list);
2966 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2968 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2970 account = (account_h)iter->data;
2972 account_s *testaccount = (account_s*)account;
2974 ACCOUNT_VERBOSE("id = %d", testaccount->id);
2975 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
2976 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
2977 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
2978 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
2979 ACCOUNT_VERBOSE("source = %s", testaccount->source);
2980 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
2981 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
2982 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
2983 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
2984 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
2985 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
2987 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2988 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2990 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2992 cb_func(account, user_data);
2996 error_code = ACCOUNT_ERROR_NONE;
2999 if (hstmt != NULL) {
3000 _account_query_finalize(hstmt);
3004 if (account_head->account_list) {
3005 _account_glist_free(account_head->account_list);
3006 account_head->account_list = NULL;
3007 _account_free_account_items(account_head);
3009 _ACCOUNT_FREE(account_head);
3012 pthread_mutex_unlock(&account_mutex);
3016 ACCOUNT_API int account_query_account_by_capability(account_cb cb_func, const char* capability_type, account_capability_state_e capability_value, void* user_data)
3018 int error_code = ACCOUNT_ERROR_NONE;
3019 account_stmt hstmt = NULL;
3020 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3022 int ret = ACCOUNT_ERROR_NONE;
3024 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3026 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
3027 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
3028 return ACCOUNT_ERROR_INVALID_PARAMETER;
3031 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3032 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3034 ret = _do_account_owner_existance_check();
3035 if (ret != ACCOUNT_ERROR_NONE) {
3036 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
3039 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3041 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3043 hstmt = _account_prepare_query(query);
3045 int binding_count = 1;
3046 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3047 _account_query_bind_int(hstmt, binding_count++, (int)capability_value);
3049 rc = _account_query_step(hstmt);
3051 account_s* account_head = NULL;
3053 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3057 account_head = (account_s*) malloc(sizeof(account_s));
3058 if (account_head == NULL) {
3059 ACCOUNT_FATAL("malloc Failed");
3060 if (hstmt != NULL) {
3061 _account_query_finalize(hstmt);
3064 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3066 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3068 while (rc == SQLITE_ROW) {
3069 account_s* account_record = NULL;
3071 account_record = (account_s*) malloc(sizeof(account_s));
3073 if (account_record == NULL) {
3074 ACCOUNT_FATAL("malloc Failed");
3077 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3079 _account_convert_column_to_account(hstmt, account_record);
3081 account_head->account_list = g_list_append(account_head->account_list, account_record);
3083 rc = _account_query_step(hstmt);
3085 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3088 _account_query_finalize(hstmt);
3094 tmp = g_list_length(account_head->account_list);
3095 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3097 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3098 account_h account = NULL;
3099 account = (account_h)iter->data;
3100 account_s* testaccount = (account_s*)account;
3102 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3103 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3105 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3107 cb_func(account, user_data);
3112 error_code = ACCOUNT_ERROR_NONE;
3115 if (hstmt != NULL) {
3116 _account_query_finalize(hstmt);
3120 if (account_head->account_list) {
3121 _account_glist_free(account_head->account_list);
3122 account_head->account_list = NULL;
3123 _account_free_account_items(account_head);
3125 _ACCOUNT_FREE(account_head);
3128 pthread_mutex_unlock(&account_mutex);
3132 ACCOUNT_API int account_query_account_by_capability_type(account_cb cb_func, const char* capability_type, void* user_data)
3134 int error_code = ACCOUNT_ERROR_NONE;
3135 account_stmt hstmt = NULL;
3136 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3138 int ret = ACCOUNT_ERROR_NONE;
3140 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3141 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3142 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3144 ret = _do_account_owner_existance_check();
3145 if (ret != ACCOUNT_ERROR_NONE) {
3146 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
3149 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3151 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3153 hstmt = _account_prepare_query(query);
3155 int binding_count = 1;
3156 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3158 rc = _account_query_step(hstmt);
3160 account_s* account_head = NULL;
3162 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3166 account_head = (account_s*) malloc(sizeof(account_s));
3167 if (account_head == NULL) {
3168 ACCOUNT_FATAL("malloc Failed");
3169 if (hstmt != NULL) {
3170 _account_query_finalize(hstmt);
3173 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3175 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3177 while (rc == SQLITE_ROW) {
3178 account_s* account_record = NULL;
3180 account_record = (account_s*) malloc(sizeof(account_s));
3182 if (account_record == NULL) {
3183 ACCOUNT_FATAL("malloc Failed");
3186 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3188 _account_convert_column_to_account(hstmt, account_record);
3190 account_head->account_list = g_list_append(account_head->account_list, account_record);
3192 rc = _account_query_step(hstmt);
3194 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3197 _account_query_finalize(hstmt);
3203 tmp = g_list_length(account_head->account_list);
3204 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3206 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3207 account_h account = NULL;
3208 account = (account_h)iter->data;
3209 account_s* testaccount = (account_s*)account;
3211 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3212 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3214 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3216 cb_func(account, user_data);
3220 error_code = ACCOUNT_ERROR_NONE;
3223 if (hstmt != NULL) {
3224 _account_query_finalize(hstmt);
3228 if (account_head->account_list) {
3229 _account_glist_free(account_head->account_list);
3230 account_head->account_list = NULL;
3231 _account_free_account_items(account_head);
3233 _ACCOUNT_FREE(account_head);
3236 pthread_mutex_unlock(&account_mutex);
3240 ACCOUNT_API int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
3242 int error_code = ACCOUNT_ERROR_NONE;
3243 account_stmt hstmt = NULL;
3244 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3246 int ret = ACCOUNT_ERROR_NONE;
3248 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
3249 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3250 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3252 ret = _do_account_owner_existance_check();
3253 if (ret != ACCOUNT_ERROR_NONE) {
3254 ACCOUNT_ERROR("_do_account_owner_existance_check Failed !!!\n");
3257 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3259 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3261 hstmt = _account_prepare_query(query);
3263 int binding_count = 1;
3264 _account_query_bind_text(hstmt, binding_count++, package_name);
3266 rc = _account_query_step(hstmt);
3268 account_s* account_head = NULL;
3270 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3274 account_head = (account_s*) malloc(sizeof(account_s));
3275 if (account_head == NULL) {
3276 ACCOUNT_FATAL("malloc Failed");
3277 if (hstmt != NULL) {
3278 _account_query_finalize(hstmt);
3281 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3283 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3285 while (rc == SQLITE_ROW) {
3286 account_s* account_record = NULL;
3288 account_record = (account_s*) malloc(sizeof(account_s));
3290 if (account_record == NULL) {
3291 ACCOUNT_FATAL("malloc Failed");
3294 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3296 _account_convert_column_to_account(hstmt, account_record);
3298 account_head->account_list = g_list_append(account_head->account_list, account_record);
3300 rc = _account_query_step(hstmt);
3302 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3305 _account_query_finalize(hstmt);
3310 tmp = g_list_length(account_head->account_list);
3311 ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
3313 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3314 account_h account = NULL;
3315 account = (account_h)iter->data;
3317 account_s* testaccount = (account_s*)account;
3319 ACCOUNT_VERBOSE("id = %d", testaccount->id);
3320 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
3321 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
3322 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
3323 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
3324 ACCOUNT_VERBOSE("source = %s", testaccount->source);
3325 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
3326 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
3327 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
3328 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
3329 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
3330 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
3332 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3333 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3335 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3337 cb_func(account, user_data);
3341 error_code = ACCOUNT_ERROR_NONE;
3344 if (hstmt != NULL) {
3345 _account_query_finalize(hstmt);
3349 if (account_head->account_list) {
3350 _account_glist_free(account_head->account_list);
3351 account_head->account_list = NULL;
3352 _account_free_account_items(account_head);
3354 _ACCOUNT_FREE(account_head);
3357 pthread_mutex_unlock(&account_mutex);
3361 ACCOUNT_API int account_delete(int account_id)
3363 int error_code = ACCOUNT_ERROR_NONE;
3364 account_stmt hstmt = NULL;
3365 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3367 int ret_transaction = 0;
3368 bool is_success = FALSE;
3370 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3372 #ifdef ACCOUNT_VERIFY
3374 account_h account = NULL;
3375 /* Check requested ID to delete */
3376 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3378 count = _account_get_record_count(query);
3380 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3381 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3384 /* Check permission of requested appid */
3385 account_create(&account);
3386 account_query_account_by_account_id(account_id, &account);
3388 if(!_account_verify_permission(((account_s*)account)->package_name)) {
3389 ACCOUNT_ERROR("No permission to delete\n");
3390 account_destroy(account);
3391 return ACCOUNT_ERROR_PERMISSION_DENIED;
3394 account_destroy(account);
3397 /* transaction control required*/
3398 ret_transaction = _account_begin_transaction();
3400 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3401 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3402 pthread_mutex_unlock(&account_mutex);
3403 return ret_transaction;
3406 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3407 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3409 hstmt = _account_prepare_query(query);
3410 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3411 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3413 rc = _account_query_step(hstmt);
3414 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3416 _account_query_finalize(hstmt);
3419 ACCOUNT_MEMSET(query, 0, sizeof(query));
3421 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3423 hstmt = _account_prepare_query(query);
3424 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3425 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3427 rc = _account_query_step(hstmt);
3428 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3430 _account_query_finalize(hstmt);
3433 /* delete custom data */
3434 ACCOUNT_MEMSET(query, 0, sizeof(query));
3436 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3438 hstmt = _account_prepare_query(query);
3439 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3440 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3442 rc = _account_query_step(hstmt);
3443 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3445 _account_query_finalize(hstmt);
3450 if (hstmt != NULL) {
3451 _account_query_finalize(hstmt);
3455 ret_transaction = _account_end_transaction(is_success);
3457 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3458 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3460 if (is_success == true) {
3462 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3463 _account_insert_delete_update_notification_send(buf);
3467 pthread_mutex_unlock(&account_mutex);
3473 ACCOUNT_API int account_delete_from_db_by_id(int account_db_id)
3475 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
3477 return account_delete(account_db_id);
3480 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3482 int error_code = ACCOUNT_ERROR_NONE;
3483 account_stmt hstmt = NULL;
3484 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3486 int ret = ACCOUNT_ERROR_NONE;
3487 int binding_count = 1;
3489 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3490 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3491 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3492 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3494 ret = _do_account_owner_existance_check();
3495 if (ret != ACCOUNT_ERROR_NONE) {
3496 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
3499 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3501 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3502 hstmt = _account_prepare_query(query);
3504 _account_query_bind_text(hstmt, binding_count++, username);
3505 _account_query_bind_text(hstmt, binding_count++, package_name);
3507 rc = _account_query_step(hstmt);
3508 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3510 account_s *account_record = (account_s *)(*account);
3512 while (rc == SQLITE_ROW) {
3513 _account_convert_column_to_account(hstmt, account_record);
3514 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
3515 rc = _account_query_step(hstmt);
3518 _account_query_finalize(hstmt);
3519 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3520 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3523 error_code = ACCOUNT_ERROR_NONE;
3526 if (hstmt != NULL) {
3527 _account_query_finalize(hstmt);
3531 pthread_mutex_unlock(&account_mutex);
3535 ACCOUNT_API int account_delete_from_db_by_user_name(char *user_name, char *package_name)
3537 int error_code = ACCOUNT_ERROR_NONE;
3538 account_stmt hstmt = NULL;
3539 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3541 int ret_transaction = 0;
3542 bool is_success = FALSE;
3543 account_h account = NULL;
3544 int binding_count = 1;
3545 int account_id = -1;
3547 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3548 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3549 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3551 rc = account_create(&account);
3552 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3554 rc = account_get_account_id(account, &account_id);
3556 rc = account_destroy(account);
3558 /* transaction control required*/
3559 ret_transaction = _account_begin_transaction();
3561 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3562 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3563 pthread_mutex_unlock(&account_mutex);
3564 return ret_transaction;
3567 /* delete custom data */
3568 ACCOUNT_MEMSET(query, 0, sizeof(query));
3569 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3571 hstmt = _account_prepare_query(query);
3573 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3574 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3576 _account_query_bind_int(hstmt, binding_count++, account_id);
3578 rc = _account_query_step(hstmt);
3579 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3581 _account_query_finalize(hstmt);
3584 /* delete capability */
3585 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3587 hstmt = _account_prepare_query(query);
3589 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3590 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3593 _account_query_bind_text(hstmt, binding_count++, user_name);
3594 _account_query_bind_text(hstmt, binding_count++, package_name);
3596 rc = _account_query_step(hstmt);
3597 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3599 _account_query_finalize(hstmt);
3602 ACCOUNT_MEMSET(query, 0, sizeof(query));
3604 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3606 hstmt = _account_prepare_query(query);
3607 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3608 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3611 _account_query_bind_text(hstmt, binding_count++, user_name);
3612 _account_query_bind_text(hstmt, binding_count++, package_name);
3614 rc = _account_query_step(hstmt);
3615 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. user_name=%s, package_name=%s, rc=%d\n", user_name, package_name, rc));
3617 _account_query_finalize(hstmt);
3623 if (hstmt != NULL) {
3624 _account_query_finalize(hstmt);
3628 ret_transaction = _account_end_transaction(is_success);
3630 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3631 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3633 if (is_success == true) {
3635 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3636 _account_insert_delete_update_notification_send(buf);
3640 pthread_mutex_unlock(&account_mutex);
3645 ACCOUNT_API int account_delete_from_db_by_package_name(char *package_name)
3647 int error_code = ACCOUNT_ERROR_NONE;
3648 account_stmt hstmt = NULL;
3649 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3651 int ret_transaction = 0;
3652 bool is_success = FALSE;
3653 int binding_count = 1;
3655 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3656 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3658 /* transaction control required*/
3659 ret_transaction = _account_begin_transaction();
3661 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3662 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3663 pthread_mutex_unlock(&account_mutex);
3664 return ret_transaction;
3667 /* delete custom table */
3668 ACCOUNT_MEMSET(query, 0, sizeof(query));
3669 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3671 hstmt = _account_prepare_query(query);
3673 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3674 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3677 _account_query_bind_text(hstmt, binding_count++, package_name);
3679 rc = _account_query_step(hstmt);
3680 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3682 _account_query_finalize(hstmt);
3685 /* delete capability table */
3686 ACCOUNT_MEMSET(query, 0, sizeof(query));
3687 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3689 hstmt = _account_prepare_query(query);
3691 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3692 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3695 _account_query_bind_text(hstmt, binding_count++, package_name);
3697 rc = _account_query_step(hstmt);
3698 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3700 _account_query_finalize(hstmt);
3703 /* delete account table */
3704 ACCOUNT_MEMSET(query, 0, sizeof(query));
3706 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3708 hstmt = _account_prepare_query(query);
3709 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3710 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3713 _account_query_bind_text(hstmt, binding_count++, package_name);
3715 rc = _account_query_step(hstmt);
3716 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. package_name=%s, rc=%d\n", package_name, rc));
3718 _account_query_finalize(hstmt);
3724 if (hstmt != NULL) {
3725 _account_query_finalize(hstmt);
3729 ret_transaction = _account_end_transaction(is_success);
3731 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3732 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3734 if (is_success == true) {
3736 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, -1);
3737 _account_insert_delete_update_notification_send(buf);
3741 pthread_mutex_unlock(&account_mutex);
3746 ACCOUNT_API int account_get_total_count_from_db(int *count)
3749 ACCOUNT_ERROR("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3750 return ACCOUNT_ERROR_INVALID_PARAMETER;
3752 int ret = ACCOUNT_ERROR_NONE;
3753 char query[1024] = {0, };
3754 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3755 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3757 ret = _do_account_owner_existance_check();
3758 if (ret != ACCOUNT_ERROR_NONE) {
3759 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
3762 *count = _account_get_record_count(query);
3765 account_stmt pStmt = NULL;
3767 assert(NULL != g_hAccountDB);
3768 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3770 rc = sqlite3_step(pStmt);
3771 if (SQLITE_ROW != rc) {
3772 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3773 sqlite3_finalize(pStmt);
3774 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3777 ncount = sqlite3_column_int(pStmt, 0);
3781 ACCOUNT_VERBOSE("Number of account : %d", ncount);
3782 sqlite3_finalize(pStmt);
3785 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3786 return ACCOUNT_ERROR_DB_FAILED;
3789 return ACCOUNT_ERROR_NONE;
3792 ACCOUNT_API int account_destroy(account_h account)
3794 account_s *data = (account_s*)account;
3796 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3798 ACCOUNT_INFO("destroy handle=%p\n", data);
3800 _account_free_account_items(data);
3801 _ACCOUNT_FREE(data);
3803 return ACCOUNT_ERROR_NONE;
3806 static int _account_type_free_label_items(label_s *data)
3808 _ACCOUNT_FREE(data->app_id);
3809 _ACCOUNT_FREE(data->label);
3810 _ACCOUNT_FREE(data->locale);
3812 return ACCOUNT_ERROR_NONE;
3815 static int _account_type_free_feature_items(provider_feature_s *data)
3817 _ACCOUNT_FREE(data->app_id);
3818 _ACCOUNT_FREE(data->key);
3820 return ACCOUNT_ERROR_NONE;
3823 static int _account_type_gslist_free(GSList* list)
3825 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
3829 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
3830 label_s *label_data = (label_s*)iter->data;
3831 _account_type_free_label_items(label_data);
3832 _ACCOUNT_FREE(label_data);
3838 return ACCOUNT_ERROR_NONE;
3841 static int _account_type_item_free(account_type_s *data)
3843 _ACCOUNT_FREE(data->app_id);
3844 _ACCOUNT_FREE(data->service_provider_id);
3845 _ACCOUNT_FREE(data->icon_path);
3846 _ACCOUNT_FREE(data->small_icon_path);
3848 return ACCOUNT_ERROR_NONE;
3851 static int _account_type_glist_free(GList* list)
3853 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
3857 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
3858 account_type_s *account_type_record = (account_type_s*)iter->data;
3859 _account_type_item_free(account_type_record);
3860 _ACCOUNT_FREE(account_type_record);
3866 return ACCOUNT_ERROR_NONE;
3869 static int _account_type_free_account_type_items(account_type_s *data)
3871 _account_type_item_free(data);
3873 _account_type_gslist_free(data->label_list);
3874 _account_type_glist_free(data->account_type_list);
3876 return ACCOUNT_ERROR_NONE;
3879 ACCOUNT_API int account_type_create(account_type_h *account_type)
3881 if (!account_type) {
3882 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3883 return ACCOUNT_ERROR_INVALID_PARAMETER;
3886 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3889 ACCOUNT_ERROR("Memory Allocation Failed");
3890 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3893 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3895 ACCOUNT_VERBOSE("create handle=%p\n", *account_type);
3897 *account_type = (account_type_h)data;
3899 return ACCOUNT_ERROR_NONE;
3902 ACCOUNT_API int account_type_destroy(account_type_h account_type)
3904 account_type_s *data = (account_type_s*)account_type;
3906 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
3908 ACCOUNT_VERBOSE("destroy handle=%p\n", data);
3910 _account_type_free_account_type_items(data);
3911 _ACCOUNT_FREE(data);
3913 return ACCOUNT_ERROR_NONE;
3916 //app_id mandatory field
3917 ACCOUNT_API int account_type_set_app_id(account_type_h account_type, const char *app_id)
3919 if (!account_type) {
3920 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3921 return ACCOUNT_ERROR_INVALID_PARAMETER;
3925 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
3926 return ACCOUNT_ERROR_INVALID_PARAMETER;
3929 account_type_s *data = (account_type_s*)account_type;
3931 _ACCOUNT_FREE(data->app_id);
3932 data->app_id = _account_get_text(app_id);
3934 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->app_id, app_id);
3935 return ACCOUNT_ERROR_NONE;
3938 //service_provider_id mandatory field
3939 ACCOUNT_API int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
3941 if (!account_type) {
3942 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3943 return ACCOUNT_ERROR_INVALID_PARAMETER;
3946 if (!service_provider_id) {
3947 ACCOUNT_ERROR("(%s)-(%d) service_provider_id is NULL.\n", __FUNCTION__, __LINE__);
3948 return ACCOUNT_ERROR_INVALID_PARAMETER;
3951 account_type_s *data = (account_type_s*)account_type;
3953 _ACCOUNT_FREE(data->service_provider_id);
3954 data->service_provider_id = _account_get_text(service_provider_id);
3956 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->service_provider_id, service_provider_id);
3957 return ACCOUNT_ERROR_NONE;
3960 ACCOUNT_API int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
3962 if (!account_type) {
3963 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3964 return ACCOUNT_ERROR_INVALID_PARAMETER;
3968 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
3969 return ACCOUNT_ERROR_INVALID_PARAMETER;
3972 account_type_s *data = (account_type_s*)account_type;
3974 _ACCOUNT_FREE(data->icon_path);
3975 data->icon_path = _account_get_text(icon_path);
3977 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
3978 return ACCOUNT_ERROR_NONE;
3981 ACCOUNT_API int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
3983 if (!account_type) {
3984 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3985 return ACCOUNT_ERROR_INVALID_PARAMETER;
3988 if (!small_icon_path) {
3989 ACCOUNT_ERROR("(%s)-(%d) small_icon_path is NULL.\n", __FUNCTION__, __LINE__);
3990 return ACCOUNT_ERROR_INVALID_PARAMETER;
3993 account_type_s *data = (account_type_s*)account_type;
3995 _ACCOUNT_FREE(data->small_icon_path);
3996 data->small_icon_path = _account_get_text(small_icon_path);
3998 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->small_icon_path, small_icon_path);
3999 return ACCOUNT_ERROR_NONE;
4002 ACCOUNT_API int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
4004 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
4006 account_type_s *data = (account_type_s*)account_type;
4008 data->multiple_account_support = multiple_account_support;
4010 return ACCOUNT_ERROR_NONE;
4014 ACCOUNT_API int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
4016 if (!account_type) {
4017 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4018 return ACCOUNT_ERROR_INVALID_PARAMETER;
4021 if(!label || !locale) {
4022 ACCOUNT_ERROR("(%s)-(%d) label(%p) or locale(%p) is NULL.\n", __FUNCTION__, __LINE__, label, locale);
4023 return ACCOUNT_ERROR_INVALID_PARAMETER;
4026 account_type_s *data = (account_type_s*)account_type;
4027 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4029 if (label_data == NULL) {
4030 ACCOUNT_FATAL("(%s)-(%d) Malloc fail. label_data is NULL.\n", __FUNCTION__, __LINE__);
4031 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4033 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4035 label_data->label = _account_get_text(label);
4036 label_data->locale = _account_get_text(locale);
4038 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4040 return ACCOUNT_ERROR_NONE;
4043 ACCOUNT_API int account_type_get_app_id(account_type_h account_type, char **app_id)
4045 if (!account_type) {
4046 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4047 return ACCOUNT_ERROR_INVALID_PARAMETER;
4051 ACCOUNT_ERROR("(%s)-(%d) app id is NULL.\n", __FUNCTION__, __LINE__);
4052 return ACCOUNT_ERROR_INVALID_PARAMETER;
4055 account_type_s *data = (account_type_s*)account_type;
4058 *app_id = _account_get_text(data->app_id);
4060 return ACCOUNT_ERROR_NONE;
4063 ACCOUNT_API int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4065 if (!account_type) {
4066 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4067 return ACCOUNT_ERROR_INVALID_PARAMETER;
4070 if (!service_provider_id) {
4071 ACCOUNT_ERROR("(%s)-(%d) service provider id is NULL.\n", __FUNCTION__, __LINE__);
4072 return ACCOUNT_ERROR_INVALID_PARAMETER;
4075 account_type_s *data = (account_type_s*)account_type;
4077 (*service_provider_id) = NULL;
4078 *service_provider_id = _account_get_text(data->service_provider_id);
4080 return ACCOUNT_ERROR_NONE;
4083 ACCOUNT_API int account_type_get_icon_path(account_type_h account_type, char **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;
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 (*icon_path) = NULL;
4098 *icon_path = _account_get_text(data->icon_path);
4100 return ACCOUNT_ERROR_NONE;
4103 ACCOUNT_API int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4105 if (!account_type) {
4106 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4107 return ACCOUNT_ERROR_INVALID_PARAMETER;
4110 if (!small_icon_path) {
4111 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4112 return ACCOUNT_ERROR_INVALID_PARAMETER;
4115 account_type_s *data = (account_type_s*)account_type;
4117 (*small_icon_path) = NULL;
4118 *small_icon_path = _account_get_text(data->small_icon_path);
4120 return ACCOUNT_ERROR_NONE;
4123 ACCOUNT_API int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4125 if (!account_type) {
4126 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4127 return ACCOUNT_ERROR_INVALID_PARAMETER;
4129 if (!multiple_account_support) {
4130 ACCOUNT_ERROR("(%s)-(%d) multiple_account_support is NULL.\n", __FUNCTION__, __LINE__);
4131 return ACCOUNT_ERROR_INVALID_PARAMETER;
4134 account_type_s *data = (account_type_s*)account_type;
4136 *multiple_account_support = data->multiple_account_support;
4138 return ACCOUNT_ERROR_NONE;
4141 ACCOUNT_API int account_type_get_label(account_type_h account_type, account_label_cb cb_func, void *user_data)
4143 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4144 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4147 account_type_s *data = (account_type_s*)account_type;
4149 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4150 label_s *label_data = NULL;
4152 label_data = (label_s*)iter->data;
4154 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4156 cb_func(label_data->app_id, label_data->label, label_data->locale, user_data);
4159 return ACCOUNT_ERROR_NONE;
4162 static gboolean _account_type_check_duplicated(account_type_s *data)
4164 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4167 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4169 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4170 , ACCOUNT_TYPE_TABLE, data->app_id);
4172 count = _account_get_record_count(query);
4174 ACCOUNT_VERBOSE("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
4175 count, data->app_id, data->service_provider_id);
4182 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4186 /*Caution : Keep insert query orders.*/
4189 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4191 /* 2. service provider id*/
4192 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4195 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4197 /* 4. small icon path*/
4198 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4200 /* 5. multiple accont support*/
4201 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4207 static int _account_type_execute_insert_query(account_type_s *account_type)
4210 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4211 int error_code = ACCOUNT_ERROR_NONE;
4212 account_stmt hstmt = NULL;
4214 /* check mandatory field */
4215 // app id & service provider id
4216 if (!account_type->app_id) {
4217 ACCOUNT_ERROR("App id is mandetory field, it can not be NULL!!!!\n");
4218 return ACCOUNT_ERROR_INVALID_PARAMETER;
4221 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4222 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4223 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4225 hstmt = _account_prepare_query(query);
4226 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4228 _account_type_convert_account_to_sql(account_type, hstmt, query);
4230 rc = _account_query_step(hstmt);
4231 if (rc != SQLITE_DONE) {
4232 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4233 error_code = ACCOUNT_ERROR_DB_FAILED;
4236 _account_query_finalize(hstmt);
4242 static int _account_type_insert_label(account_type_s *account_type)
4245 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4246 account_stmt hstmt = NULL;
4248 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4250 if (g_slist_length( account_type->label_list)==0) {
4251 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4252 return ACCOUNT_ERROR_NONE;
4255 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4257 rc = _account_get_record_count(query);
4260 ACCOUNT_ERROR( "_account_insert_label : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
4261 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4267 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4270 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4271 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4272 "(?, ?, ?) ", LABEL_TABLE);
4274 hstmt = _account_prepare_query(query);
4276 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4278 label_s* label_data = NULL;
4279 label_data = (label_s*)iter->data;
4280 ACCOUNT_VERBOSE( "label_data->appid = %s, label_data->label = %s, label_data->locale \n", label_data->app_id, label_data->label, label_data->locale);
4282 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4283 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4284 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4285 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4286 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4287 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4289 rc = _account_query_step(hstmt);
4291 if (rc != SQLITE_DONE) {
4292 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4296 _account_query_finalize(hstmt);
4301 ACCOUNT_VERBOSE( "_account_type_insert_label() DONE\n");
4303 return ACCOUNT_ERROR_NONE;
4306 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4308 char *textbuf = NULL;
4310 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4311 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4313 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4314 _account_db_data_to_text(textbuf, &(feature_record->key));
4316 ACCOUNT_VERBOSE("END _account_type_convert_column_to_provider_feature");
4319 ACCOUNT_API int account_type_query_provider_feature_by_app_id(provider_feature_cb cb_func, const char* app_id, void *user_data )
4321 int error_code = ACCOUNT_ERROR_NONE;
4322 account_stmt hstmt = NULL;
4323 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4324 int rc = 0, binding_count = 1;
4326 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4327 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4328 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4330 ACCOUNT_VERBOSE("app_id = %s", app_id);
4332 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4334 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4335 hstmt = _account_prepare_query(query);
4337 _account_query_bind_text(hstmt, binding_count++, app_id);
4339 rc = _account_query_step(hstmt);
4340 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4342 provider_feature_s* feature_record = NULL;
4344 while (rc == SQLITE_ROW) {
4345 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4347 if (feature_record == NULL) {
4348 ACCOUNT_FATAL("malloc Failed");
4352 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4354 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4356 cb_func(feature_record->app_id, feature_record->key, user_data);
4358 _account_type_free_feature_items(feature_record);
4359 _ACCOUNT_FREE(feature_record);
4361 rc = _account_query_step(hstmt);
4364 _account_query_finalize(hstmt);
4367 error_code = ACCOUNT_ERROR_NONE;
4370 if (hstmt != NULL) {
4371 _account_query_finalize(hstmt);
4375 pthread_mutex_unlock(&account_mutex);
4379 ACCOUNT_API bool account_type_query_supported_feature(const char* app_id, const char* capability)
4381 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4382 int record_count = 0;
4384 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4385 ACCOUNT_RETURN_VAL((capability != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CAPABILITY"));
4387 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4389 record_count = _account_get_record_count(query);
4391 if (record_count <= 0) {
4392 ACCOUNT_WARNING( "related capability type item is not existed rc=%d , %s", record_count, _account_db_err_msg());
4401 ACCOUNT_API int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb cb_func, void* user_data)
4403 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4404 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4407 account_type_s *data = (account_type_s*)account_type;
4409 for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4410 provider_feature_s *feature_data = NULL;
4412 feature_data = (provider_feature_s*)iter->data;
4414 ACCOUNT_VERBOSE("appid = %s, key = %s", feature_data->key, feature_data->app_id);
4416 cb_func(feature_data->app_id, feature_data->key, user_data);
4419 return ACCOUNT_ERROR_NONE;
4422 ACCOUNT_API int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4424 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4425 ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4427 account_type_s *data = (account_type_s*)account_type;
4429 GSList *iter = NULL;
4430 bool b_is_new = TRUE;
4432 for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4433 provider_feature_s *feature_data = NULL;
4434 feature_data = (provider_feature_s*)iter->data;
4436 if(!strcmp(feature_data->key, provider_feature)) {
4443 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4445 if (feature_data == NULL)
4446 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4447 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4449 feature_data->key = _account_get_text(provider_feature);
4450 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4453 return ACCOUNT_ERROR_NONE;
4456 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4459 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4460 account_stmt hstmt = NULL;
4462 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4463 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4465 if (g_slist_length( account_type->provider_feature_list)==0) {
4466 ACCOUNT_ERROR( "no capability\n");
4467 return ACCOUNT_ERROR_NONE;
4470 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4472 rc = _account_get_record_count(query);
4475 ACCOUNT_WARNING( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4476 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4483 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4486 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4487 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4488 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4490 hstmt = _account_prepare_query(query);
4492 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4494 provider_feature_s* feature_data = NULL;
4495 feature_data = (provider_feature_s*)iter->data;
4496 ACCOUNT_VERBOSE("key = %s, app_id = %s \n", feature_data->key, app_id);
4498 ret = _account_query_bind_text(hstmt, count++, app_id);
4499 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4500 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4501 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4503 rc = _account_query_step(hstmt);
4505 if (rc != SQLITE_DONE) {
4506 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4510 _account_query_finalize(hstmt);
4515 ACCOUNT_VERBOSE( "_account_type_insert_provider_feature() DONE\n");
4517 return ACCOUNT_ERROR_NONE;
4520 ACCOUNT_API int account_type_insert_to_db(account_type_h account_type, int* account_type_id)
4522 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4524 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4525 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4526 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4528 account_type_s *data = (account_type_s*)account_type;
4530 pthread_mutex_lock(&account_mutex);
4533 /* transaction control required*/
4534 ret_transaction = _account_begin_transaction();
4536 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4537 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4538 pthread_mutex_unlock(&account_mutex);
4539 return ret_transaction;
4542 if (_account_type_check_duplicated(data)) {
4543 ret_transaction = _account_end_transaction(FALSE);
4544 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4545 *account_type_id = -1;
4546 pthread_mutex_unlock(&account_mutex);
4547 return ACCOUNT_ERROR_DUPLICATED;
4549 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4551 error_code = _account_type_execute_insert_query(data);
4553 if (error_code != ACCOUNT_ERROR_NONE){
4554 error_code = ACCOUNT_ERROR_DUPLICATED;
4555 ret_transaction = _account_end_transaction(FALSE);
4556 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4557 *account_type_id = -1;
4558 pthread_mutex_unlock(&account_mutex);
4563 ACCOUNT_INFO( "_account_type_execute_insert_query, insert error_code : %d", error_code);
4565 error_code = _account_type_insert_provider_feature(data, data->app_id);
4566 if(error_code != ACCOUNT_ERROR_NONE) {
4567 ret_transaction = _account_end_transaction(FALSE);
4568 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4569 pthread_mutex_unlock(&account_mutex);
4572 error_code = _account_type_insert_label(data);
4573 if(error_code != ACCOUNT_ERROR_NONE) {
4574 ret_transaction = _account_end_transaction(FALSE);
4575 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4576 pthread_mutex_unlock(&account_mutex);
4580 ret_transaction = _account_end_transaction(TRUE);
4581 ACCOUNT_DEBUG("Insert success(%x), commit insert query(%x)!!!!\n", error_code, ret_transaction);
4583 pthread_mutex_unlock(&account_mutex);
4585 return ACCOUNT_ERROR_NONE;
4588 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4591 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4592 account_stmt hstmt = NULL;
4594 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4596 if (g_slist_length( account_type->provider_feature_list)==0) {
4597 ACCOUNT_ERROR( "no feature\n");
4598 return ACCOUNT_ERROR_NONE;
4601 ACCOUNT_DEBUG( "app id", app_id);
4603 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4605 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4606 hstmt = _account_prepare_query(query);
4608 _account_query_bind_text(hstmt, count++, app_id);
4609 rc = _account_query_step(hstmt);
4611 if (rc != SQLITE_DONE) {
4612 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4613 return ACCOUNT_ERROR_DB_FAILED;
4615 _account_query_finalize(hstmt);
4620 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4623 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4624 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4625 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4627 hstmt = _account_prepare_query(query);
4629 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4631 provider_feature_s* feature_data = NULL;
4632 feature_data = (provider_feature_s*)iter->data;
4634 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4635 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4636 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4637 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4639 rc = _account_query_step(hstmt);
4641 if (rc != SQLITE_DONE) {
4642 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4645 _account_query_finalize(hstmt);
4649 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4651 return ACCOUNT_ERROR_NONE;
4654 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4657 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4658 account_stmt hstmt = NULL;
4660 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4662 if (g_slist_length( account_type->label_list)==0) {
4663 ACCOUNT_ERROR( "_account_type_update_label, no label\n");
4664 return ACCOUNT_ERROR_NONE;
4667 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4669 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4670 hstmt = _account_prepare_query(query);
4672 _account_query_bind_text(hstmt, count++, app_id);
4673 rc = _account_query_step(hstmt);
4675 if (rc != SQLITE_DONE) {
4676 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4677 return ACCOUNT_ERROR_DB_FAILED;
4679 _account_query_finalize(hstmt);
4684 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4687 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4688 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4689 "(?, ?, ?) ", LABEL_TABLE);
4691 hstmt = _account_prepare_query(query);
4693 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4695 label_s* label_data = NULL;
4696 label_data = (label_s*)iter->data;
4698 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4699 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4700 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4701 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4702 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
4703 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4705 rc = _account_query_step(hstmt);
4707 if (rc != SQLITE_DONE) {
4708 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4711 _account_query_finalize(hstmt);
4715 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4717 return ACCOUNT_ERROR_NONE;
4721 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
4723 int rc = 0, binding_count =1;
4724 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4725 int error_code = ACCOUNT_ERROR_NONE;
4726 account_stmt hstmt = NULL;
4728 if (!account_type->app_id) {
4729 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
4730 return ACCOUNT_ERROR_INVALID_PARAMETER;
4733 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4734 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
4735 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
4737 hstmt = _account_prepare_query(query);
4738 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
4740 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
4741 _account_query_bind_text(hstmt, binding_count++, app_id);
4743 rc = _account_query_step(hstmt);
4744 if (rc != SQLITE_DONE) {
4745 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4748 _account_query_finalize(hstmt);
4752 error_code = _account_type_update_label(account_type, app_id);
4753 /* update provider feature */
4754 error_code = _account_type_update_provider_feature(account_type, app_id);
4759 ACCOUNT_API int account_type_update_to_db_by_app_id(account_type_h account_type, const char* app_id)
4761 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
4762 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4763 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4765 int error_code = ACCOUNT_ERROR_NONE;
4766 account_type_s* data = (account_type_s*)account_type;
4768 pthread_mutex_lock(&account_mutex);
4770 error_code = _account_type_update_account(data, app_id);
4772 pthread_mutex_unlock(&account_mutex);
4777 ACCOUNT_API int account_type_delete_by_app_id(const char* app_id)
4779 int error_code = ACCOUNT_ERROR_NONE;
4780 account_stmt hstmt = NULL;
4781 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4782 int rc = 0, count = -1;
4783 int ret_transaction = 0;
4784 int binding_count = 1;
4785 bool is_success = FALSE;
4787 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4788 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
4790 /* Check requested ID to delete */
4791 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
4793 count = _account_get_record_count(query);
4795 ACCOUNT_ERROR("app id(%s) is not exist. count(%d)\n", app_id, count);
4796 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4799 /* transaction control required*/
4800 ret_transaction = _account_begin_transaction();
4802 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4803 ACCOUNT_FATAL("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4804 pthread_mutex_unlock(&account_mutex);
4805 return ret_transaction;
4808 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
4810 hstmt = _account_prepare_query(query);
4811 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4812 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4814 _account_query_bind_text(hstmt, binding_count++, app_id);
4816 rc = _account_query_step(hstmt);
4817 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4819 _account_query_finalize(hstmt);
4823 ACCOUNT_MEMSET(query, 0, sizeof(query));
4825 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_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. AppId=%s, rc=%d\n", app_id, rc));
4836 _account_query_finalize(hstmt);
4842 ACCOUNT_MEMSET(query, 0, sizeof(query));
4844 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
4846 hstmt = _account_prepare_query(query);
4847 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4848 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4850 _account_query_bind_text(hstmt, binding_count++, app_id);
4852 rc = _account_query_step(hstmt);
4853 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4855 _account_query_finalize(hstmt);
4861 if (hstmt != NULL) {
4862 _account_query_finalize(hstmt);
4866 ret_transaction = _account_end_transaction(is_success);
4868 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4869 ACCOUNT_FATAL("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4872 pthread_mutex_unlock(&account_mutex);
4877 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
4879 char *textbuf = NULL;
4881 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
4883 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
4884 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
4886 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
4887 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
4889 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
4890 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
4892 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
4893 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
4895 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
4897 ACCOUNT_VERBOSE("END _account_type_convert_column_to_account_type");
4900 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
4902 char *textbuf = NULL;
4904 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
4905 _account_db_data_to_text(textbuf, &(label_record->app_id));
4907 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
4908 _account_db_data_to_text(textbuf, &(label_record->label));
4910 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
4911 _account_db_data_to_text(textbuf, &(label_record->locale));
4913 ACCOUNT_VERBOSE("END _account_type_convert_column_to_label");
4916 ACCOUNT_API int account_type_query_label_by_app_id(account_label_cb cb_func, const char* app_id, void *user_data )
4918 int error_code = ACCOUNT_ERROR_NONE;
4919 account_stmt hstmt = NULL;
4920 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4921 int rc = 0, binding_count = 1;
4923 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4924 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4925 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4927 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4929 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
4930 hstmt = _account_prepare_query(query);
4932 _account_query_bind_text(hstmt, binding_count++, app_id);
4934 rc = _account_query_step(hstmt);
4935 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4937 label_s* label_record = NULL;
4939 while (rc == SQLITE_ROW) {
4940 label_record = (label_s*) malloc(sizeof(label_s));
4942 if (label_record == NULL) {
4943 ACCOUNT_FATAL("malloc Failed");
4947 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4949 _account_type_convert_column_to_label(hstmt, label_record);
4951 cb_func(label_record->app_id, label_record->label , label_record->locale, user_data);
4953 _account_type_free_label_items(label_record);
4954 _ACCOUNT_FREE(label_record);
4956 rc = _account_query_step(hstmt);
4959 _account_query_finalize(hstmt);
4962 error_code = ACCOUNT_ERROR_NONE;
4965 if (hstmt != NULL) {
4966 _account_query_finalize(hstmt);
4970 pthread_mutex_unlock(&account_mutex);
4974 int _account_type_label_get_app_id(label_h label, char **app_id)
4977 ACCOUNT_ERROR("(%s)-(%d) label handle is NULL.\n", __FUNCTION__, __LINE__);
4978 return ACCOUNT_ERROR_INVALID_PARAMETER;
4982 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4983 return ACCOUNT_ERROR_INVALID_PARAMETER;
4986 label_s *data = (label_s*)label;
4990 *app_id = _account_get_text(data->app_id);
4992 return ACCOUNT_ERROR_NONE;
4995 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
4997 account_type_s *data = (account_type_s*)user_data;
4999 label_s *label_data = (label_s*)malloc(sizeof(label_s));
5001 if (label_data == NULL) {
5002 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
5005 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
5007 label_data->app_id = _account_get_text(app_id);
5008 label_data->label = _account_get_text(label);
5009 label_data->locale = _account_get_text(locale);
5011 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5013 ACCOUNT_VERBOSE("_account_get_label_text_cb :: appid=%s, label=%s\n", label_data->app_id, label_data->label);
5018 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5020 account_type_s *data = (account_type_s*)user_data;
5022 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5024 if (feature_data == NULL) {
5025 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5028 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5030 feature_data->app_id = _account_get_text(app_id);
5031 feature_data->key = _account_get_text(key);
5033 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5035 ACCOUNT_VERBOSE("appid=%s, key=%s\n", feature_data->app_id, feature_data->key);
5040 ACCOUNT_API int account_type_query_by_app_id(const char* app_id, account_type_h *account_type)
5042 int error_code = ACCOUNT_ERROR_NONE;
5043 account_stmt hstmt = NULL;
5044 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5045 int rc = 0, binding_count = 1;
5047 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5048 ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL"));
5049 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5051 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5053 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5054 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5055 hstmt = _account_prepare_query(query);
5057 _account_query_bind_text(hstmt, binding_count++, app_id);
5059 rc = _account_query_step(hstmt);
5060 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5062 account_type_s *account_type_record = (account_type_s *)(*account_type);
5064 while (rc == SQLITE_ROW) {
5065 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5066 ACCOUNT_DEBUG("get account info by id %p\n", account_type_record);
5067 rc = _account_query_step(hstmt);
5070 _account_query_finalize(hstmt);
5071 account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)account_type_record);
5072 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)account_type_record);
5075 error_code = ACCOUNT_ERROR_NONE;
5078 if (hstmt != NULL) {
5079 _account_query_finalize(hstmt);
5083 pthread_mutex_unlock(&account_mutex);
5087 ACCOUNT_API int account_type_query_app_id_exist(const char* app_id)
5089 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5092 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5093 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5095 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5097 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5099 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5100 rc = _account_get_record_count(query);
5103 ACCOUNT_DEBUG("(%s) not exist in account type db");
5104 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5107 return ACCOUNT_ERROR_NONE;
5110 ACCOUNT_API int account_type_query_by_provider_feature(account_type_cb cb_func, const char* key, void* user_data)
5112 int error_code = ACCOUNT_ERROR_NONE;
5113 account_stmt hstmt = NULL;
5114 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5116 GList *account_type_list = NULL;
5118 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
5119 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
5120 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5122 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5124 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5126 hstmt = _account_prepare_query(query);
5128 int binding_count = 1;
5129 _account_query_bind_text(hstmt, binding_count++, (char*)key);
5131 rc = _account_query_step(hstmt);
5133 account_type_s *account_type_record = NULL;
5135 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5138 while(rc == SQLITE_ROW) {
5139 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5141 if (account_type_record == NULL) {
5142 ACCOUNT_FATAL("malloc Failed");
5146 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5147 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5148 account_type_list = g_list_append(account_type_list, account_type_record);
5149 rc = _account_query_step(hstmt);
5153 _account_query_finalize(hstmt);
5158 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5159 account_type_s *account_type = NULL;
5160 account_type = (account_type_s*)iter->data;
5161 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5162 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5163 cb_func((account_type_h)account_type, user_data);
5167 error_code = ACCOUNT_ERROR_NONE;
5170 if (hstmt != NULL) {
5171 _account_query_finalize(hstmt);
5174 if (account_type_list) {
5175 _account_type_glist_free(account_type_list);
5176 account_type_list = NULL;
5183 ACCOUNT_API int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data)
5185 int error_code = ACCOUNT_ERROR_NONE;
5186 account_stmt hstmt = NULL;
5187 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5189 GList *account_type_list = NULL;
5191 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
5192 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5194 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5196 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5197 hstmt = _account_prepare_query(query);
5199 rc = _account_query_step(hstmt);
5201 account_type_s *account_type_record = NULL;
5203 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5206 while(rc == SQLITE_ROW) {
5207 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5209 if (account_type_record == NULL) {
5210 ACCOUNT_FATAL("malloc Failed");
5214 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5215 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5216 account_type_list = g_list_append(account_type_list, account_type_record);
5217 rc = _account_query_step(hstmt);
5221 _account_query_finalize(hstmt);
5226 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5227 account_type_s *account_type = NULL;
5228 account_type = (account_type_s*)iter->data;
5229 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5230 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5231 callback((account_type_h)account_type, user_data);
5235 error_code = ACCOUNT_ERROR_NONE;
5238 if (hstmt != NULL) {
5239 _account_query_finalize(hstmt);
5242 if (account_type_list) {
5243 _account_type_glist_free(account_type_list);
5244 account_type_list = NULL;
5250 // output parameter label must be free
5251 ACCOUNT_API int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label)
5253 int error_code = ACCOUNT_ERROR_NONE;
5254 account_stmt hstmt = NULL;
5255 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5256 int rc = 0, binding_count = 1;
5258 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5259 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5260 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5262 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5264 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, locale);
5265 hstmt = _account_prepare_query(query);
5267 _account_query_bind_text(hstmt, binding_count++, app_id);
5269 rc = _account_query_step(hstmt);
5270 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5272 label_s* label_record = NULL;
5274 while (rc == SQLITE_ROW) {
5275 label_record = (label_s*) malloc(sizeof(label_s));
5277 if (label_record == NULL) {
5278 ACCOUNT_FATAL("malloc Failed");
5282 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5284 _account_type_convert_column_to_label(hstmt,label_record);
5286 *label = _account_get_text(label_record->label);
5288 _account_type_free_label_items(label_record);
5289 _ACCOUNT_FREE(label_record);
5291 rc = _account_query_step(hstmt);
5294 _account_query_finalize(hstmt);
5297 error_code = ACCOUNT_ERROR_NONE;
5300 if (hstmt != NULL) {
5301 _account_query_finalize(hstmt);
5305 pthread_mutex_unlock(&account_mutex);
5309 static int _account_insert_custom(account_s *account, int account_id)
5312 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5313 account_stmt hstmt = NULL;
5315 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5317 if (g_slist_length( account->custom_list)==0) {
5318 ACCOUNT_ERROR( "_account_insert_custom, no custom data\n");
5319 return ACCOUNT_ERROR_NONE;
5322 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5324 rc = _account_get_record_count(query);
5327 ACCOUNT_ERROR( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5328 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5335 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5338 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5339 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5340 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5342 hstmt = _account_prepare_query(query);
5344 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5346 account_custom_s* custom_data = NULL;
5347 custom_data = (account_custom_s*)iter->data;
5348 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
5350 ret = _account_query_bind_int(hstmt, count++, account_id);
5351 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5352 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5353 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5354 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5355 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5356 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5357 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5359 rc = _account_query_step(hstmt);
5361 if (rc != SQLITE_DONE) {
5362 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5366 _account_query_finalize(hstmt);
5371 ACCOUNT_VERBOSE( "_account_insert_custom() DONE\n");
5373 return ACCOUNT_ERROR_NONE;
5376 static int _account_update_custom(account_s *account, int account_id)
5379 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5380 account_stmt hstmt = NULL;
5382 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5384 ACCOUNT_INFO( "account handle=%p, account_id=%d", account, account_id);
5386 if (g_slist_length( account->custom_list)==0) {
5387 ACCOUNT_ERROR( "_account_update_custom, no custom data\n");
5388 return ACCOUNT_ERROR_NONE;
5391 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5393 rc = _account_get_record_count(query);
5396 ACCOUNT_ERROR( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5397 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5400 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5402 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5403 hstmt = _account_prepare_query(query);
5405 _account_query_bind_int(hstmt, count++, (int)account_id);
5406 rc = _account_query_step(hstmt);
5408 if (rc != SQLITE_DONE) {
5409 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5410 return ACCOUNT_ERROR_DB_FAILED;
5412 _account_query_finalize(hstmt);
5417 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5420 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5421 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5422 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5424 hstmt = _account_prepare_query(query);
5426 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5428 account_custom_s* custom_data = NULL;
5429 custom_data = (account_custom_s*)iter->data;
5431 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5432 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5433 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5434 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5435 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5436 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5437 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5438 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5440 rc = _account_query_step(hstmt);
5442 if (rc != SQLITE_DONE) {
5443 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5447 _account_query_finalize(hstmt);
5452 ACCOUNT_VERBOSE( "_account_update_custom() DONE\n");
5454 return ACCOUNT_ERROR_NONE;
5457 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data )
5459 int error_code = ACCOUNT_ERROR_NONE;
5460 account_stmt hstmt = NULL;
5461 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5464 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5465 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5466 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5468 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5470 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5471 hstmt = _account_prepare_query(query);
5473 rc = _account_query_step(hstmt);
5474 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5476 account_custom_s* custom_record = NULL;
5478 while (rc == SQLITE_ROW) {
5479 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5481 if (custom_record == NULL) {
5482 ACCOUNT_FATAL("malloc Failed");
5486 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5488 _account_convert_column_to_custom(hstmt, custom_record);
5490 cb_func(custom_record->key, custom_record->value, user_data);
5492 _account_custom_item_free(custom_record);
5493 _ACCOUNT_FREE(custom_record);
5495 rc = _account_query_step(hstmt);
5498 _account_query_finalize(hstmt);
5501 error_code = ACCOUNT_ERROR_NONE;
5504 if (hstmt != NULL) {
5505 _account_query_finalize(hstmt);
5509 pthread_mutex_unlock(&account_mutex);
5513 static void _account_subscribe_vconf_callback(keynode_t *key, void *user_data)
5515 account_subscribe_s* tmp = (account_subscribe_s*)user_data;
5516 char *msg = NULL, *vconf_key = NULL;
5517 char event_msg[256] ={0, };
5518 int account_id = -1;
5520 ACCOUNT_DEBUG("START\n");
5523 ACCOUNT_ERROR("No subscribtion msg !!!!!\n");
5528 ACCOUNT_ERROR("user data required\n");
5532 if(!memcmp(vconf_keynode_get_name(key), VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR)))
5534 vconf_key = vconf_keynode_get_str(key);
5535 msg = strdup(vconf_key);
5537 char* event_type = NULL;
5541 event_type = strtok_r(msg, ":", &ptr);
5542 id = strtok_r(NULL, ":", &ptr);
5544 ACCOUNT_DEBUG("msg(%s), event_type(%s), id(%s)\n", msg, event_type, id);
5546 ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type);
5548 account_id = atoi(id);
5550 if(tmp->account_subscription_callback)
5551 tmp->account_subscription_callback(event_msg, account_id, tmp->user_data);
5556 ACCOUNT_DEBUG("END\n");
5559 ACCOUNT_API int account_subscribe_create(account_subscribe_h* account_subscribe)
5561 if (!account_subscribe) {
5562 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
5563 return ACCOUNT_ERROR_INVALID_PARAMETER;
5566 account_subscribe_s *data = (account_subscribe_s*)calloc(1,sizeof(account_subscribe_s));
5569 ACCOUNT_FATAL("OUT OF MEMORY\n");
5570 return ACCOUNT_ERROR_OUT_OF_MEMORY;
5573 ACCOUNT_VERBOSE("create handle=%p\n", *data);
5575 *account_subscribe = (account_subscribe_h)data;
5577 return ACCOUNT_ERROR_NONE;
5580 ACCOUNT_API int account_subscribe_notification(account_subscribe_h account_subscribe, account_event_cb cb_func, void* user_data)
5582 ACCOUNT_DEBUG("START\n");
5584 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5586 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5588 tmp->account_subscription_callback = cb_func;
5589 tmp->user_data = user_data;
5591 if (vconf_notify_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5592 (vconf_callback_fn)_account_subscribe_vconf_callback,
5594 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5595 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5598 ACCOUNT_DEBUG("Vconf Subscription Success!!!!\n");
5599 return ACCOUNT_ERROR_NONE;
5602 ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_subscribe)
5604 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5606 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5610 if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5611 (vconf_callback_fn)_account_subscribe_vconf_callback) != 0) {
5612 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5613 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5616 ACCOUNT_DEBUG("Vconf Unsubscription Success!!!!\n");
5617 return ACCOUNT_ERROR_NONE;