Work in progress. Missing files
[platform/upstream/evolution-data-server.git] / camel / providers / mbox / camel-mbox-folder.c
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 */
3
4 /* 
5  *
6  * Copyright (C) 1999 Bertrand Guiheneuf <Bertrand.Guiheneuf@aful.org> .
7  *
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.
12  *
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.
17  *
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
21  * USA
22  */
23
24
25 #include <config.h> 
26
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-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"
42
43 #include "camel-exception.h"
44
45 #if 0
46 #include "mbox-utils.h"
47 #include "mbox-uid.h"
48 #include "mbox-summary.h"
49 #endif 
50
51 static CamelFolderClass *parent_class=NULL;
52
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)
57
58
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);
61
62
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);
68 #if 0
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);
79 #endif
80
81 static void _finalize (GtkObject *object);
82
83 static void
84 camel_mbox_folder_class_init (CamelMboxFolderClass *camel_mbox_folder_class)
85 {
86         CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_mbox_folder_class);
87         GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (camel_folder_class);
88
89         parent_class = gtk_type_class (camel_folder_get_type ());
90                 
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;
100 #if 0
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;
111 #endif
112         gtk_object_class->finalize = _finalize;
113         
114 }
115
116
117
118 static void           
119 _finalize (GtkObject *object)
120 {
121         CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER (object);
122
123         CAMEL_LOG_FULL_DEBUG ("Entering CamelFolder::finalize\n");
124
125         
126         g_free (mbox_folder->folder_file_path);
127         g_free (mbox_folder->folder_dir_path);
128
129         GTK_OBJECT_CLASS (parent_class)->finalize (object);
130         CAMEL_LOG_FULL_DEBUG ("Leaving CamelFolder::finalize\n");
131 }
132
133
134
135
136
137 GtkType
138 camel_mbox_folder_get_type (void)
139 {
140         static GtkType camel_mbox_folder_type = 0;
141         
142         if (!camel_mbox_folder_type)    {
143                 GtkTypeInfo camel_mbox_folder_info =    
144                 {
145                         "CamelMboxFolder",
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,
153                 };
154                 
155                 camel_mbox_folder_type = gtk_type_unique (CAMEL_FOLDER_TYPE, &camel_mbox_folder_info);
156         }
157         
158         return camel_mbox_folder_type;
159 }
160
161
162
163
164
165
166 static void 
167 _init_with_store (CamelFolder *folder, CamelStore *parent_store, CamelException *ex)
168 {
169         CAMEL_LOG_FULL_DEBUG ("Entering CamelMhFolder::init_with_store\n");
170
171         /* call parent method */
172         parent_class->init_with_store (folder, parent_store, ex);
173         if (camel_exception_get_id (ex)) return;
174
175         /* we assume that the parent init_with_store 
176            method checks for the existance of @folder */
177            
178         folder->can_hold_messages = TRUE;
179         folder->can_hold_folders = TRUE;
180         folder->has_summary_capability = TRUE;
181         folder->has_uid_capability = TRUE;
182  
183         folder->summary = NULL;
184         
185         CAMEL_LOG_FULL_DEBUG ("Leaving CamelMhFolder::init_with_store\n");
186 }
187
188
189
190
191
192
193 static void
194 _open (CamelFolder *folder, CamelFolderOpenMode mode, CamelException *ex)
195 {
196         CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER (folder);
197         struct dirent *dir_entry;
198         DIR *dir_handle;
199         
200         
201         if (folder->open_state == FOLDER_OPEN) {
202                 camel_exception_set (ex, 
203                                      CAMEL_EXCEPTION_FOLDER_INVALID_STATE,
204                                      "folder is already open");
205                 return;
206         }
207
208         
209 #if 0
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);
215
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);
220         
221 #endif 
222 }
223
224
225
226
227
228
229 static void
230 _close (CamelFolder *folder, gboolean expunge, CamelException *ex)
231 {
232         CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER (folder);
233
234
235         /* call parent implementation */
236         parent_class->close (folder, expunge, ex);
237 }
238
239
240
241
242 static void
243 _set_name (CamelFolder *folder, const gchar *name, CamelException *ex)
244 {
245         CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER (folder);
246         const gchar *root_dir_path;
247         gchar *full_name;
248         const gchar *parent_full_name;
249         gchar separator;
250         
251         CAMEL_LOG_FULL_DEBUG ("Entering CamelMboxFolder::set_name\n");
252
253         /* call default implementation */
254         parent_class->set_name (folder, name, ex);
255         if (camel_exception_get_id (ex)) return;
256
257         g_free (mbox_folder->folder_file_path);
258         g_free (mbox_folder->folder_dir_path);
259
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));
262
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);
266
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);
269         
270         
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");
276 }
277
278
279
280
281
282
283 static gboolean
284 _exists (CamelFolder *folder, CamelException *ex)
285 {
286         CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER(folder);
287         struct stat stat_buf;
288         gint stat_error;
289         gboolean exists;
290
291         CAMEL_LOG_FULL_DEBUG ("Entering CamelMboxFolder::exists\n");
292
293         /* check if the folder object exists */
294         if (!folder) {
295                 camel_exception_set (ex, 
296                                      CAMEL_EXCEPTION_FOLDER_NULL,
297                                      "folder object is NULL");
298                 return FALSE;
299         }
300
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 ?");
306                 return FALSE;
307         }
308
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 ?");
314                 return FALSE;
315         }
316         
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,
325                                      strerror(errno));
326                 return FALSE;
327         }
328         exists = S_ISDIR (stat_buf.st_mode);
329         if (!exists) return FALSE;
330
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,
339                                      strerror(errno));
340                 return FALSE;
341         }
342
343         exists = S_REG (stat_buf.st_mode);
344         /* we should  check the rights here  */
345         
346         CAMEL_LOG_FULL_DEBUG ("Leaving CamelMboxFolder::exists\n");
347         return exists;
348 }
349
350
351
352
353
354
355
356
357 static gboolean
358 _create (CamelFolder *folder, CamelException *ex)
359 {
360         CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER(folder);
361         const gchar *folder_file_path, *folder_dir_path;
362         mode_t dir_mode = S_IRWXU;
363         gint mkdir_error;
364         gboolean folder_already_exists;
365         int creat_fd;
366         mode_t old_umask;
367
368         /* check if the folder object exists */
369         if (!folder) {
370                 camel_exception_set (ex, 
371                                      CAMEL_EXCEPTION_FOLDER_NULL,
372                                      "folder object is NULL");
373                 return FALSE;
374         }
375
376
377         /* call default implementation */
378         parent_class->create (folder, ex);
379
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;
383         
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 ?");
388                 return FALSE;
389         }
390
391         
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;
395
396         if (folder_already_exists) return TRUE;
397
398
399         /* create the directory for the subfolders */
400         mkdir_error = mkdir (folder_dir_path, dir_mode);
401         if (mkdir_error == -1) goto io_error;
402         
403
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 | 
409                          S_IRUSR  | S_IWUSR); 
410         umask (old_umask);
411         if (creat_fd == -1) goto io_error;
412                 
413         
414         return TRUE;
415
416         /* exception handling for io errors */
417         io_error :
418
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));
422
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.");
427                         return FALSE;
428                 } else {
429                         camel_exception_set (ex, 
430                                              CAMEL_EXCEPTION_SYSTEM,
431                                              "Unable to create the mbox file.");
432                         return FALSE;
433                 }
434 }
435
436
437
438
439
440
441
442
443 static gboolean
444 _delete (CamelFolder *folder, gboolean recurse, CamelException *ex)
445 {
446         CamelMboxFolder *mbox_folder = CAMEL_MBOX_FOLDER(folder);
447         const gchar *folder_file_path, *folder_dir_path;
448         gint rmdir_error = 0;
449
450         /* check if the folder object exists */
451         if (!folder) {
452                 camel_exception_set (ex, 
453                                      CAMEL_EXCEPTION_FOLDER_NULL,
454                                      "folder object is NULL");
455                 return FALSE;
456         }
457
458
459         /* in the case where the folder does not exist, 
460            return immediatly */
461         if (!camel_folder_exists (folder, ex)) return TRUE;
462
463
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);
468         
469
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;
473         
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 ?");
478                 return FALSE;
479         }
480
481         
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) 
486                 switch errno { 
487                 case EACCES :
488                         camel_exception_set (ex, 
489                                              CAMEL_EXCEPTION_FOLDER_INSUFFICIENT_PERMISSION,
490                                              "Not enough permission to delete the mbox folder");
491                         return FALSE;                   
492                         break;
493                         
494                 case ENOTEMPTY :
495                                 camel_exception_set (ex, 
496                                              CAMEL_EXCEPTION_FOLDER_NON_EMPTY,
497                                                      "mbox folder not empty. Cannot delete it. Maybe use recurse flag ?");
498                                 return FALSE;           
499                                 break;
500                 default :
501                         camel_exception_set (ex, 
502                                              CAMEL_EXCEPTION_SYSTEM,
503                                              "Unable to delete the mbox folder.");
504                         return FALSE;
505         }
506         
507         /** Ber : tu dois supprimer le fichier maintenant */
508         return TRUE;
509 }