Browse Source

move config file parsing into its own module.

sisyphus
Dustin Falgout 8 years ago
parent
commit
c10131240c
  1. 2
      data/com.antergos.webkit2-greeter.appdata.xml
  2. 11
      meson.build
  3. 255
      src/greeter-config.c
  4. 58
      src/greeter-config.h
  5. 90
      src/greeter.c
  6. 2
      src/gresource/greeter-resources.gresource.xml
  7. 2
      src/meson.build

2
data/com.antergos.webkit2-greeter.appdata.xml

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Copyright 2017 Dustin Falgout <dustin@antergos.com> -->
<component type="desktop">
<id>com.antergos.lightdm-webkit2-greeter</id>
<id>com.antergos.webkit2-greeter</id>
<metadata_license>CC0-1.0</metadata_license>
<project_license>GPL-3.0+</project_license>
<name>LightDM WebKit2 Greeter</name>

11
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

255
src/greeter-config.c

@ -0,0 +1,255 @@
/*
* greeter-config.c
*
* Copyright © 2017 Antergos Developers <dev@antergos.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <gtk/gtk.h>
#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;
}

58
src/greeter-config.h

@ -0,0 +1,58 @@
/*
* greeter-config.h
*
* Copyright © 2017 Antergos Developers <dev@antergos.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef LIGHTDM_WEBKIT2_GREETER_GREETER_CONFIG_H
#define LIGHTDM_WEBKIT2_GREETER_GREETER_CONFIG_H
#include <glib.h>
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

90
src/greeter.c

@ -32,7 +32,6 @@
*
*/
#include <stdlib.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
@ -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);

2
src/gresource/greeter-resources.gresource.xml

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/antergos/lightdm-webkit2-greeter/">
<gresource prefix="/com/antergos/webkit2-greeter/">
<file>css/style.css</file>
<file>js/bundle.js</file>
<!--<file>js/_vendor/moment-with-locales.min.js</file>

2
src/meson.build

@ -49,7 +49,7 @@ webext = library(
# ------->>> Greeter <<<------- #
# ============================= #
greeter_sources = [gresources, 'greeter.c']
greeter_sources = [gresources, 'greeter.c', 'greeter-config.c']
greeter = executable(
'lightdm-webkit2-greeter',

Loading…
Cancel
Save