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-mh-folder.h"
36 #include "camel-mh-store.h"
37 #include "string-utils.h"
38 #include "camel-stream-fs.h"
39 #include "camel-mh-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 CamelMhFolder */
49 #define CMHF_CLASS(so) CAMEL_MH_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
50 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
51 #define CMHS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
53 static CamelLocalSummary *mh_create_summary(const char *path, const char *folder, CamelIndex *index);
55 static void mh_append_message(CamelFolder * folder, CamelMimeMessage * message, const CamelMessageInfo *info, char **appended_uid, CamelException * ex);
56 static CamelMimeMessage *mh_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex);
58 static void mh_finalize(CamelObject * object);
60 static void camel_mh_folder_class_init(CamelObjectClass * camel_mh_folder_class)
62 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mh_folder_class);
63 CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_mh_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 = mh_append_message;
71 camel_folder_class->get_message = mh_get_message;
73 lclass->create_summary = mh_create_summary;
76 static void mh_init(gpointer object, gpointer klass)
78 /*CamelFolder *folder = object;
79 CamelMhFolder *mh_folder = object;*/
82 static void mh_finalize(CamelObject * object)
84 /*CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(object);*/
87 CamelType camel_mh_folder_get_type(void)
89 static CamelType camel_mh_folder_type = CAMEL_INVALID_TYPE;
91 if (camel_mh_folder_type == CAMEL_INVALID_TYPE) {
92 camel_mh_folder_type = camel_type_register(CAMEL_LOCAL_FOLDER_TYPE, "CamelMhFolder",
93 sizeof(CamelMhFolder),
94 sizeof(CamelMhFolderClass),
95 (CamelObjectClassInitFunc) camel_mh_folder_class_init,
97 (CamelObjectInitFunc) mh_init,
98 (CamelObjectFinalizeFunc) mh_finalize);
101 return camel_mh_folder_type;
105 camel_mh_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
109 d(printf("Creating mh folder: %s\n", full_name));
111 folder = (CamelFolder *)camel_object_new(CAMEL_MH_FOLDER_TYPE);
112 folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
113 parent_store, full_name, flags, ex);
118 static CamelLocalSummary *mh_create_summary(const char *path, const char *folder, CamelIndex *index)
120 return (CamelLocalSummary *)camel_mh_summary_new(path, folder, index);
124 mh_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
126 CamelMhFolder *mh_folder = (CamelMhFolder *)folder;
127 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
128 CamelStream *output_stream;
129 CamelMessageInfo *mi;
132 /* FIXME: probably needs additional locking (although mh doesn't appear do do it) */
134 d(printf("Appending message\n"));
136 /* add it to the summary/assign the uid, etc */
137 mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
138 if (camel_exception_is_set (ex))
141 d(printf("Appending message: uid is %s\n", camel_message_info_uid(mi)));
143 /* write it out, use the uid we got from the summary */
144 name = g_strdup_printf("%s/%s", lf->folder_path, camel_message_info_uid(mi));
145 output_stream = camel_stream_fs_new_with_name(name, O_WRONLY|O_CREAT, 0600);
146 if (output_stream == NULL)
149 if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *)message, output_stream) == -1
150 || camel_stream_close (output_stream) == -1)
154 camel_object_unref (CAMEL_OBJECT (output_stream));
158 camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed",
159 ((CamelLocalFolder *)mh_folder)->changes);
160 camel_folder_change_info_clear (((CamelLocalFolder *)mh_folder)->changes);
163 *appended_uid = g_strdup(camel_message_info_uid(mi));
169 /* remove the summary info so we are not out-of-sync with the mh folder */
170 camel_folder_summary_remove_uid (CAMEL_FOLDER_SUMMARY (folder->summary),
171 camel_message_info_uid (mi));
174 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
175 _("MH append message cancelled"));
177 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
178 _("Cannot append message to mh folder: %s: %s"),
179 name, g_strerror (errno));
182 camel_object_unref (CAMEL_OBJECT (output_stream));
189 static CamelMimeMessage *mh_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex)
191 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
192 CamelStream *message_stream = NULL;
193 CamelMimeMessage *message = NULL;
194 CamelMessageInfo *info;
197 d(printf("getting message: %s\n", uid));
199 /* get the message summary info */
200 if ((info = camel_folder_summary_uid(folder->summary, uid)) == NULL) {
201 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID, _("Cannot get message: %s\n %s"), uid, _("No such message"));
205 /* we only need it to check the message exists */
206 camel_folder_summary_info_free(folder->summary, info);
208 name = g_strdup_printf("%s/%s", lf->folder_path, uid);
209 if ((message_stream = camel_stream_fs_new_with_name(name, O_RDONLY, 0)) == NULL) {
210 camel_exception_setv (ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
211 _("Cannot get message: %s\n %s"),
212 name, g_strerror (errno));
217 message = camel_mime_message_new();
218 if (camel_data_wrapper_construct_from_stream((CamelDataWrapper *)message, message_stream) == -1) {
219 camel_exception_setv (ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
220 _("Cannot get message: %s\n %s"),
221 name, _("Invalid message contents"));
223 camel_object_unref((CamelObject *)message_stream);
224 camel_object_unref((CamelObject *)message);
228 camel_object_unref((CamelObject *)message_stream);