1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-mbox-folder.c : Abstract class for an email folder */
6 * Copyright (C) 1999 Bertrand Guiheneuf <Bertrand.Guiheneuf@aful.org> .
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/types.h>
35 #include "camel-mbox-folder.h"
36 #include "camel-mbox-store.h"
37 #include "gstring-util.h"
38 #include "camel-log.h"
39 #include "camel-stream-buffered-fs.h"
40 #include "camel-folder-summary.h"
41 #include "gmime-utils.h"
43 #include "camel-exception.h"
46 #include "mbox-utils.h"
48 #include "mbox-summary.h"
51 static CamelFolderClass *parent_class=NULL;
53 /* Returns the class for a CamelMboxFolder */
54 #define CMBOXF_CLASS(so) CAMEL_MBOX_FOLDER_CLASS (GTK_OBJECT(so)->klass)
55 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (GTK_OBJECT(so)->klass)
56 #define CMBOXS_CLASS(so) CAMEL_STORE_CLASS (GTK_OBJECT(so)->klass)
59 static void _init_with_store (CamelFolder *folder, CamelStore *parent_store, CamelException *ex);
60 static void _set_name(CamelFolder *folder, const gchar *name, CamelException *ex);
63 static void _open (CamelFolder *folder, CamelFolderOpenMode mode, CamelException *ex);
64 static void _close (CamelFolder *folder, gboolean expunge, CamelException *ex);
65 static gboolean _exists (CamelFolder *folder, CamelException *ex);
66 static gboolean _create(CamelFolder *folder, CamelException *ex);
67 static gboolean _delete (CamelFolder *folder, gboolean recurse, CamelException *ex);
69 static gboolean _delete_messages (CamelFolder *folder, CamelException *ex);
70 static GList *_list_subfolders (CamelFolder *folder, CamelException *ex);
71 static CamelMimeMessage *_get_message (CamelFolder *folder, gint number, CamelException *ex);
72 static gint _get_message_count (CamelFolder *folder, CamelException *ex);
73 static gint _append_message (CamelFolder *folder, CamelMimeMessage *message, CamelException *ex);
74 static void _expunge (CamelFolder *folder, CamelException *ex);
75 static void _copy_message_to (CamelFolder *folder, CamelMimeMessage *message, CamelFolder *dest_folder, CamelException *ex);
76 static const gchar *_get_message_uid (CamelFolder *folder, CamelMimeMessage *message, CamelException *ex);
77 static CamelMimeMessage *_get_message_by_uid (CamelFolder *folder, const gchar *uid, CamelException *ex);
78 static GList *_get_uid_list (CamelFolder *folder, CamelException *ex);
81 static void _finalize (GtkObject *object);
84 camel_mbox_folder_class_init (CamelMboxFolderClass *camel_mbox_folder_class)
86 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_mbox_folder_class);
87 GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (camel_folder_class);
89 parent_class = gtk_type_class (camel_folder_get_type ());
91 /* virtual method definition */
92 /* virtual method overload */
93 camel_folder_class->init_with_store = _init_with_store;
94 camel_folder_class->set_name = _set_name;
95 camel_folder_class->open = _open;
96 camel_folder_class->close = _close;
97 camel_folder_class->exists = _exists;
98 camel_folder_class->create = _create;
99 camel_folder_class->delete = _delete;
101 camel_folder_class->delete_messages = _delete_messages;
102 camel_folder_class->list_subfolders = _list_subfolders;
103 camel_folder_class->get_message_by_number = _get_message_by_number;
104 camel_folder_class->get_message_count = _get_message_count;
105 camel_folder_class->append_message = _append_message;
106 camel_folder_class->expunge = _expunge;
107 camel_folder_class->copy_message_to = _copy_message_to;
108 camel_folder_class->get_message_uid = _get_message_uid;
109 camel_folder_class->get_message_by_uid = _get_message_by_uid;
110 camel_folder_class->get_uid_list = _get_uid_list;
112 gtk_object_class->finalize = _finalize;
119 _finalize (GtkObject *object)
121 CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER (object);
123 CAMEL_LOG_FULL_DEBUG ("Entering CamelFolder::finalize\n");
126 g_free (mbox_folder->folder_file_path);
127 g_free (mbox_folder->folder_dir_path);
129 GTK_OBJECT_CLASS (parent_class)->finalize (object);
130 CAMEL_LOG_FULL_DEBUG ("Leaving CamelFolder::finalize\n");
138 camel_mbox_folder_get_type (void)
140 static GtkType camel_mbox_folder_type = 0;
142 if (!camel_mbox_folder_type) {
143 GtkTypeInfo camel_mbox_folder_info =
146 sizeof (CamelMboxFolder),
147 sizeof (CamelMboxFolderClass),
148 (GtkClassInitFunc) camel_mbox_folder_class_init,
149 (GtkObjectInitFunc) NULL,
150 /* reserved_1 */ NULL,
151 /* reserved_2 */ NULL,
152 (GtkClassInitFunc) NULL,
155 camel_mbox_folder_type = gtk_type_unique (CAMEL_FOLDER_TYPE, &camel_mbox_folder_info);
158 return camel_mbox_folder_type;
167 _init_with_store (CamelFolder *folder, CamelStore *parent_store, CamelException *ex)
169 CAMEL_LOG_FULL_DEBUG ("Entering CamelMhFolder::init_with_store\n");
171 /* call parent method */
172 parent_class->init_with_store (folder, parent_store, ex);
173 if (camel_exception_get_id (ex)) return;
175 /* we assume that the parent init_with_store
176 method checks for the existance of @folder */
178 folder->can_hold_messages = TRUE;
179 folder->can_hold_folders = TRUE;
180 folder->has_summary_capability = TRUE;
181 folder->has_uid_capability = TRUE;
183 folder->summary = NULL;
185 CAMEL_LOG_FULL_DEBUG ("Leaving CamelMhFolder::init_with_store\n");
194 _open (CamelFolder *folder, CamelFolderOpenMode mode, CamelException *ex)
196 CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER (folder);
197 struct dirent *dir_entry;
201 if (folder->open_state == FOLDER_OPEN) {
202 camel_exception_set (ex,
203 CAMEL_EXCEPTION_FOLDER_INVALID_STATE,
204 "folder is already open");
210 Here, we need to check for the summary file
211 existence and create it if necessary.
212 /* get (or create) uid list */
213 if (!(mbox_load_uid_list (mbox_folder) > 0))
214 mbox_generate_uid_list (mbox_folder);
216 /* get or create summary */
217 /* it is important that it comes after uid list reading/generation */
218 if (!(mbox_load_summary (mbox_folder) > 0))
219 mbox_generate_summary (folder);
230 _close (CamelFolder *folder, gboolean expunge, CamelException *ex)
232 CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER (folder);
235 /* call parent implementation */
236 parent_class->close (folder, expunge, ex);
243 _set_name (CamelFolder *folder, const gchar *name, CamelException *ex)
245 CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER (folder);
246 const gchar *root_dir_path;
248 const gchar *parent_full_name;
251 CAMEL_LOG_FULL_DEBUG ("Entering CamelMboxFolder::set_name\n");
253 /* call default implementation */
254 parent_class->set_name (folder, name, ex);
255 if (camel_exception_get_id (ex)) return;
257 g_free (mbox_folder->folder_file_path);
258 g_free (mbox_folder->folder_dir_path);
260 separator = camel_store_get_separator (folder->parent_store);
261 root_dir_path = camel_mbox_store_get_toplevel_dir (CAMEL_MBOX_STORE(folder->parent_store));
263 CAMEL_LOG_FULL_DEBUG ("CamelMboxFolder::set_name full_name is %s\n", folder->full_name);
264 CAMEL_LOG_FULL_DEBUG ("CamelMboxFolder::set_name root_dir_path is %s\n", root_dir_path);
265 CAMEL_LOG_FULL_DEBUG ("CamelMboxFolder::separator is %c\n", separator);
267 mbox_folder->folder_file_path = g_strdup_printf ("%s%c%s", root_dir_path, separator, folder->full_name);
268 mbox_folder->folder_dir_path = g_strdup_printf ("%s%c%s.sdb", root_dir_path, separator, folder->full_name);
271 CAMEL_LOG_FULL_DEBUG ("CamelMboxFolder::set_name mbox_folder->folder_file_path is %s\n",
272 mbox_folder->folder_file_path);
273 CAMEL_LOG_FULL_DEBUG ("CamelMboxFolder::set_name mbox_folder->folder_dir_path is %s\n",
274 mbox_folder->folder_dir_path);
275 CAMEL_LOG_FULL_DEBUG ("Leaving CamelMboxFolder::set_name\n");
284 _exists (CamelFolder *folder, CamelException *ex)
286 CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER(folder);
287 struct stat stat_buf;
291 CAMEL_LOG_FULL_DEBUG ("Entering CamelMboxFolder::exists\n");
293 /* check if the folder object exists */
295 camel_exception_set (ex,
296 CAMEL_EXCEPTION_FOLDER_NULL,
297 "folder object is NULL");
301 /* check if the mbox file path is determined */
302 if (!mbox_folder->folder_file_path) {
303 camel_exception_set (ex,
304 CAMEL_EXCEPTION_FOLDER_INVALID,
305 "undetermined folder file path. Maybe use set_name ?");
309 /* check if the mbox dir path is determined */
310 if (!mbox_folder->folder_dir_path) {
311 camel_exception_set (ex,
312 CAMEL_EXCEPTION_FOLDER_INVALID,
313 "undetermined folder directory path. Maybe use set_name ?");
317 /* check if the mbox directory exists */
318 stat_error = stat (mbox_folder->folder_dir_path, &stat_buf);
319 if (stat_error == -1) {
320 CAMEL_LOG_FULL_DEBUG ("CamelMboxFolder::exists when executing stat on %s, stat_error = %d\n",
321 mbox_folder->folder_dir_path, stat_error);
322 CAMEL_LOG_FULL_DEBUG (" Full error text is : %s\n", strerror(errno));
323 camel_exception_set (ex,
324 CAMEL_EXCEPTION_SYSTEM,
328 exists = S_ISDIR (stat_buf.st_mode);
329 if (!exists) return FALSE;
331 /* check if the mbox file exists */
332 stat_error = stat (mbox_folder->folder_file_path, &stat_buf);
333 if (stat_error == -1) {
334 CAMEL_LOG_FULL_DEBUG ("CamelMboxFolder::exists when executing stat on %s, stat_error = %d\n",
335 mbox_folder->folder_file_path, stat_error);
336 CAMEL_LOG_FULL_DEBUG (" Full error text is : %s\n", strerror(errno));
337 camel_exception_set (ex,
338 CAMEL_EXCEPTION_SYSTEM,
343 exists = S_REG (stat_buf.st_mode);
344 /* we should check the rights here */
346 CAMEL_LOG_FULL_DEBUG ("Leaving CamelMboxFolder::exists\n");
358 _create (CamelFolder *folder, CamelException *ex)
360 CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER(folder);
361 const gchar *folder_file_path, *folder_dir_path;
362 mode_t dir_mode = S_IRWXU;
364 gboolean folder_already_exists;
368 /* check if the folder object exists */
370 camel_exception_set (ex,
371 CAMEL_EXCEPTION_FOLDER_NULL,
372 "folder object is NULL");
377 /* call default implementation */
378 parent_class->create (folder, ex);
380 /* get the paths of what we need to create */
381 folder_file_path = mbox_folder->folder_file_path;
382 folder_dir_path = mbox_folder->folder_file_path;
384 if (!(folder_file_path || folder_dir_path)) {
385 camel_exception_set (ex,
386 CAMEL_EXCEPTION_FOLDER_INVALID,
387 "invalid folder path. Use set_name ?");
392 /* if the folder already exists, simply return */
393 folder_already_exists = camel_folder_exists (folder,ex);
394 if (camel_exception_get_id (ex)) return FALSE;
396 if (folder_already_exists) return TRUE;
399 /* create the directory for the subfolders */
400 mkdir_error = mkdir (folder_dir_path, dir_mode);
401 if (mkdir_error == -1) goto io_error;
404 /* create the mbox file */
405 /* it must be rw for the user and none for the others */
406 old_umask = umask (0700);
407 creat_fd = open (folder_file_path,
408 O_WRONLY | O_CREAT | O_APPEND |
411 if (creat_fd == -1) goto io_error;
416 /* exception handling for io errors */
419 CAMEL_LOG_WARNING ("CamelMboxFolder::create, error when creating %s and %s\n",
420 folder_dir_path, folder_file_path);
421 CAMEL_LOG_FULL_DEBUG ( " Full error text is : %s\n", strerror(errno));
423 if (errno == EACCES) {
424 camel_exception_set (ex,
425 CAMEL_EXCEPTION_FOLDER_INSUFFICIENT_PERMISSION,
426 "You don't have the permission to create the mbox file.");
429 camel_exception_set (ex,
430 CAMEL_EXCEPTION_SYSTEM,
431 "Unable to create the mbox file.");
444 _delete (CamelFolder *folder, gboolean recurse, CamelException *ex)
446 CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER(folder);
447 const gchar *folder_file_path, *folder_dir_path;
448 gint rmdir_error = 0;
450 /* check if the folder object exists */
452 camel_exception_set (ex,
453 CAMEL_EXCEPTION_FOLDER_NULL,
454 "folder object is NULL");
459 /* in the case where the folder does not exist,
461 if (!camel_folder_exists (folder, ex)) return TRUE;
464 /* call default implementation.
465 It should delete the messages in the folder
466 and recurse the operation to subfolders */
467 parent_class->delete (folder, recurse, ex);
470 /* get the paths of what we need to delete */
471 folder_file_path = mbox_folder->folder_file_path;
472 folder_dir_path = mbox_folder->folder_file_path;
474 if (!(folder_file_path || folder_dir_path)) {
475 camel_exception_set (ex,
476 CAMEL_EXCEPTION_FOLDER_INVALID,
477 "invalid folder path. Use set_name ?");
482 /* physically delete the directory */
483 CAMEL_LOG_FULL_DEBUG ("CamelMboxFolder::delete removing directory %s\n", folder_dir_path);
484 rmdir_error = rmdir (folder_dir_path);
485 if (rmdir_error == -1)
488 camel_exception_set (ex,
489 CAMEL_EXCEPTION_FOLDER_INSUFFICIENT_PERMISSION,
490 "Not enough permission to delete the mbox folder");
495 camel_exception_set (ex,
496 CAMEL_EXCEPTION_FOLDER_NON_EMPTY,
497 "mbox folder not empty. Cannot delete it. Maybe use recurse flag ?");
501 camel_exception_set (ex,
502 CAMEL_EXCEPTION_SYSTEM,
503 "Unable to delete the mbox folder.");
507 /** Ber : tu dois supprimer le fichier maintenant */