[f-spot] Change PhotoVersion such that it creates photos from IBrowsableItems.



commit ea7fafe22347d785f1c7ec875cab111306b89286
Author: Ruben Vermeersch <ruben savanne be>
Date:   Sat Jun 12 19:01:22 2010 +0200

    Change PhotoVersion such that it creates photos from IBrowsableItems.

 extensions/Tools/MergeDb/MergeDb.cs       |   25 +++++++--------
 extensions/Tools/MergeDb/MergeDbDialog.cs |    1 -
 src/Core/IBrowsableItem.cs                |    2 -
 src/Core/IBrowsableItemVersion.cs         |    2 +-
 src/Core/PhotoVersion.cs                  |   46 ++++++++++++++++------------
 src/FileBrowsableItem.cs                  |    9 +++++-
 src/Import/ImportController.cs            |    9 ++----
 src/Import/ImportSource.cs                |   17 +++++++++-
 src/PhotoStore.cs                         |   29 ++++++------------
 src/PhotoVersionCommands.cs               |    2 +-
 10 files changed, 76 insertions(+), 66 deletions(-)
---
diff --git a/extensions/Tools/MergeDb/MergeDb.cs b/extensions/Tools/MergeDb/MergeDb.cs
index 85effb6..b1a4c66 100644
--- a/extensions/Tools/MergeDb/MergeDb.cs
+++ b/extensions/Tools/MergeDb/MergeDb.cs
@@ -188,7 +188,6 @@ namespace MergeDbExtension
 		void ImportPhoto (Photo photo, bool copy)
 		{
 			Log.WarningFormat ("Importing {0}", photo.Name);
-			PhotoStore from_store = from_db.Photos;
 			PhotoStore to_store = to_db.Photos;
 
 			string photo_path = photo.VersionUri (Photo.OriginalVersionId).AbsolutePath;
@@ -232,19 +231,21 @@ namespace MergeDbExtension
 			Photo newp;
 
 			if (copy)
-				destination = FindImportDestination (new SafeUri (photo_path)).AbsolutePath;
+				destination = FindImportDestination (new SafeUri (photo_path), photo.Time).AbsolutePath;
 			else
 				destination = photo_path;
+			var dest_uri = new SafeUri (photo_path);
 
-            var hash = Photo.GenerateMD5 (new SafeUri (photo_path));
+			photo.DefaultVersionId = 1;
+			photo.DefaultVersion.Uri = dest_uri;
 
-			// Don't copy if we are already home
-			if (photo_path == destination)
-				newp = to_store.Create (new SafeUri (destination), roll_map [photo.RollId], hash);
-			else {
+			if (photo.DefaultVersion.ImportMD5 == String.Empty) {
+				(photo.DefaultVersion as PhotoVersion).ImportMD5 = Photo.GenerateMD5 (photo.DefaultVersion.Uri);
+			}
+
+			if (photo_path != destination) {
 				System.IO.File.Copy (photo_path, destination);
 
-				newp = to_store.Create (new SafeUri (destination), new SafeUri (photo_path), roll_map [photo.RollId], hash);
 				try {
 					File.SetAttributes (destination, File.GetAttributes (destination) & ~FileAttributes.ReadOnly);
 					DateTime create = File.GetCreationTime (photo_path);
@@ -256,6 +257,8 @@ namespace MergeDbExtension
 				}
 			}
 
+			newp = to_store.CreateFrom (photo, roll_map [photo.RollId]);
+
 			if (newp == null)
 				return;
 
@@ -280,14 +283,10 @@ namespace MergeDbExtension
 			to_store.Commit (newp);
 		}
 
-        SafeUri FindImportDestination (SafeUri uri)
+        SafeUri FindImportDestination (SafeUri uri, DateTime time)
         {
             // Find a new unique location inside the photo folder
             string name = uri.GetFilename ();
-            DateTime time;
-            using (FSpot.ImageFile img = FSpot.ImageFile.Create (uri)) {
-                time = img.Date;
-            }
 
             var dest_uri = FSpot.Global.PhotoUri.Append (time.Year.ToString ())
                                           .Append (String.Format ("{0:D2}", time.Month))
diff --git a/extensions/Tools/MergeDb/MergeDbDialog.cs b/extensions/Tools/MergeDb/MergeDbDialog.cs
index 7efbf2d..e2bcf0b 100644
--- a/extensions/Tools/MergeDb/MergeDbDialog.cs
+++ b/extensions/Tools/MergeDb/MergeDbDialog.cs
@@ -17,7 +17,6 @@ namespace MergeDbExtension
 	{
 		[Glade.Widget] Gtk.Dialog mergedb_dialog;
 		[Glade.Widget] Gtk.Button apply_button;
-		[Glade.Widget] Gtk.Button cancel_button;
 		[Glade.Widget] Gtk.FileChooserButton db_filechooser;
 		[Glade.Widget] Gtk.RadioButton newrolls_radio;
 		[Glade.Widget] Gtk.RadioButton allrolls_radio;
diff --git a/src/Core/IBrowsableItem.cs b/src/Core/IBrowsableItem.cs
index 72ce751..ab9ecfc 100644
--- a/src/Core/IBrowsableItem.cs
+++ b/src/Core/IBrowsableItem.cs
@@ -34,6 +34,4 @@ namespace FSpot
 			get; 
 		}
 	}
-
-
 }
diff --git a/src/Core/IBrowsableItemVersion.cs b/src/Core/IBrowsableItemVersion.cs
index 366716f..345172a 100644
--- a/src/Core/IBrowsableItemVersion.cs
+++ b/src/Core/IBrowsableItemVersion.cs
@@ -9,7 +9,7 @@ namespace FSpot
         string Filename { get; }
 
         // For convenience
-        SafeUri Uri { get; }
+        SafeUri Uri { get; set; }
 
 		string ImportMD5 { get; }
     }
diff --git a/src/Core/PhotoVersion.cs b/src/Core/PhotoVersion.cs
index 683a3a9..0725b93 100644
--- a/src/Core/PhotoVersion.cs
+++ b/src/Core/PhotoVersion.cs
@@ -14,26 +14,32 @@ using Hyena;
 
 namespace FSpot
 {
-	public class PhotoVersion : IBrowsableItemVersion
-	{
-		public string Name { get; set; }
-		public IBrowsableItem Photo { get; private set; }
-		public SafeUri BaseUri { get; set; }
+    public class PhotoVersion : IBrowsableItemVersion
+    {
+        public string Name { get; set; }
+        public IBrowsableItem Photo { get; private set; }
+        public SafeUri BaseUri { get; set; }
         public string Filename { get; set; }
-        public SafeUri Uri { get { return BaseUri.Append (Filename); } }
-		public string ImportMD5 { get; internal set; }
-		public uint VersionId { get; private set; }
-		public bool IsProtected { get; private set; }
-	
-		public PhotoVersion (IBrowsableItem photo, uint version_id, SafeUri base_uri, string filename, string md5_sum, string name, bool is_protected)
-		{
-			Photo = photo;
-			VersionId = version_id;
-			BaseUri = base_uri;
+        public SafeUri Uri {
+            get { return BaseUri.Append (Filename); }
+            set {
+                BaseUri = value.GetBaseUri ();
+                Filename = value.GetFilename ();
+            }
+        }
+        public string ImportMD5 { get; set; }
+        public uint VersionId { get; private set; }
+        public bool IsProtected { get; private set; }
+
+        public PhotoVersion (IBrowsableItem photo, uint version_id, SafeUri base_uri, string filename, string md5_sum, string name, bool is_protected)
+        {
+            Photo = photo;
+            VersionId = version_id;
+            BaseUri = base_uri;
             Filename = filename;
-			ImportMD5 = md5_sum;
-			Name = name;
-			IsProtected = is_protected;
-		}
-	}
+            ImportMD5 = md5_sum;
+            Name = name;
+            IsProtected = is_protected;
+        }
+    }
 }
diff --git a/src/FileBrowsableItem.cs b/src/FileBrowsableItem.cs
index d69bed7..884a9aa 100644
--- a/src/FileBrowsableItem.cs
+++ b/src/FileBrowsableItem.cs
@@ -95,7 +95,14 @@ namespace FSpot {
 			public string Filename { get { return Uri.GetFilename (); } }
 			public SafeUri Uri { get; set; }
 
-			public string ImportMD5 { get { return String.Empty; } }
+			private string import_md5 = String.Empty;
+			public string ImportMD5 {
+				get {
+					if (import_md5 == String.Empty)
+						import_md5 = Photo.GenerateMD5 (Uri);
+					return import_md5;
+				}
+			}
 		}
 	}
 }
diff --git a/src/Import/ImportController.cs b/src/Import/ImportController.cs
index 71bb30a..73e9b53 100644
--- a/src/Import/ImportController.cs
+++ b/src/Import/ImportController.cs
@@ -292,10 +292,9 @@ namespace FSpot.Import
         void ImportPhoto (IBrowsableItem item, Roll roll)
         {
             var destination = FindImportDestination (item);
-            string hash = String.Empty;
 
             // Do duplicate detection
-            if (DuplicateDetect && store.HasDuplicate (item.DefaultVersion.Uri, out hash)) {
+            if (DuplicateDetect && store.HasDuplicate (item)) {
                 return;
             }
 
@@ -305,13 +304,11 @@ namespace FSpot.Import
 				var new_file = GLib.FileFactory.NewForUri (destination);
 				file.Copy (new_file, GLib.FileCopyFlags.AllMetadata, null, null);
                 copied_files.Add (destination);
+                item.DefaultVersion.Uri = destination;
             }
 
             // Import photo
-            var photo = store.Create (destination,
-                                      item.DefaultVersion.Uri,
-                                      roll.Id,
-                                      hash);
+            var photo = store.CreateFrom (item, roll.Id);
 
             bool needs_commit = false;
 
diff --git a/src/Import/ImportSource.cs b/src/Import/ImportSource.cs
index ba27d96..fd2e3b9 100644
--- a/src/Import/ImportSource.cs
+++ b/src/Import/ImportSource.cs
@@ -17,8 +17,21 @@ namespace FSpot.Import
 		public SafeUri BaseUri { get; set; }
 		public string Filename { get; set; }
 
-		public SafeUri Uri { get { return BaseUri.Append (Filename); } }
+		public SafeUri Uri {
+            get { return BaseUri.Append (Filename); }
+            set {
+                BaseUri = value.GetBaseUri ();
+                Filename = value.GetFilename ();
+            }
+        }
 
-        public string ImportMD5 { get { return String.Empty; } }
+        private string import_md5 = String.Empty;
+        public string ImportMD5 {
+            get {
+                if (import_md5 == String.Empty)
+                    import_md5 = Photo.GenerateMD5 (Uri);
+                return import_md5;
+            }
+        }
 	}
 }
diff --git a/src/PhotoStore.cs b/src/PhotoStore.cs
index 98ad319..2248750 100644
--- a/src/PhotoStore.cs
+++ b/src/PhotoStore.cs
@@ -100,8 +100,9 @@ public class PhotoStore : DbStore<Photo> {
 		Database.ExecuteNonQuery ("CREATE INDEX idx_photos_roll_id ON photos(roll_id)");
 	}
 
-	public bool HasDuplicate (SafeUri uri, out string hash) {
-		hash = Photo.GenerateMD5 (uri);
+	public bool HasDuplicate (IBrowsableItem item) {
+		var uri = item.DefaultVersion.Uri;
+		string hash = item.DefaultVersion.ImportMD5;
 		var condition = new ConditionWrapper (String.Format ("import_md5 = \"{0}\"", hash));
 		var dupes_by_hash = Count ("photo_versions", condition);
 		if (dupes_by_hash > 0)
@@ -122,9 +123,7 @@ public class PhotoStore : DbStore<Photo> {
 				Log.DebugFormat ("Found one possible duplicate for {0}", reader["filename"].ToString ());
 				if (!time.HasValue) {
 					// Only read time when needed
-					using (FSpot.ImageFile img = FSpot.ImageFile.Create (uri)) {
-						time = img.Date;
-					}
+					time = item.Time;
 				}
 
 				if (reader["time"].ToString () == DbUtils.UnixTimeFromDateTime (time.Value).ToString ()) {
@@ -142,20 +141,12 @@ public class PhotoStore : DbStore<Photo> {
 		return false;
 	}
 
-	public Photo Create (SafeUri uri, uint roll_id, string import_md5)
-	{
-		return Create (uri, uri, roll_id, import_md5);
-	}
-
-	public Photo Create (SafeUri new_uri, SafeUri orig_uri, uint roll_id, string import_md5)
+	public Photo CreateFrom (IBrowsableItem item, uint roll_id)
 	{
 		Photo photo;
 
-		var new_base_uri = new_uri.GetBaseUri ();
-		var filename = new_uri.GetFilename ();
-
-		using (FSpot.ImageFile img = FSpot.ImageFile.Create (orig_uri)) {
-			long unix_time = DbUtils.UnixTimeFromDateTime (img.Date);
+		using (FSpot.ImageFile img = FSpot.ImageFile.Create (item.DefaultVersion.Uri)) {
+			long unix_time = DbUtils.UnixTimeFromDateTime (item.Time);
 			string description = img.Description != null  ? img.Description.Split ('\0') [0] : String.Empty;
 
 	 		uint id = (uint) Database.Execute (
@@ -163,8 +154,8 @@ public class PhotoStore : DbStore<Photo> {
 					"INSERT INTO photos (time, base_uri, filename, description, roll_id, default_version_id, rating) "	+
 					"VALUES (:time, :base_uri, :filename, :description, :roll_id, :default_version_id, :rating)",
 	 				"time", unix_time,
-					"base_uri", new_base_uri.ToString (),
-					"filename", filename,
+					"base_uri", item.DefaultVersion.BaseUri.ToString (),
+					"filename", item.DefaultVersion.Filename,
 	 				"description", description,
 					"roll_id", roll_id,
 	 				"default_version_id", Photo.OriginalVersionId,
@@ -173,7 +164,7 @@ public class PhotoStore : DbStore<Photo> {
 			);
 
 			photo = new Photo (id, unix_time);
-			photo.AddVersionUnsafely (Photo.OriginalVersionId, new_base_uri, filename, import_md5, Catalog.GetString ("Original"), true);
+			photo.AddVersionUnsafely (Photo.OriginalVersionId, item.DefaultVersion.BaseUri, item.DefaultVersion.Filename, item.DefaultVersion.ImportMD5, Catalog.GetString ("Original"), true);
 			photo.Loaded = true;
 
 			InsertVersion (photo, photo.DefaultVersion as PhotoVersion);
diff --git a/src/PhotoVersionCommands.cs b/src/PhotoVersionCommands.cs
index bae1fae..d2e3eda 100644
--- a/src/PhotoVersionCommands.cs
+++ b/src/PhotoVersionCommands.cs
@@ -173,7 +173,7 @@ public class PhotoVersionCommands
 			try {
 				if (ResponseType.Ok == HigMessageDialog.RunHigConfirmation(parent_window, DialogFlags.DestroyWithParent, 
 									   MessageType.Warning, msg, desc, ok_caption)) {
-					Photo new_photo = store.Create (photo.DefaultVersion.Uri, photo.RollId, photo.DefaultVersion.ImportMD5);
+					Photo new_photo = store.CreateFrom (photo, photo.RollId);
 					new_photo.CopyAttributesFrom (photo);
 					photo.DeleteVersion (photo.DefaultVersionId, false, true);
 					store.Commit (new Photo[] {new_photo, photo});



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