0.9.3claws57
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 void mh_folder_init(Folder * folder,
47                            const gchar * name, const gchar * path);
48
49 static Folder *mh_folder_new(const gchar * name, const gchar * path);
50 static void mh_folder_destroy(Folder * folder);
51 static gchar *mh_fetch_msg(Folder * folder, FolderItem * item, gint num);
52 static MsgInfo *mh_get_msginfo(Folder * folder,
53                                FolderItem * item, gint num);
54 static gint mh_add_msg(Folder * folder,
55                        FolderItem * dest,
56                        const gchar * file,
57                        MsgFlags * flags);
58 static gint mh_add_msgs(Folder * folder,
59                  FolderItem * dest, GSList * file_list, MsgNumberList **newnum_list);
60 static gint mh_copy_msg(Folder * folder,
61                         FolderItem * dest, MsgInfo * msginfo);
62 static gint mh_remove_msg(Folder * folder, FolderItem * item, gint num);
63 static gint mh_remove_all_msg(Folder * folder, FolderItem * item);
64 static gboolean mh_is_msg_changed(Folder * folder,
65                                   FolderItem * item, MsgInfo * msginfo);
66
67 static gint mh_get_num_list(Folder * folder,
68                             FolderItem * item, GSList ** list);
69 static void mh_scan_tree(Folder * folder);
70
71 static gint mh_create_tree(Folder * folder);
72 static FolderItem *mh_create_folder(Folder * folder,
73                                     FolderItem * parent,
74                                     const gchar * name);
75 static gint mh_rename_folder(Folder * folder,
76                              FolderItem * item, const gchar * name);
77 static gint mh_remove_folder(Folder * folder, FolderItem * item);
78
79 static gchar *mh_get_new_msg_filename(FolderItem * dest);
80
81 static MsgInfo *mh_parse_msg(const gchar * file, FolderItem * item);
82 static void mh_scan_tree_recursive(FolderItem * item);
83
84 static gboolean mh_rename_folder_func(GNode * node, gpointer data);
85 static gchar *mh_item_get_path(Folder *folder, FolderItem *item);
86
87 FolderClass mh_class =
88 {
89         F_MH,
90         "mh",
91         "MH",
92
93         /* Folder functions */
94         mh_folder_new,
95         mh_folder_destroy,
96         mh_scan_tree,
97         mh_create_tree,
98
99         /* FolderItem functions */
100         NULL,
101         NULL,
102         mh_item_get_path,
103         mh_create_folder,
104         mh_rename_folder,
105         mh_remove_folder,
106         NULL,
107         mh_get_num_list,
108         NULL,
109         NULL,
110         NULL,
111         NULL,
112         
113         /* Message functions */
114         mh_get_msginfo,
115         NULL,
116         mh_fetch_msg,
117         mh_add_msg,
118         mh_add_msgs,
119         mh_copy_msg,
120         NULL,
121         mh_remove_msg,
122         mh_remove_all_msg,
123         mh_is_msg_changed,
124         NULL,
125 };
126
127 FolderClass *mh_get_class(void)
128 {
129         return &mh_class;
130 }
131
132 Folder *mh_folder_new(const gchar *name, const gchar *path)
133 {
134         Folder *folder;
135
136         folder = (Folder *)g_new0(MHFolder, 1);
137         folder->klass = &mh_class;
138         mh_folder_init(folder, name, path);
139
140         return folder;
141 }
142
143 static void mh_folder_destroy(Folder *folder)
144 {
145         folder_local_folder_destroy(LOCAL_FOLDER(folder));
146 }
147
148 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
149 {
150         folder_local_folder_init(folder, name, path);
151
152 }
153
154 void mh_get_last_num(Folder *folder, FolderItem *item)
155 {
156         gchar *path;
157         DIR *dp;
158         struct dirent *d;
159         struct stat s;
160         gint max = 0;
161         gint num;
162
163         g_return_if_fail(item != NULL);
164
165         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
166
167         path = folder_item_get_path(item);
168         g_return_if_fail(path != NULL);
169         if (change_dir(path) < 0) {
170                 g_free(path);
171                 return;
172         }
173         g_free(path);
174
175         if ((dp = opendir(".")) == NULL) {
176                 FILE_OP_ERROR(item->path, "opendir");
177                 return;
178         }
179
180         while ((d = readdir(dp)) != NULL) {
181                 if ((num = to_number(d->d_name)) >= 0 &&
182                     stat(d->d_name, &s) == 0 &&
183                     S_ISREG(s.st_mode)) {
184                         if (max < num)
185                                 max = num;
186                 }
187         }
188         closedir(dp);
189
190         debug_print("Last number in dir %s = %d\n", item->path, max);
191         item->last_num = max;
192 }
193
194 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list)
195 {
196
197         gchar *path;
198         DIR *dp;
199         struct dirent *d;
200         struct stat s;
201         gint num, nummsgs = 0;
202
203         g_return_val_if_fail(item != NULL, -1);
204
205         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
206
207         path = folder_item_get_path(item);
208         g_return_val_if_fail(path != NULL, -1);
209         if (change_dir(path) < 0) {
210                 g_free(path);
211                 return -1;
212         }
213         g_free(path);
214
215         if ((dp = opendir(".")) == NULL) {
216                 FILE_OP_ERROR(item->path, "opendir");
217                 return -1;
218         }
219
220         while ((d = readdir(dp)) != NULL) {
221                 if ((num = to_number(d->d_name)) >= 0 &&
222                     stat(d->d_name, &s) == 0 &&
223                     S_ISREG(s.st_mode)) {
224                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
225                     nummsgs++;
226                 }
227         }
228         closedir(dp);
229
230         return nummsgs;
231 }
232
233 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
234 {
235         gchar *path;
236         gchar *file;
237
238         g_return_val_if_fail(item != NULL, NULL);
239         g_return_val_if_fail(num > 0, NULL);
240
241         path = folder_item_get_path(item);
242         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
243         g_free(path);
244         if (!is_file_exist(file)) {
245                 g_free(file);
246                 return NULL;
247         }
248
249         return file;
250 }
251
252 MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
253 {
254         MsgInfo *msginfo;
255         gchar *file;
256
257         g_return_val_if_fail(item != NULL, NULL);
258         g_return_val_if_fail(num > 0, NULL);
259
260         file = mh_fetch_msg(folder, item, num);
261         if (!file) return NULL;
262
263         msginfo = mh_parse_msg(file, item);
264         if (msginfo)
265                 msginfo->msgnum = num;
266
267         g_free(file);
268
269         return msginfo;
270 }
271
272 gchar *mh_get_new_msg_filename(FolderItem *dest)
273 {
274         gchar *destfile;
275         gchar *destpath;
276
277         destpath = folder_item_get_path(dest);
278         g_return_val_if_fail(destpath != NULL, NULL);
279
280         if (!is_dir_exist(destpath))
281                 make_dir_hier(destpath);
282
283         for (;;) {
284                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
285                                            dest->last_num + 1);
286                 if (is_file_entry_exist(destfile)) {
287                         dest->last_num++;
288                         g_free(destfile);
289                 } else
290                         break;
291         }
292
293         g_free(destpath);
294
295         return destfile;
296 }
297
298 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
299 { \
300         MsgInfo newmsginfo; \
301  \
302         newmsginfo.msgnum = dest->last_num; \
303         newmsginfo.flags = msginfo->flags; \
304         if (dest->stype == F_OUTBOX || \
305             dest->stype == F_QUEUE  || \
306             dest->stype == F_DRAFT  || \
307             dest->stype == F_TRASH) \
308                 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
309                                      MSG_NEW|MSG_UNREAD|MSG_DELETED); \
310  \
311         procmsg_write_flags(&newmsginfo, fp); \
312 }
313
314 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
315 {
316         gint ret;
317         GSList file_list;
318         MsgFileInfo fileinfo;
319         MsgNumberList *newnum_list = NULL;
320
321         g_return_val_if_fail(file != NULL, -1);
322
323         fileinfo.file = (gchar *)file;
324         fileinfo.flags = flags;
325         file_list.data = &fileinfo;
326         file_list.next = NULL;
327
328         ret = mh_add_msgs(folder, dest, &file_list, &newnum_list);
329         g_slist_free(newnum_list);
330         return ret;
331
332  
333 gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
334                  MsgNumberList **newnum_list) 
335
336         gchar *destfile;
337         GSList *cur;
338         MsgFileInfo *fileinfo;
339
340         g_return_val_if_fail(dest != NULL, -1);
341         g_return_val_if_fail(file_list != NULL, -1);
342
343         if (dest->last_num < 0) {
344                 mh_get_last_num(folder, dest);
345                 if (dest->last_num < 0) return -1;
346         }
347
348         for (cur = file_list; cur != NULL; cur = cur->next) {
349                 fileinfo = (MsgFileInfo *)cur->data;
350
351                 destfile = mh_get_new_msg_filename(dest);
352                 if (destfile == NULL) return -1;
353
354                 if (link(fileinfo->file, destfile) < 0) {
355                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
356                                 g_warning(_("can't copy message %s to %s\n"),
357                                           fileinfo->file, destfile);
358                                 g_free(destfile);
359                                 return -1;
360                         }
361                 }
362                 *newnum_list = g_slist_append(*newnum_list, GINT_TO_POINTER(dest->last_num + 1));
363
364                 g_free(destfile);
365                 dest->last_num++;
366         }
367
368         return dest->last_num;
369 }
370
371 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
372 {
373         gchar *srcfile;
374         gchar *destfile;
375         gint filemode = 0;
376         FolderItemPrefs *prefs;
377
378         g_return_val_if_fail(dest != NULL, -1);
379         g_return_val_if_fail(msginfo != NULL, -1);
380
381         if (msginfo->folder == dest) {
382                 g_warning("the src folder is identical to the dest.\n");
383                 return -1;
384         }
385
386         if (dest->last_num < 0) {
387                 mh_get_last_num(folder, dest);
388                 if (dest->last_num < 0) return -1;
389         }
390
391         prefs = dest->prefs;
392
393         srcfile = procmsg_get_message_file(msginfo);
394         destfile = mh_get_new_msg_filename(dest);
395         if (!destfile) {
396                 g_free(srcfile);
397                 return -1;
398         }
399         
400         debug_print("Copying message %s%c%d to %s ...\n",
401                     msginfo->folder->path, G_DIR_SEPARATOR,
402                     msginfo->msgnum, dest->path);
403         
404
405         if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
406         &&  dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
407                 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
408                         g_free(srcfile);
409                         g_free(destfile);
410                         return -1;
411                 }
412         } else if (copy_file(srcfile, destfile, TRUE) < 0) {
413                 FILE_OP_ERROR(srcfile, "copy");
414                 g_free(srcfile);
415                 g_free(destfile);
416                 return -1;
417         }
418
419
420         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
421                 if (chmod(destfile, prefs->folder_chmod) < 0)
422                         FILE_OP_ERROR(destfile, "chmod");
423
424                 /* for mark file */
425                 filemode = prefs->folder_chmod;
426                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
427                 if (filemode & S_IROTH) filemode |= S_IWOTH;
428         }
429
430         g_free(srcfile);
431         g_free(destfile);
432         dest->last_num++;
433
434         return dest->last_num;
435 }
436
437 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
438 {
439         gchar *file;
440
441         g_return_val_if_fail(item != NULL, -1);
442
443         file = mh_fetch_msg(folder, item, num);
444         g_return_val_if_fail(file != NULL, -1);
445
446         if (unlink(file) < 0) {
447                 FILE_OP_ERROR(file, "unlink");
448                 g_free(file);
449                 return -1;
450         }
451
452         g_free(file);
453         return 0;
454 }
455
456 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
457 {
458         gchar *path;
459         gint val;
460
461         g_return_val_if_fail(item != NULL, -1);
462
463         path = folder_item_get_path(item);
464         g_return_val_if_fail(path != NULL, -1);
465         val = remove_all_numbered_files(path);
466         g_free(path);
467
468         return val;
469 }
470
471 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
472 {
473         struct stat s;
474
475         if (stat(itos(msginfo->msgnum), &s) < 0 ||
476             msginfo->size  != s.st_size ||
477             msginfo->mtime != s.st_mtime)
478                 return TRUE;
479
480         return FALSE;
481 }
482
483 void mh_scan_tree(Folder *folder)
484 {
485         FolderItem *item;
486         gchar *rootpath;
487
488         g_return_if_fail(folder != NULL);
489
490         item = folder_item_new(folder, folder->name, NULL);
491         item->folder = folder;
492         folder->node = g_node_new(item);
493
494         rootpath = folder_item_get_path(item);
495         if (change_dir(rootpath) < 0) {
496                 g_free(rootpath);
497                 return;
498         }
499         g_free(rootpath);
500
501         mh_create_tree(folder);
502         mh_scan_tree_recursive(item);
503 }
504
505 #define MAKE_DIR_IF_NOT_EXIST(dir) \
506 { \
507         if (!is_dir_exist(dir)) { \
508                 if (is_file_exist(dir)) { \
509                         g_warning("File `%s' already exists.\n" \
510                                     "Can't create folder.", dir); \
511                         return -1; \
512                 } \
513                 if (make_dir(dir) < 0) \
514                         return -1; \
515         } \
516 }
517
518 gint mh_create_tree(Folder *folder)
519 {
520         gchar *rootpath;
521
522         g_return_val_if_fail(folder != NULL, -1);
523
524         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
525         rootpath = LOCAL_FOLDER(folder)->rootpath;
526         MAKE_DIR_IF_NOT_EXIST(rootpath);
527         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
528         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
529         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
530         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
531         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
532         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
533
534         return 0;
535 }
536
537 #undef MAKE_DIR_IF_NOT_EXIST
538
539 gchar *mh_item_get_path(Folder *folder, FolderItem *item)
540 {
541         gchar *folder_path, *path;
542
543         g_return_val_if_fail(folder != NULL, NULL);
544         g_return_val_if_fail(item != NULL, NULL);
545
546         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
547         g_return_val_if_fail(folder_path != NULL, NULL);
548
549         if (folder_path[0] == G_DIR_SEPARATOR) {
550                 if (item->path)
551                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
552                                            item->path, NULL);
553                 else
554                         path = g_strdup(folder_path);
555         } else {
556                 if (item->path)
557                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
558                                            folder_path, G_DIR_SEPARATOR_S,
559                                            item->path, NULL);
560                 else
561                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
562                                            folder_path, NULL);
563         }
564         g_free(folder_path);
565
566         return path;
567 }
568
569 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
570                              const gchar *name)
571 {
572         gchar *path;
573         gchar *fullpath;
574         FolderItem *new_item;
575         gchar *mh_sequences_filename;
576         FILE *mh_sequences_file;
577
578         g_return_val_if_fail(folder != NULL, NULL);
579         g_return_val_if_fail(parent != NULL, NULL);
580         g_return_val_if_fail(name != NULL, NULL);
581
582         path = folder_item_get_path(parent);
583         if (!is_dir_exist(path)) 
584                 if (make_dir_hier(path) != 0)
585                         return NULL;
586                 
587         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
588         g_free(path);
589
590         if (make_dir(fullpath) < 0) {
591                 g_free(fullpath);
592                 return NULL;
593         }
594
595         g_free(fullpath);
596
597         if (parent->path)
598                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
599                                    NULL);
600         else
601                 path = g_strdup(name);
602         new_item = folder_item_new(folder, name, path);
603         folder_item_append(parent, new_item);
604
605         g_free(path);
606
607         path = folder_item_get_path(new_item);
608         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
609                                             ".mh_sequences", NULL);
610         if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
611                 fclose(mh_sequences_file);
612         }
613         g_free(mh_sequences_filename);
614         g_free(path);
615
616         return new_item;
617 }
618
619 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
620 {
621         gchar *oldpath;
622         gchar *dirname;
623         gchar *newpath;
624         GNode *node;
625         gchar *paths[2];
626
627         g_return_val_if_fail(folder != NULL, -1);
628         g_return_val_if_fail(item != NULL, -1);
629         g_return_val_if_fail(item->path != NULL, -1);
630         g_return_val_if_fail(name != NULL, -1);
631
632         oldpath = folder_item_get_path(item);
633         if (!is_dir_exist(oldpath))
634                 make_dir_hier(oldpath);
635
636         dirname = g_dirname(oldpath);
637         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
638         g_free(dirname);
639
640         if (rename(oldpath, newpath) < 0) {
641                 FILE_OP_ERROR(oldpath, "rename");
642                 g_free(oldpath);
643                 g_free(newpath);
644                 return -1;
645         }
646
647         g_free(oldpath);
648         g_free(newpath);
649
650         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
651                 dirname = g_dirname(item->path);
652                 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
653                 g_free(dirname);
654         } else
655                 newpath = g_strdup(name);
656
657         g_free(item->name);
658         item->name = g_strdup(name);
659
660         node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
661                            item);
662         paths[0] = g_strdup(item->path);
663         paths[1] = newpath;
664         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
665                         mh_rename_folder_func, paths);
666
667         g_free(paths[0]);
668         g_free(paths[1]);
669         return 0;
670 }
671
672 gint mh_remove_folder(Folder *folder, FolderItem *item)
673 {
674         gchar *path;
675
676         g_return_val_if_fail(folder != NULL, -1);
677         g_return_val_if_fail(item != NULL, -1);
678         g_return_val_if_fail(item->path != NULL, -1);
679
680         path = folder_item_get_path(item);
681         if (remove_dir_recursive(path) < 0) {
682                 g_warning("can't remove directory `%s'\n", path);
683                 g_free(path);
684                 return -1;
685         }
686
687         g_free(path);
688         folder_item_remove(item);
689         return 0;
690 }
691
692 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
693 {
694         struct stat s;
695         MsgInfo *msginfo;
696         MsgFlags flags;
697
698         flags.perm_flags = MSG_NEW|MSG_UNREAD;
699         flags.tmp_flags = 0;
700
701         g_return_val_if_fail(item != NULL, NULL);
702         g_return_val_if_fail(file != NULL, NULL);
703
704         if (item->stype == F_QUEUE) {
705                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
706         } else if (item->stype == F_DRAFT) {
707                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
708         }
709
710         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
711         if (!msginfo) return NULL;
712
713         msginfo->msgnum = atoi(file);
714         msginfo->folder = item;
715
716         if (stat(file, &s) < 0) {
717                 FILE_OP_ERROR(file, "stat");
718                 msginfo->size = 0;
719                 msginfo->mtime = 0;
720         } else {
721                 msginfo->size = s.st_size;
722                 msginfo->mtime = s.st_mtime;
723         }
724
725         return msginfo;
726 }
727
728 #if 0
729 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
730 {
731         gchar *entry;
732         gboolean result;
733
734         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
735         result = is_dir_exist(entry);
736         g_free(entry);
737
738         return result;
739 }
740
741 /*
742  * check whether PATH is a Maildir style mailbox.
743  * This is the case if the 3 subdir: new, cur, tmp are existing.
744  * This functon assumes that entry is an directory
745  */
746 static gboolean mh_is_maildir(const gchar *path)
747 {
748         return mh_is_maildir_one(path, "new") &&
749                mh_is_maildir_one(path, "cur") &&
750                mh_is_maildir_one(path, "tmp");
751 }
752 #endif
753
754 static void mh_scan_tree_recursive(FolderItem *item)
755 {
756         DIR *dp;
757         struct dirent *d;
758         struct stat s;
759         gchar *entry;
760         gint n_msg = 0;
761
762         g_return_if_fail(item != NULL);
763         g_return_if_fail(item->folder != NULL);
764
765         dp = opendir(item->path ? item->path : ".");
766         if (!dp) {
767                 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
768                 return;
769         }
770
771         debug_print("scanning %s ...\n",
772                     item->path ? item->path
773                     : LOCAL_FOLDER(item->folder)->rootpath);
774         if (item->folder->ui_func)
775                 item->folder->ui_func(item->folder, item,
776                                       item->folder->ui_func_data);
777
778         while ((d = readdir(dp)) != NULL) {
779                 if (d->d_name[0] == '.') continue;
780
781                 if (item->path)
782                         entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
783                                             d->d_name, NULL);
784                 else
785                         entry = g_strdup(d->d_name);
786
787                 if (stat(entry, &s) < 0) {
788                         FILE_OP_ERROR(entry, "stat");
789                         g_free(entry);
790                         continue;
791                 }
792
793                 if (S_ISDIR(s.st_mode)) {
794                         FolderItem *new_item;
795
796 #if 0
797                         if (mh_is_maildir(entry)) {
798                                 g_free(entry);
799                                 continue;
800                         }
801 #endif
802
803                         new_item = folder_item_new(item->folder, d->d_name, entry);
804                         folder_item_append(item, new_item);
805                         if (!item->path) {
806                                 if (!strcmp(d->d_name, INBOX_DIR)) {
807                                         new_item->stype = F_INBOX;
808                                         item->folder->inbox = new_item;
809                                 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
810                                         new_item->stype = F_OUTBOX;
811                                         item->folder->outbox = new_item;
812                                 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
813                                         new_item->stype = F_DRAFT;
814                                         item->folder->draft = new_item;
815                                 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
816                                         new_item->stype = F_QUEUE;
817                                         item->folder->queue = new_item;
818                                 } else if (!strcmp(d->d_name, TRASH_DIR)) {
819                                         new_item->stype = F_TRASH;
820                                         item->folder->trash = new_item;
821                                 }
822                         }
823                         mh_scan_tree_recursive(new_item);
824                 } else if (to_number(d->d_name) != -1) n_msg++;
825
826                 g_free(entry);
827         }
828
829         closedir(dp);
830
831 /*
832         if (item->path) {
833                 gint new, unread, total, min, max;
834
835                 procmsg_get_mark_sum(item->path, &new, &unread, &total,
836                                      &min, &max, 0);
837                 if (n_msg > total) {
838                         new += n_msg - total;
839                         unread += n_msg - total;
840                 }
841                 item->new = new;
842                 item->unread = unread;
843                 item->total = n_msg;
844         }
845 */
846 }
847
848 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
849 {
850         FolderItem *item = node->data;
851         gchar **paths = data;
852         const gchar *oldpath = paths[0];
853         const gchar *newpath = paths[1];
854         gchar *base;
855         gchar *new_itempath;
856         gint oldpathlen;
857
858         oldpathlen = strlen(oldpath);
859         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
860                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
861                 return TRUE;
862         }
863
864         base = item->path + oldpathlen;
865         while (*base == G_DIR_SEPARATOR) base++;
866         if (*base == '\0')
867                 new_itempath = g_strdup(newpath);
868         else
869                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
870                                            NULL);
871         g_free(item->path);
872         item->path = new_itempath;
873
874         return FALSE;
875 }