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

« back to all changes in this revision

Viewing changes to addressbook/tests/ebook/ebook-test-utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Trudel-Lapierre, Ken VanDine, Mathieu Trudel-Lapierre
  • Date: 2011-06-23 17:40:41 UTC
  • mfrom: (1.1.88 upstream)
  • Revision ID: james.westby@ubuntu.com-20110623174041-4wd9jvs07wfinyet
Tags: 3.1.2-0ubuntu1
* New upstream version
  - bgo 589495 - Search folder by Size (KB) counts bytes, not KB (LP: #385859)
  - bgo 649757 - Filtering on a source account always succeeded (LP: #781391)
  - bgo 418954 - Add a separate entry combo for port numbers (LP: #160304)

[ Ken VanDine ]
* debian/libebook1.2-dev.install
  - Include EBook-1.2.gir
* debian/control
  - Added gir1.2-ebook-1.2 binary

[ Mathieu Trudel-Lapierre ]
* debian/control: drop libegroupwise1.2-13, libegroupwise-dev, they are now
  an independent module.
* debian/libegroupwise1.2-13.install,
  debian/libegroupwise1.2-dev.install,
  debian/lintian/libegroupwise1.2-13: dropped, see above.
* debian/control, debian/libe*.install, debian/lintian/libe*: rename and
  update files where needed to follow upstream soname changes.
* debian/control: bump evolution-data-server's Depends to libcamel-1.2-26.
* debian/rules: update to use makeshlibs with the new libcamel soname.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
 
 
3
 
#include <stdlib.h>
4
 
#include <glib.h>
5
 
#include <gio/gio.h>
6
 
#include <libebook/e-book.h>
7
 
 
8
 
#include "ebook-test-utils.h"
9
 
 
10
 
void
11
 
test_print (const gchar *format,
12
 
            ...)
13
 
{
14
 
        va_list args;
15
 
        const gchar *debug_string;
16
 
        static gboolean debug_set = FALSE;
17
 
        static gboolean debug = FALSE;
18
 
 
19
 
        if (!debug_set) {
20
 
                debug_string = g_getenv ("EDS_TEST_DEBUG");
21
 
                if (debug_string) {
22
 
                        debug = (g_ascii_strtoll (debug_string, NULL, 10) >= 1);
23
 
                }
24
 
                debug_set = TRUE;
25
 
        }
26
 
 
27
 
        if (debug) {
28
 
                va_start (args, format);
29
 
                vprintf (format, args);
30
 
                va_end (args);
31
 
        }
32
 
}
33
 
 
34
 
gboolean
35
 
ebook_test_utils_callback_quit (gpointer user_data)
36
 
{
37
 
        EBookTestClosure *closure = user_data;
38
 
        g_main_loop_quit ((GMainLoop*) closure->user_data);
39
 
 
40
 
        return FALSE;
41
 
}
42
 
 
43
 
gchar *
44
 
ebook_test_utils_new_vcard_from_test_case (const gchar *case_name)
45
 
{
46
 
        gchar *filename;
47
 
        gchar *case_filename;
48
 
        GFile* file;
49
 
        GError *error = NULL;
50
 
        gchar *vcard;
51
 
 
52
 
        case_filename = g_strdup_printf ("%s.vcf", case_name);
53
 
        filename = g_build_filename (SRCDIR, EBOOK_TEST_UTILS_DATA_DIR, EBOOK_TEST_UTILS_VCARDS_DIR, case_filename, NULL);
54
 
        file = g_file_new_for_path (filename);
55
 
        if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
56
 
                g_warning ("failed to read test contact file '%s': %s",
57
 
                                filename, error->message);
58
 
                exit (1);
59
 
        }
60
 
 
61
 
        g_free (case_filename);
62
 
        g_free (filename);
63
 
        g_object_unref (file);
64
 
 
65
 
        return vcard;
66
 
}
67
 
 
68
 
gchar *
69
 
ebook_test_utils_book_add_contact_from_test_case_verify (EBook       *book,
70
 
                                                         const gchar  *case_name,
71
 
                                                         EContact   **contact)
72
 
{
73
 
        gchar *vcard;
74
 
        EContact *contact_orig;
75
 
        EContact *contact_final;
76
 
        gchar *uid;
77
 
 
78
 
        vcard = ebook_test_utils_new_vcard_from_test_case (case_name);
79
 
        contact_orig = e_contact_new_from_vcard (vcard);
80
 
        uid = g_strdup (ebook_test_utils_book_add_contact (book, contact_orig));
81
 
        contact_final = ebook_test_utils_book_get_contact (book, uid);
82
 
 
83
 
        /* verify the contact was added "successfully" (not thorough) */
84
 
        g_assert (ebook_test_utils_contacts_are_equal_shallow (contact_orig, contact_final));
85
 
 
86
 
        if (contact)
87
 
                *contact = g_object_ref (contact_final);
88
 
 
89
 
        return uid;
90
 
}
91
 
 
92
 
/* This is not a thorough comparison (which is difficult, assuming we give the
93
 
 * back-ends leniency in implementation) and is best suited for simple tests */
94
 
gboolean
95
 
ebook_test_utils_contacts_are_equal_shallow (EContact *a,
96
 
                                             EContact *b)
97
 
 
98
 
{
99
 
        const gchar *uid_a, *uid_b;
100
 
 
101
 
        /* Avoid warnings if one or more are NULL, to make this function
102
 
         * "NULL-friendly" */
103
 
        if (!a && !b)
104
 
                return TRUE;
105
 
        if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
106
 
                return FALSE;
107
 
 
108
 
        uid_a = e_contact_get_const (a, E_CONTACT_UID);
109
 
        uid_b = e_contact_get_const (b, E_CONTACT_UID);
110
 
 
111
 
        return g_strcmp0 (uid_a, uid_b) == 0;
112
 
}
113
 
 
114
 
const gchar *
115
 
ebook_test_utils_book_add_contact (EBook    *book,
116
 
                                   EContact *contact)
117
 
{
118
 
        GError *error = NULL;
119
 
 
120
 
        if (!e_book_add_contact (book, contact, &error)) {
121
 
                const gchar *uri;
122
 
 
123
 
                uri = e_book_get_uri (book);
124
 
                g_warning ("failed to add contact to addressbook: `%s': %s",
125
 
                                uri, error->message);
126
 
                exit (1);
127
 
        }
128
 
 
129
 
        return e_contact_get_const (contact, E_CONTACT_UID);
130
 
}
131
 
 
132
 
static void
133
 
add_contact_cb (EBook            *book,
134
 
                const GError     *error,
135
 
                const gchar       *uid,
136
 
                EBookTestClosure *closure)
137
 
{
138
 
        if (error) {
139
 
                g_warning ("failed to asynchronously add the contact '%s': "
140
 
                                "status %d (%s)", uid, error->code, error->message);
141
 
                exit (1);
142
 
        }
143
 
 
144
 
        test_print ("successfully asynchronously added the contact "
145
 
                        "addressbook\n");
146
 
        if (closure) {
147
 
                (*closure->cb) (closure);
148
 
                g_free (closure);
149
 
        }
150
 
}
151
 
 
152
 
void
153
 
ebook_test_utils_book_async_add_contact (EBook       *book,
154
 
                                         EContact    *contact,
155
 
                                         GSourceFunc  callback,
156
 
                                         gpointer     user_data)
157
 
{
158
 
        EBookTestClosure *closure;
159
 
 
160
 
        closure = g_new0 (EBookTestClosure, 1);
161
 
        closure->cb = callback;
162
 
        closure->user_data = user_data;
163
 
        if (!e_book_add_contact_async (book, contact,
164
 
                                (EBookIdAsyncCallback) add_contact_cb, closure)) {
165
 
                g_warning ("failed to set up contact add");
166
 
                exit (1);
167
 
        }
168
 
}
169
 
 
170
 
void
171
 
ebook_test_utils_book_commit_contact (EBook    *book,
172
 
                                      EContact *contact)
173
 
{
174
 
        GError *error = NULL;
175
 
 
176
 
        if (!e_book_commit_contact (book, contact, &error)) {
177
 
                const gchar *uid;
178
 
                const gchar *uri;
179
 
 
180
 
                uid = (const gchar *) e_contact_get_const (contact, E_CONTACT_UID);
181
 
                uri = e_book_get_uri (book);
182
 
                g_warning ("failed to commit changes to contact '%s' to addressbook: `%s': %s",
183
 
                                uid, uri, error->message);
184
 
                exit (1);
185
 
        }
186
 
}
187
 
 
188
 
static void
189
 
commit_contact_cb (EBook            *book,
190
 
                   const GError     *error,
191
 
                   EBookTestClosure *closure)
192
 
{
193
 
        if (error) {
194
 
                g_warning ("failed to asynchronously commit the contact: "
195
 
                                "status %d (%s)", error->code, error->message);
196
 
                exit (1);
197
 
        }
198
 
 
199
 
        test_print ("successfully asynchronously committed the contact to the "
200
 
                        "addressbook\n");
201
 
        if (closure) {
202
 
                (*closure->cb) (closure);
203
 
                g_free (closure);
204
 
        }
205
 
}
206
 
 
207
 
void
208
 
ebook_test_utils_book_async_commit_contact (EBook       *book,
209
 
                                            EContact    *contact,
210
 
                                            GSourceFunc  callback,
211
 
                                            gpointer     user_data)
212
 
{
213
 
        EBookTestClosure *closure;
214
 
 
215
 
        closure = g_new0 (EBookTestClosure, 1);
216
 
        closure->cb = callback;
217
 
        closure->user_data = user_data;
218
 
        if (!e_book_commit_contact_async (book, contact,
219
 
                                (EBookAsyncCallback) commit_contact_cb, closure)) {
220
 
                g_warning ("failed to set up contact commit");
221
 
                exit (1);
222
 
        }
223
 
}
224
 
 
225
 
EContact*
226
 
ebook_test_utils_book_get_contact (EBook      *book,
227
 
                                   const gchar *uid)
228
 
{
229
 
        EContact *contact = NULL;
230
 
        GError *error = NULL;
231
 
 
232
 
        if (!e_book_get_contact (book, uid, &contact, &error)) {
233
 
                const gchar *uri;
234
 
 
235
 
                uri = e_book_get_uri (book);
236
 
                g_warning ("failed to get contact '%s' in addressbook: `%s': "
237
 
                                "%s", uid, uri, error->message);
238
 
                exit (1);
239
 
        }
240
 
 
241
 
        return contact;
242
 
}
243
 
 
244
 
static void
245
 
get_contact_cb (EBook            *book,
246
 
                const GError     *error,
247
 
                EContact         *contact,
248
 
                EBookTestClosure *closure)
249
 
{
250
 
        const gchar *uid;
251
 
 
252
 
        if (error) {
253
 
                g_warning ("failed to asynchronously get the contact: "
254
 
                                "status %d (%s)", error->code, error->message);
255
 
                exit (1);
256
 
        }
257
 
 
258
 
        uid = e_contact_get_const (contact, E_CONTACT_UID);
259
 
        test_print ("successfully asynchronously retrieved the contact '%s'\n",
260
 
                        uid);
261
 
 
262
 
        if (closure) {
263
 
                (*closure->cb) (closure);
264
 
                g_free (closure);
265
 
        }
266
 
}
267
 
 
268
 
void
269
 
ebook_test_utils_book_async_get_contact (EBook       *book,
270
 
                                         const gchar  *uid,
271
 
                                         GSourceFunc  callback,
272
 
                                         gpointer     user_data)
273
 
{
274
 
        EBookTestClosure *closure;
275
 
 
276
 
        closure = g_new0 (EBookTestClosure, 1);
277
 
        closure->cb = callback;
278
 
        closure->user_data = user_data;
279
 
        if (!e_book_get_contact_async (book, uid,
280
 
                                (EBookContactAsyncCallback) get_contact_cb,
281
 
                                closure)) {
282
 
                g_warning ("failed to set up async getContact");
283
 
                exit (1);
284
 
        }
285
 
}
286
 
 
287
 
GList*
288
 
ebook_test_utils_book_get_required_fields (EBook *book)
289
 
{
290
 
        GList *fields = NULL;
291
 
        GError *error = NULL;
292
 
 
293
 
        if (!e_book_get_required_fields (book, &fields, &error)) {
294
 
                const gchar *uri;
295
 
 
296
 
                uri = e_book_get_uri (book);
297
 
                g_warning ("failed to get required fields for addressbook "
298
 
                                "`%s': %s", uri, error->message);
299
 
                exit (1);
300
 
        }
301
 
 
302
 
        return fields;
303
 
}
304
 
 
305
 
static void
306
 
get_required_fields_cb (EBook            *book,
307
 
                        const GError     *error,
308
 
                        EList            *fields,
309
 
                        EBookTestClosure *closure)
310
 
{
311
 
        if (error) {
312
 
                g_warning ("failed to asynchronously get the required fields: "
313
 
                                "status %d (%s)", error->code, error->message);
314
 
                exit (1);
315
 
        }
316
 
 
317
 
        closure->list = fields;
318
 
 
319
 
        test_print ("successfully asynchronously retrieved the required fields\n");
320
 
 
321
 
        if (closure) {
322
 
                (*closure->cb) (closure);
323
 
                g_free (closure);
324
 
        }
325
 
}
326
 
 
327
 
void
328
 
ebook_test_utils_book_async_get_required_fields (EBook       *book,
329
 
                                                 GSourceFunc  callback,
330
 
                                                 gpointer     user_data)
331
 
{
332
 
        EBookTestClosure *closure;
333
 
 
334
 
        closure = g_new0 (EBookTestClosure, 1);
335
 
        closure->cb = callback;
336
 
        closure->user_data = user_data;
337
 
        if (!e_book_get_required_fields_async (book,
338
 
                                (EBookEListAsyncCallback) get_required_fields_cb,
339
 
                                closure)) {
340
 
                g_warning ("failed to set up async getRequiredFields");
341
 
                exit (1);
342
 
        }
343
 
}
344
 
 
345
 
const gchar *
346
 
ebook_test_utils_book_get_static_capabilities (EBook *book)
347
 
{
348
 
        GError *error = NULL;
349
 
        const gchar *caps;
350
 
 
351
 
        if (!(caps = e_book_get_static_capabilities (book, &error))) {
352
 
                const gchar *uri;
353
 
 
354
 
                uri = e_book_get_uri (book);
355
 
                g_warning ("failed to get capabilities for addressbook: `%s': "
356
 
                                "%s", uri, error->message);
357
 
                exit (1);
358
 
        }
359
 
 
360
 
        return caps;
361
 
}
362
 
 
363
 
GList*
364
 
ebook_test_utils_book_get_supported_auth_methods (EBook *book)
365
 
{
366
 
        GList *fields = NULL;
367
 
        GError *error = NULL;
368
 
 
369
 
        if (!e_book_get_supported_auth_methods (book, &fields, &error)) {
370
 
                const gchar *uri;
371
 
 
372
 
                uri = e_book_get_uri (book);
373
 
                g_warning ("failed to get supported auth methods for "
374
 
                                "addressbook `%s': %s", uri, error->message);
375
 
                exit (1);
376
 
        }
377
 
 
378
 
        return fields;
379
 
}
380
 
 
381
 
static void
382
 
get_supported_auth_methods_cb (EBook            *book,
383
 
                               const GError     *error,
384
 
                               EList            *methods,
385
 
                               EBookTestClosure *closure)
386
 
{
387
 
        if (error) {
388
 
                g_warning ("failed to asynchronously get the supported auth "
389
 
                                "methods: status %d (%s)", error->code, error->message);
390
 
                exit (1);
391
 
        }
392
 
 
393
 
        closure->list = methods;
394
 
 
395
 
        test_print ("successfully asynchronously retrieved the supported auth "
396
 
                        "methods\n");
397
 
 
398
 
        if (closure) {
399
 
                (*closure->cb) (closure);
400
 
                g_free (closure);
401
 
        }
402
 
}
403
 
 
404
 
void
405
 
ebook_test_utils_book_async_get_supported_auth_methods (EBook       *book,
406
 
                                                        GSourceFunc  callback,
407
 
                                                        gpointer     user_data)
408
 
{
409
 
        EBookTestClosure *closure;
410
 
 
411
 
        closure = g_new0 (EBookTestClosure, 1);
412
 
        closure->cb = callback;
413
 
        closure->user_data = user_data;
414
 
        if (!e_book_get_supported_auth_methods_async (book,
415
 
                                (EBookEListAsyncCallback) get_supported_auth_methods_cb,
416
 
                                closure)) {
417
 
                g_warning ("failed to set up async getSupportedAuthMethods");
418
 
                exit (1);
419
 
        }
420
 
}
421
 
 
422
 
GList*
423
 
ebook_test_utils_book_get_supported_fields (EBook *book)
424
 
{
425
 
        GList *fields = NULL;
426
 
        GError *error = NULL;
427
 
 
428
 
        if (!e_book_get_supported_fields (book, &fields, &error)) {
429
 
                const gchar *uri;
430
 
 
431
 
                uri = e_book_get_uri (book);
432
 
                g_warning ("failed to get supported fields for addressbook "
433
 
                                "`%s': %s", uri, error->message);
434
 
                exit (1);
435
 
        }
436
 
 
437
 
        return fields;
438
 
}
439
 
 
440
 
static void
441
 
get_supported_fields_cb (EBook           *book,
442
 
                        const GError     *error,
443
 
                        EList            *fields,
444
 
                        EBookTestClosure *closure)
445
 
{
446
 
        if (error) {
447
 
                g_warning ("failed to asynchronously get the supported fields: "
448
 
                                "status %d (%s)", error->code, error->message);
449
 
                exit (1);
450
 
        }
451
 
 
452
 
        closure->list = fields;
453
 
 
454
 
        test_print ("successfully asynchronously retrieved the supported fields\n");
455
 
 
456
 
        if (closure) {
457
 
                (*closure->cb) (closure);
458
 
                g_free (closure);
459
 
        }
460
 
}
461
 
 
462
 
void
463
 
ebook_test_utils_book_async_get_supported_fields (EBook       *book,
464
 
                                                 GSourceFunc  callback,
465
 
                                                 gpointer     user_data)
466
 
{
467
 
        EBookTestClosure *closure;
468
 
 
469
 
        closure = g_new0 (EBookTestClosure, 1);
470
 
        closure->cb = callback;
471
 
        closure->user_data = user_data;
472
 
        if (!e_book_get_supported_fields_async (book,
473
 
                                (EBookEListAsyncCallback) get_supported_fields_cb,
474
 
                                closure)) {
475
 
                g_warning ("failed to set up async getSupportedFields");
476
 
                exit (1);
477
 
        }
478
 
}
479
 
 
480
 
void
481
 
ebook_test_utils_book_remove_contact (EBook      *book,
482
 
                                      const gchar *uid)
483
 
{
484
 
        GError *error = NULL;
485
 
 
486
 
        if (!e_book_remove_contact (book, uid, &error)) {
487
 
                const gchar *uri;
488
 
 
489
 
                uri = e_book_get_uri (book);
490
 
                g_warning ("failed to remove contact '%s' from addressbook: `%s': %s",
491
 
                                uid, uri, error->message);
492
 
                exit (1);
493
 
        }
494
 
}
495
 
 
496
 
static void
497
 
remove_contact_cb (EBook            *book,
498
 
                   const GError     *error,
499
 
                   EBookTestClosure *closure)
500
 
{
501
 
        if (error) {
502
 
                g_warning ("failed to asynchronously remove the contact: "
503
 
                                "status %d (%s)", error->code, error->message);
504
 
                exit (1);
505
 
        }
506
 
 
507
 
        test_print ("successfully asynchronously removed the contact\n");
508
 
 
509
 
        if (closure) {
510
 
                (*closure->cb) (closure);
511
 
                g_free (closure);
512
 
        }
513
 
}
514
 
 
515
 
void
516
 
ebook_test_utils_book_async_remove_contact (EBook       *book,
517
 
                                            EContact    *contact,
518
 
                                            GSourceFunc  callback,
519
 
                                            gpointer     user_data)
520
 
{
521
 
        EBookTestClosure *closure;
522
 
 
523
 
        closure = g_new0 (EBookTestClosure, 1);
524
 
        closure->cb = callback;
525
 
        closure->user_data = user_data;
526
 
        if (!e_book_remove_contact_async (book, contact,
527
 
                                (EBookAsyncCallback) remove_contact_cb,
528
 
                                closure)) {
529
 
                g_warning ("failed to set up async removeContacts (for a single contact)");
530
 
                exit (1);
531
 
        }
532
 
}
533
 
 
534
 
static void
535
 
remove_contact_by_id_cb (EBook            *book,
536
 
                         const GError     *error,
537
 
                         EBookTestClosure *closure)
538
 
{
539
 
        if (error) {
540
 
                g_warning ("failed to asynchronously remove the contact by id: "
541
 
                                "status %d (%s)", error->code, error->message);
542
 
                exit (1);
543
 
        }
544
 
 
545
 
        test_print ("successfully asynchronously removed the contact by id\n");
546
 
 
547
 
        if (closure) {
548
 
                (*closure->cb) (closure);
549
 
                g_free (closure);
550
 
        }
551
 
}
552
 
 
553
 
void
554
 
ebook_test_utils_book_async_remove_contact_by_id (EBook       *book,
555
 
                                                  const gchar  *uid,
556
 
                                                  GSourceFunc  callback,
557
 
                                                  gpointer     user_data)
558
 
{
559
 
        EBookTestClosure *closure;
560
 
 
561
 
        closure = g_new0 (EBookTestClosure, 1);
562
 
        closure->cb = callback;
563
 
        closure->user_data = user_data;
564
 
        if (!e_book_remove_contact_by_id_async (book, uid,
565
 
                                (EBookAsyncCallback) remove_contact_by_id_cb,
566
 
                                closure)) {
567
 
                g_warning ("failed to set up async removeContacts (by id)");
568
 
                exit (1);
569
 
        }
570
 
}
571
 
 
572
 
void
573
 
ebook_test_utils_book_remove_contacts (EBook *book,
574
 
                                       GList *ids)
575
 
{
576
 
        GError *error = NULL;
577
 
 
578
 
        if (!e_book_remove_contacts (book, ids, &error)) {
579
 
                const gchar *uri;
580
 
 
581
 
                uri = e_book_get_uri (book);
582
 
                g_warning ("failed to remove contacts from addressbook: `%s': %s",
583
 
                                uri, error->message);
584
 
                exit (1);
585
 
        }
586
 
}
587
 
 
588
 
static void
589
 
remove_contacts_cb (EBook            *book,
590
 
                    const GError     *error,
591
 
                    EBookTestClosure *closure)
592
 
{
593
 
        if (error) {
594
 
                g_warning ("failed to asynchronously remove the contacts: "
595
 
                                "status %d (%s)", error->code, error->message);
596
 
                exit (1);
597
 
        }
598
 
 
599
 
        test_print ("successfully asynchronously removed the contacts\n");
600
 
 
601
 
        if (closure) {
602
 
                (*closure->cb) (closure);
603
 
                g_free (closure);
604
 
        }
605
 
}
606
 
 
607
 
void
608
 
ebook_test_utils_book_async_remove_contacts (EBook       *book,
609
 
                                             GList       *uids,
610
 
                                             GSourceFunc  callback,
611
 
                                             gpointer     user_data)
612
 
{
613
 
        EBookTestClosure *closure;
614
 
 
615
 
        closure = g_new0 (EBookTestClosure, 1);
616
 
        closure->cb = callback;
617
 
        closure->user_data = user_data;
618
 
        if (!e_book_remove_contacts_async (book, uids,
619
 
                                (EBookAsyncCallback) remove_contacts_cb,
620
 
                                closure)) {
621
 
                g_warning ("failed to set up async removeContacts");
622
 
                exit (1);
623
 
        }
624
 
}
625
 
 
626
 
EBook*
627
 
ebook_test_utils_book_new_from_uri (const gchar *uri)
628
 
{
629
 
        EBook *book;
630
 
        GError *error = NULL;
631
 
 
632
 
        test_print ("loading addressbook\n");
633
 
        book = e_book_new_from_uri (uri, &error);
634
 
        if (!book) {
635
 
                g_error ("failed to create addressbook: `%s': %s", uri,
636
 
                                error->message);
637
 
        }
638
 
 
639
 
        return book;
640
 
}
641
 
 
642
 
EBook*
643
 
ebook_test_utils_book_new_temp (gchar **uri)
644
 
{
645
 
        EBook *book;
646
 
        gchar *file_template;
647
 
        gchar *uri_result;
648
 
 
649
 
        file_template = g_build_filename (g_get_tmp_dir (),
650
 
                        "ebook-test-XXXXXX/", NULL);
651
 
        g_mkstemp (file_template);
652
 
 
653
 
        uri_result = g_strconcat ("local:", file_template, NULL);
654
 
        if (!uri_result) {
655
 
                g_warning ("failed to convert %s to a 'local:' URI", file_template);
656
 
                exit (1);
657
 
        }
658
 
        g_free (file_template);
659
 
 
660
 
        book = ebook_test_utils_book_new_from_uri (uri_result);
661
 
 
662
 
        if (uri)
663
 
                *uri = g_strdup (uri_result);
664
 
 
665
 
        g_free (uri_result);
666
 
 
667
 
        return book;
668
 
}
669
 
 
670
 
void
671
 
ebook_test_utils_book_open (EBook    *book,
672
 
                            gboolean  only_if_exists)
673
 
{
674
 
        GError *error = NULL;
675
 
 
676
 
        if (!e_book_open (book, only_if_exists, &error)) {
677
 
                const gchar *uri;
678
 
 
679
 
                uri = e_book_get_uri (book);
680
 
 
681
 
                g_warning ("failed to open addressbook: `%s': %s", uri,
682
 
                                error->message);
683
 
                exit (1);
684
 
        }
685
 
}
686
 
 
687
 
void
688
 
ebook_test_utils_book_remove (EBook *book)
689
 
{
690
 
        GError *error = NULL;
691
 
 
692
 
        if (!e_book_remove (book, &error)) {
693
 
                g_warning ("failed to remove book; %s\n", error->message);
694
 
                exit (1);
695
 
        }
696
 
        test_print ("successfully removed the temporary addressbook\n");
697
 
 
698
 
        g_object_unref (book);
699
 
}
700
 
 
701
 
static void
702
 
remove_cb (EBook *book, const GError *error, EBookTestClosure *closure)
703
 
{
704
 
        if (error) {
705
 
                g_warning ("failed to asynchronously remove the book: "
706
 
                                "status %d (%s)", error->code, error->message);
707
 
                exit (1);
708
 
        }
709
 
 
710
 
        test_print ("successfully asynchronously removed the temporary "
711
 
                        "addressbook\n");
712
 
        if (closure)
713
 
                (*closure->cb) (closure);
714
 
}
715
 
 
716
 
void
717
 
ebook_test_utils_book_async_remove (EBook       *book,
718
 
                                    GSourceFunc  callback,
719
 
                                    gpointer     user_data)
720
 
{
721
 
        EBookTestClosure *closure;
722
 
 
723
 
        closure = g_new0 (EBookTestClosure, 1);
724
 
        closure->cb = callback;
725
 
        closure->user_data = user_data;
726
 
        if (!e_book_remove_async (book, (EBookAsyncCallback) remove_cb, closure)) {
727
 
                g_warning ("failed to set up book removal");
728
 
                exit (1);
729
 
        }
730
 
}
731
 
 
732
 
void
733
 
ebook_test_utils_book_get_book_view (EBook       *book,
734
 
                                     EBookQuery  *query,
735
 
                                     EBookView  **view)
736
 
{
737
 
        GError *error = NULL;
738
 
 
739
 
        if (!e_book_get_book_view (book, query, NULL, -1, view, &error)) {
740
 
                const gchar *uri;
741
 
 
742
 
                uri = e_book_get_uri (book);
743
 
 
744
 
                g_warning ("failed to get view for addressbook: `%s': %s", uri,
745
 
                                error->message);
746
 
                exit (1);
747
 
        }
748
 
}
749
 
 
750
 
static void
751
 
get_book_view_cb (EBook            *book,
752
 
                  const GError     *error,
753
 
                  EBookView        *view,
754
 
                  EBookTestClosure *closure)
755
 
{
756
 
        if (error) {
757
 
                g_warning ("failed to asynchronously get book view for the "
758
 
                                "book: status %d (%s)", error->code, error->message);
759
 
                exit (1);
760
 
        }
761
 
 
762
 
        closure->view = view;
763
 
 
764
 
        test_print ("successfully asynchronously retrieved the book view\n");
765
 
        if (closure)
766
 
                (*closure->cb) (closure);
767
 
}
768
 
 
769
 
void
770
 
ebook_test_utils_book_async_get_book_view (EBook       *book,
771
 
                                           EBookQuery  *query,
772
 
                                           GSourceFunc  callback,
773
 
                                           gpointer     user_data)
774
 
{
775
 
        EBookTestClosure *closure;
776
 
 
777
 
        closure = g_new0 (EBookTestClosure, 1);
778
 
        closure->cb = callback;
779
 
        closure->user_data = user_data;
780
 
        if (!e_book_get_book_view_async (book, query, NULL, -1, (EBookBookViewAsyncCallback) get_book_view_cb, closure)) {
781
 
                g_warning ("failed to set up book view retrieval");
782
 
                exit (1);
783
 
        }
784
 
}