elementary: add elm_icon_thumb_set.
authorCedric BAIL <cedric.bail@free.fr>
Tue, 31 May 2011 16:16:01 +0000 (16:16 +0000)
committerCedric BAIL <cedric.bail@free.fr>
Tue, 31 May 2011 16:16:01 +0000 (16:16 +0000)
NOTE: it will only display thumbnail if you requested ethumb in your app.
See fileselector example for more information.

SVN revision: 59848

src/bin/test_fileselector.c
src/lib/Elementary.h.in
src/lib/elc_fileselector.c
src/lib/elm_icon.c
src/lib/elm_thumb.c

index 9a18756..7918f10 100644 (file)
@@ -115,6 +115,8 @@ test_fileselector(void *data       __UNUSED__,
     */
    setlocale(LC_ALL, "");
 
+   elm_need_ethumb();
+
    win = elm_win_add(NULL, "fileselector", ELM_WIN_BASIC);
    elm_win_title_set(win, "File Selector");
    elm_win_autodel_set(win, 1);
index 9e9d5f5..86393c6 100644 (file)
@@ -723,6 +723,7 @@ extern "C" {
 
    EAPI Evas_Object          *elm_icon_add(Evas_Object *parent) EINA_ARG_NONNULL(1);
    EAPI Eina_Bool             elm_icon_file_set(Evas_Object *obj, const char *file, const char *group) EINA_ARG_NONNULL(1, 2);
+   EAPI void                  elm_icon_thumb_set(const Evas_Object *obj, const char *file, const char *group) EINA_ARG_NONNULL(1, 2);
    EAPI void                  elm_icon_file_get(const Evas_Object *obj, const char **file, const char **group) EINA_ARG_NONNULL(1);
    EAPI Eina_Bool             elm_icon_standard_set(Evas_Object *obj, const char *name) EINA_ARG_NONNULL(1);
    EAPI const char           *elm_icon_standard_get(const Evas_Object *obj) EINA_ARG_NONNULL(1);
index e38ec38..bb42a30 100644 (file)
@@ -220,11 +220,43 @@ _itc_icon_get(void        *data,
 
    if (!strcmp(source, "elm.swallow.icon"))
      {
+        const char *filename = data;
+
         ic = elm_icon_add(obj);
         if (ecore_file_is_dir((char *)data))
           elm_icon_standard_set(ic, "folder");
         else
-          elm_icon_standard_set(ic, "file");
+          {
+             static const char *extensions[][2] = {
+               { ".jpg", "image" },
+               { ".png", "image" },
+               { ".gif", "image" },
+               { ".jpeg", "image" },
+               { NULL, NULL }
+             };
+             int len, i;
+             Eina_Bool found = EINA_FALSE;
+
+             len = eina_stringshare_strlen(filename);
+
+             for (i = 0; extensions[i][0]; ++i)
+               {
+                  int lext;
+
+                  lext = strlen(extensions[i][0]);
+                  if (len < lext) continue;
+                  if (!strcasecmp(filename + len - lext, extensions[i][0]))
+                    {
+                       found = EINA_TRUE;
+                       elm_icon_standard_set(ic, extensions[i][1]);
+                       elm_icon_thumb_set(ic, filename, NULL);
+                    }
+               }
+
+             if (!found)
+               elm_icon_standard_set(ic, "file");
+          }
+
         evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL,
                                          1, 1);
         evas_object_show(ic);
index b82c78e..1c4c8a6 100644 (file)
@@ -26,6 +26,26 @@ struct _Widget_Data
    Evas_Object *img;
    const char *stdicon;
    Elm_Icon_Lookup_Order lookup_order;
+
+#ifdef HAVE_ELEMENTARY_ETHUMB
+   struct {
+      int id;
+
+      struct {
+         const char *path;
+         const char *key;
+      } file, thumb;
+
+      Ethumb_Exists *exists;
+
+      Ecore_Event_Handler *eeh;
+
+      Ethumb_Thumb_Format format;
+
+      Eina_Bool retry : 1;
+   } thumb;
+#endif
+
 #ifdef ELM_EFREET
    struct {
         int requested_size;
@@ -39,6 +59,13 @@ struct _Widget_Data
    Eina_Bool no_scale : 1;
 };
 
+#ifdef HAVE_ELEMENTARY_ETHUMB
+static Eina_List *_elm_icon_retry = NULL;
+static int _icon_pending_request = 0;
+
+static void _icon_thumb_exists(Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread, Eina_Bool exists, void *data);
+#endif
+
 static const char *widtype = NULL;
 static void _del_hook(Evas_Object *obj);
 static void _theme_hook(Evas_Object *obj);
@@ -72,13 +99,241 @@ _path_is_absolute(const char *path)
 }
 #endif
 
+#ifdef HAVE_ELEMENTARY_ETHUMB
+static void
+_icon_thumb_stop(Widget_Data *wd, void *ethumbd)
+{
+   if (wd->thumb.id > 0)
+     {
+        ethumb_client_generate_cancel(ethumbd, wd->thumb.id, NULL, NULL, NULL);
+        wd->thumb.id = -1;
+        _icon_pending_request--;
+     }
+
+   if (wd->thumb.exists)
+     {
+        ethumb_client_thumb_exists_cancel(wd->thumb.exists, _icon_thumb_exists, wd);
+        wd->thumb.exists = NULL;
+        _icon_pending_request--;
+     }
+
+   if (wd->thumb.retry)
+     {
+        _elm_icon_retry = eina_list_remove(_elm_icon_retry, wd);
+        wd->thumb.retry = EINA_FALSE;
+     }
+}
+
+static Eina_Bool
+_icon_thumb_display(Widget_Data *wd)
+{
+   Eina_Bool ret = EINA_FALSE;
+
+   if (wd->thumb.format == ETHUMB_THUMB_EET)
+     {
+        static const char *extensions[] = {
+          ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
+        };
+        const char **ext, *ptr;
+        int prefix_size;
+        Eina_Bool video = EINA_FALSE;
+
+        prefix_size = eina_stringshare_strlen(wd->thumb.file.path) - 4;
+        if (prefix_size >= 0)
+          {
+             ptr = wd->thumb.file.path + prefix_size;
+             for (ext = extensions; *ext; ++ext)
+               if (!strcasecmp(ptr, *ext))
+                 {
+                    video = EINA_TRUE;
+                    break;
+                 }
+          }
+
+        if (video)
+          ret = _els_smart_icon_file_edje_set(wd->img, wd->thumb.thumb.path, "movie/thumb");
+     }
+
+   if (!ret)
+     ret = _els_smart_icon_file_key_set(wd->img, wd->thumb.thumb.path, wd->thumb.thumb.key);
+
+   return ret;
+}
+
+static Eina_Bool
+_icon_thumb_retry(Widget_Data *wd)
+{
+   return _icon_thumb_display(wd);
+}
+
+static void
+_icon_thumb_cleanup(Ethumb_Client *ethumbd)
+{
+   Eina_List *l, *ll;
+   Widget_Data *wd;
+
+   EINA_LIST_FOREACH_SAFE(_elm_icon_retry, l, ll, wd)
+     if (_icon_thumb_retry(wd))
+       {
+          _elm_icon_retry = eina_list_remove_list(_elm_icon_retry, l);
+          wd->thumb.retry = EINA_FALSE;
+       }
+
+   if (_icon_pending_request == 0)
+     EINA_LIST_FREE(_elm_icon_retry, wd)
+       _icon_thumb_stop(wd, ethumbd);
+}
+
+static void
+_icon_thumb_finish(Widget_Data *wd, Ethumb_Client *ethumbd)
+{
+   const char *file, *group;
+   Eina_Bool ret;
+
+   _els_smart_icon_file_get(wd->img, &file, &group);
+   file = eina_stringshare_ref(file);
+   group = eina_stringshare_ref(group);
+
+   ret = _icon_thumb_display(wd);
+
+   if (!ret)
+     {
+        const char *p;
+
+        if (!wd->thumb.retry)
+          {
+             _elm_icon_retry = eina_list_append(_elm_icon_retry, wd);
+             wd->thumb.retry = EINA_TRUE;
+          }
+
+        /* Back to previous image */
+        if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
+          _els_smart_icon_file_edje_set(wd->img, file, group);
+        else
+          _els_smart_icon_file_key_set(wd->img, file, group);
+     }
+
+   _icon_thumb_cleanup(ethumbd);
+
+   eina_stringshare_del(file);
+   eina_stringshare_del(group);
+}
+
+static void
+_icon_thumb_cb(void *data,
+               Ethumb_Client *ethumbd,
+               int id,
+               const char *file __UNUSED__,
+               const char *key __UNUSED__,
+               const char *thumb_path,
+               const char *thumb_key,
+               Eina_Bool success)
+{
+   Widget_Data *wd = data;
+
+   EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
+   wd->thumb.id = -1;
+
+   _icon_pending_request--;
+
+   if (success)
+     {
+        eina_stringshare_replace(&wd->thumb.thumb.path, thumb_path);
+        eina_stringshare_replace(&wd->thumb.thumb.key, thumb_key);
+        wd->thumb.format = ethumb_client_format_get(ethumbd);
+
+        _icon_thumb_finish(wd, ethumbd);
+     }
+   else
+     {
+        ERR("could not generate thumbnail for %s (key: %s)", file, key);
+        _icon_thumb_cleanup(ethumbd);
+     }
+}
+
+static void
+_icon_thumb_exists(Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread, Eina_Bool exists, void *data)
+{
+   Widget_Data *wd = data;
+   Ethumb_Client *ethumbd;
+
+   if (ethumb_client_thumb_exists_check(thread))
+     return ;
+
+   wd->thumb.exists = NULL;
+
+   ethumbd = elm_thumb_ethumb_client_get();
+
+   if (exists)
+     {
+        const char *thumb_path, *thumb_key;
+
+        _icon_pending_request--;
+        ethumb_client_thumb_path_get(ethumbd, &thumb_path, &thumb_key);
+        eina_stringshare_replace(&wd->thumb.thumb.path, thumb_path);
+        eina_stringshare_replace(&wd->thumb.thumb.key, thumb_key);
+        wd->thumb.format = ethumb_client_format_get(ethumbd);
+
+        _icon_thumb_finish(wd, ethumbd);
+     }
+   else if ((wd->thumb.id = ethumb_client_generate(ethumbd, _icon_thumb_cb, wd, NULL)) == -1)
+     {
+        /* Failed to generate thumbnail */
+        _icon_pending_request--;
+     }
+}
+
+static void
+_icon_thumb_apply(Widget_Data *wd)
+{
+   Ethumb_Client *ethumbd;
+
+   ethumbd = elm_thumb_ethumb_client_get();
+
+   _icon_thumb_stop(wd, ethumbd);
+
+   if (!wd->thumb.file.path) return ;
+
+   _icon_pending_request++;
+   ethumb_client_file_set(ethumbd, wd->thumb.file.path, wd->thumb.file.key);
+   ethumb_client_size_set(ethumbd, 32, 32);
+   wd->thumb.exists = ethumb_client_thumb_exists(ethumbd, _icon_thumb_exists, wd);
+}
+
+static Eina_Bool
+_icon_thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
+{
+   Widget_Data *wd = data;
+
+   _icon_thumb_apply(wd);
+   return ECORE_CALLBACK_RENEW;
+}
+#endif
+
 static void
 _del_hook(Evas_Object *obj)
 {
    Widget_Data *wd = elm_widget_data_get(obj);
+#ifdef HAVE_ELEMENTARY_ETHUMB
+   Ethumb_Client *ethumbd;
+#endif
 
    if (!wd) return;
    if (wd->stdicon) eina_stringshare_del(wd->stdicon);
+
+#ifdef HAVE_ELEMENTARY_ETHUMB
+   ethumbd = elm_thumb_ethumb_client_get();
+   _icon_thumb_stop(wd, ethumbd);
+
+   eina_stringshare_del(wd->thumb.file.path);
+   eina_stringshare_del(wd->thumb.file.key);
+   eina_stringshare_del(wd->thumb.thumb.path);
+   eina_stringshare_del(wd->thumb.thumb.key);
+
+   if (wd->thumb.eeh)
+     ecore_event_handler_del(wd->thumb.eeh);
+#endif
+
    free(wd);
 }
 
@@ -216,6 +471,10 @@ elm_icon_add(Evas_Object *parent)
 
    evas_object_smart_callbacks_descriptions_set(obj, _signals);
 
+#ifdef HAVE_ELEMENTARY_ETHUMB
+   wd->thumb.id = -1;
+#endif
+
    wd->smooth = EINA_TRUE;
    wd->scale_up = EINA_TRUE;
    wd->scale_down = EINA_TRUE;
@@ -273,6 +532,34 @@ elm_icon_file_get(const Evas_Object *obj, const char **file, const char **group)
    _els_smart_icon_file_get(wd->img, file, group);
 }
 
+EAPI void
+elm_icon_thumb_set(const Evas_Object *obj, const char *file, const char *group)
+{
+   ELM_CHECK_WIDTYPE(obj, widtype);
+   Widget_Data *wd = elm_widget_data_get(obj);
+   if (!wd) return;
+
+#ifdef HAVE_ELEMENTARY_ETHUMB
+   eina_stringshare_replace(&wd->thumb.file.path, file);
+   eina_stringshare_replace(&wd->thumb.file.key, group);
+
+   if (elm_thumb_ethumb_client_connected())
+     {
+        _icon_thumb_apply(wd);
+        return ;
+     }
+
+   if (!wd->thumb.eeh)
+     {
+        wd->thumb.eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, _icon_thumb_apply_cb, wd);
+     }
+#else
+   (void) obj;
+   (void) file;
+   (void) group;
+#endif
+}
+
 static Eina_Bool
 _icon_standard_set(Widget_Data *wd, Evas_Object *obj, const char *name)
 {
index a55bf13..8e8047c 100644 (file)
@@ -87,6 +87,10 @@ Eina_Bool _elm_ethumb_connected = EINA_FALSE;
 #ifdef HAVE_ELEMENTARY_ETHUMB
 static Eina_List *retry = NULL;
 static int pending_request = 0;
+
+static void _thumb_exists(Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread,
+                          Eina_Bool exists, void *data);
+
 #endif
 
 EAPI int ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
@@ -105,7 +109,7 @@ _del_hook(Evas_Object *obj)
      }
    if (wd->thumb.exists)
      {
-        ethumb_client_thumb_exists_cancel(wd->thumb.exists);
+        ethumb_client_thumb_exists_cancel(wd->thumb.exists, _thumb_exists, wd);
         wd->thumb.exists = NULL;
      }
    if (wd->thumb.retry)
@@ -441,7 +445,7 @@ _thumb_apply(Widget_Data *wd)
 
    if (wd->thumb.exists)
      {
-        ethumb_client_thumb_exists_cancel(wd->thumb.exists);
+        ethumb_client_thumb_exists_cancel(wd->thumb.exists, _thumb_exists, wd);
         wd->thumb.exists = NULL;
      }
 
@@ -508,7 +512,7 @@ _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void
 
    if (wd->thumb.exists)
      {
-        ethumb_client_thumb_exists_cancel(wd->thumb.exists);
+        ethumb_client_thumb_exists_cancel(wd->thumb.exists, _thumb_exists, wd);
         wd->thumb.exists = NULL;
      }