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, 2003 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 "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"
43 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
45 static CamelLocalFolderClass *parent_class = NULL;
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))
52 static CamelLocalSummary *maildir_create_summary(const char *path, const char *folder, CamelIndex *index);
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);
57 static void maildir_finalize(CamelObject * object);
59 static void camel_maildir_folder_class_init(CamelObjectClass * camel_maildir_folder_class)
61 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_maildir_folder_class);
62 CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_maildir_folder_class;
64 parent_class = CAMEL_LOCAL_FOLDER_CLASS (camel_type_get_global_classfuncs(camel_local_folder_get_type()));
66 /* virtual method definition */
68 /* virtual method overload */
69 camel_folder_class->append_message = maildir_append_message;
70 camel_folder_class->get_message = maildir_get_message;
72 lclass->create_summary = maildir_create_summary;
75 static void maildir_init(gpointer object, gpointer klass)
77 /*CamelFolder *folder = object;
78 CamelMaildirFolder *maildir_folder = object;*/
81 static void maildir_finalize(CamelObject * object)
83 /*CamelMaildirFolder *maildir_folder = CAMEL_MAILDIR_FOLDER(object);*/
86 CamelType camel_maildir_folder_get_type(void)
88 static CamelType camel_maildir_folder_type = CAMEL_INVALID_TYPE;
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,
96 (CamelObjectInitFunc) maildir_init,
97 (CamelObjectFinalizeFunc) maildir_finalize);
100 return camel_maildir_folder_type;
104 camel_maildir_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
108 d(printf("Creating maildir folder: %s\n", full_name));
110 folder = (CamelFolder *)camel_object_new(CAMEL_MAILDIR_FOLDER_TYPE);
112 if (parent_store->flags & CAMEL_STORE_FILTER_INBOX
113 && strcmp(full_name, ".") == 0)
114 folder->folder_flags |= CAMEL_FOLDER_FILTER_RECENT;
116 folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
117 parent_store, full_name, flags, ex);
122 static CamelLocalSummary *maildir_create_summary(const char *path, const char *folder, CamelIndex *index)
124 return (CamelLocalSummary *)camel_maildir_summary_new(path, folder, index);
128 maildir_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
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;
137 d(printf("Appending message\n"));
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))
144 mdi = (CamelMaildirMessageInfo *)mi;
146 d(printf("Appending message: uid is %s filename is %s\n", camel_message_info_uid(mi), mdi->filename));
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)
154 if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *)message, output_stream) == -1
155 || camel_stream_close (output_stream) == -1)
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)
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);
171 *appended_uid = g_strdup(camel_message_info_uid(mi));
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));
182 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
183 _("Maildir append message cancelled"));
185 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
186 _("Cannot append message to maildir folder: %s: %s"),
187 name, g_strerror (errno));
190 camel_object_unref (CAMEL_OBJECT (output_stream));
198 static CamelMimeMessage *maildir_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex)
200 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
201 CamelStream *message_stream = NULL;
202 CamelMimeMessage *message = NULL;
203 CamelMessageInfo *info;
205 CamelMaildirMessageInfo *mdi;
207 d(printf("getting message: %s\n", uid));
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"));
215 mdi = (CamelMaildirMessageInfo *)info;
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));
220 camel_folder_summary_info_free(folder->summary, info);
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));
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"));
236 camel_object_unref((CamelObject *)message_stream);
237 camel_object_unref((CamelObject *)message);
241 camel_object_unref((CamelObject *)message_stream);