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) 2001-2003 Ximian, Inc. (www.ximian.com)
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-file-utils.h"
40 #include "camel-local-private.h"
41 #include "camel-lock-client.h"
42 #include "camel-mime-filter-from.h"
43 #include "camel-mime-message.h"
44 #include "camel-session.h"
45 #include "camel-stream-filter.h"
46 #include "camel-stream-fs.h"
48 #include "camel-spool-folder.h"
49 #include "camel-spool-store.h"
50 #include "camel-spool-summary.h"
52 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
54 static CamelFolderClass *parent_class = NULL;
56 /* Returns the class for a CamelSpoolFolder */
57 #define CSPOOLF_CLASS(so) CAMEL_SPOOL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
58 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
59 #define CSPOOLS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
61 static CamelLocalSummary *spool_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index);
63 static int spool_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex);
64 static void spool_unlock(CamelLocalFolder *lf);
66 static void spool_finalize(CamelObject * object);
69 camel_spool_folder_class_init(CamelSpoolFolderClass *klass)
71 CamelLocalFolderClass *lklass = (CamelLocalFolderClass *)klass;
73 parent_class = (CamelFolderClass *)camel_mbox_folder_get_type();
75 lklass->create_summary = spool_create_summary;
76 lklass->lock = spool_lock;
77 lklass->unlock = spool_unlock;
81 spool_init(gpointer object, gpointer klass)
83 CamelSpoolFolder *spool_folder = object;
85 spool_folder->lockid = -1;
89 spool_finalize(CamelObject * object)
91 /*CamelSpoolFolder *spool_folder = CAMEL_SPOOL_FOLDER(object);*/
94 CamelType camel_spool_folder_get_type(void)
96 static CamelType camel_spool_folder_type = CAMEL_INVALID_TYPE;
98 if (camel_spool_folder_type == CAMEL_INVALID_TYPE) {
99 camel_spool_folder_type = camel_type_register(camel_mbox_folder_get_type(), "CamelSpoolFolder",
100 sizeof(CamelSpoolFolder),
101 sizeof(CamelSpoolFolderClass),
102 (CamelObjectClassInitFunc) camel_spool_folder_class_init,
104 (CamelObjectInitFunc) spool_init,
105 (CamelObjectFinalizeFunc) spool_finalize);
108 return camel_spool_folder_type;
112 camel_spool_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
116 d(printf("Creating spool folder: %s in %s\n", full_name, camel_local_store_get_toplevel_dir((CamelLocalStore *)parent_store)));
118 folder = (CamelFolder *)camel_object_new(CAMEL_SPOOL_FOLDER_TYPE);
120 if (parent_store->flags & CAMEL_STORE_FILTER_INBOX
121 && strcmp(full_name, "INBOX") == 0)
122 folder->folder_flags |= CAMEL_FOLDER_FILTER_RECENT;
123 flags &= ~CAMEL_STORE_FOLDER_BODY_INDEX;
125 folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder, parent_store, full_name, flags, ex);
127 if (camel_url_get_param(((CamelService *)parent_store)->url, "xstatus"))
128 camel_mbox_summary_xstatus((CamelMboxSummary *)folder->summary, TRUE);
134 static CamelLocalSummary *
135 spool_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index)
137 return (CamelLocalSummary *)camel_spool_summary_new((CamelFolder *)lf, folder);
141 spool_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex)
144 CamelMboxFolder *mf = (CamelMboxFolder *)lf;
145 CamelSpoolFolder *sf = (CamelSpoolFolder *)lf;
147 mf->lockfd = open(lf->folder_path, O_RDWR, 0);
148 if (mf->lockfd == -1) {
149 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
150 _("Cannot create folder lock on %s: %s"),
151 lf->folder_path, g_strerror (errno));
155 while (retry < CAMEL_LOCK_RETRY) {
157 sleep(CAMEL_LOCK_DELAY);
159 camel_exception_clear(ex);
161 if (camel_lock_fcntl(mf->lockfd, type, ex) == 0) {
162 if (camel_lock_flock(mf->lockfd, type, ex) == 0) {
163 if ((sf->lockid = camel_lock_helper_lock(lf->folder_path, ex)) != -1)
165 camel_unlock_flock(mf->lockfd);
167 camel_unlock_fcntl(mf->lockfd);
179 spool_unlock(CamelLocalFolder *lf)
181 CamelMboxFolder *mf = (CamelMboxFolder *)lf;
182 CamelSpoolFolder *sf = (CamelSpoolFolder *)lf;
184 camel_lock_helper_unlock(sf->lockid);
186 camel_unlock_flock(mf->lockfd);
187 camel_unlock_fcntl(mf->lockfd);