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 #include <pkgmgr-info.h>
39 #define EAS_CMDLINE "/usr/bin/eas-engine"
40 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
41 #define ACTIVESYNC_APPID "activesync-ui"
43 static sqlite3* g_hAccountDB = NULL;
44 static int g_refCntDB = 0;
45 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
47 static char *_account_get_text(const char *text_data);
48 static int _account_gslist_free(GSList* list);
49 static int _account_glist_free(GList* list);
50 static char *_account_query_table_column_text(account_stmt pStmt, int pos);
51 static int _account_insert_custom(account_s *account, int account_id);
52 static int _account_update_custom(account_s *account, int account_id);
53 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data );
54 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
56 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
61 GSList** appid_list = (GSList**)user_data;
63 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
64 item = _account_get_text(appid);
65 *appid_list = g_slist_append(*appid_list, item);
70 static inline int __read_proc(const char *path, char *buf, int size)
74 if (buf == NULL || path == NULL) {
75 ACCOUNT_ERROR("path and buffer is mandatory\n");
79 fd = open(path, O_RDONLY);
81 ACCOUNT_ERROR("fd open error(%d)\n", fd);
85 ret = read(fd, buf, size - 1);
87 ACCOUNT_ERROR("fd read error(%d)\n", fd);
98 char *_account_get_proc_cmdline_bypid(int pid)
103 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
104 ret = __read_proc(buf, buf, sizeof(buf));
106 ACCOUNT_INFO("No proc directory (%d)\n", pid);
114 static char* _account_get_current_appid()
117 char appid[128]={0,};
118 pkgmgrinfo_appinfo_h ahandle=NULL;
119 char* appid_ret = NULL;
122 ACCOUNT_INFO("pid (%d)\n", pid, ret);
123 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
124 ACCOUNT_INFO("appid (%s) ret(%d)!!!!!!\n", appid, ret);
126 /* SLP platform core exception */
127 if(strlen(appid) == 0){
128 char* cmdline = NULL;
129 cmdline = _account_get_proc_cmdline_bypid(pid);
130 ACCOUNT_INFO("cmdline (%s)!!!!!!\n", cmdline);
131 if(!strcmp(cmdline, EAS_CMDLINE)) {
132 appid_ret = _account_get_text(ACTIVESYNC_APPID);
135 ACCOUNT_ERROR("No app id\n");
140 appid_ret = _account_get_text(appid);
142 pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
146 static int _account_check_account_type_with_appid_group(const char* appid, char** verified_appid)
148 int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
150 pkgmgrinfo_appinfo_h ahandle=NULL;
151 pkgmgrinfo_pkginfo_h phandle=NULL;
153 GSList* appid_list = NULL;
157 ACCOUNT_INFO("input param is null\n");
158 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
162 ACCOUNT_INFO("output param is null\n");
163 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
166 if(!strcmp(appid, "com.samsung.setting")){
167 ACCOUNT_DEBUG("Setting exception\n");
168 *verified_appid = _account_get_text("com.samsung.setting");
169 return ACCOUNT_ERROR_NONE;
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;
187 if(account_type_query_app_id_exist(tmp) == ACCOUNT_ERROR_NONE) {
188 ACCOUNT_INFO("permission verified appid(%s), current appid(%s)\n", tmp, appid);
189 *verified_appid = _account_get_text(tmp);
190 error_code = ACCOUNT_ERROR_NONE;
194 ACCOUNT_DEBUG("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
200 g_slist_free(appid_list);
201 pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
202 pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
207 static const char *_account_db_err_msg()
209 assert(NULL != g_hAccountDB);
210 return sqlite3_errmsg(g_hAccountDB);
213 static void _account_insert_delete_update_notification_send(char *noti_name)
216 ACCOUNT_ERROR("Noti Name is NULL!!!!!!\n");
220 ACCOUNT_DEBUG("Sending notification with value %s\n", noti_name);
222 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) == 0) {
223 ACCOUNT_VERBOSE("Vconf MSG Str set SUCCESS !!!!!!\n");;
225 ACCOUNT_ERROR("Vconf MSG Str set FAILED !!!!!!\n");;
229 static int _account_get_record_count(char* query)
233 account_stmt pStmt = NULL;
235 assert(NULL != query);
236 assert(NULL != g_hAccountDB);
237 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
239 rc = sqlite3_step(pStmt);
240 if (SQLITE_ROW != rc) {
241 ACCOUNT_ERROR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
242 sqlite3_finalize(pStmt);
243 return ACCOUNT_ERROR_DB_FAILED;
246 ncount = sqlite3_column_int(pStmt, 0);
248 ACCOUNT_VERBOSE("count : %d, End", ncount);
249 sqlite3_finalize(pStmt);
254 static int _account_execute_query(char *query)
257 char* pszErrorMsg = NULL;
259 assert(NULL != query);
260 assert(NULL != g_hAccountDB);
262 ACCOUNT_INFO("query : %s", query);
264 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
265 if (SQLITE_OK != rc) {
266 ACCOUNT_FATAL("sqlite3_exec(%s) failed(%s).", query, pszErrorMsg);
267 sqlite3_free(pszErrorMsg);
273 static int _account_begin_transaction(void)
277 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
279 if (ret != SQLITE_OK) {
280 ACCOUNT_FATAL("_account_svc_begin_transaction fail :: %d", ret);
281 return ACCOUNT_ERROR_DB_FAILED;
284 return ACCOUNT_ERROR_NONE;
287 static int _account_end_transaction(bool is_success)
291 if (is_success == true) {
292 ret = _account_execute_query("COMMIT TRANSACTION");
294 ret = _account_execute_query("ROLLBACK TRANSACTION");
297 if (ret != SQLITE_OK) {
298 ACCOUNT_FATAL("_account_svc_end_transaction fail :: %d", ret);
299 return ACCOUNT_ERROR_DB_FAILED;
302 return ACCOUNT_ERROR_NONE;
305 static bool _account_check_add_more_account(const char* app_id)
307 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
310 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
311 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
313 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
315 ACCOUNT_DEBUG("app id (%s)\n", app_id);
317 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
318 rc = _account_get_record_count(query);
320 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
322 /* multiple account support case */
324 ACCOUNT_DEBUG("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
328 /* multiple account not support case */
329 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
330 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
331 rc = _account_get_record_count(query);
334 ACCOUNT_DEBUG("app id (%s) supports single account. and there is no account of the app id\n", app_id);
341 static int _account_create_all_tables(void)
344 int error_code = ACCOUNT_ERROR_NONE;
345 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
348 ACCOUNT_MEMSET(query, 0, sizeof(query));
350 ACCOUNT_VERBOSE("_account_create_all_tables begin");
352 /*Create the account table*/
353 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
354 rc = _account_get_record_count(query);
356 ACCOUNT_MEMSET(query, 0, sizeof(query));
357 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_SCHEMA, ACCOUNT_TABLE);
358 ACCOUNT_INFO("Create %s table", ACCOUNT_TABLE);
359 rc = _account_execute_query(query);
360 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
363 /*Create capability table*/
364 ACCOUNT_MEMSET(query, 0, sizeof(query));
365 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
366 rc = _account_get_record_count(query);
368 ACCOUNT_MEMSET(query, 0, sizeof(query));
369 ACCOUNT_SNPRINTF(query, sizeof(query), CAPABILITY_SCHEMA, CAPABILITY_TABLE);
370 ACCOUNT_INFO("Create %s table", CAPABILITY_TABLE);
371 rc = _account_execute_query(query);
372 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
375 /* Create account custom table */
376 ACCOUNT_MEMSET(query, 0, sizeof(query));
377 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
378 rc = _account_get_record_count(query);
380 ACCOUNT_MEMSET(query, 0, sizeof(query));
381 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_CUSTOM_SCHEMA, ACCOUNT_CUSTOM_TABLE);
382 ACCOUNT_INFO("Create %s table", ACCOUNT_CUSTOM_TABLE);
383 rc = _account_execute_query(query);
384 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
387 /* Create account type table */
388 ACCOUNT_MEMSET(query, 0, sizeof(query));
389 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
390 rc = _account_get_record_count(query);
392 ACCOUNT_MEMSET(query, 0, sizeof(query));
393 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_TYPE_SCHEMA, ACCOUNT_TYPE_TABLE);
394 ACCOUNT_INFO("Create %s table", ACCOUNT_TYPE_TABLE);
395 rc = _account_execute_query(query);
396 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
399 /* Create label table */
400 ACCOUNT_MEMSET(query, 0, sizeof(query));
401 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
402 rc = _account_get_record_count(query);
404 ACCOUNT_MEMSET(query, 0, sizeof(query));
405 ACCOUNT_SNPRINTF(query, sizeof(query), LABEL_SCHEMA, LABEL_TABLE);
406 ACCOUNT_INFO("Create %s table", LABEL_TABLE);
407 rc = _account_execute_query(query);
408 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
411 /* Create account feature table */
412 ACCOUNT_MEMSET(query, 0, sizeof(query));
413 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
414 rc = _account_get_record_count(query);
416 ACCOUNT_MEMSET(query, 0, sizeof(query));
417 ACCOUNT_SNPRINTF(query, sizeof(query), PROVIDER_FEATURE_SCHEMA, PROVIDER_FEATURE_TABLE);
418 ACCOUNT_INFO("Create %s table", PROVIDER_FEATURE_TABLE);
419 rc = _account_execute_query(query);
420 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
426 static bool _account_check_is_all_table_exists()
429 char query[ACCOUNT_SQL_LEN_MAX] = {0,};
430 ACCOUNT_MEMSET(query, 0, sizeof(query));
432 ACCOUNT_VERBOSE("_account_check_is_all_table_exists");
434 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
435 ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
436 rc = _account_get_record_count(query);
438 ACCOUNT_DEBUG("Table count = %d\n", rc);
440 if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
441 ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
445 ACCOUNT_VERBOSE("END of _account_check_is_all_table_exists\n");
450 static int _account_db_open(void)
453 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
455 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
458 rc = db_util_open(ACCOUNT_DB_NAME, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
459 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
462 ACCOUNT_INFO("_account_db_open: The database connected. refcnt=%d ", g_refCntDB);
465 ACCOUNT_INFO("The database already connected. refcnt=%d ", g_refCntDB);
468 return ACCOUNT_ERROR_NONE;
471 static int _account_db_close(void)
477 if (g_refCntDB > 0) {
479 ACCOUNT_INFO("_account_svc_db_close: The database disconnected. refcnt=%d ", g_refCntDB);
481 if (g_refCntDB == 0) {
482 rc = db_util_close(g_hAccountDB);
483 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
485 ACCOUNT_INFO( "_account_svc_db_close: The database disconnected really. ");
487 ret = ACCOUNT_ERROR_NONE;
489 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
490 ret = ACCOUNT_ERROR_DB_FAILED;
496 static int _account_connect(void)
498 int error_code = ACCOUNT_ERROR_NONE;
500 pthread_mutex_lock(&account_mutex);
502 ACCOUNT_VERBOSE("db path = %s\n", ACCOUNT_DB_NAME);
504 error_code = _account_db_open();
505 if (ACCOUNT_ERROR_NONE != error_code) {
506 ACCOUNT_ERROR("The database isn't connected.\n");
507 pthread_mutex_unlock(&account_mutex);
508 return ACCOUNT_ERROR_DB_NOT_OPENED;
511 if (FALSE == _account_check_is_all_table_exists())
512 error_code = _account_create_all_tables();
514 pthread_mutex_unlock(&account_mutex);
515 return ACCOUNT_ERROR_NONE;
518 ACCOUNT_API int account_connect (void)
520 return _account_connect();
523 static int _account_disconnect(void)
525 int error_code = ACCOUNT_ERROR_NONE;
527 pthread_mutex_lock(&account_mutex);
528 ACCOUNT_INFO("db path = %s have been closed!!!\n", ACCOUNT_DB_NAME);
530 error_code = _account_db_close();
531 pthread_mutex_unlock(&account_mutex);
536 ACCOUNT_API int account_disconnect (void)
538 return _account_disconnect();
541 static int _account_free_capability_items(account_capability_s *data)
543 _ACCOUNT_FREE(data->type);
544 _ACCOUNT_FREE(data->package_name);
545 _ACCOUNT_FREE(data->user_name);
547 return ACCOUNT_ERROR_NONE;
550 static int _account_custom_item_free(account_custom_s *data)
552 _ACCOUNT_FREE(data->app_id);
553 _ACCOUNT_FREE(data->key);
554 _ACCOUNT_FREE(data->value);
556 return ACCOUNT_ERROR_NONE;
559 static int _account_custom_gslist_free(GSList* list)
561 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
565 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
566 account_custom_s *custom_data = (account_custom_s*)iter->data;
567 _account_custom_item_free(custom_data);
568 _ACCOUNT_FREE(custom_data);
574 return ACCOUNT_ERROR_NONE;
577 static int _account_free_account_items(account_s *data)
579 _ACCOUNT_FREE(data->user_name);
580 _ACCOUNT_FREE(data->email_address);
581 _ACCOUNT_FREE(data->display_name);
582 _ACCOUNT_FREE(data->icon_path);
583 _ACCOUNT_FREE(data->source);
584 _ACCOUNT_FREE(data->package_name);
585 _ACCOUNT_FREE(data->domain_name);
586 _ACCOUNT_FREE(data->access_token);
589 for(i=0;i<USER_TXT_CNT;i++)
590 _ACCOUNT_FREE(data->user_data_txt[i]);
592 _account_gslist_free(data->capablity_list);
593 _account_glist_free(data->account_list);
594 _account_custom_gslist_free(data->custom_list);
596 return ACCOUNT_ERROR_NONE;
599 static int _account_gslist_free(GSList* list)
601 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
605 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
606 account_capability_s *cap_data = (account_capability_s*)iter->data;
607 _account_free_capability_items(cap_data);
608 _ACCOUNT_FREE(cap_data);
614 return ACCOUNT_ERROR_NONE;
617 static int _account_glist_free(GList* list)
619 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
623 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
624 account_s *account_record = (account_s*)iter->data;
625 _account_free_account_items(account_record);
626 _ACCOUNT_FREE(account_record);
632 return ACCOUNT_ERROR_NONE;
635 static gboolean _account_check_duplicated(account_s *data)
638 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
641 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
643 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and package_name='%s'"
644 , ACCOUNT_TABLE, data->user_name, data->package_name);
646 count = _account_get_record_count(query);
648 ACCOUNT_INFO("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
649 count, data->user_name, data->domain_name );
657 static int _account_get_next_sequence(char *pszName)
660 account_stmt pStmt = NULL;
662 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
664 ACCOUNT_VERBOSE( "[Enter] pszName:%s\n", pszName);
666 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
667 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
668 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
670 rc = sqlite3_step(pStmt);
671 max_seq = sqlite3_column_int(pStmt, 0);
674 ACCOUNT_VERBOSE( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
676 /*Finalize Statement*/
677 rc = sqlite3_finalize(pStmt);
683 static account_stmt _account_prepare_query(char *query)
686 account_stmt pStmt = NULL;
688 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
690 ACCOUNT_INFO( "prepare query : %s", query);
692 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
693 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
698 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
700 assert(NULL != pStmt);
702 return sqlite3_bind_int(pStmt, pos, num);
705 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
707 assert(NULL != pStmt);
710 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
712 return sqlite3_bind_null(pStmt, pos);
715 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
717 ACCOUNT_VERBOSE( "_account_convert_account_to_sql");
720 /*Caution : Keep insert query orders.*/
723 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
725 /* 2. email address*/
726 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
729 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
732 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
735 _account_query_bind_text(hstmt, count++, (char*)account->source);
738 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
741 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
744 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
747 _account_query_bind_int(hstmt, count++, account->auth_type);
750 _account_query_bind_int(hstmt, count++, account->secret);
752 /* 11. sync_support */
753 _account_query_bind_int(hstmt, count++, account->sync_support);
758 for(i=0; i< USER_TXT_CNT; i++)
759 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
761 /* 13. user integer */
762 for(i=0; i< USER_INT_CNT; i++)
763 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
768 static void _account_query_finalize(account_stmt pStmt)
773 ACCOUNT_FATAL( "pStmt is NULL");
777 rc = sqlite3_finalize(pStmt);
778 if (rc != SQLITE_OK) {
779 ACCOUNT_FATAL( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
782 ACCOUNT_VERBOSE( "sqlite3_finalize finish");
785 static int _account_query_step(account_stmt pStmt)
787 assert(NULL != pStmt);
788 return sqlite3_step(pStmt);
791 static int _account_execute_insert_query(account_s *account)
794 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
795 int error_code = ACCOUNT_ERROR_NONE;
796 account_stmt hstmt = NULL;
798 /* check whether app id exist in account type db */
800 if (!account->user_name && !account->display_name && !account->email_address) {
801 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
802 return ACCOUNT_ERROR_INVALID_PARAMETER;
805 /* Check account own icon existence */
806 if(!account->icon_path) {
807 char* temptxt = NULL;
808 account_type_h account_type = NULL;
809 error_code = account_type_create(&account_type);
810 error_code = account_type_query_by_app_id((const char*)account->package_name, &account_type);
811 error_code = account_type_get_icon_path(account_type, &temptxt);
812 if (error_code != ACCOUNT_ERROR_NONE) {
813 ACCOUNT_ERROR("account_type_get_icon_path: Failed \n");
814 } else if (temptxt && strlen(temptxt)) {
815 ACCOUNT_DEBUG("icon path (%s) app_id(%s) \n", temptxt, account->package_name);
816 account->icon_path = _account_get_text(temptxt);
818 ACCOUNT_DEBUG("account_get_icon_path: returned NULL \n");
820 _ACCOUNT_FREE(temptxt);
821 error_code = account_type_destroy(account_type);
824 /* End of Check account own icon existence */
826 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
827 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , "
828 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
829 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values "
830 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?)", ACCOUNT_TABLE);
832 hstmt = _account_prepare_query(query);
833 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
835 _account_convert_account_to_sql(account, hstmt, query);
837 rc = _account_query_step(hstmt);
838 if (rc != SQLITE_DONE) {
839 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
840 error_code = ACCOUNT_ERROR_DB_FAILED;
843 _account_query_finalize(hstmt);
849 static int _account_insert_capability(account_s *account, int account_id)
852 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
853 account_stmt hstmt = NULL;
855 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
857 if (g_slist_length( account->capablity_list)==0) {
858 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
859 return ACCOUNT_ERROR_NONE;
862 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
864 rc = _account_get_record_count(query);
867 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
868 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
875 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
879 account_capability_s* cap_data = NULL;
880 cap_data = (account_capability_s*)iter->data;
882 ACCOUNT_VERBOSE("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
884 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
885 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
886 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
887 hstmt = _account_prepare_query(query);
889 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
891 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
892 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
893 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
894 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
895 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
896 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
897 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
898 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
899 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
900 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
902 rc = _account_query_step(hstmt);
904 if (rc != SQLITE_DONE) {
905 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
909 _account_query_finalize(hstmt);
914 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
916 return ACCOUNT_ERROR_NONE;
919 static int _account_update_capability(account_s *account, int account_id)
922 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
923 account_stmt hstmt = NULL;
925 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
927 if (g_slist_length( account->capablity_list)==0) {
928 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
929 return ACCOUNT_ERROR_NONE;
932 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
934 rc = _account_get_record_count(query);
937 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
938 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
941 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
943 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
944 hstmt = _account_prepare_query(query);
946 _account_query_bind_int(hstmt, count++, (int)account_id);
947 rc = _account_query_step(hstmt);
949 if (rc != SQLITE_DONE) {
950 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
951 return ACCOUNT_ERROR_DB_FAILED;
953 _account_query_finalize(hstmt);
958 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
961 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
962 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
963 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
965 hstmt = _account_prepare_query(query);
967 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
969 account_capability_s* cap_data = NULL;
970 cap_data = (account_capability_s*)iter->data;
972 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
973 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
974 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
975 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
976 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
977 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
978 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
979 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
980 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
981 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
983 rc = _account_query_step(hstmt);
985 if (rc != SQLITE_DONE) {
986 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
990 _account_query_finalize(hstmt);
995 ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
997 return ACCOUNT_ERROR_NONE;
1000 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
1003 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1004 account_stmt hstmt = NULL;
1006 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1008 if (g_slist_length( account->capablity_list)==0) {
1009 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
1010 return ACCOUNT_ERROR_NONE;
1013 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
1015 rc = _account_get_record_count(query);
1018 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1019 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1022 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1024 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1025 hstmt = _account_prepare_query(query);
1027 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1028 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1029 rc = _account_query_step(hstmt);
1030 if (rc != SQLITE_DONE) {
1031 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1032 return ACCOUNT_ERROR_DB_FAILED;
1035 _account_query_finalize(hstmt);
1040 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1043 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1044 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1045 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1047 hstmt = _account_prepare_query(query);
1049 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1051 account_capability_s* cap_data = NULL;
1052 cap_data = (account_capability_s*)iter->data;
1054 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1055 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1056 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1057 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1058 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1059 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1060 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1061 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1062 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1063 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1065 rc = _account_query_step(hstmt);
1067 if (rc != SQLITE_DONE) {
1068 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1072 _account_query_finalize(hstmt);
1077 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
1079 return ACCOUNT_ERROR_NONE;
1082 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1084 assert(NULL != pStmt);
1086 return sqlite3_column_int(pStmt, pos);
1089 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
1091 assert(NULL != pStmt);
1093 return (char *)sqlite3_column_text(pStmt, pos);
1096 static void _account_db_data_to_text(const char *textbuf, char **output)
1098 if (textbuf && strlen(textbuf)>0) {
1103 *output = strdup(textbuf);
1107 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1109 char *textbuf = NULL;
1111 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1113 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1114 _account_db_data_to_text(textbuf, &(account_record->user_name));
1116 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1117 _account_db_data_to_text(textbuf, &(account_record->email_address));
1119 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1120 _account_db_data_to_text(textbuf, &(account_record->display_name));
1122 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1123 _account_db_data_to_text(textbuf, &(account_record->icon_path));
1125 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1126 _account_db_data_to_text(textbuf, &(account_record->source));
1128 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1129 _account_db_data_to_text(textbuf, &(account_record->package_name));
1131 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1132 _account_db_data_to_text(textbuf, &(account_record->access_token));
1134 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1135 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1137 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1139 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1141 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1143 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1144 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1146 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1147 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1149 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1150 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1152 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1153 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1155 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1156 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1158 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1159 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1160 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1161 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1162 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1164 ACCOUNT_VERBOSE("END _account_convert_column_to_account");
1167 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1169 char *textbuf = NULL;
1171 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1173 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1174 _account_db_data_to_text(textbuf, &(capability_record->type));
1176 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1178 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1179 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1181 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1182 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1184 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1186 ACCOUNT_VERBOSE("END _account_convert_column_to_capability");
1189 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1191 char *textbuf = NULL;
1193 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1195 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1196 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1198 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1199 _account_db_data_to_text(textbuf, &(custom_record->key));
1201 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1202 _account_db_data_to_text(textbuf, &(custom_record->value));
1204 ACCOUNT_VERBOSE("END _account_convert_column_to_custom");
1207 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1209 account_s *data = (account_s*)user_data;
1211 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1213 if (cap_data == NULL)
1215 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1217 cap_data->type = _account_get_text(capability_type);
1218 cap_data->value = capability_value;
1220 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1222 ACCOUNT_VERBOSE("_account_get_capability_text_cb :: %s\n", capability_type);
1228 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1230 account_s *data = (account_s*)user_data;
1232 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1234 if (custom_data == NULL) {
1235 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1238 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1240 custom_data->account_id = data->id;
1241 custom_data->app_id = _account_get_text(data->package_name);
1242 custom_data->key = _account_get_text(key);
1243 custom_data->value = _account_get_text(value);
1245 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1251 static char *_account_get_text(const char *text_data)
1253 char *text_value = NULL;
1255 if (text_data != NULL) {
1256 text_value = strdup(text_data);
1257 ACCOUNT_VERBOSE("text_value = %s", text_value);
1262 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
1264 int error_code = ACCOUNT_ERROR_NONE;
1265 account_stmt hstmt = NULL;
1266 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1268 account_s *old_account = NULL;
1270 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1271 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1272 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1273 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1275 old_account = (account_s*)calloc(1, sizeof(account_s));
1277 ACCOUNT_FATAL("Memory alloc fail\n");
1278 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1281 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1283 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
1284 hstmt = _account_prepare_query(query);
1286 rc = _account_query_step(hstmt);
1287 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1289 while (rc == SQLITE_ROW) {
1290 _account_convert_column_to_account(hstmt, old_account);
1291 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
1292 rc = _account_query_step(hstmt);
1295 _account_query_finalize(hstmt);
1299 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1300 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1303 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1304 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1307 new_account->id = old_account->id;
1310 if(!new_account->user_name) {
1311 if(old_account->user_name)
1312 new_account->user_name = _account_get_text(old_account->user_name);
1316 if(!new_account->display_name) {
1317 if(old_account->display_name)
1318 new_account->display_name = _account_get_text(old_account->display_name);
1322 if(!new_account->email_address) {
1323 if(old_account->email_address)
1324 new_account->email_address = _account_get_text(old_account->email_address);
1328 if(!new_account->domain_name) {
1329 if(old_account->domain_name)
1330 new_account->domain_name = _account_get_text(old_account->domain_name);
1334 if(!new_account->icon_path) {
1335 if(old_account->icon_path)
1336 new_account->icon_path = _account_get_text(old_account->icon_path);
1340 if(!new_account->source) {
1341 if(old_account->source)
1342 new_account->source = _account_get_text(old_account->source);
1345 _ACCOUNT_FREE(new_account->package_name);
1346 new_account->package_name = _account_get_text(old_account->package_name);
1349 if(!new_account->access_token) {
1350 if(old_account->access_token)
1351 new_account->access_token = _account_get_text(old_account->access_token);
1356 for(i=0;i<USER_TXT_CNT;i++) {
1357 if(!new_account->user_data_txt[i]) {
1358 if(old_account->user_data_txt[i])
1359 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1364 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1365 new_account->auth_type = old_account->auth_type;
1369 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1370 new_account->secret = old_account->secret;
1374 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1375 new_account->sync_support = old_account->sync_support;
1379 for(i=0;i<USER_INT_CNT;i++) {
1380 if(new_account->user_data_int[i] == 0) {
1381 new_account->user_data_int[i] = old_account->user_data_int[i];
1387 // user custom table
1392 if (hstmt != NULL) {
1393 _account_query_finalize(hstmt);
1398 _account_free_account_items(old_account);
1399 _ACCOUNT_FREE(old_account);
1402 return ACCOUNT_ERROR_NONE;
1407 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
1409 int rc = 0, binding_count = 0, count = 0;
1410 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1411 int error_code = ACCOUNT_ERROR_NONE;
1412 account_stmt hstmt = NULL;
1414 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1415 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1417 char* current_appid = NULL;
1418 char* verified_appid = NULL;
1420 current_appid = _account_get_current_appid();
1421 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
1423 _ACCOUNT_FREE(current_appid);
1424 _ACCOUNT_FREE(verified_appid);
1426 if(error_code != ACCOUNT_ERROR_NONE){
1427 ACCOUNT_ERROR("No permission to update\n");
1428 return ACCOUNT_ERROR_PERMISSION_DENIED;
1431 _account_compare_old_record_by_user_name(account, user_name, package_name);
1433 if (!account->package_name) {
1434 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1435 return ACCOUNT_ERROR_INVALID_PARAMETER;
1438 if (!account->user_name && !account->display_name && !account->email_address) {
1439 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1440 return ACCOUNT_ERROR_INVALID_PARAMETER;
1443 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1444 , ACCOUNT_TABLE, user_name, package_name);
1446 count = _account_get_record_count(query);
1448 ACCOUNT_INFO("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1449 count, user_name, package_name);
1450 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1453 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1454 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1455 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1456 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1457 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1459 hstmt = _account_prepare_query(query);
1460 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1462 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1464 _account_query_bind_text(hstmt, binding_count++, user_name);
1465 _account_query_bind_text(hstmt, binding_count++, package_name);
1466 rc = _account_query_step(hstmt);
1467 if (rc != SQLITE_DONE) {
1468 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1470 _account_query_finalize(hstmt);
1473 /*update capability*/
1474 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1477 error_code = _account_update_custom(account, account->id);
1482 ACCOUNT_API int account_insert_to_db(account_h account, int *account_id)
1484 int error_code = ACCOUNT_ERROR_NONE;
1485 int ret_transaction = 0;
1487 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1488 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1489 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1491 account_s *data = (account_s*)account;
1493 pthread_mutex_lock(&account_mutex);
1495 /* transaction control required*/
1496 ret_transaction = _account_begin_transaction();
1498 if (ret_transaction != ACCOUNT_ERROR_NONE) {
1499 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
1500 pthread_mutex_unlock(&account_mutex);
1501 return ret_transaction;
1504 if (_account_check_duplicated(data)) {
1505 ret_transaction = _account_end_transaction(FALSE);
1506 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
1508 pthread_mutex_unlock(&account_mutex);
1509 return ACCOUNT_ERROR_DUPLICATED;
1511 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1514 appid = _account_get_current_appid();
1517 // API caller cannot be recognized
1518 ACCOUNT_ERROR("APP ID not detectable!\n");
1519 ret_transaction = _account_end_transaction(FALSE);
1520 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1521 pthread_mutex_unlock(&account_mutex);
1522 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1525 char* verified_appid = NULL;
1526 error_code = _account_check_account_type_with_appid_group(appid, &verified_appid);
1527 if(error_code != ACCOUNT_ERROR_NONE){
1528 ret_transaction = _account_end_transaction(FALSE);
1529 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1530 _ACCOUNT_FREE(verified_appid);
1531 _ACCOUNT_FREE(appid);
1532 pthread_mutex_unlock(&account_mutex);
1536 _ACCOUNT_FREE(appid);
1539 if(!_account_check_add_more_account(verified_appid)) {
1540 ret_transaction = _account_end_transaction(FALSE);
1541 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1542 pthread_mutex_unlock(&account_mutex);
1543 _ACCOUNT_FREE(verified_appid);
1544 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1547 _ACCOUNT_FREE(data->package_name);
1548 data->package_name = _account_get_text(verified_appid);
1549 _ACCOUNT_FREE(verified_appid);
1552 error_code = _account_execute_insert_query(data);
1554 if (error_code != ACCOUNT_ERROR_NONE) {
1555 ret_transaction = _account_end_transaction(FALSE);
1556 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1558 pthread_mutex_unlock(&account_mutex);
1563 ACCOUNT_VERBOSE( "_account_execute_insert_query, insert error_code : %d", error_code);
1565 error_code = _account_insert_capability(data, *account_id);
1566 if (error_code != ACCOUNT_ERROR_NONE) {
1567 ret_transaction = _account_end_transaction(FALSE);
1568 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1570 pthread_mutex_unlock(&account_mutex);
1574 error_code = _account_insert_custom(data, *account_id);
1575 if (error_code != ACCOUNT_ERROR_NONE) {
1576 ret_transaction = _account_end_transaction(FALSE);
1577 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1579 pthread_mutex_unlock(&account_mutex);
1583 pthread_mutex_unlock(&account_mutex);
1584 _account_end_transaction(TRUE);
1587 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1588 _account_insert_delete_update_notification_send(buf);
1590 return ACCOUNT_ERROR_NONE;
1594 ACCOUNT_API int account_create(account_h *account)
1597 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1598 return ACCOUNT_ERROR_INVALID_PARAMETER;
1601 account_s *data = (account_s*)malloc(sizeof(account_s));
1604 ACCOUNT_FATAL("Memory Allocation Failed");
1605 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1607 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1609 ACCOUNT_VERBOSE("create handle=%p\n", *account);
1611 /*Setting account as visible by default*/
1612 data->secret = ACCOUNT_SECRECY_VISIBLE;
1614 /*Setting account as not supporting sync by default*/
1615 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
1617 *account = (account_h)data;
1619 return ACCOUNT_ERROR_NONE;
1622 ACCOUNT_API int account_set_user_name(account_h account, const char *user_name)
1625 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1626 return ACCOUNT_ERROR_INVALID_PARAMETER;
1630 ACCOUNT_ERROR("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1631 return ACCOUNT_ERROR_INVALID_PARAMETER;
1634 account_s *data = (account_s*)account;
1636 _ACCOUNT_FREE(data->user_name);
1637 data->user_name = _account_get_text(user_name);
1639 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1641 return ACCOUNT_ERROR_NONE;
1644 ACCOUNT_API int account_set_display_name(account_h account, const char *display_name)
1647 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1648 return ACCOUNT_ERROR_INVALID_PARAMETER;
1651 if (!display_name) {
1652 ACCOUNT_ERROR("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1653 return ACCOUNT_ERROR_INVALID_PARAMETER;
1656 account_s *data = (account_s*)account;
1658 _ACCOUNT_FREE(data->display_name);
1659 data->display_name = _account_get_text(display_name);
1661 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1663 return ACCOUNT_ERROR_NONE;
1666 ACCOUNT_API int account_set_email_address(account_h account, const char *email_address)
1669 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1670 return ACCOUNT_ERROR_INVALID_PARAMETER;
1673 if (!email_address) {
1674 ACCOUNT_ERROR("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1675 return ACCOUNT_ERROR_INVALID_PARAMETER;
1678 account_s *data = (account_s*)account;
1680 _ACCOUNT_FREE(data->email_address);
1681 data->email_address = _account_get_text(email_address);
1683 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1685 return ACCOUNT_ERROR_NONE;
1688 ACCOUNT_API int account_set_icon_path(account_h account, const char *icon_path)
1691 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1692 return ACCOUNT_ERROR_INVALID_PARAMETER;
1696 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1697 return ACCOUNT_ERROR_INVALID_PARAMETER;
1700 account_s *data = (account_s*)account;
1702 _ACCOUNT_FREE(data->icon_path);
1703 data->icon_path = _account_get_text(icon_path);
1705 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1707 return ACCOUNT_ERROR_NONE;
1710 ACCOUNT_API int account_set_source(account_h account, const char *source)
1713 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1714 return ACCOUNT_ERROR_INVALID_PARAMETER;
1718 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1719 return ACCOUNT_ERROR_INVALID_PARAMETER;
1721 account_s *data = (account_s*)account;
1723 _ACCOUNT_FREE(data->source);
1724 data->source = _account_get_text(source);
1726 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1728 return ACCOUNT_ERROR_NONE;
1731 ACCOUNT_API int account_set_package_name(account_h account, const char *package_name)
1734 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1735 return ACCOUNT_ERROR_INVALID_PARAMETER;
1738 if (!package_name) {
1739 ACCOUNT_ERROR("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1740 return ACCOUNT_ERROR_INVALID_PARAMETER;
1743 account_s *data = (account_s*)account;
1745 _ACCOUNT_FREE(data->package_name);
1746 data->package_name = _account_get_text(package_name);
1748 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1750 return ACCOUNT_ERROR_NONE;
1753 ACCOUNT_API int account_set_domain_name(account_h account, const char *domain_name)
1756 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1757 return ACCOUNT_ERROR_INVALID_PARAMETER;
1761 ACCOUNT_ERROR("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1762 return ACCOUNT_ERROR_INVALID_PARAMETER;
1764 account_s *data = (account_s*)account;
1766 _ACCOUNT_FREE(data->domain_name);
1767 data->domain_name = _account_get_text(domain_name);
1769 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1771 return ACCOUNT_ERROR_NONE;
1774 ACCOUNT_API int account_set_access_token(account_h account, const char *access_token)
1777 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1778 return ACCOUNT_ERROR_INVALID_PARAMETER;
1781 if (!access_token) {
1782 ACCOUNT_ERROR("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1783 return ACCOUNT_ERROR_INVALID_PARAMETER;
1786 account_s *data = (account_s*)account;
1788 _ACCOUNT_FREE(data->access_token);
1789 data->access_token = _account_get_text(access_token);
1791 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1793 return ACCOUNT_ERROR_NONE;
1796 ACCOUNT_API int account_set_user_text(account_h account, int index, const char *user_txt)
1799 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1800 return ACCOUNT_ERROR_INVALID_PARAMETER;
1804 ACCOUNT_ERROR("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1805 return ACCOUNT_ERROR_INVALID_PARAMETER;
1807 if (index >= USER_TXT_CNT || index < 0) {
1808 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1809 return ACCOUNT_ERROR_INVALID_PARAMETER;
1812 account_s *data = (account_s*)account;
1814 _ACCOUNT_FREE(data->user_data_txt[index]);
1815 data->user_data_txt[index] = _account_get_text(user_txt);
1817 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1819 return ACCOUNT_ERROR_NONE;
1822 ACCOUNT_API int account_set_custom(account_h account, const char* key, const char* value)
1825 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1826 return ACCOUNT_ERROR_INVALID_PARAMETER;
1830 ACCOUNT_ERROR("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__);
1831 return ACCOUNT_ERROR_INVALID_PARAMETER;
1835 ACCOUNT_ERROR("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__);
1836 return ACCOUNT_ERROR_INVALID_PARAMETER;
1839 account_s *data = (account_s*)account;
1842 bool b_is_new = TRUE;
1844 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1846 account_custom_s* custom_data = NULL;
1847 custom_data = (account_custom_s*)iter->data;
1848 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
1850 if(!strcmp(custom_data->key, key)) {
1851 _ACCOUNT_FREE(custom_data->value);
1852 custom_data->value = _account_get_text(value);
1858 account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1860 if (custom_data == NULL) {
1861 ACCOUNT_FATAL("(%s)-(%d) MALLOC FAIL\n", __FUNCTION__, __LINE__);
1862 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1864 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1865 custom_data->account_id = data->id;
1866 custom_data->app_id = _account_get_text(data->package_name);
1867 custom_data->key = _account_get_text(key);
1868 custom_data->value = _account_get_text(value);
1869 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1872 return ACCOUNT_ERROR_NONE;
1875 ACCOUNT_API int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1877 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1879 if ( (auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
1880 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1881 return ACCOUNT_ERROR_INVALID_PARAMETER;
1884 account_s *data = (account_s*)account;
1886 data->auth_type = (int)auth_type;
1888 return ACCOUNT_ERROR_NONE;
1891 ACCOUNT_API int account_set_secret(account_h account, const account_secrecy_state_e secret)
1893 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1895 if ( (secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
1896 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1897 return ACCOUNT_ERROR_INVALID_PARAMETER;
1900 account_s *data = (account_s*)account;
1902 data->secret = (int)secret;
1904 return ACCOUNT_ERROR_NONE;
1907 ACCOUNT_API int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
1909 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1911 if ( (sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) {
1912 ACCOUNT_ERROR("(%s)-(%d) sync_support is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1913 return ACCOUNT_ERROR_INVALID_PARAMETER;
1916 account_s *data = (account_s*)account;
1918 data->sync_support= (int)sync_support;
1920 return ACCOUNT_ERROR_NONE;
1923 ACCOUNT_API int account_set_user_int(account_h account, int index, const int user_int)
1926 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1927 return ACCOUNT_ERROR_INVALID_PARAMETER;
1930 if (index >= USER_INT_CNT ||index < 0) {
1931 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1932 return ACCOUNT_ERROR_INVALID_PARAMETER;
1935 account_s *data = (account_s*)account;
1937 data->user_data_int[index] = user_int;
1939 return ACCOUNT_ERROR_NONE;
1942 ACCOUNT_API int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value)
1944 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null"));
1945 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null"));
1947 if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
1948 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1949 return ACCOUNT_ERROR_INVALID_PARAMETER;
1952 account_s *data = (account_s*)account;
1954 GSList *iter = NULL;
1955 bool b_is_new = TRUE;
1957 for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1958 account_capability_s *cap_data = NULL;
1959 cap_data = (account_capability_s*)iter->data;
1961 if(!strcmp(cap_data->type, capability_type)) {
1962 cap_data->value = capability_value;
1969 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1971 if (cap_data == NULL)
1972 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1973 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1975 cap_data->type = _account_get_text(capability_type);
1976 cap_data->value = capability_value;
1977 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1980 return ACCOUNT_ERROR_NONE;
1983 ACCOUNT_API int account_get_user_name(account_h account, char **user_name)
1986 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1987 return ACCOUNT_ERROR_INVALID_PARAMETER;
1991 ACCOUNT_ERROR("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
1992 return ACCOUNT_ERROR_INVALID_PARAMETER;
1995 account_s *data = (account_s*)account;
1997 (*user_name) = NULL;
1998 *user_name = _account_get_text(data->user_name);
2000 return ACCOUNT_ERROR_NONE;
2003 ACCOUNT_API int account_get_display_name(account_h account, char **display_name)
2006 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2007 return ACCOUNT_ERROR_INVALID_PARAMETER;
2010 if (!display_name) {
2011 ACCOUNT_ERROR("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
2012 return ACCOUNT_ERROR_INVALID_PARAMETER;
2015 account_s *data = (account_s*)account;
2017 (*display_name) = NULL;
2019 *display_name = _account_get_text(data->display_name);
2021 return ACCOUNT_ERROR_NONE;
2024 ACCOUNT_API int account_get_email_address(account_h account,char **email_address)
2027 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2028 return ACCOUNT_ERROR_INVALID_PARAMETER;
2031 if (!email_address) {
2032 ACCOUNT_ERROR("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
2033 return ACCOUNT_ERROR_INVALID_PARAMETER;
2036 account_s *data = (account_s*)account;
2038 (*email_address) = NULL;
2040 *email_address = _account_get_text(data->email_address);
2042 return ACCOUNT_ERROR_NONE;
2045 ACCOUNT_API int account_get_icon_path(account_h account, char **icon_path)
2048 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2049 return ACCOUNT_ERROR_INVALID_PARAMETER;
2053 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
2054 return ACCOUNT_ERROR_INVALID_PARAMETER;
2057 account_s *data = (account_s*)account;
2059 (*icon_path) = NULL;
2061 *icon_path = _account_get_text(data->icon_path);
2063 return ACCOUNT_ERROR_NONE;
2066 ACCOUNT_API int account_get_source(account_h account, char **source)
2069 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2070 return ACCOUNT_ERROR_INVALID_PARAMETER;
2074 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
2075 return ACCOUNT_ERROR_INVALID_PARAMETER;
2078 account_s *data = (account_s*)account;
2082 *source = _account_get_text(data->source);
2084 return ACCOUNT_ERROR_NONE;
2087 ACCOUNT_API int account_get_package_name(account_h account, char **package_name)
2090 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2091 return ACCOUNT_ERROR_INVALID_PARAMETER;
2094 if (!package_name) {
2095 ACCOUNT_ERROR("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
2096 return ACCOUNT_ERROR_INVALID_PARAMETER;
2099 account_s *data = (account_s*)account;
2101 (*package_name) = NULL;
2103 *package_name = _account_get_text(data->package_name);
2105 return ACCOUNT_ERROR_NONE;
2108 ACCOUNT_API int account_get_domain_name(account_h account, char **domain_name)
2111 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2112 return ACCOUNT_ERROR_INVALID_PARAMETER;
2116 ACCOUNT_ERROR("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
2117 return ACCOUNT_ERROR_INVALID_PARAMETER;
2120 account_s *data = (account_s*)account;
2122 (*domain_name) = NULL;
2124 *domain_name = _account_get_text(data->domain_name);
2126 return ACCOUNT_ERROR_NONE;
2129 ACCOUNT_API int account_get_access_token(account_h account, char **access_token)
2132 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2133 return ACCOUNT_ERROR_INVALID_PARAMETER;
2136 if (!access_token) {
2137 ACCOUNT_ERROR("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
2138 return ACCOUNT_ERROR_INVALID_PARAMETER;
2141 account_s *data = (account_s*)account;
2143 (*access_token) = NULL;
2145 *access_token = _account_get_text(data->access_token);
2147 return ACCOUNT_ERROR_NONE;
2150 ACCOUNT_API int account_get_user_text(account_h account, int user_text_index, char **text)
2153 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2154 return ACCOUNT_ERROR_INVALID_PARAMETER;
2158 ACCOUNT_ERROR("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
2159 return ACCOUNT_ERROR_INVALID_PARAMETER;
2161 ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2163 account_s *data = (account_s*)account;
2167 *text = _account_get_text(data->user_data_txt[user_text_index]);
2169 return ACCOUNT_ERROR_NONE;
2172 ACCOUNT_API int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
2175 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2176 return ACCOUNT_ERROR_INVALID_PARAMETER;
2179 ACCOUNT_ERROR("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
2180 return ACCOUNT_ERROR_INVALID_PARAMETER;
2183 account_s* data = (account_s*)account;
2185 *auth_type = data->auth_type;
2187 return ACCOUNT_ERROR_NONE;
2190 ACCOUNT_API int account_get_secret(account_h account, account_secrecy_state_e *secret)
2193 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2194 return ACCOUNT_ERROR_INVALID_PARAMETER;
2197 ACCOUNT_ERROR("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
2198 return ACCOUNT_ERROR_INVALID_PARAMETER;
2201 account_s* data = (account_s*)account;
2203 *secret = data->secret;
2205 return ACCOUNT_ERROR_NONE;
2208 ACCOUNT_API int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
2211 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2212 return ACCOUNT_ERROR_INVALID_PARAMETER;
2214 if (!sync_support) {
2215 ACCOUNT_ERROR("(%s)-(%d) sync_support is NULL.\n", __FUNCTION__, __LINE__);
2216 return ACCOUNT_ERROR_INVALID_PARAMETER;
2219 account_s* data = (account_s*)account;
2221 *sync_support = data->sync_support;
2223 return ACCOUNT_ERROR_NONE;
2226 ACCOUNT_API int account_get_account_id(account_h account, int *account_id)
2229 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2230 return ACCOUNT_ERROR_INVALID_PARAMETER;
2233 ACCOUNT_ERROR("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
2234 return ACCOUNT_ERROR_INVALID_PARAMETER;
2237 account_s *data = (account_s*)account;
2239 *account_id = data->id;
2241 return ACCOUNT_ERROR_NONE;
2244 ACCOUNT_API int account_get_user_int(account_h account, int user_int_index, int *integer)
2247 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2248 return ACCOUNT_ERROR_INVALID_PARAMETER;
2251 ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2254 ACCOUNT_ERROR("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
2255 return ACCOUNT_ERROR_INVALID_PARAMETER;
2258 account_s *data = (account_s*)account;
2260 *integer = data->user_data_int[user_int_index];
2262 return ACCOUNT_ERROR_NONE;
2265 ACCOUNT_API int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value)
2267 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2268 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL"));
2269 ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL"));
2272 account_s *data = (account_s*)account;
2274 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2275 account_capability_s *cap_data = NULL;
2277 cap_data = (account_capability_s*)iter->data;
2279 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2281 if(!strcmp(capability_type, cap_data->type)) {
2282 *capability_value = cap_data->value;
2283 return ACCOUNT_ERROR_NONE;
2287 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2290 ACCOUNT_API int account_get_capability_all(account_h account, capability_cb cb_func, void *user_data)
2292 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2293 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2296 account_s *data = (account_s*)account;
2298 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2299 account_capability_s *cap_data = NULL;
2301 cap_data = (account_capability_s*)iter->data;
2303 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2305 //cb_func(cap_data->type, cap_data->value, user_data);
2306 if(cb_func(cap_data->type, cap_data->value, user_data)!=TRUE){
2307 ACCOUNT_VERBOSE("account_get_capability :: cb_func returns false, it is stopped\n");
2308 return ACCOUNT_ERROR_NONE;
2312 return ACCOUNT_ERROR_NONE;
2315 ACCOUNT_API int account_get_custom(account_h account, const char* key, char** value)
2317 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2318 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST"));
2319 ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL"));
2322 account_s *data = (account_s*)account;
2324 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2325 account_custom_s *custom_data = NULL;
2327 custom_data = (account_custom_s*)iter->data;
2329 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2331 if(!strcmp(key, custom_data->key)) {
2333 *value = _account_get_text(custom_data->value);
2334 return ACCOUNT_ERROR_NONE;
2338 ACCOUNT_INFO("key is not found %s", key);
2340 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2343 ACCOUNT_API int account_get_custom_all(account_h account, account_custom_cb cb_func, void* user_data)
2345 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2346 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2349 account_s *data = (account_s*)account;
2351 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2352 bool cb_ret = FALSE;
2353 account_custom_s *custom_data = NULL;
2355 custom_data = (account_custom_s*)iter->data;
2357 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2359 cb_ret = cb_func(custom_data->key, custom_data->value, user_data);
2361 ACCOUNT_INFO("account_get_custom_all callback func ret = %d", cb_ret);
2366 return ACCOUNT_ERROR_NONE;
2369 ACCOUNT_API int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
2371 int error_code = ACCOUNT_ERROR_NONE;
2372 account_stmt hstmt = NULL;
2373 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2376 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2377 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2378 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2380 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2382 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2383 hstmt = _account_prepare_query(query);
2385 rc = _account_query_step(hstmt);
2386 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2388 account_capability_s* capability_record = NULL;
2390 while (rc == SQLITE_ROW) {
2391 bool cb_ret = FALSE;
2392 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2394 if (capability_record == NULL) {
2395 ACCOUNT_FATAL("malloc Failed");
2399 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2401 _account_convert_column_to_capability(hstmt, capability_record);
2403 cb_ret = cb_func(capability_record->type, capability_record->value, user_data);
2405 _account_free_capability_items(capability_record);
2406 _ACCOUNT_FREE(capability_record);
2408 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2410 rc = _account_query_step(hstmt);
2413 _account_query_finalize(hstmt);
2416 error_code = ACCOUNT_ERROR_NONE;
2419 if (hstmt != NULL) {
2420 _account_query_finalize(hstmt);
2424 pthread_mutex_unlock(&account_mutex);
2428 static int _account_compare_old_record(account_s *new_account, int account_id)
2430 int error_code = ACCOUNT_ERROR_NONE;
2431 account_stmt hstmt = NULL;
2432 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2434 account_s *old_account = NULL;
2436 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2437 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2438 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2440 old_account = (account_s*)calloc(1, sizeof(account_s));
2442 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2444 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2445 hstmt = _account_prepare_query(query);
2447 rc = _account_query_step(hstmt);
2448 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2450 while (rc == SQLITE_ROW) {
2451 _account_convert_column_to_account(hstmt, old_account);
2452 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
2453 rc = _account_query_step(hstmt);
2456 _account_query_finalize(hstmt);
2460 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2461 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2464 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2465 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2469 new_account->id = old_account->id;
2472 if(!new_account->user_name) {
2473 if(old_account->user_name)
2474 new_account->user_name = _account_get_text(old_account->user_name);
2478 if(!new_account->display_name) {
2479 if(old_account->display_name)
2480 new_account->display_name = _account_get_text(old_account->display_name);
2484 if(!new_account->email_address) {
2485 if(old_account->email_address)
2486 new_account->email_address = _account_get_text(old_account->email_address);
2490 if(!new_account->domain_name) {
2491 if(old_account->domain_name)
2492 new_account->domain_name = _account_get_text(old_account->domain_name);
2496 if(!new_account->icon_path) {
2497 if(old_account->icon_path)
2498 new_account->icon_path = _account_get_text(old_account->icon_path);
2502 if(!new_account->source) {
2503 if(old_account->source)
2504 new_account->source = _account_get_text(old_account->source);
2507 _ACCOUNT_FREE(new_account->package_name);
2508 new_account->package_name = _account_get_text(old_account->package_name);
2511 if(!new_account->access_token) {
2512 if(old_account->access_token)
2513 new_account->access_token = _account_get_text(old_account->access_token);
2518 for(i=0;i<USER_TXT_CNT;i++) {
2519 if(!new_account->user_data_txt[i]) {
2520 if(old_account->user_data_txt[i])
2521 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2526 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2527 new_account->auth_type = old_account->auth_type;
2531 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2532 new_account->secret = old_account->secret;
2536 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2537 new_account->sync_support = old_account->sync_support;
2541 for(i=0;i<USER_INT_CNT;i++) {
2542 if(new_account->user_data_int[i] == 0) {
2543 new_account->user_data_int[i] = old_account->user_data_int[i];
2549 // user custom table
2554 if (hstmt != NULL) {
2555 _account_query_finalize(hstmt);
2560 _account_free_account_items(old_account);
2561 _ACCOUNT_FREE(old_account);
2564 return ACCOUNT_ERROR_NONE;
2567 static int _account_update_account(account_s *account, int account_id)
2569 int rc = 0, binding_count =0;
2570 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2571 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2572 account_stmt hstmt = NULL;
2574 if (!account->package_name) {
2575 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2576 return ACCOUNT_ERROR_INVALID_PARAMETER;
2579 char* current_appid = NULL;
2580 char* verified_appid = NULL;
2582 current_appid = _account_get_current_appid();
2583 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
2585 _ACCOUNT_FREE(current_appid);
2586 _ACCOUNT_FREE(verified_appid);
2588 if(error_code != ACCOUNT_ERROR_NONE){
2589 ACCOUNT_ERROR("No permission to update\n");
2590 return ACCOUNT_ERROR_PERMISSION_DENIED;
2593 _account_compare_old_record(account, account_id);
2595 if (!account->user_name && !account->display_name && !account->email_address) {
2596 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2597 return ACCOUNT_ERROR_INVALID_PARAMETER;
2600 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2602 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2604 count = _account_get_record_count(query);
2606 ACCOUNT_WARNING(" Account record not found, count = %d\n", count);
2607 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2610 /* transaction control required*/
2611 ret_transaction = _account_begin_transaction();
2613 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2614 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2615 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2616 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2617 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2619 hstmt = _account_prepare_query(query);
2620 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)));
2622 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2623 _account_query_bind_int(hstmt, binding_count++, account_id);
2625 rc = _account_query_step(hstmt);
2626 if (rc != SQLITE_DONE) {
2627 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2630 _account_query_finalize(hstmt);
2633 /*update capability*/
2634 error_code = _account_update_capability(account, account_id);
2635 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2636 ret_transaction = _account_end_transaction(FALSE);
2637 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2642 error_code = _account_update_custom(account, account_id);
2643 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2644 ret_transaction = _account_end_transaction(FALSE);
2645 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2649 ret_transaction = _account_end_transaction(TRUE);
2650 ACCOUNT_DEBUG("update capability success, trying to commit(%x) !!!\n", ret_transaction);
2655 ACCOUNT_API int account_update_to_db_by_id(const account_h account, int account_id)
2657 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2658 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2659 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2660 int error_code = ACCOUNT_ERROR_NONE;
2661 account_s* data = (account_s*)account;
2663 pthread_mutex_lock(&account_mutex);
2665 error_code = _account_update_account(data, account_id);
2667 if(error_code != ACCOUNT_ERROR_NONE) {
2668 pthread_mutex_unlock(&account_mutex);
2672 pthread_mutex_unlock(&account_mutex);
2675 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2676 _account_insert_delete_update_notification_send(buf);
2678 return ACCOUNT_ERROR_NONE;
2681 ACCOUNT_API int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
2683 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2684 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2685 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2686 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2688 int error_code = ACCOUNT_ERROR_NONE;
2689 account_s *data = (account_s*)account;
2691 pthread_mutex_lock(&account_mutex);
2693 error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
2695 pthread_mutex_unlock(&account_mutex);
2698 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2699 _account_insert_delete_update_notification_send(buf);
2704 ACCOUNT_API int account_foreach_account_from_db(account_cb callback, void *user_data)
2706 int error_code = ACCOUNT_ERROR_NONE;
2707 account_stmt hstmt = NULL;
2708 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2710 GList *account_list = NULL;
2712 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
2713 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
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, };
2838 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2839 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2840 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2842 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2844 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2845 hstmt = _account_prepare_query(query);
2847 rc = _account_query_step(hstmt);
2848 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2850 account_s *account_record = (account_s *)(*account);
2852 while (rc == SQLITE_ROW) {
2853 _account_convert_column_to_account(hstmt, account_record);
2854 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
2855 rc = _account_query_step(hstmt);
2858 _account_query_finalize(hstmt);
2859 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2860 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2863 error_code = ACCOUNT_ERROR_NONE;
2866 if (hstmt != NULL) {
2867 _account_query_finalize(hstmt);
2871 pthread_mutex_unlock(&account_mutex);
2875 ACCOUNT_API int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
2877 int error_code = ACCOUNT_ERROR_NONE;
2878 account_stmt hstmt = NULL;
2879 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2882 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2883 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2885 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2887 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2889 hstmt = _account_prepare_query(query);
2891 int binding_count = 1;
2892 _account_query_bind_text(hstmt, binding_count++, user_name);
2894 rc = _account_query_step(hstmt);
2896 account_s *account_head = NULL;
2898 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2902 account_head = (account_s*) malloc(sizeof(account_s));
2903 if (account_head == NULL) {
2904 ACCOUNT_FATAL("malloc Failed");
2905 if (hstmt != NULL) {
2906 _account_query_finalize(hstmt);
2909 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2911 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2913 while (rc == SQLITE_ROW) {
2914 account_s* account_record = NULL;
2916 account_record = (account_s*) malloc(sizeof(account_s));
2918 if (account_record == NULL) {
2919 ACCOUNT_FATAL("malloc Failed");
2922 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2924 _account_convert_column_to_account(hstmt, account_record);
2926 account_head->account_list = g_list_append(account_head->account_list, account_record);
2928 rc = _account_query_step(hstmt);
2930 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2933 _account_query_finalize(hstmt);
2939 tmp = g_list_length(account_head->account_list);
2940 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2942 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2944 account = (account_h)iter->data;
2946 account_s *testaccount = (account_s*)account;
2948 ACCOUNT_VERBOSE("id = %d", testaccount->id);
2949 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
2950 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
2951 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
2952 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
2953 ACCOUNT_VERBOSE("source = %s", testaccount->source);
2954 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
2955 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
2956 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
2957 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
2958 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
2959 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
2961 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2962 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2964 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2966 //cb_func(account, user_data);
2967 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("cb_func returns false, it is stopped.\n"));
2971 error_code = ACCOUNT_ERROR_NONE;
2974 if (hstmt != NULL) {
2975 _account_query_finalize(hstmt);
2979 if (account_head->account_list) {
2980 _account_glist_free(account_head->account_list);
2981 account_head->account_list = NULL;
2982 _account_free_account_items(account_head);
2984 _ACCOUNT_FREE(account_head);
2987 pthread_mutex_unlock(&account_mutex);
2991 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)
2993 int error_code = ACCOUNT_ERROR_NONE;
2994 account_stmt hstmt = NULL;
2995 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2998 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3000 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
3001 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
3002 return ACCOUNT_ERROR_INVALID_PARAMETER;
3005 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3006 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3008 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3010 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3012 hstmt = _account_prepare_query(query);
3014 int binding_count = 1;
3015 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3016 _account_query_bind_int(hstmt, binding_count++, (int)capability_value);
3018 rc = _account_query_step(hstmt);
3020 account_s* account_head = NULL;
3022 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3026 account_head = (account_s*) malloc(sizeof(account_s));
3027 if (account_head == NULL) {
3028 ACCOUNT_FATAL("malloc Failed");
3029 if (hstmt != NULL) {
3030 _account_query_finalize(hstmt);
3033 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3035 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3037 while (rc == SQLITE_ROW) {
3038 account_s* account_record = NULL;
3040 account_record = (account_s*) malloc(sizeof(account_s));
3042 if (account_record == NULL) {
3043 ACCOUNT_FATAL("malloc Failed");
3046 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3048 _account_convert_column_to_account(hstmt, account_record);
3050 account_head->account_list = g_list_append(account_head->account_list, account_record);
3052 rc = _account_query_step(hstmt);
3054 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3057 _account_query_finalize(hstmt);
3063 tmp = g_list_length(account_head->account_list);
3064 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3066 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3067 account_h account = NULL;
3068 account = (account_h)iter->data;
3069 account_s* testaccount = (account_s*)account;
3071 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3072 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3074 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3076 //cb_func(account, user_data);
3077 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("cb_func returns false, it is stopped.\n"));
3081 error_code = ACCOUNT_ERROR_NONE;
3084 if (hstmt != NULL) {
3085 _account_query_finalize(hstmt);
3089 if (account_head->account_list) {
3090 _account_glist_free(account_head->account_list);
3091 account_head->account_list = NULL;
3092 _account_free_account_items(account_head);
3094 _ACCOUNT_FREE(account_head);
3097 pthread_mutex_unlock(&account_mutex);
3101 ACCOUNT_API int account_query_account_by_capability_type(account_cb cb_func, const char* capability_type, void* user_data)
3103 int error_code = ACCOUNT_ERROR_NONE;
3104 account_stmt hstmt = NULL;
3105 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3108 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3109 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3110 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3112 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3114 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3116 hstmt = _account_prepare_query(query);
3118 int binding_count = 1;
3119 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3121 rc = _account_query_step(hstmt);
3123 account_s* account_head = NULL;
3125 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3129 account_head = (account_s*) malloc(sizeof(account_s));
3130 if (account_head == NULL) {
3131 ACCOUNT_FATAL("malloc Failed");
3132 if (hstmt != NULL) {
3133 _account_query_finalize(hstmt);
3136 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3138 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3140 while (rc == SQLITE_ROW) {
3141 account_s* account_record = NULL;
3143 account_record = (account_s*) malloc(sizeof(account_s));
3145 if (account_record == NULL) {
3146 ACCOUNT_FATAL("malloc Failed");
3149 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3151 _account_convert_column_to_account(hstmt, account_record);
3153 account_head->account_list = g_list_append(account_head->account_list, account_record);
3155 rc = _account_query_step(hstmt);
3157 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3160 _account_query_finalize(hstmt);
3166 tmp = g_list_length(account_head->account_list);
3167 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3169 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3170 account_h account = NULL;
3171 account = (account_h)iter->data;
3172 account_s* testaccount = (account_s*)account;
3174 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3175 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3177 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3179 //cb_func(account, user_data);
3180 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n"));
3184 error_code = ACCOUNT_ERROR_NONE;
3187 if (hstmt != NULL) {
3188 _account_query_finalize(hstmt);
3192 if (account_head->account_list) {
3193 _account_glist_free(account_head->account_list);
3194 account_head->account_list = NULL;
3195 _account_free_account_items(account_head);
3197 _ACCOUNT_FREE(account_head);
3200 pthread_mutex_unlock(&account_mutex);
3204 ACCOUNT_API int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
3206 int error_code = ACCOUNT_ERROR_NONE;
3207 account_stmt hstmt = NULL;
3208 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3211 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
3212 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3213 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3215 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3217 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3219 hstmt = _account_prepare_query(query);
3221 int binding_count = 1;
3222 _account_query_bind_text(hstmt, binding_count++, package_name);
3224 rc = _account_query_step(hstmt);
3226 account_s* account_head = NULL;
3228 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3232 account_head = (account_s*) malloc(sizeof(account_s));
3233 if (account_head == NULL) {
3234 ACCOUNT_FATAL("malloc Failed");
3235 if (hstmt != NULL) {
3236 _account_query_finalize(hstmt);
3239 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3241 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3243 while (rc == SQLITE_ROW) {
3244 account_s* account_record = NULL;
3246 account_record = (account_s*) malloc(sizeof(account_s));
3248 if (account_record == NULL) {
3249 ACCOUNT_FATAL("malloc Failed");
3252 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3254 _account_convert_column_to_account(hstmt, account_record);
3256 account_head->account_list = g_list_append(account_head->account_list, account_record);
3258 rc = _account_query_step(hstmt);
3260 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3263 _account_query_finalize(hstmt);
3268 tmp = g_list_length(account_head->account_list);
3269 ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
3271 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3272 account_h account = NULL;
3273 account = (account_h)iter->data;
3275 account_s* testaccount = (account_s*)account;
3277 ACCOUNT_VERBOSE("id = %d", testaccount->id);
3278 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
3279 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
3280 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
3281 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
3282 ACCOUNT_VERBOSE("source = %s", testaccount->source);
3283 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
3284 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
3285 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
3286 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
3287 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
3288 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
3290 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3291 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3293 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3295 //cb_func(account, user_data);
3296 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n"));
3300 error_code = ACCOUNT_ERROR_NONE;
3303 if (hstmt != NULL) {
3304 _account_query_finalize(hstmt);
3308 if (account_head->account_list) {
3309 _account_glist_free(account_head->account_list);
3310 account_head->account_list = NULL;
3311 _account_free_account_items(account_head);
3313 _ACCOUNT_FREE(account_head);
3316 pthread_mutex_unlock(&account_mutex);
3320 ACCOUNT_API int account_delete(int account_id)
3322 int error_code = ACCOUNT_ERROR_NONE;
3323 account_stmt hstmt = NULL;
3324 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3326 int ret_transaction = 0;
3327 bool is_success = FALSE;
3329 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3332 account_h account = NULL;
3333 /* Check requested ID to delete */
3334 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3336 count = _account_get_record_count(query);
3338 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3339 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3342 /* Check permission of requested appid */
3343 account_create(&account);
3344 account_query_account_by_account_id(account_id, &account);
3346 char* current_appid = NULL;
3347 char* verified_appid = NULL;
3349 current_appid = _account_get_current_appid();
3350 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
3352 _ACCOUNT_FREE(current_appid);
3353 _ACCOUNT_FREE(verified_appid);
3355 if(error_code != ACCOUNT_ERROR_NONE){
3356 ACCOUNT_ERROR("No permission to delete\n");
3357 return ACCOUNT_ERROR_PERMISSION_DENIED;
3360 account_destroy(account);
3362 /* transaction control required*/
3363 ret_transaction = _account_begin_transaction();
3365 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3366 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3367 pthread_mutex_unlock(&account_mutex);
3368 return ret_transaction;
3371 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3372 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3374 hstmt = _account_prepare_query(query);
3375 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3376 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3378 rc = _account_query_step(hstmt);
3379 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3381 _account_query_finalize(hstmt);
3384 ACCOUNT_MEMSET(query, 0, sizeof(query));
3386 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3388 hstmt = _account_prepare_query(query);
3389 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3390 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3392 rc = _account_query_step(hstmt);
3393 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3395 _account_query_finalize(hstmt);
3398 /* delete custom data */
3399 ACCOUNT_MEMSET(query, 0, sizeof(query));
3401 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3403 hstmt = _account_prepare_query(query);
3404 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3405 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3407 rc = _account_query_step(hstmt);
3408 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3410 _account_query_finalize(hstmt);
3415 if (hstmt != NULL) {
3416 _account_query_finalize(hstmt);
3420 ret_transaction = _account_end_transaction(is_success);
3422 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3423 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3425 if (is_success == true) {
3427 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3428 _account_insert_delete_update_notification_send(buf);
3432 pthread_mutex_unlock(&account_mutex);
3438 ACCOUNT_API int account_delete_from_db_by_id(int account_db_id)
3440 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
3442 return account_delete(account_db_id);
3445 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3447 int error_code = ACCOUNT_ERROR_NONE;
3448 account_stmt hstmt = NULL;
3449 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3451 int binding_count = 1;
3453 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3454 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3455 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3456 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3458 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3460 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3461 hstmt = _account_prepare_query(query);
3463 _account_query_bind_text(hstmt, binding_count++, username);
3464 _account_query_bind_text(hstmt, binding_count++, package_name);
3466 rc = _account_query_step(hstmt);
3467 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3469 account_s *account_record = (account_s *)(*account);
3471 while (rc == SQLITE_ROW) {
3472 _account_convert_column_to_account(hstmt, account_record);
3473 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
3474 rc = _account_query_step(hstmt);
3477 _account_query_finalize(hstmt);
3478 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3479 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3482 error_code = ACCOUNT_ERROR_NONE;
3485 if (hstmt != NULL) {
3486 _account_query_finalize(hstmt);
3490 pthread_mutex_unlock(&account_mutex);
3494 ACCOUNT_API int account_delete_from_db_by_user_name(char *user_name, char *package_name)
3496 int error_code = ACCOUNT_ERROR_NONE;
3497 account_stmt hstmt = NULL;
3498 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3500 int ret_transaction = 0;
3501 bool is_success = FALSE;
3502 account_h account = NULL;
3503 int binding_count = 1;
3504 int account_id = -1;
3506 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3507 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3508 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3510 rc = account_create(&account);
3511 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3513 rc = account_get_account_id(account, &account_id);
3515 rc = account_destroy(account);
3517 /* transaction control required*/
3518 ret_transaction = _account_begin_transaction();
3520 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3521 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3522 pthread_mutex_unlock(&account_mutex);
3523 return ret_transaction;
3526 /* delete custom data */
3527 ACCOUNT_MEMSET(query, 0, sizeof(query));
3528 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3530 hstmt = _account_prepare_query(query);
3532 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3533 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3535 _account_query_bind_int(hstmt, binding_count++, account_id);
3537 rc = _account_query_step(hstmt);
3538 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3540 _account_query_finalize(hstmt);
3543 /* delete capability */
3544 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3546 hstmt = _account_prepare_query(query);
3548 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3549 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3552 _account_query_bind_text(hstmt, binding_count++, user_name);
3553 _account_query_bind_text(hstmt, binding_count++, package_name);
3555 rc = _account_query_step(hstmt);
3556 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3558 _account_query_finalize(hstmt);
3561 ACCOUNT_MEMSET(query, 0, sizeof(query));
3563 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3565 hstmt = _account_prepare_query(query);
3566 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3567 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3570 _account_query_bind_text(hstmt, binding_count++, user_name);
3571 _account_query_bind_text(hstmt, binding_count++, package_name);
3573 rc = _account_query_step(hstmt);
3574 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));
3576 _account_query_finalize(hstmt);
3582 if (hstmt != NULL) {
3583 _account_query_finalize(hstmt);
3587 ret_transaction = _account_end_transaction(is_success);
3589 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3590 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3592 if (is_success == true) {
3594 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3595 _account_insert_delete_update_notification_send(buf);
3599 pthread_mutex_unlock(&account_mutex);
3604 ACCOUNT_API int account_delete_from_db_by_package_name(char *package_name)
3606 int error_code = ACCOUNT_ERROR_NONE;
3607 account_stmt hstmt = NULL;
3608 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3610 int ret_transaction = 0;
3611 bool is_success = FALSE;
3612 int binding_count = 1;
3614 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3615 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3617 /* transaction control required*/
3618 ret_transaction = _account_begin_transaction();
3620 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3621 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3622 pthread_mutex_unlock(&account_mutex);
3623 return ret_transaction;
3626 /* delete custom table */
3627 ACCOUNT_MEMSET(query, 0, sizeof(query));
3628 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3630 hstmt = _account_prepare_query(query);
3632 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3633 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3636 _account_query_bind_text(hstmt, binding_count++, package_name);
3638 rc = _account_query_step(hstmt);
3639 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3641 _account_query_finalize(hstmt);
3644 /* delete capability table */
3645 ACCOUNT_MEMSET(query, 0, sizeof(query));
3646 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3648 hstmt = _account_prepare_query(query);
3650 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3651 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3654 _account_query_bind_text(hstmt, binding_count++, package_name);
3656 rc = _account_query_step(hstmt);
3657 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3659 _account_query_finalize(hstmt);
3662 /* delete account table */
3663 ACCOUNT_MEMSET(query, 0, sizeof(query));
3665 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3667 hstmt = _account_prepare_query(query);
3668 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3669 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3672 _account_query_bind_text(hstmt, binding_count++, package_name);
3674 rc = _account_query_step(hstmt);
3675 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));
3677 _account_query_finalize(hstmt);
3683 if (hstmt != NULL) {
3684 _account_query_finalize(hstmt);
3688 ret_transaction = _account_end_transaction(is_success);
3690 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3691 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3693 if (is_success == true) {
3695 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, -1);
3696 _account_insert_delete_update_notification_send(buf);
3700 pthread_mutex_unlock(&account_mutex);
3705 ACCOUNT_API int account_get_total_count_from_db(int *count)
3708 ACCOUNT_ERROR("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3709 return ACCOUNT_ERROR_INVALID_PARAMETER;
3711 char query[1024] = {0, };
3712 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3713 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3715 *count = _account_get_record_count(query);
3718 account_stmt pStmt = NULL;
3720 assert(NULL != g_hAccountDB);
3721 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3723 rc = sqlite3_step(pStmt);
3724 if (SQLITE_ROW != rc) {
3725 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3726 sqlite3_finalize(pStmt);
3727 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3730 ncount = sqlite3_column_int(pStmt, 0);
3734 ACCOUNT_VERBOSE("Number of account : %d", ncount);
3735 sqlite3_finalize(pStmt);
3738 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3739 return ACCOUNT_ERROR_DB_FAILED;
3742 return ACCOUNT_ERROR_NONE;
3745 ACCOUNT_API int account_destroy(account_h account)
3747 account_s *data = (account_s*)account;
3749 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3751 ACCOUNT_INFO("destroy handle=%p\n", data);
3753 _account_free_account_items(data);
3754 _ACCOUNT_FREE(data);
3756 return ACCOUNT_ERROR_NONE;
3759 static int _account_type_free_label_items(label_s *data)
3761 _ACCOUNT_FREE(data->app_id);
3762 _ACCOUNT_FREE(data->label);
3763 _ACCOUNT_FREE(data->locale);
3765 return ACCOUNT_ERROR_NONE;
3768 static int _account_type_free_feature_items(provider_feature_s *data)
3770 _ACCOUNT_FREE(data->app_id);
3771 _ACCOUNT_FREE(data->key);
3773 return ACCOUNT_ERROR_NONE;
3776 static int _account_type_gslist_free(GSList* list)
3778 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
3782 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
3783 label_s *label_data = (label_s*)iter->data;
3784 _account_type_free_label_items(label_data);
3785 _ACCOUNT_FREE(label_data);
3791 return ACCOUNT_ERROR_NONE;
3794 static int _account_type_item_free(account_type_s *data)
3796 _ACCOUNT_FREE(data->app_id);
3797 _ACCOUNT_FREE(data->service_provider_id);
3798 _ACCOUNT_FREE(data->icon_path);
3799 _ACCOUNT_FREE(data->small_icon_path);
3801 return ACCOUNT_ERROR_NONE;
3804 static int _account_type_glist_free(GList* list)
3806 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
3810 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
3811 account_type_s *account_type_record = (account_type_s*)iter->data;
3812 _account_type_item_free(account_type_record);
3813 _ACCOUNT_FREE(account_type_record);
3819 return ACCOUNT_ERROR_NONE;
3822 static int _account_type_free_account_type_items(account_type_s *data)
3824 _account_type_item_free(data);
3826 _account_type_gslist_free(data->label_list);
3827 _account_type_glist_free(data->account_type_list);
3829 return ACCOUNT_ERROR_NONE;
3832 ACCOUNT_API int account_type_create(account_type_h *account_type)
3834 if (!account_type) {
3835 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3836 return ACCOUNT_ERROR_INVALID_PARAMETER;
3839 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3842 ACCOUNT_ERROR("Memory Allocation Failed");
3843 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3846 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3848 ACCOUNT_VERBOSE("create handle=%p\n", *account_type);
3850 *account_type = (account_type_h)data;
3852 return ACCOUNT_ERROR_NONE;
3855 ACCOUNT_API int account_type_destroy(account_type_h account_type)
3857 account_type_s *data = (account_type_s*)account_type;
3859 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
3861 ACCOUNT_VERBOSE("destroy handle=%p\n", data);
3863 _account_type_free_account_type_items(data);
3864 _ACCOUNT_FREE(data);
3866 return ACCOUNT_ERROR_NONE;
3869 //app_id mandatory field
3870 ACCOUNT_API int account_type_set_app_id(account_type_h account_type, const char *app_id)
3872 if (!account_type) {
3873 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3874 return ACCOUNT_ERROR_INVALID_PARAMETER;
3878 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
3879 return ACCOUNT_ERROR_INVALID_PARAMETER;
3882 account_type_s *data = (account_type_s*)account_type;
3884 _ACCOUNT_FREE(data->app_id);
3885 data->app_id = _account_get_text(app_id);
3887 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->app_id, app_id);
3888 return ACCOUNT_ERROR_NONE;
3891 //service_provider_id mandatory field
3892 ACCOUNT_API int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
3894 if (!account_type) {
3895 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3896 return ACCOUNT_ERROR_INVALID_PARAMETER;
3899 if (!service_provider_id) {
3900 ACCOUNT_ERROR("(%s)-(%d) service_provider_id is NULL.\n", __FUNCTION__, __LINE__);
3901 return ACCOUNT_ERROR_INVALID_PARAMETER;
3904 account_type_s *data = (account_type_s*)account_type;
3906 _ACCOUNT_FREE(data->service_provider_id);
3907 data->service_provider_id = _account_get_text(service_provider_id);
3909 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->service_provider_id, service_provider_id);
3910 return ACCOUNT_ERROR_NONE;
3913 ACCOUNT_API int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
3915 if (!account_type) {
3916 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3917 return ACCOUNT_ERROR_INVALID_PARAMETER;
3921 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
3922 return ACCOUNT_ERROR_INVALID_PARAMETER;
3925 account_type_s *data = (account_type_s*)account_type;
3927 _ACCOUNT_FREE(data->icon_path);
3928 data->icon_path = _account_get_text(icon_path);
3930 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
3931 return ACCOUNT_ERROR_NONE;
3934 ACCOUNT_API int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
3936 if (!account_type) {
3937 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3938 return ACCOUNT_ERROR_INVALID_PARAMETER;
3941 if (!small_icon_path) {
3942 ACCOUNT_ERROR("(%s)-(%d) small_icon_path is NULL.\n", __FUNCTION__, __LINE__);
3943 return ACCOUNT_ERROR_INVALID_PARAMETER;
3946 account_type_s *data = (account_type_s*)account_type;
3948 _ACCOUNT_FREE(data->small_icon_path);
3949 data->small_icon_path = _account_get_text(small_icon_path);
3951 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->small_icon_path, small_icon_path);
3952 return ACCOUNT_ERROR_NONE;
3955 ACCOUNT_API int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
3957 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
3959 account_type_s *data = (account_type_s*)account_type;
3961 data->multiple_account_support = multiple_account_support;
3963 return ACCOUNT_ERROR_NONE;
3967 ACCOUNT_API int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
3969 if (!account_type) {
3970 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3971 return ACCOUNT_ERROR_INVALID_PARAMETER;
3974 if(!label || !locale) {
3975 ACCOUNT_ERROR("(%s)-(%d) label(%p) or locale(%p) is NULL.\n", __FUNCTION__, __LINE__, label, locale);
3976 return ACCOUNT_ERROR_INVALID_PARAMETER;
3979 account_type_s *data = (account_type_s*)account_type;
3980 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3982 if (label_data == NULL) {
3983 ACCOUNT_FATAL("(%s)-(%d) Malloc fail. label_data is NULL.\n", __FUNCTION__, __LINE__);
3984 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3986 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3988 label_data->label = _account_get_text(label);
3989 label_data->locale = _account_get_text(locale);
3991 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3993 return ACCOUNT_ERROR_NONE;
3996 ACCOUNT_API int account_type_get_app_id(account_type_h account_type, char **app_id)
3998 if (!account_type) {
3999 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4000 return ACCOUNT_ERROR_INVALID_PARAMETER;
4004 ACCOUNT_ERROR("(%s)-(%d) app id is NULL.\n", __FUNCTION__, __LINE__);
4005 return ACCOUNT_ERROR_INVALID_PARAMETER;
4008 account_type_s *data = (account_type_s*)account_type;
4011 *app_id = _account_get_text(data->app_id);
4013 return ACCOUNT_ERROR_NONE;
4016 ACCOUNT_API int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4018 if (!account_type) {
4019 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4020 return ACCOUNT_ERROR_INVALID_PARAMETER;
4023 if (!service_provider_id) {
4024 ACCOUNT_ERROR("(%s)-(%d) service provider id is NULL.\n", __FUNCTION__, __LINE__);
4025 return ACCOUNT_ERROR_INVALID_PARAMETER;
4028 account_type_s *data = (account_type_s*)account_type;
4030 (*service_provider_id) = NULL;
4031 *service_provider_id = _account_get_text(data->service_provider_id);
4033 return ACCOUNT_ERROR_NONE;
4036 ACCOUNT_API int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4038 if (!account_type) {
4039 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4040 return ACCOUNT_ERROR_INVALID_PARAMETER;
4044 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4045 return ACCOUNT_ERROR_INVALID_PARAMETER;
4048 account_type_s *data = (account_type_s*)account_type;
4050 (*icon_path) = NULL;
4051 *icon_path = _account_get_text(data->icon_path);
4053 return ACCOUNT_ERROR_NONE;
4056 ACCOUNT_API int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4058 if (!account_type) {
4059 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4060 return ACCOUNT_ERROR_INVALID_PARAMETER;
4063 if (!small_icon_path) {
4064 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4065 return ACCOUNT_ERROR_INVALID_PARAMETER;
4068 account_type_s *data = (account_type_s*)account_type;
4070 (*small_icon_path) = NULL;
4071 *small_icon_path = _account_get_text(data->small_icon_path);
4073 return ACCOUNT_ERROR_NONE;
4076 ACCOUNT_API int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4078 if (!account_type) {
4079 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4080 return ACCOUNT_ERROR_INVALID_PARAMETER;
4082 if (!multiple_account_support) {
4083 ACCOUNT_ERROR("(%s)-(%d) multiple_account_support is NULL.\n", __FUNCTION__, __LINE__);
4084 return ACCOUNT_ERROR_INVALID_PARAMETER;
4087 account_type_s *data = (account_type_s*)account_type;
4089 *multiple_account_support = data->multiple_account_support;
4091 return ACCOUNT_ERROR_NONE;
4094 ACCOUNT_API int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4096 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4097 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4100 account_type_s *data = (account_type_s*)account_type;
4102 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4103 label_s *label_data = NULL;
4105 label_data = (label_s*)iter->data;
4107 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4111 if(!strcmp(locale, label_data->locale)) {
4112 *label = _account_get_text(label_data->label);
4113 return ACCOUNT_ERROR_NONE;
4117 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4120 ACCOUNT_API int account_type_get_label(account_type_h account_type, account_label_cb cb_func, void *user_data)
4122 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4123 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4126 account_type_s *data = (account_type_s*)account_type;
4128 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4129 label_s *label_data = NULL;
4131 label_data = (label_s*)iter->data;
4133 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4135 //cb_func(label_data->app_id, label_data->label, label_data->locale, user_data);
4136 if(cb_func(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4137 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4138 return ACCOUNT_ERROR_NONE;
4142 return ACCOUNT_ERROR_NONE;
4145 static gboolean _account_type_check_duplicated(account_type_s *data)
4147 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4150 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4152 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4153 , ACCOUNT_TYPE_TABLE, data->app_id);
4155 count = _account_get_record_count(query);
4157 ACCOUNT_VERBOSE("_account_type_check_duplicated : duplicated %d account type(s) exist!, app_id=%s, service_provider_id=%s\n",
4158 count, data->app_id, data->service_provider_id);
4165 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4169 /*Caution : Keep insert query orders.*/
4172 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4174 /* 2. service provider id*/
4175 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4178 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4180 /* 4. small icon path*/
4181 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4183 /* 5. multiple accont support*/
4184 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4190 static int _account_type_execute_insert_query(account_type_s *account_type)
4193 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4194 int error_code = ACCOUNT_ERROR_NONE;
4195 account_stmt hstmt = NULL;
4197 /* check mandatory field */
4198 // app id & service provider id
4199 if (!account_type->app_id) {
4200 ACCOUNT_ERROR("App id is mandetory field, it can not be NULL!!!!\n");
4201 return ACCOUNT_ERROR_INVALID_PARAMETER;
4204 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4205 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4206 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4208 hstmt = _account_prepare_query(query);
4209 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4211 _account_type_convert_account_to_sql(account_type, hstmt, query);
4213 rc = _account_query_step(hstmt);
4214 if (rc != SQLITE_DONE) {
4215 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4216 error_code = ACCOUNT_ERROR_DB_FAILED;
4219 _account_query_finalize(hstmt);
4225 static int _account_type_insert_label(account_type_s *account_type)
4228 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4229 account_stmt hstmt = NULL;
4231 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4233 if (g_slist_length( account_type->label_list)==0) {
4234 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4235 return ACCOUNT_ERROR_NONE;
4238 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4240 rc = _account_get_record_count(query);
4243 ACCOUNT_ERROR( "_account_insert_label : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
4244 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4250 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4253 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4254 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4255 "(?, ?, ?) ", LABEL_TABLE);
4257 hstmt = _account_prepare_query(query);
4259 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4261 label_s* label_data = NULL;
4262 label_data = (label_s*)iter->data;
4263 ACCOUNT_VERBOSE( "label_data->appid = %s, label_data->label = %s, label_data->locale \n", label_data->app_id, label_data->label, label_data->locale);
4265 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4266 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4267 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4268 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4269 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4270 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4272 rc = _account_query_step(hstmt);
4274 if (rc != SQLITE_DONE) {
4275 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4279 _account_query_finalize(hstmt);
4284 ACCOUNT_VERBOSE( "_account_type_insert_label() DONE\n");
4286 return ACCOUNT_ERROR_NONE;
4289 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4291 char *textbuf = NULL;
4293 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4294 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4296 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4297 _account_db_data_to_text(textbuf, &(feature_record->key));
4299 ACCOUNT_VERBOSE("END _account_type_convert_column_to_provider_feature");
4302 ACCOUNT_API int account_type_query_provider_feature_by_app_id(provider_feature_cb cb_func, const char* app_id, void *user_data )
4304 int error_code = ACCOUNT_ERROR_NONE;
4305 account_stmt hstmt = NULL;
4306 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4307 int rc = 0, binding_count = 1;
4309 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4310 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4311 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4313 ACCOUNT_VERBOSE("app_id = %s", app_id);
4315 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4317 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4318 hstmt = _account_prepare_query(query);
4320 _account_query_bind_text(hstmt, binding_count++, app_id);
4322 rc = _account_query_step(hstmt);
4323 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4325 provider_feature_s* feature_record = NULL;
4327 while (rc == SQLITE_ROW) {
4328 bool cb_ret = FALSE;
4329 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4331 if (feature_record == NULL) {
4332 ACCOUNT_FATAL("malloc Failed");
4336 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4338 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4340 cb_ret = cb_func(feature_record->app_id, feature_record->key, user_data);
4342 _account_type_free_feature_items(feature_record);
4343 _ACCOUNT_FREE(feature_record);
4345 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4347 rc = _account_query_step(hstmt);
4350 _account_query_finalize(hstmt);
4353 error_code = ACCOUNT_ERROR_NONE;
4356 if (hstmt != NULL) {
4357 _account_query_finalize(hstmt);
4361 pthread_mutex_unlock(&account_mutex);
4365 ACCOUNT_API bool account_type_query_supported_feature(const char* app_id, const char* capability)
4367 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4368 int record_count = 0;
4370 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4371 ACCOUNT_RETURN_VAL((capability != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CAPABILITY"));
4373 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4375 record_count = _account_get_record_count(query);
4377 if (record_count <= 0) {
4378 ACCOUNT_WARNING( "related capability type item is not existed rc=%d , %s", record_count, _account_db_err_msg());
4387 ACCOUNT_API int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb cb_func, void* user_data)
4389 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4390 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4393 account_type_s *data = (account_type_s*)account_type;
4395 for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4396 provider_feature_s *feature_data = NULL;
4398 feature_data = (provider_feature_s*)iter->data;
4400 ACCOUNT_VERBOSE("appid = %s, key = %s", feature_data->key, feature_data->app_id);
4402 //cb_func(feature_data->app_id, feature_data->key, user_data);
4403 if(cb_func(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
4404 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4405 return ACCOUNT_ERROR_NONE;
4409 return ACCOUNT_ERROR_NONE;
4412 ACCOUNT_API int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4414 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4415 ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4417 account_type_s *data = (account_type_s*)account_type;
4419 GSList *iter = NULL;
4420 bool b_is_new = TRUE;
4422 for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4423 provider_feature_s *feature_data = NULL;
4424 feature_data = (provider_feature_s*)iter->data;
4426 if(!strcmp(feature_data->key, provider_feature)) {
4433 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4435 if (feature_data == NULL)
4436 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4437 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4439 feature_data->key = _account_get_text(provider_feature);
4440 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4443 return ACCOUNT_ERROR_NONE;
4446 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4449 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4450 account_stmt hstmt = NULL;
4452 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4453 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4455 if (g_slist_length( account_type->provider_feature_list)==0) {
4456 ACCOUNT_ERROR( "no capability\n");
4457 return ACCOUNT_ERROR_NONE;
4460 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4462 rc = _account_get_record_count(query);
4465 ACCOUNT_WARNING( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4466 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4473 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4476 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4477 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4478 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4480 hstmt = _account_prepare_query(query);
4482 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4484 provider_feature_s* feature_data = NULL;
4485 feature_data = (provider_feature_s*)iter->data;
4486 ACCOUNT_VERBOSE("key = %s, app_id = %s \n", feature_data->key, app_id);
4488 ret = _account_query_bind_text(hstmt, count++, app_id);
4489 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4490 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4491 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4493 rc = _account_query_step(hstmt);
4495 if (rc != SQLITE_DONE) {
4496 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4500 _account_query_finalize(hstmt);
4505 ACCOUNT_VERBOSE( "_account_type_insert_provider_feature() DONE\n");
4507 return ACCOUNT_ERROR_NONE;
4510 ACCOUNT_API int account_type_insert_to_db(account_type_h account_type, int* account_type_id)
4512 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4514 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4515 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4516 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4518 account_type_s *data = (account_type_s*)account_type;
4520 pthread_mutex_lock(&account_mutex);
4523 /* transaction control required*/
4524 ret_transaction = _account_begin_transaction();
4526 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4527 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4528 pthread_mutex_unlock(&account_mutex);
4529 return ret_transaction;
4532 if (_account_type_check_duplicated(data)) {
4533 ret_transaction = _account_end_transaction(FALSE);
4534 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4535 *account_type_id = -1;
4536 pthread_mutex_unlock(&account_mutex);
4537 return ACCOUNT_ERROR_DUPLICATED;
4539 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4541 error_code = _account_type_execute_insert_query(data);
4543 if (error_code != ACCOUNT_ERROR_NONE){
4544 error_code = ACCOUNT_ERROR_DUPLICATED;
4545 ret_transaction = _account_end_transaction(FALSE);
4546 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4547 *account_type_id = -1;
4548 pthread_mutex_unlock(&account_mutex);
4553 ACCOUNT_INFO( "_account_type_execute_insert_query, insert error_code : %d", error_code);
4555 error_code = _account_type_insert_provider_feature(data, data->app_id);
4556 if(error_code != ACCOUNT_ERROR_NONE) {
4557 ret_transaction = _account_end_transaction(FALSE);
4558 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4559 pthread_mutex_unlock(&account_mutex);
4562 error_code = _account_type_insert_label(data);
4563 if(error_code != ACCOUNT_ERROR_NONE) {
4564 ret_transaction = _account_end_transaction(FALSE);
4565 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4566 pthread_mutex_unlock(&account_mutex);
4570 ret_transaction = _account_end_transaction(TRUE);
4571 ACCOUNT_DEBUG("Insert success(%x), commit insert query(%x)!!!!\n", error_code, ret_transaction);
4573 pthread_mutex_unlock(&account_mutex);
4575 return ACCOUNT_ERROR_NONE;
4578 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4581 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4582 account_stmt hstmt = NULL;
4584 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4586 if (g_slist_length( account_type->provider_feature_list)==0) {
4587 ACCOUNT_ERROR( "no feature\n");
4588 return ACCOUNT_ERROR_NONE;
4591 ACCOUNT_DEBUG( "app id", app_id);
4593 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4595 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4596 hstmt = _account_prepare_query(query);
4598 _account_query_bind_text(hstmt, count++, app_id);
4599 rc = _account_query_step(hstmt);
4601 if (rc != SQLITE_DONE) {
4602 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4603 return ACCOUNT_ERROR_DB_FAILED;
4605 _account_query_finalize(hstmt);
4610 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4613 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4614 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4615 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4617 hstmt = _account_prepare_query(query);
4619 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4621 provider_feature_s* feature_data = NULL;
4622 feature_data = (provider_feature_s*)iter->data;
4624 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4625 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4626 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4627 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4629 rc = _account_query_step(hstmt);
4631 if (rc != SQLITE_DONE) {
4632 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4635 _account_query_finalize(hstmt);
4639 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4641 return ACCOUNT_ERROR_NONE;
4644 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4647 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4648 account_stmt hstmt = NULL;
4650 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4652 if (g_slist_length( account_type->label_list)==0) {
4653 ACCOUNT_ERROR( "_account_type_update_label, no label\n");
4654 return ACCOUNT_ERROR_NONE;
4657 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4659 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4660 hstmt = _account_prepare_query(query);
4662 _account_query_bind_text(hstmt, count++, app_id);
4663 rc = _account_query_step(hstmt);
4665 if (rc != SQLITE_DONE) {
4666 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4667 return ACCOUNT_ERROR_DB_FAILED;
4669 _account_query_finalize(hstmt);
4674 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4677 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4678 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4679 "(?, ?, ?) ", LABEL_TABLE);
4681 hstmt = _account_prepare_query(query);
4683 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4685 label_s* label_data = NULL;
4686 label_data = (label_s*)iter->data;
4688 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4689 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4690 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4691 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4692 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
4693 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4695 rc = _account_query_step(hstmt);
4697 if (rc != SQLITE_DONE) {
4698 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4701 _account_query_finalize(hstmt);
4705 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4707 return ACCOUNT_ERROR_NONE;
4711 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
4713 int rc = 0, binding_count =1;
4714 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4715 int error_code = ACCOUNT_ERROR_NONE;
4716 account_stmt hstmt = NULL;
4718 if (!account_type->app_id) {
4719 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
4720 return ACCOUNT_ERROR_INVALID_PARAMETER;
4723 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4724 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
4725 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
4727 hstmt = _account_prepare_query(query);
4728 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
4730 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
4731 _account_query_bind_text(hstmt, binding_count++, app_id);
4733 rc = _account_query_step(hstmt);
4734 if (rc != SQLITE_DONE) {
4735 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4738 _account_query_finalize(hstmt);
4742 error_code = _account_type_update_label(account_type, app_id);
4743 /* update provider feature */
4744 error_code = _account_type_update_provider_feature(account_type, app_id);
4749 ACCOUNT_API int account_type_update_to_db_by_app_id(account_type_h account_type, const char* app_id)
4751 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
4752 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4753 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4755 int error_code = ACCOUNT_ERROR_NONE;
4756 account_type_s* data = (account_type_s*)account_type;
4758 pthread_mutex_lock(&account_mutex);
4760 error_code = _account_type_update_account(data, app_id);
4762 pthread_mutex_unlock(&account_mutex);
4767 ACCOUNT_API int account_type_delete_by_app_id(const char* app_id)
4769 int error_code = ACCOUNT_ERROR_NONE;
4770 account_stmt hstmt = NULL;
4771 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4772 int rc = 0, count = -1;
4773 int ret_transaction = 0;
4774 int binding_count = 1;
4775 bool is_success = FALSE;
4777 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4778 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
4780 /* Check requested ID to delete */
4781 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
4783 count = _account_get_record_count(query);
4785 ACCOUNT_ERROR("app id(%s) is not exist. count(%d)\n", app_id, count);
4786 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4789 /* transaction control required*/
4790 ret_transaction = _account_begin_transaction();
4792 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4793 ACCOUNT_FATAL("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4794 pthread_mutex_unlock(&account_mutex);
4795 return ret_transaction;
4798 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
4800 hstmt = _account_prepare_query(query);
4801 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4802 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4804 _account_query_bind_text(hstmt, binding_count++, app_id);
4806 rc = _account_query_step(hstmt);
4807 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4809 _account_query_finalize(hstmt);
4813 ACCOUNT_MEMSET(query, 0, sizeof(query));
4815 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
4817 hstmt = _account_prepare_query(query);
4818 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4819 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4821 _account_query_bind_text(hstmt, binding_count++, app_id);
4823 rc = _account_query_step(hstmt);
4824 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4826 _account_query_finalize(hstmt);
4832 ACCOUNT_MEMSET(query, 0, sizeof(query));
4834 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
4836 hstmt = _account_prepare_query(query);
4837 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4838 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4840 _account_query_bind_text(hstmt, binding_count++, app_id);
4842 rc = _account_query_step(hstmt);
4843 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4845 _account_query_finalize(hstmt);
4851 if (hstmt != NULL) {
4852 _account_query_finalize(hstmt);
4856 ret_transaction = _account_end_transaction(is_success);
4858 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4859 ACCOUNT_FATAL("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4862 pthread_mutex_unlock(&account_mutex);
4867 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
4869 char *textbuf = NULL;
4871 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
4873 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
4874 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
4876 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
4877 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
4879 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
4880 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
4882 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
4883 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
4885 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
4887 ACCOUNT_VERBOSE("END _account_type_convert_column_to_account_type");
4890 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
4892 char *textbuf = NULL;
4894 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
4895 _account_db_data_to_text(textbuf, &(label_record->app_id));
4897 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
4898 _account_db_data_to_text(textbuf, &(label_record->label));
4900 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
4901 _account_db_data_to_text(textbuf, &(label_record->locale));
4903 ACCOUNT_VERBOSE("END _account_type_convert_column_to_label");
4906 ACCOUNT_API int account_type_query_label_by_app_id(account_label_cb cb_func, const char* app_id, void *user_data )
4908 int error_code = ACCOUNT_ERROR_NONE;
4909 account_stmt hstmt = NULL;
4910 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4911 int rc = 0, binding_count = 1;
4913 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4914 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4915 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4917 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4919 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
4920 hstmt = _account_prepare_query(query);
4922 _account_query_bind_text(hstmt, binding_count++, app_id);
4924 rc = _account_query_step(hstmt);
4925 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4927 label_s* label_record = NULL;
4929 while (rc == SQLITE_ROW) {
4930 bool cb_ret = FALSE;
4931 label_record = (label_s*) malloc(sizeof(label_s));
4933 if (label_record == NULL) {
4934 ACCOUNT_FATAL("malloc Failed");
4938 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4940 _account_type_convert_column_to_label(hstmt, label_record);
4942 cb_ret = cb_func(label_record->app_id, label_record->label , label_record->locale, user_data);
4944 _account_type_free_label_items(label_record);
4945 _ACCOUNT_FREE(label_record);
4947 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4949 rc = _account_query_step(hstmt);
4952 _account_query_finalize(hstmt);
4955 error_code = ACCOUNT_ERROR_NONE;
4958 if (hstmt != NULL) {
4959 _account_query_finalize(hstmt);
4963 pthread_mutex_unlock(&account_mutex);
4967 int _account_type_label_get_app_id(label_h label, char **app_id)
4970 ACCOUNT_ERROR("(%s)-(%d) label handle is NULL.\n", __FUNCTION__, __LINE__);
4971 return ACCOUNT_ERROR_INVALID_PARAMETER;
4975 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4976 return ACCOUNT_ERROR_INVALID_PARAMETER;
4979 label_s *data = (label_s*)label;
4983 *app_id = _account_get_text(data->app_id);
4985 return ACCOUNT_ERROR_NONE;
4988 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
4990 account_type_s *data = (account_type_s*)user_data;
4992 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4994 if (label_data == NULL) {
4995 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
4998 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
5000 label_data->app_id = _account_get_text(app_id);
5001 label_data->label = _account_get_text(label);
5002 label_data->locale = _account_get_text(locale);
5004 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5006 ACCOUNT_VERBOSE("_account_get_label_text_cb :: appid=%s, label=%s\n", label_data->app_id, label_data->label);
5011 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5013 account_type_s *data = (account_type_s*)user_data;
5015 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5017 if (feature_data == NULL) {
5018 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5021 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5023 feature_data->app_id = _account_get_text(app_id);
5024 feature_data->key = _account_get_text(key);
5026 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5028 ACCOUNT_VERBOSE("appid=%s, key=%s\n", feature_data->app_id, feature_data->key);
5033 ACCOUNT_API int account_type_query_by_app_id(const char* app_id, account_type_h *account_type)
5035 int error_code = ACCOUNT_ERROR_NONE;
5036 account_stmt hstmt = NULL;
5037 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5038 int rc = 0, binding_count = 1;
5040 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5041 ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL"));
5042 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5044 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5046 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5047 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5048 hstmt = _account_prepare_query(query);
5050 _account_query_bind_text(hstmt, binding_count++, app_id);
5052 rc = _account_query_step(hstmt);
5053 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5055 account_type_s *account_type_record = (account_type_s *)(*account_type);
5057 while (rc == SQLITE_ROW) {
5058 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5059 ACCOUNT_DEBUG("get account info by id %p\n", account_type_record);
5060 rc = _account_query_step(hstmt);
5063 _account_query_finalize(hstmt);
5064 account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)account_type_record);
5065 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)account_type_record);
5068 error_code = ACCOUNT_ERROR_NONE;
5071 if (hstmt != NULL) {
5072 _account_query_finalize(hstmt);
5076 pthread_mutex_unlock(&account_mutex);
5080 ACCOUNT_API int account_type_query_app_id_exist(const char* app_id)
5082 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5085 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5086 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5088 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5090 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5092 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5093 rc = _account_get_record_count(query);
5096 ACCOUNT_DEBUG("(%s) not exist in account type db");
5097 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5100 return ACCOUNT_ERROR_NONE;
5103 ACCOUNT_API int account_type_query_by_provider_feature(account_type_cb cb_func, const char* key, void* user_data)
5105 int error_code = ACCOUNT_ERROR_NONE;
5106 account_stmt hstmt = NULL;
5107 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5109 GList *account_type_list = NULL;
5111 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
5112 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
5113 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5115 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5117 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5119 hstmt = _account_prepare_query(query);
5121 int binding_count = 1;
5122 _account_query_bind_text(hstmt, binding_count++, (char*)key);
5124 rc = _account_query_step(hstmt);
5126 account_type_s *account_type_record = NULL;
5128 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5131 while(rc == SQLITE_ROW) {
5132 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5134 if (account_type_record == NULL) {
5135 ACCOUNT_FATAL("malloc Failed");
5139 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5140 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5141 account_type_list = g_list_append(account_type_list, account_type_record);
5142 rc = _account_query_step(hstmt);
5146 _account_query_finalize(hstmt);
5151 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5152 account_type_s *account_type = NULL;
5153 account_type = (account_type_s*)iter->data;
5154 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5155 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5156 //cb_func((account_type_h)account_type, user_data);
5157 ACCOUNT_CATCH_ERROR(cb_func((account_type_h)account_type, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5161 error_code = ACCOUNT_ERROR_NONE;
5164 if (hstmt != NULL) {
5165 _account_query_finalize(hstmt);
5168 if (account_type_list) {
5169 _account_type_glist_free(account_type_list);
5170 account_type_list = NULL;
5177 ACCOUNT_API int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data)
5179 int error_code = ACCOUNT_ERROR_NONE;
5180 account_stmt hstmt = NULL;
5181 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5183 GList *account_type_list = NULL;
5185 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
5186 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5188 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5190 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5191 hstmt = _account_prepare_query(query);
5193 rc = _account_query_step(hstmt);
5195 account_type_s *account_type_record = NULL;
5197 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5200 while(rc == SQLITE_ROW) {
5201 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5203 if (account_type_record == NULL) {
5204 ACCOUNT_FATAL("malloc Failed");
5208 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5209 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5210 account_type_list = g_list_append(account_type_list, account_type_record);
5211 rc = _account_query_step(hstmt);
5215 _account_query_finalize(hstmt);
5220 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5221 account_type_s *account_type = NULL;
5222 account_type = (account_type_s*)iter->data;
5223 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5224 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5225 callback((account_type_h)account_type, user_data);
5229 error_code = ACCOUNT_ERROR_NONE;
5232 if (hstmt != NULL) {
5233 _account_query_finalize(hstmt);
5236 if (account_type_list) {
5237 _account_type_glist_free(account_type_list);
5238 account_type_list = NULL;
5244 // output parameter label must be free
5245 ACCOUNT_API int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label)
5247 int error_code = ACCOUNT_ERROR_NONE;
5248 account_stmt hstmt = NULL;
5249 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5250 int rc = 0, binding_count = 1;
5252 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5253 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5254 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5256 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5258 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, locale);
5259 hstmt = _account_prepare_query(query);
5261 _account_query_bind_text(hstmt, binding_count++, app_id);
5263 rc = _account_query_step(hstmt);
5264 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5266 label_s* label_record = NULL;
5268 while (rc == SQLITE_ROW) {
5269 label_record = (label_s*) malloc(sizeof(label_s));
5271 if (label_record == NULL) {
5272 ACCOUNT_FATAL("malloc Failed");
5276 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5278 _account_type_convert_column_to_label(hstmt,label_record);
5280 *label = _account_get_text(label_record->label);
5282 _account_type_free_label_items(label_record);
5283 _ACCOUNT_FREE(label_record);
5285 rc = _account_query_step(hstmt);
5288 _account_query_finalize(hstmt);
5291 error_code = ACCOUNT_ERROR_NONE;
5294 if (hstmt != NULL) {
5295 _account_query_finalize(hstmt);
5299 pthread_mutex_unlock(&account_mutex);
5303 static int _account_insert_custom(account_s *account, int account_id)
5306 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5307 account_stmt hstmt = NULL;
5309 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5311 if (g_slist_length( account->custom_list)==0) {
5312 ACCOUNT_ERROR( "_account_insert_custom, no custom data\n");
5313 return ACCOUNT_ERROR_NONE;
5316 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5318 rc = _account_get_record_count(query);
5321 ACCOUNT_ERROR( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5322 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5329 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5332 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5333 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5334 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5336 hstmt = _account_prepare_query(query);
5338 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5340 account_custom_s* custom_data = NULL;
5341 custom_data = (account_custom_s*)iter->data;
5342 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
5344 ret = _account_query_bind_int(hstmt, count++, account_id);
5345 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5346 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5347 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5348 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5349 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5350 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5351 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5353 rc = _account_query_step(hstmt);
5355 if (rc != SQLITE_DONE) {
5356 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5360 _account_query_finalize(hstmt);
5365 ACCOUNT_VERBOSE( "_account_insert_custom() DONE\n");
5367 return ACCOUNT_ERROR_NONE;
5370 static int _account_update_custom(account_s *account, int account_id)
5373 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5374 account_stmt hstmt = NULL;
5376 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5378 ACCOUNT_INFO( "account handle=%p, account_id=%d", account, account_id);
5380 if (g_slist_length( account->custom_list)==0) {
5381 ACCOUNT_ERROR( "_account_update_custom, no custom data\n");
5382 return ACCOUNT_ERROR_NONE;
5385 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5387 rc = _account_get_record_count(query);
5390 ACCOUNT_ERROR( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5391 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5394 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5396 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5397 hstmt = _account_prepare_query(query);
5399 _account_query_bind_int(hstmt, count++, (int)account_id);
5400 rc = _account_query_step(hstmt);
5402 if (rc != SQLITE_DONE) {
5403 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5404 return ACCOUNT_ERROR_DB_FAILED;
5406 _account_query_finalize(hstmt);
5411 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5414 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5415 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5416 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5418 hstmt = _account_prepare_query(query);
5420 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5422 account_custom_s* custom_data = NULL;
5423 custom_data = (account_custom_s*)iter->data;
5425 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5426 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5427 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5428 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5429 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5430 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5431 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5432 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5434 rc = _account_query_step(hstmt);
5436 if (rc != SQLITE_DONE) {
5437 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5441 _account_query_finalize(hstmt);
5446 ACCOUNT_VERBOSE( "_account_update_custom() DONE\n");
5448 return ACCOUNT_ERROR_NONE;
5451 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data )
5453 int error_code = ACCOUNT_ERROR_NONE;
5454 account_stmt hstmt = NULL;
5455 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5458 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5459 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5460 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5462 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5464 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5465 hstmt = _account_prepare_query(query);
5467 rc = _account_query_step(hstmt);
5468 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5470 account_custom_s* custom_record = NULL;
5472 while (rc == SQLITE_ROW) {
5473 bool cb_ret = FALSE;
5474 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5476 if (custom_record == NULL) {
5477 ACCOUNT_FATAL("malloc Failed");
5481 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5483 _account_convert_column_to_custom(hstmt, custom_record);
5485 cb_ret = cb_func(custom_record->key, custom_record->value, user_data);
5487 _account_custom_item_free(custom_record);
5488 _ACCOUNT_FREE(custom_record);
5490 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5492 rc = _account_query_step(hstmt);
5495 _account_query_finalize(hstmt);
5498 error_code = ACCOUNT_ERROR_NONE;
5501 if (hstmt != NULL) {
5502 _account_query_finalize(hstmt);
5506 pthread_mutex_unlock(&account_mutex);
5510 static void _account_subscribe_vconf_callback(keynode_t *key, void *user_data)
5512 account_subscribe_s* tmp = (account_subscribe_s*)user_data;
5513 char *msg = NULL, *vconf_key = NULL;
5514 char event_msg[256] ={0, };
5515 int account_id = -1;
5517 ACCOUNT_DEBUG("START\n");
5520 ACCOUNT_ERROR("No subscribtion msg !!!!!\n");
5525 ACCOUNT_ERROR("user data required\n");
5529 if(!memcmp(vconf_keynode_get_name(key), VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR)))
5531 vconf_key = vconf_keynode_get_str(key);
5532 msg = strdup(vconf_key);
5534 char* event_type = NULL;
5538 event_type = strtok_r(msg, ":", &ptr);
5539 id = strtok_r(NULL, ":", &ptr);
5541 ACCOUNT_DEBUG("msg(%s), event_type(%s), id(%s)\n", msg, event_type, id);
5543 ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type);
5545 account_id = atoi(id);
5547 if(tmp->account_subscription_callback)
5548 tmp->account_subscription_callback(event_msg, account_id, tmp->user_data);
5553 ACCOUNT_DEBUG("END\n");
5556 ACCOUNT_API int account_subscribe_create(account_subscribe_h* account_subscribe)
5558 if (!account_subscribe) {
5559 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
5560 return ACCOUNT_ERROR_INVALID_PARAMETER;
5563 account_subscribe_s *data = (account_subscribe_s*)calloc(1,sizeof(account_subscribe_s));
5566 ACCOUNT_FATAL("OUT OF MEMORY\n");
5567 return ACCOUNT_ERROR_OUT_OF_MEMORY;
5570 ACCOUNT_VERBOSE("create handle=%p\n", *data);
5572 *account_subscribe = (account_subscribe_h)data;
5574 return ACCOUNT_ERROR_NONE;
5577 ACCOUNT_API int account_subscribe_notification(account_subscribe_h account_subscribe, account_event_cb cb_func, void* user_data)
5579 ACCOUNT_DEBUG("START\n");
5581 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5583 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5585 tmp->account_subscription_callback = cb_func;
5586 tmp->user_data = user_data;
5588 if (vconf_notify_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5589 (vconf_callback_fn)_account_subscribe_vconf_callback,
5591 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5592 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5595 ACCOUNT_DEBUG("Vconf Subscription Success!!!!\n");
5596 return ACCOUNT_ERROR_NONE;
5599 ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_subscribe)
5601 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5603 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5607 if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5608 (vconf_callback_fn)_account_subscribe_vconf_callback) != 0) {
5609 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5610 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5613 ACCOUNT_DEBUG("Vconf Unsubscription Success!!!!\n");
5614 return ACCOUNT_ERROR_NONE;