[gnome-menus] Replace monitor API with a simple "changed" signal



commit 474b86aac925d7b45d3bbbaba9111719ca0d9b8a
Author: Colin Walters <walters verbum org>
Date:   Sun Apr 17 08:40:47 2011 -0400

    Replace monitor API with a simple "changed" signal
    
    So much simpler...
    
    https://bugzilla.gnome.org/show_bug.cgi?id=647968

 libmenu/gmenu-tree.c  |   96 +++++++++----------------------------------------
 libmenu/gmenu-tree.h  |   10 -----
 util/test-menu-spec.c |    9 +----
 3 files changed, 18 insertions(+), 97 deletions(-)
---
diff --git a/libmenu/gmenu-tree.c b/libmenu/gmenu-tree.c
index f8e01ea..2303a37 100644
--- a/libmenu/gmenu-tree.c
+++ b/libmenu/gmenu-tree.c
@@ -39,6 +39,7 @@ enum {
 /* Signals */
 enum
 {
+  CHANGED,
   LAST_SIGNAL
 };
 
@@ -63,12 +64,6 @@ struct _GMenuTree
 
 G_DEFINE_TYPE (GMenuTree, gmenu_tree, G_TYPE_OBJECT)
 
-typedef struct
-{
-  GMenuTreeChangedFunc callback;
-  gpointer             user_data;
-} GMenuTreeMonitor;
-
 struct GMenuTreeItem
 {
   GMenuTreeItemType type;
@@ -537,6 +532,21 @@ gmenu_tree_class_init (GMenuTreeClass *klass)
 						       GMENU_TREE_FLAGS_NONE,
 						       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
+  /**
+   * GMenuTree:changed
+   *
+   * This signal is emitted when applications are added, removed, or
+   * upgraded.  But note the new data will only be visible after
+   * gmenu_tree_load_sync() or a variant thereof is invoked.
+   */
+  gmenu_tree_signals[CHANGED] =
+      g_signal_new ("changed",
+                    G_TYPE_FROM_CLASS (klass),
+                    G_SIGNAL_RUN_LAST,
+                    0,
+                    NULL, NULL,
+                    g_cclosure_marshal_VOID__VOID,
+                    G_TYPE_NONE, 0);
 }
 
 
@@ -660,82 +670,10 @@ gmenu_tree_get_directory_from_path (GMenuTree  *tree,
   return directory ? gmenu_tree_item_ref (directory) : NULL;
 }
 
-void
-gmenu_tree_add_monitor (GMenuTree            *tree,
-                       GMenuTreeChangedFunc   callback,
-                       gpointer               user_data)
-{
-  GMenuTreeMonitor *monitor;
-  GSList           *tmp;
-
-  g_return_if_fail (tree != NULL);
-  g_return_if_fail (callback != NULL);
-
-  tmp = tree->monitors;
-  while (tmp != NULL)
-    {
-      monitor = tmp->data;
-
-      if (monitor->callback  == callback &&
-          monitor->user_data == user_data)
-        break;
-
-      tmp = tmp->next;
-    }
-
-  if (tmp == NULL)
-    {
-      monitor = g_new0 (GMenuTreeMonitor, 1);
-
-      monitor->callback  = callback;
-      monitor->user_data = user_data;
-
-      tree->monitors = g_slist_append (tree->monitors, monitor);
-    }
-}
-
-void
-gmenu_tree_remove_monitor (GMenuTree            *tree,
-			   GMenuTreeChangedFunc  callback,
-			   gpointer              user_data)
-{
-  GSList *tmp;
-
-  g_return_if_fail (tree != NULL);
-  g_return_if_fail (callback != NULL);
-
-  tmp = tree->monitors;
-  while (tmp != NULL)
-    {
-      GMenuTreeMonitor *monitor = tmp->data;
-      GSList          *next = tmp->next;
-
-      if (monitor->callback  == callback &&
-          monitor->user_data == user_data)
-        {
-          tree->monitors = g_slist_delete_link (tree->monitors, tmp);
-          g_free (monitor);
-        }
-
-      tmp = next;
-    }
-}
-
 static void
 gmenu_tree_invoke_monitors (GMenuTree *tree)
 {
-  GSList *tmp;
-
-  tmp = tree->monitors;
-  while (tmp != NULL)
-    {
-      GMenuTreeMonitor *monitor = tmp->data;
-      GSList           *next    = tmp->next;
-
-      monitor->callback (tree, monitor->user_data);
-
-      tmp = next;
-    }
+  g_signal_emit (tree, gmenu_tree_signals[CHANGED], 0);
 }
 
 GMenuTreeItemType
diff --git a/libmenu/gmenu-tree.h b/libmenu/gmenu-tree.h
index 825b025..02fcad4 100644
--- a/libmenu/gmenu-tree.h
+++ b/libmenu/gmenu-tree.h
@@ -52,9 +52,6 @@ typedef struct GMenuTreeSeparator GMenuTreeSeparator;
 typedef struct GMenuTreeHeader    GMenuTreeHeader;
 typedef struct GMenuTreeAlias     GMenuTreeAlias;
 
-typedef void (*GMenuTreeChangedFunc) (GMenuTree *tree,
-				      gpointer  user_data);
-
 typedef enum
 {
   GMENU_TREE_ITEM_INVALID = 0,
@@ -131,13 +128,6 @@ GMenuTreeDirectory *gmenu_tree_header_get_directory (GMenuTreeHeader *header);
 GMenuTreeDirectory *gmenu_tree_alias_get_directory (GMenuTreeAlias *alias);
 GMenuTreeItem      *gmenu_tree_alias_get_item      (GMenuTreeAlias *alias);
 
-void gmenu_tree_add_monitor    (GMenuTree            *tree,
-				GMenuTreeChangedFunc  callback,
-				gpointer             user_data);
-void gmenu_tree_remove_monitor (GMenuTree            *tree,
-				GMenuTreeChangedFunc  callback,
-				gpointer             user_data);
-
 G_END_DECLS
 
 #endif /* __GMENU_TREE_H__ */
diff --git a/util/test-menu-spec.c b/util/test-menu-spec.c
index 2be39c7..fdf14ac 100644
--- a/util/test-menu-spec.c
+++ b/util/test-menu-spec.c
@@ -226,18 +226,11 @@ main (int argc, char **argv)
     {
       GMainLoop *main_loop;
 
-      gmenu_tree_add_monitor (tree,
-			      (GMenuTreeChangedFunc) handle_tree_changed,
-			      NULL);
+      g_signal_connect (tree, "changed", G_CALLBACK (handle_tree_changed), NULL);
 
       main_loop = g_main_loop_new (NULL, FALSE);
       g_main_loop_run (main_loop);
       g_main_loop_unref (main_loop);
-
-      gmenu_tree_remove_monitor (tree,
-				 (GMenuTreeChangedFunc) handle_tree_changed,
-				 NULL);
-
     }
 
   g_object_unref (tree);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]