aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mail-remote/e-corba-utils.c
blob: 0b4aae19a4159025b2adaaf53c21898ef9040657 (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

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

#include <errno.h>

#include "e-corba-utils.h"

#include "evolution-mail-store.h"
#include "evolution-mail-folder.h"
#include "evolution-mail-messagestream.h"

#include "em-message-stream.h"

#include <camel/camel-folder-summary.h>
#include <camel/camel-stream-mem.h>
#include <camel/camel-mime-message.h>

#include <bonobo/bonobo-stream-memory.h>
#include <bonobo/bonobo-i18n.h>
#include <bonobo/bonobo-exception.h>

#include <libedataserver/e-msgport.h>

static CORBA_char *
e_corba_strdup(const char *v)
{
    if (v)
        return CORBA_string_dup(v);
    else
        return CORBA_string_dup("");
}

void
e_mail_property_set_string(Evolution_Mail_Property *prop, const char *name, const char *val)
{
    prop->value._release = CORBA_TRUE;
    prop->value._type = TC_CORBA_string;
    prop->value._value = CORBA_sequence_CORBA_string_allocbuf(1);
    ((char **)prop->value._value)[0] = CORBA_string_dup(val);
    prop->name = CORBA_string_dup(name);
}

void
e_mail_property_set_null(Evolution_Mail_Property *prop, const char *name)
{
    prop->value._release = CORBA_TRUE;
    prop->value._type = TC_null;
    prop->name = CORBA_string_dup(name);
}

void
e_mail_storeinfo_set_store(Evolution_Mail_StoreInfo *si, EvolutionMailStore *store)
{
    si->name = CORBA_string_dup(evolution_mail_store_get_name(store));
    si->uid = CORBA_string_dup(evolution_mail_store_get_uid(store));
    si->store = CORBA_Object_duplicate(bonobo_object_corba_objref((BonoboObject *)store), NULL);
}

void
e_mail_messageinfo_set_message(Evolution_Mail_MessageInfo *mi, CamelMessageInfo *info)
{
    const CamelTag *tag;
    const CamelFlag *flag;
    int i;

    mi->uid = CORBA_string_dup(camel_message_info_uid(info));
    mi->subject = e_corba_strdup(camel_message_info_subject(info));
    mi->to = e_corba_strdup(camel_message_info_to(info));
    mi->from = e_corba_strdup(camel_message_info_from(info));
    mi->flags = camel_message_info_flags(info);

    flag = camel_message_info_user_flags(info);
    mi->userFlags._maximum = camel_flag_list_size((CamelFlag **)&flag);
    mi->userFlags._length = mi->userFlags._maximum;
    if (mi->userFlags._maximum) {
        mi->userFlags._buffer = Evolution_Mail_UserFlags_allocbuf(mi->userFlags._maximum);
        CORBA_sequence_set_release(&mi->userFlags, CORBA_TRUE);

        for (i=0;flag;flag = flag->next,i++) {
            mi->userFlags._buffer[i] = CORBA_string_dup(flag->name);
            g_assert(mi->userFlags._buffer[i]);
        }
    }

    tag = camel_message_info_user_tags(info);
    mi->userTags._maximum = camel_tag_list_size((CamelTag **)&tag);
    mi->userTags._length = mi->userTags._maximum;
    if (mi->userTags._maximum) {
        mi->userTags._buffer = Evolution_Mail_UserTags_allocbuf(mi->userTags._maximum);
        CORBA_sequence_set_release(&mi->userFlags, CORBA_TRUE);

        for (i=0;tag;tag = tag->next,i++) {
            mi->userTags._buffer[i].name = CORBA_string_dup(tag->name);
            mi->userTags._buffer[i].value = CORBA_string_dup(tag->value);
            g_assert(mi->userTags._buffer[i].name);
            g_assert(mi->userTags._buffer[i].value);
        }
    }
}

CamelMessageInfo *
e_mail_messageinfoset_to_info(const Evolution_Mail_MessageInfoSet *mi)
{
    CamelMessageInfo *info;
    int i;

    info = camel_message_info_new(NULL);
    camel_message_info_set_flags(info, mi->flagSet, mi->flagMask);
    for (i=0;i<mi->userFlagSet._length;i++)
        camel_message_info_set_user_flag(info, mi->userFlagSet._buffer[i], TRUE);
    for (i=0;i<mi->userTags._length;i++)
        camel_message_info_set_user_tag(info, mi->userTags._buffer[i].name, mi->userTags._buffer[i].value);

    return info;
}

void
e_mail_folderinfo_set_folder(Evolution_Mail_FolderInfo *fi, EvolutionMailFolder *emf)
{
    fi->name = CORBA_string_dup(emf->name);
    fi->full_name = CORBA_string_dup(emf->full_name);
    fi->folder = CORBA_Object_duplicate(bonobo_object_corba_objref((BonoboObject *)emf), NULL);
}

CamelMimeMessage *
e_messagestream_to_message(const Evolution_Mail_MessageStream in, CORBA_Environment *ev)
{
    CamelStream *emms;
    CamelMimeMessage *msg;

    emms = em_message_stream_new(in);
    if (emms == NULL) {
        e_mail_exception_set(ev, Evolution_Mail_FAILED, _("Unknown reason"));
        return NULL;
    }

    msg = camel_mime_message_new();
    if (camel_data_wrapper_construct_from_stream((CamelDataWrapper *)msg, emms) == -1) {
        e_mail_exception_set(ev, Evolution_Mail_SYSTEM_ERROR, g_strerror(errno));
        camel_object_unref(msg);
        msg = NULL;
    }
    camel_object_unref(emms);

    return msg;
}

Evolution_Mail_MessageStream
e_messagestream_from_message(CamelMimeMessage *msg, CORBA_Environment *ev)
{
    CamelStreamMem *mem;
    EvolutionMailMessageStream *emms;
    Evolution_Mail_MessageStream out;

    /* didn't say it was going to be efficient ... */

    mem = (CamelStreamMem *)camel_stream_mem_new();
    if (camel_data_wrapper_write_to_stream((CamelDataWrapper *)msg, (CamelStream *)mem) == -1) {
        e_mail_exception_set(ev, Evolution_Mail_SYSTEM_ERROR, g_strerror(errno));
        out = CORBA_OBJECT_NIL;
    } else {
        camel_stream_reset((CamelStream *)mem);
        emms = evolution_mail_messagestream_new((CamelStream *)mem);
        out = CORBA_Object_duplicate(bonobo_object_corba_objref((BonoboObject *)emms), NULL);
    }
    camel_object_unref(mem);

    return out;
}

struct _e_mail_listener {
    struct _e_mail_listener *next;
    struct _e_mail_listener *prev;

    CORBA_Object listener;
};

static struct _e_mail_listener *
eml_find(struct _EDList *list, CORBA_Object listener)
{
    struct _e_mail_listener *l, *n;

    l = (struct _e_mail_listener *)list->head;
    n = l->next;
    while (n) {
        if (l->listener == listener)
            return l;
        l = n;
        n = n->next;
    }

    return NULL;
}

static void
eml_remove(struct _e_mail_listener *l)
{
    CORBA_Environment ev = { 0 };

    e_dlist_remove((EDListNode *)l);
    CORBA_Object_release(l->listener, &ev);
    g_free(l);

    if (ev._major != CORBA_NO_EXCEPTION)
        CORBA_exception_free(&ev);
}

void e_mail_listener_add(struct _EDList *list, CORBA_Object listener)
{
    struct _e_mail_listener *l;
    CORBA_Environment ev = { 0 };

    if (eml_find(list, listener) != NULL)
        return;

    listener = CORBA_Object_duplicate(listener, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        CORBA_exception_free(&ev);
    } else {
        l = g_malloc(sizeof(*l));
        l->listener = listener;
        e_dlist_addtail(list, (EDListNode *)l);
    }
}

gboolean e_mail_listener_remove(struct _EDList *list, CORBA_Object listener)
{
    struct _e_mail_listener *l;

    l = eml_find(list, listener);
    if (l)
        eml_remove(l);

    return !e_dlist_empty(list);
}

gboolean e_mail_listener_emit(struct _EDList *list, EMailListenerChanged emit, CORBA_Object source, void *changes)
{
    struct _e_mail_listener *l, *n;
    CORBA_Environment ev = { 0 };

    l = (struct _e_mail_listener *)list->head;
    n = l->next;
    while (n) {
        emit(l->listener, source, changes, &ev);
        if (ev._major != CORBA_NO_EXCEPTION) {
            e(printf("emit changed failed '%s', removing listener\n", ev._id));
            CORBA_exception_free(&ev);
            eml_remove(l);
        }
        l = n;
        n = n->next;
    }

    return !e_dlist_empty(list);
}

void e_mail_listener_free(struct _EDList *list)
{
    struct _e_mail_listener *l, *n;

    l = (struct _e_mail_listener *)list->head;
    n = l->next;
    while (n) {
        eml_remove(l);

        l = n;
        n = n->next;
    }
}

void e_mail_exception_set(CORBA_Environment *ev, Evolution_Mail_ErrorType id, const char *desc)
{
    Evolution_Mail_MailException *x;

    x = Evolution_Mail_MailException__alloc();
    x->id = id;
    x->desc = CORBA_string_dup(desc);
    CORBA_exception_set(ev, CORBA_USER_EXCEPTION, ex_Evolution_Mail_MailException, x);
}

void e_mail_exception_xfer_camel(CORBA_Environment *ev, CamelException *ex)
{
    e_mail_exception_set(ev, Evolution_Mail_CAMEL_ERROR, ex && ex->desc ? ex->desc:"");
    camel_exception_clear(ex);
}