[mistelix] Allow draging files from other applications to the slideshow view



commit 076b56515260b937f2bcdc087094c0bad19d4754
Author: Jordi Mas <jmas softcatala org>
Date:   Fri Aug 14 22:02:37 2009 +0200

    Allow draging files from other applications to the slideshow view

 src/DataModel/PathList.cs         |   23 +++++++++
 src/Widgets/SlideShowImageView.cs |   99 ++++++++++++++++++++++++++++++++-----
 2 files changed, 109 insertions(+), 13 deletions(-)
---
diff --git a/src/DataModel/PathList.cs b/src/DataModel/PathList.cs
index ab67937..33ea131 100644
--- a/src/DataModel/PathList.cs
+++ b/src/DataModel/PathList.cs
@@ -45,6 +45,29 @@ namespace Mistelix.DataModel
 				Add (items[i]);
 		}
 
+		// Drag & Drop from outside our application
+		public void FromExternalString (string data) 
+		{
+			string [] items = data.Split ('\n');
+			
+			foreach (String str in items)
+			{
+				string final;
+
+				if (str.StartsWith ("#") == true)
+					continue;
+
+				if (str.EndsWith ("\r")) {
+					final = str.Substring (0, str.Length - 1);
+				}
+				else 
+					final = str;
+
+				if (final.Length > 0)
+					Add (final);
+			}
+		}
+
 		public override string ToString () 
 		{
 			StringBuilder sb = new StringBuilder (Count * 128);
diff --git a/src/Widgets/SlideShowImageView.cs b/src/Widgets/SlideShowImageView.cs
index 7e26ae3..7d652d5 100644
--- a/src/Widgets/SlideShowImageView.cs
+++ b/src/Widgets/SlideShowImageView.cs
@@ -82,7 +82,9 @@ namespace Mistelix.Widgets
 		public const string EFFECT = "effect";
 
 		static TargetEntry [] tag_dest_target_table = new TargetEntry [] {
-					new TargetEntry ("application/x-mistelix-img", 0, (uint) 2)};
+			new TargetEntry ("text/uri-list", 0, (uint) Gtk.TargetFlags.App),
+			new TargetEntry ("application/x-mistelix-img", 0, (uint) 2)
+		};
 
 		public ShowImageSelectionEventHandler 		ChangeEvent;
 		public ShowImageUpdatedElementsEventHandler	UpdatedElements;
@@ -102,7 +104,7 @@ namespace Mistelix.Widgets
 			DragDrop += new DragDropHandler (HandleTargetDragDrop);
 			DragDataReceived += new DragDataReceivedHandler (HandleTargetDragDataReceived);
 			Gtk.Drag.DestSet (this, DestDefaults.All, tag_dest_target_table,
-					  DragAction.Copy | DragAction.Move );
+					  DragAction.Copy | DragAction.Move);
 
 			CursorChanged += OnCursorChanged;
 			thumbnailing = new BackgroundWorker ();
@@ -237,33 +239,106 @@ namespace Mistelix.Widgets
 		// Processing of dropped files from the FileView is done here
 		void HandleTargetDragDataReceived (object sender, DragDataReceivedArgs args)
 		{
+			bool external = Gtk.Drag.GetSourceWidget (args.Context) == null; // null if the drag is from outside the app
+			string data = System.Text.Encoding.UTF8.GetString (args.SelectionData.Data);
+
 			Logger.Debug ("ImageView.HandleTargetDragDataReceived");
 
+			Gdk.Cursor watch = new Gdk.Cursor (Gdk.CursorType.Watch);
+			GdkWindow.Cursor = watch;
+			GdkWindow.Display.Sync ();
+
 			if (paint_control == false) {
 				CreateColumns ();
 				paint_control = true;
 			}			
 
 			PathList list = new PathList ();
-			list.FromString (System.Text.Encoding.UTF8.GetString (args.SelectionData.Data));
+		
+			if (external == true)
+				list.FromExternalString (System.Text.Encoding.UTF8.GetString (args.SelectionData.Data));
+			else
+				list.FromString (System.Text.Encoding.UTF8.GetString (args.SelectionData.Data));
+					
 			foreach (string file in list)
-				LoadFile (file);
+				LoadFile (file, external);
 
 			args.RetVal = true;
 			UpdateButtonSensitivity ();
 			Gtk.Drag.Finish (args.Context, true, false, args.Time);
 
+			// Remove the cursor when the importing process is completed
+			if (external == true) {
+				Task task;
+				task = new Task (this);
+				task.DoEventHandler += delegate
+				{
+					Application.Invoke (delegate 
+					{
+						GdkWindow.Cursor = null;
+						QueueDraw ();
+					});
+				};
+				dispatcher.AddTask (task);
+			} else
+				GdkWindow.Cursor = null;
+
 			if (thumbnailing.IsBusy == false)
 				thumbnailing.RunWorkerAsync (store);
 		}
 
 		// Adds a new dropped file into the view
-		void LoadFile (string file)
+		void LoadFile (string file, bool external)
+		{
+			FileInfo fi;
+
+			if (external == false) {
+				fi = new FileInfo (file);
+				store.AppendValues (next_id.ToString (), null, notitle,
+					new SlideImage (fi.FullName));
+				next_id++;
+				return;
+			}
+
+			Task task;
+			task = new Task (file);
+			task.DoEventHandler += AddElement;
+			dispatcher.AddTask (task);
+		}
+
+		// Element loading from dropped files is not asyncronous
+		void AddElement (object obj, EventArgs args)
 		{
-			FileInfo fi = new FileInfo (file);
-			store.AppendValues (next_id.ToString (), null, notitle,
-				new SlideImage (fi.FullName));
-			next_id++;
+			DataImageSurface cairo_image;
+			SlideImage image;
+			Uri uri;
+			Task item;
+			string filename;
+			FileInfo fi;
+
+			try 
+			{
+				item = (Task) obj;
+				filename = (string) item.Data;
+				uri = new Uri (filename);
+
+				filename = uri.AbsolutePath;
+				fi = new FileInfo (filename);
+				image = new SlideImage (filename);
+
+				cairo_image = image.GetThumbnail (thumbnail_width, thumbnail_height);
+
+				Application.Invoke (delegate 
+				{
+					store.AppendValues (next_id.ToString (), cairo_image, notitle, image);
+				});
+				
+				next_id++;
+			}
+			catch (Exception e)
+			{
+				Logger.Error ("SlideShowImageView.AddElement. Exception {0}", e);
+			}
 		}
 
 		void HandleDropBegin (object sender, DragBeginArgs a)
@@ -288,7 +363,7 @@ namespace Mistelix.Widgets
 
 		protected override bool OnExposeEvent (Gdk.EventExpose args)
 		{
-			if (paint_control == true) 
+			if (paint_control == true)
 				return base.OnExposeEvent (args);
 			
 			// See: http://www.mail-archive.com/mono-list lists ximian com/msg26065.html
@@ -299,7 +374,7 @@ namespace Mistelix.Widgets
 
 				Pango.Layout layout = new Pango.Layout (this.PangoContext);
 				layout.Width = winWidth * (int) Pango.Scale.PangoScale;
-				layout.SetMarkup (Catalog.GetString ("Drag your images here to add them to the slideshow. You can also use the contextual menu to sort the images."));
+				layout.SetMarkup (Catalog.GetString ("Drag your images here from the right pane or from other applications. You can also use the contextual menu to sort the images."));
 				layout.GetPixelSize (out w, out h);
 
 				Gdk.GC light = Style.DarkGC (StateType.Normal);
@@ -428,8 +503,6 @@ namespace Mistelix.Widgets
 			foreach (TreePath path in paths)
 			{
 				store.GetIter (out iter, path);
-				//image = (SlideImage) model.GetValue (iter, SlideShowImageView.COL_OBJECT);
-				//image.time = spinner.ValueAsInt;
 				store.Swap (prev, iter);
 			}
 		}



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