[Xfce4-commits] <thunar:master> Improve clipboard handing (bug #8271).

Nick Schermer noreply at xfce.org
Thu Sep 27 20:56:04 CEST 2012


Updating branch refs/heads/master
         to c69f40c34b794567ff890570d7064e555c39b991 (commit)
       from acc411d36b8c82f105ff0be79beb35f7fe8ce47f (commit)

commit c69f40c34b794567ff890570d7064e555c39b991
Author: Nick Schermer <nick at xfce.org>
Date:   Thu Sep 27 20:54:10 2012 +0200

    Improve clipboard handing (bug #8271).
    
    Add case if the caller accepts uris. If normal utf-8 text
    is requested, return filenames, not uris.

 thunar/thunar-clipboard-manager.c |   68 ++++++++++++++++++++++++++++++------
 thunar/thunar-gio-extensions.c    |   34 +++++++-----------
 thunar/thunar-gio-extensions.h    |    1 +
 thunar/thunar-location-button.c   |   15 ++++----
 thunar/thunar-path-entry.c        |   17 ++++-----
 thunar/thunar-standard-view.c     |   11 ++++--
 6 files changed, 92 insertions(+), 54 deletions(-)

diff --git a/thunar/thunar-clipboard-manager.c b/thunar/thunar-clipboard-manager.c
index 747ef03..19b50d0 100644
--- a/thunar/thunar-clipboard-manager.c
+++ b/thunar/thunar-clipboard-manager.c
@@ -52,6 +52,7 @@ enum
 
 enum
 {
+  TARGET_TEXT_URI_LIST,
   TARGET_GNOME_COPIED_FILES,
   TARGET_UTF8_STRING,
 };
@@ -118,6 +119,7 @@ typedef struct
 
 static const GtkTargetEntry clipboard_targets[] =
 {
+  { "text/uri-list", 0, TARGET_TEXT_URI_LIST },
   { "x-special/gnome-copied-files", 0, TARGET_GNOME_COPIED_FILES },
   { "UTF8_STRING", 0, TARGET_UTF8_STRING }
 };
@@ -404,16 +406,53 @@ thunar_clipboard_manager_targets_received (GtkClipboard     *clipboard,
 
 
 
+static gchar *
+thunar_clipboard_manager_g_file_list_to_string (GList       *list,
+                                                const gchar *prefix,
+                                                gboolean     format_for_text,
+                                                gsize       *len)
+{
+  GString *string;
+  gchar   *tmp;
+  GList   *lp;
+
+  /* allocate initial string */
+  string = g_string_new (prefix);
+
+  for (lp = list; lp != NULL; lp = lp->next)
+    {
+      if (format_for_text)
+        tmp = g_file_get_parse_name (G_FILE (lp->data));
+      else
+        tmp = g_file_get_uri (G_FILE (lp->data));
+
+      string = g_string_append (string, tmp);
+      g_free (tmp);
+
+      if (lp->next != NULL)
+        string = g_string_append_c (string, '\n');
+    }
+
+  if (len != NULL)
+    *len = string->len;
+
+  return g_string_free (string, FALSE);
+}
+
+
+
 static void
 thunar_clipboard_manager_get_callback (GtkClipboard     *clipboard,
                                        GtkSelectionData *selection_data,
                                        guint             target_info,
                                        gpointer          user_data)
 {
-  ThunarClipboardManager *manager = THUNAR_CLIPBOARD_MANAGER (user_data);
-  GList                  *file_list = NULL;
-  gchar                  *string_list;
-  gchar                  *data;
+  ThunarClipboardManager  *manager = THUNAR_CLIPBOARD_MANAGER (user_data);
+  GList                   *file_list;
+  gchar                   *str;
+  gchar                  **uris;
+  const gchar             *prefix;
+  gsize                    len;
 
   _thunar_return_if_fail (GTK_IS_CLIPBOARD (clipboard));
   _thunar_return_if_fail (THUNAR_IS_CLIPBOARD_MANAGER (manager));
@@ -422,19 +461,25 @@ thunar_clipboard_manager_get_callback (GtkClipboard     *clipboard,
   /* determine the path list from the file list */
   file_list = thunar_file_list_to_thunar_g_file_list (manager->files);
 
-  /* determine the string representation of the path list */
-  string_list = thunar_g_file_list_to_string (file_list);
-
   switch (target_info)
     {
+    case TARGET_TEXT_URI_LIST:
+      uris = thunar_g_file_list_to_stringv (file_list);
+      gtk_selection_data_set_uris (selection_data, uris);
+      g_strfreev (uris);
+      break;
+
     case TARGET_GNOME_COPIED_FILES:
-      data = g_strconcat (manager->files_cutted ? "cut\n" : "copy\n", string_list, NULL);
-      gtk_selection_data_set (selection_data, selection_data->target, 8, (guchar *) data, strlen (data));
-      g_free (data);
+      prefix = manager->files_cutted ? "cut\n" : "copy\n";
+      str = thunar_clipboard_manager_g_file_list_to_string (file_list, prefix, FALSE, &len);
+      gtk_selection_data_set (selection_data, selection_data->target, 8, (guchar *) str, len);
+      g_free (str);
       break;
 
     case TARGET_UTF8_STRING:
-      gtk_selection_data_set (selection_data, selection_data->target, 8, (guchar *) string_list, strlen (string_list));
+      str = thunar_clipboard_manager_g_file_list_to_string (file_list, NULL, TRUE, &len);
+      gtk_selection_data_set_text (selection_data, str, len);
+      g_free (str);
       break;
 
     default:
@@ -443,7 +488,6 @@ thunar_clipboard_manager_get_callback (GtkClipboard     *clipboard,
 
   /* cleanup */
   thunar_g_file_list_free (file_list);
-  g_free (string_list);
 }
 
 
diff --git a/thunar/thunar-gio-extensions.c b/thunar/thunar-gio-extensions.c
index 97cb651..feeb328 100644
--- a/thunar/thunar-gio-extensions.c
+++ b/thunar/thunar-gio-extensions.c
@@ -312,36 +312,28 @@ thunar_g_file_list_new_from_string (const gchar *string)
 
 
 /**
- * thunar_g_file_list_to_string:
+ * thunar_g_file_list_to_stringv:
  * @list : a list of #GFile<!---->s.
  *
- * Free the returned value using g_free() when you
- * are done with it.
+ * Free the returned value using g_strfreev() when you
+ * are done with it. Useful for gtk_selection_data_set_uris.
  *
- * Return value: the string representation of @list conforming to the
- *               text/uri-list mime type defined in RFC 2483.
+ * Return value: and array of uris.
  **/
-gchar *
-thunar_g_file_list_to_string (GList *list)
+gchar **
+thunar_g_file_list_to_stringv (GList *list)
 {
-  GString *string;
-  gchar   *uri;
-  GList   *lp;
+  gchar **uris;
+  guint   n;
+  GList  *lp;
 
   /* allocate initial string */
-  string = g_string_new (NULL);
-
-  for (lp = list; lp != NULL; lp = lp->next)
-    {
-      uri = g_file_get_uri (lp->data);
-      string = g_string_append (string, uri);
-      g_free (uri);
+  uris = g_new0 (gchar *, g_list_length (list) + 1);
 
-      if (lp->next != NULL)
-        string = g_string_append (string, "\r\n");
-    }
+  for (lp = list, n = 0; lp != NULL; lp = lp->next)
+    uris[n++] = g_file_get_uri (G_FILE (lp->data));
 
-  return g_string_free (string, FALSE);
+  return uris;
 }
 
 
diff --git a/thunar/thunar-gio-extensions.h b/thunar/thunar-gio-extensions.h
index 51fd75a..18b6f7e 100644
--- a/thunar/thunar-gio-extensions.h
+++ b/thunar/thunar-gio-extensions.h
@@ -59,6 +59,7 @@ gboolean  thunar_g_vfs_is_uri_scheme_supported   (const gchar          *scheme);
 GType     thunar_g_file_list_get_type               (void);
 
 GList    *thunar_g_file_list_new_from_string        (const gchar       *string);
+gchar   **thunar_g_file_list_to_stringv             (GList             *list);
 gchar    *thunar_g_file_list_to_string              (GList             *list);
 GList    *thunar_g_file_list_append                 (GList             *list,
                                                      GFile             *file);
diff --git a/thunar/thunar-location-button.c b/thunar/thunar-location-button.c
index 5ffd83e..50a7ead 100644
--- a/thunar/thunar-location-button.c
+++ b/thunar/thunar-location-button.c
@@ -634,8 +634,8 @@ thunar_location_button_drag_data_get (GtkWidget            *button,
                                       guint                 timestamp,
                                       ThunarLocationButton *location_button)
 {
-  gchar *uri_string;
-  GList  path_list;
+  gchar **uris;
+  GList   path_list;
 
   _thunar_return_if_fail (GTK_IS_WIDGET (button));
   _thunar_return_if_fail (THUNAR_IS_LOCATION_BUTTON (location_button));
@@ -644,14 +644,13 @@ thunar_location_button_drag_data_get (GtkWidget            *button,
   if (G_LIKELY (location_button->file != NULL))
     {
       /* transform the path into an uri list string */
-      path_list.data = thunar_file_get_file (location_button->file); path_list.next = path_list.prev = NULL;
-      uri_string = thunar_g_file_list_to_string (&path_list);
+      path_list.next = path_list.prev = NULL;
+      path_list.data = thunar_file_get_file (location_button->file);
 
       /* set the uri list for the drag selection */
-      gtk_selection_data_set (selection_data, selection_data->target, 8, (guchar *) uri_string, strlen (uri_string));
-
-      /* cleanup */
-      g_free (uri_string);
+      uris = thunar_g_file_list_to_stringv (&path_list);
+      gtk_selection_data_set_uris (selection_data, uris);
+      g_strfreev (uris);
     }
 }
 
diff --git a/thunar/thunar-path-entry.c b/thunar/thunar-path-entry.c
index 8f4624e..7167b83 100644
--- a/thunar/thunar-path-entry.c
+++ b/thunar/thunar-path-entry.c
@@ -716,22 +716,21 @@ thunar_path_entry_drag_data_get (GtkWidget        *widget,
                                  guint             info,
                                  guint             timestamp)
 {
-  ThunarPathEntry *path_entry = THUNAR_PATH_ENTRY (widget);
-  GList            file_list;
-  gchar           *uri_string;
+  ThunarPathEntry  *path_entry = THUNAR_PATH_ENTRY (widget);
+  GList             file_list;
+  gchar           **uris;
 
   /* verify that we actually display a path */
   if (G_LIKELY (path_entry->current_file != NULL))
     {
       /* transform the path for the current file into an uri string list */
-      file_list.data = thunar_file_get_file (path_entry->current_file); file_list.next = file_list.prev = NULL;
-      uri_string = thunar_g_file_list_to_string (&file_list);
+      file_list.next = file_list.prev = NULL;
+      file_list.data = thunar_file_get_file (path_entry->current_file);
 
       /* setup the uri list for the drag selection */
-      gtk_selection_data_set (selection_data, selection_data->target, 8, (guchar *) uri_string, strlen (uri_string));
-
-      /* clean up */
-      g_free (uri_string);
+      uris = thunar_g_file_list_to_stringv (&file_list);
+      gtk_selection_data_set_uris (selection_data, uris);
+      g_strfreev (uris);
     }
 }
 
diff --git a/thunar/thunar-standard-view.c b/thunar/thunar-standard-view.c
index 91b290d..6a90e41 100644
--- a/thunar/thunar-standard-view.c
+++ b/thunar/thunar-standard-view.c
@@ -2982,12 +2982,15 @@ thunar_standard_view_drag_data_get (GtkWidget          *view,
                                     guint               timestamp,
                                     ThunarStandardView *standard_view)
 {
-  gchar *uri_string;
+  gchar **uris;
 
   /* set the URI list for the drag selection */
-  uri_string = thunar_g_file_list_to_string (standard_view->priv->drag_g_file_list);
-  gtk_selection_data_set (selection_data, selection_data->target, 8, (guchar *) uri_string, strlen (uri_string));
-  g_free (uri_string);
+  if (standard_view->priv->drag_g_file_list != NULL)
+    {
+      uris = thunar_g_file_list_to_stringv (standard_view->priv->drag_g_file_list);
+      gtk_selection_data_set_uris (selection_data, uris);
+      g_strfreev (uris);
+    }
 }
 
 


More information about the Xfce4-commits mailing list