d3cbc490b54b36ed482333b650bac98487046d56
[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, 2000 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 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <fcntl.h>
34
35 #include "camel-maildir-folder.h"
36 #include "camel-maildir-store.h"
37 #include "string-utils.h"
38 #include "camel-stream-fs.h"
39 #include "camel-maildir-summary.h"
40 #include "camel-data-wrapper.h"
41 #include "camel-mime-message.h"
42 #include "camel-exception.h"
43
44 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
45
46 static CamelLocalFolderClass *parent_class = NULL;
47
48 /* Returns the class for a CamelMaildirFolder */
49 #define CMAILDIRF_CLASS(so) CAMEL_MAILDIR_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
50 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
51 #define CMAILDIRS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
52
53 static CamelLocalSummary *maildir_create_summary(const char *path, const char *folder, CamelIndex *index);
54
55 static void maildir_append_message(CamelFolder * folder, CamelMimeMessage * message, const CamelMessageInfo *info, char **appended_uid, CamelException * ex);
56 static CamelMimeMessage *maildir_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex);
57
58 static void maildir_finalize(CamelObject * object);
59
60 static void camel_maildir_folder_class_init(CamelObjectClass * camel_maildir_folder_class)
61 {
62         CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_maildir_folder_class);
63         CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_maildir_folder_class;
64
65         parent_class = CAMEL_LOCAL_FOLDER_CLASS (camel_type_get_global_classfuncs(camel_local_folder_get_type()));
66
67         /* virtual method definition */
68
69         /* virtual method overload */
70         camel_folder_class->append_message = maildir_append_message;
71         camel_folder_class->get_message = maildir_get_message;
72
73         lclass->create_summary = maildir_create_summary;
74 }
75
76 static void maildir_init(gpointer object, gpointer klass)
77 {
78         /*CamelFolder *folder = object;
79           CamelMaildirFolder *maildir_folder = object;*/
80 }
81
82 static void maildir_finalize(CamelObject * object)
83 {
84         /*CamelMaildirFolder *maildir_folder = CAMEL_MAILDIR_FOLDER(object);*/
85 }
86
87 CamelType camel_maildir_folder_get_type(void)
88 {
89         static CamelType camel_maildir_folder_type = CAMEL_INVALID_TYPE;
90
91         if (camel_maildir_folder_type == CAMEL_INVALID_TYPE) {
92                 camel_maildir_folder_type = camel_type_register(CAMEL_LOCAL_FOLDER_TYPE, "CamelMaildirFolder",
93                                                            sizeof(CamelMaildirFolder),
94                                                            sizeof(CamelMaildirFolderClass),
95                                                            (CamelObjectClassInitFunc) camel_maildir_folder_class_init,
96                                                            NULL,
97                                                            (CamelObjectInitFunc) maildir_init,
98                                                            (CamelObjectFinalizeFunc) maildir_finalize);
99         }
100  
101         return camel_maildir_folder_type;
102 }
103
104 CamelFolder *
105 camel_maildir_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
106 {
107         CamelFolder *folder;
108
109         d(printf("Creating maildir folder: %s\n", full_name));
110
111         folder = (CamelFolder *)camel_object_new(CAMEL_MAILDIR_FOLDER_TYPE);
112
113         if (parent_store->flags & CAMEL_STORE_FILTER_INBOX
114             && strcmp(full_name, ".") == 0)
115                 folder->folder_flags |= CAMEL_FOLDER_FILTER_RECENT;
116
117         folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
118                                                              parent_store, full_name, flags, ex);
119
120         return folder;
121 }
122
123 static CamelLocalSummary *maildir_create_summary(const char *path, const char *folder, CamelIndex *index)
124 {
125         return (CamelLocalSummary *)camel_maildir_summary_new(path, folder, index);
126 }
127
128 static void
129 maildir_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
130 {
131         CamelMaildirFolder *maildir_folder = (CamelMaildirFolder *)folder;
132         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
133         CamelStream *output_stream;
134         CamelMessageInfo *mi;
135         CamelMaildirMessageInfo *mdi;
136         char *name, *dest = NULL;
137         
138         d(printf("Appending message\n"));
139
140         /* add it to the summary/assign the uid, etc */
141         mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
142         if (camel_exception_is_set (ex))
143                 return;
144         
145         mdi = (CamelMaildirMessageInfo *)mi;
146
147         d(printf("Appending message: uid is %s filename is %s\n", camel_message_info_uid(mi), mdi->filename));
148
149         /* write it out to tmp, use the uid we got from the summary */
150         name = g_strdup_printf ("%s/tmp/%s", lf->folder_path, camel_message_info_uid(mi));
151         output_stream = camel_stream_fs_new_with_name (name, O_WRONLY|O_CREAT, 0600);
152         if (output_stream == NULL)
153                 goto fail_write;
154         
155         if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *)message, output_stream) == -1
156             || camel_stream_close (output_stream) == -1)
157                 goto fail_write;
158         
159         /* now move from tmp to cur (bypass new, does it matter?) */
160         dest = g_strdup_printf("%s/cur/%s", lf->folder_path, camel_maildir_info_filename (mdi));
161         if (rename (name, dest) == 1)
162                 goto fail_write;
163
164         g_free (dest);
165         g_free (name);
166         
167         camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed",
168                                     ((CamelLocalFolder *)maildir_folder)->changes);
169         camel_folder_change_info_clear (((CamelLocalFolder *)maildir_folder)->changes);
170         
171         if (appended_uid)
172                 *appended_uid = g_strdup(camel_message_info_uid(mi));
173
174         return;
175         
176  fail_write:
177         
178         /* remove the summary info so we are not out-of-sync with the mh folder */
179         camel_folder_summary_remove_uid (CAMEL_FOLDER_SUMMARY (folder->summary),
180                                          camel_message_info_uid (mi));
181         
182         if (errno == EINTR)
183                 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
184                                      _("Maildir append message cancelled"));
185         else
186                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
187                                       _("Cannot append message to maildir folder: %s: %s"),
188                                       name, g_strerror (errno));
189         
190         if (output_stream) {
191                 camel_object_unref (CAMEL_OBJECT (output_stream));
192                 unlink (name);
193         }
194         
195         g_free (name);
196         g_free (dest);
197 }
198
199 static CamelMimeMessage *maildir_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex)
200 {
201         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
202         CamelStream *message_stream = NULL;
203         CamelMimeMessage *message = NULL;
204         CamelMessageInfo *info;
205         char *name;
206         CamelMaildirMessageInfo *mdi;
207
208         d(printf("getting message: %s\n", uid));
209
210         /* get the message summary info */
211         if ((info = camel_folder_summary_uid(folder->summary, uid)) == NULL) {
212                 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID, _("Cannot get message: %s\n  %s"), uid, _("No such message"));
213                 return NULL;
214         }
215
216         mdi = (CamelMaildirMessageInfo *)info;
217
218         /* what do we do if the message flags (and :info data) changes?  filename mismatch - need to recheck I guess */
219         name = g_strdup_printf("%s/cur/%s", lf->folder_path, camel_maildir_info_filename(mdi));
220
221         camel_folder_summary_info_free(folder->summary, info);
222
223         if ((message_stream = camel_stream_fs_new_with_name(name, O_RDONLY, 0)) == NULL) {
224                 camel_exception_setv (ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
225                                       _("Cannot get message: %s\n  %s"),
226                                       name, g_strerror (errno));
227                 g_free(name);
228                 return NULL;
229         }
230
231         message = camel_mime_message_new();
232         if (camel_data_wrapper_construct_from_stream((CamelDataWrapper *)message, message_stream) == -1) {
233                 camel_exception_setv(ex, (errno==EINTR)?CAMEL_EXCEPTION_USER_CANCEL:CAMEL_EXCEPTION_FOLDER_INVALID_UID,
234                                      _("Cannot get message: %s\n  %s"),
235                                      name, _("Invalid message contents"));
236                 g_free(name);
237                 camel_object_unref((CamelObject *)message_stream);
238                 camel_object_unref((CamelObject *)message);
239                 return NULL;
240
241         }
242         camel_object_unref((CamelObject *)message_stream);
243         g_free(name);
244
245         return message;
246 }