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