aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mail-remote/evolution-mail-session.c
blob: 7f1e854f1a56e3165673c2b7053afc9617620ca4 (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
/* -*- 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 FACTORY_ID "OAFIID:GNOME_Evolution_Mail_Session_Factory:" BASE_VERSION
#define MAIL_SESSION_ID  "OAFIID:GNOME_Evolution_Mail_Session:" BASE_VERSION

#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 */
    GSList *listeners;

    guint account_added;
    guint account_changed;
    guint account_removed;
};

static int
is_storage(EAccount *ea)
{
    const char *uri;
    CamelProvider *prov;

    return(uri = e_account_get_string(ea, E_ACCOUNT_SOURCE_URL))
        && (prov = camel_provider_get(uri, NULL))
        && (prov->flags & CAMEL_PROVIDER_IS_STORAGE);
}

/* GObject methods */

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

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

    /* FIXME: Free accounts */

    (* 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 GNOME_Evolution_Mail_PropertyNames* names,
           GNOME_Evolution_Mail_Properties **props,
           CORBA_Environment * ev)
{
    EvolutionMailSession *ems = (EvolutionMailSession *)bonobo_object_from_servant(_servant);

    ems = ems;

    return CORBA_TRUE;
}

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

    seq = GNOME_Evolution_Mail_StoreInfos__alloc();

    /* FIXME: pattern? */

    len = g_list_length(p->stores);

    seq->_length = len;
    seq->_maximum = len;
    seq->_buffer = GNOME_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;

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

    return seq;
}

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

    printf("Adding listener to session\n");

    p->listeners = g_slist_append(p->listeners, CORBA_Object_duplicate(listener, ev));
}

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

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

    /* FIXME: need to use proper comparison function & free stuff, this works with orbit though */
    p->listeners = g_slist_remove(p->listeners, listener);
    CORBA_Object_release(listener, ev);
}

/* Initialization */

static void
evolution_mail_session_class_init (EvolutionMailSessionClass *klass)
{
    POA_GNOME_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_listener_event(EvolutionMailSession *ems, GNOME_Evolution_Mail_ChangeType how, EvolutionMailStore *store)
{
    struct _EvolutionMailSessionPrivate *p = _PRIVATE(ems);
    GNOME_Evolution_Mail_SessionChange *change;
    GSList *l;

    for (l=p->listeners;l;l=g_slist_next(l)) {
        CORBA_Environment ev;

        change = GNOME_Evolution_Mail_SessionChange__alloc();
        change->type = how;

        change->stores._length = 1;
        change->stores._maximum = 1;
        change->stores._buffer = GNOME_Evolution_Mail_StoreInfos_allocbuf(change->stores._maximum);
        CORBA_sequence_set_release(&change->stores, TRUE);
        e_mail_storeinfo_set_store(&change->stores._buffer[0], store);

        GNOME_Evolution_Mail_Listener_sessionChanged(l->data, bonobo_object_corba_objref((BonoboObject *)ems), change, &ev);
        if (ev._major != CORBA_NO_EXCEPTION) {
            printf("listener.sessionChanged() failed: %s\n", ev._id);
            /* TODO: if it fails, remove the listener? */
            CORBA_exception_free(&ev);
        } else {
            printf("listener.sessionChanged() successful\n");
        }
    }
}

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

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

        printf("Account added %s\n", ea->uid);
        store = evolution_mail_store_new(ems, ea);
        p->stores = g_list_append(p->stores, store);
        ems_listener_event(ems, GNOME_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) {
            printf("Account changed, now disabled %s\n", ea->uid);
            p->stores = g_list_remove(p->stores, store);
            ems_listener_event(ems, GNOME_Evolution_Mail_REMOVED, store);
            g_object_unref(store);
        } else {
            printf("Account changed, dont know how %s\n", ea->uid);
            ems_listener_event(ems, GNOME_Evolution_Mail_CHANGED, store);
        }
    } else if (ea->enabled && is_storage(ea)) {
        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_event(ems, GNOME_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) {
            printf("Account removed %s\n", ea->uid);
            p->stores = g_list_remove(p->stores, store);
            ems_listener_event(ems, GNOME_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;

    /* FIXME: listen to changes */

    /* 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_TYPE_FUNC_FULL (EvolutionMailSession, GNOME_Evolution_Mail_Session, PARENT_TYPE, evolution_mail_session)