From c10131240c1eb8e59e52e45192fb16ed12463096 Mon Sep 17 00:00:00 2001 From: Dustin Falgout Date: Sat, 7 Jan 2017 23:40:40 -0600 Subject: [PATCH] move config file parsing into its own module. --- data/com.antergos.webkit2-greeter.appdata.xml | 2 +- meson.build | 11 +- src/greeter-config.c | 255 ++++++++++++++++++ src/greeter-config.h | 58 ++++ src/greeter.c | 90 +------ src/gresource/greeter-resources.gresource.xml | 2 +- src/meson.build | 2 +- 7 files changed, 332 insertions(+), 88 deletions(-) create mode 100644 src/greeter-config.c create mode 100644 src/greeter-config.h diff --git a/data/com.antergos.webkit2-greeter.appdata.xml b/data/com.antergos.webkit2-greeter.appdata.xml index a5d05fd..065549f 100644 --- a/data/com.antergos.webkit2-greeter.appdata.xml +++ b/data/com.antergos.webkit2-greeter.appdata.xml @@ -1,7 +1,7 @@ - com.antergos.lightdm-webkit2-greeter + com.antergos.webkit2-greeter CC0-1.0 GPL-3.0+ LightDM WebKit2 Greeter diff --git a/meson.build b/meson.build index c056dd0..374acf0 100644 --- a/meson.build +++ b/meson.build @@ -1,6 +1,5 @@ project('lightdm-webkit2-greeter', 'c', version: '2.2.1', license: 'GPL-3') - # ================================== # # ------->>> Version Vars <<<------- # # ================================== # @@ -54,9 +53,9 @@ conf.set('THEME_DIR', '"@0@"'.format(get_option('with-theme-dir'))) conf.set('CONFIG_DIR', '"@0@"'.format(get_option('with-config-dir'))) -conf.set('CONFIG_FILE', '"@0@/@1@"'.format(get_option('with-config-dir'), config_file) +conf.set('CONFIG_FILE', '"@0@"'.format(join_paths(get_option('with-config-dir'), config_file))) -conf.set('CONFIG_FILE_LEGACY', '"@0@/@1@"'.format(get_option('with-config-dir'), config_file_legacy) +conf.set('CONFIG_FILE_LEGACY', '"@0@"'.format(join_paths(get_option('with-config-dir'), config_file_legacy))) conf.set('DESKTOP_DIR', '"@0@"'.format(get_option('with-desktop-dir'))) @@ -64,6 +63,12 @@ conf.set('WEBEXT_DIR', '"@0@"'.format(get_option('with-webext-dir'))) conf.set('GRESOURCE_PATH', '"/com/antergos/webkit2-greeter"') +conf.set('BACKGROUND_IMAGES_DIR', '"/usr/share/backgrounds"') + +conf.set('LOGO_IMAGE', '"@0@"'.format(join_paths(get_option('with-theme-dir'), 'antergos/img/antergos.png'))) + +conf.set('USER_IMAGE', '"@0@"'.format(join_paths(get_option('with-theme-dir'), 'antergos/img/antergos-logo-user.png'))) + if has_webkitgtk_2_14 conf.set('HAS_WEBKITGTK_2_14', has_webkitgtk_2_14) endif diff --git a/src/greeter-config.c b/src/greeter-config.c new file mode 100644 index 0000000..83328d3 --- /dev/null +++ b/src/greeter-config.c @@ -0,0 +1,255 @@ +/* + * greeter-config.c + * + * Copyright © 2017 Antergos Developers + * + * This file is part of lightdm-webkit2-greeter. + * + * lightdm-webkit2-greeter is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * lightdm-webkit2-greeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * The following additional terms are in effect as per Section 7 of the license: + * + * The preservation of all legal notices and author attributions in + * the material or in the Appropriate Legal Notices displayed + * by works containing it is required. + * + * You should have received a copy of the GNU General Public License + * along with lightdm-webkit2-greeter; If not, see . + */ + +#include +#include + +#include "greeter-config.h" +#include "config.h" +#include "greeter-resources.h" + +/* Work-around CLion bug */ +#ifndef CONFIG_DIR +#include "../build/src/config.h" +#include "../build/src/greeter-resources.h" +#endif + +static Config config_instance; +static Config *config_instance_p = NULL; + + +static void +apply_defaults(void) { + config_instance.greeter->debug_mode = FALSE; + config_instance.greeter->detect_theme_errors = TRUE; + config_instance.greeter->secure_mode = TRUE; + config_instance.greeter->screensaver_timeout = 300; + config_instance.greeter->time_format = "LT"; + config_instance.greeter->time_language = "auto"; + config_instance.greeter->webkit_theme = "antergos"; + + config_instance.branding->background_images = BACKGROUND_IMAGES_DIR; + config_instance.branding->logo = LOGO_IMAGE; + config_instance.branding->user_image = USER_IMAGE; +} + + +static gchar * +rtrim_comments(gchar *str) { + gchar *ptr = NULL; + + ptr = strchr(str, '#'); + + if (NULL != ptr) { + *ptr = '\0'; + } + + return g_strstrip(str); +} + + +Config* +get_config(void) { + if (NULL != config_instance_p) { + return config_instance_p; + } + + // Apply default config. + apply_defaults(); + + GError *err = NULL; + GKeyFile *keyfile = g_key_file_new(); + + g_key_file_load_from_file(keyfile, CONFIG_FILE, G_KEY_FILE_NONE, &err); + + if (NULL != err) { + g_clear_error(&err); + g_key_file_load_from_file(keyfile, CONFIG_FILE_LEGACY, G_KEY_FILE_NONE, &err); + + if (NULL != err) { + // Can't load config file. Bail. + goto cleanup; + } + } + + + /* ----->>> WebKit Theme <<<----- */ + gchar *theme = g_key_file_get_string(keyfile, "greeter", "webkit_theme", &err); + + if (NULL != err) { + g_clear_error(&err); + theme = g_key_file_get_string(keyfile, "greeter", "webkit-theme", &err); + } + + if (NULL != err) { + g_clear_error(&err); + } else { + config_instance.greeter->webkit_theme = rtrim_comments(theme); + } + + + /* ----->>> Screensaver Timeout <<<----- */ + gint screensaver_timeout = g_key_file_get_integer(keyfile, "greeter", "screensaver_timeout", &err); + + if (NULL != err) { + g_clear_error(&err); + screensaver_timeout = g_key_file_get_integer(keyfile, "greeter", "screensaver-timeout", &err); + } + + if (NULL != err) { + g_clear_error(&err); + } else { + config_instance.greeter->screensaver_timeout = screensaver_timeout; + } + + + /* ----->>> Debug Mode <<<----- */ + gboolean debug_mode = g_key_file_get_boolean(keyfile, "greeter", "debug_mode", &err); + + if (NULL != err) { + g_clear_error(&err); + debug_mode = g_key_file_get_integer(keyfile, "greeter", "debug-mode", &err); + } + + if (NULL != err) { + g_clear_error(&err); + } else { + config_instance.greeter->debug_mode = debug_mode; + } + + + /* ----->>> Secure Mode <<<----- */ + gboolean secure_mode = g_key_file_get_boolean(keyfile, "greeter", "secure_mode", &err); + + if (NULL != err) { + g_clear_error(&err); + secure_mode = g_key_file_get_integer(keyfile, "greeter", "secure-mode", &err); + } + + if (NULL != err) { + g_clear_error(&err); + } else { + config_instance.greeter->secure_mode = secure_mode; + } + + + /* ----->>> Theme Error Detection <<<----- */ + gboolean detect_theme_errors = g_key_file_get_boolean(keyfile, "greeter", "detect_theme_errors", &err); + + if (NULL != err) { + g_clear_error(&err); + detect_theme_errors = g_key_file_get_integer(keyfile, "greeter", "detect-theme-errors", &err); + } + + if (NULL != err) { + g_clear_error(&err); + } else { + config_instance.greeter->detect_theme_errors = detect_theme_errors; + } + + + /* ----->>> Time Format <<<----- */ + gchar *time_format = g_key_file_get_string(keyfile, "greeter", "time_format", &err); + + if (NULL != err) { + g_clear_error(&err); + time_format = g_key_file_get_string(keyfile, "greeter", "time-format", &err); + } + + if (NULL != err) { + g_clear_error(&err); + } else { + config_instance.greeter->time_format = time_format; + } + + + /* ----->>> Time Language <<<----- */ + gchar *time_language = g_key_file_get_string(keyfile, "greeter", "time_language", &err); + + if (NULL != err) { + g_clear_error(&err); + time_language = g_key_file_get_string(keyfile, "greeter", "time-language", &err); + } + + if (NULL != err) { + g_clear_error(&err); + } else { + config_instance.greeter->time_language = time_language; + } + + + /* ----->>> Background Images Directory <<<----- */ + gchar *background_images = g_key_file_get_string(keyfile, "branding", "background_images", &err); + + if (NULL != err) { + g_clear_error(&err); + background_images = g_key_file_get_string(keyfile, "branding", "background-images", &err); + } + + if (NULL != err) { + g_clear_error(&err); + } else { + config_instance.branding->background_images = background_images; + } + + + /* ----->>> User Avatar Image <<<----- */ + gchar *user_image = g_key_file_get_string(keyfile, "branding", "user_image", &err); + + if (NULL != err) { + g_clear_error(&err); + user_image = g_key_file_get_string(keyfile, "branding", "user-image", &err); + } + + if (NULL != err) { + g_clear_error(&err); + } else { + config_instance.branding->user_image = user_image; + } + + + /* ----->>> Logo <<<----- */ + gchar *logo = g_key_file_get_string(keyfile, "branding", "logo", &err); + + if (NULL != err) { + g_clear_error(&err); + } else { + config_instance.branding->logo = logo; + } + + goto cleanup; + + + cleanup: + config_instance_p = &config_instance; + + if (NULL != err) { + g_error_free(err); + } + + return config_instance_p; +} diff --git a/src/greeter-config.h b/src/greeter-config.h new file mode 100644 index 0000000..9db7755 --- /dev/null +++ b/src/greeter-config.h @@ -0,0 +1,58 @@ +/* + * greeter-config.h + * + * Copyright © 2017 Antergos Developers + * + * This file is part of lightdm-webkit2-greeter. + * + * lightdm-webkit2-greeter is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * lightdm-webkit2-greeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * The following additional terms are in effect as per Section 7 of the license: + * + * The preservation of all legal notices and author attributions in + * the material or in the Appropriate Legal Notices displayed + * by works containing it is required. + * + * You should have received a copy of the GNU General Public License + * along with lightdm-webkit2-greeter; If not, see . + */ + +#ifndef LIGHTDM_WEBKIT2_GREETER_GREETER_CONFIG_H +#define LIGHTDM_WEBKIT2_GREETER_GREETER_CONFIG_H + +#include + +typedef struct greeter_config { + gboolean debug_mode; + gboolean detect_theme_errors; + gint screensaver_timeout; + gboolean secure_mode; + gchar *time_format; + gchar *time_language; + gchar *webkit_theme; +} Greeter_Config; + +typedef struct branding_config { + gchar *background_images; + gchar *logo; + gchar *user_image; +} Branding_Config; + +typedef struct config { + Greeter_Config *greeter; + Branding_Config *branding; +} Config; + + +Config* get_config(void); + + +#endif // LIGHTDM_WEBKIT2_GREETER_GREETER_CONFIG_H diff --git a/src/greeter.c b/src/greeter.c index b0bd726..1fdd997 100644 --- a/src/greeter.c +++ b/src/greeter.c @@ -32,7 +32,6 @@ * */ -#include #include #include #include @@ -45,12 +44,7 @@ #include "config.h" #include "greeter-resources.h" - -/* Work-around CLion bug */ -#ifndef CONFIG_DIR -#include "../build/src/config.h" -#include "../build/src/greeter-resources.h" -#endif +#include "greeter-config.h" static GtkWidget *web_view; @@ -67,9 +61,7 @@ static int prefer_blanking, allow_exposures; -static gint config_timeout; - -static gboolean debug_mode; +static Config *conf; static void @@ -103,7 +95,7 @@ context_menu_cb(WebKitWebView *view, * menu being shown. Thus, we are returning the opposite of debug_mode to get * desired result (which is only show menu when debug_mode is enabled. */ - return (! debug_mode); + return (! conf->greeter->debug_mode); } @@ -114,11 +106,10 @@ context_menu_cb(WebKitWebView *view, static void lock_hint_enabled_handler(void) { Display *display = gdk_x11_display_get_xdisplay(default_display); - config_timeout = (0 != config_timeout) ? config_timeout : 300; XGetScreenSaver(display, &timeout, &interval, &prefer_blanking, &allow_exposures); XForceScreenSaver(display, ScreenSaverActive); - XSetScreenSaver(display, config_timeout, 0, PreferBlanking, DefaultExposures); + XSetScreenSaver(display, conf->greeter->screensaver_timeout, 0, PreferBlanking, DefaultExposures); } @@ -276,7 +267,7 @@ message_received_cb(WebKitUserContentManager *manager, lock_hint_enabled_handler(); } else { - printf("UI PROCESS - message_received_cb(): no match!"); + printf("UI PROCESS - message_received_cb(): no match for: %s", message_str); } g_free(message_str); @@ -290,20 +281,6 @@ quit_cb(void) { } -static gchar * -rtrim_comments(gchar *str) { - gchar *ptr = NULL; - - ptr = strchr(str, '#'); - - if (NULL != ptr) { - *ptr = '\0'; - } - - return g_strstrip(str); -} - - static void javascript_bundle_injection_setup() { WebKitUserScript *bundle; @@ -339,9 +316,6 @@ main(int argc, char **argv) { GdkScreen *screen; GdkWindow *root_window; GdkRectangle geometry; - GKeyFile *keyfile; - gchar *theme; - GError *err = NULL; GdkRGBA bg_color; WebKitWebContext *context; GtkCssProvider *css_provider; @@ -364,56 +338,8 @@ main(int argc, char **argv) { g_unix_signal_add(SIGINT, (GSourceFunc) quit_cb, NULL); g_unix_signal_add(SIGHUP, (GSourceFunc) quit_cb, NULL); - /* BEGIN Load Greeter Config */ - /* TODO: Handle config values and fallbacks some other way, this is ugly! */ - keyfile = g_key_file_new(); - - g_key_file_load_from_file(keyfile, CONFIG_FILE, G_KEY_FILE_NONE, &err); - - if (NULL != err) { - g_clear_error(&err); - g_key_file_load_from_file(keyfile, CONFIG_FILE_LEGACY, G_KEY_FILE_NONE, &err); - - if (NULL != err) { - // Unable to load config file. Use defaults. - theme = "antergos"; - config_timeout = 300; - debug_mode = FALSE; - } - } - - theme = g_key_file_get_string(keyfile, "greeter", "webkit_theme", &err); - - if ( NULL != err) { - g_clear_error(&err); - theme = g_key_file_get_string(keyfile, "greeter", "webkit-theme", &err); - - if ( NULL != err) { - g_clear_error(&err); - theme = "antergos"; - } - } - - theme = rtrim_comments(theme); - config_timeout = g_key_file_get_integer(keyfile, "greeter", "screensaver_timeout", &err); - - if ( NULL != err) { - g_clear_error(&err); - config_timeout = g_key_file_get_integer(keyfile, "greeter", "screensaver-timeout", &err); - - if ( NULL != err) { - g_clear_error(&err); - config_timeout = 300; - } - } - - debug_mode = g_key_file_get_boolean(keyfile, "greeter", "debug_mode", &err); - - if (NULL != err) { - g_error_free(err); - debug_mode = FALSE; - } - /* END Greeter Config File */ + /* Initialize greeter config */ + conf = get_config(); /* Set default cursor */ root_window = gdk_get_default_root_window(); @@ -491,7 +417,7 @@ main(int argc, char **argv) { /* There's no turning back now, let's go! */ gtk_container_add(GTK_CONTAINER(window), web_view); webkit_web_view_load_uri(WEBKIT_WEB_VIEW(web_view), - g_strdup_printf("file://%s/%s/index.html", THEME_DIR, theme)); + g_strdup_printf("file://%s/%s/index.html", THEME_DIR, conf->greeter->webkit_theme)); gtk_widget_show_all(window); gtk_widget_set_can_focus(GTK_WIDGET(web_view), TRUE); diff --git a/src/gresource/greeter-resources.gresource.xml b/src/gresource/greeter-resources.gresource.xml index 0db86d4..fa2c5bc 100644 --- a/src/gresource/greeter-resources.gresource.xml +++ b/src/gresource/greeter-resources.gresource.xml @@ -1,6 +1,6 @@ - + css/style.css js/bundle.js >> Greeter <<<------- # # ============================= # -greeter_sources = [gresources, 'greeter.c'] +greeter_sources = [gresources, 'greeter.c', 'greeter-config.c'] greeter = executable( 'lightdm-webkit2-greeter',