Shared folders support with a GUI.
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #undef MEASURE_TIME
34
35 #ifdef MEASURE_TIME
36 #  include <sys/time.h>
37 #endif
38
39 #include "intl.h"
40 #include "folder.h"
41 #include "mh.h"
42 #include "procmsg.h"
43 #include "procheader.h"
44 #include "utils.h"
45
46 static GSList  *mh_get_uncached_msgs            (GHashTable     *msg_table,
47                                                  FolderItem     *item);
48 static MsgInfo *mh_parse_msg                    (const gchar    *file,
49                                                  FolderItem     *item);
50 static void     mh_scan_tree_recursive          (FolderItem     *item);
51
52 static gboolean mh_rename_folder_func           (GNode          *node,
53                                                  gpointer        data);
54
55
56 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
57 {
58         GSList *mlist;
59         GHashTable *msg_table;
60         gchar *path;
61         struct stat s;
62         gboolean scan_new = TRUE;
63 #ifdef MEASURE_TIME
64         struct timeval tv_before, tv_after, tv_result;
65
66         gettimeofday(&tv_before, NULL);
67 #endif
68
69         g_return_val_if_fail(item != NULL, NULL);
70
71         path = folder_item_get_path(item);
72         if (stat(path, &s) < 0) {
73                 FILE_OP_ERROR(path, "stat");
74         } else {
75                 if (item->mtime == s.st_mtime) {
76                         debug_print("Folder is not modified.\n");
77                         scan_new = FALSE;
78                 } else
79                         item->mtime = s.st_mtime;
80         }
81         g_free(path);
82
83         if (use_cache && !scan_new) {
84                 mlist = procmsg_read_cache(item, FALSE);
85                 if (!mlist)
86                         mlist = mh_get_uncached_msgs(NULL, item);
87         } else if (use_cache) {
88                 GSList *newlist;
89
90                 mlist = procmsg_read_cache(item, TRUE);
91                 msg_table = procmsg_msg_hash_table_create(mlist);
92
93                 newlist = mh_get_uncached_msgs(msg_table, item);
94                 if (msg_table)
95                         g_hash_table_destroy(msg_table);
96
97                 mlist = g_slist_concat(mlist, newlist);
98         } else
99                 mlist = mh_get_uncached_msgs(NULL, item);
100
101         procmsg_set_flags(mlist, item);
102
103 #ifdef MEASURE_TIME
104         gettimeofday(&tv_after, NULL);
105
106         timersub(&tv_after, &tv_before, &tv_result);
107         g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
108                 item->path, tv_result.tv_sec, tv_result.tv_usec);
109 #endif
110
111         return mlist;
112 }
113
114 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
115 {
116         gchar *path;
117         gchar *file;
118
119         g_return_val_if_fail(item != NULL, NULL);
120         g_return_val_if_fail(num > 0 && num <= item->last_num, NULL);
121
122         path = folder_item_get_path(item);
123         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
124         g_free(path);
125         if (!is_file_exist(file)) {
126                 g_free(file);
127                 return NULL;
128         }
129
130         return file;
131 }
132
133 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
134                 gboolean remove_source)
135 {
136         gchar *destpath;
137         gchar *destfile;
138
139         g_return_val_if_fail(dest != NULL, -1);
140         g_return_val_if_fail(file != NULL, -1);
141
142         if (dest->last_num < 0) {
143                 mh_scan_folder(folder, dest);
144                 if (dest->last_num < 0) return -1;
145         }
146
147         destpath = folder_item_get_path(dest);
148         g_return_val_if_fail(destpath != NULL, -1);
149         if (!is_dir_exist(destpath))
150                 make_dir_hier(destpath);
151
152         destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
153                                    dest->last_num + 1);
154
155         if (link(file, destfile) < 0) {
156                 if (EXDEV == errno) {
157                         if (copy_file(file, destfile) < 0) {
158                                 g_warning(_("can't copy message %s to %s\n"),
159                                           file, destfile);
160                                 g_free(destfile);
161                                 return -1;
162                         }
163                 } else {
164                         FILE_OP_ERROR(file, "link");
165                         g_free(destfile);
166                         return -1;
167                 }
168         }
169
170         if (remove_source) {
171                 if (unlink(file) < 0)
172                         FILE_OP_ERROR(file, "unlink");
173         }
174
175         g_free(destfile);
176         dest->last_num++;
177         return dest->last_num;
178 }
179
180 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
181 {
182         gchar *destdir;
183         gchar *srcfile;
184         gchar *destfile;
185         FILE *fp;
186         gint filemode = 0;
187         PrefsFolderItem *prefs;
188
189         g_return_val_if_fail(dest != NULL, -1);
190         g_return_val_if_fail(msginfo != NULL, -1);
191
192         if (msginfo->folder == dest) {
193                 g_warning(_("the src folder is identical to the dest.\n"));
194                 return -1;
195         }
196
197         if (dest->last_num < 0) {
198                 mh_scan_folder(folder, dest);
199                 if (dest->last_num < 0) return -1;
200         }
201
202         prefs = dest->prefs;
203
204         destdir = folder_item_get_path(dest);
205         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
206                 g_warning(_("Can't open mark file.\n"));
207
208         debug_print(_("Moving message %s%c%d to %s ...\n"),
209                     msginfo->folder->path, G_DIR_SEPARATOR,
210                     msginfo->msgnum, dest->path);
211         srcfile = procmsg_get_message_file_path(msginfo);
212         destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
213                                    dest->last_num + 1);
214         g_free(destdir);
215
216         if (move_file(srcfile, destfile) < 0) {
217                 g_free(srcfile);
218                 g_free(destfile);
219                 if (fp) fclose(fp);
220                 return -1;
221         }
222
223         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
224                 if (chmod(destfile, prefs->folder_chmod) < 0)
225                         FILE_OP_ERROR(destfile, "chmod");
226
227                 /* for mark file */
228                 filemode = prefs->folder_chmod;
229                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
230                 if (filemode & S_IROTH) filemode |= S_IWOTH;
231         }
232
233         g_free(srcfile);
234         g_free(destfile);
235         dest->last_num++;
236
237         if (fp) {
238                 MsgInfo newmsginfo;
239
240                 newmsginfo.msgnum = dest->last_num;
241                 newmsginfo.flags = msginfo->flags;
242                 if (dest->stype == F_OUTBOX ||
243                     dest->stype == F_QUEUE  ||
244                     dest->stype == F_DRAFT  ||
245                     dest->stype == F_TRASH)
246                         MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
247                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
248
249                 procmsg_write_flags(&newmsginfo, fp);
250
251                 if (filemode) {
252 #if HAVE_FCHMOD
253                         fchmod(fileno(fp), filemode);
254 #else
255                         markfile = folder_item_get_mark_file(dest);
256                         if (markfile) {
257                                 chmod(markfile, filemode);
258                                 g_free(markfile);
259                         }
260 #endif
261                 }
262
263                 fclose(fp);
264         }
265
266         return dest->last_num;
267 }
268
269 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
270 {
271         gchar *destdir;
272         gchar *srcfile;
273         gchar *destfile;
274         FILE *fp;
275         GSList *cur;
276         MsgInfo *msginfo;
277         PrefsFolderItem *prefs;
278
279         g_return_val_if_fail(dest != NULL, -1);
280         g_return_val_if_fail(msglist != NULL, -1);
281
282         if (dest->last_num < 0) {
283                 mh_scan_folder(folder, dest);
284                 if (dest->last_num < 0) return -1;
285         }
286
287         prefs = dest->prefs;
288
289         destdir = folder_item_get_path(dest);
290         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
291                 g_warning(_("Can't open mark file.\n"));
292
293         for (cur = msglist; cur != NULL; cur = cur->next) {
294                 msginfo = (MsgInfo *)cur->data;
295
296                 if (msginfo->folder == dest) {
297                         g_warning(_("the src folder is identical to the dest.\n"));
298                         continue;
299                 }
300                 debug_print(_("Moving message %s%c%d to %s ...\n"),
301                             msginfo->folder->path, G_DIR_SEPARATOR,
302                             msginfo->msgnum, dest->path);
303
304                 srcfile = procmsg_get_message_file_path(msginfo);
305                 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
306                                            dest->last_num + 1);
307
308                 if (move_file(srcfile, destfile) < 0) {
309                         g_free(srcfile);
310                         g_free(destfile);
311                         break;
312                 }
313
314                 g_free(srcfile);
315                 g_free(destfile);
316                 dest->last_num++;
317
318                 if (fp) {
319                         MsgInfo newmsginfo;
320
321                         newmsginfo.msgnum = dest->last_num;
322                         newmsginfo.flags = msginfo->flags;
323                         if (dest->stype == F_OUTBOX ||
324                             dest->stype == F_QUEUE  ||
325                             dest->stype == F_DRAFT  ||
326                             dest->stype == F_TRASH)
327                                 MSG_UNSET_PERM_FLAGS
328                                         (newmsginfo.flags,
329                                          MSG_NEW|MSG_UNREAD|MSG_DELETED);
330
331                         procmsg_write_flags(&newmsginfo, fp);
332                 }
333         }
334
335         g_free(destdir);
336         if (fp) fclose(fp);
337
338         return dest->last_num;
339 }
340
341 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
342 {
343         gchar *destdir;
344         gchar *srcfile;
345         gchar *destfile;
346         FILE *fp;
347         gint filemode = 0;
348         PrefsFolderItem *prefs;
349
350         g_return_val_if_fail(dest != NULL, -1);
351         g_return_val_if_fail(msginfo != NULL, -1);
352
353         if (msginfo->folder == dest) {
354                 g_warning(_("the src folder is identical to the dest.\n"));
355                 return -1;
356         }
357
358         if (dest->last_num < 0) {
359                 mh_scan_folder(folder, dest);
360                 if (dest->last_num < 0) return -1;
361         }
362
363         prefs = dest->prefs;
364
365         destdir = folder_item_get_path(dest);
366         if (!is_dir_exist(destdir))
367                 make_dir_hier(destdir);
368
369         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
370                 g_warning(_("Can't open mark file.\n"));
371
372         debug_print(_("Copying message %s%c%d to %s ...\n"),
373                     msginfo->folder->path, G_DIR_SEPARATOR,
374                     msginfo->msgnum, dest->path);
375         srcfile = procmsg_get_message_file_path(msginfo);
376         destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
377                                    dest->last_num + 1);
378         g_free(destdir);
379
380         if (is_file_exist(destfile)) {
381                 g_warning(_("%s already exists."), destfile);
382                 g_free(srcfile);
383                 g_free(destfile);
384                 if (fp) fclose(fp);
385                 return -1;
386         }
387
388         if (copy_file(srcfile, destfile) < 0) {
389                 FILE_OP_ERROR(srcfile, "copy");
390                 g_free(srcfile);
391                 g_free(destfile);
392                 if (fp) fclose(fp);
393                 return -1;
394         }
395
396         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
397                 if (chmod(destfile, prefs->folder_chmod) < 0)
398                         FILE_OP_ERROR(destfile, "chmod");
399
400                 /* for mark file */
401                 filemode = prefs->folder_chmod;
402                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
403                 if (filemode & S_IROTH) filemode |= S_IWOTH;
404         }
405
406         g_free(srcfile);
407         g_free(destfile);
408         dest->last_num++;
409
410         if (fp) {
411                 MsgInfo newmsginfo;
412
413                 newmsginfo.msgnum = dest->last_num;
414                 newmsginfo.flags = msginfo->flags;
415                 if (dest->stype == F_OUTBOX ||
416                     dest->stype == F_QUEUE  ||
417                     dest->stype == F_DRAFT  ||
418                     dest->stype == F_TRASH)
419                         MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
420                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
421                 procmsg_write_flags(&newmsginfo, fp);
422
423                 if (filemode) {
424 #if HAVE_FCHMOD
425                         fchmod(fileno(fp), filemode);
426 #else
427                         markfile = folder_item_get_mark_file(dest);
428                         if (markfile) {
429                                 chmod(markfile, filemode);
430                                 g_free(markfile);
431                         }
432 #endif
433                 }
434
435                 fclose(fp);
436         }
437
438         return dest->last_num;
439 }
440
441 /*
442 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
443 {
444         Folder * src_folder;
445         gchar * filename;
446         gint num;
447         gchar * destdir;
448         FILE * fp;
449
450         src_folder = msginfo->folder->folder;
451         
452         g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
453         
454         filename = src_folder->fetch_msg(src_folder,
455                                          msginfo->folder,
456                                          msginfo->msgnum);
457         if (filename == NULL)
458                 return -1;
459
460         num = folder->add_msg(folder, dest, filename, FALSE);
461
462         destdir = folder_item_get_path(dest);
463         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
464                 g_warning(_("Can't open mark file.\n"));
465
466         if (fp) {
467                 MsgInfo newmsginfo;
468
469                 newmsginfo.msgnum = dest->last_num;
470                 newmsginfo.flags = msginfo->flags;
471                 if (dest->stype == F_OUTBOX ||
472                     dest->stype == F_QUEUE  ||
473                     dest->stype == F_DRAFT  ||
474                     dest->stype == F_TRASH)
475                         MSG_UNSET_FLAGS(newmsginfo.flags,
476                                         MSG_NEW|MSG_UNREAD|MSG_DELETED);
477
478                 procmsg_write_flags(&newmsginfo, fp);
479                 fclose(fp);
480         }
481         
482         return num;
483 }
484 */
485
486 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
487 {
488         gchar *destdir;
489         gchar *srcfile;
490         gchar *destfile;
491         FILE *fp;
492         GSList *cur;
493         MsgInfo *msginfo;
494
495         g_return_val_if_fail(dest != NULL, -1);
496         g_return_val_if_fail(msglist != NULL, -1);
497
498         if (dest->last_num < 0) {
499                 mh_scan_folder(folder, dest);
500                 if (dest->last_num < 0) return -1;
501         }
502
503         destdir = folder_item_get_path(dest);
504         if (!is_dir_exist(destdir))
505                 make_dir_hier(destdir);
506
507         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
508                 g_warning(_("Can't open mark file.\n"));
509
510         for (cur = msglist; cur != NULL; cur = cur->next) {
511                 msginfo = (MsgInfo *)cur->data;
512
513                 if (msginfo->folder == dest) {
514                         g_warning(_("the src folder is identical to the dest.\n"));
515                         continue;
516                 }
517                 debug_print(_("Copying message %s%c%d to %s ...\n"),
518                             msginfo->folder->path, G_DIR_SEPARATOR,
519                             msginfo->msgnum, dest->path);
520
521                 srcfile = procmsg_get_message_file_path(msginfo);
522                 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
523                                            dest->last_num + 1);
524
525                 if (is_file_exist(destfile)) {
526                         g_warning(_("%s already exists."), destfile);
527                         g_free(srcfile);
528                         g_free(destfile);
529                         break;
530                 }
531
532                 if (copy_file(srcfile, destfile) < 0) {
533                         FILE_OP_ERROR(srcfile, "copy");
534                         g_free(srcfile);
535                         g_free(destfile);
536                         break;
537                 }
538
539                 g_free(srcfile);
540                 g_free(destfile);
541                 dest->last_num++;
542
543                 if (fp) {
544                         MsgInfo newmsginfo;
545
546                         newmsginfo.msgnum = dest->last_num;
547                         newmsginfo.flags = msginfo->flags;
548                         if (dest->stype == F_OUTBOX ||
549                             dest->stype == F_QUEUE  ||
550                             dest->stype == F_DRAFT  ||
551                             dest->stype == F_TRASH)
552                                 MSG_UNSET_PERM_FLAGS
553                                         (newmsginfo.flags,
554                                          MSG_NEW|MSG_UNREAD|MSG_DELETED);
555                         procmsg_write_flags(&newmsginfo, fp);
556                 }
557         }
558
559         g_free(destdir);
560         if (fp) fclose(fp);
561
562         return dest->last_num;
563 }
564
565 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
566 {
567         gchar *file;
568
569         g_return_val_if_fail(item != NULL, -1);
570
571         file = mh_fetch_msg(folder, item, num);
572         g_return_val_if_fail(file != NULL, -1);
573
574         if (unlink(file) < 0) {
575                 FILE_OP_ERROR(file, "unlink");
576                 g_free(file);
577                 return -1;
578         }
579
580         g_free(file);
581         return 0;
582 }
583
584 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
585 {
586         gchar *path;
587         gint val;
588
589         g_return_val_if_fail(item != NULL, -1);
590
591         path = folder_item_get_path(item);
592         g_return_val_if_fail(path != NULL, -1);
593         val = remove_all_numbered_files(path);
594         g_free(path);
595
596         return val;
597 }
598
599 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
600 {
601         struct stat s;
602
603         if (stat(itos(msginfo->msgnum), &s) < 0 ||
604             msginfo->size  != s.st_size ||
605             msginfo->mtime != s.st_mtime)
606                 return TRUE;
607
608         return FALSE;
609 }
610
611 void mh_scan_folder(Folder *folder, FolderItem *item)
612 {
613         gchar *path;
614         DIR *dp;
615         struct dirent *d;
616         struct stat s;
617         gint max = 0;
618         gint num;
619         gint n_msg = 0;
620
621         g_return_if_fail(item != NULL);
622
623         path = folder_item_get_path(item);
624         g_return_if_fail(path != NULL);
625         if (change_dir(path) < 0) {
626                 g_free(path);
627                 return;
628         }
629         g_free(path);
630
631         if ((dp = opendir(".")) == NULL) {
632                 FILE_OP_ERROR(item->path, "opendir");
633                 return;
634         }
635
636         if (folder->ui_func)
637                 folder->ui_func(folder, item, folder->ui_func_data);
638
639         while ((d = readdir(dp)) != NULL) {
640                 if ((num = to_number(d->d_name)) >= 0 &&
641                     stat(d->d_name, &s) == 0 &&
642                     S_ISREG(s.st_mode)) {
643                         n_msg++;
644                         if (max < num)
645                                 max = num;
646                 }
647         }
648
649         closedir(dp);
650
651         if (n_msg == 0)
652                 item->new = item->unread = item->total = 0;
653         else {
654                 gint new, unread, total;
655
656                 procmsg_get_mark_sum(".", &new, &unread, &total);
657                 if (n_msg > total) {
658                         new += n_msg - total;
659                         unread += n_msg - total;
660                 }
661                 item->new = new;
662                 item->unread = unread;
663                 item->total = n_msg;
664         }
665
666         debug_print(_("Last number in dir %s = %d\n"), item->path, max);
667         item->last_num = max;
668 }
669
670 void mh_scan_tree(Folder *folder)
671 {
672         FolderItem *item;
673         gchar *rootpath;
674
675         g_return_if_fail(folder != NULL);
676
677         folder_tree_destroy(folder);
678         item = folder_item_new(folder->name, NULL);
679         item->folder = folder;
680         folder->node = g_node_new(item);
681
682         rootpath = folder_item_get_path(item);
683         if (change_dir(rootpath) < 0) {
684                 g_free(rootpath);
685                 return;
686         }
687         g_free(rootpath);
688
689         mh_scan_tree_recursive(item);
690 }
691
692 #define MAKE_DIR_IF_NOT_EXIST(dir) \
693 { \
694         if (!is_dir_exist(dir)) { \
695                 if (is_file_exist(dir)) { \
696                         g_warning(_("File `%s' already exists.\n" \
697                                     "Can't create folder."), dir); \
698                         return -1; \
699                 } \
700                 if (mkdir(dir, S_IRWXU) < 0) { \
701                         FILE_OP_ERROR(dir, "mkdir"); \
702                         return -1; \
703                 } \
704                 if (chmod(dir, S_IRWXU) < 0) \
705                         FILE_OP_ERROR(dir, "chmod"); \
706         } \
707 }
708
709 gint mh_create_tree(Folder *folder)
710 {
711         gchar *rootpath;
712
713         g_return_val_if_fail(folder != NULL, -1);
714
715         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
716         rootpath = LOCAL_FOLDER(folder)->rootpath;
717         MAKE_DIR_IF_NOT_EXIST(rootpath);
718         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
719         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
720         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
721         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
722         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
723         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
724
725         return 0;
726 }
727
728 #undef MAKE_DIR_IF_NOT_EXIST
729
730 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
731                              const gchar *name)
732 {
733         gchar *path;
734         gchar *fullpath;
735         FolderItem *new_item;
736
737         g_return_val_if_fail(folder != NULL, NULL);
738         g_return_val_if_fail(parent != NULL, NULL);
739         g_return_val_if_fail(name != NULL, NULL);
740
741         path = folder_item_get_path(parent);
742         if (!is_dir_exist(path))
743                 make_dir_hier(path);
744
745         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
746         g_free(path);
747
748         if (mkdir(fullpath, S_IRWXU) < 0) {
749                 FILE_OP_ERROR(fullpath, "mkdir");
750                 g_free(fullpath);
751                 return NULL;
752         }
753         if (chmod(fullpath, S_IRWXU) < 0)
754                 FILE_OP_ERROR(fullpath, "chmod");
755
756         g_free(fullpath);
757
758         if (parent->path)
759                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
760                                    NULL);
761         else
762                 path = g_strdup(name);
763         new_item = folder_item_new(name, path);
764         folder_item_append(parent, new_item);
765         g_free(path);
766
767         return new_item;
768 }
769
770 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
771 {
772         gchar *oldpath;
773         gchar *dirname;
774         gchar *newpath;
775         GNode *node;
776         gchar *paths[2];
777
778         g_return_val_if_fail(folder != NULL, -1);
779         g_return_val_if_fail(item != NULL, -1);
780         g_return_val_if_fail(item->path != NULL, -1);
781         g_return_val_if_fail(name != NULL, -1);
782
783         oldpath = folder_item_get_path(item);
784         if (!is_dir_exist(oldpath))
785                 make_dir_hier(oldpath);
786
787         dirname = g_dirname(oldpath);
788         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
789         g_free(dirname);
790
791         if (rename(oldpath, newpath) < 0) {
792                 FILE_OP_ERROR(oldpath, "rename");
793                 g_free(oldpath);
794                 g_free(newpath);
795                 return -1;
796         }
797
798         g_free(oldpath);
799         g_free(newpath);
800
801         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
802                 dirname = g_dirname(item->path);
803                 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
804                 g_free(dirname);
805         } else
806                 newpath = g_strdup(name);
807
808         g_free(item->name);
809         item->name = g_strdup(name);
810
811         node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
812                            item);
813         paths[0] = g_strdup(item->path);
814         paths[1] = newpath;
815         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
816                         mh_rename_folder_func, paths);
817
818         g_free(paths[0]);
819         g_free(paths[1]);
820         return 0;
821 }
822
823 gint mh_remove_folder(Folder *folder, FolderItem *item)
824 {
825         gchar *path;
826
827         g_return_val_if_fail(folder != NULL, -1);
828         g_return_val_if_fail(item != NULL, -1);
829         g_return_val_if_fail(item->path != NULL, -1);
830
831         path = folder_item_get_path(item);
832         if (remove_dir_recursive(path) < 0) {
833                 g_warning("can't remove directory `%s'\n", path);
834                 g_free(path);
835                 return -1;
836         }
837
838         g_free(path);
839         folder_item_remove(item);
840         return 0;
841 }
842
843
844 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
845 {
846         gchar *path;
847         DIR *dp;
848         struct dirent *d;
849         struct stat s;
850         GSList *newlist = NULL;
851         GSList *last = NULL;
852         MsgInfo *msginfo;
853         gint n_newmsg = 0;
854         gint num;
855
856         g_return_val_if_fail(item != NULL, NULL);
857
858         path = folder_item_get_path(item);
859         g_return_val_if_fail(path != NULL, NULL);
860         if (change_dir(path) < 0) {
861                 g_free(path);
862                 return NULL;
863         }
864         g_free(path);
865
866         if ((dp = opendir(".")) == NULL) {
867                 FILE_OP_ERROR(item->path, "opendir");
868                 return NULL;
869         }
870
871         debug_print(_("\tSearching uncached messages... "));
872
873         if (msg_table) {
874                 while ((d = readdir(dp)) != NULL) {
875                         if ((num = to_number(d->d_name)) < 0) continue;
876                         if (stat(d->d_name, &s) < 0) {
877                                 FILE_OP_ERROR(d->d_name, "stat");
878                                 continue;
879                         }
880                         if (!S_ISREG(s.st_mode)) continue;
881
882                         msginfo = g_hash_table_lookup
883                                 (msg_table, GUINT_TO_POINTER(num));
884
885                         if (!msginfo) {
886                                 /* not found in the cache (uncached message) */
887                                 msginfo = mh_parse_msg(d->d_name, item);
888                                 if (!msginfo) continue;
889
890                                 if (!newlist)
891                                         last = newlist =
892                                                 g_slist_append(NULL, msginfo);
893                                 else {
894                                         last = g_slist_append(last, msginfo);
895                                         last = last->next;
896                                 }
897                                 n_newmsg++;
898                         }
899                 }
900         } else {
901                 /* discard all previous cache */
902                 while ((d = readdir(dp)) != NULL) {
903                         if (to_number(d->d_name) < 0) continue;
904                         if (stat(d->d_name, &s) < 0) {
905                                 FILE_OP_ERROR(d->d_name, "stat");
906                                 continue;
907                         }
908                         if (!S_ISREG(s.st_mode)) continue;
909
910                         msginfo = mh_parse_msg(d->d_name, item);
911                         if (!msginfo) continue;
912
913                         if (!newlist)
914                                 last = newlist = g_slist_append(NULL, msginfo);
915                         else {
916                                 last = g_slist_append(last, msginfo);
917                                 last = last->next;
918                         }
919                         n_newmsg++;
920                 }
921         }
922
923         closedir(dp);
924
925         if (n_newmsg)
926                 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
927         else
928                 debug_print(_("done.\n"));
929
930         /* sort new messages in numerical order */
931         if (newlist) {
932                 debug_print(_("\tSorting uncached messages in numerical order... "));
933                 newlist = g_slist_sort
934                         (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
935                 debug_print(_("done.\n"));
936         }
937
938         return newlist;
939 }
940
941 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
942 {
943         struct stat s;
944         MsgInfo *msginfo;
945         MsgFlags flags;
946
947         flags.perm_flags = MSG_NEW|MSG_UNREAD;
948         flags.tmp_flags = 0;
949
950         g_return_val_if_fail(item != NULL, NULL);
951         g_return_val_if_fail(file != NULL, NULL);
952
953         if (item->stype == F_QUEUE) {
954                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
955         } else if (item->stype == F_DRAFT) {
956                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
957         }
958
959         msginfo = procheader_parse(file, flags, FALSE);
960         if (!msginfo) return NULL;
961
962         msginfo->msgnum = atoi(file);
963         msginfo->folder = item;
964
965         if (stat(file, &s) < 0) {
966                 FILE_OP_ERROR(file, "stat");
967                 msginfo->size = 0;
968                 msginfo->mtime = 0;
969         } else {
970                 msginfo->size = s.st_size;
971                 msginfo->mtime = s.st_mtime;
972         }
973
974         return msginfo;
975 }
976
977 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
978 {
979         gchar *entry;
980         gboolean result;
981
982         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
983         result = is_dir_exist(entry);
984         g_free(entry);
985
986         return result;
987 }
988
989 /*
990  * check whether PATH is a Maildir style mailbox.
991  * This is the case if the 3 subdir: new, cur, tmp are existing.
992  * This functon assumes that entry is an directory
993  */
994 static gboolean mh_is_maildir(const gchar *path)
995 {
996         return mh_is_maildir_one(path, "new") &&
997                mh_is_maildir_one(path, "cur") &&
998                mh_is_maildir_one(path, "tmp");
999 }
1000
1001 static void mh_scan_tree_recursive(FolderItem *item)
1002 {
1003         DIR *dp;
1004         struct dirent *d;
1005         struct stat s;
1006         gchar *entry;
1007         gint n_msg = 0;
1008
1009         g_return_if_fail(item != NULL);
1010         g_return_if_fail(item->folder != NULL);
1011
1012         dp = opendir(item->path ? item->path : ".");
1013         if (!dp) {
1014                 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1015                 return;
1016         }
1017
1018         debug_print("scanning %s ...\n",
1019                     item->path ? item->path
1020                     : LOCAL_FOLDER(item->folder)->rootpath);
1021         if (item->folder->ui_func)
1022                 item->folder->ui_func(item->folder, item,
1023                                       item->folder->ui_func_data);
1024
1025         while ((d = readdir(dp)) != NULL) {
1026                 if (d->d_name[0] == '.') continue;
1027
1028                 if (item->path)
1029                         entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1030                                             d->d_name, NULL);
1031                 else
1032                         entry = g_strdup(d->d_name);
1033
1034                 if (stat(entry, &s) < 0) {
1035                         FILE_OP_ERROR(entry, "stat");
1036                         g_free(entry);
1037                         continue;
1038                 }
1039
1040                 if (S_ISDIR(s.st_mode)) {
1041                         FolderItem *new_item;
1042
1043                         if (mh_is_maildir(entry)) {
1044                                 g_free(entry);
1045                                 continue;
1046                         }
1047
1048                         new_item = folder_item_new(d->d_name, entry);
1049                         folder_item_append(item, new_item);
1050                         if (!item->path) {
1051                                 if (!strcmp(d->d_name, "inbox")) {
1052                                         new_item->stype = F_INBOX;
1053                                         item->folder->inbox = new_item;
1054                                 } else if (!strcmp(d->d_name, "outbox")) {
1055                                         new_item->stype = F_OUTBOX;
1056                                         item->folder->outbox = new_item;
1057                                 } else if (!strcmp(d->d_name, "draft")) {
1058                                         new_item->stype = F_DRAFT;
1059                                         item->folder->draft = new_item;
1060                                 } else if (!strcmp(d->d_name, "queue")) {
1061                                         new_item->stype = F_QUEUE;
1062                                         item->folder->queue = new_item;
1063                                 } else if (!strcmp(d->d_name, "trash")) {
1064                                         new_item->stype = F_TRASH;
1065                                         item->folder->trash = new_item;
1066                                 }
1067                         }
1068                         mh_scan_tree_recursive(new_item);
1069                 } else if (to_number(d->d_name) != -1) n_msg++;
1070
1071                 g_free(entry);
1072         }
1073
1074         closedir(dp);
1075
1076         if (item->path) {
1077                 gint new, unread, total;
1078
1079                 procmsg_get_mark_sum(item->path, &new, &unread, &total);
1080                 if (n_msg > total) {
1081                         new += n_msg - total;
1082                         unread += n_msg - total;
1083                 }
1084                 item->new = new;
1085                 item->unread = unread;
1086                 item->total = n_msg;
1087         }
1088 }
1089
1090 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1091 {
1092         FolderItem *item = node->data;
1093         gchar **paths = data;
1094         const gchar *oldpath = paths[0];
1095         const gchar *newpath = paths[1];
1096         gchar *base;
1097         gchar *new_itempath;
1098         gint oldpathlen;
1099
1100         oldpathlen = strlen(oldpath);
1101         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1102                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1103                 return TRUE;
1104         }
1105
1106         base = item->path + oldpathlen;
1107         while (*base == G_DIR_SEPARATOR) base++;
1108         if (*base == '\0')
1109                 new_itempath = g_strdup(newpath);
1110         else
1111                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1112                                            NULL);
1113         g_free(item->path);
1114         item->path = new_itempath;
1115
1116         return FALSE;
1117 }