elm store: Added more comment for store test. Patch by Hyoyoung Chang
[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 static Elm_Genlist_Item_Class itc1 =
43 {
44   "message", { NULL, NULL, NULL, NULL}
45 };
46
47 static const Elm_Store_Item_Mapping it1_mapping[] =
48 {
49   {
50     ELM_STORE_ITEM_MAPPING_LABEL,
51       "elm.title.1", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, from),
52       { .empty = {
53         EINA_TRUE
54       } } },
55   {
56     ELM_STORE_ITEM_MAPPING_LABEL,
57       "elm.title.2", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, subject),
58       { .empty = {
59         EINA_TRUE
60       } } },
61   {
62     ELM_STORE_ITEM_MAPPING_LABEL,
63       "elm.text", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, head_content),
64       { .empty = {
65         EINA_TRUE
66       } } },
67   {
68     ELM_STORE_ITEM_MAPPING_ICON,
69       "elm.swallow.icon", 0,
70       { .icon = {
71         48, 48,
72         ELM_ICON_LOOKUP_THEME_FDO,
73         EINA_TRUE, EINA_FALSE,
74         EINA_TRUE,
75         EINA_FALSE, EINA_FALSE,
76       } } },
77   {
78     ELM_STORE_ITEM_MAPPING_CUSTOM,
79       "elm.swallow.end", 0,
80       { .custom = {
81         NULL
82       } } },
83   ELM_STORE_ITEM_MAPPING_END
84 };
85
86
87 ////// **** WARNING ***********************************************************
88 ////   * This function runs inside a thread outside efl mainloop. Be careful! *
89 //     ************************************************************************
90 static Eina_Bool
91 _st_store_list(void *data __UNUSED__, Elm_Store_Item_Info *item_info)
92 {
93   Elm_Store_Item_Info_Filesystem *info = (Elm_Store_Item_Info_Filesystem *)item_info;
94   int id;
95   char sort_id[7];
96
97   // create a sort id based on the filename itself assuming it is a numeric
98   // value like the id number in mh mail folders which is what this test
99   // uses as a data source
100   char *file = strrchr(info->path, '/');
101   if (file) file++;
102   else file = info->path;
103   id = atoi(file);
104   sort_id[0] = ((id >> 30) & 0x3f) + 32;
105   sort_id[1] = ((id >> 24) & 0x3f) + 32;
106   sort_id[2] = ((id >> 18) & 0x3f) + 32;
107   sort_id[3] = ((id >> 12) & 0x3f) + 32;
108   sort_id[4] = ((id >>  6) & 0x3f) + 32;
109   sort_id[5] = ((id >>  0) & 0x3f) + 32;
110   sort_id[6] = 0;
111   info->base.sort_id = strdup(sort_id);
112   // choose the item genlist item class to use (only item style should be
113   // provided by the app, store will fill everything else in, so it also
114   // has to be writable
115   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)
116   info->base.mapping = it1_mapping;
117   info->base.data = NULL; // if we can already parse and load all of item here and want to - set this
118   return EINA_TRUE; // return true to include this, false not to
119 }
120 //     ************************************************************************
121 ////   * End of separate thread function.                                     *
122 ////// ************************************************************************
123
124 ////// **** WARNING ***********************************************************
125 ////   * This function runs inside a thread outside efl mainloop. Be careful! *
126 //     ************************************************************************
127 static void
128 _st_store_fetch(void *data __UNUSED__, Elm_Store_Item *sti)
129 {
130   const char *path = elm_store_item_filesystem_path_get(sti);
131   My_Item *myit;
132   FILE *f;
133   char buf[4096], *p;
134   Eina_Bool have_content = EINA_FALSE;
135   char *content = NULL, *content_pos = NULL, *content_end = NULL;
136
137   // if we already have my item data - skip
138   if (elm_store_item_data_get(sti)) return;
139   // open the mail file and parse it
140   f = fopen(path, "rb");
141   if (!f) return;
142
143   // alloc my item in memory that holds data to show in the list
144   myit = calloc(1, sizeof(My_Item));
145   if (!myit)
146     {
147       fclose(f);
148       return;
149     }
150   while (fgets(buf, sizeof(buf), f))
151     {
152       if (!have_content)
153         {
154           if (!isblank(buf[0]))
155             {
156               // get key: From:, Subject: etc.
157               if (!strncmp(buf, "From:", 5))
158                 {
159                   p = buf + 5;
160                   while ((*p) && (isblank(*p))) p++;
161                   p = strdup(p);
162                   if (p)
163                     {
164                       myit->from = p;
165                       p = strchr(p, '\n');
166                       if (p) *p = 0;
167                     }
168                 }
169               else if (!strncmp(buf, "Subject:", 8))
170                 {
171                   p = buf + 8;
172                   while ((*p) && (isblank(*p))) p++;
173                   p = strdup(p);
174                   if (p)
175                     {
176                       myit->subject = p;
177                       p = strchr(p, '\n');
178                       if (p) *p = 0;
179                     }
180                 }
181               else if (!strncmp(buf, "Date:", 5))
182                 {
183                   p = buf + 5;
184                   while ((*p) && (isblank(*p))) p++;
185                   p = strdup(p);
186                   if (p)
187                     {
188                       myit->date = p;
189                       p = strchr(p, '\n');
190                       if (p) *p = 0;
191                     }
192                 }
193               else if (buf[0] == '\n') // begin of content
194                 have_content = EINA_TRUE;
195             }
196         }
197       else
198         {
199           // get first 320 bytes of content/body
200           if (!content)
201             {
202               content = calloc(1, 320);
203               content_pos = content;
204               content_end = content + 319;
205             }
206           strncat(content_pos, buf, content_end - content_pos - 1);
207           content_pos = content + strlen(content);
208         }
209     }
210   fclose(f);
211   myit->head_content = elm_entry_utf8_to_markup(content);
212   free(content);
213   elm_store_item_data_set(sti, myit);
214 }
215 //     ************************************************************************
216 ////   * End of separate thread function.                                     *
217 ////// ************************************************************************
218
219 static void
220 _st_store_unfetch(void *data __UNUSED__, Elm_Store_Item *sti)
221 {
222   My_Item *myit = elm_store_item_data_get(sti);
223   if (!myit) return;
224   if (myit->from) free(myit->from);
225   if (myit->subject) free(myit->subject);
226   if (myit->date) free(myit->date);
227   if (myit->head_content) free(myit->head_content);
228   free(myit);
229   elm_store_item_data_set(sti, NULL);
230 }
231
232 void
233 test_store(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
234 {
235   Evas_Object *win, *bg, *gl, *bx;
236
237   Elm_Store *st;
238
239   win = elm_win_add(NULL, "store", ELM_WIN_BASIC);
240   elm_win_title_set(win, "Store");
241   elm_win_autodel_set(win, EINA_TRUE);
242
243   bg = elm_bg_add(win);
244   elm_win_resize_object_add(win, bg);
245   evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
246   evas_object_show(bg);
247
248   bx = elm_box_add(win);
249   evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
250   elm_win_resize_object_add(win, bx);
251   evas_object_show(bx);
252
253   gl = elm_genlist_add(win);
254   elm_genlist_height_for_width_mode_set(gl, EINA_TRUE);
255   evas_object_smart_callback_add(gl, "selected", _st_selected, NULL);
256   evas_object_smart_callback_add(gl, "clicked,double", _st_double_clicked, NULL);
257   evas_object_smart_callback_add(gl, "longpressed", _st_longpress, NULL);
258   evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
259   evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
260   elm_box_pack_end(bx, gl);
261   evas_object_show(gl);
262
263   st = elm_store_filesystem_new();
264   elm_store_list_func_set(st, _st_store_list, NULL);
265   elm_store_fetch_func_set(st, _st_store_fetch, NULL);
266   //elm_store_fetch_thread_set(st, EINA_FALSE);
267   elm_store_unfetch_func_set(st, _st_store_unfetch, NULL);
268   elm_store_sorted_set(st, EINA_TRUE);
269   elm_store_target_genlist_set(st, gl);
270   elm_store_filesystem_directory_set(st, "./store");
271
272   evas_object_resize(win, 480, 800);
273   evas_object_show(win);
274 }
275 #endif