2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
15 #include "mutt_curses.h"
16 #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 {
64 struct folder_file *ff;
68 static char LastDir[_POSIX_PATH_MAX] = "";
69 static char LastDirBackup[_POSIX_PATH_MAX] = "";
71 /* Frees up the memory allocated for the local-global variables. */
72 static void destroy_state (struct browser_state *state)
76 for (c = 0; c < state->entrylen; c++) {
77 FREE (&((state->entry)[c].name));
78 FREE (&((state->entry)[c].desc));
79 FREE (&((state->entry)[c].st));
82 FREE (&state->folder);
87 static int browser_compare_subject (const void *a, const void *b)
89 struct folder_file *pa = (struct folder_file *) a;
90 struct folder_file *pb = (struct folder_file *) b;
92 int r = safe_strcoll (pa->name, pb->name);
94 return ((BrowserSort & SORT_REVERSE) ? -r : r);
97 static int browser_compare_date (const void *a, const void *b)
99 struct folder_file *pa = (struct folder_file *) a;
100 struct folder_file *pb = (struct folder_file *) b;
102 int r = pa->mtime - pb->mtime;
104 return ((BrowserSort & SORT_REVERSE) ? -r : r);
107 static int browser_compare_size (const void *a, const void *b)
109 struct folder_file *pa = (struct folder_file *) a;
110 struct folder_file *pb = (struct folder_file *) b;
112 int r = pa->size - pb->size;
114 return ((BrowserSort & SORT_REVERSE) ? -r : r);
117 static void browser_sort (struct browser_state *state)
119 int (*f) (const void *, const void *);
121 switch (BrowserSort & SORT_MASK) {
126 if (option (OPTNEWS))
129 f = browser_compare_date;
133 if (option (OPTNEWS))
136 f = browser_compare_size;
140 f = browser_compare_subject;
143 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
146 static int link_is_dir (const char *folder, const char *path)
149 char fullpath[_POSIX_PATH_MAX];
151 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
153 if (stat (fullpath, &st) == 0)
154 return (S_ISDIR (st.st_mode));
159 static const char *folder_format_str (char *dest, size_t destlen, char op,
160 const char *src, const char *fmt,
161 const char *ifstring,
162 const char *elsestring,
163 unsigned long data, format_flag flags)
165 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
166 char date[16], *t_fmt;
168 FOLDER *folder = (FOLDER *) data;
171 int optional = (flags & M_FORMAT_OPTIONAL);
175 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
176 snprintf (dest, destlen, tmp, folder->num + 1);
180 if (folder->ff->st != NULL) {
183 tnow - folder->ff->st->st_mtime <
184 31536000 ? "%b %d %H:%M" : "%b %d %Y";
185 strftime (date, sizeof (date), t_fmt,
186 localtime (&folder->ff->st->st_mtime));
187 mutt_format_s (dest, destlen, fmt, date);
190 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) !=
208 0 ? "*" : ""))) : "");
210 mutt_format_s (dest, destlen, fmt, fn);
214 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->
217 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
218 st_mode) ? 'l' : '-'),
219 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
220 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
221 (folder->ff->st->st_mode & S_ISUID) !=
222 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
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) !=
227 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
229 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
230 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
231 (folder->ff->st->st_mode & S_ISVTX) !=
232 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
234 mutt_format_s (dest, destlen, fmt, permission);
237 else if (folder->ff->imap) {
238 /* mark folders with subfolders AND mail */
239 snprintf (permission, sizeof (permission), "IMAP %c",
240 (folder->ff->inferiors
241 && folder->ff->selectable) ? '+' : ' ');
242 mutt_format_s (dest, destlen, fmt, permission);
246 mutt_format_s (dest, destlen, fmt, "");
250 if (folder->ff->st != NULL) {
251 if ((gr = getgrgid (folder->ff->st->st_gid)))
252 mutt_format_s (dest, destlen, fmt, gr->gr_name);
254 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
255 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
259 mutt_format_s (dest, destlen, fmt, "");
263 if (folder->ff->st != NULL) {
264 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
265 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
268 mutt_format_s (dest, destlen, fmt, "");
273 if (mx_get_magic (folder->ff->desc) == M_IMAP) {
275 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
276 snprintf (dest, destlen, tmp, folder->ff->new);
278 else if (!folder->ff->new)
283 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
284 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
288 if (folder->ff->st != NULL) {
289 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
290 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
293 mutt_format_s (dest, destlen, fmt, "");
297 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
298 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
302 if (folder->ff->st != NULL) {
303 if ((pw = getpwuid (folder->ff->st->st_uid)))
304 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);
329 static const char *newsgroup_format_str (char *dest, size_t destlen, char op,
330 const char *src, const char *fmt,
331 const char *ifstring,
332 const char *elsestring,
336 char fn[SHORT_STRING], tmp[SHORT_STRING];
337 FOLDER *folder = (FOLDER *) data;
341 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
342 snprintf (dest, destlen, tmp, folder->num + 1);
346 strncpy (fn, folder->ff->name, sizeof (fn) - 1);
347 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
348 snprintf (dest, destlen, tmp, fn);
352 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
353 if (folder->ff->nd->subscribed)
354 snprintf (dest, destlen, tmp, ' ');
356 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
360 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
361 if (folder->ff->nd->deleted)
362 snprintf (dest, destlen, tmp, 'D');
364 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
368 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) {
377 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
378 snprintf (dest, destlen, tmp, Context->unread);
381 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
382 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
387 if (Context && Context->data == folder->ff->nd) {
388 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
389 snprintf (dest, destlen, tmp, Context->new);
391 else if (option (OPTMARKOLD) &&
392 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
393 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
394 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
395 snprintf (dest, destlen, tmp,
396 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
399 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
400 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
405 if (folder->ff->nd->desc != NULL) {
406 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
407 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
410 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
411 snprintf (dest, destlen, tmp, "");
417 #endif /* USE_NNTP */
419 static void add_folder (MUTTMENU * m, struct browser_state *state,
420 const char *name, const struct stat *s,
423 if (state->entrylen == state->entrymax) {
424 /* need to allocate more space */
425 safe_realloc (&state->entry,
426 sizeof (struct folder_file) * (state->entrymax += 256));
427 memset (&state->entry[state->entrylen], 0,
428 sizeof (struct folder_file) * 256);
430 m->data = state->entry;
434 (state->entry)[state->entrylen].mode = s->st_mode;
435 (state->entry)[state->entrylen].mtime = s->st_mtime;
436 (state->entry)[state->entrylen].size = s->st_size;
438 (state->entry)[state->entrylen].st = safe_malloc (sizeof (struct stat));
439 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
442 (state->entry)[state->entrylen].new = new;
443 (state->entry)[state->entrylen].name = safe_strdup (name);
444 (state->entry)[state->entrylen].desc = safe_strdup (name);
446 (state->entry)[state->entrylen].imap = 0;
449 if (option (OPTNEWS))
450 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
455 static void init_state (struct browser_state *state, MUTTMENU * menu)
458 state->entrymax = 256;
460 (struct folder_file *) safe_calloc (state->entrymax,
461 sizeof (struct folder_file));
463 state->imap_browse = 0;
466 menu->data = state->entry;
469 /* get list of all files/newsgroups with mask */
470 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
471 char *d, const char *prefix)
474 if (option (OPTNEWS)) {
477 NNTP_SERVER *news = CurrentNewsSrv;
479 /* mutt_buffy_check (0); */
480 init_state (state, menu);
482 for (tmp = news->list; tmp; tmp = tmp->next) {
483 if (!(data = (NNTP_DATA *) tmp->data))
485 if (prefix && *prefix && strncmp (prefix, data->group,
486 safe_strlen (prefix)) != 0)
488 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
490 add_folder (menu, state, data->group, NULL, data, data->new);
494 #endif /* USE_NNTP */
499 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
502 while (stat (d, &s) == -1) {
503 if (errno == ENOENT) {
504 /* The last used directory is deleted, try to use the parent dir. */
505 char *c = strrchr (d, '/');
516 if (!S_ISDIR (s.st_mode)) {
517 mutt_error (_("%s is not a directory."), d);
521 mutt_buffy_check (0);
523 if ((dp = opendir (d)) == NULL) {
528 init_state (state, menu);
530 while ((de = readdir (dp)) != NULL) {
531 if (safe_strcmp (de->d_name, ".") == 0)
532 continue; /* we don't need . */
534 if (prefix && *prefix
535 && safe_strncmp (prefix, de->d_name, safe_strlen (prefix)) != 0)
537 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
540 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
541 if (lstat (buffer, &s) == -1)
544 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
545 (!S_ISLNK (s.st_mode)))
548 i = buffy_lookup (buffer);
549 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
553 sidebar_draw (CurrentMenu);
554 browser_sort (state);
558 /* get list of mailboxes/subscribed newsgroups */
559 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
562 char buffer[LONG_STRING];
565 if (option (OPTNEWS)) {
568 NNTP_SERVER *news = CurrentNewsSrv;
570 /* mutt_buffy_check (0); */
571 init_state (state, menu);
573 for (tmp = news->list; tmp; tmp = tmp->next) {
574 if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
580 add_folder (menu, state, data->group, NULL, data, data->new);
591 mutt_buffy_check (0);
593 init_state (state, menu);
595 for (i = 0; i < Incoming->length; i++) {
596 tmp = (BUFFY*) Incoming->data[i];
598 if (mx_get_magic (tmp->path) == M_IMAP) {
599 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
604 if (mx_get_magic (tmp->path) == M_POP) {
605 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
610 if (mx_get_magic (tmp->path) == M_NNTP) {
611 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
615 if (lstat (tmp->path, &s) == -1)
618 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
619 (!S_ISLNK (s.st_mode)))
622 strfcpy (buffer, NONULL (tmp->path), sizeof (buffer));
623 mutt_pretty_mailbox (buffer);
625 add_folder (menu, state, buffer, &s, NULL, tmp->new);
628 browser_sort (state);
632 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
635 if (option (OPTNEWS))
637 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
640 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
643 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
647 folder.ff = &((struct folder_file *) menu->data)[num];
651 if (option (OPTNEWS))
652 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
653 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
656 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
657 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
660 static void init_menu (struct browser_state *state, MUTTMENU * menu,
661 char *title, size_t titlelen, int buffy)
663 char path[_POSIX_PATH_MAX];
665 menu->max = state->entrylen;
667 if (menu->current >= menu->max)
668 menu->current = menu->max - 1;
669 if (menu->current < 0)
671 if (menu->top > menu->current)
677 if (option (OPTNEWS)) {
679 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
681 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
682 CurrentNewsSrv->conn->account.host);
687 snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
689 strfcpy (path, LastDir, sizeof (path));
690 mutt_pretty_mailbox (path);
692 if (state->imap_browse && option (OPTIMAPLSUB))
693 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
694 path, NONULL (Mask.pattern));
697 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
698 path, NONULL (Mask.pattern));
700 menu->redraw = REDRAW_FULL;
703 static int file_tag (MUTTMENU * menu, int n, int m)
705 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
708 if (S_ISDIR (ff->mode)
709 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
710 mutt_error _("Can't attach a directory!");
716 ff->tagged = (m >= 0 ? m : !ff->tagged);
718 return ff->tagged - ot;
721 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
724 char buf[_POSIX_PATH_MAX];
725 char prefix[_POSIX_PATH_MAX] = "";
726 char helpstr[SHORT_STRING];
728 struct browser_state state;
731 int i, killPrefix = 0;
732 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
733 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
734 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
736 buffy = buffy && folder;
738 memset (&state, 0, sizeof (struct browser_state));
741 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
744 if (option (OPTNEWS)) {
746 strfcpy (prefix, f, sizeof (prefix));
750 /* default state for news reader mode is browse subscribed newsgroups */
752 for (list = CurrentNewsSrv->list; list; list = list->next) {
753 NNTP_DATA *data = (NNTP_DATA *) list->data;
755 if (data && data->subscribed) {
765 mutt_expand_path (f, flen);
767 if (mx_get_magic (f) == M_IMAP) {
768 init_state (&state, NULL);
769 state.imap_browse = 1;
770 imap_browse (f, &state);
771 strfcpy (LastDir, state.folder, sizeof (LastDir));
775 for (i = safe_strlen (f) - 1; i > 0 && f[i] != '/'; i--);
778 if (i > sizeof (LastDir) - 1)
779 i = sizeof (LastDir) - 1;
780 strncpy (LastDir, f, i);
784 getcwd (LastDir, sizeof (LastDir));
785 safe_strcat (LastDir, sizeof (LastDir), "/");
786 safe_strncat (LastDir, sizeof (LastDir), f, i);
791 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
793 getcwd (LastDir, sizeof (LastDir));
796 if (i <= 0 && f[0] != '/')
797 strfcpy (prefix, f, sizeof (prefix));
799 strfcpy (prefix, f + i + 1, sizeof (prefix));
807 getcwd (LastDir, sizeof (LastDir));
808 else if (!LastDir[0])
809 strfcpy (LastDir, NONULL (Maildir), sizeof (LastDir));
812 if (!buffy && mx_get_magic (LastDir) == M_IMAP) {
813 init_state (&state, NULL);
814 state.imap_browse = 1;
815 imap_browse (LastDir, &state);
823 if (examine_mailboxes (NULL, &state) == -1)
828 if (!state.imap_browse)
830 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
833 menu = mutt_new_menu ();
834 menu->menu = MENU_FOLDER;
835 menu->make_entry = folder_entry;
836 menu->search = select_file_search;
838 menu->data = state.entry;
840 menu->tag = file_tag;
842 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
844 (option (OPTNEWS)) ? FolderNewsHelp :
848 init_menu (&state, menu, title, sizeof (title), buffy);
851 switch (i = mutt_menuLoop (menu)) {
852 case OP_GENERIC_SELECT_ENTRY:
854 if (!state.entrylen) {
855 mutt_error _("No files match the file mask");
860 if (S_ISDIR (state.entry[menu->current].mode) ||
861 (S_ISLNK (state.entry[menu->current].mode) &&
862 link_is_dir (LastDir, state.entry[menu->current].name))
864 || state.entry[menu->current].inferiors
867 /* make sure this isn't a MH or maildir mailbox */
869 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
870 mutt_expand_path (buf, sizeof (buf));
873 else if (state.imap_browse) {
874 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
878 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
881 if ((mx_get_magic (buf) <= 0)
883 || state.entry[menu->current].inferiors
886 char OldLastDir[_POSIX_PATH_MAX];
888 /* save the old directory */
889 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
891 if (safe_strcmp (state.entry[menu->current].name, "..") == 0) {
892 if (safe_strcmp ("..", LastDir + safe_strlen (LastDir) - 2) == 0)
893 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
895 char *p = strrchr (LastDir + 1, '/');
900 if (LastDir[0] == '/')
903 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
908 strfcpy (LastDir, state.entry[menu->current].name,
910 mutt_expand_path (LastDir, sizeof (LastDir));
913 else if (state.imap_browse) {
917 strfcpy (LastDir, state.entry[menu->current].name,
919 /* tack on delimiter here */
920 n = safe_strlen (LastDir) + 1;
922 /* special case "" needs no delimiter */
923 url_parse_ciss (&url, state.entry[menu->current].name);
925 (state.entry[menu->current].delim != '\0') &&
926 (n < sizeof (LastDir))) {
928 LastDir[n - 1] = state.entry[menu->current].delim;
933 char tmp[_POSIX_PATH_MAX];
935 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name,
937 strfcpy (LastDir, tmp, sizeof (LastDir));
940 destroy_state (&state);
947 if (state.imap_browse) {
948 init_state (&state, NULL);
949 state.imap_browse = 1;
950 imap_browse (LastDir, &state);
951 menu->data = state.entry;
955 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
956 /* try to restore the old values */
957 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
958 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
959 strfcpy (LastDir, NONULL (Homedir), sizeof (LastDir));
965 init_menu (&state, menu, title, sizeof (title), buffy);
971 if (buffy || option (OPTNEWS)) /* news have not path */
976 strfcpy (f, state.entry[menu->current].name, flen);
977 mutt_expand_path (f, flen);
980 else if (state.imap_browse)
981 strfcpy (f, state.entry[menu->current].name, flen);
984 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
986 /* Fall through to OP_EXIT */
995 *numfiles = menu->tagged;
996 tfiles = safe_calloc (*numfiles, sizeof (char *));
997 for (i = 0, j = 0; i < state.entrylen; i++) {
998 struct folder_file ff = state.entry[i];
999 char full[_POSIX_PATH_MAX];
1002 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1003 mutt_expand_path (full, sizeof (full));
1004 tfiles[j++] = safe_strdup (full);
1009 else if (f[0]) { /* no tagged entries. return selected entry */
1011 tfiles = safe_calloc (*numfiles, sizeof (char *));
1012 mutt_expand_path (f, flen);
1013 tfiles[0] = safe_strdup (f);
1018 destroy_state (&state);
1019 mutt_menuDestroy (&menu);
1022 case OP_BROWSER_TELL:
1024 mutt_message ("%s", state.entry[menu->current].name);
1028 case OP_BROWSER_TOGGLE_LSUB:
1029 if (option (OPTIMAPLSUB)) {
1030 unset_option (OPTIMAPLSUB);
1033 set_option (OPTIMAPLSUB);
1035 mutt_ungetch (0, OP_CHECK_NEW);
1038 case OP_CREATE_MAILBOX:
1039 if (!state.imap_browse)
1040 mutt_error (_("Create is only supported for IMAP mailboxes"));
1042 imap_mailbox_create (LastDir);
1043 /* TODO: find a way to detect if the new folder would appear in
1044 * this window, and insert it without starting over. */
1045 destroy_state (&state);
1046 init_state (&state, NULL);
1047 state.imap_browse = 1;
1048 imap_browse (LastDir, &state);
1049 menu->data = state.entry;
1052 init_menu (&state, menu, title, sizeof (title), buffy);
1053 MAYBE_REDRAW (menu->redraw);
1057 case OP_RENAME_MAILBOX:
1058 if (!state.entry[menu->current].imap)
1059 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1061 int nentry = menu->current;
1063 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1064 destroy_state (&state);
1065 init_state (&state, NULL);
1066 state.imap_browse = 1;
1067 imap_browse (LastDir, &state);
1068 menu->data = state.entry;
1071 init_menu (&state, menu, title, sizeof (title), buffy);
1072 MAYBE_REDRAW (menu->redraw);
1077 case OP_DELETE_MAILBOX:
1078 if (!state.entry[menu->current].imap)
1079 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1081 char msg[SHORT_STRING];
1083 int nentry = menu->current;
1085 imap_parse_path (state.entry[nentry].name, &mx);
1086 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1088 if (mutt_yesorno (msg, M_NO) == M_YES) {
1089 if (!imap_delete_mailbox (Context, mx)) {
1090 /* free the mailbox from the browser */
1091 FREE (&((state.entry)[nentry].name));
1092 FREE (&((state.entry)[nentry].desc));
1093 /* and move all other entries up */
1094 if (nentry + 1 < state.entrylen)
1095 memmove (state.entry + nentry, state.entry + nentry + 1,
1096 sizeof (struct folder_file) * (state.entrylen -
1099 mutt_message _("Mailbox deleted.");
1101 init_menu (&state, menu, title, sizeof (title), buffy);
1102 MAYBE_REDRAW (menu->redraw);
1106 mutt_message _("Mailbox not deleted.");
1112 case OP_CHANGE_DIRECTORY:
1115 if (option (OPTNEWS))
1119 strfcpy (buf, LastDir, sizeof (buf));
1121 if (!state.imap_browse)
1124 /* add '/' at the end of the directory name if not already there */
1125 int len = safe_strlen (LastDir);
1127 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1131 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1134 mutt_expand_path (buf, sizeof (buf));
1136 if (mx_get_magic (buf) == M_IMAP) {
1137 strfcpy (LastDir, buf, sizeof (LastDir));
1138 destroy_state (&state);
1139 init_state (&state, NULL);
1140 state.imap_browse = 1;
1141 imap_browse (LastDir, &state);
1142 menu->data = state.entry;
1145 init_menu (&state, menu, title, sizeof (title), buffy);
1149 if (stat (buf, &st) == 0) {
1150 if (S_ISDIR (st.st_mode)) {
1151 destroy_state (&state);
1152 if (examine_directory (menu, &state, buf, prefix) == 0)
1153 strfcpy (LastDir, buf, sizeof (LastDir));
1155 mutt_error _("Error scanning directory.");
1157 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1158 mutt_menuDestroy (&menu);
1164 init_menu (&state, menu, title, sizeof (title), buffy);
1167 mutt_error (_("%s is not a directory."), buf);
1172 MAYBE_REDRAW (menu->redraw);
1177 strfcpy (buf, NONULL (Mask.pattern), sizeof (buf));
1178 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1179 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1184 /* assume that the user wants to see everything */
1186 strfcpy (buf, ".", sizeof (buf));
1194 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1195 regerror (err, rx, buf, sizeof (buf));
1198 mutt_error ("%s", buf);
1201 str_replace (&Mask.pattern, buf);
1207 destroy_state (&state);
1209 if (state.imap_browse) {
1210 init_state (&state, NULL);
1211 state.imap_browse = 1;
1212 imap_browse (LastDir, &state);
1213 menu->data = state.entry;
1214 init_menu (&state, menu, title, sizeof (title), buffy);
1218 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1219 init_menu (&state, menu, title, sizeof (title), buffy);
1221 mutt_error _("Error scanning directory.");
1223 mutt_menuDestroy (&menu);
1227 if (!state.entrylen) {
1228 mutt_error _("No files match the file mask");
1234 MAYBE_REDRAW (menu->redraw);
1238 case OP_SORT_REVERSE:
1242 int reverse = (i == OP_SORT_REVERSE);
1244 switch (mutt_multi_choice ((reverse) ?
1246 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1249 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1251 case -1: /* abort */
1255 case 1: /* (d)ate */
1256 BrowserSort = SORT_DATE;
1259 case 2: /* (a)lpha */
1260 BrowserSort = SORT_SUBJECT;
1263 case 3: /* si(z)e */
1264 BrowserSort = SORT_SIZE;
1267 case 4: /* do(n)'t sort */
1268 BrowserSort = SORT_ORDER;
1273 BrowserSort |= reverse ? SORT_REVERSE : 0;
1274 browser_sort (&state);
1275 menu->redraw = REDRAW_FULL;
1280 case OP_TOGGLE_MAILBOXES:
1284 destroy_state (&state);
1289 if (examine_mailboxes (menu, &state) == -1)
1293 else if (mx_get_magic (LastDir) == M_IMAP) {
1294 init_state (&state, NULL);
1295 state.imap_browse = 1;
1296 imap_browse (LastDir, &state);
1297 menu->data = state.entry;
1300 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1302 init_menu (&state, menu, title, sizeof (title), buffy);
1309 case OP_BROWSER_NEW_FILE:
1311 snprintf (buf, sizeof (buf), "%s/", LastDir);
1312 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1314 strfcpy (f, buf, flen);
1315 destroy_state (&state);
1316 mutt_menuDestroy (&menu);
1319 MAYBE_REDRAW (menu->redraw);
1322 case OP_BROWSER_VIEW_FILE:
1323 if (!state.entrylen) {
1324 mutt_error _("No files match the file mask");
1330 if (state.entry[menu->current].selectable) {
1331 strfcpy (f, state.entry[menu->current].name, flen);
1332 destroy_state (&state);
1333 mutt_menuDestroy (&menu);
1338 if (S_ISDIR (state.entry[menu->current].mode) ||
1339 (S_ISLNK (state.entry[menu->current].mode) &&
1340 link_is_dir (LastDir, state.entry[menu->current].name))) {
1341 mutt_error _("Can't view a directory");
1347 char buf[_POSIX_PATH_MAX];
1349 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
1351 b = mutt_make_file_attach (buf);
1353 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1354 mutt_free_body (&b);
1355 menu->redraw = REDRAW_FULL;
1358 mutt_error _("Error trying to view file");
1365 if (option (OPTNEWS)) {
1366 struct folder_file *f = &state.entry[menu->current];
1369 if (i == OP_CATCHUP)
1370 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1372 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1376 struct folder_file ff;
1377 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1380 folder.ff->name = f->name;
1381 folder.ff->st = NULL;
1382 folder.ff->is_new = nd->new;
1385 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1386 newsgroup_format_str, (unsigned long) &folder,
1387 M_FORMAT_ARROWCURSOR);
1388 f->desc = safe_strdup (buffer); */
1389 if (menu->current + 1 < menu->max)
1391 menu->redraw = REDRAW_MOTION_RESYNCH;
1396 case OP_LOAD_ACTIVE:
1397 if (!option (OPTNEWS))
1404 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1405 if ((data = (NNTP_DATA *) tmp->data))
1409 nntp_get_active (CurrentNewsSrv);
1411 destroy_state (&state);
1413 examine_mailboxes (menu, &state);
1415 examine_directory (menu, &state, NULL, NULL);
1416 init_menu (&state, menu, title, sizeof (title), buffy);
1418 #endif /* USE_NNTP */
1420 #if defined USE_IMAP || defined USE_NNTP
1421 case OP_BROWSER_SUBSCRIBE:
1422 case OP_BROWSER_UNSUBSCRIBE:
1425 case OP_SUBSCRIBE_PATTERN:
1426 case OP_UNSUBSCRIBE_PATTERN:
1427 if (option (OPTNEWS)) {
1428 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1430 int j = menu->current;
1432 NNTP_SERVER *news = CurrentNewsSrv;
1434 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1439 if (i == OP_SUBSCRIBE_PATTERN)
1440 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1442 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1443 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1448 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1449 regerror (err, rx, buf, sizeof (buf));
1452 mutt_error ("%s", buf);
1455 menu->redraw = REDRAW_FULL;
1458 else if (!state.entrylen) {
1459 mutt_error _("No newsgroups match the mask");
1464 for (; j < state.entrylen; j++) {
1465 struct folder_file *f = &state.entry[j];
1467 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1468 regexec (rx, f->name, 0, NULL, 0) == 0) {
1469 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1470 nd = mutt_newsgroup_subscribe (news, f->name);
1472 nd = mutt_newsgroup_unsubscribe (news, f->name);
1476 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1478 folder.name = f->name;
1480 folder.new = nd->new;
1483 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1484 newsgroup_format_str, (unsigned long) &folder,
1485 M_FORMAT_ARROWCURSOR);
1486 f->desc = safe_strdup (buffer);
1489 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1490 if (menu->current + 1 < menu->max)
1492 menu->redraw = REDRAW_MOTION_RESYNCH;
1496 if (i == OP_SUBSCRIBE_PATTERN) {
1497 LIST *grouplist = NULL;
1500 grouplist = news->list;
1501 for (; grouplist; grouplist = grouplist->next) {
1502 nd = (NNTP_DATA *) grouplist->data;
1503 if (nd && nd->group && !nd->subscribed) {
1504 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1505 mutt_newsgroup_subscribe (news, nd->group);
1506 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1510 init_menu (&state, menu, title, sizeof (title), buffy);
1512 mutt_newsrc_update (news);
1513 nntp_clear_cacheindex (news);
1514 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1520 #endif /* USE_IMAP && USE_NNTP */
1521 #endif /* USE_NNTP */
1524 if (i == OP_BROWSER_SUBSCRIBE)
1525 imap_subscribe (state.entry[menu->current].name, 1);
1527 imap_subscribe (state.entry[menu->current].name, 0);
1529 #endif /* USE_IMAP */
1536 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));