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