~ubuntu-branches/ubuntu/maverick/f-spot/maverick

1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
1
/*
2
 * FSpot.TimeAdaptor.cs
3
 *
4
 * Author(s):
5
 *	Larry Ewing  <lewing@novell.com>
6
 * 	Stephane Delcroix  <stephnae@delcroix.org>
7
 *
8
 * This is free software. See COPYING for details.
9
 */
10
1 by Brandon Hale
Import upstream version 0.0.12
11
using System;
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
12
using System.Threading;
13
using System.Collections.Generic;
1.1.14 by Matvey Kozhev
Import upstream version 0.4.2
14
using FSpot.Query;
1.3.6 by Iain Lane
Import upstream version 0.7.0
15
using Hyena;
1 by Brandon Hale
Import upstream version 0.0.12
16
17
namespace FSpot {
18
	public class TimeAdaptor : GroupAdaptor, FSpot.ILimitable {
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
19
		Dictionary <int, int[]> years = new Dictionary<int, int[]> ();
1 by Brandon Hale
Import upstream version 0.0.12
20
21
		public override event GlassSetHandler GlassSet;
22
		public override void SetGlass (int min)
23
		{
24
			DateTime date = DateFromIndex (min);
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
25
1 by Brandon Hale
Import upstream version 0.0.12
26
			if (GlassSet != null)
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
27
				GlassSet (this, query.LookupItem (date));
28
		}
29
1 by Brandon Hale
Import upstream version 0.0.12
30
		public void SetLimits (int min, int max) 
31
		{
32
			DateTime start = DateFromIndex (min);
1.1.5 by Andrew Mitchell
Import upstream version 0.2.0
33
34
			DateTime end = DateFromIndex(max);
35
36
			if (order_ascending)
37
				end = end.AddMonths (1);
38
			else
39
			 	end = end.AddMonths(-1);
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
40
41
			SetLimits (start, end);
42
		}
43
44
		public void SetLimits (DateTime start, DateTime end)
45
		{
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
46
			query.Range = (start > end) ? new DateRange (end, start) : new DateRange (start, end);
1 by Brandon Hale
Import upstream version 0.0.12
47
		}
48
49
		public override int Count ()
50
		{
51
			return years.Count * 12;
52
		}
53
54
		public override string GlassLabel (int item)
55
		{
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
56
			return String.Format ("{0} ({1})", DateFromIndex (item).ToString ("MMMM yyyy"), Value (item));
1 by Brandon Hale
Import upstream version 0.0.12
57
		}
58
59
		public override string TickLabel (int item)
60
		{
61
			DateTime start = DateFromIndex (item);
62
			
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
63
			if ((start.Month == 12 && !order_ascending) || (start.Month == 1 && order_ascending))
1 by Brandon Hale
Import upstream version 0.0.12
64
				return start.Year.ToString ();
65
			else 
66
				return null;
67
		}
68
69
		public override int Value (int item)
70
		{
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
71
			if (order_ascending)
72
				return years [startyear + item/12][item % 12];
73
			else
74
				return years [endyear - item/12][11 - item % 12];
1 by Brandon Hale
Import upstream version 0.0.12
75
		}
76
77
		public DateTime DateFromIndex (int item) 
78
		{
79
			item = Math.Max (item, 0);
80
			item = Math.Min (years.Count * 12 - 1, item);
1.1.2 by Andrew Mitchell
Import upstream version 0.1.5
81
			
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
82
			if (order_ascending)
83
				return DateFromIndexAscending (item);
84
85
			return DateFromIndexDescending (item);
86
		}
87
88
		private DateTime DateFromIndexAscending (int item)
89
		{
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
90
			int year = startyear + item/12;
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
91
			int month = 1 + (item % 12);
92
93
			return new DateTime(year, month, 1);
94
		}
95
96
		private DateTime DateFromIndexDescending (int item)
97
		{
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
98
			int year = endyear - item/12;
1.1.2 by Andrew Mitchell
Import upstream version 0.1.5
99
			int month = 12 - (item % 12);
1 by Brandon Hale
Import upstream version 0.0.12
100
			
1.1.2 by Andrew Mitchell
Import upstream version 0.1.5
101
			return new DateTime (year, month, DateTime.DaysInMonth (year, month)).AddDays (1.0).AddMilliseconds (-.1);
1 by Brandon Hale
Import upstream version 0.0.12
102
		}
1.1.1 by Andrew Mitchell
Import upstream version 0.1.3
103
		
104
		public override int IndexFromPhoto (FSpot.IBrowsableItem photo) 
105
		{
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
106
			if (order_ascending)
1.1.5 by Andrew Mitchell
Import upstream version 0.2.0
107
			       return IndexFromDateAscending (photo.Time);
108
109
			return IndexFromDateDescending (photo.Time);	
110
		}
111
112
		public int IndexFromDate (DateTime date)
113
		{
114
			if (order_ascending)
115
				return IndexFromDateAscending(date);
116
117
			return IndexFromDateDescending(date);
118
		}
119
120
		private int IndexFromDateAscending(DateTime date)
121
		{
122
			int year = date.Year;
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
123
			int min_year = startyear;
124
			int max_year = endyear;
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
125
126
			if (year < min_year || year > max_year) {
1.3.6 by Iain Lane
Import upstream version 0.7.0
127
				Log.DebugFormat ("TimeAdaptor.IndexFromDate year out of range[{1},{2}]: {0}", year, min_year, max_year);
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
128
				return 0;
129
			}
130
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
131
			return (year - startyear) * 12 + date.Month - 1 ;
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
132
		}
133
1.1.5 by Andrew Mitchell
Import upstream version 0.2.0
134
		private int IndexFromDateDescending(DateTime date)
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
135
		{
1.1.5 by Andrew Mitchell
Import upstream version 0.2.0
136
			int year = date.Year;
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
137
			int min_year = startyear;
138
			int max_year = endyear;
1.1.1 by Andrew Mitchell
Import upstream version 0.1.3
139
		
140
			if (year < min_year || year > max_year) {
1.3.6 by Iain Lane
Import upstream version 0.7.0
141
				Log.DebugFormat ("TimeAdaptor.IndexFromPhoto year out of range[{1},{2}]: {0}", year, min_year, max_year);
1.1.1 by Andrew Mitchell
Import upstream version 0.1.3
142
				return 0;
143
			}
144
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
145
			return 12 * (endyear - year) + 12 - date.Month;
1.1.1 by Andrew Mitchell
Import upstream version 0.1.3
146
		}
1 by Brandon Hale
Import upstream version 0.0.12
147
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
148
		public override FSpot.IBrowsableItem PhotoFromIndex (int item)
149
	       	{
150
			DateTime start = DateFromIndex (item);
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
151
			return query [query.LookupItem (start)];
1.1.3 by Andrew Mitchell
Import upstream version 0.1.10
152
		
153
		}
154
1 by Brandon Hale
Import upstream version 0.0.12
155
		public override event ChangedHandler Changed;
1.1.1 by Andrew Mitchell
Import upstream version 0.1.3
156
		
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
157
		uint timer;
1.1.12 by Andrew Mitchell
Import upstream version 0.4.0
158
		protected override void Reload () 
1 by Brandon Hale
Import upstream version 0.0.12
159
		{
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
160
			timer = Log.DebugTimerStart ();
161
			Thread reload = new Thread (new ThreadStart (DoReload));
162
			reload.IsBackground = true;
163
			reload.Priority = ThreadPriority.Lowest;
164
			reload.Start ();
165
		}
166
167
		int startyear = Int32.MaxValue, endyear = Int32.MinValue;
168
		void DoReload ()
169
		{
170
			Thread.Sleep (200);
1.1.19 by Iain Lane
Import upstream version 0.6.0.0
171
			Dictionary <int, int[]> years_tmp = query.Store.PhotosPerMonth ();
172
			int startyear_tmp = Int32.MaxValue;
173
			int endyear_tmp = Int32.MinValue;
174
			
175
			foreach (int year in years_tmp.Keys) {
176
				startyear_tmp = Math.Min (year, startyear_tmp);
177
				endyear_tmp = Math.Max (year, endyear_tmp);
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
178
			}
1.1.19 by Iain Lane
Import upstream version 0.6.0.0
179
			
180
			Gtk.Application.Invoke(delegate {
181
				
182
				years = years_tmp;
183
				startyear = startyear_tmp;
184
				endyear = endyear_tmp;
185
				
186
				if (Changed != null)
187
					Changed (this);
188
			});
189
			
1.1.17 by Chris Coulson
Import upstream version 0.5.0.2
190
			Log.DebugTimerPrint (timer, "TimeAdaptor REAL Reload took {0}");
1 by Brandon Hale
Import upstream version 0.0.12
191
		}
192
1.2.1 by Tim Retout
Import upstream version 0.4.4
193
		public TimeAdaptor (PhotoQuery query, bool order_ascending) 
194
			: base (query, order_ascending)
1.1.12 by Andrew Mitchell
Import upstream version 0.4.0
195
		{ }
1 by Brandon Hale
Import upstream version 0.0.12
196
	}
197
}