Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / providers / local / camel-spool-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) 2001-2003 Ximian, Inc. (www.ximian.com)
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-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"
47
48 #include "camel-spool-folder.h"
49 #include "camel-spool-store.h"
50 #include "camel-spool-summary.h"
51
52 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
53
54 static CamelFolderClass *parent_class = NULL;
55
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))
60
61 static CamelLocalSummary *spool_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index);
62
63 static int spool_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex);
64 static void spool_unlock(CamelLocalFolder *lf);
65
66 static void spool_finalize(CamelObject * object);
67
68 static void
69 camel_spool_folder_class_init(CamelSpoolFolderClass *klass)
70 {
71         CamelLocalFolderClass *lklass = (CamelLocalFolderClass *)klass;
72
73         parent_class = (CamelFolderClass *)camel_mbox_folder_get_type();
74
75         lklass->create_summary = spool_create_summary;
76         lklass->lock = spool_lock;
77         lklass->unlock = spool_unlock;
78 }
79
80 static void
81 spool_init(gpointer object, gpointer klass)
82 {
83         CamelSpoolFolder *spool_folder = object;
84
85         spool_folder->lockid = -1;
86 }
87
88 static void
89 spool_finalize(CamelObject * object)
90 {
91         /*CamelSpoolFolder *spool_folder = CAMEL_SPOOL_FOLDER(object);*/
92 }
93
94 CamelType camel_spool_folder_get_type(void)
95 {
96         static CamelType camel_spool_folder_type = CAMEL_INVALID_TYPE;
97
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,
103                                                              NULL,
104                                                              (CamelObjectInitFunc) spool_init,
105                                                              (CamelObjectFinalizeFunc) spool_finalize);
106         }
107
108         return camel_spool_folder_type;
109 }
110
111 CamelFolder *
112 camel_spool_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
113 {
114         CamelFolder *folder;
115
116         d(printf("Creating spool folder: %s in %s\n", full_name, camel_local_store_get_toplevel_dir((CamelLocalStore *)parent_store)));
117
118         folder = (CamelFolder *)camel_object_new(CAMEL_SPOOL_FOLDER_TYPE);
119
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;
124
125         folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder, parent_store, full_name, flags, ex);
126         if (folder) {
127                 if (camel_url_get_param(((CamelService *)parent_store)->url, "xstatus"))
128                         camel_mbox_summary_xstatus((CamelMboxSummary *)folder->summary, TRUE);
129         }
130
131         return folder;
132 }
133
134 static CamelLocalSummary *
135 spool_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index)
136 {
137         return (CamelLocalSummary *)camel_spool_summary_new((CamelFolder *)lf, folder);
138 }
139
140 static int
141 spool_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex)
142 {
143         int retry = 0;
144         CamelMboxFolder *mf = (CamelMboxFolder *)lf;
145         CamelSpoolFolder *sf = (CamelSpoolFolder *)lf;
146
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));
152                 return -1;
153         }
154
155         while (retry < CAMEL_LOCK_RETRY) {
156                 if (retry > 0)
157                         sleep(CAMEL_LOCK_DELAY);
158
159                 camel_exception_clear(ex);
160
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)
164                                         return 0;
165                                 camel_unlock_flock(mf->lockfd);
166                         }
167                         camel_unlock_fcntl(mf->lockfd);
168                 }
169                 retry++;
170         }
171         
172         close (mf->lockfd);
173         mf->lockfd = -1;
174         
175         return -1;
176 }
177
178 static void
179 spool_unlock(CamelLocalFolder *lf)
180 {
181         CamelMboxFolder *mf = (CamelMboxFolder *)lf;
182         CamelSpoolFolder *sf = (CamelSpoolFolder *)lf;
183
184         camel_lock_helper_unlock(sf->lockid);
185         sf->lockid = -1;
186         camel_unlock_flock(mf->lockfd);
187         camel_unlock_fcntl(mf->lockfd);
188
189         close(mf->lockfd);
190         mf->lockfd = -1;
191 }