about summary refs log tree commit diff stats
path: root/src/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/api.c12
-rw-r--r--src/plugins/api.h3
-rw-r--r--src/plugins/c_api.c14
-rw-r--r--src/plugins/plugins.c3
-rw-r--r--src/plugins/profapi.c3
-rw-r--r--src/plugins/profapi.h2
-rw-r--r--src/plugins/python_api.c56
-rw-r--r--src/plugins/settings.c116
-rw-r--r--src/plugins/settings.h43
-rw-r--r--src/plugins/themes.h5
10 files changed, 252 insertions, 5 deletions
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 <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);
+}
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 <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
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