bu5hm4n pushed a commit to branch master.

http://git.enlightenment.org/apps/extra.git/commit/?id=7175cb317b066789d3aaba4419ccbb06891ed71b

commit 7175cb317b066789d3aaba4419ccbb06891ed71b
Author: Marcel Hollerbach <[email protected]>
Date:   Mon Jan 9 21:44:08 2017 +0100

    extra: implement image caching
    
    this just downloads the images to a cache location
---
 src/bin/extra_main.c | 56 ++++++++++++++++++++----------
 src/lib/extra.c      | 98 ++++++++++++++++++++++++++++++++++++++--------------
 src/lib/extra.h      |  3 +-
 3 files changed, 111 insertions(+), 46 deletions(-)

diff --git a/src/bin/extra_main.c b/src/bin/extra_main.c
index 90792bd..bf105cd 100644
--- a/src/bin/extra_main.c
+++ b/src/bin/extra_main.c
@@ -36,7 +36,7 @@ typedef struct {
 
 static Elm_Genlist_Item_Class _theme_class;
 
-static Extra_Progress _sync_progress, _install_progress;
+static Extra_Progress _sync_progress, _install_progress, _preview_progress;
 
 static Extra_Theme *_selected_theme;
 
@@ -134,9 +134,30 @@ _extra_win_ask_for_default(Extra_Theme *theme)
 }
 
 static void
+_download_progress_cb(double progress)
+{
+   elm_progressbar_value_set(ui.theme_ui.progress, progress);
+}
+
+static void
+_download_done(void)
+{
+   char *preview;
+
+   preview = extra_theme_preview_get(_selected_theme);
+   elm_image_file_set(ui.theme_ui.screenshot, preview, NULL);
+   evas_object_show(ui.theme_ui.screenshot);
+   free(preview);
+
+   evas_object_hide(ui.theme_ui.progress);
+}
+
+static void
 extra_win_show(Extra_Theme *theme)
 {
    char title[1024], author[1024];
+   char *preview;
+
    _selected_theme = theme;
 
    if (!theme) return;
@@ -150,8 +171,21 @@ extra_win_show(Extra_Theme *theme)
    elm_object_text_set(ui.theme_ui.description, theme->description);
 
    elm_progressbar_value_set(ui.theme_ui.progress, 0.0);
-   elm_image_file_set(ui.theme_ui.screenshot, 
extra_theme_preview_url_get(_selected_theme), NULL);
-   evas_object_show(ui.theme_ui.progress);
+
+   preview = extra_theme_preview_get(_selected_theme);
+   if (preview)
+     {
+        elm_image_file_set(ui.theme_ui.screenshot, preview, NULL);
+        free(preview);
+     }
+   else
+     {
+        _preview_progress.progress_cb = _download_progress_cb;
+        _preview_progress.done_cb = _download_done;
+        extra_theme_preview_download(&_preview_progress, _selected_theme);
+        evas_object_hide(ui.theme_ui.screenshot);
+        evas_object_show(ui.theme_ui.progress);
+     }
 }
 
 static void
@@ -278,20 +312,6 @@ _extra_win_install_click_cb(void *data EINA_UNUSED,
 
 }
 
-static void
-_download_progress_cb(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, 
void *event_info EINA_UNUSED)
-{
-   Elm_Photocam_Progress *prog = event_info;
-
-   if (prog->total > 0)
-     elm_progressbar_value_set(ui.theme_ui.progress, prog->now / prog->total);
-}
-
-static void
-_download_done(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void 
*event_info EINA_UNUSED)
-{
-   evas_object_hide(ui.theme_ui.progress);
-}
 
 static Evas_Object *
 extra_win_setup(void)
@@ -366,8 +386,6 @@ extra_win_setup(void)
    ui.theme_ui.install = install;
 
    ui.theme_ui.screenshot = elm_image_add(table);
-   evas_object_smart_callback_add(ui.theme_ui.screenshot, "download,progress", 
_download_progress_cb, NULL);
-   evas_object_smart_callback_add(ui.theme_ui.screenshot, "download,done", 
_download_done, NULL);
    evas_object_size_hint_weight_set(ui.theme_ui.screenshot, EVAS_HINT_EXPAND, 
EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(ui.theme_ui.screenshot, EVAS_HINT_FILL, 
EVAS_HINT_FILL);
    elm_table_pack(table, ui.theme_ui.screenshot, 0, 1, 4, 2);
diff --git a/src/lib/extra.c b/src/lib/extra.c
index b3dd697..05c0eb3 100644
--- a/src/lib/extra.c
+++ b/src/lib/extra.c
@@ -326,8 +326,34 @@ extra_theme_install_path_get(Extra_Theme *theme)
    return path;
 }
 
-EAPI char *
-extra_theme_preview_url_get(Extra_Theme *theme)
+static void
+_download_complete_cb(void *data, const char *file EINA_UNUSED, int status 
EINA_UNUSED)
+{
+   Extra_Progress *progress = data;
+
+   if (progress->done_cb)
+     progress->done_cb();
+}
+
+static int
+_download_progress_cb(void *data EINA_UNUSED, const char *file EINA_UNUSED,
+                      long int dltotal EINA_UNUSED, long int dlnow EINA_UNUSED,
+                      long int ultotal EINA_UNUSED, long int ulnow EINA_UNUSED)
+{
+   Extra_Progress *p = data;
+   double percent = 0.f;
+
+   if (dlnow > 0.f)
+     percent = ((double)(double)dlnow / (double)dltotal);
+
+   if (p->progress_cb)
+     p->progress_cb(percent);
+
+   return ECORE_FILE_PROGRESS_CONTINUE;
+}
+
+static char *
+_extra_theme_preview_remote_generate(Extra_Theme *theme)
 {
    const char *pattern = "http://"; HOSTNAME "/themes/preview/%s.png";
    char *url;
@@ -340,46 +366,66 @@ extra_theme_preview_url_get(Extra_Theme *theme)
    return url;
 }
 
-EAPI char *
-extra_theme_download_url_get(Extra_Theme *theme)
+static char *
+_extra_theme_preview_local_generate(Extra_Theme *theme)
 {
-   const char *pattern = "http://"; HOSTNAME "/themes/%s-%d.edj";
-   char *url;
+   Eina_Strbuf *local_preview;
+   char *tmp;
 
-   EINA_SAFETY_ON_NULL_RETURN_VAL(theme, NULL);
+   local_preview = eina_strbuf_new();
 
-   url = malloc((strlen(pattern) + strlen(theme->id) - 1 + 
(int)(log10(theme->version))) * sizeof(char));
-   sprintf(url, pattern, theme->id, theme->version);
+   eina_strbuf_append(local_preview, efreet_cache_home_get());
+   eina_strbuf_append_printf(local_preview, "/%s/%s-%d.png", PACKAGE_NAME, 
theme->id, theme->version);
+   tmp = eina_strbuf_string_steal(local_preview);
+   eina_strbuf_free(local_preview);
 
-   return url;
+   return tmp;
 }
 
-static void
-_download_complete_cb(void *data, const char *file EINA_UNUSED, int status 
EINA_UNUSED)
+EAPI char*
+extra_theme_preview_get(Extra_Theme *theme)
 {
-   Extra_Progress *progress = data;
+   char *local;
 
-   if (progress->done_cb)
-     progress->done_cb();
+   local = _extra_theme_preview_local_generate(theme);
+   if (!ecore_file_exists(local))
+     {
+        free(local);
+        local = NULL;
+     }
+
+   return local;
 }
 
-static int
-_download_progress_cb(void *data EINA_UNUSED, const char *file EINA_UNUSED,
-                      long int dltotal EINA_UNUSED, long int dlnow EINA_UNUSED,
-                      long int ultotal EINA_UNUSED, long int ulnow EINA_UNUSED)
+EAPI void
+extra_theme_preview_download(Extra_Progress *progress, Extra_Theme *theme)
 {
-   Extra_Progress *p = data;
-   double percent = 0.f;
+   char *remote, *dst;
 
-   if (dlnow > 0.f)
-     percent = ((double)(double)dlnow / (double)dltotal);
+   remote = _extra_theme_preview_remote_generate(theme);
+   dst = _extra_theme_preview_local_generate(theme);
 
-   if (p->progress_cb)
-     p->progress_cb(percent);
+   ecore_file_download(remote, dst, _download_complete_cb, 
_download_progress_cb, progress, NULL);
 
-   return ECORE_FILE_PROGRESS_CONTINUE;
+   free(remote);
+   free(dst);
+}
+
+EAPI char *
+extra_theme_download_url_get(Extra_Theme *theme)
+{
+   const char *pattern = "http://"; HOSTNAME "/themes/%s-%d.edj";
+   char *url;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(theme, NULL);
+
+   url = malloc((strlen(pattern) + strlen(theme->id) - 1 + 
(int)(log10(theme->version))) * sizeof(char));
+   sprintf(url, pattern, theme->id, theme->version);
+
+   return url;
 }
 
+
 EAPI void
 extra_theme_download(Extra_Progress *progress, Extra_Theme *theme)
 {
diff --git a/src/lib/extra.h b/src/lib/extra.h
index 355a703..3a76ba0 100644
--- a/src/lib/extra.h
+++ b/src/lib/extra.h
@@ -138,7 +138,8 @@ EAPI Eina_Bool extra_theme_installed(Extra_Theme *theme);
 
 EAPI char *extra_theme_install_path_get(Extra_Theme *theme);
 
-EAPI char *extra_theme_preview_url_get(Extra_Theme *theme);
+EAPI char *extra_theme_preview_get(Extra_Theme *theme);
+EAPI void extra_theme_preview_download(Extra_Progress *progress, Extra_Theme 
*theme);
 
 EAPI char *extra_theme_download_url_get(Extra_Theme *theme);
 

-- 


Reply via email to