From a4e6d50a4016fa025f72bf42d3da2d4fe3f4b2dc Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 25 Mar 2016 22:06:24 +0000 Subject: Added boolean settings for plugins --- Makefile.am | 4 +- configure-debug | 2 +- src/plugins/api.c | 12 +++++ src/plugins/api.h | 3 ++ src/plugins/c_api.c | 14 ++++++ src/plugins/plugins.c | 3 ++ src/plugins/profapi.c | 3 ++ src/plugins/profapi.h | 2 + src/plugins/python_api.c | 56 +++++++++++++++++++++-- src/plugins/settings.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++ src/plugins/settings.h | 43 ++++++++++++++++++ src/plugins/themes.h | 5 ++ 12 files changed, 256 insertions(+), 7 deletions(-) create mode 100644 src/plugins/settings.c create mode 100644 src/plugins/settings.h diff --git a/Makefile.am b/Makefile.am index 30694534..57deb1a0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -46,7 +46,8 @@ core_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/plugins/themes.c src/plugins/themes.h \ + src/plugins/settings.c src/plugins/settings.h unittest_sources = \ src/contact.c src/contact.h src/common.c \ @@ -79,6 +80,7 @@ unittest_sources = \ 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/plugins/settings.c src/plugins/settings.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/configure-debug b/configure-debug index c61a0f50..48b5fb5c 100755 --- a/configure-debug +++ b/configure-debug @@ -1,3 +1,3 @@ #!/bin/sh -./configure CFLAGS='-g3 -O0' CXXFLAGS='-g3 -O0' $@ +./configure CFLAGS='-g3 -O0' CXXFLAGS='-g3 -O0' --enable-python-plugins $@ diff --git a/src/plugins/api.c b/src/plugins/api.c index 81b464ca..dec8bcb8 100644 --- a/src/plugins/api.c +++ b/src/plugins/api.c @@ -42,6 +42,7 @@ #include "plugins/callbacks.h" #include "plugins/autocompleters.h" #include "plugins/themes.h" +#include "plugins/settings.h" #include "profanity.h" #include "ui/ui.h" #include "config/theme.h" @@ -332,3 +333,14 @@ api_send_stanza(const char *const stanza) return jabber_send_stanza(stanza); } +gboolean +api_settings_get_boolean(const char *const group, const char *const key, gboolean def) +{ + return plugin_settings_get_boolean(group, key, def); +} + +void +api_settings_set_boolean(const char *const group, const char *const key, gboolean value) +{ + plugin_settings_set_boolean(group, key, value); +} diff --git a/src/plugins/api.h b/src/plugins/api.h index 2ded857c..8212eb65 100644 --- a/src/plugins/api.h +++ b/src/plugins/api.h @@ -72,4 +72,7 @@ int api_win_show_themed(const char *tag, const char *const group, const char *co int api_send_stanza(const char *const stanza); +gboolean api_settings_get_boolean(const char *const group, const char *const key, gboolean def); +void api_settings_set_boolean(const char *const group, const char *const key, gboolean value); + #endif diff --git a/src/plugins/c_api.c b/src/plugins/c_api.c index 82b89bfd..309a3492 100644 --- a/src/plugins/c_api.c +++ b/src/plugins/c_api.c @@ -194,6 +194,18 @@ c_api_send_stanza(char *stanza) return api_send_stanza(stanza); } +static int +c_api_settings_get_boolean(char *group, char *key, int def) +{ + return api_settings_get_boolean(group, key, def); +} + +static void +c_api_settings_set_boolean(char *group, char *key, int value) +{ + api_settings_set_boolean(group, key, value); +} + void c_command_callback(PluginCommand *command, gchar **args) { @@ -243,4 +255,6 @@ c_api_init(void) prof_win_show = c_api_win_show; prof_win_show_themed = c_api_win_show_themed; prof_send_stanza = c_api_send_stanza; + prof_settings_get_boolean = c_api_settings_get_boolean; + prof_settings_set_boolean = c_api_settings_set_boolean; } diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 976a6698..75fc01d6 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -44,6 +44,7 @@ #include "plugins/api.h" #include "plugins/plugins.h" #include "plugins/themes.h" +#include "plugins/settings.h" #ifdef PROF_HAVE_PYTHON #include "plugins/python_plugins.h" @@ -74,6 +75,7 @@ plugins_init(void) #endif plugin_themes_init(); + plugin_settings_init(); // load plugins gchar **plugins_load = prefs_get_plugins(); @@ -435,6 +437,7 @@ plugins_shutdown(void) autocompleters_destroy(); plugin_themes_close(); + plugin_settings_close(); callbacks_close(); } diff --git a/src/plugins/profapi.c b/src/plugins/profapi.c index 33bc1694..e8b64b60 100644 --- a/src/plugins/profapi.c +++ b/src/plugins/profapi.c @@ -70,3 +70,6 @@ int (*prof_win_show)(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; int (*prof_send_stanza)(char *stanza) = NULL; + +int (*prof_settings_get_boolean)(char *group, char *key, int def) = NULL; +void (*prof_settings_set_boolean)(char *group, char *key, int value) = NULL; diff --git a/src/plugins/profapi.h b/src/plugins/profapi.h index 5e212110..30197279 100644 --- a/src/plugins/profapi.h +++ b/src/plugins/profapi.h @@ -71,5 +71,7 @@ int (*prof_win_show_themed)(PROF_WIN_TAG tag, char *group, char *key, char *def, int (*prof_send_stanza)(char *stanza); +int (*prof_settings_get_boolean)(char *group, char *key, int def); +void (*prof_settings_set_boolean)(char *group, char *key, int value); #endif diff --git a/src/plugins/python_api.c b/src/plugins/python_api.c index 7d0ebc89..bc45c10c 100644 --- a/src/plugins/python_api.c +++ b/src/plugins/python_api.c @@ -355,9 +355,9 @@ python_api_win_exists(PyObject *self, PyObject *args) disable_python_threads(); if (exists) { - return Py_BuildValue("i", 1); + return Py_BuildValue("O", Py_True); } else { - return Py_BuildValue("i", 0); + return Py_BuildValue("O", Py_False); } } @@ -438,17 +438,61 @@ python_api_send_stanza(PyObject *self, PyObject *args) { const char *stanza = NULL; if (!PyArg_ParseTuple(args, "s", &stanza)) { - return Py_BuildValue("i", 0); + return Py_BuildValue("O", Py_False); } allow_python_threads(); int res = api_send_stanza(stanza); disable_python_threads(); if (res) { - return Py_BuildValue("i", 1); + return Py_BuildValue("O", Py_True); } else { - return Py_BuildValue("i", 0); + return Py_BuildValue("O", Py_False); + } +} + +static PyObject* +python_api_settings_get_boolean(PyObject *self, PyObject *args) +{ + char *group = NULL; + char *key = NULL; + PyObject *defobj = NULL; + + if (!PyArg_ParseTuple(args, "ssO!", &group, &key, &PyBool_Type, &defobj)) { + return Py_BuildValue(""); } + + int def = PyObject_IsTrue(defobj); + + allow_python_threads(); + int res = api_settings_get_boolean(group, key, def); + disable_python_threads(); + + if (res) { + return Py_BuildValue("O", Py_True); + } else { + return Py_BuildValue("O", Py_False); + } +} + +static PyObject* +python_api_settings_set_boolean(PyObject *self, PyObject *args) +{ + char *group = NULL; + char *key = NULL; + PyObject *valobj = NULL; + + if (!PyArg_ParseTuple(args, "ssO!", &group, &key, &PyBool_Type, &valobj)) { + return Py_BuildValue(""); + } + + int val = PyObject_IsTrue(valobj); + + allow_python_threads(); + api_settings_set_boolean(group, key, val); + disable_python_threads(); + + return Py_BuildValue(""); } void @@ -541,6 +585,8 @@ static PyMethodDef apiMethods[] = { { "win_show", python_api_win_show, METH_VARARGS, "Show text in the window." }, { "win_show_themed", python_api_win_show_themed, METH_VARARGS, "Show themed text in the window." }, { "send_stanza", python_api_send_stanza, METH_VARARGS, "Send an XMPP stanza." }, + { "settings_get_boolean", python_api_settings_get_boolean, METH_VARARGS, "Get a boolean setting" }, + { "settings_set_boolean", python_api_settings_set_boolean, METH_VARARGS, "Set a boolean setting" }, { NULL, NULL, 0, NULL } }; diff --git a/src/plugins/settings.c b/src/plugins/settings.c new file mode 100644 index 00000000..7d915e9b --- /dev/null +++ b/src/plugins/settings.c @@ -0,0 +1,116 @@ +/* + * settings.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 +#include + +#include "config/theme.h" +#include "common.h" + +static GKeyFile *settings; + +static void _save_settings(void); + +void +plugin_settings_init(void) +{ + gchar *xdg_data = xdg_get_data_home(); + GString *fileloc = g_string_new(xdg_data); + g_string_append(fileloc, "/profanity/plugin_settings"); + g_free(xdg_data); + + if (g_file_test(fileloc->str, G_FILE_TEST_EXISTS)) { + g_chmod(fileloc->str, S_IRUSR | S_IWUSR); + } + + settings = g_key_file_new(); + g_key_file_load_from_file(settings, fileloc->str, G_KEY_FILE_KEEP_COMMENTS, NULL); + + gsize g_data_size; + gchar *g_data = g_key_file_to_data(settings, &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_settings_close(void) +{ + g_key_file_free(settings); + settings = NULL; +} + +gboolean +plugin_settings_get_boolean(const char *const group, const char *const key, gboolean def) +{ + if (group && key && g_key_file_has_key(settings, group, key, NULL)) { + return g_key_file_get_boolean(settings, group, key, NULL); + } else { + return def; + } +} + +void +plugin_settings_set_boolean(const char *const group, const char *const key, gboolean value) +{ + g_key_file_set_boolean(settings, group, key, value); + _save_settings(); +} + +static void +_save_settings(void) +{ + gsize g_data_size; + gchar *g_data = g_key_file_to_data(settings, &g_data_size, NULL); + + gchar *xdg_data = xdg_get_data_home(); + GString *fileloc = g_string_new(xdg_data); + g_free(xdg_data); + + g_string_append(fileloc, "/profanity/"); + char *base = strdup(fileloc->str); + g_string_append(fileloc, "plugin_settings"); + + gchar *true_loc = get_file_or_linked(fileloc->str, base); + free(base); + g_file_set_contents(true_loc, g_data, g_data_size, NULL); + free(true_loc); + g_free(g_data); + g_chmod(fileloc->str, S_IRUSR | S_IWUSR); + g_string_free(fileloc, TRUE); +} diff --git a/src/plugins/settings.h b/src/plugins/settings.h new file mode 100644 index 00000000..65486ea3 --- /dev/null +++ b/src/plugins/settings.h @@ -0,0 +1,43 @@ +/* + * settings.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. + * + */ + +#ifndef PLUGIN_SETTINGS_H +#define PLUGIN_SETTINGS_H + +void plugin_settings_init(void); +void plugin_settings_close(void); +gboolean plugin_settings_get_boolean(const char *const group, const char *const key, gboolean def); +void plugin_settings_set_boolean(const char *const group, const char *const key, gboolean value); + +#endif diff --git a/src/plugins/themes.h b/src/plugins/themes.h index 1ed1bbf5..6dfcfe41 100644 --- a/src/plugins/themes.h +++ b/src/plugins/themes.h @@ -32,6 +32,11 @@ * */ +#ifndef PLUGIN_THEMES_H +#define PLUGIN_THEMES_H + 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); + +#endif -- cgit 1.4.1-2-gfad0 #n98'>98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184