~ubuntu-branches/ubuntu/saucy/evolution-data-server/saucy

« back to all changes in this revision

Viewing changes to libebackend/e-backend.c

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2012-10-08 12:58:16 UTC
  • mfrom: (181.1.7 quantal)
  • Revision ID: package-import@ubuntu.com-20121008125816-i3n76e8c0m64e7xp
Tags: 3.6.0-0ubuntu2
* Fix LP: #1038047 part 1 - Don't abort in e_source_registry_new* when a
  problem occurs connecting to the Dbus service
  - add debian/patches/dont-abort-in-e_source_registry_new.patch
  - update debian/patches/series
* Fix LP: #1038047 part 2 - libedataserver depends on
  evolution-data-server-common to ensure that the GSettings schemas are
  present
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include "e-backend.h"
35
35
 
36
36
#include <config.h>
 
37
#include <glib/gi18n-lib.h>
 
38
 
37
39
#include <gio/gio.h>
38
40
 
39
41
#define E_BACKEND_GET_PRIVATE(obj) \
40
42
        (G_TYPE_INSTANCE_GET_PRIVATE \
41
43
        ((obj), E_TYPE_BACKEND, EBackendPrivate))
42
44
 
 
45
typedef struct _AsyncContext AsyncContext;
 
46
 
43
47
struct _EBackendPrivate {
44
48
        ESource *source;
45
49
        gboolean online;
46
50
};
47
51
 
 
52
struct _AsyncContext {
 
53
        ESourceAuthenticator *auth;
 
54
};
 
55
 
48
56
enum {
49
57
        PROP_0,
50
58
        PROP_ONLINE,
54
62
G_DEFINE_ABSTRACT_TYPE (EBackend, e_backend, G_TYPE_OBJECT)
55
63
 
56
64
static void
 
65
async_context_free (AsyncContext *async_context)
 
66
{
 
67
        if (async_context->auth != NULL)
 
68
                g_object_unref (async_context->auth);
 
69
 
 
70
        g_slice_free (AsyncContext, async_context);
 
71
}
 
72
 
 
73
static void
57
74
backend_set_source (EBackend *backend,
58
75
                    ESource *source)
59
76
{
144
161
}
145
162
 
146
163
static void
 
164
backend_authenticate_thread (GSimpleAsyncResult *simple,
 
165
                             GObject *object,
 
166
                             GCancellable *cancellable)
 
167
{
 
168
        AsyncContext *async_context;
 
169
        GError *error = NULL;
 
170
 
 
171
        async_context = g_simple_async_result_get_op_res_gpointer (simple);
 
172
 
 
173
        e_backend_authenticate_sync (
 
174
                E_BACKEND (object),
 
175
                async_context->auth,
 
176
                cancellable, &error);
 
177
 
 
178
        if (error != NULL)
 
179
                g_simple_async_result_take_error (simple, error);
 
180
}
 
181
 
 
182
static gboolean
 
183
backend_authenticate_sync (EBackend *backend,
 
184
                           ESourceAuthenticator *auth,
 
185
                           GCancellable *cancellable,
 
186
                           GError **error)
 
187
{
 
188
        g_set_error (
 
189
                error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
 
190
                _("%s does not support authentication"),
 
191
                G_OBJECT_TYPE_NAME (backend));
 
192
 
 
193
        return FALSE;
 
194
}
 
195
 
 
196
static void
 
197
backend_authenticate (EBackend *backend,
 
198
                      ESourceAuthenticator *auth,
 
199
                      GCancellable *cancellable,
 
200
                      GAsyncReadyCallback callback,
 
201
                      gpointer user_data)
 
202
{
 
203
        GSimpleAsyncResult *simple;
 
204
        AsyncContext *async_context;
 
205
 
 
206
        async_context = g_slice_new0 (AsyncContext);
 
207
        async_context->auth = g_object_ref (auth);
 
208
 
 
209
        simple = g_simple_async_result_new (
 
210
                G_OBJECT (backend), callback,
 
211
                user_data, backend_authenticate);
 
212
 
 
213
        g_simple_async_result_set_check_cancellable (simple, cancellable);
 
214
 
 
215
        g_simple_async_result_set_op_res_gpointer (
 
216
                simple, async_context, (GDestroyNotify) async_context_free);
 
217
 
 
218
        g_simple_async_result_run_in_thread (
 
219
                simple, backend_authenticate_thread,
 
220
                G_PRIORITY_DEFAULT, cancellable);
 
221
 
 
222
        g_object_unref (simple);
 
223
}
 
224
 
 
225
static gboolean
 
226
backend_authenticate_finish (EBackend *backend,
 
227
                             GAsyncResult *result,
 
228
                             GError **error)
 
229
{
 
230
        GSimpleAsyncResult *simple;
 
231
 
 
232
        g_return_val_if_fail (
 
233
                g_simple_async_result_is_valid (
 
234
                result, G_OBJECT (backend),
 
235
                backend_authenticate), FALSE);
 
236
 
 
237
        simple = G_SIMPLE_ASYNC_RESULT (result);
 
238
 
 
239
        /* Assume success unless a GError is set. */
 
240
        return !g_simple_async_result_propagate_error (simple, error);
 
241
}
 
242
 
 
243
static void
147
244
e_backend_class_init (EBackendClass *class)
148
245
{
149
246
        GObjectClass *object_class;
156
253
        object_class->dispose = backend_dispose;
157
254
        object_class->constructed = backend_constructed;
158
255
 
 
256
        class->authenticate_sync = backend_authenticate_sync;
 
257
        class->authenticate = backend_authenticate;
 
258
        class->authenticate_finish = backend_authenticate_finish;
 
259
 
159
260
        g_object_class_install_property (
160
261
                object_class,
161
262
                PROP_ONLINE,
251
352
        return backend->priv->source;
252
353
}
253
354
 
 
355
/**
 
356
 * e_backend_authenticate_sync:
 
357
 * @backend: an #EBackend
 
358
 * @auth: an #ESourceAuthenticator
 
359
 * @cancellable: optional #GCancellable object, or %NULL
 
360
 * @error: return location for a #GError, or %NULL
 
361
 *
 
362
 * Convenience function providing a consistent authentication interface
 
363
 * for backends running in either the registry service itself or a client
 
364
 * process communicating with the registry service over D-Bus.
 
365
 *
 
366
 * Authenticates @backend's #EBackend:source, using @auth to handle
 
367
 * authentication attempts.  The @backend and @auth arguments may be one
 
368
 * and the same if @backend implements the #ESourceAuthenticator interface.
 
369
 * The operation loops until authentication is successful or the user aborts
 
370
 * further authentication attempts.  If an error occurs, the function will
 
371
 * set @error and return %FALSE.
 
372
 *
 
373
 * Returns: %TRUE on success, %FALSE on failure
 
374
 *
 
375
 * Since: 3.6
 
376
 **/
 
377
gboolean
 
378
e_backend_authenticate_sync (EBackend *backend,
 
379
                             ESourceAuthenticator *auth,
 
380
                             GCancellable *cancellable,
 
381
                             GError **error)
 
382
{
 
383
        EBackendClass *class;
 
384
 
 
385
        g_return_val_if_fail (E_IS_BACKEND (backend), FALSE);
 
386
        g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATOR (auth), FALSE);
 
387
 
 
388
        class = E_BACKEND_GET_CLASS (backend);
 
389
        g_return_val_if_fail (class->authenticate_sync != NULL, FALSE);
 
390
 
 
391
        return class->authenticate_sync (backend, auth, cancellable, error);
 
392
}
 
393
 
 
394
/**
 
395
 * e_backend_authenticate:
 
396
 * @backend: an #EBackend
 
397
 * @auth: an #ESourceAuthenticator
 
398
 * @cancellable: optional #GCancellable object, or %NULL
 
399
 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
 
400
 * @user_data: data to pass to the callback function
 
401
 *
 
402
 * Convenience function providing a consistent authentication interface
 
403
 * for backends running in either the registry service itself or a client
 
404
 * process communicating with the registry service over D-Bus.
 
405
 *
 
406
 * Asynchronously authenticates @backend's #EBackend:source, using @auth
 
407
 * to handle authentication attempts.  The @backend and @auth arguments may
 
408
 * be one and the same if @backend implements the #ESourceAuthenticator
 
409
 * interface.  The operation loops until authentication is succesful or the
 
410
 * user aborts further authentication attempts.
 
411
 *
 
412
 * When the operation is finished, @callback will be called.  You can then
 
413
 * call e_backend_authenticate_finish() to get the result of the operation.
 
414
 *
 
415
 * Since: 3.6
 
416
 **/
 
417
void
 
418
e_backend_authenticate (EBackend *backend,
 
419
                        ESourceAuthenticator *auth,
 
420
                        GCancellable *cancellable,
 
421
                        GAsyncReadyCallback callback,
 
422
                        gpointer user_data)
 
423
{
 
424
        EBackendClass *class;
 
425
 
 
426
        g_return_if_fail (E_IS_BACKEND (backend));
 
427
        g_return_if_fail (E_IS_SOURCE_AUTHENTICATOR (auth));
 
428
 
 
429
        class = E_BACKEND_GET_CLASS (backend);
 
430
        g_return_if_fail (class->authenticate != NULL);
 
431
 
 
432
        class->authenticate (backend, auth, cancellable, callback, user_data);
 
433
}
 
434
 
 
435
/**
 
436
 * e_backend_authenticate_finish:
 
437
 * @backend: an #EBackend
 
438
 * @result: a #GAsyncResult
 
439
 * @error: return location for a #GError, or %NULL
 
440
 *
 
441
 * Finishes the operation started with e_backend_authenticate().  If
 
442
 * an error occurred, the function will set @error and return %FALSE.
 
443
 *
 
444
 * Returns: %TRUE on success, %FALSE on failure
 
445
 *
 
446
 * Since: 3.6
 
447
 **/
 
448
gboolean
 
449
e_backend_authenticate_finish (EBackend *backend,
 
450
                               GAsyncResult *result,
 
451
                               GError **error)
 
452
{
 
453
        EBackendClass *class;
 
454
 
 
455
        g_return_val_if_fail (E_IS_BACKEND (backend), FALSE);
 
456
        g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
 
457
 
 
458
        class = E_BACKEND_GET_CLASS (backend);
 
459
        g_return_val_if_fail (class->authenticate_finish != NULL, FALSE);
 
460
 
 
461
        return class->authenticate_finish (backend, result, error);
 
462
}
 
463