2
// DynamicItemSource.cs
5
// Christopher James Halse Rogers <raof@ubuntu.com>
7
// Copyright © 2011 Christopher James Halse Rogers <raof@ubuntu.com>
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.
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.
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
25
using System.Collections.Generic;
29
public class ItemsAvailableEventArgs : EventArgs
31
public IEnumerable<Item> newItems;
34
public class ItemsUnavailableEventArgs : EventArgs
36
public IEnumerable<Item> unavailableItems;
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.
43
public abstract class DynamicItemSource : Item, IChildItemSource
45
private object event_lock = new object ();
46
private bool available_connected = false;
47
private bool unavailable_connected = false;
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
54
protected abstract void Enable ();
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.
61
protected abstract void Disable ();
63
protected bool Connected {
64
get { return available_connected && unavailable_connected; }
67
private EventHandler<ItemsAvailableEventArgs> itemsAvailable;
69
/// The <typeparamref>DynamicItemSource</typeparamref> raises this event when
70
/// new Items are available and should be added to the Universe.
72
public event EventHandler<ItemsAvailableEventArgs> ItemsAvailable {
75
if (available_connected) {
76
throw new InvalidOperationException ("Attempt to subscribe to ItemsAvailable while a subscriber already exists");
78
itemsAvailable += value;
79
available_connected = true;
87
if (!available_connected) {
90
itemsAvailable -= value;
94
available_connected = false;
99
protected void RaiseItemsAvailable (ItemsAvailableEventArgs args)
101
EventHandler<ItemsAvailableEventArgs> handler;
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.");
107
handler = itemsAvailable;
109
handler (this, args);
112
private EventHandler<ItemsUnavailableEventArgs> itemsUnavailable;
114
/// The <typeparamref>DynamicItemSource</typeparamref> raises this event when
115
/// one or more items are no longer available and should be removed from the
118
public event EventHandler<ItemsUnavailableEventArgs> ItemsUnavailable {
121
if (unavailable_connected) {
122
throw new InvalidOperationException ("Attempt to subscribe to ItemsUnavailable while a subscriber already exists");
124
itemsUnavailable += value;
125
unavailable_connected = true;
133
if (!unavailable_connected) {
136
itemsUnavailable -= value;
140
unavailable_connected = false;
145
protected void RaiseItemsUnavailable (ItemsUnavailableEventArgs args)
147
EventHandler<ItemsUnavailableEventArgs> handler;
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.");
153
handler = itemsUnavailable;
155
handler (this, args);
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
166
public abstract IEnumerable<Type> SupportedItemTypes { get; }
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.
174
public virtual IEnumerable<Item> ChildrenOfItem (Item item)