~ubuntu-branches/ubuntu/trusty/gnome-do/trusty

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// UniverseManager.cs
// 
// GNOME Do is the legal property of its developers. Please refer to the
// COPYRIGHT file distributed with this source distribution.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Collections.Generic;

using Do;
using Do.Platform;
using Do.Universe;
using Do.Universe.Safe;

using Mono.Addins;

using UniverseCollection = System.Collections.Generic.Dictionary<string, Do.Universe.Item>;

namespace Do.Core
{
	
	public class UniverseManager
	{

		Thread update_thread;
		UniverseCollection universe;
		EventHandler initialized;
		object universe_lock;
		ManualResetEvent reload_requested;

		const float epsilon = 0.00001f;
		
		/// <value>
		/// The amount of time between updates.
		/// </value>
		TimeSpan UpdateTimeout {
			get {
				int minutes = Services.System.GetOnBatteryPower () ? 10 : 2;
				return new TimeSpan (0, minutes, 0);
			}
		}
		
		/// <value>
		/// The amount of time spent on each update.
		/// </value>
		TimeSpan UpdateRunTime {
			get {
				int milliseconds = Services.System.GetOnBatteryPower () ? 600 : 200;
				return new TimeSpan (0, 0, 0, 0, milliseconds);
			}
		}
		
		bool BuildCompleted { get; set; }

		public event EventHandler Initialized {
			add {
				if (BuildCompleted)
					value (this, EventArgs.Empty);
				initialized += value;
			}
			remove {
				initialized -= value;
			}
		}

		private Dictionary<DynamicItemSource, UniverseCollection> dynamicUniverses;
		private IEnumerable<Item> DynamicItems {
			get {
				return dynamicUniverses.Values.SelectMany (collection => collection.Values);
			}
		}

		private void OnItemsAvailable (object sender, ItemsAvailableEventArgs args)
		{
			DynamicItemSource source = sender as DynamicItemSource;
			if (source == null) {
				Log<UniverseManager>.Error ("OnItemsAvailable called from a non-DynamicItemSource.");
				return;
			}
			lock (universe_lock) {
				foreach (Item item in args.newItems) {
					try {
						dynamicUniverses[source].Add (item.UniqueId, item);
					} catch (ArgumentException) {
						Log<UniverseManager>.Error ("DynamicItemSource {0} attmpted to add duplicate Item {1}", source.Name, item.UniqueId);
					}
				}
			}
		}

		private void OnItemsUnavailable (object sender, ItemsUnavailableEventArgs args)
		{
			DynamicItemSource source = sender as DynamicItemSource;
			if (source == null) {
				Log<UniverseManager>.Error ("OnItemsUnavailable called from a non-DynamicItemSource.");
				return;
			}
			lock (universe_lock) {
				foreach (Item item in args.unavailableItems) {
					dynamicUniverses[source].Remove (item.UniqueId);
				}
			}
		}

		private void OnPluginChanged (object sender, ExtensionNodeEventArgs args)
		{
			DynamicItemSource source = args.ExtensionObject as DynamicItemSource;
			switch (args.Change) {
			case ExtensionChange.Add:
				lock (universe_lock) {
					dynamicUniverses[source] = new UniverseCollection ();
				}
				source.ItemsAvailable += OnItemsAvailable;
				source.ItemsUnavailable += OnItemsUnavailable;
				Log<UniverseManager>.Debug ("Added new DynamicItemSource: {0}", source.Name);
				break;
			case ExtensionChange.Remove:
				source.ItemsAvailable -= OnItemsAvailable;
				source.ItemsUnavailable -= OnItemsUnavailable;
				lock (universe_lock) {
					dynamicUniverses.Remove (source);
				}
				Log<UniverseManager>.Debug ("Removed DynamicItemSource: {0}", source.Name);
				break;
			}
		}

		public UniverseManager ()
		{
			universe = new UniverseCollection ();
			dynamicUniverses = new Dictionary<DynamicItemSource, Dictionary<string, Item>> ();
			universe_lock = new object ();
			reload_requested = new ManualResetEvent (false);

			update_thread = new Thread (new ThreadStart (UniverseUpdateLoop));
			update_thread.IsBackground = true;
			update_thread.Priority = ThreadPriority.Lowest;
			update_thread.Name = "Universe Update Dispatcher";
			
			Services.Network.StateChanged += OnNetworkStateChanged;
		}

		void OnNetworkStateChanged (object sender, NetworkStateChangedEventArgs e)
		{
			Reload ();
		}
			
		public void Initialize ()
		{
			Services.Application.RunOnThread (InitializeAsync);
		}

		public void InitializeAsync ()
		{
			// Do the initial load of the universe.
			ReloadUniverse ();

			// Hook up to DynamicItemSource notifications
			AddinManager.AddExtensionNodeHandler ("/Do/DynamicItemSource", OnPluginChanged);

			// Notify subscribers that the universe has been loaded.
			Services.Application.RunOnMainThread (() => {
				BuildCompleted = true;
				if (initialized != null)
					initialized (this, EventArgs.Empty);
			});

			// Start the update thread.
			update_thread.Start ();
		}

		public IEnumerable<Item> Search (string query, IEnumerable<Type> filter)
		{
			return Search (query, filter, (Item) null);
		}
		
		public IEnumerable<Item> Search (string query, IEnumerable<Type> filter, Item other)
		{
			lock (universe_lock)
				return Search (query, filter, universe.Values.Concat (DynamicItems), other);
		}
		
		public IEnumerable<Item> Search (string query, IEnumerable<Type> filter, IEnumerable<Item> objects)
		{
			return Search (query, filter, objects, null);
		}

		public IEnumerable<Item> Search (string query, IEnumerable<Type> filter, IEnumerable<Item> elements, Item other)
		{
			Item text = new ImplicitTextItem (query);
			string lquery = query.ToLower ();

			return elements
				.Where (element => element.PassesTypeFilter (filter) && epsilon < Math.Abs (element.UpdateRelevance (lquery, other)))
				.OrderByDescending (element => element.Relevance)
				.Concat (text.PassesTypeFilter (filter) ? new [] { text } : Enumerable.Empty<Item> ())
				.ToArray ();
		}
		
		/// <summary>
		/// Returns if an object likely contains children.
		/// </summary>
		/// <param name="o">
		/// A <see cref="Item"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/>
		/// </returns>
		public bool ItemHasChildren (Item item)
		{
			return item.HasChildren ();
		}
		
		/// <summary>
		/// Continuously updates the universe on a worker thread.
		/// </summary>
		void UniverseUpdateLoop ()
		{
			Random rand = new Random ();
			DateTime startUpdate;

			while (true) {
				if (reload_requested.WaitOne (UpdateTimeout)) {
					// We've been asked to do a full reload.  Kick this off, then go back to waiting for the timeout.
					reload_requested.Reset ();
					ReloadUniverse ();
					continue;
				}

				if (Do.Controller.IsSummoned)
					continue;
				startUpdate = DateTime.UtcNow;

				if (rand.Next (10) == 0) {
					ReloadActions (universe);
				}

				foreach (ItemSource source in PluginManager.ItemSources) {
					ReloadSource (source, universe);

					if (UpdateRunTime < DateTime.UtcNow - startUpdate) {
						// We've exhausted our update timer.  Continue after UpdateTimeout.
						if (reload_requested.WaitOne (UpdateTimeout)) {
							// We've been asked to reload the universe; break back to the start of the loop
							break;
						}
						// Start the update timer again, and continue reloading sources…
						startUpdate = DateTime.UtcNow;
					}
				}
			}
		}
		
		/// <summary>
		/// Reloads all actions in the universe.
		/// </summary>
		void ReloadActions (UniverseCollection universe)
		{
			Log<UniverseManager>.Debug ("Reloading actions...");
			lock (universe_lock) {
				foreach (Act action in PluginManager.Actions) {
					if (universe.ContainsKey (action.UniqueId))
						universe.Remove (action.UniqueId);
				}
				foreach (Act action in PluginManager.Actions)
					universe [action.UniqueId] = action;
			}
		}
		
		/// <summary>
		/// Updates an item source and syncs it into the universe. This should
		/// not be called on the main thread to avoid blocking the UI if the
		/// item source takes a long time to update.
		/// </summary>
		void ReloadSource (ItemSource source, UniverseCollection universe)
		{
			SafeItemSource safeSource;
			IEnumerable<Item> oldItems, newItems;

			if (source == null) throw new ArgumentNullException ("source");
			
			safeSource = source.RetainSafe ();
			oldItems = safeSource.Items;
			// We call UpdateItems outside of the lock so as not to block other
			// threads in contention for the lock if UpdateItems blocks.
			Log<UniverseManager>.Debug ("Reloading item source \"{0}\"...", safeSource.Name);
			safeSource.UpdateItems ();
			newItems = safeSource.Items;
			
			lock (universe_lock) {
				foreach (Item item in oldItems) {
					if (universe.ContainsKey (item.UniqueId))
						universe.Remove (item.UniqueId);
				}
				foreach (Item item in newItems) {
					universe  [item.UniqueId] = item;
				}
			}
		}
		
		void ReloadUniverse ()
		{
			Log<UniverseManager>.Info ("Reloading universe...");
			
			// A new temporary universe is created so that searches made during the reload (as threaded 
			// searches are allowed will not see an interuption in available items). Additionally this 
			// serves to clear out unused items that are orphaned from their item service.
			UniverseCollection tmpUniverse = new UniverseCollection ();
			ReloadActions (tmpUniverse);
			PluginManager.ItemSources.ForEach (source => ReloadSource (source, tmpUniverse));
			
			// Clearing the old universe is not needed and considered harmful as enumerables in existence
			// already will be based off the old universe. Clearing it may cause an exception to be thrown.
			// Once those enumerables are destroyed, so too will the old universe.
			universe = tmpUniverse;
			Log<UniverseManager>.Info ("Universe contains {0} items.", universe.Count);
		}
		
		/// <summary>
		/// Add a list of Items to the universe
		/// </summary>
		/// <param name="items">
		/// A <see cref="IEnumerable`1"/>
		/// </param>
		public void AddItems (IEnumerable<Item> items)
		{
			lock (universe_lock) {
				foreach (Item item in items) {
					if (universe.ContainsKey (item.UniqueId)) continue;
					universe [item.UniqueId] = item;
				}
			}
		}

		/// <summary>
		/// Remove a list of Items from the universe.  This removal does not prevent these
		/// items from returning to the universe at a future date.
		/// </summary>
		/// <param name="items">
		/// A <see cref="IEnumerable`1"/>
		/// </param>
		public void DeleteItems (IEnumerable<Item> items)
		{
			lock (universe_lock) {
				foreach (Item item in items) {
					universe.Remove (item.UniqueId);
				}
			}
		}

		/// <summary>
		/// Attempts to get an Item for a given UniqueId.
		/// </summary>
		/// <param name="UniqueId">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="item">
		/// A <see cref="Item"/>
		/// </param>
		public bool TryGetItemForUniqueId (string uid, out Item element)
		{
			lock (universe_lock) {
				if (universe.ContainsKey (uid)) {
					element = universe [uid];
				} else {
					element = null;
				}
			}
			return element == null;
		}
		
		/// <summary>
		/// Causes the universe to be rebuilt in the background.
		/// </summary>
		public void Reload ()
		{
			Log<UniverseManager>.Info ("Requesting universe reload");
			reload_requested.Set ();
		}
	}
}