a8add2fd281d13515b3e52e8aa335a532fe8060e
[apps/home/clock.git] / alarm / src / alarm_fwk_vconf.c
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 "alarm_fwk_vconf.h"
18 #include "alarm_view_edit.h"
19 #include "alarm_subview_type.h"
20 #include <heynoti.h>
21 /**********************************************************************
22 ******************define, struct ,typedef, union, enum, global val *************************************
23 ***********************************************************************/
24
25 //
26 typedef struct _noti_callback {
27         char *setting_key;
28         void (*noti_func) (keynode_t *, void *);
29 } noti_callback;
30
31 static int g_fd = 0;
32 /**********************************************************************
33 ******************Local function declear, extern function declear*************************************
34 ***********************************************************************/
35 static int alarm_vconfkey_alarm_off_cb(void *data);
36 static int alarm_vconfkey_alarm_snooze_on_cb(void *data);
37 static void alarm_vconfkey_update_cb(keynode_t *, void *);
38 static void alarm_vconfkey_delete_cb(keynode_t * key, void *data);
39 static void alarm_vconfkey_idle_lock_cb(keynode_t * key, void *data);
40 static void alarm_vconfkey_volume_type_alarm_cb(keynode_t * key, void *data);
41 static void alarm_vconfkey_timezone_cb(keynode_t * node, void *data);
42 static void alarm_time_changed_cb(void *data);
43 /**********************************************************************
44 ******************Global val , static global val*************************************
45 ***********************************************************************/
46 #define ALARM_NOTI_MAX            5
47 static noti_callback s_notis[ALARM_NOTI_MAX] = {
48         {VCONFKEY_ALARM_UPDATE, alarm_vconfkey_update_cb},
49         {VCONFKEY_ALARM_DELETE, alarm_vconfkey_delete_cb},
50         {VCONFKEY_IDLE_LOCK_STATE, alarm_vconfkey_idle_lock_cb},
51         {VCONF_KEY_VOLUME_TYPE_ALARM, alarm_vconfkey_volume_type_alarm_cb},
52         {VCONFKEY_SETAPPL_TIMEZONE_INT, alarm_vconfkey_timezone_cb},
53 };
54
55 /**********************************************************************
56 ******************Local function ref*************************************
57 ***********************************************************************/
58
59 /**
60 * send
61 * This function is  used to handle vconf key: VCONFKEY_ALARM_UPDATE, value: VCONFKEY_ALARM_OFF
62 * @param           data[in]           pointer to data
63 * @return          when success, return SUCCESS  or FAILED if error
64 * @exception
65 */
66 static int alarm_vconfkey_alarm_off_cb(void *data)
67 {
68         struct appdata *ad = (struct appdata *)data;
69         if (IS_EQUAL(ad->page, PAGE_ALARM_GENLIST)) {
70                 //update the alarm genlist
71                 alarm_view_genlist.update(data);
72         }
73         //ad->alarm update
74         ad->alarm->alarm_mgr_id = -1;
75         ad->alarm->enable = EINA_FALSE;
76         return SUCCESS;
77 }
78
79 /**
80 * send
81 * This function is  used to handle vconf key: VCONFKEY_ALARM_UPDATE, value: VCONFKEY_ALARM_SNOOZE_ON
82 * @param           data[in]           pointer to data
83 * @return          when success, return SUCCESS  or FAILED if error
84 * @exception
85 */
86 static int alarm_vconfkey_alarm_snooze_on_cb(void *data)
87 {
88         struct appdata *ad = (struct appdata *)data;
89         if (IS_EQUAL(ad->page, PAGE_ALARM_GENLIST)) {
90                 //update the alarm genlist
91                 alarm_view_genlist.update(data);
92         }
93         return SUCCESS;
94 }
95
96 /**
97 * send
98 * This function is  used to handle vconf key: VCONFKEY_ALARM_UPDATE
99 * @param           data[in]           pointer to data
100 * @return          void
101 * @exception
102 */
103 static void alarm_vconfkey_update_cb(keynode_t * key, void *data)
104 {
105         int alarm_update_value = vconf_keynode_get_int(key);
106         CLK_FUN_BEG();
107         switch (alarm_update_value) {
108         case VCONFKEY_ALARM_OFF:
109                 alarm_vconfkey_alarm_off_cb(data);
110                 break;
111         case VCONFKEY_ALARM_SNOOZE_ON:
112                 alarm_vconfkey_alarm_snooze_on_cb(data);
113                 break;
114         default:
115                 CLK_ERR("error alarm_update_value");
116                 break;
117         }
118         CLK_FUN_END();
119 }
120
121 /**
122 * send
123 * This function is  used to handle vconf key: VCONFKEY_ALARM_DELETE
124 * @param           data[in]           pointer to data
125 * @return          void
126 * @exception
127 */
128 static void alarm_vconfkey_delete_cb(keynode_t * key, void *data)
129 {
130         int alarm_delete_id = vconf_keynode_get_int(key);
131         struct appdata *ad = (struct appdata *)data;
132         CLK_FUN_BEG();
133         //for name
134         alarm_genlist_name_array_del(alarm_delete_id);
135         if (IS_EQUAL(ad->page, PAGE_ALARM_GENLIST)) {
136                 alarm_view_genlist.update(data);
137         }
138         CLK_FUN_END();
139 }
140
141 /**
142 * send
143 * This function is  used to handle vconf key: VCONFKEY_IDLE_LOCK_STATE
144 * @param           data[in]           pointer to data
145 * @return          void
146 * @exception
147 */
148 static void alarm_vconfkey_idle_lock_cb(keynode_t * key, void *data)
149 {
150         int lock_state = vconf_keynode_get_int(key);
151         struct appdata *ad = (struct appdata *)data;
152         CLK_FUN_BEG();
153         if (IS_EQUAL(lock_state, VCONFKEY_IDLE_LOCK) &&
154             IS_EQUAL(ad->page, PAGE_ALARM_SETTING)) {
155                 alarm_setting_stop_sound(ad);
156                 alarm_type_stop_sound(ad);
157         }
158         CLK_FUN_END();
159 }
160
161 //
162 static void alarm_vconfkey_volume_type_alarm_cb(keynode_t * key, void *data)
163 {
164         int value = vconf_keynode_get_int(key);
165         struct appdata *ad = (struct appdata *)data;
166         setting_view *view = ad->view_setting;
167         CLK_FUN_BEG();
168         if (IS_EQUAL(ad->page, PAGE_ALARM_SETTING)) {
169                 if (!elm_object_item_disabled_get(view->item_volume)) {
170                         elm_slider_value_set(view->eo_volume_slider, value);
171                 }
172         }
173         CLK_FUN_END();
174 }
175
176 static void alarm_vconfkey_timezone_cb(keynode_t * node, void *data)
177 {
178         ret_if(!data);
179         alarm_time_changed_cb(data);
180 }
181
182 #define _TOUCH_VCONFKEY_TYPE_INT(key, value) \
183 do { \
184     if (vconf_get_int(key, &value) != SUCCESS) {\
185         vconf_set_int(key, 0);\
186     } \
187 } while (0);
188
189 //
190 static void _alarm_noti_touch()
191 {
192         int iValue = 0;
193         int index = 0;
194         //
195         for (index = 0; index < ALARM_NOTI_MAX; index++) {
196                 _TOUCH_VCONFKEY_TYPE_INT(s_notis[index].setting_key, iValue)
197         }
198 }
199
200 static int _alarm_region_changed_cb(void *data)
201 {
202         retvm_if(!data, FAILED, "data");
203         alarm_time_changed_cb(data);
204         return SUCCESS;
205 }
206
207 static void alarm_setting_time_changed_cb(void *data)
208 {
209         ret_if(!data);
210         alarm_time_changed_cb(data);
211 }
212
213 static void alarm_time_changed_cb(void *data)
214 {
215         ret_if(!data);
216         struct appdata *ad = (struct appdata *)data;
217         ret_if(!ad->is_alarm);
218         tzset();
219         clk_timezone_update();
220         vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &ad->time_format);
221         if (IS_EQUAL(ad->page, PAGE_ALARM_GENLIST)) {
222                 alarm_view_genlist.update(data);
223         } else if (IS_EQUAL(ad->page, PAGE_ALARM_DELETE)) {
224                 alarm_view_delete.update(data);
225         } else if (IS_EQUAL(ad->page, PAGE_ALARM_SETTING)) {
226                 alarm_view_setting.update(data);
227         }
228 }
229
230 /**********************************************************************
231 ******************Global function ref*************************************
232 ***********************************************************************/
233
234 /**
235 * send
236 * This function is  used to init noti of vconf
237 * @param           data[in]           pointer to data
238 * @return          when success, return SUCCESS  or FAILED if error
239 * @exception
240 */
241 int alarm_noti_init(void *data)
242 {
243         //temp handle to protect
244         _alarm_noti_touch();
245         //set cb
246         struct appdata *ad = (struct appdata *)data;
247         int i = 0;
248         appcore_set_event_callback(APPCORE_EVENT_REGION_CHANGE,
249                                    _alarm_region_changed_cb, data);
250         alarm_heynoti_init(ad);
251         for (i = 0; i < ALARM_NOTI_MAX; i++) {
252                 if (0 !=
253                     vconf_notify_key_changed(s_notis[i].setting_key,
254                                              s_notis[i].noti_func, ad)) {
255                         return FAILED;
256                 }
257         }
258         return SUCCESS;
259 }
260
261 /**
262 * send
263 * This function is  used to fini noti of vconf
264 * @param           void
265 * @return          void
266 * @exception
267 */
268 void alarm_noti_fini()
269 {
270         int i;
271         for (i = 0; i < ALARM_NOTI_MAX; i++) {
272                 vconf_ignore_key_changed(s_notis[i].setting_key,
273                                          s_notis[i].noti_func);
274         }
275         alarm_heynoti_fini();
276 }
277
278 int alarm_heynoti_init(void *data)
279 {
280         int fd = -1;
281         int ret = SUCCESS;
282
283         fd = heynoti_init();
284         retv_if(fd < 0, FAILED);
285
286         ret = heynoti_subscribe(fd, "setting_time_changed",
287                                 alarm_setting_time_changed_cb, data);
288         if (SUCCESS != ret) {
289                 CLK_ERR("heynoti_subscribe");
290                 goto end;
291         }
292
293         ret = heynoti_attach_handler(fd);
294         if (SUCCESS != ret) {
295                 CLK_ERR("heynoti_attach_handler");
296                 goto end;
297         }
298
299         g_fd = fd;
300  end:
301         if (SUCCESS != ret) {
302                 heynoti_detach_handler(fd);
303                 heynoti_unsubscribe(fd, "setting_time_changed",
304                                     alarm_setting_time_changed_cb);
305                 heynoti_close(fd);
306         }
307         return ret;
308 }
309
310 void alarm_heynoti_fini()
311 {
312         int fd = g_fd;
313         ret_if(fd < 0);
314
315         heynoti_detach_handler(fd);
316         heynoti_unsubscribe(fd, "setting_time_changed",
317                             alarm_setting_time_changed_cb);
318         heynoti_close(fd);
319         g_fd = -1;
320 }