Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / providers / local / camel-maildir-folder.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
2  *
3  * Authors: Michael Zucchi <notzed@ximian.com>
4  *
5  * Copyright (C) 1999, 2003 Ximian Inc.
6  *
7  * This program is free software; you can redistribute it and/or 
8  * modify it under the terms of version 2 of the GNU Lesser General Public 
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19  * USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #include <glib/gi18n-lib.h>
36
37 #include "camel-data-wrapper.h"
38 #include "camel-exception.h"
39 #include "camel-mime-message.h"
40 #include "camel-stream-fs.h"
41
42 #include "camel-maildir-folder.h"
43 #include "camel-maildir-store.h"
44 #include "camel-maildir-summary.h"
45
46 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
47
48 static CamelLocalFolderClass *parent_class = NULL;
49
50 /* Returns the class for a CamelMaildirFolder */
51 #define CMAILDIRF_CLASS(so) CAMEL_MAILDIR_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
52 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
53 #define CMAILDIRS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
54
55 static CamelLocalSummary *maildir_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index);
56
57 static void maildir_append_message(CamelFolder * folder, CamelMimeMessage * message, const CamelMessageInfo *info, char **appended_uid, CamelException * ex);
58 static CamelMimeMessage *maildir_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex);
59
60 static void maildir_finalize(CamelObject * object);
61
62 static int
63 maildir_folder_getv(CamelObject *object, CamelException *ex, CamelArgGetV *args)
64 {
65         CamelFolder *folder = (CamelFolder *)object;
66         int i;
67         guint32 tag;
68
69         for (i=0;i<args->argc;i++) {
70                 CamelArgGet *arg = &args->argv[i];
71
72                 tag = arg->tag;
73
74                 switch (tag & CAMEL_ARG_TAG) {
75                 case CAMEL_FOLDER_ARG_NAME:
76                         if (!strcmp(folder->full_name, "."))
77                                 *arg->ca_str = _("Inbox");
78                         else
79                                 *arg->ca_str = folder->name;
80                         break;
81                 default:
82                         continue;
83                 }
84
85                 arg->tag = (tag & CAMEL_ARG_TYPE) | CAMEL_ARG_IGNORE;
86         }
87
88         return ((CamelObjectClass *)parent_class)->getv(object, ex, args);
89 }
90
91 static void camel_maildir_folder_class_init(CamelObjectClass * camel_maildir_folder_class)
92 {
93         CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_maildir_folder_class);
94         CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_maildir_folder_class;
95
96         parent_class = CAMEL_LOCAL_FOLDER_CLASS (camel_type_get_global_classfuncs(camel_local_folder_get_type()));
97
98         /* virtual method definition */
99
100         /* virtual method overload */
101         ((CamelObjectClass *)camel_folder_class)->getv = maildir_folder_getv;
102
103         camel_folder_class->append_message = maildir_append_message;
104         camel_folder_class->get_message = maildir_get_message;
105
106         lclass->create_summary = maildir_create_summary;
107 }
108
109 static void maildir_init(gpointer object, gpointer klass)
110 {
111         /*CamelFolder *folder = object;
112           CamelMaildirFolder *maildir_folder = object;*/
113 }
114
115 static void maildir_finalize(CamelObject * object)
116 {
117         /*CamelMaildirFolder *maildir_folder = CAMEL_MAILDIR_FOLDER(object);*/
118 }
119
120 CamelType camel_maildir_folder_get_type(void)
121 {
122         static CamelType camel_maildir_folder_type = CAMEL_INVALID_TYPE;
123
124         if (camel_maildir_folder_type == CAMEL_INVALID_TYPE) {
125                 camel_maildir_folder_type = camel_type_register(CAMEL_LOCAL_FOLDER_TYPE, "CamelMaildirFolder",
126                                                            sizeof(CamelMaildirFolder),
127                                                            sizeof(CamelMaildirFolderClass),
128                                                            (CamelObjectClassInitFunc) camel_maildir_folder_class_init,
129                                                            NULL,
130                                                            (CamelObjectInitFunc) maildir_init,
131                                                            (CamelObjectFinalizeFunc) maildir_finalize);
132         }
133  
134         return camel_maildir_folder_type;
135 }
136
137 CamelFolder *
138 camel_maildir_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
139 {
140         CamelFolder *folder;
141
142         d(printf("Creating maildir folder: %s\n", full_name));
143
144         folder = (CamelFolder *)camel_object_new(CAMEL_MAILDIR_FOLDER_TYPE);
145
146         if (parent_store->flags & CAMEL_STORE_FILTER_INBOX
147             && strcmp(full_name, ".") == 0)
148                 folder->folder_flags |= CAMEL_FOLDER_FILTER_RECENT;
149
150         folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
151                                                              parent_store, full_name, flags, ex);
152
153         return folder;
154 }
155
156 static CamelLocalSummary *maildir_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index)
157 {
158         return (CamelLocalSummary *)camel_maildir_summary_new((CamelFolder *)lf, path, folder, index);
159 }
160
161 static void
162 maildir_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
163 {
164         CamelMaildirFolder *maildir_folder = (CamelMaildirFolder *)folder;
165         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
166         CamelStream *output_stream;
167         CamelMessageInfo *mi;
168         CamelMaildirMessageInfo *mdi;
169         char *name, *dest = NULL;
170         
171         d(printf("Appending message\n"));
172
173         /* add it to the summary/assign the uid, etc */
174         mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
175         if (camel_exception_is_set (ex))
176                 return;
177         
178         mdi = (CamelMaildirMessageInfo *)mi;
179
180         d(printf("Appending message: uid is %s filename is %s\n", camel_message_info_uid(mi), mdi->filename));
181
182         /* write it out to tmp, use the uid we got from the summary */
183         name = g_strdup_printf ("%s/tmp/%s", lf->folder_path, camel_message_info_uid(mi));
184         output_stream = camel_stream_fs_new_with_name (name, O_WRONLY|O_CREAT, 0600);
185         if (output_stream == NULL)
186                 goto fail_write;
187         
188         if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *)message, output_stream) == -1
189             || camel_stream_close (output_stream) == -1)
190                 goto fail_write;
191         
192         /* now move from tmp to cur (bypass new, does it matter?) */
193         dest = g_strdup_printf("%s/cur/%s", lf->folder_path, camel_maildir_info_filename (mdi));
194         if (rename (name, dest) == -1)
195                 goto fail_write;
196
197         g_free (dest);
198         g_free (name);
199         
200         camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed",
201                                     ((CamelLocalFolder *)maildir_folder)->changes);
202         camel_folder_change_info_clear (((CamelLocalFolder *)maildir_folder)->changes);
203         
204         if (appended_uid)
205                 *appended_uid = g_strdup(camel_message_info_uid(mi));
206
207         return;
208         
209  fail_write:
210         
211         /* remove the summary info so we are not out-of-sync with the mh folder */
212         camel_folder_summary_remove_uid (CAMEL_FOLDER_SUMMARY (folder->summary),
213                                          camel_message_info_uid (mi));
214         
215         if (errno == EINTR)
216                 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
217                                      _("Maildir append message canceled"));
218         else
219                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
220                                       _("Cannot append message to maildir folder: %s: %s"),
221                                       name, g_strerror (errno));
222         
223         if (output_stream) {
224                 camel_object_unref (CAMEL_OBJECT (output_stream));
225                 unlink (name);
226         }
227         
228         g_free (name);
229         g_free (dest);
230 }
231
232 static CamelMimeMessage *maildir_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex)
233 {
234         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
235         CamelStream *message_stream = NULL;
236         CamelMimeMessage *message = NULL;
237         CamelMessageInfo *info;
238         char *name;
239         CamelMaildirMessageInfo *mdi;
240
241         d(printf("getting message: %s\n", uid));
242
243         /* get the message summary info */
244         if ((info = camel_folder_summary_uid(folder->summary, uid)) == NULL) {
245                 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
246                                      _("Cannot get message: %s from folder %s\n  %s"),
247                                      uid, lf->folder_path, _("No such message"));
248                 return NULL;
249         }
250
251         mdi = (CamelMaildirMessageInfo *)info;
252
253         /* what do we do if the message flags (and :info data) changes?  filename mismatch - need to recheck I guess */
254         name = g_strdup_printf("%s/cur/%s", lf->folder_path, camel_maildir_info_filename(mdi));
255
256         camel_message_info_free(info);
257
258         if ((message_stream = camel_stream_fs_new_with_name(name, O_RDONLY, 0)) == NULL) {
259                 camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
260                                      _("Cannot get message: %s from folder %s\n  %s"),
261                                      uid, lf->folder_path, g_strerror(errno));
262                 g_free(name);
263                 return NULL;
264         }
265
266         message = camel_mime_message_new();
267         if (camel_data_wrapper_construct_from_stream((CamelDataWrapper *)message, message_stream) == -1) {
268                 camel_exception_setv(ex, (errno==EINTR)?CAMEL_EXCEPTION_USER_CANCEL:CAMEL_EXCEPTION_SYSTEM,
269                                      _("Cannot get message: %s from folder %s\n  %s"),
270                                      uid, lf->folder_path, _("Invalid message contents"));
271                 g_free(name);
272                 camel_object_unref((CamelObject *)message_stream);
273                 camel_object_unref((CamelObject *)message);
274                 return NULL;
275
276         }
277         camel_object_unref((CamelObject *)message_stream);
278         g_free(name);
279
280         return message;
281 }