2005-12-23 [cleroy] 1.9.100cvs105
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <glib/gi18n.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #undef MEASURE_TIME
35
36 #ifdef MEASURE_TIME
37 #  include <sys/time.h>
38 #endif
39
40 #include "folder.h"
41 #include "mh.h"
42 #include "procmsg.h"
43 #include "procheader.h"
44 #include "utils.h"
45 #include "codeconv.h"
46
47 /* Define possible missing constants for Windows. */
48 #ifdef G_OS_WIN32
49 # ifndef S_IRGRP
50 # define S_IRGRP 0
51 # define S_IWGRP 0
52 # endif
53 # ifndef S_IROTH
54 # define S_IROTH 0
55 # define S_IWOTH 0
56 # endif
57 #endif
58
59
60 static void     mh_folder_init          (Folder         *folder,
61                                          const gchar    *name,
62                                          const gchar    *path);
63
64 static Folder   *mh_folder_new          (const gchar    *name,
65                                          const gchar    *path);
66 static void     mh_folder_destroy       (Folder         *folder);
67 static gchar   *mh_fetch_msg            (Folder         *folder,
68                                          FolderItem     *item,
69                                          gint            num);
70 static MsgInfo *mh_get_msginfo          (Folder         *folder,
71                                          FolderItem     *item,
72                                          gint            num);
73 static gint     mh_add_msg              (Folder         *folder,
74                                          FolderItem     *dest,
75                                          const gchar    *file,
76                                          MsgFlags       *flags);
77 static gint     mh_add_msgs             (Folder         *folder,
78                                          FolderItem     *dest,
79                                          GSList         *file_list,
80                                          GRelation      *relation);
81 static gint     mh_copy_msg             (Folder         *folder,
82                                          FolderItem     *dest,
83                                          MsgInfo        *msginfo);
84 static gint     mh_remove_msg           (Folder         *folder,
85                                          FolderItem     *item,
86                                          gint            num);
87 static gint     mh_remove_all_msg       (Folder         *folder,
88                                          FolderItem     *item);
89 static gboolean mh_is_msg_changed       (Folder         *folder,
90                                          FolderItem     *item,
91                                          MsgInfo        *msginfo);
92
93 static gint     mh_get_num_list         (Folder         *folder,
94                                          FolderItem     *item, 
95                                          GSList         **list, 
96                                          gboolean       *old_uids_valid);
97 static gint     mh_scan_tree            (Folder         *folder);
98
99 static gint    mh_create_tree           (Folder         *folder);
100 static FolderItem *mh_create_folder     (Folder         *folder,
101                                          FolderItem     *parent,
102                                          const gchar    *name);
103 static gint    mh_rename_folder         (Folder         *folder,
104                                          FolderItem     *item,
105                                          const gchar    *name);
106 static gint    mh_remove_folder         (Folder         *folder,
107                                          FolderItem     *item);
108
109 static gchar   *mh_get_new_msg_filename         (FolderItem     *dest);
110
111 static MsgInfo *mh_parse_msg                    (const gchar    *file,
112                                                  FolderItem     *item);
113 static void     mh_remove_missing_folder_items  (Folder         *folder);
114 static gchar    *mh_filename_from_utf8          (const gchar    *path);
115 static gchar    *mh_filename_to_utf8            (const gchar    *path);
116 static void     mh_scan_tree_recursive          (FolderItem     *item);
117
118 static gboolean mh_rename_folder_func           (GNode          *node,
119                                                  gpointer        data);
120 static gchar   *mh_item_get_path                (Folder *folder, 
121                                                  FolderItem *item);
122
123 static gboolean mh_scan_required        (Folder         *folder,
124                                          FolderItem     *item);
125
126 static FolderClass mh_class;
127
128 FolderClass *mh_get_class(void)
129 {
130         if (mh_class.idstr == NULL) {
131                 mh_class.type = F_MH;
132                 mh_class.idstr = "mh";
133                 mh_class.uistr = "MH";
134                 
135                 /* Folder functions */
136                 mh_class.new_folder = mh_folder_new;
137                 mh_class.destroy_folder = mh_folder_destroy;
138                 mh_class.set_xml = folder_local_set_xml;
139                 mh_class.get_xml = folder_local_get_xml;
140                 mh_class.scan_tree = mh_scan_tree;
141                 mh_class.create_tree = mh_create_tree;
142
143                 /* FolderItem functions */
144                 mh_class.item_get_path = mh_item_get_path;
145                 mh_class.create_folder = mh_create_folder;
146                 mh_class.rename_folder = mh_rename_folder;
147                 mh_class.remove_folder = mh_remove_folder;
148                 mh_class.get_num_list = mh_get_num_list;
149                 mh_class.scan_required = mh_scan_required;
150                 
151                 /* Message functions */
152                 mh_class.get_msginfo = mh_get_msginfo;
153                 mh_class.fetch_msg = mh_fetch_msg;
154                 mh_class.add_msg = mh_add_msg;
155                 mh_class.add_msgs = mh_add_msgs;
156                 mh_class.copy_msg = mh_copy_msg;
157                 mh_class.remove_msg = mh_remove_msg;
158                 mh_class.remove_all_msg = mh_remove_all_msg;
159                 mh_class.is_msg_changed = mh_is_msg_changed;
160         }
161
162         return &mh_class;
163 }
164
165 static Folder *mh_folder_new(const gchar *name, const gchar *path)
166 {
167         Folder *folder;
168
169         folder = (Folder *)g_new0(MHFolder, 1);
170         folder->klass = &mh_class;
171         mh_folder_init(folder, name, path);
172
173         return folder;
174 }
175
176 static void mh_folder_destroy(Folder *folder)
177 {
178         folder_local_folder_destroy(LOCAL_FOLDER(folder));
179 }
180
181 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
182 {
183         folder_local_folder_init(folder, name, path);
184
185 }
186
187 gboolean mh_scan_required(Folder *folder, FolderItem *item)
188 {
189         gchar *path;
190         struct stat s;
191
192         path = folder_item_get_path(item);
193         g_return_val_if_fail(path != NULL, FALSE);
194
195         if (stat(path, &s) < 0) {
196                 FILE_OP_ERROR(path, "stat");
197                 g_free(path);
198                 return FALSE;
199         }
200
201         if ((s.st_mtime > item->mtime) &&
202                 (s.st_mtime - 3600 != item->mtime)) {
203                 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
204                             path,
205                             (long int) s.st_mtime,
206                             (long int) item->mtime);
207                 g_free(path);
208                 return TRUE;
209         }
210
211         debug_print("MH scan not required: %s (%ld <= %ld)\n",
212                     path,
213                     (long int) s.st_mtime,
214                     (long int) item->mtime);
215         g_free(path);
216         return FALSE;
217 }
218
219 void mh_get_last_num(Folder *folder, FolderItem *item)
220 {
221         gchar *path;
222         DIR *dp;
223         struct dirent *d;
224         gint max = 0;
225         gint num;
226
227         g_return_if_fail(item != NULL);
228
229         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
230
231         path = folder_item_get_path(item);
232         g_return_if_fail(path != NULL);
233         if (change_dir(path) < 0) {
234                 g_free(path);
235                 return;
236         }
237         g_free(path);
238
239         if ((dp = opendir(".")) == NULL) {
240                 FILE_OP_ERROR(item->path, "opendir");
241                 return;
242         }
243
244         while ((d = readdir(dp)) != NULL) {
245                 if ((num = to_number(d->d_name)) > 0 &&
246                     dirent_is_regular_file(d)) {
247                         if (max < num)
248                                 max = num;
249                 }
250         }
251         closedir(dp);
252
253         debug_print("Last number in dir %s = %d\n", item->path, max);
254         item->last_num = max;
255 }
256
257 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
258 {
259
260         gchar *path;
261         DIR *dp;
262         struct dirent *d;
263         gint num, nummsgs = 0;
264
265         g_return_val_if_fail(item != NULL, -1);
266
267         debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
268
269         *old_uids_valid = TRUE;
270
271         path = folder_item_get_path(item);
272         g_return_val_if_fail(path != NULL, -1);
273         if (change_dir(path) < 0) {
274                 g_free(path);
275                 return -1;
276         }
277         g_free(path);
278
279         if ((dp = opendir(".")) == NULL) {
280                 FILE_OP_ERROR(item->path, "opendir");
281                 return -1;
282         }
283
284         while ((d = readdir(dp)) != NULL) {
285                 if ((num = to_number(d->d_name)) > 0) {
286                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
287                         nummsgs++;
288                 }
289         }
290         closedir(dp);
291
292         item->mtime = time(NULL);
293         return nummsgs;
294 }
295
296 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
297 {
298         gchar *path;
299         gchar *file;
300
301         g_return_val_if_fail(item != NULL, NULL);
302         g_return_val_if_fail(num > 0, NULL);
303
304         path = folder_item_get_path(item);
305         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
306
307         if (!is_file_exist(file)) {
308                 g_free(file);
309                 g_free(path);
310                 return NULL;
311         }
312         g_free(path);
313         return file;
314 }
315
316 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
317 {
318         MsgInfo *msginfo;
319         gchar *file;
320
321         g_return_val_if_fail(item != NULL, NULL);
322         g_return_val_if_fail(num > 0, NULL);
323
324         file = mh_fetch_msg(folder, item, num);
325         if (!file) return NULL;
326
327         msginfo = mh_parse_msg(file, item);
328         if (msginfo)
329                 msginfo->msgnum = num;
330
331         g_free(file);
332
333         return msginfo;
334 }
335
336 static gchar *mh_get_new_msg_filename(FolderItem *dest)
337 {
338         gchar *destfile;
339         gchar *destpath;
340
341         destpath = folder_item_get_path(dest);
342         g_return_val_if_fail(destpath != NULL, NULL);
343
344         if (!is_dir_exist(destpath))
345                 make_dir_hier(destpath);
346
347         for (;;) {
348                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
349                                            dest->last_num + 1);
350                 if (is_file_entry_exist(destfile)) {
351                         dest->last_num++;
352                         g_free(destfile);
353                 } else
354                         break;
355         }
356
357         g_free(destpath);
358
359         return destfile;
360 }
361
362 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
363 {
364         gint ret;
365         GSList file_list;
366         MsgFileInfo fileinfo;
367
368         g_return_val_if_fail(file != NULL, -1);
369
370         fileinfo.msginfo = NULL;
371         fileinfo.file = (gchar *)file;
372         fileinfo.flags = flags;
373         file_list.data = &fileinfo;
374         file_list.next = NULL;
375
376         ret = mh_add_msgs(folder, dest, &file_list, NULL);
377         return ret;
378
379  
380 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
381                  GRelation *relation)
382
383         gchar *destfile;
384         GSList *cur;
385         MsgFileInfo *fileinfo;
386
387         g_return_val_if_fail(dest != NULL, -1);
388         g_return_val_if_fail(file_list != NULL, -1);
389
390         if (dest->last_num < 0) {
391                 mh_get_last_num(folder, dest);
392                 if (dest->last_num < 0) return -1;
393         }
394
395         for (cur = file_list; cur != NULL; cur = cur->next) {
396                 fileinfo = (MsgFileInfo *)cur->data;
397
398                 destfile = mh_get_new_msg_filename(dest);
399                 if (destfile == NULL) return -1;
400
401 #ifdef G_OS_UNIX
402                 if (link(fileinfo->file, destfile) < 0) {
403 #endif
404                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
405                                 g_warning(_("can't copy message %s to %s\n"),
406                                           fileinfo->file, destfile);
407                                 g_free(destfile);
408                                 return -1;
409                         }
410 #ifdef G_OS_UNIX
411                 }
412 #endif
413
414                 if (relation != NULL)
415                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
416                 g_free(destfile);
417                 dest->last_num++;
418         }
419
420         return dest->last_num;
421 }
422
423 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
424 {
425         gboolean dest_need_scan = FALSE;
426         gchar *srcfile;
427         gchar *destfile;
428         gint filemode = 0;
429         FolderItemPrefs *prefs;
430
431         g_return_val_if_fail(dest != NULL, -1);
432         g_return_val_if_fail(msginfo != NULL, -1);
433
434         if (msginfo->folder == dest) {
435                 g_warning("the src folder is identical to the dest.\n");
436                 return -1;
437         }
438
439         if (dest->last_num < 0) {
440                 mh_get_last_num(folder, dest);
441                 if (dest->last_num < 0) return -1;
442         }
443
444         prefs = dest->prefs;
445
446         srcfile = procmsg_get_message_file(msginfo);
447         destfile = mh_get_new_msg_filename(dest);
448         if (!destfile) {
449                 g_free(srcfile);
450                 return -1;
451         }
452         
453         debug_print("Copying message %s%c%d to %s ...\n",
454                     msginfo->folder->path, G_DIR_SEPARATOR,
455                     msginfo->msgnum, dest->path);
456         
457
458         if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
459         && !folder_has_parent_of_type(dest, F_QUEUE)
460         && !folder_has_parent_of_type(dest, F_DRAFT)) {
461                 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
462                         g_free(srcfile);
463                         g_free(destfile);
464                         return -1;
465                 }
466         } else if (!(MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
467         && (folder_has_parent_of_type(dest, F_QUEUE)
468          || folder_has_parent_of_type(dest, F_DRAFT))) {
469                 g_free(srcfile);
470                 g_free(destfile);
471                 return -1;
472         } else if (copy_file(srcfile, destfile, TRUE) < 0) {
473                 FILE_OP_ERROR(srcfile, "copy");
474                 g_free(srcfile);
475                 g_free(destfile);
476                 return -1;
477         }
478
479         dest_need_scan = mh_scan_required(dest->folder, dest);
480         if (!dest_need_scan)
481                 dest->mtime = time(NULL);
482
483
484         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
485                 if (chmod(destfile, prefs->folder_chmod) < 0)
486                         FILE_OP_ERROR(destfile, "chmod");
487
488                 /* for mark file */
489                 filemode = prefs->folder_chmod;
490                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
491                 if (filemode & S_IROTH) filemode |= S_IWOTH;
492         }
493
494         g_free(srcfile);
495         g_free(destfile);
496         dest->last_num++;
497
498         return dest->last_num;
499 }
500
501 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
502 {
503         gboolean need_scan = FALSE;
504         gchar *file;
505
506         g_return_val_if_fail(item != NULL, -1);
507
508         file = mh_fetch_msg(folder, item, num);
509         g_return_val_if_fail(file != NULL, -1);
510
511         need_scan = mh_scan_required(folder, item);
512
513         if (g_unlink(file) < 0) {
514                 FILE_OP_ERROR(file, "unlink");
515                 g_free(file);
516                 return -1;
517         }
518
519         if (!need_scan)
520                 item->mtime = time(NULL);
521
522         g_free(file);
523         return 0;
524 }
525
526 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
527 {
528         gchar *path;
529         gint val;
530
531         g_return_val_if_fail(item != NULL, -1);
532
533         path = folder_item_get_path(item);
534         g_return_val_if_fail(path != NULL, -1);
535         val = remove_all_numbered_files(path);
536         g_free(path);
537
538         return val;
539 }
540
541 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
542                                   MsgInfo *msginfo)
543 {
544         struct stat s;
545
546         if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
547             msginfo->size  != s.st_size || (
548                 (msginfo->mtime - s.st_mtime != 0) &&
549                 (msginfo->mtime - s.st_mtime != 3600) &&
550                 (msginfo->mtime - s.st_mtime != -3600)))
551                 return TRUE;
552
553         return FALSE;
554 }
555
556 static gint mh_scan_tree(Folder *folder)
557 {
558         FolderItem *item;
559         gchar *rootpath;
560
561         g_return_val_if_fail(folder != NULL, -1);
562
563         if (!folder->node) {
564                 item = folder_item_new(folder, folder->name, NULL);
565                 item->folder = folder;
566                 folder->node = item->node = g_node_new(item);
567         } else
568                 item = FOLDER_ITEM(folder->node->data);
569
570         rootpath = folder_item_get_path(item);
571         if (change_dir(rootpath) < 0) {
572                 g_free(rootpath);
573                 return -1;
574         }
575         g_free(rootpath);
576
577         mh_create_tree(folder);
578         mh_remove_missing_folder_items(folder);
579         mh_scan_tree_recursive(item);
580
581         return 0;
582 }
583
584 #define MAKE_DIR_IF_NOT_EXIST(dir) \
585 { \
586         if (!is_dir_exist(dir)) { \
587                 if (is_file_exist(dir)) { \
588                         g_warning("File `%s' already exists.\n" \
589                                     "Can't create folder.", dir); \
590                         return -1; \
591                 } \
592                 if (make_dir(dir) < 0) \
593                         return -1; \
594         } \
595 }
596
597 static gint mh_create_tree(Folder *folder)
598 {
599         gchar *rootpath;
600
601         g_return_val_if_fail(folder != NULL, -1);
602
603         CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
604         rootpath = LOCAL_FOLDER(folder)->rootpath;
605         MAKE_DIR_IF_NOT_EXIST(rootpath);
606         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
607         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
608         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
609         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
610         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
611         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
612
613         return 0;
614 }
615
616 #undef MAKE_DIR_IF_NOT_EXIST
617
618 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
619 {
620         gchar *folder_path, *path;
621         gchar *real_path;
622         g_return_val_if_fail(folder != NULL, NULL);
623         g_return_val_if_fail(item != NULL, NULL);
624
625         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
626         g_return_val_if_fail(folder_path != NULL, NULL);
627
628         /* FIXME: [W32] The code below does not correctly merge
629            relative filenames; there should be a function to handle
630            this.  */
631         if ( !is_relative_filename (folder_path) ) {
632                 if (item->path)
633                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
634                                            item->path, NULL);
635                 else
636                         path = g_strdup(folder_path);
637         } else {
638                 if (item->path)
639                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
640                                            folder_path, G_DIR_SEPARATOR_S,
641                                            item->path, NULL);
642                 else
643                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
644                                            folder_path, NULL);
645         }
646         g_free(folder_path);
647         real_path = mh_filename_from_utf8(path);
648         if (!is_dir_exist(real_path) && is_dir_exist(path)) {
649                 /* mmh, older version did put utf8 filenames instead of
650                  * the correct encoding */
651                 rename(path, real_path);
652                 folder_item_scan(item);
653         }
654
655         g_free(path);
656         return real_path;
657 }
658
659 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
660                                     const gchar *name)
661 {
662         gchar *path, *real_name;
663         gchar *fullpath;
664         FolderItem *new_item;
665         gchar *mh_sequences_filename;
666         FILE *mh_sequences_file;
667
668         g_return_val_if_fail(folder != NULL, NULL);
669         g_return_val_if_fail(parent != NULL, NULL);
670         g_return_val_if_fail(name != NULL, NULL);
671
672         path = folder_item_get_path(parent);
673         if (!is_dir_exist(path)) 
674                 if (make_dir_hier(path) != 0)
675                         return NULL;
676                 
677         real_name = mh_filename_from_utf8(name);
678         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
679         g_free(real_name);
680         g_free(path);
681
682         if (make_dir(fullpath) < 0) {
683                 g_free(fullpath);
684                 return NULL;
685         }
686
687         g_free(fullpath);
688
689         if (parent->path)
690                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
691                                    NULL);
692         else
693                 path = g_strdup(name);
694         new_item = folder_item_new(folder, name, path);
695         folder_item_append(parent, new_item);
696
697         g_free(path);
698
699         path = folder_item_get_path(new_item);
700         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
701                                             ".mh_sequences", NULL);
702         if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
703                 fclose(mh_sequences_file);
704         }
705         g_free(mh_sequences_filename);
706         g_free(path);
707
708         return new_item;
709 }
710
711 static gint mh_rename_folder(Folder *folder, FolderItem *item,
712                              const gchar *name)
713 {
714         gchar *real_name;
715         gchar *oldpath;
716         gchar *dirname;
717         gchar *newpath, *utf8newpath;
718         gchar *paths[2];
719
720         g_return_val_if_fail(folder != NULL, -1);
721         g_return_val_if_fail(item != NULL, -1);
722         g_return_val_if_fail(item->path != NULL, -1);
723         g_return_val_if_fail(name != NULL, -1);
724
725         oldpath = folder_item_get_path(item);
726         if (!is_dir_exist(oldpath))
727                 make_dir_hier(oldpath);
728
729         dirname = g_path_get_dirname(oldpath);
730         real_name = mh_filename_from_utf8(name);
731         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
732         g_free(real_name);
733
734         if (g_rename(oldpath, newpath) < 0) {
735                 FILE_OP_ERROR(oldpath, "rename");
736                 g_free(oldpath);
737                 g_free(newpath);
738                 return -1;
739         }
740
741         g_free(oldpath);
742         g_free(newpath);
743
744         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
745                 dirname = g_path_get_dirname(item->path);
746                 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
747                                           name, NULL);
748                 g_free(dirname);
749         } else
750                 utf8newpath = g_strdup(name);
751
752         g_free(item->name);
753         item->name = g_strdup(name);
754
755         paths[0] = g_strdup(item->path);
756         paths[1] = utf8newpath;
757         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
758                         mh_rename_folder_func, paths);
759
760         g_free(paths[0]);
761         g_free(paths[1]);
762         return 0;
763 }
764
765 static gint mh_remove_folder(Folder *folder, FolderItem *item)
766 {
767         gchar *path;
768
769         g_return_val_if_fail(folder != NULL, -1);
770         g_return_val_if_fail(item != NULL, -1);
771         g_return_val_if_fail(item->path != NULL, -1);
772
773         path = folder_item_get_path(item);
774         if (remove_dir_recursive(path) < 0) {
775                 g_warning("can't remove directory `%s'\n", path);
776                 g_free(path);
777                 return -1;
778         }
779
780         g_free(path);
781         folder_item_remove(item);
782         return 0;
783 }
784
785 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
786 {
787         MsgInfo *msginfo;
788         MsgFlags flags;
789
790         g_return_val_if_fail(item != NULL, NULL);
791         g_return_val_if_fail(file != NULL, NULL);
792
793         flags.perm_flags = MSG_NEW|MSG_UNREAD;
794         flags.tmp_flags = 0;
795
796         if (folder_has_parent_of_type(item, F_QUEUE)) {
797                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
798         } else if (folder_has_parent_of_type(item, F_DRAFT)) {
799                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
800         }
801
802         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
803         if (!msginfo) return NULL;
804
805         msginfo->msgnum = atoi(file);
806         msginfo->folder = item;
807
808         return msginfo;
809 }
810
811 #if 0
812 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
813 {
814         gchar *entry;
815         gboolean result;
816
817         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
818         result = is_dir_exist(entry);
819         g_free(entry);
820
821         return result;
822 }
823
824 /*
825  * check whether PATH is a Maildir style mailbox.
826  * This is the case if the 3 subdir: new, cur, tmp are existing.
827  * This functon assumes that entry is an directory
828  */
829 static gboolean mh_is_maildir(const gchar *path)
830 {
831         return mh_is_maildir_one(path, "new") &&
832                mh_is_maildir_one(path, "cur") &&
833                mh_is_maildir_one(path, "tmp");
834 }
835 #endif
836
837 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
838 {
839         FolderItem *item;
840         gchar *path;
841
842         g_return_val_if_fail(node->data != NULL, FALSE);
843
844         if (G_NODE_IS_ROOT(node))
845                 return FALSE;
846
847         item = FOLDER_ITEM(node->data);
848
849         path = folder_item_get_path(item);
850         if (!is_dir_exist(path)) {
851                 debug_print("folder '%s' not found. removing...\n", path);
852                 folder_item_remove(item);
853         }
854         g_free(path);
855
856         return FALSE;
857 }
858
859 static void mh_remove_missing_folder_items(Folder *folder)
860 {
861         g_return_if_fail(folder != NULL);
862
863         debug_print("searching missing folders...\n");
864
865         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
866                         mh_remove_missing_folder_items_func, folder);
867 }
868
869 static void mh_scan_tree_recursive(FolderItem *item)
870 {
871         Folder *folder;
872 #ifdef G_OS_WIN32
873         GDir *dir;
874 #else
875         DIR *dp;
876         struct dirent *d;
877 #endif
878         const gchar *dir_name;
879         struct stat s;
880         gchar *real_path, *entry, *utf8entry, *utf8name;
881         gint n_msg = 0;
882
883         g_return_if_fail(item != NULL);
884         g_return_if_fail(item->folder != NULL);
885
886         folder = item->folder;
887
888         real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
889 #ifdef G_OS_WIN32
890         dir = g_dir_open(real_path, 0, NULL);
891         if (!dir) {
892                 g_warning("failed to open directory: %s\n", real_path);
893                 g_free(real_path);
894                 return;
895         }
896 #else
897         dp = opendir(real_path);
898         if (!dp) {
899                 FILE_OP_ERROR(real_path, "opendir");
900                 return;
901         }
902 #endif
903         g_free(real_path);
904
905         debug_print("scanning %s ...\n",
906                     item->path ? item->path
907                     : LOCAL_FOLDER(item->folder)->rootpath);
908         if (folder->ui_func)
909                 folder->ui_func(folder, item, folder->ui_func_data);
910
911 #ifdef G_OS_WIN32
912         while ((dir_name = g_dir_read_name(dir)) != NULL) {
913 #else
914         while ((d = readdir(dp)) != NULL) {
915                 dir_name = d->d_name;
916 #endif
917                 if (dir_name[0] == '.') continue;
918
919                 utf8name = mh_filename_to_utf8(dir_name);
920                 if (item->path)
921                         utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
922                                                 utf8name, NULL);
923                 else
924                         utf8entry = g_strdup(utf8name);
925                 entry = mh_filename_from_utf8(utf8entry);
926
927                 if (
928 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
929                         d->d_type == DT_DIR ||
930                         (d->d_type == DT_UNKNOWN &&
931 #endif
932                         g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
933 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
934                         )
935 #endif
936                    ) {
937                         FolderItem *new_item = NULL;
938                         GNode *node;
939
940 #if 0
941                         if (mh_is_maildir(entry)) {
942                                 g_free(entry);
943                                 g_free(utf8entry);
944                                 g_free(utf8name);
945                                 continue;
946                         }
947 #endif
948
949                         node = item->node;
950                         for (node = node->children; node != NULL; node = node->next) {
951                                 FolderItem *cur_item = FOLDER_ITEM(node->data);
952                                 if (!strcmp2(cur_item->path, entry)) {
953                                         new_item = cur_item;
954                                         break;
955                                 }
956                         }
957                         if (!new_item) {
958                                 debug_print("new folder '%s' found.\n", entry);
959                                 new_item = folder_item_new(folder, utf8name, utf8entry);
960                                 folder_item_append(item, new_item);
961                         }
962
963                         if (!item->path) {
964                                 if (!folder->inbox &&
965                                     !strcmp(dir_name, INBOX_DIR)) {
966                                         new_item->stype = F_INBOX;
967                                         folder->inbox = new_item;
968                                 } else if (!folder->outbox &&
969                                            !strcmp(dir_name, OUTBOX_DIR)) {
970                                         new_item->stype = F_OUTBOX;
971                                         folder->outbox = new_item;
972                                 } else if (!folder->draft &&
973                                            !strcmp(dir_name, DRAFT_DIR)) {
974                                         new_item->stype = F_DRAFT;
975                                         folder->draft = new_item;
976                                 } else if (!folder->queue &&
977                                            !strcmp(dir_name, QUEUE_DIR)) {
978                                         new_item->stype = F_QUEUE;
979                                         folder->queue = new_item;
980                                 } else if (!folder->trash &&
981                                            !strcmp(dir_name, TRASH_DIR)) {
982                                         new_item->stype = F_TRASH;
983                                         folder->trash = new_item;
984                                 }
985                         }
986
987                         mh_scan_tree_recursive(new_item);
988                 } else if (to_number(dir_name) > 0) n_msg++;
989
990                 g_free(entry);
991                 g_free(utf8entry);
992                 g_free(utf8name);
993         }
994
995 #ifdef G_OS_WIN32
996         g_dir_close(dir);
997 #else
998         closedir(dp);
999 #endif
1000
1001         item->mtime = time(NULL);
1002
1003 /*
1004         if (item->path) {
1005                 gint new, unread, total, min, max;
1006
1007                 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1008                                      &min, &max, 0);
1009                 if (n_msg > total) {
1010                         new += n_msg - total;
1011                         unread += n_msg - total;
1012                 }
1013                 item->new = new;
1014                 item->unread = unread;
1015                 item->total = n_msg;
1016         }
1017 */
1018 }
1019
1020 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1021 {
1022         FolderItem *item = node->data;
1023         gchar **paths = data;
1024         const gchar *oldpath = paths[0];
1025         const gchar *newpath = paths[1];
1026         gchar *base;
1027         gchar *new_itempath;
1028         gint oldpathlen;
1029
1030         oldpathlen = strlen(oldpath);
1031         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1032                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1033                 return TRUE;
1034         }
1035
1036         base = item->path + oldpathlen;
1037         while (*base == G_DIR_SEPARATOR) base++;
1038         if (*base == '\0')
1039                 new_itempath = g_strdup(newpath);
1040         else
1041                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1042                                            NULL);
1043         g_free(item->path);
1044         item->path = new_itempath;
1045
1046         return FALSE;
1047 }
1048
1049 static gchar *mh_filename_from_utf8(const gchar *path)
1050 {
1051         gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1052
1053         if (!real_path) {
1054                 g_warning("mh_filename_from_utf8: faild to convert character set\n");
1055                 real_path = g_strdup(path);
1056         }
1057
1058         return real_path;
1059 }
1060
1061 static gchar *mh_filename_to_utf8(const gchar *path)
1062 {
1063         gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1064         if (!utf8path) {
1065                 g_warning("mh_filename_to_utf8: faild to convert character set\n");
1066                 utf8path = g_strdup(path);
1067         }
1068
1069         return utf8path;
1070 }