1 #include <Elementary.h>
2 #include <Elementary_Cursor.h>
5 #ifndef EFL_HAVE_THREADS
6 # error "No thread support. Required."
9 #ifdef EFL_HAVE_POSIX_THREADS
11 # define LK(x) pthread_mutex_t x
12 # define LKI(x) pthread_mutex_init(&(x), NULL);
13 # define LKD(x) pthread_mutex_destroy(&(x));
14 # define LKL(x) pthread_mutex_lock(&(x));
15 # define LKU(x) pthread_mutex_unlock(&(x));
16 #else /* EFL_HAVE_WIN32_THREADS */
17 # define WIN32_LEAN_AND_MEAN
19 # undef WIN32_LEAN_AND_MEAN
20 # define LK(x) HANDLE x
21 # define LKI(x) x = CreateMutex(NULL, FALSE, NULL)
22 # define LKD(x) CloseHandle(x)
23 # define LKL(x) WaitForSingleObject(x, INFINITE)
24 # define LKU(x) ReleaseMutex(x)
27 #define ELM_STORE_MAGIC 0x3f89ea56
28 #define ELM_STORE_FILESYSTEM_MAGIC 0x3f89ea57
29 #define ELM_STORE_ITEM_MAGIC 0x5afe8c1d
34 void (*free)(Elm_Store *store);
36 void (*free)(Elm_Store_Item *item);
39 Ecore_Thread *list_th;
46 Elm_Store_Item_List_Cb func;
50 Elm_Store_Item_Fetch_Cb func;
54 Elm_Store_Item_Unfetch_Cb func;
59 Eina_Bool fetch_thread : 1;
62 struct _Elm_Store_Item
67 Elm_Genlist_Item *item;
68 Ecore_Thread *fetch_th;
70 const Elm_Store_Item_Mapping *mapping;
74 Eina_Bool was_live : 1;
75 Eina_Bool realized : 1;
76 Eina_Bool fetched : 1;
79 struct _Elm_Store_Filesystem
86 struct _Elm_Store_Item_Filesystem
92 static Elm_Genlist_Item_Class _store_item_class;
95 _store_cache_trim(Elm_Store *st)
97 while ((st->realized ) &&
98 (((int)eina_list_count(st->realized) - st->realized_count)
101 Elm_Store_Item *sti = st->realized->data;
104 st->realized = eina_list_remove_list(st->realized, st->realized);
105 sti->realized = EINA_FALSE;
113 ecore_thread_cancel(sti->fetch_th);
114 sti->fetch_th = NULL;
118 sti->fetched = EINA_FALSE;
120 if (st->cb.unfetch.func)
121 st->cb.unfetch.func(st->cb.unfetch.data, sti);
129 _store_genlist_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
131 Elm_Store *st = data;
135 ecore_thread_cancel(st->list_th);
138 eina_list_free(st->realized);
141 Elm_Store_Item *sti = (Elm_Store_Item *)st->items;
142 if (sti->eval_job) ecore_job_del(sti->eval_job);
145 ecore_thread_cancel(sti->fetch_th);
146 sti->fetch_th = NULL;
148 if (sti->store->item.free) sti->store->item.free(sti);
151 if (st->cb.unfetch.func)
152 st->cb.unfetch.func(st->cb.unfetch.data, sti);
159 // FIXME: kill threads and more
162 ////// **** WARNING ***********************************************************
163 //// * This function runs inside a thread outside efl mainloop. Be careful! *
164 // ************************************************************************
165 /* TODO: refactor lock part into core? this does not depend on filesystm part */
167 _store_filesystem_fetch_do(void *data, Ecore_Thread *th __UNUSED__)
169 Elm_Store_Item *sti = data;
179 if (sti->store->cb.fetch.func)
180 sti->store->cb.fetch.func(sti->store->cb.fetch.data, sti);
182 sti->fetched = EINA_TRUE;
186 // ************************************************************************
187 //// * End of separate thread function. *
188 ////// ************************************************************************
189 /* TODO: refactor lock part into core? this does not depend on filesystm part */
191 _store_filesystem_fetch_end(void *data, Ecore_Thread *th)
193 Elm_Store_Item *sti = data;
195 if (sti->data) elm_genlist_item_update(sti->item);
197 if (th == sti->fetch_th) sti->fetch_th = NULL;
200 /* TODO: refactor lock part into core? this does not depend on filesystm part */
202 _store_filesystem_fetch_cancel(void *data, Ecore_Thread *th)
204 Elm_Store_Item *sti = data;
206 if (th == sti->fetch_th) sti->fetch_th = NULL;
207 if (sti->data) elm_genlist_item_update(sti->item);
212 _store_item_eval(void *data)
214 Elm_Store_Item *sti = data;
215 sti->eval_job = NULL;
216 if (sti->live == sti->was_live) return;
217 sti->was_live = sti->live;
220 _store_cache_trim(sti->store);
222 sti->store->realized = eina_list_remove(sti->store->realized, sti);
223 sti->store->realized = eina_list_append(sti->store->realized, sti);
224 sti->realized = EINA_TRUE;
225 if ((sti->store->fetch_thread) && (!sti->fetch_th))
226 sti->fetch_th = ecore_thread_run(_store_filesystem_fetch_do,
227 _store_filesystem_fetch_end,
228 _store_filesystem_fetch_cancel,
230 else if ((!sti->store->fetch_thread))
232 _store_filesystem_fetch_do(sti, NULL);
233 _store_filesystem_fetch_end(sti, NULL);
240 ecore_thread_cancel(sti->fetch_th);
241 sti->fetch_th = NULL;
243 _store_cache_trim(sti->store);
248 _store_genlist_item_realized(void *data, Evas_Object *obj __UNUSED__, void *event_info)
250 Elm_Store *st = data;
251 Elm_Genlist_Item *gli = event_info;
252 Elm_Store_Item *sti = elm_genlist_item_data_get(gli);
254 st->realized_count++;
255 sti->live = EINA_TRUE;
256 if (sti->eval_job) ecore_job_del(sti->eval_job);
257 sti->eval_job = ecore_job_add(_store_item_eval, sti);
261 _store_genlist_item_unrealized(void *data, Evas_Object *obj __UNUSED__, void *event_info)
263 Elm_Store *st = data;
264 Elm_Genlist_Item *gli = event_info;
265 Elm_Store_Item *sti = elm_genlist_item_data_get(gli);
267 st->realized_count--;
268 sti->live = EINA_FALSE;
269 if (sti->eval_job) ecore_job_del(sti->eval_job);
270 sti->eval_job = ecore_job_add(_store_item_eval, sti);
273 static const Elm_Store_Item_Mapping *
274 _store_item_mapping_find(Elm_Store_Item *sti, const char *part)
276 const Elm_Store_Item_Mapping *m;
278 for (m = sti->mapping; m; m ++)
280 if (m->type == ELM_STORE_ITEM_MAPPING_NONE) break;
281 if (!strcmp(part, m->part)) return m;
287 _store_item_label_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
289 Elm_Store_Item *sti = data;
294 const Elm_Store_Item_Mapping *m = _store_item_mapping_find(sti, part);
299 case ELM_STORE_ITEM_MAPPING_LABEL:
300 s = *(char **)(((unsigned char *)sti->data) + m->offset);
302 case ELM_STORE_ITEM_MAPPING_CUSTOM:
303 if (m->details.custom.func)
304 s = m->details.custom.func(sti->data, sti, part);
312 return s ? strdup(s) : NULL;
316 _store_item_content_get(void *data, Evas_Object *obj, const char *part)
318 Elm_Store_Item *sti = data;
322 const Elm_Store_Item_Mapping *m = _store_item_mapping_find(sti, part);
325 Evas_Object *ic = NULL;
326 const char *s = NULL;
330 case ELM_STORE_ITEM_MAPPING_ICON:
331 ic = elm_icon_add(obj);
332 s = *(char **)(((unsigned char *)sti->data) + m->offset);
333 elm_icon_order_lookup_set(ic, m->details.icon.lookup_order);
334 evas_object_size_hint_aspect_set(ic,
335 EVAS_ASPECT_CONTROL_VERTICAL,
338 elm_icon_smooth_set(ic, m->details.icon.smooth);
339 elm_icon_no_scale_set(ic, m->details.icon.no_scale);
340 elm_icon_scale_set(ic,
341 m->details.icon.scale_up,
342 m->details.icon.scale_down);
345 if (m->details.icon.standard_name)
346 elm_icon_standard_set(ic, s);
348 elm_icon_file_set(ic, s, NULL);
351 case ELM_STORE_ITEM_MAPPING_PHOTO:
352 ic = elm_icon_add(obj);
353 s = *(char **)(((unsigned char *)sti->data) + m->offset);
354 elm_photo_size_set(ic, m->details.photo.size);
356 elm_photo_file_set(ic, s);
358 case ELM_STORE_ITEM_MAPPING_CUSTOM:
359 if (m->details.custom.func)
360 ic = m->details.custom.func(sti->data, sti, part);
374 _store_item_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__)
378 ////// **** WARNING ***********************************************************
379 //// * This function runs inside a thread outside efl mainloop. Be careful! *
380 // ************************************************************************
382 _store_filesystem_sort_cb(void *d1, void *d2)
384 Elm_Store_Item_Info *info1 = d1, *info2 = d2;
385 if ((!info1->sort_id) || (!info2->sort_id)) return 0;
386 return strcoll(info1->sort_id, info2->sort_id);
390 _store_filesystem_list_do(void *data, Ecore_Thread *th __UNUSED__)
392 Elm_Store_Filesystem *st = data;
394 const Eina_File_Direct_Info *finf;
395 Eina_List *sorted = NULL;
396 Elm_Store_Item_Info_Filesystem *info;
398 // FIXME: need a way to abstract the open, list, feed items from list
399 // and maybe get initial sortable key vals etc.
400 it = eina_file_stat_ls(st->dir);
402 EINA_ITERATOR_FOREACH(it, finf)
405 size_t pathsz = finf->path_length + 1;
407 if (finf->path[finf->name_start] == '.') continue ;
409 info = calloc(1, sizeof(Elm_Store_Item_Info_Filesystem) + pathsz);
411 info->path = ((char *)info) + sizeof(Elm_Store_Item_Info_Filesystem);
412 memcpy(info->path, finf->path, pathsz);
414 if (st->base.cb.list.func)
415 ok = st->base.cb.list.func(st->base.cb.list.data, &info->base);
418 if (!st->base.sorted) ecore_thread_feedback(th, info);
419 else sorted = eina_list_append(sorted, info);
423 if (info->base.sort_id) free(info->base.sort_id);
426 if (ecore_thread_check(th)) break;
428 eina_iterator_free(it);
431 sorted = eina_list_sort(sorted, 0,
432 EINA_COMPARE_CB(_store_filesystem_sort_cb));
433 EINA_LIST_FREE(sorted, info)
435 if (!ecore_thread_check(th)) ecore_thread_feedback(th, info);
439 // ************************************************************************
440 //// * End of separate thread function. *
441 ////// ************************************************************************
444 _store_filesystem_list_end(void *data, Ecore_Thread *th)
446 Elm_Store *st = data;
447 if (th == st->list_th) st->list_th = NULL;
451 _store_filesystem_list_cancel(void *data, Ecore_Thread *th)
453 Elm_Store *st = data;
454 if (th == st->list_th) st->list_th = NULL;
458 _store_filesystem_list_update(void *data, Ecore_Thread *th __UNUSED__, void *msg)
460 Elm_Store *st = data;
461 Elm_Store_Item_Filesystem *sti;
462 Elm_Genlist_Item_Class *itc;
463 Elm_Store_Item_Info_Filesystem *info = msg;
465 sti = calloc(1, sizeof(Elm_Store_Item_Filesystem));
468 EINA_MAGIC_SET(&(sti->base), ELM_STORE_ITEM_MAGIC);
469 sti->base.store = st;
470 sti->base.data = info->base.data;
471 sti->base.mapping = info->base.mapping;
472 sti->path = eina_stringshare_add(info->path);
474 itc = info->base.item_class;
475 if (!itc) itc = &_store_item_class;
478 itc->func.label_get = _store_item_label_get;
479 itc->func.content_get = _store_item_content_get;
480 itc->func.state_get = NULL; // FIXME: support state gets later
481 itc->func.del = _store_item_del;
484 // FIXME: handle being a parent (tree)
485 sti->base.item = elm_genlist_item_append(st->genlist, itc,
488 ELM_GENLIST_ITEM_NONE,
490 NULL/* func data */);
491 st->items = eina_inlist_append(st->items, (Eina_Inlist *)sti);
493 if (info->base.sort_id) free(info->base.sort_id);
499 _elm_store_new(size_t size)
501 Elm_Store *st = calloc(1, size);
502 EINA_SAFETY_ON_NULL_RETURN_VAL(st, NULL);
504 // TODO: BEGIN - move to elm_store_init()
505 eina_magic_string_set(ELM_STORE_MAGIC, "Elm_Store");
506 eina_magic_string_set(ELM_STORE_FILESYSTEM_MAGIC, "Elm_Store_Filesystem");
507 eina_magic_string_set(ELM_STORE_ITEM_MAGIC, "Elm_Store_Item");
508 // setup default item class (always the same) if list cb doesnt provide one
509 _store_item_class.item_style = "default";
510 _store_item_class.func.label_get = _store_item_label_get;
511 _store_item_class.func.content_get = _store_item_content_get;
512 _store_item_class.func.state_get = NULL; // FIXME: support state gets later
513 _store_item_class.func.del = _store_item_del;
514 // TODO: END - move to elm_store_init()
516 EINA_MAGIC_SET(st, ELM_STORE_MAGIC);
518 st->fetch_thread = EINA_TRUE;
521 #define elm_store_new(type) (type*)_elm_store_new(sizeof(type))
524 _elm_store_filesystem_free(Elm_Store *store)
526 Elm_Store_Filesystem *st = (Elm_Store_Filesystem *)store;
527 eina_stringshare_del(st->dir);
531 _elm_store_filesystem_item_free(Elm_Store_Item *item)
533 Elm_Store_Item_Filesystem *sti = (Elm_Store_Item_Filesystem *)item;
534 eina_stringshare_del(sti->path);
538 elm_store_filesystem_new(void)
540 Elm_Store_Filesystem *st = elm_store_new(Elm_Store_Filesystem);
541 EINA_SAFETY_ON_NULL_RETURN_VAL(st, NULL);
543 EINA_MAGIC_SET(st, ELM_STORE_FILESYSTEM_MAGIC);
544 st->base.free = _elm_store_filesystem_free;
545 st->base.item.free = _elm_store_filesystem_item_free;
551 elm_store_free(Elm_Store *st)
553 void (*item_free)(Elm_Store_Item *);
554 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
557 ecore_thread_cancel(st->list_th);
560 eina_list_free(st->realized);
561 item_free = st->item.free;
564 Elm_Store_Item *sti = (Elm_Store_Item *)st->items;
565 if (sti->eval_job) ecore_job_del(sti->eval_job);
568 ecore_thread_cancel(sti->fetch_th);
569 sti->fetch_th = NULL;
571 if (item_free) item_free(sti);
574 if (st->cb.unfetch.func)
575 st->cb.unfetch.func(st->cb.unfetch.data, sti);
583 evas_object_event_callback_del_full(st->genlist, EVAS_CALLBACK_DEL, _store_genlist_del, st);
584 evas_object_smart_callback_del(st->genlist, "realized", _store_genlist_item_realized);
585 evas_object_smart_callback_del(st->genlist, "unrealized", _store_genlist_item_unrealized);
586 elm_gen_clear(st->genlist);
589 if (st->free) st->free(st);
594 elm_store_target_genlist_set(Elm_Store *st, Evas_Object *obj)
596 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
597 if (st->genlist == obj) return;
600 evas_object_event_callback_del_full(st->genlist, EVAS_CALLBACK_DEL, _store_genlist_del, st);
601 evas_object_smart_callback_del(st->genlist, "realized", _store_genlist_item_realized);
602 evas_object_smart_callback_del(st->genlist, "unrealized", _store_genlist_item_unrealized);
603 elm_gen_clear(st->genlist);
606 if (!st->genlist) return;
607 evas_object_smart_callback_add(st->genlist, "realized", _store_genlist_item_realized, st);
608 evas_object_smart_callback_add(st->genlist, "unrealized", _store_genlist_item_unrealized, st);
609 evas_object_event_callback_add(st->genlist, EVAS_CALLBACK_DEL, _store_genlist_del, st);
610 elm_gen_clear(st->genlist);
614 elm_store_filesystem_directory_set(Elm_Store *store, const char *dir)
616 Elm_Store_Filesystem *st = (Elm_Store_Filesystem *)store;
617 if (!EINA_MAGIC_CHECK(store, ELM_STORE_MAGIC)) return;
618 if (!EINA_MAGIC_CHECK(st, ELM_STORE_FILESYSTEM_MAGIC)) return;
621 ecore_thread_cancel(store->list_th);
622 store->list_th = NULL;
624 if (!eina_stringshare_replace(&st->dir, dir)) return;
625 store->list_th = ecore_thread_feedback_run(_store_filesystem_list_do,
626 _store_filesystem_list_update,
627 _store_filesystem_list_end,
628 _store_filesystem_list_cancel,
633 elm_store_filesystem_directory_get(const Elm_Store *store)
635 const Elm_Store_Filesystem *st = (const Elm_Store_Filesystem *)store;
636 if (!EINA_MAGIC_CHECK(store, ELM_STORE_MAGIC)) return NULL;
637 if (!EINA_MAGIC_CHECK(st, ELM_STORE_FILESYSTEM_MAGIC)) return NULL;
642 elm_store_cache_set(Elm_Store *st, int max)
644 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
645 if (max < 0) max = 0;
647 _store_cache_trim(st);
651 elm_store_cache_get(const Elm_Store *st)
653 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return 0;
654 return st->cache_max;
658 elm_store_list_func_set(Elm_Store *st, Elm_Store_Item_List_Cb func, const void *data)
660 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
661 st->cb.list.func = func;
662 st->cb.list.data = (void *)data;
666 elm_store_fetch_func_set(Elm_Store *st, Elm_Store_Item_Fetch_Cb func, const void *data)
668 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
669 st->cb.fetch.func = func;
670 st->cb.fetch.data = (void *)data;
674 elm_store_fetch_thread_set(Elm_Store *st, Eina_Bool use_thread)
676 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
677 st->fetch_thread = !!use_thread;
681 elm_store_fetch_thread_get(const Elm_Store *st)
683 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return EINA_FALSE;
684 return st->fetch_thread;
688 elm_store_unfetch_func_set(Elm_Store *st, Elm_Store_Item_Unfetch_Cb func, const void *data)
690 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
691 st->cb.unfetch.func = func;
692 st->cb.unfetch.data = (void *)data;
696 elm_store_sorted_set(Elm_Store *st, Eina_Bool sorted)
698 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return;
703 elm_store_sorted_get(const Elm_Store *st)
705 if (!EINA_MAGIC_CHECK(st, ELM_STORE_MAGIC)) return EINA_FALSE;
710 elm_store_item_data_set(Elm_Store_Item *sti, void *data)
712 if (!EINA_MAGIC_CHECK(sti, ELM_STORE_ITEM_MAGIC)) return;
719 elm_store_item_data_get(Elm_Store_Item *sti)
721 if (!EINA_MAGIC_CHECK(sti, ELM_STORE_ITEM_MAGIC)) return NULL;
729 EAPI const Elm_Store *
730 elm_store_item_store_get(const Elm_Store_Item *sti)
732 if (!EINA_MAGIC_CHECK(sti, ELM_STORE_ITEM_MAGIC)) return NULL;
737 EAPI const Elm_Genlist_Item *
738 elm_store_item_genlist_item_get(const Elm_Store_Item *sti)
740 if (!EINA_MAGIC_CHECK(sti, ELM_STORE_ITEM_MAGIC)) return NULL;
746 elm_store_item_filesystem_path_get(const Elm_Store_Item *item)
748 Elm_Store_Item_Filesystem *sti = (Elm_Store_Item_Filesystem *)item;
749 Elm_Store_Filesystem *st;
750 if (!EINA_MAGIC_CHECK(item, ELM_STORE_ITEM_MAGIC)) return NULL;
751 if (!EINA_MAGIC_CHECK(item->store, ELM_STORE_MAGIC)) return NULL;
752 /* ensure we're dealing with filesystem item */
753 st = (Elm_Store_Filesystem *)item->store;
754 if (!EINA_MAGIC_CHECK(st, ELM_STORE_FILESYSTEM_MAGIC)) return NULL;