Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add helper functions for setting up configuration change tracking
Having helper functions for both reading initial values and installing
change notifiers allows builtin-gconf value tracking setup to be more
compact and readable.
  • Loading branch information
spiiroin committed Oct 24, 2014
1 parent 3397839 commit 0695840
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
65 changes: 65 additions & 0 deletions mce-gconf.c
Expand Up @@ -23,6 +23,9 @@

#include "mce-log.h"

#include <stdlib.h>
#include <string.h>

/** Pointer to the GConf client */
static GConfClient *gconf_client = NULL;
/** Is GConf disabled on purpose */
Expand Down Expand Up @@ -412,6 +415,68 @@ void mce_gconf_notifier_remove_cb(gpointer cb_id, gpointer user_data)
mce_gconf_notifier_remove(GPOINTER_TO_INT(cb_id));
}

/** Helper for getting path of gconf key
*/
static gchar *mce_gconf_get_path(const gchar *key)
{
gchar *res = 0;
const gchar *end = strrchr(key, '/');

if( end )
res = g_strndup(key, end - key);

return res;
}

/** Get initial value of integer setting and start tracking changes
*
* @param key key name
* @param val where to store the initial value
* @param def default value to use if getting key value fails;
* or -1 to leave *val unmodified
* @param cb change notification callback
* @param cb_id where to store notification callback id
*/
void mce_gconf_track_int(const gchar *key, gint *val, gint def,
GConfClientNotifyFunc cb, guint *cb_id)
{
gchar *path = mce_gconf_get_path(key);

if( path && cb && cb_id )
mce_gconf_notifier_add(path, key, cb, cb_id);

if( !mce_gconf_get_int(key, val) && def != -1 )
*val = def;

g_free(path);
}

/** Get initial value of integer setting and start tracking changes
*
* Note: Caller must release returned string with g_free() when it
* is no longer needed.
*
* @param key key name
* @param val where to store the initial value
* @param def default value to use if getting key value fails;
* or NULL to leave *val unmodified
* @param cb change notification callback
* @param cb_id where to store notification callback id
*/
void mce_gconf_track_string(const gchar *key, gchar **val, const gchar *def,
GConfClientNotifyFunc cb, guint *cb_id)
{
gchar *path = mce_gconf_get_path(key);

if( path && cb && cb_id )
mce_gconf_notifier_add(path, key, cb, cb_id);

if( !mce_gconf_get_string(key, val) && def != 0 )
*val = g_strdup(def);

g_free(path);
}

/**
* Init function for the mce-gconf component
*
Expand Down
5 changes: 5 additions & 0 deletions mce-gconf.h
Expand Up @@ -36,6 +36,11 @@ gboolean mce_gconf_notifier_add(const gchar *path, const gchar *key,
void mce_gconf_notifier_remove(guint id);
void mce_gconf_notifier_remove_cb(gpointer cb_id, gpointer user_data);

void mce_gconf_track_int(const gchar *key, gint *val, gint def,
GConfClientNotifyFunc cb, guint *cb_id);
void mce_gconf_track_string(const gchar *key, gchar **val, const gchar *def,
GConfClientNotifyFunc cb, guint *cb_id);

gboolean mce_gconf_init(void);
void mce_gconf_exit(void);

Expand Down

0 comments on commit 0695840

Please sign in to comment.