b80bf24d1131912fc989a1d6c58a8ad45c314770
[profile/ivi/indicator-win.git] / modules / clock / clock.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 <stdio.h>
18 #include <stdlib.h>
19 #include <vconf.h>
20 #include <heynoti.h>
21 #include <runtime_info.h>
22 #include <Ecore_X.h>
23 #include "common.h"
24 #include "indicator.h"
25 #include "indicator_ui.h"
26 #include "indicator_gui.h"
27 #include "indicator_icon_util.h"
28 #include "modules.h"
29
30 #define SETTING_TIME_CHANGED_NOTI       "setting_time_changed"
31 #define SYSTEM_RESUME                           "system_wakeup"
32
33 #define TIME_FONT_SIZE          26
34 #define TIME_FONT_COLOR         127, 104, 79, 255
35
36 #define AMPM_FONT_SIZE          20
37 #define AMPM_FONT_COLOR         148, 125, 99, 255
38 #define LABEL_STRING            "<font_size=%d>%s" \
39                                 "</font_size></font>"
40
41 enum {
42         INDICATOR_CLOCK_MODE_12H = 0,
43         INDICATOR_CLOCK_MODE_24H,
44         INDICATOR_CLOCK_MODE_MAX
45 };
46
47 static int notifd;
48 static int clock_mode = INDICATOR_CLOCK_MODE_12H;
49 static Ecore_Timer *timer;
50
51 static int register_clock_module(void *data);
52 static int unregister_clock_module(void);
53 static int hib_enter_clock_module(void);
54 static int hib_leave_clock_module(void *data);
55 static int language_changed_cb(void *data);
56 static int region_changed_cb(void *data);
57
58 Indicator_Icon_Object sysclock = {
59         .type = INDICATOR_TXT_ICON,
60         .name = "clock",
61         .priority = INDICATOR_PRIORITY_FIXED1,
62         .always_top = EINA_FALSE,
63         .txt_obj = {0,},
64         .img_obj = {0,},
65         .obj_exist = EINA_FALSE,
66         .exist_in_view = EINA_FALSE,
67         .init = register_clock_module,
68         .fini = unregister_clock_module,
69         .hib_enter = hib_enter_clock_module,
70         .hib_leave = hib_leave_clock_module,
71         .lang_changed = NULL,
72         .region_changed = region_changed_cb,
73 };
74
75 static void indicator_clock_changed_cb(void *data)
76 {
77         struct appdata *ad = (struct appdata *)data;
78         char time_str[32];
79         char time_buf[128], ampm_buf[128];
80         char buf[256];
81         struct tm *ts = NULL;
82         time_t ctime;
83         int len;
84
85         retif(data == NULL, , "Invalid parameter!");
86
87         /* Set time */
88         ctime = time(NULL);
89         ts = localtime(&ctime);
90         if (ts == NULL)
91                 return;
92
93         if (timer != NULL) {
94                 ecore_timer_del(timer);
95                 timer = NULL;
96         }
97
98         memset(time_str, 0x00, sizeof(time_str));
99         memset(time_buf, 0x00, sizeof(time_buf));
100         memset(ampm_buf, 0x00, sizeof(ampm_buf));
101         memset(buf, 0x00, sizeof(buf));
102
103         timer =
104             ecore_timer_add(60 - ts->tm_sec, (void *)indicator_clock_changed_cb,
105                             data);
106         if (clock_mode == INDICATOR_CLOCK_MODE_12H) {
107                 if (ts->tm_hour >= 0 && ts->tm_hour < 12)
108                         snprintf(ampm_buf, sizeof(ampm_buf),
109                                  LABEL_STRING, AMPM_FONT_SIZE,
110                                  "AM");
111                 else
112                         snprintf(ampm_buf, sizeof(ampm_buf),
113                                  LABEL_STRING, AMPM_FONT_SIZE,
114                                  "PM");
115                 strftime(time_str, sizeof(time_str), "%I:%M", ts);
116         } else {
117                 strftime(time_str, sizeof(time_str), "%H:%M", ts);
118         }
119
120         snprintf(time_buf, sizeof(time_buf), LABEL_STRING,
121                  TIME_FONT_SIZE, time_str);
122         len = snprintf(buf, sizeof(buf), "%s%s", time_buf, ampm_buf);
123         if (len < 0) {
124                 ERR("Unexpected ERROR!");
125                 return;
126         }
127
128         INFO("[CLOCK MODULE] Timer Status : %d Time: %s", timer, buf);
129
130         edje_object_part_text_set(elm_layout_edje_get(ad->layout_main),
131                                   "elm.text.clock", buf);
132         return;
133 }
134
135 static void indicator_clock_format_changed_cb(keynode_t *node, void *data)
136 {
137         retif(data == NULL, , "Invalid parameter!");
138
139         int r = -1;
140
141         bool is_24hour_enabled = false;
142
143         INFO("[Enter] indicator_clock_format_changed_cb");
144
145         r = runtime_info_get_value_bool(
146                         RUNTIME_INFO_KEY_24HOUR_CLOCK_FORMAT_ENABLED, &is_24hour_enabled);
147
148         /* Check Time format. If timeformat have invalid value, Set to 12H */
149         if( r==RUNTIME_INFO_ERROR_NONE&&is_24hour_enabled==true)
150         {
151                 clock_mode = INDICATOR_CLOCK_MODE_24H;
152         }
153         else
154         {
155                 clock_mode = INDICATOR_CLOCK_MODE_12H;
156         }
157
158         indicator_clock_changed_cb(data);
159 }
160
161 static int language_changed_cb(void *data)
162 {
163         indicator_clock_changed_cb(data);
164         return OK;
165 }
166
167 static int region_changed_cb(void *data)
168 {
169         indicator_clock_format_changed_cb(NULL, data);
170         return OK;
171 }
172
173 static int register_clock_module(void *data)
174 {
175         int r = 0, ret = -1;
176
177         retif(data == NULL, FAIL, "Invalid parameter!");
178
179         notifd = heynoti_init();
180
181         ret =
182             heynoti_subscribe(notifd, SETTING_TIME_CHANGED_NOTI,
183                               indicator_clock_changed_cb, data);
184         if (ret != OK) {
185                 ERR("Fail: register SETTING_TIME_CHANGED_NOTI");
186                 r = ret;
187         }
188
189         ret =
190             heynoti_subscribe(notifd, SYSTEM_RESUME, indicator_clock_changed_cb,
191                               data);
192         if (ret != OK) {
193                 ERR("Fail: register SYSTEM_RESUME");
194                 r = r | ret;
195         }
196
197         ret = heynoti_attach_handler(notifd);
198         if (ret != OK) {
199                 ERR("Failed to attach heynoti handler!");
200                 r = r | ret;
201         }
202
203         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
204                                        indicator_clock_format_changed_cb, data);
205         if (ret != OK) {
206                 ERR("Fail: register VCONFKEY_REGIONFORMAT_TIME1224");
207                 r = r | ret;
208         }
209
210         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT,
211                                        indicator_clock_format_changed_cb, data);
212         if (ret != OK) {
213                 ERR("Fail: register VCONFKEY_SETAPPL_TIMEZONE_INT");
214                 r = r | ret;
215         }
216
217         indicator_clock_format_changed_cb(NULL, data);
218
219         return r;
220 }
221
222 static int unregister_clock_module(void)
223 {
224         int ret;
225
226         heynoti_unsubscribe(notifd, SETTING_TIME_CHANGED_NOTI,
227                             indicator_clock_changed_cb);
228
229         heynoti_unsubscribe(notifd, SYSTEM_RESUME, indicator_clock_changed_cb);
230
231         heynoti_close(notifd);
232         notifd = 0;
233
234         ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
235                                        indicator_clock_format_changed_cb);
236         if (ret != OK)
237                 ERR("Fail: unregister VCONFKEY_REGIONFORMAT_TIME1224");
238
239         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT,
240                                        indicator_clock_format_changed_cb);
241         if (ret != OK)
242                 ERR("Fail: unregister VCONFKEY_SETAPPL_TIMEZONE_INT");
243
244         if (timer != NULL) {
245                 ecore_timer_del(timer);
246                 timer = NULL;
247         }
248         return OK;
249 }
250
251 static int hib_enter_clock_module(void)
252 {
253         int ret;
254
255         ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
256                                        indicator_clock_format_changed_cb);
257         if (ret != OK)
258                 ERR("Fail: unregister VCONFKEY_REGIONFORMAT_TIME1224");
259
260         if (timer != NULL) {
261                 ecore_timer_del(timer);
262                 timer = NULL;
263         }
264
265         return OK;
266 }
267
268 static int hib_leave_clock_module(void *data)
269 {
270         int ret;
271
272         retif(data == NULL, FAIL, "Invalid parameter!");
273
274         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
275                                        indicator_clock_format_changed_cb, data);
276         retif(ret != OK, FAIL, "Failed to register callback!");
277
278         indicator_clock_format_changed_cb(NULL, data);
279         return OK;
280 }