fix for applying wayland (removing X)
[apps/home/settings.git] / src / setting-support-shortcut / support_help_ringtone.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 <appcore-efl.h>
27 #include <appcore-common.h>
28 #include <vconf.h>
29 #include <feedback.h>
30 #include <dd-led.h>
31 #include <device.h>
32 #include <efl_assist.h>
33 #include <setting-common-general-func.h>
34
35 #define EXPORT_PUBLIC __attribute__((visibility("default")))
36
37
38 typedef struct _support_help_appdata {
39
40         ui_gadget_h ug;
41
42         Evas *evas;
43         Evas_Object *win_main;
44         Evas_Object *navibar_main;
45         Evas_Object *ly_main;                   /**< seting view main */
46         app_control_h service;
47         Ecore_Event_Handler *event_handler;
48 } support_help_appdata;
49
50 /**
51 * The function is called when Setting is terminated
52 */
53 static void support_help_app_terminate(void *data)
54 {
55         SETTING_TRACE_BEGIN;
56         support_help_appdata *ad = data;
57
58         SETTING_TRACE("support_help_terminate!");
59
60         if (ad->event_handler) {
61                 ecore_event_handler_del(ad->event_handler);
62                 ad->event_handler = NULL;
63         }
64
65         if (ad->ly_main) {
66                 evas_object_del(ad->ly_main);
67                 ad->ly_main = NULL;
68         }
69
70         if (ad->win_main) {
71                 evas_object_del(ad->win_main);
72                 ad->win_main = NULL;
73         }
74         elm_exit();
75
76         SETTING_TRACE_END;
77 }
78
79 /**
80 * The event process when win object is destroyed
81 */
82 static void support_help_del_win(void *data, Evas_Object *obj, void *event)
83 {
84         /*ui_app_exit(); */
85         elm_exit();
86 }
87
88 /**
89 * To create a win object, the win is shared between the App and all its UGs
90 */
91 static Evas_Object *support_help_create_win(const char *name)
92 {
93         SETTING_TRACE_BEGIN;
94         Evas_Object *eo;
95         int w, h;
96
97         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
98
99         if (eo) {
100                 elm_win_title_set(eo, name);
101                 elm_win_borderless_set(eo, EINA_TRUE);
102                 evas_object_smart_callback_add(eo, "delete,request",
103                                                support_help_del_win, NULL);
104                 #if 0
105                 ecore_x_window_size_get(ecore_x_window_root_first_get(),
106                                         &w, &h);
107                 evas_object_resize(eo, w, h);
108                 #endif
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_help_appdata *ad = data;
123
124 #if 0
125         if (strcmp(ev->keyname, KEY_HOME) == 0) {
126                 setting_ug_destroy(ad->ug);
127         }
128 #endif
129         return ECORE_CALLBACK_RENEW;
130 }
131
132 /**
133 * The function is called to create Setting view widgets
134 */
135 static bool support_help_app_create(void *data)
136 {
137         SETTING_TRACE_BEGIN;
138
139         support_help_appdata *ad = data;
140
141         SETTING_TRACE("[TIME] 3. it taked %d msec from main to setting_help_app_create ", appcore_measure_time());
142         appcore_measure_start();
143
144         /* create window */
145         ad->win_main = support_help_create_win("org.tizen.setting.helpringtone");
146         if (ad->win_main == NULL) {
147                 SETTING_TRACE("Can't create window");
148                 return 0;
149         }
150
151         if (elm_win_wm_rotation_supported_get(ad->win_main)) {
152                 int rots[4] = { 0, 90, 180, 270 };
153                 elm_win_wm_rotation_available_rotations_set(ad->win_main, rots, 4);
154         }
155
156         UG_INIT_EFL(ad->win_main, UG_OPT_INDICATOR_ENABLE);
157         ad->evas = evas_object_evas_get(ad->win_main);
158
159         elm_win_indicator_mode_set(ad->win_main, ELM_WIN_INDICATOR_SHOW);
160         elm_win_indicator_opacity_set(ad->win_main, ELM_WIN_INDICATOR_OPAQUE);
161
162         /* call ug */
163         Evas_Object *conform = elm_conformant_add(ad->win_main);
164         ad->ly_main = elm_layout_add(ad->win_main);
165         evas_object_size_hint_weight_set(ad->ly_main, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
166         elm_layout_theme_set(ad->ly_main, "layout", "application", "default");
167
168         /* add bg */
169         Evas_Object *bg = elm_bg_add(ad->ly_main);
170         elm_object_style_set(bg, "group_list");
171         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
172         elm_object_part_content_set(ad->ly_main, "elm.swallow.bg", bg);
173         evas_object_show(bg);
174
175         /* Indicator bg */
176         Evas_Object *indicator_bg = elm_bg_add(conform);
177         elm_object_style_set(indicator_bg, "indicator/headerbg");
178         elm_object_part_content_set(conform, "elm.swallow.indicator_bg", indicator_bg);
179         evas_object_show(indicator_bg);
180
181         evas_object_size_hint_weight_set(conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
182         evas_object_size_hint_align_set(conform, EVAS_HINT_FILL, EVAS_HINT_FILL);
183         elm_win_resize_object_add(ad->win_main, conform);
184         elm_object_content_set(conform, ad->ly_main);
185         evas_object_show(conform);
186         elm_win_conformant_set(ad->win_main, EINA_TRUE);
187
188         SETTING_TRACE("[TIME] 4. setting_main_app_create taked %d msec ", appcore_measure_time());
189         appcore_measure_start();
190
191         /* add event handler */
192         ad->event_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, __key_press_cb, ad);
193
194         return TRUE;
195 }
196
197 /**
198 * The function is called when Setting begins run in background from forground
199 */
200 static void support_help_app_pause(void *data)
201 {
202         SETTING_TRACE_BEGIN;
203         support_help_appdata *ad = data;
204
205         if (ad->ug) {
206                 ug_pause();
207                 /*ad->ug = NULL; */
208         }
209 }
210
211 /**
212 * The function is called when Setting begins run in forground from background
213 */
214 static void support_help_app_resume(void *data)
215 {
216         SETTING_TRACE_BEGIN;
217         support_help_appdata *ad = data;
218
219         if (ad->ug)
220                 ug_resume();
221 }
222
223 void support_help_app_destroy_ug_cb(ui_gadget_h ug, void *priv)
224 {
225         support_help_appdata *ad = (support_help_appdata *) priv;
226         if (!ad)
227                 return;         /*  do nothing if ad is NULL */
228
229         elm_win_lower(ad->win_main);
230
231         if (ug) {
232                 setting_ug_destroy(ug);
233                 ad->ug = NULL;
234         }
235 }
236
237 void support_help_app_end_ug_cb(ui_gadget_h ug, void *priv)
238 {
239         if (!ug)
240                 return;
241         support_help_appdata *ad = (support_help_appdata *) priv;
242         if (!ad)
243                 return;         /*  do nothing if ad is NULL */
244         support_help_app_terminate(ad);
245 }
246
247 void support_help_app_layout_ug_cb(ui_gadget_h ug, enum ug_mode mode,
248                                    void *priv)
249 {
250         Evas_Object *base;
251
252         if (!priv)
253                 return;
254
255         base = (Evas_Object *) ug_get_layout(ug);
256         if (!base)
257                 return;
258
259         switch (mode) {
260                 case UG_MODE_FULLVIEW:
261                         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
262                                                          EVAS_HINT_EXPAND);
263                         ug_disable_effect(ug);
264                         evas_object_show(base);
265                         break;
266                 default:
267                         break;
268         }
269 }
270
271 static int _set;
272 EXPORT_PUBLIC void update_lang(void)
273 {
274         char *lang;
275         char *r;
276
277         lang = vconf_get_str(VCONFKEY_LANGSET);
278         if (lang) {
279                 setenv("LANG", lang, 1);
280                 setenv("LC_MESSAGES", lang, 1);
281                 r = setlocale(LC_ALL, "");
282                 if (r == NULL) {
283                         setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET));
284                 }
285                 free(lang);
286         }
287 }
288
289 EXPORT_PUBLIC void update_region(void)
290 {
291         char *region;
292
293         region = vconf_get_str(VCONFKEY_REGIONFORMAT);
294         if (region) {
295                 setenv("LC_CTYPE", region, 1);
296                 setenv("LC_NUMERIC", region, 1);
297                 setenv("LC_TIME", region, 1);
298                 setenv("LC_COLLATE", region, 1);
299                 setenv("LC_MONETARY", region, 1);
300                 setenv("LC_PAPER", region, 1);
301                 setenv("LC_NAME", region, 1);
302                 setenv("LC_ADDRESS", region, 1);
303                 setenv("LC_TELEPHONE", region, 1);
304                 setenv("LC_MEASUREMENT", region, 1);
305                 setenv("LC_IDENTIFICATION", region, 1);
306                 free(region);
307         }
308 }
309
310 static int __set_i18n(const char *domain, const char *dir)
311 {
312         char *r;
313         if (domain == NULL) {
314                 errno = EINVAL;
315                 return -1;
316         }
317
318         r = setlocale(LC_ALL, "");
319         /* if locale is not set properly, try again to set as language base */
320         if (r == NULL) {
321                 r = setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET));
322         }
323         bindtextdomain(domain, dir);
324         textdomain(domain);
325         return 0;
326 }
327
328 int set_i18n(char *pkgname, char *localedir)
329 /*int set_i18n(const char *domainname, const char *dirname) */
330 {
331         if (_set)
332                 return 0;
333         update_lang();
334         update_region();
335
336         return __set_i18n(pkgname, localedir);
337 }
338
339 /**
340 * The function is called by app-fwk after app_create. It always do the process which cost much time.
341 */
342 static void support_help_app_reset(app_control_h service, void *data)
343 {
344         SETTING_TRACE_BEGIN;
345         support_help_appdata *ad = data;
346
347         app_control_h svc;
348
349         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
350         if (!cbs) {
351                 support_help_app_terminate(ad);
352                 return;
353         }
354
355         cbs->layout_cb = support_help_app_layout_ug_cb;
356         cbs->result_cb = NULL;
357         cbs->destroy_cb = support_help_app_destroy_ug_cb;
358         cbs->end_cb = support_help_app_end_ug_cb;
359         cbs->priv = (void *)ad;
360
361         if (app_control_create(&svc)) {
362                 FREE(cbs);
363                 support_help_app_terminate(ad);
364                 return;
365         }
366
367         app_control_set_uri(svc, "tizen-help://org.tizen.setting/ringtone");
368
369         char *cur_ringtone_path = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR);
370         if (cur_ringtone_path == NULL)
371                 cur_ringtone_path = (char *)strdup("/opt/usr/share/settings/Ringtones/Over_the_horizon.ogg");
372         app_control_add_extra_data(svc, "title", "IDS_ST_HEADER_RINGTONES");
373         app_control_add_extra_data(svc, "domain", "setting");
374         app_control_add_extra_data(svc, "path", "/opt/usr/share/settings/Ringtones");
375         app_control_add_extra_data(svc, "select_type", "SINGLE_FILE");
376         app_control_add_extra_data(svc, "file_type", "SOUND");
377         app_control_add_extra_data(svc, "drm_type", "DRM_ALL");
378         app_control_add_extra_data(svc, "silent", "silent_show");
379         if (safeStrCmp(cur_ringtone_path, "silent") == 0)
380                 app_control_add_extra_data(svc, "marked_mode", "silent");
381         else
382                 app_control_add_extra_data(svc, "marked_mode", cur_ringtone_path);
383         ad->ug = setting_ug_create(NULL, "myfile-efl", UG_MODE_FULLVIEW, svc, cbs);
384
385         app_control_destroy(svc);
386
387         if (!ad->ug) {
388                 evas_object_show(ad->ly_main);
389                 if (cbs) {
390                         FREE(cbs);
391                         FREE(cur_ringtone_path);
392                         support_help_app_terminate(ad);
393                         return;
394                 }
395         }
396
397         if (ad->win_main) {
398                 evas_object_show(ad->win_main);
399                 elm_win_activate(ad->win_main);
400         }
401         if (cbs)
402                 FREE(cbs);
403
404         FREE(cur_ringtone_path);
405 }
406
407
408 static void support_help_app_lang_changed(app_event_info_h event_info, void *data)
409 {
410         SETTING_TRACE_BEGIN;
411         support_help_appdata *ad = data;
412         char *lang = NULL;
413
414         lang = vconf_get_str(VCONFKEY_LANGSET);
415         if (lang) {
416                 elm_language_set((const char *)lang);
417                 FREE(lang);
418         }
419
420         if (ad->ug)
421                 ug_send_event(UG_EVENT_LANG_CHANGE);
422 }
423
424 EXPORT_PUBLIC
425 int main(int argc, char *argv[])
426 {
427         SETTING_TRACE_BEGIN;
428         int r = 0;
429         support_help_appdata ad;
430
431         ui_app_lifecycle_callback_s ops = {
432                 .create = support_help_app_create,
433                 .terminate = support_help_app_terminate,
434                 .pause = support_help_app_pause,
435                 .resume = support_help_app_resume,
436                 .app_control = support_help_app_reset,
437         };
438
439         app_event_handler_h handlers[5] = {NULL, };
440         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, NULL, NULL);
441         ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, support_help_app_lang_changed, NULL);
442         ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, NULL, NULL);
443         ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, NULL, NULL);
444
445         memset(&ad, 0x00, sizeof(support_help_appdata));
446
447         r = ui_app_main(argc, argv, &ops, &ad);
448         SETTING_TRACE_DEBUG("r = %d", r);
449
450         if (r == -1) {
451                 SETTING_TRACE_ERROR("ui_app_main() returns -1");
452                 return -1;
453         }
454
455         return 0;
456 }