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)
637 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
640 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
642 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and package_name='%s'"
643 , ACCOUNT_TABLE, data->user_name, data->package_name);
645 count = _account_get_record_count(query);
647 ACCOUNT_INFO("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
648 count, data->user_name, data->domain_name );
655 static int _account_get_next_sequence(char *pszName)
658 account_stmt pStmt = NULL;
660 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
662 ACCOUNT_VERBOSE( "[Enter] pszName:%s\n", pszName);
664 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
665 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
666 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
668 rc = sqlite3_step(pStmt);
669 max_seq = sqlite3_column_int(pStmt, 0);
672 ACCOUNT_VERBOSE( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
674 /*Finalize Statement*/
675 rc = sqlite3_finalize(pStmt);
681 static account_stmt _account_prepare_query(char *query)
684 account_stmt pStmt = NULL;
686 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
688 ACCOUNT_INFO( "prepare query : %s", query);
690 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
691 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
696 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
698 assert(NULL != pStmt);
700 return sqlite3_bind_int(pStmt, pos, num);
703 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
705 assert(NULL != pStmt);
708 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
710 return sqlite3_bind_null(pStmt, pos);
713 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
715 ACCOUNT_VERBOSE( "_account_convert_account_to_sql");
718 /*Caution : Keep insert query orders.*/
721 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
723 /* 2. email address*/
724 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
727 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
730 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
733 _account_query_bind_text(hstmt, count++, (char*)account->source);
736 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
739 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
742 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
745 _account_query_bind_int(hstmt, count++, account->auth_type);
748 _account_query_bind_int(hstmt, count++, account->secret);
750 /* 11. sync_support */
751 _account_query_bind_int(hstmt, count++, account->sync_support);
756 for(i=0; i< USER_TXT_CNT; i++)
757 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
759 /* 13. user integer */
760 for(i=0; i< USER_INT_CNT; i++)
761 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
766 static void _account_query_finalize(account_stmt pStmt)
771 ACCOUNT_FATAL( "pStmt is NULL");
775 rc = sqlite3_finalize(pStmt);
776 if (rc != SQLITE_OK) {
777 ACCOUNT_FATAL( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
780 ACCOUNT_VERBOSE( "sqlite3_finalize finish");
783 static int _account_query_step(account_stmt pStmt)
785 assert(NULL != pStmt);
786 return sqlite3_step(pStmt);
789 static int _account_execute_insert_query(account_s *account)
792 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
793 int error_code = ACCOUNT_ERROR_NONE;
794 account_stmt hstmt = NULL;
796 /* check whether app id exist in account type db */
798 if (!account->user_name && !account->display_name && !account->email_address) {
799 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
800 return ACCOUNT_ERROR_INVALID_PARAMETER;
803 /* Check account own icon existence */
804 if(!account->icon_path) {
805 char* temptxt = NULL;
806 account_type_h account_type = NULL;
807 error_code = account_type_create(&account_type);
808 error_code = account_type_query_by_app_id((const char*)account->package_name, &account_type);
809 error_code = account_type_get_icon_path(account_type, &temptxt);
810 if (error_code != ACCOUNT_ERROR_NONE) {
811 ACCOUNT_ERROR("account_type_get_icon_path: Failed \n");
812 } else if (temptxt && strlen(temptxt)) {
813 ACCOUNT_DEBUG("icon path (%s) app_id(%s) \n", temptxt, account->package_name);
814 account->icon_path = _account_get_text(temptxt);
816 ACCOUNT_DEBUG("account_get_icon_path: returned NULL \n");
818 _ACCOUNT_FREE(temptxt);
819 error_code = account_type_destroy(account_type);
822 /* End of Check account own icon existence */
824 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
825 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , "
826 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
827 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values "
828 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?)", ACCOUNT_TABLE);
830 hstmt = _account_prepare_query(query);
831 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
833 _account_convert_account_to_sql(account, hstmt, query);
835 rc = _account_query_step(hstmt);
836 if (rc != SQLITE_DONE) {
837 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
838 error_code = ACCOUNT_ERROR_DB_FAILED;
841 _account_query_finalize(hstmt);
847 static int _account_insert_capability(account_s *account, int account_id)
850 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
851 account_stmt hstmt = NULL;
853 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
855 if (g_slist_length( account->capablity_list)==0) {
856 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
857 return ACCOUNT_ERROR_NONE;
860 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
862 rc = _account_get_record_count(query);
865 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
866 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
873 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
877 account_capability_s* cap_data = NULL;
878 cap_data = (account_capability_s*)iter->data;
880 ACCOUNT_VERBOSE("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
882 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
883 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
884 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
885 hstmt = _account_prepare_query(query);
887 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
889 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
890 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
891 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
892 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
893 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
894 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
895 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
896 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
897 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
898 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
900 rc = _account_query_step(hstmt);
902 if (rc != SQLITE_DONE) {
903 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
907 _account_query_finalize(hstmt);
912 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
914 return ACCOUNT_ERROR_NONE;
917 static int _account_update_capability(account_s *account, int account_id)
920 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
921 account_stmt hstmt = NULL;
923 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
925 if (g_slist_length( account->capablity_list)==0) {
926 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
927 return ACCOUNT_ERROR_NONE;
930 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
932 rc = _account_get_record_count(query);
935 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
936 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
939 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
941 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
942 hstmt = _account_prepare_query(query);
944 _account_query_bind_int(hstmt, count++, (int)account_id);
945 rc = _account_query_step(hstmt);
947 if (rc != SQLITE_DONE) {
948 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
949 return ACCOUNT_ERROR_DB_FAILED;
951 _account_query_finalize(hstmt);
956 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
959 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
960 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
961 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
963 hstmt = _account_prepare_query(query);
965 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
967 account_capability_s* cap_data = NULL;
968 cap_data = (account_capability_s*)iter->data;
970 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
971 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
972 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
973 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
974 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
975 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
976 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
977 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
978 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
979 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
981 rc = _account_query_step(hstmt);
983 if (rc != SQLITE_DONE) {
984 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
988 _account_query_finalize(hstmt);
993 ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
995 return ACCOUNT_ERROR_NONE;
998 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
1001 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1002 account_stmt hstmt = NULL;
1004 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1006 if (g_slist_length( account->capablity_list)==0) {
1007 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
1008 return ACCOUNT_ERROR_NONE;
1011 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
1013 rc = _account_get_record_count(query);
1016 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1017 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1020 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1022 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1023 hstmt = _account_prepare_query(query);
1025 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1026 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1027 rc = _account_query_step(hstmt);
1028 if (rc != SQLITE_DONE) {
1029 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1030 return ACCOUNT_ERROR_DB_FAILED;
1033 _account_query_finalize(hstmt);
1038 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1041 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1042 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1043 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1045 hstmt = _account_prepare_query(query);
1047 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1049 account_capability_s* cap_data = NULL;
1050 cap_data = (account_capability_s*)iter->data;
1052 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1053 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1054 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1055 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1056 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1057 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1058 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1059 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1060 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1061 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1063 rc = _account_query_step(hstmt);
1065 if (rc != SQLITE_DONE) {
1066 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1070 _account_query_finalize(hstmt);
1075 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
1077 return ACCOUNT_ERROR_NONE;
1080 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1082 assert(NULL != pStmt);
1084 return sqlite3_column_int(pStmt, pos);
1087 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
1089 assert(NULL != pStmt);
1091 return (char *)sqlite3_column_text(pStmt, pos);
1094 static void _account_db_data_to_text(const char *textbuf, char **output)
1096 if (textbuf && strlen(textbuf)>0) {
1101 *output = strdup(textbuf);
1105 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1107 char *textbuf = NULL;
1109 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1111 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1112 _account_db_data_to_text(textbuf, &(account_record->user_name));
1114 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1115 _account_db_data_to_text(textbuf, &(account_record->email_address));
1117 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1118 _account_db_data_to_text(textbuf, &(account_record->display_name));
1120 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1121 _account_db_data_to_text(textbuf, &(account_record->icon_path));
1123 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1124 _account_db_data_to_text(textbuf, &(account_record->source));
1126 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1127 _account_db_data_to_text(textbuf, &(account_record->package_name));
1129 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1130 _account_db_data_to_text(textbuf, &(account_record->access_token));
1132 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1133 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1135 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1137 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1139 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1141 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1142 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1144 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1145 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1147 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1148 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1150 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1151 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1153 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1154 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1156 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1157 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1158 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1159 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1160 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1162 ACCOUNT_VERBOSE("END _account_convert_column_to_account");
1165 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1167 char *textbuf = NULL;
1169 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1171 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1172 _account_db_data_to_text(textbuf, &(capability_record->type));
1174 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1176 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1177 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1179 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1180 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1182 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1184 ACCOUNT_VERBOSE("END _account_convert_column_to_capability");
1187 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1189 char *textbuf = NULL;
1191 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1193 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1194 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1196 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1197 _account_db_data_to_text(textbuf, &(custom_record->key));
1199 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1200 _account_db_data_to_text(textbuf, &(custom_record->value));
1202 ACCOUNT_VERBOSE("END _account_convert_column_to_custom");
1205 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1207 account_s *data = (account_s*)user_data;
1209 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1211 if (cap_data == NULL)
1213 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1215 cap_data->type = _account_get_text(capability_type);
1216 cap_data->value = capability_value;
1218 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1220 ACCOUNT_VERBOSE("_account_get_capability_text_cb :: %s\n", capability_type);
1226 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1228 account_s *data = (account_s*)user_data;
1230 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1232 if (custom_data == NULL) {
1233 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1236 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1238 custom_data->account_id = data->id;
1239 custom_data->app_id = _account_get_text(data->package_name);
1240 custom_data->key = _account_get_text(key);
1241 custom_data->value = _account_get_text(value);
1243 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1249 static char *_account_get_text(const char *text_data)
1251 char *text_value = NULL;
1253 if (text_data != NULL) {
1254 text_value = strdup(text_data);
1255 ACCOUNT_VERBOSE("text_value = %s", text_value);
1260 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
1262 int error_code = ACCOUNT_ERROR_NONE;
1263 account_stmt hstmt = NULL;
1264 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1266 account_s *old_account = NULL;
1268 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1269 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1270 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1271 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1273 old_account = (account_s*)calloc(1, sizeof(account_s));
1275 ACCOUNT_FATAL("Memory alloc fail\n");
1276 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1279 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1281 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
1282 hstmt = _account_prepare_query(query);
1284 rc = _account_query_step(hstmt);
1285 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1287 while (rc == SQLITE_ROW) {
1288 _account_convert_column_to_account(hstmt, old_account);
1289 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
1290 rc = _account_query_step(hstmt);
1293 _account_query_finalize(hstmt);
1297 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1298 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1301 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1302 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1305 new_account->id = old_account->id;
1308 if(!new_account->user_name) {
1309 if(old_account->user_name)
1310 new_account->user_name = _account_get_text(old_account->user_name);
1314 if(!new_account->display_name) {
1315 if(old_account->display_name)
1316 new_account->display_name = _account_get_text(old_account->display_name);
1320 if(!new_account->email_address) {
1321 if(old_account->email_address)
1322 new_account->email_address = _account_get_text(old_account->email_address);
1326 if(!new_account->domain_name) {
1327 if(old_account->domain_name)
1328 new_account->domain_name = _account_get_text(old_account->domain_name);
1332 if(!new_account->icon_path) {
1333 if(old_account->icon_path)
1334 new_account->icon_path = _account_get_text(old_account->icon_path);
1338 if(!new_account->source) {
1339 if(old_account->source)
1340 new_account->source = _account_get_text(old_account->source);
1343 _ACCOUNT_FREE(new_account->package_name);
1344 new_account->package_name = _account_get_text(old_account->package_name);
1347 if(!new_account->access_token) {
1348 if(old_account->access_token)
1349 new_account->access_token = _account_get_text(old_account->access_token);
1354 for(i=0;i<USER_TXT_CNT;i++) {
1355 if(!new_account->user_data_txt[i]) {
1356 if(old_account->user_data_txt[i])
1357 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1362 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1363 new_account->auth_type = old_account->auth_type;
1367 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1368 new_account->secret = old_account->secret;
1372 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1373 new_account->sync_support = old_account->sync_support;
1377 for(i=0;i<USER_INT_CNT;i++) {
1378 if(new_account->user_data_int[i] == 0) {
1379 new_account->user_data_int[i] = old_account->user_data_int[i];
1385 // user custom table
1390 if (hstmt != NULL) {
1391 _account_query_finalize(hstmt);
1396 _account_free_account_items(old_account);
1397 _ACCOUNT_FREE(old_account);
1400 return ACCOUNT_ERROR_NONE;
1405 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
1407 int rc = 0, binding_count = 0, count = 0;
1408 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1409 int error_code = ACCOUNT_ERROR_NONE;
1410 account_stmt hstmt = NULL;
1412 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1413 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1415 char* current_appid = NULL;
1416 char* verified_appid = NULL;
1418 current_appid = _account_get_current_appid();
1419 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
1421 _ACCOUNT_FREE(current_appid);
1422 _ACCOUNT_FREE(verified_appid);
1424 if(error_code != ACCOUNT_ERROR_NONE){
1425 ACCOUNT_ERROR("No permission to update\n");
1426 return ACCOUNT_ERROR_PERMISSION_DENIED;
1429 _account_compare_old_record_by_user_name(account, user_name, package_name);
1431 if (!account->package_name) {
1432 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1433 return ACCOUNT_ERROR_INVALID_PARAMETER;
1436 if (!account->user_name && !account->display_name && !account->email_address) {
1437 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1438 return ACCOUNT_ERROR_INVALID_PARAMETER;
1441 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1442 , ACCOUNT_TABLE, user_name, package_name);
1444 count = _account_get_record_count(query);
1446 ACCOUNT_INFO("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1447 count, user_name, package_name);
1448 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1451 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1452 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1453 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1454 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1455 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1457 hstmt = _account_prepare_query(query);
1458 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1460 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1462 _account_query_bind_text(hstmt, binding_count++, user_name);
1463 _account_query_bind_text(hstmt, binding_count++, package_name);
1464 rc = _account_query_step(hstmt);
1465 if (rc != SQLITE_DONE) {
1466 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1468 _account_query_finalize(hstmt);
1471 /*update capability*/
1472 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1475 error_code = _account_update_custom(account, account->id);
1480 ACCOUNT_API int account_insert_to_db(account_h account, int *account_id)
1482 int error_code = ACCOUNT_ERROR_NONE;
1483 int ret_transaction = 0;
1485 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1486 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1487 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1489 account_s *data = (account_s*)account;
1491 pthread_mutex_lock(&account_mutex);
1493 /* transaction control required*/
1494 ret_transaction = _account_begin_transaction();
1496 if (ret_transaction != ACCOUNT_ERROR_NONE) {
1497 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
1498 pthread_mutex_unlock(&account_mutex);
1499 return ret_transaction;
1502 if (_account_check_duplicated(data)) {
1503 ret_transaction = _account_end_transaction(FALSE);
1504 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
1506 pthread_mutex_unlock(&account_mutex);
1507 return ACCOUNT_ERROR_DUPLICATED;
1509 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1512 appid = _account_get_current_appid();
1515 // API caller cannot be recognized
1516 ACCOUNT_ERROR("APP ID not detectable!\n");
1517 ret_transaction = _account_end_transaction(FALSE);
1518 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1519 pthread_mutex_unlock(&account_mutex);
1520 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1523 char* verified_appid = NULL;
1524 error_code = _account_check_account_type_with_appid_group(appid, &verified_appid);
1525 if(error_code != ACCOUNT_ERROR_NONE){
1526 ret_transaction = _account_end_transaction(FALSE);
1527 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1528 _ACCOUNT_FREE(verified_appid);
1529 _ACCOUNT_FREE(appid);
1530 pthread_mutex_unlock(&account_mutex);
1534 _ACCOUNT_FREE(appid);
1537 if(!_account_check_add_more_account(verified_appid)) {
1538 ret_transaction = _account_end_transaction(FALSE);
1539 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1540 pthread_mutex_unlock(&account_mutex);
1541 _ACCOUNT_FREE(verified_appid);
1542 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1545 _ACCOUNT_FREE(data->package_name);
1546 data->package_name = _account_get_text(verified_appid);
1547 _ACCOUNT_FREE(verified_appid);
1550 error_code = _account_execute_insert_query(data);
1552 if (error_code != ACCOUNT_ERROR_NONE) {
1553 ret_transaction = _account_end_transaction(FALSE);
1554 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1556 pthread_mutex_unlock(&account_mutex);
1561 ACCOUNT_VERBOSE( "_account_execute_insert_query, insert error_code : %d", error_code);
1563 error_code = _account_insert_capability(data, *account_id);
1564 if (error_code != ACCOUNT_ERROR_NONE) {
1565 ret_transaction = _account_end_transaction(FALSE);
1566 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1568 pthread_mutex_unlock(&account_mutex);
1572 error_code = _account_insert_custom(data, *account_id);
1573 if (error_code != ACCOUNT_ERROR_NONE) {
1574 ret_transaction = _account_end_transaction(FALSE);
1575 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1577 pthread_mutex_unlock(&account_mutex);
1581 pthread_mutex_unlock(&account_mutex);
1582 _account_end_transaction(TRUE);
1585 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1586 _account_insert_delete_update_notification_send(buf);
1588 return ACCOUNT_ERROR_NONE;
1592 ACCOUNT_API int account_create(account_h *account)
1595 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1596 return ACCOUNT_ERROR_INVALID_PARAMETER;
1599 account_s *data = (account_s*)malloc(sizeof(account_s));
1602 ACCOUNT_FATAL("Memory Allocation Failed");
1603 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1605 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1607 ACCOUNT_VERBOSE("create handle=%p\n", *account);
1609 /*Setting account as visible by default*/
1610 data->secret = ACCOUNT_SECRECY_VISIBLE;
1612 /*Setting account as not supporting sync by default*/
1613 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
1615 *account = (account_h)data;
1617 return ACCOUNT_ERROR_NONE;
1620 ACCOUNT_API int account_set_user_name(account_h account, const char *user_name)
1623 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1624 return ACCOUNT_ERROR_INVALID_PARAMETER;
1628 ACCOUNT_ERROR("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1629 return ACCOUNT_ERROR_INVALID_PARAMETER;
1632 account_s *data = (account_s*)account;
1634 _ACCOUNT_FREE(data->user_name);
1635 data->user_name = _account_get_text(user_name);
1637 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1639 return ACCOUNT_ERROR_NONE;
1642 ACCOUNT_API int account_set_display_name(account_h account, const char *display_name)
1645 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1646 return ACCOUNT_ERROR_INVALID_PARAMETER;
1649 if (!display_name) {
1650 ACCOUNT_ERROR("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1651 return ACCOUNT_ERROR_INVALID_PARAMETER;
1654 account_s *data = (account_s*)account;
1656 _ACCOUNT_FREE(data->display_name);
1657 data->display_name = _account_get_text(display_name);
1659 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1661 return ACCOUNT_ERROR_NONE;
1664 ACCOUNT_API int account_set_email_address(account_h account, const char *email_address)
1667 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1668 return ACCOUNT_ERROR_INVALID_PARAMETER;
1671 if (!email_address) {
1672 ACCOUNT_ERROR("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1673 return ACCOUNT_ERROR_INVALID_PARAMETER;
1676 account_s *data = (account_s*)account;
1678 _ACCOUNT_FREE(data->email_address);
1679 data->email_address = _account_get_text(email_address);
1681 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1683 return ACCOUNT_ERROR_NONE;
1686 ACCOUNT_API int account_set_icon_path(account_h account, const char *icon_path)
1689 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1690 return ACCOUNT_ERROR_INVALID_PARAMETER;
1694 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1695 return ACCOUNT_ERROR_INVALID_PARAMETER;
1698 account_s *data = (account_s*)account;
1700 _ACCOUNT_FREE(data->icon_path);
1701 data->icon_path = _account_get_text(icon_path);
1703 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1705 return ACCOUNT_ERROR_NONE;
1708 ACCOUNT_API int account_set_source(account_h account, const char *source)
1711 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1712 return ACCOUNT_ERROR_INVALID_PARAMETER;
1716 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1717 return ACCOUNT_ERROR_INVALID_PARAMETER;
1719 account_s *data = (account_s*)account;
1721 _ACCOUNT_FREE(data->source);
1722 data->source = _account_get_text(source);
1724 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1726 return ACCOUNT_ERROR_NONE;
1729 ACCOUNT_API int account_set_package_name(account_h account, const char *package_name)
1732 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1733 return ACCOUNT_ERROR_INVALID_PARAMETER;
1736 if (!package_name) {
1737 ACCOUNT_ERROR("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1738 return ACCOUNT_ERROR_INVALID_PARAMETER;
1741 account_s *data = (account_s*)account;
1743 _ACCOUNT_FREE(data->package_name);
1744 data->package_name = _account_get_text(package_name);
1746 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1748 return ACCOUNT_ERROR_NONE;
1751 ACCOUNT_API int account_set_domain_name(account_h account, const char *domain_name)
1754 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1755 return ACCOUNT_ERROR_INVALID_PARAMETER;
1759 ACCOUNT_ERROR("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1760 return ACCOUNT_ERROR_INVALID_PARAMETER;
1762 account_s *data = (account_s*)account;
1764 _ACCOUNT_FREE(data->domain_name);
1765 data->domain_name = _account_get_text(domain_name);
1767 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1769 return ACCOUNT_ERROR_NONE;
1772 ACCOUNT_API int account_set_access_token(account_h account, const char *access_token)
1775 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1776 return ACCOUNT_ERROR_INVALID_PARAMETER;
1779 if (!access_token) {
1780 ACCOUNT_ERROR("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1781 return ACCOUNT_ERROR_INVALID_PARAMETER;
1784 account_s *data = (account_s*)account;
1786 _ACCOUNT_FREE(data->access_token);
1787 data->access_token = _account_get_text(access_token);
1789 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1791 return ACCOUNT_ERROR_NONE;
1794 ACCOUNT_API int account_set_user_text(account_h account, int index, const char *user_txt)
1797 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1798 return ACCOUNT_ERROR_INVALID_PARAMETER;
1802 ACCOUNT_ERROR("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1803 return ACCOUNT_ERROR_INVALID_PARAMETER;
1805 if (index >= USER_TXT_CNT || index < 0) {
1806 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1807 return ACCOUNT_ERROR_INVALID_PARAMETER;
1810 account_s *data = (account_s*)account;
1812 _ACCOUNT_FREE(data->user_data_txt[index]);
1813 data->user_data_txt[index] = _account_get_text(user_txt);
1815 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1817 return ACCOUNT_ERROR_NONE;
1820 ACCOUNT_API int account_set_custom(account_h account, const char* key, const char* value)
1823 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1824 return ACCOUNT_ERROR_INVALID_PARAMETER;
1828 ACCOUNT_ERROR("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__);
1829 return ACCOUNT_ERROR_INVALID_PARAMETER;
1833 ACCOUNT_ERROR("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__);
1834 return ACCOUNT_ERROR_INVALID_PARAMETER;
1837 account_s *data = (account_s*)account;
1840 bool b_is_new = TRUE;
1842 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1844 account_custom_s* custom_data = NULL;
1845 custom_data = (account_custom_s*)iter->data;
1846 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
1848 if(!strcmp(custom_data->key, key)) {
1849 _ACCOUNT_FREE(custom_data->value);
1850 custom_data->value = _account_get_text(value);
1856 account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1858 if (custom_data == NULL) {
1859 ACCOUNT_FATAL("(%s)-(%d) MALLOC FAIL\n", __FUNCTION__, __LINE__);
1860 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1862 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1863 custom_data->account_id = data->id;
1864 custom_data->app_id = _account_get_text(data->package_name);
1865 custom_data->key = _account_get_text(key);
1866 custom_data->value = _account_get_text(value);
1867 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1870 return ACCOUNT_ERROR_NONE;
1873 ACCOUNT_API int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1875 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1877 if ( (auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
1878 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1879 return ACCOUNT_ERROR_INVALID_PARAMETER;
1882 account_s *data = (account_s*)account;
1884 data->auth_type = (int)auth_type;
1886 return ACCOUNT_ERROR_NONE;
1889 ACCOUNT_API int account_set_secret(account_h account, const account_secrecy_state_e secret)
1891 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1893 if ( (secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
1894 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1895 return ACCOUNT_ERROR_INVALID_PARAMETER;
1898 account_s *data = (account_s*)account;
1900 data->secret = (int)secret;
1902 return ACCOUNT_ERROR_NONE;
1905 ACCOUNT_API int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
1907 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1909 if ( (sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) {
1910 ACCOUNT_ERROR("(%s)-(%d) sync_support is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1911 return ACCOUNT_ERROR_INVALID_PARAMETER;
1914 account_s *data = (account_s*)account;
1916 data->sync_support= (int)sync_support;
1918 return ACCOUNT_ERROR_NONE;
1921 ACCOUNT_API int account_set_user_int(account_h account, int index, const int user_int)
1924 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1925 return ACCOUNT_ERROR_INVALID_PARAMETER;
1928 if (index >= USER_INT_CNT ||index < 0) {
1929 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1930 return ACCOUNT_ERROR_INVALID_PARAMETER;
1933 account_s *data = (account_s*)account;
1935 data->user_data_int[index] = user_int;
1937 return ACCOUNT_ERROR_NONE;
1940 ACCOUNT_API int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value)
1942 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null"));
1943 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null"));
1945 if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
1946 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1947 return ACCOUNT_ERROR_INVALID_PARAMETER;
1950 account_s *data = (account_s*)account;
1952 GSList *iter = NULL;
1953 bool b_is_new = TRUE;
1955 for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1956 account_capability_s *cap_data = NULL;
1957 cap_data = (account_capability_s*)iter->data;
1959 if(!strcmp(cap_data->type, capability_type)) {
1960 cap_data->value = capability_value;
1967 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1969 if (cap_data == NULL)
1970 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1971 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1973 cap_data->type = _account_get_text(capability_type);
1974 cap_data->value = capability_value;
1975 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1978 return ACCOUNT_ERROR_NONE;
1981 ACCOUNT_API int account_get_user_name(account_h account, char **user_name)
1984 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1985 return ACCOUNT_ERROR_INVALID_PARAMETER;
1989 ACCOUNT_ERROR("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
1990 return ACCOUNT_ERROR_INVALID_PARAMETER;
1993 account_s *data = (account_s*)account;
1995 (*user_name) = NULL;
1996 *user_name = _account_get_text(data->user_name);
1998 return ACCOUNT_ERROR_NONE;
2001 ACCOUNT_API int account_get_display_name(account_h account, char **display_name)
2004 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2005 return ACCOUNT_ERROR_INVALID_PARAMETER;
2008 if (!display_name) {
2009 ACCOUNT_ERROR("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
2010 return ACCOUNT_ERROR_INVALID_PARAMETER;
2013 account_s *data = (account_s*)account;
2015 (*display_name) = NULL;
2017 *display_name = _account_get_text(data->display_name);
2019 return ACCOUNT_ERROR_NONE;
2022 ACCOUNT_API int account_get_email_address(account_h account,char **email_address)
2025 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2026 return ACCOUNT_ERROR_INVALID_PARAMETER;
2029 if (!email_address) {
2030 ACCOUNT_ERROR("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
2031 return ACCOUNT_ERROR_INVALID_PARAMETER;
2034 account_s *data = (account_s*)account;
2036 (*email_address) = NULL;
2038 *email_address = _account_get_text(data->email_address);
2040 return ACCOUNT_ERROR_NONE;
2043 ACCOUNT_API int account_get_icon_path(account_h account, char **icon_path)
2046 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2047 return ACCOUNT_ERROR_INVALID_PARAMETER;
2051 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
2052 return ACCOUNT_ERROR_INVALID_PARAMETER;
2055 account_s *data = (account_s*)account;
2057 (*icon_path) = NULL;
2059 *icon_path = _account_get_text(data->icon_path);
2061 return ACCOUNT_ERROR_NONE;
2064 ACCOUNT_API int account_get_source(account_h account, char **source)
2067 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2068 return ACCOUNT_ERROR_INVALID_PARAMETER;
2072 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
2073 return ACCOUNT_ERROR_INVALID_PARAMETER;
2076 account_s *data = (account_s*)account;
2080 *source = _account_get_text(data->source);
2082 return ACCOUNT_ERROR_NONE;
2085 ACCOUNT_API int account_get_package_name(account_h account, char **package_name)
2088 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2089 return ACCOUNT_ERROR_INVALID_PARAMETER;
2092 if (!package_name) {
2093 ACCOUNT_ERROR("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
2094 return ACCOUNT_ERROR_INVALID_PARAMETER;
2097 account_s *data = (account_s*)account;
2099 (*package_name) = NULL;
2101 *package_name = _account_get_text(data->package_name);
2103 return ACCOUNT_ERROR_NONE;
2106 ACCOUNT_API int account_get_domain_name(account_h account, char **domain_name)
2109 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2110 return ACCOUNT_ERROR_INVALID_PARAMETER;
2114 ACCOUNT_ERROR("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
2115 return ACCOUNT_ERROR_INVALID_PARAMETER;
2118 account_s *data = (account_s*)account;
2120 (*domain_name) = NULL;
2122 *domain_name = _account_get_text(data->domain_name);
2124 return ACCOUNT_ERROR_NONE;
2127 ACCOUNT_API int account_get_access_token(account_h account, char **access_token)
2130 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2131 return ACCOUNT_ERROR_INVALID_PARAMETER;
2134 if (!access_token) {
2135 ACCOUNT_ERROR("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
2136 return ACCOUNT_ERROR_INVALID_PARAMETER;
2139 account_s *data = (account_s*)account;
2141 (*access_token) = NULL;
2143 *access_token = _account_get_text(data->access_token);
2145 return ACCOUNT_ERROR_NONE;
2148 ACCOUNT_API int account_get_user_text(account_h account, int user_text_index, char **text)
2151 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2152 return ACCOUNT_ERROR_INVALID_PARAMETER;
2156 ACCOUNT_ERROR("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
2157 return ACCOUNT_ERROR_INVALID_PARAMETER;
2159 ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2161 account_s *data = (account_s*)account;
2165 *text = _account_get_text(data->user_data_txt[user_text_index]);
2167 return ACCOUNT_ERROR_NONE;
2170 ACCOUNT_API int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
2173 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2174 return ACCOUNT_ERROR_INVALID_PARAMETER;
2177 ACCOUNT_ERROR("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
2178 return ACCOUNT_ERROR_INVALID_PARAMETER;
2181 account_s* data = (account_s*)account;
2183 *auth_type = data->auth_type;
2185 return ACCOUNT_ERROR_NONE;
2188 ACCOUNT_API int account_get_secret(account_h account, account_secrecy_state_e *secret)
2191 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2192 return ACCOUNT_ERROR_INVALID_PARAMETER;
2195 ACCOUNT_ERROR("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
2196 return ACCOUNT_ERROR_INVALID_PARAMETER;
2199 account_s* data = (account_s*)account;
2201 *secret = data->secret;
2203 return ACCOUNT_ERROR_NONE;
2206 ACCOUNT_API int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
2209 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2210 return ACCOUNT_ERROR_INVALID_PARAMETER;
2212 if (!sync_support) {
2213 ACCOUNT_ERROR("(%s)-(%d) sync_support is NULL.\n", __FUNCTION__, __LINE__);
2214 return ACCOUNT_ERROR_INVALID_PARAMETER;
2217 account_s* data = (account_s*)account;
2219 *sync_support = data->sync_support;
2221 return ACCOUNT_ERROR_NONE;
2224 ACCOUNT_API int account_get_account_id(account_h account, int *account_id)
2227 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2228 return ACCOUNT_ERROR_INVALID_PARAMETER;
2231 ACCOUNT_ERROR("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
2232 return ACCOUNT_ERROR_INVALID_PARAMETER;
2235 account_s *data = (account_s*)account;
2237 *account_id = data->id;
2239 return ACCOUNT_ERROR_NONE;
2242 ACCOUNT_API int account_get_user_int(account_h account, int user_int_index, int *integer)
2245 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
2246 return ACCOUNT_ERROR_INVALID_PARAMETER;
2249 ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
2252 ACCOUNT_ERROR("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
2253 return ACCOUNT_ERROR_INVALID_PARAMETER;
2256 account_s *data = (account_s*)account;
2258 *integer = data->user_data_int[user_int_index];
2260 return ACCOUNT_ERROR_NONE;
2263 ACCOUNT_API int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value)
2265 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2266 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL"));
2267 ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL"));
2270 account_s *data = (account_s*)account;
2272 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2273 account_capability_s *cap_data = NULL;
2275 cap_data = (account_capability_s*)iter->data;
2277 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2279 if(!strcmp(capability_type, cap_data->type)) {
2280 *capability_value = cap_data->value;
2281 return ACCOUNT_ERROR_NONE;
2285 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2288 ACCOUNT_API int account_get_capability_all(account_h account, capability_cb cb_func, void *user_data)
2290 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2291 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2294 account_s *data = (account_s*)account;
2296 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2297 account_capability_s *cap_data = NULL;
2299 cap_data = (account_capability_s*)iter->data;
2301 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
2303 //cb_func(cap_data->type, cap_data->value, user_data);
2304 if(cb_func(cap_data->type, cap_data->value, user_data)!=TRUE){
2305 ACCOUNT_VERBOSE("account_get_capability :: cb_func returns false, it is stopped\n");
2306 return ACCOUNT_ERROR_NONE;
2310 return ACCOUNT_ERROR_NONE;
2313 ACCOUNT_API int account_get_custom(account_h account, const char* key, char** value)
2315 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2316 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST"));
2317 ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL"));
2320 account_s *data = (account_s*)account;
2322 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2323 account_custom_s *custom_data = NULL;
2325 custom_data = (account_custom_s*)iter->data;
2327 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2329 if(!strcmp(key, custom_data->key)) {
2331 *value = _account_get_text(custom_data->value);
2332 return ACCOUNT_ERROR_NONE;
2336 ACCOUNT_INFO("key is not found %s", key);
2338 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2341 ACCOUNT_API int account_get_custom_all(account_h account, account_custom_cb cb_func, void* user_data)
2343 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2344 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2347 account_s *data = (account_s*)account;
2349 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2350 bool cb_ret = FALSE;
2351 account_custom_s *custom_data = NULL;
2353 custom_data = (account_custom_s*)iter->data;
2355 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
2357 cb_ret = cb_func(custom_data->key, custom_data->value, user_data);
2359 ACCOUNT_INFO("account_get_custom_all callback func ret = %d", cb_ret);
2364 return ACCOUNT_ERROR_NONE;
2367 ACCOUNT_API int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
2369 int error_code = ACCOUNT_ERROR_NONE;
2370 account_stmt hstmt = NULL;
2371 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2374 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2375 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2376 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2378 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2380 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2381 hstmt = _account_prepare_query(query);
2383 rc = _account_query_step(hstmt);
2384 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2386 account_capability_s* capability_record = NULL;
2388 while (rc == SQLITE_ROW) {
2389 bool cb_ret = FALSE;
2390 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2392 if (capability_record == NULL) {
2393 ACCOUNT_FATAL("malloc Failed");
2397 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2399 _account_convert_column_to_capability(hstmt, capability_record);
2401 cb_ret = cb_func(capability_record->type, capability_record->value, user_data);
2403 _account_free_capability_items(capability_record);
2404 _ACCOUNT_FREE(capability_record);
2406 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2408 rc = _account_query_step(hstmt);
2411 _account_query_finalize(hstmt);
2414 error_code = ACCOUNT_ERROR_NONE;
2417 if (hstmt != NULL) {
2418 _account_query_finalize(hstmt);
2422 pthread_mutex_unlock(&account_mutex);
2426 static int _account_compare_old_record(account_s *new_account, int account_id)
2428 int error_code = ACCOUNT_ERROR_NONE;
2429 account_stmt hstmt = NULL;
2430 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2432 account_s *old_account = NULL;
2434 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2435 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2436 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2438 old_account = (account_s*)calloc(1, sizeof(account_s));
2440 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2442 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2443 hstmt = _account_prepare_query(query);
2445 rc = _account_query_step(hstmt);
2446 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2448 while (rc == SQLITE_ROW) {
2449 _account_convert_column_to_account(hstmt, old_account);
2450 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
2451 rc = _account_query_step(hstmt);
2454 _account_query_finalize(hstmt);
2458 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2459 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2462 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2463 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2467 new_account->id = old_account->id;
2470 if(!new_account->user_name) {
2471 if(old_account->user_name)
2472 new_account->user_name = _account_get_text(old_account->user_name);
2476 if(!new_account->display_name) {
2477 if(old_account->display_name)
2478 new_account->display_name = _account_get_text(old_account->display_name);
2482 if(!new_account->email_address) {
2483 if(old_account->email_address)
2484 new_account->email_address = _account_get_text(old_account->email_address);
2488 if(!new_account->domain_name) {
2489 if(old_account->domain_name)
2490 new_account->domain_name = _account_get_text(old_account->domain_name);
2494 if(!new_account->icon_path) {
2495 if(old_account->icon_path)
2496 new_account->icon_path = _account_get_text(old_account->icon_path);
2500 if(!new_account->source) {
2501 if(old_account->source)
2502 new_account->source = _account_get_text(old_account->source);
2505 _ACCOUNT_FREE(new_account->package_name);
2506 new_account->package_name = _account_get_text(old_account->package_name);
2509 if(!new_account->access_token) {
2510 if(old_account->access_token)
2511 new_account->access_token = _account_get_text(old_account->access_token);
2516 for(i=0;i<USER_TXT_CNT;i++) {
2517 if(!new_account->user_data_txt[i]) {
2518 if(old_account->user_data_txt[i])
2519 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2524 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2525 new_account->auth_type = old_account->auth_type;
2529 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2530 new_account->secret = old_account->secret;
2534 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2535 new_account->sync_support = old_account->sync_support;
2539 for(i=0;i<USER_INT_CNT;i++) {
2540 if(new_account->user_data_int[i] == 0) {
2541 new_account->user_data_int[i] = old_account->user_data_int[i];
2547 // user custom table
2552 if (hstmt != NULL) {
2553 _account_query_finalize(hstmt);
2558 _account_free_account_items(old_account);
2559 _ACCOUNT_FREE(old_account);
2562 return ACCOUNT_ERROR_NONE;
2565 static int _account_update_account(account_s *account, int account_id)
2567 int rc = 0, binding_count =0;
2568 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2569 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2570 account_stmt hstmt = NULL;
2572 if (!account->package_name) {
2573 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2574 return ACCOUNT_ERROR_INVALID_PARAMETER;
2577 char* current_appid = NULL;
2578 char* verified_appid = NULL;
2580 current_appid = _account_get_current_appid();
2581 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
2583 _ACCOUNT_FREE(current_appid);
2584 _ACCOUNT_FREE(verified_appid);
2586 if(error_code != ACCOUNT_ERROR_NONE){
2587 ACCOUNT_ERROR("No permission to update\n");
2588 return ACCOUNT_ERROR_PERMISSION_DENIED;
2591 _account_compare_old_record(account, account_id);
2593 if (!account->user_name && !account->display_name && !account->email_address) {
2594 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2595 return ACCOUNT_ERROR_INVALID_PARAMETER;
2598 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2600 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2602 count = _account_get_record_count(query);
2604 ACCOUNT_WARNING(" Account record not found, count = %d\n", count);
2605 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2608 /* transaction control required*/
2609 ret_transaction = _account_begin_transaction();
2611 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2612 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2613 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2614 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2615 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2617 hstmt = _account_prepare_query(query);
2618 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)));
2620 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2621 _account_query_bind_int(hstmt, binding_count++, account_id);
2623 rc = _account_query_step(hstmt);
2624 if (rc != SQLITE_DONE) {
2625 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2628 _account_query_finalize(hstmt);
2631 /*update capability*/
2632 error_code = _account_update_capability(account, account_id);
2633 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2634 ret_transaction = _account_end_transaction(FALSE);
2635 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2640 error_code = _account_update_custom(account, account_id);
2641 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2642 ret_transaction = _account_end_transaction(FALSE);
2643 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2647 ret_transaction = _account_end_transaction(TRUE);
2648 ACCOUNT_DEBUG("update capability success, trying to commit(%x) !!!\n", ret_transaction);
2653 ACCOUNT_API int account_update_to_db_by_id(const account_h account, int account_id)
2655 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2656 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2657 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2658 int error_code = ACCOUNT_ERROR_NONE;
2659 account_s* data = (account_s*)account;
2661 pthread_mutex_lock(&account_mutex);
2663 error_code = _account_update_account(data, account_id);
2665 if(error_code != ACCOUNT_ERROR_NONE) {
2666 pthread_mutex_unlock(&account_mutex);
2670 pthread_mutex_unlock(&account_mutex);
2673 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2674 _account_insert_delete_update_notification_send(buf);
2676 return ACCOUNT_ERROR_NONE;
2679 ACCOUNT_API int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
2681 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2682 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2683 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2684 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2686 int error_code = ACCOUNT_ERROR_NONE;
2687 account_s *data = (account_s*)account;
2689 pthread_mutex_lock(&account_mutex);
2691 error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
2693 pthread_mutex_unlock(&account_mutex);
2696 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2697 _account_insert_delete_update_notification_send(buf);
2702 ACCOUNT_API int account_foreach_account_from_db(account_cb callback, void *user_data)
2704 int error_code = ACCOUNT_ERROR_NONE;
2705 account_stmt hstmt = NULL;
2706 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2708 GList *account_list = NULL;
2710 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
2711 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2713 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2715 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2716 hstmt = _account_prepare_query(query);
2718 rc = _account_query_step(hstmt);
2720 account_s *account_record = NULL;
2722 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2725 while(rc == SQLITE_ROW) {
2726 account_record = (account_s*) malloc(sizeof(account_s));
2728 if (account_record == NULL) {
2729 ACCOUNT_FATAL("malloc Failed");
2733 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2734 _account_convert_column_to_account(hstmt, account_record);
2735 account_list = g_list_append(account_list, account_record);
2736 rc = _account_query_step(hstmt);
2740 _account_query_finalize(hstmt);
2745 for (iter = account_list; iter != NULL; iter = g_list_next(iter)) {
2746 account_s *account = NULL;
2747 account = (account_s*)iter->data;
2748 account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2749 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2750 callback((account_h)account, user_data);
2754 error_code = ACCOUNT_ERROR_NONE;
2757 if (hstmt != NULL) {
2758 _account_query_finalize(hstmt);
2762 _account_glist_free(account_list);
2763 account_list = NULL;
2769 ACCOUNT_API int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status)
2771 int error_code = ACCOUNT_ERROR_NONE;
2772 account_stmt hstmt = NULL;
2773 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2777 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2778 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2779 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2780 ACCOUNT_ERROR("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2781 return ACCOUNT_ERROR_INVALID_PARAMETER;
2784 pthread_mutex_lock(&account_mutex);
2786 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2788 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2790 rc = _account_get_record_count(query);
2793 ACCOUNT_ERROR( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2794 pthread_mutex_unlock(&account_mutex);
2795 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2798 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2800 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2801 hstmt = _account_prepare_query(query);
2803 _account_query_bind_int(hstmt, count, (int)sync_status);
2805 rc = _account_query_step(hstmt);
2807 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2808 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2810 _account_query_finalize(hstmt);
2813 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2814 _account_insert_delete_update_notification_send(buf);
2817 error_code = ACCOUNT_ERROR_NONE;
2820 if (hstmt != NULL) {
2821 _account_query_finalize(hstmt);
2825 pthread_mutex_unlock(&account_mutex);
2829 ACCOUNT_API int account_query_account_by_account_id(int account_db_id, account_h *account)
2831 int error_code = ACCOUNT_ERROR_NONE;
2832 account_stmt hstmt = NULL;
2833 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2836 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2837 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2838 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2840 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2842 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2843 hstmt = _account_prepare_query(query);
2845 rc = _account_query_step(hstmt);
2846 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2848 account_s *account_record = (account_s *)(*account);
2850 while (rc == SQLITE_ROW) {
2851 _account_convert_column_to_account(hstmt, account_record);
2852 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
2853 rc = _account_query_step(hstmt);
2856 _account_query_finalize(hstmt);
2857 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2858 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2861 error_code = ACCOUNT_ERROR_NONE;
2864 if (hstmt != NULL) {
2865 _account_query_finalize(hstmt);
2869 pthread_mutex_unlock(&account_mutex);
2873 ACCOUNT_API int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
2875 int error_code = ACCOUNT_ERROR_NONE;
2876 account_stmt hstmt = NULL;
2877 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2880 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2881 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2883 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2885 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2887 hstmt = _account_prepare_query(query);
2889 int binding_count = 1;
2890 _account_query_bind_text(hstmt, binding_count++, user_name);
2892 rc = _account_query_step(hstmt);
2894 account_s *account_head = NULL;
2896 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2900 account_head = (account_s*) malloc(sizeof(account_s));
2901 if (account_head == NULL) {
2902 ACCOUNT_FATAL("malloc Failed");
2903 if (hstmt != NULL) {
2904 _account_query_finalize(hstmt);
2907 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2909 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2911 while (rc == SQLITE_ROW) {
2912 account_s* account_record = NULL;
2914 account_record = (account_s*) malloc(sizeof(account_s));
2916 if (account_record == NULL) {
2917 ACCOUNT_FATAL("malloc Failed");
2920 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2922 _account_convert_column_to_account(hstmt, account_record);
2924 account_head->account_list = g_list_append(account_head->account_list, account_record);
2926 rc = _account_query_step(hstmt);
2928 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2931 _account_query_finalize(hstmt);
2937 tmp = g_list_length(account_head->account_list);
2938 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2940 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2942 account = (account_h)iter->data;
2944 account_s *testaccount = (account_s*)account;
2946 ACCOUNT_VERBOSE("id = %d", testaccount->id);
2947 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
2948 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
2949 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
2950 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
2951 ACCOUNT_VERBOSE("source = %s", testaccount->source);
2952 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
2953 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
2954 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
2955 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
2956 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
2957 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
2959 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2960 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2962 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2964 //cb_func(account, user_data);
2965 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("cb_func returns false, it is stopped.\n"));
2969 error_code = ACCOUNT_ERROR_NONE;
2972 if (hstmt != NULL) {
2973 _account_query_finalize(hstmt);
2977 if (account_head->account_list) {
2978 _account_glist_free(account_head->account_list);
2979 account_head->account_list = NULL;
2980 _account_free_account_items(account_head);
2982 _ACCOUNT_FREE(account_head);
2985 pthread_mutex_unlock(&account_mutex);
2989 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)
2991 int error_code = ACCOUNT_ERROR_NONE;
2992 account_stmt hstmt = NULL;
2993 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2996 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
2998 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
2999 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
3000 return ACCOUNT_ERROR_INVALID_PARAMETER;
3003 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3004 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3006 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3008 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3010 hstmt = _account_prepare_query(query);
3012 int binding_count = 1;
3013 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3014 _account_query_bind_int(hstmt, binding_count++, (int)capability_value);
3016 rc = _account_query_step(hstmt);
3018 account_s* account_head = NULL;
3020 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3024 account_head = (account_s*) malloc(sizeof(account_s));
3025 if (account_head == NULL) {
3026 ACCOUNT_FATAL("malloc Failed");
3027 if (hstmt != NULL) {
3028 _account_query_finalize(hstmt);
3031 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3033 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3035 while (rc == SQLITE_ROW) {
3036 account_s* account_record = NULL;
3038 account_record = (account_s*) malloc(sizeof(account_s));
3040 if (account_record == NULL) {
3041 ACCOUNT_FATAL("malloc Failed");
3044 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3046 _account_convert_column_to_account(hstmt, account_record);
3048 account_head->account_list = g_list_append(account_head->account_list, account_record);
3050 rc = _account_query_step(hstmt);
3052 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3055 _account_query_finalize(hstmt);
3061 tmp = g_list_length(account_head->account_list);
3062 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3064 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3065 account_h account = NULL;
3066 account = (account_h)iter->data;
3067 account_s* testaccount = (account_s*)account;
3069 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3070 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3072 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3074 //cb_func(account, user_data);
3075 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("cb_func returns false, it is stopped.\n"));
3079 error_code = ACCOUNT_ERROR_NONE;
3082 if (hstmt != NULL) {
3083 _account_query_finalize(hstmt);
3087 if (account_head->account_list) {
3088 _account_glist_free(account_head->account_list);
3089 account_head->account_list = NULL;
3090 _account_free_account_items(account_head);
3092 _ACCOUNT_FREE(account_head);
3095 pthread_mutex_unlock(&account_mutex);
3099 ACCOUNT_API int account_query_account_by_capability_type(account_cb cb_func, const char* capability_type, void* user_data)
3101 int error_code = ACCOUNT_ERROR_NONE;
3102 account_stmt hstmt = NULL;
3103 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3106 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
3107 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3108 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3110 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3112 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3114 hstmt = _account_prepare_query(query);
3116 int binding_count = 1;
3117 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
3119 rc = _account_query_step(hstmt);
3121 account_s* account_head = NULL;
3123 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3127 account_head = (account_s*) malloc(sizeof(account_s));
3128 if (account_head == NULL) {
3129 ACCOUNT_FATAL("malloc Failed");
3130 if (hstmt != NULL) {
3131 _account_query_finalize(hstmt);
3134 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3136 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3138 while (rc == SQLITE_ROW) {
3139 account_s* account_record = NULL;
3141 account_record = (account_s*) malloc(sizeof(account_s));
3143 if (account_record == NULL) {
3144 ACCOUNT_FATAL("malloc Failed");
3147 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3149 _account_convert_column_to_account(hstmt, account_record);
3151 account_head->account_list = g_list_append(account_head->account_list, account_record);
3153 rc = _account_query_step(hstmt);
3155 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3158 _account_query_finalize(hstmt);
3164 tmp = g_list_length(account_head->account_list);
3165 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
3167 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3168 account_h account = NULL;
3169 account = (account_h)iter->data;
3170 account_s* testaccount = (account_s*)account;
3172 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3173 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3175 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3177 //cb_func(account, user_data);
3178 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n"));
3182 error_code = ACCOUNT_ERROR_NONE;
3185 if (hstmt != NULL) {
3186 _account_query_finalize(hstmt);
3190 if (account_head->account_list) {
3191 _account_glist_free(account_head->account_list);
3192 account_head->account_list = NULL;
3193 _account_free_account_items(account_head);
3195 _ACCOUNT_FREE(account_head);
3198 pthread_mutex_unlock(&account_mutex);
3202 ACCOUNT_API int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
3204 int error_code = ACCOUNT_ERROR_NONE;
3205 account_stmt hstmt = NULL;
3206 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3209 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
3210 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
3211 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3213 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3215 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3217 hstmt = _account_prepare_query(query);
3219 int binding_count = 1;
3220 _account_query_bind_text(hstmt, binding_count++, package_name);
3222 rc = _account_query_step(hstmt);
3224 account_s* account_head = NULL;
3226 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3230 account_head = (account_s*) malloc(sizeof(account_s));
3231 if (account_head == NULL) {
3232 ACCOUNT_FATAL("malloc Failed");
3233 if (hstmt != NULL) {
3234 _account_query_finalize(hstmt);
3237 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3239 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3241 while (rc == SQLITE_ROW) {
3242 account_s* account_record = NULL;
3244 account_record = (account_s*) malloc(sizeof(account_s));
3246 if (account_record == NULL) {
3247 ACCOUNT_FATAL("malloc Failed");
3250 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3252 _account_convert_column_to_account(hstmt, account_record);
3254 account_head->account_list = g_list_append(account_head->account_list, account_record);
3256 rc = _account_query_step(hstmt);
3258 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
3261 _account_query_finalize(hstmt);
3266 tmp = g_list_length(account_head->account_list);
3267 ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
3269 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3270 account_h account = NULL;
3271 account = (account_h)iter->data;
3273 account_s* testaccount = (account_s*)account;
3275 ACCOUNT_VERBOSE("id = %d", testaccount->id);
3276 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
3277 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
3278 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
3279 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
3280 ACCOUNT_VERBOSE("source = %s", testaccount->source);
3281 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
3282 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
3283 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
3284 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
3285 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
3286 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
3288 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3289 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3291 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
3293 //cb_func(account, user_data);
3294 ACCOUNT_CATCH_ERROR(cb_func(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n"));
3298 error_code = ACCOUNT_ERROR_NONE;
3301 if (hstmt != NULL) {
3302 _account_query_finalize(hstmt);
3306 if (account_head->account_list) {
3307 _account_glist_free(account_head->account_list);
3308 account_head->account_list = NULL;
3309 _account_free_account_items(account_head);
3311 _ACCOUNT_FREE(account_head);
3314 pthread_mutex_unlock(&account_mutex);
3318 ACCOUNT_API int account_delete(int account_id)
3320 int error_code = ACCOUNT_ERROR_NONE;
3321 account_stmt hstmt = NULL;
3322 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3324 int ret_transaction = 0;
3325 bool is_success = FALSE;
3327 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3330 account_h account = NULL;
3331 /* Check requested ID to delete */
3332 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3334 count = _account_get_record_count(query);
3336 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3337 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3340 /* Check permission of requested appid */
3341 account_create(&account);
3342 account_query_account_by_account_id(account_id, &account);
3344 char* current_appid = NULL;
3345 char* verified_appid = NULL;
3347 current_appid = _account_get_current_appid();
3348 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
3350 _ACCOUNT_FREE(current_appid);
3351 _ACCOUNT_FREE(verified_appid);
3353 if(error_code != ACCOUNT_ERROR_NONE){
3354 ACCOUNT_ERROR("No permission to delete\n");
3355 return ACCOUNT_ERROR_PERMISSION_DENIED;
3358 account_destroy(account);
3360 /* transaction control required*/
3361 ret_transaction = _account_begin_transaction();
3363 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3364 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3365 pthread_mutex_unlock(&account_mutex);
3366 return ret_transaction;
3369 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3370 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3372 hstmt = _account_prepare_query(query);
3373 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3374 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3376 rc = _account_query_step(hstmt);
3377 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3379 _account_query_finalize(hstmt);
3382 ACCOUNT_MEMSET(query, 0, sizeof(query));
3384 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3386 hstmt = _account_prepare_query(query);
3387 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3388 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3390 rc = _account_query_step(hstmt);
3391 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3393 _account_query_finalize(hstmt);
3396 /* delete custom data */
3397 ACCOUNT_MEMSET(query, 0, sizeof(query));
3399 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3401 hstmt = _account_prepare_query(query);
3402 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3403 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3405 rc = _account_query_step(hstmt);
3406 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3408 _account_query_finalize(hstmt);
3413 if (hstmt != NULL) {
3414 _account_query_finalize(hstmt);
3418 ret_transaction = _account_end_transaction(is_success);
3420 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3421 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3423 if (is_success == true) {
3425 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3426 _account_insert_delete_update_notification_send(buf);
3430 pthread_mutex_unlock(&account_mutex);
3436 ACCOUNT_API int account_delete_from_db_by_id(int account_db_id)
3438 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
3440 return account_delete(account_db_id);
3443 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3445 int error_code = ACCOUNT_ERROR_NONE;
3446 account_stmt hstmt = NULL;
3447 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3449 int binding_count = 1;
3451 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3452 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3453 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3454 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3456 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3458 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3459 hstmt = _account_prepare_query(query);
3461 _account_query_bind_text(hstmt, binding_count++, username);
3462 _account_query_bind_text(hstmt, binding_count++, package_name);
3464 rc = _account_query_step(hstmt);
3465 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3467 account_s *account_record = (account_s *)(*account);
3469 while (rc == SQLITE_ROW) {
3470 _account_convert_column_to_account(hstmt, account_record);
3471 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
3472 rc = _account_query_step(hstmt);
3475 _account_query_finalize(hstmt);
3476 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3477 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3480 error_code = ACCOUNT_ERROR_NONE;
3483 if (hstmt != NULL) {
3484 _account_query_finalize(hstmt);
3488 pthread_mutex_unlock(&account_mutex);
3492 ACCOUNT_API int account_delete_from_db_by_user_name(char *user_name, char *package_name)
3494 int error_code = ACCOUNT_ERROR_NONE;
3495 account_stmt hstmt = NULL;
3496 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3498 int ret_transaction = 0;
3499 bool is_success = FALSE;
3500 account_h account = NULL;
3501 int binding_count = 1;
3502 int account_id = -1;
3504 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3505 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3506 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3508 rc = account_create(&account);
3509 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3511 rc = account_get_account_id(account, &account_id);
3513 rc = account_destroy(account);
3515 /* transaction control required*/
3516 ret_transaction = _account_begin_transaction();
3518 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3519 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3520 pthread_mutex_unlock(&account_mutex);
3521 return ret_transaction;
3524 /* delete custom data */
3525 ACCOUNT_MEMSET(query, 0, sizeof(query));
3526 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3528 hstmt = _account_prepare_query(query);
3530 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3531 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3533 _account_query_bind_int(hstmt, binding_count++, account_id);
3535 rc = _account_query_step(hstmt);
3536 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3538 _account_query_finalize(hstmt);
3541 /* delete capability */
3542 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3544 hstmt = _account_prepare_query(query);
3546 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3547 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3550 _account_query_bind_text(hstmt, binding_count++, user_name);
3551 _account_query_bind_text(hstmt, binding_count++, package_name);
3553 rc = _account_query_step(hstmt);
3554 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3556 _account_query_finalize(hstmt);
3559 ACCOUNT_MEMSET(query, 0, sizeof(query));
3561 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3563 hstmt = _account_prepare_query(query);
3564 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3565 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3568 _account_query_bind_text(hstmt, binding_count++, user_name);
3569 _account_query_bind_text(hstmt, binding_count++, package_name);
3571 rc = _account_query_step(hstmt);
3572 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));
3574 _account_query_finalize(hstmt);
3580 if (hstmt != NULL) {
3581 _account_query_finalize(hstmt);
3585 ret_transaction = _account_end_transaction(is_success);
3587 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3588 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3590 if (is_success == true) {
3592 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3593 _account_insert_delete_update_notification_send(buf);
3597 pthread_mutex_unlock(&account_mutex);
3602 ACCOUNT_API int account_delete_from_db_by_package_name(char *package_name)
3604 int error_code = ACCOUNT_ERROR_NONE;
3605 account_stmt hstmt = NULL;
3606 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3608 int ret_transaction = 0;
3609 bool is_success = FALSE;
3610 int binding_count = 1;
3612 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3613 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3615 /* transaction control required*/
3616 ret_transaction = _account_begin_transaction();
3618 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3619 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3620 pthread_mutex_unlock(&account_mutex);
3621 return ret_transaction;
3624 /* delete custom table */
3625 ACCOUNT_MEMSET(query, 0, sizeof(query));
3626 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3628 hstmt = _account_prepare_query(query);
3630 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3631 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3634 _account_query_bind_text(hstmt, binding_count++, package_name);
3636 rc = _account_query_step(hstmt);
3637 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3639 _account_query_finalize(hstmt);
3642 /* delete capability table */
3643 ACCOUNT_MEMSET(query, 0, sizeof(query));
3644 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3646 hstmt = _account_prepare_query(query);
3648 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3649 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3652 _account_query_bind_text(hstmt, binding_count++, package_name);
3654 rc = _account_query_step(hstmt);
3655 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3657 _account_query_finalize(hstmt);
3660 /* delete account table */
3661 ACCOUNT_MEMSET(query, 0, sizeof(query));
3663 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3665 hstmt = _account_prepare_query(query);
3666 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3667 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3670 _account_query_bind_text(hstmt, binding_count++, package_name);
3672 rc = _account_query_step(hstmt);
3673 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));
3675 _account_query_finalize(hstmt);
3681 if (hstmt != NULL) {
3682 _account_query_finalize(hstmt);
3686 ret_transaction = _account_end_transaction(is_success);
3688 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3689 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3691 if (is_success == true) {
3693 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, -1);
3694 _account_insert_delete_update_notification_send(buf);
3698 pthread_mutex_unlock(&account_mutex);
3703 ACCOUNT_API int account_get_total_count_from_db(int *count)
3706 ACCOUNT_ERROR("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3707 return ACCOUNT_ERROR_INVALID_PARAMETER;
3709 char query[1024] = {0, };
3710 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3711 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3713 *count = _account_get_record_count(query);
3716 account_stmt pStmt = NULL;
3718 assert(NULL != g_hAccountDB);
3719 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3721 rc = sqlite3_step(pStmt);
3722 if (SQLITE_ROW != rc) {
3723 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3724 sqlite3_finalize(pStmt);
3725 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3728 ncount = sqlite3_column_int(pStmt, 0);
3732 ACCOUNT_VERBOSE("Number of account : %d", ncount);
3733 sqlite3_finalize(pStmt);
3736 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3737 return ACCOUNT_ERROR_DB_FAILED;
3740 return ACCOUNT_ERROR_NONE;
3743 ACCOUNT_API int account_destroy(account_h account)
3745 account_s *data = (account_s*)account;
3747 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3749 ACCOUNT_INFO("destroy handle=%p\n", data);
3751 _account_free_account_items(data);
3752 _ACCOUNT_FREE(data);
3754 return ACCOUNT_ERROR_NONE;
3757 static int _account_type_free_label_items(label_s *data)
3759 _ACCOUNT_FREE(data->app_id);
3760 _ACCOUNT_FREE(data->label);
3761 _ACCOUNT_FREE(data->locale);
3763 return ACCOUNT_ERROR_NONE;
3766 static int _account_type_free_feature_items(provider_feature_s *data)
3768 _ACCOUNT_FREE(data->app_id);
3769 _ACCOUNT_FREE(data->key);
3771 return ACCOUNT_ERROR_NONE;
3774 static int _account_type_gslist_free(GSList* list)
3776 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
3780 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
3781 label_s *label_data = (label_s*)iter->data;
3782 _account_type_free_label_items(label_data);
3783 _ACCOUNT_FREE(label_data);
3789 return ACCOUNT_ERROR_NONE;
3792 static int _account_type_item_free(account_type_s *data)
3794 _ACCOUNT_FREE(data->app_id);
3795 _ACCOUNT_FREE(data->service_provider_id);
3796 _ACCOUNT_FREE(data->icon_path);
3797 _ACCOUNT_FREE(data->small_icon_path);
3799 return ACCOUNT_ERROR_NONE;
3802 static int _account_type_glist_free(GList* list)
3804 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
3808 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
3809 account_type_s *account_type_record = (account_type_s*)iter->data;
3810 _account_type_item_free(account_type_record);
3811 _ACCOUNT_FREE(account_type_record);
3817 return ACCOUNT_ERROR_NONE;
3820 static int _account_type_free_account_type_items(account_type_s *data)
3822 _account_type_item_free(data);
3824 _account_type_gslist_free(data->label_list);
3825 _account_type_glist_free(data->account_type_list);
3827 return ACCOUNT_ERROR_NONE;
3830 ACCOUNT_API int account_type_create(account_type_h *account_type)
3832 if (!account_type) {
3833 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3834 return ACCOUNT_ERROR_INVALID_PARAMETER;
3837 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3840 ACCOUNT_ERROR("Memory Allocation Failed");
3841 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3844 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3846 ACCOUNT_VERBOSE("create handle=%p\n", *account_type);
3848 *account_type = (account_type_h)data;
3850 return ACCOUNT_ERROR_NONE;
3853 ACCOUNT_API int account_type_destroy(account_type_h account_type)
3855 account_type_s *data = (account_type_s*)account_type;
3857 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
3859 ACCOUNT_VERBOSE("destroy handle=%p\n", data);
3861 _account_type_free_account_type_items(data);
3862 _ACCOUNT_FREE(data);
3864 return ACCOUNT_ERROR_NONE;
3867 //app_id mandatory field
3868 ACCOUNT_API int account_type_set_app_id(account_type_h account_type, const char *app_id)
3870 if (!account_type) {
3871 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3872 return ACCOUNT_ERROR_INVALID_PARAMETER;
3876 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
3877 return ACCOUNT_ERROR_INVALID_PARAMETER;
3880 account_type_s *data = (account_type_s*)account_type;
3882 _ACCOUNT_FREE(data->app_id);
3883 data->app_id = _account_get_text(app_id);
3885 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->app_id, app_id);
3886 return ACCOUNT_ERROR_NONE;
3889 //service_provider_id mandatory field
3890 ACCOUNT_API int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
3892 if (!account_type) {
3893 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3894 return ACCOUNT_ERROR_INVALID_PARAMETER;
3897 if (!service_provider_id) {
3898 ACCOUNT_ERROR("(%s)-(%d) service_provider_id is NULL.\n", __FUNCTION__, __LINE__);
3899 return ACCOUNT_ERROR_INVALID_PARAMETER;
3902 account_type_s *data = (account_type_s*)account_type;
3904 _ACCOUNT_FREE(data->service_provider_id);
3905 data->service_provider_id = _account_get_text(service_provider_id);
3907 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->service_provider_id, service_provider_id);
3908 return ACCOUNT_ERROR_NONE;
3911 ACCOUNT_API int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
3913 if (!account_type) {
3914 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3915 return ACCOUNT_ERROR_INVALID_PARAMETER;
3919 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
3920 return ACCOUNT_ERROR_INVALID_PARAMETER;
3923 account_type_s *data = (account_type_s*)account_type;
3925 _ACCOUNT_FREE(data->icon_path);
3926 data->icon_path = _account_get_text(icon_path);
3928 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
3929 return ACCOUNT_ERROR_NONE;
3932 ACCOUNT_API int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
3934 if (!account_type) {
3935 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3936 return ACCOUNT_ERROR_INVALID_PARAMETER;
3939 if (!small_icon_path) {
3940 ACCOUNT_ERROR("(%s)-(%d) small_icon_path is NULL.\n", __FUNCTION__, __LINE__);
3941 return ACCOUNT_ERROR_INVALID_PARAMETER;
3944 account_type_s *data = (account_type_s*)account_type;
3946 _ACCOUNT_FREE(data->small_icon_path);
3947 data->small_icon_path = _account_get_text(small_icon_path);
3949 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->small_icon_path, small_icon_path);
3950 return ACCOUNT_ERROR_NONE;
3953 ACCOUNT_API int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
3955 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
3957 account_type_s *data = (account_type_s*)account_type;
3959 data->multiple_account_support = multiple_account_support;
3961 return ACCOUNT_ERROR_NONE;
3965 ACCOUNT_API int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
3967 if (!account_type) {
3968 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3969 return ACCOUNT_ERROR_INVALID_PARAMETER;
3972 if(!label || !locale) {
3973 ACCOUNT_ERROR("(%s)-(%d) label(%p) or locale(%p) is NULL.\n", __FUNCTION__, __LINE__, label, locale);
3974 return ACCOUNT_ERROR_INVALID_PARAMETER;
3977 account_type_s *data = (account_type_s*)account_type;
3978 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3980 if (label_data == NULL) {
3981 ACCOUNT_FATAL("(%s)-(%d) Malloc fail. label_data is NULL.\n", __FUNCTION__, __LINE__);
3982 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3984 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3986 label_data->label = _account_get_text(label);
3987 label_data->locale = _account_get_text(locale);
3989 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3991 return ACCOUNT_ERROR_NONE;
3994 ACCOUNT_API int account_type_get_app_id(account_type_h account_type, char **app_id)
3996 if (!account_type) {
3997 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3998 return ACCOUNT_ERROR_INVALID_PARAMETER;
4002 ACCOUNT_ERROR("(%s)-(%d) app id is NULL.\n", __FUNCTION__, __LINE__);
4003 return ACCOUNT_ERROR_INVALID_PARAMETER;
4006 account_type_s *data = (account_type_s*)account_type;
4009 *app_id = _account_get_text(data->app_id);
4011 return ACCOUNT_ERROR_NONE;
4014 ACCOUNT_API int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4016 if (!account_type) {
4017 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4018 return ACCOUNT_ERROR_INVALID_PARAMETER;
4021 if (!service_provider_id) {
4022 ACCOUNT_ERROR("(%s)-(%d) service provider id is NULL.\n", __FUNCTION__, __LINE__);
4023 return ACCOUNT_ERROR_INVALID_PARAMETER;
4026 account_type_s *data = (account_type_s*)account_type;
4028 (*service_provider_id) = NULL;
4029 *service_provider_id = _account_get_text(data->service_provider_id);
4031 return ACCOUNT_ERROR_NONE;
4034 ACCOUNT_API int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4036 if (!account_type) {
4037 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4038 return ACCOUNT_ERROR_INVALID_PARAMETER;
4042 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4043 return ACCOUNT_ERROR_INVALID_PARAMETER;
4046 account_type_s *data = (account_type_s*)account_type;
4048 (*icon_path) = NULL;
4049 *icon_path = _account_get_text(data->icon_path);
4051 return ACCOUNT_ERROR_NONE;
4054 ACCOUNT_API int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4056 if (!account_type) {
4057 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4058 return ACCOUNT_ERROR_INVALID_PARAMETER;
4061 if (!small_icon_path) {
4062 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
4063 return ACCOUNT_ERROR_INVALID_PARAMETER;
4066 account_type_s *data = (account_type_s*)account_type;
4068 (*small_icon_path) = NULL;
4069 *small_icon_path = _account_get_text(data->small_icon_path);
4071 return ACCOUNT_ERROR_NONE;
4074 ACCOUNT_API int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4076 if (!account_type) {
4077 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4078 return ACCOUNT_ERROR_INVALID_PARAMETER;
4080 if (!multiple_account_support) {
4081 ACCOUNT_ERROR("(%s)-(%d) multiple_account_support is NULL.\n", __FUNCTION__, __LINE__);
4082 return ACCOUNT_ERROR_INVALID_PARAMETER;
4085 account_type_s *data = (account_type_s*)account_type;
4087 *multiple_account_support = data->multiple_account_support;
4089 return ACCOUNT_ERROR_NONE;
4092 ACCOUNT_API int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4094 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4095 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4098 account_type_s *data = (account_type_s*)account_type;
4100 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4101 label_s *label_data = NULL;
4103 label_data = (label_s*)iter->data;
4105 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4109 if(!strcmp(locale, label_data->locale)) {
4110 *label = _account_get_text(label_data->label);
4111 return ACCOUNT_ERROR_NONE;
4115 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4118 ACCOUNT_API int account_type_get_label(account_type_h account_type, account_label_cb cb_func, void *user_data)
4120 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4121 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4124 account_type_s *data = (account_type_s*)account_type;
4126 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4127 label_s *label_data = NULL;
4129 label_data = (label_s*)iter->data;
4131 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
4133 //cb_func(label_data->app_id, label_data->label, label_data->locale, user_data);
4134 if(cb_func(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4135 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4136 return ACCOUNT_ERROR_NONE;
4140 return ACCOUNT_ERROR_NONE;
4143 static gboolean _account_type_check_duplicated(account_type_s *data)
4145 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4148 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4150 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4151 , ACCOUNT_TYPE_TABLE, data->app_id);
4153 count = _account_get_record_count(query);
4155 ACCOUNT_VERBOSE("_account_type_check_duplicated : duplicated %d account type(s) exist!, app_id=%s, service_provider_id=%s\n",
4156 count, data->app_id, data->service_provider_id);
4163 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4167 /*Caution : Keep insert query orders.*/
4170 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4172 /* 2. service provider id*/
4173 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4176 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4178 /* 4. small icon path*/
4179 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4181 /* 5. multiple accont support*/
4182 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4188 static int _account_type_execute_insert_query(account_type_s *account_type)
4191 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4192 int error_code = ACCOUNT_ERROR_NONE;
4193 account_stmt hstmt = NULL;
4195 /* check mandatory field */
4196 // app id & service provider id
4197 if (!account_type->app_id) {
4198 ACCOUNT_ERROR("App id is mandetory field, it can not be NULL!!!!\n");
4199 return ACCOUNT_ERROR_INVALID_PARAMETER;
4202 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4203 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4204 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4206 hstmt = _account_prepare_query(query);
4207 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4209 _account_type_convert_account_to_sql(account_type, hstmt, query);
4211 rc = _account_query_step(hstmt);
4212 if (rc != SQLITE_DONE) {
4213 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4214 error_code = ACCOUNT_ERROR_DB_FAILED;
4217 _account_query_finalize(hstmt);
4223 static int _account_type_insert_label(account_type_s *account_type)
4226 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4227 account_stmt hstmt = NULL;
4229 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4231 if (g_slist_length( account_type->label_list)==0) {
4232 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4233 return ACCOUNT_ERROR_NONE;
4236 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4238 rc = _account_get_record_count(query);
4241 ACCOUNT_ERROR( "_account_insert_label : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
4242 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4248 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4251 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4252 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4253 "(?, ?, ?) ", LABEL_TABLE);
4255 hstmt = _account_prepare_query(query);
4257 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4259 label_s* label_data = NULL;
4260 label_data = (label_s*)iter->data;
4261 ACCOUNT_VERBOSE( "label_data->appid = %s, label_data->label = %s, label_data->locale \n", label_data->app_id, label_data->label, label_data->locale);
4263 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4264 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4265 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4266 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4267 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4268 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4270 rc = _account_query_step(hstmt);
4272 if (rc != SQLITE_DONE) {
4273 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4277 _account_query_finalize(hstmt);
4282 ACCOUNT_VERBOSE( "_account_type_insert_label() DONE\n");
4284 return ACCOUNT_ERROR_NONE;
4287 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4289 char *textbuf = NULL;
4291 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4292 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4294 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4295 _account_db_data_to_text(textbuf, &(feature_record->key));
4297 ACCOUNT_VERBOSE("END _account_type_convert_column_to_provider_feature");
4300 ACCOUNT_API int account_type_query_provider_feature_by_app_id(provider_feature_cb cb_func, const char* app_id, void *user_data )
4302 int error_code = ACCOUNT_ERROR_NONE;
4303 account_stmt hstmt = NULL;
4304 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4305 int rc = 0, binding_count = 1;
4307 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4308 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4309 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4311 ACCOUNT_VERBOSE("app_id = %s", app_id);
4313 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4315 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4316 hstmt = _account_prepare_query(query);
4318 _account_query_bind_text(hstmt, binding_count++, app_id);
4320 rc = _account_query_step(hstmt);
4321 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4323 provider_feature_s* feature_record = NULL;
4325 while (rc == SQLITE_ROW) {
4326 bool cb_ret = FALSE;
4327 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4329 if (feature_record == NULL) {
4330 ACCOUNT_FATAL("malloc Failed");
4334 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4336 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4338 cb_ret = cb_func(feature_record->app_id, feature_record->key, user_data);
4340 _account_type_free_feature_items(feature_record);
4341 _ACCOUNT_FREE(feature_record);
4343 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4345 rc = _account_query_step(hstmt);
4348 _account_query_finalize(hstmt);
4351 error_code = ACCOUNT_ERROR_NONE;
4354 if (hstmt != NULL) {
4355 _account_query_finalize(hstmt);
4359 pthread_mutex_unlock(&account_mutex);
4363 ACCOUNT_API bool account_type_query_supported_feature(const char* app_id, const char* capability)
4365 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4366 int record_count = 0;
4368 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4369 ACCOUNT_RETURN_VAL((capability != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CAPABILITY"));
4371 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4373 record_count = _account_get_record_count(query);
4375 if (record_count <= 0) {
4376 ACCOUNT_WARNING( "related capability type item is not existed rc=%d , %s", record_count, _account_db_err_msg());
4385 ACCOUNT_API int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb cb_func, void* user_data)
4387 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4388 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4391 account_type_s *data = (account_type_s*)account_type;
4393 for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4394 provider_feature_s *feature_data = NULL;
4396 feature_data = (provider_feature_s*)iter->data;
4398 ACCOUNT_VERBOSE("appid = %s, key = %s", feature_data->key, feature_data->app_id);
4400 //cb_func(feature_data->app_id, feature_data->key, user_data);
4401 if(cb_func(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
4402 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4403 return ACCOUNT_ERROR_NONE;
4407 return ACCOUNT_ERROR_NONE;
4410 ACCOUNT_API int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4412 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4413 ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4415 account_type_s *data = (account_type_s*)account_type;
4417 GSList *iter = NULL;
4418 bool b_is_new = TRUE;
4420 for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4421 provider_feature_s *feature_data = NULL;
4422 feature_data = (provider_feature_s*)iter->data;
4424 if(!strcmp(feature_data->key, provider_feature)) {
4431 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4433 if (feature_data == NULL)
4434 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4435 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4437 feature_data->key = _account_get_text(provider_feature);
4438 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4441 return ACCOUNT_ERROR_NONE;
4444 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4447 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4448 account_stmt hstmt = NULL;
4450 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4451 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4453 if (g_slist_length( account_type->provider_feature_list)==0) {
4454 ACCOUNT_ERROR( "no capability\n");
4455 return ACCOUNT_ERROR_NONE;
4458 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4460 rc = _account_get_record_count(query);
4463 ACCOUNT_WARNING( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4464 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4471 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4474 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4475 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4476 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4478 hstmt = _account_prepare_query(query);
4480 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4482 provider_feature_s* feature_data = NULL;
4483 feature_data = (provider_feature_s*)iter->data;
4484 ACCOUNT_VERBOSE("key = %s, app_id = %s \n", feature_data->key, app_id);
4486 ret = _account_query_bind_text(hstmt, count++, app_id);
4487 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4488 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4489 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4491 rc = _account_query_step(hstmt);
4493 if (rc != SQLITE_DONE) {
4494 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4498 _account_query_finalize(hstmt);
4503 ACCOUNT_VERBOSE( "_account_type_insert_provider_feature() DONE\n");
4505 return ACCOUNT_ERROR_NONE;
4508 ACCOUNT_API int account_type_insert_to_db(account_type_h account_type, int* account_type_id)
4510 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4512 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4513 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4514 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4516 account_type_s *data = (account_type_s*)account_type;
4518 pthread_mutex_lock(&account_mutex);
4521 /* transaction control required*/
4522 ret_transaction = _account_begin_transaction();
4524 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4525 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4526 pthread_mutex_unlock(&account_mutex);
4527 return ret_transaction;
4530 if (_account_type_check_duplicated(data)) {
4531 ret_transaction = _account_end_transaction(FALSE);
4532 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4533 *account_type_id = -1;
4534 pthread_mutex_unlock(&account_mutex);
4535 return ACCOUNT_ERROR_DUPLICATED;
4537 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4539 error_code = _account_type_execute_insert_query(data);
4541 if (error_code != ACCOUNT_ERROR_NONE){
4542 error_code = ACCOUNT_ERROR_DUPLICATED;
4543 ret_transaction = _account_end_transaction(FALSE);
4544 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4545 *account_type_id = -1;
4546 pthread_mutex_unlock(&account_mutex);
4551 ACCOUNT_INFO( "_account_type_execute_insert_query, insert error_code : %d", error_code);
4553 error_code = _account_type_insert_provider_feature(data, data->app_id);
4554 if(error_code != ACCOUNT_ERROR_NONE) {
4555 ret_transaction = _account_end_transaction(FALSE);
4556 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4557 pthread_mutex_unlock(&account_mutex);
4560 error_code = _account_type_insert_label(data);
4561 if(error_code != ACCOUNT_ERROR_NONE) {
4562 ret_transaction = _account_end_transaction(FALSE);
4563 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4564 pthread_mutex_unlock(&account_mutex);
4568 ret_transaction = _account_end_transaction(TRUE);
4569 ACCOUNT_DEBUG("Insert success(%x), commit insert query(%x)!!!!\n", error_code, ret_transaction);
4571 pthread_mutex_unlock(&account_mutex);
4573 return ACCOUNT_ERROR_NONE;
4576 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4579 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4580 account_stmt hstmt = NULL;
4582 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4584 if (g_slist_length( account_type->provider_feature_list)==0) {
4585 ACCOUNT_ERROR( "no feature\n");
4586 return ACCOUNT_ERROR_NONE;
4589 ACCOUNT_DEBUG( "app id", app_id);
4591 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4593 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4594 hstmt = _account_prepare_query(query);
4596 _account_query_bind_text(hstmt, count++, app_id);
4597 rc = _account_query_step(hstmt);
4599 if (rc != SQLITE_DONE) {
4600 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4601 return ACCOUNT_ERROR_DB_FAILED;
4603 _account_query_finalize(hstmt);
4608 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4611 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4612 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4613 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4615 hstmt = _account_prepare_query(query);
4617 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4619 provider_feature_s* feature_data = NULL;
4620 feature_data = (provider_feature_s*)iter->data;
4622 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4623 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4624 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4625 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4627 rc = _account_query_step(hstmt);
4629 if (rc != SQLITE_DONE) {
4630 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4633 _account_query_finalize(hstmt);
4637 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4639 return ACCOUNT_ERROR_NONE;
4642 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4645 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4646 account_stmt hstmt = NULL;
4648 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4650 if (g_slist_length( account_type->label_list)==0) {
4651 ACCOUNT_ERROR( "_account_type_update_label, no label\n");
4652 return ACCOUNT_ERROR_NONE;
4655 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4657 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4658 hstmt = _account_prepare_query(query);
4660 _account_query_bind_text(hstmt, count++, app_id);
4661 rc = _account_query_step(hstmt);
4663 if (rc != SQLITE_DONE) {
4664 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4665 return ACCOUNT_ERROR_DB_FAILED;
4667 _account_query_finalize(hstmt);
4672 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4675 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4676 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4677 "(?, ?, ?) ", LABEL_TABLE);
4679 hstmt = _account_prepare_query(query);
4681 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4683 label_s* label_data = NULL;
4684 label_data = (label_s*)iter->data;
4686 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4687 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4688 ret = _account_query_bind_text(hstmt, count++, label_data->label);
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->locale);
4691 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4693 rc = _account_query_step(hstmt);
4695 if (rc != SQLITE_DONE) {
4696 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4699 _account_query_finalize(hstmt);
4703 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
4705 return ACCOUNT_ERROR_NONE;
4709 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
4711 int rc = 0, binding_count =1;
4712 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4713 int error_code = ACCOUNT_ERROR_NONE;
4714 account_stmt hstmt = NULL;
4716 if (!account_type->app_id) {
4717 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
4718 return ACCOUNT_ERROR_INVALID_PARAMETER;
4721 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4722 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
4723 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
4725 hstmt = _account_prepare_query(query);
4726 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
4728 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
4729 _account_query_bind_text(hstmt, binding_count++, app_id);
4731 rc = _account_query_step(hstmt);
4732 if (rc != SQLITE_DONE) {
4733 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4736 _account_query_finalize(hstmt);
4740 error_code = _account_type_update_label(account_type, app_id);
4741 /* update provider feature */
4742 error_code = _account_type_update_provider_feature(account_type, app_id);
4747 ACCOUNT_API int account_type_update_to_db_by_app_id(account_type_h account_type, const char* app_id)
4749 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
4750 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4751 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4753 int error_code = ACCOUNT_ERROR_NONE;
4754 account_type_s* data = (account_type_s*)account_type;
4756 pthread_mutex_lock(&account_mutex);
4758 error_code = _account_type_update_account(data, app_id);
4760 pthread_mutex_unlock(&account_mutex);
4765 ACCOUNT_API int account_type_delete_by_app_id(const char* app_id)
4767 int error_code = ACCOUNT_ERROR_NONE;
4768 account_stmt hstmt = NULL;
4769 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4770 int rc = 0, count = -1;
4771 int ret_transaction = 0;
4772 int binding_count = 1;
4773 bool is_success = FALSE;
4775 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4776 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
4778 /* Check requested ID to delete */
4779 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
4781 count = _account_get_record_count(query);
4783 ACCOUNT_ERROR("app id(%s) is not exist. count(%d)\n", app_id, count);
4784 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4787 /* transaction control required*/
4788 ret_transaction = _account_begin_transaction();
4790 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4791 ACCOUNT_FATAL("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4792 pthread_mutex_unlock(&account_mutex);
4793 return ret_transaction;
4796 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
4798 hstmt = _account_prepare_query(query);
4799 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4800 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4802 _account_query_bind_text(hstmt, binding_count++, app_id);
4804 rc = _account_query_step(hstmt);
4805 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4807 _account_query_finalize(hstmt);
4811 ACCOUNT_MEMSET(query, 0, sizeof(query));
4813 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
4815 hstmt = _account_prepare_query(query);
4816 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4817 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4819 _account_query_bind_text(hstmt, binding_count++, app_id);
4821 rc = _account_query_step(hstmt);
4822 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4824 _account_query_finalize(hstmt);
4830 ACCOUNT_MEMSET(query, 0, sizeof(query));
4832 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
4834 hstmt = _account_prepare_query(query);
4835 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4836 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4838 _account_query_bind_text(hstmt, binding_count++, app_id);
4840 rc = _account_query_step(hstmt);
4841 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
4843 _account_query_finalize(hstmt);
4849 if (hstmt != NULL) {
4850 _account_query_finalize(hstmt);
4854 ret_transaction = _account_end_transaction(is_success);
4856 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4857 ACCOUNT_FATAL("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4860 pthread_mutex_unlock(&account_mutex);
4865 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
4867 char *textbuf = NULL;
4869 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
4871 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
4872 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
4874 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
4875 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
4877 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
4878 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
4880 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
4881 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
4883 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
4885 ACCOUNT_VERBOSE("END _account_type_convert_column_to_account_type");
4888 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
4890 char *textbuf = NULL;
4892 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
4893 _account_db_data_to_text(textbuf, &(label_record->app_id));
4895 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
4896 _account_db_data_to_text(textbuf, &(label_record->label));
4898 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
4899 _account_db_data_to_text(textbuf, &(label_record->locale));
4901 ACCOUNT_VERBOSE("END _account_type_convert_column_to_label");
4904 ACCOUNT_API int account_type_query_label_by_app_id(account_label_cb cb_func, const char* app_id, void *user_data )
4906 int error_code = ACCOUNT_ERROR_NONE;
4907 account_stmt hstmt = NULL;
4908 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4909 int rc = 0, binding_count = 1;
4911 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4912 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4913 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4915 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4917 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
4918 hstmt = _account_prepare_query(query);
4920 _account_query_bind_text(hstmt, binding_count++, app_id);
4922 rc = _account_query_step(hstmt);
4923 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4925 label_s* label_record = NULL;
4927 while (rc == SQLITE_ROW) {
4928 bool cb_ret = FALSE;
4929 label_record = (label_s*) malloc(sizeof(label_s));
4931 if (label_record == NULL) {
4932 ACCOUNT_FATAL("malloc Failed");
4936 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4938 _account_type_convert_column_to_label(hstmt, label_record);
4940 cb_ret = cb_func(label_record->app_id, label_record->label , label_record->locale, user_data);
4942 _account_type_free_label_items(label_record);
4943 _ACCOUNT_FREE(label_record);
4945 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4947 rc = _account_query_step(hstmt);
4950 _account_query_finalize(hstmt);
4953 error_code = ACCOUNT_ERROR_NONE;
4956 if (hstmt != NULL) {
4957 _account_query_finalize(hstmt);
4961 pthread_mutex_unlock(&account_mutex);
4965 int _account_type_label_get_app_id(label_h label, char **app_id)
4968 ACCOUNT_ERROR("(%s)-(%d) label handle is NULL.\n", __FUNCTION__, __LINE__);
4969 return ACCOUNT_ERROR_INVALID_PARAMETER;
4973 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4974 return ACCOUNT_ERROR_INVALID_PARAMETER;
4977 label_s *data = (label_s*)label;
4981 *app_id = _account_get_text(data->app_id);
4983 return ACCOUNT_ERROR_NONE;
4986 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
4988 account_type_s *data = (account_type_s*)user_data;
4990 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4992 if (label_data == NULL) {
4993 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
4996 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4998 label_data->app_id = _account_get_text(app_id);
4999 label_data->label = _account_get_text(label);
5000 label_data->locale = _account_get_text(locale);
5002 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5004 ACCOUNT_VERBOSE("_account_get_label_text_cb :: appid=%s, label=%s\n", label_data->app_id, label_data->label);
5009 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5011 account_type_s *data = (account_type_s*)user_data;
5013 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5015 if (feature_data == NULL) {
5016 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5019 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5021 feature_data->app_id = _account_get_text(app_id);
5022 feature_data->key = _account_get_text(key);
5024 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5026 ACCOUNT_VERBOSE("appid=%s, key=%s\n", feature_data->app_id, feature_data->key);
5031 ACCOUNT_API int account_type_query_by_app_id(const char* app_id, account_type_h *account_type)
5033 int error_code = ACCOUNT_ERROR_NONE;
5034 account_stmt hstmt = NULL;
5035 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5036 int rc = 0, binding_count = 1;
5038 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5039 ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL"));
5040 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5042 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5044 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5045 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5046 hstmt = _account_prepare_query(query);
5048 _account_query_bind_text(hstmt, binding_count++, app_id);
5050 rc = _account_query_step(hstmt);
5051 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5053 account_type_s *account_type_record = (account_type_s *)(*account_type);
5055 while (rc == SQLITE_ROW) {
5056 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5057 ACCOUNT_DEBUG("get account info by id %p\n", account_type_record);
5058 rc = _account_query_step(hstmt);
5061 _account_query_finalize(hstmt);
5062 account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)account_type_record);
5063 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)account_type_record);
5066 error_code = ACCOUNT_ERROR_NONE;
5069 if (hstmt != NULL) {
5070 _account_query_finalize(hstmt);
5074 pthread_mutex_unlock(&account_mutex);
5078 ACCOUNT_API int account_type_query_app_id_exist(const char* app_id)
5080 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5083 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5084 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5086 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5088 ACCOUNT_DEBUG("app id (%s)\n", app_id);
5090 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5091 rc = _account_get_record_count(query);
5094 ACCOUNT_DEBUG("(%s) not exist in account type db");
5095 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5098 return ACCOUNT_ERROR_NONE;
5101 ACCOUNT_API int account_type_query_by_provider_feature(account_type_cb cb_func, const char* key, void* user_data)
5103 int error_code = ACCOUNT_ERROR_NONE;
5104 account_stmt hstmt = NULL;
5105 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5107 GList *account_type_list = NULL;
5109 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
5110 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
5111 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5113 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5115 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5117 hstmt = _account_prepare_query(query);
5119 int binding_count = 1;
5120 _account_query_bind_text(hstmt, binding_count++, (char*)key);
5122 rc = _account_query_step(hstmt);
5124 account_type_s *account_type_record = NULL;
5126 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5129 while(rc == SQLITE_ROW) {
5130 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5132 if (account_type_record == NULL) {
5133 ACCOUNT_FATAL("malloc Failed");
5137 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5138 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5139 account_type_list = g_list_append(account_type_list, account_type_record);
5140 rc = _account_query_step(hstmt);
5144 _account_query_finalize(hstmt);
5149 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5150 account_type_s *account_type = NULL;
5151 account_type = (account_type_s*)iter->data;
5152 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5153 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5154 //cb_func((account_type_h)account_type, user_data);
5155 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"));
5159 error_code = ACCOUNT_ERROR_NONE;
5162 if (hstmt != NULL) {
5163 _account_query_finalize(hstmt);
5166 if (account_type_list) {
5167 _account_type_glist_free(account_type_list);
5168 account_type_list = NULL;
5175 ACCOUNT_API int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data)
5177 int error_code = ACCOUNT_ERROR_NONE;
5178 account_stmt hstmt = NULL;
5179 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5181 GList *account_type_list = NULL;
5183 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
5184 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5186 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5188 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5189 hstmt = _account_prepare_query(query);
5191 rc = _account_query_step(hstmt);
5193 account_type_s *account_type_record = NULL;
5195 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5198 while(rc == SQLITE_ROW) {
5199 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5201 if (account_type_record == NULL) {
5202 ACCOUNT_FATAL("malloc Failed");
5206 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5207 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5208 account_type_list = g_list_append(account_type_list, account_type_record);
5209 rc = _account_query_step(hstmt);
5213 _account_query_finalize(hstmt);
5218 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
5219 account_type_s *account_type = NULL;
5220 account_type = (account_type_s*)iter->data;
5221 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5222 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5223 callback((account_type_h)account_type, user_data);
5227 error_code = ACCOUNT_ERROR_NONE;
5230 if (hstmt != NULL) {
5231 _account_query_finalize(hstmt);
5234 if (account_type_list) {
5235 _account_type_glist_free(account_type_list);
5236 account_type_list = NULL;
5242 // output parameter label must be free
5243 ACCOUNT_API int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label)
5245 int error_code = ACCOUNT_ERROR_NONE;
5246 account_stmt hstmt = NULL;
5247 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5248 int rc = 0, binding_count = 1;
5250 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5251 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5252 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5254 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5256 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, locale);
5257 hstmt = _account_prepare_query(query);
5259 _account_query_bind_text(hstmt, binding_count++, app_id);
5261 rc = _account_query_step(hstmt);
5262 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5264 label_s* label_record = NULL;
5266 while (rc == SQLITE_ROW) {
5267 label_record = (label_s*) malloc(sizeof(label_s));
5269 if (label_record == NULL) {
5270 ACCOUNT_FATAL("malloc Failed");
5274 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5276 _account_type_convert_column_to_label(hstmt,label_record);
5278 *label = _account_get_text(label_record->label);
5280 _account_type_free_label_items(label_record);
5281 _ACCOUNT_FREE(label_record);
5283 rc = _account_query_step(hstmt);
5286 _account_query_finalize(hstmt);
5289 error_code = ACCOUNT_ERROR_NONE;
5292 if (hstmt != NULL) {
5293 _account_query_finalize(hstmt);
5297 pthread_mutex_unlock(&account_mutex);
5301 static int _account_insert_custom(account_s *account, int account_id)
5304 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5305 account_stmt hstmt = NULL;
5307 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5309 if (g_slist_length( account->custom_list)==0) {
5310 ACCOUNT_ERROR( "_account_insert_custom, no custom data\n");
5311 return ACCOUNT_ERROR_NONE;
5314 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5316 rc = _account_get_record_count(query);
5319 ACCOUNT_ERROR( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5320 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5327 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5330 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5331 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5332 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5334 hstmt = _account_prepare_query(query);
5336 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5338 account_custom_s* custom_data = NULL;
5339 custom_data = (account_custom_s*)iter->data;
5340 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
5342 ret = _account_query_bind_int(hstmt, count++, account_id);
5343 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5344 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5345 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5346 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
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->value);
5349 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5351 rc = _account_query_step(hstmt);
5353 if (rc != SQLITE_DONE) {
5354 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5358 _account_query_finalize(hstmt);
5363 ACCOUNT_VERBOSE( "_account_insert_custom() DONE\n");
5365 return ACCOUNT_ERROR_NONE;
5368 static int _account_update_custom(account_s *account, int account_id)
5371 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5372 account_stmt hstmt = NULL;
5374 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5376 ACCOUNT_INFO( "account handle=%p, account_id=%d", account, account_id);
5378 if (g_slist_length( account->custom_list)==0) {
5379 ACCOUNT_ERROR( "_account_update_custom, no custom data\n");
5380 return ACCOUNT_ERROR_NONE;
5383 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5385 rc = _account_get_record_count(query);
5388 ACCOUNT_ERROR( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5389 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5392 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5394 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5395 hstmt = _account_prepare_query(query);
5397 _account_query_bind_int(hstmt, count++, (int)account_id);
5398 rc = _account_query_step(hstmt);
5400 if (rc != SQLITE_DONE) {
5401 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5402 return ACCOUNT_ERROR_DB_FAILED;
5404 _account_query_finalize(hstmt);
5409 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5412 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5413 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5414 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5416 hstmt = _account_prepare_query(query);
5418 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5420 account_custom_s* custom_data = NULL;
5421 custom_data = (account_custom_s*)iter->data;
5423 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5424 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5425 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5426 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5427 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
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->value);
5430 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5432 rc = _account_query_step(hstmt);
5434 if (rc != SQLITE_DONE) {
5435 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5439 _account_query_finalize(hstmt);
5444 ACCOUNT_VERBOSE( "_account_update_custom() DONE\n");
5446 return ACCOUNT_ERROR_NONE;
5449 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data )
5451 int error_code = ACCOUNT_ERROR_NONE;
5452 account_stmt hstmt = NULL;
5453 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5456 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5457 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5458 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5460 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5462 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5463 hstmt = _account_prepare_query(query);
5465 rc = _account_query_step(hstmt);
5466 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5468 account_custom_s* custom_record = NULL;
5470 while (rc == SQLITE_ROW) {
5471 bool cb_ret = FALSE;
5472 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5474 if (custom_record == NULL) {
5475 ACCOUNT_FATAL("malloc Failed");
5479 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5481 _account_convert_column_to_custom(hstmt, custom_record);
5483 cb_ret = cb_func(custom_record->key, custom_record->value, user_data);
5485 _account_custom_item_free(custom_record);
5486 _ACCOUNT_FREE(custom_record);
5488 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5490 rc = _account_query_step(hstmt);
5493 _account_query_finalize(hstmt);
5496 error_code = ACCOUNT_ERROR_NONE;
5499 if (hstmt != NULL) {
5500 _account_query_finalize(hstmt);
5504 pthread_mutex_unlock(&account_mutex);
5508 static void _account_subscribe_vconf_callback(keynode_t *key, void *user_data)
5510 account_subscribe_s* tmp = (account_subscribe_s*)user_data;
5511 char *msg = NULL, *vconf_key = NULL;
5512 char event_msg[256] ={0, };
5513 int account_id = -1;
5515 ACCOUNT_DEBUG("START\n");
5518 ACCOUNT_ERROR("No subscribtion msg !!!!!\n");
5523 ACCOUNT_ERROR("user data required\n");
5527 if(!memcmp(vconf_keynode_get_name(key), VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR)))
5529 vconf_key = vconf_keynode_get_str(key);
5530 msg = strdup(vconf_key);
5532 char* event_type = NULL;
5536 event_type = strtok_r(msg, ":", &ptr);
5537 id = strtok_r(NULL, ":", &ptr);
5539 ACCOUNT_DEBUG("msg(%s), event_type(%s), id(%s)\n", msg, event_type, id);
5541 ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type);
5543 account_id = atoi(id);
5545 if(tmp->account_subscription_callback)
5546 tmp->account_subscription_callback(event_msg, account_id, tmp->user_data);
5551 ACCOUNT_DEBUG("END\n");
5554 ACCOUNT_API int account_subscribe_create(account_subscribe_h* account_subscribe)
5556 if (!account_subscribe) {
5557 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
5558 return ACCOUNT_ERROR_INVALID_PARAMETER;
5561 account_subscribe_s *data = (account_subscribe_s*)calloc(1,sizeof(account_subscribe_s));
5564 ACCOUNT_FATAL("OUT OF MEMORY\n");
5565 return ACCOUNT_ERROR_OUT_OF_MEMORY;
5568 ACCOUNT_VERBOSE("create handle=%p\n", *data);
5570 *account_subscribe = (account_subscribe_h)data;
5572 return ACCOUNT_ERROR_NONE;
5575 ACCOUNT_API int account_subscribe_notification(account_subscribe_h account_subscribe, account_event_cb cb_func, void* user_data)
5577 ACCOUNT_DEBUG("START\n");
5579 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5581 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5583 tmp->account_subscription_callback = cb_func;
5584 tmp->user_data = user_data;
5586 if (vconf_notify_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5587 (vconf_callback_fn)_account_subscribe_vconf_callback,
5589 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5590 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5593 ACCOUNT_DEBUG("Vconf Subscription Success!!!!\n");
5594 return ACCOUNT_ERROR_NONE;
5597 ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_subscribe)
5599 ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__));
5601 account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
5605 if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
5606 (vconf_callback_fn)_account_subscribe_vconf_callback) != 0) {
5607 ACCOUNT_FATAL("Vconf Subscription Failed !!!!!\n");
5608 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
5611 ACCOUNT_DEBUG("Vconf Unsubscription Success!!!!\n");
5612 return ACCOUNT_ERROR_NONE;