~ubuntu-branches/ubuntu/oneiric/libgdata/oneiric-backports

« back to all changes in this revision

Viewing changes to gdata/services/contacts/gdata-contacts-service.c

  • Committer: Package Import Robot
  • Author(s): Evan Broder
  • Date: 2011-11-15 21:59:48 UTC
  • mfrom: (4.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20111115215948-e17s889ocgu5fv4f
Tags: 0.10.1-1~oneiric1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
157
157
#include "gdata-private.h"
158
158
#include "gdata-query.h"
159
159
 
 
160
static GList *get_authorization_domains (void);
 
161
 
 
162
_GDATA_DEFINE_AUTHORIZATION_DOMAIN (contacts, "cp", "https://www.google.com/m8/feeds/")
160
163
G_DEFINE_TYPE_WITH_CODE (GDataContactsService, gdata_contacts_service, GDATA_TYPE_SERVICE,
161
164
                         G_IMPLEMENT_INTERFACE (GDATA_TYPE_BATCHABLE, NULL))
162
165
 
164
167
gdata_contacts_service_class_init (GDataContactsServiceClass *klass)
165
168
{
166
169
        GDataServiceClass *service_class = GDATA_SERVICE_CLASS (klass);
167
 
        service_class->service_name = "cp";
168
170
        service_class->api_version = "3";
 
171
        service_class->get_authorization_domains = get_authorization_domains;
169
172
}
170
173
 
171
174
static void
174
177
        /* Nothing to see here */
175
178
}
176
179
 
 
180
static GList *
 
181
get_authorization_domains (void)
 
182
{
 
183
        return g_list_prepend (NULL, get_contacts_authorization_domain ());
 
184
}
 
185
 
177
186
/**
178
187
 * gdata_contacts_service_new:
179
 
 * @client_id: your application's client ID
180
 
 *
181
 
 * Creates a new #GDataContactsService. The @client_id must be unique for your application, and as registered with Google.
182
 
 *
183
 
 * Return value: a new #GDataContactsService, or %NULL
184
 
 *
185
 
 * Since: 0.2.0
186
 
 **/
 
188
 * @authorizer: (allow-none): a #GDataAuthorizer to authorize the service's requests, or %NULL
 
189
 *
 
190
 * Creates a new #GDataContactsService using the given #GDataAuthorizer. If @authorizer is %NULL, all requests are made as an unauthenticated user.
 
191
 *
 
192
 * Return value: a new #GDataContactsService, or %NULL; unref with g_object_unref()
 
193
 *
 
194
 * Since: 0.9.0
 
195
 */
187
196
GDataContactsService *
188
 
gdata_contacts_service_new (const gchar *client_id)
 
197
gdata_contacts_service_new (GDataAuthorizer *authorizer)
189
198
{
190
 
        g_return_val_if_fail (client_id != NULL, NULL);
 
199
        g_return_val_if_fail (authorizer == NULL || GDATA_IS_AUTHORIZER (authorizer), NULL);
191
200
 
192
201
        return g_object_new (GDATA_TYPE_CONTACTS_SERVICE,
193
 
                             "client-id", client_id,
 
202
                             "authorizer", authorizer,
194
203
                             NULL);
195
204
}
196
205
 
197
206
/**
 
207
 * gdata_contacts_service_get_primary_authorization_domain:
 
208
 *
 
209
 * The primary #GDataAuthorizationDomain for interacting with Google Contacts. This will not normally need to be used, as it's used internally
 
210
 * by the #GDataContactsService methods. However, if using the plain #GDataService methods to implement custom queries or requests which libgdata
 
211
 * does not support natively, then this domain may be needed to authorize the requests.
 
212
 *
 
213
 * The domain never changes, and is interned so that pointer comparison can be used to differentiate it from other authorization domains.
 
214
 *
 
215
 * Return value: (transfer none): the service's authorization domain
 
216
 *
 
217
 * Since: 0.9.0
 
218
 */
 
219
GDataAuthorizationDomain *
 
220
gdata_contacts_service_get_primary_authorization_domain (void)
 
221
{
 
222
        return get_contacts_authorization_domain ();
 
223
}
 
224
 
 
225
/**
198
226
 * gdata_contacts_service_query_contacts:
199
227
 * @self: a #GDataContactsService
200
228
 * @query: (allow-none): a #GDataQuery with the query parameters, or %NULL
201
 
 * @cancellable: optional #GCancellable object, or %NULL
202
 
 * @progress_callback: (scope call): a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
 
229
 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
 
230
 * @progress_callback: (allow-none) (scope call) (closure progress_user_data): a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
203
231
 * @progress_user_data: (closure): data to pass to the @progress_callback function
204
232
 * @error: a #GError, or %NULL
205
233
 *
224
252
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
225
253
 
226
254
        /* Ensure we're authenticated first */
227
 
        if (gdata_service_is_authenticated (GDATA_SERVICE (self)) == FALSE) {
 
255
        if (gdata_authorizer_is_authorized_for_domain (gdata_service_get_authorizer (GDATA_SERVICE (self)),
 
256
                                                       get_contacts_authorization_domain ()) == FALSE) {
228
257
                g_set_error_literal (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED,
229
258
                                     _("You must be authenticated to query contacts."));
230
259
                return NULL;
231
260
        }
232
261
 
233
262
        request_uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/contacts/default/full", NULL);
234
 
        feed = gdata_service_query (GDATA_SERVICE (self), request_uri, GDATA_QUERY (query),
 
263
        feed = gdata_service_query (GDATA_SERVICE (self), get_contacts_authorization_domain (), request_uri, GDATA_QUERY (query),
235
264
                                    GDATA_TYPE_CONTACTS_CONTACT, cancellable, progress_callback, progress_user_data, error);
236
265
        g_free (request_uri);
237
266
 
239
268
}
240
269
 
241
270
/**
242
 
 * gdata_contacts_service_query_contacts_async: (skip)
 
271
 * gdata_contacts_service_query_contacts_async:
243
272
 * @self: a #GDataContactsService
244
273
 * @query: (allow-none): a #GDataQuery with the query parameters, or %NULL
245
 
 * @cancellable: optional #GCancellable object, or %NULL
246
 
 * @progress_callback: a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
 
274
 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
 
275
 * @progress_callback: (allow-none) (closure progress_user_data): a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
247
276
 * @progress_user_data: (closure): data to pass to the @progress_callback function
 
277
 * @destroy_progress_user_data: (allow-none): the function to call when @progress_callback will not be called any more, or %NULL. This function will be
 
278
 * called with @progress_user_data as a parameter and can be used to free any memory allocated for it.
248
279
 * @callback: a #GAsyncReadyCallback to call when the query is finished
249
280
 * @user_data: (closure): data to pass to the @callback function
250
281
 *
254
285
 * For more details, see gdata_contacts_service_query_contacts(), which is the synchronous version of this function,
255
286
 * and gdata_service_query_async(), which is the base asynchronous query function.
256
287
 *
257
 
 * Since: 0.2.0
 
288
 * Since: 0.9.1
258
289
 **/
259
290
void
260
291
gdata_contacts_service_query_contacts_async (GDataContactsService *self, GDataQuery *query, GCancellable *cancellable,
261
292
                                             GDataQueryProgressCallback progress_callback, gpointer progress_user_data,
 
293
                                             GDestroyNotify destroy_progress_user_data,
262
294
                                             GAsyncReadyCallback callback, gpointer user_data)
263
295
{
264
296
        gchar *request_uri;
269
301
        g_return_if_fail (callback != NULL);
270
302
 
271
303
        /* Ensure we're authenticated first */
272
 
        if (gdata_service_is_authenticated (GDATA_SERVICE (self)) == FALSE) {
273
 
                g_simple_async_report_error_in_idle (G_OBJECT (self), callback, user_data,
274
 
                                                     GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED,
275
 
                                                     _("You must be authenticated to query contacts."));
 
304
        if (gdata_authorizer_is_authorized_for_domain (gdata_service_get_authorizer (GDATA_SERVICE (self)),
 
305
                                                       get_contacts_authorization_domain ()) == FALSE) {
 
306
                GSimpleAsyncResult *result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, gdata_service_query_async);
 
307
                g_simple_async_result_set_error (result, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED, "%s",
 
308
                                                 _("You must be authenticated to query contacts."));
 
309
                g_simple_async_result_complete_in_idle (result);
 
310
                g_object_unref (result);
 
311
 
276
312
                return;
277
313
        }
278
314
 
279
315
        request_uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/contacts/default/full", NULL);
280
 
        gdata_service_query_async (GDATA_SERVICE (self), request_uri, GDATA_QUERY (query),
281
 
                                   GDATA_TYPE_CONTACTS_CONTACT, cancellable, progress_callback, progress_user_data, callback, user_data);
 
316
        gdata_service_query_async (GDATA_SERVICE (self), get_contacts_authorization_domain (), request_uri, GDATA_QUERY (query),
 
317
                                   GDATA_TYPE_CONTACTS_CONTACT, cancellable, progress_callback, progress_user_data,
 
318
                                   destroy_progress_user_data, callback, user_data);
282
319
        g_free (request_uri);
283
320
}
284
321
 
286
323
 * gdata_contacts_service_insert_contact:
287
324
 * @self: a #GDataContactsService
288
325
 * @contact: the #GDataContactsContact to insert
289
 
 * @cancellable: optional #GCancellable object, or %NULL
 
326
 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
290
327
 * @error: a #GError, or %NULL
291
328
 *
292
329
 * Inserts @contact by uploading it to the online contacts service.
309
346
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
310
347
 
311
348
        uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/contacts/default/full", NULL);
312
 
        entry = gdata_service_insert_entry (GDATA_SERVICE (self), uri, GDATA_ENTRY (contact), cancellable, error);
 
349
        entry = gdata_service_insert_entry (GDATA_SERVICE (self), get_contacts_authorization_domain (), uri, GDATA_ENTRY (contact), cancellable,
 
350
                                            error);
313
351
        g_free (uri);
314
352
 
315
353
        return GDATA_CONTACTS_CONTACT (entry);
319
357
 * gdata_contacts_service_insert_contact_async:
320
358
 * @self: a #GDataContactsService
321
359
 * @contact: the #GDataContactsContact to insert
322
 
 * @cancellable: optional #GCancellable object, or %NULL
 
360
 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
323
361
 * @callback: a #GAsyncReadyCallback to call when insertion is finished
324
362
 * @user_data: (closure): data to pass to the @callback function
325
363
 *
345
383
        g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
346
384
 
347
385
        uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/contacts/default/full", NULL);
348
 
        gdata_service_insert_entry_async (GDATA_SERVICE (self), uri, GDATA_ENTRY (contact), cancellable, callback, user_data);
 
386
        gdata_service_insert_entry_async (GDATA_SERVICE (self), get_contacts_authorization_domain (), uri, GDATA_ENTRY (contact), cancellable,
 
387
                                          callback, user_data);
349
388
        g_free (uri);
350
389
}
351
390
 
353
392
 * gdata_contacts_service_query_groups:
354
393
 * @self: a #GDataContactsService
355
394
 * @query: (allow-none): a #GDataQuery with the query parameters, or %NULL
356
 
 * @cancellable: optional #GCancellable object, or %NULL
357
 
 * @progress_callback: (scope call): a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
 
395
 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
 
396
 * @progress_callback: (allow-none) (scope call) (closure progress_user_data): a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
358
397
 * @progress_user_data: (closure): data to pass to the @progress_callback function
359
398
 * @error: a #GError, or %NULL
360
399
 *
379
418
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
380
419
 
381
420
        /* Ensure we're authenticated first */
382
 
        if (gdata_service_is_authenticated (GDATA_SERVICE (self)) == FALSE) {
 
421
        if (gdata_authorizer_is_authorized_for_domain (gdata_service_get_authorizer (GDATA_SERVICE (self)),
 
422
                                                       get_contacts_authorization_domain ()) == FALSE) {
383
423
                g_set_error_literal (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED,
384
424
                                     _("You must be authenticated to query contact groups."));
385
425
                return NULL;
386
426
        }
387
427
 
388
428
        request_uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/groups/default/full", NULL);
389
 
        feed = gdata_service_query (GDATA_SERVICE (self), request_uri, GDATA_QUERY (query),
 
429
        feed = gdata_service_query (GDATA_SERVICE (self), get_contacts_authorization_domain (), request_uri, GDATA_QUERY (query),
390
430
                                    GDATA_TYPE_CONTACTS_GROUP, cancellable, progress_callback, progress_user_data, error);
391
431
        g_free (request_uri);
392
432
 
394
434
}
395
435
 
396
436
/**
397
 
 * gdata_contacts_service_query_groups_async: (skip)
 
437
 * gdata_contacts_service_query_groups_async:
398
438
 * @self: a #GDataContactsService
399
439
 * @query: (allow-none): a #GDataQuery with the query parameters, or %NULL
400
 
 * @cancellable: optional #GCancellable object, or %NULL
401
 
 * @progress_callback: a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
 
440
 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
 
441
 * @progress_callback: (allow-none) (closure progress_user_data): a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
402
442
 * @progress_user_data: (closure): data to pass to the @progress_callback function
 
443
 * @destroy_progress_user_data: (allow-none): the function to call when @progress_callback will not be called any more, or %NULL. This function will be
 
444
 * called with @progress_user_data as a parameter and can be used to free any memory allocated for it.
403
445
 * @callback: a #GAsyncReadyCallback to call when the query is finished
404
446
 * @user_data: (closure): data to pass to the @callback function
405
447
 *
409
451
 * For more details, see gdata_contacts_service_query_groups(), which is the synchronous version of this function, and gdata_service_query_async(),
410
452
 * which is the base asynchronous query function.
411
453
 *
412
 
 * Since: 0.7.0
 
454
 * Since: 0.9.1
413
455
 **/
414
456
void
415
457
gdata_contacts_service_query_groups_async (GDataContactsService *self, GDataQuery *query, GCancellable *cancellable,
416
458
                                           GDataQueryProgressCallback progress_callback, gpointer progress_user_data,
 
459
                                           GDestroyNotify destroy_progress_user_data,
417
460
                                           GAsyncReadyCallback callback, gpointer user_data)
418
461
{
419
462
        gchar *request_uri;
424
467
        g_return_if_fail (callback != NULL);
425
468
 
426
469
        /* Ensure we're authenticated first */
427
 
        if (gdata_service_is_authenticated (GDATA_SERVICE (self)) == FALSE) {
428
 
                g_simple_async_report_error_in_idle (G_OBJECT (self), callback, user_data,
429
 
                                                     GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED,
430
 
                                                     _("You must be authenticated to query contact groups."));
 
470
        if (gdata_authorizer_is_authorized_for_domain (gdata_service_get_authorizer (GDATA_SERVICE (self)),
 
471
                                                       get_contacts_authorization_domain ()) == FALSE) {
 
472
                GSimpleAsyncResult *result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, gdata_service_query_async);
 
473
                g_simple_async_result_set_error (result, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED, "%s",
 
474
                                                 _("You must be authenticated to query contact groups."));
 
475
                g_simple_async_result_complete_in_idle (result);
 
476
                g_object_unref (result);
 
477
 
431
478
                return;
432
479
        }
433
480
 
434
481
        request_uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/groups/default/full", NULL);
435
 
        gdata_service_query_async (GDATA_SERVICE (self), request_uri, GDATA_QUERY (query),
436
 
                                   GDATA_TYPE_CONTACTS_GROUP, cancellable, progress_callback, progress_user_data, callback, user_data);
 
482
        gdata_service_query_async (GDATA_SERVICE (self), get_contacts_authorization_domain (), request_uri, GDATA_QUERY (query),
 
483
                                   GDATA_TYPE_CONTACTS_GROUP, cancellable, progress_callback, progress_user_data,
 
484
                                   destroy_progress_user_data, callback, user_data);
437
485
        g_free (request_uri);
438
486
}
439
487
 
441
489
 * gdata_contacts_service_insert_group:
442
490
 * @self: a #GDataContactsService
443
491
 * @group: a #GDataContactsGroup to create on the server
444
 
 * @cancellable: optional #GCancellable object, or %NULL
 
492
 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
445
493
 * @error: a #GError, or %NULL
446
494
 *
447
495
 * Inserts a new contact group described by @group. The user must be authenticated to use this function.
467
515
                return NULL;
468
516
        }
469
517
 
470
 
        if (gdata_service_is_authenticated (GDATA_SERVICE (self)) == FALSE) {
 
518
        if (gdata_authorizer_is_authorized_for_domain (gdata_service_get_authorizer (GDATA_SERVICE (self)),
 
519
                                                       get_contacts_authorization_domain ()) == FALSE) {
471
520
                g_set_error_literal (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED,
472
521
                                     _("You must be authenticated to insert a group."));
473
522
                return NULL;
474
523
        }
475
524
 
476
525
        request_uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/groups/default/full", NULL);
477
 
        new_group = gdata_service_insert_entry (GDATA_SERVICE (self), request_uri, GDATA_ENTRY (group), cancellable, error);
 
526
        new_group = gdata_service_insert_entry (GDATA_SERVICE (self), get_contacts_authorization_domain (), request_uri, GDATA_ENTRY (group),
 
527
                                                cancellable, error);
478
528
        g_free (request_uri);
479
529
 
480
530
        return GDATA_CONTACTS_GROUP (new_group);
484
534
 * gdata_contacts_service_insert_group_async:
485
535
 * @self: a #GDataContactsService
486
536
 * @group: the #GDataContactsGroup to insert
487
 
 * @cancellable: optional #GCancellable object, or %NULL
 
537
 * @cancellable: (allow-none): optional #GCancellable object, or %NULL
488
538
 * @callback: a #GAsyncReadyCallback to call when insertion is finished
489
539
 * @user_data: (closure): data to pass to the @callback function
490
540
 *
510
560
        g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
511
561
 
512
562
        request_uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/groups/default/full", NULL);
513
 
        gdata_service_insert_entry_async (GDATA_SERVICE (self), request_uri, GDATA_ENTRY (group), cancellable, callback, user_data);
 
563
        gdata_service_insert_entry_async (GDATA_SERVICE (self), get_contacts_authorization_domain (), request_uri, GDATA_ENTRY (group), cancellable,
 
564
                                          callback, user_data);
514
565
        g_free (request_uri);
515
566
}