[efl-upgrade]
[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  * @ingroup Elementary
7  *
8  * A thumb object is used for displaying the thumbnail of an image or video.
9  * You must have compiled Elementary with Ethumb_Client support and the DBus
10  * service must be present and auto-activated in order to have thumbnails to
11  * be generated.
12  *
13  * Signals that you can add callbacks for are:
14  *
15  * clicked - This is called when a user has clicked the thumb without dragging
16  * around.
17  *
18  * clicked,double - This is called when a user has double-clicked the thumb.
19  *
20  * press - This is called when a user has pressed down the thumb.
21  *
22  * generate,start - The thumbnail generation started.
23  *
24  * generate,stop - The generation process stopped.
25  *
26  * generate,error - The generation failed.
27  *
28  * load,error - The thumbnail image loading failed.
29  */
30
31 typedef struct _Widget_Data Widget_Data;
32
33 struct _Widget_Data
34 {
35    Evas_Object *self;
36    Evas_Object *frame;
37    Evas_Object *view;
38    const char *file;
39    const char *key;
40    struct
41      {
42         int id;
43         const char *file;
44         const char *key;
45      } thumb;
46    Ecore_Event_Handler *eeh;
47    Elm_Thumb_Animation_Setting anim_setting;
48    Eina_Bool on_hold : 1;
49    Eina_Bool is_video : 1;
50    Eina_Bool was_video : 1;
51 };
52
53 static const char *widtype = NULL;
54
55 static const char SIG_CLICKED[] = "clicked";
56 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
57 static const char SIG_GENERATE_ERROR[] = "generate,error";
58 static const char SIG_GENERATE_START[] = "generate,start";
59 static const char SIG_GENERATE_STOP[] = "generate,stop";
60 static const char SIG_LOAD_ERROR[] = "load,error";
61 static const char SIG_PRESS[]= "press";
62 static const Evas_Smart_Cb_Description _signals[] = {
63   {SIG_CLICKED, ""},
64   {SIG_CLICKED_DOUBLE, ""},
65   {SIG_GENERATE_ERROR, ""},
66   {SIG_GENERATE_START, ""},
67   {SIG_GENERATE_STOP, ""},
68   {SIG_LOAD_ERROR, ""},
69   {SIG_PRESS, ""},
70   {NULL, NULL}
71 };
72
73 static const char EDJE_SIGNAL_GENERATE_START[] = "elm,thumb,generate,start";
74 static const char EDJE_SIGNAL_GENERATE_STOP[] = "elm,thumb,generate,stop";
75 static const char EDJE_SIGNAL_GENERATE_ERROR[] = "elm,thumb,generate,error";
76 static const char EDJE_SIGNAL_LOAD_ERROR[] = "elm,thumb,load,error";
77 static const char EDJE_SIGNAL_PULSE_START[] = "elm,state,pulse,start";
78 static const char EDJE_SIGNAL_PULSE_STOP[] = "elm,state,pulse,stop";
79
80 #ifdef HAVE_ELEMENTARY_ETHUMB
81 Ethumb_Client *_elm_ethumb_client = NULL;
82 #endif
83 Eina_Bool _elm_ethumb_connected = EINA_FALSE;
84
85 EAPI int ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
86
87 static void
88 _del_hook(Evas_Object *obj)
89 {
90    Widget_Data *wd = elm_widget_data_get(obj);
91
92 #ifdef HAVE_ELEMENTARY_ETHUMB
93    if (wd->thumb.id >= 0)
94      ethumb_client_generate_cancel(_elm_ethumb_client, wd->thumb.id,
95                                    NULL, NULL, NULL);
96 #endif
97
98    eina_stringshare_del(wd->file);
99    eina_stringshare_del(wd->key);
100    if (wd->eeh) ecore_event_handler_del(wd->eeh);
101    free(wd);
102 }
103
104 static void
105 _theme_hook(Evas_Object *obj)
106 {
107    Widget_Data *wd = elm_widget_data_get(obj);
108    _elm_theme_object_set(obj, wd->frame, "thumb", "base", elm_widget_style_get(obj));
109 }
110
111 #ifdef HAVE_ELEMENTARY_ETHUMB
112 static void
113 _mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
114 {
115    Widget_Data *wd = data;
116    Evas_Event_Mouse_Down *ev = event_info;
117    if (ev->button != 1)
118      return;
119    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
120      wd->on_hold = EINA_TRUE;
121    else
122      wd->on_hold = EINA_FALSE;
123    if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
124      evas_object_smart_callback_call(wd->self, SIG_CLICKED_DOUBLE, NULL);
125    else
126      evas_object_smart_callback_call(wd->self, SIG_PRESS, NULL);
127 }
128
129 static void
130 _mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
131 {
132
133    Widget_Data *wd = data;
134    Evas_Event_Mouse_Up *ev = event_info;
135    if (ev->button != 1)
136      return;
137    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
138      wd->on_hold = EINA_TRUE;
139    else
140      wd->on_hold = EINA_FALSE;
141    if (!wd->on_hold)
142      evas_object_smart_callback_call(wd->self, SIG_CLICKED, NULL);
143    wd->on_hold = EINA_FALSE;
144 }
145
146 static void
147 _finished_thumb(Widget_Data *wd, const char *thumb_path, const char *thumb_key)
148 {
149    Eina_Bool new_view = EINA_FALSE;
150    int r;
151    Evas_Coord mw, mh;
152    Evas *evas;
153
154    evas = evas_object_evas_get(wd->self);
155    if ((wd->view) && (wd->is_video ^ wd->was_video))
156      {
157         evas_object_del(wd->view);
158         wd->view = NULL;
159      }
160    wd->was_video = wd->is_video;
161
162    if ((wd->is_video) &&
163        (ethumb_client_format_get(_elm_ethumb_client) == ETHUMB_THUMB_EET))
164      {
165         if (!wd->view)
166           {
167              wd->view = edje_object_add(evas);
168              if (!wd->view)
169                {
170                   ERR("could not create edje object");
171                   goto err;
172                }
173              new_view = EINA_TRUE;
174           }
175
176         if (!edje_object_file_set(wd->view, thumb_path, "movie/thumb"))
177           {
178              ERR("could not set file=%s key=%s for %s", thumb_path, thumb_key,
179                  wd->file);
180              goto view_err;
181           }
182      }
183    else
184      {
185         if (!wd->view)
186           {
187              wd->view = evas_object_image_filled_add(evas);
188              if (!wd->view)
189                {
190                   ERR("could not create image object");
191                   goto err;
192                }
193              new_view = EINA_TRUE;
194           }
195
196         evas_object_image_file_set(wd->view, thumb_path, thumb_key);
197         r = evas_object_image_load_error_get(wd->view);
198         if (r != EVAS_LOAD_ERROR_NONE)
199           {
200              ERR("%s: %s", thumb_path, evas_load_error_str(r));
201              goto view_err;
202           }
203      }
204
205    if (new_view) elm_widget_sub_object_add(wd->self, wd->view);
206    edje_object_part_swallow(wd->frame, "elm.swallow.content", wd->view);
207    edje_object_size_min_get(wd->frame, &mw, &mh);
208    edje_object_size_min_restricted_calc(wd->frame, &mw, &mh, mw, mh);
209    evas_object_size_hint_min_set(wd->self, mw, mh);
210    eina_stringshare_replace(&(wd->thumb.file), thumb_path);
211    eina_stringshare_replace(&(wd->thumb.key), thumb_key);
212    edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
213    evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
214    return;
215
216 view_err:
217    evas_object_del(wd->view);
218    wd->view = NULL;
219 err:
220    edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
221    evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
222 }
223
224 static void
225 _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)
226 {
227    Widget_Data *wd = data;
228
229    EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
230    wd->thumb.id = -1;
231
232    edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_STOP, "elm");
233
234    if (success)
235      {
236         _finished_thumb(wd, thumb_path, thumb_key);
237         return;
238      }
239
240    ERR("could not generate thumbnail for %s (key: %s)", file, key ? key : "");
241    edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
242    evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
243 }
244
245 static void
246 _thumb_apply(Widget_Data *wd)
247 {
248    ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
249    if (ethumb_client_thumb_exists(_elm_ethumb_client))
250      {
251         const char *thumb_path, *thumb_key;
252         wd->thumb.id = -1;
253         ethumb_client_thumb_path_get(_elm_ethumb_client, &thumb_path,
254                                      &thumb_key);
255         _finished_thumb(wd, thumb_path, thumb_key);
256         return;
257      }
258    else if ((wd->thumb.id = ethumb_client_generate
259              (_elm_ethumb_client, _finished_thumb_cb, wd, NULL)) != -1)
260      {
261         edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_START,
262                                 "elm");
263         edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_START,
264                                 "elm");
265         evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
266      }
267    else
268      {
269         wd->thumb.id = -1;
270         edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR,
271                                 "elm");
272         evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
273      }
274 }
275
276 static Eina_Bool
277 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
278 {
279    _thumb_apply(data);
280    return ECORE_CALLBACK_RENEW;
281 }
282
283 static void
284 _thumb_show(Widget_Data *wd)
285 {
286    evas_object_show(wd->frame);
287
288    if (elm_thumb_ethumb_client_connected())
289      {
290         _thumb_apply(wd);
291         return;
292      }
293
294    if (!wd->eeh)
295      wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
296                                        _thumb_apply_cb, wd);
297 }
298
299 static void
300 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
301 {
302    _thumb_show(data);
303 }
304
305 static void
306 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
307 {
308    Widget_Data *wd = data;
309
310    evas_object_hide(wd->frame);
311
312    if (wd->thumb.id >= 0)
313      {
314         ethumb_client_generate_cancel
315           (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
316         wd->thumb.id = -1;
317
318         edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
319         evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
320      }
321
322    if (wd->eeh)
323      {
324         ecore_event_handler_del(wd->eeh);
325         wd->eeh = NULL;
326      }
327 }
328
329 #endif
330
331 #ifdef ELM_ETHUMB
332 static Eina_Bool _elm_need_ethumb = EINA_FALSE;
333
334 static void _on_die_cb(void *, Ethumb_Client *);
335
336 static void
337 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
338 {
339    if (success)
340      {
341         ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
342         _elm_ethumb_connected = EINA_TRUE;
343         ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
344      }
345    else
346      _elm_ethumb_client = NULL;
347 }
348
349 static void
350 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
351 {
352    ethumb_client_disconnect(_elm_ethumb_client);
353    _elm_ethumb_client = NULL;
354    _elm_ethumb_connected = EINA_FALSE;
355    _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
356 }
357 #endif
358
359 void
360 _elm_unneed_ethumb(void)
361 {
362 #ifdef ELM_ETHUMB
363    if (_elm_need_ethumb)
364      {
365         _elm_need_ethumb = 0;
366         ethumb_client_disconnect(_elm_ethumb_client);
367         _elm_ethumb_client = NULL;
368         ethumb_client_shutdown();
369         ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
370      }
371 #endif
372 }
373
374 /**
375  * This must be called before any other function that handle with
376  * elm_thumb objects or ethumb_client instances.
377  *
378  * @ingroup Thumb
379  */
380 EAPI void
381 elm_need_ethumb(void)
382 {
383 #ifdef ELM_ETHUMB
384    if (_elm_need_ethumb)
385      return;
386    ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
387    _elm_need_ethumb = 1;
388    ethumb_client_init();
389    _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
390 #endif
391 }
392
393 /**
394  * Add a new thumb object to the parent.
395  *
396  * @param parent The parent object.
397  * @return The new object or NULL if it cannot be created.
398  *
399  * @see elm_thumb_file_set()
400  * @see elm_thumb_ethumb_client_get()
401  *
402  * @ingroup Thumb
403  */
404 EAPI Evas_Object *
405 elm_thumb_add(Evas_Object *parent)
406 {
407    Evas *evas;
408    Widget_Data *wd;
409    Evas_Object *obj;
410    Evas_Coord minw, minh;
411
412    wd = ELM_NEW(Widget_Data);
413    evas = evas_object_evas_get(parent);
414    obj = elm_widget_add(evas);
415    ELM_SET_WIDTYPE(widtype, "thumb");
416    elm_widget_type_set(obj, "thumb");
417    elm_widget_sub_object_add(parent, obj);
418    elm_widget_data_set(obj, wd);
419    elm_widget_del_hook_set(obj, _del_hook);
420    elm_widget_theme_hook_set(obj, _theme_hook);
421
422    wd->frame = edje_object_add(evas);
423    _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
424    elm_widget_resize_object_set(obj, wd->frame);
425
426    edje_object_size_min_calc(obj, &minw, &minh);
427    evas_object_size_hint_min_set(obj, minw, minh);
428
429    wd->self = obj;
430    wd->view = NULL;
431    wd->file = NULL;
432    wd->key = NULL;
433    wd->eeh = NULL;
434    wd->thumb.id = -1;
435    wd->on_hold = EINA_FALSE;
436    wd->is_video = EINA_FALSE;
437    wd->was_video = EINA_FALSE;
438
439 #ifdef HAVE_ELEMENTARY_ETHUMB
440    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
441                                   _mouse_down_cb, wd);
442    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
443                                   _mouse_up_cb, wd);
444    evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
445                                   _thumb_show_cb, wd);
446    evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
447                                   _thumb_hide_cb, wd);
448 #endif
449
450    // TODO: convert Elementary to subclassing of Evas_Smart_Class
451    // TODO: and save some bytes, making descriptions per-class and not instance!
452    evas_object_smart_callbacks_descriptions_set(obj, _signals);
453    return obj;
454 }
455
456 /**
457  * Set the file that will be used as thumbnail.
458  *
459  * The file can be an image or a video (in that case, acceptable extensions are:
460  * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
461  * function elm_thumb_animate().
462  *
463  * @param obj The thumb object.
464  * @param file The path to file that will be used as thumb.
465  * @param key The key used in case of an EET file.
466  *
467  * @see elm_thumb_file_get()
468  * @see elm_thumb_animate()
469  *
470  * @ingroup Thumb
471  */
472 EAPI void
473 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
474 {
475    ELM_CHECK_WIDTYPE(obj, widtype);
476    Eina_Bool file_replaced, key_replaced;
477    Widget_Data *wd = elm_widget_data_get(obj);
478
479    file_replaced = eina_stringshare_replace(&(wd->file), file);
480    key_replaced = eina_stringshare_replace(&(wd->key), key);
481
482    if (file_replaced)
483      {
484         int prefix_size;
485         const char **ext, *ptr;
486         static const char *extensions[] = { ".avi", ".mp4", ".ogv", ".mov",
487                                             ".mpg", ".wmv", NULL };
488
489         prefix_size = eina_stringshare_strlen(wd->file) - 4;
490         if (prefix_size >= 0)
491           {
492              ptr = wd->file + prefix_size;
493              wd->is_video = EINA_FALSE;
494              for (ext = extensions; *ext; ext++)
495                if (!strcasecmp(ptr, *ext))
496                  {
497                     wd->is_video = EINA_TRUE;
498                     break;
499                  }
500           }
501      }
502
503    eina_stringshare_replace(&(wd->thumb.file), NULL);
504    eina_stringshare_replace(&(wd->thumb.key), NULL);
505
506 #ifdef HAVE_ELEMENTARY_ETHUMB
507    if ((file_replaced || key_replaced) && evas_object_visible_get(obj))
508      _thumb_show(wd);
509 #endif
510 }
511
512 /**
513  * Get the image or video path and key used to generate the thumbnail.
514  *
515  * @param obj The thumb object.
516  * @param file Pointer to filename.
517  * @param key Pointer to key.
518  *
519  * @see elm_thumb_file_set()
520  * @see elm_thumb_path_get()
521  * @see elm_thumb_animate()
522  *
523  * @ingroup Thumb
524  */
525 EAPI void
526 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
527 {
528    ELM_CHECK_WIDTYPE(obj, widtype);
529    Widget_Data *wd = elm_widget_data_get(obj);
530    if (file)
531      *file = wd->file;
532    if (key)
533      *key = wd->key;
534 }
535
536 /**
537  * Get the path and key to the image or video generated by ethumb.
538  *
539  * One just need to make sure that the thumbnail was generated before getting
540  * its path; otherwise, the path will be NULL. One way to do that is by asking
541  * for the path when/after the "generate,stop" smart callback is called.
542  *
543  * @param obj The thumb object.
544  * @param file Pointer to thumb path.
545  * @param key Pointer to thumb key.
546  *
547  * @see elm_thumb_file_get()
548  *
549  * @ingroup Thumb
550  */
551 EAPI void
552 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
553 {
554    ELM_CHECK_WIDTYPE(obj, widtype);
555    Widget_Data *wd = elm_widget_data_get(obj);
556    if (file)
557      *file = wd->thumb.file;
558    if (key)
559      *key = wd->thumb.key;
560 }
561
562 /**
563  * Set the animation state for the thumb object. If its content is an animated
564  * video, you may start/stop the animation or tell it to play continuously and
565  * looping.
566  *
567  * @param obj The thumb object.
568  * @param setting The animation setting.
569  *
570  * @see elm_thumb_file_set()
571  *
572  * @ingroup Thumb
573  */
574 EAPI void
575 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
576 {
577    ELM_CHECK_WIDTYPE(obj, widtype);
578    Widget_Data *wd = elm_widget_data_get(obj);
579
580    if (setting < ELM_THUMB_ANIMATION_START ||
581        setting >= ELM_THUMB_ANIMATION_LAST)
582      {
583         return;
584      }
585
586    wd->anim_setting = setting;
587    if (setting == ELM_THUMB_ANIMATION_LOOP)
588      edje_object_signal_emit(wd->view, "animate_loop", "");
589    else if (setting == ELM_THUMB_ANIMATION_START)
590      edje_object_signal_emit(wd->view, "animate", "");
591    else if (setting == ELM_THUMB_ANIMATION_STOP)
592      edje_object_signal_emit(wd->view, "animate_stop", "");
593 }
594
595 /**
596  * Get the animation state for the thumb object.
597  *
598  * @param obj The thumb object.
599  * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
600  * on errors.
601  *
602  * @see elm_thumb_file_get()
603  *
604  * @ingroup Thumb
605  */
606 EAPI Elm_Thumb_Animation_Setting
607 elm_thumb_animate_get(const Evas_Object *obj)
608 {
609    ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
610    Widget_Data *wd = elm_widget_data_get(obj);
611
612    return wd->anim_setting;
613 }
614
615 /**
616  * Get the ethumb_client handle so custom configuration can be made.
617  * This must be called before the objects are created to be sure no object is
618  * visible and no generation started.
619  *
620  * @return Ethumb_Client instance or NULL.
621  *
622  * Example of usage:
623  *
624  * @code
625  * #include <Elementary.h>
626  * #ifndef ELM_LIB_QUICKLAUNCH
627  * EAPI int
628  * elm_main(int argc, char **argv)
629  * {
630  *    Ethumb_Client *client;
631  *
632  *    elm_need_ethumb();
633  *
634  *    // ... your code
635  *
636  *    client = elm_thumb_ethumb_client_get();
637  *    if (!client)
638  *      {
639  *         ERR("could not get ethumb_client");
640  *         return 1;
641  *      }
642  *    ethumb_client_size_set(client, 100, 100);
643  *    ethumb_client_crop_align_set(client, 0.5, 0.5);
644  *    // ... your code
645  *
646  *    // Create elm_thumb objects here
647  *
648  *    elm_run();
649  *    elm_shutdown();
650  *    return 0;
651  * }
652  * #endif
653  * ELM_MAIN()
654  * @endcode
655  *
656  * @ingroup Thumb
657  */
658 #ifdef ELM_ETHUMB
659 EAPI Ethumb_Client *
660 elm_thumb_ethumb_client_get(void)
661 {
662    return _elm_ethumb_client;
663 }
664 #else
665 EAPI void *
666 elm_thumb_ethumb_client_get(void)
667 {
668    return NULL;
669 }
670 #endif
671
672 /**
673  * Get the ethumb_client connection state.
674  *
675  * @return EINA_TRUE if the client is connected to the server or
676  *         EINA_FALSE otherwise.
677  */
678 EAPI Eina_Bool
679 elm_thumb_ethumb_client_connected(void)
680 {
681    return _elm_ethumb_connected;
682 }