1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Added theme support to plugins

This commit is contained in:
James Booth 2016-02-21 23:58:13 +00:00
parent 1654f16a2f
commit ac91e7ef85
9 changed files with 234 additions and 119 deletions

View File

@ -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 \

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -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 *

View File

@ -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;

View File

@ -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

142
src/plugins/themes.c Normal file
View File

@ -0,0 +1,142 @@
/*
* themes.c
*
* Copyright (C) 2012 - 2016 James Booth <boothj5@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* 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 <glib.h>
#include <glib/gstdio.h>
#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;
}
}

37
src/plugins/themes.h Normal file
View File

@ -0,0 +1,37 @@
/*
* themes.h
*
* Copyright (C) 2012 - 2016 James Booth <boothj5@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* 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);