2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 #include "mutt_curses.h"
25 #include "mutt_menu.h"
46 static struct mapping_t FolderHelp[] = {
47 { N_("Exit"), OP_EXIT },
48 { N_("Chdir"), OP_CHANGE_DIRECTORY },
49 { N_("Mask"), OP_ENTER_MASK },
50 { N_("Help"), OP_HELP },
55 static struct mapping_t FolderNewsHelp[] = {
56 { N_("Exit"), OP_EXIT },
57 { N_("List"), OP_TOGGLE_MAILBOXES },
58 { N_("Subscribe"), OP_BROWSER_SUBSCRIBE },
59 { N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE },
60 { N_("Catchup"), OP_CATCHUP },
61 { N_("Mask"), OP_ENTER_MASK },
62 { N_("Help"), OP_HELP },
67 typedef struct folder_t
69 struct folder_file *ff;
73 static char LastDir[_POSIX_PATH_MAX] = "";
74 static char LastDirBackup[_POSIX_PATH_MAX] = "";
76 /* Frees up the memory allocated for the local-global variables. */
77 static void destroy_state (struct browser_state *state)
81 for (c = 0; c < state->entrylen; c++)
83 FREE (&((state->entry)[c].name));
84 FREE (&((state->entry)[c].desc));
85 FREE (&((state->entry)[c].st));
88 FREE (&state->folder);
93 static int browser_compare_subject (const void *a, const void *b)
95 struct folder_file *pa = (struct folder_file *) a;
96 struct folder_file *pb = (struct folder_file *) b;
98 int r = mutt_strcoll (pa->name, pb->name);
100 return ((BrowserSort & SORT_REVERSE) ? -r : r);
103 static int browser_compare_date (const void *a, const void *b)
105 struct folder_file *pa = (struct folder_file *) a;
106 struct folder_file *pb = (struct folder_file *) b;
108 int r = pa->mtime - pb->mtime;
110 return ((BrowserSort & SORT_REVERSE) ? -r : r);
113 static int browser_compare_size (const void *a, const void *b)
115 struct folder_file *pa = (struct folder_file *) a;
116 struct folder_file *pb = (struct folder_file *) b;
118 int r = pa->size - pb->size;
120 return ((BrowserSort & SORT_REVERSE) ? -r : r);
123 static void browser_sort (struct browser_state *state)
125 int (*f) (const void *, const void *);
127 switch (BrowserSort & SORT_MASK)
133 if (option (OPTNEWS))
136 f = browser_compare_date;
140 if (option (OPTNEWS))
143 f = browser_compare_size;
147 f = browser_compare_subject;
150 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
153 static int link_is_dir (const char *folder, const char *path)
156 char fullpath[_POSIX_PATH_MAX];
158 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
160 if (stat (fullpath, &st) == 0)
161 return (S_ISDIR (st.st_mode));
167 folder_format_str (char *dest, size_t destlen, char op, const char *src,
168 const char *fmt, const char *ifstring, const char *elsestring,
169 unsigned long data, format_flag flags)
171 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
172 char date[16], *t_fmt;
174 FOLDER *folder = (FOLDER *) data;
177 int optional = (flags & M_FORMAT_OPTIONAL);
182 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
183 snprintf (dest, destlen, tmp, folder->num + 1);
187 if (folder->ff->st != NULL)
190 t_fmt = tnow - folder->ff->st->st_mtime < 31536000 ? "%b %d %H:%M" : "%b %d %Y";
191 strftime (date, sizeof (date), t_fmt, localtime (&folder->ff->st->st_mtime));
192 mutt_format_s (dest, destlen, fmt, date);
195 mutt_format_s (dest, destlen, fmt, "");
202 if (folder->ff->imap)
203 s = NONULL (folder->ff->desc);
206 s = NONULL (folder->ff->name);
208 snprintf (fn, sizeof (fn), "%s%s", s,
209 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
210 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
211 ((folder->ff->st->st_mode & S_IXUSR) != 0 ? "*" : ""))) : "");
213 mutt_format_s (dest, destlen, fmt, fn);
217 if (folder->ff->st != NULL)
219 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
220 S_ISDIR(folder->ff->st->st_mode) ? 'd' : (S_ISLNK(folder->ff->st->st_mode) ? 'l' : '-'),
221 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r': '-',
222 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
223 (folder->ff->st->st_mode & S_ISUID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) != 0 ? 'x': '-',
224 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
225 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
226 (folder->ff->st->st_mode & S_ISGID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) != 0 ? 'x': '-',
227 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
228 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
229 (folder->ff->st->st_mode & S_ISVTX) != 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) != 0 ? 'x': '-');
230 mutt_format_s (dest, destlen, fmt, permission);
233 else if (folder->ff->imap)
235 /* mark folders with subfolders AND mail */
236 snprintf (permission, sizeof (permission), "IMAP %c",
237 (folder->ff->inferiors && folder->ff->selectable) ? '+' : ' ');
238 mutt_format_s (dest, destlen, fmt, permission);
242 mutt_format_s (dest, destlen, fmt, "");
246 if (folder->ff->st != NULL)
248 if ((gr = getgrgid (folder->ff->st->st_gid)))
249 mutt_format_s (dest, destlen, fmt, gr->gr_name);
252 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
253 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
257 mutt_format_s (dest, destlen, fmt, "");
261 if (folder->ff->st != NULL)
263 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
264 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
267 mutt_format_s (dest, destlen, fmt, "");
272 if (mx_is_imap (folder->ff->desc))
276 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
277 snprintf (dest, destlen, tmp, folder->ff->new);
279 else if (!folder->ff->new)
284 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
285 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
289 if (folder->ff->st != NULL)
291 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
292 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
295 mutt_format_s (dest, destlen, fmt, "");
299 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
300 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
304 if (folder->ff->st != NULL)
306 if ((pw = getpwuid (folder->ff->st->st_uid)))
307 mutt_format_s (dest, destlen, fmt, pw->pw_name);
310 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
311 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
315 mutt_format_s (dest, destlen, fmt, "");
319 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
320 snprintf (dest, destlen, tmp, op);
325 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
326 else if (flags & M_FORMAT_OPTIONAL)
327 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
334 newsgroup_format_str (char *dest, size_t destlen, char op, const char *src,
335 const char *fmt, const char *ifstring, const char *elsestring,
336 unsigned long data, format_flag flags)
338 char fn[SHORT_STRING], tmp[SHORT_STRING];
339 FOLDER *folder = (FOLDER *) data;
344 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
345 snprintf (dest, destlen, tmp, folder->num + 1);
349 strncpy (fn, folder->ff->name, sizeof(fn) - 1);
350 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
351 snprintf (dest, destlen, tmp, fn);
355 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
356 if (folder->ff->nd->subscribed)
357 snprintf (dest, destlen, tmp, ' ');
359 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
363 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
364 if (folder->ff->nd->deleted)
365 snprintf (dest, destlen, tmp, 'D');
367 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
371 if (flags & M_FORMAT_OPTIONAL)
373 if (folder->ff->nd->unread != 0)
374 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
377 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
380 else if (Context && Context->data == folder->ff->nd)
382 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
383 snprintf (dest, destlen, tmp, Context->unread);
387 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
388 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
393 if (Context && Context->data == folder->ff->nd)
395 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
396 snprintf (dest, destlen, tmp, Context->new);
398 else if (option (OPTMARKOLD) &&
399 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
400 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage)
402 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
403 snprintf (dest, destlen, tmp, folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
407 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
408 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
413 if (folder->ff->nd->desc != NULL)
415 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
416 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
420 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
421 snprintf (dest, destlen, tmp, "");
427 #endif /* USE_NNTP */
429 static void add_folder (MUTTMENU *m, struct browser_state *state,
430 const char *name, const struct stat *s,
433 if (state->entrylen == state->entrymax)
435 /* need to allocate more space */
436 safe_realloc (&state->entry,
437 sizeof (struct folder_file) * (state->entrymax += 256));
438 memset (&state->entry[state->entrylen], 0,
439 sizeof (struct folder_file) * 256);
441 m->data = state->entry;
446 (state->entry)[state->entrylen].mode = s->st_mode;
447 (state->entry)[state->entrylen].mtime = s->st_mtime;
448 (state->entry)[state->entrylen].size = s->st_size;
450 (state->entry)[state->entrylen].st = safe_malloc (sizeof (struct stat));
451 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
454 (state->entry)[state->entrylen].new = new;
455 (state->entry)[state->entrylen].name = safe_strdup (name);
456 (state->entry)[state->entrylen].desc = safe_strdup (name);
458 (state->entry)[state->entrylen].imap = 0;
461 if (option (OPTNEWS))
462 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
467 static void init_state (struct browser_state *state, MUTTMENU *menu)
470 state->entrymax = 256;
471 state->entry = (struct folder_file *) safe_calloc (state->entrymax, sizeof (struct folder_file));
473 state->imap_browse = 0;
476 menu->data = state->entry;
479 /* get list of all files/newsgroups with mask */
480 static int examine_directory (MUTTMENU *menu, struct browser_state *state,
481 char *d, const char *prefix)
484 if (option (OPTNEWS))
488 NNTP_SERVER *news = CurrentNewsSrv;
490 /* mutt_buffy_check (0); */
491 init_state (state, menu);
493 for (tmp = news->list; tmp; tmp = tmp->next)
495 if (!(data = (NNTP_DATA *)tmp->data))
497 if (prefix && *prefix && strncmp (prefix, data->group,
498 strlen (prefix)) != 0)
500 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
502 add_folder (menu, state, data->group, NULL, data, data->new);
506 #endif /* USE_NNTP */
511 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
514 while (stat (d, &s) == -1)
518 /* The last used directory is deleted, try to use the parent dir. */
519 char *c = strrchr (d, '/');
531 if (!S_ISDIR (s.st_mode))
533 mutt_error (_("%s is not a directory."), d);
537 mutt_buffy_check (0);
539 if ((dp = opendir (d)) == NULL)
545 init_state (state, menu);
547 while ((de = readdir (dp)) != NULL)
549 if (mutt_strcmp (de->d_name, ".") == 0)
550 continue; /* we don't need . */
552 if (prefix && *prefix && mutt_strncmp (prefix, de->d_name, mutt_strlen (prefix)) != 0)
554 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
557 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
558 if (lstat (buffer, &s) == -1)
561 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
562 (! S_ISLNK (s.st_mode)))
566 while (tmp && mutt_strcmp (buffer, tmp->path))
568 add_folder (menu, state, de->d_name, &s, NULL, (tmp) ? tmp->new : 0);
572 browser_sort (state);
576 /* get list of mailboxes/subscribed newsgroups */
577 static int examine_mailboxes (MUTTMENU *menu, struct browser_state *state)
580 char buffer[LONG_STRING];
583 if (option (OPTNEWS))
587 NNTP_SERVER *news = CurrentNewsSrv;
589 /* mutt_buffy_check (0); */
590 init_state (state, menu);
592 for (tmp = news->list; tmp; tmp = tmp->next)
594 if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
595 (data->subscribed && (!option (OPTSHOWONLYUNREAD) || data->unread))))
596 add_folder (menu, state, data->group, NULL, data, data->new);
602 BUFFY *tmp = Incoming;
606 mutt_buffy_check (0);
608 init_state (state, menu);
613 if (mx_is_imap (tmp->path))
615 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
620 if (mx_is_pop (tmp->path))
622 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
627 if (mx_is_nntp (tmp->path))
629 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
633 if (lstat (tmp->path, &s) == -1)
636 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
637 (! S_ISLNK (s.st_mode)))
640 strfcpy (buffer, NONULL(tmp->path), sizeof (buffer));
641 mutt_pretty_mailbox (buffer);
643 add_folder (menu, state, buffer, &s, NULL, tmp->new);
645 while ((tmp = tmp->next));
647 browser_sort (state);
651 static int select_file_search (MUTTMENU *menu, regex_t *re, int n)
654 if (option (OPTNEWS))
655 return (regexec (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
657 return (regexec (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
660 static void folder_entry (char *s, size_t slen, MUTTMENU *menu, int num)
664 folder.ff = &((struct folder_file *) menu->data)[num];
668 if (option (OPTNEWS))
669 mutt_FormatString (s, slen, NONULL(GroupFormat), newsgroup_format_str,
670 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
673 mutt_FormatString (s, slen, NONULL(FolderFormat), folder_format_str,
674 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
677 static void init_menu (struct browser_state *state, MUTTMENU *menu, char *title,
678 size_t titlelen, int buffy)
680 char path[_POSIX_PATH_MAX];
682 menu->max = state->entrylen;
684 if(menu->current >= menu->max)
685 menu->current = menu->max - 1;
686 if (menu->current < 0)
688 if (menu->top > menu->current)
694 if (option (OPTNEWS))
697 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
699 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
700 CurrentNewsSrv->conn->account.host);
705 snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
708 strfcpy (path, LastDir, sizeof (path));
709 mutt_pretty_mailbox (path);
711 if (state->imap_browse && option (OPTIMAPLSUB))
712 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
713 path, NONULL (Mask.pattern));
716 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
717 path, NONULL(Mask.pattern));
719 menu->redraw = REDRAW_FULL;
722 static int file_tag (MUTTMENU *menu, int n, int m)
724 struct folder_file *ff = &(((struct folder_file *)menu->data)[n]);
726 if (S_ISDIR (ff->mode) || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name)))
728 mutt_error _("Can't attach a directory!");
733 ff->tagged = (m >= 0 ? m : !ff->tagged);
735 return ff->tagged - ot;
738 void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *numfiles)
740 char buf[_POSIX_PATH_MAX];
741 char prefix[_POSIX_PATH_MAX] = "";
742 char helpstr[SHORT_STRING];
744 struct browser_state state;
747 int i, killPrefix = 0;
748 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
749 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
750 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
752 buffy = buffy && folder;
754 memset (&state, 0, sizeof (struct browser_state));
757 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
760 if (option (OPTNEWS))
763 strfcpy (prefix, f, sizeof (prefix));
768 /* default state for news reader mode is browse subscribed newsgroups */
770 for (list = CurrentNewsSrv->list; list; list = list->next)
772 NNTP_DATA *data = (NNTP_DATA *) list->data;
774 if (data && data->subscribed)
786 mutt_expand_path (f, flen);
790 init_state (&state, NULL);
791 state.imap_browse = 1;
792 imap_browse (f, &state);
793 strfcpy (LastDir, state.folder, sizeof (LastDir));
798 for (i = mutt_strlen (f) - 1; i > 0 && f[i] != '/' ; i--);
803 if (i > sizeof (LastDir) - 1) i = sizeof (LastDir) - 1;
804 strncpy (LastDir, f, i);
809 getcwd (LastDir, sizeof (LastDir));
810 safe_strcat (LastDir, sizeof (LastDir), "/");
811 safe_strncat (LastDir, sizeof (LastDir), f, i);
817 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
819 getcwd (LastDir, sizeof (LastDir));
822 if (i <= 0 && f[0] != '/')
823 strfcpy (prefix, f, sizeof (prefix));
825 strfcpy (prefix, f + i + 1, sizeof (prefix));
834 getcwd (LastDir, sizeof (LastDir));
835 else if (!LastDir[0])
836 strfcpy (LastDir, NONULL(Maildir), sizeof (LastDir));
839 if (!buffy && mx_is_imap (LastDir))
841 init_state (&state, NULL);
842 state.imap_browse = 1;
843 imap_browse (LastDir, &state);
852 if (examine_mailboxes (NULL, &state) == -1)
857 if (!state.imap_browse)
859 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
862 menu = mutt_new_menu ();
863 menu->menu = MENU_FOLDER;
864 menu->make_entry = folder_entry;
865 menu->search = select_file_search;
867 menu->data = state.entry;
869 menu->tag = file_tag;
871 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
873 (option (OPTNEWS)) ? FolderNewsHelp :
877 init_menu (&state, menu, title, sizeof (title), buffy);
881 switch (i = mutt_menuLoop (menu))
883 case OP_GENERIC_SELECT_ENTRY:
887 mutt_error _("No files match the file mask");
891 if (S_ISDIR (state.entry[menu->current].mode) ||
892 (S_ISLNK (state.entry[menu->current].mode) &&
893 link_is_dir (LastDir, state.entry[menu->current].name))
895 || state.entry[menu->current].inferiors
899 /* make sure this isn't a MH or maildir mailbox */
902 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
903 mutt_expand_path (buf, sizeof (buf));
906 else if (state.imap_browse)
908 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
912 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
914 if ((mx_get_magic (buf) <= 0)
916 || state.entry[menu->current].inferiors
920 char OldLastDir[_POSIX_PATH_MAX];
922 /* save the old directory */
923 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
925 if (mutt_strcmp (state.entry[menu->current].name, "..") == 0)
927 if (mutt_strcmp ("..", LastDir + mutt_strlen (LastDir) - 2) == 0)
928 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
931 char *p = strrchr (LastDir + 1, '/');
937 if (LastDir[0] == '/')
940 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
946 strfcpy (LastDir, state.entry[menu->current].name, sizeof (LastDir));
947 mutt_expand_path (LastDir, sizeof (LastDir));
950 else if (state.imap_browse)
955 strfcpy (LastDir, state.entry[menu->current].name,
957 /* tack on delimiter here */
958 n = strlen (LastDir)+1;
960 /* special case "" needs no delimiter */
961 url_parse_ciss (&url, state.entry[menu->current].name);
963 (state.entry[menu->current].delim != '\0') &&
964 (n < sizeof (LastDir)))
967 LastDir[n-1] = state.entry[menu->current].delim;
973 char tmp[_POSIX_PATH_MAX];
974 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name, sizeof (tmp));
975 strfcpy (LastDir, tmp, sizeof (LastDir));
978 destroy_state (&state);
986 if (state.imap_browse)
988 init_state (&state, NULL);
989 state.imap_browse = 1;
990 imap_browse (LastDir, &state);
991 menu->data = state.entry;
995 if (examine_directory (menu, &state, LastDir, prefix) == -1)
997 /* try to restore the old values */
998 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
999 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1001 strfcpy (LastDir, NONULL(Homedir), sizeof (LastDir));
1007 init_menu (&state, menu, title, sizeof (title), buffy);
1013 if (buffy || option (OPTNEWS)) /* news have not path */
1018 strfcpy (f, state.entry[menu->current].name, flen);
1019 mutt_expand_path (f, flen);
1022 else if (state.imap_browse)
1023 strfcpy (f, state.entry[menu->current].name, flen);
1026 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
1028 /* Fall through to OP_EXIT */
1039 *numfiles = menu->tagged;
1040 tfiles = safe_calloc (*numfiles, sizeof (char *));
1041 for (i = 0, j = 0; i < state.entrylen; i++)
1043 struct folder_file ff = state.entry[i];
1044 char full[_POSIX_PATH_MAX];
1047 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1048 mutt_expand_path (full, sizeof (full));
1049 tfiles[j++] = safe_strdup (full);
1054 else if (f[0]) /* no tagged entries. return selected entry */
1057 tfiles = safe_calloc (*numfiles, sizeof (char *));
1058 mutt_expand_path (f, flen);
1059 tfiles[0] = safe_strdup (f);
1064 destroy_state (&state);
1065 mutt_menuDestroy (&menu);
1068 case OP_BROWSER_TELL:
1070 mutt_message("%s", state.entry[menu->current].name);
1074 case OP_BROWSER_TOGGLE_LSUB:
1075 if (option (OPTIMAPLSUB))
1077 unset_option (OPTIMAPLSUB);
1081 set_option (OPTIMAPLSUB);
1083 mutt_ungetch (0, OP_CHECK_NEW);
1086 case OP_CREATE_MAILBOX:
1087 if (!state.imap_browse)
1088 mutt_error (_("Create is only supported for IMAP mailboxes"));
1091 imap_mailbox_create (LastDir);
1092 /* TODO: find a way to detect if the new folder would appear in
1093 * this window, and insert it without starting over. */
1094 destroy_state (&state);
1095 init_state (&state, NULL);
1096 state.imap_browse = 1;
1097 imap_browse (LastDir, &state);
1098 menu->data = state.entry;
1101 init_menu (&state, menu, title, sizeof (title), buffy);
1102 MAYBE_REDRAW (menu->redraw);
1106 case OP_RENAME_MAILBOX:
1107 if (!state.entry[menu->current].imap)
1108 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1111 int nentry = menu->current;
1113 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1114 destroy_state (&state);
1115 init_state (&state, NULL);
1116 state.imap_browse = 1;
1117 imap_browse (LastDir, &state);
1118 menu->data = state.entry;
1121 init_menu (&state, menu, title, sizeof (title), buffy);
1122 MAYBE_REDRAW (menu->redraw);
1127 case OP_DELETE_MAILBOX:
1128 if (!state.entry[menu->current].imap)
1129 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1132 char msg[SHORT_STRING];
1134 int nentry = menu->current;
1136 imap_parse_path (state.entry[nentry].name, &mx);
1137 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1139 if (mutt_yesorno (msg, M_NO) == M_YES)
1141 if (!imap_delete_mailbox (Context, mx))
1143 /* free the mailbox from the browser */
1144 FREE (&((state.entry)[nentry].name));
1145 FREE (&((state.entry)[nentry].desc));
1146 /* and move all other entries up */
1147 if (nentry+1 < state.entrylen)
1148 memmove (state.entry + nentry, state.entry + nentry + 1,
1149 sizeof (struct folder_file) * (state.entrylen - (nentry+1)));
1151 mutt_message _("Mailbox deleted.");
1152 init_menu (&state, menu, title, sizeof (title), buffy);
1153 MAYBE_REDRAW (menu->redraw);
1157 mutt_message _("Mailbox not deleted.");
1163 case OP_CHANGE_DIRECTORY:
1166 if (option (OPTNEWS))
1170 strfcpy (buf, LastDir, sizeof (buf));
1172 if (!state.imap_browse)
1175 /* add '/' at the end of the directory name if not already there */
1176 int len=mutt_strlen(LastDir);
1177 if (len && LastDir[len-1] != '/' && sizeof (buf) > len)
1181 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1185 mutt_expand_path (buf, sizeof (buf));
1187 if (mx_is_imap (buf))
1189 strfcpy (LastDir, buf, sizeof (LastDir));
1190 destroy_state (&state);
1191 init_state (&state, NULL);
1192 state.imap_browse = 1;
1193 imap_browse (LastDir, &state);
1194 menu->data = state.entry;
1197 init_menu (&state, menu, title, sizeof (title), buffy);
1201 if (stat (buf, &st) == 0)
1203 if (S_ISDIR (st.st_mode))
1205 destroy_state (&state);
1206 if (examine_directory (menu, &state, buf, prefix) == 0)
1207 strfcpy (LastDir, buf, sizeof (LastDir));
1210 mutt_error _("Error scanning directory.");
1211 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1213 mutt_menuDestroy (&menu);
1219 init_menu (&state, menu, title, sizeof (title), buffy);
1222 mutt_error (_("%s is not a directory."), buf);
1227 MAYBE_REDRAW (menu->redraw);
1232 strfcpy (buf, NONULL(Mask.pattern), sizeof (buf));
1233 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0)
1235 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1240 /* assume that the user wants to see everything */
1242 strfcpy (buf, ".", sizeof (buf));
1251 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1253 regerror (err, rx, buf, sizeof (buf));
1256 mutt_error ("%s", buf);
1260 mutt_str_replace (&Mask.pattern, buf);
1266 destroy_state (&state);
1268 if (state.imap_browse)
1270 init_state (&state, NULL);
1271 state.imap_browse = 1;
1272 imap_browse (LastDir, &state);
1273 menu->data = state.entry;
1274 init_menu (&state, menu, title, sizeof (title), buffy);
1278 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1279 init_menu (&state, menu, title, sizeof (title), buffy);
1282 mutt_error _("Error scanning directory.");
1283 mutt_menuDestroy (&menu);
1287 if (!state.entrylen)
1289 mutt_error _("No files match the file mask");
1294 MAYBE_REDRAW (menu->redraw);
1298 case OP_SORT_REVERSE:
1302 int reverse = (i == OP_SORT_REVERSE);
1304 switch (mutt_multi_choice ((reverse) ?
1305 _("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ") :
1306 _("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1309 case -1: /* abort */
1313 case 1: /* (d)ate */
1314 BrowserSort = SORT_DATE;
1317 case 2: /* (a)lpha */
1318 BrowserSort = SORT_SUBJECT;
1321 case 3: /* si(z)e */
1322 BrowserSort = SORT_SIZE;
1325 case 4: /* do(n)'t sort */
1326 BrowserSort = SORT_ORDER;
1332 BrowserSort |= reverse ? SORT_REVERSE : 0;
1333 browser_sort (&state);
1334 menu->redraw = REDRAW_FULL;
1339 case OP_TOGGLE_MAILBOXES:
1343 destroy_state (&state);
1349 if (examine_mailboxes (menu, &state) == -1)
1353 else if (mx_is_imap (LastDir))
1355 init_state (&state, NULL);
1356 state.imap_browse = 1;
1357 imap_browse (LastDir, &state);
1358 menu->data = state.entry;
1361 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1363 init_menu (&state, menu, title, sizeof (title), buffy);
1370 case OP_BROWSER_NEW_FILE:
1372 snprintf (buf, sizeof (buf), "%s/", LastDir);
1373 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) == 0)
1375 strfcpy (f, buf, flen);
1376 destroy_state (&state);
1377 mutt_menuDestroy (&menu);
1380 MAYBE_REDRAW (menu->redraw);
1383 case OP_BROWSER_VIEW_FILE:
1384 if (!state.entrylen)
1386 mutt_error _("No files match the file mask");
1391 if (state.entry[menu->current].selectable)
1393 strfcpy (f, state.entry[menu->current].name, flen);
1394 destroy_state (&state);
1395 mutt_menuDestroy (&menu);
1400 if (S_ISDIR (state.entry[menu->current].mode) ||
1401 (S_ISLNK (state.entry[menu->current].mode) &&
1402 link_is_dir (LastDir, state.entry[menu->current].name)))
1404 mutt_error _("Can't view a directory");
1410 char buf[_POSIX_PATH_MAX];
1412 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
1413 b = mutt_make_file_attach (buf);
1416 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1417 mutt_free_body (&b);
1418 menu->redraw = REDRAW_FULL;
1421 mutt_error _("Error trying to view file");
1428 if (option (OPTNEWS))
1430 struct folder_file *f = &state.entry[menu->current];
1433 if (i == OP_CATCHUP)
1434 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1436 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1441 struct folder_file ff;
1442 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1445 folder.ff->name = f->name;
1446 folder.ff->st = NULL;
1447 folder.ff->is_new = nd->new;
1450 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1451 newsgroup_format_str, (unsigned long) &folder,
1452 M_FORMAT_ARROWCURSOR);
1453 f->desc = safe_strdup (buffer); */
1454 if (menu->current + 1 < menu->max)
1456 menu->redraw = REDRAW_MOTION_RESYNCH;
1461 case OP_LOAD_ACTIVE:
1462 if (!option (OPTNEWS))
1469 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next)
1471 if ((data = (NNTP_DATA *)tmp->data))
1475 nntp_get_active (CurrentNewsSrv);
1477 destroy_state (&state);
1479 examine_mailboxes (menu, &state);
1481 examine_directory (menu, &state, NULL, NULL);
1482 init_menu (&state, menu, title, sizeof (title), buffy);
1484 #endif /* USE_NNTP */
1486 #if defined USE_IMAP || defined USE_NNTP
1487 case OP_BROWSER_SUBSCRIBE:
1488 case OP_BROWSER_UNSUBSCRIBE:
1491 case OP_SUBSCRIBE_PATTERN:
1492 case OP_UNSUBSCRIBE_PATTERN:
1493 if (option (OPTNEWS))
1495 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1497 int j = menu->current;
1499 NNTP_SERVER *news = CurrentNewsSrv;
1501 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN)
1507 if (i == OP_SUBSCRIBE_PATTERN)
1508 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1510 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1511 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0])
1517 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1519 regerror (err, rx, buf, sizeof (buf));
1522 mutt_error ("%s", buf);
1525 menu->redraw = REDRAW_FULL;
1528 else if (!state.entrylen)
1530 mutt_error _("No newsgroups match the mask");
1534 for ( ; j < state.entrylen; j++)
1536 struct folder_file *f = &state.entry[j];
1538 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1539 regexec (rx, f->name, 0, NULL, 0) == 0)
1541 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1542 nd = mutt_newsgroup_subscribe (news, f->name);
1544 nd = mutt_newsgroup_unsubscribe (news, f->name);
1548 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1550 folder.name = f->name;
1552 folder.new = nd->new;
1555 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1556 newsgroup_format_str, (unsigned long) &folder,
1557 M_FORMAT_ARROWCURSOR);
1558 f->desc = safe_strdup (buffer);
1561 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE)
1563 if (menu->current + 1 < menu->max)
1565 menu->redraw = REDRAW_MOTION_RESYNCH;
1569 if (i == OP_SUBSCRIBE_PATTERN)
1571 LIST *grouplist = NULL;
1574 grouplist = news->list;
1575 for (; grouplist; grouplist = grouplist->next)
1577 nd = (NNTP_DATA *) grouplist->data;
1578 if (nd && nd->group && !nd->subscribed)
1580 if (regexec (rx, nd->group, 0, NULL, 0) == 0)
1582 mutt_newsgroup_subscribe (news, nd->group);
1583 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1587 init_menu (&state, menu, title, sizeof (title), buffy);
1589 mutt_newsrc_update (news);
1590 nntp_clear_cacheindex (news);
1591 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1597 #endif /* USE_IMAP && USE_NNTP */
1598 #endif /* USE_NNTP */
1601 if (i == OP_BROWSER_SUBSCRIBE)
1602 imap_subscribe (state.entry[menu->current].name, 1);
1604 imap_subscribe (state.entry[menu->current].name, 0);
1606 #endif /* USE_IMAP */
1613 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));