Author: jannis
Date: 2007-09-19 22:28:31 +0000 (Wed, 19 Sep 2007)
New Revision: 26088

Modified:
   libxfce4menu/trunk/ChangeLog
   libxfce4menu/trunk/TODO
   libxfce4menu/trunk/libxfce4menu/xfce-menu-monitor.c
   libxfce4menu/trunk/libxfce4menu/xfce-menu-monitor.h
   libxfce4menu/trunk/libxfce4menu/xfce-menu.c
Log:
        * libxfce4menu/xfce-menu-monitor.{c,h}: Added methods to 
          monitor files and directories. That way we can now add 
          monitors for the main menu file as well as menu .directory 
          files and the application directories - I think that's
          all we need. Since there may be several XfceMenu's monotoring
          the same application directories and menu files, those
          are managed using a reference counting mechanism, so that
          the monitor is only removed when all menus have asked
          to remove the monitor on them.
        * libxfce4menu/xfce-menu.c: Monitor the main menu file as
          well as .directory files and application directories. And
          of course remove the monitors when the menu is destroyed.

Modified: libxfce4menu/trunk/ChangeLog
===================================================================
--- libxfce4menu/trunk/ChangeLog        2007-09-18 12:54:21 UTC (rev 26087)
+++ libxfce4menu/trunk/ChangeLog        2007-09-19 22:28:31 UTC (rev 26088)
@@ -1,3 +1,18 @@
+2007-09-20     Jannis Pohlmann <[EMAIL PROTECTED]>
+
+       * libxfce4menu/xfce-menu-monitor.{c,h}: Added methods to 
+         monitor files and directories. That way we can now add 
+         monitors for the main menu file as well as menu .directory 
+         files and the application directories - I think that's
+         all we need. Since there may be several XfceMenu's monotoring
+         the same application directories and menu files, those
+         are managed using a reference counting mechanism, so that
+         the monitor is only removed when all menus have asked
+         to remove the monitor on them.
+       * libxfce4menu/xfce-menu.c: Monitor the main menu file as
+         well as .directory files and application directories. And
+         of course remove the monitors when the menu is destroyed.
+
 2007-05-22     Benedikt Meurer <[EMAIL PROTECTED]>
 
        * libxfce4menu/Makefile.am: Fix typo.

Modified: libxfce4menu/trunk/TODO
===================================================================
--- libxfce4menu/trunk/TODO     2007-09-18 12:54:21 UTC (rev 26087)
+++ libxfce4menu/trunk/TODO     2007-09-19 22:28:31 UTC (rev 26088)
@@ -1,7 +1,6 @@
 TODO
 ========================================================================
 
-  * Remove FrapMenu from libfrap during the next 24-48 hours
   * Add libxfce4menu.spec.in for building RPMs
   * Re-add test scripts
   * Add #ifdef's for GTK+ 2.6 and GLib 2.6

Modified: libxfce4menu/trunk/libxfce4menu/xfce-menu-monitor.c
===================================================================
--- libxfce4menu/trunk/libxfce4menu/xfce-menu-monitor.c 2007-09-18 12:54:21 UTC 
(rev 26087)
+++ libxfce4menu/trunk/libxfce4menu/xfce-menu-monitor.c 2007-09-19 22:28:31 UTC 
(rev 26088)
@@ -43,16 +43,28 @@
 /* User data as provided by the client */
 static gpointer xfce_menu_monitor_user_data = NULL;
 
-/* Hash table with (XfceMenuElement => gpointer) pairs */
-static GHashTable *xfce_menu_monitor_handles;
+/* Hash table with (XfceMenuItem => gpointer) pairs */
+static GHashTable *xfce_menu_monitor_item_handles;
 
+/* Hash table with (Directory => gpointer) pairs */
+static GHashTable *xfce_menu_monitor_shared_handles;
 
+/* Structure for directory handles */
+typedef struct _SharedHandle SharedHandle;
+struct _SharedHandle  
+{
+  gpointer monitor_handle;
+  int      references;
+};
 
+
+
 void 
 _xfce_menu_monitor_init (void)
 {
-  /* Initialize hash table */
-  xfce_menu_monitor_handles = g_hash_table_new (NULL, NULL);
+  /* Initialize hash tables */
+  xfce_menu_monitor_item_handles = g_hash_table_new (NULL, NULL);
+  xfce_menu_monitor_shared_handles = g_hash_table_new_full (NULL, NULL, 
g_free, g_free);
 }
 
 
@@ -61,9 +73,11 @@
 _xfce_menu_monitor_shutdown (void)
 {
 #if GLIB_CHECK_VERSION(2,10,0)
-  g_hash_table_unref (xfce_menu_monitor_handles);
+  g_hash_table_unref (xfce_menu_monitor_item_handles);
+  g_hash_table_unref (xfce_menu_monitor_shared_handles);
 #else
-  g_hash_table_destroy (xfce_menu_monitor_handles);
+  g_hash_table_destroy (xfce_menu_monitor_item_handles);
+  g_hash_table_destroy (xfce_menu_monitor_shared_handles);
 #endif
 }
 
@@ -114,7 +128,7 @@
   if (G_LIKELY (monitor_handle != NULL))
     {
       /* Store the item => handle pair in the hash table */
-      g_hash_table_insert (xfce_menu_monitor_handles, item, monitor_handle);
+      g_hash_table_insert (xfce_menu_monitor_item_handles, item, 
monitor_handle);
     }
 
   return monitor_handle;
@@ -134,11 +148,178 @@
     return;
 
   /* Lookup the monitor handle for this item */
-  monitor_handle = g_hash_table_lookup (xfce_menu_monitor_handles, item);
+  monitor_handle = g_hash_table_lookup (xfce_menu_monitor_item_handles, item);
 
   if (G_LIKELY (monitor_handle != NULL))
     {
       /* Remove monitor handle from the library client */
       xfce_menu_monitor_vtable.remove_monitor (menu, monitor_handle);
+
+      /* ... and remove the item from the hash table */
+      g_hash_table_remove (xfce_menu_monitor_item_handles, item);
     }
 }
+
+
+
+gpointer
+xfce_menu_monitor_add_directory (XfceMenu    *menu,
+                                 const gchar *directory)
+{
+  SharedHandle *shared_handle;
+  gpointer         monitor_handle;
+
+  g_return_val_if_fail (XFCE_IS_MENU (menu), NULL);
+  g_return_val_if_fail (directory != NULL, NULL);
+  
+  if (G_UNLIKELY (xfce_menu_monitor_vtable.monitor_directory == NULL))
+    return NULL;
+
+  /* Load directory handle from the hash table */
+  shared_handle = (SharedHandle *)g_hash_table_lookup 
(xfce_menu_monitor_shared_handles, directory);
+
+  /* Check if the directory is already monitored */
+  if (G_LIKELY (shared_handle != NULL))
+    {
+      /* Increment reference counter */
+      shared_handle->references++;
+
+      /* Return the monitor handle */
+      monitor_handle = shared_handle->monitor_handle;
+    }
+  else
+    {
+      /* Request monitor handle from the library client */
+      monitor_handle = xfce_menu_monitor_vtable.monitor_directory (menu, 
directory, xfce_menu_monitor_user_data);
+
+      if (G_LIKELY (monitor_handle != NULL))
+        {
+          /* Allocate new directory handle */
+          shared_handle = g_new0 (SharedHandle, 1);
+
+          /* Set values (reference counter starts with 1) */
+          shared_handle->references = 1;
+          shared_handle->monitor_handle = monitor_handle;
+
+          /* Store the item => handle pair in the hash table */
+          g_hash_table_insert (xfce_menu_monitor_shared_handles, g_strdup 
(directory), shared_handle);
+        }
+    }
+
+  return monitor_handle;
+}
+
+
+
+void
+xfce_menu_monitor_remove_directory (XfceMenu    *menu,
+                                    const gchar *directory)
+{
+  SharedHandle *shared_handle;
+
+  g_return_if_fail (directory != NULL);
+  
+  if (G_UNLIKELY (xfce_menu_monitor_vtable.remove_monitor == NULL))
+    return;
+
+  /* Lookup the directory handle for this directory */
+  shared_handle = g_hash_table_lookup (xfce_menu_monitor_shared_handles, 
directory);
+
+  if (G_LIKELY (shared_handle != NULL))
+    {
+      /* Decrement the reference counter */
+      shared_handle->references--;
+
+      /* Check if there are no references left */
+      if (G_UNLIKELY (shared_handle->references == 0)) 
+        {
+          /* Remove monitor handle from the library client */
+          xfce_menu_monitor_vtable.remove_monitor (menu, 
shared_handle->monitor_handle);
+
+          /* Remove directory handle from the hash table and destroy it */
+          g_hash_table_remove (xfce_menu_monitor_shared_handles, 
shared_handle);
+        }
+    }
+}
+
+
+
+gpointer
+xfce_menu_monitor_add_file (XfceMenu    *menu,
+                            const gchar *filename)
+{
+  SharedHandle *shared_handle;
+  gpointer      monitor_handle;
+
+  g_return_val_if_fail (XFCE_IS_MENU (menu), NULL);
+  g_return_val_if_fail (filename != NULL, NULL);
+  
+  if (G_UNLIKELY (xfce_menu_monitor_vtable.monitor_file == NULL))
+    return NULL;
+
+  /* Load filename handle from the hash table */
+  shared_handle = (SharedHandle *)g_hash_table_lookup 
(xfce_menu_monitor_shared_handles, filename);
+
+  /* Check if the file is already monitored */
+  if (G_LIKELY (shared_handle != NULL))
+    {
+      /* Increment reference counter */
+      shared_handle->references++;
+
+      /* Return the monitor handle */
+      monitor_handle = shared_handle->monitor_handle;
+    }
+  else
+    {
+      /* Request monitor handle from the library client */
+      monitor_handle = xfce_menu_monitor_vtable.monitor_file (menu, filename, 
xfce_menu_monitor_user_data);
+
+      if (G_LIKELY (monitor_handle != NULL))
+        {
+          /* Allocate new filename handle */
+          shared_handle = g_new0 (SharedHandle, 1);
+
+          /* Set values (reference counter starts with 1) */
+          shared_handle->references = 1;
+          shared_handle->monitor_handle = monitor_handle;
+
+          /* Store the item => handle pair in the hash table */
+          g_hash_table_insert (xfce_menu_monitor_shared_handles, g_strdup 
(filename), shared_handle);
+        }
+    }
+
+  return monitor_handle;
+}
+
+
+
+void
+xfce_menu_monitor_remove_file (XfceMenu    *menu,
+                               const gchar *filename)
+{
+  SharedHandle *shared_handle;
+
+  g_return_if_fail (filename != NULL);
+  
+  if (G_UNLIKELY (xfce_menu_monitor_vtable.remove_monitor == NULL))
+    return;
+
+  /* Lookup the filename handle for this file */
+  shared_handle = g_hash_table_lookup (xfce_menu_monitor_shared_handles, 
filename);
+
+  if (G_LIKELY (shared_handle != NULL))
+    {
+      /* Decrement the reference counter */
+      shared_handle->references--;
+
+      /* Check if there are no references left */
+      if (G_UNLIKELY (shared_handle->references == 0)) 
+        {
+          /* Remove monitor handle from the library client */
+          xfce_menu_monitor_vtable.remove_monitor (menu, 
shared_handle->monitor_handle);
+
+          /* Remove filename handle from the hash table and destroy it */
+          g_hash_table_remove (xfce_menu_monitor_shared_handles, 
shared_handle);
+        }
+    }
+}

Modified: libxfce4menu/trunk/libxfce4menu/xfce-menu-monitor.h
===================================================================
--- libxfce4menu/trunk/libxfce4menu/xfce-menu-monitor.h 2007-09-18 12:54:21 UTC 
(rev 26087)
+++ libxfce4menu/trunk/libxfce4menu/xfce-menu-monitor.h 2007-09-19 22:28:31 UTC 
(rev 26088)
@@ -34,12 +34,20 @@
 
 typedef struct _XfceMenuMonitorVTable XfceMenuMonitorVTable;
 
-void     xfce_menu_monitor_set_vtable  (XfceMenuMonitorVTable *vtable, 
-                                        gpointer               user_data);
-gpointer xfce_menu_monitor_add_item    (XfceMenu              *menu,
-                                        XfceMenuItem          *item);
-void     xfce_menu_monitor_remove_item (XfceMenu              *menu,
-                                        XfceMenuItem          *item);
+void     xfce_menu_monitor_set_vtable       (XfceMenuMonitorVTable *vtable, 
+                                             gpointer               user_data);
+gpointer xfce_menu_monitor_add_item         (XfceMenu              *menu,
+                                             XfceMenuItem          *item);
+void     xfce_menu_monitor_remove_item      (XfceMenu              *menu,
+                                             XfceMenuItem          *item);
+gpointer xfce_menu_monitor_add_directory    (XfceMenu              *menu,
+                                             const gchar           *directory);
+void     xfce_menu_monitor_remove_directory (XfceMenu              *menu,
+                                             const gchar           *directory);
+gpointer xfce_menu_monitor_add_file         (XfceMenu              *menu,
+                                             const gchar           *filename);
+void     xfce_menu_monitor_remove_file      (XfceMenu              *menu,
+                                             const gchar           *filename);
 
 /**
  * XfceMenuMonitorVTable:

Modified: libxfce4menu/trunk/libxfce4menu/xfce-menu.c
===================================================================
--- libxfce4menu/trunk/libxfce4menu/xfce-menu.c 2007-09-18 12:54:21 UTC (rev 
26087)
+++ libxfce4menu/trunk/libxfce4menu/xfce-menu.c 2007-09-19 22:28:31 UTC (rev 
26088)
@@ -3148,6 +3148,17 @@
 
   g_return_if_fail (XFCE_IS_MENU (menu));
 
+  /* Monitor the menu file */
+  xfce_menu_monitor_add_file (menu, menu->priv->filename);
+
+  /* Monitor the menu directory file */
+  if (XFCE_IS_MENU_DIRECTORY (menu->priv->directory))
+    xfce_menu_monitor_add_file (menu, xfce_menu_directory_get_filename 
(menu->priv->directory));
+
+  /* Monitor the application directories */
+  for (iter = menu->priv->app_dirs; iter != NULL; iter = g_slist_next (iter))
+    xfce_menu_monitor_add_directory (menu, (const gchar *)iter->data);
+
   /* Monitor items in the menu pool */
   xfce_menu_item_pool_foreach (menu->priv->pool, (GHFunc) item_monitor_start, 
menu);
 
@@ -3171,8 +3182,25 @@
 static void
 xfce_menu_monitor_stop (XfceMenu *menu)
 {
+  GSList *iter;
+
   g_return_if_fail (XFCE_IS_MENU (menu));
 
+  /* Stop monitoring items in submenus */
+  for (iter = menu->priv->submenus; iter != NULL; iter = g_slist_next (iter))
+    xfce_menu_monitor_stop (XFCE_MENU (iter->data));
+
   /* Stop monitoring the items */
   xfce_menu_item_pool_foreach (menu->priv->pool, (GHFunc) item_monitor_stop, 
menu);
+
+  /* Stop monitoring the application directories */
+  for (iter = menu->priv->app_dirs; iter != NULL; iter = g_slist_next (iter))
+    xfce_menu_monitor_remove_directory (menu, (const gchar *)iter->data);
+
+  /* Stop monitoring the menu directory file */
+  if (XFCE_IS_MENU_DIRECTORY (menu->priv->directory))
+    xfce_menu_monitor_remove_file (menu, xfce_menu_directory_get_filename 
(menu->priv->directory));
+
+  /* Stop monitoring the menu file */
+  xfce_menu_monitor_remove_file (menu, menu->priv->filename);
 }

_______________________________________________
Xfce4-commits mailing list
[email protected]
http://foo-projects.org/mailman/listinfo/xfce4-commits

Reply via email to