1 #include "e_mod_main.h"
2 
3 static Eina_List *actions = NULL;
4 
5 int
evry_plugins_init(void)6 evry_plugins_init(void)
7 {
8    return 1;
9 }
10 
11 void
evry_plugins_shutdown(void)12 evry_plugins_shutdown(void)
13 {
14    Evry_Action *act;
15 
16    EINA_LIST_FREE (actions, act)
17      evry_action_free(act);
18 }
19 
20 static int
_evry_cb_plugin_sort(const void * data1,const void * data2)21 _evry_cb_plugin_sort(const void *data1, const void *data2)
22 {
23    const Plugin_Config *pc1 = data1;
24    const Plugin_Config *pc2 = data2;
25 
26    return pc1->priority - pc2->priority;
27 }
28 
29 void
_evry_plugin_free(Evry_Item * it)30 _evry_plugin_free(Evry_Item *it)
31 {
32    GET_EVRY_PLUGIN(p, it);
33 
34    evry_plugin_unregister(p);
35 
36    if (p->config) p->config->plugin = NULL;
37    if (p->name) eina_stringshare_del(p->name);
38 
39    E_FREE(p);
40 }
41 
42 Evry_Plugin *
evry_plugin_new(Evry_Plugin * base,const char * name,const char * label,const char * icon,Evry_Type item_type,Evry_Plugin * (* begin)(Evry_Plugin * p,const Evry_Item * item),void (* finish)(Evry_Plugin * p),int (* fetch)(Evry_Plugin * p,const char * input))43 evry_plugin_new(Evry_Plugin *base, const char *name, const char *label,
44                 const char *icon, Evry_Type item_type,
45                 Evry_Plugin *(*begin)(Evry_Plugin * p, const Evry_Item * item),
46                 void (*finish)(Evry_Plugin *p),
47                 int (*fetch)(Evry_Plugin *p, const char *input))
48 {
49    Evry_Plugin *p;
50    Evry_Item *it;
51 
52    if (base)
53      p = base;
54    else
55      p = E_NEW(Evry_Plugin, 1);
56 
57    it = evry_item_new(EVRY_ITEM(p), NULL, label, NULL, _evry_plugin_free);
58    it->plugin = p;
59    it->browseable = EINA_TRUE;
60    it->type = EVRY_TYPE_PLUGIN;
61    if (item_type)
62      it->subtype = item_type;
63    if (icon)
64      it->icon = eina_stringshare_add(icon);
65 
66    p->name = eina_stringshare_add(name);
67    p->begin = begin;
68    p->finish = finish;
69    p->fetch = fetch;
70 
71    p->async_fetch = EINA_FALSE;
72    p->history = EINA_TRUE;
73 
74    return p;
75 }
76 
77 void
evry_plugin_free(Evry_Plugin * p)78 evry_plugin_free(Evry_Plugin *p)
79 {
80    evry_item_free(EVRY_ITEM(p));
81 }
82 
83 static int
_evry_plugin_action_browse(Evry_Action * act)84 _evry_plugin_action_browse(Evry_Action *act)
85 {
86    Evry_Plugin *p;
87    Eina_List *plugins = NULL;
88    Evry_Selector *sel;
89 
90    GET_ITEM(it, act->it1.item);
91    GET_EVRY_PLUGIN(pp, EVRY_ITEM(act)->data);
92 
93    if (!it->plugin || !it->plugin->state)
94      return 0;
95 
96    sel = it->plugin->state->selector;
97 
98    evry_selectors_switch(sel->win, -1, EINA_TRUE);
99 
100    if ((p = pp->begin(pp, it)))
101      {
102         plugins = eina_list_append(plugins, p);
103 
104         if (!evry_state_push(sel, plugins))
105           eina_list_free(plugins);
106      }
107 
108    return 0;
109 }
110 
111 int
evry_plugin_register(Evry_Plugin * p,int type,int priority)112 evry_plugin_register(Evry_Plugin *p, int type, int priority)
113 {
114    Eina_List *l;
115    Plugin_Config *pc;
116    Eina_List *conf = NULL;
117    int new_conf = 0;
118 
119    if ((type < 0) || (type > 2))
120      return 0;
121 
122    if (type == EVRY_PLUGIN_SUBJECT)
123      conf = evry_conf->conf_subjects;
124    else if (type == EVRY_PLUGIN_ACTION)
125      conf = evry_conf->conf_actions;
126    else if (type == EVRY_PLUGIN_OBJECT)
127      conf = evry_conf->conf_objects;
128 
129    EINA_LIST_FOREACH (conf, l, pc)
130      if (pc->name && p->name && !strcmp(pc->name, p->name))
131        break;
132 
133    /* check if module of same name is already loaded */
134    /* if ((pc) && (pc->plugin))
135     *   return 0; */
136 
137    /* collection plugin sets its own config */
138    if (!pc && p->config)
139      {
140         conf = eina_list_append(conf, p->config);
141         pc = p->config;
142      }
143    else if (!pc)
144      {
145         new_conf = 1;
146         pc = E_NEW(Plugin_Config, 1);
147         pc->name = eina_stringshare_add(p->name);
148         pc->enabled = 1;
149         pc->priority = priority ? priority : 100;
150         pc->view_mode = VIEW_MODE_NONE;
151         pc->aggregate = EINA_TRUE;
152         pc->top_level = EINA_TRUE;
153 
154         conf = eina_list_append(conf, pc);
155      }
156    if (pc->trigger && strlen(pc->trigger) == 0)
157      {
158         eina_stringshare_del(pc->trigger);
159         pc->trigger = NULL;
160      }
161 
162    p->config = pc;
163    pc->plugin = p;
164 
165    conf = eina_list_sort(conf, -1, _evry_cb_plugin_sort);
166 
167    if (type == EVRY_PLUGIN_SUBJECT)
168      evry_conf->conf_subjects = conf;
169    else if (type == EVRY_PLUGIN_ACTION)
170      evry_conf->conf_actions = conf;
171    else if (type == EVRY_PLUGIN_OBJECT)
172      evry_conf->conf_objects = conf;
173 
174    if ((type == EVRY_PLUGIN_SUBJECT) && (p->name && strcmp(p->name, "All")))
175      {
176         char buf[256];
177         snprintf(buf, sizeof(buf), _("Show %s Plugin"), p->name);
178 
179         e_action_predef_name_set(N_("Everything Launcher"), buf,
180                                  "everything", p->name, NULL, 1);
181      }
182 
183    if (p->input_type)
184      {
185         Evry_Action *act;
186         char buf[256];
187         snprintf(buf, sizeof(buf), _("Browse %s"), EVRY_ITEM(p)->label);
188 
189         act = EVRY_ACTION_NEW(buf, p->input_type, 0, EVRY_ITEM(p)->icon,
190                               _evry_plugin_action_browse, NULL);
191         EVRY_ITEM(act)->icon_get = EVRY_ITEM(p)->icon_get;
192         EVRY_ITEM(act)->data = p;
193         evry_action_register(act, 1);
194         actions = eina_list_append(actions, act);
195      }
196 
197    return new_conf;
198 }
199 
200 void
evry_plugin_unregister(Evry_Plugin * p)201 evry_plugin_unregister(Evry_Plugin *p)
202 {
203    Eina_List *l = evry_conf->conf_subjects;
204 
205    if (l && eina_list_data_find_list(l, p->config))
206      {
207         char buf[256];
208         snprintf(buf, sizeof(buf), _("Show %s Plugin"), p->name);
209 
210         e_action_predef_name_del("Everything Launcher", buf);
211      }
212 }
213 
214 Evry_Plugin *
evry_plugin_find(const char * name)215 evry_plugin_find(const char *name)
216 {
217    Plugin_Config *pc = NULL;
218    Eina_List *l;
219    const char *n = eina_stringshare_add(name);
220 
221    EINA_LIST_FOREACH (evry_conf->conf_subjects, l, pc)
222      {
223         if (!pc->plugin) continue;
224         if (pc->name == n)
225           break;
226      }
227 
228    eina_stringshare_del(n);
229 
230    if (!pc) return NULL;
231 
232    return pc->plugin;
233 }
234 
235