aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/e_configure.c
blob: ab598972c394956cf921550e7c4a1400cb62946a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include "e.h"

static void      _e_configure_menu_module_item_cb(void *data, E_Menu *m, E_Menu_Item *mi);
static void      _e_configure_menu_add(void *data, E_Menu *m);
static void      _e_configure_efreet_desktop_cleanup(void);
static void      _e_configure_efreet_desktop_update(void);
static Eina_Bool _e_configure_cb_efreet_desktop_cache_update(void *data, int type, void *event);
static void      _e_configure_registry_item_full_add(const char *path, int pri, const char *label, const char *icon_file, const char *icon, E_Config_Dialog *(*func)(Evas_Object *parent, const char *params), void (*generic_func)(Evas_Object *parent, const char *params), Efreet_Desktop *desktop, const char *params);
static void      _e_configure_registry_item_free(E_Configure_It *eci);

static void      _configure_job(void *data);
static Eina_Bool _configure_init_timer(void *data);

E_API Eina_List *e_configure_registry = NULL;

static Eina_List *handlers = NULL;
static E_Int_Menu_Augmentation *maug = NULL;
static Ecore_Job *update_job = NULL;

static struct
{
   void        (*func)(const void *data, const char *params, Efreet_Desktop *desktop);
   const char *data;
} custom_desktop_exec = { NULL, NULL };

EINTERN void
e_configure_init(void)
{
   e_configure_registry_category_add("extensions", 90, _("Extensions"), NULL, "preferences-extensions");
   e_configure_registry_item_add("extensions/modules", 10, _("Modules"), NULL, "preferences-plugin", e_int_config_modules);
   e_configure_registry_category_add("appearance", 10, _("Look"), NULL,
                                     "preferences-look");
   e_configure_registry_item_add("appearance/comp", 120, _("Compositor"), NULL, "preferences-composite", e_int_config_comp);
   e_configure_registry_category_add("internal", -1, _("Internal"),
                                     NULL, "enlightenment/internal");
   e_configure_registry_item_add("internal/comp_matches", -1, _("Composite Style Settings"),
                                 NULL, "preferences-composite", e_int_config_comp_match);
   maug = e_int_menus_menu_augmentation_add_sorted
       ("config/1", _("Modules"), _e_configure_menu_add, NULL, NULL, NULL);

   if (update_job)
     {
        ecore_job_del(update_job);
        update_job = NULL;
     }
   ecore_timer_loop_add(0.0, _configure_init_timer, NULL);
}

E_API void
e_configure_registry_call(const char *path, Evas_Object *parent, const char *params)
{
   E_Configure_Cat *ecat;
   Eina_List *l;
   char *cat;
   const char *item;

   /* path is "category/item" */
   cat = ecore_file_dir_get(path);
   if (!cat) return;
   item = ecore_file_file_get(path);
   EINA_LIST_FOREACH(e_configure_registry, l, ecat)
     if (!strcmp(cat, ecat->cat))
       {
          E_Configure_It *eci;
          Eina_List *ll;

          EINA_LIST_FOREACH(ecat->items, ll, eci)
            if (!strcmp(item, eci->item))
              {
                 if (!params) params = eci->params;

                 if (eci->func) eci->func(parent, params);
                 else if (eci->generic_func)
                   eci->generic_func(parent, params);
                 else if (eci->desktop)
                   {
                      if (custom_desktop_exec.func)
                        custom_desktop_exec.func(custom_desktop_exec.data,
                                                 params, eci->desktop);
                      else
                        e_exec(e_zone_current_get(),
                               eci->desktop, NULL, NULL, "config");
                   }
                 break;
              }
          break;
       }
   free(cat);
}

E_API void
e_configure_registry_item_add(const char *path, int pri, const char *label, const char *icon_file, const char *icon, E_Config_Dialog *(*func)(Evas_Object *parent, const char *params))
{
   _e_configure_registry_item_full_add(path, pri, label, icon_file, icon, func, NULL, NULL, NULL);
}

E_API void
e_configure_registry_generic_item_add(const char *path, int pri, const char *label, const char *icon_file, const char *icon, void (*generic_func)(Evas_Object *parent, const char *params))
{
   _e_configure_registry_item_full_add(path, pri, label, icon_file, icon, NULL, generic_func, NULL, NULL);
}

E_API void
e_configure_registry_item_params_add(const char *path, int pri, const char *label, const char *icon_file, const char *icon, E_Config_Dialog *(*func)(Evas_Object *parent, const char *params), const char *params)
{
   _e_configure_registry_item_full_add(path, pri, label, icon_file, icon, func, NULL, NULL, params);
}

/**
 *Delete an item in the configuration panel.
 *
 *@param path location the item to delete
 */
E_API void
e_configure_registry_item_del(const char *path)
{
   E_Configure_Cat *ecat;
   Eina_List *l;
   const char *item;
   char *cat;

   /* path is "category/item" */
   cat = ecore_file_dir_get(path);
   if (!cat) return;
   item = ecore_file_file_get(path);

   EINA_LIST_FOREACH(e_configure_registry, l, ecat)
     if (!strcmp(cat, ecat->cat))
       {
          E_Configure_It *eci;
          Eina_List *ll;

          EINA_LIST_FOREACH(ecat->items, ll, eci)
            if (!strcmp(item, eci->item))
              {
                 ecat->items = eina_list_remove_list(ecat->items, ll);

                 _e_configure_registry_item_free(eci);
                 break;
              }
          break;
       }
   free(cat);
}

/**
 *Add a category to the configuration panel.
 *
 *@param path location the new category
 *@param pri the priority for sorting the category in the category list
 *@param label the name the user will see in configuration panel
 *@param icon_file the edje file that holds the icon for the category.
 *Can be null to use current theme.
 *@param icon the name of the edje group to use as icon
 */
static int
_E_configure_category_pri_cb(E_Configure_Cat *ecat, E_Configure_Cat *ecat2)
{
   if (ecat->pri == ecat2->pri)
     return strcmp(ecat->label, ecat2->label);
   return ecat->pri - ecat2->pri;
}

E_API void
e_configure_registry_category_add(const char *path, int pri, const char *label, const char *icon_file, const char *icon)
{
   E_Configure_Cat *ecat2;
   E_Configure_Cat *ecat;
   Eina_List *l;

   /* if it exists - ignore this */
   EINA_LIST_FOREACH(e_configure_registry, l, ecat2)
     if (!strcmp(ecat2->cat, path)) return;

   ecat = E_NEW(E_Configure_Cat, 1);
   if (!ecat) return;

   ecat->cat = eina_stringshare_add(path);
   ecat->pri = pri;
   ecat->label = eina_stringshare_add(label);
   if (icon_file) ecat->icon_file = eina_stringshare_add(icon_file);
   if (icon) ecat->icon = eina_stringshare_add(icon);
   e_configure_registry = eina_list_sorted_insert(e_configure_registry,
                                                  EINA_COMPARE_CB(_E_configure_category_pri_cb),
                                                  ecat);
}

/**
 *Delete a category in the configuration panel.
 *
 *@param path location the category to delete
 */
E_API void
e_configure_registry_category_del(const char *path)
{
   E_Configure_Cat *ecat;
   Eina_List *l;
   char *cat;

   cat = ecore_file_dir_get(path);
   if (!cat) return;
   EINA_LIST_FOREACH(e_configure_registry, l, ecat)
     if (!strcmp(cat, ecat->cat))
       {
          if (ecat->items) break;
          e_configure_registry = eina_list_remove_list(e_configure_registry, l);
          eina_stringshare_del(ecat->cat);
          eina_stringshare_del(ecat->label);
          if (ecat->icon) eina_stringshare_del(ecat->icon);
          if (ecat->icon_file) eina_stringshare_del(ecat->icon_file);
          free(ecat);
          break;
       }
   free(cat);
}

/**
 *Add a item to the configuration panel.
 *
 *@param path location the location to place configuration item
 *@param pri the priority for sorting the item in the category list
 *@param label the name the user will see in configuration panel
 *@param icon_file the edje file that holds the icon for the category.
 *Can be null to use current theme.
 *@param icon the name of the edje group to use as icon
 *@param func the callback to use when the configuration item is clicked
 */

E_API void
e_configure_registry_custom_desktop_exec_callback_set(void (*func)(const void *data, const char *params, Efreet_Desktop *desktop), const void *data)
{
   custom_desktop_exec.func = func;
   custom_desktop_exec.data = data;
}

E_API int
e_configure_registry_exists(const char *path)
{
   E_Configure_Cat *ecat;
   Eina_List *l;
   char *cat;
   const char *item;
   int ret = 0;

   /* path is "category/item" */
   cat = ecore_file_dir_get(path);
   if (!cat) return 0;
   item = ecore_file_file_get(path);
   EINA_LIST_FOREACH(e_configure_registry, l, ecat)
     if (!strcmp(cat, ecat->cat))
       {
          E_Configure_It *eci;
          Eina_List *ll;

          if (!item)
            {
               ret = 1;
               break;
            }
          EINA_LIST_FOREACH(ecat->items, ll, eci)
            if (!strcmp(item, eci->item))
              {
                 ret = 1;
                 break;
              }
          break;
       }

   free(cat);
   return ret;
}

static void
_e_configure_menu_module_item_cb(void *data EINA_UNUSED, E_Menu *m EINA_UNUSED, E_Menu_Item *mi EINA_UNUSED)
{
   e_int_config_modules(NULL, NULL);
}

static void
_e_configure_menu_add(void *data EINA_UNUSED, E_Menu *m)
{
   E_Menu_Item *mi;

   mi = e_menu_item_new(m);
   e_menu_item_label_set(mi, _("Modules"));
   e_util_menu_item_theme_icon_set(mi, "preferences-plugin");
   e_menu_item_callback_set(mi, _e_configure_menu_module_item_cb, NULL);
}

static void
_configure_job(void *data EINA_UNUSED)
{
   _e_configure_efreet_desktop_update();
   update_job = NULL;
}

static Eina_Bool
_configure_init_timer(void *data EINA_UNUSED)
{
   handlers = eina_list_append
       (handlers, ecore_event_handler_add
         (EFREET_EVENT_DESKTOP_CACHE_UPDATE, _e_configure_cb_efreet_desktop_cache_update, NULL));
   if (update_job) ecore_job_del(update_job);
   update_job = ecore_job_add(_configure_job, NULL);
   return EINA_FALSE;
}

static void
_e_configure_efreet_desktop_cleanup(void)
{
   Eina_List *l;
   E_Configure_Cat *ecat;

//   printf("_e_configure_efreet_desktop_cleanup\n");
/* remove anything with a desktop entry */
   EINA_LIST_FOREACH(e_configure_registry, l, ecat)
     {
        E_Configure_It *eci;
        Eina_List *ll, *ln;

        EINA_LIST_FOREACH_SAFE(ecat->items, ll, ln, eci)
          if (eci->desktop)
            {
               _e_configure_registry_item_free(eci);
               ecat->items = eina_list_remove_list(ecat->items, ll);
            }
     }
}

static void
_e_configure_efreet_desktop_update(void)
{
   Eina_List *settings_desktops, *system_desktops;
   Efreet_Desktop *desktop;
   Eina_List *l;
   char buf[1024];

   /* get desktops */
   settings_desktops = efreet_util_desktop_category_list("Settings");
   system_desktops = efreet_util_desktop_category_list("System");
   if ((!settings_desktops) || (!system_desktops))
     {
        EINA_LIST_FREE(settings_desktops, desktop)
          efreet_desktop_free(desktop);
        EINA_LIST_FREE(system_desktops, desktop)
          efreet_desktop_free(desktop);
        return;
     }

   /* get ones in BOTH lists */
   EINA_LIST_FOREACH(settings_desktops, l, desktop)
     {
        char *s;
        char *cfg_cat_name;
        const char *cfg_cat_icon;
        char *cfg_cat;
        char *cfg_cat_cfg;
        const char *cfg_icon;
        char *label;
        int cfg_pri;
        int dopref;

        dopref = 0;
        cfg_cat = NULL;
        cfg_icon = NULL;
        cfg_cat_cfg = NULL;
        cfg_pri = 1000;
        cfg_cat_name = NULL;
        cfg_cat_icon = NULL;
        label = NULL;
        if (!eina_list_data_find(system_desktops, desktop))
          {
             /* settings desktop but not in system -> put in preferences */
             dopref = 1;
          }
        if (desktop->x)
          {
             cfg_cat_cfg = eina_hash_find(desktop->x, "X-Enlightenment-Config-Category");
             s = eina_hash_find(desktop->x, "X-Enlightenment-Config-Priority");
             if (s) cfg_pri = atoi(s);
             cfg_cat_name = eina_hash_find(desktop->x, "X-Enlightenment-Config-Category-Name");
             cfg_cat_icon = eina_hash_find(desktop->x, "X-Enlightenment-Config-Category-Icon");
             if ((cfg_cat_icon) && (cfg_cat_icon[0] != '/'))
               cfg_cat_icon = efreet_icon_path_find(e_config->icon_theme,
                                                    cfg_cat_icon, 64);
          }
        if (desktop->icon)
          {
             if (desktop->icon[0] == '/')
               cfg_icon = desktop->icon;
             else
               cfg_icon = efreet_icon_path_find(e_config->icon_theme,
                                                desktop->icon, 64);
          }
        if (desktop->name) label = desktop->name;
        else if (desktop->generic_name)
          label = desktop->generic_name;
        else label = "???";
        if (!cfg_cat_cfg)
          {
             const char *ic = cfg_cat_icon;

             if (dopref)
               {
                  snprintf(buf, sizeof(buf), "preferences/%s", label);
                  if (!ic) ic = "preferences-preferences";
                  e_configure_registry_category_add("preferences", 900,
                                                    _("Preferences"),
                                                    NULL, ic);
               }
             else
               {
                  snprintf(buf, sizeof(buf), "system/%s", label);
                  if (!ic) ic = "preferences-system";
                  e_configure_registry_category_add("system", 1000,
                                                    _("System"),
                                                    NULL, ic);
               }
             cfg_cat_cfg = buf;
          }
        else
          {
             cfg_cat = ecore_file_dir_get(cfg_cat_cfg);
             if (!cfg_cat) cfg_cat = strdup(cfg_cat_cfg);
             if (cfg_cat)
               {
                  if (!cfg_cat_name) cfg_cat_name = cfg_cat;
                  e_configure_registry_category_add(cfg_cat,
                                                    1000, cfg_cat_name,
                                                    NULL, cfg_cat_icon);
                  free(cfg_cat);
                  cfg_cat = NULL;
               }
          }
        _e_configure_registry_item_full_add(cfg_cat_cfg, cfg_pri, label,
                                            NULL, cfg_icon,
                                            NULL, NULL, desktop, NULL);
     }
   EINA_LIST_FREE(settings_desktops, desktop)
     efreet_desktop_free(desktop);
   EINA_LIST_FREE(system_desktops, desktop)
     efreet_desktop_free(desktop);
}

static Eina_Bool
_e_configure_cb_efreet_desktop_cache_update(void *data EINA_UNUSED, int type EINA_UNUSED, void *event EINA_UNUSED)
{
   _e_configure_efreet_desktop_cleanup();
   if (update_job) ecore_job_del(update_job);
   update_job = ecore_job_add(_configure_job, NULL);
   return 1;
}

static int
_e_configure_compare_cb(E_Configure_It *eci, E_Configure_It *eci2)
{
   return e_util_strcasecmp(eci->label, eci2->label);
}

static int
_e_configure_compare_pri_cb(E_Configure_It *eci, E_Configure_It *eci2)
{
   if (eci->pri == eci2->pri)
     return strcmp(eci->label, eci2->label);
   return eci->pri - eci2->pri;
}

static void
_e_configure_registry_item_full_add(const char *path, int pri, const char *label, const char *icon_file, const char *icon, E_Config_Dialog *(*func)(Evas_Object *parent, const char *params), void (*generic_func)(Evas_Object *parent, const char *params), Efreet_Desktop *desktop, const char *params)
{
   Eina_List *l;
   char *cat;
   const char *item;
   E_Configure_It *eci;
   E_Configure_Cat *ecat;
   Eina_Bool external;

   EINA_SAFETY_ON_NULL_RETURN(path);
   EINA_SAFETY_ON_NULL_RETURN(label);
   /* path is "category/item" */
   cat = ecore_file_dir_get(path);
   if (!cat) return;

   EINA_LIST_FOREACH(e_configure_registry, l, ecat)
     {
        if (strcmp(cat, ecat->cat)) continue;
        item = ecore_file_file_get(path);
        eci = E_NEW(E_Configure_It, 1);

        eci->item = eina_stringshare_add(item);
        eci->pri = pri;
        eci->label = eina_stringshare_add(label);
        if (icon_file) eci->icon_file = eina_stringshare_add(icon_file);
        if (icon) eci->icon = eina_stringshare_add(icon);
        if (params) eci->params = eina_stringshare_add(params);
        eci->func = func;
        eci->generic_func = generic_func;
        eci->desktop = desktop;
        if (eci->desktop) efreet_desktop_ref(eci->desktop);
        external = !strncmp(path, "preferences/", sizeof("preferences/") - 1);
        if (!external) external = !strncmp(path, "system/", sizeof("system/") - 1);
        if (external)
          ecat->items = eina_list_sorted_insert(ecat->items, EINA_COMPARE_CB(_e_configure_compare_cb), eci);
        else
          ecat->items = eina_list_sorted_insert(ecat->items, EINA_COMPARE_CB(_e_configure_compare_pri_cb), eci);
        break;
     }
   free(cat);
}

static void
_e_configure_registry_item_free(E_Configure_It *eci)
{
   eina_stringshare_del(eci->item);
   eina_stringshare_del(eci->label);
   eina_stringshare_del(eci->icon);
   if (eci->icon_file) eina_stringshare_del(eci->icon_file);
   if (eci->desktop) efreet_desktop_free(eci->desktop);
   if (eci->params) eina_stringshare_del(eci->params);
   free(eci);
}