1 #include "e_mod_main.h"
2 
3 /* actual module specifics */
4 static void _e_mod_action_fileman_cb(E_Object   *obj,
5                                      const char *params);
6 static void _e_mod_action_fileman_show_cb(E_Object   *obj,
7                                           const char *params);
8 static void _e_mod_action_fileman_reset_cb(E_Object *obj EINA_UNUSED, const char *params EINA_UNUSED);
9 static void      _e_mod_menu_add(void   *data, E_Menu *m);
10 static void      _e_mod_fileman_config_load(void);
11 static void      _e_mod_fileman_config_free(void);
12 static Eina_Bool _e_mod_zone_add(void *data,
13                                  int   type,
14                                  void *event);
15 
16 static E_Module *conf_module = NULL;
17 static E_Action *act = NULL;
18 static E_Action *act2 = NULL;
19 static E_Action *act3 = NULL;
20 static E_Int_Menu_Augmentation *maug = NULL;
21 static Ecore_Event_Handler *zone_add_handler = NULL;
22 
23 static E_Config_DD *paths_edd = NULL, *conf_edd = NULL;
24 Config *fileman_config = NULL;
25 
26 /* module setup */
27 E_API E_Module_Api e_modapi =
28 {
29    E_MODULE_API_VERSION,
30    "Fileman"
31 };
32 
33 E_API void *
e_modapi_init(E_Module * m)34 e_modapi_init(E_Module *m)
35 {
36    const Eina_List *l;
37    E_Zone *zone;
38 
39    conf_module = m;
40 
41    /* Setup Entry in Config Panel */
42    e_configure_registry_category_add("fileman", 100, _("Files"),
43                                      NULL, "system-file-manager");
44    e_configure_registry_item_add("fileman/fileman", 10, _("File Manager"),
45                                  NULL, "system-file-manager",
46                                  e_int_config_fileman);
47    e_configure_registry_item_add("fileman/file_icons", 20, _("File Icons"),
48                                  NULL, "preferences-file-icons",
49                                  e_int_config_mime);
50    /* Setup Config edd */
51    _e_mod_fileman_config_load();
52 
53    /* add module supplied action */
54    act = e_action_add("fileman");
55    if (act)
56      {
57         act->func.go = _e_mod_action_fileman_cb;
58         e_action_predef_name_set(N_("Launch"), N_("File Manager"),
59                                  "fileman", NULL, "syntax: /path/to/dir or ~/path/to/dir or favorites or desktop, examples: /boot/grub, ~/downloads", 1);
60      }
61    act2 = e_action_add("fileman_reset");
62    if (act2)
63      act2->func.go = _e_mod_action_fileman_reset_cb;
64    act3 = e_action_add("fileman_show");
65    if (act3)
66      {
67         act3->func.go = _e_mod_action_fileman_show_cb;
68         e_action_predef_name_set(N_("Show Dir"), N_("File Manager"),
69                                  "fileman_show", NULL, "syntax: /path/to/dir or ~/path/to/dir or favorites or desktop, examples: /boot/grub, ~/downloads", 1);
70      }
71    maug = e_int_menus_menu_augmentation_add_sorted("main/1", _("Navigate"), _e_mod_menu_add, NULL, NULL, NULL);
72 
73    e_fwin_init();
74 
75    /* Hook into zones */
76    EINA_LIST_FOREACH(e_comp->zones, l, zone)
77      {
78         if (e_fwin_zone_find(zone)) continue;
79         if (e_config->show_desktop_icons)
80           e_fwin_zone_new(zone, e_mod_fileman_path_find(zone));
81      }
82    zone_add_handler = ecore_event_handler_add(E_EVENT_ZONE_ADD,
83                                               _e_mod_zone_add, NULL);
84 
85    /* FIXME: add system event for new zone creation, and on creation, add an fwin to the zone */
86 
87    e_fileman_dbus_init();
88 
89    e_fwin_nav_init();
90 
91    return m;
92 }
93 
94 E_API int
e_modapi_shutdown(E_Module * m EINA_UNUSED)95 e_modapi_shutdown(E_Module *m EINA_UNUSED)
96 {
97    const Eina_List *l;
98    E_Zone *zone;
99    E_Config_Dialog *cfd;
100 
101    e_fileman_dbus_shutdown();
102 
103    ecore_event_handler_del(zone_add_handler);
104    zone_add_handler = NULL;
105 
106    /* Unhook zone fm */
107    EINA_LIST_FOREACH(e_comp->zones, l, zone)
108      e_fwin_zone_shutdown(zone);
109 
110    e_fwin_nav_shutdown();
111 
112    /* remove module-supplied menu additions */
113    if (maug)
114      {
115         e_int_menus_menu_augmentation_del("main/1", maug);
116         maug = NULL;
117      }
118    e_fwin_shutdown();
119    /* remove module-supplied action */
120    if (act)
121      {
122         e_action_predef_name_del("Launch", "File Manager");
123         e_action_del("fileman");
124         act = NULL;
125      }
126    if (act2)
127      {
128         e_action_del("fileman_reset");
129         act2 = NULL;
130      }
131    if (act3)
132      {
133         e_action_del("fileman_show");
134         act3 = NULL;
135      }
136    while ((cfd = e_config_dialog_get("E", "fileman/mime_edit_dialog")))
137       e_object_del(E_OBJECT(cfd));
138    while ((cfd = e_config_dialog_get("E", "fileman/file_icons")))
139       e_object_del(E_OBJECT(cfd));
140    while ((cfd = e_config_dialog_get("E", "fileman/fileman")))
141       e_object_del(E_OBJECT(cfd));
142 
143    e_configure_registry_item_del("fileman/file_icons");
144    e_configure_registry_item_del("fileman/fileman");
145    e_configure_registry_category_del("fileman");
146 
147    e_config_domain_save("module.fileman", conf_edd, fileman_config);
148 
149    _e_mod_fileman_config_free();
150    E_CONFIG_DD_FREE(conf_edd);
151    E_CONFIG_DD_FREE(paths_edd);
152 
153    //eina_shutdown();
154 
155    conf_module = NULL;
156    return 1;
157 }
158 
159 E_API int
e_modapi_save(E_Module * m EINA_UNUSED)160 e_modapi_save(E_Module *m EINA_UNUSED)
161 {
162    e_config_domain_save("module.fileman", conf_edd, fileman_config);
163    return 1;
164 }
165 
166 /* action callback */
167 static void
_e_mod_action_fileman_reset_cb(E_Object * obj EINA_UNUSED,const char * params EINA_UNUSED)168 _e_mod_action_fileman_reset_cb(E_Object *obj EINA_UNUSED, const char *params EINA_UNUSED)
169 {
170    e_fwin_reload_all();
171 }
172 
173 static void
fwin(const char * dev,const char * path)174 fwin(const char *dev, const char *path)
175 {
176    if (!e_fwin_show(dev, path)) e_fwin_new(dev, path);
177 }
178 
179 static void
_e_mod_action_fileman_cb(E_Object * obj EINA_UNUSED,const char * params)180 _e_mod_action_fileman_cb(E_Object   *obj EINA_UNUSED,
181                          const char *params)
182 {
183    E_Zone *zone = NULL;
184 
185    zone = e_zone_current_get();
186    if (zone)
187      {
188         if (params && params[0] == '/')
189           e_fwin_new("/", params);
190         else if (params && params[0] == '~')
191           e_fwin_new("~/", params + 1);
192         else if (params && strcmp(params, "(none)")) /* avoid matching paths that no longer exist */
193           {
194              char *path;
195              path = e_util_shell_env_path_eval(params);
196              if (path)
197                {
198                   e_fwin_new(path, "/");
199                   free(path);
200                }
201           }
202         else
203           e_fwin_new("favorites", "/");
204      }
205 }
206 
207 static void
_e_mod_action_fileman_show_cb(E_Object * obj EINA_UNUSED,const char * params)208 _e_mod_action_fileman_show_cb(E_Object   *obj EINA_UNUSED,
209                               const char *params)
210 {
211    const char *dev = "/", *path = "/";
212    char *p = NULL;
213    E_Zone *zone = NULL;
214 
215    zone = e_zone_current_get();
216    if (zone)
217      {
218         if (params && params[0] == '/')
219           path = params;
220         else if (params && params[0] == '~')
221           path = params + 1;
222         else if (params && strcmp(params, "(none)")) /* avoid matching paths that no longer exist */
223           {
224              p = e_util_shell_env_path_eval(params);
225              if (p)
226                {
227                   dev = p;
228                   path = "/";
229                }
230           }
231         else
232           {
233              dev = "favorites";
234              path = "/";
235           }
236      }
237    fwin(dev, path);
238    free(p);
239 }
240 
241 void
_e_mod_menu_add(void * data EINA_UNUSED,E_Menu * m)242 _e_mod_menu_add(void *data EINA_UNUSED, E_Menu *m)
243 {
244    e_mod_menu_add(m, NULL);
245 }
246 
247 /* Abstract fileman config load/create to one function for maintainability */
248 static void
_e_mod_fileman_config_load(void)249 _e_mod_fileman_config_load(void)
250 {
251 #undef T
252 #undef D
253 #define T Fileman_Path
254 #define D paths_edd
255    paths_edd = E_CONFIG_DD_NEW("Fileman_Path", Fileman_Path);
256    E_CONFIG_VAL(D, T, dev, STR);
257    E_CONFIG_VAL(D, T, path, STR);
258    E_CONFIG_VAL(D, T, zone, UINT);
259    E_CONFIG_VAL(D, T, desktop_mode, INT);
260    conf_edd = E_CONFIG_DD_NEW("Fileman_Config", Config);
261    #undef T
262    #undef D
263    #define T Config
264    #define D conf_edd
265    E_CONFIG_VAL(D, T, config_version, INT);
266    E_CONFIG_VAL(D, T, view.mode, INT);
267    E_CONFIG_VAL(D, T, view.open_dirs_in_place, UCHAR);
268    E_CONFIG_VAL(D, T, view.selector, UCHAR);
269    E_CONFIG_VAL(D, T, view.single_click, UCHAR);
270    E_CONFIG_VAL(D, T, view.no_subdir_jump, UCHAR);
271    E_CONFIG_VAL(D, T, view.no_subdir_drop, UCHAR);
272    E_CONFIG_VAL(D, T, view.always_order, UCHAR);
273    E_CONFIG_VAL(D, T, view.link_drop, UCHAR);
274    E_CONFIG_VAL(D, T, view.fit_custom_pos, UCHAR);
275    E_CONFIG_VAL(D, T, view.show_full_path, UCHAR);
276    E_CONFIG_VAL(D, T, view.show_toolbar, UCHAR);
277    E_CONFIG_VAL(D, T, view.show_sidebar, UCHAR);
278    E_CONFIG_VAL(D, T, view.desktop_navigation, UCHAR);
279    E_CONFIG_VAL(D, T, icon.icon.w, INT);
280    E_CONFIG_VAL(D, T, icon.icon.h, INT);
281    E_CONFIG_VAL(D, T, icon.list.w, INT);
282    E_CONFIG_VAL(D, T, icon.list.h, INT);
283    E_CONFIG_VAL(D, T, icon.fixed.w, UCHAR);
284    E_CONFIG_VAL(D, T, icon.fixed.h, UCHAR);
285    E_CONFIG_VAL(D, T, icon.extension.show, UCHAR);
286    E_CONFIG_VAL(D, T, icon.max_thumb_size, UINT);
287    E_CONFIG_VAL(D, T, list.sort.no_case, UCHAR);
288    E_CONFIG_VAL(D, T, list.sort.extension, UCHAR);
289    E_CONFIG_VAL(D, T, list.sort.mtime, UCHAR);
290    E_CONFIG_VAL(D, T, list.sort.size, UCHAR);
291    E_CONFIG_VAL(D, T, list.sort.dirs.first, UCHAR);
292    E_CONFIG_VAL(D, T, list.sort.dirs.last, UCHAR);
293    E_CONFIG_VAL(D, T, selection.single, UCHAR);
294    E_CONFIG_VAL(D, T, selection.windows_modifiers, UCHAR);
295    E_CONFIG_VAL(D, T, theme.background, STR);
296    E_CONFIG_VAL(D, T, theme.frame, STR);
297    E_CONFIG_VAL(D, T, theme.icons, STR);
298    E_CONFIG_VAL(D, T, theme.fixed, UCHAR);
299    E_CONFIG_VAL(D, T, tooltip.delay, DOUBLE);
300    E_CONFIG_VAL(D, T, tooltip.size, DOUBLE);
301    E_CONFIG_VAL(D, T, tooltip.enable, UCHAR);
302    E_CONFIG_VAL(D, T, tooltip.clamp_size, UCHAR);
303    E_CONFIG_VAL(D, T, view.spring_delay, INT);
304    E_CONFIG_VAL(D, T, view.toolbar_orient, UINT);
305    E_CONFIG_LIST(D, T, paths, paths_edd);
306 
307    fileman_config = e_config_domain_load("module.fileman", conf_edd);
308    if (fileman_config)
309      {
310         if (!e_util_module_config_check(_("Fileman"), fileman_config->config_version, MOD_CONFIG_FILE_VERSION))
311           _e_mod_fileman_config_free();
312      }
313 
314    if (!fileman_config)
315      {
316         fileman_config = E_NEW(Config, 1);
317         fileman_config->view.mode = E_FM2_VIEW_MODE_GRID_ICONS;
318         e_config->show_desktop_icons = 1;
319         fileman_config->icon.icon.w = 48;
320         fileman_config->icon.icon.h = 48;
321         fileman_config->icon.extension.show = 1;
322         fileman_config->list.sort.no_case = 1;
323         fileman_config->list.sort.dirs.first = 1;
324         fileman_config->view.show_toolbar = 1;
325         fileman_config->view.open_dirs_in_place = 1;
326         fileman_config->tooltip.delay = 1.0;
327         fileman_config->tooltip.size = 30.0;
328         fileman_config->view.show_sidebar = 1;
329         fileman_config->tooltip.enable = 1;
330         fileman_config->view.spring_delay = 1;
331         fileman_config->icon.max_thumb_size = 5;
332         fileman_config->view.toolbar_orient = E_GADCON_ORIENT_TOP;
333      }
334     fileman_config->config_version = MOD_CONFIG_FILE_VERSION;
335     fileman_config->icon.icon.h = fileman_config->icon.icon.w;
336 
337     /* UCHAR's give nasty compile warnings about comparisons so not gonna limit those */
338     E_CONFIG_LIMIT(fileman_config->view.mode, E_FM2_VIEW_MODE_ICONS, E_FM2_VIEW_MODE_LIST);
339     E_CONFIG_LIMIT(fileman_config->icon.icon.w, 16, 256);
340     E_CONFIG_LIMIT(fileman_config->icon.icon.h, 16, 256);
341     E_CONFIG_LIMIT(fileman_config->icon.list.w, 16, 256);
342     E_CONFIG_LIMIT(fileman_config->icon.list.h, 16, 256);
343 
344     E_CONFIG_LIMIT(fileman_config->tooltip.delay, 0.0, 5.0);
345     E_CONFIG_LIMIT(fileman_config->tooltip.size, 10.0, 75.0);
346     E_CONFIG_LIMIT(fileman_config->view.spring_delay, 1, 10);
347     E_CONFIG_LIMIT(fileman_config->icon.max_thumb_size, 0, 1024);
348 
349     fileman_config->view.menu_shows_files = 0;
350 
351     e_config_save_queue();
352 }
353 
354 static void
_e_mod_fileman_path_free(Fileman_Path * path)355 _e_mod_fileman_path_free(Fileman_Path *path)
356 {
357    if (!path) return;
358    eina_stringshare_del(path->dev);
359    eina_stringshare_del(path->path);
360    free(path);
361 }
362 
363 static void
_e_mod_fileman_config_free(void)364 _e_mod_fileman_config_free(void)
365 {
366    eina_stringshare_del(fileman_config->theme.background);
367    eina_stringshare_del(fileman_config->theme.frame);
368    eina_stringshare_del(fileman_config->theme.icons);
369    E_FREE_LIST(fileman_config->paths, _e_mod_fileman_path_free);
370    E_FREE(fileman_config);
371 }
372 
373 static Eina_Bool
_e_mod_zone_add(EINA_UNUSED void * data,int type,void * event)374 _e_mod_zone_add(EINA_UNUSED void *data,
375                 int              type,
376                 void            *event)
377 {
378    E_Event_Zone_Add *ev;
379    E_Zone *zone;
380 
381    if (type != E_EVENT_ZONE_ADD) return ECORE_CALLBACK_PASS_ON;
382    ev = event;
383    zone = ev->zone;
384    if (e_fwin_zone_find(zone)) return ECORE_CALLBACK_PASS_ON;
385    if (e_config->show_desktop_icons)
386      e_fwin_zone_new(zone, e_mod_fileman_path_find(zone));
387    return ECORE_CALLBACK_PASS_ON;
388 }
389 
390 Fileman_Path *
e_mod_fileman_path_find(E_Zone * zone)391 e_mod_fileman_path_find(E_Zone *zone)
392 {
393    Eina_List *l;
394    Fileman_Path *path;
395 
396    EINA_LIST_FOREACH(fileman_config->paths, l, path)
397      if (path->zone == zone->num) break;
398    if (l && fileman_config->view.desktop_navigation) return path;
399    if (l)
400      {
401         eina_stringshare_replace(&path->path, NULL);
402         eina_stringshare_replace(&path->dev, "desktop");
403      }
404    else
405      {
406         path = E_NEW(Fileman_Path, 1);
407         path->zone = zone->num;
408         path->dev = eina_stringshare_add("desktop");
409         fileman_config->paths = eina_list_append(fileman_config->paths, path);
410         path->desktop_mode = E_FM2_VIEW_MODE_CUSTOM_ICONS;
411      }
412    if (zone->num == 0)
413      path->path = eina_stringshare_add("/");
414    else
415      path->path = eina_stringshare_printf("%d", zone->num);
416    return path;
417 }
418