RSA sync with private
[platform/core/messaging/msg-service.git] / utils / MsgGconfWrapper.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://www.tizenopensource.org/license
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18
19 #include <pmapi.h>
20
21 #include "MsgDebug.h"
22 #include "MsgUtilStorage.h"
23 #include "MsgNotificationWrapper.h"
24 #include "MsgGconfWrapper.h"
25
26 #ifdef USE_GCONF
27
28 #define GCONF_SUCCESS 1
29
30 MSG_GOBJECT_CLIENT_S* pClient = NULL;
31
32 #endif
33
34 bool bAutoReject = false;
35 bool bUnknownAutoReject = false;
36
37
38 /*==================================================================================================
39                                      DEFINES
40 ==================================================================================================*/
41 #define MSG_UNREAD_CNT          "db/badge/org.tizen.message"
42
43
44 /*==================================================================================================
45                                      FUNCTION IMPLEMENTATION
46 ==================================================================================================*/
47 static void MsgVconfCB(keynode_t *key, void* data)
48 {
49         char *keyStr = NULL;
50         keyStr = vconf_keynode_get_name(key);
51
52         if (!keyStr)
53                 return;
54
55         if (!strcmp(keyStr, VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL)) {
56                 bAutoReject = vconf_keynode_get_bool(key);
57                 MSG_DEBUG("[%s] key CB called. set to [%d].", VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, bAutoReject);
58         } else if (!strcmp(keyStr, VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL)) {
59                 bUnknownAutoReject = vconf_keynode_get_bool(key);
60                 MSG_DEBUG("[%s] key CB called. set to [%d].", VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, bUnknownAutoReject);
61         } else {
62                 MSG_DEBUG("key did not match.");
63         }
64 }
65
66 msg_error_t MsgSettingSetString(const char *pKey, const char *pSetValue)
67 {
68         if (pKey == NULL || pSetValue == NULL)
69         {
70                 MSG_DEBUG("IN Parameter is NULL");
71                 return MSG_ERR_NULL_POINTER;
72         }
73
74 #ifdef USE_GCONF
75         if (gconf_client_set_string((GConfClient*)pClient, pKey, pSetValue, NULL) !=  GCONF_SUCCESS)
76                 return MSG_ERR_SET_SETTING;
77 #else
78         if (vconf_set_str(pKey, pSetValue) != 0)
79                 return MSG_ERR_SET_SETTING;
80 #endif
81
82         return MSG_SUCCESS;
83 }
84
85
86 msg_error_t MsgSettingSetInt(const char *pKey, int nSetValue)
87 {
88         if (pKey == NULL)
89         {
90                 MSG_DEBUG("IN Parameter is NULL");
91                 return MSG_ERR_NULL_POINTER;
92         }
93
94 #ifdef USE_GCONF
95         if (gconf_client_set_int((GConfClient*)pClient, pKey, nSetValue, NULL) !=  GCONF_SUCCESS)
96                 return MSG_ERR_SET_SETTING;
97 #else
98         if (vconf_set_int(pKey, nSetValue) != 0)
99                 return MSG_ERR_SET_SETTING;
100 #endif
101
102         return MSG_SUCCESS;
103 }
104
105
106 msg_error_t MsgSettingSetBool(const char *pKey, bool bSetValue)
107 {
108         if (pKey == NULL)
109         {
110                 MSG_DEBUG("IN Parameter is NULL");
111                 return MSG_ERR_NULL_POINTER;
112         }
113
114 #ifdef USE_GCONF
115         if (gconf_client_set_bool((GConfClient*)pClient, pKey, bSetValue, NULL) !=  GCONF_SUCCESS)
116                 return MSG_ERR_SET_SETTING;
117 #else
118         if (vconf_set_bool(pKey, bSetValue) != 0)
119                 return MSG_ERR_SET_SETTING;
120 #endif
121
122         return MSG_SUCCESS;
123 }
124
125
126 char* MsgSettingGetString(const char *pKey)
127 {
128         if (pKey == NULL)
129         {
130                 MSG_DEBUG("IN Parameter is NULL");
131                 return NULL;
132         }
133
134 #ifdef USE_GCONF
135         return gconf_client_get_string((GConfClient*)pClient, pKey, NULL);
136 #else
137         return vconf_get_str(pKey);
138 #endif
139 }
140
141
142 int MsgSettingGetInt(const char *pKey)
143 {
144         if (pKey == NULL)
145         {
146                 MSG_DEBUG("IN Parameter is NULL");
147                 return -1;
148         }
149
150         int retVal = 0;
151
152 #ifdef USE_GCONF
153         retVal = gconf_client_get_int((GConfClient*)pClient, pKey, NULL);
154 #else
155         if (vconf_get_int(pKey, &retVal) < 0)
156                 return -1;
157 #endif
158
159         return retVal;
160 }
161
162
163 int MsgSettingGetBool(const char *pKey, bool *pVal)
164 {
165         if (pKey == NULL)
166         {
167                 MSG_DEBUG("IN Parameter is NULL");
168                 return -1;
169         }
170
171         int retVal = 0, param = 0;
172
173 #ifdef USE_GCONF
174         *pVal = gconf_client_get_bool((GConfClient*)pClient, pKey, NULL);
175 #else
176         if (vconf_get_bool(pKey, &param) < 0)
177                 return -1;
178 #endif
179
180         *pVal = (bool)param;
181
182         return retVal;
183 }
184
185 void MsgChangePmState()
186 {
187         MSG_BEGIN();
188         int callStatus = 0;
189
190         callStatus = MsgSettingGetInt(VCONFKEY_CALL_STATE);
191         MSG_DEBUG("Call Status = %d", callStatus);
192
193         if (callStatus > VCONFKEY_CALL_OFF && callStatus < VCONFKEY_CALL_STATE_MAX) {
194                 MSG_DEBUG("Call is activated. Do not turn on the lcd.");
195         } else {
196                 MSG_DEBUG("Call is activated. Turn on the lcd.");
197                 pm_change_state(LCD_NORMAL);
198         }
199
200         MSG_END();
201 }
202
203 msg_error_t MsgSettingHandleNewMsg(int SmsCnt, int MmsCnt)
204 {
205         MSG_BEGIN();
206
207         MSG_DEBUG("smsCnt = %d, mmsCnt = %d ##", SmsCnt, MmsCnt);
208
209         // Set Msg Count into VConf
210         if (MsgSettingSetIndicator(SmsCnt, MmsCnt) != MSG_SUCCESS)
211         {
212                 MSG_DEBUG("MsgSettingSetIndicator() FAILED");
213                 return MSG_ERR_SET_SETTING;
214         }
215
216         if (SmsCnt == 0 && MmsCnt == 0)
217         {
218                 MSG_DEBUG("No New Message.");
219         }
220         else
221         {
222                 MSG_DEBUG("New Message.");
223                 MsgChangePmState();
224         }
225
226         MSG_END();
227
228         return MSG_SUCCESS;
229 }
230
231
232 msg_error_t MsgSettingSetIndicator(int SmsCnt, int MmsCnt)
233 {
234
235         if (MsgSettingSetInt(VCONFKEY_MESSAGE_RECV_SMS_STATE, SmsCnt) != 0)
236                 return MSG_ERR_SET_SETTING;
237         if (MsgSettingSetInt(VCONFKEY_MESSAGE_RECV_MMS_STATE, MmsCnt) != 0)
238                 return MSG_ERR_SET_SETTING;
239
240         int sumCnt = SmsCnt + MmsCnt;
241
242 //      if (MsgSettingSetInt(MSG_UNREAD_CNT, sumCnt) != 0)
243         if (MsgInsertBadge(sumCnt) != MSG_SUCCESS)
244                 return MSG_ERR_SET_SETTING;
245
246         return MSG_SUCCESS;
247 }
248
249
250 bool MsgSettingGetAutoReject()
251 {
252         return bAutoReject;
253 }
254
255 bool MsgSettingGetUnknownAutoReject()
256 {
257         return bUnknownAutoReject;
258 }
259
260 void MsgSettingRegVconfCBCommon(const char *pKey, _vconf_change_cb pCb)
261 {
262         if (vconf_notify_key_changed(pKey, pCb, NULL) < 0) {
263                 MSG_DEBUG("Fail to regist vconf CB with [%s]", pKey);
264         } else {
265                 MSG_DEBUG("Success to regist vconf CB with [%s]", pKey);
266         }
267 }
268
269 void MsgSettingRemoveVconfCBCommon(const char *pKey, _vconf_change_cb pCb)
270 {
271         if (vconf_ignore_key_changed(pKey, pCb) < 0) {
272                 MSG_DEBUG("Fail to remove vconf CB with [%s]", pKey);
273         } else {
274                 MSG_DEBUG("Success to remove vconf CB with [%s]", pKey);
275         }
276 }
277
278
279 void MsgSettingRegVconfCB()
280 {
281         // Set default values.
282         MsgSettingGetBool(VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, &bAutoReject);
283         MsgSettingGetBool(VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, &bUnknownAutoReject);
284
285         MsgSettingRegVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, MsgVconfCB);
286         MsgSettingRegVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, MsgVconfCB);
287 }
288
289 void MsgSettingRemoveVconfCB()
290 {
291         MsgSettingRemoveVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, MsgVconfCB);
292         MsgSettingRemoveVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, MsgVconfCB);
293
294 }