/* * oma-ds-agent * Copyright (c) 2012 Samsung Electronics Co., Ltd. * * 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @Common_Util.c * @version 0.1 * @brief This file is the source file of implementation of utility function */ #include #include "common/common_util.h" #include "common/common_define_internal.h" #ifndef OMADS_AGENT_LOG #undef LOG_TAG #define LOG_TAG "OMA_DS_COMMON" #endif #define MAX_RETRY_COUNT 10 bool create_config_str(int account_id, char *key, char *value, char *type, char *access_name, sync_agent_da_config_s ** config) { _EXTERN_FUNC_ENTER; sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS; da_err = sync_agent_create_config(config); if (da_err != SYNC_AGENT_DA_SUCCESS) { _DEBUG_ERROR("sync_agent_create_config is failed"); goto error; } (*config)->config_id = account_id; (*config)->key = strdup(key); (*config)->value = value != NULL ? strcmp(value, "") == 0 ? NULL : strdup(value) : NULL; (*config)->type = strdup(type); (*config)->access_name = strdup(access_name); error: _EXTERN_FUNC_EXIT; if (da_err == SYNC_AGENT_DA_SUCCESS) return true; else return false; } bool set_config_str(int accountID, char *key, char *value, char *type, char *access_name) { _EXTERN_FUNC_ENTER; sync_agent_da_return_e result; sync_agent_da_config_s config; config.config_id = accountID; config.key = key; config.value = value != NULL ? strcmp(value, "") == 0 ? NULL : value : NULL; config.type = type; config.access_name = access_name; result = sync_agent_update_config(&config); _EXTERN_FUNC_EXIT; if (result == SYNC_AGENT_DA_SUCCESS) return true; else return false; } bool set_config_int(int accountID, char *key, int value, char *type, char *access_name) { _EXTERN_FUNC_ENTER; char *value_str = NULL; value_str = g_strdup_printf("%u", value); sync_agent_da_return_e result; sync_agent_da_config_s config; config.config_id = accountID; config.key = key; config.value = value_str; config.type = type; config.access_name = access_name; result = sync_agent_update_config(&config); if (value_str != NULL) free(value_str); _EXTERN_FUNC_EXIT; if (result == SYNC_AGENT_DA_SUCCESS) return true; else return false; } bool get_config(int account_id, char *key, char **value) { _EXTERN_FUNC_ENTER; sync_agent_da_return_e result = SYNC_AGENT_DA_SUCCESS; sync_agent_da_config_s *config = NULL; *value = NULL; _DEBUG_INFO("get_config - account_id[%d], key[%s]",account_id,key); result = sync_agent_create_config(&config); if (result != SYNC_AGENT_DA_SUCCESS) { _DEBUG_INFO("sync_agent_create_config() failed!!"); return false; } result = sync_agent_get_config(account_id, key, &config); if (result == SYNC_AGENT_DA_SUCCESS) { if (config != NULL) { if (config->value != NULL) { *value = strdup(config->value); _DEBUG_INFO("value = %s", *value); } } } else { /* FIXME temporary solution * Try MAX_RETRY_COUNT when fail to get_config * */ int i; bool success = false; for (i = 0; i < MAX_RETRY_COUNT; i++) { result = sync_agent_get_config(account_id, key, &config); if (result == SYNC_AGENT_DA_SUCCESS) { if (config != NULL) { if (config->value != NULL) { *value = strdup(config->value); _DEBUG_INFO("value = %s", *value); success = true; break; } } } else { _DEBUG_INFO("sync_agent_get_config() failed!!"); } } if (success == false) { sync_agent_free_config(config); _EXTERN_FUNC_EXIT; return false; } } sync_agent_free_config(config); _EXTERN_FUNC_EXIT; return true; } int get_account_id(char *profile, bool open) { _EXTERN_FUNC_ENTER; retvm_if(profile == NULL, -1, "profile is NULL"); int accountId = -1; char *profileDirName = NULL; bool result; sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS; sync_agent_fw_account_s *fw_account = NULL; GList *account_info_list = NULL; GList *iter = NULL; if (open == false) { sync_agent_da_return_e da_err = sync_agent_open_agent(); if (da_err != SYNC_AGENT_DA_SUCCESS) { _DEBUG_ERROR("failed in sync_agent_open_agent"); goto error; } } sync_agent_fw_account_query_s query; query.query = ACCOUNT_QUERY_BY_NONE; acc_err = sync_agent_query_fw_account(&query, &account_info_list); if (acc_err != SYNC_AGENT_ACC_SUCCESS) { _DEBUG_ERROR("sync_agent_query_fw_account is failed"); goto error; } for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) { fw_account = (sync_agent_fw_account_s *) iter->data; if (profileDirName != NULL) free(profileDirName); result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName); if (result == false) { _DEBUG_ERROR("failed in get_Config"); goto error; } _DEBUG_INFO("profileDirName = %s", profileDirName); if (profileDirName == NULL) continue; if (strcmp(profile, profileDirName) == 0) { _DEBUG_INFO("account_list[i] = %d", fw_account->account_id); accountId = fw_account->account_id; break; } } _EXTERN_FUNC_EXIT; error: sync_agent_free_fw_account_list(account_info_list); if (profileDirName != NULL) free(profileDirName); if (open == false) sync_agent_close_agent(); return accountId; } /*int convert_synctype_value(char *syncType_str) { int syncType_value; if (strcmp(syncType_str, DEFINE_ALERT_SLOW_SYNC_STR) ==0) syncType_value = ALERT_SLOW_SYNC ; else if (strcmp(syncType_str, DEFINE_ALERT_TWO_WAY_STR) ==0) syncType_value = ALERT_TWO_WAY ; else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR) ==0) syncType_value = ALERT_ONE_WAY_FROM_CLIENT; else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR) ==0) syncType_value = ALERT_ONE_WAY_FROM_SERVER; else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_SERVER_STR) ==0) syncType_value = ALERT_REFRESH_FROM_SERVER; else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_CLIENT_STR) ==0) syncType_value = ALERT_REFRESH_FROM_CLIENT; else syncType_value = ALERT_UNKNOWN; return syncType_value; }*/ /*char *convert_synctype_str(char *syncType_value) { char *syncType_str = NULL; if (strcmp(syncType_value, DEFINE_ALERT_SLOW_SYNC_VALUE) ==0) syncType_str = DEFINE_ALERT_SLOW_SYNC_STR; else if (strcmp(syncType_value, DEFINE_ALERT_TWO_WAY_VALUE) ==0) syncType_str = DEFINE_ALERT_TWO_WAY_STR; else if (strcmp(syncType_value, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_VALUE) ==0) syncType_str = DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR; else if (strcmp(syncType_value, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_VALUE) ==0) syncType_str = DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR; else if (strcmp(syncType_value, DEFINE_ALERT_REFRESH_FROM_SERVER_VALUE) ==0) syncType_str = DEFINE_ALERT_REFRESH_FROM_SERVER_STR; else if (strcmp(syncType_value, DEFINE_ALERT_REFRESH_FROM_CLIENT_VALUE) ==0) syncType_str = DEFINE_ALERT_REFRESH_FROM_CLIENT_STR; else syncType_str = DEFINE_ALERT_SLOW_SYNC_STR; return syncType_str; }*/