8e4264a68765b29984fb4b5e1491568ba737661d
[framework/uifw/e17.git] / src / modules / ibar / e_mod_config.c
1 #include "e.h"
2 #include "e_mod_main.h"
3
4 struct _E_Config_Dialog_Data
5 {
6    const char *dir;
7    int show_label, eap_label;
8
9    Evas_Object *tlist;
10    Evas_Object *radio_name;
11    Evas_Object *radio_comment;
12    Evas_Object *radio_generic;
13    E_Confirm_Dialog *dialog_delete;
14 };
15
16 /* Protos */
17 static void *_create_data(E_Config_Dialog *cfd);
18 static void _free_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
19 static Evas_Object *_basic_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
20 static int _basic_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
21 static void _cb_add(void *data, void *data2);
22 static void _cb_del(void *data, void *data2);
23 static void _cb_config(void *data, void *data2);
24 static void _cb_entry_ok(char *text, void *data);
25 static void _cb_confirm_dialog_yes(void *data);
26 static void _cb_confirm_dialog_destroy(void *data);
27 static void _load_tlist(E_Config_Dialog_Data *cfdata);
28 static void _show_label_cb_change(void *data, Evas_Object *obj);
29
30 void 
31 _config_ibar_module(Config_Item *ci)
32 {
33    E_Config_Dialog *cfd;
34    E_Config_Dialog_View *v;
35    char buf[4096];
36
37    v = E_NEW(E_Config_Dialog_View, 1);
38
39    /* Dialog Methods */
40    v->create_cfdata = _create_data;
41    v->free_cfdata = _free_data;
42    v->basic.apply_cfdata = _basic_apply_data;
43    v->basic.create_widgets = _basic_create_widgets;
44    v->advanced.apply_cfdata = NULL;
45    v->advanced.create_widgets = NULL;
46
47    snprintf(buf, sizeof(buf), "%s/e-module-ibar.edj", 
48             e_module_dir_get(ibar_config->module));
49
50    /* Create The Dialog */
51    cfd = e_config_dialog_new(e_container_current_get(e_manager_current_get()),
52                              _("IBar Settings"),
53                              "E", "_e_mod_ibar_config_dialog",
54                              buf, 0, v, ci);
55    ibar_config->config_dialog = cfd;
56 }
57
58 static void 
59 _fill_data(Config_Item *ci, E_Config_Dialog_Data *cfdata)
60 {
61    if (ci->dir)
62      cfdata->dir = eina_stringshare_ref(ci->dir);
63    else
64      cfdata->dir = eina_stringshare_add("");
65    cfdata->show_label = ci->show_label;
66    cfdata->eap_label = ci->eap_label;
67 }
68
69 static void *
70 _create_data(E_Config_Dialog *cfd)
71 {
72    E_Config_Dialog_Data *cfdata;
73    Config_Item *ci;
74
75    ci = cfd->data;
76    cfdata = E_NEW(E_Config_Dialog_Data, 1);
77    _fill_data(ci, cfdata);
78    return cfdata;
79 }
80
81 static void 
82 _free_data(E_Config_Dialog *cfd __UNUSED__, E_Config_Dialog_Data *cfdata)
83 {
84    if (cfdata->dir) eina_stringshare_del(cfdata->dir);
85    if (cfdata->dialog_delete) e_object_del(E_OBJECT(cfdata->dialog_delete));
86    ibar_config->config_dialog = NULL;
87    E_FREE(cfdata);
88 }
89
90 static Evas_Object *
91 _basic_create_widgets(E_Config_Dialog *cfd __UNUSED__, Evas *evas, E_Config_Dialog_Data *cfdata)
92 {
93    Evas_Object *o, *of, *ol, *ob, *ot;
94    E_Radio_Group *rg;
95
96    o = e_widget_list_add(evas, 0, 0);
97
98    of = e_widget_frametable_add(evas, _("Selected Bar Source"), 0);
99    ol = e_widget_ilist_add(evas, 32, 32, &(cfdata->dir));
100    cfdata->tlist = ol;
101    _load_tlist(cfdata);
102    e_widget_size_min_set(ol, 140, 140);
103    e_widget_frametable_object_append(of, ol, 0, 0, 1, 2, 1, 1, 1, 0);
104
105    ot = e_widget_table_add(evas, 0);
106    ob = e_widget_button_add(evas, _("Add"), "list-add", _cb_add, cfdata, NULL);
107    e_widget_table_object_append(ot, ob, 0, 0, 1, 1, 1, 1, 1, 0);
108    ob = e_widget_button_add(evas, _("Delete"), "list-remove", _cb_del, cfdata, NULL);
109    e_widget_table_object_append(ot, ob, 0, 1, 1, 1, 1, 1, 1, 0);
110    ob = e_widget_button_add(evas, _("Setup"), "configure", _cb_config, cfdata, NULL);
111    e_widget_table_object_append(ot, ob, 0, 2, 1, 1, 1, 1, 1, 0);   
112
113    if (!e_configure_registry_exists("applications/ibar_applications")) 
114      e_widget_disabled_set(ob, 1);
115
116    e_widget_frametable_object_append(of, ot, 1, 0, 1, 1, 1, 1, 1, 0);
117    e_widget_list_object_append(o, of, 1, 1, 0.5);
118
119    of = e_widget_framelist_add(evas, _("Icon Labels"), 0);
120    ob = e_widget_check_add(evas, _("Show Icon Label"), &(cfdata->show_label));
121    e_widget_on_change_hook_set(ob, _show_label_cb_change, cfdata);
122    e_widget_framelist_object_append(of, ob);  
123
124    rg = e_widget_radio_group_new(&(cfdata->eap_label));
125
126    cfdata->radio_name = e_widget_radio_add(evas, _("Display App Name"), 0, rg);
127    e_widget_framelist_object_append(of, cfdata->radio_name);
128    if (!cfdata->show_label) e_widget_disabled_set(cfdata->radio_name, 1);
129
130    cfdata->radio_comment = e_widget_radio_add(evas, _("Display App Comment"), 1, rg);
131    e_widget_framelist_object_append(of, cfdata->radio_comment);
132    if (!cfdata->show_label) e_widget_disabled_set(cfdata->radio_comment, 1);
133
134    cfdata->radio_generic = e_widget_radio_add(evas, _("Display App Generic"), 2, rg);
135    e_widget_framelist_object_append(of, cfdata->radio_generic);
136    if (!cfdata->show_label) e_widget_disabled_set(cfdata->radio_generic, 1);
137
138    e_widget_list_object_append(o, of, 1, 1, 0.5);
139    return o;
140 }
141
142 static int 
143 _basic_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
144 {
145    Config_Item *ci;
146
147    ci = cfd->data;
148    if (ci->dir) eina_stringshare_del(ci->dir);
149    ci->dir = NULL;
150    if (cfdata->dir) ci->dir = eina_stringshare_ref(cfdata->dir);
151    ci->show_label = cfdata->show_label;
152    ci->eap_label = cfdata->eap_label;
153    _ibar_config_update(ci);
154    e_config_save_queue();
155    return 1;
156 }
157
158 static void 
159 _cb_add(void *data, void *data2 __UNUSED__) 
160 {
161    E_Config_Dialog_Data *cfdata;
162
163    cfdata = data;
164    e_entry_dialog_show(_("Create new IBar source"), "enlightenment",
165                        _("Enter a name for this new source:"), "", NULL, NULL,
166                        _cb_entry_ok, NULL, cfdata);
167 }
168
169 static void 
170 _cb_del(void *data, void *data2 __UNUSED__) 
171 {
172    char buf[4096];
173    E_Config_Dialog_Data *cfdata;
174    E_Confirm_Dialog *dialog;
175
176    cfdata = data;   
177    if (cfdata->dialog_delete) return;
178
179    snprintf(buf, sizeof(buf), _("You requested to delete \"%s\".<br><br>"
180                                 "Are you sure you want to delete this bar source?"),
181             cfdata->dir);
182
183    dialog = e_confirm_dialog_show(_("Are you sure you want to delete this bar source?"),
184                                   "application-exit", buf, NULL, NULL, 
185                                   _cb_confirm_dialog_yes, NULL, cfdata, NULL, 
186                                   _cb_confirm_dialog_destroy, cfdata);
187    cfdata->dialog_delete = dialog;
188 }
189
190 static void 
191 _cb_config(void *data, void *data2 __UNUSED__) 
192 {
193    char path[PATH_MAX];
194    E_Config_Dialog_Data *cfdata;
195
196    cfdata = data;
197    e_user_dir_snprintf(path, sizeof(path), "applications/bar/%s/.order", 
198                        cfdata->dir);
199    e_configure_registry_call("internal/ibar_other",
200                              e_container_current_get(e_manager_current_get()),
201                              path);
202 }
203
204 static void
205 _cb_entry_ok(char *text, void *data) 
206 {
207    char buf[4096];
208    char tmp[4096];
209    FILE *f;
210    size_t len;
211
212    len = e_user_dir_snprintf(buf, sizeof(buf), "applications/bar/%s", text);
213    if (len + sizeof("/.order") >= sizeof(buf)) return;
214    if (!ecore_file_exists(buf))
215      {
216         ecore_file_mkdir(buf);
217         memcpy(buf + len, "/.order", sizeof("/.order"));
218         f = fopen(buf, "w");
219         if (f)
220           {
221              int ret = 0;
222
223              /* Populate this .order file with some defaults */
224              snprintf(tmp, sizeof(tmp), "xterm.desktop\n" "sylpheed.desktop\n" 
225                       "firefox.desktop\n" "openoffice.desktop\n" "xchat.desktop\n"
226                       "gimp.desktop\n" "xmms.desktop\n");
227              ret = fwrite(tmp, sizeof(char), strlen(tmp), f);
228              fclose(f);
229           }
230      }
231
232    _load_tlist(data);
233 }
234
235 static void
236 _cb_confirm_dialog_yes(void *data) 
237 {
238    E_Config_Dialog_Data *cfdata;
239    char buf[4096];
240
241    cfdata = data;
242    if (e_user_dir_snprintf(buf, sizeof(buf), "applications/bar/%s", cfdata->dir) >= sizeof(buf))
243      return;
244    if (ecore_file_is_dir(buf))
245      ecore_file_recursive_rm(buf);
246
247    _load_tlist(cfdata);
248 }
249
250 static void
251 _cb_confirm_dialog_destroy(void *data) 
252 {
253    E_Config_Dialog_Data *cfdata;
254
255    cfdata = data;
256    cfdata->dialog_delete = NULL;
257 }
258
259 static void 
260 _load_tlist(E_Config_Dialog_Data *cfdata) 
261 {
262    Eina_List *dirs;
263    char buf[4096], *file;
264    int selnum = -1;
265    int i = 0;
266    size_t len;
267
268    e_widget_ilist_clear(cfdata->tlist);
269
270    len = e_user_dir_concat_static(buf, "applications/bar");
271    if (len + 2 >= sizeof(buf)) return;
272    dirs = ecore_file_ls(buf);
273
274    buf[len] = '/';
275    len++;
276
277    EINA_LIST_FREE(dirs, file)
278      {
279         if (file[0] == '.') continue;
280         if (eina_strlcpy(buf + len, file, sizeof(buf) - len) >= sizeof(buf) - len)
281           continue;
282         if (ecore_file_is_dir(buf))
283           {
284              e_widget_ilist_append(cfdata->tlist, NULL, file, NULL, NULL, file);
285              if ((cfdata->dir) && (!strcmp(cfdata->dir, file)))
286                selnum = i;
287              i++;
288           }
289
290         free(file);
291      }
292
293    e_widget_ilist_go(cfdata->tlist);
294    if (selnum >= 0) e_widget_ilist_selected_set(cfdata->tlist, selnum);   
295 }
296
297 static void 
298 _show_label_cb_change(void *data, Evas_Object *obj __UNUSED__) 
299 {
300    E_Config_Dialog_Data *cfdata;
301
302    cfdata = data;
303    if (!cfdata) return;
304    e_widget_disabled_set(cfdata->radio_name, !cfdata->show_label);
305    e_widget_disabled_set(cfdata->radio_comment, !cfdata->show_label);
306    e_widget_disabled_set(cfdata->radio_generic, !cfdata->show_label);   
307 }