glib r7596 - branches/glib-2-18/gio



Author: alexl
Date: Mon Oct 13 10:03:15 2008
New Revision: 7596
URL: http://svn.gnome.org/viewvc/glib?rev=7596&view=rev

Log:
2008-10-13  Alexander Larsson  <alexl redhat com>

	Merged from trunk:

        * gdesktopappinfo.c:
	When adding an application as handling a mime type (but
	not as the default), copy the full list of desktop ids handling
	that type in before adding the new one on the end of the list.
	This means we're not accidentally changing the default by overriding
	the info from the later directories in the search path.
	
	Also, fixes small leak of removed_entries.



Modified:
   branches/glib-2-18/gio/ChangeLog
   branches/glib-2-18/gio/gdesktopappinfo.c

Modified: branches/glib-2-18/gio/gdesktopappinfo.c
==============================================================================
--- branches/glib-2-18/gio/gdesktopappinfo.c	(original)
+++ branches/glib-2-18/gio/gdesktopappinfo.c	Mon Oct 13 10:03:15 2008
@@ -61,7 +61,8 @@
 #define MIME_CACHE_GROUP            "MIME Cache"
 
 static void     g_desktop_app_info_iface_init         (GAppInfoIface    *iface);
-static GList *  get_all_desktop_entries_for_mime_type (const char       *base_mime_type);
+static GList *  get_all_desktop_entries_for_mime_type (const char       *base_mime_type,
+						       const char      **except);
 static void     mime_info_cache_reload                (const char       *dir);
 static gboolean g_desktop_app_info_ensure_saved       (GDesktopAppInfo  *info,
 						       GError          **error);
@@ -1159,22 +1160,22 @@
 static gboolean
 update_mimeapps_list (const char  *desktop_id, 
 		      const char  *content_type, 
-		      gboolean     add_at_start, 
-		      gboolean     add_at_end, 
+		      gboolean     add_as_default,
+		      gboolean     add_non_default,
 		      gboolean     remove, 
 		      GError     **error)
 {
   char *dirname, *filename;
   GKeyFile *key_file;
   gboolean load_succeeded, res;
-  char **old_list;
-  char **list;
+  char **old_list, **list;
+  GList *system_list, *l;
   gsize length, data_size;
   char *data;
   int i, j;
 
   /* Don't add both at start and end */
-  g_assert (!(add_at_start && add_at_end));
+  g_assert (!(add_as_default && add_non_default));
   
   dirname = ensure_dir (APP_DIR, error);
   if (!dirname)
@@ -1200,7 +1201,7 @@
   list = g_new (char *, 1 + length + 1);
 
   i = 0;
-  if (add_at_start)
+  if (add_as_default)
     list[i++] = g_strdup (desktop_id);
   if (old_list)
     {
@@ -1208,10 +1209,40 @@
 	{
 	  if (strcmp (old_list[j], desktop_id) != 0)
 	    list[i++] = g_strdup (old_list[j]);
+	  else if (add_non_default)
+	    {
+	      /* If adding as non-default, and its already in,
+		 don't change order of desktop ids */
+	      add_non_default = FALSE;
+	      list[i++] = g_strdup (old_list[j]);
+	    }
 	}
     }
-  if (add_at_end)
-    list[i++] = g_strdup (desktop_id);
+       
+  if (add_non_default)
+    {
+      /* We're adding as non-default, and it wasn't already in the list,
+	 so we add at the end. But to avoid listing the app before the
+	 current system default (thus changing the default) we have to
+	 add the current list of (not yet listed) apps before it. */
+      
+      list[i] = NULL; /* Terminate current list so we can use it */
+ 	  system_list =  get_all_desktop_entries_for_mime_type (content_type, list);
+	  
+ 	  list = g_renew (char *, list, 1 + length + g_list_length (system_list) + 1);
+ 	  
+ 	  for (l = system_list; l != NULL; l = l->next)
+ 	    {
+ 	      list[i++] = l->data; /* no strdup, taking ownership */
+ 	      if (strcmp (l->data, desktop_id) == 0)
+ 		add_non_default = FALSE;
+ 	    }
+ 	  g_list_free (system_list);
+	  
+ 	  if (add_non_default)
+ 	    list[i++] = g_strdup (desktop_id);
+    }
+  
   list[i] = NULL;
   
   g_strfreev (old_list);
@@ -1620,7 +1651,7 @@
 
   g_return_val_if_fail (content_type != NULL, NULL);
   
-  desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
+  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL);
 
   infos = NULL;
   for (l = desktop_entries; l != NULL; l = l->next)
@@ -1663,7 +1694,7 @@
 
   g_return_val_if_fail (content_type != NULL, NULL);
   
-  desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
+  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL);
 
   info = NULL;
   for (l = desktop_entries; l != NULL; l = l->next)
@@ -2429,17 +2460,21 @@
 /**
  * get_all_desktop_entries_for_mime_type:
  * @mime_type: a mime type.
+ * @except: NULL or a strv list
  *
  * Returns all the desktop ids for @mime_type. The desktop files
  * are listed in an order so that default applications are listed before
  * non-default ones, and handlers for inherited mimetypes are listed
  * after the base ones.
  *
+ * Optionally doesn't list the desktop ids given in the @except 
+ *
  * Return value: a #GList containing the desktop ids which claim
  *    to handle @mime_type.
  */
 static GList *
-get_all_desktop_entries_for_mime_type (const char *base_mime_type)
+get_all_desktop_entries_for_mime_type (const char *base_mime_type,
+				       const char **except)
 {
   GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
   MimeInfoCacheDir *dir;
@@ -2481,6 +2516,10 @@
   
   removed_entries = NULL;
   desktop_entries = NULL;
+
+  for (i = 0; except != NULL && except[i] != NULL; i++)
+    removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
+  
   for (i = 0; mime_types[i] != NULL; i++)
     {
       mime_type = mime_types[i];
@@ -2525,6 +2564,7 @@
 
   g_strfreev (mime_types);
 
+  g_list_foreach (removed_entries, (GFunc)g_free, NULL);
   g_list_free (removed_entries);
   
   desktop_entries = g_list_reverse (desktop_entries);



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