update for beta release
[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 EVRY_PLUGIN_HAS_ITEMS(p);
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)
207           evry->item_changed(it, 0, 0);
208      }
209
210    return ECORE_CALLBACK_PASS_ON;
211 }
212
213 static Eina_Bool
214 _cb_error(void *data, int type __UNUSED__, void *event)
215 {
216    Ecore_Exe_Event_Data *ev = event;
217    Evry_Plugin *p = data;
218
219    if (ev->exe != exe)
220      return ECORE_CALLBACK_PASS_ON;
221
222    p->items = eina_list_remove(p->items, cur_item);
223    EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_ADD);
224    error = 1;
225
226    return ECORE_CALLBACK_PASS_ON;
227 }
228
229 static Eina_Bool
230 _cb_del(void *data __UNUSED__, int type __UNUSED__, void *event)
231 {
232    Ecore_Exe_Event_Del *e = event;
233
234    if (e->exe != exe)
235      return ECORE_CALLBACK_PASS_ON;
236
237    exe = NULL;
238    return ECORE_CALLBACK_PASS_ON;
239 }
240
241 static int
242 _plugins_init(const Evry_API *_api)
243 {
244    evry = _api;
245
246    if (!evry->api_version_check(EVRY_API_VERSION))
247      return EINA_FALSE;
248
249    action_handler = evry->event_handler_add(EVRY_EVENT_ACTION_PERFORMED,
250                                             _cb_action_performed, NULL);
251
252    _plug = EVRY_PLUGIN_BASE("Calculator", _module_icon, EVRY_TYPE_TEXT,
253                             _begin, _finish, _fetch);
254
255    _plug->history = EINA_FALSE;
256    _plug->async_fetch = EINA_TRUE;
257
258    if (evry->plugin_register(_plug, EVRY_PLUGIN_SUBJECT, 0))
259      {
260         Plugin_Config *pc = _plug->config;
261         pc->view_mode = VIEW_MODE_LIST;
262         pc->trigger = eina_stringshare_add("=");
263         pc->trigger_only = EINA_TRUE;
264         pc->aggregate = EINA_FALSE;
265         /* pc->top_level = EINA_FALSE; */
266         /* pc->min_query = 3; */
267      }
268
269    return EINA_TRUE;
270 }
271
272 static void
273 _plugins_shutdown(void)
274 {
275    ecore_event_handler_del(action_handler);
276    action_handler = NULL;
277
278    EVRY_PLUGIN_FREE(_plug);
279 }
280
281 /***************************************************************************/
282
283 Eina_Bool
284 evry_plug_calc_init(E_Module *m __UNUSED__)
285 {
286    EVRY_MODULE_NEW(evry_module, evry, _plugins_init, _plugins_shutdown);
287
288    return EINA_TRUE;
289 }
290
291 void
292 evry_plug_calc_shutdown(void)
293 {
294    EVRY_MODULE_FREE(evry_module);
295 }
296
297 void
298 evry_plug_calc_save(void){}
299