[f-spot/replace-import] More fixes, now attaches tags on import.



commit fd1b71dc03d4c795e148ab70a4f4e4fe791c2bb3
Author: Ruben Vermeersch <ruben savanne be>
Date:   Sat Jun 5 20:18:08 2010 +0200

    More fixes, now attaches tags on import.

 src/Core/App.cs                |    1 +
 src/Core/Photo.cs              |    2 +-
 src/Import/FileImportSource.cs |   10 +++
 src/Import/ImportController.cs |   48 ++++++++++++-
 src/ImportCommand.cs           |  163 ----------------------------------------
 src/MainWindow.cs              |   13 +---
 src/UI.Dialog/ImportDialog.cs  |   14 +++-
 7 files changed, 71 insertions(+), 180 deletions(-)
---
diff --git a/src/Core/App.cs b/src/Core/App.cs
index 550aff4..ccd1328 100644
--- a/src/Core/App.cs
+++ b/src/Core/App.cs
@@ -169,6 +169,7 @@ namespace FSpot
 #endregion
 
 #region Command Handlers
+
 		void HandleMessageReceived (object sender, MessageReceivedArgs e)
 		{
 			switch ((Command)e.Command) {
diff --git a/src/Core/Photo.cs b/src/Core/Photo.cs
index 8830bdb..aa723d0 100644
--- a/src/Core/Photo.cs
+++ b/src/Core/Photo.cs
@@ -461,7 +461,7 @@ namespace FSpot
 				AddTagUnsafely (tag);
 		}
 	
-		public void AddTag (Tag []taglist)
+		public void AddTag (IEnumerable<Tag> taglist)
 		{
 			/*
 			 * FIXME need a better naming convention here, perhaps just
diff --git a/src/Import/FileImportSource.cs b/src/Import/FileImportSource.cs
index feade92..3cb26b7 100644
--- a/src/Import/FileImportSource.cs
+++ b/src/Import/FileImportSource.cs
@@ -3,6 +3,7 @@ using System;
 using System.Threading;
 using System.Collections.Generic;
 using FSpot.Utils;
+using Gtk;
 
 namespace FSpot.Import
 {
@@ -76,6 +77,15 @@ namespace FSpot.Import
         {
             if (PhotoScanner != null) {
                 run_photoscanner = false;
+                PhotoScanner.Join ();
+
+                // Make sure all photos are added. This is needed to prevent
+                // a race condition where a source is deactivated, yet photos
+                // are still added to the collection because they are
+                // queued on the mainloop.
+                while (Application.EventsPending ())
+                    Application.RunIteration (false);
+
                 PhotoScanner = null;
             }
         }
diff --git a/src/Import/ImportController.cs b/src/Import/ImportController.cs
index cfcba0a..3ea4057 100644
--- a/src/Import/ImportController.cs
+++ b/src/Import/ImportController.cs
@@ -3,6 +3,7 @@ using FSpot.Utils;
 using System;
 using System.Collections.Generic;
 using System.Threading;
+using Mono.Unix;
 
 namespace FSpot.Import
 {
@@ -266,7 +267,19 @@ namespace FSpot.Import
                                       item.DefaultVersion.Uri,
                                       roll.Id);
 
-            // FIXME: Add tags, import xmp crap
+            bool needs_commit = false;
+
+            // Add tags
+            if (attach_tags.Count > 0) {
+                photo.AddTag (attach_tags);
+                needs_commit = true;
+            }
+
+            if (needs_commit) {
+                store.Commit (photo);
+            }
+
+            // FIXME: import xmp crap
             imported_photos.Add (photo.Id);
         }
 
@@ -322,5 +335,38 @@ namespace FSpot.Import
 
 #endregion
 
+#region Tagging
+
+        List<Tag> attach_tags = new List<Tag> ();
+        TagStore tag_store = App.Instance.Database.Tags;
+
+        // Set the tags that will be added on import.
+        public void AttachTags (IEnumerable<string> tags)
+        {
+            App.Instance.Database.BeginTransaction ();
+            var import_category = GetImportedTagsCategory ();
+            foreach (var tagname in tags) {
+                var tag = tag_store.GetTagByName (tagname);
+                if (tag == null) {
+                    tag = tag_store.CreateCategory (import_category, tagname, false) as Tag;
+                    tag_store.Commit (tag);
+                }
+                attach_tags.Add (tag);
+            }
+            App.Instance.Database.CommitTransaction ();
+        }
+
+        Category GetImportedTagsCategory ()
+        {
+            var default_category = tag_store.GetTagByName (Catalog.GetString ("Imported Tags")) as Category;
+            if (default_category == null) {
+                default_category = tag_store.CreateCategory (null, Catalog.GetString ("Imported Tags"), false);
+                default_category.ThemeIconName = "gtk-new"; 
+            }
+            return default_category;
+        }
+
+#endregion
+
     }
 }
diff --git a/src/ImportCommand.cs b/src/ImportCommand.cs
index bdb7f23..af3c50a 100644
--- a/src/ImportCommand.cs
+++ b/src/ImportCommand.cs
@@ -40,40 +40,6 @@ public class ImportCommand : GladeDialog
 	
 	private SourceItem Source;
 
-	private bool Step ()
-	{	
-		StepStatusInfo status_info;		
-		bool ongoing = true;
-
-		if (importer == null)
-			return false;
-		
-		try {
-			// FIXME this is really just an incredibly ugly way of dealing
-			// with the recursive DoImport loops we sometimes get into
-			ongoing = importer.Step (out status_info);
-		} catch (ImportException e){
-			Hyena.Log.Exception (e);
-			return false;
-		}
-
-		if (status_info.Count > 0 && status_info.Count % 25 == 0)
-			System.GC.Collect ();
-
-		if (status_info.Count < total)
-			UpdateProgressBar (status_info.Count + 1, total);
-
-		if (ongoing && total > 0)
-			return true;
-		else {
-			Hyena.Log.Debug ("Stopping");
-			if (progress_bar != null)
-				progress_bar.Text = Catalog.GetString ("Done Loading");
-			
-			AllowFinish = true;
-			return false;
-		}
-	}
 
 	private int DoImport (ImportBackend imp)
 	{
@@ -107,73 +73,6 @@ public class ImportCommand : GladeDialog
 
 	public int ImportFromUri (PhotoStore store, SafeUri uri)
 	{
-		this.store = store;
-		this.CreateDialog ("import_dialog");
-		this.Dialog.TransientFor = main_window;
-		this.Dialog.WindowPosition = Gtk.WindowPosition.CenterOnParent;
-		this.Dialog.Response += HandleDialogResponse;
-		this.Dialog.DefaultResponse = ResponseType.Ok;
-
-		AllowFinish = false;
-		
-		LoadPreferences ();
-		
-		//import_folder_entry.Activated += HandleEntryActivate;
-		duplicate_check.Toggled += HandleRecurseToggled;
-		recurse_check.Toggled += HandleRecurseToggled;
-		copy_check.Toggled += HandleRecurseToggled;
-
-		menu = new SourceMenu (this);
-		source_option_menu.Menu = menu;
-
-		collection = new FSpot.PhotoList (new Photo [0]);
-		tray = new ScalingIconView (collection);
-		tray.Selection.Changed += HandleTraySelectionChanged;
-		icon_scrolled.SetSizeRequest (400, 200);
-		icon_scrolled.Add (tray);
-		//icon_scrolled.Visible = false;
-		tray.DisplayTags = false;
-		tray.Show ();
-
-		photo_view = new PhotoImageView (collection);
-		photo_scrolled.Add (photo_view);
-		photo_scrolled.SetSizeRequest (200, 200);
-		photo_view.Show ();
-
-		//GtkUtil.ModifyColors (frame_eventbox);
-		GtkUtil.ModifyColors (photo_scrolled);
-		GtkUtil.ModifyColors (photo_view);
-
-		photo_view.Pixbuf = GtkUtil.TryLoadIcon (FSpot.Global.IconTheme, "f-spot", 128, (Gtk.IconLookupFlags)0);
-		photo_view.ZoomFit (false);
-			
-		tag_entry = new FSpot.Widgets.TagEntry (App.Instance.Database.Tags, false);
-		tag_entry.UpdateFromTagNames (new string []{});
-		tagentry_box.Add (tag_entry);
-
-		tag_entry.Show ();
-
-		this.Dialog.Show ();
-        progress_bar.Hide ();
-
-		source_option_menu.Changed += menu.HandleSourceSelectionChanged;
-		if (uri != null) {
-			ImportUri = uri;
-			int i = menu.FindItemPosition (uri);
-
-			var file = FileFactory.NewForUri (uri);
-
-			if (i > 0) {
-				source_option_menu.SetHistory ((uint)i);
-			} else if (file.QueryExists (null)) {
-				SourceItem uri_item = new SourceItem (new VfsSource (uri));
-				menu.Prepend (uri_item);
-				uri_item.ShowAll ();
-				ImportUri = uri;
-				source_option_menu.SetHistory (0);
-			} 
-			idle_start.Start ();
-		}
 						
 		ResponseType response = (ResponseType) this.Dialog.Run ();
 		
@@ -200,67 +99,5 @@ public class ImportCommand : GladeDialog
 
 			response = (Gtk.ResponseType) this.Dialog.Run ();
 		}
-
-		if (response == ResponseType.Ok) {
-			this.UpdateTagStore (tag_entry.GetTypedTagNames ());
-			SavePreferences ();
-			this.Finish ();
-
-			if (tags_selected != null && tags_selected.Count > 0) {
-				for (int i = 0; i < collection.Count; i++) {
-					Photo p = collection [i] as Photo;
-					
-					if (p == null)
-						continue;
-					
-					p.AddTag ((Tag [])tags_selected.ToArray(typeof(Tag)));
-					store.Commit (p);
-				}
-			}
-
-			int width, height;
-			this.Dialog.GetSize (out width, out height);
-
-			FSpot.Preferences.Set (FSpot.Preferences.IMPORT_WINDOW_WIDTH, width);
-			FSpot.Preferences.Set (FSpot.Preferences.IMPORT_WINDOW_HEIGHT, height);
-			FSpot.Preferences.Set (FSpot.Preferences.IMPORT_WINDOW_PANE_POSITION, import_hpaned.Position);
-
-			this.Dialog.Destroy ();
-			return collection.Count;
-		} else {
-			this.Cancel ();
-			//this.Dialog.Destroy();
-			return 0;
-		}
-	}
-
-	private void UpdateTagStore (string [] new_tags)
-	{
-		if (new_tags == null || new_tags.Length == 0)
-			return;
-
-		tags_selected = new ArrayList ();
-		Db db = App.Instance.Database;	
-		db.BeginTransaction ();
-		foreach (string tagname in new_tags) {
-			Tag t = db.Tags.GetTagByName (tagname);
-			if (t == null) {
-				Category default_category = db.Tags.GetTagByName (Catalog.GetString ("Imported Tags")) as Category;
-				if (default_category == null) {
-					default_category = db.Tags.CreateCategory (null, Catalog.GetString ("Imported Tags"), false);
-					default_category.ThemeIconName = "gtk-new"; 
-				}
-				t = db.Tags.CreateCategory (default_category, tagname, false) as Tag;
-				db.Tags.Commit (t);
-			}
-
-			tags_selected.Add (t);
-		}
-		db.CommitTransaction ();
-		
-		ArrayList tagnames = new ArrayList ();
-		foreach (Tag t in tags_selected)
-			tagnames.Add (t.Name);
-		tag_entry.UpdateFromTagNames ((string [])tagnames.ToArray(typeof(string)));
 	}
 }
diff --git a/src/MainWindow.cs b/src/MainWindow.cs
index 19944c6..7d0c492 100644
--- a/src/MainWindow.cs
+++ b/src/MainWindow.cs
@@ -42,8 +42,6 @@ namespace FSpot
 		}
 	
 		[GtkBeans.Builder.Object] Gtk.HPaned main_hpaned;
-		[GtkBeans.Builder.Object] Gtk.VBox left_vbox;
-		[GtkBeans.Builder.Object] Gtk.VBox group_vbox;
 		[GtkBeans.Builder.Object] Gtk.VBox view_vbox;
 	
 		[GtkBeans.Builder.Object] Gtk.VBox toolbar_vbox;
@@ -193,13 +191,6 @@ namespace FSpot
 				DragDropTargets.TagListEntry
 		};
 		
-		private static TargetEntry [] tag_dest_target_table = 
-			new TargetEntry [] {
-				DragDropTargets.PhotoListEntry,
-				DragDropTargets.UriListEntry,
-				DragDropTargets.TagListEntry
-		};
-	
 		const int PHOTO_IDX_NONE = -1;
 	
 		private static Gtk.Tooltips toolTips;
@@ -1159,7 +1150,7 @@ namespace FSpot
 			controller.DuplicateDetect = true;
 			controller.RecurseSubdirectories = true;
 
-			var import_window = new ImportDialog (controller);
+			var import_window = new ImportDialog (controller, Window);
 			import_window.Show ();
 
 			controller.StartImport ();
@@ -1181,7 +1172,7 @@ namespace FSpot
 					}
 				}
 			};
-			var import_window = new ImportDialog (controller);
+			var import_window = new ImportDialog (controller, Window);
 			import_window.Show ();
 		}
 	
diff --git a/src/UI.Dialog/ImportDialog.cs b/src/UI.Dialog/ImportDialog.cs
index 41c11a4..f4c87db 100644
--- a/src/UI.Dialog/ImportDialog.cs
+++ b/src/UI.Dialog/ImportDialog.cs
@@ -31,19 +31,23 @@ namespace FSpot.UI.Dialog
         [GtkBeans.Builder.Object] ScrolledWindow photo_scrolled;
 
         private PhotoImageView photo_view;
+        private TagEntry tag_entry;
 
-        public ImportDialog (ImportController controller) : base ("import.ui", "import_dialog")
+        public ImportDialog (ImportController controller, Window parent) : base ("import.ui", "import_dialog")
         {
             Controller = controller;
-            BuildUI ();
+            BuildUI (parent);
             ResetPreview ();
             LoadPreferences ();
             ScanSources ();
             ConnectEvents ();
         }
 
-        void BuildUI ()
+        void BuildUI (Window parent)
         {
+            TransientFor = parent;
+            WindowPosition = WindowPosition.CenterOnParent;
+
             photo_view = new PhotoImageView (Controller.Photos);
             photo_scrolled.Add (photo_view);
             photo_scrolled.SetSizeRequest (200, 200);
@@ -65,7 +69,7 @@ namespace FSpot.UI.Dialog
 
             import_button.Sensitive = false;
 
-            var tag_entry = new FSpot.Widgets.TagEntry (App.Instance.Database.Tags, false);
+            tag_entry = new TagEntry (App.Instance.Database.Tags, false);
             tag_entry.UpdateFromTagNames (new string []{});
             tagentry_box.Add (tag_entry);
             tag_entry.Show ();
@@ -107,6 +111,7 @@ namespace FSpot.UI.Dialog
 
             GLib.Idle.Add (() => {
                 PopulateSourceCombo (null);
+                QueueDraw ();
                 return false;
             });
         }
@@ -265,6 +270,7 @@ namespace FSpot.UI.Dialog
 
         void StartImport ()
         {
+            Controller.AttachTags (tag_entry.GetTypedTagNames ());
             Controller.StartImport ();
         }
 



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