3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * @brief This file is the source file of implementation of utility function
24 #include <sync_agent.h>
26 #include "common/common_util.h"
27 #include "common/common_define_internal.h"
29 #ifndef OMADS_AGENT_LOG
31 #define LOG_TAG "OMA_DS_COMMON"
34 #define MAX_RETRY_COUNT 10
36 bool create_config_str(int account_id, char *key, char *value, char *type, char *access_name, sync_agent_da_config_s ** config)
40 sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
42 da_err = sync_agent_create_config(config);
43 if (da_err != SYNC_AGENT_DA_SUCCESS) {
44 _DEBUG_ERROR("sync_agent_create_config is failed");
48 (*config)->config_id = account_id;
49 (*config)->key = strdup(key);
50 (*config)->value = value != NULL ? strcmp(value, "") == 0 ? NULL : strdup(value) : NULL;
51 (*config)->type = strdup(type);
52 (*config)->access_name = strdup(access_name);
58 if (da_err == SYNC_AGENT_DA_SUCCESS)
65 bool set_config_str(int accountID, char *key, char *value, char *type, char *access_name)
69 sync_agent_da_return_e result;
70 sync_agent_da_config_s config;
72 config.config_id = accountID;
74 config.value = value != NULL ? strcmp(value, "") == 0 ? NULL : value : NULL;
76 config.access_name = access_name;
78 result = sync_agent_update_config(&config);
82 if (result == SYNC_AGENT_DA_SUCCESS)
88 bool set_config_int(int accountID, char *key, int value, char *type, char *access_name)
92 char *value_str = NULL;
93 value_str = g_strdup_printf("%u", value);
95 sync_agent_da_return_e result;
96 sync_agent_da_config_s config;
98 config.config_id = accountID;
100 config.value = value_str;
102 config.access_name = access_name;
104 result = sync_agent_update_config(&config);
106 if (value_str != NULL)
111 if (result == SYNC_AGENT_DA_SUCCESS)
117 bool get_config(int account_id, char *key, char **value)
121 sync_agent_da_return_e result = SYNC_AGENT_DA_SUCCESS;
122 sync_agent_da_config_s *config = NULL;
126 result = sync_agent_create_config(&config);
127 if (result != SYNC_AGENT_DA_SUCCESS)
130 result = sync_agent_get_config(account_id, key, &config);
131 if (result == SYNC_AGENT_DA_SUCCESS) {
132 if (config != NULL) {
133 if (config->value != NULL) {
134 *value = strdup(config->value);
135 _DEBUG_INFO("value = %s", *value);
139 /* FIXME temporary solution
140 * Try MAX_RETRY_COUNT when fail to get_config
143 bool success = false;
144 for (i = 0; i < MAX_RETRY_COUNT; i++) {
145 result = sync_agent_get_config(account_id, key, &config);
146 if (result == SYNC_AGENT_DA_SUCCESS) {
147 if (config != NULL) {
148 if (config->value != NULL) {
149 *value = strdup(config->value);
150 _DEBUG_INFO("value = %s", *value);
157 if (success == false) {
158 sync_agent_free_config(config);
164 sync_agent_free_config(config);
169 int get_account_id(char *profile, bool open)
173 retvm_if(profile == NULL, -1, "profile is NULL");
176 char *profileDirName = NULL;
179 sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
180 sync_agent_fw_account_s *fw_account = NULL;
181 GList *account_info_list = NULL;
185 sync_agent_da_return_e da_err = sync_agent_open_agent();
186 if (da_err != SYNC_AGENT_DA_SUCCESS) {
187 _DEBUG_ERROR("failed in sync_agent_open_agent");
192 sync_agent_fw_account_query_s query;
193 query.query = ACCOUNT_QUERY_BY_NONE;
195 acc_err = sync_agent_query_fw_account(&query, &account_info_list);
196 if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
197 _DEBUG_ERROR("sync_agent_query_fw_account is failed");
201 for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
202 fw_account = (sync_agent_fw_account_s *) iter->data;
204 if (profileDirName != NULL)
205 free(profileDirName);
207 result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
208 if (result == false) {
209 _DEBUG_ERROR("failed in get_Config");
213 _DEBUG_INFO("profileDirName = %s", profileDirName);
214 if (profileDirName == NULL)
217 if (strcmp(profile, profileDirName) == 0) {
218 _DEBUG_INFO("account_list[i] = %d", fw_account->account_id);
219 accountId = fw_account->account_id;
228 sync_agent_free_fw_account_list(account_info_list);
230 if (profileDirName != NULL)
231 free(profileDirName);
234 sync_agent_close_agent();
239 /*int convert_synctype_value(char *syncType_str)
243 if (strcmp(syncType_str, DEFINE_ALERT_SLOW_SYNC_STR) ==0)
244 syncType_value = ALERT_SLOW_SYNC ;
245 else if (strcmp(syncType_str, DEFINE_ALERT_TWO_WAY_STR) ==0)
246 syncType_value = ALERT_TWO_WAY ;
247 else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR) ==0)
248 syncType_value = ALERT_ONE_WAY_FROM_CLIENT;
249 else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR) ==0)
250 syncType_value = ALERT_ONE_WAY_FROM_SERVER;
251 else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_SERVER_STR) ==0)
252 syncType_value = ALERT_REFRESH_FROM_SERVER;
253 else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_CLIENT_STR) ==0)
254 syncType_value = ALERT_REFRESH_FROM_CLIENT;
256 syncType_value = ALERT_UNKNOWN;
258 return syncType_value;
261 /*char *convert_synctype_str(char *syncType_value)
263 char *syncType_str = NULL;
265 if (strcmp(syncType_value, DEFINE_ALERT_SLOW_SYNC_VALUE) ==0)
266 syncType_str = DEFINE_ALERT_SLOW_SYNC_STR;
267 else if (strcmp(syncType_value, DEFINE_ALERT_TWO_WAY_VALUE) ==0)
268 syncType_str = DEFINE_ALERT_TWO_WAY_STR;
269 else if (strcmp(syncType_value, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_VALUE) ==0)
270 syncType_str = DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR;
271 else if (strcmp(syncType_value, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_VALUE) ==0)
272 syncType_str = DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR;
273 else if (strcmp(syncType_value, DEFINE_ALERT_REFRESH_FROM_SERVER_VALUE) ==0)
274 syncType_str = DEFINE_ALERT_REFRESH_FROM_SERVER_STR;
275 else if (strcmp(syncType_value, DEFINE_ALERT_REFRESH_FROM_CLIENT_VALUE) ==0)
276 syncType_str = DEFINE_ALERT_REFRESH_FROM_CLIENT_STR;
278 syncType_str = DEFINE_ALERT_SLOW_SYNC_STR;