mirror of
https://github.com/profanity-im/profanity.git
synced 2024-11-03 19:37:16 -05:00
Added boolean settings for plugins
This commit is contained in:
parent
57ccfcee4d
commit
a4e6d50a40
@ -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 \
|
||||
|
@ -1,3 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
./configure CFLAGS='-g3 -O0' CXXFLAGS='-g3 -O0' $@
|
||||
./configure CFLAGS='-g3 -O0' CXXFLAGS='-g3 -O0' --enable-python-plugins $@
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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,19 +438,63 @@ 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
|
||||
python_command_callback(PluginCommand *command, gchar **args)
|
||||
{
|
||||
@ -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 }
|
||||
};
|
||||
|
||||
|
116
src/plugins/settings.c
Normal file
116
src/plugins/settings.c
Normal file
@ -0,0 +1,116 @@
|
||||
/*
|
||||
* settings.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 <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#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);
|
||||
}
|
43
src/plugins/settings.h
Normal file
43
src/plugins/settings.h
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* settings.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.
|
||||
*
|
||||
*/
|
||||
|
||||
#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
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user