[f-spot/hyena-models: 6/6] Base PhotoQuery on DatabasePhotoListModel



commit e14b10a58594b19fb382680a13a37a58df59c990
Author: Mike Gemünde <mike gemuende de>
Date:   Mon Oct 4 13:42:08 2010 +0200

    Base PhotoQuery on DatabasePhotoListModel
    
    PhotoQuery is going to be replaced by DatabasePhotoListModel. This is
    a first step on this.

 .../MainApp/FSpot.Database/DatabasePhoto.cs        |    2 +-
 .../FSpot.Database/DatabasePhotoListModel.cs       |  115 ++++-
 .../MainApp/FSpot.Database/DatabaseSource.cs       |   13 +-
 .../MainApp/FSpot.UI.Dialog/EditTagIconDialog.cs   |    4 +-
 src/Clients/MainApp/FSpot/App.cs                   |    2 +-
 src/Clients/MainApp/FSpot/MainWindow.cs            |   45 +-
 src/Clients/MainApp/FSpot/PhotoQuery.cs            |  580 +++++++-------------
 src/Clients/MainApp/FSpot/TimeAdaptor.cs           |    3 +-
 8 files changed, 365 insertions(+), 399 deletions(-)
---
diff --git a/src/Clients/MainApp/FSpot.Database/DatabasePhoto.cs b/src/Clients/MainApp/FSpot.Database/DatabasePhoto.cs
index 9416f75..8549d61 100644
--- a/src/Clients/MainApp/FSpot.Database/DatabasePhoto.cs
+++ b/src/Clients/MainApp/FSpot.Database/DatabasePhoto.cs
@@ -68,7 +68,7 @@ namespace FSpot.Database
 #region Remaining IPhoto Implementation
 
         public Tag[] Tags {
-            get { return tags.ToArray (); }
+            get { return TagList.ToArray (); }
         }
 
         public IPhotoVersion DefaultVersion {
diff --git a/src/Clients/MainApp/FSpot.Database/DatabasePhotoListModel.cs b/src/Clients/MainApp/FSpot.Database/DatabasePhotoListModel.cs
index 8c18b17..3651eb3 100644
--- a/src/Clients/MainApp/FSpot.Database/DatabasePhotoListModel.cs
+++ b/src/Clients/MainApp/FSpot.Database/DatabasePhotoListModel.cs
@@ -92,9 +92,64 @@ 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 ();
         }
 
+
 #endregion
 
 #region ICacheableDatabaseModel Implementation
@@ -168,10 +223,68 @@ namespace FSpot.Database
 
         public int IndexOf (IPhoto 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/Clients/MainApp/FSpot.Database/DatabaseSource.cs b/src/Clients/MainApp/FSpot.Database/DatabaseSource.cs
index 43f7eca..ff5b88b 100644
--- a/src/Clients/MainApp/FSpot.Database/DatabaseSource.cs
+++ b/src/Clients/MainApp/FSpot.Database/DatabaseSource.cs
@@ -11,6 +11,8 @@ using System;
 
 using Mono.Unix;
 
+using Hyena.Data.Sqlite;
+
 using FSpot.Core;
 
 
@@ -23,18 +25,21 @@ namespace FSpot.Database
 
 #region Private Fields
 
-        private Db database;
         private PhotoQuery query;
+        private HyenaSqliteConnection connection;
+        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/Clients/MainApp/FSpot.UI.Dialog/EditTagIconDialog.cs b/src/Clients/MainApp/FSpot.UI.Dialog/EditTagIconDialog.cs
index 2582902..297e4fb 100644
--- a/src/Clients/MainApp/FSpot.UI.Dialog/EditTagIconDialog.cs
+++ b/src/Clients/MainApp/FSpot.UI.Dialog/EditTagIconDialog.cs
@@ -53,7 +53,9 @@ namespace FSpot.UI.Dialog
 			} else
 				preview_image.Pixbuf = preview_pixbuf;
 
-			query = new FSpot.PhotoQuery (db.Photos);
+            // TODO
+			//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});
diff --git a/src/Clients/MainApp/FSpot/App.cs b/src/Clients/MainApp/FSpot/App.cs
index 79fb9dc..3777c15 100644
--- a/src/Clients/MainApp/FSpot/App.cs
+++ b/src/Clients/MainApp/FSpot/App.cs
@@ -87,7 +87,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/Clients/MainApp/FSpot/MainWindow.cs b/src/Clients/MainApp/FSpot/MainWindow.cs
index 62dbb0d..dfaba95 100644
--- a/src/Clients/MainApp/FSpot/MainWindow.cs
+++ b/src/Clients/MainApp/FSpot/MainWindow.cs
@@ -374,13 +374,14 @@ namespace FSpot
 
 			LoadPreference (Preferences.TAG_ICON_SIZE);
 
-			try {
+            query = App.Instance.ActiveSource.Photos as PhotoQuery;
+			/*try {
 				query = new FSpot.PhotoQuery (Database.Photos);
 			} catch (System.Exception e) {
 				//FIXME assume any exception here is due to a corrupt db and handle that.
 				new RepairDbDialog (e, Database.Repair (), main_window);
 				query = new FSpot.PhotoQuery (Database.Photos);
-			}
+			}*/
 
 			UpdateStatusLabel ();
 			query.Changed += HandleQueryChanged;
@@ -861,18 +862,12 @@ namespace FSpot
 		// Selection Interface
 		//
 
-		private Photo [] SelectedPhotos (int [] selected_ids)
+		private IPhoto [] SelectedPhotos (int [] selected_ids)
 		{
-			Photo [] photo_list = new Photo [selected_ids.Length];
-
-			int i = 0;
-			foreach (int num in selected_ids)
-				photo_list [i ++] = query [num] as Photo;
-
-			return photo_list;
+            return (from i in selected_ids select query [i]).ToArray ();
 		}
 
-		public Photo [] SelectedPhotos ()
+		public IPhoto [] SelectedPhotos ()
 		{
 			return SelectedPhotos (SelectedIds ());
 		}
@@ -1031,7 +1026,7 @@ namespace FSpot
 
 		public void HandleIconViewDragBegin (object sender, DragBeginArgs args)
 		{
-			Photo [] photos = SelectedPhotos ();
+			var photos = SelectedPhotos ();
 
 			if (photos.Length > 0) {
 				int len = Math.Min (photos.Length, 4);
@@ -1090,7 +1085,8 @@ namespace FSpot
 			}
 
 			if (args.Info == DragDropTargets.PhotoListEntry.Info) {
-				args.SelectionData.SetPhotosData (SelectedPhotos (), args.Context.Targets[0]);
+                // TODO
+				//args.SelectionData.SetPhotosData (SelectedPhotos (), args.Context.Targets[0]);
 				return;
 			}
 
@@ -1209,10 +1205,10 @@ namespace FSpot
 					if (icon_view.Selection.Contains (p_item)) //We don't want to reparent ourselves!
 						return;
 					PhotoVersionCommands.Reparent cmd = new PhotoVersionCommands.Reparent ();
-					Photo[] photos_to_reparent = SelectedPhotos ();
+					var photos_to_reparent = SelectedPhotos ();
 					// Give feedback to user that something happened, and leave the parent selected after reparenting
 					icon_view.Selection.Add (p_item);
-					cmd.Execute (Database.Photos, photos_to_reparent, query.Photos [p_item], GetToplevel (null));
+					cmd.Execute (Database.Photos, photos_to_reparent as Photo[], query [p_item] as Photo, GetToplevel (null));
 					UpdateQuery ();
 				}
 				Gtk.Drag.Finish (args.Context, true, false, args.Time);
@@ -2081,7 +2077,7 @@ namespace FSpot
 				return;
 			}
 
-			Photo[] photos = SelectedPhotos();
+			var photos = SelectedPhotos();
 			string header = Catalog.GetPluralString ("Delete the selected photo permanently?",
 									    "Delete the {0} selected photos permanently?",
 									    photos.Length);
@@ -2106,7 +2102,7 @@ namespace FSpot
 						}
 					}
 				}
-				Database.Photos.Remove (photos);
+				Database.Photos.Remove (photos as Photo []);
 
 				UpdateQuery ();
 				Log.DebugTimerPrint (timer, "HandleDeleteCommand took {0}");
@@ -2121,7 +2117,7 @@ namespace FSpot
 				return;
 			}
 
-			Photo[] photos = SelectedPhotos();
+			var photos = SelectedPhotos();
 			if (photos.Length == 0)
 				return;
 
@@ -2134,7 +2130,7 @@ namespace FSpot
 			string ok_caption = Catalog.GetString("_Remove from Catalog");
 			if (ResponseType.Ok == HigMessageDialog.RunHigConfirmation(GetToplevel (sender), DialogFlags.DestroyWithParent,
 										   MessageType.Warning, header, msg, ok_caption)) {
-				Database.Photos.Remove (photos);
+				Database.Photos.Remove (photos as Photo []);
 				UpdateQuery ();
 			}
 		}
@@ -2199,8 +2195,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);
-			count_query.Terms = FSpot.OrTerm.FromTags(tags);
+			//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;
 
 			string header;
@@ -2747,7 +2746,7 @@ namespace FSpot
 		public void HandleOpenWith (object sender, ApplicationActivatedEventArgs e)
 		{
 			GLib.AppInfo application = e.AppInfo;
-			Photo[] selected = SelectedPhotos ();
+			var selected = SelectedPhotos ();
 
 			if (selected == null || selected.Length < 1)
 				return;
@@ -2827,7 +2826,7 @@ namespace FSpot
 			}
 
 			if (create_new_versions)
-				Database.Photos.Commit (selected);
+				Database.Photos.Commit (selected as Photo []);
 
 			try {
 				application.LaunchUris (uri_list, null);
diff --git a/src/Clients/MainApp/FSpot/PhotoQuery.cs b/src/Clients/MainApp/FSpot/PhotoQuery.cs
index 7e19210..c0e99a8 100644
--- a/src/Clients/MainApp/FSpot/PhotoQuery.cs
+++ b/src/Clients/MainApp/FSpot/PhotoQuery.cs
@@ -1,9 +1,9 @@
 /*
  * FSpot.PhotoQuery.cs
- *
+ * 
  * Author(s):
- *	Larry Ewing  <lewing novell com>
- * 	Stephane Delcroix  <stephane delcroix org>
+ *  Larry Ewing  <lewing novell com>
+ *  Stephane Delcroix  <stephane delcroix org>
  *
  * This is free software. See COPYING for details.
  */
@@ -11,373 +11,219 @@
 using System;
 using System.Collections;
 using System.Collections.Generic;
+
 using FSpot.Core;
 using FSpot.Query;
+using FSpot.Database;
+
 using Hyena;
+using Hyena.Data.Sqlite;
 
 namespace FSpot {
-	public class PhotoQuery : 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];
-			}
-		}
-
-		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)
-		{
-			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);
-
-			RequestReload ();
-		}
-
-		public int Count {
-			get {
-				if (count < 0)
-					count = store.Count (temp_table);
-				return count;
-			}
-		}
-
-		public bool Contains (IPhoto item) {
-			return IndexOf (item) >= 0;
-		}
-
-		// IPhotoCollection Interface
-		public event IBrowsableCollectionChangedHandler Changed;
-		public event IBrowsableCollectionItemsChangedHandler ItemsChanged;
-
-		public IPhoto 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 IPhoto [] 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 {
-				terms = value;
-				untagged = false;
-				RequestReload ();
-			}
-		}
-
-		public DateRange Range {
-			get { return GetCondition<DateRange> (); }
-			set {
-				if (value == null && UnSetCondition<DateRange> () || value != null && SetCondition (value))
-					RequestReload ();
-			}
-		}
-
-		private bool untagged = false;
-		public bool Untagged {
-			get { return untagged; }
-			set {
-				if (untagged != value) {
-					untagged = value;
-
-					if (untagged) {
-						UnSetCondition<ConditionWrapper> ();
-						UnSetCondition<HiddenTag> ();
-					}
-
-					RequestReload ();
-				}
-			}
-		}
-
-		public RollSet RollSet {
-			get { return GetCondition<RollSet> (); }
-			set {
-				if (value == null && UnSetCondition<RollSet> () || value != null && SetCondition (value))
-					RequestReload ();
-			}
-		}
-
-		public RatingRange RatingRange {
-			get { return GetCondition<RatingRange> (); }
-			set {
-				if (value == null && UnSetCondition<RatingRange>() || value != null && SetCondition (value))
-					RequestReload ();
-			}
-		}
-
-		public HiddenTag HiddenTag {
-			get { return GetCondition<HiddenTag> (); }
-			set {
-				if (value == null && UnSetCondition<HiddenTag>() || value != null && SetCondition (value))
-					RequestReload ();
-			}
-		}
-
-		public ConditionWrapper TagTerm {
-			get { return GetCondition<ConditionWrapper> (); }
-			set {
-				if (value == null && UnSetCondition<ConditionWrapper>()
-				    || value != null && SetCondition (value)) {
-
-					if (value != null) {
-						untagged = false;
-						SetCondition (HiddenTag.ShowHiddenTag);
-					} else {
-						UnSetCondition<HiddenTag> ();
-					}
-
-					RequestReload ();
-				}
-			}
-		}
-
-		public OrderByTime OrderByTime {
-			get { return GetCondition<OrderByTime> (); }
-			set {
-				if (value != null && SetCondition (value))
-					RequestReload ();
-			}
-		}
-
-		public bool TimeOrderAsc {
-			get { return OrderByTime.Asc; }
-			set {
-				if (value != OrderByTime.Asc)
-					OrderByTime = new OrderByTime (value);
-			}
-		}
-
-		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> ();
-
-			if (Changed != null)
-				Changed (this);
-
-			Log.DebugTimerPrint (timer, "Reloading the query took {0}");
-		}
-
-		public int IndexOf (IPhoto 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 ();
-			List<int> indices = new List<int> ();
-			List<uint> items_to_search = new List<uint> ();
-			int cur;
-			foreach (DbItem dbitem in dbitems) {
-				if (reverse_lookup.TryGetValue (dbitem.Id, out cur))
-					indices.Add (cur);
-				else
-					items_to_search.Add (dbitem.Id);
-			}
-
-			if (items_to_search.Count > 0)
-				indices.AddRange (store.IndicesOf (temp_table, items_to_search.ToArray ()));
-			Log.DebugTimerPrint (timer, "IndicesOf took {0}");
-			return indices.ToArray ();
-		}
-
-		public int LookupItem (System.DateTime date)
-		{
-			return LookupItem (date, TimeOrderAsc);
-		}
-
-		private int LookupItem (System.DateTime date, bool asc)
-		{
-			if (Count == 0)
-				return -1;
-
-			uint timer = Log.DebugTimerStart ();
-			int low = 0;
-			int high = Count - 1;
-			int mid = (low + high) / 2;
-			Photo current;
-			while (low <= high) {
-				mid = (low + high) / 2;
-				if (!cache.TryGetPhoto (mid, out current))
-					//the item we're looking for is not in the cache
-					//a binary search could take up to ln2 (N/cache.SIZE) request
-					//lets reduce that number to 1
-					return store.IndexOf (temp_table, date, asc);
-
-				int comp = this [mid].Time.CompareTo (date);
-				if (!asc && comp < 0 || asc && comp > 0)
-					high = mid - 1;
-				else if (!asc && comp > 0 || asc && comp < 0)
-					low = mid + 1;
-				else
-					return mid;
-			}
-			Log.DebugTimerPrint (timer, "LookupItem took {0}");
-			if (asc)
-				return this[mid].Time < date ? mid + 1 : mid;
-			return this[mid].Time > date ? mid + 1 : mid;
-
-		}
-
-		public void Commit (int index)
-		{
-			Commit (new int [] {index});
-		}
-
-		public void Commit (int [] indexes)
-		{
-			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 ());
-		}
-
-		private void MarkChanged (object sender, DbItemEventArgs<Photo> args)
-		{
-			int [] indexes = IndicesOf (args.Items);
-
-			if (indexes.Length > 0 && ItemsChanged != null)
-				ItemsChanged (this, new BrowsableEventArgs(indexes, (args as PhotoEventArgs).Changes));
-		}
-
-		public void MarkChanged (int index, IBrowsableItemChanges changes)
-		{
-			MarkChanged (new int [] {index}, changes);
-		}
-
-		public void MarkChanged (int [] indexes, IBrowsableItemChanges changes)
-		{
-			ItemsChanged (this, new BrowsableEventArgs (indexes, changes));
-		}
-	}
+    public class PhotoQuery : DatabasePhotoListModel {
+
+        private Term terms;
+
+        int count = -1;
+
+        // Constructor
+        public PhotoQuery (HyenaSqliteConnection connection, string uuid, PhotoModelProvider provider, params IQueryCondition [] conditions)
+            : base (connection, uuid, provider)
+        {
+            SetCondition (OrderByTime.OrderByTimeDesc);
+
+            foreach (IQueryCondition condition in conditions)
+                SetCondition (condition);
+
+            RequestReload ();
+        }
+
+        public Term Terms {
+            get { return terms; }
+            set {
+                terms = value;
+                untagged = false;
+                RequestReload ();
+            }
+        }
+    
+        public DateRange Range {
+            get { return GetCondition<DateRange> (); }
+            set {
+                if (value == null && UnSetCondition<DateRange> () || value != null && SetCondition (value))
+                    RequestReload ();
+            }
+        }
+        
+        private bool untagged = false;
+        public bool Untagged {
+            get { return untagged; }
+            set {
+                if (untagged != value) {
+                    untagged = value;
+                    
+                    if (untagged) {
+                        UnSetCondition<ConditionWrapper> ();
+                        UnSetCondition<HiddenTag> ();
+                    }
+                    
+                    RequestReload ();
+                }
+            }
+        }
+
+        public RollSet RollSet {
+            get { return GetCondition<RollSet> (); }
+            set {
+                if (value == null && UnSetCondition<RollSet> () || value != null && SetCondition (value))
+                    RequestReload ();
+            }
+        }
+
+        public RatingRange RatingRange {
+            get { return GetCondition<RatingRange> (); }
+            set {
+                if (value == null && UnSetCondition<RatingRange>() || value != null && SetCondition (value))
+                    RequestReload ();
+            }
+        }
+        
+        public HiddenTag HiddenTag {
+            get { return GetCondition<HiddenTag> (); }
+            set {
+                if (value == null && UnSetCondition<HiddenTag>() || value != null && SetCondition (value))
+                    RequestReload ();
+            }
+        }
+        
+        public ConditionWrapper TagTerm {
+            get { return GetCondition<ConditionWrapper> (); }
+            set {
+                if (value == null && UnSetCondition<ConditionWrapper>()
+                    || value != null && SetCondition (value)) {
+                    
+                    if (value != null) {
+                        untagged = false;
+                        SetCondition (HiddenTag.ShowHiddenTag);
+                    } else {
+                        UnSetCondition<HiddenTag> ();
+                    }
+                    
+                    RequestReload ();
+                }
+            }
+        }
+
+        public OrderByTime OrderByTime {
+            get { return GetCondition<OrderByTime> (); }
+            set {
+                if (value != null && SetCondition (value))
+                    RequestReload ();
+            }
+        }
+
+        public bool TimeOrderAsc {
+            get { return OrderByTime.Asc; }
+            set {
+                if (value != OrderByTime.Asc)
+                    OrderByTime = new OrderByTime (value);
+            }
+        }
+
+        /*
+        private int [] IndicesOf (DbItem [] dbitems)
+        {
+            uint timer = Log.DebugTimerStart ();
+            List<int> indices = new List<int> ();
+            List<uint> items_to_search = new List<uint> ();
+            int cur;
+            foreach (DbItem dbitem in dbitems) {
+                if (reverse_lookup.TryGetValue (dbitem.Id, out cur))
+                    indices.Add (cur);
+                else
+                    items_to_search.Add (dbitem.Id);
+            }
+
+            if (items_to_search.Count > 0)
+                indices.AddRange (store.IndicesOf (temp_table, items_to_search.ToArray ()));
+            Log.DebugTimerPrint (timer, "IndicesOf took {0}");
+            return indices.ToArray ();
+        }
+         */
+        public int LookupItem (System.DateTime date)
+        {
+            return LookupItem (date, TimeOrderAsc);
+        }
+
+        private int LookupItem (System.DateTime date, bool asc)
+        {
+/*          if (Count == 0)
+                return -1;
+            
+            uint timer = Log.DebugTimerStart ();
+            int low = 0;
+            int high = Count - 1;
+            int mid = (low + high) / 2;
+            Photo current;
+            while (low <= high) {
+                mid = (low + high) / 2;
+                if (!cache.TryGetPhoto (mid, out current))
+                    //the item we're looking for is not in the cache
+                    //a binary search could take up to ln2 (N/cache.SIZE) request
+                    //lets reduce that number to 1
+                    return store.IndexOf (temp_table, date, asc);
+
+                int comp = this [mid].Time.CompareTo (date);
+                if (!asc && comp < 0 || asc && comp > 0)
+                    high = mid - 1;
+                else if (!asc && comp > 0 || asc && comp < 0)
+                    low = mid + 1;
+                else
+                    return mid;
+            }
+            Log.DebugTimerPrint (timer, "LookupItem took {0}");
+            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)
+        {
+            Commit (new int [] {index});
+        }
+
+        public void Commit (int [] indexes)
+        {
+/*          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 ());*/
+        }
+
+        private void MarkChanged (object sender, DbItemEventArgs<Photo> args)
+        {
+/*          int [] indexes = IndicesOf (args.Items);
+
+            if (indexes.Length > 0 && ItemsChanged != null)
+                ItemsChanged (this, new BrowsableEventArgs(indexes, (args as PhotoEventArgs).Changes));*/
+        }
+
+        public void MarkChanged (int index, IBrowsableItemChanges changes)
+        {
+        //  MarkChanged (new int [] {index}, changes);
+        }
+
+        public void MarkChanged (int [] indexes, IBrowsableItemChanges changes)
+        {
+        //  ItemsChanged (this, new BrowsableEventArgs (indexes, changes));
+        }
+        /*
+        public PhotoSelection Selection {
+            get; protected set;
+        }*/
+    }
 }
diff --git a/src/Clients/MainApp/FSpot/TimeAdaptor.cs b/src/Clients/MainApp/FSpot/TimeAdaptor.cs
index 236d3af..40f3f48 100644
--- a/src/Clients/MainApp/FSpot/TimeAdaptor.cs
+++ b/src/Clients/MainApp/FSpot/TimeAdaptor.cs
@@ -169,7 +169,8 @@ namespace FSpot {
 		void DoReload ()
 		{
 			Thread.Sleep (200);
-			Dictionary <int, int[]> years_tmp = query.Store.PhotosPerMonth ();
+            // TODO
+			Dictionary <int, int[]> years_tmp = new Dictionary<int, int[]> (); //query.Store.PhotosPerMonth ();
 			int startyear_tmp = Int32.MaxValue;
 			int endyear_tmp = Int32.MinValue;
 



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