12h time format change
[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                 char bf1[32] = { 0, };
108                 int hour;
109                 if (ts->tm_hour >= 0 && ts->tm_hour < 12)
110                         snprintf(ampm_buf, sizeof(ampm_buf),
111                                  LABEL_STRING, AMPM_FONT_SIZE,
112                                  _("IDS_COM_POP_AM_M_ABB"));
113                 else
114                         snprintf(ampm_buf, sizeof(ampm_buf),
115                                  LABEL_STRING, AMPM_FONT_SIZE,
116                                  _("IDS_COM_POP_PM_M_ABB"));
117
118                 strftime(bf1, sizeof(bf1), "%l", ts);
119                 hour = atoi(bf1);
120                 strftime(bf1, sizeof(bf1), ":%M", ts);
121
122                 snprintf(time_str, sizeof(time_str), "%d%s", hour, bf1);
123 //              strftime(time_str, sizeof(time_str), "%I:%M", ts);
124         } else {
125                 strftime(time_str, sizeof(time_str), "%H:%M", ts);
126         }
127
128         snprintf(time_buf, sizeof(time_buf), LABEL_STRING,
129                  TIME_FONT_SIZE, time_str);
130         len = snprintf(buf, sizeof(buf), "%s%s", time_buf, ampm_buf);
131         if (len < 0) {
132                 ERR("Unexpected ERROR!");
133                 return;
134         }
135
136         INFO("[CLOCK MODULE] Timer Status : %d Time: %s", timer, buf);
137
138         edje_object_part_text_set(elm_layout_edje_get(ad->layout_main),
139                                   "elm.text.clock", buf);
140         return;
141 }
142
143 static void indicator_clock_format_changed_cb(keynode_t *node, void *data)
144 {
145         retif(data == NULL, , "Invalid parameter!");
146
147         int r = -1;
148
149         bool is_24hour_enabled = false;
150
151         INFO("[Enter] indicator_clock_format_changed_cb");
152
153         r = runtime_info_get_value_bool(
154                         RUNTIME_INFO_KEY_24HOUR_CLOCK_FORMAT_ENABLED, &is_24hour_enabled);
155
156         /* Check Time format. If timeformat have invalid value, Set to 12H */
157         if( r==RUNTIME_INFO_ERROR_NONE&&is_24hour_enabled==true)
158         {
159                 clock_mode = INDICATOR_CLOCK_MODE_24H;
160         }
161         else
162         {
163                 clock_mode = INDICATOR_CLOCK_MODE_12H;
164         }
165
166         indicator_clock_changed_cb(data);
167 }
168
169 static int language_changed_cb(void *data)
170 {
171         indicator_clock_changed_cb(data);
172         return OK;
173 }
174
175 static int region_changed_cb(void *data)
176 {
177         indicator_clock_format_changed_cb(NULL, data);
178         return OK;
179 }
180
181 static int register_clock_module(void *data)
182 {
183         int r = 0, ret = -1;
184
185         retif(data == NULL, FAIL, "Invalid parameter!");
186
187         notifd = heynoti_init();
188
189         ret =
190             heynoti_subscribe(notifd, SETTING_TIME_CHANGED_NOTI,
191                               indicator_clock_changed_cb, data);
192         if (ret != OK) {
193                 ERR("Fail: register SETTING_TIME_CHANGED_NOTI");
194                 r = ret;
195         }
196
197         ret =
198             heynoti_subscribe(notifd, SYSTEM_RESUME, indicator_clock_changed_cb,
199                               data);
200         if (ret != OK) {
201                 ERR("Fail: register SYSTEM_RESUME");
202                 r = r | ret;
203         }
204
205         ret = heynoti_attach_handler(notifd);
206         if (ret != OK) {
207                 ERR("Failed to attach heynoti handler!");
208                 r = r | ret;
209         }
210
211         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
212                                        indicator_clock_format_changed_cb, data);
213         if (ret != OK) {
214                 ERR("Fail: register VCONFKEY_REGIONFORMAT_TIME1224");
215                 r = r | ret;
216         }
217
218         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT,
219                                        indicator_clock_format_changed_cb, data);
220         if (ret != OK) {
221                 ERR("Fail: register VCONFKEY_SETAPPL_TIMEZONE_INT");
222                 r = r | ret;
223         }
224
225         indicator_clock_format_changed_cb(NULL, data);
226
227         return r;
228 }
229
230 static int unregister_clock_module(void)
231 {
232         int ret;
233
234         heynoti_unsubscribe(notifd, SETTING_TIME_CHANGED_NOTI,
235                             indicator_clock_changed_cb);
236
237         heynoti_unsubscribe(notifd, SYSTEM_RESUME, indicator_clock_changed_cb);
238
239         heynoti_close(notifd);
240         notifd = 0;
241
242         ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
243                                        indicator_clock_format_changed_cb);
244         if (ret != OK)
245                 ERR("Fail: unregister VCONFKEY_REGIONFORMAT_TIME1224");
246
247         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT,
248                                        indicator_clock_format_changed_cb);
249         if (ret != OK)
250                 ERR("Fail: unregister VCONFKEY_SETAPPL_TIMEZONE_INT");
251
252         if (timer != NULL) {
253                 ecore_timer_del(timer);
254                 timer = NULL;
255         }
256         return OK;
257 }
258
259 static int hib_enter_clock_module(void)
260 {
261         int ret;
262
263         ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
264                                        indicator_clock_format_changed_cb);
265         if (ret != OK)
266                 ERR("Fail: unregister VCONFKEY_REGIONFORMAT_TIME1224");
267
268         if (timer != NULL) {
269                 ecore_timer_del(timer);
270                 timer = NULL;
271         }
272
273         return OK;
274 }
275
276 static int hib_leave_clock_module(void *data)
277 {
278         int ret;
279
280         retif(data == NULL, FAIL, "Invalid parameter!");
281
282         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
283                                        indicator_clock_format_changed_cb, data);
284         retif(ret != OK, FAIL, "Failed to register callback!");
285
286         indicator_clock_format_changed_cb(NULL, data);
287         return OK;
288 }