Tizen 2.1 release
[platform/core/uifw/e17.git] / src / modules / conf / e_mod_main.c
1 #include "e.h"
2 #include "e_mod_main.h"
3
4 typedef struct _Instance Instance;
5 struct _Instance
6 {
7    E_Gadcon_Client *gcc;
8    Evas_Object     *o_toggle;
9 };
10
11 /* actual module specifics */
12
13 static void             _e_mod_action_conf_cb(E_Object *obj, const char *params);
14 static void             _e_mod_conf_cb(void *data, E_Menu *m, E_Menu_Item *mi);
15 static void             _e_mod_menu_add(void *data, E_Menu *m);
16 static void             _e_mod_run_cb(void *data, E_Menu *m, E_Menu_Item *mi);
17 static void             _config_pre_activate_cb(void *data, E_Menu *m);
18
19 /* gadcon requirements */
20 static E_Gadcon_Client *_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style);
21 static void             _gc_shutdown(E_Gadcon_Client *gcc);
22 static void             _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient);
23 static const char      *_gc_label(const E_Gadcon_Client_Class *client_class);
24 static Evas_Object     *_gc_icon(const E_Gadcon_Client_Class *client_class, Evas *evas);
25 static const char      *_gc_id_new(const E_Gadcon_Client_Class *client_class);
26 static void             _cb_action_conf(void *data, Evas_Object *obj, const char *emission, const char *source);
27
28 static void             _conf_new(void);
29 static void             _conf_free(void);
30
31 static E_Module *conf_module = NULL;
32 static E_Action *act = NULL;
33 static E_Int_Menu_Augmentation *maug = NULL;
34 static E_Config_DD *conf_edd = NULL;
35 Config *conf = NULL;
36
37 static Eina_List *instances = NULL;
38
39 /* and actually define the gadcon class that this module provides (just 1) */
40 static const E_Gadcon_Client_Class _gadcon_class =
41 {
42    GADCON_CLIENT_CLASS_VERSION, "configuration",
43    {
44       _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, NULL,
45       e_gadcon_site_is_not_toolbar
46    },
47    E_GADCON_CLIENT_STYLE_PLAIN
48 };
49
50 static E_Gadcon_Client *
51 _gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style)
52 {
53    Instance *inst;
54
55    inst = E_NEW(Instance, 1);
56    inst->o_toggle = edje_object_add(gc->evas);
57    e_theme_edje_object_set(inst->o_toggle,
58                            "base/theme/modules/conf",
59                            "e/modules/conf/main");
60
61    inst->gcc = e_gadcon_client_new(gc, name, id, style, inst->o_toggle);
62    inst->gcc->data = inst;
63
64    edje_object_signal_callback_add(inst->o_toggle, "e,action,conf", "",
65                                    _cb_action_conf, inst);
66
67    instances = eina_list_append(instances, inst);
68    e_gadcon_client_util_menu_attach(inst->gcc);
69
70    return inst->gcc;
71 }
72
73 static void
74 _gc_shutdown(E_Gadcon_Client *gcc)
75 {
76    Instance *inst;
77
78    if (!(inst = gcc->data)) return;
79    instances = eina_list_remove(instances, inst);
80    if (inst->o_toggle) evas_object_del(inst->o_toggle);
81    E_FREE(inst);
82 }
83
84 static void
85 _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient __UNUSED__)
86 {
87    Evas_Coord mw, mh;
88
89    edje_object_size_min_get(gcc->o_base, &mw, &mh);
90    if ((mw < 1) || (mh < 1))
91      edje_object_size_min_calc(gcc->o_base, &mw, &mh);
92    if (mw < 4) mw = 4;
93    if (mh < 4) mh = 4;
94    e_gadcon_client_aspect_set(gcc, mw, mh);
95    e_gadcon_client_min_size_set(gcc, mw, mh);
96 }
97
98 static const char *
99 _gc_label(const E_Gadcon_Client_Class *client_class __UNUSED__)
100 {
101    return _("Settings");
102 }
103
104 static Evas_Object *
105 _gc_icon(const E_Gadcon_Client_Class *client_class __UNUSED__, Evas *evas)
106 {
107    Evas_Object *o;
108    char buf[PATH_MAX];
109
110    o = edje_object_add(evas);
111    snprintf(buf, sizeof(buf), "%s/e-module-conf.edj",
112             e_module_dir_get(conf_module));
113    edje_object_file_set(o, buf, "icon");
114    return o;
115 }
116
117 static const char *
118 _gc_id_new(const E_Gadcon_Client_Class *client_class __UNUSED__)
119 {
120    return _gadcon_class.name;
121 }
122
123 /*
124    static void
125    _cb_button_click(void *data __UNUSED__, void *data2 __UNUSED__)
126    {
127    E_Action *a;
128
129    a = e_action_find("configuration");
130    if ((a) && (a->func.go)) a->func.go(NULL, NULL);
131    }
132  */
133
134 static void
135 _cb_action_conf(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
136 {
137    Instance *inst;
138    E_Action *a;
139
140    if (!(inst = data)) return;
141    a = e_action_find("configuration");
142    if ((a) && (a->func.go)) a->func.go(NULL, NULL);
143 }
144
145 static void
146 _e_mod_run_cb(void *data, E_Menu *m, E_Menu_Item *mi __UNUSED__)
147 {
148    Eina_List *l;
149    E_Configure_Cat *ecat;
150
151    EINA_LIST_FOREACH(e_configure_registry, l, ecat)
152      {
153         if ((ecat->pri >= 0) && (ecat->items))
154           {
155              E_Configure_It *eci;
156              Eina_List *ll;
157
158              EINA_LIST_FOREACH(ecat->items, ll, eci)
159                {
160                   char buf[1024];
161
162                   if ((eci->pri >= 0) && (eci == data))
163                     {
164                        snprintf(buf, sizeof(buf), "%s/%s", ecat->cat, eci->item);
165                        e_configure_registry_call(buf, m->zone->container, NULL);
166                     }
167                }
168           }
169      }
170 }
171
172 static void
173 _config_pre_activate_cb(void *data, E_Menu *m)
174 {
175    E_Configure_Cat *ecat = data;
176    E_Configure_It *eci;
177    Eina_List *l;
178    E_Menu_Item *mi;
179
180    e_menu_pre_activate_callback_set(m, NULL, NULL);
181
182    EINA_LIST_FOREACH(ecat->items, l, eci)
183      {
184         if (eci->pri >= 0)
185           {
186              mi = e_menu_item_new(m);
187              e_menu_item_label_set(mi, eci->label);
188              if (eci->icon)
189                {
190                   if (eci->icon_file)
191                     e_menu_item_icon_edje_set(mi, eci->icon_file, eci->icon);
192                   else
193                     e_util_menu_item_theme_icon_set(mi, eci->icon);
194                }
195              e_menu_item_callback_set(mi, _e_mod_run_cb, eci);
196           }
197      }
198 }
199
200 static void
201 _config_item_activate_cb(void *data, E_Menu *m, E_Menu_Item *mi __UNUSED__)
202 {
203    E_Configure_Cat *ecat = data;
204    e_configure_show(m->zone->container, ecat->cat);
205 }
206
207 static void
208 _config_all_pre_activate_cb(void *data __UNUSED__, E_Menu *m)
209 {
210    const Eina_List *l;
211    E_Configure_Cat *ecat;
212
213    e_menu_pre_activate_callback_set(m, NULL, NULL);
214
215    EINA_LIST_FOREACH(e_configure_registry, l, ecat)
216      {
217         E_Menu_Item *mi;
218         E_Menu *sub;
219
220         if ((ecat->pri < 0) || (!ecat->items)) continue;
221
222         mi = e_menu_item_new(m);
223         e_menu_item_label_set(mi, ecat->label);
224         if (ecat->icon)
225           {
226              if (ecat->icon_file)
227                e_menu_item_icon_edje_set(mi, ecat->icon_file, ecat->icon);
228              else
229                e_util_menu_item_theme_icon_set(mi, ecat->icon);
230           }
231         e_menu_item_callback_set(mi, _config_item_activate_cb, ecat);
232         sub = e_menu_new();
233         e_menu_item_submenu_set(mi, sub);
234         e_object_unref(E_OBJECT(sub));
235         e_menu_pre_activate_callback_set(sub, _config_pre_activate_cb, ecat);
236      }
237 }
238
239 /* menu item add hook */
240 void
241 e_mod_config_menu_add(void *data __UNUSED__, E_Menu *m)
242 {
243    E_Menu_Item *mi;
244    E_Menu *sub;
245
246    e_menu_pre_activate_callback_set(m, NULL, NULL);
247
248    sub = e_menu_new();
249    e_menu_pre_activate_callback_set(sub, _config_all_pre_activate_cb, NULL);
250
251    mi = e_menu_item_new(m);
252    e_menu_item_label_set(mi, _("All"));
253    e_menu_item_submenu_set(mi, sub);
254    e_object_unref(E_OBJECT(sub));
255 }
256
257 /* module setup */
258 EAPI E_Module_Api e_modapi = { E_MODULE_API_VERSION, "Conf" };
259
260 EAPI void *
261 e_modapi_init(E_Module *m)
262 {
263    char buf[PATH_MAX];
264
265    conf_module = m;
266
267    /* add module supplied action */
268    act = e_action_add("configuration");
269    if (act)
270      {
271         act->func.go = _e_mod_action_conf_cb;
272         e_action_predef_name_set(_("Launch"), _("Settings Panel"),
273                                  "configuration", NULL, NULL, 0);
274      }
275    maug =
276      e_int_menus_menu_augmentation_add_sorted("config/0", _("Settings Panel"),
277                                               _e_mod_menu_add, NULL, NULL, NULL);
278    e_module_delayed_set(m, 1);
279
280    snprintf(buf, sizeof(buf), "%s/e-module-conf.edj",
281             e_module_dir_get(conf_module));
282
283    e_configure_registry_category_add("advanced", 80, _("Advanced"),
284                                      NULL, "preferences-advanced");
285    e_configure_registry_item_add("advanced/conf", 110, _("Configuration Panel"),
286                                  NULL, buf, e_int_config_conf_module);
287
288    conf_edd = E_CONFIG_DD_NEW("Config", Config);
289 #undef T
290 #undef D
291 #define T Config
292 #define D conf_edd
293    E_CONFIG_VAL(D, T, version, INT);
294    E_CONFIG_VAL(D, T, menu_augmentation, INT);
295
296    conf = e_config_domain_load("module.conf", conf_edd);
297    if (conf)
298      {
299         if (!e_util_module_config_check("Configuration Panel", conf->version, MOD_CONFIG_FILE_VERSION))
300           _conf_free();
301      }
302
303    if (!conf) _conf_new();
304    conf->module = m;
305
306    if (conf->menu_augmentation)
307      {
308         conf->aug =
309           e_int_menus_menu_augmentation_add
310             ("config/2", e_mod_config_menu_add, NULL, NULL, NULL);
311      }
312
313    e_gadcon_provider_register(&_gadcon_class);
314    return m;
315 }
316
317 EAPI int
318 e_modapi_shutdown(E_Module *m __UNUSED__)
319 {
320    e_configure_del();
321
322    e_configure_registry_item_del("advanced/conf");
323    e_configure_registry_category_del("advanced");
324
325    if (conf->cfd) e_object_del(E_OBJECT(conf->cfd));
326    conf->cfd = NULL;
327
328    e_gadcon_provider_unregister(&_gadcon_class);
329
330    /* remove module-supplied menu additions */
331    if (maug)
332      {
333         e_int_menus_menu_augmentation_del("config/0", maug);
334         maug = NULL;
335      }
336    if (conf->aug)
337      {
338         e_int_menus_menu_augmentation_del("config/2", conf->aug);
339         conf->aug = NULL;
340      }
341
342    /* remove module-supplied action */
343    if (act)
344      {
345         e_action_predef_name_del(_("Launch"), _("Settings Panel"));
346         e_action_del("configuration");
347         act = NULL;
348      }
349    conf_module = NULL;
350
351    E_FREE(conf);
352    E_CONFIG_DD_FREE(conf_edd);
353
354    return 1;
355 }
356
357 EAPI int
358 e_modapi_save(E_Module *m __UNUSED__)
359 {
360    e_config_domain_save("module.conf", conf_edd, conf);
361    return 1;
362 }
363
364 /* action callback */
365 static void
366 _e_mod_action_conf_cb(E_Object *obj, const char *params)
367 {
368    E_Zone *zone = NULL;
369
370    if (obj)
371      {
372         if (obj->type == E_MANAGER_TYPE)
373           zone = e_util_zone_current_get((E_Manager *)obj);
374         else if (obj->type == E_CONTAINER_TYPE)
375           zone = e_util_zone_current_get(((E_Container *)obj)->manager);
376         else if (obj->type == E_ZONE_TYPE)
377           zone = ((E_Zone *)obj);
378         else
379           zone = e_util_zone_current_get(e_manager_current_get());
380      }
381    if (!zone) zone = e_util_zone_current_get(e_manager_current_get());
382    if ((zone) && (params))
383      e_configure_registry_call(params, zone->container, params);
384    else if (zone)
385      e_configure_show(zone->container, params);
386 }
387
388 /* menu item callback(s) */
389 static void
390 _e_mod_conf_cb(void *data __UNUSED__, E_Menu *m, E_Menu_Item *mi __UNUSED__)
391 {
392    e_configure_show(m->zone->container, NULL);
393 }
394
395 static void
396 _e_mod_mode_presentation_toggle(void *data __UNUSED__, E_Menu *m __UNUSED__, E_Menu_Item *mi)
397 {
398    e_config->mode.presentation = !e_config->mode.presentation;
399    e_menu_item_toggle_set(mi, e_config->mode.presentation);
400    e_config_mode_changed();
401    e_config_save_queue();
402 }
403
404 static void
405 _e_mod_mode_offline_toggle(void *data __UNUSED__, E_Menu *m __UNUSED__, E_Menu_Item *mi)
406 {
407    e_config->mode.offline = !e_config->mode.offline;
408    e_menu_item_toggle_set(mi, e_config->mode.offline);
409    e_config_mode_changed();
410    e_config_save_queue();
411 }
412
413 static void
414 _e_mod_submenu_modes_fill(void *data __UNUSED__, E_Menu *m)
415 {
416    E_Menu_Item *mi;
417
418    mi = e_menu_item_new(m);
419    e_menu_item_check_set(mi, 1);
420    e_menu_item_toggle_set(mi, e_config->mode.presentation);
421    e_menu_item_label_set(mi, _("Presentation"));
422    e_util_menu_item_theme_icon_set(mi, "preferences-modes-presentation");
423    e_menu_item_callback_set(mi, _e_mod_mode_presentation_toggle, NULL);
424
425    mi = e_menu_item_new(m);
426    e_menu_item_check_set(mi, 1);
427    e_menu_item_toggle_set(mi, e_config->mode.offline);
428    e_menu_item_label_set(mi, _("Offline"));
429    e_util_menu_item_theme_icon_set(mi, "preferences-modes-offline");
430    e_menu_item_callback_set(mi, _e_mod_mode_offline_toggle, NULL);
431
432    e_menu_pre_activate_callback_set(m, NULL, NULL);
433 }
434
435 static E_Menu *
436 _e_mod_submenu_modes_get(void)
437 {
438    E_Menu *m;
439
440    if (!(m = e_menu_new())) return NULL;
441    e_menu_pre_activate_callback_set(m, _e_mod_submenu_modes_fill, NULL);
442    return m;
443 }
444
445 /* menu item add hook */
446 static void
447 _e_mod_menu_add(void *data __UNUSED__, E_Menu *m)
448 {
449    E_Menu_Item *mi;
450
451    mi = e_menu_item_new(m);
452    e_menu_item_label_set(mi, _("Settings Panel"));
453    e_util_menu_item_theme_icon_set(mi, "preferences-system");
454    e_menu_item_callback_set(mi, _e_mod_conf_cb, NULL);
455
456    mi = e_menu_item_new(m);
457    e_menu_item_label_set(mi, _("Modes"));
458    e_util_menu_item_theme_icon_set(mi, "preferences-modes");
459    e_menu_item_submenu_set(mi, _e_mod_submenu_modes_get());
460    e_object_unref(E_OBJECT(mi->submenu));
461 }
462
463 static void
464 _conf_new(void)
465 {
466    conf = E_NEW(Config, 1);
467    conf->version = (MOD_CONFIG_FILE_EPOCH << 16);
468
469 #define IFMODCFG(v) if ((conf->version & 0xffff) < v) {
470 #define IFMODCFGEND }
471
472     IFMODCFG(0x008d);
473     conf->menu_augmentation = 1;
474     IFMODCFGEND;
475
476     conf->version = MOD_CONFIG_FILE_VERSION;
477     e_config_save_queue();
478 }
479
480 static void
481 _conf_free(void)
482 {
483    E_FREE(conf);
484 }