~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-client-gnome/src/contacts/addressbook/eds.c

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-12-24 16:33:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101224163355-tkvvikqxbrbav6up
Tags: 0.9.11-1
* New upstream release
* Add new build dependencies on libwebkit-dev and libyaml-dev

* Bump Standards-Version up to 3.9.1
* Bump debhelper compatibility to 8
* Patch another typo in the upstream code (lintian notice)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
3
3
 *  Author: Julien Bonjean <julien.bonjean@savoirfairelinux.com>
 
4
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
4
5
 *
5
6
 *  File originally copied from evolution module of deskbar-applet 2.24.1
6
7
 *   Authors :
40
41
#include <string.h>
41
42
#include <pango/pango.h>
42
43
#include "eds.h"
 
44
#include <addressbook-config.h>
43
45
 
44
46
/**
45
47
 * Structure used to store search callback and data
46
48
 */
47
 
typedef struct _Search_Handler_And_Data
48
 
{
49
 
  int search_id;
50
 
  SearchAsyncHandler handler;
51
 
  gpointer user_data;
52
 
  GList *hits;
53
 
  int max_results_remaining;
54
 
  int book_views_remaining;
 
49
typedef struct _Search_Handler_And_Data {
 
50
    SearchAsyncHandler search_handler;
 
51
    gpointer user_data;
 
52
    GList *hits;
 
53
    int max_results_remaining;
 
54
    EBookQuery *equery;
55
55
} Search_Handler_And_Data;
56
56
 
57
57
/**
58
 
 * Structure used to store open callback and data
59
 
 */
60
 
typedef struct _Open_Handler_And_Data
61
 
{
62
 
  OpenAsyncHandler handler;
63
 
} Open_Handler_And_Data;
64
 
 
65
 
/**
66
58
 * Size of image that will be displayed in contact list
67
59
 */
68
60
static int pixbuf_size = 32;
73
65
int remaining_books_to_open;
74
66
 
75
67
/**
76
 
 * Fields on which search will be performed
 
68
 * Current selected addressbook's uri and uid, initialized with default
77
69
 */
78
 
static EContactField search_fields[] =
79
 
  { E_CONTACT_FULL_NAME, E_CONTACT_PHONE_BUSINESS, E_CONTACT_NICKNAME, 0 };
 
70
static gchar *current_uri = NULL;
 
71
static gchar *current_uid = NULL;
 
72
static gchar *current_name = "Default";
80
73
 
81
 
static int n_search_fields = G_N_ELEMENTS (search_fields) - 1;
 
74
static EBookQueryTest current_test = E_BOOK_QUERY_BEGINS_WITH;
82
75
 
83
76
/**
84
77
 * Freeing a hit instance
85
78
 */
86
79
void
87
 
free_hit(Hit *h)
 
80
free_hit (Hit *h)
88
81
{
89
 
  g_free(h->name);
90
 
  g_free(h->phone_business);
91
 
  g_free(h->phone_home);
92
 
  g_free(h->phone_mobile);
93
 
  g_free(h);
 
82
    g_free (h->name);
 
83
    g_free (h->phone_business);
 
84
    g_free (h->phone_home);
 
85
    g_free (h->phone_mobile);
 
86
    g_free (h);
94
87
}
95
88
 
96
89
/**
99
92
gboolean
100
93
books_ready()
101
94
{
102
 
  return (g_slist_length(books_data) > 0);
 
95
    return (g_slist_length (books_data) > 0);
103
96
}
104
97
 
105
98
/**
108
101
gboolean
109
102
books_active()
110
103
{
111
 
  GSList *book_list_iterator;
112
 
  book_data_t *book_data;
113
 
 
114
 
  // Iterate throw the list
115
 
  for (book_list_iterator = books_data; book_list_iterator != NULL; book_list_iterator
116
 
      = book_list_iterator->next)
117
 
    {
118
 
      book_data = (book_data_t *) book_list_iterator->data;
119
 
      if (book_data->active)
120
 
        return TRUE;
121
 
    }
122
 
 
123
 
  // If no result
124
 
  return FALSE;
 
104
    GSList *book_list_iterator;
 
105
    book_data_t *book_data;
 
106
 
 
107
    if (!books_data) {
 
108
        DEBUG ("Addressbook: No books data (%s:%d)", __FILE__, __LINE__);
 
109
        return FALSE;
 
110
    }
 
111
 
 
112
    // Iterate throw the list
 
113
    for (book_list_iterator = books_data; book_list_iterator != NULL; book_list_iterator
 
114
            = book_list_iterator->next) {
 
115
        book_data = (book_data_t *) book_list_iterator->data;
 
116
 
 
117
        if (book_data->active)
 
118
            return TRUE;
 
119
    }
 
120
 
 
121
    // If no result
 
122
    return FALSE;
125
123
}
126
124
/**
127
125
 * Get a specific book data by UID
128
126
 */
129
127
book_data_t *
130
 
books_get_book_data_by_uid(gchar *uid)
 
128
books_get_book_data_by_uid (gchar *uid)
131
129
{
132
 
  GSList *book_list_iterator;
133
 
  book_data_t *book_data;
134
 
 
135
 
  // Iterate throw the list
136
 
  for (book_list_iterator = books_data; book_list_iterator != NULL; book_list_iterator
137
 
      = book_list_iterator->next)
138
 
    {
139
 
      book_data = (book_data_t *) book_list_iterator->data;
140
 
      if (strcmp(book_data->uid, uid) == 0)
141
 
        return book_data;
142
 
    }
143
 
 
144
 
  // If no result
145
 
  return NULL;
 
130
    GSList *book_list_iterator;
 
131
    book_data_t *book_data;
 
132
 
 
133
    if (!books_data) {
 
134
        DEBUG ("Addressbook: No books data (%s:%d)", __FILE__, __LINE__);
 
135
        return NULL;
 
136
    }
 
137
 
 
138
    DEBUG ("Addressbook: Get book data by uid: %s", uid);
 
139
 
 
140
    // Iterate throw the list
 
141
    for (book_list_iterator = books_data; book_list_iterator != NULL; book_list_iterator
 
142
            = book_list_iterator->next) {
 
143
        book_data = (book_data_t *) book_list_iterator->data;
 
144
 
 
145
        if (strcmp (book_data->uid, uid) == 0) {
 
146
            DEBUG ("Addressbook: Book %s found", uid);
 
147
            return book_data;
 
148
        }
 
149
    }
 
150
 
 
151
    DEBUG ("Addressbook: Could not found Book %s", uid);
 
152
    // If no result
 
153
    return NULL;
146
154
}
147
155
 
 
156
 
148
157
/**
149
 
 * Split a string of tokens separated by whitespace into an array of tokens.
 
158
 * Create a query which looks for the specified string in a contact's full name, email addresses and
 
159
 * nick name.
150
160
 */
151
 
static GArray *
152
 
split_query_string(const gchar *str)
153
 
{
154
 
  GArray *parts = g_array_sized_new(FALSE, FALSE, sizeof (char *), 2);
155
 
  PangoLogAttr *attrs;
156
 
  guint str_len = strlen (str), word_start = 0, i;
157
 
 
158
 
  attrs = g_new0 (PangoLogAttr, str_len + 1);
159
 
  /* TODO: do we need to specify a particular language or is NULL ok? */
160
 
  pango_get_log_attrs (str, -1, -1, NULL, attrs, str_len + 1);
161
 
 
162
 
  for (i = 0; i < str_len + 1; i++)
163
 
    {
164
 
      char *start_word, *end_word, *word;
165
 
      if (attrs[i].is_word_end)
166
 
        {
167
 
          start_word = g_utf8_offset_to_pointer (str, word_start);
168
 
          end_word = g_utf8_offset_to_pointer (str, i);
169
 
          word = g_strndup (start_word, end_word - start_word);
170
 
          g_array_append_val (parts, word);
171
 
        }
172
 
      if (attrs[i].is_word_start)
173
 
        {
174
 
          word_start = i;
175
 
        }
176
 
    }
177
 
  g_free (attrs);
178
 
  return parts;
179
 
}
180
 
 
181
 
  /**
182
 
   * Create a query which looks for the specified string in a contact's full name, email addresses and
183
 
   * nick name.
184
 
   */
185
161
static EBookQuery*
186
 
create_query(const char* s)
 
162
create_query (const char* s, EBookQueryTest test, AddressBook_Config *conf)
187
163
{
188
 
  EBookQuery *query;
189
 
  GArray *parts = split_query_string(s);
190
 
  EBookQuery ***field_queries;
191
 
  EBookQuery **q;
192
 
  EBookQuery **phone;
193
 
  guint j;
194
 
  int i;
195
 
 
196
 
  q = g_new0 (EBookQuery *, n_search_fields);
197
 
  field_queries = g_new0 (EBookQuery **, n_search_fields);
198
 
 
199
 
  for (i = 0; i < n_search_fields; i++)
200
 
    {
201
 
      field_queries[i] = g_new0 (EBookQuery *, parts->len);
202
 
      for (j = 0; j < parts->len; j++)
203
 
        {
204
 
          field_queries[i][j] = e_book_query_field_test(search_fields[i],
205
 
              E_BOOK_QUERY_CONTAINS, g_array_index (parts, gchar *, j));
206
 
        }
207
 
      q[i] = e_book_query_and(parts->len, field_queries[i], TRUE);
208
 
    }
209
 
  g_array_free(parts, TRUE);
210
 
 
211
 
  phone = g_new0 (EBookQuery *, 3);
212
 
  phone[0] = e_book_query_field_exists(E_CONTACT_PHONE_BUSINESS);
213
 
  phone[1] = e_book_query_field_exists(E_CONTACT_PHONE_HOME);
214
 
  phone[2] = e_book_query_field_exists(E_CONTACT_PHONE_MOBILE);
215
 
 
216
 
  query = e_book_query_andv(e_book_query_or(n_search_fields, q, FALSE),
217
 
      e_book_query_or(3, phone, FALSE), NULL);
218
 
 
219
 
  for (i = 0; i < n_search_fields; i++)
220
 
    {
221
 
      g_free(field_queries[i]);
222
 
    }
223
 
  g_free(field_queries);
224
 
  g_free(q);
225
 
  g_free(phone);
226
 
 
227
 
  return query;
 
164
 
 
165
    EBookQuery *equery;
 
166
    EBookQuery *queries[4];
 
167
 
 
168
    // Create the query
 
169
    int cpt = 0;
 
170
 
 
171
    // We could also use E_BOOK_QUERY_IS or E_BOOK_QUERY_BEGINS_WITH instead of E_BOOK_QUERY_CONTAINS
 
172
    queries[cpt++] = e_book_query_field_test (E_CONTACT_FULL_NAME, test, s);
 
173
 
 
174
    if (conf->search_phone_home)
 
175
        queries[cpt++] = e_book_query_field_test (E_CONTACT_PHONE_HOME, test, s);
 
176
 
 
177
    if (conf->search_phone_business)
 
178
        queries[cpt++] = e_book_query_field_test (E_CONTACT_PHONE_BUSINESS, test, s);
 
179
 
 
180
    if (conf->search_phone_mobile)
 
181
        queries[cpt++] = e_book_query_field_test (E_CONTACT_PHONE_MOBILE, test, s);
 
182
 
 
183
    equery = e_book_query_or (cpt, queries, TRUE);
 
184
 
 
185
    return equery;
228
186
}
229
187
 
230
188
/**
231
189
 * Retrieve the contact's picture
232
190
 */
233
191
static GdkPixbuf*
234
 
pixbuf_from_contact(EContact *contact)
235
 
{
236
 
 
237
 
  GdkPixbuf *pixbuf = NULL;
238
 
  EContactPhoto *photo = e_contact_get(contact, E_CONTACT_PHOTO);
239
 
  if (photo)
240
 
    {
241
 
      GdkPixbufLoader *loader;
242
 
 
243
 
      loader = gdk_pixbuf_loader_new();
244
 
 
245
 
      if (photo->type == E_CONTACT_PHOTO_TYPE_INLINED)
246
 
        {
247
 
          if (gdk_pixbuf_loader_write(loader,
248
 
              (guchar *) photo->data.inlined.data, photo->data.inlined.length,
249
 
              NULL))
250
 
            pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
251
 
        }
252
 
 
253
 
      // If pixbuf has been found, check size and resize if needed
254
 
      if (pixbuf)
255
 
        {
256
 
          GdkPixbuf *tmp;
257
 
          gint width = gdk_pixbuf_get_width(pixbuf);
258
 
          gint height = gdk_pixbuf_get_height(pixbuf);
259
 
          double scale = 1.0;
260
 
 
261
 
          if (height > width)
262
 
            {
263
 
              scale = pixbuf_size / (double) height;
264
 
            }
265
 
          else
266
 
            {
267
 
              scale = pixbuf_size / (double) width;
268
 
            }
269
 
 
270
 
          if (scale < 1.0)
271
 
            {
272
 
              tmp = gdk_pixbuf_scale_simple(pixbuf, width * scale, height
273
 
                  * scale, GDK_INTERP_BILINEAR);
274
 
              g_object_unref(pixbuf);
275
 
              pixbuf = tmp;
276
 
            }
277
 
        }
278
 
      e_contact_photo_free(photo);
279
 
    }
280
 
  return pixbuf;
281
 
}
 
192
pixbuf_from_contact (EContact *contact)
 
193
{
 
194
 
 
195
    GdkPixbuf *pixbuf = NULL;
 
196
    EContactPhoto *photo = e_contact_get (contact, E_CONTACT_PHOTO);
 
197
 
 
198
    if (photo) {
 
199
        GdkPixbufLoader *loader;
 
200
 
 
201
        loader = gdk_pixbuf_loader_new();
 
202
 
 
203
        if (photo->type == E_CONTACT_PHOTO_TYPE_INLINED) {
 
204
            if (gdk_pixbuf_loader_write (loader,
 
205
                                         (guchar *) photo->data.inlined.data, photo->data.inlined.length,
 
206
                                         NULL))
 
207
                pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
 
208
        }
 
209
 
 
210
        // If pixbuf has been found, check size and resize if needed
 
211
        if (pixbuf) {
 
212
            GdkPixbuf *tmp;
 
213
            gint width = gdk_pixbuf_get_width (pixbuf);
 
214
            gint height = gdk_pixbuf_get_height (pixbuf);
 
215
            double scale = 1.0;
 
216
 
 
217
            if (height > width) {
 
218
                scale = pixbuf_size / (double) height;
 
219
            } else {
 
220
                scale = pixbuf_size / (double) width;
 
221
            }
 
222
 
 
223
            if (scale < 1.0) {
 
224
                tmp = gdk_pixbuf_scale_simple (pixbuf, width * scale, height
 
225
                                               * scale, GDK_INTERP_BILINEAR);
 
226
                g_object_unref (pixbuf);
 
227
                pixbuf = tmp;
 
228
            }
 
229
        }
 
230
 
 
231
        e_contact_photo_free (photo);
 
232
    }
 
233
 
 
234
    return pixbuf;
 
235
}
 
236
 
 
237
/**
 
238
 * Final callback after all books have been processed.
 
239
 */
 
240
static void
 
241
view_finish_callback (EBookView *book_view, Search_Handler_And_Data *had)
 
242
{
 
243
    SearchAsyncHandler had_handler = had->search_handler;
 
244
    GList *had_hits = had->hits;
 
245
    gpointer had_user_data = had->user_data;
 
246
 
 
247
    g_free (had);
 
248
 
 
249
    DEBUG ("Addressbook: View finish");
 
250
 
 
251
    if (book_view != NULL)
 
252
        g_object_unref (book_view);
 
253
 
 
254
    // Call display callback
 
255
    had_handler (had_hits, had_user_data);
 
256
}
 
257
 
 
258
 
 
259
/**
 
260
 * Callback called after a contact have been found in EDS by search_async_by_contacts.
 
261
 */
 
262
static void
 
263
eds_query_result_cb (EBook *book, EBookStatus status, GList *contacts, gpointer user_data)
 
264
{
 
265
 
 
266
    DEBUG ("Addressbook: Search Result callback called");
 
267
 
 
268
    if (status != E_BOOK_ERROR_OK)
 
269
        ERROR ("Addressbook: Error: Status not OK on search callback");
 
270
 
 
271
    Search_Handler_And_Data *had = (Search_Handler_And_Data *) user_data;
 
272
 
 
273
    if (!contacts) {
 
274
        DEBUG ("Addressbook: No contact found");
 
275
        had->search_handler (NULL, user_data);
 
276
        return;
 
277
    }
 
278
 
 
279
    GList *l = NULL;
 
280
 
 
281
    if (status == E_BOOK_ERROR_OK) {
 
282
 
 
283
        gchar *number;
 
284
 
 
285
        // make sure we have a new list of hits
 
286
        had->hits = NULL;
 
287
 
 
288
        l = contacts;
 
289
 
 
290
        while (l) {
 
291
 
 
292
            Hit *hit = g_new (Hit, 1);
 
293
 
 
294
            if (hit) {
 
295
 
 
296
                // Get the photo contact
 
297
                hit->photo = pixbuf_from_contact (E_CONTACT (l->data));
 
298
 
 
299
                // Get business phone information
 
300
                fetch_information_from_contact (E_CONTACT (l->data), E_CONTACT_PHONE_BUSINESS, &number);
 
301
                hit->phone_business = g_strdup (number);
 
302
 
 
303
                // Get home phone information
 
304
                fetch_information_from_contact (E_CONTACT (l->data), E_CONTACT_PHONE_HOME, &number);
 
305
                hit->phone_home = g_strdup (number);
 
306
 
 
307
                // Get mobile phone information
 
308
                fetch_information_from_contact (E_CONTACT (l->data), E_CONTACT_PHONE_MOBILE, &number);
 
309
                hit->phone_mobile = g_strdup (number);
 
310
 
 
311
                hit->name = g_strdup ( (char *) e_contact_get_const (E_CONTACT (l->data), E_CONTACT_NAME_OR_ORG));
 
312
 
 
313
                if (!hit->name)
 
314
                    hit->name = "";
 
315
 
 
316
                /*
 
317
                        DEBUG ("Addressbook: Contact Found");
 
318
                        DEBUG ("Addressbook:     Full Name %s", hit->name);
 
319
                        DEBUG ("Addressbook:     Phone Home %s", hit->phone_home);
 
320
                        DEBUG ("Addressbook:     Phone Business %s", hit->phone_business);
 
321
                        DEBUG ("Addressbook:     Phone Mobile %s", hit->phone_mobile);
 
322
                */
 
323
 
 
324
                if (hit)
 
325
                    had->hits = g_list_append (had->hits, hit);
 
326
 
 
327
 
 
328
                // DEBUG ("Addressbook: max_result_remaining %d", had->max_results_remaining);
 
329
                had->max_results_remaining--;
 
330
 
 
331
                if (had->max_results_remaining <= 0)
 
332
                    break;
 
333
            }
 
334
 
 
335
            l = g_list_next (l);
 
336
 
 
337
        }
 
338
 
 
339
        view_finish_callback (NULL, had);
 
340
    }
 
341
 
 
342
    g_object_unref (book);
 
343
 
 
344
}
 
345
 
 
346
 
282
347
 
283
348
/**
284
349
 * Callback for asynchronous open of books
285
350
 */
286
351
static void
287
 
eds_async_open_callback(EBook *book, EBookStatus status, gpointer closure)
 
352
eds_async_open_callback (EBook *book, EBookStatus status, gpointer closure)
288
353
{
289
 
    Open_Handler_And_Data *had = (Open_Handler_And_Data *) closure;
290
 
 
291
 
    remaining_books_to_open--;
292
 
 
293
 
    DEBUG("eds_async_open_callback remaining book to open: %i", remaining_books_to_open);
 
354
    DEBUG ("Addressbook: Open book callback");
 
355
 
 
356
    Search_Handler_And_Data *had = (Search_Handler_And_Data *) closure;
294
357
 
295
358
    if (status == E_BOOK_ERROR_OK) {
296
359
 
297
 
        book_data_t *book_data = g_new(book_data_t, 1);
298
 
        book_data->active = FALSE;
299
 
        book_data->name = g_strdup(e_source_peek_name(e_book_get_source(book)));
300
 
        book_data->uid = g_strdup(e_source_peek_uid(e_book_get_source(book)));
301
 
        book_data->ebook = book;
302
 
        books_data = g_slist_prepend(books_data, book_data);
303
 
        had->handler();
304
 
    }
305
 
    else {
306
 
 
307
 
        WARN("Got error %d when opening book", status);
308
 
    }
 
360
        if (!e_book_is_opened (book)) {
 
361
            // We must open the addressbook
 
362
            e_book_open (book, FALSE, NULL);
 
363
        }
 
364
 
 
365
        if (e_book_async_get_contacts (book, had->equery, eds_query_result_cb, had))
 
366
            ERROR ("Addressbook: Error: While querying addressbook");
 
367
 
 
368
    } else {
 
369
        WARN ("Addressbook: Got error when opening book");
 
370
        gchar *state_string = NULL;
 
371
 
 
372
        switch (status) {
 
373
            case E_BOOK_ERROR_INVALID_ARG :
 
374
                state_string = g_strdup ("E_BOOK_ERROR_INVALID_ARG");
 
375
                break;
 
376
            case E_BOOK_ERROR_BUSY :
 
377
                state_string = g_strdup ("E_BOOK_ERROR_BUSY");
 
378
                break;
 
379
            case E_BOOK_ERROR_REPOSITORY_OFFLINE :
 
380
                state_string = g_strdup ("E_BOOK_ERROR_REPOSITORY_OFFLINE");
 
381
                break;
 
382
            case E_BOOK_ERROR_NO_SUCH_BOOK :
 
383
                state_string = g_strdup ("E_BOOK_ERROR_NO_SUCH_BOOK");
 
384
                break;
 
385
            case E_BOOK_ERROR_NO_SELF_CONTACT :
 
386
                state_string = g_strdup ("E_BOOK_ERROR_NO_SELF_CONTACT");
 
387
                break;
 
388
            case E_BOOK_ERROR_SOURCE_NOT_LOADED:
 
389
                state_string = g_strdup ("E_BOOK_ERROR_SOURCE_NOT_LOADED");
 
390
                break;
 
391
            case E_BOOK_ERROR_SOURCE_ALREADY_LOADED :
 
392
                state_string = g_strdup ("E_BOOK_ERROR_SOURCE_ALREADY_LOADED");
 
393
                break;
 
394
            case E_BOOK_ERROR_PERMISSION_DENIED :
 
395
                state_string = g_strdup ("E_BOOK_ERROR_PERMISSION_DENIED");
 
396
                break;
 
397
            case E_BOOK_ERROR_CONTACT_NOT_FOUND :
 
398
                state_string = g_strdup ("E_BOOK_ERROR_CONTACT_NOT_FOUND");
 
399
                break;
 
400
            case E_BOOK_ERROR_CONTACT_ID_ALREADY_EXISTS :
 
401
                state_string = g_strdup ("E_BOOK_ERROR_CONTACT_ID_ALREADY_EXISTS");
 
402
                break;
 
403
            case E_BOOK_ERROR_PROTOCOL_NOT_SUPPORTED :
 
404
                state_string = g_strdup ("E_BOOK_ERROR_PROTOCOL_NOT_SUPPORTED");
 
405
                break;
 
406
            case E_BOOK_ERROR_CANCELLED :
 
407
                state_string = g_strdup ("E_BOOK_ERROR_CANCELLED");
 
408
                break;
 
409
            case E_BOOK_ERROR_COULD_NOT_CANCEL :
 
410
                state_string = g_strdup ("E_BOOK_ERROR_COULD_NOT_CANCEL");
 
411
                break;
 
412
            case E_BOOK_ERROR_AUTHENTICATION_FAILED :
 
413
                state_string = g_strdup ("E_BOOK_ERROR_AUTHENTICATION_FAILED");
 
414
                break;
 
415
            case E_BOOK_ERROR_AUTHENTICATION_REQUIRED :
 
416
                state_string = g_strdup ("E_BOOK_ERROR_AUTHENTICATION_REQUIRED");
 
417
                break;
 
418
            case E_BOOK_ERROR_TLS_NOT_AVAILABLE :
 
419
                state_string = g_strdup ("E_BOOK_ERROR_TLS_NOT_AVAILABLE");
 
420
                break;
 
421
            case E_BOOK_ERROR_CORBA_EXCEPTION :
 
422
                state_string = g_strdup ("E_BOOK_ERROR_CORBA_EXCEPTION");
 
423
                break;
 
424
            case E_BOOK_ERROR_NO_SUCH_SOURCE :
 
425
                state_string = g_strdup ("E_BOOK_ERROR_NO_SUCH_SOURCE");
 
426
                break;
 
427
            case E_BOOK_ERROR_OFFLINE_UNAVAILABLE :
 
428
                state_string = g_strdup ("E_BOOK_ERROR_OFFLINE_UNAVAILABLE");
 
429
                break;
 
430
            case E_BOOK_ERROR_OTHER_ERROR :
 
431
                state_string = g_strdup ("E_BOOK_ERROR_OTHER_ERROR");
 
432
                break;
 
433
            case E_BOOK_ERROR_INVALID_SERVER_VERSION :
 
434
                state_string = g_strdup ("E_BOOK_ERROR_INVALID_SERVER_VERSION");
 
435
                break;
 
436
            default:
 
437
                break;
 
438
 
 
439
        }
 
440
 
 
441
        ERROR ("%s", state_string);
 
442
 
 
443
        g_free (state_string);
 
444
    }
 
445
 
309
446
}
310
447
 
311
448
/**
312
449
 * Initialize address book
313
450
 */
314
451
void
315
 
init(OpenAsyncHandler callback)
316
 
{
317
 
  GSList *list, *l;
318
 
  ESourceList *source_list = NULL;
319
 
  remaining_books_to_open = 0;
320
 
  books_data = NULL;
321
 
 
322
 
  source_list = e_source_list_new_for_gconf_default("/apps/evolution/addressbook/sources");
323
 
  
324
 
  if (source_list == NULL)
325
 
    {
326
 
      DEBUG("Error could not initialize source list for addressbook");
327
 
      return;
328
 
    }
329
 
 
330
 
  list = e_source_list_peek_groups(source_list);
331
 
 
332
 
  Open_Handler_And_Data *had = g_new (Open_Handler_And_Data, 1);
333
 
  had->handler = callback;
334
 
 
335
 
  for (l = list; l != NULL; l = l->next)
336
 
    {
337
 
 
338
 
      ESourceGroup *group = l->data;
339
 
      GSList *sources = NULL, *m;
340
 
      sources = e_source_group_peek_sources(group);
341
 
      for (m = sources; m != NULL; m = m->next)
342
 
        {
343
 
          ESource *source = m->data;
344
 
          EBook *book = e_book_new(source, NULL);
345
 
          if (book != NULL)
346
 
            {
347
 
              // Keep count of remaining books to open
348
 
 
349
 
              DEBUG("init addressbook %i", remaining_books_to_open);
350
 
              remaining_books_to_open++;
351
 
              
352
 
              // Asynchronous open
353
 
              e_book_async_open(book, FALSE, eds_async_open_callback, had);
354
 
            }
355
 
        }
356
 
    }
357
 
  current_search_id = 0;
358
 
 
359
 
  g_object_unref(source_list);
360
 
}
361
 
 
362
 
/**
363
 
 * Final callback after all books have been processed.
364
 
 */
365
 
static void
366
 
view_finish(EBookView *book_view, Search_Handler_And_Data *had)
367
 
{
368
 
  GList *i;
369
 
  SearchAsyncHandler had_handler = had->handler;
370
 
  GList *had_hits = had->hits;
371
 
  gpointer had_user_data = had->user_data;
372
 
  int search_id = had->search_id;
373
 
  g_free(had);
374
 
 
375
 
  if (book_view != NULL)
376
 
    g_object_unref(book_view);
377
 
 
378
 
  if (search_id == current_search_id)
379
 
    {
380
 
      // Reinitialize search id to prevent overflow
381
 
      if (current_search_id > 5000)
382
 
        current_search_id = 0;
383
 
 
384
 
      // Call display callback
385
 
      had_handler(had_hits, had_user_data);
386
 
    }
387
 
  else
388
 
    {
389
 
      // Some hits could have been processed but will not be used
390
 
      for (i = had_hits; i != NULL; i = i->next)
391
 
        {
392
 
          Hit *entry;
393
 
          entry = i->data;
394
 
          free_hit(entry);
395
 
        }
396
 
      g_list_free(had_hits);
397
 
    }
398
 
}
399
 
 
400
 
/**
401
 
 * Callback called after each ebook search completed.
402
 
 * Used to store book search results.
403
 
 */
404
 
static void
405
 
view_contacts_added_cb(EBookView *book_view, GList *contacts,
406
 
    gpointer user_data)
407
 
{
408
 
  GdkPixbuf *photo;
409
 
 
410
 
  Search_Handler_And_Data *had = (Search_Handler_And_Data *) user_data;
411
 
 
412
 
  // If it's not the last search launched, stop it
413
 
  if (had->search_id != current_search_id)
414
 
    {
415
 
      e_book_view_stop(book_view);
416
 
      return;
417
 
    }
418
 
 
419
 
  // If we reached max results
420
 
  if (had->max_results_remaining <= 0)
421
 
    {
422
 
      e_book_view_stop(book_view);
423
 
      had->book_views_remaining--;
424
 
 
425
 
      // All books have been computed
426
 
      if (had->book_views_remaining == 0)
427
 
        {
428
 
          view_finish(book_view, had);
429
 
          return;
430
 
        }
431
 
    }
432
 
 
433
 
  // For each contact
434
 
  for (; contacts != NULL; contacts = g_list_next (contacts))
435
 
    {
436
 
      EContact *contact;
437
 
      Hit *hit;
438
 
      gchar *number;
439
 
 
440
 
      contact = E_CONTACT (contacts->data);
441
 
      hit = g_new (Hit, 1);
442
 
 
443
 
      // Get the photo contact
444
 
      photo = pixbuf_from_contact(contact);
445
 
      hit->photo = photo;
446
 
 
447
 
      // Get business phone information
448
 
      fetch_information_from_contact(contact, E_CONTACT_PHONE_BUSINESS, &number);
449
 
      hit->phone_business = g_strdup(number);
450
 
 
451
 
      // Get home phone information
452
 
      fetch_information_from_contact(contact, E_CONTACT_PHONE_HOME, &number);
453
 
      hit->phone_home = g_strdup(number);
454
 
 
455
 
      // Get mobile phone information
456
 
      fetch_information_from_contact(contact, E_CONTACT_PHONE_MOBILE, &number);
457
 
      hit->phone_mobile = g_strdup(number);
458
 
 
459
 
      hit->name = g_strdup((char*) e_contact_get_const(contact,
460
 
          E_CONTACT_NAME_OR_ORG));
461
 
      if (!hit->name)
462
 
        hit->name = "";
463
 
 
464
 
      // Append list of contacts
465
 
      had->hits = g_list_append(had->hits, hit);
466
 
      had->max_results_remaining--;
467
 
 
468
 
      // If we reached max results
469
 
      if (had->max_results_remaining <= 0)
470
 
        {
471
 
          e_book_view_stop(book_view);
472
 
          had->book_views_remaining--;
473
 
          if (had->book_views_remaining == 0)
474
 
            {
475
 
              view_finish(book_view, had);
476
 
            }
477
 
          break;
478
 
        }
479
 
    }
480
 
}
481
 
 
482
 
/**
483
 
 * Callback called after each ebook search completed.
484
 
 * Used to call final callback when all books have been read.
485
 
 */
486
 
static void
487
 
view_completed_cb(EBookView *book_view, EBookViewStatus status UNUSED,
488
 
gpointer user_data)
489
 
{
490
 
  Search_Handler_And_Data *had = (Search_Handler_And_Data *) user_data;
491
 
  had->book_views_remaining--;
492
 
 
493
 
  // All books have been prcessed
494
 
  if (had->book_views_remaining == 0)
495
 
    {
496
 
      // Call finish function
497
 
      view_finish(book_view, had);
498
 
    }
499
 
}
500
 
 
501
 
/**
502
 
 * Perform an asynchronous search
503
 
 */
504
 
void
505
 
search_async(const char *query, int max_results, SearchAsyncHandler handler,
506
 
    gpointer user_data)
507
 
{
508
 
  // Increment search id
509
 
  current_search_id++;
510
 
 
511
 
  // If query is null
512
 
  if (strlen(query) < 1 || g_slist_length(books_data) == 0)
513
 
    {
514
 
      // If data displayed (from previous search), directly call callback
515
 
      handler(NULL, user_data);
516
 
 
517
 
      return;
518
 
    }
519
 
 
520
 
  GSList *iter;
521
 
  EBookQuery* book_query = create_query(query);
522
 
  Search_Handler_And_Data *had = g_new (Search_Handler_And_Data, 1);
523
 
  int search_count = 0;
524
 
 
525
 
  // Initialize search data
526
 
  had->search_id = current_search_id;
527
 
  had->handler = handler;
528
 
  had->user_data = user_data;
529
 
  had->hits = NULL;
530
 
  had->max_results_remaining = max_results;
531
 
  had->book_views_remaining = 0;
532
 
 
533
 
  // Iterate throw books data
534
 
  for (iter = books_data; iter != NULL; iter = iter->next)
535
 
    {
536
 
      book_data_t *book_data = (book_data_t *) iter->data;
537
 
 
538
 
      // If book is active
539
 
      if (book_data->active)
540
 
        {
541
 
          EBookView *book_view = NULL;
542
 
          e_book_get_book_view(book_data->ebook, book_query, NULL, max_results,
543
 
              &book_view, NULL);
544
 
 
545
 
          // If book view exists
546
 
          if (book_view != NULL)
547
 
            {
548
 
              // Perform search
549
 
              had->book_views_remaining++;
550
 
              g_signal_connect (book_view, "contacts_added", (GCallback) view_contacts_added_cb, had);
551
 
              g_signal_connect (book_view, "sequence_complete", (GCallback) view_completed_cb, had);
552
 
              e_book_view_start(book_view);
553
 
              search_count++;
554
 
            }
555
 
        }
556
 
    }
557
 
 
558
 
  e_book_query_unref(book_query);
559
 
 
560
 
  // If no search has been executed (no book selected)
561
 
  if (search_count == 0)
562
 
    {
563
 
      // Call last callback anyway
564
 
      view_finish(NULL, had);
565
 
    }
 
452
init ()
 
453
{
 
454
    GError *err = NULL;
 
455
    gchar *absuri, *reluri;
 
456
 
 
457
    EBook *default_addressbook = e_book_new_default_addressbook (&err);
 
458
 
 
459
    if (err)
 
460
        ERROR ("Addressbook: Error: Could not create new book from source: %s", err->message);
 
461
 
 
462
    ESource *default_source = e_book_get_source (default_addressbook);
 
463
 
 
464
    // DEBUG ("Addressbook: Default source relative uri %s", e_source_peek_relative_uri (default_source));
 
465
    // DEBUG ("Addressbook: Default source absolute uri %s", e_source_peek_absolute_uri (default_source));
 
466
 
 
467
    if (current_uri) {
 
468
        g_free (current_uri);
 
469
        current_uri = NULL;
 
470
    }
 
471
 
 
472
    if (current_uid) {
 
473
        g_free (current_uid);
 
474
        current_uid = NULL;
 
475
    }
 
476
 
 
477
    if (strcmp (current_name, "Default") != 0) {
 
478
        g_free (current_name);
 
479
        current_name = NULL;
 
480
    }
 
481
 
 
482
    // TODO: This should work but return a NULL pointer ...
 
483
    // if (! (group = e_source_peek_group (default_source)));
 
484
 
 
485
    // ERROR ("Addressbook: Error: No group found for default addressbook");
 
486
 
 
487
    absuri = g_strdup (e_source_peek_absolute_uri (default_source));
 
488
    // absuri = g_strdup (e_source_group_peek_base_uri (group));
 
489
    reluri = g_strdup (e_source_peek_relative_uri (default_source));
 
490
 
 
491
    if (!absuri) {
 
492
        absuri = g_malloc (1);
 
493
        *absuri = 0;
 
494
    }
 
495
 
 
496
    if (!reluri) {
 
497
        reluri = g_malloc (1);
 
498
        *reluri = 0;
 
499
    }
 
500
 
 
501
    // Do not overwrite current_name for default
 
502
    // current_name = g_strdup (e_source_peek_name (default_source));
 
503
 
 
504
    current_uid = g_strdup (e_source_peek_uid (default_source));
 
505
 
 
506
    if (strcmp (absuri+strlen (absuri)-1, "/") == 0)
 
507
        current_uri = g_strjoin ("", absuri, reluri, NULL);
 
508
    else
 
509
        current_uri = g_strjoin ("/", absuri, reluri, NULL);
 
510
 
 
511
    g_free (absuri);
 
512
    g_free (reluri);
 
513
}
 
514
 
 
515
 
 
516
/**
 
517
 * Fill book data
 
518
 */
 
519
void
 
520
fill_books_data ()
 
521
{
 
522
    GSList *list, *l;
 
523
    ESourceList *source_list = NULL;
 
524
    remaining_books_to_open = 0;
 
525
    books_data = NULL;
 
526
 
 
527
    source_list = e_source_list_new_for_gconf_default ("/apps/evolution/addressbook/sources");
 
528
 
 
529
    if (source_list == NULL) {
 
530
        DEBUG ("Addressbook: Error could not initialize source list for addressbook (%s:%d)", __FILE__, __LINE__);
 
531
        return;
 
532
    }
 
533
 
 
534
    list = e_source_list_peek_groups (source_list);
 
535
 
 
536
    if (!list) {
 
537
        DEBUG ("Addressbook: Address Book source groups are missing (%s:%d)! Check your GConf setup.", __FILE__, __LINE__);
 
538
        return;
 
539
    }
 
540
 
 
541
 
 
542
    for (l = list; l != NULL; l = l->next) {
 
543
 
 
544
        ESourceGroup *group = l->data;
 
545
        GSList *sources = NULL, *m;
 
546
        gchar *absuri;
 
547
 
 
548
        absuri = g_strdup (e_source_group_peek_base_uri (group));
 
549
 
 
550
        sources = e_source_group_peek_sources (group);
 
551
 
 
552
        for (m = sources; m != NULL; m = m->next) {
 
553
 
 
554
            ESource *source = m->data;
 
555
 
 
556
            book_data_t *book_data = g_new (book_data_t, 1);
 
557
            book_data->active = FALSE;
 
558
            book_data->name = g_strdup (e_source_peek_name (source));
 
559
            book_data->uid = g_strdup (e_source_peek_uid (source));
 
560
 
 
561
            const gchar *property_name = "default";
 
562
            const gchar *prop = e_source_get_property (source, property_name);
 
563
 
 
564
            if (prop)
 
565
                if (strcmp (prop, "true") == 0)
 
566
                    book_data->isdefault = TRUE;
 
567
                else
 
568
                    book_data->isdefault = FALSE;
 
569
            else
 
570
                book_data->isdefault = FALSE;
 
571
 
 
572
            if (strcmp (absuri+strlen (absuri)-1, "/") == 0)
 
573
                book_data->uri = g_strjoin ("", absuri, e_source_peek_relative_uri (source), NULL);
 
574
            else
 
575
                book_data->uri = g_strjoin ("/", absuri, e_source_peek_relative_uri (source), NULL);
 
576
 
 
577
            books_data = g_slist_prepend (books_data, book_data);
 
578
 
 
579
        }
 
580
 
 
581
        g_free (absuri);
 
582
    }
 
583
 
 
584
    g_object_unref (source_list);
 
585
}
 
586
 
 
587
void
 
588
search_async_by_contacts (const char *query, int max_results, SearchAsyncHandler handler, gpointer user_data)
 
589
{
 
590
    GError *err = NULL;
 
591
    EBook *book;
 
592
 
 
593
    DEBUG ("Addressbook: New search by contacts: %s, max_results %d", query, max_results);
 
594
 
 
595
    if (strlen (query) < 1) {
 
596
        DEBUG ("Addressbook: Query is empty");
 
597
        handler (NULL, user_data);
 
598
        return;
 
599
    }
 
600
 
 
601
    Search_Handler_And_Data *had = g_new (Search_Handler_And_Data, 1);
 
602
 
 
603
    // initialize search data
 
604
    had->search_handler = handler;
 
605
    had->user_data = user_data;
 
606
    had->hits = NULL;
 
607
    had->max_results_remaining = max_results;
 
608
    had->equery = create_query (query, current_test, (AddressBook_Config *) (user_data));
 
609
 
 
610
    if (!current_uri)
 
611
        ERROR ("Addressbook: Error: Current addressbook uri not specified uri");
 
612
 
 
613
 
 
614
    DEBUG ("Addressbook: Opening addressbook: %s", current_uri);
 
615
    DEBUG ("Addressbook: Opening addressbook: %s", current_name);
 
616
 
 
617
    // TODO: This hack is necessary as we cannot access group's base_uri of the default book
 
618
    // see init()
 
619
 
 
620
    if (strcmp (current_name, "Default") == 0)
 
621
        book = e_book_new_default_addressbook (&err);
 
622
    else
 
623
        book = e_book_new_from_uri (current_uri, &err);
 
624
 
 
625
    if (err)
 
626
        ERROR ("Addressbook: Error: Could not open new book: %s", err->message);
 
627
 
 
628
    if (book) {
 
629
        DEBUG ("Addressbook: Created empty book successfully");
 
630
 
 
631
        // Asynchronous open
 
632
        e_book_async_open (book, TRUE, eds_async_open_callback, had);
 
633
    } else
 
634
        ERROR ("Addressbook: Error: No book available");
 
635
 
 
636
 
566
637
}
567
638
 
568
639
/**
569
640
 * Fetch information for a specific contact
570
641
 */
571
642
void
572
 
fetch_information_from_contact(EContact *contact, EContactField field,
573
 
    gchar **info)
574
 
{
575
 
  gchar *to_fetch;
576
 
 
577
 
  to_fetch = g_strdup((char*) e_contact_get_const(contact, field));
578
 
  if (!to_fetch)
579
 
    {
580
 
      to_fetch = g_strdup(EMPTY_ENTRY);
581
 
    }
582
 
 
583
 
  *info = g_strdup(to_fetch);
584
 
}
585
 
 
 
643
fetch_information_from_contact (EContact *contact, EContactField field, gchar **info)
 
644
{
 
645
    gchar *to_fetch;
 
646
 
 
647
    to_fetch = g_strdup ( (char*) e_contact_get_const (contact, field));
 
648
 
 
649
    if (!to_fetch) {
 
650
        to_fetch = g_strdup (EMPTY_ENTRY);
 
651
    }
 
652
 
 
653
    *info = g_strdup (to_fetch);
 
654
}
 
655
 
 
656
void
 
657
set_current_addressbook (const gchar *name)
 
658
{
 
659
 
 
660
    GSList *book_list_iterator;
 
661
    book_data_t *book_data;
 
662
 
 
663
    if (!books_data) {
 
664
        DEBUG ("Addressbook: No books data (%s:%d)", __FILE__, __LINE__);
 
665
        return NULL;
 
666
    }
 
667
 
 
668
    // Iterate throw the list
 
669
    for (book_list_iterator = books_data; book_list_iterator != NULL; book_list_iterator
 
670
            = book_list_iterator->next) {
 
671
        book_data = (book_data_t *) book_list_iterator->data;
 
672
 
 
673
        if (strcmp (book_data->name, name) == 0) {
 
674
            current_uri = book_data->uri;
 
675
            current_uid = book_data->uid;
 
676
            current_name = book_data->name;
 
677
        }
 
678
    }
 
679
}
 
680
 
 
681
 
 
682
const gchar *
 
683
get_current_addressbook (void)
 
684
{
 
685
    return current_name;
 
686
}
 
687
 
 
688
 
 
689
void
 
690
set_current_addressbook_test (EBookQueryTest test)
 
691
{
 
692
    current_test = test;
 
693
}
 
694
 
 
695
EBookQueryTest
 
696
get_current_addressbook_test (void)
 
697
{
 
698
    return current_test;
 
699
}