fixed plugin image size problem
[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
9 #ifdef HAVE_CONFIG_H
10 # include "elementary_config.h"
11 #endif
12 #include <Elementary.h>
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 static const Elm_Store_Item_Mapping it1_mapping[] =
45 {
46   {
47     ELM_STORE_ITEM_MAPPING_LABEL,
48       "elm.title.1", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, from),
49       { .empty = {
50         EINA_TRUE
51       } } },
52   {
53     ELM_STORE_ITEM_MAPPING_LABEL,
54       "elm.title.2", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, subject),
55       { .empty = {
56         EINA_TRUE
57       } } },
58   {
59     ELM_STORE_ITEM_MAPPING_LABEL,
60       "elm.text", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, head_content),
61       { .empty = {
62         EINA_TRUE
63       } } },
64   {
65     ELM_STORE_ITEM_MAPPING_ICON,
66       "elm.swallow.icon", 0,
67       { .icon = {
68         48, 48,
69         ELM_ICON_LOOKUP_THEME_FDO,
70         EINA_TRUE, EINA_FALSE,
71         EINA_TRUE,
72         EINA_FALSE, EINA_FALSE,
73       } } },
74   {
75     ELM_STORE_ITEM_MAPPING_CUSTOM,
76       "elm.swallow.end", 0,
77       { .custom = {
78         NULL
79       } } },
80   ELM_STORE_ITEM_MAPPING_END
81 };
82
83
84 ////// **** WARNING ***********************************************************
85 ////   * This function runs inside a thread outside efl mainloop. Be careful! *
86 //     ************************************************************************
87 static Eina_Bool
88 _st_store_list(void *data __UNUSED__, Elm_Store_Item_Info *item_info)
89 {
90    Elm_Store_Item_Info_Filesystem *info = (Elm_Store_Item_Info_Filesystem *)item_info;
91    int id;
92    char sort_id[7];
93
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, '/');
98    if (file) file++;
99    else file = info->path;
100    id = atoi(file);
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;
107    sort_id[6] = 0;
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
116 }
117 //     ************************************************************************
118 ////   * End of separate thread function.                                     *
119 ////// ************************************************************************
120
121 ////// **** WARNING ***********************************************************
122 ////   * This function runs inside a thread outside efl mainloop. Be careful! *
123 //     ************************************************************************
124 static void
125 _st_store_fetch(void *data __UNUSED__, Elm_Store_Item *sti)
126 {
127    const char *path = elm_store_item_filesystem_path_get(sti);
128    My_Item *myit;
129    FILE *f;
130    char buf[4096], *p;
131    Eina_Bool have_content = EINA_FALSE;
132    char *content = NULL, *content_pos = NULL, *content_end = NULL;
133
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");
138    if (!f) return;
139
140    // alloc my item in memory that holds data to show in the list
141    myit = calloc(1, sizeof(My_Item));
142    if (!myit)
143      {
144         fclose(f);
145         return;
146      }
147    while (fgets(buf, sizeof(buf), f))
148      {
149         if (!have_content)
150           {
151              if (!isblank(buf[0]))
152                {
153                   // get key: From:, Subject: etc.
154                   if (!strncmp(buf, "From:", 5))
155                     {
156                        p = buf + 5;
157                        while ((*p) && (isblank(*p))) p++;
158                        p = strdup(p);
159                        if (p)
160                          {
161                             myit->from = p;
162                             p = strchr(p, '\n');
163                             if (p) *p = 0;
164                          }
165                     }
166                   else if (!strncmp(buf, "Subject:", 8))
167                     {
168                        p = buf + 8;
169                        while ((*p) && (isblank(*p))) p++;
170                        p = strdup(p);
171                        if (p)
172                          {
173                             myit->subject = p;
174                             p = strchr(p, '\n');
175                             if (p) *p = 0;
176                          }
177                     }
178                   else if (!strncmp(buf, "Date:", 5))
179                     {
180                        p = buf + 5;
181                        while ((*p) && (isblank(*p))) p++;
182                        p = strdup(p);
183                        if (p)
184                          {
185                             myit->date = p;
186                             p = strchr(p, '\n');
187                             if (p) *p = 0;
188                          }
189                     }
190                   else if (buf[0] == '\n') // begin of content
191                     have_content = EINA_TRUE;
192                }
193           }
194         else
195           {
196              // get first 320 bytes of content/body
197              if (!content)
198                {
199                   content = calloc(1, 320);
200                   content_pos = content;
201                   content_end = content + 319;
202                }
203              strncat(content_pos, buf, content_end - content_pos - 1);
204              content_pos = content + strlen(content);
205           }
206      }
207    fclose(f);
208    myit->head_content = elm_entry_utf8_to_markup(content);
209    free(content);
210    elm_store_item_data_set(sti, myit);
211 }
212 //     ************************************************************************
213 ////   * End of separate thread function.                                     *
214 ////// ************************************************************************
215
216 static void
217 _st_store_unfetch(void *data __UNUSED__, Elm_Store_Item *sti)
218 {
219    My_Item *myit = elm_store_item_data_get(sti);
220    if (!myit) return;
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);
225    free(myit);
226 }
227
228 void
229 test_store(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
230 {
231    Evas_Object *win, *gl, *bx;
232    Elm_Store *st;
233
234    win = elm_win_util_standard_add("store", "Store");
235    elm_win_autodel_set(win, EINA_TRUE);
236
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);
241
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);
251
252    itc1 = elm_genlist_item_class_new();
253    itc1->item_style = "message";
254
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");
263
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);
267
268    evas_object_resize(win, 480, 800);
269    evas_object_show(win);
270 }
271 #endif