2933c81729e87d1e46e9578fb11632a01cd3bdb8
[framework/uifw/e17.git] / src / modules / conf_theme / e_int_config_theme.c
1 #include "e.h"
2 #include "e_int_config_theme_import.h"
3 #include "e_int_config_theme_web.h"
4
5 static void *_create_data(E_Config_Dialog *cfd);
6 static void _free_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
7 static void _fill_data(E_Config_Dialog_Data *cfdata);
8 static int _basic_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
9 static Evas_Object *_basic_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
10 static int _advanced_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
11 static Evas_Object *_advanced_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
12 static Eina_List *_get_theme_categories_list(void);
13
14 struct _E_Config_Dialog_Data
15 {
16    E_Config_Dialog *cfd;
17
18    /* Basic */
19    Evas_Object *o_fm;
20    Evas_Object *o_up_button;
21    Evas_Object *o_preview;
22    Evas_Object *o_personal;
23    Evas_Object *o_system;
24    int fmdir;
25    const char *theme;
26
27    /* Advanced */
28    Evas_Object *o_categories_ilist;
29    Evas_Object *o_files_ilist;
30    int personal_file_count;
31    Eina_List *theme_list;
32    Eina_List *parts_list;
33
34    /* Dialog */
35    E_Win *win_import;
36    E_Dialog *dia_web;
37 };
38
39 static const char *parts_list[] = 
40 {
41    "about:e/widgets/about/main",
42      "borders:e/widgets/border/default/border",
43      "background:e/desktop/background",
44      "configure:e/widgets/configure/main",
45      "dialog:e/widgets/dialog/main",
46      "dnd:ZZZ",
47      "error:e/error/main",
48      "exebuf:e/widgets/exebuf/main",
49      "fileman:ZZZ",
50      "gadman:e/gadman/control",
51      "icons:ZZZ",
52      "menus:ZZZ",
53      "modules:ZZZ",
54      "modules/pager:e/widgets/pager/popup",
55      "modules/ibar:ZZZ",
56      "modules/ibox:ZZZ",
57      "modules/clock:e/modules/clock/main",
58      "modules/battery:e/modules/battery/main",
59      "modules/cpufreq:e/modules/cpufreq/main",
60      "modules/start:e/modules/start/main",
61      "modules/temperature:e/modules/temperature/main",
62      "pointer:e/pointer",
63      "shelf:e/shelf/default/base",
64      "transitions:ZZZ",
65      "widgets:ZZZ",
66      "winlist:e/widgets/winlist/main",
67      NULL
68 };
69
70 E_Config_Dialog *
71 e_int_config_theme(E_Container *con, const char *params __UNUSED__)
72 {
73    E_Config_Dialog *cfd;
74    E_Config_Dialog_View *v;
75
76    if (e_config_dialog_find("E", "appearance/theme")) return NULL;
77    v = E_NEW(E_Config_Dialog_View, 1);
78
79    v->create_cfdata = _create_data;
80    v->free_cfdata = _free_data;
81    v->basic.apply_cfdata = _basic_apply_data;
82    v->basic.create_widgets = _basic_create_widgets;
83    v->advanced.apply_cfdata = _advanced_apply_data;
84    v->advanced.create_widgets = _advanced_create_widgets;
85    v->override_auto_apply = 1;
86    cfd = e_config_dialog_new(con,
87                              _("Theme Selector"),
88                              "E", "appearance/theme",
89                              "preferences-desktop-theme", 0, v, NULL);
90    return cfd;
91 }
92
93 void
94 e_int_config_theme_import_done(E_Config_Dialog *dia)
95 {
96    E_Config_Dialog_Data *cfdata;
97
98    cfdata = dia->cfdata;
99    cfdata->win_import = NULL;
100 }
101
102 void
103 e_int_config_theme_web_done(E_Config_Dialog *dia)
104 {
105    E_Config_Dialog_Data *cfdata;
106
107    cfdata = dia->cfdata;
108    cfdata->dia_web = NULL;
109 }
110
111 void
112 e_int_config_theme_update(E_Config_Dialog *dia, char *file)
113 {
114    E_Config_Dialog_Data *cfdata;
115    char path[4096];
116
117    cfdata = dia->cfdata;
118
119    cfdata->fmdir = 1;
120    e_widget_radio_toggle_set(cfdata->o_personal, 1);
121
122    e_user_dir_concat_static(path, "themes");
123    eina_stringshare_del(cfdata->theme);
124    cfdata->theme = eina_stringshare_add(file);
125
126    if (cfdata->o_fm)
127      e_widget_flist_path_set(cfdata->o_fm, path, "/");
128
129    if (cfdata->o_preview)
130      e_widget_preview_edje_set(cfdata->o_preview, cfdata->theme, 
131                                "e/desktop/background");
132    if (cfdata->o_fm) e_widget_change(cfdata->o_fm);
133 }
134
135 static void
136 _cb_button_up(void *data1, void *data2 __UNUSED__)
137 {
138    E_Config_Dialog_Data *cfdata;
139
140    cfdata = data1;
141    if (cfdata->o_fm) e_widget_flist_parent_go(cfdata->o_fm);
142 }
143
144 static void
145 _cb_files_changed(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
146 {
147    E_Config_Dialog_Data *cfdata;
148
149    cfdata = data;
150    if (!cfdata->o_fm) return;
151    if (cfdata->o_up_button)
152      e_widget_disabled_set(cfdata->o_up_button, 
153                            !e_widget_flist_has_parent_get(cfdata->o_fm));
154 }
155
156 static void
157 _cb_files_selection_change(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
158 {
159    E_Config_Dialog_Data *cfdata;
160    Eina_List *selected;
161    E_Fm2_Icon_Info *ici;
162    const char *realpath;
163    char buf[4096];
164
165    cfdata = data;
166    if (!cfdata->o_fm) return;
167
168    if (!(selected = e_widget_flist_selected_list_get(cfdata->o_fm))) return;
169
170    ici = selected->data;
171    realpath = e_widget_flist_real_path_get(cfdata->o_fm);
172
173    if (!strcmp(realpath, "/"))
174      snprintf(buf, sizeof(buf), "/%s", ici->file);
175    else
176      snprintf(buf, sizeof(buf), "%s/%s", realpath, ici->file);
177    eina_list_free(selected);
178
179    if (ecore_file_is_dir(buf)) return;
180
181    eina_stringshare_del(cfdata->theme);
182    cfdata->theme = eina_stringshare_add(buf);
183    if (cfdata->o_preview)
184      e_widget_preview_edje_set(cfdata->o_preview, buf, "e/desktop/background");
185    if (cfdata->o_fm) e_widget_change(cfdata->o_fm);
186 }
187
188 #if 0
189 /* FIXME unused */
190 static void
191 _cb_files_selected(void *data, Evas_Object *obj, void *event_info)
192 {
193    E_Config_Dialog_Data *cfdata;
194
195    cfdata = data;
196 }
197 #endif
198
199 static void
200 _cb_files_files_changed(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
201 {
202    E_Config_Dialog_Data *cfdata;
203    const char *p;
204    char buf[4096];
205    size_t len;
206
207    cfdata = data;
208    if ((!cfdata->theme) || (!cfdata->o_fm)) return;
209
210    p = e_widget_flist_real_path_get(cfdata->o_fm);
211    if (p)
212      {
213         if (strncmp(p, cfdata->theme, strlen(p))) return;
214      }
215    if (!p) return;
216
217    len = e_user_dir_concat_static(buf, "themes");
218    if (!strncmp(cfdata->theme, buf, len))
219      p = cfdata->theme + len + 1;
220    else
221      {
222         len = e_prefix_data_concat_static(buf, "data/themes");
223         if (!strncmp(cfdata->theme, buf, len))
224           p = cfdata->theme + len + 1;
225         else
226           p = cfdata->theme;
227      }
228    e_widget_flist_select_set(cfdata->o_fm, p, 1);
229    e_widget_flist_file_show(cfdata->o_fm, p);
230 }
231
232 static void
233 _cb_dir(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
234 {
235    E_Config_Dialog_Data *cfdata;
236    char path[4096];
237
238    cfdata = data;
239    if (cfdata->fmdir == 1)
240      e_prefix_data_concat_static(path, "data/themes");
241    else
242      e_user_dir_concat_static(path, "themes");
243    e_widget_flist_path_set(cfdata->o_fm, path, "/");
244 }
245
246 static void
247 _cb_files_files_deleted(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
248 {
249    E_Config_Dialog_Data *cfdata;
250    Eina_List *sel, *all, *n;
251    E_Fm2_Icon_Info *ici, *ic;
252
253    cfdata = data;
254    if ((!cfdata->theme) || (!cfdata->o_fm)) return;
255
256    if (!(all = e_widget_flist_all_list_get(cfdata->o_fm))) return;
257    if (!(sel = e_widget_flist_selected_list_get(cfdata->o_fm))) return;
258
259    ici = sel->data;
260
261    all = eina_list_data_find_list(all, ici);
262    n = eina_list_next(all);
263    if (!n)
264      {
265         n = eina_list_prev(all);
266         if (!n) return;
267      }
268
269    if (!(ic = n->data)) return;
270
271    e_widget_flist_select_set(cfdata->o_fm, ic->file, 1);
272    e_widget_flist_file_show(cfdata->o_fm, ic->file);
273
274    eina_list_free(n);
275
276    evas_object_smart_callback_call(cfdata->o_fm, "selection_change", cfdata);
277 }
278
279 static void
280 _cb_import(void *data1, void *data2 __UNUSED__)
281 {
282    E_Config_Dialog_Data *cfdata;
283
284    cfdata = data1;
285    if (cfdata->win_import)
286      e_win_raise(cfdata->win_import);
287    else
288      cfdata->win_import = e_int_config_theme_import(cfdata->cfd);
289 }
290
291 #ifdef HAVE_EXCHANGE
292 static void
293 _cb_web(void *data1, void *data2 __UNUSED__)
294 {
295    E_Config_Dialog_Data *cfdata;
296
297    cfdata = data1;
298    if (cfdata->dia_web)
299      e_win_raise(cfdata->dia_web->win);
300    else
301      cfdata->dia_web = e_int_config_theme_web(cfdata->cfd);
302 }
303 #endif
304
305 static void
306 _fill_data(E_Config_Dialog_Data *cfdata)
307 {
308    E_Config_Theme * c;
309    char path[4096];
310    size_t len;
311
312    c = e_theme_config_get("theme");
313    if (c)
314      cfdata->theme = eina_stringshare_add(c->file);
315    else
316      {
317         e_prefix_data_concat_static(path, "data/themes/default.edj");
318         cfdata->theme = eina_stringshare_add(path);
319      }
320    if (cfdata->theme[0] != '/')
321      {
322         e_user_dir_snprintf(path, sizeof(path), "themes/%s", cfdata->theme);
323         if (ecore_file_exists(path))
324           {
325              eina_stringshare_del(cfdata->theme);
326              cfdata->theme = eina_stringshare_add(path);
327           }
328         else
329           {
330              e_prefix_data_snprintf(path, sizeof(path), "data/themes/%s",
331                                     cfdata->theme);
332              if (ecore_file_exists(path))
333                {
334                   eina_stringshare_del(cfdata->theme);
335                   cfdata->theme = eina_stringshare_add(path);
336                }
337           }
338      }
339
340    cfdata->theme_list = _get_theme_categories_list();
341
342    len = e_prefix_data_concat_static(path, "data/themes");
343    if (!strncmp(cfdata->theme, path, len))
344      cfdata->fmdir = 1;
345 }
346
347 static void *
348 _create_data(E_Config_Dialog *cfd)
349 {
350    E_Config_Dialog_Data *cfdata;
351
352    cfdata = E_NEW(E_Config_Dialog_Data, 1);
353    cfd->cfdata = cfdata;
354    cfdata->cfd = cfd;
355    _fill_data(cfdata);
356    return cfdata;
357 }
358
359 static void
360 _free_data(E_Config_Dialog *cfd __UNUSED__, E_Config_Dialog_Data *cfdata)
361 {
362    E_Config_Theme *t;
363
364    if (cfdata->win_import)
365      e_int_config_theme_del(cfdata->win_import);
366
367    EINA_LIST_FREE(cfdata->theme_list, t)
368      {
369         eina_stringshare_del(t->file);
370         eina_stringshare_del(t->category);
371         free(t);
372      }
373
374    E_FREE(cfdata);
375 }
376
377 static Evas_Object *
378 _basic_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata)
379 {
380    Evas_Object *o, *ot, *of, *il, *ol;
381    E_Zone *z;
382    E_Radio_Group *rg;
383    char path[4096];
384
385    z = e_zone_current_get(cfd->con);
386
387    ot = e_widget_table_add(evas, 0);
388    ol = e_widget_table_add(evas, 0);
389    il = e_widget_table_add(evas, 1);
390
391    rg = e_widget_radio_group_new(&(cfdata->fmdir));
392    o = e_widget_radio_add(evas, _("Personal"), 0, rg);
393    cfdata->o_personal = o;
394    evas_object_smart_callback_add(o, "changed", _cb_dir, cfdata);
395    e_widget_table_object_append(il, o, 0, 0, 1, 1, 1, 1, 0, 0);
396    o = e_widget_radio_add(evas, _("System"), 1, rg);
397    cfdata->o_system = o;
398    evas_object_smart_callback_add(o, "changed", _cb_dir, cfdata);
399    e_widget_table_object_append(il, o, 1, 0, 1, 1, 1, 1, 0, 0);
400
401    e_widget_table_object_append(ol, il, 0, 0, 1, 1, 0, 0, 0, 0);
402
403    o = e_widget_button_add(evas, _("Go up a Directory"), "go-up",
404                            _cb_button_up, cfdata, NULL);
405    cfdata->o_up_button = o;
406    e_widget_table_object_append(ol, o, 0, 1, 1, 1, 0, 0, 0, 0);
407
408    if (cfdata->fmdir == 1)
409      e_prefix_data_concat_static(path, "data/themes");
410    else
411      e_user_dir_concat_static(path, "themes");
412
413    o = e_widget_flist_add(evas);
414    cfdata->o_fm = o;
415    evas_object_smart_callback_add(o, "dir_changed",
416                                   _cb_files_changed, cfdata);
417    evas_object_smart_callback_add(o, "selection_change",
418                                   _cb_files_selection_change, cfdata);
419    evas_object_smart_callback_add(o, "changed",
420                                   _cb_files_files_changed, cfdata);
421    evas_object_smart_callback_add(o, "files_deleted",
422                                   _cb_files_files_deleted, cfdata);
423    e_widget_flist_path_set(o, path, "/");
424
425    e_widget_size_min_set(o, 160, 160);
426    e_widget_table_object_append(ol, o, 0, 2, 1, 1, 1, 1, 1, 1);
427    e_widget_table_object_append(ot, ol, 0, 0, 1, 1, 1, 1, 1, 1);
428
429    of = e_widget_list_add(evas, 0, 0);
430
431    il = e_widget_list_add(evas, 0, 1);
432    o = e_widget_button_add(evas, _(" Import..."), "preferences-desktop-theme",
433                            _cb_import, cfdata, NULL);
434    e_widget_list_object_append(il, o, 1, 0, 0.5);
435 #ifdef HAVE_EXCHANGE
436    o = e_widget_button_add(evas, _(" Online..."), "network-website",
437                            _cb_web, cfdata, NULL);
438    e_widget_list_object_append(il, o, 1, 0, 0.5);
439 #endif
440    e_widget_list_object_append(of, il, 1, 0, 0.0);
441
442    {
443       Evas_Object *oa;
444       int mw, mh;
445
446       mw = 320;
447       mh = (mw * z->h) / z->w;
448       oa = e_widget_aspect_add(evas, mw, mh);
449       o = e_widget_preview_add(evas, mw, mh);
450       cfdata->o_preview = o;
451       if (cfdata->theme)
452         e_widget_preview_edje_set(o, cfdata->theme, "e/desktop/background");
453       e_widget_aspect_child_set(oa, o);
454       e_widget_list_object_append(of, oa, 1, 1, 0);
455       evas_object_show(o);
456       evas_object_show(oa);
457    }
458    e_widget_table_object_append(ot, of, 1, 0, 1, 1, 1, 1, 1, 1);
459
460    e_dialog_resizable_set(cfd->dia, 1);
461    return ot;
462 }
463
464 static int
465 _basic_apply_data(E_Config_Dialog *cfd __UNUSED__, E_Config_Dialog_Data *cfdata)
466 {
467    E_Action *a;
468    E_Config_Theme *ct;
469
470    /* Actually take our cfdata settings and apply them in real life */
471    ct = e_theme_config_get("theme");
472    if ((ct) && (!strcmp(ct->file, cfdata->theme))) return 1;
473
474    e_theme_config_set("theme", cfdata->theme);
475    e_config_save_queue();
476
477    a = e_action_find("restart");
478    if ((a) && (a->func.go)) a->func.go(NULL, NULL);
479    return 1; /* Apply was OK */
480 }
481
482 /*
483  * --------------------------------------
484  * --- A D V A N C E D  S U P P O R T ---
485  * --------------------------------------
486  */
487
488 static int
489 _cb_sort(const void *data1, const void *data2)
490 {
491    const char *d1, *d2;
492
493    d1 = data1;
494    d2 = data2;
495    if (!d1) return 1;
496    if (!d2) return -1;
497
498    return strcmp(d1, d2);
499 }
500
501 static Eina_List *
502 _get_theme_categories_list(void)
503 {
504    Eina_List *themes, *tcl = NULL;
505    Eina_List *cats = NULL, *g = NULL, *cats2 = NULL;
506    const char *c;
507    char *category;
508
509    /* Setup some default theme categories */
510    cats = eina_list_append(cats, eina_stringshare_add("base/theme/about"));
511    cats = eina_list_append(cats, eina_stringshare_add("base/theme/borders"));
512    cats = eina_list_append(cats, eina_stringshare_add("base/theme/background"));
513    cats = eina_list_append(cats, eina_stringshare_add("base/theme/configure"));
514    cats = eina_list_append(cats, eina_stringshare_add("base/theme/dialog"));
515    cats = eina_list_append(cats, eina_stringshare_add("base/theme/dnd"));
516    cats = eina_list_append(cats, eina_stringshare_add("base/theme/error"));
517    cats = eina_list_append(cats, eina_stringshare_add("base/theme/exebuf"));
518    cats = eina_list_append(cats, eina_stringshare_add("base/theme/fileman"));
519    cats = eina_list_append(cats, eina_stringshare_add("base/theme/gadman"));
520    cats = eina_list_append(cats, eina_stringshare_add("base/theme/icons"));
521    cats = eina_list_append(cats, eina_stringshare_add("base/theme/menus"));
522    cats = eina_list_append(cats, eina_stringshare_add("base/theme/modules"));
523    cats = eina_list_append(cats, eina_stringshare_add("base/theme/modules/pager"));
524    cats = eina_list_append(cats, eina_stringshare_add("base/theme/modules/ibar"));
525    cats = eina_list_append(cats, eina_stringshare_add("base/theme/modules/ibox"));
526    cats = eina_list_append(cats, eina_stringshare_add("base/theme/modules/clock"));
527    cats = eina_list_append(cats, eina_stringshare_add("base/theme/modules/battery"));
528    cats = eina_list_append(cats, eina_stringshare_add("base/theme/modules/cpufreq"));
529    cats = eina_list_append(cats, eina_stringshare_add("base/theme/modules/start"));
530    cats = eina_list_append(cats, eina_stringshare_add("base/theme/modules/temperature"));
531    cats = eina_list_append(cats, eina_stringshare_add("base/theme/pointer"));
532    cats = eina_list_append(cats, eina_stringshare_add("base/theme/shelf"));
533    cats = eina_list_append(cats, eina_stringshare_add("base/theme/transitions"));
534    cats = eina_list_append(cats, eina_stringshare_add("base/theme/widgets"));
535    cats = eina_list_append(cats, eina_stringshare_add("base/theme/winlist"));
536
537    cats = eina_list_sort(cats, 0, _cb_sort);
538
539    /*
540     * Get a list of registered themes.
541     * Add those which are not in the above list
542     */
543    EINA_LIST_FOREACH(e_theme_category_list(), g, c)
544      {
545         int res;
546
547         if (!c) continue;
548
549         cats2 = eina_list_search_sorted_near_list(cats, _cb_sort, c, &res);
550         if (!res) continue;
551         if (res < 0)
552           cats = eina_list_prepend_relative_list(cats, eina_stringshare_ref(c), cats2);
553         else
554           cats = eina_list_append_relative_list(cats, eina_stringshare_ref(c), cats2);
555      }
556
557    EINA_LIST_FREE(cats, category)
558      {
559         E_Config_Theme *theme, *newtheme = NULL;
560
561         /* Not interested in adding "base" */
562         if (strcmp(category, "base"))
563           {
564              newtheme = (E_Config_Theme *)malloc(sizeof(E_Config_Theme));
565              if (!newtheme) break;
566              if (!strcmp(category, "base/theme"))
567                newtheme->category = eina_stringshare_add("base/theme/Base Theme");
568              else
569                newtheme->category = eina_stringshare_ref(category);
570              newtheme->file = NULL;
571
572              EINA_LIST_FOREACH(e_config->themes, themes, theme)
573                {
574                   if (!strcmp(category + 5, theme->category))
575                     {
576                        newtheme->file = eina_stringshare_add(theme->file);
577                     }
578                }
579              tcl = eina_list_append(tcl, newtheme);
580           }
581         eina_stringshare_del(category);
582      }
583
584    return tcl;
585 }
586
587 static const char *
588 _files_ilist_nth_label_to_file(void *data, int n)
589 {
590    E_Config_Dialog_Data *cfdata;
591    char file[1024];
592
593    if (!(cfdata = data)) return NULL;
594    if (!cfdata->o_files_ilist) return NULL;
595
596    if (n > cfdata->personal_file_count)
597      e_prefix_data_snprintf(file, sizeof(file), "data/themes/%s.edj",
598               e_widget_ilist_nth_label_get(cfdata->o_files_ilist, n));
599    else
600      e_user_dir_snprintf(file, sizeof(file), "themes/%s.edj",
601               e_widget_ilist_nth_label_get(cfdata->o_files_ilist, n));
602
603    return eina_stringshare_add(file);
604 }
605
606 static void
607 _preview_set(void *data)
608 {
609    E_Config_Dialog_Data *cfdata;
610    const char *theme;
611    char c_label[128];
612    int n;
613
614    if (!(cfdata = data)) return;
615
616    n = e_widget_ilist_selected_get(cfdata->o_files_ilist);
617    theme = _files_ilist_nth_label_to_file(cfdata, n);
618    snprintf(c_label, sizeof(c_label), "%s:",
619             e_widget_ilist_selected_label_get(cfdata->o_categories_ilist));
620    if (theme)
621      {
622         int ret = 0;
623         int i;
624
625         for (i = 0; parts_list[i]; i++)
626           if (strstr(parts_list[i], c_label)) break;
627
628         if (parts_list[i])
629           ret = e_widget_preview_edje_set(cfdata->o_preview, theme,
630                                           parts_list[i] + strlen(c_label));
631         if (!ret)
632           e_widget_preview_edje_set(cfdata->o_preview, theme,
633                                     "e/desktop/background");
634         eina_stringshare_del(theme);
635      }
636 }
637
638 static void
639 _cb_adv_categories_change(void *data, Evas_Object *obj __UNUSED__)
640 {
641    E_Config_Dialog_Data *cfdata;
642    const char *label = NULL;
643    const char *file = NULL;
644    char category[256];
645    Eina_List *themes = NULL;
646    E_Config_Theme *t;
647    Evas_Object *ic = NULL;
648    int n, cnt;
649
650    if (!(cfdata = data)) return;
651
652    label = e_widget_ilist_selected_label_get(cfdata->o_categories_ilist);
653    if (!label) return;
654
655    n = e_widget_ilist_selected_get(cfdata->o_categories_ilist);
656    ic = e_widget_ilist_nth_icon_get(cfdata->o_categories_ilist, n);
657    if (!ic)
658      {
659         _preview_set(data);
660         return;
661      }
662
663    snprintf(category, sizeof(category), "base/theme/%s", label);
664    EINA_LIST_FOREACH(cfdata->theme_list, themes, t)
665      {
666         if (!strcmp(category, t->category) && (t->file))
667           {
668              file = t->file;
669              break;
670           }
671      }
672    if (!file) return;
673
674    cnt = e_widget_ilist_count(cfdata->o_files_ilist);
675    for (n = 0; n < cnt; n++)
676      {
677         const char *tmp;
678
679         tmp = _files_ilist_nth_label_to_file(cfdata, n);
680         eina_stringshare_del(tmp);
681         if (file == tmp) /* We don't need the value, just the address. */
682           {
683              e_widget_ilist_selected_set(cfdata->o_files_ilist, n);
684              break;
685           }
686      }
687 }
688
689 static void
690 _cb_adv_theme_change(void *data, Evas_Object *obj __UNUSED__)
691 {
692    _preview_set(data);
693 }
694
695 static int
696 _theme_file_used(Eina_List *tlist, const char *filename)
697 {
698    E_Config_Theme *theme;
699    Eina_List *l;
700
701    if (!filename) return 0;
702
703    EINA_LIST_FOREACH(tlist, l, theme)
704      if (theme->file == filename) return 1;
705
706    return 0;
707 }
708
709 static void
710 _ilist_files_add(E_Config_Dialog_Data *cfdata,
711                  const char *header, const char *dir,
712                  int *count_cb)
713 {
714    Eina_Iterator *it;
715    const char *file;
716    Eina_List *themefiles = NULL;
717    int count = 0;
718    char *tmp;
719    Evas_Object *o;
720    const char *theme;
721    Evas *evas;
722
723    o = cfdata->o_files_ilist;
724    e_widget_ilist_header_append(o, NULL, header);
725    evas = evas_object_evas_get(o);
726
727    it = eina_file_ls(dir);
728
729    if (it)
730      {
731         EINA_ITERATOR_FOREACH(it, file)
732           if (strstr(file, ".edj"))
733             {
734                themefiles = eina_list_append(themefiles, file);
735             }
736           else
737             {
738                eina_stringshare_del(file);
739             }
740
741         eina_iterator_free(it);
742      }
743
744    if (themefiles)
745      {
746         char themename[PATH_MAX];
747
748         themefiles = eina_list_sort(themefiles, -1, _cb_sort);
749         count = eina_list_count(themefiles);
750
751         EINA_LIST_FREE(themefiles, theme)
752           {
753              Evas_Object *ic = NULL;
754
755              if (_theme_file_used(cfdata->theme_list, theme))
756                {
757                   ic = e_icon_add(evas);
758                   e_util_icon_theme_set(ic, "preferences-desktop-theme");
759                }
760              tmp = strdup(strrchr(theme, '/') + 1);
761              strncpy(themename, tmp, strlen(tmp) - 3);
762              themename[strlen(tmp) - 4] = '\0';
763              e_widget_ilist_append(o, ic, themename, NULL, NULL, NULL);
764              free(tmp);
765
766              eina_stringshare_del(theme);
767           }
768      }
769
770    if (count_cb)
771      *count_cb = count;
772 }
773
774 static void
775 _fill_files_ilist(E_Config_Dialog_Data *cfdata)
776 {
777    Evas *evas;
778    Evas_Object *o;
779    char theme_dir[4096];
780
781    if (!(o = cfdata->o_files_ilist)) return;
782
783    evas = evas_object_evas_get(o);
784    evas_event_freeze(evas);
785    edje_freeze();
786    e_widget_ilist_freeze(o);
787    e_widget_ilist_clear(o);
788
789    /* Grab the "Personal" themes. */
790    e_user_dir_concat_static(theme_dir, "themes");
791    _ilist_files_add(cfdata, _("Personal"), theme_dir, &cfdata->personal_file_count);
792
793    /* Grab the "System" themes. */
794    e_prefix_data_concat_static(theme_dir, "data/themes");
795    _ilist_files_add(cfdata, _("System"), theme_dir, NULL);
796
797    e_widget_ilist_go(o);
798    e_widget_ilist_thaw(o);
799    edje_thaw();
800    evas_event_thaw(evas);
801 }
802
803 static void
804 _fill_categories_ilist(E_Config_Dialog_Data *cfdata)
805 {
806    Evas *evas;
807    Eina_List *themes;
808    E_Config_Theme *theme;
809    Evas_Object *o;
810
811    if (!(o = cfdata->o_categories_ilist)) return;
812
813    evas = evas_object_evas_get(o);
814    evas_event_freeze(evas);
815    edje_freeze();
816    e_widget_ilist_freeze(o);
817    e_widget_ilist_clear(o);
818
819    EINA_LIST_FOREACH(cfdata->theme_list, themes, theme)
820      {
821         Evas_Object *ic = NULL;
822
823         if (theme->file)
824           {
825              ic = e_icon_add(evas);
826              e_util_icon_theme_set(ic, "dialog-ok-apply");
827           }
828         e_widget_ilist_append(o, ic, theme->category + 11, NULL, NULL, NULL);
829      }
830
831    e_widget_ilist_go(o);
832    e_widget_ilist_thaw(o);
833    edje_thaw();
834    evas_event_thaw(evas);
835 }
836
837 static void
838 _cb_adv_btn_assign(void *data1, void *data2 __UNUSED__)
839 {
840    Evas *evas;
841    E_Config_Dialog_Data *cfdata;
842    E_Config_Theme *newtheme, *t;
843    Eina_List *themes;
844    Evas_Object *ic = NULL, *oc = NULL, *of = NULL;
845    char buf[1024];
846    const char *label;
847    int n, cnt;
848
849    if (!(cfdata = data1)) return;
850
851    if (!(oc = cfdata->o_categories_ilist)) return;
852    if (!(of = cfdata->o_files_ilist)) return;
853
854    evas = evas_object_evas_get(oc);
855    n = e_widget_ilist_selected_get(oc);
856    ic = e_icon_add(evas);
857    e_util_icon_theme_set(ic, "enlightenment");
858    e_widget_ilist_nth_icon_set(oc, n, ic);
859
860    newtheme = malloc(sizeof(E_Config_Theme));
861    if (!newtheme) return;
862
863    label = e_widget_ilist_selected_label_get(oc);
864    snprintf(buf, sizeof(buf), "base/theme/%s", label);
865    newtheme->category = eina_stringshare_add(buf);
866
867    n = e_widget_ilist_selected_get(of);
868    ic = e_icon_add(evas);
869    e_util_icon_theme_set(ic, "preferences-desktop-theme");
870    e_widget_ilist_nth_icon_set(of, n, ic);
871    newtheme->file = _files_ilist_nth_label_to_file(cfdata, n);
872
873    EINA_LIST_FOREACH(cfdata->theme_list, themes, t)
874      {
875         const char *filename = NULL;
876
877         if (!strcmp(t->category, newtheme->category))
878           {
879              if ((t->file) && (strcmp(t->file, newtheme->file)))
880                {
881                   filename = t->file;
882                   t->file = NULL;
883
884                   if (!_theme_file_used(cfdata->theme_list, filename))
885                     {
886                        cnt = e_widget_ilist_count(of);
887                        for (n = 0; n < cnt; n++)
888                          {
889                             const char *tmp;
890
891                             tmp = _files_ilist_nth_label_to_file(cfdata, n);
892                             eina_stringshare_del(tmp);
893                             if (filename == tmp) /* We just need the pointer, not the value. */
894                               e_widget_ilist_nth_icon_set(of, n, NULL);
895                          }
896                     }
897                }
898              t->file = eina_stringshare_add(newtheme->file);
899              if (filename) eina_stringshare_del(filename);
900              break;
901           }
902      }
903    if (!themes)
904      cfdata->theme_list = eina_list_append(cfdata->theme_list, newtheme);
905    else
906      {
907         eina_stringshare_del(newtheme->category);
908         eina_stringshare_del(newtheme->file);
909         free(newtheme);
910      }
911
912    return;
913 }
914
915 static void
916 _cb_adv_btn_clear(void *data1, void *data2 __UNUSED__)
917 {
918    E_Config_Dialog_Data *cfdata;
919    E_Config_Theme *t;
920    Eina_List *themes;
921    Evas_Object *oc = NULL, *of = NULL;
922    char cat[1024];
923    const char *label;
924    const char *filename = NULL;
925    int n, cnt;
926
927    if (!(cfdata = data1)) return;
928
929    if (!(oc = cfdata->o_categories_ilist)) return;
930    if (!(of = cfdata->o_files_ilist)) return;
931
932    n = e_widget_ilist_selected_get(oc);
933    e_widget_ilist_nth_icon_set(oc, n, NULL);
934
935    label = e_widget_ilist_selected_label_get(oc);
936    snprintf(cat, sizeof(cat), "base/theme/%s", label);
937
938    EINA_LIST_FOREACH(cfdata->theme_list, themes, t)
939      {
940         if (!strcmp(t->category, cat))
941           {
942              if (t->file)
943                {
944                   filename = t->file;
945                   t->file = NULL;
946                }
947              break;
948           }
949      }
950
951    if ((filename) && (!_theme_file_used(cfdata->theme_list, filename)))
952      {
953         cnt = e_widget_ilist_count(of);
954         for (n = 0; n < cnt; n++)
955           {
956              const char *tmp;
957
958              tmp = _files_ilist_nth_label_to_file(cfdata, n);
959              if (filename == tmp)
960                e_widget_ilist_nth_icon_set(of, n, NULL);
961              eina_stringshare_del(tmp);
962           }
963         eina_stringshare_del(filename);
964      }
965
966    return;
967 }
968
969 static void
970 _cb_adv_btn_clearall(void *data1, void *data2 __UNUSED__)
971 {
972    E_Config_Dialog_Data *cfdata;
973    E_Config_Theme *t;
974    Eina_List *themes;
975    Evas_Object *oc = NULL, *of = NULL;
976    int n, cnt;
977
978    if (!(cfdata = data1)) return;
979
980    if (!(oc = cfdata->o_categories_ilist)) return;
981    if (!(of = cfdata->o_files_ilist)) return;
982
983    cnt = e_widget_ilist_count(oc);
984    for (n = 0; n < cnt; n++)
985      e_widget_ilist_nth_icon_set(oc, n, NULL);
986
987    cnt = e_widget_ilist_count(of);
988    for (n = 0; n < cnt; n++)
989      e_widget_ilist_nth_icon_set(of, n, NULL);
990
991    EINA_LIST_FOREACH(cfdata->theme_list, themes, t)
992      {
993         eina_stringshare_del(t->file);
994         t->file = NULL;
995      }
996
997    return;
998 }
999
1000 static Evas_Object *
1001 _advanced_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata)
1002 {
1003    Evas_Object *ot, *of, *ob, *oa, *ol;
1004    int mw, mh;
1005    E_Zone *zone;
1006
1007    zone = e_zone_current_get(cfd->con);
1008    ot = e_widget_table_add(evas, 0);
1009
1010    of = e_widget_framelist_add(evas, _("Theme Categories"), 0);
1011    ob = e_widget_ilist_add(evas, 16, 16, NULL);
1012    e_widget_on_change_hook_set(ob, _cb_adv_categories_change, cfdata);
1013    cfdata->o_categories_ilist = ob;
1014    e_widget_ilist_multi_select_set(ob, 0);
1015    e_widget_size_min_set(ob, 150, 250);
1016    e_widget_framelist_object_append(of, ob);
1017    e_widget_table_object_append(ot, of, 0, 0, 1, 1, 1, 1, 0, 1);
1018
1019    of = e_widget_framelist_add(evas, _("Themes"), 0);
1020    ob = e_widget_ilist_add(evas, 16, 16, NULL);
1021    e_widget_on_change_hook_set(ob, _cb_adv_theme_change, cfdata);
1022    cfdata->o_files_ilist = ob;
1023    e_widget_size_min_set(ob, 150, 250);
1024    e_widget_framelist_object_append(of, ob);
1025    e_widget_table_object_append(ot, of, 1, 0, 1, 1, 1, 1, 1, 1);
1026
1027    ol = e_widget_list_add(evas, 1, 1);
1028    ob = e_widget_button_add(evas, _("Assign"), NULL, 
1029                             _cb_adv_btn_assign, cfdata, NULL);
1030    e_widget_list_object_append(ol, ob, 1, 0, 0.5);
1031    ob = e_widget_button_add(evas, _("Clear"), NULL, 
1032                             _cb_adv_btn_clear, cfdata, NULL);
1033    e_widget_list_object_append(ol, ob, 1, 0, 0.5);
1034    ob = e_widget_button_add(evas, _("Clear All"), NULL, 
1035                             _cb_adv_btn_clearall, cfdata, NULL);
1036    e_widget_list_object_append(ol, ob, 1, 0, 0.5);
1037    e_widget_table_object_append(ot, ol, 0, 1, 1, 1, 1, 0, 0, 0);
1038
1039    of = e_widget_framelist_add(evas, _("Preview"), 0);
1040    mw = 320;
1041    mh = (mw * zone->h) / zone->w;
1042    oa = e_widget_aspect_add(evas, mw, mh);
1043    ob = e_widget_preview_add(evas, mw, mh);
1044    cfdata->o_preview = ob;
1045    if (cfdata->theme)
1046      e_widget_preview_edje_set(ob, cfdata->theme, "e/desktop/background");
1047    e_widget_aspect_child_set(oa, ob);
1048    e_widget_framelist_object_append(of, oa);
1049    e_widget_table_object_append(ot, of, 2, 0, 1, 1, 1, 1, 1, 1);
1050
1051    _fill_files_ilist(cfdata);
1052    _fill_categories_ilist(cfdata);
1053
1054    e_widget_ilist_selected_set(cfdata->o_files_ilist, 1);
1055    e_widget_ilist_selected_set(cfdata->o_categories_ilist, 0);
1056
1057    e_dialog_resizable_set(cfd->dia, 1);
1058    return ot;
1059 }
1060
1061 static int
1062 _advanced_apply_data(E_Config_Dialog *cfd __UNUSED__, E_Config_Dialog_Data *cfdata)
1063 {
1064    E_Config_Theme *theme;
1065    Eina_List *themes;
1066    E_Action *a;
1067
1068    EINA_LIST_FOREACH(cfdata->theme_list, themes, theme)
1069      {
1070         E_Config_Theme *ec_theme;
1071         Eina_List *ec_themes;
1072
1073         if (!strcmp(theme->category, "base/theme/Base Theme"))
1074           theme->category = eina_stringshare_add("base/theme");
1075
1076         EINA_LIST_FOREACH(e_config->themes, ec_themes, ec_theme)
1077           {
1078              if (!strcmp(theme->category + 5, ec_theme->category))
1079                {
1080                   if (theme->file)
1081                     e_theme_config_set(theme->category + 5, theme->file);
1082                   else
1083                     e_theme_config_remove(theme->category + 5);
1084                   break;
1085                }
1086           }
1087         if ((!ec_themes) && (theme->file))
1088           e_theme_config_set(theme->category + 5, theme->file);
1089      }
1090
1091    e_config_save_queue();
1092
1093    a = e_action_find("restart");
1094    if ((a) && (a->func.go)) a->func.go(NULL, NULL);
1095
1096    return 1;
1097 }