Remove hardcoded path for multiuser support
[platform/core/messaging/msg-service.git] / utils / MsgGconfWrapper.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.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 if (!strcmp(keyStr, VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER)) {
62                 int contactDisplayOrder = vconf_keynode_get_int(key);
63                 MSG_DEBUG("[%s] key CB called. Apply [%d]", VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, contactDisplayOrder);
64                 MsgStoRefreshConversationDisplayName();
65         } else {
66                 MSG_DEBUG("key did not match.");
67         }
68 }
69
70 msg_error_t MsgSettingSetString(const char *pKey, const char *pSetValue)
71 {
72         if (pKey == NULL || pSetValue == NULL)
73         {
74                 MSG_DEBUG("IN Parameter is NULL");
75                 return MSG_ERR_NULL_POINTER;
76         }
77
78 #ifdef USE_GCONF
79         if (gconf_client_set_string((GConfClient*)pClient, pKey, pSetValue, NULL) !=  GCONF_SUCCESS)
80                 return MSG_ERR_SET_SETTING;
81 #else
82         if (vconf_set_str(pKey, pSetValue) != 0)
83                 return MSG_ERR_SET_SETTING;
84 #endif
85
86         return MSG_SUCCESS;
87 }
88
89
90 msg_error_t MsgSettingSetInt(const char *pKey, int nSetValue)
91 {
92         if (pKey == NULL)
93         {
94                 MSG_DEBUG("IN Parameter is NULL");
95                 return MSG_ERR_NULL_POINTER;
96         }
97
98 #ifdef USE_GCONF
99         if (gconf_client_set_int((GConfClient*)pClient, pKey, nSetValue, NULL) !=  GCONF_SUCCESS)
100                 return MSG_ERR_SET_SETTING;
101 #else
102         if (vconf_set_int(pKey, nSetValue) != 0)
103                 return MSG_ERR_SET_SETTING;
104 #endif
105
106         return MSG_SUCCESS;
107 }
108
109
110 msg_error_t MsgSettingSetBool(const char *pKey, bool bSetValue)
111 {
112         if (pKey == NULL)
113         {
114                 MSG_DEBUG("IN Parameter is NULL");
115                 return MSG_ERR_NULL_POINTER;
116         }
117
118 #ifdef USE_GCONF
119         if (gconf_client_set_bool((GConfClient*)pClient, pKey, bSetValue, NULL) !=  GCONF_SUCCESS)
120                 return MSG_ERR_SET_SETTING;
121 #else
122         if (vconf_set_bool(pKey, bSetValue) != 0)
123                 return MSG_ERR_SET_SETTING;
124 #endif
125
126         return MSG_SUCCESS;
127 }
128
129
130 char* MsgSettingGetString(const char *pKey)
131 {
132         if (pKey == NULL)
133         {
134                 MSG_DEBUG("IN Parameter is NULL");
135                 return NULL;
136         }
137
138 #ifdef USE_GCONF
139         return gconf_client_get_string((GConfClient*)pClient, pKey, NULL);
140 #else
141         return vconf_get_str(pKey);
142 #endif
143 }
144
145
146 int MsgSettingGetInt(const char *pKey)
147 {
148         if (pKey == NULL)
149         {
150                 MSG_DEBUG("IN Parameter is NULL");
151                 return -1;
152         }
153
154         int retVal = 0;
155
156 #ifdef USE_GCONF
157         retVal = gconf_client_get_int((GConfClient*)pClient, pKey, NULL);
158 #else
159         if (vconf_get_int(pKey, &retVal) < 0)
160                 return -1;
161 #endif
162
163         return retVal;
164 }
165
166
167 int MsgSettingGetBool(const char *pKey, bool *pVal)
168 {
169         if (pKey == NULL)
170         {
171                 MSG_DEBUG("IN Parameter is NULL");
172                 return -1;
173         }
174
175         int retVal = 0, param = 0;
176
177 #ifdef USE_GCONF
178         *pVal = gconf_client_get_bool((GConfClient*)pClient, pKey, NULL);
179 #else
180         if (vconf_get_bool(pKey, &param) < 0)
181                 return -1;
182 #endif
183
184         *pVal = (bool)param;
185
186         return retVal;
187 }
188
189 void MsgChangePmState()
190 {
191         MSG_BEGIN();
192         int callStatus = 0;
193
194         callStatus = MsgSettingGetInt(VCONFKEY_CALL_STATE);
195         MSG_DEBUG("Call Status = %d", callStatus);
196
197         if (callStatus > VCONFKEY_CALL_OFF && callStatus < VCONFKEY_CALL_STATE_MAX) {
198                 MSG_DEBUG("Call is activated. Do not turn on the lcd.");
199         } else {
200                 MSG_DEBUG("Call is activated. Turn on the lcd.");
201                 pm_change_state(LCD_NORMAL);
202         }
203
204         MSG_END();
205 }
206
207 msg_error_t MsgSettingHandleNewMsg(int SmsCnt, int MmsCnt)
208 {
209         MSG_BEGIN();
210
211         MSG_DEBUG("smsCnt = %d, mmsCnt = %d ##", SmsCnt, MmsCnt);
212
213         // Set Msg Count into VConf
214         if (MsgSettingSetIndicator(SmsCnt, MmsCnt) != MSG_SUCCESS)
215         {
216                 MSG_DEBUG("MsgSettingSetIndicator() FAILED");
217                 return MSG_ERR_SET_SETTING;
218         }
219
220         if (SmsCnt == 0 && MmsCnt == 0)
221         {
222                 MSG_DEBUG("No New Message.");
223         }
224         else
225         {
226                 MSG_DEBUG("New Message.");
227                 MsgChangePmState();
228         }
229
230         MSG_END();
231
232         return MSG_SUCCESS;
233 }
234
235
236 msg_error_t MsgSettingSetIndicator(int SmsCnt, int MmsCnt)
237 {
238
239         if (MsgSettingSetInt(VCONFKEY_MESSAGE_RECV_SMS_STATE, SmsCnt) != 0)
240                 return MSG_ERR_SET_SETTING;
241         if (MsgSettingSetInt(VCONFKEY_MESSAGE_RECV_MMS_STATE, MmsCnt) != 0)
242                 return MSG_ERR_SET_SETTING;
243
244         int sumCnt = SmsCnt + MmsCnt;
245
246 //      if (MsgSettingSetInt(MSG_UNREAD_CNT, sumCnt) != 0)
247         if (MsgInsertBadge(sumCnt) != MSG_SUCCESS)
248                 return MSG_ERR_SET_SETTING;
249
250         return MSG_SUCCESS;
251 }
252
253
254 bool MsgSettingGetAutoReject()
255 {
256         return bAutoReject;
257 }
258
259 bool MsgSettingGetUnknownAutoReject()
260 {
261         return bUnknownAutoReject;
262 }
263
264 void MsgSettingRegVconfCBCommon(const char *pKey, _vconf_change_cb pCb)
265 {
266         if (vconf_notify_key_changed(pKey, pCb, NULL) < 0) {
267                 MSG_DEBUG("Fail to regist vconf CB with [%s]", pKey);
268         } else {
269                 MSG_DEBUG("Success to regist vconf CB with [%s]", pKey);
270         }
271 }
272
273 void MsgSettingRemoveVconfCBCommon(const char *pKey, _vconf_change_cb pCb)
274 {
275         if (vconf_ignore_key_changed(pKey, pCb) < 0) {
276                 MSG_DEBUG("Fail to remove vconf CB with [%s]", pKey);
277         } else {
278                 MSG_DEBUG("Success to remove vconf CB with [%s]", pKey);
279         }
280 }
281
282
283 void MsgSettingRegVconfCB()
284 {
285         // Set default values.
286         MsgSettingGetBool(VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, &bAutoReject);
287         MsgSettingGetBool(VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, &bUnknownAutoReject);
288
289         MsgSettingRegVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, MsgVconfCB);
290         MsgSettingRegVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, MsgVconfCB);
291         MsgSettingRegVconfCBCommon(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, MsgVconfCB);
292 }
293
294 void MsgSettingRemoveVconfCB()
295 {
296         MsgSettingRemoveVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, MsgVconfCB);
297         MsgSettingRemoveVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, MsgVconfCB);
298         MsgSettingRemoveVconfCBCommon(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, MsgVconfCB);
299 }