[f-spot/cleanup-backend: 22/24] Base PhotoQuery on DatabasePhotoListModel



commit b677777a666f4d85df093280b6034730b8a37fb3
Author: Mike Gemünde <mike gemuende de>
Date:   Fri Jul 16 17:17:40 2010 +0200

    Base PhotoQuery on DatabasePhotoListModel
    
    This commits starts switching to the new backend. The displaying of tags
    are also broken for now ;-).

 src/Core/App.cs                              |    2 +-
 src/FSpot.Database/DatabasePhotoListModel.cs |  118 +++++++++++++++-
 src/MainWindow.cs                            |    6 +-
 src/PhotoQuery.cs                            |  200 +++-----------------------
 src/Sources/DatabaseSource.cs                |   15 ++-
 src/UI.Dialog/EditTagIconDialog.cs           |    2 +-
 6 files changed, 151 insertions(+), 192 deletions(-)
---
diff --git a/src/Core/App.cs b/src/Core/App.cs
index edf00ac..6cb4962 100644
--- a/src/Core/App.cs
+++ b/src/Core/App.cs
@@ -86,7 +86,7 @@ namespace FSpot
             get {
                 lock (sync_handle) {
                     if (active_source == null) {
-                        active_source = new DatabaseSource (Database);
+                        active_source = new DatabaseSource (Database.Database);
                     }
                 }
 
diff --git a/src/FSpot.Database/DatabasePhotoListModel.cs b/src/FSpot.Database/DatabasePhotoListModel.cs
index cc85af1..51ce717 100644
--- a/src/FSpot.Database/DatabasePhotoListModel.cs
+++ b/src/FSpot.Database/DatabasePhotoListModel.cs
@@ -41,7 +41,7 @@ namespace FSpot.Database
 
         public DatabasePhotoListModel (HyenaSqliteConnection connection, string uuid, PhotoModelProvider provider)
         {
-            Selection = new PhotoSelection (this);
+            base.Selection = new PhotoSelection (this);
             cache = new DatabasePhotoModelCache (connection, uuid, this, provider);
         }
 
@@ -92,6 +92,60 @@ namespace FSpot.Database
         {
             StringBuilder query_builder = new StringBuilder ("FROM photos ");
 
+            bool where_added = false;
+            bool hidden_contained = false;
+            foreach (var pair in Conditions) {
+
+                IQueryCondition condition = pair.Value;
+
+                if (condition == null)
+                    continue;
+
+//              if (condition is HiddenTag)
+//                  hidden_contained = true;
+
+                if (condition is IOrderCondition)
+                    continue;
+
+                string sql_clause = condition.SqlClause ();
+
+                if (sql_clause == null || sql_clause.Trim () == String.Empty)
+                    continue;
+                query_builder.Append (where_added ? " AND " : " WHERE ");
+                query_builder.Append (sql_clause);
+                where_added = true;
+            }
+
+            /* if a HiddenTag condition is not explicitly given, we add one */
+//          if ( ! hidden_contained) {
+//              string sql_clause = HiddenTag.HideHiddenTag.SqlClause ();
+//
+//              if (sql_clause != null && sql_clause.Trim () != String.Empty) {
+//                  query_builder.Append (where_added ? " AND " : " WHERE ");
+//                  query_builder.Append (sql_clause);
+//              }
+//          }
+
+            bool order_added = false;
+            foreach (var pair in Conditions) {
+
+                IQueryCondition condition = pair.Value;
+
+                if (condition == null)
+                    continue;
+
+                if (!(condition is IOrderCondition))
+                    continue;
+
+                string sql_clause = condition.SqlClause ();
+
+                if (sql_clause == null || sql_clause.Trim () == String.Empty)
+                    continue;
+                query_builder.Append (order_added ? " , " : "ORDER BY ");
+                query_builder.Append (sql_clause);
+                order_added = true;
+            }
+
             ReloadFragment = query_builder.ToString ();
         }
 
@@ -161,14 +215,72 @@ namespace FSpot.Database
             return cache.ContainsKey (photo.Id);
         }
 
-        public PhotoSelection Selection { get; protected set; }
+        public PhotoSelection Selection {
+            get { return base.Selection as PhotoSelection; }
+        }
 
         public event IBrowsableCollectionChangedHandler Changed;
         public event IBrowsableCollectionItemsChangedHandler ItemsChanged;
 
         public int IndexOf (IBrowsableItem item)
         {
-            return (int)cache.IndexOf (item);
+            DatabasePhoto photo = item as DatabasePhoto;
+
+            if (photo == null)
+                return -1;
+
+            return (int) cache.IndexOf (photo);
+        }
+
+#endregion
+
+#region PhotoQuery Methods (to be removed)
+
+        //Query Conditions
+        private Dictionary<Type, IQueryCondition> conditions;
+        private Dictionary<Type, IQueryCondition> Conditions {
+            get {
+                if (conditions == null)
+                    conditions = new Dictionary<Type, IQueryCondition> ();
+                return conditions;
+            }
+        }
+
+        public bool SetCondition (IQueryCondition condition)
+        {
+            if (condition == null)
+                throw new ArgumentNullException ("condition");
+            if (Conditions.ContainsKey (condition.GetType ()) && Conditions [condition.GetType ()] == condition)
+                return false;
+            Conditions [condition.GetType ()] = condition;
+            return true;
+        }
+
+        public T GetCondition<T> () where T : IQueryCondition
+        {
+            IQueryCondition val;
+            Conditions.TryGetValue (typeof (T), out val);
+            return (T)val;
+        }
+
+        public bool UnSetCondition<T> ()
+        {
+            if (!Conditions.ContainsKey (typeof(T)))
+                return false;
+            Conditions.Remove (typeof(T));
+            return true;
+        }
+
+        public void RequestReload ()
+        {
+            uint timer = Log.DebugTimerStart ();
+            Reload ();
+
+
+            if (Changed != null)
+                Changed (this);
+
+            Log.DebugTimerPrint (timer, "Reloading the query took {0}");
         }
 
 #endregion
diff --git a/src/MainWindow.cs b/src/MainWindow.cs
index 950a9b8..3331306 100644
--- a/src/MainWindow.cs
+++ b/src/MainWindow.cs
@@ -2203,7 +2203,11 @@ namespace FSpot
 		
 			//How many pictures are associated to these tags?
 			Db db = App.Instance.Database;
-			FSpot.PhotoQuery count_query = new FSpot.PhotoQuery(db.Photos);
+			FSpot.PhotoQuery count_query =
+                new FSpot.PhotoQuery(
+                                     db.Database,
+                                     "tmp44",
+                                     new FSpot.Database.PhotoModelProvider (db.Database));
 			count_query.Terms = FSpot.OrTerm.FromTags(tags);
 			int associated_photos = count_query.Count;
 	
diff --git a/src/PhotoQuery.cs b/src/PhotoQuery.cs
index 6816f8c..d61057d 100644
--- a/src/PhotoQuery.cs
+++ b/src/PhotoQuery.cs
@@ -14,153 +14,30 @@ using System.Collections.Generic;
 
 using FSpot.Query;
 using FSpot.Collections;
+using FSpot.Database;
 
 using Hyena;
+using Hyena.Data.Sqlite;
 
 namespace FSpot {
-	public class PhotoQuery : FSpot.IBrowsableCollection {
-		class PhotoCache
-		{
-			static int SIZE = 100;
-			public int Size {
-				get { return SIZE; }
-			}
-
-			Dictionary <int, Photo []> cache;
-			string temp_table;
-			PhotoStore store;
-
-			public PhotoCache (PhotoStore store, string temp_table)
-			{
-				this.temp_table = temp_table;
-				this.store = store;
-				cache = new Dictionary<int, Photo[]> ();
-			}
-
-			public bool TryGetPhoto (int index, out Photo photo)
-			{
-				photo = null;
-				Photo [] val;
-				int offset = index - index % SIZE;
-				if (!cache.TryGetValue (offset, out val))
-					return false;
-				photo = val [index - offset];
-				return true;
-			}
-
-			public Photo Get (int index)
-			{
-				Photo [] val;
-				int offset = index - index % SIZE;
-				if (!cache.TryGetValue (offset, out val)) {
-					val = store.QueryFromTemp (temp_table, offset, SIZE);
-					cache [offset] = val;
-				}
-				return val [index - offset];
-			}
-		}
+	public class PhotoQuery : DatabasePhotoListModel {
 
-		PhotoCache cache;
-		private PhotoStore store;
 		private Term terms;
 
-		static int query_count = 0;
-		static int QueryCount {
-			get {return query_count ++;}
-		}
-
-		Dictionary<uint, int> reverse_lookup;
-
 		int count = -1;
-		
-		string temp_table = String.Format ("photoquery_temp_{0}", QueryCount);
 
 		// Constructor
-		public PhotoQuery (PhotoStore store, params IQueryCondition [] conditions)
+		public PhotoQuery (HyenaSqliteConnection connection, string uuid, PhotoModelProvider provider, params IQueryCondition [] conditions)
+            : base (connection, uuid, provider)
 		{
-			this.store = store;
-			this.store.ItemsChanged += MarkChanged;
-			cache = new PhotoCache (store, temp_table);
-			reverse_lookup = new Dictionary<uint, int> ();
 			SetCondition (OrderByTime.OrderByTimeDesc);
 
 			foreach (IQueryCondition condition in conditions)
 				SetCondition (condition);
 
-            Selection = new PhotoSelection (this);
-
 			RequestReload ();
 		}
 
-		public int Count {
-			get {
-				if (count < 0)
-					count = store.Count (temp_table);
-				return count;
-			}
-		}
-		
-		public bool Contains (IBrowsableItem item) {
-			return IndexOf (item) >= 0;
-		}
-
-		// IPhotoCollection Interface
-		public event FSpot.IBrowsableCollectionChangedHandler Changed;
-		public event FSpot.IBrowsableCollectionItemsChangedHandler ItemsChanged;
-		
-		public IBrowsableItem this [int index] {
-			get { return cache.Get (index); }
-		}
-
-		[Obsolete ("DO NOT USE THIS, IT'S TOO SLOW")]
-		public Photo [] Photos {
-			get { return store.QueryFromTemp (temp_table); }
-		}
-
-		[Obsolete ("DO NOT USE Items on PhotoQuery")]
-		public IBrowsableItem [] Items {
-			get { throw new NotImplementedException (); }
-		}
-
-		public PhotoStore Store {
-			get { return store; }
-		}
-		
-		//Query Conditions
-		private Dictionary<Type, IQueryCondition> conditions;
-		private Dictionary<Type, IQueryCondition> Conditions {
-			get {
-				if (conditions == null)
-					conditions = new Dictionary<Type, IQueryCondition> ();
-				return conditions;
-			}
-		}
-
-		internal bool SetCondition (IQueryCondition condition)
-		{
-			if (condition == null)
-				throw new ArgumentNullException ("condition");
-			if (Conditions.ContainsKey (condition.GetType ()) && Conditions [condition.GetType ()] == condition)
-				return false;
-			Conditions [condition.GetType ()] = condition;
-			return true;
-		}
-
-		internal T GetCondition<T> () where T : IQueryCondition
-		{
-			IQueryCondition val;
-			Conditions.TryGetValue (typeof (T), out val);
-			return (T)val;
-		}
-
-		internal bool UnSetCondition<T> ()
-		{
-			if (!Conditions.ContainsKey (typeof(T)))
-				return false;
-			Conditions.Remove (typeof(T));
-			return true;
-		}
-
 		public Term Terms {
 			get { return terms; }
 			set {
@@ -253,49 +130,7 @@ namespace FSpot {
 			}
 		}
 
-		public void RequestReload ()
-		{
-			uint timer = Log.DebugTimerStart ();
-			IQueryCondition[] condition_array;
-			
-			int i = 0;
-			if (untagged) {
-				condition_array = new IQueryCondition[conditions.Count + 1];
-				condition_array[0] = new UntaggedCondition ();
-				i = 1;
-			} else {
-				condition_array = new IQueryCondition[conditions.Count + 2];
-		//		condition_array[0] = new ConditionWrapper (extra_condition);
-				condition_array[1] = new ConditionWrapper (terms != null ? terms.SqlCondition () : null);
-				i = 2;
-			}
-			
-			foreach (IQueryCondition condition in Conditions.Values) {
-				condition_array[i] = condition;
-				i++;
-			}
-	
-			store.QueryToTemp (temp_table, condition_array);
-
-			count = -1;
-			cache = new PhotoCache (store, temp_table);
-			reverse_lookup = new Dictionary<uint,int> ();
-
-            Selection.MaxIndex = Count - 1;
-
-			if (Changed != null)
-				Changed (this);
-			
-			Log.DebugTimerPrint (timer, "Reloading the query took {0}");
-		}
-		
-		public int IndexOf (IBrowsableItem photo)
-		{
-			if (photo == null || !(photo is Photo))
-				return -1;
-			return store.IndexOf (temp_table, photo as Photo);
-		}
-
+        /*
 		private int [] IndicesOf (DbItem [] dbitems)
 		{
 			uint timer = Log.DebugTimerStart ();
@@ -314,7 +149,7 @@ namespace FSpot {
 			Log.DebugTimerPrint (timer, "IndicesOf took {0}");
 			return indices.ToArray ();
 		}
-
+         */
 		public int LookupItem (System.DateTime date)
 		{
 			return LookupItem (date, TimeOrderAsc);
@@ -322,7 +157,7 @@ namespace FSpot {
 
 		private int LookupItem (System.DateTime date, bool asc)
 		{
-			if (Count == 0)
+/*			if (Count == 0)
 				return -1;
 			
 			uint timer = Log.DebugTimerStart ();
@@ -350,7 +185,8 @@ namespace FSpot {
 			if (asc)
 				return this[mid].Time < date ? mid + 1 : mid;
 			return this[mid].Time > date ? mid + 1 : mid;
-
+         */
+            return -1;
 		}
 
 		public void Commit (int index)
@@ -360,34 +196,34 @@ namespace FSpot {
 
 		public void Commit (int [] indexes)
 		{
-			List<Photo> to_commit = new List<Photo>();
+/*			List<Photo> to_commit = new List<Photo>();
 			foreach (int index in indexes) {
 				to_commit.Add (this [index] as Photo);
 				reverse_lookup [(this [index] as Photo).Id] = index;
 			}
-			store.Commit (to_commit.ToArray ());
+			store.Commit (to_commit.ToArray ());*/
 		}
 
 		private void MarkChanged (object sender, DbItemEventArgs<Photo> args)
 		{
-			int [] indexes = IndicesOf (args.Items);
+/*			int [] indexes = IndicesOf (args.Items);
 
 			if (indexes.Length > 0 && ItemsChanged != null)
-				ItemsChanged (this, new BrowsableEventArgs(indexes, (args as PhotoEventArgs).Changes));
+				ItemsChanged (this, new BrowsableEventArgs(indexes, (args as PhotoEventArgs).Changes));*/
 		}
 
 		public void MarkChanged (int index, IBrowsableItemChanges changes)
 		{
-			MarkChanged (new int [] {index}, changes);
+		//	MarkChanged (new int [] {index}, changes);
 		}
 
 		public void MarkChanged (int [] indexes, IBrowsableItemChanges changes)
 		{
-			ItemsChanged (this, new BrowsableEventArgs (indexes, changes));
+		//	ItemsChanged (this, new BrowsableEventArgs (indexes, changes));
 		}
-
+        /*
         public PhotoSelection Selection {
             get; protected set;
-        }
+        }*/
 	}
 }
diff --git a/src/Sources/DatabaseSource.cs b/src/Sources/DatabaseSource.cs
index 607a001..ad34fd9 100644
--- a/src/Sources/DatabaseSource.cs
+++ b/src/Sources/DatabaseSource.cs
@@ -11,6 +11,9 @@ using System;
 
 using Mono.Unix;
 
+using Hyena.Data.Sqlite;
+
+using FSpot.Database;
 
 namespace FSpot.Sources
 {
@@ -21,18 +24,22 @@ namespace FSpot.Sources
 
 #region Private Fields
 
-        private Db database;
+        private HyenaSqliteConnection connection;
         private PhotoQuery query;
 
+        private PhotoModelProvider photo_provider;
+
 #endregion
 
 #region Constructors
 
-        public DatabaseSource (Db database)
+        public DatabaseSource (HyenaSqliteConnection connection)
         {
-            this.database = database;
+            this.connection = connection;
+
+            photo_provider = new PhotoModelProvider (connection);
 
-            query = new PhotoQuery (database.Photos);
+            query = new PhotoQuery (connection, "CoreCache", photo_provider);
         }
 
 #endregion
diff --git a/src/UI.Dialog/EditTagIconDialog.cs b/src/UI.Dialog/EditTagIconDialog.cs
index 059d60d..7a25577 100644
--- a/src/UI.Dialog/EditTagIconDialog.cs
+++ b/src/UI.Dialog/EditTagIconDialog.cs
@@ -51,7 +51,7 @@ namespace FSpot.UI.Dialog
 			} else
 				preview_image.Pixbuf = preview_pixbuf;
 
-			query = new FSpot.PhotoQuery (db.Photos);
+			query = new FSpot.PhotoQuery (db.Database, "dsf", new FSpot.Database.PhotoModelProvider (db.Database));
 			
 			if (db.Tags.Hidden != null)
 				query.Terms = FSpot.OrTerm.FromTags (new Tag [] {t});



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