tizen 2.3.1 release
[apps/home/settings.git] / src / setting-support-shortcut / support_shortcut_manage.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <setting-debug.h>
22 #include <app.h>
23 #include <ui-gadget.h>
24 #include <Elementary.h>
25 #include <Evas.h>
26 #include <Ecore_X.h>
27 #include <utilX.h>
28 #include <appcore-efl.h>
29 #include <appcore-common.h>
30 #include <vconf.h>
31 #include <feedback.h>
32 #include <dd-led.h>
33 #include <device.h>
34 #include <efl_assist.h>
35 #include <setting-common-general-func.h>
36
37 #define EXPORT_PUBLIC __attribute__ ((visibility ("default")))
38
39
40 typedef struct _support_display_appdata {
41
42         ui_gadget_h ug;
43
44         Evas *evas;
45         Evas_Object *win_main;
46         Evas_Object *navibar_main;
47         Evas_Object *ly_main;                   /**< seting view main */
48         app_control_h service;
49         Ecore_Event_Handler *event_handler;
50 } support_display_appdata;
51
52 /**
53 * The function is called when Setting is terminated
54 */
55 static void support_display_app_terminate(void *data)
56 {
57         SETTING_TRACE_BEGIN;
58         support_display_appdata *ad = data;
59
60         SETTING_TRACE("support_display_terminate!");
61
62         if (ad->event_handler) {
63                 ecore_event_handler_del(ad->event_handler);
64                 ad->event_handler = NULL;
65         }
66
67         if (ad->ly_main) {
68                 evas_object_del(ad->ly_main);
69                 ad->ly_main = NULL;
70         }
71
72         if (ad->win_main) {
73                 evas_object_del(ad->win_main);
74                 ad->win_main = NULL;
75         }
76         elm_exit();
77
78         SETTING_TRACE_END;
79 }
80
81 /**
82 * The event process when win object is destroyed
83 */
84 static void support_display_del_win(void *data, Evas_Object *obj, void *event)
85 {
86         /*app_efl_exit(); */
87         elm_exit();
88 }
89
90 /**
91 * To create a win object, the win is shared between the App and all its UGs
92 */
93 static Evas_Object *support_display_create_win(const char *name)
94 {
95         SETTING_TRACE_BEGIN;
96         Evas_Object *eo;
97         int w, h;
98
99         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
100
101         if (eo) {
102                 elm_win_title_set(eo, name);
103                 elm_win_borderless_set(eo, EINA_TRUE);
104                 evas_object_smart_callback_add(eo, "delete,request",
105                                                support_display_del_win, NULL);
106                 ecore_x_window_size_get(ecore_x_window_root_first_get(),
107                                         &w, &h);
108                 evas_object_resize(eo, w, h);
109         }
110
111         return eo;
112 }
113
114 static Eina_Bool __key_press_cb(void *data, int type, void *event)
115 {
116         SETTING_TRACE_BEGIN;
117         Evas_Event_Key_Down *ev = event;
118         if (!ev || !data) {
119                 return ECORE_CALLBACK_RENEW;
120         }
121
122         support_display_appdata *ad = data;
123
124         if (strcmp(ev->keyname, KEY_HOME) == 0) {
125                 setting_ug_destroy(ad->ug);
126         }
127         return ECORE_CALLBACK_RENEW;
128 }
129
130 /**
131 * The function is called to create Setting view widgets
132 */
133 static bool support_display_app_create(void *data)
134 {
135         SETTING_TRACE_BEGIN;
136
137         support_display_appdata *ad = data;
138
139         SETTING_TRACE("[TIME] 3. it taked %d msec from main to setting_display_app_create ", appcore_measure_time());
140         appcore_measure_start();
141
142         /* create window */
143         ad->win_main = support_display_create_win("org.tizen.setting.display");
144         if (ad->win_main == NULL) {
145                 SETTING_TRACE("Can't create window");
146                 return 0;
147         }
148
149         if (elm_win_wm_rotation_supported_get(ad->win_main)) {
150                 int rots[4] = { 0, 90, 180, 270 };
151                 elm_win_wm_rotation_available_rotations_set(ad->win_main, rots, 4);
152         }
153
154         UG_INIT_EFL(ad->win_main, UG_OPT_INDICATOR_ENABLE);
155         ad->evas = evas_object_evas_get(ad->win_main);
156
157         elm_win_indicator_mode_set(ad->win_main, ELM_WIN_INDICATOR_SHOW);
158         elm_win_indicator_opacity_set(ad->win_main, ELM_WIN_INDICATOR_OPAQUE);
159
160         /* call ug */
161         Evas_Object *conform = elm_conformant_add(ad->win_main);
162         ad->ly_main = elm_layout_add(ad->win_main);
163         evas_object_size_hint_weight_set(ad->ly_main, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
164         elm_layout_theme_set(ad->ly_main, "layout", "application", "default");
165
166         /* add bg */
167         Evas_Object *bg = elm_bg_add(ad->ly_main);
168         elm_object_style_set(bg, "group_list");
169         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
170         elm_object_part_content_set(ad->ly_main, "elm.swallow.bg", bg);
171         evas_object_show(bg);
172
173         /* Indicator bg */
174         Evas_Object *indicator_bg = elm_bg_add(conform);
175         elm_object_style_set(indicator_bg, "indicator/headerbg");
176         elm_object_part_content_set(conform, "elm.swallow.indicator_bg", indicator_bg);
177         evas_object_show(indicator_bg);
178
179         evas_object_size_hint_weight_set(conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
180         evas_object_size_hint_align_set(conform, EVAS_HINT_FILL, EVAS_HINT_FILL);
181         elm_win_resize_object_add(ad->win_main, conform);
182         elm_object_content_set(conform, ad->ly_main);
183         evas_object_show(conform);
184         elm_win_conformant_set(ad->win_main, EINA_TRUE);
185
186         SETTING_TRACE("[TIME] 4. setting_main_app_create taked %d msec ", appcore_measure_time());
187         appcore_measure_start();
188
189         /* add event handler */
190         ad->event_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, __key_press_cb, ad);
191
192         return TRUE;
193 }
194
195 /**
196 * The function is called when Setting begins run in background from forground
197 */
198 static void support_display_app_pause(void *data)
199 {
200         SETTING_TRACE_BEGIN;
201         support_display_appdata *ad = data;
202
203         if (ad->ug) {
204                 ug_pause();
205                 /*ad->ug = NULL; */
206         }
207 }
208
209 /**
210 * The function is called when Setting begins run in forground from background
211 */
212 static void support_display_app_resume(void *data)
213 {
214         SETTING_TRACE_BEGIN;
215         support_display_appdata *ad = data;
216
217         if (ad->ug)
218                 ug_resume();
219 }
220
221 void support_display_app_destroy_ug_cb(ui_gadget_h ug, void *priv)
222 {
223         support_display_appdata *ad = (support_display_appdata *) priv;
224         if (!ad)
225                 return;         /*  do nothing if ad is NULL */
226
227         elm_win_lower(ad->win_main);
228
229         if (ug) {
230                 setting_ug_destroy(ug);
231                 ad->ug = NULL;
232         }
233 }
234
235 void support_display_app_end_ug_cb(ui_gadget_h ug, void *priv)
236 {
237         if (!ug)
238                 return;
239         support_display_appdata *ad = (support_display_appdata *) priv;
240         if (!ad)
241                 return;         /*  do nothing if ad is NULL */
242         support_display_app_terminate(ad);
243 }
244
245 void support_display_app_layout_ug_cb(ui_gadget_h ug, enum ug_mode mode,
246                                       void *priv)
247 {
248         Evas_Object *base;
249
250         if (!priv)
251                 return;
252
253         base = (Evas_Object *) ug_get_layout(ug);
254         if (!base)
255                 return;
256
257         switch (mode) {
258                 case UG_MODE_FULLVIEW:
259                         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
260                                                          EVAS_HINT_EXPAND);
261                         ug_disable_effect(ug);
262                         evas_object_show(base);
263                         break;
264                 default:
265                         break;
266         }
267 }
268
269 static int _set;
270 EXPORT_PUBLIC void update_lang(void)
271 {
272         char *lang;
273         char *r;
274
275         lang = vconf_get_str(VCONFKEY_LANGSET);
276         if (lang) {
277                 setenv("LANG", lang, 1);
278                 setenv("LC_MESSAGES", lang, 1);
279                 r = setlocale(LC_ALL, "");
280                 if (r == NULL) {
281                         setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET));
282                 }
283                 free(lang);
284         }
285 }
286
287 EXPORT_PUBLIC void update_region(void)
288 {
289         char *region;
290
291         region = vconf_get_str(VCONFKEY_REGIONFORMAT);
292         if (region) {
293                 setenv("LC_CTYPE", region, 1);
294                 setenv("LC_NUMERIC", region, 1);
295                 setenv("LC_TIME", region, 1);
296                 setenv("LC_COLLATE", region, 1);
297                 setenv("LC_MONETARY", region, 1);
298                 setenv("LC_PAPER", region, 1);
299                 setenv("LC_NAME", region, 1);
300                 setenv("LC_ADDRESS", region, 1);
301                 setenv("LC_TELEPHONE", region, 1);
302                 setenv("LC_MEASUREMENT", region, 1);
303                 setenv("LC_IDENTIFICATION", region, 1);
304                 free(region);
305         }
306 }
307
308 static int __set_i18n(const char *domain, const char *dir)
309 {
310         char *r;
311         if (domain == NULL) {
312                 errno = EINVAL;
313                 return -1;
314         }
315
316         r = setlocale(LC_ALL, "");
317         /* if locale is not set properly, try again to set as language base */
318         if (r == NULL) {
319                 r = setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET));
320         }
321         bindtextdomain(domain, dir);
322         textdomain(domain);
323         return 0;
324 }
325
326 int set_i18n(char *pkgname, char *localedir)
327 /*int set_i18n(const char *domainname, const char *dirname) */
328 {
329         if (_set)
330                 return 0;
331         update_lang();
332         update_region();
333
334         return __set_i18n(pkgname, localedir);
335 }
336
337 /**
338 * The function is called by app-fwk after app_create. It always do the process which cost much time.
339 */
340 static void support_display_app_reset(app_control_h service, void *data)
341 {
342         SETTING_TRACE_BEGIN;
343         support_display_appdata *ad = data;
344
345         app_control_h svc;
346
347         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
348         if (!cbs) {
349                 support_display_app_terminate(ad);
350                 return;
351         }
352
353         cbs->layout_cb = support_display_app_layout_ug_cb;
354         cbs->result_cb = NULL;
355         cbs->destroy_cb = support_display_app_destroy_ug_cb;
356         cbs->end_cb = support_display_app_end_ug_cb;
357         cbs->priv = (void *)ad;
358
359         if (app_control_create(&svc)) {
360                 FREE(cbs);
361                 support_display_app_terminate(ad);
362                 return;
363         }
364         app_control_add_extra_data(svc, "viewtype", "manage-applications");
365         ad->ug = setting_ug_create(NULL, "setting-manage-applications-efl", UG_MODE_FULLVIEW, svc, cbs);
366
367         app_control_destroy(svc);
368
369         if (!ad->ug) {
370                 evas_object_show(ad->ly_main);
371                 if (cbs) {
372                         FREE(cbs);
373                         support_display_app_terminate(ad);
374                         return;
375                 }
376         }
377
378         if (ad->win_main) {
379                 evas_object_show(ad->win_main);
380                 elm_win_activate(ad->win_main);
381         }
382         if (cbs)
383                 FREE(cbs);
384 }
385
386
387 static void support_display_app_lang_changed(void *data)
388 {
389         SETTING_TRACE_BEGIN;
390         support_display_appdata *ad = data;
391         char *lang = NULL;
392
393         lang = vconf_get_str(VCONFKEY_LANGSET);
394         if (lang) {
395                 elm_language_set((const char *)lang);
396                 FREE(lang);
397         }
398
399         if (ad->ug)
400                 ug_send_event(UG_EVENT_LANG_CHANGE);
401 }
402
403 EXPORT_PUBLIC
404 int main(int argc, char *argv[])
405 {
406         SETTING_TRACE_BEGIN;
407         int r = 0;
408         support_display_appdata ad;
409
410         app_event_callback_s ops = {
411                 .create = support_display_app_create,
412                 .terminate = support_display_app_terminate,
413                 .pause = support_display_app_pause,
414                 .resume = support_display_app_resume,
415                 .app_control = support_display_app_reset,
416                 .low_battery = NULL,
417                 .language_changed = support_display_app_lang_changed,
418                 .region_format_changed = NULL,
419                 .device_orientation = NULL,
420         };
421
422         memset(&ad, 0x00, sizeof(support_display_appdata));
423
424         r = app_efl_main(&argc, &argv, &ops, &ad);
425         SETTING_TRACE_DEBUG("r = %d", r);
426
427         if (r == -1) {
428                 SETTING_TRACE_ERROR("app_efl_main() returns -1");
429                 return -1;
430         }
431
432         return 0;
433 }