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 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 General Public License for more details.
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
27 #include <sys/types.h>
35 #include "camel-spool-folder.h"
36 #include "camel-spool-store.h"
37 #include "camel-stream-fs.h"
38 #include "camel-spool-summary.h"
39 #include "camel-data-wrapper.h"
40 #include "camel-mime-message.h"
41 #include "camel-stream-filter.h"
42 #include "camel-mime-filter-from.h"
43 #include "camel-exception.h"
45 #include "camel-lock-client.h"
47 #include "camel-local-private.h"
49 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
51 static CamelFolderClass *parent_class = NULL;
53 /* Returns the class for a CamelSpoolFolder */
54 #define CSPOOLF_CLASS(so) CAMEL_SPOOL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
55 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
56 #define CSPOOLS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
58 static CamelLocalSummary *spool_create_summary(const char *path, const char *folder, CamelIndex *index);
60 static int spool_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex);
61 static void spool_unlock(CamelLocalFolder *lf);
63 static void spool_finalize(CamelObject * object);
66 camel_spool_folder_class_init(CamelSpoolFolderClass *klass)
68 CamelLocalFolderClass *lklass = (CamelLocalFolderClass *)klass;
70 parent_class = (CamelFolderClass *)camel_mbox_folder_get_type();
72 /* virtual method overload */
73 lklass->create_summary = spool_create_summary;
74 lklass->lock = spool_lock;
75 lklass->unlock = spool_unlock;
79 spool_init(gpointer object, gpointer klass)
81 CamelSpoolFolder *spool_folder = object;
83 spool_folder->lockid = -1;
87 spool_finalize(CamelObject * object)
89 /*CamelSpoolFolder *spool_folder = CAMEL_SPOOL_FOLDER(object);*/
92 CamelType camel_spool_folder_get_type(void)
94 static CamelType camel_spool_folder_type = CAMEL_INVALID_TYPE;
96 if (camel_spool_folder_type == CAMEL_INVALID_TYPE) {
97 camel_spool_folder_type = camel_type_register(camel_mbox_folder_get_type(), "CamelSpoolFolder",
98 sizeof(CamelSpoolFolder),
99 sizeof(CamelSpoolFolderClass),
100 (CamelObjectClassInitFunc) camel_spool_folder_class_init,
102 (CamelObjectInitFunc) spool_init,
103 (CamelObjectFinalizeFunc) spool_finalize);
106 return camel_spool_folder_type;
110 camel_spool_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
114 d(printf("Creating spool folder: %s in %s\n", full_name, camel_local_store_get_toplevel_dir((CamelLocalStore *)parent_store)));
116 folder = (CamelFolder *)camel_object_new(CAMEL_SPOOL_FOLDER_TYPE);
118 if (parent_store->flags & CAMEL_STORE_FILTER_INBOX
119 && strcmp(full_name, "INBOX") == 0)
120 folder->folder_flags |= CAMEL_FOLDER_FILTER_RECENT;
121 flags &= CAMEL_STORE_FOLDER_BODY_INDEX;
123 folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder, parent_store, full_name, flags, ex);
125 if (camel_url_get_param(((CamelService *)parent_store)->url, "xstatus"))
126 camel_mbox_summary_xstatus((CamelMboxSummary *)folder->summary, TRUE);
132 static CamelLocalSummary *
133 spool_create_summary(const char *path, const char *folder, CamelIndex *index)
135 return (CamelLocalSummary *)camel_spool_summary_new(folder);
139 spool_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex)
142 CamelMboxFolder *mf = (CamelMboxFolder *)lf;
143 CamelSpoolFolder *sf = (CamelSpoolFolder *)lf;
145 mf->lockfd = open(lf->folder_path, O_RDWR, 0);
146 if (mf->lockfd == -1) {
147 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
148 _("Cannot create folder lock on %s: %s"),
149 lf->folder_path, g_strerror (errno));
153 while (retry < CAMEL_LOCK_RETRY) {
155 sleep(CAMEL_LOCK_DELAY);
157 camel_exception_clear(ex);
159 if (camel_lock_fcntl(mf->lockfd, type, ex) == 0) {
160 if (camel_lock_flock(mf->lockfd, type, ex) == 0) {
161 if ((sf->lockid = camel_lock_helper_lock(lf->folder_path, ex)) != -1)
163 camel_unlock_flock(mf->lockfd);
165 camel_unlock_fcntl(mf->lockfd);
174 spool_unlock(CamelLocalFolder *lf)
176 CamelMboxFolder *mf = (CamelMboxFolder *)lf;
177 CamelSpoolFolder *sf = (CamelSpoolFolder *)lf;
179 camel_lock_helper_unlock(sf->lockid);
181 camel_unlock_flock(mf->lockfd);
182 camel_unlock_fcntl(mf->lockfd);