1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
3 * Authors: Michael Zucchi <notzed@ximian.com>
5 * Copyright (C) 1999, 2000 Ximian Inc.
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.
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.
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
27 #include <sys/types.h>
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"
44 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
46 static CamelLocalFolderClass *parent_class = NULL;
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))
53 static CamelLocalSummary *maildir_create_summary(const char *path, const char *folder, CamelIndex *index);
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);
58 static void maildir_finalize(CamelObject * object);
60 static void camel_maildir_folder_class_init(CamelObjectClass * camel_maildir_folder_class)
62 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_maildir_folder_class);
63 CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_maildir_folder_class;
65 parent_class = CAMEL_LOCAL_FOLDER_CLASS (camel_type_get_global_classfuncs(camel_local_folder_get_type()));
67 /* virtual method definition */
69 /* virtual method overload */
70 camel_folder_class->append_message = maildir_append_message;
71 camel_folder_class->get_message = maildir_get_message;
73 lclass->create_summary = maildir_create_summary;
76 static void maildir_init(gpointer object, gpointer klass)
78 /*CamelFolder *folder = object;
79 CamelMaildirFolder *maildir_folder = object;*/
82 static void maildir_finalize(CamelObject * object)
84 /*CamelMaildirFolder *maildir_folder = CAMEL_MAILDIR_FOLDER(object);*/
87 CamelType camel_maildir_folder_get_type(void)
89 static CamelType camel_maildir_folder_type = CAMEL_INVALID_TYPE;
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,
97 (CamelObjectInitFunc) maildir_init,
98 (CamelObjectFinalizeFunc) maildir_finalize);
101 return camel_maildir_folder_type;
105 camel_maildir_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
109 d(printf("Creating maildir folder: %s\n", full_name));
111 folder = (CamelFolder *)camel_object_new(CAMEL_MAILDIR_FOLDER_TYPE);
113 if (parent_store->flags & CAMEL_STORE_FILTER_INBOX
114 && strcmp(full_name, ".") == 0)
115 folder->folder_flags |= CAMEL_FOLDER_FILTER_RECENT;
117 folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
118 parent_store, full_name, flags, ex);
123 static CamelLocalSummary *maildir_create_summary(const char *path, const char *folder, CamelIndex *index)
125 return (CamelLocalSummary *)camel_maildir_summary_new(path, folder, index);
129 maildir_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
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;
138 d(printf("Appending message\n"));
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))
145 mdi = (CamelMaildirMessageInfo *)mi;
147 d(printf("Appending message: uid is %s filename is %s\n", camel_message_info_uid(mi), mdi->filename));
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)
155 if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *)message, output_stream) == -1
156 || camel_stream_close (output_stream) == -1)
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)
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);
172 *appended_uid = g_strdup(camel_message_info_uid(mi));
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));
183 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
184 _("Maildir append message cancelled"));
186 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
187 _("Cannot append message to maildir folder: %s: %s"),
188 name, g_strerror (errno));
191 camel_object_unref (CAMEL_OBJECT (output_stream));
199 static CamelMimeMessage *maildir_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex)
201 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
202 CamelStream *message_stream = NULL;
203 CamelMimeMessage *message = NULL;
204 CamelMessageInfo *info;
206 CamelMaildirMessageInfo *mdi;
208 d(printf("getting message: %s\n", uid));
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"));
216 mdi = (CamelMaildirMessageInfo *)info;
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));
221 camel_folder_summary_info_free(folder->summary, info);
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));
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"));
237 camel_object_unref((CamelObject *)message_stream);
238 camel_object_unref((CamelObject *)message);
242 camel_object_unref((CamelObject *)message_stream);