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

« back to all changes in this revision

Viewing changes to Do/src/Do.Core/UniverseManager.cs

  • Committer: Package Import Robot
  • Author(s): Christopher James Halse Rogers
  • Date: 2012-03-26 11:12:21 UTC
  • mfrom: (0.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20120326111221-1jk143fy37zxi3e4
Tags: 0.9-1
* New upstream version no longer uses deprecated internal glib headers.
  (Closes: #665537)
* [59fa37b9] Fix watch file
* [63486516] Imported Upstream version 0.9
* [8c636d84] Disable testsuite for now; requires running dbus and gconf daemons
* [e46de4b9] Remove inaccurate README.Source
* [4591d677] Add git-buildpackage configuration to default to pristine-tar

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
using Do.Universe;
29
29
using Do.Universe.Safe;
30
30
 
 
31
using Mono.Addins;
 
32
 
31
33
using UniverseCollection = System.Collections.Generic.Dictionary<string, Do.Universe.Item>;
32
34
 
33
35
namespace Do.Core
76
78
                                initialized -= value;
77
79
                        }
78
80
                }
79
 
                
 
81
 
 
82
                private Dictionary<DynamicItemSource, UniverseCollection> dynamicUniverses;
 
83
                private IEnumerable<Item> DynamicItems {
 
84
                        get {
 
85
                                return dynamicUniverses.Values.SelectMany (collection => collection.Values);
 
86
                        }
 
87
                }
 
88
 
 
89
                private void OnItemsAvailable (object sender, ItemsAvailableEventArgs args)
 
90
                {
 
91
                        DynamicItemSource source = sender as DynamicItemSource;
 
92
                        if (source == null) {
 
93
                                Log<UniverseManager>.Error ("OnItemsAvailable called from a non-DynamicItemSource.");
 
94
                                return;
 
95
                        }
 
96
                        lock (universe_lock) {
 
97
                                foreach (Item item in args.newItems) {
 
98
                                        try {
 
99
                                                dynamicUniverses[source].Add (item.UniqueId, item);
 
100
                                        } catch (ArgumentException) {
 
101
                                                Log<UniverseManager>.Error ("DynamicItemSource {0} attmpted to add duplicate Item {1}", source.Name, item.UniqueId);
 
102
                                        }
 
103
                                }
 
104
                        }
 
105
                }
 
106
 
 
107
                private void OnItemsUnavailable (object sender, ItemsUnavailableEventArgs args)
 
108
                {
 
109
                        DynamicItemSource source = sender as DynamicItemSource;
 
110
                        if (source == null) {
 
111
                                Log<UniverseManager>.Error ("OnItemsUnavailable called from a non-DynamicItemSource.");
 
112
                                return;
 
113
                        }
 
114
                        lock (universe_lock) {
 
115
                                foreach (Item item in args.unavailableItems) {
 
116
                                        dynamicUniverses[source].Remove (item.UniqueId);
 
117
                                }
 
118
                        }
 
119
                }
 
120
 
 
121
                private void OnPluginChanged (object sender, ExtensionNodeEventArgs args)
 
122
                {
 
123
                        DynamicItemSource source = args.ExtensionObject as DynamicItemSource;
 
124
                        switch (args.Change) {
 
125
                        case ExtensionChange.Add:
 
126
                                lock (universe_lock) {
 
127
                                        dynamicUniverses[source] = new UniverseCollection ();
 
128
                                }
 
129
                                source.ItemsAvailable += OnItemsAvailable;
 
130
                                source.ItemsUnavailable += OnItemsUnavailable;
 
131
                                Log<UniverseManager>.Debug ("Added new DynamicItemSource: {0}", source.Name);
 
132
                                break;
 
133
                        case ExtensionChange.Remove:
 
134
                                source.ItemsAvailable -= OnItemsAvailable;
 
135
                                source.ItemsUnavailable -= OnItemsUnavailable;
 
136
                                lock (universe_lock) {
 
137
                                        dynamicUniverses.Remove (source);
 
138
                                }
 
139
                                Log<UniverseManager>.Debug ("Removed DynamicItemSource: {0}", source.Name);
 
140
                                break;
 
141
                        }
 
142
                }
 
143
 
80
144
                public UniverseManager ()
81
145
                {
82
146
                        universe = new UniverseCollection ();
 
147
                        dynamicUniverses = new Dictionary<DynamicItemSource, Dictionary<string, Item>> ();
83
148
                        universe_lock = new object ();
84
149
                        reload_requested = new ManualResetEvent (false);
85
150
 
106
171
                        // Do the initial load of the universe.
107
172
                        ReloadUniverse ();
108
173
 
 
174
                        // Hook up to DynamicItemSource notifications
 
175
                        AddinManager.AddExtensionNodeHandler ("/Do/DynamicItemSource", OnPluginChanged);
 
176
 
109
177
                        // Notify subscribers that the universe has been loaded.
110
178
                        Services.Application.RunOnMainThread (() => {
111
179
                                BuildCompleted = true;
124
192
                
125
193
                public IEnumerable<Item> Search (string query, IEnumerable<Type> filter, Item other)
126
194
                {
127
 
                        lock (universe_lock) 
128
 
                                return Search (query, filter, universe.Values, other);
 
195
                        lock (universe_lock)
 
196
                                return Search (query, filter, universe.Values.Concat (DynamicItems), other);
129
197
                }
130
198
                
131
199
                public IEnumerable<Item> Search (string query, IEnumerable<Type> filter, IEnumerable<Item> objects)
132
200
                {
133
201
                        return Search (query, filter, objects, null);
134
202
                }
135
 
                
 
203
 
136
204
                public IEnumerable<Item> Search (string query, IEnumerable<Type> filter, IEnumerable<Item> elements, Item other)
137
205
                {
138
206
                        Item text = new ImplicitTextItem (query);