aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mail-remote/evolution-mail-session.c
blob: b5a508b91db0a485bc967e9041d91da02a1cc713 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* 
 * Copyright (C) 2005  Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Michael Zucchi <notzed@novell.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <string.h>
#include <bonobo/bonobo-shlib-factory.h>
#include <bonobo/bonobo-control.h>
#include <bonobo/bonobo-i18n.h>
#include <bonobo/bonobo-exception.h>
#include "evolution-mail-session.h"

#include <e-util/e-account-list.h>

#include "evolution-mail-store.h"
#include "e-corba-utils.h"

#include <camel/camel-session.h>

#define PARENT_TYPE bonobo_object_get_type ()

static BonoboObjectClass *parent_class = NULL;

#define _PRIVATE(o) (g_type_instance_get_private ((GTypeInstance *)o, evolution_mail_session_get_type()))

struct _EvolutionMailSessionPrivate {
    EAccountList *accounts;
    GList *stores;

    /* FIXME: locking */
    EDList listeners;

    guint account_added;
    guint account_changed;
    guint account_removed;
};

static int
is_storage(EAccount *ea)
{
    const char *uri;
    int ok = FALSE;

    uri = e_account_get_string(ea, E_ACCOUNT_SOURCE_URL);
    if (uri && uri[0]) {
        CamelProvider *prov;
        CamelException ex = { 0 };

        prov = camel_provider_get(uri, &ex);
        camel_exception_clear(&ex);
        ok = prov && (prov->flags & CAMEL_PROVIDER_IS_STORAGE);
    }

    return ok;
}

/* GObject methods */

static void
impl_dispose (GObject *object)
{
    struct _EvolutionMailSessionPrivate *p = _PRIVATE(object);

    if (p->stores) {
        /* FIXME: free stores */
    }

    /* FIXME: Free accounts */

    /* FIXME: free listners */

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

static void
impl_finalize (GObject *object)
{
    g_warning("EvolutionMailStore is finalised!\n");

    (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}

/* Evolution.Mail.Session */

static CORBA_boolean
impl_getProperties(PortableServer_Servant _servant,
           const Evolution_Mail_PropertyNames* names,
           Evolution_Mail_Properties **props,
           CORBA_Environment * ev)
{
    EvolutionMailSession *ems = (EvolutionMailSession *)bonobo_object_from_servant(_servant);

    ems = ems;

    return CORBA_TRUE;
}

static Evolution_Mail_StoreInfos *
impl_getStores(PortableServer_Servant _servant,
           const CORBA_char * pattern,
           const Evolution_Mail_StoreListener listener,
           CORBA_Environment * ev)
{
    EvolutionMailSession *ems = (EvolutionMailSession *)bonobo_object_from_servant(_servant);
    struct _EvolutionMailSessionPrivate *p = _PRIVATE(ems);
    Evolution_Mail_StoreInfos *seq;
    int i, len;
    GList *l;

    seq = Evolution_Mail_StoreInfos__alloc();

    /* FIXME: pattern? */

    len = g_list_length(p->stores);

    seq->_length = len;
    seq->_maximum = len;
    seq->_buffer = Evolution_Mail_StoreInfos_allocbuf(seq->_length);
    CORBA_sequence_set_release(seq, TRUE);

    l = p->stores;
    for (i=0;l && i<len;i++) {
        EvolutionMailStore *store = l->data;

        evolution_mail_store_addlistener(store, listener);

        e_mail_storeinfo_set_store(&seq->_buffer[i], store);
        l = g_list_next(l);
    }

    return seq;
}

static void
impl_addListener(PortableServer_Servant _servant,
         const Evolution_Mail_SessionListener listener,
         CORBA_Environment * ev)
{
    EvolutionMailSession *ems = (EvolutionMailSession *)bonobo_object_from_servant(_servant);
    struct _EvolutionMailSessionPrivate *p = _PRIVATE(ems);

    e_mail_listener_add(&p->listeners, listener);
}

static void
impl_removeListener(PortableServer_Servant _servant,
            const Evolution_Mail_SessionListener listener,
            CORBA_Environment * ev)
{
    EvolutionMailSession *ems = (EvolutionMailSession *)bonobo_object_from_servant(_servant);
    struct _EvolutionMailSessionPrivate *p = _PRIVATE(ems);

    d(printf("Removing listener from session\n"));

    if (!e_mail_listener_remove(&p->listeners, listener)) {
        d(printf("no more listeners, could shut down session?\n"));
    }
}

/* Initialization */

static void
evolution_mail_session_class_init (EvolutionMailSessionClass *klass)
{
    POA_Evolution_Mail_Session__epv *epv = &klass->epv;
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    epv->getProperties = impl_getProperties;
    epv->getStores = impl_getStores;

    epv->addListener = impl_addListener;
    epv->removeListener = impl_removeListener;

    object_class->dispose = impl_dispose;
    object_class->finalize = impl_finalize;

    g_type_class_add_private(klass, sizeof(struct _EvolutionMailSessionPrivate));
}

static void
ems_set_changes(Evolution_Mail_SessionChange *change, Evolution_Mail_ChangeType how, EvolutionMailStore *store)
{
    change->type = how;
    change->stores._length = 1;
    change->stores._maximum = 1;
    change->stores._buffer = Evolution_Mail_StoreInfos_allocbuf(change->stores._maximum);
    CORBA_sequence_set_release(&change->stores, TRUE);

    e_mail_storeinfo_set_store(&change->stores._buffer[0], store);
}

static void
ems_listener_session_event(EvolutionMailSession *ems, Evolution_Mail_ChangeType how, EvolutionMailStore *store)
{
    Evolution_Mail_SessionChanges *changes;

    /* NB: we only ever create 1 changetype at the moment */

    changes = Evolution_Mail_SessionChanges__alloc();
    changes->_maximum = 1;
    changes->_length = 1;
    changes->_buffer = Evolution_Mail_SessionChanges_allocbuf(1);
    CORBA_sequence_set_release(changes, TRUE);
    ems_set_changes(&changes->_buffer[0], how, store);

    evolution_mail_session_changed(ems, changes);

    CORBA_free(changes);
}

static void
ems_account_added(EAccountList *eal, EAccount *ea, EvolutionMailSession *ems)
{
    struct _EvolutionMailSessionPrivate *p = _PRIVATE(ems);

    if (ea->enabled && is_storage(ea)) {
        EvolutionMailStore *store;

        d(printf("Account added %s\n", ea->uid));
        store = evolution_mail_store_new(ems, ea);
        p->stores = g_list_append(p->stores, store);
        ems_listener_session_event(ems, Evolution_Mail_ADDED, store);
    }
}

static void
ems_account_changed(EAccountList *eal, EAccount *ea, EvolutionMailSession *ems)
{
    struct _EvolutionMailSessionPrivate *p = _PRIVATE(ems);
    EvolutionMailStore *store = NULL;
    GList *l;

    for (l = p->stores;l;l=l->next) {
        if (((EvolutionMailStore *)l->data)->account == ea) {
            store = l->data;
            break;
        }
    }

    if (store) {
        /* account has been disabled? */
        if (!ea->enabled) {
            d(printf("Account changed, now disabled %s\n", ea->uid));
            p->stores = g_list_remove(p->stores, store);
            ems_listener_session_event(ems, Evolution_Mail_REMOVED, store);
            g_object_unref(store);
        } else {
            d(printf("Account changed, dont know how %s\n", ea->uid));
            ems_listener_session_event(ems, Evolution_Mail_CHANGED, store);
        }
    } else if (ea->enabled && is_storage(ea)) {
        d(printf("Account changed, now added %s\n", ea->uid));
        store = evolution_mail_store_new(ems, ea);
        p->stores = g_list_append(p->stores, store);
        ems_listener_session_event(ems, Evolution_Mail_ADDED, store);
    }
}

static void
ems_account_removed(EAccountList *eal, EAccount *ea, EvolutionMailSession *ems)
{
    struct _EvolutionMailSessionPrivate *p = _PRIVATE(ems);
    GList *l;

    /* for accounts we dont have, we dont care */

    for (l = p->stores;l;l=l->next) {
        EvolutionMailStore *store = l->data;

        if (store->account == ea) {
            d(printf("Account removed %s\n", ea->uid));
            p->stores = g_list_remove(p->stores, store);
            ems_listener_session_event(ems, Evolution_Mail_REMOVED, store);
            g_object_unref(store);
            break;
        }
    }
}

static void
evolution_mail_session_init (EvolutionMailSession *ems, EvolutionMailSessionClass *klass)
{
    GConfClient *gconf = gconf_client_get_default();
    struct _EvolutionMailSessionPrivate *p = _PRIVATE(ems);
    EIterator *iter;

    d(printf("EvolutionMailSession.init\n"));

    e_dlist_init(&p->listeners);

    /* local store first */
    p->stores = g_list_append(p->stores, evolution_mail_store_new(ems, NULL));

    p->accounts = e_account_list_new(gconf);
    iter = e_list_get_iterator((EList *)p->accounts);
    while (e_iterator_is_valid (iter)) {
        EAccount *ea;

        if ((ea = (EAccount *)e_iterator_get(iter))
            && ea->enabled
            && is_storage(ea)) {
            p->stores = g_list_append(p->stores, evolution_mail_store_new(ems, ea));
        }

        e_iterator_next(iter);
    }
    g_object_unref(iter);

    g_object_unref(gconf);

    p->account_added = g_signal_connect(p->accounts, "account_added", G_CALLBACK(ems_account_added), ems);
    p->account_changed = g_signal_connect(p->accounts, "account_changed", G_CALLBACK(ems_account_changed), ems);
    p->account_removed = g_signal_connect(p->accounts, "account_removed", G_CALLBACK(ems_account_removed), ems);

    ems->session = mail_component_peek_session(NULL);

    bonobo_object_set_immortal((BonoboObject *)ems, TRUE);
}

void
evolution_mail_session_changed(EvolutionMailSession *ems, Evolution_Mail_SessionChanges *changes)
{
    struct _EvolutionMailSessionPrivate *p = _PRIVATE(ems);

    if (!e_mail_listener_emit(&p->listeners, (EMailListenerChanged)Evolution_Mail_SessionListener_changed,
                  bonobo_object_corba_objref((BonoboObject *)ems), changes)) {
        w(printf("No more listeners for store, could dispose session object now?\n"));
    }
}

BONOBO_TYPE_FUNC_FULL (EvolutionMailSession, Evolution_Mail_Session, PARENT_TYPE, evolution_mail_session)