f-spot r3974 - in trunk/src: . Utils Widgets



Author: sdelcroix
Date: Thu May 22 14:55:25 2008
New Revision: 3974
URL: http://svn.gnome.org/viewvc/f-spot?rev=3974&view=rev

Log:
new generci Cache to replace ThumbnailCache here and there

Added:
   trunk/src/Utils/Cache.cs
Modified:
   trunk/src/Makefile.am
   trunk/src/PhotoLoader.cs
   trunk/src/PixbufUtils.cs
   trunk/src/ThumbnailCache.cs
   trunk/src/Widgets/Filmstrip.cs
   trunk/src/Widgets/PreviewPopup.cs

Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am	(original)
+++ trunk/src/Makefile.am	Thu May 22 14:55:25 2008
@@ -39,6 +39,7 @@
 	$(srcdir)/Query/UntaggedCondition.cs
 
 UTILS_CSDISTFILES =				\
+	$(srcdir)/Utils/Cache.cs		\
 	$(srcdir)/Utils/ConsoleCrayon.cs	\
 	$(srcdir)/Utils/DbUtils.cs		\
 	$(srcdir)/Utils/GnomeUtil.cs		\

Modified: trunk/src/PhotoLoader.cs
==============================================================================
--- trunk/src/PhotoLoader.cs	(original)
+++ trunk/src/PhotoLoader.cs	Thu May 22 14:55:25 2008
@@ -57,7 +57,7 @@
 
 			if (pixbuf != null && thumbnail != null) {
 				if (!ThumbnailIsValid (uri, thumbnail)) {
-					System.Console.WriteLine ("regnerating thumbnail");
+					Log.DebugFormat ("regenerating thumbnail for {0}", uri);
 					FSpot.ThumbnailGenerator.Default.Request (uri, 0, 256, 256);
 				}
 

Modified: trunk/src/PixbufUtils.cs
==============================================================================
--- trunk/src/PixbufUtils.cs	(original)
+++ trunk/src/PixbufUtils.cs	Thu May 22 14:55:25 2008
@@ -185,6 +185,8 @@
 
 	public static Pixbuf ShallowCopy (Pixbuf pixbuf)
 	{
+		if (pixbuf == null)
+			return null;
 		Pixbuf result = new Pixbuf (pixbuf, 0, 0, pixbuf.Width, pixbuf.Height);
 		CopyThumbnailOptions (pixbuf, result);
 		return result;

Modified: trunk/src/ThumbnailCache.cs
==============================================================================
--- trunk/src/ThumbnailCache.cs	(original)
+++ trunk/src/ThumbnailCache.cs	Thu May 22 14:55:25 2008
@@ -1,7 +1,22 @@
+/*
+ * FSpot.ThumbnailCache.cs
+ *
+ * Author(s):
+ * 	Ettore Perazzoli
+ *	Larry Ewing  <lewing novell com>
+ *	Staphen Delcroix  <stephane delcroix org>
+ *
+ * This is free software. See COPYING for details.
+ */
+
 using System;
 using System.Collections;
 using Gdk;
 
+using FSpot.Utils;
+
+namespace FSpot
+{
 public class ThumbnailCache : IDisposable {
 
 	// Types.
@@ -65,14 +80,7 @@
 		pixbuf_mru.Remove (item);
 		pixbuf_mru.Insert (0, item);
 
-		// Shallow Copy
-		Pixbuf copy = new Pixbuf (item.pixbuf, 0, 0, 
-					  item.pixbuf.Width,
-					  item.pixbuf.Height);
-
-		PixbufUtils.CopyThumbnailOptions (item.pixbuf, copy);
-
-		return copy;
+		return PixbufUtils.ShallowCopy (item.pixbuf);
 	}
 
 	public void RemoveThumbnailForPath (string path)
@@ -96,6 +104,18 @@
 			thumb.pixbuf.Dispose ();
 		}
 		pixbuf_mru.Clear ();
+		System.GC.SuppressFinalize (this);
+	}
+
+	~ThumbnailCache ()
+	{
+		Log.DebugFormat ("Finalizer called on {0}. Should be Disposed", GetType ());		
+		foreach (object item in pixbuf_mru) {
+			Thumbnail thumb = item as Thumbnail;
+			pixbuf_hash.Remove (thumb.path);
+			thumb.pixbuf.Dispose ();
+		}
+		pixbuf_mru.Clear ();
 	}
 
 	// Private utility methods.
@@ -112,3 +132,4 @@
 		}
 	}
 }
+}

Added: trunk/src/Utils/Cache.cs
==============================================================================
--- (empty file)
+++ trunk/src/Utils/Cache.cs	Thu May 22 14:55:25 2008
@@ -0,0 +1,145 @@
+/*
+ * FSpot.Utils.Cache.cs
+ *
+ * Author(s):
+ *	Stephane Delcroix  <stephane delcroix org>
+ *
+ * This is free software.See COPYING for details.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+
+
+namespace FSpot.Utils
+{
+	public class Cache<TKey, TValue>
+	{
+		private const int DEFAULT_CACHE_SIZE = 10;
+		protected int max_count;
+		protected Dictionary<TKey, TValue> hash;
+		protected List<TKey> mru;
+		protected object o = new object ();
+
+		public Cache () : this (DEFAULT_CACHE_SIZE)
+		{
+		}
+
+		public Cache (int max_count)
+		{
+			this.max_count = max_count;
+			hash = new Dictionary<TKey, TValue> (max_count + 1);
+			mru = new List<TKey> (max_count + 1);
+		}
+
+		public virtual void Add (TKey key, TValue value)
+		{
+			lock (o) {
+				if (mru.Contains (key))
+					mru.Remove (key);
+
+				mru.Insert (0, key);
+				hash [key] = value;
+				
+				while (mru.Count >= max_count) {
+					hash.Remove (mru [max_count - 1]);
+					mru.RemoveAt (max_count - 1);
+				}	
+			}
+		}
+
+		public TValue Get (TKey key)
+		{
+			lock (o) {
+				if (!mru.Contains (key))
+					return default(TValue);
+
+				mru.Remove (key);
+				mru.Insert (0, key);
+
+				return hash [key];
+			}
+		}
+
+		public virtual void Clear ()
+		{
+			lock (o) {
+				mru.Clear ();
+				hash.Clear ();
+			}
+		}
+
+		public virtual void Remove (TKey key)
+		{
+			lock (o) {
+				mru.Remove (key);
+				hash.Remove (key);
+			}
+		}
+	}
+
+	public class DisposableCache<TKey, TValue> : Cache<TKey, TValue>, IDisposable
+	{
+		public DisposableCache () : base ()
+		{
+		}
+
+		public DisposableCache (int max_count) : base (max_count)
+		{
+		}
+
+		public override void Clear ()
+		{
+			lock (o) {
+				foreach (TValue value in hash.Values)
+					if (value is IDisposable)
+						(value as IDisposable).Dispose ();
+				mru.Clear ();
+				hash.Clear ();
+			}
+		}
+
+		public override void Add (TKey key, TValue value)
+		{
+			lock (o) {
+				if (mru.Contains (key))
+					mru.Remove (key);
+
+				mru.Insert (0, key);
+				hash [key] = value;
+				
+				while (mru.Count >= max_count) {
+					if (hash [mru [max_count - 1]] is IDisposable)
+						(hash [mru [max_count - 1]] as IDisposable).Dispose ();
+					hash.Remove (mru [max_count - 1]);
+					mru.RemoveAt (max_count - 1);
+				}	
+			}
+		}
+
+		public override void Remove (TKey key)
+		{
+			lock (o) {
+				if (hash [key] is IDisposable)
+					(hash [key] as IDisposable).Dispose ();
+				mru.Remove (key);
+				hash.Remove (key);
+			}
+		}
+
+
+		public void Dispose ()
+		{
+			Clear ();
+			GC.SuppressFinalize (this);
+		}
+
+		~DisposableCache ()
+		{
+			Log.DebugFormat ("Finalizer called on {0}. Should be Disposed", GetType ());		
+			Clear ();	
+		}
+	}
+}
+

Modified: trunk/src/Widgets/Filmstrip.cs
==============================================================================
--- trunk/src/Widgets/Filmstrip.cs	(original)
+++ trunk/src/Widgets/Filmstrip.cs	Thu May 22 14:55:25 2008
@@ -300,7 +300,7 @@
 		}
 
 		FSpot.BrowsablePointer selection;
-		ThumbnailCache thumb_cache;
+		DisposableCache<string, Pixbuf> thumb_cache;
 
 		public Filmstrip (FSpot.BrowsablePointer selection) : this (selection, true) { }
 
@@ -312,7 +312,7 @@
 			this.selection.Collection.Changed += HandleCollectionChanged;
 			this.selection.Collection.ItemsChanged += HandleCollectionItemsChanged;
 			this.squared_thumbs = squared_thumbs;
-			thumb_cache = new ThumbnailCache (30);
+			thumb_cache = new DisposableCache<string, Pixbuf> (30);
 			ThumbnailGenerator.Default.OnPixbufLoaded += delegate (PixbufLoader pl, string path, int order, Pixbuf p) {QueueDraw ();};
 		}
 	
@@ -462,7 +462,7 @@
 
 			//invalidate the thumbs cache
 			thumb_cache.Dispose ();
-			thumb_cache = new ThumbnailCache (30);
+			thumb_cache = new DisposableCache<string, Pixbuf> (30);
 			QueueDraw ();
 		}
 
@@ -494,7 +494,7 @@
 			Pixbuf current;
 			try {
 				thumb_path = FSpot.ThumbnailGenerator.ThumbnailPath ((selection.Collection [i]).DefaultVersionUri);
-				current = thumb_cache.GetThumbnailForPath (thumb_path);
+				current = PixbufUtils.ShallowCopy (thumb_cache.Get (thumb_path));
 			} catch (IndexOutOfRangeException) {
 				thumb_path = null;
 				current = null;
@@ -509,7 +509,7 @@
 						current = PixbufUtils.IconFromPixbuf (current, ThumbSize);
 					} else 
 						current = new Pixbuf (thumb_path, -1, ThumbSize);
-					thumb_cache.AddThumbnail (thumb_path, current);
+					thumb_cache.Add (thumb_path, current);
 				} catch {
 					try {
 						current = FSpot.Global.IconTheme.LoadIcon ("gtk-missing-image", ThumbSize, (Gtk.IconLookupFlags)0);

Modified: trunk/src/Widgets/PreviewPopup.cs
==============================================================================
--- trunk/src/Widgets/PreviewPopup.cs	(original)
+++ trunk/src/Widgets/PreviewPopup.cs	Thu May 22 14:55:25 2008
@@ -12,6 +12,7 @@
 using Cairo;
 using Gdk;
 using FSpot.Widgets;
+using FSpot.Utils;
 
 namespace FSpot {
 	public class PreviewPopup : Gtk.Window {
@@ -26,17 +27,17 @@
 				return show_histogram;
 			}
 			set {
-				if (value != show_histogram) {	
-					preview_cache.Dispose ();
-					preview_cache = new ThumbnailCache (50);
-					item = -1;
-				}
+			//	if (value != show_histogram) {	
+			//		preview_cache.Dispose ();
+			//		preview_cache = new DisposableCache<string, Pixbuf> (50);
+			//		item = -1;
+			//	}
 				show_histogram = value;
 			}
 		}
 					
 		private FSpot.Histogram hist;
-		private ThumbnailCache preview_cache = new ThumbnailCache (50);
+		private DisposableCache<string, Pixbuf> preview_cache = new DisposableCache<string, Pixbuf> (50);
 
 		private int item = -1;
 		new public int Item {
@@ -111,7 +112,7 @@
 			
 			string orig_path = item.DefaultVersionUri.LocalPath;
 
-			Gdk.Pixbuf pixbuf = preview_cache.GetThumbnailForPath (orig_path);
+			Gdk.Pixbuf pixbuf = PixbufUtils.ShallowCopy (preview_cache.Get (orig_path + show_histogram.ToString ()));
 			if (pixbuf == null) {
 				// A bizarre pixbuf = hack to try to deal with cinematic displays, etc.
 				int preview_size = ((this.Screen.Width + this.Screen.Height)/2)/3;
@@ -125,7 +126,7 @@
 				}
 
 				if (pixbuf != null) {
-					preview_cache.AddThumbnail (orig_path, pixbuf);
+					preview_cache.Add (orig_path, pixbuf + show_histogram.ToString ());
 					AddHistogram (pixbuf);
 					image.Pixbuf = pixbuf;
 				} else {



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