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