1 #include "e_mod_main.h"
2 
3 static void      _e_mod_action_cb(E_Object *obj, const char *params);
4 static void      _e_mod_action_cb_edge(E_Object *obj, const char *params, E_Event_Zone_Edge *ev);
5 static Eina_Bool _e_mod_run_defer_cb(void *data);
6 static void      _e_mod_run_cb(void *data, E_Menu *m, E_Menu_Item *mi);
7 static void      _e_mod_menu_add(void *data, E_Menu *m);
8 static void      _config_init(void);
9 static void      _config_free(void);
10 static Eina_Bool _cleanup_history(void *data);
11 static void      _evry_type_init(const char *type);
12 static Eina_List *_evry_types = NULL;
13 static E_Int_Menu_Augmentation *maug = NULL;
14 static E_Action *act = NULL;
15 static Ecore_Timer *cleanup_timer;
16 static const char *module_icon = NULL;
17 static E_Config_DD *conf_edd = NULL;
18 static E_Config_DD *plugin_conf_edd = NULL;
19 static E_Config_DD *gadget_conf_edd = NULL;
20 
21 Evry_API *evry = NULL;
22 Evry_Config *evry_conf = NULL;
23 int _evry_events[NUM_EVRY_EVENTS];
24 E_Module *_mod_evry = NULL;
25 
26 /* module setup */
27 E_API E_Module_Api e_modapi =
28 {
29    E_MODULE_API_VERSION,
30    "Everything"
31 };
32 
33 E_API void *
e_modapi_init(E_Module * m)34 e_modapi_init(E_Module *m)
35 {
36    Eina_List *l;
37    Evry_Module *em;
38    char buf[4096];
39 
40    _mod_evry = m;
41 
42    /* add module supplied action */
43    act = e_action_add("everything");
44    if (act)
45      {
46         act->func.go = _e_mod_action_cb;
47         act->func.go_edge = _e_mod_action_cb_edge;
48         e_action_predef_name_set
49           (N_("Everything Launcher"),
50           N_("Show Everything Launcher"),
51           "everything", "", NULL, 0);
52      }
53 
54    maug = e_int_menus_menu_augmentation_add
55        ("main/1", _e_mod_menu_add, NULL, NULL, NULL);
56 
57    e_configure_registry_category_add
58      ("launcher", 80, _("Launcher"), NULL, "modules-launcher");
59 
60    snprintf(buf, sizeof(buf), "%s/e-module-everything.edj", e_module_dir_get(m));
61    module_icon = eina_stringshare_add(buf);
62 
63    e_configure_registry_item_add
64      ("launcher/run_everything", 40, _("Everything Configuration"),
65      NULL, module_icon, evry_config_dialog);
66    evry_init();
67 
68    _evry_type_init("NONE");
69    _evry_type_init("FILE");
70    _evry_type_init("DIRECTORY");
71    _evry_type_init("APPLICATION");
72    _evry_type_init("ACTION");
73    _evry_type_init("PLUGIN");
74    _evry_type_init("BORDER");
75    _evry_type_init("TEXT");
76 
77    _config_init();
78 
79    _evry_events[EVRY_EVENT_ITEMS_UPDATE] = ecore_event_type_new();
80    _evry_events[EVRY_EVENT_ITEM_SELECTED] = ecore_event_type_new();
81    _evry_events[EVRY_EVENT_ITEM_CHANGED] = ecore_event_type_new();
82    _evry_events[EVRY_EVENT_ACTION_PERFORMED] = ecore_event_type_new();
83    _evry_events[EVRY_EVENT_PLUGIN_SELECTED] = ecore_event_type_new();
84 
85    evry = E_NEW(Evry_API, 1);
86 #define SET(func) (evry->func = &evry_##func);
87    SET(api_version_check);
88    SET(item_new);
89    SET(item_free);
90    SET(item_ref);
91    SET(plugin_new);
92    SET(plugin_free);
93    SET(plugin_register);
94    SET(plugin_unregister);
95    SET(plugin_update);
96    SET(plugin_find);
97    SET(action_new);
98    SET(action_free);
99    SET(action_register);
100    SET(action_unregister);
101    SET(action_find);
102    SET(api_version_check);
103    SET(type_register);
104    SET(icon_theme_get);
105    SET(fuzzy_match);
106    SET(util_exec_app);
107    SET(util_url_escape);
108    SET(util_url_unescape);
109    SET(util_file_detail_set);
110    SET(util_plugin_items_add);
111    SET(util_md5_sum);
112    SET(util_icon_get);
113    SET(item_changed);
114    SET(file_path_get);
115    SET(file_url_get);
116    SET(history_item_add);
117    SET(history_types_get);
118    SET(history_item_usage_set);
119    SET(event_handler_add);
120 #undef SET
121 
122    evry_history_init();
123    evry_plug_actions_init();
124    evry_plug_apps_init(m);
125    evry_plug_files_init(m);
126    evry_plug_windows_init(m);
127    evry_plug_settings_init(m);
128    evry_plug_calc_init(m);
129    e_datastore_set("evry_api", evry);
130 
131    EINA_LIST_FOREACH (e_datastore_get("evry_modules"), l, em)
132      em->active = em->init(evry);
133 
134    evry_plug_collection_init();
135    evry_plug_clipboard_init();
136    evry_plug_text_init();
137    evry_view_init();
138    evry_view_help_init();
139    evry_gadget_init();
140 
141    /* cleanup every hour :) */
142    cleanup_timer = ecore_timer_loop_add(3600, _cleanup_history, NULL);
143 
144    return m;
145 }
146 
147 E_API int
e_modapi_shutdown(E_Module * m EINA_UNUSED)148 e_modapi_shutdown(E_Module *m EINA_UNUSED)
149 {
150    E_Config_Dialog *cfd;
151    const char *t;
152    Eina_List *l;
153    Evry_Module *em;
154 
155    EINA_LIST_FOREACH (e_datastore_get("evry_modules"), l, em)
156      {
157         if (em->active)
158           em->shutdown();
159 
160         em->active = EINA_FALSE;
161      }
162 
163    evry_plug_apps_shutdown();
164    evry_plug_files_shutdown();
165    evry_plug_settings_shutdown();
166    evry_plug_windows_shutdown();
167    evry_plug_calc_shutdown();
168    evry_plug_clipboard_shutdown();
169    evry_plug_text_shutdown();
170    evry_plug_collection_shutdown();
171    evry_plug_actions_shutdown();
172    evry_view_shutdown();
173    evry_view_help_shutdown();
174    evry_gadget_shutdown();
175    evry_shutdown();
176 
177    e_datastore_del("evry_api");
178    E_FREE(evry);
179    evry = NULL;
180 
181    _config_free();
182    evry_history_free();
183 
184    EINA_LIST_FREE (_evry_types, t)
185      eina_stringshare_del(t);
186 
187    e_configure_registry_item_del("launcher/run_everything");
188    e_configure_registry_category_del("launcher");
189 
190    while ((cfd = e_config_dialog_get("E", "launcher/run_everything")))
191      e_object_del(E_OBJECT(cfd));
192 
193    if (act)
194      {
195         e_action_predef_name_del("Everything Launcher",
196                                  "Show Everything Launcher");
197         e_action_del("everything");
198      }
199 
200    if (maug)
201      {
202         e_int_menus_menu_augmentation_del("main/1", maug);
203         maug = NULL;
204      }
205 
206    if (module_icon)
207      eina_stringshare_del(module_icon);
208 
209    /* Clean EET */
210    E_CONFIG_DD_FREE(conf_edd);
211    E_CONFIG_DD_FREE(plugin_conf_edd);
212    E_CONFIG_DD_FREE(gadget_conf_edd);
213 
214    if (cleanup_timer)
215      ecore_timer_del(cleanup_timer);
216 
217 #ifdef CHECK_REFS
218    Evry_Item *it;
219    EINA_LIST_FREE (_refd, it)
220      printf("%d %s\n", it->ref, it->label);
221 #endif
222 
223    _mod_evry = NULL;
224 
225    return 1;
226 }
227 
228 E_API int
e_modapi_save(E_Module * m EINA_UNUSED)229 e_modapi_save(E_Module *m EINA_UNUSED)
230 {
231    e_config_domain_save("module.everything", conf_edd, evry_conf);
232 
233    evry_plug_apps_save();
234    evry_plug_files_save();
235    evry_plug_settings_save();
236    evry_plug_windows_save();
237    evry_plug_calc_save();
238 
239    return 1;
240 }
241 
242 /***************************************************************************/
243 
244 Ecore_Event_Handler *
evry_event_handler_add(int type,Eina_Bool (* func)(void * data,int type,void * event),const void * data)245 evry_event_handler_add(int type, Eina_Bool (*func)(void *data, int type, void *event), const void *data)
246 {
247    return ecore_event_handler_add(_evry_events[type], func, data);
248 }
249 
250 Evry_Type
evry_type_register(const char * type)251 evry_type_register(const char *type)
252 {
253    const char *t = eina_stringshare_add(type);
254    Evry_Type ret = NUM_EVRY_TYPES;
255    const char *i;
256    Eina_List *l;
257 
258    EINA_LIST_FOREACH (_evry_types, l, i)
259      {
260         if (i == t) break;
261         ret++;
262      }
263 
264    if (!l)
265      {
266         _evry_types = eina_list_append(_evry_types, t);
267         return ret;
268      }
269    eina_stringshare_del(t);
270 
271    return ret;
272 }
273 
274 static void
_evry_type_init(const char * type)275 _evry_type_init(const char *type)
276 {
277    const char *t = eina_stringshare_add(type);
278    _evry_types = eina_list_append(_evry_types, t);
279 }
280 
281 const char *
evry_type_get(Evry_Type type)282 evry_type_get(Evry_Type type)
283 {
284    const char *ret = eina_list_nth(_evry_types, type);
285    if (!ret)
286      return eina_stringshare_add("");
287 
288    return ret;
289 }
290 
291 int
evry_api_version_check(int version)292 evry_api_version_check(int version)
293 {
294    if (EVRY_API_VERSION == version)
295      return 1;
296 
297    ERR("module API is %d, required is %d", version, EVRY_API_VERSION);
298 
299    return 0;
300 }
301 
302 static int
_evry_cb_view_sort(const void * data1,const void * data2)303 _evry_cb_view_sort(const void *data1, const void *data2)
304 {
305    const Evry_View *v1 = data1;
306    const Evry_View *v2 = data2;
307    return v1->priority - v2->priority;
308 }
309 
310 void
evry_view_register(Evry_View * view,int priority)311 evry_view_register(Evry_View *view, int priority)
312 {
313    view->priority = priority;
314 
315    evry_conf->views = eina_list_append(evry_conf->views, view);
316 
317    evry_conf->views = eina_list_sort(evry_conf->views,
318                                      eina_list_count(evry_conf->views),
319                                      _evry_cb_view_sort);
320 }
321 
322 void
evry_view_unregister(Evry_View * view)323 evry_view_unregister(Evry_View *view)
324 {
325    evry_conf->views = eina_list_remove(evry_conf->views, view);
326 }
327 
328 /***************************************************************************/
329 
330 static Eina_Bool
_cleanup_history(void * data EINA_UNUSED)331 _cleanup_history(void *data EINA_UNUSED)
332 {
333    /* evrything is active */
334    if (evry_hist)
335      return ECORE_CALLBACK_RENEW;
336 
337    /* cleanup old entries */
338    evry_history_free();
339    evry_history_init();
340 
341    return ECORE_CALLBACK_RENEW;
342 }
343 
344 static void
_config_init()345 _config_init()
346 {
347    Plugin_Config *pc, *pcc;
348 
349 #undef T
350 #undef D
351 #define T Plugin_Config
352 #define D plugin_conf_edd
353    plugin_conf_edd = E_CONFIG_DD_NEW("Plugin_Config", Plugin_Config);
354    E_CONFIG_VAL(D, T, name, STR);
355    E_CONFIG_VAL(D, T, enabled, INT);
356    E_CONFIG_VAL(D, T, priority, INT);
357    E_CONFIG_VAL(D, T, trigger, STR);
358    E_CONFIG_VAL(D, T, trigger_only, INT);
359    E_CONFIG_VAL(D, T, view_mode, INT);
360    E_CONFIG_VAL(D, T, aggregate, INT);
361    E_CONFIG_VAL(D, T, top_level, INT);
362    E_CONFIG_VAL(D, T, min_query, INT);
363    E_CONFIG_LIST(D, T, plugins, plugin_conf_edd);
364 #undef T
365 #undef D
366 #define T Gadget_Config
367 #define D gadget_conf_edd
368    gadget_conf_edd = E_CONFIG_DD_NEW("Gadget_Config", Gadget_Config);
369    E_CONFIG_VAL(D, T, id, STR);
370    E_CONFIG_VAL(D, T, plugin, STR);
371    E_CONFIG_VAL(D, T, hide_after_action, INT);
372    E_CONFIG_VAL(D, T, popup, INT);
373 #undef T
374 #undef D
375 #define T Evry_Config
376 #define D conf_edd
377    conf_edd = E_CONFIG_DD_NEW("Config", Evry_Config);
378    E_CONFIG_VAL(D, T, version, INT);
379    E_CONFIG_VAL(D, T, width, INT);
380    E_CONFIG_VAL(D, T, height, INT);
381    E_CONFIG_VAL(D, T, edge_width, INT);
382    E_CONFIG_VAL(D, T, edge_height, INT);
383    E_CONFIG_VAL(D, T, rel_x, DOUBLE);
384    E_CONFIG_VAL(D, T, rel_y, DOUBLE);
385    E_CONFIG_VAL(D, T, scroll_animate, INT);
386    E_CONFIG_VAL(D, T, scroll_speed, DOUBLE);
387    E_CONFIG_VAL(D, T, hide_input, INT);
388    E_CONFIG_VAL(D, T, hide_list, INT);
389    E_CONFIG_VAL(D, T, quick_nav, INT);
390    E_CONFIG_VAL(D, T, view_mode, INT);
391    E_CONFIG_VAL(D, T, view_zoom, INT);
392    E_CONFIG_VAL(D, T, cycle_mode, INT);
393    E_CONFIG_VAL(D, T, history_sort_mode, INT);
394    E_CONFIG_LIST(D, T, conf_subjects, plugin_conf_edd);
395    E_CONFIG_LIST(D, T, conf_actions, plugin_conf_edd);
396    E_CONFIG_LIST(D, T, conf_objects, plugin_conf_edd);
397    E_CONFIG_LIST(D, T, conf_views, plugin_conf_edd);
398    E_CONFIG_LIST(D, T, collections, plugin_conf_edd);
399    E_CONFIG_LIST(D, T, gadgets, gadget_conf_edd);
400    E_CONFIG_VAL(D, T, first_run, UCHAR);
401 #undef T
402 #undef D
403    evry_conf = e_config_domain_load("module.everything", conf_edd);
404 
405    if (evry_conf && !e_util_module_config_check(_("Everything Module"),
406                                                 evry_conf->version,
407                                                 MOD_CONFIG_FILE_VERSION))
408      _config_free();
409 
410    if (!evry_conf)
411      {
412         evry_conf = E_NEW(Evry_Config, 1);
413 
414         /* setup defaults */
415         evry_conf->rel_x = 0.5;
416         evry_conf->rel_y = 0.43;
417         evry_conf->width = 455;
418         evry_conf->height = 430;
419         evry_conf->scroll_animate = 1;
420         evry_conf->scroll_speed = 10.0;
421         evry_conf->hide_input = 0;
422         evry_conf->hide_list = 0;
423         evry_conf->quick_nav = 1;
424         evry_conf->view_mode = VIEW_MODE_DETAIL;
425         evry_conf->view_zoom = 0;
426         evry_conf->cycle_mode = 0;
427         evry_conf->history_sort_mode = 0;
428         evry_conf->edge_width = 340;
429         evry_conf->edge_height = 385;
430         evry_conf->first_run = EINA_TRUE;
431 
432         pcc = E_NEW(Plugin_Config, 1);
433         pcc->name = eina_stringshare_add("Start");
434         pcc->enabled = EINA_FALSE;
435         pcc->aggregate = EINA_FALSE;
436         pcc->top_level = EINA_TRUE;
437         pcc->view_mode = VIEW_MODE_THUMB;
438         evry_conf->collections = eina_list_append(evry_conf->collections, pcc);
439 
440         pc = E_NEW(Plugin_Config, 1);
441         pc->name = eina_stringshare_add("Windows");
442         pc->enabled = EINA_TRUE;
443         pc->view_mode = VIEW_MODE_NONE;
444         pcc->plugins = eina_list_append(pcc->plugins, pc);
445 
446         pc = E_NEW(Plugin_Config, 1);
447         pc->name = eina_stringshare_add("Settings");
448         pc->enabled = EINA_TRUE;
449         pc->view_mode = VIEW_MODE_NONE;
450         pcc->plugins = eina_list_append(pcc->plugins, pc);
451 
452         pc = E_NEW(Plugin_Config, 1);
453         pc->name = eina_stringshare_add("Files");
454         pc->enabled = EINA_TRUE;
455         pc->view_mode = VIEW_MODE_NONE;
456         pcc->plugins = eina_list_append(pcc->plugins, pc);
457 
458         pc = E_NEW(Plugin_Config, 1);
459         pc->name = eina_stringshare_add("Applications");
460         pc->enabled = EINA_TRUE;
461         pc->view_mode = VIEW_MODE_NONE;
462         pcc->plugins = eina_list_append(pcc->plugins, pc);
463         evry_conf->width = 464;
464         evry_conf->height = 366;
465      }
466 
467     evry_conf->version = MOD_CONFIG_FILE_VERSION;
468 }
469 
470 static void
_config_free(void)471 _config_free(void)
472 {
473    Plugin_Config *pc, *pc2;
474    Gadget_Config *gc;
475 
476    EINA_LIST_FREE (evry_conf->collections, pc)
477      {
478         EINA_LIST_FREE (pc->plugins, pc2)
479           {
480              IF_RELEASE(pc2->name);
481              IF_RELEASE(pc2->trigger);
482              E_FREE(pc2);
483           }
484         // collections become registered as SUBJECT
485         // plugin, therefore dont free  pc here
486      }
487 
488    EINA_LIST_FREE (evry_conf->conf_subjects, pc)
489      {
490         IF_RELEASE(pc->name);
491         IF_RELEASE(pc->trigger);
492         E_FREE(pc);
493      }
494    EINA_LIST_FREE (evry_conf->conf_actions, pc)
495      {
496         IF_RELEASE(pc->name);
497         IF_RELEASE(pc->trigger);
498         E_FREE(pc);
499      }
500    EINA_LIST_FREE (evry_conf->conf_objects, pc)
501      {
502         IF_RELEASE(pc->name);
503         IF_RELEASE(pc->trigger);
504         E_FREE(pc);
505      }
506    EINA_LIST_FREE (evry_conf->conf_views, pc)
507      {
508         IF_RELEASE(pc->name);
509         IF_RELEASE(pc->trigger);
510         E_FREE(pc);
511      }
512    EINA_LIST_FREE (evry_conf->gadgets, gc)
513      {
514         IF_RELEASE(gc->id);
515         IF_RELEASE(gc->plugin);
516         E_FREE(gc);
517      }
518 
519    E_FREE(evry_conf);
520 }
521 
522 /***************************************************************************/
523 /* action callback */
524 
525 static Ecore_Idle_Enterer *_idler = NULL;
526 static const char *_params = NULL;
527 
528 static Eina_Bool
_e_mod_run_defer_cb(void * data)529 _e_mod_run_defer_cb(void *data)
530 {
531    E_Zone *zone;
532 
533    zone = data;
534    if (zone) evry_show(zone, E_ZONE_EDGE_NONE, _params, EINA_TRUE);
535 
536    _idler = NULL;
537    return ECORE_CALLBACK_CANCEL;
538 }
539 
540 static void
_e_mod_action_cb(E_Object * obj EINA_UNUSED,const char * params)541 _e_mod_action_cb(E_Object *obj EINA_UNUSED, const char *params)
542 {
543    E_Zone *zone = NULL;
544 
545    zone = e_zone_current_get();
546    if (!zone) return;
547 
548    IF_RELEASE(_params);
549    if (params && params[0])
550      _params = eina_stringshare_add(params);
551    /* if (zone) evry_show(zone, _params); */
552 
553    if (_idler) ecore_idle_enterer_del(_idler);
554    _idler = ecore_idle_enterer_add(_e_mod_run_defer_cb, zone);
555 }
556 
557 static void
_e_mod_action_cb_edge(E_Object * obj EINA_UNUSED,const char * params,E_Event_Zone_Edge * ev)558 _e_mod_action_cb_edge(E_Object *obj  EINA_UNUSED, const char *params, E_Event_Zone_Edge *ev)
559 {
560    IF_RELEASE(_params);
561    if (params && params[0])
562      _params = eina_stringshare_add(params);
563 
564    if (_idler) ecore_idle_enterer_del(_idler);
565 
566    evry_show(ev->zone, ev->edge, _params, EINA_TRUE);
567 }
568 
569 /* menu item callback(s) */
570 static void
_e_mod_run_cb(void * data EINA_UNUSED,E_Menu * m,E_Menu_Item * mi EINA_UNUSED)571 _e_mod_run_cb(void *data EINA_UNUSED, E_Menu *m, E_Menu_Item *mi EINA_UNUSED)
572 {
573    IF_RELEASE(_params);
574    ecore_idle_enterer_add(_e_mod_run_defer_cb, m->zone);
575 }
576 
577 /* menu item add hook */
578 static void
_e_mod_menu_add(void * data EINA_UNUSED,E_Menu * m)579 _e_mod_menu_add(void *data EINA_UNUSED, E_Menu *m)
580 {
581    E_Menu_Item *mi;
582 
583    mi = e_menu_item_new(m);
584    e_menu_item_label_set(mi, _("Run Everything"));
585    e_util_menu_item_theme_icon_set(mi, "system-run");
586    e_menu_item_callback_set(mi, _e_mod_run_cb, NULL);
587 }
588 
589