[f-spot] Generalize compare logic to IBrowsableItem.



commit c182222c0eed8edc9db58304b8c1674e42f8696b
Author: Ruben Vermeersch <ruben savanne be>
Date:   Tue May 18 09:16:14 2010 +0200

    Generalize compare logic to IBrowsableItem.
    
    Most of this stuff is not Photo specific but also works for
    IBrowsableItem. In the long term IBrowsableItem should really become a
    general purpose kind of IPhoto, with Photo becoming a SqliteStorePhoto
    (or something like that). This commit removes weight from Photo to ease
    that transition.

 src/Core/App.cs                      |    2 +-
 src/Core/IBrowsableItemComparer.cs   |   44 +++++++++++++
 src/Core/IBrowsableItemExtensions.cs |   34 ++++++++++
 src/Core/Photo.cs                    |  118 +++++++--------------------------
 src/MainWindow.cs                    |    2 +-
 src/Makefile.am                      |    2 +
 6 files changed, 107 insertions(+), 95 deletions(-)
---
diff --git a/src/Core/App.cs b/src/Core/App.cs
index 883ee80..9f1a28a 100644
--- a/src/Core/App.cs
+++ b/src/Core/App.cs
@@ -252,7 +252,7 @@ namespace FSpot
 			window.ModifyBg (Gtk.StateType.Normal, new Gdk.Color (0, 0, 0));
 
 			if (photos.Length > 0) {
-				Array.Sort (photos, new Photo.RandomSort ());
+				Array.Sort (photos, new IBrowsableItemComparer.RandomSort ());
 				slideshow = new FSpot.Widgets.SlideShow (new BrowsablePointer (new PhotoList (photos), 0), (uint)(delay * 1000), true);
 				slideshow.Transition = new FSpot.Widgets.DissolveTransition ();
 				window.Add (slideshow);
diff --git a/src/Core/IBrowsableItemComparer.cs b/src/Core/IBrowsableItemComparer.cs
new file mode 100644
index 0000000..d68e0f5
--- /dev/null
+++ b/src/Core/IBrowsableItemComparer.cs
@@ -0,0 +1,44 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+namespace FSpot
+{
+	public static class IBrowsableItemComparer {
+		public class CompareDateName : IComparer<IBrowsableItem>
+		{
+			public int Compare (IBrowsableItem p1, IBrowsableItem p2)
+			{
+				int result = p1.CompareDate (p2);
+
+				if (result == 0)
+					result = p1.CompareName (p2);
+
+				return result;
+			}
+		}
+
+		public class RandomSort : IComparer
+		{
+			Random random = new Random ();
+
+			public int Compare (object obj1, object obj2)
+			{
+				return random.Next (-5, 5);
+			}
+		}
+
+		public class CompareDirectory : IComparer<IBrowsableItem>
+		{
+			public int Compare (IBrowsableItem p1, IBrowsableItem p2)
+			{
+				int result = p1.CompareDefaultVersionUri (p2);
+
+				if (result == 0)
+					result = p1.CompareName (p2);
+
+				return result;
+			}
+		}
+	}
+}
diff --git a/src/Core/IBrowsableItemExtensions.cs b/src/Core/IBrowsableItemExtensions.cs
new file mode 100644
index 0000000..0b3f59a
--- /dev/null
+++ b/src/Core/IBrowsableItemExtensions.cs
@@ -0,0 +1,34 @@
+using System;
+
+namespace FSpot
+{
+	public static class IBrowsableItemExtensions {
+		public static int CompareDate (this IBrowsableItem photo1, IBrowsableItem photo2)
+		{
+			return DateTime.Compare (photo1.Time, photo2.Time);
+		}
+
+		public static int CompareName (this IBrowsableItem photo1, IBrowsableItem photo2)
+		{
+			return string.Compare (photo1.Name, photo2.Name);
+		}
+
+		public static int Compare (this IBrowsableItem photo1, IBrowsableItem photo2)
+		{
+			int result = photo1.CompareDate (photo2);
+
+			if (result == 0)
+				result = CompareDefaultVersionUri (photo1, photo2);
+
+			if (result == 0)
+				result = photo1.CompareName (photo2);
+
+			return result;
+		}
+
+		public static int CompareDefaultVersionUri (this IBrowsableItem photo1, IBrowsableItem photo2)
+		{
+			return string.Compare (photo1.DefaultVersionUri.ToString (), photo2.DefaultVersionUri.ToString ());
+		}
+	}
+}
diff --git a/src/Core/Photo.cs b/src/Core/Photo.cs
index 3f595cd..38cad79 100644
--- a/src/Core/Photo.cs
+++ b/src/Core/Photo.cs
@@ -22,100 +22,7 @@ using FSpot.Platform;
 namespace FSpot
 {
 	public class Photo : DbItem, IComparable, IBrowsableItem {
-		// IComparable 
-		public int CompareTo (object obj) {
-			if (this.GetType () == obj.GetType ()) {
-				return Compare (this, (Photo)obj);
-			} else if (obj is DateTime) {
-				return this.time.CompareTo ((DateTime)obj);
-			} else {
-				throw new Exception ("Object must be of type Photo");
-			}
-		}
-	
-		public int CompareTo (Photo photo)
-		{
-			return Compare (this, photo);
-		}
 		
-		public static int Compare (Photo photo1, Photo photo2)
-		{
-			int result = photo1.Id.CompareTo (photo2.Id);
-			
-			if (result == 0)
-				return 0;
-			else 
-				result = CompareDate (photo1, photo2);
-	
-			if (result == 0)
-				result = CompareCurrentDir (photo1, photo2);
-			
-			if (result == 0)
-				result = CompareName (photo1, photo2);
-			
-			if (result == 0)
-				result = photo1.Id.CompareTo (photo2.Id);
-			
-			return result;
-		}
-	
-		private static int CompareDate (Photo photo1, Photo photo2)
-		{
-			return DateTime.Compare (photo1.time, photo2.time);
-		}
-	
-		private static int CompareCurrentDir (Photo photo1, Photo photo2)
-		{
-			return string.Compare (photo1.DirectoryPath, photo2.DirectoryPath);
-		}
-	
-		private static int CompareName (Photo photo1, Photo photo2)
-		{
-			return string.Compare (photo1.Name, photo2.Name);
-		}
-	
-		public class CompareDateName : IComparer<IBrowsableItem>
-		{
-			public int Compare (IBrowsableItem obj1, IBrowsableItem obj2)
-			{
-				Photo p1 = (Photo)obj1;
-				Photo p2 = (Photo)obj2;
-	
-				int result = Photo.CompareDate (p1, p2);
-				
-				if (result == 0)
-					result = CompareName (p1, p2);
-	
-				return result;
-			}
-		}
-	
-		public class CompareDirectory : IComparer
-		{
-			public int Compare (object obj1, object obj2)
-			{
-				Photo p1 = (Photo)obj1;
-				Photo p2 = (Photo)obj2;
-	
-				int result = Photo.CompareCurrentDir (p1, p2);
-				
-				if (result == 0)
-					result = CompareName (p1, p2);
-	
-				return result;
-			}
-		}
-	
-		public class RandomSort : IComparer
-		{
-			Random random = new Random ();
-			
-			public int Compare (object obj1, object obj2)
-			{
-				return random.Next (-5, 5);
-			}
-		}
-	
 		PhotoChanges changes = new PhotoChanges ();
 		public PhotoChanges Changes {
 			get{ return changes; }
@@ -670,5 +577,30 @@ namespace FSpot
 			// database.
 			AddVersionUnsafely (OriginalVersionId, uri, md5_sum, Catalog.GetString ("Original"), true);
 		}
+
+#region IComparable implementation
+
+		// IComparable 
+		public int CompareTo (object obj) {
+			if (this.GetType () == obj.GetType ()) {
+				return this.Compare((Photo)obj);
+			} else if (obj is DateTime) {
+				return this.time.CompareTo ((DateTime)obj);
+			} else {
+				throw new Exception ("Object must be of type Photo");
+			}
+		}
+
+		public int CompareTo (Photo photo)
+		{
+			int result = Id.CompareTo (photo.Id);
+			
+			if (result == 0)
+				return 0;
+			else 
+				return (this as IBrowsableItem).Compare (photo);
+		}
+
+#endregion
 	}
 }
diff --git a/src/MainWindow.cs b/src/MainWindow.cs
index 0d9fbbe..f36a27d 100644
--- a/src/MainWindow.cs
+++ b/src/MainWindow.cs
@@ -1884,7 +1884,7 @@ namespace FSpot
 		void HandleAdjustTime (object sender, EventArgs args)
 		{
 			PhotoList list = new PhotoList (Selection.Items);
-			list.Sort (new Photo.CompareDateName ());
+			list.Sort (new IBrowsableItemComparer.CompareDateName ());
 			(new AdjustTimeDialog (Database, list)).Run ();
 		}
 	
diff --git a/src/Makefile.am b/src/Makefile.am
index 50a30ab..3b68737 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -32,6 +32,8 @@ CORE_CSDISTFILES =				\
 	$(srcdir)/Core/Global.cs		\
 	$(srcdir)/Core/IBrowsableItem.cs	\
 	$(srcdir)/Core/IBrowsableItemChanges.cs	\
+	$(srcdir)/Core/IBrowsableItemComparer.cs	\
+	$(srcdir)/Core/IBrowsableItemExtensions.cs	\
 	$(srcdir)/Core/IBrowsableCollection.cs	\
 	$(srcdir)/Core/PhotoChanges.cs		\
 	$(srcdir)/Core/PhotosChanges.cs		\



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