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-mh-folder.h"
40 #include "camel-mh-store.h"
41 #include "camel-mh-summary.h"
42 #include "camel-mime-message.h"
43 #include "camel-stream-fs.h"
45 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
47 static CamelLocalFolderClass *parent_class = NULL;
49 /* Returns the class for a CamelMhFolder */
50 #define CMHF_CLASS(so) CAMEL_MH_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
51 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
52 #define CMHS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
54 static CamelLocalSummary *mh_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index);
56 static void mh_append_message(CamelFolder * folder, CamelMimeMessage * message, const CamelMessageInfo *info, char **appended_uid, CamelException * ex);
57 static CamelMimeMessage *mh_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex);
59 static void mh_finalize(CamelObject * object);
61 static void camel_mh_folder_class_init(CamelObjectClass * camel_mh_folder_class)
63 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mh_folder_class);
64 CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_mh_folder_class;
66 parent_class = CAMEL_LOCAL_FOLDER_CLASS (camel_type_get_global_classfuncs(camel_local_folder_get_type()));
68 /* virtual method definition */
70 /* virtual method overload */
71 camel_folder_class->append_message = mh_append_message;
72 camel_folder_class->get_message = mh_get_message;
74 lclass->create_summary = mh_create_summary;
77 static void mh_init(gpointer object, gpointer klass)
79 /*CamelFolder *folder = object;
80 CamelMhFolder *mh_folder = object;*/
83 static void mh_finalize(CamelObject * object)
85 /*CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(object);*/
88 CamelType camel_mh_folder_get_type(void)
90 static CamelType camel_mh_folder_type = CAMEL_INVALID_TYPE;
92 if (camel_mh_folder_type == CAMEL_INVALID_TYPE) {
93 camel_mh_folder_type = camel_type_register(CAMEL_LOCAL_FOLDER_TYPE, "CamelMhFolder",
94 sizeof(CamelMhFolder),
95 sizeof(CamelMhFolderClass),
96 (CamelObjectClassInitFunc) camel_mh_folder_class_init,
98 (CamelObjectInitFunc) mh_init,
99 (CamelObjectFinalizeFunc) mh_finalize);
102 return camel_mh_folder_type;
106 camel_mh_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
110 d(printf("Creating mh folder: %s\n", full_name));
112 folder = (CamelFolder *)camel_object_new(CAMEL_MH_FOLDER_TYPE);
113 folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
114 parent_store, full_name, flags, ex);
119 static CamelLocalSummary *mh_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index)
121 return (CamelLocalSummary *)camel_mh_summary_new((CamelFolder *)lf, path, folder, index);
125 mh_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
127 CamelMhFolder *mh_folder = (CamelMhFolder *)folder;
128 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
129 CamelStream *output_stream;
130 CamelMessageInfo *mi;
133 /* FIXME: probably needs additional locking (although mh doesn't appear do do it) */
135 d(printf("Appending message\n"));
137 /* add it to the summary/assign the uid, etc */
138 mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
139 if (camel_exception_is_set (ex))
142 d(printf("Appending message: uid is %s\n", camel_message_info_uid(mi)));
144 /* write it out, use the uid we got from the summary */
145 name = g_strdup_printf("%s/%s", lf->folder_path, camel_message_info_uid(mi));
146 output_stream = camel_stream_fs_new_with_name(name, O_WRONLY|O_CREAT, 0600);
147 if (output_stream == NULL)
150 if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *)message, output_stream) == -1
151 || camel_stream_close (output_stream) == -1)
155 camel_object_unref (CAMEL_OBJECT (output_stream));
159 camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed",
160 ((CamelLocalFolder *)mh_folder)->changes);
161 camel_folder_change_info_clear (((CamelLocalFolder *)mh_folder)->changes);
164 *appended_uid = g_strdup(camel_message_info_uid(mi));
170 /* remove the summary info so we are not out-of-sync with the mh folder */
171 camel_folder_summary_remove_uid (CAMEL_FOLDER_SUMMARY (folder->summary),
172 camel_message_info_uid (mi));
175 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
176 _("MH append message canceled"));
178 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
179 _("Cannot append message to mh folder: %s: %s"),
180 name, g_strerror (errno));
183 camel_object_unref (CAMEL_OBJECT (output_stream));
190 static CamelMimeMessage *mh_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex)
192 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
193 CamelStream *message_stream = NULL;
194 CamelMimeMessage *message = NULL;
195 CamelMessageInfo *info;
198 d(printf("getting message: %s\n", uid));
200 /* get the message summary info */
201 if ((info = camel_folder_summary_uid(folder->summary, uid)) == NULL) {
202 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
203 _("Cannot get message: %s from folder %s\n %s"), uid, lf->folder_path,
204 _("No such message"));
208 /* we only need it to check the message exists */
209 camel_message_info_free(info);
211 name = g_strdup_printf("%s/%s", lf->folder_path, uid);
212 if ((message_stream = camel_stream_fs_new_with_name(name, O_RDONLY, 0)) == NULL) {
213 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
214 _("Cannot get message: %s from folder %s\n %s"), name, lf->folder_path,
220 message = camel_mime_message_new();
221 if (camel_data_wrapper_construct_from_stream((CamelDataWrapper *)message, message_stream) == -1) {
222 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
223 _("Cannot get message: %s from folder %s\n %s"), name, lf->folder_path,
224 _("Message construction failed."));
226 camel_object_unref((CamelObject *)message_stream);
227 camel_object_unref((CamelObject *)message);
231 camel_object_unref((CamelObject *)message_stream);