aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-digest-folder.c
blob: 070e5aed3b7d766816e9d82e253b2a4880a3d96b (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2001 Ximian, Inc. (www.ximian.com)
 *
 * 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.
 *
 */

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

#include "camel-digest-folder.h"
#include "camel-digest-summary.h"

#include "camel-exception.h"
#include "camel-multipart.h"
#include "camel-mime-message.h"
#include "camel-folder-search.h"

#define d(x)

#define _PRIVATE(o) (((CamelDigestFolder *)(o))->priv)

struct _CamelDigestFolderPrivate {
    CamelMimeMessage *message;
    CamelFolderSearch *search;
#ifdef ENABLE_THREADS
    GMutex *search_lock;
#endif
};

#ifdef ENABLE_THREADS
#define CAMEL_DIGEST_FOLDER_LOCK(f, l) (g_mutex_lock(((CamelDigestFolder *)f)->priv->l))
#define CAMEL_DIGEST_FOLDER_UNLOCK(f, l) (g_mutex_unlock(((CamelDigestFolder *)f)->priv->l))
#else
#define CAMEL_DIGEST_FOLDER_LOCK(f, l)
#define CAMEL_DIGEST_FOLDER_UNLOCK(f, l)
#endif

static CamelFolderClass *parent_class = NULL;

static void digest_refresh_info (CamelFolder *folder, CamelException *ex);
static void digest_sync (CamelFolder *folder, gboolean expunge, CamelException *ex);
static const char *digest_get_full_name (CamelFolder *folder);
static void digest_expunge (CamelFolder *folder, CamelException *ex);

/* message manipulation */
static CamelMimeMessage *digest_get_message (CamelFolder *folder, const gchar *uid,
                         CamelException *ex);
static void digest_append_message (CamelFolder *folder, CamelMimeMessage *message,
                   const CamelMessageInfo *info, char **appended_uid, CamelException *ex);
static void digest_transfer_messages_to (CamelFolder *source, GPtrArray *uids,
                     CamelFolder *dest, GPtrArray **transferred_uids,
                     gboolean delete_originals, CamelException *ex);

static GPtrArray *digest_search_by_expression (CamelFolder *folder, const char *expression,
                           CamelException *ex);

static GPtrArray *digest_search_by_uids (CamelFolder *folder, const char *expression,
                     GPtrArray *uids, CamelException *ex);

static void digest_search_free (CamelFolder *folder, GPtrArray *result);

static void
camel_digest_folder_class_init (CamelDigestFolderClass *camel_digest_folder_class)
{
    CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_digest_folder_class);
    
    parent_class = CAMEL_FOLDER_CLASS (camel_type_get_global_classfuncs (camel_folder_get_type ()));
    
    /* virtual method definition */
    
    /* virtual method overload */
    camel_folder_class->refresh_info = digest_refresh_info;
    camel_folder_class->sync = digest_sync;
    camel_folder_class->expunge = digest_expunge;
    camel_folder_class->get_full_name = digest_get_full_name;
    
    camel_folder_class->get_message = digest_get_message;
    camel_folder_class->append_message = digest_append_message;
    camel_folder_class->transfer_messages_to = digest_transfer_messages_to;
    
    camel_folder_class->search_by_expression = digest_search_by_expression;
    camel_folder_class->search_by_uids = digest_search_by_uids;
    camel_folder_class->search_free = digest_search_free;
}

static void
camel_digest_folder_init (gpointer object, gpointer klass)
{
    CamelDigestFolder *digest_folder = CAMEL_DIGEST_FOLDER (object);
    CamelFolder *folder = CAMEL_FOLDER (object);
    
    folder->folder_flags |= CAMEL_FOLDER_HAS_SUMMARY_CAPABILITY | CAMEL_FOLDER_HAS_SEARCH_CAPABILITY;
    
    folder->summary = camel_digest_summary_new ();
    
    digest_folder->priv = g_new (struct _CamelDigestFolderPrivate, 1);
    digest_folder->priv->message = NULL;
    digest_folder->priv->search = NULL;
#ifdef ENABLE_THREADS
    digest_folder->priv->search_lock = g_mutex_new ();
#endif
}

static void           
digest_finalize (CamelObject *object)
{
    CamelDigestFolder *digest_folder = CAMEL_DIGEST_FOLDER (object);
    CamelFolder *folder = CAMEL_FOLDER (object);
    
    if (folder->summary) {
        camel_object_unref (CAMEL_OBJECT (folder->summary));
        folder->summary = NULL;
    }
    
    camel_object_unref (CAMEL_OBJECT (digest_folder->priv->message));
    
    if (digest_folder->priv->search)
        camel_object_unref (CAMEL_OBJECT (digest_folder->priv->search));
    
#ifdef ENABLE_THREADS
    g_mutex_free (digest_folder->priv->search_lock);
#endif
    
    g_free (digest_folder->priv);
}

CamelType
camel_digest_folder_get_type (void)
{
    static CamelType type = CAMEL_INVALID_TYPE;
    
    if (type == CAMEL_INVALID_TYPE) {
        type = camel_type_register (CAMEL_FOLDER_TYPE,
                        "CamelDigestFolder",
                        sizeof (CamelDigestFolder),
                        sizeof (CamelDigestFolderClass),
                        (CamelObjectClassInitFunc) camel_digest_folder_class_init,
                        NULL,
                        (CamelObjectInitFunc) camel_digest_folder_init,
                        (CamelObjectFinalizeFunc) digest_finalize);
    }
    
    return type;
}

static gboolean
multipart_contains_message_parts (CamelMultipart *multipart)
{
    gboolean has_message_parts = FALSE;
    CamelDataWrapper *wrapper;
    CamelMimePart *part;
    int i, parts;
    
    parts = camel_multipart_get_number (multipart);
    for (i = 0; i < parts && !has_message_parts; i++) {
        part = camel_multipart_get_part (multipart, i);
        wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (part));
        if (CAMEL_IS_MULTIPART (wrapper)) {
            has_message_parts = multipart_contains_message_parts (CAMEL_MULTIPART (wrapper));
        } else if (CAMEL_IS_MIME_MESSAGE (wrapper)) {
            has_message_parts = TRUE;
        }
    }
    
    return has_message_parts;
}

static void
digest_add_multipart (CamelFolder *folder, CamelMultipart *multipart, const char *preuid)
{
    CamelDataWrapper *wrapper;
    CamelMessageInfo *info;
    CamelMimePart *part;
    int parts, i;
    char *uid;
    
    parts = camel_multipart_get_number (multipart);
    for (i = 0; i < parts; i++) {
        part = camel_multipart_get_part (multipart, i);
        
        wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (part));
        
        if (CAMEL_IS_MULTIPART (wrapper)) {
            uid = g_strdup_printf ("%s%d.", preuid, i);
            digest_add_multipart (folder, CAMEL_MULTIPART (wrapper), uid);
            g_free (uid);
            continue;
        } else if (!CAMEL_IS_MIME_MESSAGE (wrapper)) {
            continue;
        }
        
        info = camel_folder_summary_info_new_from_message (folder->summary, CAMEL_MIME_MESSAGE (wrapper));
        
        uid = g_strdup_printf ("%s%d", preuid, i);
        camel_message_info_set_uid (info, uid);
        camel_folder_summary_add (folder->summary, info);
    }
}

static void
construct_summary (CamelFolder *folder, CamelMultipart *multipart)
{
    digest_add_multipart (folder, multipart, "");
}

CamelFolder *
camel_digest_folder_new (CamelStore *parent_store, CamelMimeMessage *message)
{
    CamelDigestFolder *digest_folder;
    CamelDataWrapper *wrapper;
    CamelFolder *folder;
    
    wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (message));
    if (!wrapper || !CAMEL_IS_MULTIPART (wrapper))
        return NULL;
    
    /* Make sure we have a multipart/digest subpart or at least some message/rfc822 attachments... */
    if (!header_content_type_is (CAMEL_MIME_PART (message)->content_type, "multipart", "digest")) {
        if (!multipart_contains_message_parts (CAMEL_MULTIPART (wrapper)))
            return NULL;
    }
    
    folder = CAMEL_FOLDER (camel_object_new (camel_digest_folder_get_type ()));
    digest_folder = CAMEL_DIGEST_FOLDER (folder);
    
    camel_folder_construct (folder, parent_store, "folder_name", "short_name");
    
    camel_object_ref (CAMEL_OBJECT (message));
    digest_folder->priv->message = message;
    
    construct_summary (folder, CAMEL_MULTIPART (wrapper));
    
    return folder;
}

static void
digest_refresh_info (CamelFolder *folder, CamelException *ex)
{
    
}

static void
digest_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
{
    /* no-op */
}

static void
digest_expunge (CamelFolder *folder, CamelException *ex)
{
    /* no-op */
}

static const char *
digest_get_full_name (CamelFolder *folder)
{
    return folder->full_name;
}

static void
digest_append_message (CamelFolder *folder, CamelMimeMessage *message,
               const CamelMessageInfo *info, char **appended_uid,
               CamelException *ex)
{
    /* no-op */
    if (appended_uid)
        *appended_uid = NULL;
}

static void
digest_transfer_messages_to (CamelFolder *source, GPtrArray *uids,
                 CamelFolder *dest, GPtrArray **transferred_uids,
                 gboolean delete_originals, CamelException *ex)
{
    /* no-op */
    if (transferred_uids)
        *transferred_uids = NULL;
}

static CamelMimeMessage *
digest_get_message (CamelFolder *folder, const char *uid, CamelException *ex)
{
    CamelDigestFolder *digest = CAMEL_DIGEST_FOLDER (folder);
    CamelDataWrapper *wrapper;
    CamelMimeMessage *message;
    CamelMimePart *part;
    char *subuid;
    int id;
    
    part = CAMEL_MIME_PART (digest->priv->message);
    wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (part));
    
    do {
        id = strtoul (uid, &subuid, 10);
        if (!CAMEL_IS_MULTIPART (wrapper))
            return NULL;
        
        part = camel_multipart_get_part (CAMEL_MULTIPART (wrapper), id);
        wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (part));
        uid = subuid + 1;
    } while (*subuid == '.');
    
    if (!CAMEL_IS_MIME_MESSAGE (wrapper))
        return NULL;
    
    message = CAMEL_MIME_MESSAGE (wrapper);
    camel_object_ref (CAMEL_OBJECT (message));
    
    return message;
}


static GPtrArray *
digest_search_by_expression (CamelFolder *folder, const char *expression, CamelException *ex)
{
    CamelDigestFolder *df = (CamelDigestFolder *) folder;
    CamelFolderSearch *search;
    GPtrArray *summary, *matches;
    
    summary = camel_folder_get_summary (folder);
    
    CAMEL_DIGEST_FOLDER_LOCK (folder, search_lock);
    
    if (!df->priv->search)
        df->priv->search = camel_folder_search_new ();
    
    search = df->priv->search;
    camel_folder_search_set_folder (search, folder);
    camel_folder_search_set_summary (search, summary);
    
    matches = camel_folder_search_execute_expression (search, expression, ex);
    
    CAMEL_DIGEST_FOLDER_UNLOCK (folder, search_lock);
    
    camel_folder_free_summary (folder, summary);
    
    return matches;
}

static GPtrArray *
digest_search_by_uids (CamelFolder *folder, const char *expression, GPtrArray *uids, CamelException *ex)
{
    CamelDigestFolder *df = (CamelDigestFolder *) folder;
    CamelFolderSearch *search;
    GPtrArray *summary, *matches;
    int i;
    
    summary = g_ptr_array_new ();
    for (i = 0; i < uids->len; i++) {
        CamelMessageInfo *info;
        
        info = camel_folder_get_message_info (folder, uids->pdata[i]);
        if (info)
            g_ptr_array_add (summary, info);
    }
    
    if (summary->len == 0)
        return summary;
    
    CAMEL_DIGEST_FOLDER_LOCK (folder, search_lock);
    
    if (!df->priv->search)
        df->priv->search = camel_folder_search_new ();
    
    search = df->priv->search;
    camel_folder_search_set_folder (search, folder);
    camel_folder_search_set_summary (search, summary);
    
    matches = camel_folder_search_execute_expression (search, expression, ex);
    
    CAMEL_DIGEST_FOLDER_UNLOCK (folder, search_lock);
    
    for (i = 0; i < summary->len; i++)
        camel_folder_free_message_info (folder, summary->pdata[i]);
    g_ptr_array_free (summary, TRUE);
    
    return matches;
}

static void
digest_search_free (CamelFolder *folder, GPtrArray *result)
{
    CamelDigestFolder *digest_folder = CAMEL_DIGEST_FOLDER (folder);
    
    CAMEL_DIGEST_FOLDER_LOCK (folder, search_lock);
    
    camel_folder_search_free_result (digest_folder->priv->search, result);
    
    CAMEL_DIGEST_FOLDER_UNLOCK (folder, search_lock);
}