Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / src / agent / common / common_util.c
1 /*
2  * oma-ds-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  *   @Common_Util.c
20  *   @version                                                                   0.1
21  *   @brief                                                                             This file is the source file of implementation of utility function
22  */
23
24 #include <sync_agent.h>
25
26 #include "common/common_util.h"
27 #include "common/common_define_internal.h"
28
29 #ifndef OMADS_AGENT_LOG
30 #undef LOG_TAG
31 #define LOG_TAG "OMA_DS_COMMON"
32 #endif
33
34 #define MAX_RETRY_COUNT 10
35
36 bool create_config_str(int account_id, char *key, char *value, char *type, char *access_name, sync_agent_da_config_s ** config)
37 {
38         _EXTERN_FUNC_ENTER;
39
40         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
41
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");
45                 goto error;
46         }
47
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);
53
54  error:
55
56         _EXTERN_FUNC_EXIT;
57
58         if (da_err == SYNC_AGENT_DA_SUCCESS)
59                 return true;
60         else
61                 return false;
62
63 }
64
65 bool set_config_str(int accountID, char *key, char *value, char *type, char *access_name)
66 {
67         _EXTERN_FUNC_ENTER;
68
69         sync_agent_da_return_e result;
70         sync_agent_da_config_s config;
71
72         config.config_id = accountID;
73         config.key = key;
74         config.value = value != NULL ? strcmp(value, "") == 0 ? NULL : value : NULL;
75         config.type = type;
76         config.access_name = access_name;
77
78         result = sync_agent_update_config(&config);
79
80         _EXTERN_FUNC_EXIT;
81
82         if (result == SYNC_AGENT_DA_SUCCESS)
83                 return true;
84         else
85                 return false;
86 }
87
88 bool set_config_int(int accountID, char *key, int value, char *type, char *access_name)
89 {
90         _EXTERN_FUNC_ENTER;
91
92         char *value_str = NULL;
93         value_str = g_strdup_printf("%u", value);
94
95         sync_agent_da_return_e result;
96         sync_agent_da_config_s config;
97
98         config.config_id = accountID;
99         config.key = key;
100         config.value = value_str;
101         config.type = type;
102         config.access_name = access_name;
103
104         result = sync_agent_update_config(&config);
105
106         if (value_str != NULL)
107                 free(value_str);
108
109         _EXTERN_FUNC_EXIT;
110
111         if (result == SYNC_AGENT_DA_SUCCESS)
112                 return true;
113         else
114                 return false;
115 }
116
117 bool get_config(int account_id, char *key, char **value)
118 {
119         _EXTERN_FUNC_ENTER;
120
121         sync_agent_da_return_e result = SYNC_AGENT_DA_SUCCESS;
122         sync_agent_da_config_s *config = NULL;
123
124         *value = NULL;
125
126         result = sync_agent_create_config(&config);
127         if (result != SYNC_AGENT_DA_SUCCESS)
128                 return false;
129
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);
136                         }
137                 }
138         } else {
139                 /* FIXME  temporary solution
140                  * Try MAX_RETRY_COUNT when fail to get_config
141                  * */
142                 int i;
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);
151                                                 success = true;
152                                                 break;
153                                         }
154                                 }
155                         }
156                 }
157                 if (success == false) {
158                         sync_agent_free_config(config);
159                         _EXTERN_FUNC_EXIT;
160                         return false;
161                 }
162         }
163
164         sync_agent_free_config(config);
165         _EXTERN_FUNC_EXIT;
166         return true;
167 }
168
169 int get_account_id(char *profile, bool open)
170 {
171         _EXTERN_FUNC_ENTER;
172
173         retvm_if(profile == NULL, -1, "profile is NULL");
174
175         int accountId = -1;
176         char *profileDirName = NULL;
177         bool result;
178
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;
182         GList *iter = NULL;
183
184         if (open == false) {
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");
188                         goto error;
189                 }
190         }
191
192         sync_agent_fw_account_query_s query;
193         query.query = ACCOUNT_QUERY_BY_NONE;
194
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");
198                 goto error;
199         }
200
201         for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
202                 fw_account = (sync_agent_fw_account_s *) iter->data;
203
204                 if (profileDirName != NULL)
205                         free(profileDirName);
206
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");
210                         goto error;
211                 }
212
213                 _DEBUG_INFO("profileDirName = %s", profileDirName);
214                 if (profileDirName == NULL)
215                         continue;
216
217                 if (strcmp(profile, profileDirName) == 0) {
218                         _DEBUG_INFO("account_list[i] = %d", fw_account->account_id);
219                         accountId = fw_account->account_id;
220                         break;
221                 }
222         }
223
224         _EXTERN_FUNC_EXIT;
225
226  error:
227
228         sync_agent_free_fw_account_list(account_info_list);
229
230         if (profileDirName != NULL)
231                 free(profileDirName);
232
233         if (open == false)
234                 sync_agent_close_agent();
235
236         return accountId;
237 }
238
239 /*int convert_synctype_value(char *syncType_str)
240 {
241         int syncType_value;
242
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;
255         else
256                 syncType_value = ALERT_UNKNOWN;
257
258         return syncType_value;
259 }*/
260
261 /*char *convert_synctype_str(char *syncType_value)
262 {
263         char *syncType_str = NULL;
264
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;
277         else
278                 syncType_str = DEFINE_ALERT_SLOW_SYNC_STR;
279
280         return syncType_str;
281 }*/