upload tizen1.0 source
[pkgs/o/oma-ds-service.git] / src / Common / Common_Util.c
1 /*
2  * oma-ds-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JuHak Park <juhaki.park@samsung.com>,
7  *          JuneHyuk Lee <junhyuk7.lee@samsung.com>,
8  *          SunBong Ha <sunbong.ha@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24
25
26
27 /*
28  * For any sort of issue you concern as to this software,
29  * you may use following point of contact.
30  * All resources contributed on this software
31  * are orinigally written by S-Core Inc., a member of Samsung Group.
32  *
33  * SeongWon Shim <seongwon.shim@samsung.com>
34  */
35
36 /**
37  *   @Common_Util.c
38  *   @version                                                                   0.1
39  *   @brief                                                                             This file is the source file of implementation of utility function
40  */
41
42 #include "agent-framework/DACI/DACI_Agent_Handler_Manager.h"
43 #include "agent-framework/Utility/fw_log.h"
44 #include "Common/Common_Util.h"
45 #include "Common/Common_Define_Internal.h"
46
47 #define LOG_TAG "OMA_DS_COMMON"
48 #define MAX_RETRY_COUNT 10
49
50 bool set_config_str(int accountID, char *key, char *value, char * type, char *accessName)
51 {
52         DACI_RETURN result;
53         daci_config config;
54         config.config_id = accountID;
55         config.key = key;
56         config.value = value;
57         config.type = type;
58         config.access_name = accessName;
59
60         result = DACI_Update_Config(&config, 1);
61
62         if (result == DACI_SUCCESS)
63                 return true;
64         else
65                 return false;
66 }
67
68
69 bool set_config_int(int accountID, char *key, int value, char * type, char *accessName)
70 {
71         char *value_str = NULL;
72         value_str = g_strdup_printf("%u", value);
73
74         DACI_RETURN result;
75         daci_config config;
76         config.config_id = accountID;
77         config.key = key;
78         config.value = value_str;
79         config.type = type;
80         config.access_name = accessName;
81
82         result = DACI_Update_Config(&config, 1);
83
84         if (value_str != NULL)
85                 free(value_str);
86
87         if (result == DACI_SUCCESS)
88                 return true;
89         else
90                 return false;
91 }
92
93
94
95 bool get_config(int accountId, char *key, char **value)
96 {
97         DACI_RETURN result;
98         daci_config daci;
99         daci.config_id = accountId;
100
101         daci.key = key;
102         result = DACI_Get_Config_By_Key(&daci);
103
104         if (result == DACI_SUCCESS) {
105                 *value = daci.value;
106                 if (daci.type != NULL)
107                         free(daci.type);
108                 return true;
109         } else {
110                 /* FIXME  temporary solution
111                  * Try MAX_RETRY_COUNT when fail to get_config
112                  * */
113                 int i;
114                 bool success = false;
115                 for (i = 0; i < MAX_RETRY_COUNT; i++){
116                         result =DACI_Get_Config_By_Key(&daci);
117                         if(result == DACI_SUCCESS){
118                                 *value = daci.value;
119                                 if (daci.type != NULL)
120                                         free(daci.type);
121                                 success = true;
122                                 break;
123                         }
124                 }
125                 if(success == false)
126                         return false;
127                 else
128                         return true;
129         }
130 }
131
132 int get_accountid(char *profile, bool open)
133 {
134         FW_LOGV("start");
135         int accountId = -1;
136         char *profileDirName = NULL;
137         int *account_list = 0;
138         int account_list_count = 0;
139         bool result;
140
141         if (open == false) {
142                 DACI_RETURN da_err = DACI_Open_Agent();
143                 if (da_err != DACI_SUCCESS) {
144                         FW_LOGE("failed in DACI_Open_Agent");
145                         goto error;
146                 }
147         }
148
149         account_list = DACI_Get_Account_Account_Id_List(&account_list_count);
150
151         int i;
152         for (i = 0; i < account_list_count; i++) {
153                 if (profileDirName != NULL)
154                         free(profileDirName);
155
156                 result = get_config(account_list[i], DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
157                 if (result == false) {
158                         FW_LOGE("failed in get_Config");
159                         goto error;
160                 }
161
162                 FW_LOGV("profileDirName = %s", profileDirName);
163                 if(profileDirName == NULL )
164                         continue;
165
166                 if (strcmp(profile, profileDirName) == 0) {
167                         FW_LOGV("account_list[i] = %d", account_list[i]);
168                         accountId = account_list[i];
169                         break;
170                 }
171         }
172
173         FW_LOGV("end");
174
175 error:
176
177         if (account_list_count > 0)
178                 free(account_list);
179
180         if (profileDirName)
181                 free(profileDirName);
182
183         if (open == false)
184                 DACI_Close_Agent();
185
186         return accountId;
187 }
188
189 /*int convert_synctype_value(char *syncType_str)
190 {
191         int syncType_value;
192
193         if (strcmp(syncType_str, DEFINE_ALERT_SLOW_SYNC_STR) ==0)
194                 syncType_value = ALERT_SLOW_SYNC ;
195         else if (strcmp(syncType_str, DEFINE_ALERT_TWO_WAY_STR) ==0)
196                 syncType_value = ALERT_TWO_WAY ;
197         else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR) ==0)
198                 syncType_value =  ALERT_ONE_WAY_FROM_CLIENT;
199         else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR) ==0)
200                 syncType_value = ALERT_ONE_WAY_FROM_SERVER;
201         else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_SERVER_STR) ==0)
202                 syncType_value = ALERT_REFRESH_FROM_SERVER;
203         else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_CLIENT_STR) ==0)
204                 syncType_value = ALERT_REFRESH_FROM_CLIENT;
205         else
206                 syncType_value = ALERT_UNKNOWN;
207
208         return syncType_value;
209 }*/
210
211 /*char *convert_synctype_str(char *syncType_value)
212 {
213         char *syncType_str = NULL;
214
215         if (strcmp(syncType_value, DEFINE_ALERT_SLOW_SYNC_VALUE) ==0)
216                 syncType_str = DEFINE_ALERT_SLOW_SYNC_STR;
217         else if (strcmp(syncType_value, DEFINE_ALERT_TWO_WAY_VALUE) ==0)
218                 syncType_str = DEFINE_ALERT_TWO_WAY_STR;
219         else if (strcmp(syncType_value, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_VALUE) ==0)
220                 syncType_str = DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR;
221         else if (strcmp(syncType_value, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_VALUE) ==0)
222                 syncType_str = DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR;
223         else if (strcmp(syncType_value, DEFINE_ALERT_REFRESH_FROM_SERVER_VALUE) ==0)
224                 syncType_str = DEFINE_ALERT_REFRESH_FROM_SERVER_STR;
225         else if (strcmp(syncType_value, DEFINE_ALERT_REFRESH_FROM_CLIENT_VALUE) ==0)
226                 syncType_str = DEFINE_ALERT_REFRESH_FROM_CLIENT_STR;
227         else
228                 syncType_str = DEFINE_ALERT_SLOW_SYNC_STR;
229
230         return syncType_str;
231 }*/