pidgin/pidgin

Add PurplePrefsUiOps API from instantbird
release-2.x.y
2016-06-15, dx
2e00aa78cb1f
Parents e61fe608aa81
Children c3548438b3a8
Add PurplePrefsUiOps API from instantbird

This allows overriding the prefs.xml storage with anything else.

The instantbird version had fewer methods, things like add_string_list or
rename_boolean_toggle which are rarely used in libpurple and that instantbird
just removed from their fork.

The add_observer and remove_observer methods are not implemented yet.
--- a/libpurple/prefs.c Wed Jun 15 21:59:51 2016 -0500
+++ b/libpurple/prefs.c Wed Jun 15 04:59:21 2016 -0300
@@ -40,6 +40,8 @@
#include "win32dep.h"
#endif
+static PurplePrefsUiOps *prefs_ui_ops = NULL;
+
struct pref_cb {
PurplePrefCallback func;
gpointer data;
@@ -79,6 +81,23 @@
static guint save_timer = 0;
static gboolean prefs_loaded = FALSE;
+#define PURPLE_PREFS_UI_OP_CALL(member, ...) \
+ { \
+ PurplePrefsUiOps *uiop = purple_prefs_get_ui_ops(); \
+ if (uiop && uiop->member) { \
+ uiop->member(__VA_ARGS__); \
+ return; \
+ } \
+ }
+
+#define PURPLE_PREFS_UI_OP_CALL_RETURN(member, ...) \
+ { \
+ PurplePrefsUiOps *uiop = purple_prefs_get_ui_ops(); \
+ if (uiop && uiop->member) { \
+ return uiop->member(__VA_ARGS__); \
+ } \
+ }
+
/*********************************************************************
* Private utility functions *
@@ -207,6 +226,8 @@
return;
}
+ PURPLE_PREFS_UI_OP_CALL(save);
+
node = prefs_to_xmlnode();
data = xmlnode_to_formatted_str(node, NULL);
purple_util_write_data_to_file("prefs.xml", data, -1);
@@ -225,6 +246,8 @@
static void
schedule_prefs_save(void)
{
+ PURPLE_PREFS_UI_OP_CALL(schedule_save);
+
if (save_timer == 0)
save_timer = purple_timeout_add_seconds(5, save_cb, NULL);
}
@@ -376,12 +399,21 @@
gboolean
purple_prefs_load()
{
- gchar *filename = g_build_filename(purple_user_dir(), "prefs.xml", NULL);
+ gchar *filename;
gchar *contents = NULL;
gsize length;
GMarkupParseContext *context;
GError *error = NULL;
+ PurplePrefsUiOps *uiop = purple_prefs_get_ui_ops();
+
+ if (uiop && uiop->load) {
+ prefs_loaded = TRUE;
+ return uiop->load();
+ }
+
+ filename = g_build_filename(purple_user_dir(), "prefs.xml", NULL);
+
if (!filename) {
prefs_loaded = TRUE;
return FALSE;
@@ -605,13 +637,19 @@
void
purple_prefs_add_none(const char *name)
{
+ PURPLE_PREFS_UI_OP_CALL(add_none, name);
+
add_pref(PURPLE_PREF_NONE, name);
}
void
purple_prefs_add_bool(const char *name, gboolean value)
{
- struct purple_pref *pref = add_pref(PURPLE_PREF_BOOLEAN, name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL(add_bool, name, value);
+
+ pref = add_pref(PURPLE_PREF_BOOLEAN, name);
if(!pref)
return;
@@ -622,7 +660,11 @@
void
purple_prefs_add_int(const char *name, int value)
{
- struct purple_pref *pref = add_pref(PURPLE_PREF_INT, name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL(add_int, name, value);
+
+ pref = add_pref(PURPLE_PREF_INT, name);
if(!pref)
return;
@@ -640,6 +682,8 @@
return;
}
+ PURPLE_PREFS_UI_OP_CALL(add_string, name, value);
+
pref = add_pref(PURPLE_PREF_STRING, name);
if(!pref)
@@ -651,9 +695,13 @@
void
purple_prefs_add_string_list(const char *name, GList *value)
{
- struct purple_pref *pref = add_pref(PURPLE_PREF_STRING_LIST, name);
+ struct purple_pref *pref;
GList *tmp;
+ PURPLE_PREFS_UI_OP_CALL(add_string_list, name, value);
+
+ pref = add_pref(PURPLE_PREF_STRING_LIST, name);
+
if(!pref)
return;
@@ -670,7 +718,12 @@
void
purple_prefs_add_path(const char *name, const char *value)
{
- struct purple_pref *pref = add_pref(PURPLE_PREF_PATH, name);
+ struct purple_pref *pref;
+
+ /* re-use the string UI OP */
+ PURPLE_PREFS_UI_OP_CALL(add_string, name, value);
+
+ pref = add_pref(PURPLE_PREF_PATH, name);
if(!pref)
return;
@@ -681,9 +734,14 @@
void
purple_prefs_add_path_list(const char *name, GList *value)
{
- struct purple_pref *pref = add_pref(PURPLE_PREF_PATH_LIST, name);
+ struct purple_pref *pref;
GList *tmp;
+ /* re-use the string list UI OP */
+ PURPLE_PREFS_UI_OP_CALL(add_string_list, name, value);
+
+ pref = add_pref(PURPLE_PREF_PATH_LIST, name);
+
if(!pref)
return;
@@ -740,7 +798,11 @@
void
purple_prefs_remove(const char *name)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL(remove, name);
+
+ pref = find_pref(name);
if(!pref)
return;
@@ -781,6 +843,7 @@
do_callbacks(name, pref);
}
+/* this function is deprecated, so it doesn't get the new UI ops */
void
purple_prefs_set_generic(const char *name, gpointer value)
{
@@ -799,7 +862,11 @@
void
purple_prefs_set_bool(const char *name, gboolean value)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL(set_bool, name, value);
+
+ pref = find_pref(name);
if(pref) {
if(pref->type != PURPLE_PREF_BOOLEAN) {
@@ -820,7 +887,11 @@
void
purple_prefs_set_int(const char *name, int value)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL(set_int, name, value);
+
+ pref = find_pref(name);
if(pref) {
if(pref->type != PURPLE_PREF_INT) {
@@ -841,13 +912,17 @@
void
purple_prefs_set_string(const char *name, const char *value)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
if(value != NULL && !g_utf8_validate(value, -1, NULL)) {
purple_debug_error("prefs", "purple_prefs_set_string: Cannot store invalid UTF8 for string pref %s\n", name);
return;
}
+ PURPLE_PREFS_UI_OP_CALL(set_string, name, value);
+
+ pref = find_pref(name);
+
if(pref) {
if(pref->type != PURPLE_PREF_STRING && pref->type != PURPLE_PREF_PATH) {
purple_debug_error("prefs",
@@ -868,7 +943,12 @@
void
purple_prefs_set_string_list(const char *name, GList *value)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL(set_string_list, name, value);
+
+ pref = find_pref(name);
+
if(pref) {
GList *tmp;
@@ -903,7 +983,11 @@
void
purple_prefs_set_path(const char *name, const char *value)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL(set_string, name, value);
+
+ pref = find_pref(name);
if(pref) {
if(pref->type != PURPLE_PREF_PATH) {
@@ -925,7 +1009,12 @@
void
purple_prefs_set_path_list(const char *name, GList *value)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL(set_string_list, name, value);
+
+ pref = find_pref(name);
+
if(pref) {
GList *tmp;
@@ -956,7 +1045,11 @@
gboolean
purple_prefs_exists(const char *name)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL_RETURN(exists, name);
+
+ pref = find_pref(name);
if (pref != NULL)
return TRUE;
@@ -967,7 +1060,11 @@
PurplePrefType
purple_prefs_get_type(const char *name)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL_RETURN(get_type, name);
+
+ pref = find_pref(name);
if (pref == NULL)
return PURPLE_PREF_NONE;
@@ -978,7 +1075,11 @@
gboolean
purple_prefs_get_bool(const char *name)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL_RETURN(get_bool, name);
+
+ pref = find_pref(name);
if(!pref) {
purple_debug_error("prefs",
@@ -996,7 +1097,11 @@
int
purple_prefs_get_int(const char *name)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL_RETURN(get_int, name);
+
+ pref = find_pref(name);
if(!pref) {
purple_debug_error("prefs",
@@ -1014,7 +1119,11 @@
const char *
purple_prefs_get_string(const char *name)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL_RETURN(get_string, name);
+
+ pref = find_pref(name);
if(!pref) {
purple_debug_error("prefs",
@@ -1032,9 +1141,13 @@
GList *
purple_prefs_get_string_list(const char *name)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
GList *ret = NULL, *tmp;
+ PURPLE_PREFS_UI_OP_CALL_RETURN(get_string_list, name);
+
+ pref = find_pref(name);
+
if(!pref) {
purple_debug_error("prefs",
"purple_prefs_get_string_list: Unknown pref %s\n", name);
@@ -1055,7 +1168,11 @@
const char *
purple_prefs_get_path(const char *name)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
+
+ PURPLE_PREFS_UI_OP_CALL_RETURN(get_string, name);
+
+ pref = find_pref(name);
if(!pref) {
purple_debug_error("prefs",
@@ -1073,9 +1190,13 @@
GList *
purple_prefs_get_path_list(const char *name)
{
- struct purple_pref *pref = find_pref(name);
+ struct purple_pref *pref;
GList *ret = NULL, *tmp;
+ PURPLE_PREFS_UI_OP_CALL_RETURN(get_string_list, name);
+
+ pref = find_pref(name);
+
if(!pref) {
purple_debug_error("prefs",
"purple_prefs_get_path_list: Unknown pref %s\n", name);
@@ -1166,6 +1287,8 @@
{
struct purple_pref *oldpref, *newpref;
+ PURPLE_PREFS_UI_OP_CALL(rename, oldname, newname);
+
oldpref = find_pref(oldname);
/* it's already been renamed, call off the dogs */
@@ -1188,6 +1311,8 @@
{
struct purple_pref *oldpref, *newpref;
+ PURPLE_PREFS_UI_OP_CALL(rename_boolean_toggle, oldname, newname);
+
oldpref = find_pref(oldname);
/* it's already been renamed, call off the cats */
@@ -1323,9 +1448,13 @@
purple_prefs_get_children_names(const char *name)
{
GList * list = NULL;
- struct purple_pref *pref = find_pref(name), *child;
+ struct purple_pref *pref, *child;
char sep[2] = "\0\0";;
+ PURPLE_PREFS_UI_OP_CALL_RETURN(get_children_names, name);
+
+ pref = find_pref(name);
+
if (pref == NULL)
return NULL;
@@ -1451,8 +1580,7 @@
if (save_timer != 0)
{
purple_timeout_remove(save_timer);
- save_timer = 0;
- sync_prefs();
+ save_cb(NULL);
}
purple_prefs_disconnect_by_handle(purple_prefs_get_handle());
@@ -1463,3 +1591,15 @@
prefs_hash = NULL;
}
+
+void
+purple_prefs_set_ui_ops(PurplePrefsUiOps *ops)
+{
+ prefs_ui_ops = ops;
+}
+
+PurplePrefsUiOps *
+purple_prefs_get_ui_ops(void)
+{
+ return prefs_ui_ops;
+}
--- a/libpurple/prefs.h Wed Jun 15 21:59:51 2016 -0500
+++ b/libpurple/prefs.h Wed Jun 15 04:59:21 2016 -0300
@@ -62,11 +62,81 @@
typedef void (*PurplePrefCallback) (const char *name, PurplePrefType type,
gconstpointer val, gpointer data);
+
+/** @copydoc _PurplePrefsUiOps */
+typedef struct _PurplePrefsUiOps PurplePrefsUiOps;
+
+
+/** Prefs UI operations
+ */
+struct _PurplePrefsUiOps
+{
+ void (*add_none)(const char *name);
+ void (*add_bool)(const char *name, gboolean value);
+ void (*add_int)(const char *name, int value);
+ void (*add_string)(const char *name, const char *value);
+ void (*add_string_list)(const char *name, GList *value);
+
+ void (*set_bool)(const char *name, gboolean value);
+ void (*set_int)(const char *name, int value);
+ void (*set_string)(const char *name, const char *value);
+ void (*set_string_list)(const char *name, GList *value);
+
+ gboolean (*get_bool)(const char *name);
+ int (*get_int)(const char *name);
+ const char *(*get_string)(const char *name);
+ GList *(*get_string_list)(const char *name);
+
+ PurplePrefType (*get_type)(const char *name);
+ GList *(*get_children_names)(const char *name);
+
+ gboolean (*exists)(const char *name);
+ void (*remove)(const char *name);
+
+ void (*rename)(const char *oldname, const char *newname);
+ void (*rename_boolean_toggle)(const char *oldname, const char *newname);
+
+ gboolean (*load)(void);
+ void (*save)(void);
+ void (*schedule_save)(void);
+
+ void *(*add_observer)(const char *name, gpointer data);
+ void (*remove_observer)(const char *name, void *observer);
+
+ void (*_purple_reserved1)(void);
+ void (*_purple_reserved2)(void);
+ void (*_purple_reserved3)(void);
+ void (*_purple_reserved4)(void);
+ void (*_purple_reserved5)(void);
+};
+
+
+
#ifdef __cplusplus
extern "C" {
#endif
/**************************************************************************/
+/** @name UI Registration Functions */
+/**************************************************************************/
+/*@{*/
+/**
+ * Sets the UI operations structure to be used for preferences.
+ *
+ * @param ops The UI operations structure.
+ */
+void purple_prefs_set_ui_ops(PurplePrefsUiOps *ops);
+
+/**
+ * Returns the UI operations structure used for preferences.
+ *
+ * @return The UI operations structure in use.
+ */
+PurplePrefsUiOps *purple_prefs_get_ui_ops(void);
+
+/*@}*/
+
+/**************************************************************************/
/** @name Prefs API
Preferences are named according to a directory-like structure.
Example: "/plugins/core/potato/is_from_idaho" (probably a boolean) */