e835dd822fed7ddf5e6a1e7ccf50beb0e3fea805
[framework/uifw/e17.git] / src / modules / wizard / page_030.c
1 /* Menu chooser */
2 #include "e.h"
3 #include "e_mod_main.h"
4
5 static const char *xdg_sel = NULL;
6 static Eina_List *menus = NULL;
7
8 static void
9 check_menu_dir(const char *dir)
10 {
11    char buf[PATH_MAX], *file;
12    Eina_List *files;
13
14    snprintf(buf, sizeof(buf), "%s/menus", dir);
15    files = ecore_file_ls(buf);
16    EINA_LIST_FREE(files, file)
17      {
18         if (e_util_glob_match(file, "*.menu"))
19           {
20              snprintf(buf, sizeof(buf), "%s/menus/%s", dir, file);
21              menus = eina_list_append(menus, strdup(buf));
22           }
23         free(file);
24      }
25 }
26
27 EAPI int
28 wizard_page_init(E_Wizard_Page *pg __UNUSED__)
29 {
30    char buf[PATH_MAX];
31    const char *dirs[] = 
32      {
33         "/etc/xdg",
34           "/usr/etc/xdg",
35           "/usr/local/etc/xdg",
36           "/usr/opt/etc/xdg",
37           "/usr/opt/xdg",
38           "/usr/local/opt/etc/xdg",
39           "/usr/local/opt/xdg",
40           "/opt/etc/xdg",
41           "/opt/xdg",
42           // FIXME: add more "known locations"
43           NULL
44      };
45    int i, newdir;
46
47    e_user_homedir_concat(buf, sizeof(buf), ".config");
48    check_menu_dir(buf);
49    
50    for (i = 0; dirs[i]; i++) check_menu_dir(dirs[i]);
51    
52    newdir = 1;
53    snprintf(buf, sizeof(buf), "%s/etc/xdg", e_prefix_get());
54    for (i = 0; dirs[i]; i++)
55      {
56         if (!strcmp(dirs[i], buf))
57           {
58              newdir = 0;
59              break;
60           }
61      }
62    if (newdir) check_menu_dir(buf);
63    return 1;
64 }
65
66 EAPI int
67 wizard_page_shutdown(E_Wizard_Page *pg __UNUSED__)
68 {
69    // FIXME: free menus
70    return 1;
71 }
72
73 EAPI int
74 wizard_page_show(E_Wizard_Page *pg)
75 {
76    Evas_Object *o, *of, *ob;
77    char *file;
78    int sel = -1, i = 0;
79
80    o = e_widget_list_add(pg->evas, 1, 0);
81    e_wizard_title_set(_("Menus"));
82
83    if (!menus)
84      {
85         of = e_widget_framelist_add(pg->evas, _("Error"), 0);
86
87         ob = e_widget_textblock_add(pg->evas);
88         e_widget_size_min_set(ob, 140 * e_scale, 140 * e_scale);
89         e_widget_textblock_markup_set
90           (ob, 
91            _("No menu files were<br>"
92              "found on your system.<br>"
93              "Please see the<br>"
94              "documentation on<br>"
95              "www.enlightenment.org<br>"
96              "for more details on<br>"
97              "how to get your<br>"
98              "application menus<br>"
99              "working."));
100         e_widget_framelist_object_append(of, ob);
101         e_widget_list_object_append(o, of, 1, 1, 0.5);
102         evas_object_show(ob);
103         evas_object_show(of);
104      }
105    else
106      {
107         of = e_widget_framelist_add(pg->evas, _("Select application menu"), 0);
108
109         ob = e_widget_ilist_add(pg->evas, 32 * e_scale, 32 * e_scale, &xdg_sel);
110         e_widget_size_min_set(ob, 140 * e_scale, 140 * e_scale);
111
112         e_widget_ilist_freeze(ob);
113
114         EINA_LIST_FREE(menus, file)
115           {
116              char buf[PATH_MAX], buf2[PATH_MAX], *p, *p2, *tlabel, *tdesc;
117              const char *label;
118
119              label = file;
120              tlabel = NULL;
121              tdesc = NULL;
122              e_user_homedir_concat(buf, sizeof(buf),
123                                    ".config/menus/applications.menu");
124              snprintf(buf2, sizeof(buf2), 
125                       "%s/etc/xdg/menus/enlightenment.menu", 
126                       e_prefix_get());
127              if (!strcmp("/etc/xdg/menus/applications.menu", file))
128                {
129                   label = _("System Default");
130                   sel = i;
131                }
132              else if (!strcmp(buf2, file))
133                {
134                   label = _("Enlightenment Default");
135                   sel = i;
136                }
137              else if (!strcmp(buf, file))
138                {
139                   label = _("Personal Default");
140                }
141              else
142                {
143                   p = strrchr(file, '/');
144                   if (p)
145                     {
146                        p++;
147                        p2 = strchr(p, '-');
148                        if (!p2) p2 = strrchr(p, '.');
149                        if (p2)
150                          {
151                             tlabel = malloc(p2 - p + 1);
152                             if (tlabel)
153                               {
154                                  eina_strlcpy(tlabel, p, p2 - p + 1);
155                                  tlabel[0] = toupper(tlabel[0]);
156                                  if (*p2 == '-')
157                                    {
158                                       p2++;
159                                       p = strrchr(p2, '.');
160                                       if (p)
161                                         {
162                                            tdesc = malloc(p - p2 + 1);
163                                            if (tdesc)
164                                              {
165                                                 eina_strlcpy(tdesc, p2, p - p2 + 1);
166                                                 tdesc[0] = toupper(tdesc[0]);
167                                                 snprintf(buf, sizeof(buf), "%s (%s)", tlabel, tdesc);
168                                              }
169                                            else
170                                              snprintf(buf, sizeof(buf), "%s", tlabel);
171                                         }
172                                       else
173                                         snprintf(buf, sizeof(buf), "%s", tlabel);
174                                    }
175                                  else
176                                    snprintf(buf, sizeof(buf), "%s", tlabel);
177                                  label = buf;
178                               }
179                          }
180                        else
181                          label = p;
182                     }
183                }
184              e_widget_ilist_append(ob, NULL, label, NULL, NULL, file);
185              if (tlabel) free(tlabel);
186              if (tdesc) free(tdesc);
187              free(file);
188              i++;
189           }
190         e_widget_ilist_go(ob);
191         e_widget_ilist_thaw(ob);
192
193         if (sel >= 0) e_widget_ilist_selected_set(ob, sel);
194
195         e_widget_framelist_object_append(of, ob);
196         e_widget_list_object_append(o, of, 1, 1, 0.5);
197         evas_object_show(ob);
198         evas_object_show(of);
199      }
200
201    e_wizard_page_show(o);
202    pg->data = of;
203    return 1; /* 1 == show ui, and wait for user, 0 == just continue */
204 }
205
206 EAPI int
207 wizard_page_hide(E_Wizard_Page *pg)
208 {
209    evas_object_del(pg->data);
210    return 1;
211 }
212
213 EAPI int
214 wizard_page_apply(E_Wizard_Page *pg __UNUSED__)
215 {
216    if ((xdg_sel) && (!strcmp("/etc/xdg/menus/applications.menu", xdg_sel)))
217      {
218         eina_stringshare_del(xdg_sel);
219         xdg_sel = NULL;
220      }
221    e_config->default_system_menu = eina_stringshare_ref(xdg_sel);
222    efreet_menu_file_set(e_config->default_system_menu);
223    return 1;
224 }