[Xfce4-commits] <garcon:master> Improve code quality of garcon_config_lookup().
Jannis Pohlmann
noreply at xfce.org
Sun Sep 5 21:42:07 CEST 2010
Updating branch refs/heads/master
to ab3b66cf90382fd8dfe161783d423858e9dd3180 (commit)
from 186d64776a647e7b924c20ae3d01ff24aaac18e5 (commit)
commit ab3b66cf90382fd8dfe161783d423858e9dd3180
Author: Jannis Pohlmann <jannis at xfce.org>
Date: Sun Sep 5 15:53:44 2010 +0200
Improve code quality of garcon_config_lookup().
garcon/garcon-config.c | 20 +++++----
garcon/garcon-menu.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 122 insertions(+), 9 deletions(-)
diff --git a/garcon/garcon-config.c b/garcon/garcon-config.c
index 1aee68d..02d5413 100644
--- a/garcon/garcon-config.c
+++ b/garcon/garcon-config.c
@@ -167,23 +167,25 @@ garcon_config_lookup (const gchar *filename)
/* Look for the file in the user's config directory */
path = g_build_filename (g_get_user_config_dir (), filename, NULL);
- if (g_path_is_absolute (path)
- && g_file_test (path, G_FILE_TEST_IS_REGULAR))
+ if (g_path_is_absolute (path) && g_file_test (path, G_FILE_TEST_IS_REGULAR))
return path;
+
g_free (path);
+ path = NULL;
/* Look for the file in the system config directories */
dirs = g_get_system_config_dirs ();
- for (i = 0; dirs[i] != NULL; ++i)
+ for (i = 0; path == NULL && dirs[i] != NULL; ++i)
{
/* Build the filename, if the file exists return the path */
path = g_build_filename (dirs[i], filename, NULL);
- if (g_path_is_absolute (path)
- && g_file_test (path, G_FILE_TEST_IS_REGULAR))
- return path;
- g_free (path);
+ if (!g_path_is_absolute (path) || !g_file_test (path, G_FILE_TEST_IS_REGULAR))
+ {
+ g_free (path);
+ path = NULL;
+ }
}
- /* Nothing found */
- return NULL;
+ /* Return the path or NULL if the file could not be found */
+ return path;
}
diff --git a/garcon/garcon-menu.c b/garcon/garcon-menu.c
index 06f74f3..37ec508 100644
--- a/garcon/garcon-menu.c
+++ b/garcon/garcon-menu.c
@@ -142,6 +142,12 @@ static gboolean garcon_menu_get_element_equal (GarconM
GarconMenuElement *other);
static void garcon_menu_start_monitoring (GarconMenu *menu);
static void garcon_menu_stop_monitoring (GarconMenu *menu);
+static void garcon_menu_monitor_menu_files (GarconMenu *menu);
+static void garcon_menu_file_changed (GarconMenu *menu,
+ GFile *file,
+ GFile *other_file,
+ GFileMonitorEvent event_type,
+ GFileMonitor *monitor);
@@ -153,6 +159,9 @@ struct _GarconMenuPrivate
/* DOM tree */
GNode *tree;
+ /* File and directory monitors */
+ GList *monitors;
+
/* Directory */
GarconMenuDirectory *directory;
@@ -243,6 +252,7 @@ garcon_menu_init (GarconMenu *menu)
menu->priv = GARCON_MENU_GET_PRIVATE (menu);
menu->priv->file = NULL;
menu->priv->tree = NULL;
+ menu->priv->monitors = NULL;
menu->priv->directory = NULL;
menu->priv->submenus = NULL;
menu->priv->parent = NULL;
@@ -1639,6 +1649,20 @@ garcon_menu_get_element_equal (GarconMenuElement *element,
static void
garcon_menu_start_monitoring (GarconMenu *menu)
{
+ GList *lp;
+
+ g_return_if_fail (GARCON_IS_MENU (menu));
+
+ /* Let only the root menu monitor menu files, merge fileS/directories and app dirs */
+ if (menu->priv->parent == NULL)
+ {
+ garcon_menu_monitor_menu_files (menu);
+ }
+
+ /* Recurse into submenus */
+ for (lp = menu->priv->submenus; lp != NULL; lp = lp->next)
+ garcon_menu_start_monitoring (lp->data);
+
}
@@ -1646,4 +1670,91 @@ garcon_menu_start_monitoring (GarconMenu *menu)
static void
garcon_menu_stop_monitoring (GarconMenu *menu)
{
+ GList *lp;
+
+ g_return_if_fail (GARCON_IS_MENU (menu));
+
+ /* Recurse into submenus */
+ for (lp = menu->priv->submenus; lp != NULL; lp = lp->next)
+ garcon_menu_stop_monitoring (lp->data);
+
+ /* Disconnect and destroy all monitors */
+ for (lp = menu->priv->monitors; lp != NULL; lp = lp->next)
+ {
+ g_signal_handlers_disconnect_matched (lp->data, G_SIGNAL_MATCH_DATA,
+ 0, 0, NULL, NULL, menu);
+ g_object_unref (lp->data);
+ }
+
+ /* Free the monitor list */
+ g_list_free (menu->priv->monitors);
+ menu->priv->monitors = NULL;
+}
+
+
+
+static void
+garcon_menu_monitor_menu_files (GarconMenu *menu)
+{
+ GFileMonitor *monitor;
+ GFile *file;
+ gchar **paths;
+ guint n;
+ guint i;
+
+ g_return_if_fail (GARCON_IS_MENU (menu));
+
+ if (menu->priv->uses_custom_path)
+ {
+ g_debug ("monitor menu file: %s", g_file_get_path (menu->priv->file));
+
+ /* Monitor the root .menu file */
+ monitor = g_file_monitor (menu->priv->file, G_FILE_MONITOR_NONE, NULL, NULL);
+ if (monitor != NULL)
+ {
+ menu->priv->monitors = g_list_prepend (menu->priv->monitors, monitor);
+ g_signal_connect_swapped (monitor, "changed",
+ G_CALLBACK (garcon_menu_file_changed), menu);
+ }
+ }
+ else
+ {
+ /* Monitor all application menu candidates */
+ for (n = 0; n < G_N_ELEMENTS (GARCON_MENU_ROOT_SPECS); ++n)
+ {
+ paths = garcon_config_build_paths (GARCON_MENU_ROOT_SPECS[n]);
+
+ for (i = 0; paths != NULL && paths[i] != NULL; ++i)
+ {
+ file = g_file_new_for_path (paths[i]);
+
+ g_debug ("monitor menu file: %s", g_file_get_path (file));
+
+ monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, NULL);
+ if (monitor != NULL)
+ {
+ menu->priv->monitors = g_list_prepend (menu->priv->monitors, monitor);
+ g_signal_connect_swapped (monitor, "changed",
+ G_CALLBACK (garcon_menu_file_changed), menu);
+ }
+
+ g_object_unref (file);
+ }
+
+ g_strfreev (paths);
+ }
+ }
+}
+
+
+
+static void
+garcon_menu_file_changed (GarconMenu *menu,
+ GFile *file,
+ GFile *other_file,
+ GFileMonitorEvent event_type,
+ GFileMonitor *monitor)
+{
+ g_return_if_fail (GARCON_IS_MENU (menu));
+ g_return_if_fail (menu->priv->parent == NULL);
}
More information about the Xfce4-commits
mailing list