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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * FSpot.TimeAdaptor.cs
 *
 * Author(s):
 *	Larry Ewing  <lewing@novell.com>
 * 	Stephane Delcroix  <stephnae@delcroix.org>
 *
 * This is free software. See COPYING for details.
 */

using System;
using System.Threading;
using System.Collections.Generic;
using FSpot.Query;
using Hyena;

namespace FSpot {
	public class TimeAdaptor : GroupAdaptor, FSpot.ILimitable {
		Dictionary <int, int[]> years = new Dictionary<int, int[]> ();

		public override event GlassSetHandler GlassSet;
		public override void SetGlass (int min)
		{
			DateTime date = DateFromIndex (min);

			if (GlassSet != null)
				GlassSet (this, query.LookupItem (date));
		}

		public void SetLimits (int min, int max) 
		{
			DateTime start = DateFromIndex (min);

			DateTime end = DateFromIndex(max);

			if (order_ascending)
				end = end.AddMonths (1);
			else
			 	end = end.AddMonths(-1);

			SetLimits (start, end);
		}

		public void SetLimits (DateTime start, DateTime end)
		{
			query.Range = (start > end) ? new DateRange (end, start) : new DateRange (start, end);
		}

		public override int Count ()
		{
			return years.Count * 12;
		}

		public override string GlassLabel (int item)
		{
			return String.Format ("{0} ({1})", DateFromIndex (item).ToString ("MMMM yyyy"), Value (item));
		}

		public override string TickLabel (int item)
		{
			DateTime start = DateFromIndex (item);
			
			if ((start.Month == 12 && !order_ascending) || (start.Month == 1 && order_ascending))
				return start.Year.ToString ();
			else 
				return null;
		}

		public override int Value (int item)
		{
			if (order_ascending)
				return years [startyear + item/12][item % 12];
			else
				return years [endyear - item/12][11 - item % 12];
		}

		public DateTime DateFromIndex (int item) 
		{
			item = Math.Max (item, 0);
			item = Math.Min (years.Count * 12 - 1, item);
			
			if (order_ascending)
				return DateFromIndexAscending (item);

			return DateFromIndexDescending (item);
		}

		private DateTime DateFromIndexAscending (int item)
		{
			int year = startyear + item/12;
			int month = 1 + (item % 12);

			return new DateTime(year, month, 1);
		}

		private DateTime DateFromIndexDescending (int item)
		{
			int year = endyear - item/12;
			int month = 12 - (item % 12);
			
			return new DateTime (year, month, DateTime.DaysInMonth (year, month)).AddDays (1.0).AddMilliseconds (-.1);
		}
		
		public override int IndexFromPhoto (FSpot.IBrowsableItem photo) 
		{
			if (order_ascending)
			       return IndexFromDateAscending (photo.Time);

			return IndexFromDateDescending (photo.Time);	
		}

		public int IndexFromDate (DateTime date)
		{
			if (order_ascending)
				return IndexFromDateAscending(date);

			return IndexFromDateDescending(date);
		}

		private int IndexFromDateAscending(DateTime date)
		{
			int year = date.Year;
			int min_year = startyear;
			int max_year = endyear;

			if (year < min_year || year > max_year) {
				Log.DebugFormat ("TimeAdaptor.IndexFromDate year out of range[{1},{2}]: {0}", year, min_year, max_year);
				return 0;
			}

			return (year - startyear) * 12 + date.Month - 1 ;
		}

		private int IndexFromDateDescending(DateTime date)
		{
			int year = date.Year;
			int min_year = startyear;
			int max_year = endyear;
		
			if (year < min_year || year > max_year) {
				Log.DebugFormat ("TimeAdaptor.IndexFromPhoto year out of range[{1},{2}]: {0}", year, min_year, max_year);
				return 0;
			}

			return 12 * (endyear - year) + 12 - date.Month;
		}

		public override FSpot.IBrowsableItem PhotoFromIndex (int item)
	       	{
			DateTime start = DateFromIndex (item);
			return query [query.LookupItem (start)];
		
		}

		public override event ChangedHandler Changed;
		
		uint timer;
		protected override void Reload () 
		{
			timer = Log.DebugTimerStart ();
			Thread reload = new Thread (new ThreadStart (DoReload));
			reload.IsBackground = true;
			reload.Priority = ThreadPriority.Lowest;
			reload.Start ();
		}

		int startyear = Int32.MaxValue, endyear = Int32.MinValue;
		void DoReload ()
		{
			Thread.Sleep (200);
			Dictionary <int, int[]> years_tmp = query.Store.PhotosPerMonth ();
			int startyear_tmp = Int32.MaxValue;
			int endyear_tmp = Int32.MinValue;
			
			foreach (int year in years_tmp.Keys) {
				startyear_tmp = Math.Min (year, startyear_tmp);
				endyear_tmp = Math.Max (year, endyear_tmp);
			}
			
			Gtk.Application.Invoke(delegate {
				
				years = years_tmp;
				startyear = startyear_tmp;
				endyear = endyear_tmp;
				
				if (Changed != null)
					Changed (this);
			});
			
			Log.DebugTimerPrint (timer, "TimeAdaptor REAL Reload took {0}");
		}

		public TimeAdaptor (PhotoQuery query, bool order_ascending) 
			: base (query, order_ascending)
		{ }
	}
}