Merge remote-tracking branch 'remotes/origin/upstream'
[framework/uifw/elementary.git] / src / bin / test_store.c
1 /* NOTE : Before testing elm_store,
2           email data files should exist in your local storage.
3           And you can just get example files in enlightenment website.
4           Use wget to obtain it. It almost 50 Megabytes.
5           http://www.enlightenment.org/~raster/store.tar.gz
6  */
7
8 #include <Elementary.h>
9
10 #ifdef HAVE_CONFIG_H
11 # include "elementary_config.h"
12 #endif
13 #ifndef ELM_LIB_QUICKLAUNCH
14
15 typedef struct _My_Item My_Item;
16
17 struct _My_Item
18 {
19   char *from, *subject, *date, *head_content;
20 };
21
22 // callbacks just to see user interacting with genlist
23 static void
24 _st_selected(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
25 {
26    printf("selected: %p\n", event_info);
27 }
28
29 static void
30 _st_double_clicked(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
31 {
32    printf("double clicked: %p\n", event_info);
33 }
34
35 static void
36 _st_longpress(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
37 {
38    printf("longpress %p\n", event_info);
39 }
40
41 // store callbacks to handle loading/parsing/freeing of store items from src
42 <<<<<<< HEAD
43 static Elm_Genlist_Item_Class itc1 =
44 {
45   "message", { NULL, NULL, NULL, NULL}
46 };
47 =======
48 static Elm_Genlist_Item_Class *itc1;
49 >>>>>>> remotes/origin/upstream
50
51 static const Elm_Store_Item_Mapping it1_mapping[] =
52 {
53   {
54     ELM_STORE_ITEM_MAPPING_LABEL,
55       "elm.title.1", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, from),
56       { .empty = {
57         EINA_TRUE
58       } } },
59   {
60     ELM_STORE_ITEM_MAPPING_LABEL,
61       "elm.title.2", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, subject),
62       { .empty = {
63         EINA_TRUE
64       } } },
65   {
66     ELM_STORE_ITEM_MAPPING_LABEL,
67       "elm.text", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, head_content),
68       { .empty = {
69         EINA_TRUE
70       } } },
71   {
72     ELM_STORE_ITEM_MAPPING_ICON,
73       "elm.swallow.icon", 0,
74       { .icon = {
75         48, 48,
76         ELM_ICON_LOOKUP_THEME_FDO,
77         EINA_TRUE, EINA_FALSE,
78         EINA_TRUE,
79         EINA_FALSE, EINA_FALSE,
80       } } },
81   {
82     ELM_STORE_ITEM_MAPPING_CUSTOM,
83       "elm.swallow.end", 0,
84       { .custom = {
85         NULL
86       } } },
87   ELM_STORE_ITEM_MAPPING_END
88 };
89
90
91 ////// **** WARNING ***********************************************************
92 ////   * This function runs inside a thread outside efl mainloop. Be careful! *
93 //     ************************************************************************
94 static Eina_Bool
95 _st_store_list(void *data __UNUSED__, Elm_Store_Item_Info *item_info)
96 {
97 <<<<<<< HEAD
98   Elm_Store_Item_Info_Filesystem *info = (Elm_Store_Item_Info_Filesystem *)item_info;
99   int id;
100   char sort_id[7];
101
102   // create a sort id based on the filename itself assuming it is a numeric
103   // value like the id number in mh mail folders which is what this test
104   // uses as a data source
105   char *file = strrchr(info->path, '/');
106   if (file) file++;
107   else file = info->path;
108   id = atoi(file);
109   sort_id[0] = ((id >> 30) & 0x3f) + 32;
110   sort_id[1] = ((id >> 24) & 0x3f) + 32;
111   sort_id[2] = ((id >> 18) & 0x3f) + 32;
112   sort_id[3] = ((id >> 12) & 0x3f) + 32;
113   sort_id[4] = ((id >>  6) & 0x3f) + 32;
114   sort_id[5] = ((id >>  0) & 0x3f) + 32;
115   sort_id[6] = 0;
116   info->base.sort_id = strdup(sort_id);
117   // choose the item genlist item class to use (only item style should be
118   // provided by the app, store will fill everything else in, so it also
119   // has to be writable
120   info->base.item_class = &itc1; // based on item info - return the item class wanted (only style field used - rest reset to internal funcs store sets up to get label/icon etc)
121   info->base.mapping = it1_mapping;
122   info->base.data = NULL; // if we can already parse and load all of item here and want to - set this
123   return EINA_TRUE; // return true to include this, false not to
124 =======
125    Elm_Store_Item_Info_Filesystem *info = (Elm_Store_Item_Info_Filesystem *)item_info;
126    int id;
127    char sort_id[7];
128
129    // create a sort id based on the filename itself assuming it is a numeric
130    // value like the id number in mh mail folders which is what this test
131    // uses as a data source
132    char *file = strrchr(info->path, '/');
133    if (file) file++;
134    else file = info->path;
135    id = atoi(file);
136    sort_id[0] = ((id >> 30) & 0x3f) + 32;
137    sort_id[1] = ((id >> 24) & 0x3f) + 32;
138    sort_id[2] = ((id >> 18) & 0x3f) + 32;
139    sort_id[3] = ((id >> 12) & 0x3f) + 32;
140    sort_id[4] = ((id >>  6) & 0x3f) + 32;
141    sort_id[5] = ((id >>  0) & 0x3f) + 32;
142    sort_id[6] = 0;
143    info->base.sort_id = strdup(sort_id);
144    // choose the item genlist item class to use (only item style should be
145    // provided by the app, store will fill everything else in, so it also
146    // has to be writable
147    info->base.item_class = itc1; // based on item info - return the item class wanted (only style field used - rest reset to internal funcs store sets up to get label/icon etc)
148    info->base.mapping = it1_mapping;
149    info->base.data = NULL; // if we can already parse and load all of item here and want to - set this
150    return EINA_TRUE; // return true to include this, false not to
151 >>>>>>> remotes/origin/upstream
152 }
153 //     ************************************************************************
154 ////   * End of separate thread function.                                     *
155 ////// ************************************************************************
156
157 ////// **** WARNING ***********************************************************
158 ////   * This function runs inside a thread outside efl mainloop. Be careful! *
159 //     ************************************************************************
160 static void
161 _st_store_fetch(void *data __UNUSED__, Elm_Store_Item *sti)
162 {
163 <<<<<<< HEAD
164   const char *path = elm_store_item_filesystem_path_get(sti);
165   My_Item *myit;
166   FILE *f;
167   char buf[4096], *p;
168   Eina_Bool have_content = EINA_FALSE;
169   char *content = NULL, *content_pos = NULL, *content_end = NULL;
170
171   // if we already have my item data - skip
172   if (elm_store_item_data_get(sti)) return;
173   // open the mail file and parse it
174   f = fopen(path, "rb");
175   if (!f) return;
176
177   // alloc my item in memory that holds data to show in the list
178   myit = calloc(1, sizeof(My_Item));
179   if (!myit)
180     {
181       fclose(f);
182       return;
183     }
184   while (fgets(buf, sizeof(buf), f))
185     {
186       if (!have_content)
187         {
188           if (!isblank(buf[0]))
189             {
190               // get key: From:, Subject: etc.
191               if (!strncmp(buf, "From:", 5))
192                 {
193                   p = buf + 5;
194                   while ((*p) && (isblank(*p))) p++;
195                   p = strdup(p);
196                   if (p)
197                     {
198                       myit->from = p;
199                       p = strchr(p, '\n');
200                       if (p) *p = 0;
201                     }
202                 }
203               else if (!strncmp(buf, "Subject:", 8))
204                 {
205                   p = buf + 8;
206                   while ((*p) && (isblank(*p))) p++;
207                   p = strdup(p);
208                   if (p)
209                     {
210                       myit->subject = p;
211                       p = strchr(p, '\n');
212                       if (p) *p = 0;
213                     }
214                 }
215               else if (!strncmp(buf, "Date:", 5))
216                 {
217                   p = buf + 5;
218                   while ((*p) && (isblank(*p))) p++;
219                   p = strdup(p);
220                   if (p)
221                     {
222                       myit->date = p;
223                       p = strchr(p, '\n');
224                       if (p) *p = 0;
225                     }
226                 }
227               else if (buf[0] == '\n') // begin of content
228                 have_content = EINA_TRUE;
229             }
230         }
231       else
232         {
233           // get first 320 bytes of content/body
234           if (!content)
235             {
236               content = calloc(1, 320);
237               content_pos = content;
238               content_end = content + 319;
239             }
240           strncat(content_pos, buf, content_end - content_pos - 1);
241           content_pos = content + strlen(content);
242         }
243     }
244   fclose(f);
245   myit->head_content = elm_entry_utf8_to_markup(content);
246   free(content);
247   elm_store_item_data_set(sti, myit);
248 =======
249    const char *path = elm_store_item_filesystem_path_get(sti);
250    My_Item *myit;
251    FILE *f;
252    char buf[4096], *p;
253    Eina_Bool have_content = EINA_FALSE;
254    char *content = NULL, *content_pos = NULL, *content_end = NULL;
255
256    // if we already have my item data - skip
257    if (elm_store_item_data_get(sti)) return;
258    // open the mail file and parse it
259    f = fopen(path, "rb");
260    if (!f) return;
261
262    // alloc my item in memory that holds data to show in the list
263    myit = calloc(1, sizeof(My_Item));
264    if (!myit)
265      {
266         fclose(f);
267         return;
268      }
269    while (fgets(buf, sizeof(buf), f))
270      {
271         if (!have_content)
272           {
273              if (!isblank(buf[0]))
274                {
275                   // get key: From:, Subject: etc.
276                   if (!strncmp(buf, "From:", 5))
277                     {
278                        p = buf + 5;
279                        while ((*p) && (isblank(*p))) p++;
280                        p = strdup(p);
281                        if (p)
282                          {
283                             myit->from = p;
284                             p = strchr(p, '\n');
285                             if (p) *p = 0;
286                          }
287                     }
288                   else if (!strncmp(buf, "Subject:", 8))
289                     {
290                        p = buf + 8;
291                        while ((*p) && (isblank(*p))) p++;
292                        p = strdup(p);
293                        if (p)
294                          {
295                             myit->subject = p;
296                             p = strchr(p, '\n');
297                             if (p) *p = 0;
298                          }
299                     }
300                   else if (!strncmp(buf, "Date:", 5))
301                     {
302                        p = buf + 5;
303                        while ((*p) && (isblank(*p))) p++;
304                        p = strdup(p);
305                        if (p)
306                          {
307                             myit->date = p;
308                             p = strchr(p, '\n');
309                             if (p) *p = 0;
310                          }
311                     }
312                   else if (buf[0] == '\n') // begin of content
313                     have_content = EINA_TRUE;
314                }
315           }
316         else
317           {
318              // get first 320 bytes of content/body
319              if (!content)
320                {
321                   content = calloc(1, 320);
322                   content_pos = content;
323                   content_end = content + 319;
324                }
325              strncat(content_pos, buf, content_end - content_pos - 1);
326              content_pos = content + strlen(content);
327           }
328      }
329    fclose(f);
330    myit->head_content = elm_entry_utf8_to_markup(content);
331    free(content);
332    elm_store_item_data_set(sti, myit);
333 >>>>>>> remotes/origin/upstream
334 }
335 //     ************************************************************************
336 ////   * End of separate thread function.                                     *
337 ////// ************************************************************************
338
339 static void
340 _st_store_unfetch(void *data __UNUSED__, Elm_Store_Item *sti)
341 {
342 <<<<<<< HEAD
343   My_Item *myit = elm_store_item_data_get(sti);
344   if (!myit) return;
345   if (myit->from) free(myit->from);
346   if (myit->subject) free(myit->subject);
347   if (myit->date) free(myit->date);
348   if (myit->head_content) free(myit->head_content);
349   free(myit);
350   elm_store_item_data_set(sti, NULL);
351 =======
352    My_Item *myit = elm_store_item_data_get(sti);
353    if (!myit) return;
354    if (myit->from) free(myit->from);
355    if (myit->subject) free(myit->subject);
356    if (myit->date) free(myit->date);
357    if (myit->head_content) free(myit->head_content);
358    free(myit);
359 >>>>>>> remotes/origin/upstream
360 }
361
362 void
363 test_store(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
364 {
365 <<<<<<< HEAD
366   Evas_Object *win, *bg, *gl, *bx;
367
368   Elm_Store *st;
369
370   win = elm_win_add(NULL, "store", ELM_WIN_BASIC);
371   elm_win_title_set(win, "Store");
372   elm_win_autodel_set(win, EINA_TRUE);
373
374   bg = elm_bg_add(win);
375   elm_win_resize_object_add(win, bg);
376   evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
377   evas_object_show(bg);
378
379   bx = elm_box_add(win);
380   evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
381   elm_win_resize_object_add(win, bx);
382   evas_object_show(bx);
383
384   gl = elm_genlist_add(win);
385   elm_genlist_height_for_width_mode_set(gl, EINA_TRUE);
386   evas_object_smart_callback_add(gl, "selected", _st_selected, NULL);
387   evas_object_smart_callback_add(gl, "clicked,double", _st_double_clicked, NULL);
388   evas_object_smart_callback_add(gl, "longpressed", _st_longpress, NULL);
389   evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
390   evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
391   elm_box_pack_end(bx, gl);
392   evas_object_show(gl);
393
394   st = elm_store_filesystem_new();
395   elm_store_list_func_set(st, _st_store_list, NULL);
396   elm_store_fetch_func_set(st, _st_store_fetch, NULL);
397   //elm_store_fetch_thread_set(st, EINA_FALSE);
398   elm_store_unfetch_func_set(st, _st_store_unfetch, NULL);
399   elm_store_sorted_set(st, EINA_TRUE);
400   elm_store_target_genlist_set(st, gl);
401   elm_store_filesystem_directory_set(st, "./store");
402
403   evas_object_resize(win, 480, 800);
404   evas_object_show(win);
405 =======
406    Evas_Object *win, *bg, *gl, *bx;
407
408    Elm_Store *st;
409
410    win = elm_win_add(NULL, "store", ELM_WIN_BASIC);
411    elm_win_title_set(win, "Store");
412    elm_win_autodel_set(win, EINA_TRUE);
413
414    bg = elm_bg_add(win);
415    elm_win_resize_object_add(win, bg);
416    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
417    evas_object_show(bg);
418
419    bx = elm_box_add(win);
420    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
421    elm_win_resize_object_add(win, bx);
422    evas_object_show(bx);
423
424    gl = elm_genlist_add(win);
425    elm_genlist_mode_set(gl, ELM_LIST_COMPRESS);
426    evas_object_smart_callback_add(gl, "selected", _st_selected, NULL);
427    evas_object_smart_callback_add(gl, "clicked,double", _st_double_clicked, NULL);
428    evas_object_smart_callback_add(gl, "longpressed", _st_longpress, NULL);
429    evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
430    evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
431    elm_box_pack_end(bx, gl);
432    evas_object_show(gl);
433
434    itc1 = elm_genlist_item_class_new();
435    itc1->item_style = "message";
436
437    st = elm_store_filesystem_new();
438    elm_store_list_func_set(st, _st_store_list, NULL);
439    elm_store_fetch_func_set(st, _st_store_fetch, NULL);
440    //elm_store_fetch_thread_set(st, EINA_FALSE);
441    elm_store_unfetch_func_set(st, _st_store_unfetch, NULL);
442    elm_store_sorted_set(st, EINA_TRUE);
443    elm_store_target_genlist_set(st, gl);
444    elm_store_filesystem_directory_set(st, "./store");
445
446    /* item_class_ref is needed for itc1. some items can be added in callbacks */
447    elm_genlist_item_class_ref(itc1);
448    elm_genlist_item_class_free(itc1);
449
450    evas_object_resize(win, 480, 800);
451    evas_object_show(win);
452 >>>>>>> remotes/origin/upstream
453 }
454 #endif