gnome-menus r893 - in trunk: . libmenu



Author: vuntz
Date: Fri May 16 23:10:04 2008
New Revision: 893
URL: http://svn.gnome.org/viewvc/gnome-menus?rev=893&view=rev

Log:
2008-05-17  Vincent Untz  <vuntz gnome org>

	Implement missing part of the menu spec: handling of $XDG_MENU_PREFIX
	for applications.menu
	Bug #533475

	* libmenu/gmenu-tree.c: (canonicalize_basename): new, based on code
	from gmenu_tree_canonicalize_path
	(gmenu_tree_canonicalize_path): if the requested file is
	applications.menu and the XDG_MENU_PREFIX environment variable is set,
	use ${XDG_MENU_PREFIX}applications.menu is used
	(gmenu_tree_get_menu_file): we need to return the basename of the real
	file, potentially with $XDG_MENU_PREFIX. So find which file is being
	used first.
	(load_merge_file): update for menu_layout_load update
	(gmenu_tree_load_layout): ditto
	* libmenu/menu-layout.[ch]: (menu_layout_load): add a
	non_prefixed_basename argument so that we can use the basename without
	$XDG_MENU_PREFIX and use it as the name for the root node


Modified:
   trunk/ChangeLog
   trunk/libmenu/gmenu-tree.c
   trunk/libmenu/menu-layout.c
   trunk/libmenu/menu-layout.h

Modified: trunk/libmenu/gmenu-tree.c
==============================================================================
--- trunk/libmenu/gmenu-tree.c	(original)
+++ trunk/libmenu/gmenu-tree.c	Fri May 16 23:10:04 2008
@@ -495,6 +495,32 @@
   return tree->canonical;
 }
 
+static void
+canonicalize_basename (GMenuTree  *tree,
+                       const char *basename)
+{
+  if (!canonicalize_basename_with_config_dir (tree,
+                                              basename,
+                                              g_get_user_config_dir ()))
+    {
+      const char * const *system_config_dirs;
+      int                 i;
+
+      system_config_dirs = g_get_system_config_dirs ();
+
+      i = 0;
+      while (system_config_dirs[i] != NULL)
+        {
+          if (canonicalize_basename_with_config_dir (tree,
+                                                     basename,
+                                                     system_config_dirs[i]))
+            break;
+
+          ++i;
+        }
+    }
+}
+
 static gboolean
 gmenu_tree_canonicalize_path (GMenuTree *tree)
 {
@@ -507,26 +533,20 @@
     {
       gmenu_tree_remove_menu_file_monitors (tree);
 
-      if (!canonicalize_basename_with_config_dir (tree,
-						  tree->basename,
-						  g_get_user_config_dir ()))
-	{
-	  const char * const *system_config_dirs;
-	  int                 i;
-
-	  system_config_dirs = g_get_system_config_dirs ();
-
-	  i = 0;
-	  while (system_config_dirs[i] != NULL)
-	    {
-	      if (canonicalize_basename_with_config_dir (tree,
-							 tree->basename,
-							 system_config_dirs[i]))
-		break;
+      if (tree->type == GMENU_TREE_BASENAME &&
+          strcmp (tree->basename, "applications.menu") == 0 &&
+          g_getenv ("XDG_MENU_PREFIX"))
+        {
+          char *prefixed_basename;
+          prefixed_basename = g_strdup_printf ("%s%s",
+                                               g_getenv ("XDG_MENU_PREFIX"),
+                                               tree->basename);
+          canonicalize_basename (tree, prefixed_basename);
+          g_free (prefixed_basename);
+        }
 
-	      ++i;
-	    }
-	}
+      if (!tree->canonical)
+        canonicalize_basename (tree, tree->basename);
 
       if (tree->canonical)
         menu_verbose ("Successfully looked up menu_file for \"%s\": %s\n",
@@ -717,9 +737,31 @@
 const char *
 gmenu_tree_get_menu_file (GMenuTree *tree)
 {
+  /* FIXME: this is horribly ugly. But it's done to keep the API. Would be bad
+   * to break the API only for a "const char *" => "char *" change. The other
+   * alternative is to leak the memory, which is bad too. */
+  static char *ugly_result_cache = NULL;
+
   g_return_val_if_fail (tree != NULL, NULL);
 
-  return tree->type == GMENU_TREE_BASENAME ? tree->basename : tree->absolute_path;
+  /* we need to canonicalize the path so we actually find out the real menu
+   * file that is being used -- and take into account XDG_MENU_PREFIX */
+  if (!gmenu_tree_canonicalize_path (tree))
+    return NULL;
+
+  if (ugly_result_cache != NULL)
+    {
+      g_free (ugly_result_cache);
+      ugly_result_cache = NULL;
+    }
+
+  if (tree->type == GMENU_TREE_BASENAME)
+    {
+      ugly_result_cache = g_path_get_basename (tree->canonical_path);
+      return ugly_result_cache;
+    }
+  else
+    return tree->absolute_path;
 }
 
 GMenuTreeDirectory *
@@ -1671,7 +1713,7 @@
 
   menu_verbose ("Merging file \"%s\"\n", canonical);
 
-  to_merge = menu_layout_load (canonical, NULL);
+  to_merge = menu_layout_load (canonical, NULL, NULL);
   if (to_merge == NULL)
     {
       menu_verbose ("No menu for file \"%s\" found when merging\n",
@@ -2793,7 +2835,10 @@
                 tree->canonical_path);
 
   error = NULL;
-  tree->layout = menu_layout_load (tree->canonical_path, &error);
+  tree->layout = menu_layout_load (tree->canonical_path,
+                                   tree->type == GMENU_TREE_BASENAME ?
+                                        tree->basename : NULL,
+                                   &error);
   if (tree->layout == NULL)
     {
       g_warning ("Error loading menu layout from \"%s\": %s",

Modified: trunk/libmenu/menu-layout.c
==============================================================================
--- trunk/libmenu/menu-layout.c	(original)
+++ trunk/libmenu/menu-layout.c	Fri May 16 23:10:04 2008
@@ -2263,6 +2263,7 @@
 
 MenuLayoutNode *
 menu_layout_load (const char  *filename,
+                  const char  *non_prefixed_basename,
                   GError     **err)
 {
   GMarkupParseContext *context;
@@ -2301,7 +2302,10 @@
   root->basedir = g_path_get_dirname (filename);
   menu_verbose ("Set basedir \"%s\"\n", root->basedir);
 
-  s = g_path_get_basename (filename);
+  if (non_prefixed_basename)
+    s = g_strdup (non_prefixed_basename);
+  else
+    s = g_path_get_basename (filename);
   str = g_string_new (s);
   if (g_str_has_suffix (str->str, ".menu"))
     g_string_truncate (str, str->len - strlen (".menu"));

Modified: trunk/libmenu/menu-layout.h
==============================================================================
--- trunk/libmenu/menu-layout.h	(original)
+++ trunk/libmenu/menu-layout.h	Fri May 16 23:10:04 2008
@@ -106,6 +106,7 @@
 
 
 MenuLayoutNode *menu_layout_load (const char  *filename,
+                                  const char  *non_prefixed_basename,
                                   GError     **error);
 
 MenuLayoutNode *menu_layout_node_new   (MenuLayoutNodeType  type);



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