ed1724e84780300fdd6bfda058c7963607a9aab6
[framework/uifw/e17.git] / src / modules / everything / evry_plug_calc.c
1 #include "e.h"
2 #include "evry_api.h"
3 // TODO - show error when input not parseable
4
5 typedef struct _Plugin Plugin;
6
7 struct _Plugin
8 {
9   Evry_Plugin base;
10 };
11
12 static Eina_Bool  _cb_data(void *data, int type, void *event);
13 static Eina_Bool  _cb_error(void *data, int type, void *event);
14 static Eina_Bool  _cb_del(void *data, int type, void *event);
15
16 static const Evry_API *evry = NULL;
17 static Evry_Module *evry_module = NULL;
18 static Evry_Plugin *_plug;
19 static Ecore_Event_Handler *action_handler = NULL;
20
21 static Ecore_Exe *exe = NULL;
22 static Eina_List *history = NULL;
23 static Eina_List *handlers = NULL;
24 static int error = 0;
25 static Eina_Bool active = EINA_FALSE;
26 static char _module_icon[] = "accessories-calculator";
27 static Evry_Item *cur_item = NULL;
28
29 static Evry_Plugin *
30 _begin(Evry_Plugin *plugin, const Evry_Item *item __UNUSED__)
31 {
32    Evry_Item *it;
33    Plugin *p;
34    
35    if (active)
36      return NULL;
37
38    EVRY_PLUGIN_INSTANCE(p, plugin)
39
40    if (history)
41      {
42         const char *result;
43
44         EINA_LIST_FREE(history, result)
45           {
46              it = EVRY_ITEM_NEW(Evry_Item, p, result, NULL, NULL);
47              it->context = eina_stringshare_ref(p->base.name);
48              p->base.items = eina_list_prepend(p->base.items, it);
49              eina_stringshare_del(result);
50           }
51      }
52
53    it = EVRY_ITEM_NEW(Evry_Item, p, "0", NULL, NULL);
54    it->context = eina_stringshare_ref(p->base.name);
55    cur_item = it;
56    active = EINA_TRUE;
57    
58    return EVRY_PLUGIN(p);
59 }
60
61 static int
62 _run_bc(Plugin *p)
63 {
64    handlers = eina_list_append
65      (handlers, ecore_event_handler_add
66       (ECORE_EXE_EVENT_DATA, _cb_data, p));
67    handlers = eina_list_append
68      (handlers, ecore_event_handler_add
69       (ECORE_EXE_EVENT_ERROR, _cb_error, p));
70    handlers = eina_list_append
71      (handlers, ecore_event_handler_add
72       (ECORE_EXE_EVENT_DEL, _cb_del, p));
73
74    exe = ecore_exe_pipe_run("bc -l",
75                             ECORE_EXE_PIPE_READ |
76                             ECORE_EXE_PIPE_READ_LINE_BUFFERED |
77                             ECORE_EXE_PIPE_WRITE |
78                             ECORE_EXE_PIPE_ERROR |
79                             ECORE_EXE_PIPE_ERROR_LINE_BUFFERED,
80                             NULL);
81    return !!exe;
82 }
83
84 static void
85 _finish(Evry_Plugin *plugin)
86 {
87    GET_PLUGIN(p, plugin);   
88    Ecore_Event_Handler *h;
89    Evry_Item *it;
90    int items = 0;
91
92    EINA_LIST_FREE(p->base.items, it)
93      {
94         if ((items++ > 1) && (items < 10))
95           history = eina_list_prepend(history, eina_stringshare_add(it->label));
96
97         EVRY_ITEM_FREE(it);
98      }
99
100    EINA_LIST_FREE(handlers, h)
101      ecore_event_handler_del(h);
102
103    if (exe)
104      {
105         ecore_exe_quit(exe);
106         ecore_exe_free(exe);
107         exe = NULL;
108      }
109    active = EINA_FALSE;
110
111    E_FREE(p);
112 }
113
114 static Eina_Bool
115 _cb_action_performed(__UNUSED__ void *data, __UNUSED__ int type, void *event)
116 {
117    Eina_List *l;
118    Evry_Item *it, *it2, *it_old;
119    Evry_Event_Action_Performed *ev = event;
120    Evry_Plugin *p = _plug;
121
122    if (!ev->it1 || !(ev->it1->plugin == p))
123      return ECORE_CALLBACK_PASS_ON;
124
125    if (!p->items)
126      return ECORE_CALLBACK_PASS_ON;
127
128    /* remove duplicates */
129    if (p->items->next)
130      {
131         it = p->items->data;
132
133         EINA_LIST_FOREACH(p->items->next, l, it2)
134           {
135              if (!strcmp(it->label, it2->label))
136                {
137                   p->items = eina_list_promote_list(p->items, l);
138                   evry->item_changed(it, 0, 1);
139                   EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_ADD);
140                   return ECORE_CALLBACK_PASS_ON;
141                }
142           }
143      }
144
145    it_old = p->items->data;
146    it_old->selected = EINA_FALSE;
147
148    it2 = EVRY_ITEM_NEW(Evry_Item, p, it_old->label, NULL, NULL);
149    it2->context = eina_stringshare_ref(p->name);
150    p->items = eina_list_prepend(p->items, it2);
151    evry->item_changed(it2, 0, 1);
152    EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_ADD);
153
154    return ECORE_CALLBACK_PASS_ON;
155 }
156
157 static int
158 _fetch(Evry_Plugin *plugin, const char *input)
159 {
160    GET_PLUGIN(p, plugin);
161    
162    char buf[1024];
163
164    if (!input) return 0;
165
166    if (!exe && !_run_bc(p)) return 0;
167
168    if (!strncmp(input, "scale=", 6))
169      snprintf(buf, 1024, "%s\n", input);
170    else
171      snprintf(buf, 1024, "scale=3;%s\n", input);
172
173    ecore_exe_send(exe, buf, strlen(buf));
174
175    /* XXX after error we get no response for first input ?! - send a
176       second time...*/
177    if (error)
178      {
179         ecore_exe_send(exe, buf, strlen(buf));
180         error = 0;
181      }
182
183    return !!(p->base.items);
184 }
185
186 static Eina_Bool
187 _cb_data(void *data, int type __UNUSED__, void *event)
188 {
189    Ecore_Exe_Event_Data *ev = event;
190    Evry_Plugin *p = data;
191    Evry_Item *it;
192
193    if (ev->exe != exe) return ECORE_CALLBACK_PASS_ON;
194
195    if (ev->lines)
196      {
197         it = cur_item;
198         eina_stringshare_del(it->label);
199         it->label = eina_stringshare_add(ev->lines->line);
200
201         if (!(it = eina_list_data_get(p->items)) || (it != cur_item))
202           {
203              p->items = eina_list_prepend(p->items, cur_item);
204              EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_ADD);
205           }
206         else if (it) evry->item_changed(it, 0, 0);
207      }
208
209    return ECORE_CALLBACK_PASS_ON;
210 }
211
212 static Eina_Bool
213 _cb_error(void *data, int type __UNUSED__, void *event)
214 {
215    Ecore_Exe_Event_Data *ev = event;
216    Evry_Plugin *p = data;
217    
218    if (ev->exe != exe)
219      return ECORE_CALLBACK_PASS_ON;
220
221    p->items = eina_list_remove(p->items, cur_item);
222    EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_ADD);
223    error = 1;
224
225    return ECORE_CALLBACK_PASS_ON;
226 }
227
228 static Eina_Bool
229 _cb_del(void *data __UNUSED__, int type __UNUSED__, void *event)
230 {
231    Ecore_Exe_Event_Del *e = event;
232
233    if (e->exe != exe)
234      return ECORE_CALLBACK_PASS_ON;
235
236    exe = NULL;
237    return ECORE_CALLBACK_PASS_ON;
238 }
239
240 static int
241 _plugins_init(const Evry_API *_api)
242 {
243    if (evry_module->active)
244      return EINA_TRUE;
245
246    evry = _api;
247
248    if (!evry->api_version_check(EVRY_API_VERSION))
249      return EINA_FALSE;
250
251    action_handler = evry->event_handler_add(EVRY_EVENT_ACTION_PERFORMED,
252                                             _cb_action_performed, NULL);
253
254    _plug = EVRY_PLUGIN_NEW(Evry_Plugin, N_("Calculator"),
255                         _module_icon,
256                         EVRY_TYPE_TEXT,
257                         _begin, _finish, _fetch, NULL);
258
259    _plug->history     = EINA_FALSE;
260    _plug->async_fetch = EINA_TRUE;
261
262    if (evry->plugin_register(_plug, EVRY_PLUGIN_SUBJECT, 0))
263      {
264         Plugin_Config *pc = _plug->config;
265         pc->view_mode = VIEW_MODE_LIST;
266         pc->trigger = eina_stringshare_add("=");
267         pc->trigger_only = EINA_TRUE;
268         pc->aggregate = EINA_FALSE;
269         /* pc->top_level = EINA_FALSE; */
270         /* pc->min_query = 3; */
271      }
272
273    return EINA_TRUE;
274 }
275
276 static void
277 _plugins_shutdown(void)
278 {
279    if (!evry_module->active) return;
280
281    ecore_event_handler_del(action_handler);
282    action_handler = NULL;
283
284    EVRY_PLUGIN_FREE(_plug);
285
286    evry_module->active = EINA_FALSE;
287 }
288
289 /***************************************************************************/
290
291 Eina_Bool
292 evry_plug_calc_init(E_Module *m)
293 {
294    EVRY_MODULE_NEW(evry_module, evry, _plugins_init, _plugins_shutdown);
295
296    return EINA_TRUE;
297 }
298
299 void
300 evry_plug_calc_shutdown(void)
301 {
302    EVRY_MODULE_FREE(evry_module);
303 }
304
305 void
306 evry_plug_calc_save(void){}