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.
20 #include "mutt_curses.h"
21 #include "mutt_menu.h"
42 static struct mapping_t FolderHelp[] = {
43 { N_("Exit"), OP_EXIT },
44 { N_("Chdir"), OP_CHANGE_DIRECTORY },
45 { N_("Mask"), OP_ENTER_MASK },
46 { N_("Help"), OP_HELP },
51 static struct mapping_t FolderNewsHelp[] = {
52 { N_("Exit"), OP_EXIT },
53 { N_("List"), OP_TOGGLE_MAILBOXES },
54 { N_("Subscribe"), OP_BROWSER_SUBSCRIBE },
55 { N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE },
56 { N_("Catchup"), OP_CATCHUP },
57 { N_("Mask"), OP_ENTER_MASK },
58 { N_("Help"), OP_HELP },
63 typedef struct folder_t
65 struct folder_file *ff;
69 static char LastDir[_POSIX_PATH_MAX] = "";
70 static char LastDirBackup[_POSIX_PATH_MAX] = "";
72 /* Frees up the memory allocated for the local-global variables. */
73 static void destroy_state (struct browser_state *state)
77 for (c = 0; c < state->entrylen; c++)
79 FREE (&((state->entry)[c].name));
80 FREE (&((state->entry)[c].desc));
81 FREE (&((state->entry)[c].st));
84 FREE (&state->folder);
89 static int browser_compare_subject (const void *a, const void *b)
91 struct folder_file *pa = (struct folder_file *) a;
92 struct folder_file *pb = (struct folder_file *) b;
94 int r = mutt_strcoll (pa->name, pb->name);
96 return ((BrowserSort & SORT_REVERSE) ? -r : r);
99 static int browser_compare_date (const void *a, const void *b)
101 struct folder_file *pa = (struct folder_file *) a;
102 struct folder_file *pb = (struct folder_file *) b;
104 int r = pa->mtime - pb->mtime;
106 return ((BrowserSort & SORT_REVERSE) ? -r : r);
109 static int browser_compare_size (const void *a, const void *b)
111 struct folder_file *pa = (struct folder_file *) a;
112 struct folder_file *pb = (struct folder_file *) b;
114 int r = pa->size - pb->size;
116 return ((BrowserSort & SORT_REVERSE) ? -r : r);
119 static void browser_sort (struct browser_state *state)
121 int (*f) (const void *, const void *);
123 switch (BrowserSort & SORT_MASK)
129 if (option (OPTNEWS))
132 f = browser_compare_date;
136 if (option (OPTNEWS))
139 f = browser_compare_size;
143 f = browser_compare_subject;
146 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
149 static int link_is_dir (const char *folder, const char *path)
152 char fullpath[_POSIX_PATH_MAX];
154 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
156 if (stat (fullpath, &st) == 0)
157 return (S_ISDIR (st.st_mode));
163 folder_format_str (char *dest, size_t destlen, char op, const char *src,
164 const char *fmt, const char *ifstring, const char *elsestring,
165 unsigned long data, format_flag flags)
167 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
168 char date[16], *t_fmt;
170 FOLDER *folder = (FOLDER *) data;
173 int optional = (flags & M_FORMAT_OPTIONAL);
178 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
179 snprintf (dest, destlen, tmp, folder->num + 1);
183 if (folder->ff->st != NULL)
186 t_fmt = tnow - folder->ff->st->st_mtime < 31536000 ? "%b %d %H:%M" : "%b %d %Y";
187 strftime (date, sizeof (date), t_fmt, localtime (&folder->ff->st->st_mtime));
188 mutt_format_s (dest, destlen, fmt, date);
191 mutt_format_s (dest, destlen, fmt, "");
198 if (folder->ff->imap)
199 s = NONULL (folder->ff->desc);
202 s = NONULL (folder->ff->name);
204 snprintf (fn, sizeof (fn), "%s%s", s,
205 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
206 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
207 ((folder->ff->st->st_mode & S_IXUSR) != 0 ? "*" : ""))) : "");
209 mutt_format_s (dest, destlen, fmt, fn);
213 if (folder->ff->st != NULL)
215 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
216 S_ISDIR(folder->ff->st->st_mode) ? 'd' : (S_ISLNK(folder->ff->st->st_mode) ? 'l' : '-'),
217 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r': '-',
218 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
219 (folder->ff->st->st_mode & S_ISUID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) != 0 ? 'x': '-',
220 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
221 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
222 (folder->ff->st->st_mode & S_ISGID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) != 0 ? 'x': '-',
223 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
224 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
225 (folder->ff->st->st_mode & S_ISVTX) != 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) != 0 ? 'x': '-');
226 mutt_format_s (dest, destlen, fmt, permission);
229 else if (folder->ff->imap)
231 /* mark folders with subfolders AND mail */
232 snprintf (permission, sizeof (permission), "IMAP %c",
233 (folder->ff->inferiors && folder->ff->selectable) ? '+' : ' ');
234 mutt_format_s (dest, destlen, fmt, permission);
238 mutt_format_s (dest, destlen, fmt, "");
242 if (folder->ff->st != NULL)
244 if ((gr = getgrgid (folder->ff->st->st_gid)))
245 mutt_format_s (dest, destlen, fmt, gr->gr_name);
248 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
249 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
253 mutt_format_s (dest, destlen, fmt, "");
257 if (folder->ff->st != NULL)
259 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
260 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
263 mutt_format_s (dest, destlen, fmt, "");
268 if (mx_is_imap (folder->ff->desc))
272 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
273 snprintf (dest, destlen, tmp, folder->ff->new);
275 else if (!folder->ff->new)
280 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
281 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
285 if (folder->ff->st != NULL)
287 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
288 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
291 mutt_format_s (dest, destlen, fmt, "");
295 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
296 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
300 if (folder->ff->st != NULL)
302 if ((pw = getpwuid (folder->ff->st->st_uid)))
303 mutt_format_s (dest, destlen, fmt, pw->pw_name);
306 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
307 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
311 mutt_format_s (dest, destlen, fmt, "");
315 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
316 snprintf (dest, destlen, tmp, op);
321 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
322 else if (flags & M_FORMAT_OPTIONAL)
323 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
330 newsgroup_format_str (char *dest, size_t destlen, char op, const char *src,
331 const char *fmt, const char *ifstring, const char *elsestring,
332 unsigned long data, format_flag flags)
334 char fn[SHORT_STRING], tmp[SHORT_STRING];
335 FOLDER *folder = (FOLDER *) data;
340 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
341 snprintf (dest, destlen, tmp, folder->num + 1);
345 strncpy (fn, folder->ff->name, sizeof(fn) - 1);
346 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
347 snprintf (dest, destlen, tmp, fn);
351 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
352 if (folder->ff->nd->subscribed)
353 snprintf (dest, destlen, tmp, ' ');
355 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
359 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
360 if (folder->ff->nd->deleted)
361 snprintf (dest, destlen, tmp, 'D');
363 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
367 if (flags & M_FORMAT_OPTIONAL)
369 if (folder->ff->nd->unread != 0)
370 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
373 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
376 else if (Context && Context->data == folder->ff->nd)
378 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
379 snprintf (dest, destlen, tmp, Context->unread);
383 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
384 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
389 if (Context && Context->data == folder->ff->nd)
391 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
392 snprintf (dest, destlen, tmp, Context->new);
394 else if (option (OPTMARKOLD) &&
395 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
396 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage)
398 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
399 snprintf (dest, destlen, tmp, folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
403 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
404 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
409 if (folder->ff->nd->desc != NULL)
411 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
412 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
416 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
417 snprintf (dest, destlen, tmp, "");
423 #endif /* USE_NNTP */
425 static void add_folder (MUTTMENU *m, struct browser_state *state,
426 const char *name, const struct stat *s,
429 if (state->entrylen == state->entrymax)
431 /* need to allocate more space */
432 safe_realloc (&state->entry,
433 sizeof (struct folder_file) * (state->entrymax += 256));
434 memset (&state->entry[state->entrylen], 0,
435 sizeof (struct folder_file) * 256);
437 m->data = state->entry;
442 (state->entry)[state->entrylen].mode = s->st_mode;
443 (state->entry)[state->entrylen].mtime = s->st_mtime;
444 (state->entry)[state->entrylen].size = s->st_size;
446 (state->entry)[state->entrylen].st = safe_malloc (sizeof (struct stat));
447 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
450 (state->entry)[state->entrylen].new = new;
451 (state->entry)[state->entrylen].name = safe_strdup (name);
452 (state->entry)[state->entrylen].desc = safe_strdup (name);
454 (state->entry)[state->entrylen].imap = 0;
457 if (option (OPTNEWS))
458 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
463 static void init_state (struct browser_state *state, MUTTMENU *menu)
466 state->entrymax = 256;
467 state->entry = (struct folder_file *) safe_calloc (state->entrymax, sizeof (struct folder_file));
469 state->imap_browse = 0;
472 menu->data = state->entry;
475 /* get list of all files/newsgroups with mask */
476 static int examine_directory (MUTTMENU *menu, struct browser_state *state,
477 char *d, const char *prefix)
480 if (option (OPTNEWS))
484 NNTP_SERVER *news = CurrentNewsSrv;
486 /* mutt_buffy_check (0); */
487 init_state (state, menu);
489 for (tmp = news->list; tmp; tmp = tmp->next)
491 if (!(data = (NNTP_DATA *)tmp->data))
493 if (prefix && *prefix && strncmp (prefix, data->group,
494 strlen (prefix)) != 0)
496 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
498 add_folder (menu, state, data->group, NULL, data, data->new);
502 #endif /* USE_NNTP */
507 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
510 while (stat (d, &s) == -1)
514 /* The last used directory is deleted, try to use the parent dir. */
515 char *c = strrchr (d, '/');
527 if (!S_ISDIR (s.st_mode))
529 mutt_error (_("%s is not a directory."), d);
533 mutt_buffy_check (0);
535 if ((dp = opendir (d)) == NULL)
541 init_state (state, menu);
543 while ((de = readdir (dp)) != NULL)
545 if (mutt_strcmp (de->d_name, ".") == 0)
546 continue; /* we don't need . */
548 if (prefix && *prefix && mutt_strncmp (prefix, de->d_name, mutt_strlen (prefix)) != 0)
550 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
553 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
554 if (lstat (buffer, &s) == -1)
557 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
558 (! S_ISLNK (s.st_mode)))
562 while (tmp && mutt_strcmp (buffer, tmp->path))
564 add_folder (menu, state, de->d_name, &s, NULL, (tmp) ? tmp->new : 0);
568 browser_sort (state);
572 /* get list of mailboxes/subscribed newsgroups */
573 static int examine_mailboxes (MUTTMENU *menu, struct browser_state *state)
576 char buffer[LONG_STRING];
579 if (option (OPTNEWS))
583 NNTP_SERVER *news = CurrentNewsSrv;
585 /* mutt_buffy_check (0); */
586 init_state (state, menu);
588 for (tmp = news->list; tmp; tmp = tmp->next)
590 if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
591 (data->subscribed && (!option (OPTSHOWONLYUNREAD) || data->unread))))
592 add_folder (menu, state, data->group, NULL, data, data->new);
598 BUFFY *tmp = Incoming;
602 mutt_buffy_check (0);
604 init_state (state, menu);
609 if (mx_is_imap (tmp->path))
611 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
616 if (mx_is_pop (tmp->path))
618 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
623 if (mx_is_nntp (tmp->path))
625 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
629 if (lstat (tmp->path, &s) == -1)
632 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
633 (! S_ISLNK (s.st_mode)))
636 strfcpy (buffer, NONULL(tmp->path), sizeof (buffer));
637 mutt_pretty_mailbox (buffer);
639 add_folder (menu, state, buffer, &s, NULL, tmp->new);
641 while ((tmp = tmp->next));
643 browser_sort (state);
647 static int select_file_search (MUTTMENU *menu, regex_t *re, int n)
650 if (option (OPTNEWS))
651 return (regexec (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
653 return (regexec (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
656 static void folder_entry (char *s, size_t slen, MUTTMENU *menu, int num)
660 folder.ff = &((struct folder_file *) menu->data)[num];
664 if (option (OPTNEWS))
665 mutt_FormatString (s, slen, NONULL(GroupFormat), newsgroup_format_str,
666 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
669 mutt_FormatString (s, slen, NONULL(FolderFormat), folder_format_str,
670 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
673 static void init_menu (struct browser_state *state, MUTTMENU *menu, char *title,
674 size_t titlelen, int buffy)
676 char path[_POSIX_PATH_MAX];
678 menu->max = state->entrylen;
680 if(menu->current >= menu->max)
681 menu->current = menu->max - 1;
682 if (menu->current < 0)
684 if (menu->top > menu->current)
690 if (option (OPTNEWS))
693 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
695 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
696 CurrentNewsSrv->conn->account.host);
701 snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
704 strfcpy (path, LastDir, sizeof (path));
705 mutt_pretty_mailbox (path);
707 if (state->imap_browse && option (OPTIMAPLSUB))
708 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
709 path, NONULL (Mask.pattern));
712 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
713 path, NONULL(Mask.pattern));
715 menu->redraw = REDRAW_FULL;
718 static int file_tag (MUTTMENU *menu, int n, int m)
720 struct folder_file *ff = &(((struct folder_file *)menu->data)[n]);
722 if (S_ISDIR (ff->mode) || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name)))
724 mutt_error _("Can't attach a directory!");
729 ff->tagged = (m >= 0 ? m : !ff->tagged);
731 return ff->tagged - ot;
734 void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *numfiles)
736 char buf[_POSIX_PATH_MAX];
737 char prefix[_POSIX_PATH_MAX] = "";
738 char helpstr[SHORT_STRING];
740 struct browser_state state;
743 int i, killPrefix = 0;
744 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
745 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
746 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
748 buffy = buffy && folder;
750 memset (&state, 0, sizeof (struct browser_state));
753 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
756 if (option (OPTNEWS))
759 strfcpy (prefix, f, sizeof (prefix));
764 /* default state for news reader mode is browse subscribed newsgroups */
766 for (list = CurrentNewsSrv->list; list; list = list->next)
768 NNTP_DATA *data = (NNTP_DATA *) list->data;
770 if (data && data->subscribed)
782 mutt_expand_path (f, flen);
786 init_state (&state, NULL);
787 state.imap_browse = 1;
788 imap_browse (f, &state);
789 strfcpy (LastDir, state.folder, sizeof (LastDir));
794 for (i = mutt_strlen (f) - 1; i > 0 && f[i] != '/' ; i--);
799 if (i > sizeof (LastDir) - 1) i = sizeof (LastDir) - 1;
800 strncpy (LastDir, f, i);
805 getcwd (LastDir, sizeof (LastDir));
806 safe_strcat (LastDir, sizeof (LastDir), "/");
807 safe_strncat (LastDir, sizeof (LastDir), f, i);
813 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
815 getcwd (LastDir, sizeof (LastDir));
818 if (i <= 0 && f[0] != '/')
819 strfcpy (prefix, f, sizeof (prefix));
821 strfcpy (prefix, f + i + 1, sizeof (prefix));
830 getcwd (LastDir, sizeof (LastDir));
831 else if (!LastDir[0])
832 strfcpy (LastDir, NONULL(Maildir), sizeof (LastDir));
835 if (!buffy && mx_is_imap (LastDir))
837 init_state (&state, NULL);
838 state.imap_browse = 1;
839 imap_browse (LastDir, &state);
848 if (examine_mailboxes (NULL, &state) == -1)
853 if (!state.imap_browse)
855 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
858 menu = mutt_new_menu ();
859 menu->menu = MENU_FOLDER;
860 menu->make_entry = folder_entry;
861 menu->search = select_file_search;
863 menu->data = state.entry;
865 menu->tag = file_tag;
867 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
869 (option (OPTNEWS)) ? FolderNewsHelp :
873 init_menu (&state, menu, title, sizeof (title), buffy);
877 switch (i = mutt_menuLoop (menu))
879 case OP_GENERIC_SELECT_ENTRY:
883 mutt_error _("No files match the file mask");
887 if (S_ISDIR (state.entry[menu->current].mode) ||
888 (S_ISLNK (state.entry[menu->current].mode) &&
889 link_is_dir (LastDir, state.entry[menu->current].name))
891 || state.entry[menu->current].inferiors
895 /* make sure this isn't a MH or maildir mailbox */
898 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
899 mutt_expand_path (buf, sizeof (buf));
902 else if (state.imap_browse)
904 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
908 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
910 if ((mx_get_magic (buf) <= 0)
912 || state.entry[menu->current].inferiors
916 char OldLastDir[_POSIX_PATH_MAX];
918 /* save the old directory */
919 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
921 if (mutt_strcmp (state.entry[menu->current].name, "..") == 0)
923 if (mutt_strcmp ("..", LastDir + mutt_strlen (LastDir) - 2) == 0)
924 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
927 char *p = strrchr (LastDir + 1, '/');
933 if (LastDir[0] == '/')
936 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
942 strfcpy (LastDir, state.entry[menu->current].name, sizeof (LastDir));
943 mutt_expand_path (LastDir, sizeof (LastDir));
946 else if (state.imap_browse)
951 strfcpy (LastDir, state.entry[menu->current].name,
953 /* tack on delimiter here */
954 n = strlen (LastDir)+1;
956 /* special case "" needs no delimiter */
957 url_parse_ciss (&url, state.entry[menu->current].name);
959 (state.entry[menu->current].delim != '\0') &&
960 (n < sizeof (LastDir)))
963 LastDir[n-1] = state.entry[menu->current].delim;
969 char tmp[_POSIX_PATH_MAX];
970 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name, sizeof (tmp));
971 strfcpy (LastDir, tmp, sizeof (LastDir));
974 destroy_state (&state);
982 if (state.imap_browse)
984 init_state (&state, NULL);
985 state.imap_browse = 1;
986 imap_browse (LastDir, &state);
987 menu->data = state.entry;
991 if (examine_directory (menu, &state, LastDir, prefix) == -1)
993 /* try to restore the old values */
994 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
995 if (examine_directory (menu, &state, LastDir, prefix) == -1)
997 strfcpy (LastDir, NONULL(Homedir), sizeof (LastDir));
1003 init_menu (&state, menu, title, sizeof (title), buffy);
1009 if (buffy || option (OPTNEWS)) /* news have not path */
1014 strfcpy (f, state.entry[menu->current].name, flen);
1015 mutt_expand_path (f, flen);
1018 else if (state.imap_browse)
1019 strfcpy (f, state.entry[menu->current].name, flen);
1022 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
1024 /* Fall through to OP_EXIT */
1035 *numfiles = menu->tagged;
1036 tfiles = safe_calloc (*numfiles, sizeof (char *));
1037 for (i = 0, j = 0; i < state.entrylen; i++)
1039 struct folder_file ff = state.entry[i];
1040 char full[_POSIX_PATH_MAX];
1043 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1044 mutt_expand_path (full, sizeof (full));
1045 tfiles[j++] = safe_strdup (full);
1050 else if (f[0]) /* no tagged entries. return selected entry */
1053 tfiles = safe_calloc (*numfiles, sizeof (char *));
1054 mutt_expand_path (f, flen);
1055 tfiles[0] = safe_strdup (f);
1060 destroy_state (&state);
1061 mutt_menuDestroy (&menu);
1064 case OP_BROWSER_TELL:
1066 mutt_message("%s", state.entry[menu->current].name);
1070 case OP_BROWSER_TOGGLE_LSUB:
1071 if (option (OPTIMAPLSUB))
1073 unset_option (OPTIMAPLSUB);
1077 set_option (OPTIMAPLSUB);
1079 mutt_ungetch (0, OP_CHECK_NEW);
1082 case OP_CREATE_MAILBOX:
1083 if (!state.imap_browse)
1084 mutt_error (_("Create is only supported for IMAP mailboxes"));
1087 imap_mailbox_create (LastDir);
1088 /* TODO: find a way to detect if the new folder would appear in
1089 * this window, and insert it without starting over. */
1090 destroy_state (&state);
1091 init_state (&state, NULL);
1092 state.imap_browse = 1;
1093 imap_browse (LastDir, &state);
1094 menu->data = state.entry;
1097 init_menu (&state, menu, title, sizeof (title), buffy);
1098 MAYBE_REDRAW (menu->redraw);
1102 case OP_DELETE_MAILBOX:
1103 if (!state.entry[menu->current].imap)
1104 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1107 char msg[SHORT_STRING];
1109 int nentry = menu->current;
1111 imap_parse_path (state.entry[nentry].name, &mx);
1112 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1114 if (mutt_yesorno (msg, M_NO) == M_YES)
1116 if (!imap_delete_mailbox (Context, mx))
1118 /* free the mailbox from the browser */
1119 FREE (&((state.entry)[nentry].name));
1120 FREE (&((state.entry)[nentry].desc));
1121 /* and move all other entries up */
1122 if (nentry+1 < state.entrylen)
1123 memmove (state.entry + nentry, state.entry + nentry + 1,
1124 sizeof (struct folder_file) * (state.entrylen - (nentry+1)));
1126 mutt_message _("Mailbox deleted.");
1127 init_menu (&state, menu, title, sizeof (title), buffy);
1128 MAYBE_REDRAW (menu->redraw);
1132 mutt_message _("Mailbox not deleted.");
1138 case OP_CHANGE_DIRECTORY:
1141 if (option (OPTNEWS))
1145 strfcpy (buf, LastDir, sizeof (buf));
1147 if (!state.imap_browse)
1150 /* add '/' at the end of the directory name if not already there */
1151 int len=mutt_strlen(LastDir);
1152 if (len && LastDir[len-1] != '/' && sizeof (buf) > len)
1156 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1160 mutt_expand_path (buf, sizeof (buf));
1162 if (mx_is_imap (buf))
1164 strfcpy (LastDir, buf, sizeof (LastDir));
1165 destroy_state (&state);
1166 init_state (&state, NULL);
1167 state.imap_browse = 1;
1168 imap_browse (LastDir, &state);
1169 menu->data = state.entry;
1172 init_menu (&state, menu, title, sizeof (title), buffy);
1176 if (stat (buf, &st) == 0)
1178 if (S_ISDIR (st.st_mode))
1180 destroy_state (&state);
1181 if (examine_directory (menu, &state, buf, prefix) == 0)
1182 strfcpy (LastDir, buf, sizeof (LastDir));
1185 mutt_error _("Error scanning directory.");
1186 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1188 mutt_menuDestroy (&menu);
1194 init_menu (&state, menu, title, sizeof (title), buffy);
1197 mutt_error (_("%s is not a directory."), buf);
1202 MAYBE_REDRAW (menu->redraw);
1207 strfcpy (buf, NONULL(Mask.pattern), sizeof (buf));
1208 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0)
1210 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1215 /* assume that the user wants to see everything */
1217 strfcpy (buf, ".", sizeof (buf));
1226 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1228 regerror (err, rx, buf, sizeof (buf));
1231 mutt_error ("%s", buf);
1235 mutt_str_replace (&Mask.pattern, buf);
1241 destroy_state (&state);
1243 if (state.imap_browse)
1245 init_state (&state, NULL);
1246 state.imap_browse = 1;
1247 imap_browse (LastDir, &state);
1248 menu->data = state.entry;
1249 init_menu (&state, menu, title, sizeof (title), buffy);
1253 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1254 init_menu (&state, menu, title, sizeof (title), buffy);
1257 mutt_error _("Error scanning directory.");
1258 mutt_menuDestroy (&menu);
1262 if (!state.entrylen)
1264 mutt_error _("No files match the file mask");
1269 MAYBE_REDRAW (menu->redraw);
1273 case OP_SORT_REVERSE:
1277 int reverse = (i == OP_SORT_REVERSE);
1279 switch (mutt_multi_choice ((reverse) ?
1280 _("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ") :
1281 _("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1284 case -1: /* abort */
1288 case 1: /* (d)ate */
1289 BrowserSort = SORT_DATE;
1292 case 2: /* (a)lpha */
1293 BrowserSort = SORT_SUBJECT;
1296 case 3: /* si(z)e */
1297 BrowserSort = SORT_SIZE;
1300 case 4: /* do(n)'t sort */
1301 BrowserSort = SORT_ORDER;
1307 BrowserSort |= reverse ? SORT_REVERSE : 0;
1308 browser_sort (&state);
1309 menu->redraw = REDRAW_FULL;
1314 case OP_TOGGLE_MAILBOXES:
1318 destroy_state (&state);
1324 if (examine_mailboxes (menu, &state) == -1)
1328 else if (mx_is_imap (LastDir))
1330 init_state (&state, NULL);
1331 state.imap_browse = 1;
1332 imap_browse (LastDir, &state);
1333 menu->data = state.entry;
1336 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1338 init_menu (&state, menu, title, sizeof (title), buffy);
1345 case OP_BROWSER_NEW_FILE:
1347 snprintf (buf, sizeof (buf), "%s/", LastDir);
1348 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) == 0)
1350 strfcpy (f, buf, flen);
1351 destroy_state (&state);
1352 mutt_menuDestroy (&menu);
1355 MAYBE_REDRAW (menu->redraw);
1358 case OP_BROWSER_VIEW_FILE:
1359 if (!state.entrylen)
1361 mutt_error _("No files match the file mask");
1366 if (state.entry[menu->current].selectable)
1368 strfcpy (f, state.entry[menu->current].name, flen);
1369 destroy_state (&state);
1370 mutt_menuDestroy (&menu);
1375 if (S_ISDIR (state.entry[menu->current].mode) ||
1376 (S_ISLNK (state.entry[menu->current].mode) &&
1377 link_is_dir (LastDir, state.entry[menu->current].name)))
1379 mutt_error _("Can't view a directory");
1385 char buf[_POSIX_PATH_MAX];
1387 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
1388 b = mutt_make_file_attach (buf);
1391 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1392 mutt_free_body (&b);
1393 menu->redraw = REDRAW_FULL;
1396 mutt_error _("Error trying to view file");
1403 if (option (OPTNEWS))
1405 struct folder_file *f = &state.entry[menu->current];
1408 if (i == OP_CATCHUP)
1409 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1411 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1416 struct folder_file ff;
1417 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1420 folder.ff->name = f->name;
1421 folder.ff->st = NULL;
1422 folder.ff->is_new = nd->new;
1425 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1426 newsgroup_format_str, (unsigned long) &folder,
1427 M_FORMAT_ARROWCURSOR);
1428 f->desc = safe_strdup (buffer); */
1429 if (menu->current + 1 < menu->max)
1431 menu->redraw = REDRAW_MOTION_RESYNCH;
1436 case OP_LOAD_ACTIVE:
1437 if (!option (OPTNEWS))
1444 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next)
1446 if ((data = (NNTP_DATA *)tmp->data))
1450 nntp_get_active (CurrentNewsSrv);
1452 destroy_state (&state);
1454 examine_mailboxes (menu, &state);
1456 examine_directory (menu, &state, NULL, NULL);
1457 init_menu (&state, menu, title, sizeof (title), buffy);
1459 #endif /* USE_NNTP */
1461 #if defined USE_IMAP || defined USE_NNTP
1462 case OP_BROWSER_SUBSCRIBE:
1463 case OP_BROWSER_UNSUBSCRIBE:
1466 case OP_SUBSCRIBE_PATTERN:
1467 case OP_UNSUBSCRIBE_PATTERN:
1468 if (option (OPTNEWS))
1470 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1472 int j = menu->current;
1474 NNTP_SERVER *news = CurrentNewsSrv;
1476 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN)
1482 if (i == OP_SUBSCRIBE_PATTERN)
1483 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1485 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1486 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0])
1492 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1494 regerror (err, rx, buf, sizeof (buf));
1497 mutt_error ("%s", buf);
1500 menu->redraw = REDRAW_FULL;
1503 else if (!state.entrylen)
1505 mutt_error _("No newsgroups match the mask");
1509 for ( ; j < state.entrylen; j++)
1511 struct folder_file *f = &state.entry[j];
1513 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1514 regexec (rx, f->name, 0, NULL, 0) == 0)
1516 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1517 nd = mutt_newsgroup_subscribe (news, f->name);
1519 nd = mutt_newsgroup_unsubscribe (news, f->name);
1523 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1525 folder.name = f->name;
1527 folder.new = nd->new;
1530 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1531 newsgroup_format_str, (unsigned long) &folder,
1532 M_FORMAT_ARROWCURSOR);
1533 f->desc = safe_strdup (buffer);
1536 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE)
1538 if (menu->current + 1 < menu->max)
1540 menu->redraw = REDRAW_MOTION_RESYNCH;
1544 if (i == OP_SUBSCRIBE_PATTERN)
1546 LIST *grouplist = NULL;
1549 grouplist = news->list;
1550 for (; grouplist; grouplist = grouplist->next)
1552 nd = (NNTP_DATA *) grouplist->data;
1553 if (nd && nd->group && !nd->subscribed)
1555 if (regexec (rx, nd->group, 0, NULL, 0) == 0)
1557 mutt_newsgroup_subscribe (news, nd->group);
1558 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1562 init_menu (&state, menu, title, sizeof (title), buffy);
1564 mutt_newsrc_update (news);
1565 nntp_clear_cacheindex (news);
1566 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1572 #endif /* USE_IMAP && USE_NNTP */
1573 #endif /* USE_NNTP */
1576 if (i == OP_BROWSER_SUBSCRIBE)
1577 imap_subscribe (state.entry[menu->current].name, 1);
1579 imap_subscribe (state.entry[menu->current].name, 0);
1581 #endif /* USE_IMAP */
1588 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));