[Xfce4-commits] <thunar:master> Only change the Name field when renaming desktop files (bug #7155).

Jannis Pohlmann noreply at xfce.org
Sun Jan 30 15:08:01 CET 2011


Updating branch refs/heads/master
         to f6bbdf98bae707b71c529d86e5896302c5119476 (commit)
       from aabb332fb07aa7b86c74eae518cadd9764c296ed (commit)

commit f6bbdf98bae707b71c529d86e5896302c5119476
Author: Jannis Pohlmann <jannis at xfce.org>
Date:   Sun Jan 30 15:01:10 2011 +0100

    Only change the Name field when renaming desktop files (bug #7155).
    
    This fixes a regression introduced in 4.8. In thunar-vfs we did the same
    thing we do now: when a desktop entry is to be renamed we only replace
    the Name field instead of renaming the file itself.
    
    This also affects xfdesktop which uses Thunar's rename feature through
    D-Bus.

 NEWS                 |    1 +
 thunar/thunar-file.c |  163 +++++++++++++++++++++++++++++++-------------------
 2 files changed, 103 insertions(+), 61 deletions(-)

diff --git a/NEWS b/NEWS
index 8439901..111d2ac 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,7 @@
 - Ship stock_folder-copy.png and stock_folder-move.png icons with
   Thunar itself (bug #6851).
 - Fix segfault when calling strcasecmp with NULL parameters (bug #7206).
+- Only change the Name field when renaming desktop files (bug #7155).
 
 1.2.0
 =====
diff --git a/thunar/thunar-file.c b/thunar/thunar-file.c
index 1171527..bda7669 100644
--- a/thunar/thunar-file.c
+++ b/thunar/thunar-file.c
@@ -1,21 +1,22 @@
-/* $Id$ */
+/* vi:set et ai sw=2 sts=2 ts=2: */
 /*-
  * Copyright (c) 2005-2007 Benedikt Meurer <benny at xfce.org>
- * Copyright (c) 2009-2010 Jannis Pohlmann <jannis at xfce.org>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA  02111-1307  USA
+ * Copyright (c) 2009-2011 Jannis Pohlmann <jannis at xfce.org>
+ *
+ * This program is free software; you can redistribute it and/or 
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of 
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public 
+ * License along with this program; if not, write to the Free 
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -1253,76 +1254,116 @@ thunar_file_rename (ThunarFile   *file,
                     gboolean      called_from_job,
                     GError      **error)
 {
-  GFile *previous_file;
-  GFile *renamed_file;
-  gint   watch_count;
+  GKeyFile *key_file;
+  GError   *err = NULL;
+  GFile    *previous_file;
+  GFile    *renamed_file;
+  gint      watch_count;
 
   _thunar_return_val_if_fail (THUNAR_IS_FILE (file), FALSE);
   _thunar_return_val_if_fail (g_utf8_validate (name, -1, NULL), FALSE);
   _thunar_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
   _thunar_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-  /* remember the previous file */
-  previous_file = g_object_ref (file->gfile);
-  
-  /* try to rename the file */
-  renamed_file = g_file_set_display_name (file->gfile, name, cancellable, error);
-
-  /* check if we succeeded */
-  if (renamed_file != NULL)
+  /* check if this file is a desktop entry */
+  if (thunar_file_is_desktop_file (file))
     {
-      /* set the new file */
-      file->gfile = renamed_file;
+      /* try to load the desktop entry into a key file */
+      key_file = thunar_g_file_query_key_file (file->gfile, cancellable, &err);
+      if (key_file == NULL)
+        {
+          g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL,
+                       _("Failed to parse the desktop file: %s"), err->message);
+          g_error_free (err);
+          return FALSE;
+        }
 
-      /* reload file information */
-      thunar_file_load (file, NULL, NULL);
+      /* change the Name field of the desktop entry */
+      g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                             G_KEY_FILE_DESKTOP_KEY_NAME, name);
 
-      /* need to re-register the monitor handle for the new uri */
-      watch_count = THUNAR_FILE_GET_WATCH_COUNT (file);
-      if (G_LIKELY (watch_count > 0))
+      /* write the changes back to the file */
+      if (thunar_g_file_write_key_file (file->gfile, key_file, cancellable, &err))
         {
-          /* drop the watch_count temporary */
-          THUNAR_FILE_SET_WATCH_COUNT (file, 1);
+          /* notify everybody that the file has changed */
+          thunar_file_changed (file);
 
-          /* drop the previous handle (with the old path) */
-          thunar_file_unwatch (file);
+          /* release the key file and return with success */
+          g_key_file_free (key_file);
+          return TRUE;
+        }
+      else
+        {
+          /* propagate the error message and return with failure */
+          g_propagate_error (error, err);
+          g_key_file_free (key_file);
+          return FALSE;
+        }
+    }
+  else
+    {
+      /* remember the previous file */
+      previous_file = g_object_ref (file->gfile);
+      
+      /* try to rename the file */
+      renamed_file = g_file_set_display_name (file->gfile, name, cancellable, error);
 
-          /* register the new handle (with the new path) */
-          thunar_file_watch (file);
+      /* check if we succeeded */
+      if (renamed_file != NULL)
+        {
+          /* set the new file */
+          file->gfile = renamed_file;
 
-          /* reset the watch count */
-          THUNAR_FILE_SET_WATCH_COUNT (file, watch_count);
-        }
+          /* reload file information */
+          thunar_file_load (file, NULL, NULL);
 
-      G_LOCK (file_cache_mutex);
+          /* need to re-register the monitor handle for the new uri */
+          watch_count = THUNAR_FILE_GET_WATCH_COUNT (file);
+          if (G_LIKELY (watch_count > 0))
+            {
+              /* drop the watch_count temporary */
+              THUNAR_FILE_SET_WATCH_COUNT (file, 1);
 
-      /* drop the previous entry from the cache */
-      g_hash_table_remove (file_cache, previous_file);
+              /* drop the previous handle (with the old path) */
+              thunar_file_unwatch (file);
 
-      /* drop the reference on the previous file */
-      g_object_unref (previous_file);
+              /* register the new handle (with the new path) */
+              thunar_file_watch (file);
 
-      /* insert the new entry */
-      g_hash_table_insert (file_cache, g_object_ref (file->gfile), file);
+              /* reset the watch count */
+              THUNAR_FILE_SET_WATCH_COUNT (file, watch_count);
+            }
 
-      G_UNLOCK (file_cache_mutex);
+          G_LOCK (file_cache_mutex);
 
-      if (!called_from_job)
-        {
-          /* tell the associated folder that the file was renamed */
-          thunarx_file_info_renamed (THUNARX_FILE_INFO (file));
+          /* drop the previous entry from the cache */
+          g_hash_table_remove (file_cache, previous_file);
 
-          /* emit the file changed signal */
-          thunar_file_changed (file);
-        }
+          /* drop the reference on the previous file */
+          g_object_unref (previous_file);
 
-      return TRUE;
-    }
-  else
-    {
-      g_object_unref (previous_file);
+          /* insert the new entry */
+          g_hash_table_insert (file_cache, g_object_ref (file->gfile), file);
 
-      return FALSE;
+          G_UNLOCK (file_cache_mutex);
+
+          if (!called_from_job)
+            {
+              /* tell the associated folder that the file was renamed */
+              thunarx_file_info_renamed (THUNARX_FILE_INFO (file));
+
+              /* emit the file changed signal */
+              thunar_file_changed (file);
+            }
+
+          return TRUE;
+        }
+      else
+        {
+          g_object_unref (previous_file);
+
+          return FALSE;
+        }
     }
 }
 



More information about the Xfce4-commits mailing list