tizen 2.4 release
[apps/home/quickpanel.git] / daemon / datetime / datetime.c
1 /*
2  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
18 #include <glib.h>
19 #include <string.h>
20 #include <Elementary.h>
21
22 #include <app.h>
23 #include <vconf.h>
24 #include <notification.h>
25 #include <tzsh.h>
26 #include <tzsh_quickpanel_service.h>
27 #include <sound_manager.h>
28
29 #include "common.h"
30 #include "common_uic.h"
31 #include "quickpanel-ui.h"
32 #include "list_util.h"
33 #include "quickpanel_def.h"
34 #include "modules.h"
35 #include "util-time.h"
36 #include "media.h"
37
38 #ifdef QP_SCREENREADER_ENABLE
39 #include "accessibility.h"
40 #endif
41
42 #ifdef QP_EMERGENCY_MODE_ENABLE
43 #include "emergency_mode.h"
44 #endif
45
46 static int _init(void *data);
47 static int _fini(void *data);
48
49 #define PKG_SETTING_EDIT "quickpanel-setting-efl"
50 #define QP_TIMEDATE_SETTING_UG "setting-time-efl"
51 #define E_DATA_EDITING_VISIBILITT "editing_visible"
52 #define E_DATA_TIME_N_DATE_EVENT        "time_n_date_event"
53
54 QP_Module qp_datetime_view = {
55         .name = "qp_datetime_view",
56         .init = _init,
57         .fini = _fini,
58         .suspend = NULL,
59         .resume = NULL,
60         .lang_changed = NULL,
61         .refresh = NULL,
62 };
63
64 static Evas_Object *_datetime_view_get(void);
65
66 static void _flag_set(Evas_Object *container, const char *key, int value)
67 {
68         retif(container == NULL, , "invalid parameter");
69         retif(key == NULL, , "invalid parameter");
70
71         evas_object_data_set(container, key, (void *)value);
72 }
73
74 static int _flag_get(Evas_Object *container, const char *key)
75 {
76         retif(container == NULL, 0, "invalid parameter");
77         retif(key == NULL, 0, "invalid parameter");
78
79         return (int)evas_object_data_get(container, key);
80 }
81
82 static void _set_text_to_part(Evas_Object *obj, const char *part, const char *text)
83 {
84         const char *old_text = NULL;
85
86         retif(obj == NULL, , "Invalid parameter!");
87         retif(part == NULL, , "Invalid parameter!");
88         retif(text == NULL, , "Invalid parameter!");
89
90         old_text = elm_object_part_text_get(obj, part);
91         if (old_text != NULL) {
92                 if (strcmp(old_text, text) == 0) {
93                         return;
94                 }
95         }
96
97         elm_object_part_text_set(obj, part, text);
98 }
99
100 static void _text_time_clicked_cb(void *data, Evas_Object *obj, void *event_info)
101 {
102         Evas_Object *view = _datetime_view_get();
103         int ret;
104
105         if (view) {
106                 if (_flag_get(view, E_DATA_TIME_N_DATE_EVENT) == 0) {
107                         DBG("Time & date area click is event disabled");
108                         return;
109                 }
110         }
111
112         quickpanel_media_play_feedback();
113
114         ret = quickpanel_uic_launch_ug_by_appcontrol(QP_TIMEDATE_SETTING_UG, NULL);
115         quickpanel_uic_launch_app_inform_result(QP_TIMEDATE_SETTING_UG, ret);
116
117         quickpanel_uic_close_quickpanel(true, 1);
118 }
119
120 static void _button_setting_clicked_cb(void *data, Evas_Object *obj, void *event_info)
121 {
122         quickpanel_media_play_feedback();
123
124 #ifdef QP_EMERGENCY_MODE_ENABLE
125         if (quickpanel_emergency_mode_is_on()) {
126                 quickpanel_uic_launch_app(PACKAGE_EMERGENCY_MODE_SETTING, NULL);
127         } else {
128                 quickpanel_uic_launch_app(QP_SETTING_PKG_SETTING, NULL);
129         }
130 #else
131         quickpanel_uic_launch_app(QP_SETTING_PKG_SETTING, NULL);
132 #endif
133         quickpanel_uic_close_quickpanel(true, 1);
134 }
135
136 static Evas_Object *_datetime_view_create(Evas_Object *parent)
137 {
138         Evas_Object *focus = NULL;
139         Eina_Bool ret = EINA_TRUE;
140         Evas_Object *view = NULL;
141
142         retif(parent == NULL, NULL, "Invalid parameter!");
143
144         view = elm_layout_add(parent);
145
146         if (view != NULL) {
147                 ret = elm_layout_file_set(view, DEFAULT_EDJ,
148                                 "quickpanel/datetime");
149                 if (ret == EINA_FALSE) {
150                         ERR("failed to load quickpanel/datetime layout");
151                 }
152                 evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
153                 evas_object_size_hint_align_set(view, EVAS_HINT_FILL, EVAS_HINT_FILL);
154                 quickpanel_uic_initial_resize(view, QP_DATE_H);
155
156                 focus = quickpanel_accessibility_ui_get_focus_object(view);
157                 elm_object_part_content_set(view, "focus.datetime", focus);
158                 evas_object_smart_callback_add(focus, "clicked", _text_time_clicked_cb, view);
159
160                 focus = quickpanel_accessibility_ui_get_focus_object(view);
161                 elm_object_part_content_set(view, "focus.setting", focus);
162                 evas_object_smart_callback_add(focus, "clicked", _button_setting_clicked_cb, view);
163
164                 _flag_set(view, E_DATA_EDITING_VISIBILITT, 0);
165
166                 if (quickpanel_emergency_mode_is_on()) {
167                         _flag_set(view, E_DATA_TIME_N_DATE_EVENT, 0);
168                         elm_object_signal_emit(view, "timendate.click.disable", "prog");
169                 } else {
170                         _flag_set(view, E_DATA_TIME_N_DATE_EVENT, 1);
171                         elm_object_signal_emit(view, "timendate.click.enable", "prog");
172                 }
173
174                 evas_object_show(view);
175         }
176
177         return view;
178 }
179
180 static Evas_Object *_datetime_view_get(void) {
181         struct appdata *ad = quickpanel_get_app_data();
182         retif(ad == NULL, NULL, "invalid argument");
183         retif(ad->view_root == NULL, NULL, "invalid argument");
184
185         return elm_object_part_content_get(ad->view_root
186                         , "qp.base.datetime.swallow");
187 }
188
189 static void _datetime_view_attach(void *data)
190 {
191         Evas_Object *view = NULL;
192         struct appdata *ad = data;
193         retif(ad == NULL, ,"invalid parameter");
194         retif(ad->view_root == NULL, ,"invalid parameter");
195
196         view = _datetime_view_create(ad->view_root);
197         if (view != NULL) {
198                 elm_object_part_content_set(ad->view_root, "qp.base.datetime.swallow", view);
199         }
200 }
201
202 static void _datetime_view_deattach(void *data)
203 {
204         Evas_Object *view = NULL;
205         struct appdata *ad = data;
206         retif(ad == NULL, ,"invalid parameter");
207         retif(ad->view_root == NULL, ,"invalid parameter");
208
209         view = elm_object_part_content_unset(ad->view_root, "qp.base.datetime.swallow");
210         if (view != NULL) {
211                 evas_object_del(view);
212                 view = NULL;
213         }
214 }
215
216 static int _init(void *data)
217 {
218         struct appdata *ad = data;
219         retif(ad == NULL, QP_FAIL,"invalid parameter");
220
221         _datetime_view_attach(ad);
222
223         return QP_OK;
224 }
225
226 static int _fini(void *data)
227 {
228         _datetime_view_deattach(data);
229
230         return QP_OK;
231 }
232
233 HAPI void quickpanel_datetime_datentime_event_set(int is_clickable)
234 {
235         Evas_Object *view = _datetime_view_get();
236
237         DBG("date n time clickable set[%d]", is_clickable);
238
239         if (view != NULL) {
240                 if (is_clickable == 1) {
241                         if (_flag_get(view, E_DATA_TIME_N_DATE_EVENT) == 0) {
242                                 _flag_set(view, E_DATA_TIME_N_DATE_EVENT, 1);
243                                 elm_object_signal_emit(view, "timendate.click.enable", "prog");
244                         }
245                 } else {
246                         if (_flag_get(view, E_DATA_TIME_N_DATE_EVENT) == 1) {
247                                 _flag_set(view, E_DATA_TIME_N_DATE_EVENT, 0);
248                                 elm_object_signal_emit(view, "timendate.click.disable", "prog");
249                         }
250                 }
251         }
252 }
253
254 HAPI void quickpanel_datetime_editing_icon_visibility_set(int is_visible)
255 {
256         Evas_Object *view = _datetime_view_get();
257
258         DBG("visibility set:%d", is_visible);
259
260         if (view != NULL) {
261                 if (is_visible == 1) {
262                         if (_flag_get(view, E_DATA_EDITING_VISIBILITT) == 0) {
263                                 _flag_set(view, E_DATA_EDITING_VISIBILITT, 1);
264                                 elm_object_signal_emit(view, "button,editing,show", "prog");
265                         }
266                 } else {
267                         if (_flag_get(view, E_DATA_EDITING_VISIBILITT) == 1) {
268                                 _flag_set(view, E_DATA_EDITING_VISIBILITT, 0);
269                                 elm_object_signal_emit(view, "button,editing,hide", "prog");
270                         }
271                 }
272         }
273 }
274
275 HAPI void quickpanel_datetime_view_update(char *date, char *time, char *meridiem, int meridiem_type)
276 {
277         Evas_Object *view = NULL;
278
279         Eina_Strbuf *strbuf_date = NULL;
280         Eina_Strbuf *strbuf_time = NULL;
281         Eina_Strbuf *strbuf_access = NULL;
282
283         view = _datetime_view_get();
284
285         if (!view) {
286                 ERR("view == NULL");
287                 return;
288         }
289
290         strbuf_date = eina_strbuf_new();
291         if(!strbuf_date) {
292                 ERR("strbuf_date == NULL");
293                 return;
294         }
295
296         strbuf_time = eina_strbuf_new();
297         if(!strbuf_time) {
298                 ERR("strbuf_time == NULL");
299                 eina_strbuf_free(strbuf_date);
300                 return;
301         }
302
303         strbuf_access = eina_strbuf_new();
304         if(!strbuf_access) {
305                 ERR("strbuf_access == NULL");
306                 eina_strbuf_free(strbuf_date);
307                 eina_strbuf_free(strbuf_time);
308                 return;
309         }
310
311
312         DBG("update time: %s %s %s", date, time, meridiem);
313
314         if (date != NULL) {
315                 eina_strbuf_append_printf(strbuf_date, "%s", date);
316                 eina_strbuf_append_printf(strbuf_access, "%s ", date);
317         }
318
319         eina_strbuf_ltrim(strbuf_date);
320
321         // -------------------------------------------------------------------------------------
322
323         if (meridiem_type == UTIL_TIME_MERIDIEM_TYPE_PRE && meridiem != NULL && strlen(meridiem) != 0) {
324                 eina_strbuf_append_printf(strbuf_time, "<ampm>%s</> ", meridiem);
325                 eina_strbuf_append_printf(strbuf_access, "%s ", meridiem);
326         }
327
328         if (time != NULL) {
329                 eina_strbuf_append_printf(strbuf_time, "<time>%s</>", time);
330                 eina_strbuf_append_printf(strbuf_access, "%s ", time);
331         }
332
333         if (meridiem_type == UTIL_TIME_MERIDIEM_TYPE_POST && meridiem != NULL && strlen(meridiem) != 0) {
334                 eina_strbuf_append_printf(strbuf_time, " <ampm>%s</>", meridiem);
335                 eina_strbuf_append_printf(strbuf_access, "%s ", meridiem);
336         }
337
338         eina_strbuf_ltrim(strbuf_time);
339
340         // -------------------------------------------------------------------------------------
341
342         LOGI("DATE STR SET: %s", eina_strbuf_string_get(strbuf_time));
343
344         _set_text_to_part(view, "text.date", eina_strbuf_string_get(strbuf_date));
345         _set_text_to_part(view, "text.time", eina_strbuf_string_get(strbuf_time));
346
347         quickpanel_accessibility_screen_reader_data_set(view, "focus.datetime", "", (char *)eina_strbuf_string_get(strbuf_access));
348
349         eina_strbuf_free(strbuf_date);
350         eina_strbuf_free(strbuf_time);
351         eina_strbuf_free(strbuf_access);
352
353         quickpanel_accessibility_screen_reader_data_set(view
354                         , "focus.setting", "", _NOT_LOCALIZED("Settings"));
355
356 }