02d3370428656d985fe29e9f87f88a671bbccd16
[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, 2000 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 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 General Public License for more details.
15  *
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
19  * USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <fcntl.h>
34
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"
43
44 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
45
46 static CamelLocalFolderClass *parent_class = NULL;
47
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))
52
53 static CamelLocalSummary *mh_create_summary(const char *path, const char *folder, CamelIndex *index);
54
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);
57
58 static void mh_finalize(CamelObject * object);
59
60 static void camel_mh_folder_class_init(CamelObjectClass * camel_mh_folder_class)
61 {
62         CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mh_folder_class);
63         CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_mh_folder_class;
64
65         parent_class = CAMEL_LOCAL_FOLDER_CLASS (camel_type_get_global_classfuncs(camel_local_folder_get_type()));
66
67         /* virtual method definition */
68
69         /* virtual method overload */
70         camel_folder_class->append_message = mh_append_message;
71         camel_folder_class->get_message = mh_get_message;
72
73         lclass->create_summary = mh_create_summary;
74 }
75
76 static void mh_init(gpointer object, gpointer klass)
77 {
78         /*CamelFolder *folder = object;
79           CamelMhFolder *mh_folder = object;*/
80 }
81
82 static void mh_finalize(CamelObject * object)
83 {
84         /*CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(object);*/
85 }
86
87 CamelType camel_mh_folder_get_type(void)
88 {
89         static CamelType camel_mh_folder_type = CAMEL_INVALID_TYPE;
90
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,
96                                                            NULL,
97                                                            (CamelObjectInitFunc) mh_init,
98                                                            (CamelObjectFinalizeFunc) mh_finalize);
99         }
100
101         return camel_mh_folder_type;
102 }
103
104 CamelFolder *
105 camel_mh_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
106 {
107         CamelFolder *folder;
108
109         d(printf("Creating mh folder: %s\n", full_name));
110
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);
114
115         return folder;
116 }
117
118 static CamelLocalSummary *mh_create_summary(const char *path, const char *folder, CamelIndex *index)
119 {
120         return (CamelLocalSummary *)camel_mh_summary_new(path, folder, index);
121 }
122
123 static void
124 mh_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
125 {
126         CamelMhFolder *mh_folder = (CamelMhFolder *)folder;
127         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
128         CamelStream *output_stream;
129         CamelMessageInfo *mi;
130         char *name;
131
132         /* FIXME: probably needs additional locking (although mh doesn't appear do do it) */
133
134         d(printf("Appending message\n"));
135
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))
139                 return;
140         
141         d(printf("Appending message: uid is %s\n", camel_message_info_uid(mi)));
142         
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)
147                 goto fail_write;
148         
149         if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *)message, output_stream) == -1
150             || camel_stream_close (output_stream) == -1)
151                 goto fail_write;
152         
153         /* close this? */
154         camel_object_unref (CAMEL_OBJECT (output_stream));
155
156         g_free(name);
157         
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);
161         
162         if (appended_uid)
163                 *appended_uid = g_strdup(camel_message_info_uid(mi));
164
165         return;
166         
167  fail_write:
168         
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));
172         
173         if (errno == EINTR)
174                 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
175                                      _("MH append message cancelled"));
176         else
177                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
178                                       _("Cannot append message to mh folder: %s: %s"),
179                                       name, g_strerror (errno));
180         
181         if (output_stream) {
182                 camel_object_unref (CAMEL_OBJECT (output_stream));
183                 unlink (name);
184         }
185         
186         g_free (name);
187 }
188
189 static CamelMimeMessage *mh_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex)
190 {
191         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
192         CamelStream *message_stream = NULL;
193         CamelMimeMessage *message = NULL;
194         CamelMessageInfo *info;
195         char *name;
196
197         d(printf("getting message: %s\n", uid));
198
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"));
202                 return NULL;
203         }
204
205         /* we only need it to check the message exists */
206         camel_folder_summary_info_free(folder->summary, info);
207
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));
213                 g_free(name);
214                 return NULL;
215         }
216
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"));
222                 g_free(name);
223                 camel_object_unref((CamelObject *)message_stream);
224                 camel_object_unref((CamelObject *)message);
225                 return NULL;
226
227         }
228         camel_object_unref((CamelObject *)message_stream);
229         g_free(name);
230
231         return message;
232 }