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