aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/cache/camel-cache-folder.c
blob: c4379f81359568aa82dd938c1cfc841633672e57 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-cache-folder.c : class for a cache folder */

/* 
 * Authors:
 *   Dan Winship <danw@helixcode.com>
 *
 * Copyright (C) 2000 Helix Code, Inc. (www.helixcode.com)
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * 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
 */


/*
 * Notes on the cache provider:
 *
 * We require that the remote folder have persistent UIDs, and nothing
 * else. We require that the local store folder have persistent UIDs
 * and summary capability.
 *
 * If the remote folder does not have summary capability, we will need
 * to sync any new messages over to the local folder when the folder
 * is opened or when it changes. If the remote folder does have
 * summary capability, we can be more relaxed about doing this.
 *
 * If the remote folder has search capability, we will use it, at
 * least when the folder isn't synced. Otherwise if the local folder
 * has search capability, we will use that (but it will require
 * syncing the remote folder locally to use). Otherwise the cache
 * folder won't have search capability.
 *
 * CamelCacheFolder UIDs are remote UIDs, because we need to be able
 * to return a complete list of them at get_uids time, and the
 * messages might not all be present in the local folder, and we can't
 * predict what UIDs will be assigned to them when they are cached
 * there. We keep hash tables mapping remote to local UIDs and vice
 * versa, and a map file to cache this information between sessions.
 * The maps must always be 100% accurate.
 *
 * The messages in the local folder may not be in the same order as
 * the messages in the remote folder.
 *
 *
 * Many operations on the local folder are done with a NULL
 * CamelException, because having them fail only results in efficiency
 * problems, not actual permanent failures. (Eg, get_message will
 * try to append the message to the local folder, but doesn't check
 * for failure, because it already has the message to pass back to the
 * user.)
 */

#include "camel-cache-folder.h"
#include "camel-cache-store.h"
#include <camel/camel-exception.h>
#include <camel/camel-medium.h>

#define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
static CamelFolderClass *folder_class = NULL;

static void init (CamelFolder *folder, CamelStore *parent_store,
          CamelFolder *parent_folder, const gchar *name,
          gchar *separator, gboolean path_begins_with_sep,
          CamelException *ex);

static void refresh_info (CamelFolder *folder, CamelException *ex);

static void cache_sync (CamelFolder *folder, gboolean expunge, 
            CamelException *ex);

static void expunge (CamelFolder *folder, CamelException *ex);

static gint get_message_count (CamelFolder *folder);

static void append_message (CamelFolder *folder, CamelMimeMessage *message, 
                const CamelMessageInfo *info, CamelException *ex);

static guint32 get_message_flags (CamelFolder *folder, const char *uid);
static void set_message_flags (CamelFolder *folder, const char *uid,
                   guint32 flags, guint32 set);
static gboolean get_message_user_flag (CamelFolder *folder, const char *uid,
                       const char *name);
static void set_message_user_flag (CamelFolder *folder, const char *uid,
                   const char *name, gboolean value);
static const char *get_message_user_tag (CamelFolder *folder, const char *uid,
                     const char *name);
static void set_message_user_tag (CamelFolder *folder, const char *uid,
                  const char *name, const char *value);

static CamelMimeMessage *get_message (CamelFolder *folder, 
                      const gchar *uid,
                      CamelException *ex);

static GPtrArray *get_uids (CamelFolder *folder);
static GPtrArray *get_summary (CamelFolder *folder);
static GPtrArray *get_subfolder_names (CamelFolder *folder);
static void free_subfolder_names (CamelFolder *folder, GPtrArray *subfolders);

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

static const CamelMessageInfo *get_message_info (CamelFolder *folder,
                         const char *uid);

static void copy_message_to (CamelFolder *source, const char *uid,
                 CamelFolder *destination, CamelException *ex);

static void move_message_to (CamelFolder *source, const char *uid,
                 CamelFolder *destination, CamelException *ex);

static void finalize (CamelObject *object);

static void
camel_cache_folder_class_init (CamelCacheFolderClass *camel_cache_folder_class)
{
    CamelFolderClass *camel_folder_class =
        CAMEL_FOLDER_CLASS (camel_cache_folder_class);

    folder_class = CAMEL_FOLDER_CLASS (camel_type_get_global_classfuncs (camel_folder_get_type ()));

    /* virtual method overload */
    camel_folder_class->init = init;
    camel_folder_class->refresh_info = refresh_info;
    camel_folder_class->sync = cache_sync;
    camel_folder_class->expunge = expunge;
    camel_folder_class->get_message_count = get_message_count;
    camel_folder_class->append_message = append_message;
    camel_folder_class->get_message_flags = get_message_flags;
    camel_folder_class->set_message_flags = set_message_flags;
    camel_folder_class->get_message_user_flag = get_message_user_flag;
    camel_folder_class->set_message_user_flag = set_message_user_flag;
    camel_folder_class->get_message_user_tag = get_message_user_tag;
    camel_folder_class->set_message_user_tag = set_message_user_tag;
    camel_folder_class->get_message = get_message;
    camel_folder_class->get_uids = get_uids;
    camel_folder_class->free_uids = camel_folder_free_nop;
    camel_folder_class->get_summary = get_summary;
    camel_folder_class->free_summary = camel_folder_free_nop;
    camel_folder_class->get_subfolder_names = get_subfolder_names;
    camel_folder_class->free_subfolder_names = free_subfolder_names;
    camel_folder_class->search_by_expression = search_by_expression;
    camel_folder_class->get_message_info = get_message_info;
    camel_folder_class->copy_message_to = copy_message_to;
    camel_folder_class->move_message_to = move_message_to;
}

CamelType
camel_cache_folder_get_type (void)
{
    static CamelType camel_cache_folder_type = CAMEL_INVALID_TYPE;

    if (camel_cache_folder_type == CAMEL_INVALID_TYPE) {
        camel_cache_folder_type = camel_type_register (
            CAMEL_FOLDER_TYPE, "CamelCacheFolder",
            sizeof (CamelCacheFolder),
            sizeof (CamelCacheFolderClass),
            (CamelObjectClassInitFunc) camel_cache_folder_class_init,
            NULL,
            NULL,
            (CamelObjectFinalizeFunc) finalize);
    }

    return camel_cache_folder_type;
}


static void
cache_free_summary (CamelCacheFolder *cache_folder)
{
    if (cache_folder->remote_summary) {
        camel_folder_free_summary (cache_folder->remote,
                       cache_folder->summary);
    } else {
        int i;

        for (i = 0; i < cache_folder->summary->len; i++) {
            camel_message_info_free (
                cache_folder->summary->pdata[i]);
        }
        g_ptr_array_free (cache_folder->summary, TRUE);
        g_hash_table_destroy (cache_folder->summary_uids);
    }
}

static void
finalize (CamelObject *object)
{
    CamelCacheFolder *cache_folder = CAMEL_CACHE_FOLDER (object);

    if (cache_folder->uids) {
        camel_folder_free_uids (cache_folder->remote,
                    cache_folder->uids);
    }
    if (cache_folder->summary)
        cache_free_summary (cache_folder);

    if (cache_folder->uidmap)
        camel_cache_map_destroy (cache_folder->uidmap);

    camel_object_unref (CAMEL_OBJECT (cache_folder->local));
    camel_object_unref (CAMEL_OBJECT (cache_folder->remote));

    g_free (cache_folder->mapfile);
}


static void
update (CamelCacheFolder *cache_folder, CamelException *ex)
{
    if (cache_folder->uids) {
        camel_folder_free_uids (cache_folder->remote,
                    cache_folder->uids);
    }
    cache_folder->uids = camel_folder_get_uids (cache_folder->remote);

    if (cache_folder->summary)
        cache_free_summary (cache_folder);

    if (cache_folder->remote_summary) {
        cache_folder->summary =
            camel_folder_get_summary (cache_folder->remote);
    } else {
        CamelMessageInfo *mi;
        GPtrArray *lsummary;
        const char *ruid;
        int i;

        if (!cache_folder->is_synced) {
            camel_cache_folder_sync (cache_folder, ex);
            if (camel_exception_is_set (ex))
                return;
        }

        cache_folder->summary = g_ptr_array_new ();
        cache_folder->summary_uids = g_hash_table_new (g_str_hash,
                                   g_str_equal);

        lsummary = camel_folder_get_summary (cache_folder->local);

        /* For each local message, duplicate its info, replace
         * the uid with the remote one, and add it to the
         * uid->info cache.
         */
        for (i = 0; i < lsummary->len; i++) {
            mi = lsummary->pdata[i];
            ruid = camel_cache_map_get_remote (cache_folder->uidmap, mi->uid);
            if (!ruid) {
                /* Stale message. Delete it from cache. */
                camel_folder_delete_message (
                    cache_folder->local, mi->uid);
                continue;
            }

            mi = g_new (CamelMessageInfo, 1);
            camel_message_info_dup_to (lsummary->pdata[i], mi);
            g_free (mi->uid);
            mi->uid = g_strdup (ruid);
            g_hash_table_insert (cache_folder->summary_uids,
                         mi->uid, mi);
        }
        camel_folder_free_summary (cache_folder->local, lsummary);

        /* Now build the summary array in remote UID order. */
        for (i = 0; i < cache_folder->uids->len; i++) {
            mi = g_hash_table_lookup (cache_folder->summary_uids,
                          cache_folder->uids->pdata[i]);
            g_ptr_array_add (cache_folder->summary, mi);
        }
    }
    
}

static void
init (CamelFolder *folder, CamelStore *parent_store,
      CamelFolder *parent_folder, const gchar *name, gchar *separator,
      gboolean path_begins_with_sep, CamelException *ex)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;
    char *path;

    CF_CLASS (folder)->init (folder, parent_store, parent_folder,
                 name, separator, path_begins_with_sep, ex);
    if (camel_exception_is_set (ex))
        return;

    folder->permanent_flags =
        camel_folder_get_permanent_flags (cache_folder->local);
    folder->can_hold_folders = cache_folder->remote->can_hold_folders;
    folder->can_hold_messages = cache_folder->remote->can_hold_messages;
    folder->has_summary_capability = TRUE;
    folder->has_search_capability =
        camel_folder_has_search_capability (cache_folder->local) ||
        camel_folder_has_search_capability (cache_folder->remote);

    cache_folder->remote_summary =
        camel_folder_has_summary_capability (cache_folder->remote);

    /* Load UIDs, summary, etc. */
    path = CAMEL_SERVICE (cache_folder->local->parent_store)->url->path;
    cache_folder->mapfile = g_strdup_printf ("%s/%s.map", path, name);
    cache_folder->uidmap = camel_cache_map_new ();
    camel_cache_map_read (cache_folder->uidmap, cache_folder->mapfile, ex);
    if (camel_exception_is_set (ex))
        return;
    update (cache_folder, ex);

    return;
}

/* If the remote folder changes, cache the new messages if necessary,
 * update the summary, and propagate the signal.
 */
static void
remote_folder_changed (CamelObject *remote_folder, gpointer type,
               gpointer user_data)
{
    CamelCacheFolder *cache_folder = user_data;

    update (cache_folder, NULL);
    camel_object_trigger_event (CAMEL_OBJECT (cache_folder),
                    "folder_changed", type);
}

/* If the local folder changes, it's because we just cached a message
 * or expunged messages. Look for new messages and update the UID maps.
 */
static void
local_folder_changed (CamelObject *local, gpointer type, gpointer data)
{
    CamelFolder *local_folder = CAMEL_FOLDER (local);
    CamelCacheFolder *cache_folder = data;
    CamelMimeMessage *msg;
    GPtrArray *new_luids;
    char *luid;
    const char *ruid;
    int i;

    /* Get the updated list of local UIDs. For any that we didn't
     * already know about, figure out the corresponding remote
     * UID.
     */
    new_luids = camel_folder_get_uids (local_folder);
    for (i = 0; i < new_luids->len; i++) {
        luid = new_luids->pdata[i];
        if (!camel_cache_map_get_remote (cache_folder->uidmap, luid)) {
            msg = camel_folder_get_message (local_folder,
                            luid, NULL);
            if (!msg)
                continue; /* Hrmph. */
            ruid = camel_medium_get_header (CAMEL_MEDIUM (msg),
                            "X-Evolution-Remote-UID");
            if (!ruid) {
                /* How'd that get here? */
                camel_folder_delete_message (local_folder,
                                 luid);
                continue;
            }

            camel_cache_map_update (cache_folder->uidmap,
                        luid, ruid);
        }
    }
    camel_folder_free_uids (local_folder, new_luids);

    /* FIXME: the uidmaps contain bad data now. */
}

/* DONE */
static void
refresh_info (CamelFolder *folder, CamelException *ex)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    camel_folder_refresh_info (cache_folder->remote, ex);
}

/* DONE */
static void
cache_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    camel_folder_sync (cache_folder->remote, expunge, ex);
    if (!camel_exception_is_set (ex)) {
        camel_folder_sync (cache_folder->local, expunge, NULL);
        camel_cache_map_write (cache_folder->uidmap,
                       cache_folder->mapfile, ex);
    }
}

/* DONE */
static void
expunge (CamelFolder *folder, CamelException *ex)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    camel_folder_expunge (cache_folder->remote, ex);
    if (!camel_exception_is_set (ex))
        camel_folder_expunge (cache_folder->local, NULL);
}

/* DONE */
static gint
get_message_count (CamelFolder *folder)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    return cache_folder->summary->len;
}

/* DONE */
static void
append_message (CamelFolder *folder, CamelMimeMessage *message, 
        const CamelMessageInfo *info, CamelException *ex)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    /* We'd like to cache this locally as well, but we have no
     * 100% reliable way to determine the UID assigned to the
     * remote message, so we can't.
     */
    camel_folder_append_message (cache_folder->remote, message, info, ex);
}

/* DONE */
static guint32
get_message_flags (CamelFolder *folder, const char *uid)
{
    const CamelMessageInfo *mi;

    mi = get_message_info (folder, uid);
    g_return_val_if_fail (mi != NULL, 0);
    return mi->flags;
}

/* DONE */
static void
set_message_flags (CamelFolder *folder, const char *uid,
           guint32 flags, guint32 set)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;
    const char *luid;

    luid = camel_cache_map_get_local (cache_folder->uidmap, uid);
    if (luid) {
        camel_folder_set_message_flags (cache_folder->local, luid,
                        flags, set);
    }
    camel_folder_set_message_flags (cache_folder->remote, uid, flags, set);
}

/* DONE */
static gboolean
get_message_user_flag (CamelFolder *folder, const char *uid, const char *name)
{
    const CamelMessageInfo *mi;

    mi = get_message_info (folder, uid);
    g_return_val_if_fail (mi != NULL, 0);
    return camel_flag_get ((CamelFlag **)&mi->user_flags, name);
}

/* DONE */
static void
set_message_user_flag (CamelFolder *folder, const char *uid,
               const char *name, gboolean value)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;
    const char *luid;

    luid = camel_cache_map_get_local (cache_folder->uidmap, uid);
    if (luid) {
        camel_folder_set_message_user_flag (cache_folder->local, luid,
                            name, value);
    }
    camel_folder_set_message_user_flag (cache_folder->remote, uid,
                        name, value);
}


/* DONE */
static const char *
get_message_user_tag (CamelFolder *folder, const char *uid, const char *name)
{
    const CamelMessageInfo *mi;

    mi = get_message_info (folder, uid);
    g_return_val_if_fail (mi != NULL, NULL);
    return camel_tag_get ((CamelTag **)&mi->user_tags, name);
}

/* DONE */
static void
set_message_user_tag (CamelFolder *folder, const char *uid,
              const char *name, const char *value)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;
    const char *luid;

    luid = camel_cache_map_get_local (cache_folder->uidmap, uid);
    if (luid) {
        camel_folder_set_message_user_tag (cache_folder->local, luid,
                           name, value);
    }
    camel_folder_set_message_user_tag (cache_folder->remote, uid,
                       name, value);
}


/* DONE */
static CamelMimeMessage *
get_message (CamelFolder *folder, const gchar *uid, CamelException *ex)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;
    CamelMimeMessage *msg;
    const CamelMessageInfo *info;
    const char *luid;

    /* Check if we have it cached first. */
    luid = camel_cache_map_get_local (cache_folder->uidmap, uid);
    if (luid) {
        msg = camel_folder_get_message (cache_folder->local,
                        luid, NULL);
        if (msg)
            return msg;

        /* Hm... Oh well. Update the map and try for real. */
        camel_cache_map_remove (cache_folder->uidmap, NULL, uid);
    }

    /* OK. It's not cached. Get the remote message. */
    msg = camel_folder_get_message (cache_folder->remote, uid, ex);
    if (!msg)
        return NULL;
    info = camel_folder_get_message_info (cache_folder->remote, uid);

    /* Add a header giving the remote UID and append it to the
     * local folder. (This should eventually invoke
     * local_folder_changed(), which will take care of updating
     * the uidmaps.)
     */
    camel_medium_add_header (CAMEL_MEDIUM (msg), "X-Evolution-Remote-UID",
                 uid);
    camel_folder_append_message (cache_folder->local, msg, info, NULL);

    return msg;
}

/* DONE */
static GPtrArray *
get_uids (CamelFolder *folder)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    return cache_folder->uids;
}

/* DONE */
static GPtrArray *
get_summary (CamelFolder *folder)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    return cache_folder->summary;
}

/* DONE */
static GPtrArray *
get_subfolder_names (CamelFolder *folder)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    return camel_folder_get_subfolder_names (cache_folder->remote);
}

/* DONE */
static void
free_subfolder_names (CamelFolder *folder, GPtrArray *subfolders)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    camel_folder_free_subfolder_names (cache_folder->remote, subfolders);
}

/* DONE */
static GPtrArray *
search_by_expression (CamelFolder *folder, const char *expression,
              CamelException *ex)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    /* Search on the remote folder if we're not synced. */
    if (!cache_folder->is_synced &&
        camel_folder_has_search_capability (cache_folder->remote)) {
        return camel_folder_search_by_expression (cache_folder->remote,
                              expression, ex);
    } else {
        GPtrArray *matches;
        const char *ruid;
        int i;

        if (!cache_folder->is_synced)
            camel_cache_folder_sync (cache_folder, ex);
        if (camel_exception_is_set (ex))
            return NULL;
        matches = search_by_expression (cache_folder->local,
                        expression, ex);
        if (camel_exception_is_set (ex))
            return NULL;

        /* Convert local uids to remote. */
        for (i = 0; i < matches->len; i++) {
            ruid = camel_cache_map_get_remote (cache_folder->uidmap,
                               matches->pdata[i]);
            g_free (matches->pdata[i]);
            matches->pdata[i] = g_strdup (ruid);
        }

        return matches;
    }
}

/* DONE */
static const CamelMessageInfo *
get_message_info (CamelFolder *folder, const char *uid)
{
    CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder;

    if (cache_folder->remote_summary) {
        return camel_folder_get_message_info (cache_folder->remote,
                              uid);
    } else
        return g_hash_table_lookup (cache_folder->summary_uids, uid);
}

/* DONE */
static void
copy_message_to (CamelFolder *source, const char *uid,
         CamelFolder *destination, CamelException *ex)
{
    CamelCacheFolder *source_cache_folder = (CamelCacheFolder *)source;
    CamelCacheFolder *dest_cache_folder = (CamelCacheFolder *)destination;

    /* If we are here, we know that the folders have the same parent
     * store, which implies their remote and local folders have the
     * same parent store as well.
     */

    if (CF_CLASS (source_cache_folder->remote)->copy_message_to !=
        folder_class->copy_message_to) {
        /* The remote store has a non-default copy method, so
         * use it to avoid unnecessary network traffic.
         */
        CF_CLASS (source_cache_folder->remote)->copy_message_to (
            source_cache_folder->remote, uid,
            dest_cache_folder->remote, ex);
    } else {
        /* The remote store uses the default copy method,
         * meaning if we proxy the copy_message_to over to it,
         * it will suck the message over the network. We may
         * already have a local copy, and if we don't, we want
         * to, and if we're going to have the message in
         * memory, then we should get it into the destination
         * cache too. So do this by hand.
         */
        CamelMimeMessage *msg;
        const CamelMessageInfo *info;

        msg = get_message (source, uid, ex);
        if (camel_exception_is_set (ex))
            return;
        info = camel_folder_get_message_info (source, uid);

        camel_medium_remove_header (CAMEL_MEDIUM (msg),
                        "X-Evolution-Remote-UID");
        append_message (destination, msg, info, ex);
    }
}

/* DONE */
static void
move_message_to (CamelFolder *source, const char *uid,
         CamelFolder *destination, CamelException *ex)
{
    CamelCacheFolder *source_cache_folder = (CamelCacheFolder *)source;
    CamelCacheFolder *dest_cache_folder = (CamelCacheFolder *)destination;

    /* See comments in copy_message_to. */

    if (CF_CLASS (source_cache_folder)->move_message_to !=
        folder_class->move_message_to) {
        CF_CLASS (source_cache_folder)->move_message_to (
            source_cache_folder->remote, uid,
            dest_cache_folder->remote, ex);
    } else {
        CamelMimeMessage *msg;
        const CamelMessageInfo *info;

        msg = get_message (source, uid, ex);
        if (camel_exception_is_set (ex))
            return;
        info = camel_folder_get_message_info (source, uid);

        camel_medium_remove_header (CAMEL_MEDIUM (msg),
                        "X-Evolution-Remote-UID");
        append_message (destination, msg, info, ex);
        if (!camel_exception_is_set (ex))
            camel_folder_delete_message (source, uid);
    }
}


CamelFolder *
camel_cache_folder_new (CamelStore *store, CamelFolder *parent,
            CamelFolder *remote, CamelFolder *local,
            CamelException *ex)
{
    CamelCacheFolder *cache_folder;
    CamelFolder *folder;

    cache_folder = CAMEL_CACHE_FOLDER (camel_object_new (CAMEL_CACHE_FOLDER_TYPE));
    folder = (CamelFolder *)cache_folder;

    cache_folder->local = local;
    camel_object_ref (CAMEL_OBJECT (local));
    camel_object_hook_event (CAMEL_OBJECT (local), "folder_changed",
                 local_folder_changed, cache_folder);

    cache_folder->remote = remote;
    camel_object_ref (CAMEL_OBJECT (remote));
    camel_object_hook_event (CAMEL_OBJECT (remote), "folder_changed",
                 remote_folder_changed, cache_folder);

    /* XXX */

    return folder;
}

void
camel_cache_folder_sync (CamelCacheFolder *cache_folder, CamelException *ex)
{
    CamelMimeMessage *msg;
    const char *ruid, *luid;
    int lsize, i;
    const CamelMessageInfo *info;

    lsize = camel_folder_get_message_count (cache_folder->local);

    camel_folder_freeze (cache_folder->local);
    for (i = 0; i < cache_folder->uids->len; i++) {
        ruid = cache_folder->uids->pdata[i];
        luid = camel_cache_map_get_local (cache_folder->uidmap, ruid);

        /* Don't re-copy messages we already have. */
        if (luid &&
            camel_folder_get_message_info (cache_folder->local, luid))
            continue;

        msg = camel_folder_get_message (cache_folder->remote,
                        ruid, ex);
        if (camel_exception_is_set (ex))
            return;
        info = camel_folder_get_message_info (cache_folder->remote,
                              ruid);

        camel_medium_add_header (CAMEL_MEDIUM (msg),
                     "X-Evolution-Remote-UID", ruid);
        camel_folder_append_message (cache_folder->local, msg,
                         info, ex);
        if (camel_exception_is_set (ex))
            return;
    }
    camel_folder_thaw (cache_folder->local);
}

static void
get_mappings (CamelCacheFolder *cache_folder, int first, CamelException *ex)
{
    GPtrArray *uids;
    CamelMimeMessage *msg;
    const char *ruid;
    int i;

    uids = camel_folder_get_uids (cache_folder->local);
    for (i = first; i < uids->len; i++) {
        msg = camel_folder_get_message (cache_folder->local,
                        uids->pdata[i], ex);
        if (!msg)
            break;
        ruid = camel_medium_get_header (CAMEL_MEDIUM (msg),
                        "X-Evolution-Remote-UID");

        camel_cache_map_add (cache_folder->uidmap,
                     uids->pdata[i], ruid);
    }
    camel_folder_free_uids (cache_folder->local, uids);
}