3
using System.Collections.Generic;
10
[Interface ("net.launchpad.People")]
11
public interface IService {
12
string[] ListAddressBooks ();
13
ObjectPath RequestAddressBook (string address_book_id);
16
public class Service {
20
service = Bus.Session.GetObject<IService>
21
("net.launchpad.People", new ObjectPath ("/net/launchpad/People"));
24
public string[] ListAddressBooks () {
25
return service.ListAddressBooks ();
28
public AddressBook RequestAddressBook (string id) {
29
IAddressBook addressBook = Bus.Session.GetObject<IAddressBook>
30
("net.launchpad.People", service.RequestAddressBook (id));
31
return new AddressBook (addressBook);
35
[Interface ("net.launchpad.People.AddressBook")]
36
public interface IAddressBook {
37
string GetIdentifier ();
38
ObjectPath Query (string query);
41
public class AddressBook {
42
IAddressBook addressBook;
44
public AddressBook (IAddressBook addressBook) {
45
this.addressBook = addressBook;
50
return addressBook.GetIdentifier ();
54
public QueryResult Query (string query) {
55
IQueryResult queryResult = Bus.Session.GetObject<IQueryResult>
56
("net.launchpad.People", addressBook.Query (query));
57
return new QueryResult (queryResult);
61
public delegate void IResultAddedHandler (ObjectPath result_path);
62
public delegate void IResultRemovedHandler (ObjectPath result_path);
63
public delegate void IDoneHandler ();
65
[Interface ("net.launchpad.People.QueryResult")]
66
public interface IQueryResult {
67
event IResultAddedHandler ResultAdded;
68
event IResultRemovedHandler ResultRemoved;
69
event IDoneHandler Done;
71
string GetIdentifier ();
77
public enum QueryResultState {
83
public class QueryResult {
84
IQueryResult queryResult;
85
IDictionary<string, Contact> contacts_by_path;
86
IDictionary<string, Contact> contacts_by_id;
88
public QueryResult (IQueryResult queryResult) {
89
this.queryResult = queryResult;
91
this.queryResult.ResultAdded += OnResultAdded;
92
this.queryResult.ResultRemoved += OnResultRemoved;
93
this.queryResult.Done += OnDone;
95
contacts_by_path = new Dictionary<string, Contact> ();
96
contacts_by_id = new Dictionary<string, Contact> ();
100
queryResult.Close ();
103
public event ResultAddedHandler ResultAdded;
104
public event ResultRemovedHandler ResultRemoved;
105
public event StateChangedHandler StateChanged;
107
public delegate void ResultAddedHandler (Contact contact);
108
public delegate void ResultRemovedHandler (Contact contact);
109
public delegate void StateChangedHandler (QueryResult query_result, QueryResultState state);
113
return queryResult.GetIdentifier ();
117
private QueryResultState state = QueryResultState.EMPTY;
118
public QueryResultState State {
124
public void Fetch () {
125
if (state == QueryResultState.FETCHING)
128
state = QueryResultState.FETCHING;
129
StateChanged (this, state);
131
queryResult.Start ();
134
public ICollection<Contact> GetAll () {
135
return contacts_by_id.Values;
138
public Contact Get (string id) {
141
contacts_by_id.TryGetValue (id, out contact);
146
private void OnResultAdded (ObjectPath result_path) {
149
if (!contacts_by_path.TryGetValue (result_path.ToString (), out contact)) {
150
IContact c = Bus.Session.GetObject<IContact>
151
("net.launchpad.People", result_path);
152
contact = new Contact (c);
153
contacts_by_path.Add (result_path.ToString (), contact);
154
contacts_by_id.Add (contact.Id, contact);
156
ResultAdded (contact);
160
private void OnResultRemoved (ObjectPath result_path) {
163
if (!contacts_by_path.TryGetValue (result_path.ToString (), out contact)) {
164
contacts_by_path.Remove (result_path.ToString ());
165
contacts_by_id.Remove (contact.Id);
167
ResultRemoved (contact);
171
private void OnDone () {
172
if (state == QueryResultState.FETCHING) {
173
state = QueryResultState.FETCHED;
174
StateChanged (this, state);
179
public delegate void IDefinedFieldsRetrievedHandler (string[] fields);
180
public delegate void IFieldsRetrievedHandler (IDictionary<string, string> fields);
182
[Interface ("net.launchpad.People.Contact")]
183
public interface IContact {
184
event IDefinedFieldsRetrievedHandler DefinedFieldsRetrieved;
185
event IFieldsRetrievedHandler FieldsRetrieved;
187
string GetIdentifier ();
188
void RequestDefinedFields ();
189
void RequestFields (string[] fields);
192
public enum ContactState {
198
public class Contact {
200
IDictionary<string, string> fields;
202
public Contact (IContact contact) {
203
this.contact = contact;
204
this.fields = new Dictionary<string, string> ();
206
this.contact.DefinedFieldsRetrieved += OnDefinedFieldsRetrieved;
207
this.contact.FieldsRetrieved += OnFieldsRetrieved;
212
return contact.GetIdentifier ();
216
private ContactState state = ContactState.EMPTY;
217
public ContactState State {
223
public event StateChangedHandler StateChanged;
225
public delegate void StateChangedHandler (Contact contact, ContactState state);
227
public void Fetch () {
228
if (state == ContactState.FETCHING)
231
state = ContactState.FETCHING;
232
StateChanged (this, state);
234
this.contact.RequestDefinedFields ();
237
public string Get (string field) {
238
return fields[field];
241
public IDictionary<string, string> GetAll () {
242
IDictionary<string, string> fields = new Dictionary<string, string> ();
244
foreach (string key in this.fields.Keys)
245
fields.Add (key, this.fields[key]);
250
private void OnDefinedFieldsRetrieved (string[] fields) {
251
if (state != ContactState.FETCHING)
254
this.contact.RequestFields (fields);
257
private void OnFieldsRetrieved (IDictionary<string, string> fields) {
258
if (state != ContactState.FETCHING)
261
foreach (string key in fields.Keys)
262
this.fields[key] = fields[key];
264
state = ContactState.FETCHED;
265
StateChanged (this, state);