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