~ubuntu-branches/ubuntu/precise/gnome-do/precise-backports

« back to all changes in this revision

Viewing changes to Do.Universe/src/Do.Universe/DynamicItemSource.cs

  • Committer: Package Import Robot
  • Author(s): Christopher James Halse Rogers
  • Date: 2012-03-26 11:12:21 UTC
  • mfrom: (1.1.12)
  • mto: This revision was merged to the branch mainline in revision 27.
  • Revision ID: package-import@ubuntu.com-20120326111221-r38me9l4lvegczar
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:
 
1
// 
 
2
//  DynamicItemSource.cs
 
3
//  
 
4
//  Author:
 
5
//       Christopher James Halse Rogers <raof@ubuntu.com>
 
6
// 
 
7
//  Copyright © 2011 Christopher James Halse Rogers <raof@ubuntu.com>
 
8
// 
 
9
//  This library is free software; you can redistribute it and/or modify
 
10
//  it under the terms of the GNU Lesser General Public License as
 
11
//  published by the Free Software Foundation; either version 2.1 of the
 
12
//  License, or (at your option) any later version.
 
13
// 
 
14
//  This library is distributed in the hope that it will be useful, but
 
15
//  WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
17
//  Lesser General Public License for more details.
 
18
// 
 
19
//  You should have received a copy of the GNU Lesser General Public
 
20
//  License along with this library; if not, write to the Free Software
 
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
22
 
 
23
using System;
 
24
using System.Linq;
 
25
using System.Collections.Generic;
 
26
 
 
27
namespace Do.Universe
 
28
{
 
29
        public class ItemsAvailableEventArgs : EventArgs
 
30
        {
 
31
                public IEnumerable<Item> newItems;
 
32
        }
 
33
 
 
34
        public class ItemsUnavailableEventArgs : EventArgs
 
35
        {
 
36
                public IEnumerable<Item> unavailableItems;
 
37
        }
 
38
 
 
39
        /// <summary>
 
40
        /// An item source with items which may appear or disappear at any time.
 
41
        /// Unlike the standard ItemSource, this does not get periodically polled.
 
42
        /// </summary>
 
43
        public abstract class DynamicItemSource : Item, IChildItemSource
 
44
        {
 
45
                private object event_lock = new object ();
 
46
                private bool available_connected = false;
 
47
                private bool unavailable_connected = false;
 
48
 
 
49
                /// <summary>
 
50
                /// This function is called when a listener has connected to the ItemsAvailable and ItemsUnavailable event.
 
51
                /// The <typeparamref>DynamicItemSource</typeparamref> MUST NOT raise either event until this has been
 
52
                /// called.
 
53
                /// </summary>
 
54
                protected abstract void Enable ();
 
55
 
 
56
                /// <summary>
 
57
                /// This function is called when a listener has disconnected to the ItemsAvailable and ItemsUnavailable event.
 
58
                /// The <typeparamref>DynamicItemSource</typeparamref> MUST NOT raise either event after this has been
 
59
                /// called until a subsequente Enable () call is made.
 
60
                /// </summary>
 
61
                protected abstract void Disable ();
 
62
 
 
63
                protected bool Connected {
 
64
                        get { return available_connected && unavailable_connected; }
 
65
                }
 
66
 
 
67
                private EventHandler<ItemsAvailableEventArgs> itemsAvailable;
 
68
                /// <summary>
 
69
                /// The <typeparamref>DynamicItemSource</typeparamref> raises this event when
 
70
                /// new Items are available and should be added to the Universe.
 
71
                /// </summary>
 
72
                public event EventHandler<ItemsAvailableEventArgs> ItemsAvailable {
 
73
                        add {
 
74
                                lock (event_lock) {
 
75
                                        if (available_connected) {
 
76
                                                throw new InvalidOperationException ("Attempt to subscribe to ItemsAvailable while a subscriber already exists");
 
77
                                        }
 
78
                                        itemsAvailable += value;
 
79
                                        available_connected = true;
 
80
                                        if (Connected) {
 
81
                                                Enable ();
 
82
                                        }
 
83
                                }
 
84
                        }
 
85
                        remove {
 
86
                                lock (event_lock) {
 
87
                                        if (!available_connected) {
 
88
                                                return;
 
89
                                        }
 
90
                                        itemsAvailable -= value;
 
91
                                        if (Connected) {
 
92
                                                Disable ();
 
93
                                        }
 
94
                                        available_connected = false;
 
95
                                }
 
96
                        }
 
97
                }
 
98
 
 
99
                protected void RaiseItemsAvailable (ItemsAvailableEventArgs args)
 
100
                {
 
101
                        EventHandler<ItemsAvailableEventArgs> handler;
 
102
                        lock (event_lock) {
 
103
                                if (!Connected) {
 
104
                                        // FIXME: This should really be a Log message rather than an exception
 
105
                                        throw new InvalidOperationException ("Attempted to raise ItemsAvailable without a subscriber connected.");
 
106
                                }
 
107
                                handler = itemsAvailable;
 
108
                        }
 
109
                        handler (this, args);
 
110
                }
 
111
 
 
112
                private EventHandler<ItemsUnavailableEventArgs> itemsUnavailable;
 
113
                /// <summary>
 
114
                /// The <typeparamref>DynamicItemSource</typeparamref> raises this event when
 
115
                /// one or more items are no longer available and should be removed from the
 
116
                /// Universe.
 
117
                /// </summary>
 
118
                public event EventHandler<ItemsUnavailableEventArgs> ItemsUnavailable {
 
119
                        add {
 
120
                                lock (event_lock) {
 
121
                                        if (unavailable_connected) {
 
122
                                                throw new InvalidOperationException ("Attempt to subscribe to ItemsUnavailable while a subscriber already exists");
 
123
                                        }
 
124
                                        itemsUnavailable += value;
 
125
                                        unavailable_connected = true;
 
126
                                        if (Connected) {
 
127
                                                Enable ();
 
128
                                        }
 
129
                                }
 
130
                        }
 
131
                        remove {
 
132
                                lock (event_lock) {
 
133
                                        if (!unavailable_connected) {
 
134
                                                return;
 
135
                                        }
 
136
                                        itemsUnavailable -= value;
 
137
                                        if (Connected) {
 
138
                                                Disable ();
 
139
                                        }
 
140
                                        unavailable_connected = false;
 
141
                                }
 
142
                        }
 
143
                }
 
144
 
 
145
                protected void RaiseItemsUnavailable (ItemsUnavailableEventArgs args)
 
146
                {
 
147
                        EventHandler<ItemsUnavailableEventArgs> handler;
 
148
                        lock (event_lock) {
 
149
                                if (!Connected) {
 
150
                                        // FIXME: This should really be a Log message rather than an exception
 
151
                                        throw new InvalidOperationException ("Attempted to raise ItemsUnavailable without a subscriber connected.");
 
152
                                }
 
153
                                handler = itemsUnavailable;
 
154
                        }
 
155
                        handler (this, args);
 
156
                }
 
157
 
 
158
                /// <value>
 
159
                /// Item sub-types provided/supported by this source. These include any
 
160
                /// types of items provided through ItemsAvailable, and the types of items
 
161
                /// that this source will provide children for.  Please provide types as
 
162
                /// close as possible in ancestry to the static types of items this source
 
163
                /// provides/supports (e.g.  FirefoxBookmarkItem instead of Item or
 
164
                /// BookmarkItem).
 
165
                /// </value>
 
166
                public abstract IEnumerable<Type> SupportedItemTypes { get; }
 
167
 
 
168
                /// <summary>
 
169
                /// Provides a collection of children of an item. Item is guaranteed to be a
 
170
                /// subtype of a type in SupportedItemTypes.
 
171
                /// An empty enumerable is ok---it signifies that no children are provided for
 
172
                /// the Item argument.
 
173
                /// </summary>
 
174
                public virtual IEnumerable<Item> ChildrenOfItem (Item item)
 
175
                {
 
176
                        yield break;
 
177
                }
 
178
 
 
179
        }
 
180
}
 
181