From ac91e7ef8578fda44704d264f78bc5004c81d947 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 21 Feb 2016 23:58:13 +0000 Subject: [PATCH] Added theme support to plugins --- Makefile.am | 4 +- src/plugins/api.c | 102 ++++++++---------------------- src/plugins/api.h | 7 +-- src/plugins/c_api.c | 44 +++++-------- src/plugins/plugins.c | 3 + src/plugins/profapi.c | 7 +-- src/plugins/profapi.h | 7 +-- src/plugins/themes.c | 142 ++++++++++++++++++++++++++++++++++++++++++ src/plugins/themes.h | 37 +++++++++++ 9 files changed, 234 insertions(+), 119 deletions(-) create mode 100644 src/plugins/themes.c create mode 100644 src/plugins/themes.h diff --git a/Makefile.am b/Makefile.am index dea7f949..fc7882d6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -45,7 +45,8 @@ core_sources = \ src/plugins/plugins.h src/plugins/plugins.c \ src/plugins/api.h src/plugins/api.c \ src/plugins/callbacks.h src/plugins/callbacks.c \ - src/plugins/autocompleters.c src/plugins/autocompleters.h + src/plugins/autocompleters.c src/plugins/autocompleters.h \ + src/plugins/themes.c src/plugins/themes.h unittest_sources = \ src/contact.c src/contact.h src/common.c \ @@ -77,6 +78,7 @@ unittest_sources = \ src/plugins/api.h src/plugins/api.c \ src/plugins/callbacks.h src/plugins/callbacks.c \ src/plugins/autocompleters.c src/plugins/autocompleters.h \ + src/plugins/themes.c src/plugins/themes.h \ src/window_list.c src/window_list.h \ src/event/server_events.c src/event/server_events.h \ src/event/client_events.c src/event/client_events.h \ diff --git a/src/plugins/api.c b/src/plugins/api.c index 9855165f..549a56e5 100644 --- a/src/plugins/api.c +++ b/src/plugins/api.c @@ -40,6 +40,7 @@ #include "log.h" #include "plugins/callbacks.h" #include "plugins/autocompleters.h" +#include "plugins/themes.h" #include "profanity.h" #include "ui/ui.h" #include "config/theme.h" @@ -68,6 +69,24 @@ api_cons_show(const char * const message) return 1; } +int +api_cons_show_themed(const char *const group, const char *const key, const char *const def, const char *const message) +{ + if (message == NULL) { + log_warning("%s", "prof_cons_show_themed failed, message is NULL"); + return 0; + } + + char *parsed = str_replace(message, "\r\n", "\n"); + theme_item_t themeitem = plugin_themes_get(group, key, def); + ProfWin *console = wins_get_console(); + win_print(console, '-', 0, NULL, 0, themeitem, "", parsed); + + free(parsed); + + return 1; +} + int api_cons_bad_cmd_usage(const char *const cmd) { @@ -267,97 +286,26 @@ api_win_show(const char *tag, const char *line) } int -api_win_show_green(const char *tag, const char *line) +api_win_show_themed(const char *tag, const char *const group, const char *const key, const char *const def, const char *line) { if (tag == NULL) { - log_warning("%s", "prof_win_show_green failed, tag is NULL"); + log_warning("%s", "prof_win_show_themed failed, tag is NULL"); return 0; } if (line == NULL) { - log_warning("%s", "prof_win_show_green failed, line is NULL"); + log_warning("%s", "prof_win_show_themed failed, line is NULL"); return 0; } ProfPluginWin *pluginwin = wins_get_plugin(tag); if (pluginwin == NULL) { - log_warning("prof_win_show_green failed, no window with tag: %s", tag); + log_warning("prof_win_show_themed failed, no window with tag: %s", tag); return 0; } + theme_item_t themeitem = plugin_themes_get(group, key, def); ProfWin *window = (ProfWin*)pluginwin; - win_print(window, '!', 0, NULL, 0, THEME_GREEN, "", line); - - return 1; -} - -int -api_win_show_red(const char *tag, const char *line) -{ - if (tag == NULL) { - log_warning("%s", "prof_win_show_red failed, tag is NULL"); - return 0; - } - if (line == NULL) { - log_warning("%s", "prof_win_show_red failed, line is NULL"); - return 0; - } - - ProfPluginWin *pluginwin = wins_get_plugin(tag); - if (pluginwin == NULL) { - log_warning("prof_win_show_red failed, no window with tag: %s", tag); - return 0; - } - - ProfWin *window = (ProfWin*)pluginwin; - win_print(window, '!', 0, NULL, 0, THEME_RED, "", line); - - return 1; -} - -int -api_win_show_cyan(const char *tag, const char *line) -{ - if (tag == NULL) { - log_warning("%s", "prof_win_show_cyan failed, tag is NULL"); - return 0; - } - if (line == NULL) { - log_warning("%s", "prof_win_show_cyan failed, line is NULL"); - return 0; - } - - ProfPluginWin *pluginwin = wins_get_plugin(tag); - if (pluginwin == NULL) { - log_warning("prof_win_show_cyan failed, no window with tag: %s", tag); - return 0; - } - - ProfWin *window = (ProfWin*)pluginwin; - win_print(window, '!', 0, NULL, 0, THEME_CYAN, "", line); - - return 1; -} - -int -api_win_show_yellow(const char *tag, const char *line) -{ - if (tag == NULL) { - log_warning("%s", "prof_win_show_yellow failed, tag is NULL"); - return 0; - } - if (line == NULL) { - log_warning("%s", "prof_win_show_yellow failed, line is NULL"); - return 0; - } - - ProfPluginWin *pluginwin = wins_get_plugin(tag); - if (pluginwin == NULL) { - log_warning("prof_win_show_yellow failed, no window with tag: %s", tag); - return 0; - } - - ProfWin *window = (ProfWin*)pluginwin; - win_print(window, '!', 0, NULL, 0, THEME_YELLOW, "", line); + win_print(window, '!', 0, NULL, 0, themeitem, "", line); return 1; } diff --git a/src/plugins/api.h b/src/plugins/api.h index baf7c132..3f1b7d1c 100644 --- a/src/plugins/api.h +++ b/src/plugins/api.h @@ -39,6 +39,7 @@ void api_cons_alert(void); int api_cons_show(const char * const message); +int api_cons_show_themed(const char *const group, const char *const item, const char *const def, const char *const message); int api_cons_bad_cmd_usage(const char *const cmd); void api_notify(const char *message, const char *category, int timeout_ms); void api_send_line(char *line); @@ -62,9 +63,7 @@ void api_win_create(const char *tag, void *callback, void(*callback_func)(PluginWindowCallback *window_callback, char *tag, char *line)); int api_win_focus(const char *tag); int api_win_show(const char *tag, const char *line); -int api_win_show_green(const char *tag, const char *line); -int api_win_show_red(const char *tag, const char *line); -int api_win_show_cyan(const char *tag, const char *line); -int api_win_show_yellow(const char *tag, const char *line); +int api_win_show_themed(const char *tag, const char *const group, const char *const key, const char *const def, const char *line); + #endif diff --git a/src/plugins/c_api.c b/src/plugins/c_api.c index c4b24561..e2b7517a 100644 --- a/src/plugins/c_api.c +++ b/src/plugins/c_api.c @@ -65,6 +65,12 @@ c_api_cons_show(const char * const message) return api_cons_show(message); } +static int +c_api_cons_show_themed(const char *const group, const char *const item, const char *const def, const char *const message) +{ + return api_cons_show_themed(group, item, def, message); +} + static int c_api_cons_bad_cmd_usage(const char *const cmd) { @@ -144,13 +150,13 @@ c_api_log_error(const char *message) api_log_error(message); } -int +static int c_api_win_exists(char *tag) { return api_win_exists(tag); } -void +static void c_api_win_create(char *tag, void(*callback)(char *tag, char *line)) { WindowWrapper *wrapper = malloc(sizeof(WindowWrapper)); @@ -158,40 +164,22 @@ c_api_win_create(char *tag, void(*callback)(char *tag, char *line)) api_win_create(tag, wrapper, c_window_callback); } -int +static int c_api_win_focus(char *tag) { return api_win_focus(tag); } -int +static int c_api_win_show(char *tag, char *line) { return api_win_show(tag, line); } -int -c_api_win_show_green(char *tag, char *line) +static int +c_api_win_show_themed(char *tag, char *group, char *key, char *def, char *line) { - return api_win_show_green(tag, line); -} - -int -c_api_win_show_red(char *tag, char *line) -{ - return api_win_show_red(tag, line); -} - -int -c_api_win_show_cyan(char *tag, char *line) -{ - return api_win_show_cyan(tag, line); -} - -int -c_api_win_show_yellow(char *tag, char *line) -{ - return api_win_show_yellow(tag, line); + return api_win_show_themed(tag, group, key, def, line); } void @@ -223,6 +211,7 @@ c_api_init(void) { prof_cons_alert = c_api_cons_alert; prof_cons_show = c_api_cons_show; + prof_cons_show_themed = c_api_cons_show_themed; prof_cons_bad_cmd_usage = c_api_cons_bad_cmd_usage; prof_register_command = c_api_register_command; prof_register_timed = c_api_register_timed; @@ -239,8 +228,5 @@ c_api_init(void) prof_win_create = c_api_win_create; prof_win_focus = c_api_win_focus; prof_win_show = c_api_win_show; - prof_win_show_green = c_api_win_show_green; - prof_win_show_red = c_api_win_show_red; - prof_win_show_cyan = c_api_win_show_cyan; - prof_win_show_yellow = c_api_win_show_yellow; + prof_win_show_themed = c_api_win_show_themed; } diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index b39bc355..20293c88 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -40,6 +40,7 @@ #include "log.h" #include "plugins/callbacks.h" #include "plugins/autocompleters.h" +#include "plugins/themes.h" #include "plugins/api.h" #include "plugins/plugins.h" @@ -57,6 +58,7 @@ plugins_init(void) { plugins = NULL; autocompleters_init(); + plugin_themes_init(); #ifdef PROF_HAVE_C c_env_init(); @@ -401,6 +403,7 @@ plugins_shutdown(void) #endif autocompleters_destroy(); + plugin_themes_close(); } gchar * diff --git a/src/plugins/profapi.c b/src/plugins/profapi.c index b8046373..5157ec72 100644 --- a/src/plugins/profapi.c +++ b/src/plugins/profapi.c @@ -39,6 +39,7 @@ void (*prof_cons_alert)(void) = NULL; int (*prof_cons_show)(const char * const message) = NULL; +int (*prof_cons_show_themed)(const char *const group, const char *const item, const char *const def, const char *const message) = NULL; int (*prof_cons_bad_cmd_usage)(const char *const cmd) = NULL; void (*prof_register_command)(const char *command_name, int min_args, int max_args, @@ -65,7 +66,5 @@ int (*prof_win_exists)(PROF_WIN_TAG win) = NULL; void (*prof_win_create)(PROF_WIN_TAG win, void(*input_handler)(PROF_WIN_TAG win, char *line)) = NULL; int (*prof_win_focus)(PROF_WIN_TAG win) = NULL; int (*prof_win_show)(PROF_WIN_TAG win, char *line) = NULL; -int (*prof_win_show_green)(PROF_WIN_TAG win, char *line) = NULL; -int (*prof_win_show_red)(PROF_WIN_TAG win, char *line) = NULL; -int (*prof_win_show_cyan)(PROF_WIN_TAG win, char *line) = NULL; -int (*prof_win_show_yellow)(PROF_WIN_TAG win, char *line) = NULL; +int (*prof_win_show_themed)(PROF_WIN_TAG tag, char *group, char *key, char *def, char *line) = NULL; + diff --git a/src/plugins/profapi.h b/src/plugins/profapi.h index 834a7210..1be99820 100644 --- a/src/plugins/profapi.h +++ b/src/plugins/profapi.h @@ -39,6 +39,7 @@ typedef char* PROF_WIN_TAG; void (*prof_cons_alert)(void); int (*prof_cons_show)(const char * const message); +int (*prof_cons_show_themed)(const char *const group, const char *const item, const char *const def, const char *const message); int (*prof_cons_bad_cmd_usage)(const char *const cmd); void (*prof_register_command)(const char *command_name, int min_args, int max_args, @@ -65,9 +66,7 @@ int (*prof_win_exists)(PROF_WIN_TAG win); void (*prof_win_create)(PROF_WIN_TAG win, void(*input_handler)(PROF_WIN_TAG win, char *line)); int (*prof_win_focus)(PROF_WIN_TAG win); int (*prof_win_show)(PROF_WIN_TAG win, char *line); -int (*prof_win_show_green)(PROF_WIN_TAG win, char *line); -int (*prof_win_show_red)(PROF_WIN_TAG win, char *line); -int (*prof_win_show_cyan)(PROF_WIN_TAG win, char *line); -int (*prof_win_show_yellow)(PROF_WIN_TAG win, char *line); +int (*prof_win_show_themed)(PROF_WIN_TAG tag, char *group, char *key, char *def, char *line); + #endif diff --git a/src/plugins/themes.c b/src/plugins/themes.c new file mode 100644 index 00000000..5a50d4e6 --- /dev/null +++ b/src/plugins/themes.c @@ -0,0 +1,142 @@ +/* + * themes.c + * + * Copyright (C) 2012 - 2016 James Booth + * + * This file is part of Profanity. + * + * Profanity 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. + * + * Profanity 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. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +#include +#include + +#include "config/theme.h" +#include "common.h" + +static GKeyFile *themes; + +void +plugin_themes_init(void) +{ + gchar *xdg_data = xdg_get_data_home(); + GString *fileloc = g_string_new(xdg_data); + g_string_append(fileloc, "/profanity/plugin_themes"); + g_free(xdg_data); + + if (g_file_test(fileloc->str, G_FILE_TEST_EXISTS)) { + g_chmod(fileloc->str, S_IRUSR | S_IWUSR); + } + + themes = g_key_file_new(); + g_key_file_load_from_file(themes, fileloc->str, G_KEY_FILE_KEEP_COMMENTS, NULL); + + gsize g_data_size; + gchar *g_data = g_key_file_to_data(themes, &g_data_size, NULL); + g_file_set_contents(fileloc->str, g_data, g_data_size, NULL); + g_chmod(fileloc->str, S_IRUSR | S_IWUSR); + g_free(g_data); + g_string_free(fileloc, TRUE); + +} + +void +plugin_themes_close(void) +{ + g_key_file_free(themes); + themes = NULL; +} + +theme_item_t +plugin_themes_get(const char *const group, const char *const key, const char *const def) +{ + if (g_key_file_has_key(themes, group, key, NULL)) { + gchar *result = g_key_file_get_string(themes, group, key, NULL); + + theme_item_t ret; + + if (g_strcmp0(result, "white") == 0) ret = THEME_WHITE; + else if (g_strcmp0(result, "bold_white") == 0) ret = THEME_WHITE_BOLD; + else if (g_strcmp0(result, "red") == 0) ret = THEME_RED; + else if (g_strcmp0(result, "bold_red") == 0) ret = THEME_RED_BOLD; + else if (g_strcmp0(result, "green") == 0) ret = THEME_GREEN; + else if (g_strcmp0(result, "bold_green") == 0) ret = THEME_GREEN_BOLD; + else if (g_strcmp0(result, "blue") == 0) ret = THEME_BLUE; + else if (g_strcmp0(result, "bold_blue") == 0) ret = THEME_BLUE_BOLD; + else if (g_strcmp0(result, "yellow") == 0) ret = THEME_YELLOW; + else if (g_strcmp0(result, "bold_yellow") == 0) ret = THEME_YELLOW_BOLD; + else if (g_strcmp0(result, "cyan") == 0) ret = THEME_CYAN; + else if (g_strcmp0(result, "bold_cyan") == 0) ret = THEME_CYAN_BOLD; + else if (g_strcmp0(result, "magenta") == 0) ret = THEME_MAGENTA; + else if (g_strcmp0(result, "bold_magenta") == 0) ret = THEME_MAGENTA_BOLD; + else if (g_strcmp0(result, "black") == 0) ret = THEME_BLACK; + else if (g_strcmp0(result, "bold_black") == 0) ret = THEME_BLACK_BOLD; + + else if (g_strcmp0(def, "white") == 0) ret = THEME_WHITE; + else if (g_strcmp0(def, "bold_white") == 0) ret = THEME_WHITE_BOLD; + else if (g_strcmp0(def, "red") == 0) ret = THEME_RED; + else if (g_strcmp0(def, "bold_red") == 0) ret = THEME_RED_BOLD; + else if (g_strcmp0(def, "green") == 0) ret = THEME_GREEN; + else if (g_strcmp0(def, "bold_green") == 0) ret = THEME_GREEN_BOLD; + else if (g_strcmp0(def, "blue") == 0) ret = THEME_BLUE; + else if (g_strcmp0(def, "bold_blue") == 0) ret = THEME_BLUE_BOLD; + else if (g_strcmp0(def, "yellow") == 0) ret = THEME_YELLOW; + else if (g_strcmp0(def, "bold_yellow") == 0) ret = THEME_YELLOW_BOLD; + else if (g_strcmp0(def, "cyan") == 0) ret = THEME_CYAN; + else if (g_strcmp0(def, "bold_cyan") == 0) ret = THEME_CYAN_BOLD; + else if (g_strcmp0(def, "magenta") == 0) ret = THEME_MAGENTA; + else if (g_strcmp0(def, "bold_magenta") == 0) ret = THEME_MAGENTA_BOLD; + else if (g_strcmp0(def, "black") == 0) ret = THEME_BLACK; + else if (g_strcmp0(def, "bold_black") == 0) ret = THEME_BLACK_BOLD; + + else ret = THEME_TEXT; + + g_free(result); + + return ret; + + } else { + if (g_strcmp0(def, "white") == 0) return THEME_WHITE; + else if (g_strcmp0(def, "bold_white") == 0) return THEME_WHITE_BOLD; + else if (g_strcmp0(def, "red") == 0) return THEME_RED; + else if (g_strcmp0(def, "bold_red") == 0) return THEME_RED_BOLD; + else if (g_strcmp0(def, "green") == 0) return THEME_GREEN; + else if (g_strcmp0(def, "bold_green") == 0) return THEME_GREEN_BOLD; + else if (g_strcmp0(def, "blue") == 0) return THEME_BLUE; + else if (g_strcmp0(def, "bold_blue") == 0) return THEME_BLUE_BOLD; + else if (g_strcmp0(def, "yellow") == 0) return THEME_YELLOW; + else if (g_strcmp0(def, "bold_yellow") == 0) return THEME_YELLOW_BOLD; + else if (g_strcmp0(def, "cyan") == 0) return THEME_CYAN; + else if (g_strcmp0(def, "bold_cyan") == 0) return THEME_CYAN_BOLD; + else if (g_strcmp0(def, "magenta") == 0) return THEME_MAGENTA; + else if (g_strcmp0(def, "bold_magenta") == 0) return THEME_MAGENTA_BOLD; + else if (g_strcmp0(def, "black") == 0) return THEME_BLACK; + else if (g_strcmp0(def, "bold_black") == 0) return THEME_BLACK_BOLD; + + else return THEME_TEXT; + } +} diff --git a/src/plugins/themes.h b/src/plugins/themes.h new file mode 100644 index 00000000..1ed1bbf5 --- /dev/null +++ b/src/plugins/themes.h @@ -0,0 +1,37 @@ +/* + * themes.h + * + * Copyright (C) 2012 - 2016 James Booth + * + * This file is part of Profanity. + * + * Profanity 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. + * + * Profanity 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. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +void plugin_themes_init(void); +void plugin_themes_close(void); +theme_item_t plugin_themes_get(const char *const group, const char *const key, const char *const def);