1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
17 #ifdef HAVE_ELEMENTARY_ETHUMB
18 const char *thumb_path;
19 const char *thumb_key;
20 Ethumb_Client_Async *request;
22 Ethumb_Thumb_Format format;
27 Ecore_Event_Handler *eeh;
28 Elm_Thumb_Animation_Setting anim_setting;
29 Eina_Bool on_hold : 1;
30 Eina_Bool is_video : 1;
31 Eina_Bool was_video : 1;
35 static const char *widtype = NULL;
37 static const char SIG_CLICKED[] = "clicked";
38 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
39 static const char SIG_GENERATE_ERROR[] = "generate,error";
40 static const char SIG_GENERATE_START[] = "generate,start";
41 static const char SIG_GENERATE_STOP[] = "generate,stop";
42 static const char SIG_LOAD_ERROR[] = "load,error";
43 static const char SIG_PRESS[] = "press";
45 static const Evas_Smart_Cb_Description _signals[] =
48 {SIG_CLICKED_DOUBLE, ""},
49 {SIG_GENERATE_ERROR, ""},
50 {SIG_GENERATE_START, ""},
51 {SIG_GENERATE_STOP, ""},
57 #define EDJE_SIGNAL_GENERATE_START "elm,thumb,generate,start"
58 #define EDJE_SIGNAL_GENERATE_STOP "elm,thumb,generate,stop"
59 #define EDJE_SIGNAL_GENERATE_ERROR "elm,thumb,generate,error"
60 #define EDJE_SIGNAL_LOAD_ERROR "elm,thumb,load,error"
61 #define EDJE_SIGNAL_PULSE_START "elm,state,pulse,start"
62 #define EDJE_SIGNAL_PULSE_STOP "elm,state,pulse,stop"
64 struct _Ethumb_Client *_elm_ethumb_client = NULL;
65 Eina_Bool _elm_ethumb_connected = EINA_FALSE;
66 #ifdef HAVE_ELEMENTARY_ETHUMB
67 static Eina_List *retry = NULL;
68 static int pending_request = 0;
71 EAPI int ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
74 _del_hook(Evas_Object *obj)
76 Widget_Data *wd = elm_widget_data_get(obj);
78 #ifdef HAVE_ELEMENTARY_ETHUMB
79 if (wd->thumb.request)
81 ethumb_client_thumb_async_cancel(_elm_ethumb_client, wd->thumb.request);
82 wd->thumb.request = NULL;
86 retry = eina_list_remove(retry, wd);
87 wd->thumb.retry = EINA_FALSE;
90 eina_stringshare_del(wd->thumb.thumb_path);
91 eina_stringshare_del(wd->thumb.thumb_key);
94 eina_stringshare_del(wd->file);
95 eina_stringshare_del(wd->key);
96 if (wd->eeh) ecore_event_handler_del(wd->eeh);
101 _theme_hook(Evas_Object *obj)
103 Widget_Data *wd = elm_widget_data_get(obj);
104 _elm_theme_object_set(obj, wd->frame, "thumb", "base",
105 elm_widget_style_get(obj));
108 #ifdef HAVE_ELEMENTARY_ETHUMB
110 _mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
112 Widget_Data *wd = data;
113 Evas_Event_Mouse_Down *ev = event_info;
117 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
118 wd->on_hold = EINA_TRUE;
120 wd->on_hold = EINA_FALSE;
121 if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
122 evas_object_smart_callback_call(wd->self, SIG_CLICKED_DOUBLE, NULL);
124 evas_object_smart_callback_call(wd->self, SIG_PRESS, NULL);
128 _mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
130 Widget_Data *wd = data;
131 Evas_Event_Mouse_Up *ev = event_info;
135 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
136 wd->on_hold = EINA_TRUE;
138 wd->on_hold = EINA_FALSE;
140 evas_object_smart_callback_call(wd->self, SIG_CLICKED, NULL);
141 wd->on_hold = EINA_FALSE;
145 _thumb_ready(Widget_Data *wd, const char *thumb_path, const char *thumb_key)
150 evas_object_image_size_get(wd->view, &aw, &ah);
151 evas_object_size_hint_aspect_set(wd->view,
152 EVAS_ASPECT_CONTROL_BOTH,
154 edje_object_part_swallow(wd->frame, "elm.swallow.content", wd->view);
155 edje_object_size_min_get(wd->frame, &mw, &mh);
156 edje_object_size_min_restricted_calc(wd->frame, &mw, &mh, mw, mh);
157 evas_object_size_hint_min_set(wd->self, mw, mh);
158 eina_stringshare_replace(&(wd->thumb.file), thumb_path);
159 eina_stringshare_replace(&(wd->thumb.key), thumb_key);
160 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
161 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
165 _thumb_loaded(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
167 Widget_Data *wd = data;
168 const char *thumb_path;
169 const char *thumb_key;
171 evas_object_image_file_get(wd->view, &thumb_path, &thumb_key);
173 _thumb_ready(wd, thumb_path, thumb_key);
176 /* As we do use stat to check if a thumbnail is available, it's possible
177 that we end up accessing before the file is completly written on disk.
178 By retrying each time a thumbnail is finished we should be fine or not.
181 _retry_thumb(Widget_Data *wd)
185 if ((wd->is_video) && (wd->thumb.format == ETHUMB_THUMB_EET))
187 edje_object_file_set(wd->view, NULL, NULL);
188 if (!edje_object_file_set(wd->view,
189 wd->thumb.thumb_path,
192 if (pending_request == 0)
193 ERR("could not set file=%s key=%s for %s",
194 wd->thumb.thumb_path,
202 evas_object_image_file_set(wd->view, NULL, NULL);
203 evas_object_image_file_set(wd->view,
204 wd->thumb.thumb_path,
205 wd->thumb.thumb_key);
206 r = evas_object_image_load_error_get(wd->view);
207 if (r != EVAS_LOAD_ERROR_NONE)
209 if (pending_request == 0)
210 ERR("%s: %s", wd->thumb.thumb_path, evas_load_error_str(r));
214 evas_object_event_callback_add(wd->view,
215 EVAS_CALLBACK_IMAGE_PRELOADED,
217 evas_object_image_preload(wd->view, EINA_TRUE);
221 _thumb_ready(wd, wd->thumb.thumb_path, wd->thumb.thumb_key);
223 eina_stringshare_del(wd->thumb.thumb_path);
224 wd->thumb.thumb_path = NULL;
226 eina_stringshare_del(wd->thumb.thumb_key);
227 wd->thumb.thumb_key = NULL;
236 _finished_thumb(Widget_Data *wd,
237 const char *thumb_path,
238 const char *thumb_key)
244 evas = evas_object_evas_get(wd->self);
245 if ((wd->view) && (wd->is_video ^ wd->was_video))
247 evas_object_del(wd->view);
250 wd->was_video = wd->is_video;
252 if ((wd->is_video) &&
253 (ethumb_client_format_get(_elm_ethumb_client) == ETHUMB_THUMB_EET))
257 wd->view = edje_object_add(evas);
260 ERR("could not create edje object");
263 elm_widget_sub_object_add(wd->self, wd->view);
266 if (!edje_object_file_set(wd->view, thumb_path, thumb_key))
268 wd->thumb.thumb_path = eina_stringshare_ref(thumb_path);
269 wd->thumb.thumb_key = eina_stringshare_ref(thumb_key);
270 wd->thumb.format = ethumb_client_format_get(_elm_ethumb_client);
271 wd->thumb.retry = EINA_TRUE;
273 retry = eina_list_append(retry, wd);
281 wd->view = evas_object_image_filled_add(evas);
284 ERR("could not create image object");
287 evas_object_event_callback_add(wd->view,
288 EVAS_CALLBACK_IMAGE_PRELOADED,
290 elm_widget_sub_object_add(wd->self, wd->view);
291 evas_object_hide(wd->view);
294 evas_object_image_file_set(wd->view, thumb_path, thumb_key);
295 r = evas_object_image_load_error_get(wd->view);
296 if (r != EVAS_LOAD_ERROR_NONE)
298 WRN("%s: %s", thumb_path, evas_load_error_str(r));
299 wd->thumb.thumb_path = eina_stringshare_ref(thumb_path);
300 wd->thumb.thumb_key = eina_stringshare_ref(thumb_key);
301 wd->thumb.format = ethumb_client_format_get(_elm_ethumb_client);
302 wd->thumb.retry = EINA_TRUE;
304 retry = eina_list_append(retry, wd);
308 evas_object_image_preload(wd->view, 0);
312 _thumb_ready(wd, thumb_path, thumb_key);
314 EINA_LIST_FOREACH_SAFE(retry, l, ll, wd)
315 if (_retry_thumb(wd))
316 retry = eina_list_remove_list(retry, l);
318 if (pending_request == 0)
319 EINA_LIST_FREE(retry, wd)
321 eina_stringshare_del(wd->thumb.thumb_path);
322 wd->thumb.thumb_path = NULL;
324 eina_stringshare_del(wd->thumb.thumb_key);
325 wd->thumb.thumb_key = NULL;
327 evas_object_del(wd->view);
330 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
331 evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
337 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
338 evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
342 _elm_thumb_done(Ethumb_Client *client __UNUSED__, const char *thumb_path, const char *thumb_key, void *data)
344 Widget_Data *wd = data;
346 assert(wd->thumb.request);
349 wd->thumb.request = NULL;
351 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_STOP, "elm");
353 _finished_thumb(wd, thumb_path, thumb_key);
357 _elm_thumb_error(Ethumb_Client *client __UNUSED__, void *data)
359 Widget_Data *wd = data;
361 assert(wd->thumb.request);
364 wd->thumb.request = NULL;
366 ERR("could not generate thumbnail for %s (key: %s)", wd->thumb.file, wd->thumb.key ? wd->thumb.key : "");
368 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
369 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
373 _thumb_apply(Widget_Data *wd)
375 if (wd->thumb.request)
377 ethumb_client_thumb_async_cancel(_elm_ethumb_client, wd->thumb.request);
378 wd->thumb.request = NULL;
382 retry = eina_list_remove(retry, wd);
383 wd->thumb.retry = EINA_FALSE;
386 if (!wd->file) return;
388 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_START, "elm");
389 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_START, "elm");
390 evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
393 ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
394 wd->thumb.request = ethumb_client_thumb_async_get(_elm_ethumb_client,
401 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
404 return ECORE_CALLBACK_RENEW;
408 _thumb_show(Widget_Data *wd)
410 evas_object_show(wd->frame);
412 if (elm_thumb_ethumb_client_connected())
419 wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
420 _thumb_apply_cb, wd);
424 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
430 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
432 Widget_Data *wd = data;
434 evas_object_hide(wd->frame);
436 if (wd->thumb.request)
438 ethumb_client_thumb_async_cancel(_elm_ethumb_client, wd->thumb.request);
439 wd->thumb.request = NULL;
441 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
442 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
447 retry = eina_list_remove(retry, wd);
448 wd->thumb.retry = EINA_FALSE;
453 ecore_event_handler_del(wd->eeh);
461 static int _elm_need_ethumb = 0;
463 static void _on_die_cb(void *, Ethumb_Client *);
466 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
470 ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
471 _elm_ethumb_connected = EINA_TRUE;
472 ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
475 _elm_ethumb_client = NULL;
479 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
481 ethumb_client_disconnect(_elm_ethumb_client);
482 _elm_ethumb_client = NULL;
483 _elm_ethumb_connected = EINA_FALSE;
484 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
489 _elm_unneed_ethumb(void)
492 if (--_elm_need_ethumb) return;
494 ethumb_client_disconnect(_elm_ethumb_client);
495 _elm_ethumb_client = NULL;
496 ethumb_client_shutdown();
497 ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
502 _elm_thumb_dropcb(void *data __UNUSED__, Evas_Object *o, Elm_Selection_Data *drop)
504 if ((!o) || (!drop) || (!drop->data)) return EINA_FALSE;
505 elm_thumb_file_set(o, drop->data, NULL);
510 elm_need_ethumb(void)
513 if (_elm_need_ethumb++) return EINA_TRUE;
514 ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
515 ethumb_client_init();
516 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
524 elm_thumb_add(Evas_Object *parent)
529 Evas_Coord minw, minh;
531 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
533 ELM_SET_WIDTYPE(widtype, "thumb");
534 elm_widget_type_set(obj, "thumb");
535 elm_widget_sub_object_add(parent, obj);
536 elm_widget_data_set(obj, wd);
537 elm_widget_del_hook_set(obj, _del_hook);
538 elm_widget_theme_hook_set(obj, _theme_hook);
539 elm_widget_can_focus_set(obj, EINA_FALSE);
541 wd->frame = edje_object_add(e);
542 _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
543 elm_widget_resize_object_set(obj, wd->frame);
545 edje_object_size_min_calc(obj, &minw, &minh);
546 evas_object_size_hint_min_set(obj, minw, minh);
553 wd->on_hold = EINA_FALSE;
554 wd->is_video = EINA_FALSE;
555 wd->was_video = EINA_FALSE;
557 #ifdef HAVE_ELEMENTARY_ETHUMB
558 wd->thumb.thumb_path = NULL;
559 wd->thumb.thumb_key = NULL;
560 wd->thumb.request = NULL;
561 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
563 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
565 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
567 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
571 // TODO: convert Elementary to subclassing of Evas_Smart_Class
572 // TODO: and save some bytes, making descriptions per-class and not instance!
573 evas_object_smart_callbacks_descriptions_set(obj, _signals);
578 elm_thumb_reload(Evas_Object *obj)
580 ELM_CHECK_WIDTYPE(obj, widtype);
581 Widget_Data *wd = elm_widget_data_get(obj);
583 eina_stringshare_replace(&(wd->thumb.file), NULL);
584 eina_stringshare_replace(&(wd->thumb.key), NULL);
586 #ifdef HAVE_ELEMENTARY_ETHUMB
587 if (evas_object_visible_get(obj))
593 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
595 ELM_CHECK_WIDTYPE(obj, widtype);
596 Eina_Bool file_replaced, key_replaced;
597 Widget_Data *wd = elm_widget_data_get(obj);
599 file_replaced = eina_stringshare_replace(&(wd->file), file);
600 key_replaced = eina_stringshare_replace(&(wd->key), key);
605 const char **ext, *ptr;
606 static const char *extensions[] =
608 ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
611 prefix_size = eina_stringshare_strlen(wd->file) - 4;
612 if (prefix_size >= 0)
614 ptr = wd->file + prefix_size;
615 wd->is_video = EINA_FALSE;
616 for (ext = extensions; *ext; ext++)
617 if (!strcasecmp(ptr, *ext))
619 wd->is_video = EINA_TRUE;
625 eina_stringshare_replace(&(wd->thumb.file), NULL);
626 eina_stringshare_replace(&(wd->thumb.key), NULL);
628 #ifdef HAVE_ELEMENTARY_ETHUMB
629 if (((file_replaced) || (key_replaced)) && (evas_object_visible_get(obj)))
635 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
637 ELM_CHECK_WIDTYPE(obj, widtype);
638 Widget_Data *wd = elm_widget_data_get(obj);
647 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
649 ELM_CHECK_WIDTYPE(obj, widtype);
650 Widget_Data *wd = elm_widget_data_get(obj);
653 *file = wd->thumb.file;
655 *key = wd->thumb.key;
659 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
661 ELM_CHECK_WIDTYPE(obj, widtype);
662 Widget_Data *wd = elm_widget_data_get(obj);
664 EINA_SAFETY_ON_TRUE_RETURN(setting >= ELM_THUMB_ANIMATION_LAST);
666 wd->anim_setting = setting;
667 if (setting == ELM_THUMB_ANIMATION_LOOP)
668 edje_object_signal_emit(wd->view, "animate_loop", "");
669 else if (setting == ELM_THUMB_ANIMATION_START)
670 edje_object_signal_emit(wd->view, "animate", "");
671 else if (setting == ELM_THUMB_ANIMATION_STOP)
672 edje_object_signal_emit(wd->view, "animate_stop", "");
675 EAPI Elm_Thumb_Animation_Setting
676 elm_thumb_animate_get(const Evas_Object *obj)
678 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
679 Widget_Data *wd = elm_widget_data_get(obj);
681 return wd->anim_setting;
685 elm_thumb_ethumb_client_get(void)
687 return _elm_ethumb_client;
691 elm_thumb_ethumb_client_connected(void)
693 return _elm_ethumb_connected;
697 elm_thumb_editable_set(Evas_Object *obj, Eina_Bool edit)
699 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
700 Widget_Data *wd = elm_widget_data_get(obj);
702 if (!wd) return EINA_FALSE;
704 if (wd->edit == edit) return EINA_TRUE;
708 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE,
709 _elm_thumb_dropcb, obj);
711 elm_drop_target_del(obj);
717 elm_thumb_editable_get(const Evas_Object *obj)
719 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
720 Widget_Data *wd = elm_widget_data_get(obj);
722 if (!wd) return EINA_FALSE;
726 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/