aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/imapp/camel-imapp-folder.c
blob: 967ab541f6d9b5997e57bbe87bb0d6527a91b550 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-imap-folder.c : class for a imap folder */

/* 
 * Authors: Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 2002 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 <errno.h>

#include "camel/camel-exception.h"
#include "camel/camel-stream-mem.h"
#include "camel/camel-stream-filter.h"
#include "camel/camel-mime-message.h"
#include "camel/camel-operation.h"
#include "camel/camel-data-cache.h"
#include "camel/camel-session.h"
#include "camel/camel-file-utils.h"

#include "camel-imapp-store.h"
#include "camel-imapp-folder.h"
#include "camel-imapp-summary.h"
#include "camel-imapp-exception.h"

#include <e-util/md5-utils.h>

#include <stdlib.h>
#include <string.h>

#define d(x)

#define CF_CLASS(o) (CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(o)))
static CamelFolderClass *parent_class;

static void imap_finalize (CamelObject *object);
static void imap_refresh_info (CamelFolder *folder, CamelException *ex);
static void imap_sync (CamelFolder *folder, gboolean expunge, CamelException *ex);
static CamelMimeMessage *imap_get_message (CamelFolder *folder, const char *uid, CamelException *ex);

static void
imap_folder_class_init (CamelIMAPPFolderClass *camel_imapp_folder_class)
{
    CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_imapp_folder_class);
    
    parent_class = CAMEL_FOLDER_CLASS(camel_folder_get_type());
    
    /* virtual method overload */
    camel_folder_class->refresh_info = imap_refresh_info;
    camel_folder_class->sync = imap_sync;
    
    camel_folder_class->get_message = imap_get_message;
    /*camel_folder_class->set_message_flags = imap_set_message_flags;*/
}

static void
imap_folder_init(CamelObject *o, CamelObjectClass *klass)
{
    CamelFolder *folder = (CamelFolder *)o;
    CamelIMAPPFolder *ifolder = (CamelIMAPPFolder *)o;

    folder->folder_flags |= (CAMEL_FOLDER_HAS_SUMMARY_CAPABILITY |
                 CAMEL_FOLDER_HAS_SEARCH_CAPABILITY);
    
    folder->permanent_flags = CAMEL_MESSAGE_ANSWERED |
        CAMEL_MESSAGE_DELETED | CAMEL_MESSAGE_DRAFT |
        CAMEL_MESSAGE_FLAGGED | CAMEL_MESSAGE_SEEN | CAMEL_MESSAGE_USER;

    /* FIXME: this is just a skeleton */

    ifolder->changes = camel_folder_change_info_new();
}

CamelType
camel_imapp_folder_get_type (void)
{
    static CamelType camel_imapp_folder_type = CAMEL_INVALID_TYPE;
    
    if (!camel_imapp_folder_type) {
        camel_imapp_folder_type = camel_type_register (CAMEL_FOLDER_TYPE, "CamelIMAPPFolder",
                                  sizeof (CamelIMAPPFolder),
                                  sizeof (CamelIMAPPFolderClass),
                                  (CamelObjectClassInitFunc) imap_folder_class_init,
                                  NULL,
                                  imap_folder_init,
                                  (CamelObjectFinalizeFunc) imap_finalize);
    }
    
    return camel_imapp_folder_type;
}

void
imap_finalize (CamelObject *object)
{
    CamelIMAPPFolder *folder = (CamelIMAPPFolder *)object;

    camel_folder_change_info_free(folder->changes);
}

CamelFolder *
camel_imapp_folder_new(CamelStore *store, const char *path)
{
    CamelFolder *folder;
    char *root;

    d(printf("opening imap folder '%s'\n", path));
    
    folder = CAMEL_FOLDER (camel_object_new (CAMEL_IMAPP_FOLDER_TYPE));
    camel_folder_construct(folder, store, path, path);

    ((CamelIMAPPFolder *)folder)->raw_name = g_strdup(path);

    folder->summary = camel_imapp_summary_new();

    root = camel_session_get_storage_path(((CamelService *)store)->session, (CamelService *)store, NULL);
    if (root) {
        char *base = g_build_filename(root, path, NULL);
        char *file = g_build_filename(base, ".ev-summary", NULL);

        camel_mkdir(base, 0777);
        g_free(base);

        camel_folder_summary_set_filename(folder->summary, file);
        printf("loading summary from '%s' (root=%s)\n", file, root);
        g_free(file);
        camel_folder_summary_load(folder->summary);
        g_free(root);
    }

    return folder;
}

void
camel_imapp_folder_open(CamelIMAPPFolder *folder, CamelException *ex)
{
    /* */
}

void
camel_imapp_folder_delete(CamelIMAPPFolder *folder, CamelException *ex)
{
}

void
camel_imapp_folder_rename(CamelIMAPPFolder *folder, const char *new, CamelException *ex)
{
}

void
camel_imapp_folder_close(CamelIMAPPFolder *folder, CamelException *ex)
{
}

static void 
imap_refresh_info (CamelFolder *folder, CamelException *ex)
{
    printf("imapp refresh info?\n");
}

static void
imap_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
{
    camel_imapp_driver_sync(((CamelIMAPPStore *)(folder->parent_store))->driver, expunge, (CamelIMAPPFolder *) folder);
}

static CamelMimeMessage *
imap_get_message (CamelFolder *folder, const char *uid, CamelException *ex)
{
    CamelMimeMessage * volatile msg = NULL;
    CamelStream * volatile stream = NULL;

    printf("get message '%s'\n", uid);

    CAMEL_TRY {
        /* simple implementation, just get whole message in 1 go */
        stream = camel_imapp_driver_fetch(((CamelIMAPPStore *)(folder->parent_store))->driver, (CamelIMAPPFolder *)folder, uid, "");
        camel_stream_reset(stream);
        msg = camel_mime_message_new();
        if (camel_data_wrapper_construct_from_stream((CamelDataWrapper *)msg, stream) != -1) {
            /* do we care? */
        }
    } CAMEL_CATCH(e) {
        if (msg)
            camel_object_unref(msg);
        msg = NULL;
        camel_exception_xfer(ex, e);
    } CAMEL_DONE;

    if (stream)
        camel_object_unref(stream);

    return msg;
}


/* Algorithm for selecting a folder:

  - If uidvalidity == old uidvalidity
    and exsists == old exists
    and recent == old recent
    and unseen == old unseen
    Assume our summary is correct
  for each summary item
    mark the summary item as 'old/not updated'
  rof
  fetch flags from 1:*
  for each fetch response
    info = summary[index]
    if info.uid != uid
      info = summary_by_uid[uid]
    fi
    if info == NULL
      create new info @ index
    fi
    if got.flags
      update flags
    fi
    if got.header
      update based on header
      mark as retrieved
    else if got.body
      update based on imap body
      mark as retrieved
    fi

  Async fetch response:
    info = summary[index]
    if info == null
       if uid == null
          force resync/select?
       info = empty @ index
    else if uid && info.uid != uid
       force a resync?
       return
    fi

    if got.flags {
      info.flags = flags
    }
    if got.header {
      info.init(header)
      info.empty = false
    }

info.state - 2 bit field in flags
   0 = empty, nothing set
   1 = uid & flags set
   2 = update required
   3 = up to date
*/