aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-vtrash.c
blob: 217a1f6eebe52a69e1001f9c0fe064a06d5db5d4 (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
/* -*- 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 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 Street #330, Boston, MA 02111-1307, USA.
 *
 */


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

#include "Evolution.h"
#include "evolution-storage.h"
#include "evolution-shell-component.h"

#include "mail.h"
#include "mail-vfolder.h"
#include "mail-vtrash.h"
#include "mail-tools.h"
#include "mail-mt.h"

#include "camel/camel.h"
#include "camel/providers/vee/camel-vee-store.h"
#include "filter/vfolder-rule.h"
#include "filter/filter-part.h"

#define d(x)

static GHashTable *vtrash_hash = NULL;

extern char *evolution_dir;
extern CamelSession *session;

/* maps the shell's uri to the real vtrash folder */
CamelFolder *
vtrash_uri_to_folder (const char *uri, CamelException *ex)
{
    CamelFolder *folder;
    
    if (!vtrash_hash)
        return NULL;
    
    if (strncmp (uri, "vtrash:", 7))
        return NULL;
    
    folder = g_hash_table_lookup (vtrash_hash, uri);
    
    camel_object_ref (CAMEL_OBJECT (folder));
    
    return folder;
}

static void
vtrash_add (CamelStore *store, CamelFolder *folder, const char *store_uri, const char *name)
{
    EvolutionStorage *storage;
    char *uri, *path;
    
    g_return_if_fail (CAMEL_IS_STORE (store));
    
    uri = g_strdup_printf ("vtrash:%s", store_uri);
    
    if (!vtrash_hash) {
        vtrash_hash = g_hash_table_new (g_str_hash, g_str_equal);
    } else if (g_hash_table_lookup (vtrash_hash, uri) != NULL) {
        g_free (uri);
        return;
    }
    
    if (!strcmp (store_uri, "file:/")) {
        storage = mail_vfolder_get_vfolder_storage ();
    } else {
        storage = mail_lookup_storage (store);
    }
    
    if (!storage) {
        g_free (uri);
        return;
    }
    
    path = g_strdup_printf ("/%s", name);
    evolution_storage_new_folder (storage, path, g_basename (path),
                      "mail", uri, name, FALSE);
    gtk_object_unref (GTK_OBJECT (storage));
    
    g_hash_table_insert (vtrash_hash, uri, folder);
    camel_object_ref (CAMEL_OBJECT (folder));
    
    g_free (path);
}

struct _get_trash_msg {
    struct _mail_msg msg;
    
    char *store_uri;
    CamelFolder *folder;
    void (*done) (char *store_uri, CamelFolder *folder, void *data);
    void *data;
};

static char *
get_trash_desc (struct _mail_msg *mm, int done)
{
    struct _get_trash_msg *m = (struct _get_trash_msg *)mm;
    
    return g_strdup_printf (_("Opening Trash folder for %s"), m->store_uri);
}

/* maps the shell's uri to the real vfolder uri and open the folder */
static CamelFolder *
create_trash_vfolder (const char *name, GPtrArray *urls, CamelException *ex)
{
    void camel_vee_folder_add_folder (CamelFolder *, CamelFolder *);
    
    char *storeuri, *foldername;
    CamelFolder *folder = NULL, *sourcefolder;
    int source = 0;
    
    d(fprintf (stderr, "Creating Trash vfolder\n"));
    
    storeuri = g_strdup_printf ("vfolder:%s/vfolder/%s", evolution_dir, name);
    foldername = g_strdup ("mbox?(match-all (system-flag \"Deleted\"))");
    
    /* we dont have indexing on vfolders */
    folder = mail_tool_get_folder_from_urlname (storeuri, foldername, CAMEL_STORE_FOLDER_CREATE|CAMEL_STORE_VEE_FOLDER_AUTO, ex);
    g_free (foldername);
    g_free (storeuri);
    if (camel_exception_is_set (ex))
        return NULL;
    
    while (source < urls->len) {
        const char *sourceuri;
        
        sourceuri = urls->pdata[source];
        d(fprintf (stderr, "adding vfolder source: %s\n", sourceuri));
        
        sourcefolder = mail_tool_uri_to_folder (sourceuri, ex);
        d(fprintf (stderr, "source folder = %p\n", sourcefolder));
        
        if (sourcefolder) {
            mail_tool_camel_lock_up ();
            camel_vee_folder_add_folder (folder, sourcefolder);
            mail_tool_camel_lock_down ();
        } else {
            /* we'll just silently ignore now-missing sources */
            camel_exception_clear (ex);
        }
        
        g_free (urls->pdata[source]);
        source++;
    }
    
    g_ptr_array_free (urls, TRUE);
    
    return folder;
}

static void
populate_folder_urls (CamelFolderInfo *info, GPtrArray *urls)
{
    if (!info)
        return;
    
    g_ptr_array_add (urls, g_strdup (info->url));
    
    if (info->child)
        populate_folder_urls (info->child, urls);
    
    if (info->sibling)
        populate_folder_urls (info->sibling, urls);
}

static void
get_trash_get (struct _mail_msg *mm)
{
    struct _get_trash_msg *m = (struct _get_trash_msg *)mm;
    CamelStore *store;
    GPtrArray *urls;
    
    urls = g_ptr_array_new ();
    
    /* we don't want to connect */
    store = (CamelStore *) camel_session_get_service (session, m->store_uri,
                              CAMEL_PROVIDER_STORE, &mm->ex);
    if (store == NULL) {
        g_warning ("Couldn't get service %s: %s\n", m->store_uri,
               camel_exception_get_description (&mm->ex));
        camel_exception_clear (&mm->ex);
        
        m->folder = NULL;
    } else {
        /* First try to see if we can save time by looking the folder up in the hash table */
        char *uri;
        
        uri = g_strdup_printf ("vtrash:%s", m->store_uri);
        m->folder = vtrash_uri_to_folder (uri, &mm->ex);
        g_free (uri);
        
        if (!m->folder) {
            /* Create and add this new vTrash folder */
            CamelFolderInfo *info;
            
            info = camel_store_get_folder_info (store, NULL, TRUE, TRUE, TRUE, &mm->ex);
            populate_folder_urls (info, urls);
            camel_store_free_folder_info (store, info);
            
            m->folder = create_trash_vfolder (_("vTrash"), urls, &mm->ex);
            vtrash_add (store, m->folder, m->store_uri, _("vTrash"));
        }
    }
}

static void
get_trash_got (struct _mail_msg *mm)
{
    struct _get_trash_msg *m = (struct _get_trash_msg *)mm;
    
    if (m->done)
        m->done (m->store_uri, m->folder, m->data);
}

static void
get_trash_free (struct _mail_msg *mm)
{
    struct _get_trash_msg *m = (struct _get_trash_msg *)mm;
    
    g_free (m->store_uri);
    if (m->folder)
        camel_object_unref (CAMEL_OBJECT (m->folder));
}

static struct _mail_msg_op get_trash_op = {
    get_trash_desc,
    get_trash_get,
    get_trash_got,
    get_trash_free,
};

int
vtrash_create (const char *store_uri,
           void (*done) (char *store_uri, CamelFolder *folder, void *data),
           void *data)
{
    struct _get_trash_msg *m;
    int id;
    
    m = mail_msg_new (&get_trash_op, NULL, sizeof (*m));
    m->store_uri = g_strdup (store_uri);
    m->data = data;
    m->done = done;
    
    id = m->msg.seq;
    e_thread_put (mail_thread_new, (EMsg *)m);
    
    return id;
}

static void
free_folder (gpointer key, gpointer value, gpointer data)
{
    CamelFolder *folder = CAMEL_FOLDER (value);
    char *uri = key;
    
    g_free (uri);
    camel_object_unref (CAMEL_OBJECT (folder));
}

void
vtrash_cleanup (void)
{
    if (!vtrash_hash)
        return;
    
    g_hash_table_foreach (vtrash_hash, free_folder, NULL);
    g_hash_table_destroy (vtrash_hash);
}