1 #include <Elementary.h>
2 #include <Elementary_Cursor.h>
5 typedef struct _Elm_Store_Filesystem Elm_Store_Filesystem;
6 typedef struct _Elm_Store_Item_Filesystem Elm_Store_Item_Filesystem;
8 #define ELM_STORE_MAGIC 0x3f89ea56
9 #define ELM_STORE_FILESYSTEM_MAGIC 0x3f89ea57
10 #define ELM_STORE_ITEM_MAGIC 0x5afe8c1d
15 void (*free)(Elm_Store *store);
18 void (*free)(Elm_Store_Item *item);
21 Ecore_Thread *list_th;
30 Elm_Store_Item_List_Cb func;
35 Elm_Store_Item_Fetch_Cb func;
40 Elm_Store_Item_Unfetch_Cb func;
45 Eina_Bool fetch_thread : 1;
48 struct _Elm_Store_Item
53 Elm_Object_Item *item;
54 Ecore_Thread *fetch_th;
56 const Elm_Store_Item_Mapping *mapping;
60 Eina_Bool was_live : 1;
61 Eina_Bool realized : 1;
62 Eina_Bool fetched : 1;
65 struct _Elm_Store_Filesystem
72 struct _Elm_Store_Item_Filesystem
78 static Elm_Genlist_Item_Class _store_item_class;
81 _store_cache_trim(Elm_Store *st)
83 while ((st->realized ) &&
84 (((int)eina_list_count(st->realized) - st->realized_count)
87 Elm_Store_Item *sti = st->realized->data;
90 st->realized = eina_list_remove_list(st->realized, st->realized);
91 sti->realized = EINA_FALSE;
93 eina_lock_take(&sti->lock);
96 eina_lock_release(&sti->lock);
99 ecore_thread_cancel(sti->fetch_th);
100 sti->fetch_th = NULL;
102 eina_lock_take(&sti->lock);
104 sti->fetched = EINA_FALSE;
105 //// let fetch/unfetch do the locking
106 // eina_lock_release(&sti->lock);
107 if (st->cb.unfetch.func)
108 st->cb.unfetch.func(st->cb.unfetch.data, sti);
109 // eina_lock_take(&sti->lock);
111 eina_lock_release(&sti->lock);
116 _store_genlist_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
118 Elm_Store *st = data;
122 ecore_thread_cancel(st->list_th);
125 eina_list_free(st->realized);
128 Elm_Store_Item *sti = (Elm_Store_Item *)st->items;
129 if (sti->eval_job) ecore_job_del(sti->eval_job);
132 ecore_thread_cancel(sti->fetch_th);
133 sti->fetch_th = NULL;
135 if (sti->store->item.free) sti->store->item.free(sti);
136 eina_lock_take(&sti->lock);
139 if (st->cb.unfetch.func)
140 st->cb.unfetch.func(st->cb.unfetch.data, sti);
143 eina_lock_release(&sti->lock);
144 eina_lock_free(&sti->lock);
148 // FIXME: kill threads and more
151 ////// **** WARNING ***********************************************************
152 //// * This function runs inside a thread outside efl mainloop. Be careful! *
153 // ************************************************************************
154 /* TODO: refactor lock part into core? this does not depend on filesystm part */
156 _store_filesystem_fetch_do(void *data, Ecore_Thread *th __UNUSED__)
158 Elm_Store_Item *sti = data;
159 eina_lock_take(&sti->lock);
162 eina_lock_release(&sti->lock);
167 //// let fetch/unfetch do the locking
168 // eina_lock_release(&sti->lock);
169 if (sti->store->cb.fetch.func)
170 sti->store->cb.fetch.func(sti->store->cb.fetch.data, sti);
171 // eina_lock_take(&sti->lock);
172 sti->fetched = EINA_TRUE;
174 eina_lock_release(&sti->lock);
176 // ************************************************************************
177 //// * End of separate thread function. *
178 ////// ************************************************************************
179 /* TODO: refactor lock part into core? this does not depend on filesystm part */
181 _store_filesystem_fetch_end(void *data, Ecore_Thread *th)
183 Elm_Store_Item *sti = data;
184 eina_lock_take(&sti->lock);
185 if (sti->data) elm_genlist_item_update(sti->item);
186 eina_lock_release(&sti->lock);
187 if (th == sti->fetch_th) sti->fetch_th = NULL;
190 /* TODO: refactor lock part into core? this does not depend on filesystm part */
192 _store_filesystem_fetch_cancel(void *data, Ecore_Thread *th)
194 Elm_Store_Item *sti = data;
195 eina_lock_take(&sti->lock);
196 if (th == sti->fetch_th) sti->fetch_th = NULL;
197 if (sti->data) elm_genlist_item_update(sti->item);
198 eina_lock_release(&sti->lock);
202 _store_item_eval(void *data)
204 Elm_Store_Item *sti = data;
205 sti->eval_job = NULL;
206 if (sti->live == sti->was_live) return;
207 sti->was_live = sti->live;
210 _store_cache_trim(sti->store);
212 sti->store->realized = eina_list_remove(sti->store->realized, sti);
213 sti->store->realized = eina_list_append(sti->store->realized, sti);
214 sti->realized = EINA_TRUE;
215 if ((sti->store->fetch_thread) && (!sti->fetch_th))
216 sti->fetch_th = ecore_thread_run(_store_filesystem_fetch_do,
217 _store_filesystem_fetch_end,
218 _store_filesystem_fetch_cancel,
220 else if ((!sti->store->fetch_thread))
222 _store_filesystem_fetch_do(sti, NULL);
223 _store_filesystem_fetch_end(sti, NULL);
230 ecore_thread_cancel(sti->fetch_th);
231 sti->fetch_th = NULL;
233 _store_cache_trim(sti->store);
238 _store_genlist_item_realized(void *data, Evas_Object *obj __UNUSED__, void *event_info)
240 Elm_Store *st = data;
241 Elm_Object_Item *gli = event_info;
242 Elm_Store_Item *sti = elm_object_item_data_get(gli);
244 st->realized_count++;
245 sti->live = EINA_TRUE;
246 if (sti->eval_job) ecore_job_del(sti->eval_job);
247 sti->eval_job = ecore_job_add(_store_item_eval, sti);
251 _store_genlist_item_unrealized(void *data, Evas_Object *obj __UNUSED__, void *event_info)
253 Elm_Store *st = data;
254 Elm_Object_Item *gli = event_info;
255 Elm_Store_Item *sti = elm_object_item_data_get(gli);
257 st->realized_count--;
258 sti->live = EINA_FALSE;
259 if (sti->eval_job) ecore_job_del(sti->eval_job);
260 sti->eval_job = ecore_job_add(_store_item_eval, sti);
263 static const Elm_Store_Item_Mapping *
264 _store_item_mapping_find(Elm_Store_Item *sti, const char *part)
266 const Elm_Store_Item_Mapping *m;
268 for (m = sti->mapping; m; m ++)
270 if (m->type == ELM_STORE_ITEM_MAPPING_NONE) break;
271 if (!strcmp(part, m->part)) return m;
277 _store_item_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
279 Elm_Store_Item *sti = data;
281 eina_lock_take(&sti->lock);
284 const Elm_Store_Item_Mapping *m = _store_item_mapping_find(sti, part);
289 case ELM_STORE_ITEM_MAPPING_LABEL:
290 s = *(char **)(((unsigned char *)sti->data) + m->offset);
292 case ELM_STORE_ITEM_MAPPING_CUSTOM:
293 if (m->details.custom.func)
294 s = m->details.custom.func(sti->data, sti, part);
301 eina_lock_release(&sti->lock);
302 return s ? strdup(s) : NULL;
306 _store_item_content_get(void *data, Evas_Object *obj, const char *part)
308 Elm_Store_Item *sti = data;
309 eina_lock_take(&sti->lock);
312 const Elm_Store_Item_Mapping *m = _store_item_mapping_find(sti, part);
315 Evas_Object *ic = NULL;
316 const char *s = NULL;
320 case ELM_STORE_ITEM_MAPPING_ICON:
321 ic = elm_icon_add(obj);
322 s = *(char **)(((unsigned char *)sti->data) + m->offset);
323 elm_icon_order_lookup_set(ic, m->details.icon.lookup_order);
324 evas_object_size_hint_aspect_set(ic,
325 EVAS_ASPECT_CONTROL_VERTICAL,
328 elm_icon_smooth_set(ic, m->details.icon.smooth);
329 elm_icon_no_scale_set(ic, m->details.icon.no_scale);
330 elm_icon_resizable_set(ic,
331 m->details.icon.scale_up,
332 m->details.icon.scale_down);
335 if (m->details.icon.standard_name)
336 elm_icon_standard_set(ic, s);
338 elm_icon_file_set(ic, s, NULL);
341 case ELM_STORE_ITEM_MAPPING_PHOTO:
342 ic = elm_icon_add(obj);
343 s = *(char **)(((unsigned char *)sti->data) + m->offset);
344 elm_photo_size_set(ic, m->details.photo.size);
346 elm_photo_file_set(ic, s);
348 case ELM_STORE_ITEM_MAPPING_CUSTOM:
349 if (m->details.custom.func)
350 ic = m->details.custom.func(sti->data, sti, part);
355 eina_lock_release(&sti->lock);
359 eina_lock_release(&sti->lock);
364 _store_item_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__)
368 ////// **** WARNING ***********************************************************
369 //// * This function runs inside a thread outside efl mainloop. Be careful! *
370 // ************************************************************************
372 _store_filesystem_sort_cb(void *d1, void *d2)
374 Elm_Store_Item_Info *info1 = d1, *info2 = d2;
375 if ((!info1->sort_id) || (!info2->sort_id)) return 0;
376 return strcoll(info1->sort_id, info2->sort_id);
380 _store_filesystem_list_do(void *data, Ecore_Thread *th __UNUSED__)
382 Elm_Store_Filesystem *st = data;
384 const Eina_File_Direct_Info *finf;
385 Eina_List *sorted = NULL;
386 Elm_Store_Item_Info_Filesystem *info;
388 // FIXME: need a way to abstract the open, list, feed items from list
389 // and maybe get initial sortable key vals etc.
390 it = eina_file_stat_ls(st->dir);
392 EINA_ITERATOR_FOREACH(it, finf)
395 size_t pathsz = finf->path_length + 1;
397 if (finf->path[finf->name_start] == '.') continue ;
399 info = calloc(1, sizeof(Elm_Store_Item_Info_Filesystem) + pathsz);
401 info->path = ((char *)info) + sizeof(Elm_Store_Item_Info_Filesystem);
402 memcpy(info->path, finf->path, pathsz);
404 if (st->base.cb.list.func)
405 ok = st->base.cb.list.func(st->base.cb.list.data, &info->base);
408 if (!st->base.sorted) ecore_thread_feedback(th, info);
409 else sorted = eina_list_append(sorted, info);
413 if (info->base.sort_id) free(info->base.sort_id);
416 if (ecore_thread_check(th)) break;
418 eina_iterator_free(it);
421 sorted = eina_list_sort(sorted, 0,
422 EINA_COMPARE_CB(_store_filesystem_sort_cb));
423 EINA_LIST_FREE(sorted, info)
425 if (!ecore_thread_check(th)) ecore_thread_feedback(th, info);
429 // ************************************************************************
430 //// * End of separate thread function. *
431 ////// ************************************************************************
434 _store_filesystem_list_end(void *data, Ecore_Thread *th)
436 Elm_Store *st = data;
437 if (th == st->list_th) st->list_th = NULL;
441 _store_filesystem_list_cancel(void *data, Ecore_Thread *th)
443 Elm_Store *st = data;
444 if (th == st->list_th) st->list_th = NULL;
448 _store_filesystem_list_update(void *data, Ecore_Thread *th __UNUSED__, void *msg)
450 Elm_Store *st = data;
451 Elm_Store_Item_Filesystem *sti;
452 Elm_Genlist_Item_Class *itc;
453 Elm_Store_Item_Info_Filesystem *info = msg;
455 sti = calloc(1, sizeof(Elm_Store_Item_Filesystem));
457 eina_lock_new(&sti->base.lock);
458 EINA_MAGIC_SET(&(sti->base), ELM_STORE_ITEM_MAGIC);
459 sti->base.store = st;
460 sti->base.data = info->base.data;
461 sti->base.mapping = info->base.mapping;
462 sti->path = eina_stringshare_add(info->path);
464 itc = info->base.item_class;
465 if (!itc) itc = &_store_item_class;
468 itc->func.text_get = _store_item_text_get;
469 itc->func.content_get = _store_item_content_get;
470 itc->func.state_get = NULL; // FIXME: support state gets later
471 itc->func.del = _store_item_del;
474 // FIXME: handle being a parent (tree)
475 sti->base.item = elm_genlist_item_append(st->genlist, itc,
478 ELM_GENLIST_ITEM_NONE,
480 NULL/* func data */);
481 st->items = eina_inlist_append(st->items, (Eina_Inlist *)sti);
483 if (info->base.sort_id) free(info->base.sort_id);
489 _elm_store_new(size_t size)
491 Elm_Store *st = calloc(1, size);
492 EINA_SAFETY_ON_NULL_RETURN_VAL(st, NULL);
494 // TODO: BEGIN - move to elm_store_init()
495 eina_magic_string_set(ELM_STORE_MAGIC, "Elm_Store");
496 eina_magic_string_set(ELM_STORE_FILESYSTEM_MAGIC, "Elm_Store_Filesystem");
497 eina_magic_string_set(ELM_STORE_ITEM_MAGIC, "Elm_Store_Item");
498 // setup default item class (always the same) if list cb doesnt provide one
499 _store_item_class.item_style = "default";
500 _store_item_class.func.text_get = _store_item_text_get;
501 _store_item_class.func.content_get = _store_item_content_get;
502 _store_item_class.func.state_get = NULL; // FIXME: support state gets later
503 _store_item_class.func.del = _store_item_del;
504 // TODO: END - move to elm_store_init()
506 EINA_MAGIC_SET(st, ELM_STORE_MAGIC);
508 st->fetch_thread = EINA_TRUE;
511 #define elm_store_new(type) (type*)_elm_store_new(sizeof(type))
514 _elm_store_filesystem_free(Elm_Store *store)
516 Elm_Store_Filesystem *st = (Elm_Store_Filesystem *)store;
517 eina_stringshare_del(st->dir);
521 _elm_store_filesystem_item_free(Elm_Store_Item *item)
523 Elm_Store_Item_Filesystem *sti = (Elm_Store_Item_Filesystem *)item;
524 eina_stringshare_del(sti->path);
528 elm_store_filesystem_new(void)
530 Elm_Store_Filesystem *st = elm_store_new(Elm_Store_Filesystem);
531 EINA_SAFETY_ON_NULL_RETURN_VAL(st, NULL);
533 EINA_MAGIC_SET(st, ELM_STORE_FILESYSTEM_MAGIC);
534 st->base.free = _elm_store_filesystem_free;
535 st->base.item.free = _elm_store_filesystem_item_free;
541 elm_store_free(Elm_Store *st)
543 void (*item_free)(Elm_Store_Item *);
544 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
547 ecore_thread_cancel(st->list_th);
550 eina_list_free(st->realized);
551 item_free = st->item.free;
554 Elm_Store_Item *sti = (Elm_Store_Item *)st->items;
555 if (sti->eval_job) ecore_job_del(sti->eval_job);
558 ecore_thread_cancel(sti->fetch_th);
559 sti->fetch_th = NULL;
561 if (item_free) item_free(sti);
562 eina_lock_take(&sti->lock);
565 if (st->cb.unfetch.func)
566 st->cb.unfetch.func(st->cb.unfetch.data, sti);
569 eina_lock_release(&sti->lock);
570 eina_lock_free(&sti->lock);
575 evas_object_event_callback_del_full(st->genlist, EVAS_CALLBACK_DEL, _store_genlist_del, st);
576 evas_object_smart_callback_del(st->genlist, "realized", _store_genlist_item_realized);
577 evas_object_smart_callback_del(st->genlist, "unrealized", _store_genlist_item_unrealized);
578 elm_genlist_clear(st->genlist);
581 if (st->free) st->free(st);
586 elm_store_target_genlist_set(Elm_Store *st, Evas_Object *obj)
588 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
589 if (st->genlist == obj) return;
592 evas_object_event_callback_del_full(st->genlist, EVAS_CALLBACK_DEL, _store_genlist_del, st);
593 evas_object_smart_callback_del(st->genlist, "realized", _store_genlist_item_realized);
594 evas_object_smart_callback_del(st->genlist, "unrealized", _store_genlist_item_unrealized);
595 elm_genlist_clear(st->genlist);
598 if (!st->genlist) return;
599 evas_object_smart_callback_add(st->genlist, "realized", _store_genlist_item_realized, st);
600 evas_object_smart_callback_add(st->genlist, "unrealized", _store_genlist_item_unrealized, st);
601 evas_object_event_callback_add(st->genlist, EVAS_CALLBACK_DEL, _store_genlist_del, st);
602 elm_genlist_clear(st->genlist);
606 elm_store_filesystem_directory_set(Elm_Store *store, const char *dir)
608 Elm_Store_Filesystem *st = (Elm_Store_Filesystem *)store;
609 if (!EINA_MAGIC_CHECK(store, ELM_STORE_MAGIC)) return;
610 if (!EINA_MAGIC_CHECK(st, ELM_STORE_FILESYSTEM_MAGIC)) return;
613 ecore_thread_cancel(store->list_th);
614 store->list_th = NULL;
616 if (!eina_stringshare_replace(&st->dir, dir)) return;
617 store->list_th = ecore_thread_feedback_run(_store_filesystem_list_do,
618 _store_filesystem_list_update,
619 _store_filesystem_list_end,
620 _store_filesystem_list_cancel,
625 elm_store_filesystem_directory_get(const Elm_Store *store)
627 const Elm_Store_Filesystem *st = (const Elm_Store_Filesystem *)store;
628 if (!EINA_MAGIC_CHECK(store, ELM_STORE_MAGIC)) return NULL;
629 if (!EINA_MAGIC_CHECK(st, ELM_STORE_FILESYSTEM_MAGIC)) return NULL;
634 elm_store_cache_set(Elm_Store *st, int max)
636 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
637 if (max < 0) max = 0;
639 _store_cache_trim(st);
643 elm_store_cache_get(const Elm_Store *st)
645 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return 0;
646 return st->cache_max;
650 elm_store_list_func_set(Elm_Store *st, Elm_Store_Item_List_Cb func, const void *data)
652 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
653 st->cb.list.func = func;
654 st->cb.list.data = (void *)data;
658 elm_store_fetch_func_set(Elm_Store *st, Elm_Store_Item_Fetch_Cb func, const void *data)
660 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
661 st->cb.fetch.func = func;
662 st->cb.fetch.data = (void *)data;
666 elm_store_fetch_thread_set(Elm_Store *st, Eina_Bool use_thread)
668 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
669 st->fetch_thread = !!use_thread;
673 elm_store_fetch_thread_get(const Elm_Store *st)
675 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return EINA_FALSE;
676 return st->fetch_thread;
680 elm_store_unfetch_func_set(Elm_Store *st, Elm_Store_Item_Unfetch_Cb func, const void *data)
682 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
683 st->cb.unfetch.func = func;
684 st->cb.unfetch.data = (void *)data;
688 elm_store_sorted_set(Elm_Store *st, Eina_Bool sorted)
690 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
695 elm_store_sorted_get(const Elm_Store *st)
697 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return EINA_FALSE;
702 elm_store_item_data_set(Elm_Store_Item *sti, void *data)
704 if (!EINA_MAGIC_CHECK(sti, ELM_STORE_ITEM_MAGIC)) return;
705 //// let fetch/unfetch do the locking
706 // eina_lock_take(&sti->lock);
708 // eina_lock_release(&sti->lock);
712 elm_store_item_data_get(Elm_Store_Item *sti)
714 if (!EINA_MAGIC_CHECK(sti, ELM_STORE_ITEM_MAGIC)) return NULL;
716 //// let fetch/unfetch do the locking
717 // eina_lock_take(&sti->lock);
719 // eina_lock_release(&sti->lock);
723 EAPI const Elm_Store *
724 elm_store_item_store_get(const Elm_Store_Item *sti)
726 if (!EINA_MAGIC_CHECK(sti, ELM_STORE_ITEM_MAGIC)) return NULL;
731 EAPI const Elm_Object_Item *
732 elm_store_item_genlist_item_get(const Elm_Store_Item *sti)
734 if (!EINA_MAGIC_CHECK(sti, ELM_STORE_ITEM_MAGIC)) return NULL;
740 elm_store_item_filesystem_path_get(const Elm_Store_Item *item)
742 Elm_Store_Item_Filesystem *sti = (Elm_Store_Item_Filesystem *)item;
743 Elm_Store_Filesystem *st;
744 if (!EINA_MAGIC_CHECK(item, ELM_STORE_ITEM_MAGIC)) return NULL;
745 if (!EINA_MAGIC_CHECK(item->store, ELM_STORE_MAGIC)) return NULL;
746 /* ensure we're dealing with filesystem item */
747 st = (Elm_Store_Filesystem *)item->store;
748 if (!EINA_MAGIC_CHECK(st, ELM_STORE_FILESYSTEM_MAGIC)) return NULL;