r23218 - gnucash/trunk/src/app-utils - GSettings: add functions to listen for changes

Geert Janssens gjanssens at code.gnucash.org
Mon Oct 7 10:04:51 EDT 2013


Author: gjanssens
Date: 2013-10-07 10:04:50 -0400 (Mon, 07 Oct 2013)
New Revision: 23218
Trac: http://svn.gnucash.org/trac/changeset/23218

Modified:
   gnucash/trunk/src/app-utils/gnc-gsettings.c
   gnucash/trunk/src/app-utils/gnc-gsettings.h
Log:
GSettings: add functions to listen for changes

Modified: gnucash/trunk/src/app-utils/gnc-gsettings.c
===================================================================
--- gnucash/trunk/src/app-utils/gnc-gsettings.c	2013-10-07 14:04:19 UTC (rev 23217)
+++ gnucash/trunk/src/app-utils/gnc-gsettings.c	2013-10-07 14:04:50 UTC (rev 23218)
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <string.h>
 #include "gnc-gsettings.h"
+#include "libqof/qof/qof.h"
 
 #define CLIENT_TAG  "%s-%s-client"
 #define NOTIFY_TAG  "%s-%s-notify_id"
@@ -34,6 +35,9 @@
 static GHashTable *schema_hash = NULL;
 static const gchar *gsettings_prefix;
 
+/* This static indicates the debugging module that this .o belongs to.  */
+static QofLogModule log_module = G_LOG_DOMAIN;
+
 /************************************************************/
 /*               Internal helper functions                  */
 /************************************************************/
@@ -89,7 +93,6 @@
 }
 
 
-
 /************************************************************/
 /*                      GSettings Utilities                 */
 /************************************************************/
@@ -122,3 +125,98 @@
 
     return g_strjoin(".", gnc_gsettings_get_prefix(), name, NULL);
 }
+
+
+/************************************************************/
+/*                   Change notification                    */
+/************************************************************/
+
+gulong
+gnc_gsettings_register_cb (const gchar *schema,
+                           const gchar *key,
+                           GCallback func,
+                           gpointer user_data)
+{
+    gulong retval = 0;
+    gchar *signal = NULL;
+
+    GSettings *schema_ptr = gnc_gsettings_get_schema_ptr (schema);
+    g_return_val_if_fail (G_IS_SETTINGS (schema_ptr), retval);
+
+    if ((!key) || (*key == '\0'))
+        signal = g_strdup ("changed");
+    else
+    {
+        if (gnc_gsettings_is_valid_key(schema_ptr, key))
+            signal = g_strconcat ("changed::", key, NULL);
+    }
+
+    retval = g_signal_connect (schema_ptr, signal, func, user_data);
+
+    g_free (signal);
+
+    return retval;
+}
+
+
+void
+gnc_gsettings_remove_cb_by_func (const gchar *schema,
+                                 const gchar *key,
+                                 GCallback func,
+                                 gpointer user_data)
+{
+    gint matched = 0;
+    gchar *signal = NULL;
+
+    GSettings *schema_ptr = gnc_gsettings_get_schema_ptr (schema);
+    g_return_if_fail (G_IS_SETTINGS (schema_ptr));
+
+    if ((!key) || (*key == '\0'))
+        signal = g_strdup ("changed");
+    else
+    {
+        if (gnc_gsettings_is_valid_key(schema_ptr, key))
+            signal = g_strconcat ("changed::", key, NULL);
+    }
+
+    matched = g_signal_handlers_disconnect_matched (
+            schema_ptr,
+            G_SIGNAL_MATCH_DETAIL ||G_SIGNAL_MATCH_FUNC || G_SIGNAL_MATCH_DATA,
+            0, /* signal_id */
+            g_quark_from_string (signal),   /* signal_detail */
+            NULL, /* closure */
+            func, /* callback function */
+            user_data);
+    DEBUG ("Removed %d handlers for signal '%s' from schema '%s'", matched, signal, schema);
+
+    g_free (signal);
+}
+
+
+void
+gnc_gsettings_remove_cb_by_id (const gchar *schema,
+                               guint handlerid)
+{
+    GSettings *schema_ptr = gnc_gsettings_get_schema_ptr (schema);
+    g_return_if_fail (G_IS_SETTINGS (schema_ptr));
+
+    g_signal_handler_disconnect (schema_ptr, handlerid);
+}
+
+
+guint
+gnc_gsettings_register_any_cb (const gchar *schema,
+                               GCallback func,
+                               gpointer user_data)
+{
+    return gnc_gsettings_register_cb (schema, NULL, func, user_data);
+}
+
+
+void
+gnc_gsettings_remove_any_cb_by_func (const gchar *schema,
+                                     GCallback func,
+                                     gpointer user_data)
+{
+    gnc_gsettings_remove_cb_by_func (schema, NULL, func, user_data);
+}

Modified: gnucash/trunk/src/app-utils/gnc-gsettings.h
===================================================================
--- gnucash/trunk/src/app-utils/gnc-gsettings.h	2013-10-07 14:04:19 UTC (rev 23217)
+++ gnucash/trunk/src/app-utils/gnc-gsettings.h	2013-10-07 14:04:50 UTC (rev 23218)
@@ -108,6 +108,121 @@
  */
 const gchar *gnc_gsettings_get_prefix (void);
 
+
+/** @name Listening for changes
+ @{
+*/
+
+
+/** Register a callback for when a specific key in the settings
+ *  schema is changed.  Any time the key's value changes, the routine
+ *  will be invoked and will be passed both the changed gsettings entry
+ *  and the user data passed to this function.
+ *
+ *  @param schema This value contains the schema name of the key
+ *  to watch.
+ *
+ *  @param key This value contains the name of the key to watch.
+ *
+ *  @param func This is a pointer to the function to call when the key
+ *  changes.
+ *
+ *  @param user_data This pointer will be passed to the callback
+ *  function.
+ *
+ *  @return This function returns the handler id for the registered
+ *  callback.
+ */
+gulong gnc_gsettings_register_cb (const char *schema,
+                                  const gchar *key,
+                                  GCallback func,
+                                  gpointer user_data);
+
+
+/** Remove a function that was registered for a callback when a
+ *  specific key in the settings schema changed.  Both the func and
+ *  user_data arguments are used to match up the callback to remove.
+ *  If no matching func and user_data are found to be registered
+ *  for the given key, nothing will happen.
+ *
+ *  @param schema This value contains the schema name of the key
+ *  that is being watched.
+ *
+ *  @param key This value contains the name of the key being watched.
+ *
+ *  @param func This is a pointer to the function that was registered
+ *  earlier.
+ *
+ *  @param user_data This pointer was passed to the callback
+ *  function when it was registered.
+ */
+void gnc_gsettings_remove_cb_by_func (const gchar *schema,
+                                      const gchar *key,
+                                      GCallback func,
+                                      gpointer user_data);
+
+
+/** Remove a function that was registered for a callback when a
+ *  specific key in the settings schema changed.  The handler id
+ *  that was generated when the callback was registered is
+ *  use to find the callback to remove.
+ *  If no handler id is found nothing will happen.
+ *
+ *  @param schema This value contains the schema name of the key
+ *  that is being watched.
+ *
+ *  @param id The handler id of the callback to be removed.
+ */
+void gnc_gsettings_remove_cb_by_id (const gchar *schema,
+                                    guint id);
+
+
+/** Register a callback for when any key in the settings schema
+ *  is changed.  Any time the value of a key in this schema changes,
+ *  the routine will be invoked and will be passed the specified
+ *  user data.
+ *
+ *  @param schema This value contains the name of the schema
+ *  that is being watched.
+ *
+ *  @param func This is a pointer to the function to call when a key
+ *  changes.
+ *
+ *  @param user_data This pointer will be passed to the callback
+ *  function.
+ */
+guint gnc_gsettings_register_any_cb (const gchar *schema,
+                                     GCallback func,
+                                     gpointer user_data);
+
+
+/** Remove a function that was registered for a callback when any key
+ *  in the given settings schema changed.  Both the func and user_data
+ *  arguments are used to match up the callback to remove.
+ *  If no matching func and user_data are found to be registered
+ *  for the given key, nothing will happen.
+ *
+ *  @param schema This value contains the name of the schema
+ *  that is being watched.
+ *
+ *  @param func This is a pointer to the function that was registered
+ *  earlier.
+ *
+ *  @param user_data This pointer was passed to the callback
+ *  function when it was registered.
+ *
+ *  @note there is no gnc_settings_remove_any_cb_by_id. Use
+ *  gnc_settings_remove_cb_by_id instead if you want to
+ *  remove a callback set with gnc_settings_register_any_cb
+ *  by its handler id.
+ */
+void gnc_gsettings_remove_any_cb_by_func (const gchar *schema,
+                                          GCallback func,
+                                          gpointer user_data);
+
+/** @} */
+
+
 #endif /* GNC_GSETTINGS_H */
 /** @} */
 /** @} */



More information about the gnucash-changes mailing list