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
10 # include "elementary_config.h"
12 #include <Elementary.h>
13 #ifndef ELM_LIB_QUICKLAUNCH
15 typedef struct _My_Item My_Item;
19 char *from, *subject, *date, *head_content;
22 // callbacks just to see user interacting with genlist
24 _st_selected(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
26 printf("selected: %p\n", event_info);
30 _st_double_clicked(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
32 printf("double clicked: %p\n", event_info);
36 _st_longpress(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
38 printf("longpress %p\n", event_info);
41 // store callbacks to handle loading/parsing/freeing of store items from src
42 static Elm_Genlist_Item_Class *itc1;
44 static const Elm_Store_Item_Mapping it1_mapping[] =
47 ELM_STORE_ITEM_MAPPING_LABEL,
48 "elm.title.1", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, from),
53 ELM_STORE_ITEM_MAPPING_LABEL,
54 "elm.title.2", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, subject),
59 ELM_STORE_ITEM_MAPPING_LABEL,
60 "elm.text", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, head_content),
65 ELM_STORE_ITEM_MAPPING_ICON,
66 "elm.swallow.icon", 0,
69 ELM_ICON_LOOKUP_THEME_FDO,
70 EINA_TRUE, EINA_FALSE,
72 EINA_FALSE, EINA_FALSE,
75 ELM_STORE_ITEM_MAPPING_CUSTOM,
80 ELM_STORE_ITEM_MAPPING_END
84 ////// **** WARNING ***********************************************************
85 //// * This function runs inside a thread outside efl mainloop. Be careful! *
86 // ************************************************************************
88 _st_store_list(void *data __UNUSED__, Elm_Store_Item_Info *item_info)
90 Elm_Store_Item_Info_Filesystem *info = (Elm_Store_Item_Info_Filesystem *)item_info;
94 // create a sort id based on the filename itself assuming it is a numeric
95 // value like the id number in mh mail folders which is what this test
96 // uses as a data source
97 char *file = strrchr(info->path, '/');
99 else file = info->path;
101 sort_id[0] = ((id >> 30) & 0x3f) + 32;
102 sort_id[1] = ((id >> 24) & 0x3f) + 32;
103 sort_id[2] = ((id >> 18) & 0x3f) + 32;
104 sort_id[3] = ((id >> 12) & 0x3f) + 32;
105 sort_id[4] = ((id >> 6) & 0x3f) + 32;
106 sort_id[5] = ((id >> 0) & 0x3f) + 32;
108 info->base.sort_id = strdup(sort_id);
109 // choose the item genlist item class to use (only item style should be
110 // provided by the app, store will fill everything else in, so it also
111 // has to be writable
112 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)
113 info->base.mapping = it1_mapping;
114 info->base.data = NULL; // if we can already parse and load all of item here and want to - set this
115 return EINA_TRUE; // return true to include this, false not to
117 // ************************************************************************
118 //// * End of separate thread function. *
119 ////// ************************************************************************
121 ////// **** WARNING ***********************************************************
122 //// * This function runs inside a thread outside efl mainloop. Be careful! *
123 // ************************************************************************
125 _st_store_fetch(void *data __UNUSED__, Elm_Store_Item *sti)
127 const char *path = elm_store_item_filesystem_path_get(sti);
131 Eina_Bool have_content = EINA_FALSE;
132 char *content = NULL, *content_pos = NULL, *content_end = NULL;
134 // if we already have my item data - skip
135 if (elm_store_item_data_get(sti)) return;
136 // open the mail file and parse it
137 f = fopen(path, "rb");
140 // alloc my item in memory that holds data to show in the list
141 myit = calloc(1, sizeof(My_Item));
147 while (fgets(buf, sizeof(buf), f))
151 if (!isblank(buf[0]))
153 // get key: From:, Subject: etc.
154 if (!strncmp(buf, "From:", 5))
157 while ((*p) && (isblank(*p))) p++;
166 else if (!strncmp(buf, "Subject:", 8))
169 while ((*p) && (isblank(*p))) p++;
178 else if (!strncmp(buf, "Date:", 5))
181 while ((*p) && (isblank(*p))) p++;
190 else if (buf[0] == '\n') // begin of content
191 have_content = EINA_TRUE;
196 // get first 320 bytes of content/body
199 content = calloc(1, 320);
200 content_pos = content;
201 content_end = content + 319;
203 strncat(content_pos, buf, content_end - content_pos - 1);
204 content_pos = content + strlen(content);
208 myit->head_content = elm_entry_utf8_to_markup(content);
210 elm_store_item_data_set(sti, myit);
212 // ************************************************************************
213 //// * End of separate thread function. *
214 ////// ************************************************************************
217 _st_store_unfetch(void *data __UNUSED__, Elm_Store_Item *sti)
219 My_Item *myit = elm_store_item_data_get(sti);
221 if (myit->from) free(myit->from);
222 if (myit->subject) free(myit->subject);
223 if (myit->date) free(myit->date);
224 if (myit->head_content) free(myit->head_content);
229 test_store(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
231 Evas_Object *win, *gl, *bx;
234 win = elm_win_util_standard_add("store", "Store");
235 elm_win_autodel_set(win, EINA_TRUE);
237 bx = elm_box_add(win);
238 evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
239 elm_win_resize_object_add(win, bx);
240 evas_object_show(bx);
242 gl = elm_genlist_add(win);
243 elm_genlist_mode_set(gl, ELM_LIST_COMPRESS);
244 evas_object_smart_callback_add(gl, "selected", _st_selected, NULL);
245 evas_object_smart_callback_add(gl, "clicked,double", _st_double_clicked, NULL);
246 evas_object_smart_callback_add(gl, "longpressed", _st_longpress, NULL);
247 evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
248 evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
249 elm_box_pack_end(bx, gl);
250 evas_object_show(gl);
252 itc1 = elm_genlist_item_class_new();
253 itc1->item_style = "message";
255 st = elm_store_filesystem_new();
256 elm_store_list_func_set(st, _st_store_list, NULL);
257 elm_store_fetch_func_set(st, _st_store_fetch, NULL);
258 //elm_store_fetch_thread_set(st, EINA_FALSE);
259 elm_store_unfetch_func_set(st, _st_store_unfetch, NULL);
260 elm_store_sorted_set(st, EINA_TRUE);
261 elm_store_target_genlist_set(st, gl);
262 elm_store_filesystem_directory_set(st, "./store");
264 /* item_class_ref is needed for itc1. some items can be added in callbacks */
265 elm_genlist_item_class_ref(itc1);
266 elm_genlist_item_class_free(itc1);
268 evas_object_resize(win, 480, 800);
269 evas_object_show(win);