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