[f-spot] Don't use ImageFile for Descriptions, prefer IBrowsableItem.



commit 436cdc2d5af6229594289b2431d510bc13bf4875
Author: Ruben Vermeersch <ruben savanne be>
Date:   Sun Jun 13 01:11:20 2010 +0200

    Don't use ImageFile for Descriptions, prefer IBrowsableItem.

 src/FileBrowsableItem.cs       |   45 +++++++++++++++----------------------
 src/Import/FileImportSource.cs |   42 +++++++++++++++++++++++-----------
 src/PhotoStore.cs              |   48 +++++++++++++++++++--------------------
 3 files changed, 69 insertions(+), 66 deletions(-)
---
diff --git a/src/FileBrowsableItem.cs b/src/FileBrowsableItem.cs
index 884a9aa..f4aa3dc 100644
--- a/src/FileBrowsableItem.cs
+++ b/src/FileBrowsableItem.cs
@@ -18,10 +18,9 @@ using Hyena;
 using FSpot.Utils;
 
 namespace FSpot {
-	public class FileBrowsableItem : IBrowsableItem, IDisposable
+	public class FileBrowsableItem : IBrowsableItem
 	{
-		ImageFile img;
-		bool attempted;
+		bool metadata_parsed = false;
 
 		public FileBrowsableItem (SafeUri uri)
 		{
@@ -30,15 +29,17 @@ namespace FSpot {
             };
 		}
 
-		protected ImageFile Image {
-			get {
-				if (!attempted) {
-					img = ImageFile.Create (DefaultVersion.Uri);
-					attempted = true;
-				}
+		private void EnsureMetadataParsed ()
+		{
+			if (metadata_parsed)
+				return;
 
-				return img;
+			using (var img = ImageFile.Create (DefaultVersion.Uri)) {
+				time = img.Date;
+				description = img.Description;
 			}
+
+			metadata_parsed = true;
 		}
 
 		public Tag [] Tags {
@@ -47,31 +48,27 @@ namespace FSpot {
 			}
 		}
 
+		private DateTime time;
 		public DateTime Time {
 			get {
-				return Image.Date;
+				EnsureMetadataParsed ();
+				return time;
 			}
 		}
 
 		public IBrowsableItemVersion DefaultVersion { get; private set; }
 
+		private string description;
 		public string Description {
 			get {
-				try {
-					if (Image != null)
-						return Image.Description;
-
-				} catch (System.Exception e) {
-					Log.Exception (e);
-				}
-
-				return null;
+				EnsureMetadataParsed ();
+				return description;
 			}
 		}
 
 		public string Name {
 			get {
-				return Path.GetFileName (Image.Uri.AbsolutePath);
+				return DefaultVersion.Uri.GetFilename ();
 			}
 		}
 
@@ -81,12 +78,6 @@ namespace FSpot {
 			}
 		}
 
-		public void Dispose ()
-		{
-			img.Dispose ();
-			GC.SuppressFinalize (this);
-		}
-
 		private class FileBrowsableItemVersion : IBrowsableItemVersion {
 			public string Name { get { return String.Empty; } }
 			public bool IsProtected { get { return true; } }
diff --git a/src/Import/FileImportSource.cs b/src/Import/FileImportSource.cs
index 4ee1942..f7edeca 100644
--- a/src/Import/FileImportSource.cs
+++ b/src/Import/FileImportSource.cs
@@ -139,6 +139,8 @@ namespace FSpot.Import
     }
 
     internal class FileImportInfo : IBrowsableItem {
+		bool metadata_parsed = false;
+
         public FileImportInfo (SafeUri original)
         {
             DefaultVersion = new ImportInfoVersion () {
@@ -147,27 +149,39 @@ namespace FSpot.Import
             };
         }
 
+		private void EnsureMetadataParsed ()
+		{
+			if (metadata_parsed)
+				return;
+
+			using (var img = ImageFile.Create (DefaultVersion.Uri)) {
+				time = img.Date;
+				description = img.Description;
+			}
+
+			metadata_parsed = true;
+		}
+
         public IBrowsableItemVersion DefaultVersion { get; private set; }
         public SafeUri DestinationUri { get; set; }
 
-        private DateTime? time = null;
+        private DateTime time;
         public System.DateTime Time {
-            get {
-                if (!time.HasValue) {
-                    try {
-                        using (FSpot.ImageFile img = FSpot.ImageFile.Create (DefaultVersion.Uri)) {
-                            time = img.Date;
-                        }
-                    } catch (Exception) {
-                        time = DateTime.Now;
-                    }
-                }
-                return time.Value;
-            }
+			get {
+				EnsureMetadataParsed ();
+				return time;
+			}
         }
 
+		private string description;
+		public string Description {
+			get {
+				EnsureMetadataParsed ();
+				return description;
+			}
+		}
+
         public Tag [] Tags { get { throw new NotImplementedException (); } }
-        public string Description { get { throw new NotImplementedException (); } }
         public string Name { get { throw new NotImplementedException (); } }
         public uint Rating { get { return 0; } }
 
diff --git a/src/PhotoStore.cs b/src/PhotoStore.cs
index 2248750..a88f82a 100644
--- a/src/PhotoStore.cs
+++ b/src/PhotoStore.cs
@@ -145,31 +145,29 @@ public class PhotoStore : DbStore<Photo> {
 	{
 		Photo photo;
 
-		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 (
-				new DbCommand (
-					"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", item.DefaultVersion.BaseUri.ToString (),
-					"filename", item.DefaultVersion.Filename,
-	 				"description", description,
-					"roll_id", roll_id,
-	 				"default_version_id", Photo.OriginalVersionId,
-					"rating", "0"
-				)
-			);
+		long unix_time = DbUtils.UnixTimeFromDateTime (item.Time);
+		string description = item.Description;
+
+		uint id = (uint) Database.Execute (
+			new DbCommand (
+				"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", item.DefaultVersion.BaseUri.ToString (),
+				"filename", item.DefaultVersion.Filename,
+				"description", description,
+				"roll_id", roll_id,
+				"default_version_id", Photo.OriginalVersionId,
+				"rating", "0"
+			)
+		);
 
-			photo = new Photo (id, unix_time);
-			photo.AddVersionUnsafely (Photo.OriginalVersionId, item.DefaultVersion.BaseUri, item.DefaultVersion.Filename, item.DefaultVersion.ImportMD5, Catalog.GetString ("Original"), true);
-			photo.Loaded = true;
+		photo = new Photo (id, unix_time);
+		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);
-			EmitAdded (photo);
-		}
+		InsertVersion (photo, photo.DefaultVersion as PhotoVersion);
+		EmitAdded (photo);
 		return photo;
 	}
 



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