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
This commit is contained in:
Andy Williams 2017-01-09 19:40:05 +00:00
parent 9009dc4fce
commit f515c7afa2
2 changed files with 72 additions and 4 deletions

View File

@ -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();

View File

@ -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)
{