about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-04-18 21:41:05 +0100
committerJames Booth <boothj5@gmail.com>2014-04-18 21:41:05 +0100
commitdf345a2cfb7b2fd55826e833ba7e15c97d3a1c18 (patch)
treeee77da3849026e62bfcd2c42cb0a58123b85239a
parentd3a23d42e4e6f049c6fdc47589438de772dcd9e4 (diff)
parent0c1092fd30984918e6c70ccdc3b6ba5f79b08f57 (diff)
downloadprofani-tty-df345a2cfb7b2fd55826e833ba7e15c97d3a1c18.tar.gz
Merge remote-tracking branch 'pasis/iss323'
-rw-r--r--Makefile.am18
-rw-r--r--configure.ac14
-rw-r--r--src/config/theme.c150
3 files changed, 115 insertions, 67 deletions
diff --git a/Makefile.am b/Makefile.am
index 8e71f369..ecd6d598 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -79,8 +79,8 @@ tests_sources = \
 	tests/test_preferences.c \
 	tests/test_server_events.c \
 	tests/test_muc.c \
-    tests/test_cmd_roster.c \
-    tests/test_cmd_win.c \
+	tests/test_cmd_roster.c \
+	tests/test_cmd_win.c \
 	tests/testsuite.c
 
 main_source = src/main.c
@@ -93,6 +93,10 @@ otr3_sources = \
 otr4_sources = \
 	src/otr/otrlib.h src/otr/otrlibv4.c src/otr/otr.h src/otr/otr.c
 
+themes_sources = themes/*
+
+man_sources = docs/profanity.1
+
 if BUILD_OTR
 if BUILD_OTR3
 core_sources += $(otr3_sources)
@@ -106,6 +110,10 @@ endif
 
 bin_PROGRAMS = profanity
 profanity_SOURCES = $(core_sources) $(main_source)
+if THEMES_INSTALL
+profanity_themesdir = @THEMES_PATH@
+profanity_themes_DATA = $(themes_sources)
+endif
 if INCLUDE_GIT_VERSION
 BUILT_SOURCES = $(git_include)
 endif
@@ -115,9 +123,13 @@ check_PROGRAMS = tests/testsuite
 tests_testsuite_SOURCES = $(tests_sources)
 tests_testsuite_LDADD = -lcmocka
 
-man_MANS = docs/profanity.1
+man_MANS = $(man_sources)
+
+EXTRA_DIST = $(man_sources) $(themes_sources)
 
 if INCLUDE_GIT_VERSION
+EXTRA_DIST += .git/HEAD .git/index
+
 $(git_include).in: .git/HEAD .git/index
 	rm -f $@
 	echo "#ifndef PROF_GIT_BRANCH" >> $@
diff --git a/configure.ac b/configure.ac
index 706cc0b4..13f7699e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -42,6 +42,8 @@ AC_ARG_WITH([libxml2],
     [AS_HELP_STRING([--with-libxml2], [link with libxml2 instead of expat])])
 AC_ARG_WITH([xscreensaver],
     [AS_HELP_STRING([--with-xscreensaver], [use libXScrnSaver to determine idle time])])
+AC_ARG_WITH([themes],
+    [AS_HELP_STRING([--with-themes[[=PATH]]], [install themes (default yes)])])
 
 ### Select first existing xml library among expat and libxml2
 PARSER=""
@@ -192,6 +194,15 @@ elif test "x$enable_otr" = x; then
         [AC_MSG_NOTICE([libotr not found, otr encryption support not enabled])])
 fi
 
+AS_IF([test "x$with_themes" = xno],
+    [THEMES_INSTALL="false"],
+    [THEMES_INSTALL="true"])
+AS_IF([test "x$with_themes" = xno -o "x$with_themes" = xyes -o "x$with_themes" = x],
+    [THEMES_PATH='${pkgdatadir}/themes'],
+    [THEMES_PATH="$with_themes"])
+AC_SUBST(THEMES_PATH)
+AM_CONDITIONAL([THEMES_INSTALL], "$THEMES_INSTALL")
+
 ### cmocka is required only for tests, profanity shouldn't be linked with it
 ### TODO: pass cmocka_CFLAGS and cmocka_LIBS to Makefile.am
 PKG_CHECK_MODULES([cmocka], [cmocka], [],
@@ -206,6 +217,7 @@ AM_CFLAGS="-Wall -Wno-deprecated-declarations"
 AS_IF([test "x$PACKAGE_STATUS" = xdevelopment],
     [AM_CFLAGS="$AM_CFLAGS -Wunused -Werror"])
 AM_CPPFLAGS="$AM_CPPFLAGS $glib_CFLAGS $curl_CFLAGS $libnotify_CFLAGS"
+AM_CPPFLAGS="$AM_CPPFLAGS -DTHEMES_PATH=\"\\\"$THEMES_PATH\\\"\""
 LIBS="$LIBS $glib_LIBS $curl_LIBS $libnotify_LIBS"
 
 AC_SUBST(AM_CFLAGS)
@@ -225,5 +237,7 @@ echo "AM_CFLAGS      : $AM_CFLAGS"
 echo "AM_CPPFLAGS    : $AM_CPPFLAGS"
 echo "LIBS           : $LIBS"
 echo "XML Parser     : $PARSER"
+echo "Install themes : $THEMES_INSTALL"
+echo "Themes path    : $THEMES_PATH"
 echo ""
 echo "Now you can run \`make' to build profanity"
diff --git a/src/config/theme.c b/src/config/theme.c
index 3b07e3d9..14e7260c 100644
--- a/src/config/theme.c
+++ b/src/config/theme.c
@@ -103,44 +103,14 @@ static void _set_colour(gchar *val, NCURSES_COLOR_T *pref,
     NCURSES_COLOR_T def);
 static void _load_colours(void);
 static gchar * _get_themes_dir(void);
+void _theme_list_dir(const gchar * const dir, GSList **result);
+static GString * _theme_find(const char * const theme_name);
 
 void
 theme_init(const char * const theme_name)
 {
-    log_info("Loading theme");
-    theme = g_key_file_new();
-
-    if (theme_name != NULL) {
-        gchar *themes_dir = _get_themes_dir();
-        theme_loc = g_string_new(themes_dir);
-        g_free(themes_dir);
-        g_string_append(theme_loc, "/");
-        g_string_append(theme_loc, theme_name);
-        g_key_file_load_from_file(theme, theme_loc->str, G_KEY_FILE_KEEP_COMMENTS,
-            NULL);
-    }
-
-    _load_colours();
-}
-
-GSList *
-theme_list(void)
-{
-    GSList *result = NULL;
-    gchar *themes_dir = _get_themes_dir();
-    GDir *themes = g_dir_open(themes_dir, 0, NULL);
-    if (themes != NULL) {
-        const gchar *theme = g_dir_read_name(themes);
-        while (theme != NULL) {
-            result = g_slist_append(result, strdup(theme));
-            theme = g_dir_read_name(themes);
-        }
-
-        g_dir_close(themes);
-        return result;
-
-    } else {
-        return NULL;
+    if (!theme_load(theme_name) && !theme_load("default")) {
+        log_error("Theme initialisation failed");
     }
 }
 
@@ -148,45 +118,54 @@ gboolean
 theme_load(const char * const theme_name)
 {
     // use default theme
-    if (strcmp(theme_name, "default") == 0) {
-        g_key_file_free(theme);
+    if (theme_name == NULL || strcmp(theme_name, "default") == 0) {
+        if (theme != NULL) {
+            g_key_file_free(theme);
+        }
         theme = g_key_file_new();
-        _load_colours();
-        return TRUE;
-    } else {
-        gchar *themes_dir = _get_themes_dir();
-        GString *new_theme_file = g_string_new(themes_dir);
-        g_free(themes_dir);
-        g_string_append(new_theme_file, "/");
-        g_string_append(new_theme_file, theme_name);
 
-        // no theme file found
-        if (!g_file_test(new_theme_file->str, G_FILE_TEST_EXISTS)) {
+    // load theme from file
+    } else {
+        GString *new_theme_file = _theme_find(theme_name);
+        if (new_theme_file == NULL) {
             log_info("Theme does not exist \"%s\"", theme_name);
-            g_string_free(new_theme_file, TRUE);
             return FALSE;
+        }
 
-        // load from theme file
-        } else {
-            if (theme_loc != NULL) {
-                g_string_free(theme_loc, TRUE);
-            }
-            theme_loc = new_theme_file;
-            log_info("Changing theme to \"%s\"", theme_name);
+        if (theme_loc != NULL) {
+            g_string_free(theme_loc, TRUE);
+        }
+        theme_loc = new_theme_file;
+        log_info("Loading theme \"%s\"", theme_name);
+        if (theme != NULL) {
             g_key_file_free(theme);
-            theme = g_key_file_new();
-            g_key_file_load_from_file(theme, theme_loc->str, G_KEY_FILE_KEEP_COMMENTS,
-                NULL);
-            _load_colours();
-            return TRUE;
         }
+        theme = g_key_file_new();
+        g_key_file_load_from_file(theme, theme_loc->str, G_KEY_FILE_KEEP_COMMENTS,
+            NULL);
     }
+
+    _load_colours();
+    return TRUE;
+}
+
+GSList *
+theme_list(void)
+{
+    GSList *result = NULL;
+    _theme_list_dir(_get_themes_dir(), &result);
+#ifdef THEMES_PATH
+    _theme_list_dir(THEMES_PATH, &result);
+#endif
+    return result;
 }
 
 void
 theme_close(void)
 {
-    g_key_file_free(theme);
+    if (theme != NULL) {
+        g_key_file_free(theme);
+    }
     if (theme_loc != NULL) {
         g_string_free(theme_loc, TRUE);
     }
@@ -434,10 +413,53 @@ _get_themes_dir(void)
 {
     gchar *xdg_config = xdg_get_config_home();
     GString *themes_dir = g_string_new(xdg_config);
-    g_string_append(themes_dir, "/profanity/themes");
-    gchar *result = strdup(themes_dir->str);
     g_free(xdg_config);
-    g_string_free(themes_dir, TRUE);
+    g_string_append(themes_dir, "/profanity/themes");
+    return g_string_free(themes_dir, FALSE);
+}
 
-    return result;
+void
+_theme_list_dir(const gchar * const dir, GSList **result)
+{
+    GDir *themes = g_dir_open(dir, 0, NULL);
+    if (themes != NULL) {
+        const gchar *theme = g_dir_read_name(themes);
+        while (theme != NULL) {
+            *result = g_slist_append(*result, strdup(theme));
+            theme = g_dir_read_name(themes);
+        }
+        g_dir_close(themes);
+    }
+}
+
+static GString *
+_theme_find(const char * const theme_name)
+{
+    GString *path = NULL;
+    gchar *themes_dir = _get_themes_dir();
+
+    if (themes_dir != NULL) {
+        path = g_string_new(themes_dir);
+        g_free(themes_dir);
+        g_string_append(path, "/");
+        g_string_append(path, theme_name);
+        if (!g_file_test(path->str, G_FILE_TEST_EXISTS)) {
+            g_string_free(path, true);
+            path = NULL;
+        }
+    }
+
+#ifdef THEMES_PATH
+    if (path == NULL) {
+        path = g_string_new(THEMES_PATH);
+        g_string_append(path, "/");
+        g_string_append(path, theme_name);
+        if (!g_file_test(path->str, G_FILE_TEST_EXISTS)) {
+            g_string_free(path, true);
+            path = NULL;
+        }
+    }
+#endif /* THEMES_PATH */
+
+    return path;
 }