Merge from 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 "MsgGconfWrapper.h"
24
25 #ifdef USE_GCONF
26
27 #define GCONF_SUCCESS 1
28
29 MSG_GOBJECT_CLIENT_S* pClient = NULL;
30
31 #endif
32
33 bool bAutoReject = false;
34 bool bUnknownAutoReject = false;
35
36
37 /*==================================================================================================
38                                      DEFINES
39 ==================================================================================================*/
40
41
42 /*==================================================================================================
43                                      FUNCTION IMPLEMENTATION
44 ==================================================================================================*/
45 static void MsgVconfCB(keynode_t *key, void* data)
46 {
47         char *keyStr = NULL;
48         keyStr = vconf_keynode_get_name(key);
49
50         if (!keyStr)
51                 return;
52
53         if (!strcmp(keyStr, VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL)) {
54                 bAutoReject = vconf_keynode_get_bool(key);
55                 MSG_DEBUG("[%s] key CB called. set to [%d].", VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, bAutoReject);
56         } else if (!strcmp(keyStr, VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL)) {
57                 bUnknownAutoReject = vconf_keynode_get_bool(key);
58                 MSG_DEBUG("[%s] key CB called. set to [%d].", VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, bUnknownAutoReject);
59         } else {
60                 MSG_DEBUG("key did not match.");
61         }
62 }
63
64 msg_error_t MsgSettingSetString(const char *pKey, const char *pSetValue)
65 {
66         if (pKey == NULL || pSetValue == NULL)
67         {
68                 MSG_DEBUG("IN Parameter is NULL");
69                 return MSG_ERR_NULL_POINTER;
70         }
71
72 #ifdef USE_GCONF
73         if (gconf_client_set_string((GConfClient*)pClient, pKey, pSetValue, NULL) !=  GCONF_SUCCESS)
74                 return MSG_ERR_SET_SETTING;
75 #else
76         if (vconf_set_str(pKey, pSetValue) != 0)
77                 return MSG_ERR_SET_SETTING;
78 #endif
79
80         return MSG_SUCCESS;
81 }
82
83
84 msg_error_t MsgSettingSetInt(const char *pKey, int nSetValue)
85 {
86         if (pKey == NULL)
87         {
88                 MSG_DEBUG("IN Parameter is NULL");
89                 return MSG_ERR_NULL_POINTER;
90         }
91
92 #ifdef USE_GCONF
93         if (gconf_client_set_int((GConfClient*)pClient, pKey, nSetValue, NULL) !=  GCONF_SUCCESS)
94                 return MSG_ERR_SET_SETTING;
95 #else
96         if (vconf_set_int(pKey, nSetValue) != 0)
97                 return MSG_ERR_SET_SETTING;
98 #endif
99
100         return MSG_SUCCESS;
101 }
102
103
104 msg_error_t MsgSettingSetBool(const char *pKey, bool bSetValue)
105 {
106         if (pKey == NULL)
107         {
108                 MSG_DEBUG("IN Parameter is NULL");
109                 return MSG_ERR_NULL_POINTER;
110         }
111
112 #ifdef USE_GCONF
113         if (gconf_client_set_bool((GConfClient*)pClient, pKey, bSetValue, NULL) !=  GCONF_SUCCESS)
114                 return MSG_ERR_SET_SETTING;
115 #else
116         if (vconf_set_bool(pKey, bSetValue) != 0)
117                 return MSG_ERR_SET_SETTING;
118 #endif
119
120         return MSG_SUCCESS;
121 }
122
123
124 char* MsgSettingGetString(const char *pKey)
125 {
126         if (pKey == NULL)
127         {
128                 MSG_DEBUG("IN Parameter is NULL");
129                 return NULL;
130         }
131
132 #ifdef USE_GCONF
133         return gconf_client_get_string((GConfClient*)pClient, pKey, NULL);
134 #else
135         return vconf_get_str(pKey);
136 #endif
137 }
138
139
140 int MsgSettingGetInt(const char *pKey)
141 {
142         if (pKey == NULL)
143         {
144                 MSG_DEBUG("IN Parameter is NULL");
145                 return -1;
146         }
147
148         int retVal = 0;
149
150 #ifdef USE_GCONF
151         retVal = gconf_client_get_int((GConfClient*)pClient, pKey, NULL);
152 #else
153         if (vconf_get_int(pKey, &retVal) < 0)
154                 return -1;
155 #endif
156
157         return retVal;
158 }
159
160
161 int MsgSettingGetBool(const char *pKey, bool *pVal)
162 {
163         if (pKey == NULL)
164         {
165                 MSG_DEBUG("IN Parameter is NULL");
166                 return -1;
167         }
168
169         int retVal = 0, param = 0;
170
171 #ifdef USE_GCONF
172         *pVal = gconf_client_get_bool((GConfClient*)pClient, pKey, NULL);
173 #else
174         if (vconf_get_bool(pKey, &param) < 0)
175                 return -1;
176 #endif
177
178         *pVal = (bool)param;
179
180         return retVal;
181 }
182
183
184 msg_error_t MsgSettingHandleNewMsg(int SmsCnt, int MmsCnt)
185 {
186         MSG_BEGIN();
187
188         MSG_DEBUG("smsCnt = %d, mmsCnt = %d ##", SmsCnt, MmsCnt);
189
190         // Set Msg Count into VConf
191         if (MsgSettingSetIndicator(SmsCnt, MmsCnt) != MSG_SUCCESS)
192         {
193                 MSG_DEBUG("MsgSettingSetIndicator() FAILED");
194                 return MSG_ERR_SET_SETTING;
195         }
196
197         if (SmsCnt == 0 && MmsCnt == 0)
198         {
199                 MSG_DEBUG("No New Message.");
200         }
201         else
202         {
203                 MSG_DEBUG("New Message.");
204                 pm_change_state(LCD_NORMAL);
205         }
206
207         MSG_END();
208
209         return MSG_SUCCESS;
210 }
211
212
213 msg_error_t MsgSettingSetIndicator(int SmsCnt, int MmsCnt)
214 {
215
216         if (MsgSettingSetInt(VCONFKEY_MESSAGE_RECV_SMS_STATE, SmsCnt) != 0)
217                 return MSG_ERR_SET_SETTING;
218         if (MsgSettingSetInt(VCONFKEY_MESSAGE_RECV_MMS_STATE, MmsCnt) != 0)
219                 return MSG_ERR_SET_SETTING;
220
221         /* Not used currently.
222         int sumCnt = SmsCnt + MmsCnt;
223
224         if (MsgSettingSetInt(MSG_UNREAD_CNT, sumCnt) != 0)
225                 return MSG_ERR_SET_SETTING;
226         */
227
228         return MSG_SUCCESS;
229 }
230
231
232 bool MsgSettingGetAutoReject()
233 {
234         return bAutoReject;
235 }
236
237 bool MsgSettingGetUnknownAutoReject()
238 {
239         return bUnknownAutoReject;
240 }
241
242 void MsgSettingRegVconfCBCommon(const char *pKey, _vconf_change_cb pCb)
243 {
244         if (vconf_notify_key_changed(pKey, pCb, NULL) < 0) {
245                 MSG_DEBUG("Fail to regist vconf CB with [%s]", pKey);
246         } else {
247                 MSG_DEBUG("Success to regist vconf CB with [%s]", pKey);
248         }
249 }
250
251 void MsgSettingRemoveVconfCBCommon(const char *pKey, _vconf_change_cb pCb)
252 {
253         if (vconf_ignore_key_changed(pKey, pCb) < 0) {
254                 MSG_DEBUG("Fail to remove vconf CB with [%s]", pKey);
255         } else {
256                 MSG_DEBUG("Success to remove vconf CB with [%s]", pKey);
257         }
258 }
259
260
261 void MsgSettingRegVconfCB()
262 {
263         // Set default values.
264         MsgSettingGetBool(VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, &bAutoReject);
265         MsgSettingGetBool(VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, &bUnknownAutoReject);
266
267         MsgSettingRegVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, MsgVconfCB);
268         MsgSettingRegVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, MsgVconfCB);
269 }
270
271 void MsgSettingRemoveVconfCB()
272 {
273         MsgSettingRemoveVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_BOOL, MsgVconfCB);
274         MsgSettingRemoveVconfCBCommon(VCONFKEY_CISSAPPL_AUTO_REJECT_UNKNOWN_BOOL, MsgVconfCB);
275
276 }