[f-spot/icon-view-cleanup] Fix Copy to Clipboard



commit 9cb2291c6c8b2d79987b906ca04a94c119ef9823
Author: Mike Gemünde <mike gemuende de>
Date:   Mon Aug 16 14:15:53 2010 +0200

    Fix Copy to Clipboard
    
    The SetUris method of SelectionData seems to not work correctly since
    it gets a string but its native pendant gets a string array. This commits
    works around it.

 src/Clients/MainApp/FSpot/MainWindow.cs            |   52 ++++++++++++--------
 .../MainApp/FSpot/SelectionDataExtensions.cs       |   14 +++++
 2 files changed, 45 insertions(+), 21 deletions(-)
---
diff --git a/src/Clients/MainApp/FSpot/MainWindow.cs b/src/Clients/MainApp/FSpot/MainWindow.cs
index bf3c66f..b5097a2 100644
--- a/src/Clients/MainApp/FSpot/MainWindow.cs
+++ b/src/Clients/MainApp/FSpot/MainWindow.cs
@@ -9,6 +9,7 @@
 using System;
 using System.Text;
 using System.Linq;
+using System.IO;
 using System.Collections;
 using System.Collections.Generic;
 
@@ -2288,30 +2289,39 @@ namespace FSpot
 				return;
 			}
 
-			clipboard.SetWithData (new TargetEntry[] {
-						DragDropTargets.PlainTextEntry,
-						DragDropTargets.UriListEntry,
-						DragDropTargets.CopyFilesEntry,},
-					delegate (Clipboard clip, SelectionData data, uint info) {
-						var paths = new List<string> ();
-						var uris = new List<string> ();
-						foreach (Photo p in SelectedPhotos ()) {
-							paths.Add (System.IO.Path.GetFullPath (p.DefaultVersion.Uri.LocalPath));
-							uris.Add (p.DefaultVersion.Uri.ToString ());
-						}
-						data.Text = String.Join (" ", paths.ToArray ());
-						data.SetUris (String.Join (" ", uris.ToArray ()));
-						data.Set (Atom.Intern ("x-special/gnome-copied-files", true), 8, System.Text.Encoding.UTF8.GetBytes ("copy\n" + String.Join ("\n", uris.ToArray ())));
+            var target_entries = new TargetEntry[] {
+                DragDropTargets.PlainTextEntry,
+                DragDropTargets.UriListEntry,
+                DragDropTargets.CopyFilesEntry};
 
-					},
-					delegate {});
+            // use eager evaluation, because we want to copy the photos which are currently selected ...
+            var uris = new UriList (from p in SelectedPhotos () select p.DefaultVersion.Uri);
+            var paths = String.Join (" ",
+                                     (from p in SelectedPhotos ()
+                                      select Path.GetFullPath (p.DefaultVersion.Uri)).ToArray ()
+                                     );
 
-			var pt = new List<string> ();
-			foreach (Photo p in SelectedPhotos ()) {
-				pt.Add (System.IO.Path.GetFullPath (p.DefaultVersion.Uri.LocalPath));
-			}
+            clipboard.SetWithData (target_entries, delegate (Clipboard clip, SelectionData data, uint info) {
+
+                if (info == DragDropTargets.PlainTextEntry.Info) {
+                    data.Text = paths;
+                    return;
+                }
+
+                if (info == DragDropTargets.UriListEntry.Info) {
+                     data.SetUriListData (uris);
+                    return;
+                }
+
+                if (info == DragDropTargets.CopyFilesEntry.Info) {
+                    data.SetCopyFiles (uris);
+                    return;
+                }
+
+                Log.DebugFormat ("Unknown Selection Data Target (info: {0})", info);
+            }, delegate {});
 
-			primary.Text = String.Join (" ", pt.ToArray ());
+			primary.Text = paths;
 		}
 
 		void HandleSetAsBackgroundCommand (object sender, EventArgs args)
diff --git a/src/Clients/MainApp/FSpot/SelectionDataExtensions.cs b/src/Clients/MainApp/FSpot/SelectionDataExtensions.cs
index 718e62b..165e091 100644
--- a/src/Clients/MainApp/FSpot/SelectionDataExtensions.cs
+++ b/src/Clients/MainApp/FSpot/SelectionDataExtensions.cs
@@ -10,6 +10,7 @@
 
 using System;
 using System.Text;
+using System.Linq;
 
 using Gtk;
 using Gdk;
@@ -109,9 +110,22 @@ namespace FSpot
 			selection_data.Set (target, 8, data, data.Length);
 		}
 
+        public static void SetUriListData (this SelectionData selection_data, UriList uri_list)
+        {
+            selection_data.SetUriListData (uri_list, Atom.Intern ("text/uri-list", true));
+        }
+
 		public static UriList GetUriListData (this SelectionData selection_data)
 		{
 			return new UriList (GetStringData (selection_data));
 		}
+
+        public static void SetCopyFiles (this SelectionData selection_data, UriList uri_list)
+        {
+            var uris = (from p in uri_list select p.ToString ()).ToArray ();
+            var data = Encoding.UTF8.GetBytes ("copy\n" + String.Join ("\n", uris));
+
+            selection_data.Set (Atom.Intern ("x-special/gnome-copied-files", true), 8, data, data.Length);
+        }
 	}
 }



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