X-Git-Url: http://review.tizen.org/git/?p=framework%2Faccount%2Flibaccounts-svc.git;a=blobdiff_plain;f=src%2Faccount.c;h=6a5ca47c221fdb624fb23daf084bcf5b8107d269;hp=3aa01c55070d3ee55f1a4f2c4dd7afc407b2348b;hb=000a4e65c7aba9ba0d7ea651781098e8d8fa5644;hpb=b3f05b473791106b358b509929807a630bf4a42b diff --git a/src/account.c b/src/account.c old mode 100755 new mode 100644 index 3aa01c5..6a5ca47 --- a/src/account.c +++ b/src/account.c @@ -1,10 +1,7 @@ /* - * account * * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved. * - * Contact: Wonyoung Lee , Sungchan Kim - * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,5221 +16,1277 @@ * */ -#include #include -#include -#include -#include #include -#include -#include -#include -#include -#include "account_internal.h" - -#define SAMSUNG_ACCOUNT_MANAGER - -#include -#include +#include +#include +#ifdef G_OS_UNIX +#include #include -#include -#include - -#define EAS_CMDLINE "/usr/bin/eas-engine" -#define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service" -#define IMS_ENGINE_CMDLINE "/usr/bin/ims-srv" -#define IMS_AGENT_CMDLINE "/usr/bin/ims-agent" -#define MDM_SERVER_CMDLINE "/usr/bin/mdm-server" +#endif +#include +#include -#define RCS_APPID "com.samsung.rcs-im" -#define IMS_SERVICE_APPID "ims-service" -#define ACTIVESYNC_APPID "activesync-ui" -#define EMAIL_APPID "email-setting-efl" -#define SYNCHRONISE_APPID "setting-synchronise-efl" -#define DS_AGENT_CMDLINE "/usr/bin/oma-ds-agent" +#include +#include +#include +#include +#include + +#ifdef TIZEN_PROFILE_MOBILE +#include "mobile/account.h" +#else +#include "wearable/account.h" +#endif -#define FACEBOOK_SDK_APPID "com.samsung.facebook-service" -#define FACEBOOK_APPID "com.samsung.facebook" +#include "account-error.h" +#include "account-types.h" +#include "account_internal.h" +#include "account_private_client.h" #define ACCOUNT_DB_OPEN_READONLY 0 #define ACCOUNT_DB_OPEN_READWRITE 1 -static sqlite3* g_hAccountDB = NULL; -static int g_refCntDB = 0; -pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER; +#define _GET_COOKIE int ret_code = 0;\ +char *e_cookie = NULL;\ +char cookie[256] = {0,};\ +int size = 0;\ +\ +size = security_server_get_cookie_size();\ +ret_code = security_server_request_cookie(cookie, size);\ +if (ret_code < 0) {\ + _ERR("security_server_request_cookie() is failed.");\ + return ACCOUNT_ERROR_PERMISSION_DENIED;\ +}\ +\ +e_cookie = g_base64_encode((const guchar *)cookie, size);\ +if (e_cookie == NULL) {\ + _ERR("g_base64_encode() is failed.");\ + return ACCOUNT_ERROR_PERMISSION_DENIED;\ +} + +#define _DESTROY_COOKIE \ +if (e_cookie) \ + g_free((gchar*)e_cookie); + +static AccountManager *_acc_mgr = NULL; -static char *_account_get_text(const char *text_data); -static int _account_gslist_free(GSList* list); -static int _account_glist_free(GList* list); -static const char *_account_query_table_column_text(account_stmt pStmt, int pos); -static int _account_insert_custom(account_s *account, int account_id); -static int _account_update_custom(account_s *account, int account_id); -static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data ); -static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id); - -int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data) +static char *_account_get_text(const char *text_data) { - char* appid = NULL; - char* item = NULL; - GSList** appid_list = (GSList**)user_data; - int pkgmgr_ret = -1; - - pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid); + char *text_value = NULL; - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret); + if (text_data != NULL) { + text_value = strdup(text_data); } - - item = _account_get_text(appid); - *appid_list = g_slist_append(*appid_list, item); - - return 0; + return text_value; } -static inline int __read_proc(const char *path, char *buf, int size) +//FIXME : add true singleton logic +AccountManager * +_account_manager_get_instance () { - int fd = 0, ret = 0; - - if (buf == NULL || path == NULL) { - ACCOUNT_ERROR("path and buffer is mandatory\n"); - return -1; - } - - fd = open(path, O_RDONLY); - if (fd < 0) { - ACCOUNT_ERROR("fd open error(%d)\n", fd); - return -1; + _INFO("_account_manager_get_instance"); + if (_acc_mgr != NULL) + { + _INFO("instance already created."); + return _acc_mgr; } - ret = read(fd, buf, size - 1); - if (ret <= 0) { - ACCOUNT_ERROR("fd read error(%d)\n", fd); - close(fd); - return -1; - } else - buf[ret] = 0; +#if !GLIB_CHECK_VERSION(2,35,0) + g_type_init(); +#endif - close(fd); + GDBusConnection *connection = NULL; + GError *error = NULL; - return ret; -} + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); -char *_account_get_proc_cmdline_bypid(int pid) -{ - char buf[128]; - int ret = 0; + _INFO("after g_bus_get_sync"); - ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid); - ret = __read_proc(buf, buf, sizeof(buf)); - if (ret <= 0) { - ACCOUNT_DEBUG("No proc directory (%d)\n", pid); + if (!connection) { + if (error) { + _ERR("Unable to connect to gdbus: %s", error->message); + g_clear_error(&error); + } return NULL; } - return strdup(buf); -} + g_clear_error(&error); + /* Create the object */ + _acc_mgr = + account_manager_proxy_new_sync(connection, + G_DBUS_PROXY_FLAGS_NONE, + "org.tizen.account.manager", + "/org/tizen/account/manager", + NULL, + &error); -static char* _account_get_current_appid() -{ - int ret=0, pid=0; - char appid[128]={0,}; - char* appid_ret = NULL; - - pid = getpid(); - ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid)); - - if(ret < 0){ - ACCOUNT_ERROR("fail to get current appid\n"); - } - - /* SLP platform core exception */ - if(strlen(appid) == 0){ - char* cmdline = NULL; - cmdline = _account_get_proc_cmdline_bypid(pid); - ACCOUNT_SLOGD("cmdline (%s)!!!!!!\n", cmdline); - if(!g_strcmp0(cmdline, EAS_CMDLINE)) { - appid_ret = _account_get_text(ACTIVESYNC_APPID); - _ACCOUNT_FREE(cmdline); - return appid_ret; - } else if (!g_strcmp0(cmdline, EMAIL_SERVICE_CMDLINE) || !g_strcmp0(cmdline, MDM_SERVER_CMDLINE)) { - appid_ret = _account_get_text(EMAIL_APPID); - _ACCOUNT_FREE(cmdline); - return appid_ret; - } else if (!g_strcmp0(cmdline, IMS_ENGINE_CMDLINE) || !g_strcmp0(cmdline, IMS_AGENT_CMDLINE)) { - if(account_type_query_app_id_exist(RCS_APPID)==ACCOUNT_ERROR_NONE){ - appid_ret = _account_get_text(RCS_APPID); - } else if(account_type_query_app_id_exist(IMS_SERVICE_APPID)==ACCOUNT_ERROR_NONE){ - appid_ret = _account_get_text(IMS_SERVICE_APPID); - } else { - appid_ret = _account_get_text(RCS_APPID); - } - _ACCOUNT_FREE(cmdline); - return appid_ret; - } else if (!g_strcmp0(cmdline, DS_AGENT_CMDLINE)) { - appid_ret = _account_get_text(SYNCHRONISE_APPID); - _ACCOUNT_FREE(cmdline); - return appid_ret; - } else { - ACCOUNT_DEBUG("No app id\n"); - _ACCOUNT_FREE(cmdline); - return NULL; + if (!_acc_mgr) { + if (error) { + _ERR("Unable account_manager_proxy_new_sync: %s", error->message); + g_clear_error(&error); } + if(connection) + g_object_unref(connection); + return NULL; } - /* temporary exception */ - if(!g_strcmp0(appid, "com.samsung.gallery")){ - appid_ret = _account_get_text("com.samsung.facebook"); - } else if(!g_strcmp0(appid, FACEBOOK_SDK_APPID)){ - appid_ret = _account_get_text(FACEBOOK_APPID); - } else { - appid_ret = _account_get_text(appid); + g_clear_error(&error); + _INFO("_account_manager_get_instance end"); + return _acc_mgr; +} + +GDBusErrorEntry _account_svc_errors[] = +{ + {ACCOUNT_ERROR_NONE, _ACCOUNT_SVC_ERROR_PREFIX".NoError"}, + {ACCOUNT_ERROR_OUT_OF_MEMORY, _ACCOUNT_SVC_ERROR_PREFIX".OutOfMemory"}, + {ACCOUNT_ERROR_INVALID_PARAMETER, _ACCOUNT_SVC_ERROR_PREFIX".InvalidParameter"}, + {ACCOUNT_ERROR_DUPLICATED, _ACCOUNT_SVC_ERROR_PREFIX".Duplicated"}, + {ACCOUNT_ERROR_NO_DATA, _ACCOUNT_SVC_ERROR_PREFIX".NoData"}, + {ACCOUNT_ERROR_RECORD_NOT_FOUND, _ACCOUNT_SVC_ERROR_PREFIX".RecordNotFound"}, + {ACCOUNT_ERROR_DB_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".DBFailed"}, + {ACCOUNT_ERROR_DB_NOT_OPENED, _ACCOUNT_SVC_ERROR_PREFIX".DBNotOpened"}, + {ACCOUNT_ERROR_QUERY_SYNTAX_ERROR, _ACCOUNT_SVC_ERROR_PREFIX".QuerySynTaxError"}, + {ACCOUNT_ERROR_ITERATOR_END, _ACCOUNT_SVC_ERROR_PREFIX".IteratorEnd"}, + {ACCOUNT_ERROR_NOTI_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".NotiFalied"}, + {ACCOUNT_ERROR_PERMISSION_DENIED, _ACCOUNT_SVC_ERROR_PREFIX".PermissionDenied"}, + {ACCOUNT_ERROR_XML_PARSE_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".XMLParseFailed"}, + {ACCOUNT_ERROR_XML_FILE_NOT_FOUND, _ACCOUNT_SVC_ERROR_PREFIX".FileNotFound"}, + {ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL, _ACCOUNT_SVC_ERROR_PREFIX".SubscriptionFailed"}, + {ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, _ACCOUNT_SVC_ERROR_PREFIX".NotRegisteredProvider"}, + {ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE, _ACCOUNT_SVC_ERROR_PREFIX".NotAllowMultiple"}, + {ACCOUNT_ERROR_DATABASE_BUSY, _ACCOUNT_SVC_ERROR_PREFIX".database_busy"}, +}; + +static int _account_get_error_code(bool is_success, GError *error) +{ + if (!is_success) + { + _INFO("Received error Domain[%d] Message[%s] Code[%d]", error->domain, error->message, error->code); + + if (g_dbus_error_is_remote_error(error)) + { + gchar *remote_error = g_dbus_error_get_remote_error(error); + if (remote_error) + { + _INFO("Remote error[%s]", remote_error); + + //FIXME: Temp fix, error->code sent from daemon is not the same as the one received. + //However error->message matches, so doing reverse lookup + int error_enum_count = G_N_ELEMENTS(_account_svc_errors); + int i = 0; + for (i = 0; i < error_enum_count; i++) + { + if (g_strcmp0(_account_svc_errors[i].dbus_error_name, remote_error) == 0) + { + _INFO("Remote error code matched[%d]", _account_svc_errors[i].error_code); + g_free(remote_error); + return _account_svc_errors[i].error_code; + } + } + g_free(remote_error); + } + } + //All undocumented errors mapped to ACCOUNT_ERROR_PERMISSION_DENIED + return ACCOUNT_ERROR_PERMISSION_DENIED; } + return ACCOUNT_ERROR_NONE; +} - return appid_ret; +#ifdef TIZEN_PROFILE_MOBILE +ACCOUNT_API int account_connect(void) +{ + return ACCOUNT_ERROR_NONE; } -static int _account_check_account_type_with_appid_group(const char* appid, char** verified_appid) +ACCOUNT_API int account_connect_readonly(void) { - int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; - pkgmgrinfo_appinfo_h ahandle=NULL; - pkgmgrinfo_pkginfo_h phandle=NULL; - char* package_id = NULL; - GSList* appid_list = NULL; - GSList* iter = NULL; + return ACCOUNT_ERROR_NONE; +} - if(!appid){ - ACCOUNT_ERROR("input param is null\n"); - return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; - } +ACCOUNT_API int account_disconnect(void) +{ + return ACCOUNT_ERROR_NONE; +} +#endif - if(!verified_appid){ - ACCOUNT_ERROR("output param is null\n"); - return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; - } - if(!strcmp(appid, "com.samsung.setting")){ - ACCOUNT_DEBUG("Setting exception\n"); - *verified_appid = _account_get_text("com.samsung.setting"); - return ACCOUNT_ERROR_NONE; - } +ACCOUNT_API int account_insert_to_db(account_h account, int *account_db_id) +{ + _INFO("1. account_insert_to_db start"); + char* account_db_path = ACCOUNT_DB_NAME; - if(!strcmp(appid, "com.samsung.samsung-account-front")){ - ACCOUNT_DEBUG("Setting exception\n"); - *verified_appid = _account_get_text("com.samsung.samsung-account-front"); - return ACCOUNT_ERROR_NONE; - } + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((account_db_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL")); - if(!strcmp(appid, IMS_SERVICE_APPID) || !strcmp(appid, RCS_APPID)){ - ACCOUNT_DEBUG("ims service exception\n"); - *verified_appid = _account_get_text(appid); - return ACCOUNT_ERROR_NONE; - } + _GET_COOKIE - /* Get app id family which is stored in account database */ - int pkgmgr_ret = -1; - pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret); - } - pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret); - } - pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret); - } - pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret); - } + account_s *account_data = (account_s*) account; + int error_code = ACCOUNT_ERROR_NONE; + GError *error = NULL; - /* Compare current app id with the stored app id family */ - for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){ - char* tmp = (char*)iter->data; - if(tmp) { - if(account_type_query_app_id_exist(tmp) == ACCOUNT_ERROR_NONE) { - *verified_appid = _account_get_text(tmp); - error_code = ACCOUNT_ERROR_NONE; - _ACCOUNT_FREE(tmp); - if( *verified_appid == NULL ) { - ACCOUNT_ERROR("Memory Allocation Failed"); - error_code = ACCOUNT_ERROR_OUT_OF_MEMORY; - } - break; - } else { - ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid); - } - } - _ACCOUNT_FREE(tmp); - } + _INFO("2. Before _account_manager_get_instance()"); + AccountManager* acc_mgr = _account_manager_get_instance(); + ACCOUNT_CATCH_ERROR((acc_mgr != NULL), {}, ACCOUNT_ERROR_PERMISSION_DENIED, "Failed to get dbus."); - g_slist_free(appid_list); - pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret); - } + int db_id = -1; + GVariant* account_serialized = marshal_account(account_data); - pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret); - } + _INFO("3. Before account_manager_call_account_add_sync"); + bool is_success = account_manager_call_account_add_sync(acc_mgr, account_db_path, account_serialized, e_cookie, &db_id, NULL, &error); - return error_code; -} + _DESTROY_COOKIE -static int _account_check_appid_group_with_package_name(const char* appid, char* package_name) -{ - int error_code = ACCOUNT_ERROR_PERMISSION_DENIED; - pkgmgrinfo_appinfo_h ahandle=NULL; - pkgmgrinfo_pkginfo_h phandle=NULL; - char* package_id = NULL; - GSList* appid_list = NULL; - GSList* iter = NULL; - - if(!appid){ - ACCOUNT_ERROR("input param -appid is null\n"); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } + ACCOUNT_CATCH_ERROR((is_success != false), {}, _account_get_error_code(is_success, error), "Failed to get dbus."); + g_clear_error(&error); - if(!package_name){ - ACCOUNT_ERROR("input param - package name is null\n"); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } + *account_db_id = db_id; + account_data->id = db_id; - /* ims-service Exception */ - if ( strcmp(appid, "ims-service") == 0 && strcmp(package_name, "ims-service") == 0 ) { - ACCOUNT_DEBUG("ims exception."); // TODO: NEED TO REMOVE, debug log. - return ACCOUNT_ERROR_NONE; - } - - /* Get app id family which is stored in account database */ - int pkgmgr_ret = -1; - pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret); - } - pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret); - } - pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret); - } - pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret); - } - - /* Compare current app id with the stored app id family */ - for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){ - char* tmp = (char*)iter->data; - if(tmp) { - //ACCOUNT_ERROR("tmp(%s)package_name(%s)\n\n", tmp, package_name); // TODO: NEED TO REMOVE, debug log. - if( strcmp(tmp, package_name) == 0) { - error_code = ACCOUNT_ERROR_NONE; - _ACCOUNT_FREE(tmp); - break; - } else if ( strcmp(tmp, SAMSUNGACCOUNTFRONT_PKG_NAME) == 0 && - strcmp(package_name, SAMSUNGACCOUNT_PKG_NAME) == 0 ) { - /* Samung Account Exception */ - error_code = ACCOUNT_ERROR_NONE; - _ACCOUNT_FREE(tmp); - break; - } else { - ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid); - } - } - _ACCOUNT_FREE(tmp); - } + _INFO("4. account_insert_to_db end, added db id [%d] [%d] [%d]", db_id, *account_db_id, account_data->id); - g_slist_free(appid_list); - pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret); - } + return ACCOUNT_ERROR_NONE; - pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle); - if( pkgmgr_ret != PMINFO_R_OK ){ - ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret); - } +CATCH: + g_clear_error(&error); + //Failed to get dbus. + _ERR("account_manager_call_account_add_sync()=[%d]", error_code); return error_code; } -static const char *_account_db_err_msg() +ACCOUNT_API int account_delete_from_db_by_id(int account_db_id) { - return sqlite3_errmsg(g_hAccountDB); -} + _INFO("1. account_delete_from_db_by_id starting [%d]", account_db_id); + char* account_db_path = ACCOUNT_DB_NAME; + int error_code = ACCOUNT_ERROR_NONE; -static int _account_db_err_code() -{ - return sqlite3_errcode(g_hAccountDB); -} + ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO.")); -static void _account_insert_delete_update_notification_send(char *noti_name) -{ - if (!noti_name) { - ACCOUNT_ERROR("Noti Name is NULL!!!!!!\n"); - return; - } + _GET_COOKIE - ACCOUNT_DEBUG("noti_type = %s", noti_name); + GError *error = NULL; - if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) { - ACCOUNT_ERROR("Vconf MSG Str set FAILED !!!!!!\n");; + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } -} -static int _account_get_record_count(char* query) -{ - int rc = -1; - int ncount = 0; - account_stmt pStmt = NULL; + _INFO("2. Before account_manager_call_account_query_account_by_account_id_sync"); + GVariant *account_serialized_old = NULL; + bool is_success = account_manager_call_account_query_account_by_account_id_sync(acc_mgr, account_db_path, account_db_id, e_cookie, &account_serialized_old, NULL, &error); + if (!is_success) + { + _DESTROY_COOKIE - if(!query){ - ACCOUNT_ERROR("NULL query\n"); - return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR; + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("account_manager_call_account_query_account_by_account_id_sync failed [%d]", error_code); + return error_code; } + g_clear_error(&error); - if(!g_hAccountDB){ - ACCOUNT_ERROR("DB is not opened\n"); - return ACCOUNT_ERROR_DB_NOT_OPENED; - } + _INFO("3. Before account_manager_call_account_delete_from_db_by_id_sync"); + is_success = account_manager_call_account_delete_from_db_by_id_sync(acc_mgr, account_db_path, account_db_id, e_cookie, NULL, &error); - rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL); + _DESTROY_COOKIE - if (SQLITE_BUSY == rc){ - ACCOUNT_ERROR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); - sqlite3_finalize(pStmt); - return ACCOUNT_ERROR_DATABASE_BUSY; - } else if (SQLITE_OK != rc) { - ACCOUNT_ERROR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); - sqlite3_finalize(pStmt); - return ACCOUNT_ERROR_DB_FAILED; + if (!is_success) + { + error_code = _account_get_error_code(is_success, error); + _ERR("account_manager_call_account_delete_from_db_by_id_sync failed [%d]", error_code); + g_clear_error(&error); + return error_code; } + g_clear_error(&error); - rc = sqlite3_step(pStmt); - if (SQLITE_BUSY == rc) { - ACCOUNT_ERROR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg()); - sqlite3_finalize(pStmt); - return ACCOUNT_ERROR_DATABASE_BUSY; - } else if (SQLITE_ROW != rc) { - ACCOUNT_ERROR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg()); - sqlite3_finalize(pStmt); - return ACCOUNT_ERROR_DB_FAILED; - } + _INFO("4. Before account_delete_from_db_by_id end"); + return ACCOUNT_ERROR_NONE; +} - ncount = sqlite3_column_int(pStmt, 0); +ACCOUNT_API int account_delete_from_db_by_user_name(char *user_name, char *package_name) +{ + _INFO("account_delete_from_db_by_user_name start"); + char* account_db_path = ACCOUNT_DB_NAME; + int error_code = ACCOUNT_ERROR_NONE; - sqlite3_finalize(pStmt); + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!")); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!")); - return ncount; -} + _GET_COOKIE -static int _account_execute_query(const char *query) -{ - int rc = -1; - char* pszErrorMsg = NULL; + GError *error = NULL; - if(!query){ - ACCOUNT_ERROR("NULL query\n"); - return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR; + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - if(!g_hAccountDB){ - ACCOUNT_ERROR("DB is not opened\n"); - return ACCOUNT_ERROR_DB_NOT_OPENED; + GVariant* account_list_variant = NULL; + bool is_success = account_manager_call_account_query_account_by_user_name_sync(acc_mgr, account_db_path, user_name, e_cookie, &account_list_variant, NULL, &error); + + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + if (error_code != ACCOUNT_ERROR_NONE) + { + _DESTROY_COOKIE + + _ERR("account_query_account_by_user_name error=[%d]", error_code); + return error_code; } - rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg); - if (SQLITE_OK != rc) { - ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg); - sqlite3_free(pszErrorMsg); + GSList* account_list = unmarshal_account_list(account_list_variant); + g_variant_unref(account_list_variant); + + if (account_list == NULL) + { + return ACCOUNT_ERROR_NO_DATA; } - return rc; -} + //TODO free account_list, account_list_variant -static int _account_begin_transaction(void) -{ - int ret = -1; + is_success = account_manager_call_account_delete_from_db_by_user_name_sync(acc_mgr, account_db_path, user_name, package_name, e_cookie, NULL, &error); - ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION"); + _DESTROY_COOKIE - if (ret == SQLITE_BUSY){ - ACCOUNT_ERROR(" sqlite3 busy = %d", ret); - return ACCOUNT_ERROR_DATABASE_BUSY; - } else if(ret != SQLITE_OK) { - ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret); - return ACCOUNT_ERROR_DB_FAILED; + if (!is_success) + { + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("account_manager_call_account_delete_from_db_by_user_name_sync failed [%d]", error_code); + _account_gslist_account_free(account_list); + return error_code; } + g_clear_error(&error); - return ACCOUNT_ERROR_NONE; + _account_gslist_account_free(account_list); + return ACCOUNT_ERROR_NONE; } -static int _account_end_transaction(bool is_success) +int _account_delete_from_db_by_package_name(const char *package_name, bool permission) { - int ret = -1; + _INFO("_account_delete_from_db_by_package_name starting permission opions = %d", permission); + char* account_db_path = ACCOUNT_DB_NAME; + int error_code = ACCOUNT_ERROR_NONE; - if (is_success == true) { - ret = _account_execute_query("COMMIT TRANSACTION"); - } else { - ret = _account_execute_query("ROLLBACK TRANSACTION"); - } + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!")); - if(ret == SQLITE_PERM){ - ACCOUNT_ERROR("Account permission denied :: %d", ret); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + _GET_COOKIE - if (ret == SQLITE_BUSY){ - ACCOUNT_DEBUG(" sqlite3 busy = %d", ret); - return ACCOUNT_ERROR_DATABASE_BUSY; - } + GError *error = NULL; - if (ret != SQLITE_OK) { - ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret); - return ACCOUNT_ERROR_DB_FAILED; + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - return ACCOUNT_ERROR_NONE; -} - -static bool _account_check_add_more_account(const char* app_id) -{ - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; + //First get account list of user_name, used for gSSO DB deletion + GVariant* account_list_variant = NULL; + bool is_success = account_manager_call_account_query_account_by_package_name_sync(acc_mgr, account_db_path, package_name, e_cookie, &account_list_variant, NULL, &error); - ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + if (error_code != ACCOUNT_ERROR_NONE) + { + _DESTROY_COOKIE - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + return error_code; + } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id); - rc = _account_get_record_count(query); + GSList* account_list = unmarshal_account_list(account_list_variant); + g_variant_unref(account_list_variant); - /* multiple account support case */ - if(rc > 0) { - ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc); - return TRUE; + if (account_list == NULL) + { + return ACCOUNT_ERROR_NO_DATA; } - /* multiple account not support case */ - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id); - rc = _account_get_record_count(query); + is_success = account_manager_call_account_delete_from_db_by_package_name_sync(acc_mgr, account_db_path, package_name, permission, e_cookie, NULL, &error); - if(rc <= 0) { - ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id); - return TRUE; + _DESTROY_COOKIE + + if (!is_success) + { + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("account_manager_call_account_delete_from_db_by_package_name_sync failed [%d]", error_code); + _account_gslist_account_free(account_list); + return error_code; } + g_clear_error(&error); - return FALSE; + _account_gslist_account_free(account_list); + return ACCOUNT_ERROR_NONE; } -static int _account_create_all_tables(void) +ACCOUNT_API int account_delete_from_db_by_package_name(const char *package_name) { - int rc = -1; - int error_code = ACCOUNT_ERROR_NONE; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - - ACCOUNT_DEBUG("create all table - BEGIN"); - ACCOUNT_MEMSET(query, 0, sizeof(query)); - - /*Create the account table*/ - ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE); - rc = _account_get_record_count(query); - if (rc <= 0) { - rc = _account_execute_query(ACCOUNT_SCHEMA); - if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; - ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", ACCOUNT_SCHEMA, rc, _account_db_err_msg())); - } - - /*Create capability table*/ - ACCOUNT_MEMSET(query, 0, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE); - rc = _account_get_record_count(query); - if (rc <= 0) { - rc = _account_execute_query(CAPABILITY_SCHEMA); - if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; - ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", CAPABILITY_SCHEMA, rc, _account_db_err_msg())); - } - - /* Create account custom table */ - ACCOUNT_MEMSET(query, 0, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE); - rc = _account_get_record_count(query); - if (rc <= 0) { - rc = _account_execute_query(ACCOUNT_CUSTOM_SCHEMA); - if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; - ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg())); - } - - /* Create account type table */ - ACCOUNT_MEMSET(query, 0, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE); - rc = _account_get_record_count(query); - if (rc <= 0) { - rc = _account_execute_query(ACCOUNT_TYPE_SCHEMA); - if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; - ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", ACCOUNT_TYPE_SCHEMA, rc, _account_db_err_msg())); - } - - /* Create label table */ - ACCOUNT_MEMSET(query, 0, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE); - rc = _account_get_record_count(query); - if (rc <= 0) { - rc = _account_execute_query(LABEL_SCHEMA); - if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; - ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", LABEL_SCHEMA, rc, _account_db_err_msg())); - } - - /* Create account feature table */ - ACCOUNT_MEMSET(query, 0, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE); - rc = _account_get_record_count(query); - if (rc <= 0) { - rc = _account_execute_query(PROVIDER_FEATURE_SCHEMA); - if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; - ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", PROVIDER_FEATURE_SCHEMA, rc, _account_db_err_msg())); - } - - ACCOUNT_DEBUG("create all table - END"); - return error_code; + _INFO("account_delete_from_db_by_package_name starting with permission"); + return _account_delete_from_db_by_package_name(package_name, true); } -static int _account_check_is_all_table_exists() +ACCOUNT_API int account_update_to_db_by_id(account_h account, int account_id) { - int rc = 0; - char query[ACCOUNT_SQL_LEN_MAX] = {0,}; - ACCOUNT_MEMSET(query, 0, sizeof(query)); + //First we will update account db + _INFO("1. account_update_to_db_by_id start"); + char* account_db_path = ACCOUNT_DB_NAME; + int error_code = ACCOUNT_ERROR_NONE; - ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')", - ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE); - rc = _account_get_record_count(query); + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid")); - if (rc != ACCOUNT_TABLE_TOTAL_COUNT) { - ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc); - } + _GET_COOKIE - return rc; -} + GError *error = NULL; -static int _account_db_open(int mode) -{ - int rc = 0; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - - if (!g_hAccountDB) { - if(mode == ACCOUNT_DB_OPEN_READWRITE) - rc = db_util_open(ACCOUNT_DB_NAME, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD); - else if(mode == ACCOUNT_DB_OPEN_READONLY) - rc = db_util_open_with_options(ACCOUNT_DB_NAME, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL); - else - return ACCOUNT_ERROR_DB_NOT_OPENED; - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _INFO("2. Before account_manager_call_account_query_account_by_account_id_sync"); + GVariant *account_serialized_old = NULL; + bool is_success = account_manager_call_account_query_account_by_account_id_sync(acc_mgr, account_db_path, account_id, e_cookie, &account_serialized_old, NULL, &error); + if (!is_success) + { + _DESTROY_COOKIE - ACCOUNT_RETURN_VAL((rc != SQLITE_PERM), {}, ACCOUNT_ERROR_PERMISSION_DENIED, ("Account permission denied rc : %d", rc)); - ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc)); - g_refCntDB++; - } else { - g_refCntDB++; + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("account_manager_call_account_query_account_by_account_id_sync failed [%d]", error_code); + return error_code; } + g_clear_error(&error); - ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("busy handler fail. rc : %d", rc)); + _INFO("3. Before account_manager_call_account_update_to_db_by_id_sync"); + GVariant* account_serialized = marshal_account((account_s*) account); + is_success = account_manager_call_account_update_to_db_by_id_sync(acc_mgr, account_db_path, account_serialized, account_id, e_cookie, NULL, &error); + + _DESTROY_COOKIE + + if (!is_success) + { + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("account_manager_call_account_update_to_db_by_id_sync failed [%d]", error_code); + return error_code; + } + g_clear_error(&error); + _INFO("4. account_update_to_db_by_id end"); return ACCOUNT_ERROR_NONE; } -static int _account_db_close(void) +ACCOUNT_API int account_update_to_db_by_id_ex(account_h account, int account_id) { - int rc = 0; int ret = -1; - - if (g_hAccountDB) { - if (g_refCntDB > 0) { - g_refCntDB--; - } - if (g_refCntDB == 0) { - rc = db_util_close(g_hAccountDB); - if( rc == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(SQLITE_PERM)"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } else if ( rc == SQLITE_BUSY ){ - ACCOUNT_ERROR( "database busy"); - return ACCOUNT_ERROR_DATABASE_BUSY; - } - ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc)); - g_hAccountDB = NULL; - } - ret = ACCOUNT_ERROR_NONE; - } else { - ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB); - ret = ACCOUNT_ERROR_DB_FAILED; - } + ret = account_update_to_db_by_id(account, account_id); return ret; } -static int _account_connect(int mode) +ACCOUNT_INTERNAL_API int account_update_to_db_by_id_without_permission(account_h account, int account_id) { + //First we will update account db + //Then we will update gSSO DB, if it fails then we will rollback account db updates + + _INFO("account_update_to_db_by_id_without_permission start"); + char* account_db_path = ACCOUNT_DB_NAME; int error_code = ACCOUNT_ERROR_NONE; - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid")); - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + _GET_COOKIE - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + GError *error = NULL; - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - pthread_mutex_lock(&account_mutex); + GVariant *account_serialized_old = NULL; + _INFO("before query() account_id[%d]", account_id); + bool is_success = account_manager_call_account_query_account_by_account_id_sync(acc_mgr, account_db_path, account_id, e_cookie, &account_serialized_old, NULL, &error); + if (!is_success) + { + _DESTROY_COOKIE - error_code = _account_db_open(mode); - if (ACCOUNT_ERROR_NONE != error_code) { - ACCOUNT_ERROR("The database isn't connected.\n"); - pthread_mutex_unlock(&account_mutex); + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("account_manager_call_account_query_account_by_account_id_sync failed [%d]", error_code); + return error_code; + } + g_clear_error(&error); - if(error_code == ACCOUNT_ERROR_PERMISSION_DENIED) - return error_code; - else - return ACCOUNT_ERROR_DB_NOT_OPENED; + _INFO("before marshal() : account_id[%d], user_name=%s", account_id, ((account_s*)account)->user_name); + GVariant* account_serialized = marshal_account((account_s*) account); + _INFO("after marshal() : account_id[%d]", account_id); + if (account_serialized == NULL) + { + _ERR("Invalid input"); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - int rc = _account_check_is_all_table_exists(); + _INFO("before call update() : account_id[%d]", account_id); + is_success = account_manager_call_account_update_to_db_by_id_ex_sync(acc_mgr, account_db_path, account_serialized, account_id, e_cookie, NULL, &error); + _INFO("after call update() : is_success=%d", is_success); + + _DESTROY_COOKIE - if ( rc < 0 ){ - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR("rc(%d)", rc); - return rc; - } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT){ - // do nothing - ACCOUNT_DEBUG("Tables OK"); - } else { - error_code = _account_create_all_tables(); + if (!is_success) + { + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("account_manager_call_account_update_to_db_by_id_ex_sync failed [%d]", error_code); + return error_code; } + g_clear_error(&error); - pthread_mutex_unlock(&account_mutex); return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_connect (void) -{ - return _account_connect(ACCOUNT_DB_OPEN_READWRITE); -} - -ACCOUNT_API int account_connect_readonly (void) -{ - return _account_connect(ACCOUNT_DB_OPEN_READONLY); -} - -static int _account_disconnect(void) +ACCOUNT_API int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name) { + //First we will update account db + _INFO("account_update_to_db_by_user_name starting"); + char* account_db_path = ACCOUNT_DB_NAME; int error_code = ACCOUNT_ERROR_NONE; - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL")); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL")); - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + _GET_COOKIE - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + GError *error = NULL; - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - pthread_mutex_lock(&account_mutex); - - error_code = _account_db_close(); - pthread_mutex_unlock(&account_mutex); - - return error_code; -} + GVariant *account_serialized_old = NULL; + account_s *account_data = (account_s*) account; + bool is_success = account_manager_call_account_query_account_by_account_id_sync(acc_mgr, account_db_path, account_data->id, e_cookie, &account_serialized_old, NULL, &error); + if (!is_success) + { + _DESTROY_COOKIE -ACCOUNT_API int account_disconnect (void) -{ - return _account_disconnect(); -} + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("account_manager_call_account_query_account_by_account_id_sync failed [%d]", error_code); + return error_code; + } + g_clear_error(&error); -static int _account_free_capability_items(account_capability_s *data) -{ - _ACCOUNT_FREE(data->type); - _ACCOUNT_FREE(data->package_name); - _ACCOUNT_FREE(data->user_name); + GVariant* account_serialized = marshal_account(account_data); + is_success = account_manager_call_account_update_to_db_by_user_name_sync(acc_mgr, account_db_path, account_serialized, user_name, package_name, e_cookie, NULL, &error); - return ACCOUNT_ERROR_NONE; -} + _DESTROY_COOKIE -static int _account_custom_item_free(account_custom_s *data) -{ - _ACCOUNT_FREE(data->app_id); - _ACCOUNT_FREE(data->key); - _ACCOUNT_FREE(data->value); + if (!is_success) + { + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("account_manager_call_account_update_to_db_by_user_name_sync failed [%d]", error_code); + return error_code; + } + g_clear_error(&error); return ACCOUNT_ERROR_NONE; } -static int _account_custom_gslist_free(GSList* list) +ACCOUNT_API int account_create(account_h *account) { - if(!list){ + _INFO("account_create start"); + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__); return ACCOUNT_ERROR_INVALID_PARAMETER; } - GSList* iter; + account_s *data = (account_s*)malloc(sizeof(account_s)); - for (iter = list; iter != NULL; iter = g_slist_next(iter)) { - account_custom_s *custom_data = (account_custom_s*)iter->data; - _account_custom_item_free(custom_data); - _ACCOUNT_FREE(custom_data); + if (data == NULL) { + ACCOUNT_FATAL("Memory Allocation Failed"); + return ACCOUNT_ERROR_OUT_OF_MEMORY; } + ACCOUNT_MEMSET(data, 0, sizeof(account_s)); - g_slist_free(list); - list = NULL; + /*Setting account as visible by default*/ + data->secret = ACCOUNT_SECRECY_VISIBLE; - return ACCOUNT_ERROR_NONE; -} + /*Setting account as not supporting sync by default*/ + data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT; -static int _account_free_account_items(account_s *data) -{ - _ACCOUNT_FREE(data->user_name); - _ACCOUNT_FREE(data->email_address); - _ACCOUNT_FREE(data->display_name); - _ACCOUNT_FREE(data->icon_path); - _ACCOUNT_FREE(data->source); - _ACCOUNT_FREE(data->package_name); - _ACCOUNT_FREE(data->domain_name); - _ACCOUNT_FREE(data->access_token); + data->auth_type = ACCOUNT_AUTH_TYPE_INVALID; - int i; - for(i=0;iuser_data_txt[i]); +// data->account_list = NULL; + data->capablity_list = NULL; + data->custom_list = NULL; +// data->domain_list = NULL; +// data->mechanism_list = NULL; - _account_gslist_free(data->capablity_list); - _account_glist_free(data->account_list); - _account_custom_gslist_free(data->custom_list); + *account = (account_h)data; + _INFO("account_create end"); return ACCOUNT_ERROR_NONE; } -static int _account_gslist_free(GSList* list) +ACCOUNT_API int account_destroy(account_h account) { - if(!list){ - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - GSList* iter; + _INFO("account_destroy start"); + account_s *data = (account_s*)account; - for (iter = list; iter != NULL; iter = g_slist_next(iter)) { - account_capability_s *cap_data = (account_capability_s*)iter->data; - _account_free_capability_items(cap_data); - _ACCOUNT_FREE(cap_data); - } + ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!")); - g_slist_free(list); - list = NULL; + _account_free_account_with_items(data); + _INFO("account_destroy end"); return ACCOUNT_ERROR_NONE; } -static int _account_glist_free(GList* list) +ACCOUNT_API int account_set_user_name(account_h account, const char *user_name) { - if(!list){ + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); return ACCOUNT_ERROR_INVALID_PARAMETER; } - GList* iter; - - for (iter = list; iter != NULL; iter = g_list_next(iter)) { - account_s *account_record = (account_s*)iter->data; - _account_free_account_items(account_record); - _ACCOUNT_FREE(account_record); + if (!user_name) { + ACCOUNT_SLOGE("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - g_list_free(list); - list = NULL; + account_s *data = (account_s*)account; + + _ACCOUNT_FREE(data->user_name); + data->user_name = _account_get_text(user_name); return ACCOUNT_ERROR_NONE; } -static gboolean _account_check_duplicated(account_s *data) +ACCOUNT_API int account_set_display_name(account_h account, const char *display_name) { -#if 0 - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int count = 0; - - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - - ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and package_name='%s'" - , ACCOUNT_TABLE, data->user_name, data->package_name); - - count = _account_get_record_count(query); - if (count > 0) { - ACCOUNT_SLOGI("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n", - count, data->user_name, data->domain_name ); - return TRUE; + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } -#endif - return FALSE; - -} -static int _account_get_next_sequence(const char *pszName) -{ - int rc = 0; - account_stmt pStmt = NULL; - int max_seq = 0; - char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,}; - - ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery)); - ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName); - rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL); - if (SQLITE_OK != rc) { - ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); - sqlite3_finalize(pStmt); - return ACCOUNT_ERROR_DB_FAILED; + if (!display_name) { + ACCOUNT_SLOGE("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - rc = sqlite3_step(pStmt); - max_seq = sqlite3_column_int(pStmt, 0); - max_seq++; + account_s *data = (account_s*)account; - /*Finalize Statement*/ - rc = sqlite3_finalize(pStmt); - pStmt = NULL; + _ACCOUNT_FREE(data->display_name); + data->display_name = _account_get_text(display_name); - return max_seq; + return ACCOUNT_ERROR_NONE; } -static account_stmt _account_prepare_query(char *query) +ACCOUNT_API int account_set_email_address(account_h account, const char *email_address) { - int rc = -1; - account_stmt pStmt = NULL; + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL")); + if (!email_address) { + ACCOUNT_SLOGE("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL); + account_s *data = (account_s*)account; - ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg())); + _ACCOUNT_FREE(data->email_address); + data->email_address = _account_get_text(email_address); - return pStmt; + return ACCOUNT_ERROR_NONE; } -static int _account_query_bind_int(account_stmt pStmt, int pos, int num) +ACCOUNT_API int account_set_icon_path(account_h account, const char *icon_path) { - if(!pStmt){ - ACCOUNT_ERROR("statement is null"); - return -1; + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - if(pos < 0){ - ACCOUNT_ERROR("invalid pos"); - return -1; + if (!icon_path) { + ACCOUNT_SLOGE("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - return sqlite3_bind_int(pStmt, pos, num); -} + account_s *data = (account_s*)account; -static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str) -{ - if(!pStmt){ - ACCOUNT_ERROR("statement is null"); - return -1; - } + _ACCOUNT_FREE(data->icon_path); + data->icon_path = _account_get_text(icon_path); - if(str) - return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC); - else - return sqlite3_bind_null(pStmt, pos); + return ACCOUNT_ERROR_NONE; } -static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value) +ACCOUNT_API int account_set_source(account_h account, const char *source) { - int count = 1; - - /*Caution : Keep insert query orders.*/ - - /* 1. user name*/ - _account_query_bind_text(hstmt, count++, (char*)account->user_name); - - /* 2. email address*/ - _account_query_bind_text(hstmt, count++, (char*)account->email_address); - - /* 3. display name*/ - _account_query_bind_text(hstmt, count++, (char*)account->display_name); - - /* 4. icon path*/ - _account_query_bind_text(hstmt, count++, (char*)account->icon_path); - - /* 5. source*/ - _account_query_bind_text(hstmt, count++, (char*)account->source); - - /* 6. package name*/ - _account_query_bind_text(hstmt, count++, (char*)account->package_name); - - /* 7. access token*/ - _account_query_bind_text(hstmt, count++, (char*)account->access_token); + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - /* 8. domain name*/ - _account_query_bind_text(hstmt, count++, (char*)account->domain_name); + if (!source) { + ACCOUNT_SLOGE("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + account_s *data = (account_s*)account; - /* 9. auth type*/ - _account_query_bind_int(hstmt, count++, account->auth_type); + _ACCOUNT_FREE(data->source); + data->source = _account_get_text(source); - /* 10. secret */ - _account_query_bind_int(hstmt, count++, account->secret); + return ACCOUNT_ERROR_NONE; +} - /* 11. sync_support */ - _account_query_bind_int(hstmt, count++, account->sync_support); +ACCOUNT_API int account_set_package_name(account_h account, const char *package_name) +{ + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - int i; + if (!package_name) { + ACCOUNT_SLOGE("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - /* 12. user text*/ - for(i=0; i< USER_TXT_CNT; i++) - _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]); + account_s *data = (account_s*)account; - /* 13. user integer */ - for(i=0; i< USER_INT_CNT; i++) - _account_query_bind_int(hstmt, count++, account->user_data_int[i]); + _ACCOUNT_FREE(data->package_name); + data->package_name = _account_get_text(package_name); - return count; + return ACCOUNT_ERROR_NONE; } -static int _account_query_finalize(account_stmt pStmt) +ACCOUNT_API int account_set_domain_name(account_h account, const char *domain_name) { - int rc = -1; - - if (!pStmt) { - ACCOUNT_ERROR( "pStmt is NULL"); + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); return ACCOUNT_ERROR_INVALID_PARAMETER; } - rc = sqlite3_finalize(pStmt); - if (rc == SQLITE_BUSY){ - ACCOUNT_ERROR(" sqlite3 busy = %d", rc); - return ACCOUNT_ERROR_DATABASE_BUSY; - } else if (rc != SQLITE_OK) { - ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_DB_FAILED; + if (!domain_name) { + ACCOUNT_SLOGE("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } + account_s *data = (account_s*)account; + + _ACCOUNT_FREE(data->domain_name); + data->domain_name = _account_get_text(domain_name); return ACCOUNT_ERROR_NONE; } -static int _account_query_step(account_stmt pStmt) +ACCOUNT_API int account_set_access_token(account_h account, const char *access_token) { - if(!pStmt){ - ACCOUNT_ERROR( "pStmt is NULL"); - return -1; + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - return sqlite3_step(pStmt); -} - -static int _account_execute_insert_query(account_s *account) -{ - int rc = 0; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - - /* check whether app id exist in account type db */ - - if (!account->user_name && !account->display_name && !account->email_address) { - ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n"); + if (!access_token) { + ACCOUNT_SLOGE("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__); return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , " - "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, " - "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa - "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?)", ACCOUNT_TABLE); + account_s *data = (account_s*)account; - hstmt = _account_prepare_query(query); - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + _ACCOUNT_FREE(data->access_token); + data->access_token = _account_get_text(access_token); - _account_convert_account_to_sql(account, hstmt, query); + return ACCOUNT_ERROR_NONE; +} - rc = _account_query_step(hstmt); - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); +ACCOUNT_API int account_set_user_text(account_h account, int idx, const char *user_txt) +{ + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - if( _account_db_err_code() == SQLITE_PERM ) - error_code = ACCOUNT_ERROR_PERMISSION_DENIED; - else - error_code = ACCOUNT_ERROR_DB_FAILED; + if (!user_txt) { + ACCOUNT_SLOGE("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + if (idx >= USER_TXT_CNT || idx < 0) { + ACCOUNT_SLOGE("(%s)-(%d) idx rage should be between 0-4.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + account_s *data = (account_s*)account; - return error_code; + _ACCOUNT_FREE(data->user_data_txt[idx]); + data->user_data_txt[idx] = _account_get_text(user_txt); + + return ACCOUNT_ERROR_NONE; } -static int _account_insert_capability(account_s *account, int account_id) +ACCOUNT_API int account_set_custom(account_h account, const char* key, const char* value) { - int rc, count = 1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - account_stmt hstmt = NULL; - - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); - - if (g_slist_length( account->capablity_list)==0) { - ACCOUNT_DEBUG( "_account_insert_capability, no capability\n"); - return ACCOUNT_ERROR_NONE; + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id); - - rc = _account_get_record_count(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; + if (!key) { + ACCOUNT_SLOGE("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - if (rc <= 0) { - return ACCOUNT_ERROR_RECORD_NOT_FOUND; + + if (!value) { + ACCOUNT_SLOGE("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - /* insert query*/ + account_s *data = (account_s*)account; GSList *iter; + bool b_is_new = TRUE; - for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) { - int ret; - count = 1; - - account_capability_s* cap_data = NULL; - cap_data = (account_capability_s*)iter->data; - - ACCOUNT_SLOGD("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value); - - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES " - "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE); - hstmt = _account_prepare_query(query); - - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); - - ret = _account_query_bind_text(hstmt, count++, cap_data->type); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_int(hstmt, count++, cap_data->value); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_int(hstmt, count++, (int)account_id); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) { - rc = _account_query_step(hstmt); + account_custom_s* custom_data = NULL; + custom_data = (account_custom_s*)iter->data; + ACCOUNT_SLOGD( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value); - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - break; + if(!strcmp(custom_data->key, key)) { + _ACCOUNT_FREE(custom_data->value); + custom_data->value = _account_get_text(value); + b_is_new = FALSE; } + } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + if(b_is_new) { + account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s)); + if (custom_data == NULL) { + return ACCOUNT_ERROR_OUT_OF_MEMORY; + } + ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s)); + custom_data->account_id = data->id; + custom_data->app_id = _account_get_text(data->package_name); + custom_data->key = _account_get_text(key); + custom_data->value = _account_get_text(value); + data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data); } return ACCOUNT_ERROR_NONE; } -static int _account_update_capability(account_s *account, int account_id) +ACCOUNT_API int account_set_auth_type(account_h account, const account_auth_type_e auth_type) { - int rc, count = 1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - account_stmt hstmt = NULL; - - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__)); - if (g_slist_length( account->capablity_list)==0) { - ACCOUNT_ERROR( "_account_update_capability, no capability\n"); - return ACCOUNT_ERROR_NONE; + if ( ((int)auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id); - - rc = _account_get_record_count(query); + account_s *data = (account_s*)account; - if (rc <= 0) { - ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } + data->auth_type = (int)auth_type; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + return ACCOUNT_ERROR_NONE; +} - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE); - hstmt = _account_prepare_query(query); - count = 1; - _account_query_bind_int(hstmt, count++, (int)account_id); - rc = _account_query_step(hstmt); +ACCOUNT_API int account_set_secret(account_h account, const account_secrecy_state_e secret) +{ + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__)); - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_DB_FAILED; + if ( ((int)secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - GSList *iter; - for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) { - int ret; - count = 1; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES " - "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE); + account_s *data = (account_s*)account; - hstmt = _account_prepare_query(query); + data->secret = (int)secret; - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + return ACCOUNT_ERROR_NONE; +} - account_capability_s* cap_data = NULL; - cap_data = (account_capability_s*)iter->data; +ACCOUNT_API int account_set_sync_support(account_h account, const account_sync_state_e sync_support) +{ + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__)); - ret = _account_query_bind_text(hstmt, count++, cap_data->type); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_int(hstmt, count++, cap_data->value); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_int(hstmt, count++, (int)account_id); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); - - rc = _account_query_step(hstmt); - - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - break; - } + if ( ((int)sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + account_s *data = (account_s*)account; - } + data->sync_support= (int)sync_support; return ACCOUNT_ERROR_NONE; } -static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name ) +ACCOUNT_API int account_set_user_int(account_h account, int idx, const int user_int) { - int rc, count = 1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - account_stmt hstmt = NULL; - - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + if (!account) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - if (g_slist_length( account->capablity_list)==0) { - ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n"); - return ACCOUNT_ERROR_NONE; + if (idx >= USER_INT_CNT ||idx < 0) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name); + account_s *data = (account_s*)account; - rc = _account_get_record_count(query); - - if (rc <= 0) { - ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } - - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE); - hstmt = _account_prepare_query(query); - count = 1; - _account_query_bind_text(hstmt, count++, (char*)account->package_name); - _account_query_bind_text(hstmt, count++, (char*)account->user_name); - rc = _account_query_step(hstmt); - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_DB_FAILED; - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - GSList* iter; - - for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) { - int ret; - count = 1; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES " - "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE); - - hstmt = _account_prepare_query(query); - - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); - - account_capability_s* cap_data = NULL; - cap_data = (account_capability_s*)iter->data; - - ret = _account_query_bind_text(hstmt, count++, cap_data->type); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_int(hstmt, count++, cap_data->value); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_int(hstmt, count++, (int)account->id); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); - - rc = _account_query_step(hstmt); - - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - break; - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - } + data->user_data_int[idx] = user_int; return ACCOUNT_ERROR_NONE; } -static int _account_query_table_column_int(account_stmt pStmt, int pos) -{ - if(!pStmt){ - ACCOUNT_ERROR("statement is null"); - return -1; - } - - if(pos < 0){ - ACCOUNT_ERROR("invalid pos"); - return -1; - } - - return sqlite3_column_int(pStmt, pos); -} - -static const char *_account_query_table_column_text(account_stmt pStmt, int pos) -{ - if(!pStmt){ - ACCOUNT_ERROR("statement is null"); - return NULL; - } - - if(pos < 0){ - ACCOUNT_ERROR("invalid pos"); - return NULL; - } - - return (const char *)sqlite3_column_text(pStmt, pos); -} - -static void _account_db_data_to_text(const char *textbuf, char **output) -{ - if (textbuf && strlen(textbuf)>0) { - if (*output) { - free(*output); - *output = NULL; - } - *output = strdup(textbuf); - } -} - -static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record) -{ - const char *textbuf = NULL; - - account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME); - _account_db_data_to_text(textbuf, &(account_record->user_name)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS); - _account_db_data_to_text(textbuf, &(account_record->email_address)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME); - _account_db_data_to_text(textbuf, &(account_record->display_name)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH); - _account_db_data_to_text(textbuf, &(account_record->icon_path)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE); - _account_db_data_to_text(textbuf, &(account_record->source)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME); - _account_db_data_to_text(textbuf, &(account_record->package_name)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN); - _account_db_data_to_text(textbuf, &(account_record->access_token)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME); - _account_db_data_to_text(textbuf, &(account_record->domain_name)); - - account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE); - - account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET); - - account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0); - _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0])); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1); - _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1])); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2); - _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2])); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3); - _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3])); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4); - _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4])); - - account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0); - account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1); - account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2); - account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3); - account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4); -} - -static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record) -{ - const char *textbuf = NULL; - - capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID); - - textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY); - _account_db_data_to_text(textbuf, &(capability_record->type)); - - capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE); - - textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME); - _account_db_data_to_text(textbuf, &(capability_record->package_name)); - - textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME); - _account_db_data_to_text(textbuf, &(capability_record->user_name)); - - capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID); -} - -static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record) -{ - const char *textbuf = NULL; - - custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID); - _account_db_data_to_text(textbuf, &(custom_record->app_id)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY); - _account_db_data_to_text(textbuf, &(custom_record->key)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE); - _account_db_data_to_text(textbuf, &(custom_record->value)); -} - -bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data) -{ - account_s *data = (account_s*)user_data; - - account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s)); - - if (cap_data == NULL) - return FALSE; - ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s)); - - cap_data->type = _account_get_text(capability_type); - cap_data->value = capability_value; - - data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data); - - return TRUE; -} - - -bool _account_get_custom_text_cb(char* key, char* value, void *user_data) -{ - account_s *data = (account_s*)user_data; - - account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s)); - - if (custom_data == NULL) { - ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n"); - return FALSE; - } - ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s)); - - custom_data->account_id = data->id; - custom_data->app_id = _account_get_text(data->package_name); - custom_data->key = _account_get_text(key); - custom_data->value = _account_get_text(value); - - data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data); - - return TRUE; -} - - -static char *_account_get_text(const char *text_data) -{ - char *text_value = NULL; - - if (text_data != NULL) { - text_value = strdup(text_data); - } - return text_value; -} - -static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name) +ACCOUNT_API int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value) { - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - account_s *old_account = NULL; - - ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); - ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL")); - ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - old_account = (account_s*)calloc(1, sizeof(account_s)); - if(!old_account) { - ACCOUNT_FATAL("Memory alloc fail\n"); - return ACCOUNT_ERROR_OUT_OF_MEMORY; - } - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name); - hstmt = _account_prepare_query(query); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - while (rc == SQLITE_ROW) { - _account_convert_column_to_account(hstmt, old_account); - rc = _account_query_step(hstmt); - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - // get capability - error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account); - ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error")); - - // get custom text - error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account); - ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error")); - - // compare - new_account->id = old_account->id; - - //user name - if(!new_account->user_name) { - if(old_account->user_name) - new_account->user_name = _account_get_text(old_account->user_name); - } - - // display name - if(!new_account->display_name) { - if(old_account->display_name) - new_account->display_name = _account_get_text(old_account->display_name); - } - - // email address - if(!new_account->email_address) { - if(old_account->email_address) - new_account->email_address = _account_get_text(old_account->email_address); - } - - // domain name - if(!new_account->domain_name) { - if(old_account->domain_name) - new_account->domain_name = _account_get_text(old_account->domain_name); - } - - // icon path - if(!new_account->icon_path) { - if(old_account->icon_path) - new_account->icon_path = _account_get_text(old_account->icon_path); - } - - // source - if(!new_account->source) { - if(old_account->source) - new_account->source = _account_get_text(old_account->source); - } - - _ACCOUNT_FREE(new_account->package_name); - new_account->package_name = _account_get_text(old_account->package_name); - - // access token - if(!new_account->access_token) { - if(old_account->access_token) - new_account->access_token = _account_get_text(old_account->access_token); - } + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null")); + ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null")); - // user text - int i; - for(i=0;iuser_data_txt[i]) { - if(old_account->user_data_txt[i]) - new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]); - } + if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - // auth type - if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) { - new_account->auth_type = old_account->auth_type; - } + account_s *data = (account_s*)account; - //secret - if(new_account->secret== ACCOUNT_SECRECY_INVALID) { - new_account->secret = old_account->secret; - } + GSList *iter = NULL; + bool b_is_new = TRUE; - // sync support - if(new_account->sync_support == ACCOUNT_SYNC_INVALID) { - new_account->sync_support = old_account->sync_support; - } + for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) { + account_capability_s *cap_data = NULL; + cap_data = (account_capability_s*)iter->data; - // user int - for(i=0;iuser_data_int[i] == 0) { - new_account->user_data_int[i] = old_account->user_data_int[i]; + if(!strcmp(cap_data->type, capability_type)) { + cap_data->value = capability_value; + b_is_new = FALSE; + break; } } - // capability - - // user custom table - - + if(b_is_new) { + account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s)); - CATCH: - if (old_account) { - _account_free_account_items(old_account); - _ACCOUNT_FREE(old_account); - } + if (cap_data == NULL) + return ACCOUNT_ERROR_OUT_OF_MEMORY; + ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s)); - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + cap_data->type = _account_get_text(capability_type); + cap_data->value = capability_value; + data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data); } return ACCOUNT_ERROR_NONE; } - - -static int _account_update_account_by_user_name(account_s *account, const char *user_name, const char *package_name) +ACCOUNT_API int account_get_user_name(account_h account, char **user_name) { - int rc = 0, binding_count = 0, count = 0; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - - ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n")); - ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n")); - - char* current_appid = NULL; - char* verified_appid = NULL; - - current_appid = _account_get_current_appid(); - error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid); - - _ACCOUNT_FREE(current_appid); - _ACCOUNT_FREE(verified_appid); - - if(error_code != ACCOUNT_ERROR_NONE){ - ACCOUNT_ERROR("No permission to update\n"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - _account_compare_old_record_by_user_name(account, user_name, package_name); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if (!account->package_name) { - ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n"); + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - if (!account->user_name && !account->display_name && !account->email_address) { - ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); + if (!user_name) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'" - , ACCOUNT_TABLE, user_name, package_name); - - count = _account_get_record_count(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if (count <= 0) { - ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n", - count, user_name, package_name); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } - - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " - "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," - "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " - "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE); - - hstmt = _account_prepare_query(query); - if( _account_db_err_code() == SQLITE_PERM ){ - _account_end_transaction(FALSE); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg())); - - binding_count = _account_convert_account_to_sql(account, hstmt, query); - - _account_query_bind_text(hstmt, binding_count++, user_name); - _account_query_bind_text(hstmt, binding_count++, package_name); - rc = _account_query_step(hstmt); - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - /*update capability*/ - error_code = _account_update_capability_by_user_name(account, user_name, package_name); - - /* update custom */ - error_code = _account_update_custom(account, account->id); - - return error_code; -} - -ACCOUNT_API int account_insert_to_db(account_h account, int *account_id) -{ - int error_code = ACCOUNT_ERROR_NONE; - int ret_transaction = 0; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); - ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL")); - - account_s *data = (account_s*)account; - ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__); - - pthread_mutex_lock(&account_mutex); - - /* transaction control required*/ - ret_transaction = _account_begin_transaction(); - - if(_account_db_err_code() == SQLITE_PERM){ - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) { - ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DATABASE_BUSY; - }else if (ret_transaction != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction); - pthread_mutex_unlock(&account_mutex); - return ret_transaction; - } - - if (_account_check_duplicated(data)) { - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_DEBUG("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction); - *account_id = -1; - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DUPLICATED; - } else { - *account_id = _account_get_next_sequence(ACCOUNT_TABLE); - - char* appid = NULL; - appid = _account_get_current_appid(); - - if(!appid){ - // API caller cannot be recognized - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; - } - - char* verified_appid = NULL; - error_code = _account_check_account_type_with_appid_group(appid, &verified_appid); - if(error_code != ACCOUNT_ERROR_NONE){ - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction); - _ACCOUNT_FREE(verified_appid); - _ACCOUNT_FREE(appid); - pthread_mutex_unlock(&account_mutex); - return error_code; - } - - _ACCOUNT_FREE(appid); - - if(verified_appid){ - if(!_account_check_add_more_account(verified_appid)) { - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction); - pthread_mutex_unlock(&account_mutex); - _ACCOUNT_FREE(verified_appid); - return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE; - } - - _ACCOUNT_FREE(data->package_name); - data->package_name = _account_get_text(verified_appid); - _ACCOUNT_FREE(verified_appid); - if(!data->package_name) { - ACCOUNT_ERROR("Memory Allocation Failed"); - return ACCOUNT_ERROR_OUT_OF_MEMORY; - } - } - -#ifdef SAMSUNG_ACCOUNT_MANAGER - if(!strcmp(data->package_name,SAMSUNGACCOUNT_PKG_NAME)){ - //temporary feature for samsung account domain - int ret = -1; - ret = account_set_domain_name(account, "Samsung"); - if(ret != ACCOUNT_ERROR_NONE) { - ACCOUNT_DEBUG("set_domain error(%x)!!!!\n", ret); - } - } -#endif - - if(!_account_check_add_more_account(data->package_name)) { - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE; - } - - error_code = _account_execute_insert_query(data); - - if (error_code != ACCOUNT_ERROR_NONE) { - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction); - *account_id = -1; - pthread_mutex_unlock(&account_mutex); - return error_code; - } - } - - error_code = _account_insert_capability(data, *account_id); - if (error_code != ACCOUNT_ERROR_NONE) { - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction); - *account_id = -1; - pthread_mutex_unlock(&account_mutex); - return error_code; - } - - error_code = _account_insert_custom(data, *account_id); - if (error_code != ACCOUNT_ERROR_NONE) { - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction); - *account_id = -1; - pthread_mutex_unlock(&account_mutex); - return error_code; - } - - pthread_mutex_unlock(&account_mutex); - _account_end_transaction(TRUE); - ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__); - - char buf[64]={0,}; - ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id); - _account_insert_delete_update_notification_send(buf); - ACCOUNT_SLOGD("(%s)-(%d) account _notification_send.\n", __FUNCTION__, __LINE__); - - return ACCOUNT_ERROR_NONE; - -} - -ACCOUNT_API int account_create(account_h *account) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)malloc(sizeof(account_s)); - - if (data == NULL) { - ACCOUNT_FATAL("Memory Allocation Failed"); - return ACCOUNT_ERROR_OUT_OF_MEMORY; - } - ACCOUNT_MEMSET(data, 0, sizeof(account_s)); - - /*Setting account as visible by default*/ - data->secret = ACCOUNT_SECRECY_VISIBLE; - - /*Setting account as not supporting sync by default*/ - data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT; - - *account = (account_h)data; - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_user_name(account_h account, const char *user_name) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!user_name) { - ACCOUNT_SLOGE("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - _ACCOUNT_FREE(data->user_name); - data->user_name = _account_get_text(user_name); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_display_name(account_h account, const char *display_name) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!display_name) { - ACCOUNT_SLOGE("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - _ACCOUNT_FREE(data->display_name); - data->display_name = _account_get_text(display_name); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_email_address(account_h account, const char *email_address) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!email_address) { - ACCOUNT_SLOGE("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - _ACCOUNT_FREE(data->email_address); - data->email_address = _account_get_text(email_address); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_icon_path(account_h account, const char *icon_path) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!icon_path) { - ACCOUNT_SLOGE("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - _ACCOUNT_FREE(data->icon_path); - data->icon_path = _account_get_text(icon_path); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_source(account_h account, const char *source) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!source) { - ACCOUNT_SLOGE("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - account_s *data = (account_s*)account; - - _ACCOUNT_FREE(data->source); - data->source = _account_get_text(source); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_package_name(account_h account, const char *package_name) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!package_name) { - ACCOUNT_SLOGE("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - _ACCOUNT_FREE(data->package_name); - data->package_name = _account_get_text(package_name); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_domain_name(account_h account, const char *domain_name) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!domain_name) { - ACCOUNT_SLOGE("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - account_s *data = (account_s*)account; - - _ACCOUNT_FREE(data->domain_name); - data->domain_name = _account_get_text(domain_name); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_access_token(account_h account, const char *access_token) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!access_token) { - ACCOUNT_SLOGE("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - _ACCOUNT_FREE(data->access_token); - data->access_token = _account_get_text(access_token); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_user_text(account_h account, int idx, const char *user_txt) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!user_txt) { - ACCOUNT_SLOGE("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - if (idx >= USER_TXT_CNT || idx < 0) { - ACCOUNT_SLOGE("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - _ACCOUNT_FREE(data->user_data_txt[idx]); - data->user_data_txt[idx] = _account_get_text(user_txt); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_custom(account_h account, const char* key, const char* value) -{ - if (!account) { - ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!key) { - ACCOUNT_SLOGE("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!value) { - ACCOUNT_SLOGE("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - GSList *iter; - bool b_is_new = TRUE; - - for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) { - - account_custom_s* custom_data = NULL; - custom_data = (account_custom_s*)iter->data; - ACCOUNT_SLOGD( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value); - - if(!strcmp(custom_data->key, key)) { - _ACCOUNT_FREE(custom_data->value); - custom_data->value = _account_get_text(value); - b_is_new = FALSE; - } - } - - if(b_is_new) { - account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s)); - - if (custom_data == NULL) { - return ACCOUNT_ERROR_OUT_OF_MEMORY; - } - ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s)); - custom_data->account_id = data->id; - custom_data->app_id = _account_get_text(data->package_name); - custom_data->key = _account_get_text(key); - custom_data->value = _account_get_text(value); - data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data); - } - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_auth_type(account_h account, const account_auth_type_e auth_type) -{ - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__)); - - if ( ((int)auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_MAX) ) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - data->auth_type = (int)auth_type; - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_secret(account_h account, const account_secrecy_state_e secret) -{ - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__)); - - if ( ((int)secret < 0) || (secret > ACCOUNT_SECRECY_MAX)) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - data->secret = (int)secret; - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_sync_support(account_h account, const account_sync_state_e sync_support) -{ - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__)); - - if ( ((int)sync_support < 0) || (sync_support > ACCOUNT_SYNC_MAX)) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - data->sync_support= (int)sync_support; - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_user_int(account_h account, int idx, const int user_int) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (idx >= USER_INT_CNT ||idx < 0) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - data->user_data_int[idx] = user_int; - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value) -{ - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null")); - ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null")); - - if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - GSList *iter = NULL; - bool b_is_new = TRUE; - - for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) { - account_capability_s *cap_data = NULL; - cap_data = (account_capability_s*)iter->data; - - if(!strcmp(cap_data->type, capability_type)) { - cap_data->value = capability_value; - b_is_new = FALSE; - break; - } - } - - if(b_is_new) { - account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s)); - - if (cap_data == NULL) - return ACCOUNT_ERROR_OUT_OF_MEMORY; - ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s)); - - cap_data->type = _account_get_text(capability_type); - cap_data->value = capability_value; - data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data); - } - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_user_name(account_h account, char **user_name) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!user_name) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - (*user_name) = NULL; - *user_name = _account_get_text(data->user_name); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_display_name(account_h account, char **display_name) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!display_name) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - (*display_name) = NULL; - - *display_name = _account_get_text(data->display_name); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_email_address(account_h account,char **email_address) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!email_address) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - (*email_address) = NULL; - - *email_address = _account_get_text(data->email_address); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_icon_path(account_h account, char **icon_path) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!icon_path) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - (*icon_path) = NULL; - - *icon_path = _account_get_text(data->icon_path); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_source(account_h account, char **source) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!source) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - (*source) = NULL; - - *source = _account_get_text(data->source); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_package_name(account_h account, char **package_name) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!package_name) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - (*package_name) = NULL; - - *package_name = _account_get_text(data->package_name); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_domain_name(account_h account, char **domain_name) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!domain_name) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - (*domain_name) = NULL; - - *domain_name = _account_get_text(data->domain_name); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_access_token(account_h account, char **access_token) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!access_token) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - (*access_token) = NULL; - - *access_token = _account_get_text(data->access_token); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_user_text(account_h account, int user_text_index, char **text) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if (!text) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX")); - - account_s *data = (account_s*)account; - - (*text) = NULL; - - *text = _account_get_text(data->user_data_txt[user_text_index]); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_auth_type(account_h account, account_auth_type_e *auth_type) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - if (!auth_type) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s* data = (account_s*)account; - - *auth_type = data->auth_type; - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_secret(account_h account, account_secrecy_state_e *secret) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - if (!secret) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s* data = (account_s*)account; - - *secret = data->secret; - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_sync_support(account_h account, account_sync_state_e *sync_support) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - if (!sync_support) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s* data = (account_s*)account; - - *sync_support = data->sync_support; - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_account_id(account_h account, int *account_id) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - if (!account_id) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - *account_id = data->id; - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_user_int(account_h account, int user_int_index, int *integer) -{ - if (!account) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX")); - - if (!integer) { - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - account_s *data = (account_s*)account; - - *integer = data->user_data_int[user_int_index]; - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value) -{ - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); - ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL")); - ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL")); - - GSList *iter; - account_s *data = (account_s*)account; - - for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) { - account_capability_s *cap_data = NULL; - - cap_data = (account_capability_s*)iter->data; - - if(!strcmp(capability_type, cap_data->type)) { - *capability_value = cap_data->value; - return ACCOUNT_ERROR_NONE; - } - } - - return ACCOUNT_ERROR_RECORD_NOT_FOUND; -} - -ACCOUNT_API int account_get_capability_all(account_h account, capability_cb callback, void *user_data) -{ - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); - - GSList *iter; - account_s *data = (account_s*)account; - - for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) { - account_capability_s *cap_data = NULL; - - cap_data = (account_capability_s*)iter->data; - - if(callback(cap_data->type, cap_data->value, user_data)!=TRUE){ - return ACCOUNT_ERROR_NONE; - } - } - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_get_custom(account_h account, const char* key, char** value) -{ - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); - ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST")); - ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL")); - - GSList *iter; - account_s *data = (account_s*)account; - - for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) { - account_custom_s *custom_data = NULL; - - custom_data = (account_custom_s*)iter->data; - - if(!strcmp(key, custom_data->key)) { - (*value) = NULL; - *value = _account_get_text(custom_data->value); - return ACCOUNT_ERROR_NONE; - } - } - - return ACCOUNT_ERROR_RECORD_NOT_FOUND; -} - -ACCOUNT_API int account_get_custom_all(account_h account, account_custom_cb callback, void* user_data) -{ - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); - - GSList *iter; - account_s *data = (account_s*)account; - - for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) { - bool cb_ret = FALSE; - account_custom_s *custom_data = NULL; - - custom_data = (account_custom_s*)iter->data; - - cb_ret = callback(custom_data->key, custom_data->value, user_data); - if(!cb_ret) { - break; - } - } - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data ) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id); - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - account_capability_s* capability_record = NULL; - - while (rc == SQLITE_ROW) { - bool cb_ret = FALSE; - capability_record = (account_capability_s*) malloc(sizeof(account_capability_s)); - - if (capability_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; - } - - ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s)); - - _account_convert_column_to_capability(hstmt, capability_record); - - cb_ret = callback(capability_record->type, capability_record->value, user_data); - - _account_free_capability_items(capability_record); - _ACCOUNT_FREE(capability_record); - - ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); - - rc = _account_query_step(hstmt); - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - error_code = ACCOUNT_ERROR_NONE; - -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - pthread_mutex_unlock(&account_mutex); - return error_code; -} - -static int _account_compare_old_record(account_s *new_account, int account_id) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - account_s *old_account = NULL; - - ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); - ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - old_account = (account_s*)calloc(1, sizeof(account_s)); - ACCOUNT_RETURN_VAL((old_account != NULL), {}, ACCOUNT_ERROR_OUT_OF_MEMORY,("Memory Allocation Failed")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id); - hstmt = _account_prepare_query(query); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - while (rc == SQLITE_ROW) { - _account_convert_column_to_account(hstmt, old_account); - rc = _account_query_step(hstmt); - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - // get capability - error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account); - ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error")); - - // get custom text - error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account); - ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error")); - - // compare - - new_account->id = old_account->id; - - //user name - if(!new_account->user_name) { - if(old_account->user_name) - new_account->user_name = _account_get_text(old_account->user_name); - } - - // display name - if(!new_account->display_name) { - if(old_account->display_name) - new_account->display_name = _account_get_text(old_account->display_name); - } - - // email address - if(!new_account->email_address) { - if(old_account->email_address) - new_account->email_address = _account_get_text(old_account->email_address); - } - - // domain name - if(!new_account->domain_name) { - if(old_account->domain_name) - new_account->domain_name = _account_get_text(old_account->domain_name); - } - - // icon path - if(!new_account->icon_path) { - if(old_account->icon_path) - new_account->icon_path = _account_get_text(old_account->icon_path); - } - - // source - if(!new_account->source) { - if(old_account->source) - new_account->source = _account_get_text(old_account->source); - } - - _ACCOUNT_FREE(new_account->package_name); - new_account->package_name = _account_get_text(old_account->package_name); - - // access token - if(!new_account->access_token) { - if(old_account->access_token) - new_account->access_token = _account_get_text(old_account->access_token); - } - - // user text - int i; - for(i=0;iuser_data_txt[i]) { - if(old_account->user_data_txt[i]) - new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]); - } - } - - // auth type - if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) { - new_account->auth_type = old_account->auth_type; - } - - //secret - if(new_account->secret== ACCOUNT_SECRECY_INVALID) { - new_account->secret = old_account->secret; - } - - // sync support - if(new_account->sync_support == ACCOUNT_SYNC_INVALID) { - new_account->sync_support = old_account->sync_support; - } - - // user int - for(i=0;iuser_data_int[i] == 0) { - new_account->user_data_int[i] = old_account->user_data_int[i]; - } - } - - // capability - - // user custom table - - - - CATCH: - if (old_account) { - _account_free_account_items(old_account); - _ACCOUNT_FREE(old_account); - } - - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - return ACCOUNT_ERROR_NONE; -} - -static int _account_get_package_name_from_account_id(int account_id, char **package_name) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - account_s *old_account = NULL; - - ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - old_account = (account_s*)calloc(1, sizeof(account_s)); - ACCOUNT_RETURN_VAL((old_account != NULL), {}, ACCOUNT_ERROR_OUT_OF_MEMORY,("Memory Allocation Failed")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id); - hstmt = _account_prepare_query(query); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - while (rc == SQLITE_ROW) { - _account_convert_column_to_account(hstmt, old_account); - rc = _account_query_step(hstmt); - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - // get package name. - *package_name = _account_get_text(old_account->package_name); - - - CATCH: - if (old_account) { - _account_free_account_items(old_account); - _ACCOUNT_FREE(old_account); - } - - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - return error_code; - -} - -static int _account_update_account(account_s *account, int account_id) -{ - int rc = 0, binding_count =0; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0; - account_stmt hstmt = NULL; - - if (!account->package_name) { - ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n"); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - /* Check permission of requested appid */ - char* current_appid = NULL; - char *package_name = NULL; - - current_appid = _account_get_current_appid(); - error_code = _account_get_package_name_from_account_id(account_id, &package_name); - - if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){ - ACCOUNT_ERROR("No package name with account_id\n"); - _ACCOUNT_FREE(current_appid); - _ACCOUNT_FREE(package_name); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } - - error_code = _account_check_appid_group_with_package_name(current_appid, package_name); - ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later. - - _ACCOUNT_FREE(current_appid); - _ACCOUNT_FREE(package_name); - - if(error_code != ACCOUNT_ERROR_NONE){ - ACCOUNT_ERROR("No permission to update\n"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - _account_compare_old_record(account, account_id); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } else if( _account_db_err_code() == SQLITE_BUSY ){ - ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_DATABASE_BUSY; - } - - if (!account->user_name && !account->display_name && !account->email_address) { - ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id); - - count = _account_get_record_count(query); - if (count <= 0) { - ACCOUNT_DEBUG(" Account record not found, count = %d\n", count); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } - - /* transaction control required*/ - ret_transaction = _account_begin_transaction(); - if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ - ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DATABASE_BUSY; - } - - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " - "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," - "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " - "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - 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))); - - binding_count = _account_convert_account_to_sql(account, hstmt, query); - _account_query_bind_int(hstmt, binding_count++, account_id); - - rc = _account_query_step(hstmt); - if (rc != SQLITE_DONE) { - ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - /*update capability*/ - error_code = _account_update_capability(account, account_id); - if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); - return error_code; - } - - /* update custom */ - error_code = _account_update_custom(account, account_id); - if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); - return error_code; - } - - ret_transaction = _account_end_transaction(TRUE); - - return error_code; -} - - -static int _account_update_account_ex(account_s *account, int account_id) -{ - int rc = 0, binding_count =0; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0; - account_stmt hstmt = NULL; - - if (!account->package_name) { - ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n"); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - _account_compare_old_record(account, account_id); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if (!account->user_name && !account->display_name && !account->email_address) { - ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id); - - count = _account_get_record_count(query); - if (count <= 0) { - ACCOUNT_DEBUG(" Account record not found, count = %d\n", count); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } - - /* transaction control required*/ - ret_transaction = _account_begin_transaction(); - if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ - ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DATABASE_BUSY; - } - - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " - "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," - "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " - "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - 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))); - - binding_count = _account_convert_account_to_sql(account, hstmt, query); - _account_query_bind_int(hstmt, binding_count++, account_id); - - rc = _account_query_step(hstmt); - if (rc != SQLITE_DONE) { - ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - /*update capability*/ - error_code = _account_update_capability(account, account_id); - if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); - return error_code; - } - - /* update custom */ - error_code = _account_update_custom(account, account_id); - if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); - return error_code; - } - - ret_transaction = _account_end_transaction(TRUE); - - return error_code; -} - - -ACCOUNT_API int account_update_to_db_by_id(const account_h account, int account_id) -{ - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); - ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - int error_code = ACCOUNT_ERROR_NONE; - account_s* data = (account_s*)account; - - pthread_mutex_lock(&account_mutex); - - error_code = _account_update_account(data, account_id); - - if(error_code != ACCOUNT_ERROR_NONE) { - pthread_mutex_unlock(&account_mutex); - return error_code; - } - - pthread_mutex_unlock(&account_mutex); - - char buf[64]={0,}; - ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id); - _account_insert_delete_update_notification_send(buf); - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_update_to_db_by_id_ex(const account_h account, int account_id) -{ - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); - ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - int error_code = ACCOUNT_ERROR_NONE; - account_s* data = (account_s*)account; - - pthread_mutex_lock(&account_mutex); - - error_code = _account_update_account_ex(data, account_id); - - if(error_code != ACCOUNT_ERROR_NONE) { - pthread_mutex_unlock(&account_mutex); - return error_code; - } - - pthread_mutex_unlock(&account_mutex); - - char buf[64]={0,}; - ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id); - _account_insert_delete_update_notification_send(buf); - - return ACCOUNT_ERROR_NONE; -} - - -ACCOUNT_API int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name) -{ - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); - ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL")); - ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - int error_code = ACCOUNT_ERROR_NONE; - account_s *data = (account_s*)account; - - pthread_mutex_lock(&account_mutex); - - error_code = _account_update_account_by_user_name(data, user_name, package_name); - - pthread_mutex_unlock(&account_mutex); - - char buf[64]={0,}; - ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id); - _account_insert_delete_update_notification_send(buf); - - return error_code; -} - -ACCOUNT_API int account_foreach_account_from_db(account_cb callback, void *user_data) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - GList *account_list = NULL; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE); - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - rc = _account_query_step(hstmt); - - account_s *account_record = NULL; - - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - int k=0; - while(rc == SQLITE_ROW) { - account_record = (account_s*) malloc(sizeof(account_s)); - - if (account_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; - } - - ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); - _account_convert_column_to_account(hstmt, account_record); - account_list = g_list_append(account_list, account_record); - rc = _account_query_step(hstmt); - k++; - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_glist_free(account_list);}, rc, ("finalize error")); - hstmt = NULL; - - GList* iter; - k = 0; - for (iter = account_list; iter != NULL; iter = g_list_next(iter)) { - account_s *account = NULL; - account = (account_s*)iter->data; - account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account); - _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account); - callback((account_h)account, user_data); - k++; - } - - error_code = ACCOUNT_ERROR_NONE; - -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - if (account_list) { - _account_glist_free(account_list); - account_list = NULL; - } - - return error_code; -} - -ACCOUNT_API int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - int count =1; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - if ( ((int)sync_status < 0) || (sync_status > ACCOUNT_SYNC_MAX) ) { - ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - pthread_mutex_lock(&account_mutex); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id); - - rc = _account_get_record_count(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if (rc <= 0) { - ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id); - hstmt = _account_prepare_query(query); - - _account_query_bind_int(hstmt, count, (int)sync_status); - - rc = _account_query_step(hstmt); - - if( _account_db_err_code() == SQLITE_PERM ){ - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED, - ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg())); - - rc = _account_query_finalize(hstmt); - if (rc != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("_account_query_finalize error"); - pthread_mutex_unlock(&account_mutex); - return rc; - } - char buf[64]={0,}; - ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id); - _account_insert_delete_update_notification_send(buf); - - hstmt = NULL; - error_code = ACCOUNT_ERROR_NONE; - -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - pthread_mutex_unlock(&account_mutex); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - pthread_mutex_unlock(&account_mutex); - return error_code; -} - -ACCOUNT_API int account_query_account_by_account_id(int account_db_id, account_h *account) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); - ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id); - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - account_s *account_record = (account_s *)(*account); - - while (rc == SQLITE_ROW) { - _account_convert_column_to_account(hstmt, account_record); - rc = _account_query_step(hstmt); - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - - account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record); - _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record); - - hstmt = NULL; - error_code = ACCOUNT_ERROR_NONE; - -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - pthread_mutex_unlock(&account_mutex); - return error_code; -} - -ACCOUNT_API int account_query_account_by_user_name(account_cb callback, const char *user_name, void *user_data) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL")); - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - int binding_count = 1; - _account_query_bind_text(hstmt, binding_count++, user_name); - - rc = _account_query_step(hstmt); - - account_s *account_head = NULL; - - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - int tmp = 0; - - account_head = (account_s*) malloc(sizeof(account_s)); - if (account_head == NULL) { - ACCOUNT_FATAL("malloc Failed"); - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - return ACCOUNT_ERROR_OUT_OF_MEMORY; - } - ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); - - while (rc == SQLITE_ROW) { - account_s* account_record = NULL; - - account_record = (account_s*) malloc(sizeof(account_s)); - - if (account_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; - } - ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); - - _account_convert_column_to_account(hstmt, account_record); - - account_head->account_list = g_list_append(account_head->account_list, account_record); - - rc = _account_query_step(hstmt); - tmp++; - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - GList *iter; - - - tmp = g_list_length(account_head->account_list); - - for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { - account_h account; - account = (account_h)iter->data; - - account_s *testaccount = (account_s*)account; - - account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); - _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); - - ACCOUNT_CATCH_ERROR(callback(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("callback returns false, it is stopped.\n")); - } - - error_code = ACCOUNT_ERROR_NONE; - -CATCH: - if (account_head) { - if (account_head->account_list) { - _account_glist_free(account_head->account_list); - account_head->account_list = NULL; - _account_free_account_items(account_head); - } - _ACCOUNT_FREE(account_head); - } - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - pthread_mutex_unlock(&account_mutex); - return error_code; -} - -ACCOUNT_API int account_query_account_by_capability(account_cb callback, const char* capability_type, account_capability_state_e capability_value, void* user_data) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL")); - - if (((int)capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_STATE_MAX)) { - ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - int binding_count = 1; - _account_query_bind_text(hstmt, binding_count++, capability_type); - _account_query_bind_int(hstmt, binding_count++, (int)capability_value); - - rc = _account_query_step(hstmt); - - account_s* account_head = NULL; - - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - int tmp = 0; - - account_head = (account_s*) malloc(sizeof(account_s)); - if (account_head == NULL) { - ACCOUNT_FATAL("malloc Failed"); - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - return ACCOUNT_ERROR_OUT_OF_MEMORY; - } - ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); - - while (rc == SQLITE_ROW) { - account_s* account_record = NULL; - - account_record = (account_s*) malloc(sizeof(account_s)); - - if (account_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; - } - ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); - - _account_convert_column_to_account(hstmt, account_record); - - account_head->account_list = g_list_append(account_head->account_list, account_record); - - rc = _account_query_step(hstmt); - tmp++; - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - GList *iter; - - - tmp = g_list_length(account_head->account_list); - - for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { - account_h account = NULL; - account = (account_h)iter->data; - account_s* testaccount = (account_s*)account; - - account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); - _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); - - ACCOUNT_CATCH_ERROR(callback(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("callback returns false, it is stopped.\n")); - } - - - error_code = ACCOUNT_ERROR_NONE; - -CATCH: - if (account_head) { - if (account_head->account_list) { - _account_glist_free(account_head->account_list); - account_head->account_list = NULL; - _account_free_account_items(account_head); - } - _ACCOUNT_FREE(account_head); - } - - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - pthread_mutex_unlock(&account_mutex); - return error_code; -} - -ACCOUNT_API int account_query_account_by_capability_type(account_cb callback, const char* capability_type, void* user_data) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL")); - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - int binding_count = 1; - _account_query_bind_text(hstmt, binding_count++, capability_type); - - rc = _account_query_step(hstmt); - - account_s* account_head = NULL; - - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - int tmp = 0; - - account_head = (account_s*) malloc(sizeof(account_s)); - if (account_head == NULL) { - ACCOUNT_FATAL("malloc Failed"); - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - return ACCOUNT_ERROR_OUT_OF_MEMORY; - } - ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); - - while (rc == SQLITE_ROW) { - account_s* account_record = NULL; - - account_record = (account_s*) malloc(sizeof(account_s)); - - if (account_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; - } - ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); - - _account_convert_column_to_account(hstmt, account_record); - - account_head->account_list = g_list_append(account_head->account_list, account_record); - - rc = _account_query_step(hstmt); - tmp++; - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - GList *iter; - - - tmp = g_list_length(account_head->account_list); - - for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { - account_h account = NULL; - account = (account_h)iter->data; - account_s* testaccount = (account_s*)account; - - account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); - _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); - - ACCOUNT_CATCH_ERROR(callback(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n")); - - } - - error_code = ACCOUNT_ERROR_NONE; - -CATCH: - if (account_head) { - if (account_head->account_list) { - _account_glist_free(account_head->account_list); - account_head->account_list = NULL; - _account_free_account_items(account_head); - } - _ACCOUNT_FREE(account_head); - } - - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - pthread_mutex_unlock(&account_mutex); - return error_code; -} - -ACCOUNT_API int account_query_account_by_package_name(account_cb callback, const char* package_name, void* user_data) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL")); - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - int binding_count = 1; - _account_query_bind_text(hstmt, binding_count++, package_name); - - rc = _account_query_step(hstmt); - - account_s* account_head = NULL; - - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name)); - - int tmp = 0; - - account_head = (account_s*) malloc(sizeof(account_s)); - if (account_head == NULL) { - ACCOUNT_FATAL("malloc Failed"); - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - return ACCOUNT_ERROR_OUT_OF_MEMORY; - } - ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); - - while (rc == SQLITE_ROW) { - account_s* account_record = NULL; - - account_record = (account_s*) malloc(sizeof(account_s)); - - if (account_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; - } - ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); - - _account_convert_column_to_account(hstmt, account_record); - - account_head->account_list = g_list_append(account_head->account_list, account_record); - - rc = _account_query_step(hstmt); - tmp++; - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - GList *iter; - - tmp = g_list_length(account_head->account_list); - - for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { - account_h account = NULL; - account = (account_h)iter->data; - - account_s* testaccount = (account_s*)account; - - account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); - _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); - - ACCOUNT_CATCH_ERROR(callback(account, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("The record isn't found.\n")); - - } - - error_code = ACCOUNT_ERROR_NONE; - -CATCH: - if (account_head) { - if (account_head->account_list) { - _account_glist_free(account_head->account_list); - account_head->account_list = NULL; - _account_free_account_items(account_head); - } - _ACCOUNT_FREE(account_head); - } - - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - pthread_mutex_unlock(&account_mutex); - return error_code; -} - -ACCOUNT_API int account_delete(int account_id) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - int ret_transaction = 0; - bool is_success = FALSE; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - int count = -1; - /* Check requested ID to delete */ - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id); - - count = _account_get_record_count(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if (count <= 0) { - ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } - - /* Check permission of requested appid */ - char* current_appid = NULL; - char *package_name = NULL; - - current_appid = _account_get_current_appid(); - - error_code = _account_get_package_name_from_account_id(account_id, &package_name); - - if(error_code != ACCOUNT_ERROR_NONE){ - ACCOUNT_ERROR("No package name with account_id\n"); - _ACCOUNT_FREE(current_appid); - _ACCOUNT_FREE(package_name); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } - ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); - - error_code = _account_check_appid_group_with_package_name(current_appid, package_name); - - _ACCOUNT_FREE(current_appid); - _ACCOUNT_FREE(package_name); - - if(error_code != ACCOUNT_ERROR_NONE){ - ACCOUNT_ERROR("No permission to delete\n"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - /* transaction control required*/ - ret_transaction = _account_begin_transaction(); - - if( _account_db_err_code() == SQLITE_PERM ){ - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ - ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DATABASE_BUSY; - } - - if (ret_transaction != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); - pthread_mutex_unlock(&account_mutex); - return ret_transaction; - } - - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - rc = _account_query_finalize(hstmt); - - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - ACCOUNT_MEMSET(query, 0, sizeof(query)); - - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id); - - hstmt = _account_prepare_query(query); - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc)); - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - /* delete custom data */ - ACCOUNT_MEMSET(query, 0, sizeof(query)); - - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id); - - hstmt = _account_prepare_query(query); - - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc)); - - rc = _account_query_finalize(hstmt); - ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc)); - hstmt = NULL; - - is_success = TRUE; - -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - if(rc != ACCOUNT_ERROR_NONE ){ - ACCOUNT_ERROR("rc (%d)", rc); - is_success = FALSE; - } - - hstmt = NULL; - } - - ret_transaction = _account_end_transaction(is_success); - - if (ret_transaction != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); - } else { - if (is_success == true) { - char buf[64]={0,}; - ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id); - _account_insert_delete_update_notification_send(buf); - } - } - - pthread_mutex_unlock(&account_mutex); - - return error_code; - -} - -ACCOUNT_API int account_delete_from_db_by_id(int account_db_id) -{ - ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO.")); - - return account_delete(account_db_id); -} - -static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - int binding_count = 1; - - ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL")); - ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL")); - ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE); - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - _account_query_bind_text(hstmt, binding_count++, username); - _account_query_bind_text(hstmt, binding_count++, package_name); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - account_s *account_record = (account_s *)(*account); - - while (rc == SQLITE_ROW) { - _account_convert_column_to_account(hstmt, account_record); - rc = _account_query_step(hstmt); - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record); - _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record); - - hstmt = NULL; - error_code = ACCOUNT_ERROR_NONE; - -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - pthread_mutex_unlock(&account_mutex); - return error_code; -} - -ACCOUNT_API int account_delete_from_db_by_user_name(char *user_name, char *package_name) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - int ret_transaction = 0; - bool is_success = FALSE; - account_h account = NULL; - int binding_count = 1; - int account_id = -1; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!")); - ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - rc = account_create(&account); - rc = _account_query_account_by_username_and_package(user_name, package_name, &account); - - - if( _account_db_err_code() == SQLITE_PERM ){ - account_destroy(account); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - rc = account_get_account_id(account, &account_id); - - rc = account_destroy(account); - - /* transaction control required*/ - ret_transaction = _account_begin_transaction(); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ - ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DATABASE_BUSY; - }else if (ret_transaction != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); - pthread_mutex_unlock(&account_mutex); - return ret_transaction; - } - - /* delete custom data */ - ACCOUNT_MEMSET(query, 0, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - _account_end_transaction(FALSE); - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); - - _account_query_bind_int(hstmt, binding_count++, account_id); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - /* delete capability */ - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE); - - hstmt = _account_prepare_query(query); - - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); - - binding_count = 1; - _account_query_bind_text(hstmt, binding_count++, user_name); - _account_query_bind_text(hstmt, binding_count++, package_name); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - ACCOUNT_MEMSET(query, 0, sizeof(query)); - - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE); - - hstmt = _account_prepare_query(query); - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); - - binding_count = 1; - _account_query_bind_text(hstmt, binding_count++, user_name); - _account_query_bind_text(hstmt, binding_count++, package_name); - - rc = _account_query_step(hstmt); - 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)); - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - is_success = TRUE; - - hstmt = NULL; - -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - ret_transaction = _account_end_transaction(is_success); - - if (ret_transaction != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); - } else { - if (is_success == true) { - char buf[64]={0,}; - ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id); - _account_insert_delete_update_notification_send(buf); - } - } - - pthread_mutex_unlock(&account_mutex); - - return error_code; -} - -static bool _account_delete_from_db_by_package_name_cb(account_h account, void *user_data) -{ - GSList **account_id_list = (GSList**)user_data; - int account_id = 0; - char id_buf[10] ={0,}; - char* tmp_ptr = NULL; - - account_get_account_id(account, &account_id); - ACCOUNT_SNPRINTF(id_buf, sizeof(id_buf), "%d", account_id); - - tmp_ptr = strdup(id_buf); - - ACCOUNT_SLOGD("id = %s", id_buf); - - *account_id_list = g_slist_append(*account_id_list, tmp_ptr); - - return TRUE; -} - -ACCOUNT_API int account_delete_from_db_by_package_name(const char *package_name) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - int ret_transaction = 0; - bool is_success = FALSE; - int binding_count = 1; - GSList *account_id_list = NULL; - int ret = -1; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ret = account_query_account_by_package_name(_account_delete_from_db_by_package_name_cb, package_name, (void*)&account_id_list); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if(ret != ACCOUNT_ERROR_NONE){ - return ret; - } - - /* transaction control required*/ - ret_transaction = _account_begin_transaction(); - - if( _account_db_err_code() == SQLITE_PERM ){ - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ - ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DATABASE_BUSY; - }else if (ret_transaction != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); - pthread_mutex_unlock(&account_mutex); - return ret_transaction; - } - - /* delete custom table */ - ACCOUNT_MEMSET(query, 0, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - _account_end_transaction(FALSE); - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); - - binding_count = 1; - _account_query_bind_text(hstmt, binding_count++, package_name); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - /* delete capability table */ - ACCOUNT_MEMSET(query, 0, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE); - - hstmt = _account_prepare_query(query); - - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); - - binding_count = 1; - _account_query_bind_text(hstmt, binding_count++, package_name); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - /* delete account table */ - ACCOUNT_MEMSET(query, 0, sizeof(query)); - - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE); - - hstmt = _account_prepare_query(query); - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); - - binding_count = 1; - _account_query_bind_text(hstmt, binding_count++, package_name); - - rc = _account_query_step(hstmt); - 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)); - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - is_success = TRUE; - - hstmt = NULL; - -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - - ret_transaction = _account_end_transaction(is_success); - - if (ret_transaction != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); - } else { - if (is_success == true) { - GSList* iter; - for (iter = account_id_list; iter != NULL; iter = g_slist_next(iter)) { - char* p_tmpid = NULL; - p_tmpid = (char*)iter->data; - char buf[64]={0,}; - ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid); - ACCOUNT_SLOGD("%s", buf); - _account_insert_delete_update_notification_send(buf); - _ACCOUNT_FREE(p_tmpid); - } - g_slist_free(account_id_list); - } - } - - pthread_mutex_unlock(&account_mutex); - - return error_code; -} - -ACCOUNT_API int account_get_total_count_from_db(int *count) -{ - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - if (!count) { - ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__); - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - if(!g_hAccountDB){ - ACCOUNT_ERROR("DB is not opened\n"); - return ACCOUNT_ERROR_DB_NOT_OPENED; - } - - char query[1024] = {0, }; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE); - - *count = _account_get_record_count(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - int rc = -1; - int ncount = 0; - account_stmt pStmt = NULL; - - rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL); - if (SQLITE_OK != rc) { - ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); - sqlite3_finalize(pStmt); - return ACCOUNT_ERROR_DB_FAILED; - } - - rc = sqlite3_step(pStmt); - if (SQLITE_ROW != rc) { - ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n"); - sqlite3_finalize(pStmt); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } - - ncount = sqlite3_column_int(pStmt, 0); - - *count = ncount; - - sqlite3_finalize(pStmt); - - if (ncount < 0) { - ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount); - return ACCOUNT_ERROR_DB_FAILED; - } - - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_API int account_destroy(account_h account) -{ - account_s *data = (account_s*)account; - - ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!")); - - _account_free_account_items(data); - _ACCOUNT_FREE(data); - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_free_label_items(label_s *data) -{ - _ACCOUNT_FREE(data->app_id); - _ACCOUNT_FREE(data->label); - _ACCOUNT_FREE(data->locale); - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_free_feature_items(provider_feature_s *data) -{ - _ACCOUNT_FREE(data->app_id); - _ACCOUNT_FREE(data->key); - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_gslist_free(GSList* list) -{ - ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL")); - - GSList* iter; - - for (iter = list; iter != NULL; iter = g_slist_next(iter)) { - label_s *label_data = (label_s*)iter->data; - _account_type_free_label_items(label_data); - _ACCOUNT_FREE(label_data); - } - - g_slist_free(list); - list = NULL; - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_item_free(account_type_s *data) -{ - _ACCOUNT_FREE(data->app_id); - _ACCOUNT_FREE(data->service_provider_id); - _ACCOUNT_FREE(data->icon_path); - _ACCOUNT_FREE(data->small_icon_path); - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_glist_free(GList* list) -{ - ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL")); - - GList* iter; - - for (iter = list; iter != NULL; iter = g_list_next(iter)) { - account_type_s *account_type_record = (account_type_s*)iter->data; - _account_type_item_free(account_type_record); - _ACCOUNT_FREE(account_type_record); - } - - g_list_free(list); - list = NULL; - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_free_account_type_items(account_type_s *data) -{ - _account_type_item_free(data); + account_s *data = (account_s*)account; - _account_type_gslist_free(data->label_list); - _account_type_glist_free(data->account_type_list); + (*user_name) = NULL; + *user_name = _account_get_text(data->user_name); return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_create(account_type_h *account_type) +ACCOUNT_API int account_get_display_name(account_h account, char **display_name) { - if (!account_type) { - ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__); + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s)); - - if (data == NULL) { - ACCOUNT_ERROR("Memory Allocation Failed"); - return ACCOUNT_ERROR_OUT_OF_MEMORY; + if (!display_name) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_MEMSET(data, 0, sizeof(account_type_s)); + account_s *data = (account_s*)account; - *account_type = (account_type_h)data; + (*display_name) = NULL; + + *display_name = _account_get_text(data->display_name); return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_destroy(account_type_h account_type) +ACCOUNT_API int account_get_email_address(account_h account,char **email_address) { - account_type_s *data = (account_type_s*)account_type; + if (!account) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!email_address) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_s *data = (account_s*)account; - ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!")); + (*email_address) = NULL; - _account_type_free_account_type_items(data); - _ACCOUNT_FREE(data); + *email_address = _account_get_text(data->email_address); return ACCOUNT_ERROR_NONE; } -//app_id mandatory field -ACCOUNT_API int account_type_set_app_id(account_type_h account_type, const char *app_id) +ACCOUNT_API int account_get_icon_path(account_h account, char **icon_path) { - if (!account_type) { - ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__); + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - if (!app_id) { - ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__); + if (!icon_path) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - account_type_s *data = (account_type_s*)account_type; + account_s *data = (account_s*)account; - _ACCOUNT_FREE(data->app_id); - data->app_id = _account_get_text(app_id); + (*icon_path) = NULL; + + *icon_path = _account_get_text(data->icon_path); return ACCOUNT_ERROR_NONE; } -//service_provider_id mandatory field -ACCOUNT_API int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id) +ACCOUNT_API int account_get_source(account_h account, char **source) { - if (!account_type) { + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - if (!service_provider_id) { + if (!source) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - account_type_s *data = (account_type_s*)account_type; + account_s *data = (account_s*)account; - _ACCOUNT_FREE(data->service_provider_id); - data->service_provider_id = _account_get_text(service_provider_id); + (*source) = NULL; + + *source = _account_get_text(data->source); return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_set_icon_path(account_type_h account_type, const char *icon_path) +ACCOUNT_API int account_get_package_name(account_h account, char **package_name) { - if (!account_type) { + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - if (!icon_path) { + if (!package_name) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - account_type_s *data = (account_type_s*)account_type; + account_s *data = (account_s*)account; - _ACCOUNT_FREE(data->icon_path); - data->icon_path = _account_get_text(icon_path); + (*package_name) = NULL; + + *package_name = _account_get_text(data->package_name); return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path) +ACCOUNT_API int account_get_domain_name(account_h account, char **domain_name) { - if (!account_type) { + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - if (!small_icon_path) { + if (!domain_name) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - account_type_s *data = (account_type_s*)account_type; + account_s *data = (account_s*)account; - _ACCOUNT_FREE(data->small_icon_path); - data->small_icon_path = _account_get_text(small_icon_path); + (*domain_name) = NULL; + + *domain_name = _account_get_text(data->domain_name); return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support) +ACCOUNT_API int account_get_access_token(account_h account, char **access_token) { - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__)); + if (!account) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - account_type_s *data = (account_type_s*)account_type; + if (!access_token) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - data->multiple_account_support = multiple_account_support; + account_s *data = (account_s*)account; + + (*access_token) = NULL; + + *access_token = _account_get_text(data->access_token); return ACCOUNT_ERROR_NONE; } -// unset? -ACCOUNT_API int account_type_set_label(account_type_h account_type, const char* label, const char* locale) +ACCOUNT_API int account_get_user_text(account_h account, int user_text_index, char **text) { - if (!account_type) { - ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__); + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - if(!label || !locale) { + if (!text) { return ACCOUNT_ERROR_INVALID_PARAMETER; } + ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX")); - account_type_s *data = (account_type_s*)account_type; - label_s *label_data = (label_s*)malloc(sizeof(label_s)); - - if (label_data == NULL) { - return ACCOUNT_ERROR_OUT_OF_MEMORY; - } - ACCOUNT_MEMSET(label_data, 0, sizeof(label_s)); + account_s *data = (account_s*)account; - label_data->label = _account_get_text(label); - label_data->locale = _account_get_text(locale); + (*text) = NULL; - data->label_list = g_slist_append(data->label_list, (gpointer)label_data); + *text = _account_get_text(data->user_data_txt[user_text_index]); return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_get_app_id(account_type_h account_type, char **app_id) +ACCOUNT_API int account_get_auth_type(account_h account, account_auth_type_e *auth_type) { - if (!account_type) { + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - - if (!app_id) { + if (!auth_type) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - account_type_s *data = (account_type_s*)account_type; + account_s* data = (account_s*)account; - (*app_id) = NULL; - *app_id = _account_get_text(data->app_id); + *auth_type = data->auth_type; return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id) +ACCOUNT_API int account_get_secret(account_h account, account_secrecy_state_e *secret) { - if (!account_type) { + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - - if (!service_provider_id) { + if (!secret) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - account_type_s *data = (account_type_s*)account_type; + account_s* data = (account_s*)account; - (*service_provider_id) = NULL; - *service_provider_id = _account_get_text(data->service_provider_id); + *secret = data->secret; return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_get_icon_path(account_type_h account_type, char **icon_path) +ACCOUNT_API int account_get_sync_support(account_h account, account_sync_state_e *sync_support) { - if (!account_type) { + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - - if (!icon_path) { + if (!sync_support) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - account_type_s *data = (account_type_s*)account_type; + account_s* data = (account_s*)account; - (*icon_path) = NULL; - *icon_path = _account_get_text(data->icon_path); + *sync_support = data->sync_support; return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path) +ACCOUNT_API int account_get_account_id(account_h account, int *account_id) { - if (!account_type) { + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - - if (!small_icon_path) { + if (!account_id) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - account_type_s *data = (account_type_s*)account_type; + account_s *data = (account_s*)account; - (*small_icon_path) = NULL; - *small_icon_path = _account_get_text(data->small_icon_path); + *account_id = data->id; return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support) +ACCOUNT_API int account_get_user_int(account_h account, int user_int_index, int *integer) { - if (!account_type) { + if (!account) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - if (!multiple_account_support) { + + ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX")); + + if (!integer) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - account_type_s *data = (account_type_s*)account_type; + account_s *data = (account_s*)account; - *multiple_account_support = data->multiple_account_support; + *integer = data->user_data_int[user_int_index]; return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label) +ACCOUNT_API int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value) { - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); - ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER")); + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL")); + ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL")); GSList *iter; - account_type_s *data = (account_type_s*)account_type; - - for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) { - label_s *label_data = NULL; + account_s *data = (account_s*)account; - label_data = (label_s*)iter->data; + for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) { + account_capability_s *cap_data = NULL; - *label = NULL; + cap_data = (account_capability_s*)iter->data; - if(!strcmp(locale, label_data->locale)) { - *label = _account_get_text(label_data->label); + if(!strcmp(capability_type, cap_data->type)) { + *capability_value = cap_data->value; return ACCOUNT_ERROR_NONE; } - gchar** tokens = g_strsplit(locale, "-", 2); - if(tokens != NULL) { - if((char*)(tokens[1]) != NULL) { - char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1])); - if(upper_token != NULL) { - char* converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token); - if(!strcmp(converted_locale, label_data->locale)) { - _ACCOUNT_FREE(converted_locale); - _ACCOUNT_FREE(upper_token); - g_strfreev(tokens); - *label = _account_get_text(label_data->label); - return ACCOUNT_ERROR_NONE; - } - _ACCOUNT_FREE(converted_locale); - } - _ACCOUNT_FREE(upper_token); - } - } - g_strfreev(tokens); } return ACCOUNT_ERROR_RECORD_NOT_FOUND; } -ACCOUNT_API int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data) +ACCOUNT_API int account_get_capability_all(account_h account, capability_cb callback, void *user_data) { - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); GSList *iter; - account_type_s *data = (account_type_s*)account_type; + account_s *data = (account_s*)account; - for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) { - label_s *label_data = NULL; + for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) { + account_capability_s *cap_data = NULL; - label_data = (label_s*)iter->data; + cap_data = (account_capability_s*)iter->data; - if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) { - ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n"); + if(callback(cap_data->type, cap_data->value, user_data)!=TRUE){ return ACCOUNT_ERROR_NONE; } } @@ -5241,1087 +1294,978 @@ ACCOUNT_API int account_type_get_label(account_type_h account_type, account_labe return ACCOUNT_ERROR_NONE; } -static gboolean _account_type_check_duplicated(account_type_s *data) +ACCOUNT_API int account_get_custom(account_h account, const char* key, char** value) +{ + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST")); + ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL")); + + GSList *iter; + account_s *data = (account_s*)account; + + for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) { + account_custom_s *custom_data = NULL; + + custom_data = (account_custom_s*)iter->data; + + if(!strcmp(key, custom_data->key)) { + (*value) = NULL; + *value = _account_get_text(custom_data->value); + return ACCOUNT_ERROR_NONE; + } + } + + return ACCOUNT_ERROR_RECORD_NOT_FOUND; +} + +ACCOUNT_API int account_get_custom_all(account_h account, account_custom_cb callback, void* user_data) { - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int count = 0; + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + GSList *iter; + account_s *data = (account_s*)account; + + for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) { + bool cb_ret = FALSE; + account_custom_s *custom_data = NULL; - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'" - , ACCOUNT_TYPE_TABLE, data->app_id); + custom_data = (account_custom_s*)iter->data; - count = _account_get_record_count(query); - if (count > 0) { - return TRUE; + cb_ret = callback(custom_data->key, custom_data->value, user_data); + if(!cb_ret) { + break; + } } - return FALSE; + return ACCOUNT_ERROR_NONE; } -static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value) +ACCOUNT_API int account_foreach_account_from_db(account_cb callback, void *user_data) { - int count = 1; - - /*Caution : Keep insert query orders.*/ + _INFO("account_foreach_account_from_db start"); + char* account_db_path = ACCOUNT_DB_NAME; - /* 1. app id*/ - _account_query_bind_text(hstmt, count++, (char*)account_type->app_id); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT Callback IS NULL")); - /* 2. service provider id*/ - _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id); + _GET_COOKIE - /* 3. icon path*/ - _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path); + GError *error = NULL; - /* 4. small icon path*/ - _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } - /* 5. multiple accont support*/ - _account_query_bind_int(hstmt, count++, account_type->multiple_account_support); + GVariant* account_list_variant = NULL; + bool is_success = account_manager_call_account_query_all_sync(acc_mgr, account_db_path, e_cookie, &account_list_variant, NULL, &error); - return count; -} + _DESTROY_COOKIE + int error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); -static int _account_type_execute_insert_query(account_type_s *account_type) -{ - int rc = 0; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - - /* check mandatory field */ - // app id & service provider id - if (!account_type->app_id) { - return ACCOUNT_ERROR_INVALID_PARAMETER; + if (error_code != ACCOUNT_ERROR_NONE) + { + return error_code; } - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values " - "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } else if( _account_db_err_code() == SQLITE_BUSY ){ - ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_DATABASE_BUSY; - } + GSList* account_list = unmarshal_account_list(account_list_variant); + g_variant_unref(account_list_variant); - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + GSList* iter; - _account_type_convert_account_to_sql(account_type, hstmt, query); + for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) + { + _INFO("iterating received account_list"); + account_s *account = NULL; + account = (account_s*)iter->data; + _INFO("Before _account_query_identity_info_by_id"); - rc = _account_query_step(hstmt); - if (rc == SQLITE_BUSY) { - ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - error_code = ACCOUNT_ERROR_DATABASE_BUSY; - } else if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - error_code = ACCOUNT_ERROR_DB_FAILED; + _INFO("account->id=%d", account->id); + if (callback((account_h)account, user_data) == false) + { + _INFO("application callback requested to discontinue."); + break; + } + _INFO("After one iteration callback"); } + _account_gslist_account_free(account_list); - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - return error_code; + _INFO("account_foreach_account_from_db end"); + return ACCOUNT_ERROR_NONE; } -static int _account_type_insert_label(account_type_s *account_type) +ACCOUNT_API int account_query_account_by_account_id(int account_db_id, account_h *account) { - int rc, count = 1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - account_stmt hstmt = NULL; - - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + _INFO("account_query_account_by_account_id start [%d]", account_db_id); + char* account_db_path = ACCOUNT_DB_NAME; - if (g_slist_length( account_type->label_list)==0) { - ACCOUNT_ERROR( "_account_type_insert_label, no label\n"); - return ACCOUNT_ERROR_NONE; - } + ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT POINTER IS NULL")); + ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id); + _GET_COOKIE - rc = _account_get_record_count(query); + GError *error = NULL; - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); return ACCOUNT_ERROR_PERMISSION_DENIED; } - if (rc <= 0) { - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } - - /* insert query*/ - GSList *iter; - - for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) { - int ret; - count = 1; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES " - "(?, ?, ?) ", LABEL_TABLE); + GVariant* account_variant = NULL; + bool is_success = account_manager_call_account_query_account_by_account_id_sync(acc_mgr, account_db_path, account_db_id, e_cookie, &account_variant, NULL, &error); - hstmt = _account_prepare_query(query); + _DESTROY_COOKIE - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + int error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + if (error_code != ACCOUNT_ERROR_NONE) + { + return error_code; + } - label_s* label_data = NULL; - label_data = (label_s*)iter->data; + _INFO("before unmarshal_account"); + account_s* account_data = umarshal_account(account_variant); + _INFO("after unmarshal_account"); - ret = _account_query_bind_text(hstmt, count++, account_type->app_id); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, label_data->label); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + if (account_data == NULL) + { + _ERR("Failed to unmarshal"); + return ACCOUNT_ERROR_DB_FAILED; + } - rc = _account_query_step(hstmt); + account_s **input = (account_s **)account; - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - break; - } + _account_free_account_with_items(*input); - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + *input = account_data; - } + _INFO("account_query_account_by_account_id end"); return ACCOUNT_ERROR_NONE; } -static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record) +ACCOUNT_API int account_query_account_by_user_name(account_cb callback, const char* user_name, void* user_data) { - const char *textbuf = NULL; - - textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID); - _account_db_data_to_text(textbuf, &(feature_record->app_id)); + _INFO("account_query_account_by_user_name starting"); + char* account_db_path = ACCOUNT_DB_NAME; - textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY); - _account_db_data_to_text(textbuf, &(feature_record->key)); - -} + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL")); -ACCOUNT_API int account_type_query_provider_feature_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data ) -{ - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0, binding_count = 1; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + _GET_COOKIE - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + GError *error = NULL; - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + GVariant* account_list_variant = NULL; + bool is_success = account_manager_call_account_query_account_by_user_name_sync(acc_mgr, account_db_path, user_name, e_cookie, &account_list_variant, NULL, &error); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE); - hstmt = _account_prepare_query(query); + _DESTROY_COOKIE - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; + int error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + if (error_code != ACCOUNT_ERROR_NONE) + { + return error_code; } - _account_query_bind_text(hstmt, binding_count++, app_id); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + GSList* account_list = unmarshal_account_list(account_list_variant); + g_variant_unref(account_list_variant); - provider_feature_s* feature_record = NULL; + if (account_list == NULL) + { + return ACCOUNT_ERROR_NO_DATA; + } - while (rc == SQLITE_ROW) { - bool cb_ret = FALSE; - feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s)); + GSList* iter; - if (feature_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); + for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) + { + _INFO("iterating received account_list"); + account_s *account = NULL; + account = (account_s*)iter->data; + if (callback((account_h)account, user_data) == false) + { + _INFO("application callback requested to discontinue."); break; } + } + _INFO("account_query_account_by_user_name end"); - ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s)); + _account_gslist_account_free(account_list); + return ACCOUNT_ERROR_NONE; +} - _account_type_convert_column_to_provider_feature(hstmt, feature_record); +ACCOUNT_API int account_query_account_by_package_name(account_cb callback, const char *package_name, void *user_data) +{ + _INFO("account_query_account_by_package_name starting"); + char* account_db_path = ACCOUNT_DB_NAME; - cb_ret = callback(feature_record->app_id, feature_record->key, user_data); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL")); - _account_type_free_feature_items(feature_record); - _ACCOUNT_FREE(feature_record); + _GET_COOKIE - ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + GError *error = NULL; - rc = _account_query_step(hstmt); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + GVariant* account_list_variant = NULL; + bool is_success = account_manager_call_account_query_account_by_package_name_sync(acc_mgr, account_db_path, package_name, e_cookie, &account_list_variant, NULL, &error); - error_code = ACCOUNT_ERROR_NONE; + _DESTROY_COOKIE -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + int error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + if (error_code != ACCOUNT_ERROR_NONE) + { + return error_code; } - pthread_mutex_unlock(&account_mutex); - return error_code; -} + GSList* account_list = unmarshal_account_list(account_list_variant); + g_variant_unref(account_list_variant); -ACCOUNT_API bool account_type_query_supported_feature(const char* app_id, const char* capability) -{ - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int record_count = 0; + if (account_list == NULL) + { + return ACCOUNT_ERROR_NO_DATA; + } - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; + GSList* iter; - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - set_last_result(ACCOUNT_ERROR_PERMISSION_DENIED); - return false; - } + for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) + { + _INFO("iterating received account_list"); + account_s *account = NULL; + account = (account_s*)iter->data; - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) + if (callback((account_h)account, user_data) == false) { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - set_last_result(ACCOUNT_ERROR_PERMISSION_DENIED); - return false; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - set_last_result(ACCOUNT_ERROR_PERMISSION_DENIED); - return false; + _INFO("application callback requested to discontinue."); + break; } - close(fd); } + _account_gslist_account_free(account_list); + _INFO("account_query_account_by_package_name end"); + return ACCOUNT_ERROR_NONE; +} - if (app_id == NULL || capability == NULL) { - set_last_result(ACCOUNT_ERROR_INVALID_PARAMETER); - return false; +ACCOUNT_API int account_query_account_by_capability(account_cb callback, const char* capability_type, account_capability_state_e capability_value, void *user_data) +{ + _INFO("account_query_account_by_capability starting"); + char* account_db_path = ACCOUNT_DB_NAME; + + ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL")); + + if (((int)capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) { + ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL")); - record_count = _account_get_record_count(query); + _GET_COOKIE - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - set_last_result(ACCOUNT_ERROR_PERMISSION_DENIED); - return false; - } + GError *error = NULL; - if (record_count <= 0) { - set_last_result(ACCOUNT_ERROR_RECORD_NOT_FOUND); - return false; + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - set_last_result(ACCOUNT_ERROR_NONE); - return true; + GVariant* account_list_variant = NULL; + bool is_success = account_manager_call_account_query_account_by_capability_sync(acc_mgr, account_db_path, capability_type, capability_value, e_cookie, &account_list_variant, NULL, &error); -} + _DESTROY_COOKIE + int error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + if (error_code != ACCOUNT_ERROR_NONE) + { + return error_code; + } -ACCOUNT_API int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb callback, void* user_data) -{ - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + GSList* account_list = unmarshal_account_list(account_list_variant); + g_variant_unref(account_list_variant); - GSList *iter; - account_type_s *data = (account_type_s*)account_type; + if (account_list == NULL) + { + return ACCOUNT_ERROR_NO_DATA; + } - for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { - provider_feature_s *feature_data = NULL; + GSList* iter; - feature_data = (provider_feature_s*)iter->data; + for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) + { + _INFO("iterating received account_list"); + account_s *account = NULL; + account = (account_s*)iter->data; - if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) { - ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n"); - return ACCOUNT_ERROR_NONE; + if (callback((account_h)account, user_data) == false) + { + _INFO("application callback requested to discontinue."); + break; } } - + _account_gslist_account_free(account_list); + _INFO("account_query_account_by_capability end"); return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature) +ACCOUNT_API int account_query_account_by_capability_type(account_cb callback, const char* capability_type, void* user_data) { - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null")); - ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null")); + _INFO("account_query_account_by_capability_type starting"); + char* account_db_path = ACCOUNT_DB_NAME; - account_type_s *data = (account_type_s*)account_type; + ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL")); - GSList *iter = NULL; - bool b_is_new = TRUE; + _GET_COOKIE - for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { - provider_feature_s *feature_data = NULL; - feature_data = (provider_feature_s*)iter->data; + GError *error = NULL; - if(!strcmp(feature_data->key, provider_feature)) { - b_is_new = FALSE; - break; - } + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - if(b_is_new) { - provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s)); + GVariant* account_list_variant = NULL; + bool is_success = account_manager_call_account_query_account_by_capability_type_sync(acc_mgr, account_db_path, capability_type, e_cookie, &account_list_variant, NULL, &error); - if (feature_data == NULL) - return ACCOUNT_ERROR_OUT_OF_MEMORY; - ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s)); + _DESTROY_COOKIE - feature_data->key = _account_get_text(provider_feature); - data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data); + int error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + if (error_code != ACCOUNT_ERROR_NONE) + { + return error_code; } - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id) -{ - int rc, count = 1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - account_stmt hstmt = NULL; - - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); - ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + GSList* account_list = unmarshal_account_list(account_list_variant); + g_variant_unref(account_list_variant); - if (g_slist_length( account_type->provider_feature_list)==0) { - ACCOUNT_ERROR( "no capability\n"); - return ACCOUNT_ERROR_NONE; + if (account_list == NULL) + { + return ACCOUNT_ERROR_NO_DATA; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id); + GSList* iter; - rc = _account_get_record_count(query); + for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) + { + _INFO("iterating received account_list"); + account_s *account = NULL; + account = (account_s*)iter->data; - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; + if (callback((account_h)account, user_data) == false) + { + _INFO("application callback requested to discontinue."); + break; + } } + _account_gslist_account_free(account_list); + _INFO("account_query_account_by_capability end"); + return ACCOUNT_ERROR_NONE; +} - if (rc <= 0) { - ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } +ACCOUNT_API int account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data) +{ + _INFO("account_query_capability_by_account_id starting"); + char* account_db_path = ACCOUNT_DB_NAME; - /* insert query*/ + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); - GSList *iter; + _GET_COOKIE - for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { - int ret; - count = 1; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES " - "(?, ?) ", PROVIDER_FEATURE_TABLE); + GError *error = NULL; + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } - hstmt = _account_prepare_query(query); + GVariant* capability_list_variant = NULL; + bool is_success = account_manager_call_account_query_capability_by_account_id_sync(acc_mgr, account_db_path, account_id, e_cookie, &capability_list_variant, NULL, &error); - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + _DESTROY_COOKIE - provider_feature_s* feature_data = NULL; - feature_data = (provider_feature_s*)iter->data; + int error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + if (error_code != ACCOUNT_ERROR_NONE) + { + return error_code; + } - ret = _account_query_bind_text(hstmt, count++, app_id); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, feature_data->key); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + _INFO("before unmarshal_capability_list"); + GSList* capability_list = unmarshal_capability_list(capability_list_variant); + _INFO("after unmarshal_capability_list"); + if (capability_list == NULL) + { + return ACCOUNT_ERROR_NO_DATA; + } - rc = _account_query_step(hstmt); + GSList* iter; - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + for (iter = capability_list; iter != NULL; iter = g_slist_next(iter)) + { + _INFO("iterating received account_list"); + account_capability_s *capability = NULL; + capability = (account_capability_s*)iter->data; + _INFO(""); + if (callback(capability->type, capability->value, user_data) == false) + { + _INFO("application callback requested to discontinue."); break; } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - + _INFO(""); } + _account_gslist_capability_free(capability_list); + _INFO("account_query_capability_by_account_id end"); return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_insert_to_db(account_type_h account_type, int* account_type_id) +static int _account_get_total_count(int *count, bool include_hidden) { - int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + _INFO("account_get_total_count_from_db starting"); + char* account_db_path = ACCOUNT_DB_NAME; - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); + if (!count) + { + ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL")); - ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL")); - - account_type_s *data = (account_type_s*)account_type; - - pthread_mutex_lock(&account_mutex); + GError *error = NULL; + if (count == NULL) + { + _INFO("Invalid input"); + return -1; + } - /* transaction control required*/ - ret_transaction = _account_begin_transaction(); + _GET_COOKIE - if( _account_db_err_code() == SQLITE_PERM ){ - pthread_mutex_unlock(&account_mutex); - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); return ACCOUNT_ERROR_PERMISSION_DENIED; } - if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ - ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DATABASE_BUSY; - } else if (ret_transaction != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction); - pthread_mutex_unlock(&account_mutex); - return ret_transaction; - } - - if (_account_type_check_duplicated(data)) { - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction); - *account_type_id = -1; - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DUPLICATED; - } else { - *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE); - - error_code = _account_type_execute_insert_query(data); - - if (error_code != ACCOUNT_ERROR_NONE){ - error_code = ACCOUNT_ERROR_DUPLICATED; - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction); - *account_type_id = -1; - pthread_mutex_unlock(&account_mutex); - return error_code; - } - } + int temp_count = -1; + bool is_success = account_manager_call_account_get_total_count_from_db_sync(acc_mgr, account_db_path, include_hidden, e_cookie, &temp_count, NULL, &error); - error_code = _account_type_insert_provider_feature(data, data->app_id); - if(error_code != ACCOUNT_ERROR_NONE) { - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction); - pthread_mutex_unlock(&account_mutex); - return error_code; - } - error_code = _account_type_insert_label(data); - if(error_code != ACCOUNT_ERROR_NONE) { - ret_transaction = _account_end_transaction(FALSE); - ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction); - pthread_mutex_unlock(&account_mutex); + _DESTROY_COOKIE + + int error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + if (error_code != ACCOUNT_ERROR_NONE) + { return error_code; } - ret_transaction = _account_end_transaction(TRUE); + *count = temp_count; + _INFO("account_get_total_count_from_db end"); + return ACCOUNT_ERROR_NONE; +} + +ACCOUNT_API int account_get_total_count_from_db(int *count) +{ + _INFO("account_get_total_count_from_db starting"); + + return _account_get_total_count(count, true); +} - pthread_mutex_unlock(&account_mutex); +ACCOUNT_INTERNAL_API int account_get_total_count_from_db_ex(int *count) +{ + _INFO("account_get_total_count_from_db_ex starting"); - return ACCOUNT_ERROR_NONE; + return _account_get_total_count(count, false); } -static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id) +ACCOUNT_API int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status) { - int rc, count = 1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - account_stmt hstmt = NULL; - - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + _INFO("account_update_sync_status_by_id starting"); + int error_code = ACCOUNT_ERROR_NONE; + char* account_db_path = ACCOUNT_DB_NAME; - if (g_slist_length( account_type->provider_feature_list)==0) { - ACCOUNT_ERROR( "no feature\n"); - return ACCOUNT_ERROR_NONE; + ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + if ( ((int)sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) { + ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_DEBUG( "app id", app_id); + _GET_COOKIE - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + GError *error = NULL; - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE); - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); return ACCOUNT_ERROR_PERMISSION_DENIED; } - count = 1; - _account_query_bind_text(hstmt, count++, app_id); - rc = _account_query_step(hstmt); - - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_DB_FAILED; - } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - GSList *iter; - - for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { - int ret; - count = 1; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES " - "(?, ?) ", PROVIDER_FEATURE_TABLE); + bool is_success = account_manager_call_account_update_sync_status_by_id_sync(acc_mgr, account_db_path, account_db_id, sync_status, e_cookie, NULL, &error); - hstmt = _account_prepare_query(query); + _DESTROY_COOKIE - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); - provider_feature_s* feature_data = NULL; - feature_data = (provider_feature_s*)iter->data; - - ret = _account_query_bind_text(hstmt, count++, account_type->app_id); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, feature_data->key); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - - rc = _account_query_step(hstmt); + return error_code; +} - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - break; - } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; +/* +static int _account_type_free_label_items(label_s *data) +{ + if(!data) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->label); + _ACCOUNT_FREE(data->locale); + return ACCOUNT_ERROR_NONE; } -static int _account_type_update_label(account_type_s *account_type, const char* app_id) + +static int _account_type_label_gslist_free(GSList* list) { - int rc, count = 1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - account_stmt hstmt = NULL; + ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL")); - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + GSList* iter; - if (g_slist_length( account_type->label_list)==0) { - return ACCOUNT_ERROR_NONE; + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + label_s *label_data = (label_s*)iter->data; + _account_type_free_label_items(label_data); + _ACCOUNT_FREE(label_data); } - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE); - hstmt = _account_prepare_query(query); + g_slist_free(list); + list = NULL; - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + return ACCOUNT_ERROR_NONE; +} - count = 1; - _account_query_bind_text(hstmt, count++, app_id); - rc = _account_query_step(hstmt); - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_DB_FAILED; +static int _account_type_free_provider_feature_items(provider_feature_s *data) +{ + if(!data) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - GSList *iter; - - for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) { - int ret; - count = 1; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES " - "(?, ?, ?) ", LABEL_TABLE); - hstmt = _account_prepare_query(query); + _ACCOUNT_FREE(data->key); + _ACCOUNT_FREE(data->app_id); - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + return ACCOUNT_ERROR_NONE; +} - label_s* label_data = NULL; - label_data = (label_s*)iter->data; - ret = _account_query_bind_text(hstmt, count++, account_type->app_id); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, label_data->label); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, label_data->locale); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); +static int _account_type_provider_feature_gslist_free(GSList* list) +{ + ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL")); - rc = _account_query_step(hstmt); + GSList* iter; - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - break; - } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + provider_feature_s *feature_data = (provider_feature_s*)iter->data; + _account_type_free_provider_feature_items(feature_data); + _ACCOUNT_FREE(feature_data); } + g_slist_free(list); + list = NULL; + return ACCOUNT_ERROR_NONE; } -static int _account_type_update_account(account_type_s *account_type, const char* app_id) +static int _account_type_free_account_type_items(account_type_s *data) { - int rc = 0, binding_count =1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - - if (!account_type->app_id) { - ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n"); + if(!data) { return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, " - "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE); + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->service_provider_id); + _ACCOUNT_FREE(data->icon_path); + _ACCOUNT_FREE(data->small_icon_path); - hstmt = _account_prepare_query(query); + _account_type_label_gslist_free(data->label_list); + _account_type_provider_feature_gslist_free(data->provider_feature_list); +// _account_type_glist_free(data->account_type_list); - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } else if (_account_db_err_code() == SQLITE_BUSY){ - ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_DATABASE_BUSY; - } + return ACCOUNT_ERROR_NONE; +} - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg())); +static int _account_type_gslist_free(GSList* list) +{ + ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL")); - binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query); - _account_query_bind_text(hstmt, binding_count++, app_id); + GSList* iter; - rc = _account_query_step(hstmt); - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + account_type_s *account_type_record = (account_type_s*)iter->data; + _account_type_free_account_type_items(account_type_record); + _ACCOUNT_FREE(account_type_record); } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - - /*update label*/ - error_code = _account_type_update_label(account_type, app_id); - /* update provider feature */ - error_code = _account_type_update_provider_feature(account_type, app_id); + g_slist_free(list); + list = NULL; - return error_code; + return ACCOUNT_ERROR_NONE; } +*/ -ACCOUNT_API int account_type_update_to_db_by_app_id(account_type_h account_type, const char* app_id) +/* +static int _account_type_glist_free(GList* list) { - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; + ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL")); - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + GList* iter; - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + for (iter = list; iter != NULL; iter = g_list_next(iter)) { + account_type_s *account_type_record = (account_type_s*)iter->data; + _account_type_free_account_type_items(account_type_record); + _ACCOUNT_FREE(account_type_record); + } - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); + g_list_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} +*/ + +ACCOUNT_API int account_type_create(account_type_h *account_type) +{ + if (!account_type) { + ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); - ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s)); - int error_code = ACCOUNT_ERROR_NONE; - account_type_s* data = (account_type_s*)account_type; + if (data == NULL) + { + ACCOUNT_ERROR("Memory Allocation Failed"); + return ACCOUNT_ERROR_OUT_OF_MEMORY; + } - pthread_mutex_lock(&account_mutex); + ACCOUNT_MEMSET(data, 0, sizeof(account_type_s)); - error_code = _account_type_update_account(data, app_id); + data->id = -1; + data->app_id = NULL; + data->service_provider_id = NULL; + data->icon_path = NULL; + data->small_icon_path = NULL; + data->multiple_account_support = false; + data->label_list = NULL; +// data->account_type_list = NULL; + data->provider_feature_list = NULL; - pthread_mutex_unlock(&account_mutex); + *account_type = (account_type_h)data; - return error_code; + return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_delete_by_app_id(const char* app_id) +ACCOUNT_API int account_type_destroy(account_type_h account_type) { - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0, count = -1; - int ret_transaction = 0; - int binding_count = 1; - bool is_success = FALSE; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + _INFO("account_type_destroy"); - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + account_type_s *data = (account_type_s*)account_type; - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); + if (data == NULL) + { + _ERR("Account type handle is null!"); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected.")); - - /* Check requested ID to delete */ - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id); + _account_type_free_account_type_with_items(data); - count = _account_get_record_count(query); + return ACCOUNT_ERROR_NONE; +} - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; +ACCOUNT_INTERNAL_API int account_type_set_app_id(account_type_h account_type, const char *app_id) +{ + if (!account_type) { + ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - if (count <= 0) { - ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; + if (!app_id) { + ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; } - /* transaction control required*/ - ret_transaction = _account_begin_transaction(); + account_type_s *data = (account_type_s*)account_type; - if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ - ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DATABASE_BUSY; - }else if (ret_transaction != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); - pthread_mutex_unlock(&account_mutex); - return ret_transaction; - } + _ACCOUNT_FREE(data->app_id); + data->app_id = _account_get_text(app_id); - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE); + return ACCOUNT_ERROR_NONE; +} - hstmt = _account_prepare_query(query); +ACCOUNT_INTERNAL_API int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_PERMISSION_DENIED; + if (!service_provider_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + account_type_s *data = (account_type_s*)account_type; - _account_query_bind_text(hstmt, binding_count++, app_id); + _ACCOUNT_FREE(data->service_provider_id); + data->service_provider_id = _account_get_text(service_provider_id); - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + return ACCOUNT_ERROR_NONE; +} - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; +ACCOUNT_INTERNAL_API int account_type_set_icon_path(account_type_h account_type, const char *icon_path) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - binding_count = 1; - ACCOUNT_MEMSET(query, 0, sizeof(query)); + if (!icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE); + account_type_s *data = (account_type_s*)account_type; - hstmt = _account_prepare_query(query); - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + _ACCOUNT_FREE(data->icon_path); + data->icon_path = _account_get_text(icon_path); - _account_query_bind_text(hstmt, binding_count++, app_id); + return ACCOUNT_ERROR_NONE; +} - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc)); +ACCOUNT_INTERNAL_API int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - is_success = TRUE; + if (!small_icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - hstmt = NULL; + account_type_s *data = (account_type_s*)account_type; - binding_count = 1; - ACCOUNT_MEMSET(query, 0, sizeof(query)); + _ACCOUNT_FREE(data->small_icon_path); + data->small_icon_path = _account_get_text(small_icon_path); - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE); + return ACCOUNT_ERROR_NONE; +} - hstmt = _account_prepare_query(query); - ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, - ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); +ACCOUNT_INTERNAL_API int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__)); - _account_query_bind_text(hstmt, binding_count++, app_id); + account_type_s *data = (account_type_s*)account_type; - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc)); + data->multiple_account_support = multiple_account_support; - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - is_success = TRUE; + return ACCOUNT_ERROR_NONE; +} - hstmt = NULL; +ACCOUNT_INTERNAL_API int account_type_set_label(account_type_h account_type, const char* label, const char* locale) +{ + if (!account_type) { + ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + if(!label || !locale) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ret_transaction = _account_end_transaction(is_success); + account_type_s *data = (account_type_s*)account_type; + label_s *label_data = (label_s*)malloc(sizeof(label_s)); - if (ret_transaction != ACCOUNT_ERROR_NONE) { - ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); + if (label_data == NULL) { + return ACCOUNT_ERROR_OUT_OF_MEMORY; } + ACCOUNT_MEMSET(label_data, 0, sizeof(label_s)); + + label_data->label = _account_get_text(label); + label_data->locale = _account_get_text(locale); - pthread_mutex_unlock(&account_mutex); + data->label_list = g_slist_append(data->label_list, (gpointer)label_data); - return error_code; + return ACCOUNT_ERROR_NONE; } -static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record) +ACCOUNT_INTERNAL_API int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature) { - const char *textbuf = NULL; - - account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID); - _account_db_data_to_text(textbuf, &(account_type_record->app_id)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID); - _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id)); - - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH); - _account_db_data_to_text(textbuf, &(account_type_record->icon_path)); + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null")); + ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null")); - textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH); - _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path)); + account_type_s *data = (account_type_s*)account_type; - account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT); + GSList *iter = NULL; + bool b_is_new = TRUE; -} + for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + provider_feature_s *feature_data = NULL; + feature_data = (provider_feature_s*)iter->data; -static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record) -{ - const char *textbuf = NULL; + if(!strcmp(feature_data->key, provider_feature)) { + b_is_new = FALSE; + break; + } + } - textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID); - _account_db_data_to_text(textbuf, &(label_record->app_id)); + if(b_is_new) { + provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s)); - textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL); - _account_db_data_to_text(textbuf, &(label_record->label)); + if (feature_data == NULL) + return ACCOUNT_ERROR_OUT_OF_MEMORY; + ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s)); - textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE); - _account_db_data_to_text(textbuf, &(label_record->locale)); + feature_data->key = _account_get_text(provider_feature); + data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data); + } + return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data ) +ACCOUNT_API int account_type_query_provider_feature_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data ) { - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0, binding_count = 1; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } + _INFO("account_type_query_provider_feature_by_app_id start"); + char* account_db_path = ACCOUNT_DB_NAME; ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + _GET_COOKIE - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE); - hstmt = _account_prepare_query(query); + GError *error = NULL; + gint error_code = ACCOUNT_ERROR_NONE; - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); return ACCOUNT_ERROR_PERMISSION_DENIED; } - _account_query_bind_text(hstmt, binding_count++, app_id); - - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + GVariant* feature_list_variant = NULL; + bool is_success = account_manager_call_account_type_query_provider_feature_by_app_id_sync(acc_mgr, account_db_path, app_id, e_cookie, &feature_list_variant, NULL, &error); + _INFO("account_manager_call_account_type_query_provider_feature_by_app_id_sync end=[%d]", is_success); + + _DESTROY_COOKIE + + if (!is_success) + { + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("Account IPC call returned error[%d]", error_code); + return error_code; + } + g_clear_error(&error); + + GSList* provider_feature_list = variant_to_provider_feature_list(feature_list_variant); + if (provider_feature_list == NULL) + { + error_code = ACCOUNT_ERROR_NO_DATA; + _ERR("[%d]", error_code); + return error_code; + } - label_s* label_record = NULL; + GSList *iter; + for (iter = provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + provider_feature_s *feature_data = NULL; - while (rc == SQLITE_ROW) { - bool cb_ret = FALSE; - label_record = (label_s*) malloc(sizeof(label_s)); + feature_data = (provider_feature_s*)iter->data; - if (label_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; + if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) { + ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n"); + return ACCOUNT_ERROR_NONE; } + } - ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s)); + _account_type_gslist_feature_free(provider_feature_list); + _INFO("account_type_query_provider_feature_by_app_id end"); + return error_code; +} - _account_type_convert_column_to_label(hstmt, label_record); +ACCOUNT_API bool account_type_query_supported_feature(const char* app_id, const char* capability) +{ + _INFO("account_type_query_supported_feature start"); + char* account_db_path = ACCOUNT_DB_NAME; - cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data); + if (app_id == NULL || capability == NULL) + { + set_last_result(ACCOUNT_ERROR_INVALID_PARAMETER); + return false; + } - _account_type_free_label_items(label_record); - _ACCOUNT_FREE(label_record); + _GET_COOKIE - ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + int is_supported = 0; + GError *error = NULL; + gint ret = ACCOUNT_ERROR_NONE; - rc = _account_query_step(hstmt); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + set_last_result(ACCOUNT_ERROR_PERMISSION_DENIED); + return false; } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + bool is_success = account_manager_call_account_type_query_supported_feature_sync(acc_mgr, account_db_path, app_id, capability, e_cookie, &is_supported, NULL, &error); + _INFO("account_manager_call_account_type_query_supported_feature_sync end=[%d]", is_success); - error_code = ACCOUNT_ERROR_NONE; + _DESTROY_COOKIE -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + if (!is_success) + { + ret = _account_get_error_code(is_success, error); + g_clear_error(&error); + _ERR("Account IPC call returned error[%d]", ret); + set_last_result(ret); + return false; } + g_clear_error(&error); - pthread_mutex_unlock(&account_mutex); - return error_code; + set_last_result(ACCOUNT_ERROR_NONE); + _INFO("account_type_query_supported_feature end"); + return is_supported; } -int _account_type_label_get_app_id(label_h label, char **app_id) +ACCOUNT_API int account_type_get_app_id(account_type_h account_type, char **app_id) { - if (!label) { + if (!account_type) { return ACCOUNT_ERROR_INVALID_PARAMETER; } @@ -6329,815 +2273,593 @@ int _account_type_label_get_app_id(label_h label, char **app_id) return ACCOUNT_ERROR_INVALID_PARAMETER; } - label_s *data = (label_s*)label; + account_type_s *data = (account_type_s*)account_type; (*app_id) = NULL; - *app_id = _account_get_text(data->app_id); return ACCOUNT_ERROR_NONE; } -bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data) +ACCOUNT_API int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id) { - account_type_s *data = (account_type_s*)user_data; - - label_s *label_data = (label_s*)malloc(sizeof(label_s)); + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - if (label_data == NULL) { - ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n"); - return FALSE; + if (!service_provider_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_MEMSET(label_data, 0, sizeof(label_s)); - label_data->app_id = _account_get_text(app_id); - label_data->label = _account_get_text(label); - label_data->locale = _account_get_text(locale); + account_type_s *data = (account_type_s*)account_type; - data->label_list = g_slist_append(data->label_list, (gpointer)label_data); + (*service_provider_id) = NULL; + *service_provider_id = _account_get_text(data->service_provider_id); - return TRUE; + return ACCOUNT_ERROR_NONE; } -bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data) +ACCOUNT_API int account_type_get_icon_path(account_type_h account_type, char **icon_path) { - account_type_s *data = (account_type_s*)user_data; - - provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s)); + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - if (feature_data == NULL) { - ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n"); - return FALSE; + if (!icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s)); - feature_data->app_id = _account_get_text(app_id); - feature_data->key = _account_get_text(key); + account_type_s *data = (account_type_s*)account_type; - data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data); + (*icon_path) = NULL; + *icon_path = _account_get_text(data->icon_path); - return TRUE; + return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_query_by_app_id(const char* app_id, account_type_h *account_type) +ACCOUNT_API int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path) { - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0, binding_count = 1; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); - ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE); - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; + if (!small_icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - _account_query_bind_text(hstmt, binding_count++, app_id); + account_type_s *data = (account_type_s*)account_type; - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + (*small_icon_path) = NULL; + *small_icon_path = _account_get_text(data->small_icon_path); - account_type_s *account_type_record = (account_type_s *)(*account_type); + return ACCOUNT_ERROR_NONE; +} - while (rc == SQLITE_ROW) { - _account_type_convert_column_to_account_type(hstmt, account_type_record); - rc = _account_query_step(hstmt); +ACCOUNT_API int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + if (!multiple_account_support) { + return ACCOUNT_ERROR_INVALID_PARAMETER; } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)account_type_record); - account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)account_type_record); - - hstmt = NULL; - error_code = ACCOUNT_ERROR_NONE; + account_type_s *data = (account_type_s*)account_type; - CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } + *multiple_account_support = data->multiple_account_support; - pthread_mutex_unlock(&account_mutex); - return error_code; + return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_query_app_id_exist(const char* app_id) +ACCOUNT_API int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb callback, void* user_data) { - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; + GSList *iter; + account_type_s *data = (account_type_s*)account_type; - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + provider_feature_s *feature_data = NULL; - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + feature_data = (provider_feature_s*)iter->data; - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; + if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) { + ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n"); + return ACCOUNT_ERROR_NONE; } - close(fd); - } - - ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id); - rc = _account_get_record_count(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if (rc <= 0) { - return ACCOUNT_ERROR_RECORD_NOT_FOUND; } return ACCOUNT_ERROR_NONE; } -ACCOUNT_API int account_type_query_by_provider_feature(account_type_cb callback, const char* key, void* user_data) +ACCOUNT_API int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label) { - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - GList *account_type_list = NULL; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER")); - ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL")); - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + GSList *iter; + account_type_s *data = (account_type_s*)account_type; - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) { + label_s *label_data = NULL; - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE); + label_data = (label_s*)iter->data; - hstmt = _account_prepare_query(query); + *label = NULL; - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; + if(!strcmp(locale, label_data->locale)) { + *label = _account_get_text(label_data->label); + return ACCOUNT_ERROR_NONE; + } + gchar** tokens = g_strsplit(locale, "-", 2); + if(tokens != NULL) { + if((char*)(tokens[1]) != NULL) { + char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1])); + if(upper_token != NULL) { + char* converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token); + if(!strcmp(converted_locale, label_data->locale)) { + _ACCOUNT_FREE(converted_locale); + _ACCOUNT_FREE(upper_token); + g_strfreev(tokens); + *label = _account_get_text(label_data->label); + return ACCOUNT_ERROR_NONE; + } + _ACCOUNT_FREE(converted_locale); + } + _ACCOUNT_FREE(upper_token); + } + } + g_strfreev(tokens); } - int binding_count = 1; - _account_query_bind_text(hstmt, binding_count++, key); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; +} - rc = _account_query_step(hstmt); +ACCOUNT_API int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); - account_type_s *account_type_record = NULL; + GSList *iter; + account_type_s *data = (account_type_s*)account_type; - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) { + label_s *label_data = NULL; - int k=0; - while(rc == SQLITE_ROW) { - account_type_record = (account_type_s*) malloc(sizeof(account_type_s)); + label_data = (label_s*)iter->data; - if (account_type_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; + if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) { + ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n"); + return ACCOUNT_ERROR_NONE; } - - ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s)); - _account_type_convert_column_to_account_type(hstmt, account_type_record); - account_type_list = g_list_append(account_type_list, account_type_record); - rc = _account_query_step(hstmt); - k++; - } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_glist_free(account_type_list);}, rc, ("finalize error")); - hstmt = NULL; - - GList* iter; - k = 0; - for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) { - account_type_s *account_type = NULL; - account_type = (account_type_s*)iter->data; - account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type); - account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type); - //cb_func((account_type_h)account_type, user_data); - ACCOUNT_CATCH_ERROR(callback((account_type_h)account_type, user_data) == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); - k++; - } - - error_code = ACCOUNT_ERROR_NONE; - -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - if (account_type_list) { - _account_type_glist_free(account_type_list); - account_type_list = NULL; } - return error_code; + return ACCOUNT_ERROR_NONE; } - -ACCOUNT_API int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data) +ACCOUNT_INTERNAL_API int account_type_insert_to_db(account_type_h account_type, int* account_type_id) { - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; - GList *account_type_list = NULL; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } + _INFO("account_type_insert_to_db starting"); + char* account_db_path = ACCOUNT_DB_NAME; - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL")); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE); - hstmt = _account_prepare_query(query); + _GET_COOKIE - rc = _account_query_step(hstmt); + GError *error = NULL; - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); return ACCOUNT_ERROR_PERMISSION_DENIED; } - account_type_s *account_type_record = NULL; + int db_id = -1; + GVariant* account_type_serialized = marshal_account_type((account_type_s*) account_type); + bool is_success = account_manager_call_account_type_add_sync(acc_mgr, account_db_path, account_type_serialized, e_cookie, &db_id, NULL, &error); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + _DESTROY_COOKIE - int k=0; - while(rc == SQLITE_ROW) { - account_type_record = (account_type_s*) malloc(sizeof(account_type_s)); + int ret = _account_get_error_code(is_success, error); + g_clear_error(&error); - if (account_type_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; - } - - ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s)); - _account_type_convert_column_to_account_type(hstmt, account_type_record); - account_type_list = g_list_append(account_type_list, account_type_record); - rc = _account_query_step(hstmt); - k++; + if (ret != ACCOUNT_ERROR_NONE) + { + return ret; } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_glist_free(account_type_list);}, rc, ("finalize error")); - hstmt = NULL; - - GList* iter; - k = 0; - for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) { - account_type_s *account_type = NULL; - account_type = (account_type_s*)iter->data; - account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type); - account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type); - callback((account_type_h)account_type, user_data); - k++; - } + _INFO("account_type_insert_to_db end id=[%d]", db_id); - error_code = ACCOUNT_ERROR_NONE; + *account_type_id = db_id; -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } - if (account_type_list) { - _account_type_glist_free(account_type_list); - account_type_list = NULL; - } + account_type_s* account_type_data = (account_type_s*)account_type; + account_type_data->id = db_id; - return error_code; + return ACCOUNT_ERROR_NONE; } -// output parameter label must be free -ACCOUNT_API int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label) +ACCOUNT_INTERNAL_API int account_type_update_to_db_by_app_id(const account_type_h account_type, const char* app_id) { - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0, binding_count = 1; - - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - char* converted_locale = NULL; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - - ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); - ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null")); - ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null")); - - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - - converted_locale = _account_get_text(locale); - gchar** tokens = g_strsplit(converted_locale, "-", 2); + _INFO("account_type_update_to_db_by_app_id starting"); + int error_code = ACCOUNT_ERROR_NONE; + char* account_db_path = ACCOUNT_DB_NAME; - if(tokens != NULL) { - if((char*)(tokens[1]) != NULL) { - char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1])); - if(upper_token != NULL) { - _ACCOUNT_FREE(converted_locale); - converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token); - } - _ACCOUNT_FREE(upper_token); - } - } - g_strfreev(tokens); + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale); - _ACCOUNT_FREE(converted_locale); + _GET_COOKIE - hstmt = _account_prepare_query(query); + GError *error = NULL; - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); return ACCOUNT_ERROR_PERMISSION_DENIED; } - _account_query_bind_text(hstmt, binding_count++, app_id); + GVariant* account_type_variant = marshal_account_type((account_type_s*) account_type); + if (account_type_variant == NULL) + { + _ERR("Failed to serialize"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } - rc = _account_query_step(hstmt); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + bool is_success = account_manager_call_account_type_update_to_db_by_app_id_sync(acc_mgr, account_db_path, account_type_variant, app_id, e_cookie, NULL, &error); - label_s* label_record = NULL; + _DESTROY_COOKIE - while (rc == SQLITE_ROW) { - label_record = (label_s*) malloc(sizeof(label_s)); + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); - if (label_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; - } + return error_code; +} - ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s)); +ACCOUNT_INTERNAL_API int account_type_delete_by_app_id(const char* app_id) +{ + _INFO("account_type_delete_by_app_id starting"); + int error_code = ACCOUNT_ERROR_NONE; + char* account_db_path = ACCOUNT_DB_NAME; - _account_type_convert_column_to_label(hstmt,label_record); + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); - _ACCOUNT_FREE(*label); - *label = _account_get_text(label_record->label); + _GET_COOKIE - _account_type_free_label_items(label_record); - _ACCOUNT_FREE(label_record); + GError *error = NULL; - rc = _account_query_step(hstmt); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + bool is_success = account_manager_call_account_type_delete_by_app_id_sync(acc_mgr, account_db_path, app_id, e_cookie, NULL, &error); - error_code = ACCOUNT_ERROR_NONE; + _DESTROY_COOKIE - CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); - pthread_mutex_unlock(&account_mutex); return error_code; } -static int _account_insert_custom(account_s *account, int account_id) +ACCOUNT_API int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data ) { - int rc, count = 1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - account_stmt hstmt = NULL; + _INFO("account_type_query_label_by_app_id starting"); + char* account_db_path = ACCOUNT_DB_NAME; - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT Callback IS NULL")); + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); - if (g_slist_length( account->custom_list)==0) { - ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n"); - return ACCOUNT_ERROR_NONE; + _GET_COOKIE + + GError *error = NULL; + + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id); + GVariant* label_list_variant = NULL; + bool is_success = account_manager_call_account_type_query_label_by_app_id_sync(acc_mgr, account_db_path, app_id, e_cookie, &label_list_variant, NULL, &error); - rc = _account_get_record_count(query); + _DESTROY_COOKIE - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; + int ret = _account_get_error_code(is_success, error); + g_clear_error(&error); + + if (ret != ACCOUNT_ERROR_NONE) + { + return ret; } - if (rc <= 0) { - ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; + _INFO("before variant_to_label_list"); + GSList* label_list = variant_to_label_list (label_list_variant); + _INFO("after variant_to_label_list"); + if (label_list == NULL) + { + return ACCOUNT_ERROR_NO_DATA; } - /* insert query*/ + GSList* iter; - GSList *iter; + for (iter = label_list; iter != NULL; iter = g_slist_next(iter)) + { + _INFO("iterating received account_list"); + label_s *label_record = NULL; + label_record = (label_s*)iter->data; + _INFO(""); + if (callback(label_record->app_id, label_record->label, label_record->locale, user_data) == false) + { + _INFO("application callback requested to discontinue."); + break; + } + _INFO(""); + } - for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) { - int ret; - count = 1; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES " - "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE); + _account_type_gslist_label_free(label_list); + _INFO("account_type_query_label_by_app_id end"); + return ACCOUNT_ERROR_NONE; +} + +ACCOUNT_API int account_type_query_by_app_id(const char* app_id, account_type_h *account_type) +{ + _INFO("account_type_query_by_app_id starting"); + char* account_db_path = ACCOUNT_DB_NAME; - hstmt = _account_prepare_query(query); + ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE'S POINTER IS NULL")); + ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL")); - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + _GET_COOKIE - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + GError *error = NULL; - account_custom_s* custom_data = NULL; - custom_data = (account_custom_s*)iter->data; + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } - ret = _account_query_bind_int(hstmt, count++, account_id); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail")); - ret = _account_query_bind_text(hstmt, count++, account->package_name); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + GVariant* account_type_variant = NULL; + account_type_s *in_data = (account_type_s*) (*account_type); - rc = _account_query_step(hstmt); + bool is_success = account_manager_call_account_type_query_by_app_id_sync(acc_mgr, account_db_path, app_id, e_cookie, &account_type_variant, NULL, &error); - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - break; - } + _DESTROY_COOKIE - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + int ret = _account_get_error_code(is_success, error); + g_clear_error(&error); + if (ret != ACCOUNT_ERROR_NONE) + { + return ret; } + account_type_s* received_account_type = umarshal_account_type (account_type_variant); + ACCOUNT_RETURN_VAL((received_account_type != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("INVALID DATA RECEIVED FROM SVC")); + + in_data->id = received_account_type->id; + in_data->app_id = received_account_type->app_id; + in_data->service_provider_id = received_account_type->service_provider_id; + in_data->icon_path = received_account_type->icon_path; + in_data->small_icon_path = received_account_type->small_icon_path; + in_data->multiple_account_support = received_account_type->multiple_account_support; + in_data->label_list = received_account_type->label_list; +// in_data->account_type_list = received_account_type->account_type_list; + in_data->provider_feature_list = received_account_type->provider_feature_list; + + _ACCOUNT_FREE(received_account_type); + _INFO("account_type_query_by_app_id end"); return ACCOUNT_ERROR_NONE; } -static int _account_update_custom(account_s *account, int account_id) +ACCOUNT_API int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data) { - int rc, count = 1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - account_stmt hstmt = NULL; + _INFO("account_type_foreach_account_type_from_db starting"); + char* account_db_path = ACCOUNT_DB_NAME; - ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); - - if (g_slist_length( account->custom_list)==0) { - ACCOUNT_DEBUG( "_account_update_custom, no custom data\n"); - return ACCOUNT_ERROR_NONE; - } + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT Callback IS NULL")); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id); + _GET_COOKIE - rc = _account_get_record_count(query); + GError *error = NULL; - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); return ACCOUNT_ERROR_PERMISSION_DENIED; - } else if( _account_db_err_code() == SQLITE_BUSY ){ - ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); - pthread_mutex_unlock(&account_mutex); - return ACCOUNT_ERROR_DATABASE_BUSY; } - if (rc <= 0) { - ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_RECORD_NOT_FOUND; - } + GVariant* account_type_list_variant = NULL; + _INFO("before account_type_query_all_sync()"); + bool is_success = account_manager_call_account_type_query_all_sync(acc_mgr, account_db_path, e_cookie, &account_type_list_variant, NULL, &error); - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + _DESTROY_COOKIE - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE); - hstmt = _account_prepare_query(query); - count = 1; - _account_query_bind_int(hstmt, count++, (int)account_id); - rc = _account_query_step(hstmt); + _INFO("after account_type_query_all_sync()"); + int ret = _account_get_error_code(is_success, error); + g_clear_error(&error); - if (rc == SQLITE_BUSY) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_DATABASE_BUSY; - } else if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); - return ACCOUNT_ERROR_DB_FAILED; + if (ret != ACCOUNT_ERROR_NONE) + { + return ret; } - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + GSList* account_type_list = unmarshal_account_type_list(account_type_list_variant); + g_variant_unref(account_type_list_variant); - GSList *iter; - - for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) { - int ret; - count = 1; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES " - "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE); - - hstmt = _account_prepare_query(query); - - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); - - account_custom_s* custom_data = NULL; - custom_data = (account_custom_s*)iter->data; + if (account_type_list == NULL) + { + return ACCOUNT_ERROR_NO_DATA; + } - ret = _account_query_bind_int(hstmt, count++, (int)account_id); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); - ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value); - ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + GSList* iter; - rc = _account_query_step(hstmt); + for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) + { + _INFO("iterating received account_list"); + account_type_s *account_type = NULL; + account_type = (account_type_s*)iter->data; - if (rc != SQLITE_DONE) { - ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + if (callback((account_type_h)account_type, user_data) == false) + { + _INFO("application callback requested to discontinue."); break; } - - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; - } + _account_type_gslist_account_type_free(account_type_list); + _INFO("account_type_foreach_account_type_from_db end"); return ACCOUNT_ERROR_NONE; } -static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data ) +ACCOUNT_API int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label) { - int error_code = ACCOUNT_ERROR_NONE; - account_stmt hstmt = NULL; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; - int rc = 0; + _INFO("account_type_query_label_by_locale starting"); + char* account_db_path = ACCOUNT_DB_NAME; - ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); - ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); - ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID")); + ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO LOCALE")); + ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null")); - ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + _GET_COOKIE - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id); - hstmt = _account_prepare_query(query); + GError *error = NULL; - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); return ACCOUNT_ERROR_PERMISSION_DENIED; } - rc = _account_query_step(hstmt); + char* label_temp = NULL; + _INFO("before account_type_query_label_by_locale_sync()"); + bool is_success = account_manager_call_account_type_query_label_by_locale_sync(acc_mgr, account_db_path, app_id, locale, e_cookie, &label_temp, NULL, &error); - ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + _DESTROY_COOKIE - account_custom_s* custom_record = NULL; + _INFO("after account_type_query_label_by_locale_sync() : is_success=%d", is_success); + int ret = _account_get_error_code(is_success, error); + g_clear_error(&error); - while (rc == SQLITE_ROW) { - bool cb_ret = FALSE; - custom_record = (account_custom_s*) malloc(sizeof(account_custom_s)); - - if (custom_record == NULL) { - ACCOUNT_FATAL("malloc Failed"); - break; - } - - ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s)); + if (ret != ACCOUNT_ERROR_NONE) + { + return ret; + } - _account_convert_column_to_custom(hstmt, custom_record); + if (label_temp == NULL) + { + return ACCOUNT_ERROR_NO_DATA; + } - cb_ret = callback(custom_record->key, custom_record->value, user_data); + *label = _account_get_text(label_temp); + _INFO("account_type_query_label_by_locale end"); + return ACCOUNT_ERROR_NONE; - _account_custom_item_free(custom_record); - _ACCOUNT_FREE(custom_record); +} - ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); +ACCOUNT_API int account_type_query_by_provider_feature(account_type_cb callback, const char* key, void* user_data) +{ + _INFO("account_type_query_by_provider_feature starting"); + char* account_db_path = ACCOUNT_DB_NAME; - rc = _account_query_step(hstmt); - } + ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL")); - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + _GET_COOKIE - error_code = ACCOUNT_ERROR_NONE; + GError *error = NULL; -CATCH: - if (hstmt != NULL) { - rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); - hstmt = NULL; + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - pthread_mutex_unlock(&account_mutex); - return error_code; -} + GVariant* account_type_list_variant = NULL; + bool is_success = account_manager_call_account_type_query_by_provider_feature_sync(acc_mgr, account_db_path, key, e_cookie, &account_type_list_variant, NULL, &error); -static void _account_subscribe_vconf_callback(keynode_t *key, void *user_data) -{ - account_subscribe_s* tmp = (account_subscribe_s*)user_data; - char *msg = NULL, *vconf_key = NULL; - char *key_name = NULL; - char event_msg[256] ={0, }; - int account_id = -1; + _DESTROY_COOKIE - if(!key) { - ACCOUNT_ERROR("No subscribtion msg !!!!!\n"); - return; - } + int ret = _account_get_error_code(is_success, error); + g_clear_error(&error); - if(!tmp) { - ACCOUNT_ERROR("user data required\n"); - return; + if (ret != ACCOUNT_ERROR_NONE) + { + return ret; } - key_name = vconf_keynode_get_name(key); - if( key_name == NULL ) { - ACCOUNT_ERROR("vconf_keynode_get_name(key) fail\n"); - return; - } + GSList* account_type_list = unmarshal_account_type_list(account_type_list_variant); + g_variant_unref(account_type_list_variant); - if(!memcmp(key_name, VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR))) + if (account_type_list == NULL) { - vconf_key = vconf_keynode_get_str(key); + return ACCOUNT_ERROR_NO_DATA; + } - if( vconf_key == NULL){ - ACCOUNT_ERROR("vconf key is NULL.\n"); - return; + GSList* iter; + + for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) + { + _INFO("iterating received account_type_list"); + account_type_s *account_type = NULL; + account_type = (account_type_s*)iter->data; + _INFO(""); + if (callback((account_type_h)account_type, user_data) == false) + { + _INFO("Application callback requested not to continue"); + break; } - msg = strdup(vconf_key); + _INFO(""); + } - char* event_type = NULL; - char* id = NULL; - char* ptr = NULL; + _account_type_gslist_account_type_free(account_type_list); + _INFO("account_type_query_by_provider_feature end"); + return ACCOUNT_ERROR_NONE; +} - event_type = strtok_r(msg, ":", &ptr); - id = strtok_r(NULL, ":", &ptr); +ACCOUNT_API int account_type_query_app_id_exist(const char* app_id) +{ + _INFO("account_type_query_app_id_exist starting"); + int error_code = ACCOUNT_ERROR_NONE; + char* account_db_path = ACCOUNT_DB_NAME; - ACCOUNT_SLOGD("msg(%s), event_type(%s), id(%s)", msg, event_type, id); + ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); - ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type); + _GET_COOKIE - account_id = atoi(id); + GError *error = NULL; - if(tmp->account_subscription_callback) - tmp->account_subscription_callback(event_msg, account_id, tmp->user_data); + AccountManager* acc_mgr = _account_manager_get_instance(); + if (acc_mgr == NULL) + { + _ERR("g_bus_get_sync failed"); + return ACCOUNT_ERROR_PERMISSION_DENIED; } - _ACCOUNT_FREE(msg); + bool is_success = account_manager_call_account_type_query_app_id_exist_sync(acc_mgr, account_db_path, app_id, e_cookie, NULL, &error); + _DESTROY_COOKIE + + error_code = _account_get_error_code(is_success, error); + g_clear_error(&error); + + return error_code; } -static void _account_subscribe_vconf_callback_ex(keynode_t *key, void *user_data) + +static void _account_subscribe_vconf_callback(keynode_t *key, void *user_data) { account_subscribe_s* tmp = (account_subscribe_s*)user_data; char *msg = NULL, *vconf_key = NULL; @@ -7156,7 +2878,8 @@ static void _account_subscribe_vconf_callback_ex(keynode_t *key, void *user_data } key_name = vconf_keynode_get_name(key); - if( key_name == NULL ) { + + if ( key_name == NULL ) { ACCOUNT_ERROR("vconf_keynode_get_name(key) fail\n"); return; } @@ -7174,11 +2897,14 @@ static void _account_subscribe_vconf_callback_ex(keynode_t *key, void *user_data char* event_type = NULL; char* id = NULL; char* ptr = NULL; + event_type = strtok_r(msg, ":", &ptr); id = strtok_r(NULL, ":", &ptr); + ACCOUNT_SLOGD("msg(%s), event_type(%s), id(%s)", msg, event_type, id); ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type); + account_id = atoi(id); if(tmp->account_subscription_callback) @@ -7186,6 +2912,7 @@ static void _account_subscribe_vconf_callback_ex(keynode_t *key, void *user_data } _ACCOUNT_FREE(msg); + } ACCOUNT_API int account_subscribe_create(account_subscribe_h* account_subscribe) @@ -7209,35 +2936,6 @@ ACCOUNT_API int account_subscribe_create(account_subscribe_h* account_subscribe) ACCOUNT_API int account_subscribe_notification(account_subscribe_h account_subscribe, account_event_cb callback, void* user_data) { - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; - - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - close(fd); - } - ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__)); account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe; @@ -7262,35 +2960,79 @@ ACCOUNT_API int account_subscribe_notification(account_subscribe_h account_subsc ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_subscribe) { - int res; - int process_uid = getuid(); - char subject_label[SMACK_LABEL_LEN + 1] = {0,}; + ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__)); - if(process_uid != 0) { - int fd = open("/proc/self/attr/current", O_RDONLY); - if(fd < 0) - { - ACCOUNT_ERROR("file open fail :: /proc/self/attr/current"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe; - res = read(fd, subject_label, SMACK_LABEL_LEN); - if(res < 0) - { - ACCOUNT_ERROR("file read fail :: /proc/self/attr/current"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + _ACCOUNT_FREE(tmp); + + if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR, + (vconf_callback_fn)_account_subscribe_vconf_callback) != 0) { + ACCOUNT_ERROR("Vconf Subscription Failed !!!!!\n"); + return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL; + } + + return ACCOUNT_ERROR_NONE; +} + +static void _account_subscribe_vconf_callback_ex(keynode_t *key, void *user_data) +{ + account_subscribe_s* tmp = (account_subscribe_s*)user_data; + char *msg = NULL, *vconf_key = NULL; + char event_msg[256] ={0, }; + int account_id = -1; + char *key_name = NULL; + + if(!key) { + ACCOUNT_ERROR("No subscribtion msg !!!!!\n"); + return; + } + + if(!tmp) { + ACCOUNT_ERROR("user data required\n"); + return; + } + + key_name = vconf_keynode_get_name(key); + + if ( key_name == NULL ) { + ACCOUNT_ERROR("vconf_keynode_get_name(key) fail\n"); + return; + } + + if(!memcmp(key_name, VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR))) + { + vconf_key = vconf_keynode_get_str(key); - res = smack_have_access(subject_label, "libaccounts-svc::check_read", "r"); - if(res != 1) { - ACCOUNT_ERROR("Account permission denied :: This API can not be used without account.read privilege"); - close(fd); - return ACCOUNT_ERROR_PERMISSION_DENIED; + if( vconf_key == NULL){ + ACCOUNT_ERROR("vconf key is NULL.\n"); + return; } - close(fd); + msg = strdup(vconf_key); + + char* event_type = NULL; + char* id = NULL; + char* ptr = NULL; + + event_type = strtok_r(msg, ":", &ptr); + id = strtok_r(NULL, ":", &ptr); + + ACCOUNT_SLOGD("msg(%s), event_type(%s), id(%s)", msg, event_type, id); + + ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type); + + account_id = atoi(id); + + if(tmp->account_subscription_callback) + tmp->account_subscription_callback(event_msg, account_id, tmp->user_data); } + _ACCOUNT_FREE(msg); + +} + +ACCOUNT_API int account_unsubscribe_notification_ex(account_subscribe_h account_subscribe) +{ ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__)); account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe; @@ -7298,7 +3040,7 @@ ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_sub _ACCOUNT_FREE(tmp); if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR, - (vconf_callback_fn)_account_subscribe_vconf_callback) != 0) { + (vconf_callback_fn)_account_subscribe_vconf_callback_ex) != 0) { ACCOUNT_ERROR("Vconf Subscription Failed !!!!!\n"); return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL; } @@ -7306,40 +3048,26 @@ ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_sub return ACCOUNT_ERROR_NONE; } - -ACCOUNT_INTERNAL_API int account_subscribe_notification_ex(account_subscribe_h account_subscribe, account_event_cb callback, void* user_data) +ACCOUNT_API int account_subscribe_notification_ex(account_subscribe_h account_subscribe, account_event_cb callback, void* user_data) { - ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__)); + ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__)); account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe; + tmp->account_subscription_callback = callback; tmp->user_data = user_data; int ret = -1; ret = vconf_notify_key_changed(VCONFKEY_ACCOUNT_MSG_STR, - (vconf_callback_fn)_account_subscribe_vconf_callback_ex, - (void*)tmp); + (vconf_callback_fn)_account_subscribe_vconf_callback_ex, + (void*)tmp); + ACCOUNT_SLOGI("vconf_notify_key_changed ret = %d", ret); if(ret != VCONF_OK) { ACCOUNT_ERROR("Vconf Subscription Failed ret = %d", ret); return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL; } - return ACCOUNT_ERROR_NONE; -} - -ACCOUNT_INTERNAL_API int account_unsubscribe_notification_ex(account_subscribe_h account_subscribe) -{ - ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n", __FUNCTION__, __LINE__)); - account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe; - - _ACCOUNT_FREE(tmp); - - if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR, - (vconf_callback_fn)_account_subscribe_vconf_callback_ex) != 0) { - ACCOUNT_ERROR("Vconf Subscription Failed !!!!!\n"); - return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL; - } return ACCOUNT_ERROR_NONE; }