Menu

[r144]: / trunk / GoogleAPI / src / gcontacts / contactrequest.cs  Maximize  Restore  History

Download this file

506 lines (454 with data), 15.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* Copyright (c) 2006 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using System.Collections;
using System.Text;
using System.Net;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Contacts;
using System.Collections.Generic;
namespace Google.Contacts
{
/// <summary>
/// the base class for contacts elements
/// </summary>
public abstract class ContactBase : Entry
{
/// <summary>
/// returns if the Contact or Group is deleted
/// </summary>
/// <returns>the deleted status of the underlying object or false if no object set yet</returns>
public bool Deleted
{
get
{
BaseContactEntry b = this.AtomEntry as BaseContactEntry;
if (b != null)
{
return b.Deleted;
}
return false;
}
}
/// <summary>
/// returns the extended properties on this object
/// </summary>
/// <returns>the properties on the underlying object, or null if no
/// object set yet</returns>
public ExtensionCollection<ExtendedProperty> ExtendedProperties
{
get
{
BaseContactEntry b = this.AtomEntry as BaseContactEntry;
if (b != null)
{
return b.ExtendedProperties;
}
return null;
}
}
}
/// <summary>
/// the Comment entry for a Comments Feed, a feed of Comment for YouTube
/// </summary>
public class Contact : ContactBase
{
/// <summary>
/// creates the inner contact object when needed
/// </summary>
/// <returns></returns>
protected override void EnsureInnerObject()
{
if (this.AtomEntry == null)
{
this.AtomEntry = new ContactEntry();
}
}
/// <summary>
/// readonly accessor for the ContactEntry that is underneath this object.
/// </summary>
/// <returns></returns>
public ContactEntry ContactEntry
{
get
{
return this.AtomEntry as ContactEntry;
}
}
/// <summary>
/// convienience accessor to find the primary Email
/// there is no setter, to change this use the Primary Flag on
/// an individual object
/// </summary>
public EMail PrimaryEmail
{
get
{
EnsureInnerObject();
return this.ContactEntry.PrimaryEmail;
}
}
/// <summary>
/// convienience accessor to find the primary Phonenumber
/// there is no setter, to change this use the Primary Flag on
/// an individual object
/// </summary>
public PhoneNumber PrimaryPhonenumber
{
get
{
if (this.ContactEntry != null)
{
return this.ContactEntry.PrimaryPhonenumber;
}
return null;
}
}
/// <summary>
/// convienience accessor to find the primary PostalAddress
/// there is no setter, to change this use the Primary Flag on
/// an individual object
/// </summary>
public StructuredPostalAddress PrimaryPostalAddress
{
get
{
EnsureInnerObject();
return this.ContactEntry.PrimaryPostalAddress;
}
}
/// <summary>
/// convienience accessor to find the primary IMAddress
/// there is no setter, to change this use the Primary Flag on
/// an individual object
/// </summary>
public IMAddress PrimaryIMAddress
{
get
{
EnsureInnerObject();
return this.ContactEntry.PrimaryIMAddress;
}
}
/// <summary>
/// returns the groupmembership info on this object
/// </summary>
/// <returns></returns>
public ExtensionCollection<GroupMembership> GroupMembership
{
get
{
EnsureInnerObject();
return this.ContactEntry.GroupMembership;
}
}
/// <summary>
/// getter/setter for the email extension element
/// </summary>
public ExtensionCollection<EMail> Emails
{
get
{
EnsureInnerObject();
return this.ContactEntry.Emails;
}
}
/// <summary>
/// getter/setter for the IM extension element
/// </summary>
public ExtensionCollection<IMAddress> IMs
{
get
{
EnsureInnerObject();
return this.ContactEntry.IMs;
}
}
/// <summary>
/// returns the phonenumber collection
/// </summary>
public ExtensionCollection<PhoneNumber> Phonenumbers
{
get
{
EnsureInnerObject();
return this.ContactEntry.Phonenumbers;
}
}
/// <summary>
/// returns the phonenumber collection
/// </summary>
public ExtensionCollection<StructuredPostalAddress> PostalAddresses
{
get
{
EnsureInnerObject();
return this.ContactEntry.PostalAddresses;
}
}
/// <summary>
/// returns the phonenumber collection
/// </summary>
public ExtensionCollection<Organization> Organizations
{
get
{
EnsureInnerObject();
return this.ContactEntry.Organizations;
}
}
/// <summary>
/// retrieves the Uri of the Photo Link. To set this, you need to create an AtomLink object
/// and add/replace it in the atomlinks colleciton.
/// </summary>
/// <returns></returns>
public Uri PhotoUri
{
get
{
EnsureInnerObject();
return this.ContactEntry.PhotoUri;
}
}
/// <summary>
/// if a photo is present on this contact, it will have an etag associated with it,
/// that needs to be used when you want to delete or update that picture.
/// </summary>
/// <returns>the etag value as a string</returns>
public string PhotoEtag
{
get
{
EnsureInnerObject();
return this.ContactEntry.PhotoEtag;
}
set
{
EnsureInnerObject();
this.ContactEntry.PhotoEtag = value;
}
}
/// <summary>
/// returns the location associated with a contact
/// </summary>
/// <returns></returns>
public string Location
{
get
{
EnsureInnerObject();
return this.ContactEntry.Location;
}
set
{
EnsureInnerObject();
this.ContactEntry.Location = value;
}
}
/// <summary>
/// the contacts name object
/// </summary>
public Name Name
{
get
{
EnsureInnerObject();
if (this.ContactEntry.Name == null)
this.ContactEntry.Name = new Name();
return this.ContactEntry.Name;
}
set
{
EnsureInnerObject();
this.ContactEntry.Name = value;
}
}
}
/// <summary>
/// the group entry for a contacts groups Feed
/// </summary>
public class Group : ContactBase
{
/// <summary>
/// creates the inner contact object when needed
/// </summary>
/// <returns></returns>
protected override void EnsureInnerObject()
{
if (this.AtomEntry == null)
{
this.AtomEntry = new GroupEntry();
}
}
/// <summary>
/// readonly accessor for the YouTubeEntry that is underneath this object.
/// </summary>
/// <returns></returns>
public GroupEntry GroupEntry
{
get
{
return this.AtomEntry as GroupEntry;
}
}
/// <summary>
/// returns the systemgroup id, if this groupentry represents
/// a system group.
/// The values of the system group ids corresponding to these
/// groups can be found in the Reference Guide for the Contacts Data API.
/// Currently the values can be Contacts, Friends, Family and Coworkers
/// </summary>
/// <returns>the system group or null</returns>
public string SystemGroup
{
get
{
EnsureInnerObject();
return this.GroupEntry.SystemGroup;
}
}
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// The Contacts Data API provides two types of feed: contacts feed and
/// contact groups feed.
/// The feeds are private read/write feeds that can be used to view and
/// manage a user's contacts/groups. Since they are private, a programmer
/// can access them only by using an authenticated request. That is, the
/// request must contain an authentication token for the user whose
/// contacts you want to retrieve.
/// </summary>
/// <example>
/// The following code illustrates a possible use of
/// the <c>ContactsRequest</c> object:
/// <code>
/// RequestSettings settings = new RequestSettings("yourApp");
/// settings.PageSize = 50;
/// settings.AutoPaging = true;
/// ContactsRequest c = new ContactsRequest(settings);
/// Feed&lt;Contacts&gt; feed = c.GetContacts();
///
/// foreach (Contact contact in feed.Entries)
/// {
/// Console.WriteLine(contact.Title);
/// }
/// </code>
/// </example>
//////////////////////////////////////////////////////////////////////
public class ContactsRequest : FeedRequest<ContactsService>
{
/// <summary>
/// default constructor for a YouTubeRequest
/// </summary>
/// <param name="settings"></param>
public ContactsRequest(RequestSettings settings) : base(settings)
{
this.Service = new ContactsService(settings.Application);
PrepareService();
}
/// <summary>
/// returns a Feed of contacts for the default user
/// </summary>
/// <returns>a feed of Videos</returns>
public Feed<Contact> GetContacts()
{
return GetContacts(null);
}
/// <summary>
/// returns a Feed of contacts for the given user
/// </summary>
/// <param name="user">the username</param>
/// <returns>a feed of Videos</returns>
public Feed<Contact> GetContacts(string user)
{
ContactsQuery q = PrepareQuery<ContactsQuery>(ContactsQuery.CreateContactsUri(user));
return PrepareFeed<Contact>(q);
}
/// <summary>
/// returns a feed of Groups for the default user
/// </summary>
/// <returns>a feed of Videos</returns>
public Feed<Group> GetGroups()
{
return GetGroups(null);
}
/// <summary>
/// returns a feed of Groups for the given user
/// </summary>
/// <param name="user">the user for whom to retrieve the feed</param>
/// <returns>a feed of Videos</returns>
public Feed<Group> GetGroups(string user)
{
GroupsQuery q = PrepareQuery<GroupsQuery>(GroupsQuery.CreateGroupsUri(user));
return PrepareFeed<Group>(q);
}
/// <summary>
/// returns the photo stream for a given contact. If there is no photo,
/// the 404 is catched and null is returned.
/// </summary>
/// <param name="c">the contact that you want to get the photo off</param>
/// <returns></returns>
public Stream GetPhoto(Contact c)
{
Stream retStream = null;
try
{
if (c.PhotoUri != null)
{
retStream = this.Service.Query(c.PhotoUri, c.PhotoEtag);
}
}
catch (GDataRequestException e)
{
HttpWebResponse r = e.Response as HttpWebResponse;
if (r != null && r.StatusCode != HttpStatusCode.NotFound)
{
throw;
}
}
return retStream;
}
/// <summary>
/// set's the photo of a given contact entry
/// </summary>
/// <param name="c">the contact that should be modified</param>
/// <param name="photoStream">a stream to an JPG image</param>
/// <returns></returns>
public void SetPhoto(Contact c, Stream photoStream)
{
Stream res = this.Service.StreamSend(c.PhotoUri, photoStream, GDataRequestType.Update, "image/jpg", null, c.PhotoEtag);
GDataReturnStream r = res as GDataReturnStream;
if (r != null)
{
c.PhotoEtag = r.Etag;
}
res.Close();
}
/// <summary>
/// set's the photo of a given contact entry
/// </summary>
/// <param name="c">the contact that should be modified</param>
/// <param name="photoStream">a stream to an JPG image</param>
/// <param name="mimeType">specifies the type of the image, like image/jpg</param>
/// <returns></returns>
public void SetPhoto(Contact c, Stream photoStream, string mimeType)
{
Stream res = this.Service.StreamSend(c.PhotoUri, photoStream, GDataRequestType.Update, mimeType, null, c.PhotoEtag);
res.Close();
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.