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"
44 #define EMAIL_APPID "email-setting-efl"
47 static sqlite3* g_hAccountDB = NULL;
48 static int g_refCntDB = 0;
49 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
51 static char *_account_get_text(const char *text_data);
52 static int _account_gslist_free(GSList* list);
53 static int _account_glist_free(GList* list);
54 static char *_account_query_table_column_text(account_stmt pStmt, int pos);
55 static int _account_insert_custom(account_s *account, int account_id);
56 static int _account_update_custom(account_s *account, int account_id);
57 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data );
58 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
61 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
66 GSList** appid_list = (GSList**)user_data;
68 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
69 item = _account_get_text(appid);
70 *appid_list = g_slist_append(*appid_list, item);
75 static inline int __read_proc(const char *path, char *buf, int size)
79 if (buf == NULL || path == NULL) {
80 ACCOUNT_ERROR("path and buffer is mandatory\n");
84 fd = open(path, O_RDONLY);
86 ACCOUNT_ERROR("fd open error(%d)\n", fd);
90 ret = read(fd, buf, size - 1);
92 ACCOUNT_ERROR("fd read error(%d)\n", fd);
103 char *_account_get_proc_cmdline_bypid(int pid)
108 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
109 ret = __read_proc(buf, buf, sizeof(buf));
111 ACCOUNT_INFO("No proc directory (%d)\n", pid);
119 static char* _account_get_current_appid()
122 char appid[128]={0,};
123 pkgmgrinfo_appinfo_h ahandle=NULL;
124 char* appid_ret = NULL;
127 ACCOUNT_INFO("pid (%d)\n", pid, ret);
128 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
129 ACCOUNT_INFO("appid (%s) ret(%d)!!!!!!\n", appid, ret);
131 /* SLP platform core exception */
132 if(strlen(appid) == 0){
133 char* cmdline = NULL;
134 cmdline = _account_get_proc_cmdline_bypid(pid);
135 ACCOUNT_INFO("cmdline (%s)!!!!!!\n", cmdline);
136 if(!strcmp(cmdline, EAS_CMDLINE)) {
137 appid_ret = _account_get_text(ACTIVESYNC_APPID);
139 } else if (!strcmp(cmdline, EMAIL_SERVICE_CMDLINE)) {
140 appid_ret = _account_get_text(EMAIL_APPID);
143 ACCOUNT_ERROR("No app id\n");
148 appid_ret = _account_get_text(appid);
150 pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
154 static bool _account_verify_permission(const char* appid)
158 pkgmgrinfo_appinfo_h ahandle=NULL;
159 pkgmgrinfo_pkginfo_h phandle=NULL;
161 GSList* appid_list = NULL;
164 char* current_appid = _account_get_current_appid();
166 if(current_appid == NULL) {
167 // assuming current process is slp core daemon
168 ACCOUNT_DEBUG("current app id is null\n");
172 /* Get app id family which is stored in account database */
173 ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
174 ACCOUNT_INFO("ahandle (%p), ret(%x)\n", ahandle, ret);
175 ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
176 ACCOUNT_INFO("package_id (%s), ret(%x)\n", package_id, ret);
177 ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
178 ACCOUNT_INFO("phandle (%p), ret(%x)\n", package_id, ret);
180 ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); //==> pkgÇڵ鿡 ¼ÓÇÑ ui-appid ¸ðµÎ ãÀ½
181 ACCOUNT_INFO("ret(%x)\n", ret);
183 /* Compare current app id with the stored app id family */
184 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
185 char* tmp = (char*)iter->data;
186 if(tmp && !strcmp(tmp, current_appid)) {
187 ACCOUNT_INFO("permission verified appid(%s), current appid(%s)\n", tmp, current_appid);
190 ACCOUNT_DEBUG("owner group app id(%s), current appid(%s)\n", tmp, current_appid);
194 g_slist_free(appid_list);
195 pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
196 pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
198 _ACCOUNT_FREE(current_appid);
205 static const char *_account_db_err_msg()
207 assert(NULL != g_hAccountDB);
208 return sqlite3_errmsg(g_hAccountDB);
211 static void _account_insert_delete_update_notification_send(char *noti_name)
214 ACCOUNT_ERROR("Noti Name is NULL!!!!!!\n");
218 ACCOUNT_DEBUG("Sending notification with value %s\n", noti_name);
220 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) == 0) {
221 ACCOUNT_VERBOSE("Vconf MSG Str set SUCCESS !!!!!!\n");;
223 ACCOUNT_ERROR("Vconf MSG Str set FAILED !!!!!!\n");;
227 static int _account_get_record_count(char* query)
231 account_stmt pStmt = NULL;
233 assert(NULL != query);
234 assert(NULL != g_hAccountDB);
235 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
237 rc = sqlite3_step(pStmt);
238 if (SQLITE_ROW != rc) {
239 ACCOUNT_ERROR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
240 sqlite3_finalize(pStmt);
241 return ACCOUNT_ERROR_DB_FAILED;
244 ncount = sqlite3_column_int(pStmt, 0);
246 ACCOUNT_VERBOSE("count : %d, End", ncount);
247 sqlite3_finalize(pStmt);
252 static int _account_execute_query(char *query)
255 char* pszErrorMsg = NULL;
257 assert(NULL != query);
258 assert(NULL != g_hAccountDB);
260 ACCOUNT_INFO("query : %s", query);
262 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
263 if (SQLITE_OK != rc) {
264 ACCOUNT_FATAL("sqlite3_exec(%s) failed(%s).", query, pszErrorMsg);
265 sqlite3_free(pszErrorMsg);
271 static int _account_begin_transaction(void)
275 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
277 if (ret != SQLITE_OK) {
278 ACCOUNT_FATAL("_account_svc_begin_transaction fail :: %d", ret);
279 return ACCOUNT_ERROR_DB_FAILED;
282 return ACCOUNT_ERROR_NONE;
285 static int _account_end_transaction(bool is_success)
289 if (is_success == true) {
290 ret = _account_execute_query("COMMIT TRANSACTION");
292 ret = _account_execute_query("ROLLBACK TRANSACTION");
295 if (ret != SQLITE_OK) {
296 ACCOUNT_FATAL("_account_svc_end_transaction fail :: %d", ret);
297 return ACCOUNT_ERROR_DB_FAILED;
300 return ACCOUNT_ERROR_NONE;
303 static bool _account_check_add_more_account(const char* app_id)
305 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
308 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
309 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
311 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
313 ACCOUNT_DEBUG("app id (%s)\n", app_id);
315 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
316 rc = _account_get_record_count(query);
318 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
320 /* multiple account support case */
322 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
326 /* multiple account not support case */
327 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
328 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
329 rc = _account_get_record_count(query);
332 ACCOUNT_DEBUG("app id (%s) supports single account. and there is no account of the app id\n", app_id);
339 static int _account_create_all_tables(void)
342 int error_code = ACCOUNT_ERROR_NONE;
343 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
346 ACCOUNT_MEMSET(query, 0, sizeof(query));
348 ACCOUNT_VERBOSE("_account_create_all_tables begin");
350 /*Create the account table*/
351 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
352 rc = _account_get_record_count(query);
354 ACCOUNT_MEMSET(query, 0, sizeof(query));
355 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_SCHEMA, ACCOUNT_TABLE);
356 ACCOUNT_INFO("Create %s table", ACCOUNT_TABLE);
357 rc = _account_execute_query(query);
358 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
361 /*Create capability table*/
362 ACCOUNT_MEMSET(query, 0, sizeof(query));
363 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
364 rc = _account_get_record_count(query);
366 ACCOUNT_MEMSET(query, 0, sizeof(query));
367 ACCOUNT_SNPRINTF(query, sizeof(query), CAPABILITY_SCHEMA, CAPABILITY_TABLE);
368 ACCOUNT_INFO("Create %s table", CAPABILITY_TABLE);
369 rc = _account_execute_query(query);
370 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
373 /* Create account custom table */
374 ACCOUNT_MEMSET(query, 0, sizeof(query));
375 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
376 rc = _account_get_record_count(query);
378 ACCOUNT_MEMSET(query, 0, sizeof(query));
379 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_CUSTOM_SCHEMA, ACCOUNT_CUSTOM_TABLE);
380 ACCOUNT_INFO("Create %s table", ACCOUNT_CUSTOM_TABLE);
381 rc = _account_execute_query(query);
382 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
385 /* Create account type table */
386 ACCOUNT_MEMSET(query, 0, sizeof(query));
387 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
388 rc = _account_get_record_count(query);
390 ACCOUNT_MEMSET(query, 0, sizeof(query));
391 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_TYPE_SCHEMA, ACCOUNT_TYPE_TABLE);
392 ACCOUNT_INFO("Create %s table", ACCOUNT_TYPE_TABLE);
393 rc = _account_execute_query(query);
394 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
397 /* Create label table */
398 ACCOUNT_MEMSET(query, 0, sizeof(query));
399 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
400 rc = _account_get_record_count(query);
402 ACCOUNT_MEMSET(query, 0, sizeof(query));
403 ACCOUNT_SNPRINTF(query, sizeof(query), LABEL_SCHEMA, LABEL_TABLE);
404 ACCOUNT_INFO("Create %s table", LABEL_TABLE);
405 rc = _account_execute_query(query);
406 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
409 /* Create account feature table */
410 ACCOUNT_MEMSET(query, 0, sizeof(query));
411 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
412 rc = _account_get_record_count(query);
414 ACCOUNT_MEMSET(query, 0, sizeof(query));
415 ACCOUNT_SNPRINTF(query, sizeof(query), PROVIDER_FEATURE_SCHEMA, PROVIDER_FEATURE_TABLE);
416 ACCOUNT_INFO("Create %s table", PROVIDER_FEATURE_TABLE);
417 rc = _account_execute_query(query);
418 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
424 static bool _account_check_is_all_table_exists()
427 char query[ACCOUNT_SQL_LEN_MAX] = {0,};
428 ACCOUNT_MEMSET(query, 0, sizeof(query));
430 ACCOUNT_VERBOSE("_account_check_is_all_table_exists");
432 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
433 ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
434 rc = _account_get_record_count(query);
436 ACCOUNT_DEBUG("Table count = %d\n", rc);
438 if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
439 ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
443 ACCOUNT_VERBOSE("END of _account_check_is_all_table_exists\n");
448 static int _account_db_open(void)
451 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
453 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
456 rc = db_util_open(ACCOUNT_DB_NAME, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
457 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
460 ACCOUNT_INFO("_account_db_open: The database connected. refcnt=%d ", g_refCntDB);
463 ACCOUNT_INFO("The database already connected. refcnt=%d ", g_refCntDB);
466 return ACCOUNT_ERROR_NONE;
469 static int _account_db_close(void)
475 if (g_refCntDB > 0) {
477 ACCOUNT_INFO("_account_svc_db_close: The database disconnected. refcnt=%d ", g_refCntDB);
479 if (g_refCntDB == 0) {
480 rc = db_util_close(g_hAccountDB);
481 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
483 ACCOUNT_INFO( "_account_svc_db_close: The database disconnected really. ");
485 ret = ACCOUNT_ERROR_NONE;
487 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
488 ret = ACCOUNT_ERROR_DB_FAILED;
494 static int _account_connect(void)
496 int error_code = ACCOUNT_ERROR_NONE;
498 pthread_mutex_lock(&account_mutex);
500 ACCOUNT_VERBOSE("db path = %s\n", ACCOUNT_DB_NAME);
502 error_code = _account_db_open();
503 if (ACCOUNT_ERROR_NONE != error_code) {
504 ACCOUNT_ERROR("The database isn't connected.\n");
505 pthread_mutex_unlock(&account_mutex);
506 return ACCOUNT_ERROR_DB_NOT_OPENED;
509 if (FALSE == _account_check_is_all_table_exists())
510 error_code = _account_create_all_tables();
512 pthread_mutex_unlock(&account_mutex);
513 return ACCOUNT_ERROR_NONE;
516 ACCOUNT_API int account_connect (void)
518 return _account_connect();
521 static int _account_disconnect(void)
523 int error_code = ACCOUNT_ERROR_NONE;
525 pthread_mutex_lock(&account_mutex);
526 ACCOUNT_INFO("db path = %s have been closed!!!\n", ACCOUNT_DB_NAME);
528 error_code = _account_db_close();
529 pthread_mutex_unlock(&account_mutex);
534 ACCOUNT_API int account_disconnect (void)
536 return _account_disconnect();
539 static int _account_free_capability_items(account_capability_s *data)
541 _ACCOUNT_FREE(data->type);
542 _ACCOUNT_FREE(data->package_name);
543 _ACCOUNT_FREE(data->user_name);
545 return ACCOUNT_ERROR_NONE;
548 static int _account_custom_item_free(account_custom_s *data)
550 _ACCOUNT_FREE(data->app_id);
551 _ACCOUNT_FREE(data->key);
552 _ACCOUNT_FREE(data->value);
554 return ACCOUNT_ERROR_NONE;
557 static int _account_custom_gslist_free(GSList* list)
559 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
563 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
564 account_custom_s *custom_data = (account_custom_s*)iter->data;
565 _account_custom_item_free(custom_data);
566 _ACCOUNT_FREE(custom_data);
572 return ACCOUNT_ERROR_NONE;
575 static int _account_free_account_items(account_s *data)
577 _ACCOUNT_FREE(data->user_name);
578 _ACCOUNT_FREE(data->email_address);
579 _ACCOUNT_FREE(data->display_name);
580 _ACCOUNT_FREE(data->icon_path);
581 _ACCOUNT_FREE(data->source);
582 _ACCOUNT_FREE(data->package_name);
583 _ACCOUNT_FREE(data->domain_name);
584 _ACCOUNT_FREE(data->access_token);
587 for(i=0;i<USER_TXT_CNT;i++)
588 _ACCOUNT_FREE(data->user_data_txt[i]);
590 _account_gslist_free(data->capablity_list);
591 _account_glist_free(data->account_list);
592 _account_custom_gslist_free(data->custom_list);
594 return ACCOUNT_ERROR_NONE;
597 static int _account_gslist_free(GSList* list)
599 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
603 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
604 account_capability_s *cap_data = (account_capability_s*)iter->data;
605 _account_free_capability_items(cap_data);
606 _ACCOUNT_FREE(cap_data);
612 return ACCOUNT_ERROR_NONE;
615 static int _account_glist_free(GList* list)
617 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
621 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
622 account_s *account_record = (account_s*)iter->data;
623 _account_free_account_items(account_record);
624 _ACCOUNT_FREE(account_record);
630 return ACCOUNT_ERROR_NONE;
633 static gboolean _account_check_duplicated(account_s *data)
635 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
638 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
640 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and package_name='%s'"
641 , ACCOUNT_TABLE, data->user_name, data->package_name);
643 count = _account_get_record_count(query);
645 ACCOUNT_INFO("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
646 count, data->user_name, data->domain_name );
653 static int _account_get_next_sequence(char *pszName)
656 account_stmt pStmt = NULL;
658 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
660 ACCOUNT_VERBOSE( "[Enter] pszName:%s\n", pszName);
662 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
663 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
664 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
666 rc = sqlite3_step(pStmt);
667 max_seq = sqlite3_column_int(pStmt, 0);
670 ACCOUNT_VERBOSE( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
672 /*Finalize Statement*/
673 rc = sqlite3_finalize(pStmt);
679 static account_stmt _account_prepare_query(char *query)
682 account_stmt pStmt = NULL;
684 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
686 ACCOUNT_INFO( "prepare query : %s", query);
688 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
689 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
694 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
696 assert(NULL != pStmt);
698 return sqlite3_bind_int(pStmt, pos, num);
701 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
703 assert(NULL != pStmt);
706 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
708 return sqlite3_bind_null(pStmt, pos);
711 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
713 ACCOUNT_VERBOSE( "_account_convert_account_to_sql");
716 /*Caution : Keep insert query orders.*/
719 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
721 /* 2. email address*/
722 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
725 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
728 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
731 _account_query_bind_text(hstmt, count++, (char*)account->source);
734 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
737 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
740 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
743 _account_query_bind_int(hstmt, count++, account->auth_type);
746 _account_query_bind_int(hstmt, count++, account->secret);
748 /* 11. sync_support */
749 _account_query_bind_int(hstmt, count++, account->sync_support);
754 for(i=0; i< USER_TXT_CNT; i++)
755 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
757 /* 13. user integer */
758 for(i=0; i< USER_INT_CNT; i++)
759 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
764 static void _account_query_finalize(account_stmt pStmt)
769 ACCOUNT_FATAL( "pStmt is NULL");
773 rc = sqlite3_finalize(pStmt);
774 if (rc != SQLITE_OK) {
775 ACCOUNT_FATAL( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
778 ACCOUNT_VERBOSE( "sqlite3_finalize finish");
781 static int _account_query_step(account_stmt pStmt)
783 assert(NULL != pStmt);
784 return sqlite3_step(pStmt);
789 static int _do_account_owner_existance_check()
791 /* TODO check owner*/
792 return ACCOUNT_ERROR_NONE;
795 static int _account_execute_insert_query(account_s *account)
798 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
799 int error_code = ACCOUNT_ERROR_NONE;
800 account_stmt hstmt = NULL;
802 #ifdef ACCOUNT_VERIFY
804 appid = _account_get_current_appid();
807 //replace appid to account->package_name
808 _ACCOUNT_FREE(account->package_name);
809 account->package_name = _account_get_text(appid);
810 _ACCOUNT_FREE(appid);
812 // API caller cannot be recognized
813 ACCOUNT_ERROR("APP ID not detectable!\n");
814 return ACCOUNT_ERROR_PERMISSION_DENIED;
817 if (!account->package_name) {
818 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
819 return ACCOUNT_ERROR_INVALID_PARAMETER;
823 /* check whether app id exist in account type db */
825 if (!account->user_name && !account->display_name && !account->email_address) {
826 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
827 return ACCOUNT_ERROR_INVALID_PARAMETER;
830 /* Check account own icon existence */
831 if(!account->icon_path) {
832 char* temptxt = NULL;
833 account_type_h account_type = NULL;
834 error_code = account_type_create(&account_type);
835 error_code = account_type_query_by_app_id((const char*)account->package_name, &account_type);
836 error_code = account_type_get_icon_path(account_type, &temptxt);
837 if (error_code != ACCOUNT_ERROR_NONE) {
838 ACCOUNT_ERROR("account_type_get_icon_path: Failed \n");
839 } else if (temptxt && strlen(temptxt)) {
840 ACCOUNT_DEBUG("icon path (%s) app_id(%s) \n", temptxt, account->package_name);
841 account->icon_path = _account_get_text(temptxt);
843 ACCOUNT_DEBUG("account_get_icon_path: returned NULL \n");
845 _ACCOUNT_FREE(temptxt);
846 error_code = account_type_destroy(account_type);
849 /* End of Check account own icon existence */
851 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
852 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , "
853 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
854 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values "
855 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?)", ACCOUNT_TABLE);
857 hstmt = _account_prepare_query(query);
858 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
860 _account_convert_account_to_sql(account, hstmt, query);
862 rc = _account_query_step(hstmt);
863 if (rc != SQLITE_DONE) {
864 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
865 error_code = ACCOUNT_ERROR_DB_FAILED;
868 _account_query_finalize(hstmt);
874 static int _account_insert_capability(account_s *account, int account_id)
877 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
878 account_stmt hstmt = NULL;
880 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
882 if (g_slist_length( account->capablity_list)==0) {
883 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
884 return ACCOUNT_ERROR_NONE;
887 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
889 rc = _account_get_record_count(query);
892 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
893 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
900 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
904 account_capability_s* cap_data = NULL;
905 cap_data = (account_capability_s*)iter->data;
907 ACCOUNT_VERBOSE("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
909 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
910 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
911 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
912 hstmt = _account_prepare_query(query);
914 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
916 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
917 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
918 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
919 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
920 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
921 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
922 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
923 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
924 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
925 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
927 rc = _account_query_step(hstmt);
929 if (rc != SQLITE_DONE) {
930 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
934 _account_query_finalize(hstmt);
939 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
941 return ACCOUNT_ERROR_NONE;
944 static int _account_update_capability(account_s *account, int account_id)
947 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
948 account_stmt hstmt = NULL;
950 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
952 if (g_slist_length( account->capablity_list)==0) {
953 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
954 return ACCOUNT_ERROR_NONE;
957 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
959 rc = _account_get_record_count(query);
962 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
963 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
966 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
968 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
969 hstmt = _account_prepare_query(query);
971 _account_query_bind_int(hstmt, count++, (int)account_id);
972 rc = _account_query_step(hstmt);
974 if (rc != SQLITE_DONE) {
975 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
976 return ACCOUNT_ERROR_DB_FAILED;
978 _account_query_finalize(hstmt);
983 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
986 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
987 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
988 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
990 hstmt = _account_prepare_query(query);
992 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
994 account_capability_s* cap_data = NULL;
995 cap_data = (account_capability_s*)iter->data;
997 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
998 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
999 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1000 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1001 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1002 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1003 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1004 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1005 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1006 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1008 rc = _account_query_step(hstmt);
1010 if (rc != SQLITE_DONE) {
1011 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1015 _account_query_finalize(hstmt);
1020 ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
1022 return ACCOUNT_ERROR_NONE;
1025 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
1028 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1029 account_stmt hstmt = NULL;
1031 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1033 if (g_slist_length( account->capablity_list)==0) {
1034 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
1035 return ACCOUNT_ERROR_NONE;
1038 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
1040 rc = _account_get_record_count(query);
1043 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1044 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1047 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1049 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1050 hstmt = _account_prepare_query(query);
1052 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1053 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1054 rc = _account_query_step(hstmt);
1055 if (rc != SQLITE_DONE) {
1056 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1057 return ACCOUNT_ERROR_DB_FAILED;
1060 _account_query_finalize(hstmt);
1065 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1068 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1069 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1070 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1072 hstmt = _account_prepare_query(query);
1074 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1076 account_capability_s* cap_data = NULL;
1077 cap_data = (account_capability_s*)iter->data;
1079 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1080 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1081 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1082 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1083 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1084 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1085 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1086 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1087 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1088 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1090 rc = _account_query_step(hstmt);
1092 if (rc != SQLITE_DONE) {
1093 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1097 _account_query_finalize(hstmt);
1102 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
1104 return ACCOUNT_ERROR_NONE;
1107 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1109 assert(NULL != pStmt);
1111 return sqlite3_column_int(pStmt, pos);
1114 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
1116 assert(NULL != pStmt);
1118 return (char *)sqlite3_column_text(pStmt, pos);
1121 static void _account_db_data_to_text(const char *textbuf, char **output)
1123 if (textbuf && strlen(textbuf)>0) {
1128 *output = strdup(textbuf);
1132 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1134 char *textbuf = NULL;
1136 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1138 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1139 _account_db_data_to_text(textbuf, &(account_record->user_name));
1141 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1142 _account_db_data_to_text(textbuf, &(account_record->email_address));
1144 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1145 _account_db_data_to_text(textbuf, &(account_record->display_name));
1147 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1148 _account_db_data_to_text(textbuf, &(account_record->icon_path));
1150 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1151 _account_db_data_to_text(textbuf, &(account_record->source));
1153 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1154 _account_db_data_to_text(textbuf, &(account_record->package_name));
1156 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1157 _account_db_data_to_text(textbuf, &(account_record->access_token));
1159 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1160 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1162 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1164 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1166 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1168 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1169 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1171 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1172 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1174 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1175 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1177 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1178 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1180 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1181 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1183 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1184 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1185 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1186 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1187 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1189 ACCOUNT_VERBOSE("END _account_convert_column_to_account");
1192 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1194 char *textbuf = NULL;
1196 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1198 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1199 _account_db_data_to_text(textbuf, &(capability_record->type));
1201 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1203 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1204 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1206 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1207 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1209 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1211 ACCOUNT_VERBOSE("END _account_convert_column_to_capability");
1214 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1216 char *textbuf = NULL;
1218 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1220 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1221 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1223 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1224 _account_db_data_to_text(textbuf, &(custom_record->key));
1226 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1227 _account_db_data_to_text(textbuf, &(custom_record->value));
1229 ACCOUNT_VERBOSE("END _account_convert_column_to_custom");
1232 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1234 account_s *data = (account_s*)user_data;
1236 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1238 if (cap_data == NULL)
1240 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1242 cap_data->type = _account_get_text(capability_type);
1243 cap_data->value = capability_value;
1245 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1247 ACCOUNT_VERBOSE("_account_get_capability_text_cb :: %s\n", capability_type);
1253 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1255 account_s *data = (account_s*)user_data;
1257 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1259 if (custom_data == NULL) {
1260 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1263 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1265 custom_data->account_id = data->id;
1266 custom_data->app_id = _account_get_text(data->package_name);
1267 custom_data->key = _account_get_text(key);
1268 custom_data->value = _account_get_text(value);
1270 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1276 static char *_account_get_text(const char *text_data)
1278 char *text_value = NULL;
1280 if (text_data != NULL) {
1281 text_value = strdup(text_data);
1282 ACCOUNT_VERBOSE("text_value = %s", text_value);
1287 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
1289 int error_code = ACCOUNT_ERROR_NONE;
1290 account_stmt hstmt = NULL;
1291 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1293 account_s *old_account = NULL;
1295 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1296 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1297 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1298 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1300 old_account = (account_s*)calloc(1, sizeof(account_s));
1302 ACCOUNT_FATAL("Memory alloc fail\n");
1303 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1306 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1308 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
1309 hstmt = _account_prepare_query(query);
1311 rc = _account_query_step(hstmt);
1312 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1314 while (rc == SQLITE_ROW) {
1315 _account_convert_column_to_account(hstmt, old_account);
1316 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
1317 rc = _account_query_step(hstmt);
1320 _account_query_finalize(hstmt);
1324 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1325 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1328 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1329 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1332 new_account->id = old_account->id;
1335 if(!new_account->user_name) {
1336 if(old_account->user_name)
1337 new_account->user_name = _account_get_text(old_account->user_name);
1341 if(!new_account->display_name) {
1342 if(old_account->display_name)
1343 new_account->display_name = _account_get_text(old_account->display_name);
1347 if(!new_account->email_address) {
1348 if(old_account->email_address)
1349 new_account->email_address = _account_get_text(old_account->email_address);
1353 if(!new_account->domain_name) {
1354 if(old_account->domain_name)
1355 new_account->domain_name = _account_get_text(old_account->domain_name);
1359 if(!new_account->icon_path) {
1360 if(old_account->icon_path)
1361 new_account->icon_path = _account_get_text(old_account->icon_path);
1365 if(!new_account->source) {
1366 if(old_account->source)
1367 new_account->source = _account_get_text(old_account->source);
1370 #ifndef ACCOUNT_VERIFY
1372 if(!new_account->package_name) {
1373 if(old_account->package_name)
1374 new_account->package_name = _account_get_text(old_account->package_name);
1379 if(!new_account->access_token) {
1380 if(old_account->access_token)
1381 new_account->access_token = _account_get_text(old_account->access_token);
1386 for(i=0;i<USER_TXT_CNT;i++) {
1387 if(!new_account->user_data_txt[i]) {
1388 if(old_account->user_data_txt[i])
1389 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1394 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1395 new_account->auth_type = old_account->auth_type;
1399 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1400 new_account->secret = old_account->secret;
1404 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1405 new_account->sync_support = old_account->sync_support;
1409 for(i=0;i<USER_INT_CNT;i++) {
1410 if(new_account->user_data_int[i] == 0) {
1411 new_account->user_data_int[i] = old_account->user_data_int[i];
1417 // user custom table
1422 if (hstmt != NULL) {
1423 _account_query_finalize(hstmt);
1428 _account_free_account_items(old_account);
1429 _ACCOUNT_FREE(old_account);
1432 return ACCOUNT_ERROR_NONE;
1437 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
1439 int rc = 0, binding_count = 0, count = 0;
1440 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1441 int error_code = ACCOUNT_ERROR_NONE;
1442 account_stmt hstmt = NULL;
1444 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1445 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1447 _account_compare_old_record_by_user_name(account, user_name, package_name);
1449 if (!account->user_name && !account->display_name && !account->email_address) {
1450 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1451 return ACCOUNT_ERROR_INVALID_PARAMETER;
1455 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1456 , ACCOUNT_TABLE, user_name, package_name);
1458 count = _account_get_record_count(query);
1460 ACCOUNT_INFO("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1461 count, user_name, package_name);
1462 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1465 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1466 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1467 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1468 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1469 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1471 hstmt = _account_prepare_query(query);
1472 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1474 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1476 _account_query_bind_text(hstmt, binding_count++, user_name);
1477 _account_query_bind_text(hstmt, binding_count++, package_name);
1478 rc = _account_query_step(hstmt);
1479 if (rc != SQLITE_DONE) {
1480 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1482 _account_query_finalize(hstmt);
1485 /*update capability*/
1486 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1489 error_code = _account_update_custom(account, account->id);
1494 ACCOUNT_API int account_insert_to_db(account_h account, int *account_id)
1496 int error_code = ACCOUNT_ERROR_NONE;
1497 int ret_transaction = 0;
1499 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1500 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1501 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1503 account_s *data = (account_s*)account;
1505 pthread_mutex_lock(&account_mutex);
1507 /* transaction control required*/
1508 ret_transaction = _account_begin_transaction();
1510 if (ret_transaction != ACCOUNT_ERROR_NONE) {
1511 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
1512 pthread_mutex_unlock(&account_mutex);
1513 return ret_transaction;
1516 if (_account_check_duplicated(data)) {
1517 ret_transaction = _account_end_transaction(FALSE);
1518 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
1520 pthread_mutex_unlock(&account_mutex);
1521 return ACCOUNT_ERROR_DUPLICATED;
1523 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1525 if(account_type_query_app_id_exist(data->package_name) != ACCOUNT_ERROR_NONE) {
1526 ret_transaction = _account_end_transaction(FALSE);
1527 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1528 pthread_mutex_unlock(&account_mutex);
1529 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1532 if(!_account_check_add_more_account(data->package_name)) {
1533 ret_transaction = _account_end_transaction(FALSE);
1534 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1535 pthread_mutex_unlock(&account_mutex);
1536 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1539 error_code = _account_execute_insert_query(data);
1541 if (error_code != ACCOUNT_ERROR_NONE) {
1542 ret_transaction = _account_end_transaction(FALSE);
1543 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1545 pthread_mutex_unlock(&account_mutex);
1550 ACCOUNT_VERBOSE( "_account_execute_insert_query, insert error_code : %d", error_code);
1552 error_code = _account_insert_capability(data, *account_id);
1553 if (error_code != ACCOUNT_ERROR_NONE) {
1554 ret_transaction = _account_end_transaction(FALSE);
1555 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1557 pthread_mutex_unlock(&account_mutex);
1561 error_code = _account_insert_custom(data, *account_id);
1562 if (error_code != ACCOUNT_ERROR_NONE) {
1563 ret_transaction = _account_end_transaction(FALSE);
1564 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1566 pthread_mutex_unlock(&account_mutex);
1570 pthread_mutex_unlock(&account_mutex);
1573 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1574 _account_insert_delete_update_notification_send(buf);
1576 _account_end_transaction(TRUE);
1578 return ACCOUNT_ERROR_NONE;
1582 ACCOUNT_API int account_create(account_h *account)
1585 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1586 return ACCOUNT_ERROR_INVALID_PARAMETER;
1589 account_s *data = (account_s*)malloc(sizeof(account_s));
1592 ACCOUNT_FATAL("Memory Allocation Failed");
1593 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1595 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1597 ACCOUNT_VERBOSE("create handle=%p\n", *account);
1599 /*Setting account as visible by default*/
1600 data->secret = ACCOUNT_SECRECY_VISIBLE;
1602 /*Setting account as not supporting sync by default*/
1603 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
1605 *account = (account_h)data;
1607 return ACCOUNT_ERROR_NONE;
1610 ACCOUNT_API int account_set_user_name(account_h account, const char *user_name)
1613 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1614 return ACCOUNT_ERROR_INVALID_PARAMETER;
1618 ACCOUNT_ERROR("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1619 return ACCOUNT_ERROR_INVALID_PARAMETER;
1622 account_s *data = (account_s*)account;
1624 _ACCOUNT_FREE(data->user_name);
1625 data->user_name = _account_get_text(user_name);
1627 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1629 return ACCOUNT_ERROR_NONE;
1632 ACCOUNT_API int account_set_display_name(account_h account, const char *display_name)
1635 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1636 return ACCOUNT_ERROR_INVALID_PARAMETER;
1639 if (!display_name) {
1640 ACCOUNT_ERROR("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1641 return ACCOUNT_ERROR_INVALID_PARAMETER;
1644 account_s *data = (account_s*)account;
1646 _ACCOUNT_FREE(data->display_name);
1647 data->display_name = _account_get_text(display_name);
1649 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1651 return ACCOUNT_ERROR_NONE;
1654 ACCOUNT_API int account_set_email_address(account_h account, const char *email_address)
1657 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1658 return ACCOUNT_ERROR_INVALID_PARAMETER;
1661 if (!email_address) {
1662 ACCOUNT_ERROR("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1663 return ACCOUNT_ERROR_INVALID_PARAMETER;
1666 account_s *data = (account_s*)account;
1668 _ACCOUNT_FREE(data->email_address);
1669 data->email_address = _account_get_text(email_address);
1671 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1673 return ACCOUNT_ERROR_NONE;
1676 ACCOUNT_API int account_set_icon_path(account_h account, const char *icon_path)
1679 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1680 return ACCOUNT_ERROR_INVALID_PARAMETER;
1684 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1685 return ACCOUNT_ERROR_INVALID_PARAMETER;
1688 account_s *data = (account_s*)account;
1690 _ACCOUNT_FREE(data->icon_path);
1691 data->icon_path = _account_get_text(icon_path);
1693 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1695 return ACCOUNT_ERROR_NONE;
1698 ACCOUNT_API int account_set_source(account_h account, const char *source)
1701 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1702 return ACCOUNT_ERROR_INVALID_PARAMETER;
1706 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1707 return ACCOUNT_ERROR_INVALID_PARAMETER;
1709 account_s *data = (account_s*)account;
1711 _ACCOUNT_FREE(data->source);
1712 data->source = _account_get_text(source);
1714 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1716 return ACCOUNT_ERROR_NONE;
1719 ACCOUNT_API int account_set_package_name(account_h account, const char *package_name)
1722 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1723 return ACCOUNT_ERROR_INVALID_PARAMETER;
1726 if (!package_name) {
1727 ACCOUNT_ERROR("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1728 return ACCOUNT_ERROR_INVALID_PARAMETER;
1731 account_s *data = (account_s*)account;
1733 _ACCOUNT_FREE(data->package_name);
1734 data->package_name = _account_get_text(package_name);
1736 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1738 return ACCOUNT_ERROR_NONE;
1741 ACCOUNT_API int account_set_domain_name(account_h account, const char *domain_name)
1744 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1745 return ACCOUNT_ERROR_INVALID_PARAMETER;
1749 ACCOUNT_ERROR("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1750 return ACCOUNT_ERROR_INVALID_PARAMETER;
1752 account_s *data = (account_s*)account;
1754 _ACCOUNT_FREE(data->domain_name);
1755 data->domain_name = _account_get_text(domain_name);
1757 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1759 return ACCOUNT_ERROR_NONE;
1762 ACCOUNT_API int account_set_access_token(account_h account, const char *access_token)
1765 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1766 return ACCOUNT_ERROR_INVALID_PARAMETER;
1769 if (!access_token) {
1770 ACCOUNT_ERROR("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1771 return ACCOUNT_ERROR_INVALID_PARAMETER;
1774 account_s *data = (account_s*)account;
1776 _ACCOUNT_FREE(data->access_token);
1777 data->access_token = _account_get_text(access_token);
1779 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1781 return ACCOUNT_ERROR_NONE;
1784 ACCOUNT_API int account_set_user_text(account_h account, int index, const char *user_txt)
1787 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1788 return ACCOUNT_ERROR_INVALID_PARAMETER;
1792 ACCOUNT_ERROR("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1793 return ACCOUNT_ERROR_INVALID_PARAMETER;
1795 if (index >= USER_TXT_CNT || index < 0) {
1796 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1797 return ACCOUNT_ERROR_INVALID_PARAMETER;
1800 account_s *data = (account_s*)account;
1802 _ACCOUNT_FREE(data->user_data_txt[index]);
1803 data->user_data_txt[index] = _account_get_text(user_txt);
1805 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1807 return ACCOUNT_ERROR_NONE;
1810 ACCOUNT_API int account_set_custom(account_h account, const char* key, const char* value)
1813 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1814 return ACCOUNT_ERROR_INVALID_PARAMETER;
1818 ACCOUNT_ERROR("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__);
1819 return ACCOUNT_ERROR_INVALID_PARAMETER;
1823 ACCOUNT_ERROR("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__);
1824 return ACCOUNT_ERROR_INVALID_PARAMETER;
1827 account_s *data = (account_s*)account;
1830 bool b_is_new = TRUE;
1832 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1834 account_custom_s* custom_data = NULL;
1835 custom_data = (account_custom_s*)iter->data;
1836 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
1838 if(!strcmp(custom_data->key, key)) {
1839 _ACCOUNT_FREE(custom_data->value);
1840 custom_data->value = _account_get_text(value);
1846 account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1848 if (custom_data == NULL) {
1849 ACCOUNT_FATAL("(%s)-(%d) MALLOC FAIL\n", __FUNCTION__, __LINE__);
1850 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1852 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1853 custom_data->account_id = data->id;
1854 custom_data->app_id = _account_get_text(data->package_name);
1855 custom_data->key = _account_get_text(key);
1856 custom_data->value = _account_get_text(value);
1857 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1860 return ACCOUNT_ERROR_NONE;
1863 ACCOUNT_API int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1865 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1867 if ( (auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
1868 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1869 return ACCOUNT_ERROR_INVALID_PARAMETER;
1872 account_s *data = (account_s*)account;
1874 data->auth_type = (int)auth_type;
1876 return ACCOUNT_ERROR_NONE;
1879 ACCOUNT_API int account_set_secret(account_h account, const account_secrecy_state_e secret)
1881 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1883 if ( (secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
1884 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1885 return ACCOUNT_ERROR_INVALID_PARAMETER;
1888 account_s *data = (account_s*)account;
1890 data->secret = (int)secret;
1892 return ACCOUNT_ERROR_NONE;
1895 ACCOUNT_API int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
1897 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1899 if ( (sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) {
1900 ACCOUNT_ERROR("(%s)-(%d) sync_support is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1901 return ACCOUNT_ERROR_INVALID_PARAMETER;
1904 account_s *data = (account_s*)account;
1906 data->sync_support= (int)sync_support;
1908 return ACCOUNT_ERROR_NONE;
1911 ACCOUNT_API int account_set_user_int(account_h account, int index, const int user_int)
1914 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1915 return ACCOUNT_ERROR_INVALID_PARAMETER;
1918 if (index >= USER_INT_CNT ||index < 0) {
1919 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1920 return ACCOUNT_ERROR_INVALID_PARAMETER;
1923 account_s *data = (account_s*)account;
1925 data->user_data_int[index] = user_int;
1927 return ACCOUNT_ERROR_NONE;
1930 ACCOUNT_API int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value)
1932 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null"));
1933 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null"));
1935 if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
1936 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1937 return ACCOUNT_ERROR_INVALID_PARAMETER;
1940 account_s *data = (account_s*)account;
1942 GSList *iter = NULL;
1943 bool b_is_new = TRUE;
1945 for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1946 account_capability_s *cap_data = NULL;
1947 cap_data = (account_capability_s*)iter->data;
1949 if(!strcmp(cap_data->type, capability_type)) {
1950 cap_data->value = capability_value;
1957 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1959 if (cap_data == NULL)
1960 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1961 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1963 cap_data->type = _account_get_text(capability_type);
1964 cap_data->value = capability_value;
1965 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1968 return ACCOUNT_ERROR_NONE;
1971 ACCOUNT_API int account_get_user_name(account_h account, char **user_name)
1974 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1975 return ACCOUNT_ERROR_INVALID_PARAMETER;
1979 ACCOUNT_ERROR("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
1980 return ACCOUNT_ERROR_INVALID_PARAMETER;
1983 account_s *data = (account_s*)account;
1985 (*user_name) = NULL;
1986 *user_name = _account_get_text(data->user_name);
1988 return ACCOUNT_ERROR_NONE;
1991 ACCOUNT_API int account_get_display_name(account_h account, char **display_name)
1994 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1995 return ACCOUNT_ERROR_INVALID_PARAMETER;
1998 if (!display_name) {
1999 ACCOUNT_ERROR("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
2000 return ACCOUNT_ERROR_INVALID_PARAMETER;
2003 account_s *data = (account_s*)account;
2005 (*display_name) = NULL;
2007 *display_name = _account_get_text(data->display_name);
2009 return ACCOUNT_ERROR_NONE;
2012 ACCOUNT_API int account_get_email_address(account_h account,char **email_address)
2015 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2016 return ACCOUNT_ERROR_INVALID_PARAMETER;
2019 if (!email_address) {
2020 ACCOUNT_ERROR("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
2021 return ACCOUNT_ERROR_INVALID_PARAMETER;
2024 account_s *data = (account_s*)account;
2026 (*email_address) = NULL;
2028 *email_address = _account_get_text(data->email_address);
2030 return ACCOUNT_ERROR_NONE;
2033 ACCOUNT_API int account_get_icon_path(account_h account, char **icon_path)
2036 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2037 return ACCOUNT_ERROR_INVALID_PARAMETER;
2041 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
2042 return ACCOUNT_ERROR_INVALID_PARAMETER;
2045 account_s *data = (account_s*)account;
2047 (*icon_path) = NULL;
2049 *icon_path = _account_get_text(data->icon_path);
2051 return ACCOUNT_ERROR_NONE;
2054 ACCOUNT_API int account_get_source(account_h account, char **source)
2057 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2058 return ACCOUNT_ERROR_INVALID_PARAMETER;
2062 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
2063 return ACCOUNT_ERROR_INVALID_PARAMETER;
2066 account_s *data = (account_s*)account;
2070 *source = _account_get_text(data->source);
2072 return ACCOUNT_ERROR_NONE;
2075 ACCOUNT_API int account_get_package_name(account_h account, char **package_name)
2078 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2079 return ACCOUNT_ERROR_INVALID_PARAMETER;
2082 if (!package_name) {
2083 ACCOUNT_ERROR("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
2084 return ACCOUNT_ERROR_INVALID_PARAMETER;
2087 account_s *data = (account_s*)account;
2089 (*package_name) = NULL;
2091 *package_name = _account_get_text(data->package_name);
2093 return ACCOUNT_ERROR_NONE;
2096 ACCOUNT_API int account_get_domain_name(account_h account, char **domain_name)
2099 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2100 return ACCOUNT_ERROR_INVALID_PARAMETER;
2104 ACCOUNT_ERROR("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
2105 return ACCOUNT_ERROR_INVALID_PARAMETER;
2108 account_s *data = (account_s*)account;
2110 (*domain_name) = NULL;
2112 *domain_name = _account_get_text(data->domain_name);
2114 return ACCOUNT_ERROR_NONE;
2117 ACCOUNT_API int account_get_access_token(account_h account, char **access_token)
2120 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2121 return ACCOUNT_ERROR_INVALID_PARAMETER;
2124 if (!access_token) {
2125 ACCOUNT_ERROR("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
2126 return ACCOUNT_ERROR_INVALID_PARAMETER;
2129 account_s *data = (account_s*)account;
2131 (*access_token) = NULL;
2133 *access_token = _account_get_text(data->access_token);
2135 return ACCOUNT_ERROR_NONE;
2138 ACCOUNT_API int account_get_user_text(account_h account, int user_text_index, char **text)
2141 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2142 return ACCOUNT_ERROR_INVALID_PARAMETER;
2146 ACCOUNT_ERROR("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
2147 return ACCOUNT_ERROR_INVALID_PARAMETER;
2149 ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2151 account_s *data = (account_s*)account;
2155 *text = _account_get_text(data->user_data_txt[user_text_index]);
2157 return ACCOUNT_ERROR_NONE;
2160 ACCOUNT_API int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
2163 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2164 return ACCOUNT_ERROR_INVALID_PARAMETER;
2167 ACCOUNT_ERROR("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
2168 return ACCOUNT_ERROR_INVALID_PARAMETER;
2171 account_s* data = (account_s*)account;
2173 *auth_type = data->auth_type;
2175 return ACCOUNT_ERROR_NONE;
2178 ACCOUNT_API int account_get_secret(account_h account, account_secrecy_state_e *secret)
2181 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2182 return ACCOUNT_ERROR_INVALID_PARAMETER;
2185 ACCOUNT_ERROR("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
2186 return ACCOUNT_ERROR_INVALID_PARAMETER;
2189 account_s* data = (account_s*)account;
2191 *secret = data->secret;
2193 return ACCOUNT_ERROR_NONE;
2196 ACCOUNT_API int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
2199 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2200 return ACCOUNT_ERROR_INVALID_PARAMETER;
2202 if (!sync_support) {
2203 ACCOUNT_ERROR("(%s)-(%d) sync_support is NULL.\n", __FUNCTION__, __LINE__);
2204 return ACCOUNT_ERROR_INVALID_PARAMETER;
2207 account_s* data = (account_s*)account;
2209 *sync_support = data->sync_support;
2211 return ACCOUNT_ERROR_NONE;
2214 ACCOUNT_API int account_get_account_id(account_h account, int *account_id)
2217 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2218 return ACCOUNT_ERROR_INVALID_PARAMETER;
2221 ACCOUNT_ERROR("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
2222 return ACCOUNT_ERROR_INVALID_PARAMETER;
2225 account_s *data = (account_s*)account;
2227 *account_id = data->id;
2229 return ACCOUNT_ERROR_NONE;
2232 ACCOUNT_API int account_get_user_int(account_h account, int user_int_index, int *integer)
2235 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2236 return ACCOUNT_ERROR_INVALID_PARAMETER;
2239 ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2242 ACCOUNT_ERROR("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
2243 return ACCOUNT_ERROR_INVALID_PARAMETER;
2246 account_s *data = (account_s*)account;
2248 *integer = data->user_data_int[user_int_index];
2250 return ACCOUNT_ERROR_NONE;
2253 ACCOUNT_API int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value)
2255 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2256 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL"));
2257 ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL"));
2260 account_s *data = (account_s*)account;
2262 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2263 account_capability_s *cap_data = NULL;
2265 cap_data = (account_capability_s*)iter->data;
2267 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2269 if(!strcmp(capability_type, cap_data->type)) {
2270 *capability_value = cap_data->value;
2271 return ACCOUNT_ERROR_NONE;
2275 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2278 ACCOUNT_API int account_get_capability_all(account_h account, capability_cb cb_func, void *user_data)
2280 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2281 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2284 account_s *data = (account_s*)account;
2286 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2287 account_capability_s *cap_data = NULL;
2289 cap_data = (account_capability_s*)iter->data;
2291 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2293 cb_func(cap_data->type, cap_data->value, user_data);
2296 return ACCOUNT_ERROR_NONE;
2299 ACCOUNT_API int account_get_custom(account_h account, const char* key, char** value)
2301 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2302 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST"));
2303 ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL"));
2306 account_s *data = (account_s*)account;
2308 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2309 account_custom_s *custom_data = NULL;
2311 custom_data = (account_custom_s*)iter->data;
2313 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2315 if(!strcmp(key, custom_data->key)) {
2317 *value = _account_get_text(custom_data->value);
2318 return ACCOUNT_ERROR_NONE;
2322 ACCOUNT_INFO("key is not found %s", key);
2324 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2327 ACCOUNT_API int account_get_custom_all(account_h account, account_custom_cb cb_func, void* user_data)
2329 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2330 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2333 account_s *data = (account_s*)account;
2335 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2336 bool cb_ret = FALSE;
2337 account_custom_s *custom_data = NULL;
2339 custom_data = (account_custom_s*)iter->data;
2341 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2343 cb_ret = cb_func(custom_data->key, custom_data->value, user_data);
2345 ACCOUNT_INFO("account_get_custom_all callback func ret = %d", cb_ret);
2350 return ACCOUNT_ERROR_NONE;
2353 ACCOUNT_API int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
2355 int error_code = ACCOUNT_ERROR_NONE;
2356 account_stmt hstmt = NULL;
2357 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2359 int ret = ACCOUNT_ERROR_NONE;
2361 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2362 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2363 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2365 ret = _do_account_owner_existance_check();
2366 if (ret != ACCOUNT_ERROR_NONE) {
2367 ACCOUNT_ERROR("_do_account_owner_existance_check Failed !!!\n");
2370 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2372 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2373 hstmt = _account_prepare_query(query);
2375 rc = _account_query_step(hstmt);
2376 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2378 account_capability_s* capability_record = NULL;
2380 while (rc == SQLITE_ROW) {
2381 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2383 if (capability_record == NULL) {
2384 ACCOUNT_FATAL("malloc Failed");
2388 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2390 _account_convert_column_to_capability(hstmt, capability_record);
2392 cb_func(capability_record->type, capability_record->value, user_data);
2394 _account_free_capability_items(capability_record);
2395 _ACCOUNT_FREE(capability_record);
2397 rc = _account_query_step(hstmt);
2400 _account_query_finalize(hstmt);
2403 error_code = ACCOUNT_ERROR_NONE;
2406 if (hstmt != NULL) {
2407 _account_query_finalize(hstmt);
2411 pthread_mutex_unlock(&account_mutex);
2415 static int _account_compare_old_record(account_s *new_account, int account_id)
2417 int error_code = ACCOUNT_ERROR_NONE;
2418 account_stmt hstmt = NULL;
2419 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2421 account_s *old_account = NULL;
2423 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2424 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2425 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2427 old_account = (account_s*)calloc(1, sizeof(account_s));
2429 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2431 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2432 hstmt = _account_prepare_query(query);
2434 rc = _account_query_step(hstmt);
2435 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2437 while (rc == SQLITE_ROW) {
2438 _account_convert_column_to_account(hstmt, old_account);
2439 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
2440 rc = _account_query_step(hstmt);
2443 _account_query_finalize(hstmt);
2447 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2448 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2451 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2452 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2456 new_account->id = old_account->id;
2459 if(!new_account->user_name) {
2460 if(old_account->user_name)
2461 new_account->user_name = _account_get_text(old_account->user_name);
2465 if(!new_account->display_name) {
2466 if(old_account->display_name)
2467 new_account->display_name = _account_get_text(old_account->display_name);
2471 if(!new_account->email_address) {
2472 if(old_account->email_address)
2473 new_account->email_address = _account_get_text(old_account->email_address);
2477 if(!new_account->domain_name) {
2478 if(old_account->domain_name)
2479 new_account->domain_name = _account_get_text(old_account->domain_name);
2483 if(!new_account->icon_path) {
2484 if(old_account->icon_path)
2485 new_account->icon_path = _account_get_text(old_account->icon_path);
2489 if(!new_account->source) {
2490 if(old_account->source)
2491 new_account->source = _account_get_text(old_account->source);
2494 #ifndef ACCOUNT_VERIFY
2496 if(!new_account->package_name) {
2497 if(old_account->package_name)
2498 new_account->package_name = _account_get_text(old_account->package_name);
2503 if(!new_account->access_token) {
2504 if(old_account->access_token)
2505 new_account->access_token = _account_get_text(old_account->access_token);
2510 for(i=0;i<USER_TXT_CNT;i++) {
2511 if(!new_account->user_data_txt[i]) {
2512 if(old_account->user_data_txt[i])
2513 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2518 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2519 new_account->auth_type = old_account->auth_type;
2523 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2524 new_account->secret = old_account->secret;
2528 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2529 new_account->sync_support = old_account->sync_support;
2533 for(i=0;i<USER_INT_CNT;i++) {
2534 if(new_account->user_data_int[i] == 0) {
2535 new_account->user_data_int[i] = old_account->user_data_int[i];
2541 // user custom table
2546 if (hstmt != NULL) {
2547 _account_query_finalize(hstmt);
2552 _account_free_account_items(old_account);
2553 _ACCOUNT_FREE(old_account);
2556 return ACCOUNT_ERROR_NONE;
2559 static int _account_update_account(account_s *account, int account_id)
2561 int rc = 0, binding_count =0;
2562 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2563 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2564 account_stmt hstmt = NULL;
2566 _account_compare_old_record(account, account_id);
2568 #ifdef ACCOUNT_VERIFY
2570 /* Check permission of requested appid */
2571 if(!_account_verify_permission(account->package_name)) {
2572 ACCOUNT_ERROR("No permission to update\n");
2573 return ACCOUNT_ERROR_PERMISSION_DENIED;
2578 if (!account->package_name) {
2579 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2580 return ACCOUNT_ERROR_INVALID_PARAMETER;
2584 if(account_type_query_app_id_exist(account->package_name) != ACCOUNT_ERROR_NONE) {
2585 ACCOUNT_ERROR("App id is not registered in account type DB!!!!\n");
2586 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
2589 if (!account->user_name && !account->display_name && !account->email_address) {
2590 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2591 return ACCOUNT_ERROR_INVALID_PARAMETER;
2594 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2596 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2598 count = _account_get_record_count(query);
2600 ACCOUNT_WARNING(" Account record not found, count = %d\n", count);
2601 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2604 /* transaction control required*/
2605 ret_transaction = _account_begin_transaction();
2607 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2608 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2609 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2610 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2611 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2613 hstmt = _account_prepare_query(query);
2614 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE)));
2616 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2617 _account_query_bind_int(hstmt, binding_count++, account_id);
2619 rc = _account_query_step(hstmt);
2620 if (rc != SQLITE_DONE) {
2621 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2624 _account_query_finalize(hstmt);
2627 /*update capability*/
2628 error_code = _account_update_capability(account, account_id);
2629 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2630 ret_transaction = _account_end_transaction(FALSE);
2631 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2636 error_code = _account_update_custom(account, account_id);
2637 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2638 ret_transaction = _account_end_transaction(FALSE);
2639 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2643 ret_transaction = _account_end_transaction(TRUE);
2644 ACCOUNT_DEBUG("update capability success, trying to commit(%x) !!!\n", ret_transaction);
2649 ACCOUNT_API int account_update_to_db_by_id(const account_h account, int account_id)
2651 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2652 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2653 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2654 int error_code = ACCOUNT_ERROR_NONE;
2655 account_s* data = (account_s*)account;
2657 pthread_mutex_lock(&account_mutex);
2659 error_code = _account_update_account(data, account_id);
2661 if(error_code != ACCOUNT_ERROR_NONE) {
2662 pthread_mutex_unlock(&account_mutex);
2666 pthread_mutex_unlock(&account_mutex);
2669 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2670 _account_insert_delete_update_notification_send(buf);
2672 return ACCOUNT_ERROR_NONE;
2675 ACCOUNT_API int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
2677 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2678 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2679 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2680 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2682 int error_code = ACCOUNT_ERROR_NONE;
2683 account_s *data = (account_s*)account;
2685 pthread_mutex_lock(&account_mutex);
2687 error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
2689 pthread_mutex_unlock(&account_mutex);
2692 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2693 _account_insert_delete_update_notification_send(buf);
2698 ACCOUNT_API int account_foreach_account_from_db(account_cb callback, void *user_data)
2700 int error_code = ACCOUNT_ERROR_NONE;
2701 account_stmt hstmt = NULL;
2702 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2704 GList *account_list = NULL;
2705 int ret = ACCOUNT_ERROR_NONE;
2707 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
2708 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2710 ret = _do_account_owner_existance_check();
2711 if (ret != ACCOUNT_ERROR_NONE) {
2712 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2715 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2717 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2718 hstmt = _account_prepare_query(query);
2720 rc = _account_query_step(hstmt);
2722 account_s *account_record = NULL;
2724 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2727 while(rc == SQLITE_ROW) {
2728 account_record = (account_s*) malloc(sizeof(account_s));
2730 if (account_record == NULL) {
2731 ACCOUNT_FATAL("malloc Failed");
2735 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2736 _account_convert_column_to_account(hstmt, account_record);
2737 account_list = g_list_append(account_list, account_record);
2738 rc = _account_query_step(hstmt);
2742 _account_query_finalize(hstmt);
2747 for (iter = account_list; iter != NULL; iter = g_list_next(iter)) {
2748 account_s *account = NULL;
2749 account = (account_s*)iter->data;
2750 account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2751 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2752 callback((account_h)account, user_data);
2756 error_code = ACCOUNT_ERROR_NONE;
2759 if (hstmt != NULL) {
2760 _account_query_finalize(hstmt);
2764 _account_glist_free(account_list);
2765 account_list = NULL;
2771 ACCOUNT_API int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status)
2773 int error_code = ACCOUNT_ERROR_NONE;
2774 account_stmt hstmt = NULL;
2775 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2779 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2780 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2781 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2782 ACCOUNT_ERROR("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2783 return ACCOUNT_ERROR_INVALID_PARAMETER;
2786 pthread_mutex_lock(&account_mutex);
2788 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2790 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2792 rc = _account_get_record_count(query);
2795 ACCOUNT_ERROR( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2796 pthread_mutex_unlock(&account_mutex);
2797 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2800 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2802 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2803 hstmt = _account_prepare_query(query);
2805 _account_query_bind_int(hstmt, count, (int)sync_status);
2807 rc = _account_query_step(hstmt);
2809 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2810 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2812 _account_query_finalize(hstmt);
2815 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2816 _account_insert_delete_update_notification_send(buf);
2819 error_code = ACCOUNT_ERROR_NONE;
2822 if (hstmt != NULL) {
2823 _account_query_finalize(hstmt);
2827 pthread_mutex_unlock(&account_mutex);
2831 ACCOUNT_API int account_query_account_by_account_id(int account_db_id, account_h *account)
2833 int error_code = ACCOUNT_ERROR_NONE;
2834 account_stmt hstmt = NULL;
2835 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2837 int ret = ACCOUNT_ERROR_NONE;
2839 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2840 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2841 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2843 ret = _do_account_owner_existance_check();
2844 if (ret != ACCOUNT_ERROR_NONE) {
2845 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2848 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2850 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2851 hstmt = _account_prepare_query(query);
2853 rc = _account_query_step(hstmt);
2854 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2856 account_s *account_record = (account_s *)(*account);
2858 while (rc == SQLITE_ROW) {
2859 _account_convert_column_to_account(hstmt, account_record);
2860 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
2861 rc = _account_query_step(hstmt);
2864 _account_query_finalize(hstmt);
2865 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2866 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2869 error_code = ACCOUNT_ERROR_NONE;
2872 if (hstmt != NULL) {
2873 _account_query_finalize(hstmt);
2877 pthread_mutex_unlock(&account_mutex);
2881 ACCOUNT_API int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
2883 int error_code = ACCOUNT_ERROR_NONE;
2884 account_stmt hstmt = NULL;
2885 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2888 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2889 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2892 int ret = ACCOUNT_ERROR_NONE;
2895 ret = _do_account_owner_existance_check();
2896 if (ret != ACCOUNT_ERROR_NONE) {
2897 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2900 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2902 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2904 hstmt = _account_prepare_query(query);
2906 int binding_count = 1;
2907 _account_query_bind_text(hstmt, binding_count++, user_name);
2909 rc = _account_query_step(hstmt);
2911 account_s *account_head = NULL;
2913 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2917 account_head = (account_s*) malloc(sizeof(account_s));
2918 if (account_head == NULL) {
2919 ACCOUNT_FATAL("malloc Failed");
2920 if (hstmt != NULL) {
2921 _account_query_finalize(hstmt);
2924 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2926 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2928 while (rc == SQLITE_ROW) {
2929 account_s* account_record = NULL;
2931 account_record = (account_s*) malloc(sizeof(account_s));
2933 if (account_record == NULL) {
2934 ACCOUNT_FATAL("malloc Failed");
2937 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2939 _account_convert_column_to_account(hstmt, account_record);
2941 account_head->account_list = g_list_append(account_head->account_list, account_record);
2943 rc = _account_query_step(hstmt);
2945 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2948 _account_query_finalize(hstmt);
2954 tmp = g_list_length(account_head->account_list);
2955 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2957 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2959 account = (account_h)iter->data;
2961 account_s *testaccount = (account_s*)account;
2963 ACCOUNT_VERBOSE("id = %d", testaccount->id);
2964 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
2965 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
2966 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
2967 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
2968 ACCOUNT_VERBOSE("source = %s", testaccount->source);
2969 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
2970 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
2971 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
2972 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
2973 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
2974 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
2976 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2977 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2979 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2981 cb_func(account, user_data);
2985 error_code = ACCOUNT_ERROR_NONE;
2988 if (hstmt != NULL) {
2989 _account_query_finalize(hstmt);
2993 if (account_head->account_list) {
2994 _account_glist_free(account_head->account_list);
2995 account_head->account_list = NULL;
2996 _account_free_account_items(account_head);
2998 _ACCOUNT_FREE(account_head);
3001 pthread_mutex_unlock(&account_mutex);
3005 ACCOUNT_API int account_query_account_by_capability(account_cb cb_func, const char* capability_type, account_capability_state_e capability_value, void* user_data)
3007 int error_code = ACCOUNT_ERROR_NONE;
3008 account_stmt hstmt = NULL;
3009 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3011 int ret = ACCOUNT_ERROR_NONE;
3013 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3015 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
3016 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
3017 return ACCOUNT_ERROR_INVALID_PARAMETER;
3020 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3021 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3023 ret = _do_account_owner_existance_check();
3024 if (ret != ACCOUNT_ERROR_NONE) {
3025 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
3028 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3030 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3032 hstmt = _account_prepare_query(query);
3034 int binding_count = 1;
3035 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3036 _account_query_bind_int(hstmt, binding_count++, (int)capability_value);
3038 rc = _account_query_step(hstmt);
3040 account_s* account_head = NULL;
3042 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3046 account_head = (account_s*) malloc(sizeof(account_s));
3047 if (account_head == NULL) {
3048 ACCOUNT_FATAL("malloc Failed");
3049 if (hstmt != NULL) {
3050 _account_query_finalize(hstmt);
3053 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3055 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3057 while (rc == SQLITE_ROW) {
3058 account_s* account_record = NULL;
3060 account_record = (account_s*) malloc(sizeof(account_s));
3062 if (account_record == NULL) {
3063 ACCOUNT_FATAL("malloc Failed");
3066 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3068 _account_convert_column_to_account(hstmt, account_record);
3070 account_head->account_list = g_list_append(account_head->account_list, account_record);
3072 rc = _account_query_step(hstmt);
3074 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3077 _account_query_finalize(hstmt);
3083 tmp = g_list_length(account_head->account_list);
3084 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3086 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3087 account_h account = NULL;
3088 account = (account_h)iter->data;
3089 account_s* testaccount = (account_s*)account;
3091 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3092 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3094 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3096 cb_func(account, user_data);
3101 error_code = ACCOUNT_ERROR_NONE;
3104 if (hstmt != NULL) {
3105 _account_query_finalize(hstmt);
3109 if (account_head->account_list) {
3110 _account_glist_free(account_head->account_list);
3111 account_head->account_list = NULL;
3112 _account_free_account_items(account_head);
3114 _ACCOUNT_FREE(account_head);
3117 pthread_mutex_unlock(&account_mutex);
3121 ACCOUNT_API int account_query_account_by_capability_type(account_cb cb_func, const char* capability_type, void* user_data)
3123 int error_code = ACCOUNT_ERROR_NONE;
3124 account_stmt hstmt = NULL;
3125 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3127 int ret = ACCOUNT_ERROR_NONE;
3129 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3130 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3131 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3133 ret = _do_account_owner_existance_check();
3134 if (ret != ACCOUNT_ERROR_NONE) {
3135 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
3138 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3140 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3142 hstmt = _account_prepare_query(query);
3144 int binding_count = 1;
3145 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3147 rc = _account_query_step(hstmt);
3149 account_s* account_head = NULL;
3151 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3155 account_head = (account_s*) malloc(sizeof(account_s));
3156 if (account_head == NULL) {
3157 ACCOUNT_FATAL("malloc Failed");
3158 if (hstmt != NULL) {
3159 _account_query_finalize(hstmt);
3162 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3164 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3166 while (rc == SQLITE_ROW) {
3167 account_s* account_record = NULL;
3169 account_record = (account_s*) malloc(sizeof(account_s));
3171 if (account_record == NULL) {
3172 ACCOUNT_FATAL("malloc Failed");
3175 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3177 _account_convert_column_to_account(hstmt, account_record);
3179 account_head->account_list = g_list_append(account_head->account_list, account_record);
3181 rc = _account_query_step(hstmt);
3183 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3186 _account_query_finalize(hstmt);
3192 tmp = g_list_length(account_head->account_list);
3193 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3195 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3196 account_h account = NULL;
3197 account = (account_h)iter->data;
3198 account_s* testaccount = (account_s*)account;
3200 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3201 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3203 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3205 cb_func(account, user_data);
3209 error_code = ACCOUNT_ERROR_NONE;
3212 if (hstmt != NULL) {
3213 _account_query_finalize(hstmt);
3217 if (account_head->account_list) {
3218 _account_glist_free(account_head->account_list);
3219 account_head->account_list = NULL;
3220 _account_free_account_items(account_head);
3222 _ACCOUNT_FREE(account_head);
3225 pthread_mutex_unlock(&account_mutex);
3229 ACCOUNT_API int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
3231 int error_code = ACCOUNT_ERROR_NONE;
3232 account_stmt hstmt = NULL;
3233 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3235 int ret = ACCOUNT_ERROR_NONE;
3237 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
3238 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3239 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3241 ret = _do_account_owner_existance_check();
3242 if (ret != ACCOUNT_ERROR_NONE) {
3243 ACCOUNT_ERROR("_do_account_owner_existance_check Failed !!!\n");
3246 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3248 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3250 hstmt = _account_prepare_query(query);
3252 int binding_count = 1;
3253 _account_query_bind_text(hstmt, binding_count++, package_name);
3255 rc = _account_query_step(hstmt);
3257 account_s* account_head = NULL;
3259 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3263 account_head = (account_s*) malloc(sizeof(account_s));
3264 if (account_head == NULL) {
3265 ACCOUNT_FATAL("malloc Failed");
3266 if (hstmt != NULL) {
3267 _account_query_finalize(hstmt);
3270 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3272 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3274 while (rc == SQLITE_ROW) {
3275 account_s* account_record = NULL;
3277 account_record = (account_s*) malloc(sizeof(account_s));
3279 if (account_record == NULL) {
3280 ACCOUNT_FATAL("malloc Failed");
3283 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3285 _account_convert_column_to_account(hstmt, account_record);
3287 account_head->account_list = g_list_append(account_head->account_list, account_record);
3289 rc = _account_query_step(hstmt);
3291 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3294 _account_query_finalize(hstmt);
3299 tmp = g_list_length(account_head->account_list);
3300 ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
3302 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3303 account_h account = NULL;
3304 account = (account_h)iter->data;
3306 account_s* testaccount = (account_s*)account;
3308 ACCOUNT_VERBOSE("id = %d", testaccount->id);
3309 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
3310 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
3311 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
3312 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
3313 ACCOUNT_VERBOSE("source = %s", testaccount->source);
3314 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
3315 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
3316 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
3317 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
3318 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
3319 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
3321 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3322 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3324 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3326 cb_func(account, user_data);
3330 error_code = ACCOUNT_ERROR_NONE;
3333 if (hstmt != NULL) {
3334 _account_query_finalize(hstmt);
3338 if (account_head->account_list) {
3339 _account_glist_free(account_head->account_list);
3340 account_head->account_list = NULL;
3341 _account_free_account_items(account_head);
3343 _ACCOUNT_FREE(account_head);
3346 pthread_mutex_unlock(&account_mutex);
3350 ACCOUNT_API int account_delete(int account_id)
3352 int error_code = ACCOUNT_ERROR_NONE;
3353 account_stmt hstmt = NULL;
3354 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3356 int ret_transaction = 0;
3357 bool is_success = FALSE;
3359 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3361 #ifdef ACCOUNT_VERIFY
3363 account_h account = NULL;
3364 /* Check requested ID to delete */
3365 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3367 count = _account_get_record_count(query);
3369 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3370 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3373 /* Check permission of requested appid */
3374 account_create(&account);
3375 account_query_account_by_account_id(account_id, &account);
3377 if(!_account_verify_permission(((account_s*)account)->package_name)) {
3378 ACCOUNT_ERROR("No permission to delete\n");
3379 account_destroy(account);
3380 return ACCOUNT_ERROR_PERMISSION_DENIED;
3383 account_destroy(account);
3386 /* transaction control required*/
3387 ret_transaction = _account_begin_transaction();
3389 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3390 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3391 pthread_mutex_unlock(&account_mutex);
3392 return ret_transaction;
3395 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3396 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3398 hstmt = _account_prepare_query(query);
3399 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3400 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3402 rc = _account_query_step(hstmt);
3403 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3405 _account_query_finalize(hstmt);
3408 ACCOUNT_MEMSET(query, 0, sizeof(query));
3410 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3412 hstmt = _account_prepare_query(query);
3413 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3414 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3416 rc = _account_query_step(hstmt);
3417 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3419 _account_query_finalize(hstmt);
3422 /* delete custom data */
3423 ACCOUNT_MEMSET(query, 0, sizeof(query));
3425 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3427 hstmt = _account_prepare_query(query);
3428 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3429 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3431 rc = _account_query_step(hstmt);
3432 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3434 _account_query_finalize(hstmt);
3439 if (hstmt != NULL) {
3440 _account_query_finalize(hstmt);
3444 ret_transaction = _account_end_transaction(is_success);
3446 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3447 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3449 if (is_success == true) {
3451 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3452 _account_insert_delete_update_notification_send(buf);
3456 pthread_mutex_unlock(&account_mutex);
3462 ACCOUNT_API int account_delete_from_db_by_id(int account_db_id)
3464 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
3466 return account_delete(account_db_id);
3469 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3471 int error_code = ACCOUNT_ERROR_NONE;
3472 account_stmt hstmt = NULL;
3473 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3475 int ret = ACCOUNT_ERROR_NONE;
3476 int binding_count = 1;
3478 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3479 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3480 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3481 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3483 ret = _do_account_owner_existance_check();
3484 if (ret != ACCOUNT_ERROR_NONE) {
3485 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
3488 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3490 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3491 hstmt = _account_prepare_query(query);
3493 _account_query_bind_text(hstmt, binding_count++, username);
3494 _account_query_bind_text(hstmt, binding_count++, package_name);
3496 rc = _account_query_step(hstmt);
3497 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3499 account_s *account_record = (account_s *)(*account);
3501 while (rc == SQLITE_ROW) {
3502 _account_convert_column_to_account(hstmt, account_record);
3503 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
3504 rc = _account_query_step(hstmt);
3507 _account_query_finalize(hstmt);
3508 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3509 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3512 error_code = ACCOUNT_ERROR_NONE;
3515 if (hstmt != NULL) {
3516 _account_query_finalize(hstmt);
3520 pthread_mutex_unlock(&account_mutex);
3524 ACCOUNT_API int account_delete_from_db_by_user_name(char *user_name, char *package_name)
3526 int error_code = ACCOUNT_ERROR_NONE;
3527 account_stmt hstmt = NULL;
3528 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3530 int ret_transaction = 0;
3531 bool is_success = FALSE;
3532 account_h account = NULL;
3533 int binding_count = 1;
3534 int account_id = -1;
3536 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3537 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3538 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3540 rc = account_create(&account);
3541 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3543 rc = account_get_account_id(account, &account_id);
3545 rc = account_destroy(account);
3547 /* transaction control required*/
3548 ret_transaction = _account_begin_transaction();
3550 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3551 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3552 pthread_mutex_unlock(&account_mutex);
3553 return ret_transaction;
3556 /* delete custom data */
3557 ACCOUNT_MEMSET(query, 0, sizeof(query));
3558 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3560 hstmt = _account_prepare_query(query);
3562 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3563 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3565 _account_query_bind_int(hstmt, binding_count++, account_id);
3567 rc = _account_query_step(hstmt);
3568 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3570 _account_query_finalize(hstmt);
3573 /* delete capability */
3574 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3576 hstmt = _account_prepare_query(query);
3578 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3579 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3582 _account_query_bind_text(hstmt, binding_count++, user_name);
3583 _account_query_bind_text(hstmt, binding_count++, package_name);
3585 rc = _account_query_step(hstmt);
3586 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3588 _account_query_finalize(hstmt);
3591 ACCOUNT_MEMSET(query, 0, sizeof(query));
3593 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3595 hstmt = _account_prepare_query(query);
3596 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3597 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3600 _account_query_bind_text(hstmt, binding_count++, user_name);
3601 _account_query_bind_text(hstmt, binding_count++, package_name);
3603 rc = _account_query_step(hstmt);
3604 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. user_name=%s, package_name=%s, rc=%d\n", user_name, package_name, rc));
3606 _account_query_finalize(hstmt);
3612 if (hstmt != NULL) {
3613 _account_query_finalize(hstmt);
3617 ret_transaction = _account_end_transaction(is_success);
3619 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3620 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3622 if (is_success == true) {
3624 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3625 _account_insert_delete_update_notification_send(buf);
3629 pthread_mutex_unlock(&account_mutex);
3634 ACCOUNT_API int account_delete_from_db_by_package_name(char *package_name)
3636 int error_code = ACCOUNT_ERROR_NONE;
3637 account_stmt hstmt = NULL;
3638 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3640 int ret_transaction = 0;
3641 bool is_success = FALSE;
3642 int binding_count = 1;
3644 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3645 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3647 /* transaction control required*/
3648 ret_transaction = _account_begin_transaction();
3650 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3651 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3652 pthread_mutex_unlock(&account_mutex);
3653 return ret_transaction;
3656 /* delete custom table */
3657 ACCOUNT_MEMSET(query, 0, sizeof(query));
3658 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3660 hstmt = _account_prepare_query(query);
3662 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3663 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3666 _account_query_bind_text(hstmt, binding_count++, package_name);
3668 rc = _account_query_step(hstmt);
3669 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3671 _account_query_finalize(hstmt);
3674 /* delete capability table */
3675 ACCOUNT_MEMSET(query, 0, sizeof(query));
3676 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3678 hstmt = _account_prepare_query(query);
3680 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3681 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3684 _account_query_bind_text(hstmt, binding_count++, package_name);
3686 rc = _account_query_step(hstmt);
3687 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3689 _account_query_finalize(hstmt);
3692 /* delete account table */
3693 ACCOUNT_MEMSET(query, 0, sizeof(query));
3695 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3697 hstmt = _account_prepare_query(query);
3698 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3699 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3702 _account_query_bind_text(hstmt, binding_count++, package_name);
3704 rc = _account_query_step(hstmt);
3705 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. package_name=%s, rc=%d\n", package_name, rc));
3707 _account_query_finalize(hstmt);
3713 if (hstmt != NULL) {
3714 _account_query_finalize(hstmt);
3718 ret_transaction = _account_end_transaction(is_success);
3720 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3721 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3723 if (is_success == true) {
3725 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, -1);
3726 _account_insert_delete_update_notification_send(buf);
3730 pthread_mutex_unlock(&account_mutex);
3735 ACCOUNT_API int account_get_total_count_from_db(int *count)
3738 ACCOUNT_ERROR("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3739 return ACCOUNT_ERROR_INVALID_PARAMETER;
3741 int ret = ACCOUNT_ERROR_NONE;
3742 char query[1024] = {0, };
3743 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3744 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3746 ret = _do_account_owner_existance_check();
3747 if (ret != ACCOUNT_ERROR_NONE) {
3748 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
3751 *count = _account_get_record_count(query);
3754 account_stmt pStmt = NULL;
3756 assert(NULL != g_hAccountDB);
3757 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3759 rc = sqlite3_step(pStmt);
3760 if (SQLITE_ROW != rc) {
3761 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3762 sqlite3_finalize(pStmt);
3763 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3766 ncount = sqlite3_column_int(pStmt, 0);
3770 ACCOUNT_VERBOSE("Number of account : %d", ncount);
3771 sqlite3_finalize(pStmt);
3774 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3775 return ACCOUNT_ERROR_DB_FAILED;
3778 return ACCOUNT_ERROR_NONE;
3781 ACCOUNT_API int account_destroy(account_h account)
3783 account_s *data = (account_s*)account;
3785 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3787 ACCOUNT_INFO("destroy handle=%p\n", data);
3789 _account_free_account_items(data);
3790 _ACCOUNT_FREE(data);
3792 return ACCOUNT_ERROR_NONE;
3795 static int _account_type_free_label_items(label_s *data)
3797 _ACCOUNT_FREE(data->app_id);
3798 _ACCOUNT_FREE(data->label);
3799 _ACCOUNT_FREE(data->locale);
3801 return ACCOUNT_ERROR_NONE;
3804 static int _account_type_free_feature_items(provider_feature_s *data)
3806 _ACCOUNT_FREE(data->app_id);
3807 _ACCOUNT_FREE(data->key);
3809 return ACCOUNT_ERROR_NONE;
3812 static int _account_type_gslist_free(GSList* list)
3814 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
3818 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
3819 label_s *label_data = (label_s*)iter->data;
3820 _account_type_free_label_items(label_data);
3821 _ACCOUNT_FREE(label_data);
3827 return ACCOUNT_ERROR_NONE;
3830 static int _account_type_item_free(account_type_s *data)
3832 _ACCOUNT_FREE(data->app_id);
3833 _ACCOUNT_FREE(data->service_provider_id);
3834 _ACCOUNT_FREE(data->icon_path);
3835 _ACCOUNT_FREE(data->small_icon_path);
3837 return ACCOUNT_ERROR_NONE;
3840 static int _account_type_glist_free(GList* list)
3842 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
3846 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
3847 account_type_s *account_type_record = (account_type_s*)iter->data;
3848 _account_type_item_free(account_type_record);
3849 _ACCOUNT_FREE(account_type_record);
3855 return ACCOUNT_ERROR_NONE;
3858 static int _account_type_free_account_type_items(account_type_s *data)
3860 _account_type_item_free(data);
3862 _account_type_gslist_free(data->label_list);
3863 _account_type_glist_free(data->account_type_list);
3865 return ACCOUNT_ERROR_NONE;
3868 ACCOUNT_API int account_type_create(account_type_h *account_type)
3870 if (!account_type) {
3871 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3872 return ACCOUNT_ERROR_INVALID_PARAMETER;
3875 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3878 ACCOUNT_ERROR("Memory Allocation Failed");
3879 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3882 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3884 ACCOUNT_VERBOSE("create handle=%p\n", *account_type);
3886 *account_type = (account_type_h)data;
3888 return ACCOUNT_ERROR_NONE;
3891 ACCOUNT_API int account_type_destroy(account_type_h account_type)
3893 account_type_s *data = (account_type_s*)account_type;
3895 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
3897 ACCOUNT_VERBOSE("destroy handle=%p\n", data);
3899 _account_type_free_account_type_items(data);
3900 _ACCOUNT_FREE(data);
3902 return ACCOUNT_ERROR_NONE;
3905 //app_id mandatory field
3906 ACCOUNT_API int account_type_set_app_id(account_type_h account_type, const char *app_id)
3908 if (!account_type) {
3909 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3910 return ACCOUNT_ERROR_INVALID_PARAMETER;
3914 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
3915 return ACCOUNT_ERROR_INVALID_PARAMETER;
3918 account_type_s *data = (account_type_s*)account_type;
3920 _ACCOUNT_FREE(data->app_id);
3921 data->app_id = _account_get_text(app_id);
3923 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->app_id, app_id);
3924 return ACCOUNT_ERROR_NONE;
3927 //service_provider_id mandatory field
3928 ACCOUNT_API int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
3930 if (!account_type) {
3931 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3932 return ACCOUNT_ERROR_INVALID_PARAMETER;
3935 if (!service_provider_id) {
3936 ACCOUNT_ERROR("(%s)-(%d) service_provider_id is NULL.\n", __FUNCTION__, __LINE__);
3937 return ACCOUNT_ERROR_INVALID_PARAMETER;
3940 account_type_s *data = (account_type_s*)account_type;
3942 _ACCOUNT_FREE(data->service_provider_id);
3943 data->service_provider_id = _account_get_text(service_provider_id);
3945 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->service_provider_id, service_provider_id);
3946 return ACCOUNT_ERROR_NONE;
3949 ACCOUNT_API int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
3951 if (!account_type) {
3952 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3953 return ACCOUNT_ERROR_INVALID_PARAMETER;
3957 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
3958 return ACCOUNT_ERROR_INVALID_PARAMETER;
3961 account_type_s *data = (account_type_s*)account_type;
3963 _ACCOUNT_FREE(data->icon_path);
3964 data->icon_path = _account_get_text(icon_path);
3966 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
3967 return ACCOUNT_ERROR_NONE;
3970 ACCOUNT_API int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
3972 if (!account_type) {
3973 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3974 return ACCOUNT_ERROR_INVALID_PARAMETER;
3977 if (!small_icon_path) {
3978 ACCOUNT_ERROR("(%s)-(%d) small_icon_path is NULL.\n", __FUNCTION__, __LINE__);
3979 return ACCOUNT_ERROR_INVALID_PARAMETER;
3982 account_type_s *data = (account_type_s*)account_type;
3984 _ACCOUNT_FREE(data->small_icon_path);
3985 data->small_icon_path = _account_get_text(small_icon_path);
3987 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->small_icon_path, small_icon_path);
3988 return ACCOUNT_ERROR_NONE;
3991 ACCOUNT_API int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
3993 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
3995 account_type_s *data = (account_type_s*)account_type;
3997 data->multiple_account_support = multiple_account_support;
3999 return ACCOUNT_ERROR_NONE;
4003 ACCOUNT_API int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
4005 if (!account_type) {
4006 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4007 return ACCOUNT_ERROR_INVALID_PARAMETER;
4010 if(!label || !locale) {
4011 ACCOUNT_ERROR("(%s)-(%d) label(%p) or locale(%p) is NULL.\n", __FUNCTION__, __LINE__, label, locale);
4012 return ACCOUNT_ERROR_INVALID_PARAMETER;
4015 account_type_s *data = (account_type_s*)account_type;
4016 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4018 if (label_data == NULL) {
4019 ACCOUNT_FATAL("(%s)-(%d) Malloc fail. label_data is NULL.\n", __FUNCTION__, __LINE__);
4020 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4022 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4024 label_data->label = _account_get_text(label);
4025 label_data->locale = _account_get_text(locale);
4027 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4029 return ACCOUNT_ERROR_NONE;
4032 ACCOUNT_API int account_type_get_app_id(account_type_h account_type, char **app_id)
4034 if (!account_type) {
4035 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4036 return ACCOUNT_ERROR_INVALID_PARAMETER;
4040 ACCOUNT_ERROR("(%s)-(%d) app id is NULL.\n", __FUNCTION__, __LINE__);
4041 return ACCOUNT_ERROR_INVALID_PARAMETER;
4044 account_type_s *data = (account_type_s*)account_type;
4047 *app_id = _account_get_text(data->app_id);
4049 return ACCOUNT_ERROR_NONE;
4052 ACCOUNT_API int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4054 if (!account_type) {
4055 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4056 return ACCOUNT_ERROR_INVALID_PARAMETER;
4059 if (!service_provider_id) {
4060 ACCOUNT_ERROR("(%s)-(%d) service provider id is NULL.\n", __FUNCTION__, __LINE__);
4061 return ACCOUNT_ERROR_INVALID_PARAMETER;
4064 account_type_s *data = (account_type_s*)account_type;
4066 (*service_provider_id) = NULL;
4067 *service_provider_id = _account_get_text(data->service_provider_id);
4069 return ACCOUNT_ERROR_NONE;
4072 ACCOUNT_API int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4074 if (!account_type) {
4075 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4076 return ACCOUNT_ERROR_INVALID_PARAMETER;
4080 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4081 return ACCOUNT_ERROR_INVALID_PARAMETER;
4084 account_type_s *data = (account_type_s*)account_type;
4086 (*icon_path) = NULL;
4087 *icon_path = _account_get_text(data->icon_path);
4089 return ACCOUNT_ERROR_NONE;
4092 ACCOUNT_API int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4094 if (!account_type) {
4095 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4096 return ACCOUNT_ERROR_INVALID_PARAMETER;
4099 if (!small_icon_path) {
4100 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4101 return ACCOUNT_ERROR_INVALID_PARAMETER;
4104 account_type_s *data = (account_type_s*)account_type;
4106 (*small_icon_path) = NULL;
4107 *small_icon_path = _account_get_text(data->small_icon_path);
4109 return ACCOUNT_ERROR_NONE;
4112 ACCOUNT_API int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4114 if (!account_type) {
4115 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4116 return ACCOUNT_ERROR_INVALID_PARAMETER;
4118 if (!multiple_account_support) {
4119 ACCOUNT_ERROR("(%s)-(%d) multiple_account_support is NULL.\n", __FUNCTION__, __LINE__);
4120 return ACCOUNT_ERROR_INVALID_PARAMETER;
4123 account_type_s *data = (account_type_s*)account_type;
4125 *multiple_account_support = data->multiple_account_support;
4127 return ACCOUNT_ERROR_NONE;
4130 ACCOUNT_API int account_type_get_label(account_type_h account_type, account_label_cb cb_func, void *user_data)
4132 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4133 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4136 account_type_s *data = (account_type_s*)account_type;
4138 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4139 label_s *label_data = NULL;
4141 label_data = (label_s*)iter->data;
4143 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4145 cb_func(label_data->app_id, label_data->label, label_data->locale, user_data);
4148 return ACCOUNT_ERROR_NONE;
4151 static gboolean _account_type_check_duplicated(account_type_s *data)
4153 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4156 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4158 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4159 , ACCOUNT_TYPE_TABLE, data->app_id);
4161 count = _account_get_record_count(query);
4163 ACCOUNT_VERBOSE("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
4164 count, data->app_id, data->service_provider_id);
4171 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4175 /*Caution : Keep insert query orders.*/
4178 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4180 /* 2. service provider id*/
4181 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4184 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4186 /* 4. small icon path*/
4187 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4189 /* 5. multiple accont support*/
4190 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4196 static int _account_type_execute_insert_query(account_type_s *account_type)
4199 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4200 int error_code = ACCOUNT_ERROR_NONE;
4201 account_stmt hstmt = NULL;
4203 /* check mandatory field */
4204 // app id & service provider id
4205 if (!account_type->app_id) {
4206 ACCOUNT_ERROR("App id is mandetory field, it can not be NULL!!!!\n");
4207 return ACCOUNT_ERROR_INVALID_PARAMETER;
4210 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4211 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4212 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4214 hstmt = _account_prepare_query(query);
4215 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4217 _account_type_convert_account_to_sql(account_type, hstmt, query);
4219 rc = _account_query_step(hstmt);
4220 if (rc != SQLITE_DONE) {
4221 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4222 error_code = ACCOUNT_ERROR_DB_FAILED;
4225 _account_query_finalize(hstmt);
4231 static int _account_type_insert_label(account_type_s *account_type)
4234 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4235 account_stmt hstmt = NULL;
4237 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4239 if (g_slist_length( account_type->label_list)==0) {
4240 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4241 return ACCOUNT_ERROR_NONE;
4244 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4246 rc = _account_get_record_count(query);
4249 ACCOUNT_ERROR( "_account_insert_label : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
4250 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4256 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4259 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4260 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4261 "(?, ?, ?) ", LABEL_TABLE);
4263 hstmt = _account_prepare_query(query);
4265 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4267 label_s* label_data = NULL;
4268 label_data = (label_s*)iter->data;
4269 ACCOUNT_VERBOSE( "label_data->appid = %s, label_data->label = %s, label_data->locale \n", label_data->app_id, label_data->label, label_data->locale);
4271 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4272 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4273 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4274 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4275 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4276 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4278 rc = _account_query_step(hstmt);
4280 if (rc != SQLITE_DONE) {
4281 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4285 _account_query_finalize(hstmt);
4290 ACCOUNT_VERBOSE( "_account_type_insert_label() DONE\n");
4292 return ACCOUNT_ERROR_NONE;
4295 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4297 char *textbuf = NULL;
4299 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4300 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4302 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4303 _account_db_data_to_text(textbuf, &(feature_record->key));
4305 ACCOUNT_VERBOSE("END _account_type_convert_column_to_provider_feature");
4308 ACCOUNT_API int account_type_query_provider_feature_by_app_id(provider_feature_cb cb_func, const char* app_id, void *user_data )
4310 int error_code = ACCOUNT_ERROR_NONE;
4311 account_stmt hstmt = NULL;
4312 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4313 int rc = 0, binding_count = 1;
4315 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4316 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4317 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4319 ACCOUNT_VERBOSE("app_id = %s", app_id);
4321 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4323 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4324 hstmt = _account_prepare_query(query);
4326 _account_query_bind_text(hstmt, binding_count++, app_id);
4328 rc = _account_query_step(hstmt);
4329 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4331 provider_feature_s* feature_record = NULL;
4333 while (rc == SQLITE_ROW) {
4334 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4336 if (feature_record == NULL) {
4337 ACCOUNT_FATAL("malloc Failed");
4341 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4343 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4345 cb_func(feature_record->app_id, feature_record->key, user_data);
4347 _account_type_free_feature_items(feature_record);
4348 _ACCOUNT_FREE(feature_record);
4350 rc = _account_query_step(hstmt);
4353 _account_query_finalize(hstmt);
4356 error_code = ACCOUNT_ERROR_NONE;
4359 if (hstmt != NULL) {
4360 _account_query_finalize(hstmt);
4364 pthread_mutex_unlock(&account_mutex);
4368 ACCOUNT_API bool account_type_query_supported_feature(const char* app_id, const char* capability)
4370 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4371 int record_count = 0;
4373 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4374 ACCOUNT_RETURN_VAL((capability != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CAPABILITY"));
4376 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4378 record_count = _account_get_record_count(query);
4380 if (record_count <= 0) {
4381 ACCOUNT_WARNING( "related capability type item is not existed rc=%d , %s", record_count, _account_db_err_msg());
4390 ACCOUNT_API int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb cb_func, void* user_data)
4392 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4393 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4396 account_type_s *data = (account_type_s*)account_type;
4398 for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4399 provider_feature_s *feature_data = NULL;
4401 feature_data = (provider_feature_s*)iter->data;
4403 ACCOUNT_VERBOSE("appid = %s, key = %s", feature_data->key, feature_data->app_id);
4405 cb_func(feature_data->app_id, feature_data->key, user_data);
4408 return ACCOUNT_ERROR_NONE;
4411 ACCOUNT_API int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4413 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4414 ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4416 account_type_s *data = (account_type_s*)account_type;
4418 GSList *iter = NULL;
4419 bool b_is_new = TRUE;
4421 for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4422 provider_feature_s *feature_data = NULL;
4423 feature_data = (provider_feature_s*)iter->data;
4425 if(!strcmp(feature_data->key, provider_feature)) {
4432 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4434 if (feature_data == NULL)
4435 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4436 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4438 feature_data->key = _account_get_text(provider_feature);
4439 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4442 return ACCOUNT_ERROR_NONE;
4445 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4448 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4449 account_stmt hstmt = NULL;
4451 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4452 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4454 if (g_slist_length( account_type->provider_feature_list)==0) {
4455 ACCOUNT_ERROR( "no capability\n");
4456 return ACCOUNT_ERROR_NONE;
4459 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4461 rc = _account_get_record_count(query);
4464 ACCOUNT_WARNING( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4465 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4472 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4475 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4476 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4477 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4479 hstmt = _account_prepare_query(query);
4481 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4483 provider_feature_s* feature_data = NULL;
4484 feature_data = (provider_feature_s*)iter->data;
4485 ACCOUNT_VERBOSE("key = %s, app_id = %s \n", feature_data->key, app_id);
4487 ret = _account_query_bind_text(hstmt, count++, app_id);
4488 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4489 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4490 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4492 rc = _account_query_step(hstmt);
4494 if (rc != SQLITE_DONE) {
4495 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4499 _account_query_finalize(hstmt);
4504 ACCOUNT_VERBOSE( "_account_type_insert_provider_feature() DONE\n");
4506 return ACCOUNT_ERROR_NONE;
4509 ACCOUNT_API int account_type_insert_to_db(account_type_h account_type, int* account_type_id)
4511 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4513 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4514 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4515 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4517 account_type_s *data = (account_type_s*)account_type;
4519 pthread_mutex_lock(&account_mutex);
4522 /* transaction control required*/
4523 ret_transaction = _account_begin_transaction();
4525 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4526 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4527 pthread_mutex_unlock(&account_mutex);
4528 return ret_transaction;
4531 if (_account_type_check_duplicated(data)) {
4532 ret_transaction = _account_end_transaction(FALSE);
4533 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4534 *account_type_id = -1;
4535 pthread_mutex_unlock(&account_mutex);
4536 return ACCOUNT_ERROR_DUPLICATED;
4538 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4540 error_code = _account_type_execute_insert_query(data);
4542 if (error_code != ACCOUNT_ERROR_NONE){
4543 error_code = ACCOUNT_ERROR_DUPLICATED;
4544 ret_transaction = _account_end_transaction(FALSE);
4545 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4546 *account_type_id = -1;
4547 pthread_mutex_unlock(&account_mutex);
4552 ACCOUNT_INFO( "_account_type_execute_insert_query, insert error_code : %d", error_code);
4554 error_code = _account_type_insert_provider_feature(data, data->app_id);
4555 if(error_code != ACCOUNT_ERROR_NONE) {
4556 ret_transaction = _account_end_transaction(FALSE);
4557 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4558 pthread_mutex_unlock(&account_mutex);
4561 error_code = _account_type_insert_label(data);
4562 if(error_code != ACCOUNT_ERROR_NONE) {
4563 ret_transaction = _account_end_transaction(FALSE);
4564 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4565 pthread_mutex_unlock(&account_mutex);
4569 ret_transaction = _account_end_transaction(TRUE);
4570 ACCOUNT_DEBUG("Insert success(%x), commit insert query(%x)!!!!\n", error_code, ret_transaction);
4572 pthread_mutex_unlock(&account_mutex);
4574 return ACCOUNT_ERROR_NONE;
4577 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4580 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4581 account_stmt hstmt = NULL;
4583 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4585 if (g_slist_length( account_type->provider_feature_list)==0) {
4586 ACCOUNT_ERROR( "no feature\n");
4587 return ACCOUNT_ERROR_NONE;
4590 ACCOUNT_DEBUG( "app id", app_id);
4592 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4594 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4595 hstmt = _account_prepare_query(query);
4597 _account_query_bind_text(hstmt, count++, app_id);
4598 rc = _account_query_step(hstmt);
4600 if (rc != SQLITE_DONE) {
4601 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4602 return ACCOUNT_ERROR_DB_FAILED;
4604 _account_query_finalize(hstmt);
4609 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4612 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4613 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4614 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4616 hstmt = _account_prepare_query(query);
4618 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4620 provider_feature_s* feature_data = NULL;
4621 feature_data = (provider_feature_s*)iter->data;
4623 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4624 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4625 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4626 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4628 rc = _account_query_step(hstmt);
4630 if (rc != SQLITE_DONE) {
4631 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4634 _account_query_finalize(hstmt);
4638 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4640 return ACCOUNT_ERROR_NONE;
4643 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4646 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4647 account_stmt hstmt = NULL;
4649 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4651 if (g_slist_length( account_type->label_list)==0) {
4652 ACCOUNT_ERROR( "_account_type_update_label, no label\n");
4653 return ACCOUNT_ERROR_NONE;
4656 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4658 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4659 hstmt = _account_prepare_query(query);
4661 _account_query_bind_text(hstmt, count++, app_id);
4662 rc = _account_query_step(hstmt);
4664 if (rc != SQLITE_DONE) {
4665 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4666 return ACCOUNT_ERROR_DB_FAILED;
4668 _account_query_finalize(hstmt);
4673 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4676 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4677 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4678 "(?, ?, ?) ", LABEL_TABLE);
4680 hstmt = _account_prepare_query(query);
4682 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4684 label_s* label_data = NULL;
4685 label_data = (label_s*)iter->data;
4687 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4688 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4689 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4690 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4691 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
4692 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4694 rc = _account_query_step(hstmt);
4696 if (rc != SQLITE_DONE) {
4697 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4700 _account_query_finalize(hstmt);
4704 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4706 return ACCOUNT_ERROR_NONE;
4710 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
4712 int rc = 0, binding_count =1;
4713 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4714 int error_code = ACCOUNT_ERROR_NONE;
4715 account_stmt hstmt = NULL;
4717 if (!account_type->app_id) {
4718 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
4719 return ACCOUNT_ERROR_INVALID_PARAMETER;
4722 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4723 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
4724 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
4726 hstmt = _account_prepare_query(query);
4727 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
4729 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
4730 _account_query_bind_text(hstmt, binding_count++, app_id);
4732 rc = _account_query_step(hstmt);
4733 if (rc != SQLITE_DONE) {
4734 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4737 _account_query_finalize(hstmt);
4741 error_code = _account_type_update_label(account_type, app_id);
4742 /* update provider feature */
4743 error_code = _account_type_update_provider_feature(account_type, app_id);
4748 ACCOUNT_API int account_type_update_to_db_by_app_id(account_type_h account_type, const char* app_id)
4750 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
4751 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4752 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4754 int error_code = ACCOUNT_ERROR_NONE;
4755 account_type_s* data = (account_type_s*)account_type;
4757 pthread_mutex_lock(&account_mutex);
4759 error_code = _account_type_update_account(data, app_id);
4761 pthread_mutex_unlock(&account_mutex);
4766 ACCOUNT_API int account_type_delete_by_app_id(const char* app_id)
4768 int error_code = ACCOUNT_ERROR_NONE;
4769 account_stmt hstmt = NULL;
4770 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4771 int rc = 0, count = -1;
4772 int ret_transaction = 0;
4773 int binding_count = 1;
4774 bool is_success = FALSE;
4776 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4777 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
4779 /* Check requested ID to delete */
4780 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
4782 count = _account_get_record_count(query);
4784 ACCOUNT_ERROR("app id(%s) is not exist. count(%d)\n", app_id, count);
4785 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4788 /* transaction control required*/
4789 ret_transaction = _account_begin_transaction();
4791 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4792 ACCOUNT_FATAL("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4793 pthread_mutex_unlock(&account_mutex);
4794 return ret_transaction;
4797 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
4799 hstmt = _account_prepare_query(query);
4800 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4801 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4803 _account_query_bind_text(hstmt, binding_count++, app_id);
4805 rc = _account_query_step(hstmt);
4806 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4808 _account_query_finalize(hstmt);
4812 ACCOUNT_MEMSET(query, 0, sizeof(query));
4814 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
4816 hstmt = _account_prepare_query(query);
4817 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4818 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4820 _account_query_bind_text(hstmt, binding_count++, app_id);
4822 rc = _account_query_step(hstmt);
4823 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4825 _account_query_finalize(hstmt);
4831 ACCOUNT_MEMSET(query, 0, sizeof(query));
4833 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
4835 hstmt = _account_prepare_query(query);
4836 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4837 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4839 _account_query_bind_text(hstmt, binding_count++, app_id);
4841 rc = _account_query_step(hstmt);
4842 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4844 _account_query_finalize(hstmt);
4850 if (hstmt != NULL) {
4851 _account_query_finalize(hstmt);
4855 ret_transaction = _account_end_transaction(is_success);
4857 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4858 ACCOUNT_FATAL("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4861 pthread_mutex_unlock(&account_mutex);
4866 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
4868 char *textbuf = NULL;
4870 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
4872 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
4873 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
4875 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
4876 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
4878 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
4879 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
4881 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
4882 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
4884 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
4886 ACCOUNT_VERBOSE("END _account_type_convert_column_to_account_type");
4889 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
4891 char *textbuf = NULL;
4893 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
4894 _account_db_data_to_text(textbuf, &(label_record->app_id));
4896 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
4897 _account_db_data_to_text(textbuf, &(label_record->label));
4899 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
4900 _account_db_data_to_text(textbuf, &(label_record->locale));
4902 ACCOUNT_VERBOSE("END _account_type_convert_column_to_label");
4905 ACCOUNT_API int account_type_query_label_by_app_id(account_label_cb cb_func, const char* app_id, void *user_data )
4907 int error_code = ACCOUNT_ERROR_NONE;
4908 account_stmt hstmt = NULL;
4909 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4910 int rc = 0, binding_count = 1;
4912 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4913 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4914 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4916 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4918 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
4919 hstmt = _account_prepare_query(query);
4921 _account_query_bind_text(hstmt, binding_count++, app_id);
4923 rc = _account_query_step(hstmt);
4924 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4926 label_s* label_record = NULL;
4928 while (rc == SQLITE_ROW) {
4929 label_record = (label_s*) malloc(sizeof(label_s));
4931 if (label_record == NULL) {
4932 ACCOUNT_FATAL("malloc Failed");
4936 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4938 _account_type_convert_column_to_label(hstmt, label_record);
4940 cb_func(label_record->app_id, label_record->label , label_record->locale, user_data);
4942 _account_type_free_label_items(label_record);
4943 _ACCOUNT_FREE(label_record);
4945 rc = _account_query_step(hstmt);
4948 _account_query_finalize(hstmt);
4951 error_code = ACCOUNT_ERROR_NONE;
4954 if (hstmt != NULL) {
4955 _account_query_finalize(hstmt);
4959 pthread_mutex_unlock(&account_mutex);
4963 int _account_type_label_get_app_id(label_h label, char **app_id)
4966 ACCOUNT_ERROR("(%s)-(%d) label handle is NULL.\n", __FUNCTION__, __LINE__);
4967 return ACCOUNT_ERROR_INVALID_PARAMETER;
4971 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4972 return ACCOUNT_ERROR_INVALID_PARAMETER;
4975 label_s *data = (label_s*)label;
4979 *app_id = _account_get_text(data->app_id);
4981 return ACCOUNT_ERROR_NONE;
4984 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
4986 account_type_s *data = (account_type_s*)user_data;
4988 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4990 if (label_data == NULL) {
4991 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
4994 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4996 label_data->app_id = _account_get_text(app_id);
4997 label_data->label = _account_get_text(label);
4998 label_data->locale = _account_get_text(locale);
5000 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5002 ACCOUNT_VERBOSE("_account_get_label_text_cb :: appid=%s, label=%s\n", label_data->app_id, label_data->label);
5007 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5009 account_type_s *data = (account_type_s*)user_data;
5011 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5013 if (feature_data == NULL) {
5014 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5017 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5019 feature_data->app_id = _account_get_text(app_id);
5020 feature_data->key = _account_get_text(key);
5022 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5024 ACCOUNT_VERBOSE("appid=%s, key=%s\n", feature_data->app_id, feature_data->key);
5029 ACCOUNT_API int account_type_query_by_app_id(const char* app_id, account_type_h *account_type)
5031 int error_code = ACCOUNT_ERROR_NONE;
5032 account_stmt hstmt = NULL;
5033 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5034 int rc = 0, binding_count = 1;
5036 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5037 ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL"));
5038 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5040 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5042 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5043 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5044 hstmt = _account_prepare_query(query);
5046 _account_query_bind_text(hstmt, binding_count++, app_id);
5048 rc = _account_query_step(hstmt);
5049 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5051 account_type_s *account_type_record = (account_type_s *)(*account_type);
5053 while (rc == SQLITE_ROW) {
5054 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5055 ACCOUNT_DEBUG("get account info by id %p\n", account_type_record);
5056 rc = _account_query_step(hstmt);
5059 _account_query_finalize(hstmt);
5060 account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)account_type_record);
5061 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)account_type_record);
5064 error_code = ACCOUNT_ERROR_NONE;
5067 if (hstmt != NULL) {
5068 _account_query_finalize(hstmt);
5072 pthread_mutex_unlock(&account_mutex);
5076 ACCOUNT_API int account_type_query_app_id_exist(const char* app_id)
5078 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5081 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5082 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5084 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5086 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5088 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5089 rc = _account_get_record_count(query);
5092 ACCOUNT_DEBUG("(%s) not exist in account type db");
5093 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5096 return ACCOUNT_ERROR_NONE;
5099 ACCOUNT_API int account_type_query_by_provider_feature(account_type_cb cb_func, const char* key, void* user_data)
5101 int error_code = ACCOUNT_ERROR_NONE;
5102 account_stmt hstmt = NULL;
5103 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5105 GList *account_type_list = NULL;
5107 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
5108 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
5109 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5111 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5113 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5115 hstmt = _account_prepare_query(query);
5117 int binding_count = 1;
5118 _account_query_bind_text(hstmt, binding_count++, (char*)key);
5120 rc = _account_query_step(hstmt);
5122 account_type_s *account_type_record = NULL;
5124 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5127 while(rc == SQLITE_ROW) {
5128 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5130 if (account_type_record == NULL) {
5131 ACCOUNT_FATAL("malloc Failed");
5135 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5136 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5137 account_type_list = g_list_append(account_type_list, account_type_record);
5138 rc = _account_query_step(hstmt);
5142 _account_query_finalize(hstmt);
5147 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5148 account_type_s *account_type = NULL;
5149 account_type = (account_type_s*)iter->data;
5150 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5151 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5152 cb_func((account_type_h)account_type, user_data);
5156 error_code = ACCOUNT_ERROR_NONE;
5159 if (hstmt != NULL) {
5160 _account_query_finalize(hstmt);
5163 if (account_type_list) {
5164 _account_type_glist_free(account_type_list);
5165 account_type_list = NULL;
5172 ACCOUNT_API int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data)
5174 int error_code = ACCOUNT_ERROR_NONE;
5175 account_stmt hstmt = NULL;
5176 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5178 GList *account_type_list = NULL;
5180 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
5181 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5183 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5185 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5186 hstmt = _account_prepare_query(query);
5188 rc = _account_query_step(hstmt);
5190 account_type_s *account_type_record = NULL;
5192 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5195 while(rc == SQLITE_ROW) {
5196 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5198 if (account_type_record == NULL) {
5199 ACCOUNT_FATAL("malloc Failed");
5203 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5204 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5205 account_type_list = g_list_append(account_type_list, account_type_record);
5206 rc = _account_query_step(hstmt);
5210 _account_query_finalize(hstmt);
5215 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5216 account_type_s *account_type = NULL;
5217 account_type = (account_type_s*)iter->data;
5218 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5219 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5220 callback((account_type_h)account_type, user_data);
5224 error_code = ACCOUNT_ERROR_NONE;
5227 if (hstmt != NULL) {
5228 _account_query_finalize(hstmt);
5231 if (account_type_list) {
5232 _account_type_glist_free(account_type_list);
5233 account_type_list = NULL;
5239 // output parameter label must be free
5240 ACCOUNT_API int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label)
5242 int error_code = ACCOUNT_ERROR_NONE;
5243 account_stmt hstmt = NULL;
5244 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5245 int rc = 0, binding_count = 1;
5247 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5248 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5249 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5251 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5253 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, locale);
5254 hstmt = _account_prepare_query(query);
5256 _account_query_bind_text(hstmt, binding_count++, app_id);
5258 rc = _account_query_step(hstmt);
5259 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5261 label_s* label_record = NULL;
5263 while (rc == SQLITE_ROW) {
5264 label_record = (label_s*) malloc(sizeof(label_s));
5266 if (label_record == NULL) {
5267 ACCOUNT_FATAL("malloc Failed");
5271 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5273 _account_type_convert_column_to_label(hstmt,label_record);
5275 *label = _account_get_text(label_record->label);
5277 _account_type_free_label_items(label_record);
5278 _ACCOUNT_FREE(label_record);
5280 rc = _account_query_step(hstmt);
5283 _account_query_finalize(hstmt);
5286 error_code = ACCOUNT_ERROR_NONE;
5289 if (hstmt != NULL) {
5290 _account_query_finalize(hstmt);
5294 pthread_mutex_unlock(&account_mutex);
5298 static int _account_insert_custom(account_s *account, int account_id)
5301 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5302 account_stmt hstmt = NULL;
5304 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5306 if (g_slist_length( account->custom_list)==0) {
5307 ACCOUNT_ERROR( "_account_insert_custom, no custom data\n");
5308 return ACCOUNT_ERROR_NONE;
5311 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5313 rc = _account_get_record_count(query);
5316 ACCOUNT_ERROR( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5317 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5324 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5327 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5328 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5329 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5331 hstmt = _account_prepare_query(query);
5333 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5335 account_custom_s* custom_data = NULL;
5336 custom_data = (account_custom_s*)iter->data;
5337 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
5339 ret = _account_query_bind_int(hstmt, count++, account_id);
5340 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5341 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5342 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5343 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5344 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5345 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5346 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5348 rc = _account_query_step(hstmt);
5350 if (rc != SQLITE_DONE) {
5351 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5355 _account_query_finalize(hstmt);
5360 ACCOUNT_VERBOSE( "_account_insert_custom() DONE\n");
5362 return ACCOUNT_ERROR_NONE;
5365 static int _account_update_custom(account_s *account, int account_id)
5368 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5369 account_stmt hstmt = NULL;
5371 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5373 ACCOUNT_INFO( "account handle=%p, account_id=%d", account, account_id);
5375 if (g_slist_length( account->custom_list)==0) {
5376 ACCOUNT_ERROR( "_account_update_custom, no custom data\n");
5377 return ACCOUNT_ERROR_NONE;
5380 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5382 rc = _account_get_record_count(query);
5385 ACCOUNT_ERROR( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5386 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5389 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5391 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5392 hstmt = _account_prepare_query(query);
5394 _account_query_bind_int(hstmt, count++, (int)account_id);
5395 rc = _account_query_step(hstmt);
5397 if (rc != SQLITE_DONE) {
5398 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5399 return ACCOUNT_ERROR_DB_FAILED;
5401 _account_query_finalize(hstmt);
5406 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5409 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5410 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5411 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5413 hstmt = _account_prepare_query(query);
5415 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5417 account_custom_s* custom_data = NULL;
5418 custom_data = (account_custom_s*)iter->data;
5420 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5421 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5422 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5423 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5424 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5425 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5426 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5427 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5429 rc = _account_query_step(hstmt);
5431 if (rc != SQLITE_DONE) {
5432 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5436 _account_query_finalize(hstmt);
5441 ACCOUNT_VERBOSE( "_account_update_custom() DONE\n");
5443 return ACCOUNT_ERROR_NONE;
5446 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data )
5448 int error_code = ACCOUNT_ERROR_NONE;
5449 account_stmt hstmt = NULL;
5450 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5453 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5454 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5455 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5457 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5459 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5460 hstmt = _account_prepare_query(query);
5462 rc = _account_query_step(hstmt);
5463 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5465 account_custom_s* custom_record = NULL;
5467 while (rc == SQLITE_ROW) {
5468 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5470 if (custom_record == NULL) {
5471 ACCOUNT_FATAL("malloc Failed");
5475 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5477 _account_convert_column_to_custom(hstmt, custom_record);
5479 cb_func(custom_record->key, custom_record->value, user_data);
5481 _account_custom_item_free(custom_record);
5482 _ACCOUNT_FREE(custom_record);
5484 rc = _account_query_step(hstmt);
5487 _account_query_finalize(hstmt);
5490 error_code = ACCOUNT_ERROR_NONE;
5493 if (hstmt != NULL) {
5494 _account_query_finalize(hstmt);
5498 pthread_mutex_unlock(&account_mutex);
5502 static void _account_subscribe_vconf_callback(keynode_t *key, void *user_data)
5504 account_subscribe_s* tmp = (account_subscribe_s*)user_data;
5505 char *msg = NULL, *vconf_key = NULL;
5506 char event_msg[256] ={0, };
5507 int account_id = -1;
5509 ACCOUNT_DEBUG("START\n");
5512 ACCOUNT_ERROR("No subscribtion msg !!!!!\n");
5517 ACCOUNT_ERROR("user data required\n");
5521 if(!memcmp(vconf_keynode_get_name(key), VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR)))
5523 vconf_key = vconf_keynode_get_str(key);
5524 msg = strdup(vconf_key);
5526 char* event_type = NULL;
5530 event_type = strtok_r(msg, ":", &ptr);
5531 id = strtok_r(NULL, ":", &ptr);
5533 ACCOUNT_DEBUG("msg(%s), event_type(%s), id(%s)\n", msg, event_type, id);
5535 ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type);
5537 account_id = atoi(id);
5539 if(tmp->account_subscription_callback)
5540 tmp->account_subscription_callback(event_msg, account_id, tmp->user_data);
5545 ACCOUNT_DEBUG("END\n");
5548 ACCOUNT_API int account_subscribe_create(account_subscribe_h* account_subscribe)
5550 if (!account_subscribe) {
5551 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
5552 return ACCOUNT_ERROR_INVALID_PARAMETER;
5555 account_subscribe_s *data = (account_subscribe_s*)calloc(1,sizeof(account_subscribe_s));
5558 ACCOUNT_FATAL("OUT OF MEMORY\n");
5559 return ACCOUNT_ERROR_OUT_OF_MEMORY;
5562 ACCOUNT_VERBOSE("create handle=%p\n", *data);
5564 *account_subscribe = (account_subscribe_h)data;
5566 return ACCOUNT_ERROR_NONE;
5569 ACCOUNT_API int account_subscribe_notification(account_subscribe_h account_subscribe, account_event_cb cb_func, void* user_data)
5571 ACCOUNT_DEBUG("START\n");
5573 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5575 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5577 tmp->account_subscription_callback = cb_func;
5578 tmp->user_data = user_data;
5580 if (vconf_notify_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5581 (vconf_callback_fn)_account_subscribe_vconf_callback,
5583 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5584 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5587 ACCOUNT_DEBUG("Vconf Subscription Success!!!!\n");
5588 return ACCOUNT_ERROR_NONE;
5591 ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_subscribe)
5593 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5595 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5599 if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5600 (vconf_callback_fn)_account_subscribe_vconf_callback) != 0) {
5601 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5602 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5605 ACCOUNT_DEBUG("Vconf Unsubscription Success!!!!\n");
5606 return ACCOUNT_ERROR_NONE;