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 Lesser 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 Lesser General Public License for more details.
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
33 #include <sys/types.h>
35 #include <glib/gi18n-lib.h>
37 #include "camel-data-wrapper.h"
38 #include "camel-exception.h"
39 #include "camel-mime-message.h"
40 #include "camel-stream-fs.h"
42 #include "camel-maildir-folder.h"
43 #include "camel-maildir-store.h"
44 #include "camel-maildir-summary.h"
46 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
48 static CamelLocalFolderClass *parent_class = NULL;
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))
55 static CamelLocalSummary *maildir_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index);
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);
60 static void maildir_finalize(CamelObject * object);
63 maildir_folder_getv(CamelObject *object, CamelException *ex, CamelArgGetV *args)
65 CamelFolder *folder = (CamelFolder *)object;
69 for (i=0;i<args->argc;i++) {
70 CamelArgGet *arg = &args->argv[i];
74 switch (tag & CAMEL_ARG_TAG) {
75 case CAMEL_FOLDER_ARG_NAME:
76 if (!strcmp(folder->full_name, "."))
77 *arg->ca_str = _("Inbox");
79 *arg->ca_str = folder->name;
85 arg->tag = (tag & CAMEL_ARG_TYPE) | CAMEL_ARG_IGNORE;
88 return ((CamelObjectClass *)parent_class)->getv(object, ex, args);
91 static void camel_maildir_folder_class_init(CamelObjectClass * camel_maildir_folder_class)
93 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_maildir_folder_class);
94 CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_maildir_folder_class;
96 parent_class = CAMEL_LOCAL_FOLDER_CLASS (camel_type_get_global_classfuncs(camel_local_folder_get_type()));
98 /* virtual method definition */
100 /* virtual method overload */
101 ((CamelObjectClass *)camel_folder_class)->getv = maildir_folder_getv;
103 camel_folder_class->append_message = maildir_append_message;
104 camel_folder_class->get_message = maildir_get_message;
106 lclass->create_summary = maildir_create_summary;
109 static void maildir_init(gpointer object, gpointer klass)
111 /*CamelFolder *folder = object;
112 CamelMaildirFolder *maildir_folder = object;*/
115 static void maildir_finalize(CamelObject * object)
117 /*CamelMaildirFolder *maildir_folder = CAMEL_MAILDIR_FOLDER(object);*/
120 CamelType camel_maildir_folder_get_type(void)
122 static CamelType camel_maildir_folder_type = CAMEL_INVALID_TYPE;
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,
130 (CamelObjectInitFunc) maildir_init,
131 (CamelObjectFinalizeFunc) maildir_finalize);
134 return camel_maildir_folder_type;
138 camel_maildir_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
142 d(printf("Creating maildir folder: %s\n", full_name));
144 folder = (CamelFolder *)camel_object_new(CAMEL_MAILDIR_FOLDER_TYPE);
146 if (parent_store->flags & CAMEL_STORE_FILTER_INBOX
147 && strcmp(full_name, ".") == 0)
148 folder->folder_flags |= CAMEL_FOLDER_FILTER_RECENT;
150 folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
151 parent_store, full_name, flags, ex);
156 static CamelLocalSummary *maildir_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index)
158 return (CamelLocalSummary *)camel_maildir_summary_new((CamelFolder *)lf, path, folder, index);
162 maildir_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
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;
171 d(printf("Appending message\n"));
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))
178 mdi = (CamelMaildirMessageInfo *)mi;
180 d(printf("Appending message: uid is %s filename is %s\n", camel_message_info_uid(mi), mdi->filename));
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)
188 if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *)message, output_stream) == -1
189 || camel_stream_close (output_stream) == -1)
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)
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);
205 *appended_uid = g_strdup(camel_message_info_uid(mi));
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));
216 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
217 _("Maildir append message canceled"));
219 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
220 _("Cannot append message to maildir folder: %s: %s"),
221 name, g_strerror (errno));
224 camel_object_unref (CAMEL_OBJECT (output_stream));
232 static CamelMimeMessage *maildir_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex)
234 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
235 CamelStream *message_stream = NULL;
236 CamelMimeMessage *message = NULL;
237 CamelMessageInfo *info;
239 CamelMaildirMessageInfo *mdi;
241 d(printf("getting message: %s\n", uid));
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"));
251 mdi = (CamelMaildirMessageInfo *)info;
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));
256 camel_message_info_free(info);
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));
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"));
272 camel_object_unref((CamelObject *)message_stream);
273 camel_object_unref((CamelObject *)message);
277 camel_object_unref((CamelObject *)message_stream);