extra/src/bin/extra_main.c

500 lines
13 KiB
C

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
/* NOTE: Respecting header order is important for portability.
* Always put system first, then EFL, then your public header,
* and finally your private one. */
#include <Ecore_Getopt.h>
#include <Elementary.h>
#include "gettext.h"
#include "extra.h"
#include "extra_private.h"
#define COPYRIGHT "Copyright © 2016 Andy Williams <andy@andywilliams.me> and various contributors (see AUTHORS)."
typedef struct {
Evas_Object *title;
Evas_Object *screenshot;
Evas_Object *author;
Evas_Object *description;
Evas_Object *progress;
Evas_Object *install;
} Theme_Ui;
typedef struct {
Theme_Ui theme_ui;
Evas_Object *sync_popup;
Evas_Object *ask_popup;
Evas_Object *win;
Evas_Object *theme_list;
} Ui;
static Elm_Genlist_Item_Class _theme_class;
static Extra_Progress _sync_progress, _install_progress, _preview_progress;
static Extra_Theme *_selected_theme;
static Ui ui;
static void
_extra_win_progress_popup_cb(double progress)
{
Evas_Object *p;
p = elm_object_content_get(ui.sync_popup);
if (elm_progressbar_pulse_get(p))
{
elm_progressbar_pulse(p, EINA_FALSE);
elm_progressbar_pulse_set(p, EINA_FALSE);
}
elm_progressbar_value_set(p, progress);
}
static void
_extra_win_progress_popup_show(const char *title)
{
Evas_Object *progress;
ui.sync_popup = elm_popup_add(ui.win);
progress = elm_progressbar_add(ui.sync_popup);
elm_progressbar_pulse_set(progress, EINA_TRUE);
elm_progressbar_pulse(progress, EINA_TRUE);
evas_object_show(progress);
elm_object_part_text_set(ui.sync_popup, "title,text", title);
elm_object_content_set(ui.sync_popup, progress);
evas_object_show(ui.sync_popup);
}
static void
_extra_win_progress_popup_del(void)
{
evas_object_del(ui.sync_popup);
ui.sync_popup = NULL;
}
static void
_extra_win_ask_yes(void *data,
Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Extra_Theme *theme = data;
extra_theme_use(theme);
evas_object_del(ui.ask_popup);
ui.ask_popup = NULL;
}
static void
_extra_win_ask_no(void *data EINA_UNUSED,
Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
evas_object_del(ui.ask_popup);
ui.ask_popup = NULL;
}
static void
_extra_win_ask_for_default(Extra_Theme *theme)
{
Evas_Object *o, *table;
ui.ask_popup = elm_popup_add(ui.win);
table = elm_table_add(ui.ask_popup);
elm_object_content_set(ui.ask_popup, table);
evas_object_show(table);
o = elm_label_add(ui.ask_popup);
elm_object_text_set(o, "Set the theme as default ?");
elm_table_pack(table, o, 0, 0, 2, 1);
evas_object_show(o);
o = elm_button_add(ui.ask_popup);
elm_object_text_set(o, "Yes");
elm_table_pack(table, o, 0, 1, 1, 1);
evas_object_smart_callback_add(o, "clicked", _extra_win_ask_yes, theme);
evas_object_show(o);
o = elm_button_add(ui.ask_popup);
elm_object_text_set(o, "No");
elm_table_pack(table, o, 1, 1, 1, 1);
evas_object_smart_callback_add(o, "clicked", _extra_win_ask_no, NULL);
evas_object_show(o);
evas_object_show(ui.ask_popup);
}
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);
if (preview)
{
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;
snprintf(title, sizeof(title), "<title>%s</title>", theme->name);
elm_object_text_set(ui.theme_ui.title, title);
snprintf(author, sizeof(author), "<link>%s</link>", theme->author);
elm_object_text_set(ui.theme_ui.author, author);
elm_object_text_set(ui.theme_ui.description, theme->description);
elm_progressbar_value_set(ui.theme_ui.progress, 0.0);
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
_extra_win_del(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
elm_exit();
}
static char *
_theme_text_get(void *data, Evas_Object *obj EINA_UNUSED, const char *source EINA_UNUSED)
{
Extra_Theme *theme;
theme = (Extra_Theme *)data;
return strdup(theme->name);
}
static Evas_Object*
_theme_content_get(void *data, Evas_Object *obj, const char *source)
{
Extra_Theme *theme;
theme = (Extra_Theme *) data;
if (!!strcmp(source, "elm.swallow.end"))
return NULL;
if (extra_theme_installed(theme))
{
Evas_Object *icon;
icon = elm_icon_add(obj);
elm_icon_standard_set(icon, "emblem-favorite");
evas_object_show(icon);
return icon;
}
return NULL;
}
static void
_theme_select(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
extra_win_show(data);
elm_object_disabled_set(ui.theme_ui.install, extra_theme_installed(_selected_theme));
}
static void
_extra_win_theme_add(Extra_Theme *theme)
{
elm_genlist_item_append(ui.theme_list, &_theme_class, theme, NULL,
ELM_GENLIST_ITEM_NONE, _theme_select, theme);
}
static void
_extra_win_theme_list_refresh()
{
Extra_Theme *theme;
Eina_List *item;
EINA_LIST_FOREACH(extra_themes_list(), item, theme)
_extra_win_theme_add(theme);
}
static void
_extra_win_sync_done_cb()
{
Extra_Theme *first;
_extra_win_progress_popup_del();
elm_genlist_clear(ui.theme_list);
_extra_win_theme_list_refresh();
first = eina_list_data_get(extra_themes_list());
_theme_select(first, NULL, NULL);
elm_genlist_item_selected_set(
elm_genlist_first_item_get(ui.theme_list), EINA_TRUE);
}
static void
extra_win_sync(void)
{
_sync_progress.progress_cb = _extra_win_progress_popup_cb;
_sync_progress.done_cb = _extra_win_sync_done_cb;
_extra_win_progress_popup_show("Downloading themes");
extra_sync(&_sync_progress);
}
static void
_extra_win_download_done_cb()
{
_extra_win_progress_popup_del();
elm_genlist_clear(ui.theme_list);
_extra_win_theme_list_refresh();
_theme_select(_selected_theme, NULL, NULL);
_extra_win_ask_for_default(_selected_theme);
}
static void
_extra_win_install_execute()
{
_extra_win_progress_popup_show("Installing theme");
_install_progress.progress_cb = _extra_win_progress_popup_cb;
_install_progress.done_cb = _extra_win_download_done_cb;
extra_theme_download(&_install_progress, _selected_theme);
}
static void
_extra_win_install_click_cb(void *data EINA_UNUSED,
Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
_extra_win_install_execute();
}
static Evas_Object *
extra_win_setup(void)
{
Evas_Object *list, *pane, *box, *frame, *table, *install, *icon, *scroller;
ui.win = elm_win_util_standard_add("main", "Extra!");
if (!ui.win) return NULL;
elm_win_focus_highlight_enabled_set(ui.win, EINA_TRUE);
evas_object_smart_callback_add(ui.win, "delete,request", _extra_win_del, NULL);
pane = elm_panes_add(ui.win);
elm_panes_horizontal_set(pane, EINA_FALSE);
elm_panes_content_left_size_set(pane, 0.33);
evas_object_size_hint_weight_set(pane, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(pane, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(pane);
elm_win_resize_object_add(ui.win, pane);
box = elm_box_add(pane);
evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(box);
elm_object_part_content_set(pane, "left", box);
list = elm_genlist_add(box);
evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(list, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(list);
elm_box_pack_end(box, list);
ui.theme_list = list;
frame = elm_frame_add(pane);
elm_object_text_set(frame, "Theme info");
evas_object_size_hint_weight_set(frame, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(frame, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(frame);
elm_object_part_content_set(pane, "right", frame);
scroller = elm_scroller_add(frame);
evas_object_size_hint_weight_set(scroller, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(scroller, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_object_content_set(frame, scroller);
evas_object_show(scroller);
table = elm_table_add(frame);
evas_object_size_hint_weight_set(table, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(table, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_object_content_set(scroller, table);
evas_object_show(table);
ui.theme_ui.title = elm_label_add(table);
evas_object_size_hint_weight_set(ui.theme_ui.title, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(ui.theme_ui.title, 0.0, EVAS_HINT_FILL);
elm_table_pack(table, ui.theme_ui.title, 0, 0, 3, 1);
evas_object_show(ui.theme_ui.title);
install = elm_button_add(table);
icon = elm_icon_add(table);
elm_icon_standard_set(icon, "emblem-downloads");
elm_object_part_content_set(install, "icon", icon);
elm_table_pack(table, install, 3, 0, 1, 1);
evas_object_show(icon);
elm_object_text_set(install, "Install");
elm_object_disabled_set(install, EINA_TRUE);
evas_object_size_hint_weight_set(install, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(install, 1.0, 0.5);
evas_object_show(install);
evas_object_smart_callback_add(install, "clicked", _extra_win_install_click_cb, NULL);
ui.theme_ui.install = install;
ui.theme_ui.screenshot = elm_image_add(table);
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);
evas_object_show(ui.theme_ui.screenshot);
ui.theme_ui.progress = elm_progressbar_add(table);
evas_object_size_hint_weight_set(ui.theme_ui.progress, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(ui.theme_ui.progress, EVAS_HINT_FILL, 0.0);
elm_table_pack(table, ui.theme_ui.progress, 0, 2, 4, 1);
ui.theme_ui.author = elm_entry_add(table);
elm_entry_editable_set(ui.theme_ui.author, EINA_FALSE);
evas_object_size_hint_weight_set(ui.theme_ui.author, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(ui.theme_ui.author, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_table_pack(table, ui.theme_ui.author, 0, 3, 4, 1);
evas_object_show(ui.theme_ui.author);
ui.theme_ui.description = elm_entry_add(table);
elm_entry_editable_set(ui.theme_ui.description, EINA_FALSE);
evas_object_size_hint_weight_set(ui.theme_ui.description, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(ui.theme_ui.description, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_table_pack(table, ui.theme_ui.description, 0, 4, 4, 1);
evas_object_show(ui.theme_ui.description);
evas_object_resize(ui.win, 420 * elm_config_scale_get(),
280 * elm_config_scale_get());
_theme_class.item_style = "default";
_theme_class.func.text_get = _theme_text_get;
_theme_class.func.content_get = _theme_content_get;
evas_object_show(ui.win);
return ui.win;
}
static const Ecore_Getopt optdesc = {
"extra",
"%prog [options]",
PACKAGE_VERSION,
COPYRIGHT,
"3 clause BSD license",
"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"),
ECORE_GETOPT_HELP('h', "help"),
ECORE_GETOPT_SENTINEL
}
};
EAPI_MAIN int
elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
Evas_Object *win;
int args;
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),
ECORE_GETOPT_VALUE_BOOL(quit_option),
ECORE_GETOPT_VALUE_NONE
};
#if ENABLE_NLS
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
bind_textdomain_codeset(PACKAGE, "UTF-8");
textdomain(PACKAGE);
#endif
extra_init();
args = ecore_getopt_parse(&optdesc, values, argc, argv);
if (args < 0)
{
EINA_LOG_CRIT("Could not parse arguments.");
goto end;
}
else if (quit_option)
{
goto end;
}
elm_app_info_set(elm_main, "extra", "images/extra.png");
if (!(win = extra_win_setup()))
goto end;
if (skip_option)
_extra_win_sync_done_cb();
else
extra_win_sync();
elm_run();
end:
extra_shutdown();
elm_shutdown();
return 0;
}
ELM_MAIN()