elementary: add elm_icon_thumb_set.
[framework/uifw/elementary.git] / src / lib / elm_thumb.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Thumb Thumb
6  *
7  * A thumb object is used for displaying the thumbnail of an image or video.
8  * You must have compiled Elementary with Ethumb_Client support and the DBus
9  * service must be present and auto-activated in order to have thumbnails to
10  * be generated.
11  *
12  * Signals that you can add callbacks for are:
13  *
14  * "clicked" - This is called when a user has clicked the thumb without dragging
15  *             around.
16  * "clicked,double" - This is called when a user has double-clicked the thumb.
17  * "press" - This is called when a user has pressed down the thumb.
18  * "generate,start" - The thumbnail generation started.
19  * "generate,stop" - The generation process stopped.
20  * "generate,error" - The generation failed.
21  * "load,error" - The thumbnail image loading failed.
22  */
23
24 typedef struct _Widget_Data Widget_Data;
25
26 struct _Widget_Data
27 {
28    Evas_Object *self;
29    Evas_Object *frame;
30    Evas_Object *view;
31    const char *file;
32    const char *key;
33    struct
34      {
35         int id;
36         const char *file;
37         const char *key;
38 #ifdef HAVE_ELEMENTARY_ETHUMB
39         const char *thumb_path;
40         const char *thumb_key;
41         Ethumb_Exists *exists;
42
43         Ethumb_Thumb_Format format;
44
45         Eina_Bool retry : 1;
46 #endif
47      } thumb;
48    Ecore_Event_Handler *eeh;
49    Elm_Thumb_Animation_Setting anim_setting;
50    Eina_Bool on_hold : 1;
51    Eina_Bool is_video : 1;
52    Eina_Bool was_video : 1;
53    Eina_Bool edit : 1;
54 };
55
56 static const char *widtype = NULL;
57
58 static const char SIG_CLICKED[] = "clicked";
59 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
60 static const char SIG_GENERATE_ERROR[] = "generate,error";
61 static const char SIG_GENERATE_START[] = "generate,start";
62 static const char SIG_GENERATE_STOP[] = "generate,stop";
63 static const char SIG_LOAD_ERROR[] = "load,error";
64 static const char SIG_PRESS[] = "press";
65
66 static const Evas_Smart_Cb_Description _signals[] =
67 {
68      {SIG_CLICKED, ""},
69      {SIG_CLICKED_DOUBLE, ""},
70      {SIG_GENERATE_ERROR, ""},
71      {SIG_GENERATE_START, ""},
72      {SIG_GENERATE_STOP, ""},
73      {SIG_LOAD_ERROR, ""},
74      {SIG_PRESS, ""},
75      {NULL, NULL}
76 };
77
78 #define EDJE_SIGNAL_GENERATE_START "elm,thumb,generate,start"
79 #define EDJE_SIGNAL_GENERATE_STOP "elm,thumb,generate,stop"
80 #define EDJE_SIGNAL_GENERATE_ERROR "elm,thumb,generate,error"
81 #define EDJE_SIGNAL_LOAD_ERROR "elm,thumb,load,error"
82 #define EDJE_SIGNAL_PULSE_START "elm,state,pulse,start"
83 #define EDJE_SIGNAL_PULSE_STOP "elm,state,pulse,stop"
84
85 struct _Ethumb_Client *_elm_ethumb_client = NULL;
86 Eina_Bool _elm_ethumb_connected = EINA_FALSE;
87 #ifdef HAVE_ELEMENTARY_ETHUMB
88 static Eina_List *retry = NULL;
89 static int pending_request = 0;
90
91 static void _thumb_exists(Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread,
92                           Eina_Bool exists, void *data);
93
94 #endif
95
96 EAPI int ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
97
98 static void
99 _del_hook(Evas_Object *obj)
100 {
101    Widget_Data *wd = elm_widget_data_get(obj);
102
103 #ifdef HAVE_ELEMENTARY_ETHUMB
104    if (wd->thumb.id >= 0)
105      {
106         ethumb_client_generate_cancel(_elm_ethumb_client, wd->thumb.id,
107                                       NULL, NULL, NULL);
108         wd->thumb.id = -1;
109      }
110    if (wd->thumb.exists)
111      {
112         ethumb_client_thumb_exists_cancel(wd->thumb.exists, _thumb_exists, wd);
113         wd->thumb.exists = NULL;
114      }
115    if (wd->thumb.retry)
116      {
117         retry = eina_list_remove(retry, wd);
118         wd->thumb.retry = EINA_FALSE;
119      }
120
121    eina_stringshare_del(wd->thumb.thumb_path);
122    eina_stringshare_del(wd->thumb.thumb_key);
123 #endif
124
125    eina_stringshare_del(wd->file);
126    eina_stringshare_del(wd->key);
127    if (wd->eeh) ecore_event_handler_del(wd->eeh);
128    free(wd);
129 }
130
131 static void
132 _theme_hook(Evas_Object *obj)
133 {
134    Widget_Data *wd = elm_widget_data_get(obj);
135    _elm_theme_object_set(obj, wd->frame, "thumb", "base",
136                          elm_widget_style_get(obj));
137 }
138
139 #ifdef HAVE_ELEMENTARY_ETHUMB
140 static void
141 _mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
142 {
143    Widget_Data *wd = data;
144    Evas_Event_Mouse_Down *ev = event_info;
145
146    if (ev->button != 1)
147      return;
148    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
149      wd->on_hold = EINA_TRUE;
150    else
151      wd->on_hold = EINA_FALSE;
152    if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
153      evas_object_smart_callback_call(wd->self, SIG_CLICKED_DOUBLE, NULL);
154    else
155      evas_object_smart_callback_call(wd->self, SIG_PRESS, NULL);
156 }
157
158 static void
159 _mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
160 {
161    Widget_Data *wd = data;
162    Evas_Event_Mouse_Up *ev = event_info;
163
164    if (ev->button != 1)
165      return;
166    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
167      wd->on_hold = EINA_TRUE;
168    else
169      wd->on_hold = EINA_FALSE;
170    if (!wd->on_hold)
171      evas_object_smart_callback_call(wd->self, SIG_CLICKED, NULL);
172    wd->on_hold = EINA_FALSE;
173 }
174
175 static void
176 _thumb_ready(Widget_Data *wd, const char *thumb_path, const char *thumb_key)
177 {
178    Evas_Coord mw, mh;
179    Evas_Coord aw, ah;
180
181    evas_object_image_size_get(wd->view, &aw, &ah);
182    evas_object_size_hint_aspect_set(wd->view,
183                                     EVAS_ASPECT_CONTROL_BOTH,
184                                     aw, ah);
185    edje_object_part_swallow(wd->frame, "elm.swallow.content", wd->view);
186    edje_object_size_min_get(wd->frame, &mw, &mh);
187    edje_object_size_min_restricted_calc(wd->frame, &mw, &mh, mw, mh);
188    evas_object_size_hint_min_set(wd->self, mw, mh);
189    eina_stringshare_replace(&(wd->thumb.file), thumb_path);
190    eina_stringshare_replace(&(wd->thumb.key), thumb_key);
191    edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
192    evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
193 }
194
195 static void
196 _thumb_loaded(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
197 {
198    Widget_Data *wd = data;
199    const char *thumb_path;
200    const char *thumb_key;
201
202    evas_object_image_file_get(wd->view, &thumb_path, &thumb_key);
203
204    _thumb_ready(wd, thumb_path, thumb_key);
205 }
206
207 /* As we do use stat to check if a thumbnail is available, it's possible
208    that we end up accessing before the file is completly written on disk.
209    By retrying each time a thumbnail is finished we should be fine or not.
210 */
211 static Eina_Bool
212 _retry_thumb(Widget_Data *wd)
213 {
214    int r;
215
216    if ((wd->is_video) && (wd->thumb.format == ETHUMB_THUMB_EET))
217      {
218         edje_object_file_set(wd->view, NULL, NULL);
219         if (!edje_object_file_set(wd->view,
220                                   wd->thumb.thumb_path,
221                                   "movie/thumb"))
222           {
223              if (pending_request == 0)
224                ERR("could not set file=%s key=%s for %s",
225                    wd->thumb.thumb_path,
226                    wd->thumb.thumb_key,
227                    wd->file);
228              goto view_err;
229           }
230      }
231    else
232      {
233         evas_object_image_file_set(wd->view, NULL, NULL);
234         evas_object_image_file_set(wd->view,
235                                    wd->thumb.thumb_path,
236                                    wd->thumb.thumb_key);
237         r = evas_object_image_load_error_get(wd->view);
238         if (r != EVAS_LOAD_ERROR_NONE)
239           {
240              if (pending_request == 0)
241                ERR("%s: %s", wd->thumb.thumb_path, evas_load_error_str(r));
242              goto view_err;
243           }
244
245         evas_object_event_callback_add(wd->view,
246                                        EVAS_CALLBACK_IMAGE_PRELOADED,
247                                        _thumb_loaded, wd);
248         evas_object_image_preload(wd->view, EINA_TRUE);
249         return EINA_TRUE;
250      }
251
252    _thumb_ready(wd, wd->thumb.thumb_path, wd->thumb.thumb_key);
253
254    eina_stringshare_del(wd->thumb.thumb_path);
255    wd->thumb.thumb_path = NULL;
256
257    eina_stringshare_del(wd->thumb.thumb_key);
258    wd->thumb.thumb_key = NULL;
259
260    return EINA_TRUE;
261
262  view_err:
263    return EINA_FALSE;
264 }
265
266 static void
267 _finished_thumb(Widget_Data *wd,
268                 const char *thumb_path,
269                 const char *thumb_key)
270 {
271    Eina_List *l, *ll;
272    Evas *evas;
273    int r;
274
275    evas = evas_object_evas_get(wd->self);
276    if ((wd->view) && (wd->is_video ^ wd->was_video))
277      {
278         evas_object_del(wd->view);
279         wd->view = NULL;
280      }
281    wd->was_video = wd->is_video;
282
283    if ((wd->is_video) &&
284        (ethumb_client_format_get(_elm_ethumb_client) == ETHUMB_THUMB_EET))
285      {
286         if (!wd->view)
287           {
288              wd->view = edje_object_add(evas);
289              if (!wd->view)
290                {
291                   ERR("could not create edje object");
292                   goto err;
293                }
294              elm_widget_sub_object_add(wd->self, wd->view);
295           }
296
297         if (!edje_object_file_set(wd->view, thumb_path, "movie/thumb"))
298           {
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;
303
304              retry = eina_list_append(retry, wd);
305              return ;
306           }
307      }
308    else
309      {
310         if (!wd->view)
311           {
312              wd->view = evas_object_image_filled_add(evas);
313              if (!wd->view)
314                {
315                   ERR("could not create image object");
316                   goto err;
317                }
318              evas_object_event_callback_add(wd->view,
319                                             EVAS_CALLBACK_IMAGE_PRELOADED,
320                                             _thumb_loaded, wd);
321              elm_widget_sub_object_add(wd->self, wd->view);
322              evas_object_hide(wd->view);
323           }
324
325         evas_object_image_file_set(wd->view, thumb_path, thumb_key);
326         r = evas_object_image_load_error_get(wd->view);
327         if (r != EVAS_LOAD_ERROR_NONE)
328           {
329              WRN("%s: %s", thumb_path, evas_load_error_str(r));
330              wd->thumb.thumb_path = eina_stringshare_ref(thumb_path);
331              wd->thumb.thumb_key = eina_stringshare_ref(thumb_key);
332              wd->thumb.format = ethumb_client_format_get(_elm_ethumb_client);
333              wd->thumb.retry = EINA_TRUE;
334
335              retry = eina_list_append(retry, wd);
336              return ;
337           }
338
339         evas_object_image_preload(wd->view, 0);
340         return ;
341      }
342
343    _thumb_ready(wd, thumb_path, thumb_key);
344
345    EINA_LIST_FOREACH_SAFE(retry, l, ll, wd)
346      if (_retry_thumb(wd))
347        retry = eina_list_remove_list(retry, l);
348
349    if (pending_request == 0)
350      EINA_LIST_FREE(retry, wd)
351        {
352           eina_stringshare_del(wd->thumb.thumb_path);
353           wd->thumb.thumb_path = NULL;
354
355           eina_stringshare_del(wd->thumb.thumb_key);
356           wd->thumb.thumb_key = NULL;
357
358           evas_object_del(wd->view);
359           wd->view = NULL;
360
361           edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
362           evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
363        }
364
365    return;
366
367 err:
368    edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
369    evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
370 }
371
372 static void
373 _finished_thumb_cb(void *data, Ethumb_Client *c __UNUSED__, int id, const char *file, const char *key, const char *thumb_path, const char *thumb_key, Eina_Bool success)
374 {
375    Widget_Data *wd = data;
376
377    EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
378    wd->thumb.id = -1;
379
380    pending_request--;
381
382    edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_STOP, "elm");
383
384    if (success)
385      {
386         _finished_thumb(wd, thumb_path, thumb_key);
387         return;
388      }
389
390    ERR("could not generate thumbnail for %s (key: %s)", file, key ? key : "");
391    edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
392    evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
393 }
394
395 static void
396 _thumb_exists(Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread,
397               Eina_Bool exists, void *data)
398 {
399    Widget_Data *wd = data;
400
401    if (ethumb_client_thumb_exists_check(thread))
402      return ;
403
404    wd->thumb.exists = NULL;
405
406    if (exists)
407      {
408         const char *thumb_path, *thumb_key;
409
410         pending_request--;
411
412         wd->thumb.id = -1;
413         ethumb_client_thumb_path_get(_elm_ethumb_client, &thumb_path,
414                                      &thumb_key);
415         _finished_thumb(wd, thumb_path, thumb_key);
416         return;
417      }
418    else if ((wd->thumb.id = ethumb_client_generate
419              (_elm_ethumb_client, _finished_thumb_cb, wd, NULL)) != -1)
420      {
421         edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_START, "elm");
422         edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_START, "elm");
423         evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
424      }
425    else
426      {
427         pending_request--;
428
429         wd->thumb.id = -1;
430         edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
431         evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
432      }
433
434 }
435
436 static void
437 _thumb_apply(Widget_Data *wd)
438 {
439    if (wd->thumb.id > 0)
440      {
441         ethumb_client_generate_cancel
442            (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
443         wd->thumb.id = -1;
444      }
445
446    if (wd->thumb.exists)
447      {
448         ethumb_client_thumb_exists_cancel(wd->thumb.exists, _thumb_exists, wd);
449         wd->thumb.exists = NULL;
450      }
451
452    if (wd->thumb.retry)
453      {
454         retry = eina_list_remove(retry, wd);
455         wd->thumb.retry = EINA_FALSE;
456      }
457
458    if (!wd->file) return;
459
460    pending_request++;
461    ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
462    wd->thumb.exists = ethumb_client_thumb_exists(_elm_ethumb_client,
463                                                  _thumb_exists,
464                                                  wd);
465 }
466
467 static Eina_Bool
468 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
469 {
470    _thumb_apply(data);
471    return ECORE_CALLBACK_RENEW;
472 }
473
474 static void
475 _thumb_show(Widget_Data *wd)
476 {
477    evas_object_show(wd->frame);
478
479    if (elm_thumb_ethumb_client_connected())
480      {
481         _thumb_apply(wd);
482         return;
483      }
484
485    if (!wd->eeh)
486      wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
487                                        _thumb_apply_cb, wd);
488 }
489
490 static void
491 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
492 {
493    _thumb_show(data);
494 }
495
496 static void
497 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
498 {
499    Widget_Data *wd = data;
500
501    evas_object_hide(wd->frame);
502
503    if (wd->thumb.id >= 0)
504      {
505         ethumb_client_generate_cancel
506            (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
507         wd->thumb.id = -1;
508
509         edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
510         evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
511      }
512
513    if (wd->thumb.exists)
514      {
515         ethumb_client_thumb_exists_cancel(wd->thumb.exists, _thumb_exists, wd);
516         wd->thumb.exists = NULL;
517      }
518
519    if (wd->thumb.retry)
520      {
521         retry = eina_list_remove(retry, wd);
522         wd->thumb.retry = EINA_FALSE;
523      }
524
525    if (wd->eeh)
526      {
527         ecore_event_handler_del(wd->eeh);
528         wd->eeh = NULL;
529      }
530 }
531
532 #endif
533
534 #ifdef ELM_ETHUMB
535 static int _elm_need_ethumb = 0;
536
537 static void _on_die_cb(void *, Ethumb_Client *);
538
539 static void
540 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
541 {
542    if (success)
543      {
544         ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
545         _elm_ethumb_connected = EINA_TRUE;
546         ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
547      }
548    else
549      _elm_ethumb_client = NULL;
550 }
551
552 static void
553 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
554 {
555    ethumb_client_disconnect(_elm_ethumb_client);
556    _elm_ethumb_client = NULL;
557    _elm_ethumb_connected = EINA_FALSE;
558    _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
559 }
560 #endif
561
562 void
563 _elm_unneed_ethumb(void)
564 {
565 #ifdef ELM_ETHUMB
566    if (--_elm_need_ethumb) return;
567
568    ethumb_client_disconnect(_elm_ethumb_client);
569    _elm_ethumb_client = NULL;
570    ethumb_client_shutdown();
571    ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
572 #endif
573 }
574
575 static Eina_Bool
576 _elm_thumb_dropcb(void *data __UNUSED__, Evas_Object *o, Elm_Selection_Data *drop)
577 {
578    if ((!o) || (!drop) || (!drop->data)) return EINA_FALSE;
579    elm_thumb_file_set(o, drop->data, NULL);
580    return EINA_TRUE;
581 }
582
583 /**
584  * This must be called before any other function that handle with
585  * elm_thumb objects or ethumb_client instances.
586  *
587  * @ingroup Thumb
588  */
589 EAPI Eina_Bool
590 elm_need_ethumb(void)
591 {
592 #ifdef ELM_ETHUMB
593    if (_elm_need_ethumb++) return EINA_TRUE;
594    ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
595    ethumb_client_init();
596    _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
597    return EINA_TRUE;
598 #else
599    return EINA_FALSE;
600 #endif
601 }
602
603 /**
604  * Add a new thumb object to the parent.
605  *
606  * @param parent The parent object.
607  * @return The new object or NULL if it cannot be created.
608  *
609  * @see elm_thumb_file_set()
610  * @see elm_thumb_ethumb_client_get()
611  *
612  * @ingroup Thumb
613  */
614 EAPI Evas_Object *
615 elm_thumb_add(Evas_Object *parent)
616 {
617    Evas *e;
618    Widget_Data *wd;
619    Evas_Object *obj;
620    Evas_Coord minw, minh;
621
622    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
623
624    ELM_SET_WIDTYPE(widtype, "thumb");
625    elm_widget_type_set(obj, "thumb");
626    elm_widget_sub_object_add(parent, obj);
627    elm_widget_data_set(obj, wd);
628    elm_widget_del_hook_set(obj, _del_hook);
629    elm_widget_theme_hook_set(obj, _theme_hook);
630    elm_widget_can_focus_set(obj, EINA_FALSE);
631
632    wd->frame = edje_object_add(e);
633    _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
634    elm_widget_resize_object_set(obj, wd->frame);
635
636    edje_object_size_min_calc(obj, &minw, &minh);
637    evas_object_size_hint_min_set(obj, minw, minh);
638
639    wd->self = obj;
640    wd->view = NULL;
641    wd->file = NULL;
642    wd->key = NULL;
643    wd->eeh = NULL;
644    wd->thumb.id = -1;
645    wd->on_hold = EINA_FALSE;
646    wd->is_video = EINA_FALSE;
647    wd->was_video = EINA_FALSE;
648
649 #ifdef HAVE_ELEMENTARY_ETHUMB
650    wd->thumb.thumb_path = NULL;
651    wd->thumb.thumb_key = NULL;
652    wd->thumb.exists = NULL;
653    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
654                                   _mouse_down_cb, wd);
655    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
656                                   _mouse_up_cb, wd);
657    evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
658                                   _thumb_show_cb, wd);
659    evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
660                                   _thumb_hide_cb, wd);
661 #endif
662
663    // TODO: convert Elementary to subclassing of Evas_Smart_Class
664    // TODO: and save some bytes, making descriptions per-class and not instance!
665    evas_object_smart_callbacks_descriptions_set(obj, _signals);
666    return obj;
667 }
668
669 /**
670  * Reload thumbnail if it was generated before.
671  *
672  * This is useful if the ethumb client configuration changed, like its
673  * size, aspect or any other property one set in the handle returned
674  * by elm_thumb_ethumb_client_get().
675  *
676  * @param obj The thumb object to reload
677  *
678  * @see elm_thumb_file_set()
679  *
680  * @ingroup Thumb
681  */
682 EAPI void
683 elm_thumb_reload(Evas_Object *obj)
684 {
685    ELM_CHECK_WIDTYPE(obj, widtype);
686    Widget_Data *wd = elm_widget_data_get(obj);
687
688    eina_stringshare_replace(&(wd->thumb.file), NULL);
689    eina_stringshare_replace(&(wd->thumb.key), NULL);
690
691 #ifdef HAVE_ELEMENTARY_ETHUMB
692    if (evas_object_visible_get(obj))
693      _thumb_show(wd);
694 #endif
695 }
696
697 /**
698  * Set the file that will be used as thumbnail.
699  *
700  * The file can be an image or a video (in that case, acceptable extensions are:
701  * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
702  * function elm_thumb_animate().
703  *
704  * @param obj The thumb object.
705  * @param file The path to file that will be used as thumb.
706  * @param key The key used in case of an EET file.
707  *
708  * @see elm_thumb_file_get()
709  * @see elm_thumb_reload()
710  * @see elm_thumb_animate()
711  *
712  * @ingroup Thumb
713  */
714 EAPI void
715 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
716 {
717    ELM_CHECK_WIDTYPE(obj, widtype);
718    Eina_Bool file_replaced, key_replaced;
719    Widget_Data *wd = elm_widget_data_get(obj);
720
721    file_replaced = eina_stringshare_replace(&(wd->file), file);
722    key_replaced = eina_stringshare_replace(&(wd->key), key);
723
724    if (file_replaced)
725      {
726         int prefix_size;
727         const char **ext, *ptr;
728         static const char *extensions[] =
729           {
730              ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
731           };
732
733         prefix_size = eina_stringshare_strlen(wd->file) - 4;
734         if (prefix_size >= 0)
735           {
736              ptr = wd->file + prefix_size;
737              wd->is_video = EINA_FALSE;
738              for (ext = extensions; *ext; ext++)
739                if (!strcasecmp(ptr, *ext))
740                  {
741                     wd->is_video = EINA_TRUE;
742                     break;
743                  }
744           }
745      }
746
747    eina_stringshare_replace(&(wd->thumb.file), NULL);
748    eina_stringshare_replace(&(wd->thumb.key), NULL);
749
750 #ifdef HAVE_ELEMENTARY_ETHUMB
751    if (((file_replaced) || (key_replaced)) && (evas_object_visible_get(obj)))
752      _thumb_show(wd);
753 #endif
754 }
755
756 /**
757  * Get the image or video path and key used to generate the thumbnail.
758  *
759  * @param obj The thumb object.
760  * @param file Pointer to filename.
761  * @param key Pointer to key.
762  *
763  * @see elm_thumb_file_set()
764  * @see elm_thumb_path_get()
765  * @see elm_thumb_animate()
766  *
767  * @ingroup Thumb
768  */
769 EAPI void
770 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
771 {
772    ELM_CHECK_WIDTYPE(obj, widtype);
773    Widget_Data *wd = elm_widget_data_get(obj);
774
775    if (file)
776      *file = wd->file;
777    if (key)
778      *key = wd->key;
779 }
780
781 /**
782  * Get the path and key to the image or video generated by ethumb.
783  *
784  * One just need to make sure that the thumbnail was generated before getting
785  * its path; otherwise, the path will be NULL. One way to do that is by asking
786  * for the path when/after the "generate,stop" smart callback is called.
787  *
788  * @param obj The thumb object.
789  * @param file Pointer to thumb path.
790  * @param key Pointer to thumb key.
791  *
792  * @see elm_thumb_file_get()
793  *
794  * @ingroup Thumb
795  */
796 EAPI void
797 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
798 {
799    ELM_CHECK_WIDTYPE(obj, widtype);
800    Widget_Data *wd = elm_widget_data_get(obj);
801
802    if (file)
803      *file = wd->thumb.file;
804    if (key)
805      *key = wd->thumb.key;
806 }
807
808 /**
809  * Set the animation state for the thumb object. If its content is an animated
810  * video, you may start/stop the animation or tell it to play continuously and
811  * looping.
812  *
813  * @param obj The thumb object.
814  * @param setting The animation setting.
815  *
816  * @see elm_thumb_file_set()
817  *
818  * @ingroup Thumb
819  */
820 EAPI void
821 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
822 {
823    ELM_CHECK_WIDTYPE(obj, widtype);
824    Widget_Data *wd = elm_widget_data_get(obj);
825
826    EINA_SAFETY_ON_TRUE_RETURN(setting >= ELM_THUMB_ANIMATION_LAST);
827
828    wd->anim_setting = setting;
829    if (setting == ELM_THUMB_ANIMATION_LOOP)
830      edje_object_signal_emit(wd->view, "animate_loop", "");
831    else if (setting == ELM_THUMB_ANIMATION_START)
832      edje_object_signal_emit(wd->view, "animate", "");
833    else if (setting == ELM_THUMB_ANIMATION_STOP)
834      edje_object_signal_emit(wd->view, "animate_stop", "");
835 }
836
837 /**
838  * Get the animation state for the thumb object.
839  *
840  * @param obj The thumb object.
841  * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
842  * on errors.
843  *
844  * @see elm_thumb_file_get()
845  *
846  * @ingroup Thumb
847  */
848 EAPI Elm_Thumb_Animation_Setting
849 elm_thumb_animate_get(const Evas_Object *obj)
850 {
851    ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
852    Widget_Data *wd = elm_widget_data_get(obj);
853
854    return wd->anim_setting;
855 }
856
857 /**
858  * Get the ethumb_client handle so custom configuration can be made.
859  * This must be called before the objects are created to be sure no object is
860  * visible and no generation started.
861  *
862  * @return Ethumb_Client instance or NULL.
863  *
864  * Example of usage:
865  *
866  * @code
867  * #include <Elementary.h>
868  * #ifndef ELM_LIB_QUICKLAUNCH
869  * EAPI int
870  * elm_main(int argc, char **argv)
871  * {
872  *    Ethumb_Client *client;
873  *
874  *    elm_need_ethumb();
875  *
876  *    // ... your code
877  *
878  *    client = elm_thumb_ethumb_client_get();
879  *    if (!client)
880  *      {
881  *         ERR("could not get ethumb_client");
882  *         return 1;
883  *      }
884  *    ethumb_client_size_set(client, 100, 100);
885  *    ethumb_client_crop_align_set(client, 0.5, 0.5);
886  *    // ... your code
887  *
888  *    // Create elm_thumb objects here
889  *
890  *    elm_run();
891  *    elm_shutdown();
892  *    return 0;
893  * }
894  * #endif
895  * ELM_MAIN()
896  * @endcode
897  *
898  * @ingroup Thumb
899  */
900 EAPI void *
901 elm_thumb_ethumb_client_get(void)
902 {
903    return _elm_ethumb_client;
904 }
905
906 /**
907  * Get the ethumb_client connection state.
908  *
909  * @return EINA_TRUE if the client is connected to the server or
910  *         EINA_FALSE otherwise.
911  */
912 EAPI Eina_Bool
913 elm_thumb_ethumb_client_connected(void)
914 {
915    return _elm_ethumb_connected;
916 }
917
918 EAPI Eina_Bool
919 elm_thumb_editable_set(Evas_Object *obj, Eina_Bool edit)
920 {
921    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
922    Widget_Data *wd = elm_widget_data_get(obj);
923
924    if (!wd) return EINA_FALSE;
925    edit = !!edit;
926    if (wd->edit == edit) return EINA_TRUE;
927
928    wd->edit = edit;
929    if (wd->edit)
930      elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE,
931                          _elm_thumb_dropcb, obj);
932    else
933      elm_drop_target_del(obj);
934
935    return EINA_TRUE;
936 }
937
938 EAPI Eina_Bool
939 elm_thumb_editable_get(const Evas_Object *obj)
940 {
941    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
942    Widget_Data *wd = elm_widget_data_get(obj);
943
944    if (!wd) return EINA_FALSE;
945    return wd->edit;
946 }
947
948 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/