summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 7340412f7d470a3b698f412ab45a3c9fad13fd38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "wiki.h"
#include <Ecore_Con.h>
#include <Elementary.h>

static Evas_Object *win, *popup;
static Ecore_Timer *timer = NULL;
static Wiki *w;

static void
_wiki_item_clicked_twice(void *data EINA_UNUSED, Evas_Object *obj, void *event_info)
{
   evas_object_smart_callback_call(win, "gadget_open_uri", elm_object_item_data_get(event_info));
}

static void
_close_popup(void)
{
  evas_object_del(popup);
  popup = NULL;  
}

static Eina_Bool
_key_up(void *data, int type, void *event)
{
  Ecore_Event_Key *key = event;

  if (key->key && !strcmp(key->key, "Escape"))
    _close_popup();

  return ECORE_CALLBACK_PASS_ON;
}

static void
_focus_out(void *data, Evas_Object *obj, void *event_info)
{
  _close_popup();
}

static Eina_Value
_wiki_arrived(void *data, const Eina_Value value, const Eina_Future *f)
{
   Evas_Object *entry = data, *list;

   if (popup)
     evas_object_del(popup);

   //this creates a new popup due to runner
   popup = elm_win_add(elm_win_get(entry), "win", ELM_WIN_POPUP_MENU);
   elm_win_alpha_set(popup, 1);

   evas_object_smart_callback_add(popup, "focus,out", _focus_out, NULL);

   list = elm_list_add(popup);
   evas_object_smart_callback_add(list, "clicked,double", _wiki_item_clicked_twice, NULL);

   for (int i = 0; i < eina_value_array_count(&value); ++i)
    {
       const char *url, *desc, *name;

       //get the data from the eina value
       Eina_Value list_val;
       Eina_Value *st = eina_value_struct_new(public_wiki_struct());

       eina_value_array_get(&value, i, &list_val);

       eina_value_set(st, list_val);

       eina_value_struct_get(st, "name", &name);
       eina_value_struct_get(st, "description", &desc);
       eina_value_struct_get(st, "url", &url);

       eina_value_free(st);

       //append it to the list
       Elm_Object_Item *item = elm_list_item_append(list, name, NULL, NULL, NULL, url);
       elm_object_item_tooltip_text_set(item, desc);
    }

   evas_object_size_hint_min_set(list, 200, 150);
   evas_object_show(list);

   elm_win_resize_object_add(popup, list);

   evas_object_show(popup);

   return value;
}


/*
int main(int argc, char const *argv[])
{
  ecore_init();
  ecore_con_init();
  ecore_con_url_init();
  Wiki *w;

  w = wiki_new(ecore_main_loop_get(), "en.wikipedia.org");


  ecore_main_loop_begin();

  ecore_con_url_shutdown();
  ecore_con_shutdown();
  ecore_shutdown();
  return 0;
}
*/
static Eina_Bool
_done(void *data)
{
   Eina_Future *p;
   const char *search_text = elm_object_text_get(data);

   //if there is search text, then search the wiki
   if (search_text && strlen(search_text) != 0)
     {
        p = wiki_remote_fetch(w, search_text);
        eina_future_then(p, _wiki_arrived, data);
     }

   timer = NULL;
   return ECORE_CALLBACK_CANCEL;
}

static void
_changed(void *data EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   //for the case a timer is still there, extend it
   if (timer)
     ecore_timer_reset(timer);
   //otherwise just create a new timer
   else
     timer = ecore_timer_add(0.5, _done, obj);
}


int elm_main(int argc, char *argv[])
{
  Evas_Object *o;

  //create the wiki instance
  w = wiki_new(ecore_main_loop_get(), "en.wikipedia.org");

  ecore_event_handler_add(ECORE_EVENT_KEY_UP, _key_up, NULL);

  //new window
  win = elm_win_add(NULL, "wiked", ELM_WIN_BASIC);
  elm_win_title_set(win, "Wiked");
  elm_win_autodel_set(win, EINA_TRUE);
  elm_win_alpha_set(win, EINA_TRUE);
  evas_object_size_hint_aspect_set(win, EVAS_ASPECT_CONTROL_BOTH, 5, 1);

  //setup the entry we prompt from
  o = elm_entry_add(win);
  evas_object_smart_callback_add(o, "changed", _changed, NULL);
  elm_entry_single_line_set(o, EINA_TRUE);
  elm_entry_scrollable_set(o, EINA_TRUE);
  elm_object_part_text_set(o, "guide", "Keyword");
  evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL);
  evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  evas_object_show(o);

  elm_win_resize_object_add(win, o);
  evas_object_show(win);

  //run app RUN!
  elm_run();

  //shutdown!
  wiki_free(w);
  return 0;
}

ELM_MAIN();