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