aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/backend/ebook/e-book-view-listener.c
blob: 3ab2ff27086c4f8114b6d58ce16bf5d52fea3fbe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Exports the BookViewListener interface.  Maintains a queue of messages
 * which come in on the interface.
 *
 * Author:
 *   Nat Friedman (nat@ximian.com)
 *
 * Copyright 2000, Ximian, Inc.
 */

#include <config.h>
#include <bonobo/bonobo-main.h>
#include "e-book-view-listener.h"
#include "e-book-view.h"
#include "e-contact.h"
#include "e-book-marshal.h"

static EBookViewStatus e_book_view_listener_convert_status (GNOME_Evolution_Addressbook_CallStatus status);

enum {
    RESPONSE,
    LAST_SIGNAL
};

static guint e_book_view_listener_signals [LAST_SIGNAL];

static BonoboObjectClass          *parent_class;

struct _EBookViewListenerPrivate {
    guint stopped      : 1;
};

static void
e_book_view_listener_queue_response (EBookViewListener         *listener,
                     EBookViewListenerResponse *response)
{
    if (response == NULL)
        return;

    if (listener->priv->stopped) {
        /* Free response and return */
        g_list_foreach (response->ids, (GFunc)g_free, NULL);
        g_list_free (response->ids);
        g_list_foreach (response->contacts, (GFunc) g_object_unref, NULL);
        g_list_free (response->contacts);
        g_free (response->message);
        g_free (response);
        return;
    }

    g_signal_emit (listener, e_book_view_listener_signals [RESPONSE], 0, response);
}

/* Add, Remove, Modify */
static void
e_book_view_listener_queue_status_event (EBookViewListener          *listener,
                     EBookViewListenerOperation  op,
                     EBookViewStatus             status)
{
    EBookViewListenerResponse *resp;

    if (listener->priv->stopped)
        return;

    resp = g_new0 (EBookViewListenerResponse, 1);

    resp->op        = op;
    resp->status    = status;

    e_book_view_listener_queue_response (listener, resp);
}

/* Add, Remove, Modify */
static void
e_book_view_listener_queue_idlist_event (EBookViewListener          *listener,
                     EBookViewListenerOperation  op,
                     const GNOME_Evolution_Addressbook_ContactIdList *ids)
{
    EBookViewListenerResponse *resp;
    int i;

    if (listener->priv->stopped)
        return;

    resp = g_new0 (EBookViewListenerResponse, 1);

    resp->op        = op;
    resp->status    = E_BOOK_VIEW_STATUS_OK;

    for (i = 0; i < ids->_length; i ++) {
        resp->ids = g_list_prepend (resp->ids, g_strdup (ids->_buffer[i]));
    }

    e_book_view_listener_queue_response (listener, resp);
}

/* Add, Remove, Modify */
static void
e_book_view_listener_queue_sequence_event (EBookViewListener          *listener,
                       EBookViewListenerOperation  op,
                       const GNOME_Evolution_Addressbook_VCardList  *vcards)
{
    EBookViewListenerResponse *resp;
    int i;

    if (listener->priv->stopped)
        return;

    resp = g_new0 (EBookViewListenerResponse, 1);

    resp->op        = op;
    resp->status    = E_BOOK_VIEW_STATUS_OK;
    
    for ( i = 0; i < vcards->_length; i++ ) {
        resp->contacts = g_list_append(resp->contacts, e_contact_new_from_vcard (vcards->_buffer[i]));
    }

    e_book_view_listener_queue_response (listener, resp);
}

/* Status Message */
static void
e_book_view_listener_queue_message_event (EBookViewListener          *listener,
                      EBookViewListenerOperation  op,
                      const char                 *message)
{
    EBookViewListenerResponse *resp;

    if (listener->priv->stopped)
        return;

    resp = g_new0 (EBookViewListenerResponse, 1);

    resp->op        = op;
    resp->status    = E_BOOK_VIEW_STATUS_OK;
    resp->message   = g_strdup(message);

    e_book_view_listener_queue_response (listener, resp);
}

static void
impl_BookViewListener_notify_contacts_added (PortableServer_Servant servant,
                         const GNOME_Evolution_Addressbook_VCardList *vcards,
                         CORBA_Environment *ev)
{
    EBookViewListener *listener = E_BOOK_VIEW_LISTENER (bonobo_object (servant));

    printf ("impl_BookViewListener_notify_contacts_added\n");

    e_book_view_listener_queue_sequence_event (
        listener, ContactsAddedEvent, vcards);
}

static void
impl_BookViewListener_notify_contacts_removed (PortableServer_Servant servant,
                           const GNOME_Evolution_Addressbook_ContactIdList *ids,
                           CORBA_Environment *ev)
{
    EBookViewListener *listener = E_BOOK_VIEW_LISTENER (bonobo_object (servant));

    printf ("impl_BookViewListener_notify_contacts_removed\n");

    e_book_view_listener_queue_idlist_event (listener, ContactsRemovedEvent, ids);
}

static void
impl_BookViewListener_notify_contacts_changed (PortableServer_Servant servant,
                           const GNOME_Evolution_Addressbook_VCardList *vcards,
                           CORBA_Environment *ev)
{
    EBookViewListener *listener = E_BOOK_VIEW_LISTENER (bonobo_object (servant));

    printf ("impl_BookViewListener_notify_contacts_changed\n");

    e_book_view_listener_queue_sequence_event (
        listener, ContactsModifiedEvent, vcards);
}

static void
impl_BookViewListener_notify_sequence_complete (PortableServer_Servant servant,
                        const GNOME_Evolution_Addressbook_CallStatus status,
                        CORBA_Environment *ev)
{
    EBookViewListener *listener = E_BOOK_VIEW_LISTENER (bonobo_object (servant));

    printf ("impl_BookViewListener_notify_sequence_complete\n");

    e_book_view_listener_queue_status_event (listener, SequenceCompleteEvent,
                         e_book_view_listener_convert_status (status));
}

static void
impl_BookViewListener_notify_progress (PortableServer_Servant  servant,
                       const char             *message,
                       const CORBA_short       percent,
                       CORBA_Environment      *ev)
{
    EBookViewListener *listener = E_BOOK_VIEW_LISTENER (bonobo_object (servant));

    printf ("impl_BookViewListener_notify_progress\n");

    e_book_view_listener_queue_message_event (listener, StatusMessageEvent, message);
}

static EBookViewStatus
e_book_view_listener_convert_status (const GNOME_Evolution_Addressbook_CallStatus status)
{
    switch (status) {
    case GNOME_Evolution_Addressbook_Success:
        return E_BOOK_VIEW_STATUS_OK;
    case GNOME_Evolution_Addressbook_SearchTimeLimitExceeded:
        return E_BOOK_VIEW_STATUS_TIME_LIMIT_EXCEEDED;
    case GNOME_Evolution_Addressbook_SearchSizeLimitExceeded:
        return E_BOOK_VIEW_STATUS_SIZE_LIMIT_EXCEEDED;
    case GNOME_Evolution_Addressbook_InvalidQuery:
        return E_BOOK_VIEW_ERROR_INVALID_QUERY;
    case GNOME_Evolution_Addressbook_QueryRefused:
        return E_BOOK_VIEW_ERROR_QUERY_REFUSED;
    case GNOME_Evolution_Addressbook_OtherError:
    default:
        return E_BOOK_VIEW_ERROR_OTHER_ERROR;
    }
}

static void
e_book_view_listener_construct      (EBookViewListener *listener)
{
    /* nothing needed here */
}

/**
 * e_book_view_listener_new:
 * @book: the #EBookView for which the listener is to be bound
 *
 * Creates and returns a new #EBookViewListener for the book.
 *
 * Returns: a new #EBookViewListener
 */
EBookViewListener *
e_book_view_listener_new ()
{
    EBookViewListener *listener;

    listener = g_object_new (E_TYPE_BOOK_VIEW_LISTENER,
                 "poa", bonobo_poa_get_threaded (ORBIT_THREAD_HINT_ALL_AT_IDLE, NULL),
                 NULL);

    e_book_view_listener_construct (listener);

    return listener;
}

static void
e_book_view_listener_init (EBookViewListener *listener)
{
    listener->priv                 = g_new0 (EBookViewListenerPrivate, 1);
    listener->priv->stopped        = FALSE;
}

void
e_book_view_listener_stop (EBookViewListener *listener)
{
    g_return_if_fail (E_IS_BOOK_VIEW_LISTENER (listener));
    listener->priv->stopped = TRUE;
}

static void
e_book_view_listener_dispose (GObject *object)
{
    EBookViewListener *listener = E_BOOK_VIEW_LISTENER (object);

    if (listener->priv) {
        g_free (listener->priv);
        listener->priv = NULL;
    }

    if (G_OBJECT_CLASS (parent_class)->dispose)
        G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
e_book_view_listener_class_init (EBookViewListenerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    POA_GNOME_Evolution_Addressbook_BookViewListener__epv *epv;

    parent_class = g_type_class_ref (BONOBO_TYPE_OBJECT);

    e_book_view_listener_signals [RESPONSE] =
        g_signal_new ("response",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EBookViewListenerClass, response),
                  NULL, NULL,
                  e_book_marshal_NONE__POINTER,
                  G_TYPE_NONE,
                  1, G_TYPE_POINTER);

    object_class->dispose = e_book_view_listener_dispose;

    epv = &klass->epv;
    epv->notifyContactsChanged  = impl_BookViewListener_notify_contacts_changed;
    epv->notifyContactsRemoved  = impl_BookViewListener_notify_contacts_removed;
    epv->notifyContactsAdded    = impl_BookViewListener_notify_contacts_added;
    epv->notifySequenceComplete = impl_BookViewListener_notify_sequence_complete;
    epv->notifyProgress         = impl_BookViewListener_notify_progress;
}

BONOBO_TYPE_FUNC_FULL (
               EBookViewListener,
               GNOME_Evolution_Addressbook_BookViewListener,
               BONOBO_TYPE_OBJECT,
               e_book_view_listener);