~people-project/people-sharp/trunk

« back to all changes in this revision

Viewing changes to people-sharp/People.cs

  • Committer: Johann Prieur
  • Date: 2009-02-23 20:18:09 UTC
  • Revision ID: johann.prieur@gmail.com-20090223201809-l3mtwgmp35aucqp9
Splitted People.cs into Service.cs, AddressBook.cs, QueryResult.cs and Contact.cs
Cleaned up imports

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
using System;
3
 
using System.Collections.Generic;
4
 
 
5
 
using NDesk.DBus;
6
 
 
7
 
 
8
 
namespace People {
9
 
 
10
 
        [Interface ("net.launchpad.People")]
11
 
        public interface IService {
12
 
                string[] ListAddressBooks ();
13
 
                ObjectPath RequestAddressBook (string address_book_id);
14
 
        }
15
 
 
16
 
        public class Service {
17
 
                IService service;
18
 
 
19
 
                public Service () {
20
 
                        service = Bus.Session.GetObject<IService>
21
 
                                ("net.launchpad.People", new ObjectPath ("/net/launchpad/People"));
22
 
                }
23
 
 
24
 
                public string[] ListAddressBooks () {
25
 
                        return service.ListAddressBooks ();
26
 
                }
27
 
 
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);
32
 
                }
33
 
        }
34
 
 
35
 
        [Interface ("net.launchpad.People.AddressBook")]
36
 
        public interface IAddressBook {
37
 
                string GetIdentifier ();
38
 
                ObjectPath Query (string query);
39
 
        }
40
 
 
41
 
        public class AddressBook {
42
 
                IAddressBook addressBook;
43
 
 
44
 
                public AddressBook (IAddressBook addressBook) {
45
 
                        this.addressBook = addressBook;
46
 
                }
47
 
 
48
 
                public string Id {
49
 
                        get {
50
 
                                return addressBook.GetIdentifier ();
51
 
                        }
52
 
                }
53
 
 
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);
58
 
                }
59
 
        }
60
 
 
61
 
        public delegate void IResultAddedHandler (ObjectPath result_path);
62
 
        public delegate void IResultRemovedHandler (ObjectPath result_path);
63
 
        public delegate void IDoneHandler ();
64
 
 
65
 
        [Interface ("net.launchpad.People.QueryResult")]
66
 
        public interface IQueryResult {
67
 
                event IResultAddedHandler ResultAdded;
68
 
                event IResultRemovedHandler ResultRemoved;
69
 
                event IDoneHandler Done;
70
 
 
71
 
                string GetIdentifier ();
72
 
                void Start ();
73
 
                void Stop ();
74
 
                void Close ();
75
 
        }
76
 
 
77
 
        public enum QueryResultState {
78
 
                EMPTY,
79
 
                FETCHING,
80
 
                FETCHED
81
 
        }
82
 
 
83
 
        public class QueryResult {
84
 
                IQueryResult queryResult;
85
 
                IDictionary<string, Contact> contacts_by_path;
86
 
                IDictionary<string, Contact> contacts_by_id;
87
 
 
88
 
                public QueryResult (IQueryResult queryResult) {
89
 
                        this.queryResult = queryResult;
90
 
 
91
 
                        this.queryResult.ResultAdded += OnResultAdded;
92
 
                        this.queryResult.ResultRemoved += OnResultRemoved;
93
 
                        this.queryResult.Done += OnDone;
94
 
 
95
 
                        contacts_by_path = new Dictionary<string, Contact> ();
96
 
                        contacts_by_id = new Dictionary<string, Contact> ();
97
 
                }
98
 
 
99
 
                ~QueryResult () {
100
 
                        queryResult.Close ();
101
 
                }
102
 
 
103
 
                public event ResultAddedHandler ResultAdded;
104
 
                public event ResultRemovedHandler ResultRemoved;
105
 
                public event StateChangedHandler StateChanged;
106
 
 
107
 
                public delegate void ResultAddedHandler (Contact contact);
108
 
                public delegate void ResultRemovedHandler (Contact contact);
109
 
                public delegate void StateChangedHandler (QueryResult query_result, QueryResultState state);
110
 
 
111
 
                public string Id {
112
 
                        get {
113
 
                                return queryResult.GetIdentifier ();
114
 
                        }
115
 
                }
116
 
 
117
 
                private QueryResultState state = QueryResultState.EMPTY;
118
 
                public QueryResultState State {
119
 
                        get {
120
 
                                return state;
121
 
                        }
122
 
                }
123
 
 
124
 
                public void Fetch () {
125
 
                        if (state == QueryResultState.FETCHING)
126
 
                                return;
127
 
 
128
 
                        state = QueryResultState.FETCHING;
129
 
                        StateChanged (this, state);
130
 
 
131
 
                        queryResult.Start ();
132
 
                }
133
 
 
134
 
                public ICollection<Contact> GetAll () {
135
 
                        return contacts_by_id.Values;
136
 
                }
137
 
 
138
 
                public Contact Get (string id) {
139
 
                        Contact contact;
140
 
 
141
 
                        contacts_by_id.TryGetValue (id, out contact);
142
 
 
143
 
                        return contact;
144
 
                }
145
 
 
146
 
                private void OnResultAdded (ObjectPath result_path) {
147
 
                        Contact contact;
148
 
 
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);
155
 
 
156
 
                                ResultAdded (contact);
157
 
                        }
158
 
                }
159
 
 
160
 
                private void OnResultRemoved (ObjectPath result_path) {
161
 
                        Contact contact;
162
 
 
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);
166
 
 
167
 
                                ResultRemoved (contact);
168
 
                        }
169
 
                }
170
 
 
171
 
                private void OnDone () {
172
 
                        if (state == QueryResultState.FETCHING) {
173
 
                                state = QueryResultState.FETCHED;
174
 
                                StateChanged (this, state);
175
 
                        }
176
 
                }
177
 
        }
178
 
 
179
 
        public delegate void IDefinedFieldsRetrievedHandler (string[] fields);
180
 
        public delegate void IFieldsRetrievedHandler (IDictionary<string, string> fields);
181
 
 
182
 
        [Interface ("net.launchpad.People.Contact")]
183
 
        public interface IContact {
184
 
                event IDefinedFieldsRetrievedHandler DefinedFieldsRetrieved;
185
 
                event IFieldsRetrievedHandler FieldsRetrieved;
186
 
 
187
 
                string GetIdentifier ();
188
 
                void RequestDefinedFields ();
189
 
                void RequestFields (string[] fields);
190
 
        }
191
 
 
192
 
        public enum ContactState {
193
 
                EMPTY,
194
 
                FETCHING,
195
 
                FETCHED
196
 
        }
197
 
 
198
 
        public class Contact {
199
 
                IContact contact;
200
 
                IDictionary<string, string> fields;
201
 
 
202
 
                public Contact (IContact contact) {
203
 
                        this.contact = contact;
204
 
                        this.fields = new Dictionary<string, string> ();
205
 
 
206
 
                        this.contact.DefinedFieldsRetrieved += OnDefinedFieldsRetrieved;
207
 
                        this.contact.FieldsRetrieved += OnFieldsRetrieved;
208
 
                }
209
 
 
210
 
                public string Id {
211
 
                        get {
212
 
                                return contact.GetIdentifier ();
213
 
                        }
214
 
                }
215
 
 
216
 
                private ContactState state = ContactState.EMPTY;
217
 
                public ContactState State {
218
 
                        get {
219
 
                                return state;
220
 
                        }
221
 
                }
222
 
 
223
 
                public event StateChangedHandler StateChanged;
224
 
 
225
 
                public delegate void StateChangedHandler (Contact contact, ContactState state);
226
 
 
227
 
                public void Fetch () {
228
 
                        if (state == ContactState.FETCHING)
229
 
                                return;
230
 
 
231
 
                        state = ContactState.FETCHING;
232
 
                        StateChanged (this, state);
233
 
 
234
 
                        this.contact.RequestDefinedFields ();
235
 
                }
236
 
 
237
 
                public string Get (string field) {
238
 
                        return fields[field];
239
 
                }
240
 
 
241
 
                public IDictionary<string, string> GetAll () {
242
 
                        IDictionary<string, string> fields = new Dictionary<string, string> ();
243
 
 
244
 
                        foreach (string key in this.fields.Keys)
245
 
                                fields.Add (key, this.fields[key]);
246
 
 
247
 
                        return fields;
248
 
                }
249
 
 
250
 
                private void OnDefinedFieldsRetrieved (string[] fields) {
251
 
                        if (state != ContactState.FETCHING)
252
 
                                return;
253
 
 
254
 
                        this.contact.RequestFields (fields);
255
 
                }
256
 
 
257
 
                private void OnFieldsRetrieved (IDictionary<string, string> fields) {
258
 
                        if (state != ContactState.FETCHING)
259
 
                                return;
260
 
 
261
 
                        foreach (string key in fields.Keys)
262
 
                                this.fields[key] = fields[key];
263
 
 
264
 
                        state = ContactState.FETCHED;
265
 
                        StateChanged (this, state);
266
 
                }
267
 
 
268
 
        }
269
 
 
270
 
}