bu5hm4n pushed a commit to branch master.

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

commit d770e2a2b1270f3626c3887a2a4a3799b714aaa4
Author: Marcel Hollerbach <marcel-hollerb...@t-online.de>
Date:   Tue Jan 17 22:07:10 2017 +0100

    extra: add api for backgrounds
---
 src/lib/extra.c            | 250 +++++++++++++++++++++++++++++++--------------
 src/lib/extra.h            |  13 ++-
 src/lib/extra_api_helper.c |  56 ++++++++++
 src/lib/extra_private.h    |   5 +
 4 files changed, 245 insertions(+), 79 deletions(-)

diff --git a/src/lib/extra.c b/src/lib/extra.c
index 6e8cb85..f63911f 100644
--- a/src/lib/extra.c
+++ b/src/lib/extra.c
@@ -20,9 +20,6 @@ Eina_List *_background_list = NULL;
 
 void _extra_theme_cache_load();
 
-#define PREVIEW_DOWNLOAD 1
-#define THEME_DOWNLOAD 2
-
 #define GEN_FILE_NAME(buf, t) eina_strbuf_append_printf(buf, "%s-%d.edj", 
t->id, t->version);
 
 //===========
@@ -30,16 +27,11 @@ void _extra_theme_cache_load();
 
 typedef struct {
   Extra_Theme theme;
-  char state; //indicates if some downloads are in progress
+  char preview_state; //indicates if some downloads are in progress
+  char main_state; //indicates if some downloads are in progress
 } Extra_Theme_Private;
 
 typedef struct {
-   Extra_Theme_Private *theme;
-   Extra_Progress *progress;
-   char nand_mask; //will be applied to the status char of the theme
-} Extra_Download_Job;
-
-typedef struct {
   Eina_Strbuf *description;
   Eina_Strbuf *author;
   Eina_Strbuf *name;
@@ -78,7 +70,6 @@ _fill_themes(Eina_Strbuf *buf)
         theme->theme.author = sec_strdup(obj->author);
         theme->theme.description = sec_strdup(obj->description);
         theme->theme.version = versionNumb;
-        theme->state = 0;
         _theme_list = eina_list_append(_theme_list, theme);
 
         extra_json_list_part_free(&template, obj);
@@ -92,6 +83,12 @@ _fill_themes(Eina_Strbuf *buf)
 //background stuff
 
 typedef struct {
+  Extra_Background background;
+  char preview_state; //indicates if some downloads are in progress
+  char main_state; //indicates if some downloads are in progress
+} Extra_Background_Private;
+
+typedef struct {
   Eina_Strbuf *author;
   Eina_Strbuf *name;
   Eina_Strbuf *version;
@@ -103,6 +100,9 @@ _fill_backgrounds(Eina_Strbuf *buf)
 {
    Background_Object *obj;
 
+   eina_list_free(_background_list);
+   _background_list = NULL;
+
    EXTRA_JSON_TO_LIST_TEMPLATE_INIT(template, Background_Object,
     EXTRA_JSON_STRUCT_FIELD("author", Background_Object, author, EINA_FALSE),
     EXTRA_JSON_STRUCT_FIELD("name", Background_Object, name, EINA_TRUE),
@@ -114,15 +114,15 @@ _fill_backgrounds(Eina_Strbuf *buf)
 
    EINA_LIST_FREE(lst, obj)
      {
-        Extra_Background *background = calloc(1, sizeof(Extra_Background));
+        Extra_Background_Private *background = calloc(1, 
sizeof(Extra_Background_Private));
         int versionNumb;
 
         versionNumb = atoi(eina_strbuf_string_get(obj->version));
 
-        background->author = sec_strdup(obj->author);
-        background->id = sec_strdup(obj->id);
-        background->name = sec_strdup(obj->name);
-        background->version = versionNumb;
+        background->background.author = sec_strdup(obj->author);
+        background->background.id = sec_strdup(obj->id);
+        background->background.name = sec_strdup(obj->name);
+        background->background.version = versionNumb;
 
         _background_list = eina_list_append(_background_list, background);
 
@@ -307,6 +307,9 @@ extra_sync(Extra_Progress *progress)
    ecore_con_url_get(req->themes);
 }
 
+//=========
+//Theme Api
+
 EAPI Eina_List *
 extra_themes_list(void)
 {
@@ -357,53 +360,20 @@ extra_theme_install_path_get(Extra_Theme *theme)
    return path;
 }
 
-static void
-_download_complete_cb(void *data, const char *file EINA_UNUSED, int status 
EINA_UNUSED)
-{
-   Extra_Download_Job *job = data;
-
-   job->theme->state &= (~job->nand_mask);
-
-   if (status != 200)
-     ecore_file_remove(file);
-
-   if (job->progress->done_cb)
-     job->progress->done_cb(job->progress->data);
-}
-
-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_Download_Job *job = data;
-   double percent = 0.f;
-
-   if (dlnow > 0.f)
-     percent = ((double)(double)dlnow / (double)dltotal);
-
-   if (job->progress->progress_cb)
-     job->progress->progress_cb(job->progress->data, percent);
-
-   return ECORE_FILE_PROGRESS_CONTINUE;
-}
-
 static char *
-_extra_theme_preview_remote_generate(Extra_Theme *theme)
+_extra_preview_remote_generate(const char *purpose, const char *id)
 {
-   const char *pattern = "http://"; HOSTNAME "/themes/preview/%s.png";
+   const char *pattern = "http://"; HOSTNAME "/%s/preview/%s.png";
    char *url;
 
-   EINA_SAFETY_ON_NULL_RETURN_VAL(theme, NULL);
-
-   url = malloc((strlen(pattern) + strlen(theme->id) - 1) * sizeof(char));
-   sprintf(url, pattern, theme->id);
+   url = malloc((strlen(pattern) + strlen(purpose) + strlen(id) - 1) * 
sizeof(char));
+   sprintf(url, pattern, purpose, id);
 
    return url;
 }
 
 static char *
-_extra_theme_preview_local_generate(Extra_Theme *theme)
+_extra_preview_local_generate(const char *purpose, const char *id, int version)
 {
    Eina_Strbuf *local_preview;
    char *tmp;
@@ -411,7 +381,7 @@ _extra_theme_preview_local_generate(Extra_Theme *theme)
    local_preview = eina_strbuf_new();
 
    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);
+   eina_strbuf_append_printf(local_preview, "/%s/%s-%s-%d.png", PACKAGE_NAME, 
purpose, id, version);
    tmp = eina_strbuf_string_steal(local_preview);
    eina_strbuf_free(local_preview);
 
@@ -425,9 +395,9 @@ extra_theme_preview_get(Extra_Theme *theme)
    Extra_Theme_Private *priv = ((Extra_Theme_Private*) theme);
 
    //download is in progress do not return the path
-   if (priv->state & PREVIEW_DOWNLOAD) return NULL;
+   if (priv->preview_state & EXTRA_STATE_DOWNLOAD_IN_PROGRESS) return NULL;
 
-   local = _extra_theme_preview_local_generate(theme);
+   local = _extra_preview_local_generate("themes", theme->id, theme->version);
    if (!ecore_file_exists(local))
      {
         free(local);
@@ -441,21 +411,12 @@ EAPI void
 extra_theme_preview_download(Extra_Progress *progress, Extra_Theme *theme)
 {
    char *remote, *dst;
-   Extra_Download_Job *job;
    Extra_Theme_Private *priv = ((Extra_Theme_Private*) theme);
 
-   if (priv->state & PREVIEW_DOWNLOAD) return;
-
-   job = calloc(1, sizeof(Extra_Download_Job));
-   job->progress = progress;
-   job->theme = priv;
-   job->nand_mask = PREVIEW_DOWNLOAD;
-   priv->state |= PREVIEW_DOWNLOAD;
+   remote = _extra_preview_remote_generate("themes", theme->id);
+   dst = _extra_preview_local_generate("themes", theme->id, theme->version);
 
-   remote = _extra_theme_preview_remote_generate(theme);
-   dst = _extra_theme_preview_local_generate(theme);
-
-   ecore_file_download(remote, dst, _download_complete_cb, 
_download_progress_cb, job, NULL);
+   extra_file_download(progress, remote, dst, &priv->preview_state);
 
    free(remote);
    free(dst);
@@ -488,17 +449,11 @@ extra_theme_download(Extra_Progress *progress, 
Extra_Theme *theme)
    EINA_SAFETY_ON_NULL_RETURN(progress);
    EINA_SAFETY_ON_NULL_RETURN(theme);
 
-   if (priv->state & THEME_DOWNLOAD) return;
-
-   Extra_Download_Job *job = calloc(1, sizeof(Extra_Download_Job));
-   job->progress = progress;
-   job->theme = priv;
-   job->nand_mask = THEME_DOWNLOAD;
-   priv->state |= THEME_DOWNLOAD;
-
    urlstr = extra_theme_download_url_get(theme);
    path = extra_theme_install_path_get(theme);
-   ecore_file_download(urlstr, path, _download_complete_cb, 
_download_progress_cb, job, NULL);
+
+   extra_file_download(progress, urlstr, path, &priv->main_state);
+
    free(urlstr);
    free(path);
 }
@@ -614,3 +569,142 @@ extra_theme_installed_old(Extra_Theme *t)
    eina_strbuf_free(buf);
    return b;
 }
+
+//==============
+//background api
+
+EAPI Eina_List*
+extra_backgrounds_list(void)
+{
+   return _background_list;
+}
+
+EAPI Extra_Background*
+extra_background_get(const char *id)
+{
+   Eina_List *n;
+   Extra_Background *b;
+
+   EINA_LIST_FOREACH(_background_list, n, b)
+     {
+        if (!strcmp(id, b->id))
+          return b;
+     }
+
+   return NULL;
+}
+
+EAPI char *
+extra_background_download_url_get(Extra_Background *background)
+{
+   Eina_Strbuf *buf;
+   char *url;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(background, NULL);
+
+   buf = eina_strbuf_new();
+   eina_strbuf_append(buf, "http://"; HOSTNAME "/backgrounds/");
+   eina_strbuf_append_printf(buf, "%s-%d.edj", background->id, 
background->version);
+
+   url = eina_strbuf_string_steal(buf);
+   eina_strbuf_free(buf);
+
+   return url;
+}
+
+EAPI char *
+extra_background_install_path_get(Extra_Background *background)
+{
+   Eina_Strbuf *buf;
+   char *path;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(background, NULL);
+
+   buf = eina_strbuf_new();
+
+   eina_strbuf_append(buf, eina_environment_home_get());
+   eina_strbuf_append(buf, "/.e/e/backgrounds/");
+   eina_strbuf_append_printf(buf, "%s-%d.edj", background->id, 
background->version);
+
+   path = eina_strbuf_string_steal(buf);
+   eina_strbuf_free(buf);
+   return path;
+}
+
+EAPI Eina_Bool
+extra_background_installed(Extra_Background *background)
+{
+   char *path;
+   Eina_Bool exists;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(background, EINA_FALSE);
+
+   path = extra_background_install_path_get(background);
+   exists = ecore_file_exists(path);
+   free(path);
+
+   return exists;
+}
+
+EAPI void
+extra_background_download(Extra_Progress *progress, Extra_Background 
*background)
+{
+   char *path, *urlstr = NULL;
+   Extra_Background_Private *priv = ((Extra_Background_Private*) background);
+
+   EINA_SAFETY_ON_NULL_RETURN(progress);
+   EINA_SAFETY_ON_NULL_RETURN(background);
+
+   urlstr = extra_background_download_url_get(background);
+   path = extra_background_install_path_get(background);
+
+   extra_file_download(progress, urlstr, path, &priv->main_state);
+
+   free(urlstr);
+   free(path);
+}
+
+EAPI char*
+extra_background_preview_get(Extra_Background *background)
+{
+   char *local;
+   Extra_Background_Private *priv = (Extra_Background_Private*) background;
+
+   if (priv->preview_state & EXTRA_STATE_DOWNLOAD_IN_PROGRESS) return NULL;
+
+   local = _extra_preview_local_generate("backgrounds", background->id, 
background->version);
+   if (!ecore_file_exists(local))
+     {
+        free(local);
+        return NULL;
+     }
+
+   return local;
+}
+
+EAPI void
+extra_background_preview_download(Extra_Progress *progress, Extra_Background 
*background)
+{
+   char *remote, *local;
+   Extra_Background_Private *priv = (Extra_Background_Private*) background;
+
+   remote = _extra_preview_remote_generate("backgrounds", background->id);
+   local = _extra_preview_local_generate("backgrounds", background->id, 
background->version);
+
+   extra_file_download(progress, remote, local, &priv->preview_state);
+
+   free(local);
+   free(remote);
+}
+
+EAPI void
+extra_background_delete(Extra_Background *b)
+{
+  char *path;
+
+  path = extra_background_install_path_get(b);
+
+  ecore_file_remove(path);
+
+  free(path);
+}
diff --git a/src/lib/extra.h b/src/lib/extra.h
index 41f9be8..68a0472 100644
--- a/src/lib/extra.h
+++ b/src/lib/extra.h
@@ -46,8 +46,8 @@ typedef struct _Extra_Theme
    const char *id;
    const char *name;
    const char *author;
-   const char *description;
    int version;
+   const char *description;
 } Extra_Theme;
 
 typedef struct _Extra_Background
@@ -174,6 +174,17 @@ EAPI Eina_Bool extra_theme_installed_old(Extra_Theme *t);
  * @}
  */
 
+EAPI Eina_List* extra_backgrounds_list(void);
+EAPI Extra_Background* extra_background_get(const char *id);
+
+EAPI Eina_Bool extra_background_installed(Extra_Background *b);
+EAPI void extra_background_download(Extra_Progress *progress, Extra_Background 
*b);
+EAPI void extra_background_delete(Extra_Background *b);
+
+EAPI char* extra_background_preview_get(Extra_Background *background);
+EAPI void extra_background_preview_download(Extra_Progress *progress, 
Extra_Background *background);
+
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/lib/extra_api_helper.c b/src/lib/extra_api_helper.c
index 4fd8299..96ebd61 100644
--- a/src/lib/extra_api_helper.c
+++ b/src/lib/extra_api_helper.c
@@ -190,3 +190,59 @@ extra_json_to_list(Extra_Json_To_List_Template *tmp, 
Eina_Strbuf *buf)
      }
    return ret;
 }
+
+typedef struct {
+   char *state;
+   Extra_Progress *progress;
+} Extra_Download_Job;
+
+static void
+_download_complete_cb(void *data, const char *file EINA_UNUSED, int status 
EINA_UNUSED)
+{
+   Extra_Download_Job *job = data;
+
+   *(job->state) &= (~EXTRA_STATE_DOWNLOAD_IN_PROGRESS);
+
+   if (status != 200)
+     ecore_file_remove(file);
+
+   if (job->progress->done_cb)
+     job->progress->done_cb(job->progress->data);
+}
+
+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_Download_Job *job = data;
+   double percent = 0.f;
+
+   if (dlnow > 0.f)
+     percent = ((double)(double)dlnow / (double)dltotal);
+
+   if (job->progress->progress_cb)
+     job->progress->progress_cb(job->progress->data, percent);
+
+   return ECORE_FILE_PROGRESS_CONTINUE;
+}
+
+void
+extra_file_download(Extra_Progress *progress, const char *from, const char 
*to, char* state)
+{
+   Extra_Download_Job *job;
+
+   if (*state & EXTRA_STATE_DOWNLOAD_IN_PROGRESS) return;
+   if (ecore_file_exists(to))
+     {
+        //TODO better check the header and handle that sanely
+        return;
+     }
+
+   job = calloc(1, sizeof(Extra_Download_Job));
+   job->progress = progress;
+   job->state = state;
+   *state |= EXTRA_STATE_DOWNLOAD_IN_PROGRESS;
+
+   ecore_file_download(from, to, _download_complete_cb, _download_progress_cb, 
job, NULL);
+ }
\ No newline at end of file
diff --git a/src/lib/extra_private.h b/src/lib/extra_private.h
index d097557..83baf27 100644
--- a/src/lib/extra_private.h
+++ b/src/lib/extra_private.h
@@ -38,6 +38,9 @@ typedef struct {
    size_t tuples_size;
 } Extra_Json_To_List_Template;
 
+#define EXTRA_STATE_DOWNLOAD_IN_PROGRESS 2
+
+
 #define EXTRA_JSON_STRUCT_FIELD(name, str, val, required) { name , 
((size_t)&(((str *)0)->val)), required }
 
 #define EXTRA_JSON_TO_LIST_TEMPLATE_INIT(v, str, ...) \
@@ -51,4 +54,6 @@ Extra_Json_To_List_Template v = { \
 Eina_List* extra_json_to_list(Extra_Json_To_List_Template *tmp, Eina_Strbuf 
*buf);
 void extra_json_list_part_free(Extra_Json_To_List_Template *tmp, void *data);
 
+void extra_file_download(Extra_Progress *progress, const char *from, const 
char *to, char* state);
+
 #endif

-- 


Reply via email to