Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / providers / local / camel-mh-folder.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
2  *
3  * Authors: Michael Zucchi <notzed@ximian.com>
4  *
5  * Copyright (C) 1999, 2003 Ximian Inc.
6  *
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.
10  *
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.
15  *
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
19  * USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #include <glib/gi18n-lib.h>
36
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"
44
45 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
46
47 static CamelLocalFolderClass *parent_class = NULL;
48
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))
53
54 static CamelLocalSummary *mh_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index);
55
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);
58
59 static void mh_finalize(CamelObject * object);
60
61 static void camel_mh_folder_class_init(CamelObjectClass * camel_mh_folder_class)
62 {
63         CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mh_folder_class);
64         CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_mh_folder_class;
65
66         parent_class = CAMEL_LOCAL_FOLDER_CLASS (camel_type_get_global_classfuncs(camel_local_folder_get_type()));
67
68         /* virtual method definition */
69
70         /* virtual method overload */
71         camel_folder_class->append_message = mh_append_message;
72         camel_folder_class->get_message = mh_get_message;
73
74         lclass->create_summary = mh_create_summary;
75 }
76
77 static void mh_init(gpointer object, gpointer klass)
78 {
79         /*CamelFolder *folder = object;
80           CamelMhFolder *mh_folder = object;*/
81 }
82
83 static void mh_finalize(CamelObject * object)
84 {
85         /*CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(object);*/
86 }
87
88 CamelType camel_mh_folder_get_type(void)
89 {
90         static CamelType camel_mh_folder_type = CAMEL_INVALID_TYPE;
91
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,
97                                                            NULL,
98                                                            (CamelObjectInitFunc) mh_init,
99                                                            (CamelObjectFinalizeFunc) mh_finalize);
100         }
101
102         return camel_mh_folder_type;
103 }
104
105 CamelFolder *
106 camel_mh_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
107 {
108         CamelFolder *folder;
109
110         d(printf("Creating mh folder: %s\n", full_name));
111
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);
115
116         return folder;
117 }
118
119 static CamelLocalSummary *mh_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index)
120 {
121         return (CamelLocalSummary *)camel_mh_summary_new((CamelFolder *)lf, path, folder, index);
122 }
123
124 static void
125 mh_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
126 {
127         CamelMhFolder *mh_folder = (CamelMhFolder *)folder;
128         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
129         CamelStream *output_stream;
130         CamelMessageInfo *mi;
131         char *name;
132
133         /* FIXME: probably needs additional locking (although mh doesn't appear do do it) */
134
135         d(printf("Appending message\n"));
136
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))
140                 return;
141         
142         d(printf("Appending message: uid is %s\n", camel_message_info_uid(mi)));
143         
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)
148                 goto fail_write;
149         
150         if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *)message, output_stream) == -1
151             || camel_stream_close (output_stream) == -1)
152                 goto fail_write;
153         
154         /* close this? */
155         camel_object_unref (CAMEL_OBJECT (output_stream));
156
157         g_free(name);
158         
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);
162         
163         if (appended_uid)
164                 *appended_uid = g_strdup(camel_message_info_uid(mi));
165
166         return;
167         
168  fail_write:
169         
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));
173         
174         if (errno == EINTR)
175                 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
176                                      _("MH append message canceled"));
177         else
178                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
179                                       _("Cannot append message to mh folder: %s: %s"),
180                                       name, g_strerror (errno));
181         
182         if (output_stream) {
183                 camel_object_unref (CAMEL_OBJECT (output_stream));
184                 unlink (name);
185         }
186         
187         g_free (name);
188 }
189
190 static CamelMimeMessage *mh_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex)
191 {
192         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
193         CamelStream *message_stream = NULL;
194         CamelMimeMessage *message = NULL;
195         CamelMessageInfo *info;
196         char *name;
197
198         d(printf("getting message: %s\n", uid));
199
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"));
205                 return NULL;
206         }
207
208         /* we only need it to check the message exists */
209         camel_message_info_free(info);
210
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,
215                                       g_strerror (errno));
216                 g_free(name);
217                 return NULL;
218         }
219
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."));
225                 g_free(name);
226                 camel_object_unref((CamelObject *)message_stream);
227                 camel_object_unref((CamelObject *)message);
228                 return NULL;
229
230         }
231         camel_object_unref((CamelObject *)message_stream);
232         g_free(name);
233
234         return message;
235 }