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