From f515c7afa2004647548e5f06c3f94045346f0d92 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Mon, 9 Jan 2017 19:40:05 +0000 Subject: [PATCH] cache: Add a theme cache if you want to work 'offline' Add a skip option as well so you can just load the cache instead --- src/bin/extra_main.c | 11 ++++++-- src/lib/extra.c | 65 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/bin/extra_main.c b/src/bin/extra_main.c index 83e36f6..6f622c2 100644 --- a/src/bin/extra_main.c +++ b/src/bin/extra_main.c @@ -132,6 +132,7 @@ _extra_win_ask_for_default(Extra_Theme *theme) evas_object_show(ui.ask_popup); } + static void extra_win_show(Extra_Theme *theme) { @@ -411,6 +412,8 @@ static const Ecore_Getopt optdesc = { "An Enlightenment theme and plugin browser", 0, { + ECORE_GETOPT_STORE_TRUE('s', "skip-sync", + "Skip the initial theme sync when loading UI"), ECORE_GETOPT_LICENSE('L', "license"), ECORE_GETOPT_COPYRIGHT('C', "copyright"), ECORE_GETOPT_VERSION('V', "version"), @@ -424,9 +427,10 @@ elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED) { Evas_Object *win; int args; - Eina_Bool quit_option = EINA_FALSE; + Eina_Bool quit_option = EINA_FALSE, skip_option = EINA_FALSE; Ecore_Getopt_Value values[] = { + ECORE_GETOPT_VALUE_BOOL(skip_option), ECORE_GETOPT_VALUE_BOOL(quit_option), ECORE_GETOPT_VALUE_BOOL(quit_option), ECORE_GETOPT_VALUE_BOOL(quit_option), @@ -459,7 +463,10 @@ elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED) if (!(win = extra_win_setup())) goto end; - extra_win_sync(); + if (skip_option) + _extra_win_sync_done_cb() + else + extra_win_sync(); elm_run(); diff --git a/src/lib/extra.c b/src/lib/extra.c index 4c19dc8..6dd1369 100644 --- a/src/lib/extra.c +++ b/src/lib/extra.c @@ -18,6 +18,8 @@ Eina_List *_theme_list; #define sec_strdup(v) v ? eina_strbuf_string_steal(v) : NULL +void _extra_theme_cache_load(); + static void _extra_theme_add(Eina_Strbuf *id, Eina_Strbuf *name, Eina_Strbuf *author, Eina_Strbuf *description, @@ -51,6 +53,7 @@ extra_init(void) } // Put here your initialization logic of your library + _extra_theme_cache_load(); eina_log_timing(_extra_lib_log_dom, EINA_LOG_STATE_STOP, EINA_LOG_STATE_INIT); @@ -126,7 +129,6 @@ _fill_themes(Eina_Strbuf *buf) const char *string = eina_strbuf_string_get(buf); n = jsmn_parse(&parser, string, strlen(string), parts, 201); - if (n == 0) { printf("No themes received\n"); @@ -141,6 +143,9 @@ _fill_themes(Eina_Strbuf *buf) c += 1; + eina_list_free(_theme_list); + _theme_list = NULL; + for (int i = 0; i < parts[0].size; ++i) { Eina_Strbuf *id = NULL, *name = NULL, *version = NULL, *description = NULL, *author = NULL; @@ -202,16 +207,40 @@ _fill_themes(Eina_Strbuf *buf) return EINA_TRUE; } + +static char * +_theme_cache_path_get() +{ + char *path; + + path = malloc(PATH_MAX * sizeof(char)); + sprintf(path, "%s/.cache/%s/%s.json", eina_environment_home_get(), PACKAGE_NAME, "themes"); + + return path; +} + static Eina_Bool _url_complete_cb(void *data, int type EINA_UNUSED, void *event_info) { Extra_Progress *progress = data;; Ecore_Con_Event_Url_Complete *complete = event_info; Eina_Strbuf *buf; + Eina_Bool parsed; buf = ecore_con_url_data_get(complete->url_con); + parsed = _fill_themes(buf); - _fill_themes(buf); + if (parsed) + { + FILE *cache; + char *cache_path = _theme_cache_path_get(); + const char *content = eina_strbuf_string_get(buf); + + cache = fopen(cache_path, "w"); + fprintf(cache, "%s", content); + fclose(cache); + free(cache_path); + } if (progress->done_cb) progress->done_cb(); @@ -355,6 +384,38 @@ extra_theme_download(Extra_Progress *progress, Extra_Theme *theme) } } +void +_extra_theme_cache_load() +{ + char *cache_path = _theme_cache_path_get(); + + if (ecore_file_exists(cache_path)) + { + Eina_File *cache; + Eina_File_Line *line; + Eina_Iterator *it; + Eina_Strbuf *buf; + INF("Loading themes from cache"); + + cache = eina_file_open(cache_path, EINA_FALSE); + it = eina_file_map_lines(cache); + + buf = eina_strbuf_new(); + EINA_ITERATOR_FOREACH(it, line) + { + eina_strbuf_append_length(buf, line->start, line->length); + } + + _fill_themes(buf); + eina_strbuf_free(buf); + eina_file_close(cache); + } + else + INF("No theme cache found"); + + free(cache_path); +} + static Eina_Bool _enlightenment_restart(void *data EINA_UNUSED) {