efl

Форк
0
/
test_store.c 
268 строк · 9.0 Кб
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
#ifdef HAVE_CONFIG_H
9
# include "elementary_config.h"
10
#endif
11
#include <Elementary.h>
12

13
typedef struct _My_Item My_Item;
14

15
struct _My_Item
16
{
17
  char *from, *subject, *date, *head_content;
18
};
19

20
// callbacks just to see user interacting with genlist
21
static void
22
_st_selected(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
23
{
24
   printf("selected: %p\n", event_info);
25
}
26

27
static void
28
_st_double_clicked(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
29
{
30
   printf("double clicked: %p\n", event_info);
31
}
32

33
static void
34
_st_longpress(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
35
{
36
   printf("longpress %p\n", event_info);
37
}
38

39
// store callbacks to handle loading/parsing/freeing of store items from src
40
static Elm_Genlist_Item_Class *itc1;
41

42
static const Elm_Store_Item_Mapping it1_mapping[] =
43
{
44
  {
45
    ELM_STORE_ITEM_MAPPING_LABEL,
46
      "elm.title.1", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, from),
47
      { .empty = {
48
        EINA_TRUE
49
      } } },
50
  {
51
    ELM_STORE_ITEM_MAPPING_LABEL,
52
      "elm.title.2", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, subject),
53
      { .empty = {
54
        EINA_TRUE
55
      } } },
56
  {
57
    ELM_STORE_ITEM_MAPPING_LABEL,
58
      "elm.text", ELM_STORE_ITEM_MAPPING_OFFSET(My_Item, head_content),
59
      { .empty = {
60
        EINA_TRUE
61
      } } },
62
  {
63
    ELM_STORE_ITEM_MAPPING_ICON,
64
      "elm.swallow.icon", 0,
65
      { .icon = {
66
        48, 48,
67
        ELM_ICON_LOOKUP_THEME_FDO,
68
        EINA_TRUE, EINA_FALSE,
69
        EINA_TRUE,
70
        EINA_FALSE, EINA_FALSE,
71
      } } },
72
  {
73
    ELM_STORE_ITEM_MAPPING_CUSTOM,
74
      "elm.swallow.end", 0,
75
      { .custom = {
76
        NULL
77
      } } },
78
  ELM_STORE_ITEM_MAPPING_END
79
};
80

81
////// **** WARNING ***********************************************************
82
////   * This function runs inside a thread outside efl mainloop. Be careful! *
83
//     ************************************************************************
84
static Eina_Bool
85
_st_store_list(void *data EINA_UNUSED, Elm_Store_Item_Info *item_info)
86
{
87
   Elm_Store_Item_Info_Filesystem *info = (Elm_Store_Item_Info_Filesystem *)item_info;
88
   int id;
89
   char sort_id[7];
90

91
   // create a sort id based on the filename itself assuming it is a numeric
92
   // value like the id number in mh mail folders which is what this test
93
   // uses as a data source
94
   char *file = strrchr(info->path, '/');
95
   if (file) file++;
96
   else file = info->path;
97
   id = atoi(file);
98
   sort_id[0] = ((id >> 30) & 0x3f) + 32;
99
   sort_id[1] = ((id >> 24) & 0x3f) + 32;
100
   sort_id[2] = ((id >> 18) & 0x3f) + 32;
101
   sort_id[3] = ((id >> 12) & 0x3f) + 32;
102
   sort_id[4] = ((id >>  6) & 0x3f) + 32;
103
   sort_id[5] = ((id >>  0) & 0x3f) + 32;
104
   sort_id[6] = 0;
105
   info->base.sort_id = strdup(sort_id);
106
   // choose the item genlist item class to use (only item style should be
107
   // provided by the app, store will fill everything else in, so it also
108
   // has to be writable
109
   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)
110
   info->base.mapping = it1_mapping;
111
   info->base.data = NULL; // if we can already parse and load all of item here and want to - set this
112
   return EINA_TRUE; // return true to include this, false not to
113
}
114
//     ************************************************************************
115
////   * End of separate thread function.                                     *
116
////// ************************************************************************
117

118
////// **** WARNING ***********************************************************
119
////   * This function runs inside a thread outside efl mainloop. Be careful! *
120
//     ************************************************************************
121
static void
122
_st_store_fetch(void *data EINA_UNUSED, Elm_Store_Item *sti)
123
{
124
   const char *path = elm_store_item_filesystem_path_get(sti);
125
   My_Item *myit;
126
   FILE *f;
127
   char buf[4096], *p;
128
   Eina_Bool have_content = EINA_FALSE;
129
   char *content = NULL, *content_pos = NULL, *content_end = NULL;
130

131
   // if we already have my item data - skip
132
   if (elm_store_item_data_get(sti)) return;
133
   // open the mail file and parse it
134
   f = fopen(path, "rb");
135
   if (!f) return;
136

137
   // alloc my item in memory that holds data to show in the list
138
   myit = calloc(1, sizeof(My_Item));
139
   if (!myit)
140
     {
141
        fclose(f);
142
        return;
143
     }
144
   while (fgets(buf, sizeof(buf), f))
145
     {
146
        if (!have_content)
147
          {
148
             if (!isblank(buf[0]))
149
               {
150
                  // get key: From:, Subject: etc.
151
                  if (!strncmp(buf, "From:", 5))
152
                    {
153
                       p = buf + 5;
154
                       while ((*p) && (isblank(*p))) p++;
155
                       p = strdup(p);
156
                       if (p)
157
                         {
158
                            myit->from = p;
159
                            p = strchr(p, '\n');
160
                            if (p) *p = 0;
161
                         }
162
                    }
163
                  else if (!strncmp(buf, "Subject:", 8))
164
                    {
165
                       p = buf + 8;
166
                       while ((*p) && (isblank(*p))) p++;
167
                       p = strdup(p);
168
                       if (p)
169
                         {
170
                            myit->subject = p;
171
                            p = strchr(p, '\n');
172
                            if (p) *p = 0;
173
                         }
174
                    }
175
                  else if (!strncmp(buf, "Date:", 5))
176
                    {
177
                       p = buf + 5;
178
                       while ((*p) && (isblank(*p))) p++;
179
                       p = strdup(p);
180
                       if (p)
181
                         {
182
                            myit->date = p;
183
                            p = strchr(p, '\n');
184
                            if (p) *p = 0;
185
                         }
186
                    }
187
                  else if (buf[0] == '\n') // begin of content
188
                    have_content = EINA_TRUE;
189
               }
190
          }
191
        else
192
          {
193
             // get first 320 bytes of content/body
194
             if (!content)
195
               {
196
                  content = calloc(1, 320);
197
                  content_pos = content;
198
                  content_end = content + 319;
199
               }
200
             strncat(content_pos, buf, content_end - content_pos - 1);
201
             content_pos = content + strlen(content);
202
          }
203
     }
204
   fclose(f);
205
   myit->head_content = elm_entry_utf8_to_markup(content);
206
   free(content);
207
   elm_store_item_data_set(sti, myit);
208
}
209
//     ************************************************************************
210
////   * End of separate thread function.                                     *
211
////// ************************************************************************
212

213
static void
214
_st_store_unfetch(void *data EINA_UNUSED, Elm_Store_Item *sti)
215
{
216
   My_Item *myit = elm_store_item_data_get(sti);
217
   if (!myit) return;
218
   if (myit->from) free(myit->from);
219
   if (myit->subject) free(myit->subject);
220
   if (myit->date) free(myit->date);
221
   if (myit->head_content) free(myit->head_content);
222
   free(myit);
223
}
224

225
void
226
test_store(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
227
{
228
   Evas_Object *win, *gl, *bx;
229
   Elm_Store *st;
230

231
   win = elm_win_util_standard_add("store", "Store");
232
   elm_win_autodel_set(win, EINA_TRUE);
233

234
   bx = elm_box_add(win);
235
   evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
236
   elm_win_resize_object_add(win, bx);
237
   evas_object_show(bx);
238

239
   gl = elm_genlist_add(win);
240
   elm_genlist_mode_set(gl, ELM_LIST_COMPRESS);
241
   evas_object_smart_callback_add(gl, "selected", _st_selected, NULL);
242
   evas_object_smart_callback_add(gl, "clicked,double", _st_double_clicked, NULL);
243
   evas_object_smart_callback_add(gl, "longpressed", _st_longpress, NULL);
244
   evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
245
   evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
246
   elm_box_pack_end(bx, gl);
247
   evas_object_show(gl);
248

249
   itc1 = elm_genlist_item_class_new();
250
   itc1->item_style = "message";
251

252
   st = elm_store_filesystem_new();
253
   elm_store_list_func_set(st, _st_store_list, NULL);
254
   elm_store_fetch_func_set(st, _st_store_fetch, NULL);
255
   //elm_store_fetch_thread_set(st, EINA_FALSE);
256
   elm_store_unfetch_func_set(st, _st_store_unfetch, NULL);
257
   elm_store_sorted_set(st, EINA_TRUE);
258
   elm_store_target_genlist_set(st, gl);
259
   elm_store_filesystem_directory_set(st, "./store");
260

261
   /* item_class_ref is needed for itc1. some items can be added in callbacks */
262
   elm_genlist_item_class_ref(itc1);
263
   elm_genlist_item_class_free(itc1);
264

265
   evas_object_resize(win, 480 * elm_config_scale_get(),
266
                           800 * elm_config_scale_get());
267
   evas_object_show(win);
268
}
269

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.