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.
17 #include "mutt_curses.h"
18 #include "mutt_menu.h"
27 #include "imap/mx_imap.h"
47 static struct mapping_t FolderHelp[] = {
48 {N_("Exit"), OP_EXIT},
49 {N_("Chdir"), OP_CHANGE_DIRECTORY},
50 {N_("Mask"), OP_ENTER_MASK},
51 {N_("Help"), OP_HELP},
56 static struct mapping_t FolderNewsHelp[] = {
57 {N_("Exit"), OP_EXIT},
58 {N_("List"), OP_TOGGLE_MAILBOXES},
59 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
60 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
61 {N_("Catchup"), OP_CATCHUP},
62 {N_("Mask"), OP_ENTER_MASK},
63 {N_("Help"), OP_HELP},
68 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++) {
82 mem_free (&((state->entry)[c].name));
83 mem_free (&((state->entry)[c].desc));
84 mem_free (&((state->entry)[c].st));
87 mem_free (&state->folder);
89 mem_free (&state->entry);
92 static int browser_compare_subject (const void *a, const void *b)
94 struct folder_file *pa = (struct folder_file *) a;
95 struct folder_file *pb = (struct folder_file *) b;
97 int r = str_coll (pa->name, pb->name);
99 return ((BrowserSort & SORT_REVERSE) ? -r : r);
102 static int browser_compare_date (const void *a, const void *b)
104 struct folder_file *pa = (struct folder_file *) a;
105 struct folder_file *pb = (struct folder_file *) b;
107 int r = pa->mtime - pb->mtime;
109 return ((BrowserSort & SORT_REVERSE) ? -r : r);
112 static int browser_compare_size (const void *a, const void *b)
114 struct folder_file *pa = (struct folder_file *) a;
115 struct folder_file *pb = (struct folder_file *) b;
117 int r = pa->size - pb->size;
119 return ((BrowserSort & SORT_REVERSE) ? -r : r);
122 static void browser_sort (struct browser_state *state)
124 int (*f) (const void *, const void *);
126 switch (BrowserSort & SORT_MASK) {
131 if (option (OPTNEWS))
134 f = browser_compare_date;
138 if (option (OPTNEWS))
141 f = browser_compare_size;
145 f = browser_compare_subject;
148 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
151 static int link_is_dir (const char *folder, const char *path)
154 char fullpath[_POSIX_PATH_MAX];
156 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
158 if (stat (fullpath, &st) == 0)
159 return (S_ISDIR (st.st_mode));
164 static const char *folder_format_str (char *dest, size_t destlen, char op,
165 const char *src, const char *fmt,
166 const char *ifstring,
167 const char *elsestring,
168 unsigned long data, format_flag flags)
170 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
171 char date[16], *t_fmt;
173 FOLDER *folder = (FOLDER *) data;
176 int optional = (flags & M_FORMAT_OPTIONAL);
180 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
181 snprintf (dest, destlen, tmp, folder->num + 1);
185 if (folder->ff->st != NULL) {
188 tnow - folder->ff->st->st_mtime <
189 31536000 ? "%b %d %H:%M" : "%b %d %Y";
190 strftime (date, sizeof (date), t_fmt,
191 localtime (&folder->ff->st->st_mtime));
192 mutt_format_s (dest, destlen, fmt, date);
195 mutt_format_s (dest, destlen, fmt, "");
203 if (folder->ff->imap)
204 s = NONULL (folder->ff->desc);
207 s = NONULL (folder->ff->name);
209 snprintf (fn, sizeof (fn), "%s%s", s,
210 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
211 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
212 ((folder->ff->st->st_mode & S_IXUSR) !=
213 0 ? "*" : ""))) : "");
215 mutt_format_s (dest, destlen, fmt, fn);
219 if (folder->ff->st != NULL) {
220 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
221 S_ISDIR (folder->ff->st->
222 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
223 st_mode) ? 'l' : '-'),
224 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
225 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
226 (folder->ff->st->st_mode & S_ISUID) !=
227 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
229 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
230 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
231 (folder->ff->st->st_mode & S_ISGID) !=
232 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
234 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
235 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
236 (folder->ff->st->st_mode & S_ISVTX) !=
237 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
239 mutt_format_s (dest, destlen, fmt, permission);
242 else if (folder->ff->imap) {
243 /* mark folders with subfolders AND mail */
244 snprintf (permission, sizeof (permission), "IMAP %c",
245 (folder->ff->inferiors
246 && folder->ff->selectable) ? '+' : ' ');
247 mutt_format_s (dest, destlen, fmt, permission);
251 mutt_format_s (dest, destlen, fmt, "");
255 if (folder->ff->st != NULL) {
256 if ((gr = getgrgid (folder->ff->st->st_gid)))
257 mutt_format_s (dest, destlen, fmt, gr->gr_name);
259 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
260 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
264 mutt_format_s (dest, destlen, fmt, "");
268 if (folder->ff->st != NULL) {
269 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
270 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
273 mutt_format_s (dest, destlen, fmt, "");
278 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
280 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
281 snprintf (dest, destlen, tmp, folder->ff->new);
283 else if (!folder->ff->new)
288 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
289 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
293 if (folder->ff->st != NULL) {
294 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
295 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
298 mutt_format_s (dest, destlen, fmt, "");
302 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
303 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
307 if (folder->ff->st != NULL) {
308 if ((pw = getpwuid (folder->ff->st->st_uid)))
309 mutt_format_s (dest, destlen, fmt, pw->pw_name);
311 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
312 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
316 mutt_format_s (dest, destlen, fmt, "");
320 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
321 snprintf (dest, destlen, tmp, op);
326 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
327 else if (flags & M_FORMAT_OPTIONAL)
328 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
334 static const char *newsgroup_format_str (char *dest, size_t destlen, char op,
335 const char *src, const char *fmt,
336 const char *ifstring,
337 const char *elsestring,
341 char fn[SHORT_STRING], tmp[SHORT_STRING];
342 FOLDER *folder = (FOLDER *) data;
346 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
347 snprintf (dest, destlen, tmp, folder->num + 1);
351 strncpy (fn, folder->ff->name, sizeof (fn) - 1);
352 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
353 snprintf (dest, destlen, tmp, fn);
357 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
358 if (folder->ff->nd->subscribed)
359 snprintf (dest, destlen, tmp, ' ');
361 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
365 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
366 if (folder->ff->nd->deleted)
367 snprintf (dest, destlen, tmp, 'D');
369 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
373 if (flags & M_FORMAT_OPTIONAL) {
374 if (folder->ff->nd->unread != 0)
375 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
378 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
381 else if (Context && Context->data == folder->ff->nd) {
382 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
383 snprintf (dest, destlen, tmp, Context->unread);
386 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
387 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
392 if (Context && Context->data == folder->ff->nd) {
393 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
394 snprintf (dest, destlen, tmp, Context->new);
396 else if (option (OPTMARKOLD) &&
397 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
398 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
399 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
400 snprintf (dest, destlen, tmp,
401 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
404 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
405 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
410 if (folder->ff->nd->desc != NULL) {
411 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
412 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
415 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
416 snprintf (dest, destlen, tmp, "");
422 #endif /* USE_NNTP */
424 static void add_folder (MUTTMENU * m, struct browser_state *state,
425 const char *name, const struct stat *s,
428 if (state->entrylen == state->entrymax) {
429 /* need to allocate more space */
430 mem_realloc (&state->entry,
431 sizeof (struct folder_file) * (state->entrymax += 256));
432 memset (&state->entry[state->entrylen], 0,
433 sizeof (struct folder_file) * 256);
435 m->data = state->entry;
439 (state->entry)[state->entrylen].mode = s->st_mode;
440 (state->entry)[state->entrylen].mtime = s->st_mtime;
441 (state->entry)[state->entrylen].size = s->st_size;
443 (state->entry)[state->entrylen].st = mem_malloc (sizeof (struct stat));
444 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
447 (state->entry)[state->entrylen].new = new;
448 (state->entry)[state->entrylen].name = str_dup (name);
449 (state->entry)[state->entrylen].desc = str_dup (name);
451 (state->entry)[state->entrylen].imap = 0;
454 if (option (OPTNEWS))
455 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
460 static void init_state (struct browser_state *state, MUTTMENU * menu)
463 state->entrymax = 256;
465 (struct folder_file *) mem_calloc (state->entrymax,
466 sizeof (struct folder_file));
468 state->imap_browse = 0;
471 menu->data = state->entry;
474 /* get list of all files/newsgroups with mask */
475 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
476 char *d, const char *prefix)
479 if (option (OPTNEWS)) {
482 NNTP_SERVER *news = CurrentNewsSrv;
484 /* buffy_check (0); */
485 init_state (state, menu);
487 for (tmp = news->list; tmp; tmp = tmp->next) {
488 if (!(data = (NNTP_DATA *) tmp->data))
490 if (prefix && *prefix && strncmp (prefix, data->group,
491 str_len (prefix)) != 0)
493 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
495 add_folder (menu, state, data->group, NULL, data, data->new);
499 #endif /* USE_NNTP */
504 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
507 while (stat (d, &s) == -1) {
508 if (errno == ENOENT) {
509 /* The last used directory is deleted, try to use the parent dir. */
510 char *c = strrchr (d, '/');
521 if (!S_ISDIR (s.st_mode)) {
522 mutt_error (_("%s is not a directory."), d);
528 if ((dp = opendir (d)) == NULL) {
533 init_state (state, menu);
535 while ((de = readdir (dp)) != NULL) {
536 if (str_cmp (de->d_name, ".") == 0)
537 continue; /* we don't need . */
539 if (prefix && *prefix
540 && str_ncmp (prefix, de->d_name, str_len (prefix)) != 0)
542 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
545 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
546 if (lstat (buffer, &s) == -1)
549 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
550 (!S_ISLNK (s.st_mode)))
553 i = buffy_lookup (buffer);
554 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
558 sidebar_draw (CurrentMenu);
559 browser_sort (state);
563 /* get list of mailboxes/subscribed newsgroups */
564 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
567 char buffer[LONG_STRING];
570 if (option (OPTNEWS)) {
573 NNTP_SERVER *news = CurrentNewsSrv;
575 /* buffy_check (0); */
576 init_state (state, menu);
578 for (tmp = news->list; tmp; tmp = tmp->next) {
579 if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
585 add_folder (menu, state, data->group, NULL, data, data->new);
598 init_state (state, menu);
600 for (i = 0; i < Incoming->length; i++) {
601 tmp = (BUFFY*) Incoming->data[i];
602 tmp->magic = mx_get_magic (tmp->path);
604 if (tmp->magic == M_IMAP) {
605 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
610 if (tmp->magic == M_POP) {
611 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
616 if (tmp->magic == M_NNTP) {
617 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
621 if (lstat (tmp->path, &s) == -1)
624 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
625 (!S_ISLNK (s.st_mode)))
628 strfcpy (buffer, NONULL (tmp->path), sizeof (buffer));
629 mutt_pretty_mailbox (buffer);
631 add_folder (menu, state, buffer, &s, NULL, tmp->new);
634 browser_sort (state);
638 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
641 if (option (OPTNEWS))
643 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
646 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
649 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
653 folder.ff = &((struct folder_file *) menu->data)[num];
657 if (option (OPTNEWS))
658 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
659 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
662 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
663 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
666 static void init_menu (struct browser_state *state, MUTTMENU * menu,
667 char *title, size_t titlelen, int buffy)
669 char path[_POSIX_PATH_MAX];
671 menu->max = state->entrylen;
673 if (menu->current >= menu->max)
674 menu->current = menu->max - 1;
675 if (menu->current < 0)
677 if (menu->top > menu->current)
683 if (option (OPTNEWS)) {
685 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
687 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
688 CurrentNewsSrv->conn->account.host);
693 snprintf (title, titlelen, _("Mailboxes [%d]"), buffy_check (0));
695 strfcpy (path, LastDir, sizeof (path));
696 mutt_pretty_mailbox (path);
698 if (state->imap_browse && option (OPTIMAPLSUB))
699 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
700 path, NONULL (Mask.pattern));
703 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
704 path, NONULL (Mask.pattern));
706 menu->redraw = REDRAW_FULL;
709 static int file_tag (MUTTMENU * menu, int n, int m)
711 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
714 if (S_ISDIR (ff->mode)
715 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
716 mutt_error _("Can't attach a directory!");
722 ff->tagged = (m >= 0 ? m : !ff->tagged);
724 return ff->tagged - ot;
727 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
730 char buf[_POSIX_PATH_MAX];
731 char prefix[_POSIX_PATH_MAX] = "";
732 char helpstr[SHORT_STRING];
734 struct browser_state state;
737 int i, killPrefix = 0;
738 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
739 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
740 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
742 buffy = buffy && folder;
744 memset (&state, 0, sizeof (struct browser_state));
747 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
750 if (option (OPTNEWS)) {
752 strfcpy (prefix, f, sizeof (prefix));
756 /* default state for news reader mode is browse subscribed newsgroups */
758 for (list = CurrentNewsSrv->list; list; list = list->next) {
759 NNTP_DATA *data = (NNTP_DATA *) list->data;
761 if (data && data->subscribed) {
771 mutt_expand_path (f, flen);
773 if (imap_is_magic (f, NULL) == M_IMAP) {
774 init_state (&state, NULL);
775 state.imap_browse = 1;
776 if (!imap_browse (f, &state))
777 strfcpy (LastDir, state.folder, sizeof (LastDir));
781 for (i = str_len (f) - 1; i > 0 && f[i] != '/'; i--);
784 if (i > sizeof (LastDir) - 1)
785 i = sizeof (LastDir) - 1;
786 strncpy (LastDir, f, i);
790 getcwd (LastDir, sizeof (LastDir));
791 str_cat (LastDir, sizeof (LastDir), "/");
792 str_ncat (LastDir, sizeof (LastDir), f, i);
797 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
799 getcwd (LastDir, sizeof (LastDir));
802 if (i <= 0 && f[0] != '/')
803 strfcpy (prefix, f, sizeof (prefix));
805 strfcpy (prefix, f + i + 1, sizeof (prefix));
813 getcwd (LastDir, sizeof (LastDir));
814 else if (!LastDir[0])
815 strfcpy (LastDir, NONULL (Maildir), sizeof (LastDir));
818 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
819 init_state (&state, NULL);
820 state.imap_browse = 1;
821 imap_browse (LastDir, &state);
829 if (examine_mailboxes (NULL, &state) == -1)
834 if (!state.imap_browse)
836 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
839 menu = mutt_new_menu ();
840 menu->menu = MENU_FOLDER;
841 menu->make_entry = folder_entry;
842 menu->search = select_file_search;
844 menu->data = state.entry;
846 menu->tag = file_tag;
848 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
850 (option (OPTNEWS)) ? FolderNewsHelp :
854 init_menu (&state, menu, title, sizeof (title), buffy);
857 switch (i = mutt_menuLoop (menu)) {
858 case OP_GENERIC_SELECT_ENTRY:
860 if (!state.entrylen) {
861 mutt_error _("No files match the file mask");
866 if (S_ISDIR (state.entry[menu->current].mode) ||
867 (S_ISLNK (state.entry[menu->current].mode) &&
868 link_is_dir (LastDir, state.entry[menu->current].name))
870 || state.entry[menu->current].inferiors
873 /* make sure this isn't a MH or maildir mailbox */
875 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
876 mutt_expand_path (buf, sizeof (buf));
879 else if (state.imap_browse) {
880 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
884 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
887 if ((mx_get_magic (buf) <= 0)
889 || state.entry[menu->current].inferiors
892 char OldLastDir[_POSIX_PATH_MAX];
894 /* save the old directory */
895 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
897 if (str_cmp (state.entry[menu->current].name, "..") == 0) {
898 if (str_cmp ("..", LastDir + str_len (LastDir) - 2) == 0)
899 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
901 char *p = strrchr (LastDir + 1, '/');
906 if (LastDir[0] == '/')
909 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
914 strfcpy (LastDir, state.entry[menu->current].name,
916 mutt_expand_path (LastDir, sizeof (LastDir));
919 else if (state.imap_browse) {
923 strfcpy (LastDir, state.entry[menu->current].name,
925 /* tack on delimiter here */
926 n = str_len (LastDir) + 1;
928 /* special case "" needs no delimiter */
929 url_parse_ciss (&url, state.entry[menu->current].name);
931 (state.entry[menu->current].delim != '\0') &&
932 (n < sizeof (LastDir))) {
934 LastDir[n - 1] = state.entry[menu->current].delim;
939 char tmp[_POSIX_PATH_MAX];
941 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name,
943 strfcpy (LastDir, tmp, sizeof (LastDir));
946 destroy_state (&state);
953 if (state.imap_browse) {
954 init_state (&state, NULL);
955 state.imap_browse = 1;
956 imap_browse (LastDir, &state);
957 menu->data = state.entry;
961 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
962 /* try to restore the old values */
963 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
964 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
965 strfcpy (LastDir, NONULL (Homedir), sizeof (LastDir));
971 init_menu (&state, menu, title, sizeof (title), buffy);
977 if (buffy || option (OPTNEWS)) /* news have not path */
982 strfcpy (f, state.entry[menu->current].name, flen);
983 mutt_expand_path (f, flen);
986 else if (state.imap_browse)
987 strfcpy (f, state.entry[menu->current].name, flen);
990 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
992 /* Fall through to OP_EXIT */
1001 *numfiles = menu->tagged;
1002 tfiles = mem_calloc (*numfiles, sizeof (char *));
1003 for (i = 0, j = 0; i < state.entrylen; i++) {
1004 struct folder_file ff = state.entry[i];
1005 char full[_POSIX_PATH_MAX];
1008 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1009 mutt_expand_path (full, sizeof (full));
1010 tfiles[j++] = str_dup (full);
1015 else if (f[0]) { /* no tagged entries. return selected entry */
1017 tfiles = mem_calloc (*numfiles, sizeof (char *));
1018 mutt_expand_path (f, flen);
1019 tfiles[0] = str_dup (f);
1024 destroy_state (&state);
1025 mutt_menuDestroy (&menu);
1028 case OP_BROWSER_TELL:
1030 mutt_message ("%s", state.entry[menu->current].name);
1034 case OP_BROWSER_TOGGLE_LSUB:
1035 if (option (OPTIMAPLSUB)) {
1036 unset_option (OPTIMAPLSUB);
1039 set_option (OPTIMAPLSUB);
1041 mutt_ungetch (0, OP_CHECK_NEW);
1044 case OP_CREATE_MAILBOX:
1045 if (!state.imap_browse)
1046 mutt_error (_("Create is only supported for IMAP mailboxes"));
1048 imap_mailbox_create (LastDir);
1049 /* TODO: find a way to detect if the new folder would appear in
1050 * this window, and insert it without starting over. */
1051 destroy_state (&state);
1052 init_state (&state, NULL);
1053 state.imap_browse = 1;
1054 imap_browse (LastDir, &state);
1055 menu->data = state.entry;
1058 init_menu (&state, menu, title, sizeof (title), buffy);
1059 MAYBE_REDRAW (menu->redraw);
1063 case OP_RENAME_MAILBOX:
1064 if (!state.entry[menu->current].imap)
1065 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1067 int nentry = menu->current;
1069 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1070 destroy_state (&state);
1071 init_state (&state, NULL);
1072 state.imap_browse = 1;
1073 imap_browse (LastDir, &state);
1074 menu->data = state.entry;
1077 init_menu (&state, menu, title, sizeof (title), buffy);
1078 MAYBE_REDRAW (menu->redraw);
1083 case OP_DELETE_MAILBOX:
1084 if (!state.entry[menu->current].imap)
1085 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1087 char msg[SHORT_STRING];
1089 int nentry = menu->current;
1091 imap_parse_path (state.entry[nentry].name, &mx);
1092 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1094 if (mutt_yesorno (msg, M_NO) == M_YES) {
1095 if (!imap_delete_mailbox (Context, mx)) {
1096 /* free the mailbox from the browser */
1097 mem_free (&((state.entry)[nentry].name));
1098 mem_free (&((state.entry)[nentry].desc));
1099 /* and move all other entries up */
1100 if (nentry + 1 < state.entrylen)
1101 memmove (state.entry + nentry, state.entry + nentry + 1,
1102 sizeof (struct folder_file) * (state.entrylen -
1105 mutt_message _("Mailbox deleted.");
1107 init_menu (&state, menu, title, sizeof (title), buffy);
1108 MAYBE_REDRAW (menu->redraw);
1112 mutt_message _("Mailbox not deleted.");
1113 mem_free (&mx.mbox);
1118 case OP_CHANGE_DIRECTORY:
1121 if (option (OPTNEWS))
1125 strfcpy (buf, LastDir, sizeof (buf));
1127 if (!state.imap_browse)
1130 /* add '/' at the end of the directory name if not already there */
1131 int len = str_len (LastDir);
1133 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1137 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1140 mutt_expand_path (buf, sizeof (buf));
1142 if (imap_is_magic (buf, NULL) == M_IMAP) {
1143 strfcpy (LastDir, buf, sizeof (LastDir));
1144 destroy_state (&state);
1145 init_state (&state, NULL);
1146 state.imap_browse = 1;
1147 imap_browse (LastDir, &state);
1148 menu->data = state.entry;
1151 init_menu (&state, menu, title, sizeof (title), buffy);
1155 if (stat (buf, &st) == 0) {
1156 if (S_ISDIR (st.st_mode)) {
1157 destroy_state (&state);
1158 if (examine_directory (menu, &state, buf, prefix) == 0)
1159 strfcpy (LastDir, buf, sizeof (LastDir));
1161 mutt_error _("Error scanning directory.");
1163 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1164 mutt_menuDestroy (&menu);
1170 init_menu (&state, menu, title, sizeof (title), buffy);
1173 mutt_error (_("%s is not a directory."), buf);
1178 MAYBE_REDRAW (menu->redraw);
1183 strfcpy (buf, NONULL (Mask.pattern), sizeof (buf));
1184 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1185 regex_t *rx = (regex_t *) mem_malloc (sizeof (regex_t));
1190 /* assume that the user wants to see everything */
1192 strfcpy (buf, ".", sizeof (buf));
1200 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1201 regerror (err, rx, buf, sizeof (buf));
1204 mutt_error ("%s", buf);
1207 str_replace (&Mask.pattern, buf);
1209 mem_free (&Mask.rx);
1213 destroy_state (&state);
1215 if (state.imap_browse) {
1216 init_state (&state, NULL);
1217 state.imap_browse = 1;
1218 imap_browse (LastDir, &state);
1219 menu->data = state.entry;
1220 init_menu (&state, menu, title, sizeof (title), buffy);
1224 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1225 init_menu (&state, menu, title, sizeof (title), buffy);
1227 mutt_error _("Error scanning directory.");
1229 mutt_menuDestroy (&menu);
1233 if (!state.entrylen) {
1234 mutt_error _("No files match the file mask");
1240 MAYBE_REDRAW (menu->redraw);
1244 case OP_SORT_REVERSE:
1248 int reverse = (i == OP_SORT_REVERSE);
1250 switch (mutt_multi_choice ((reverse) ?
1252 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1255 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1257 case -1: /* abort */
1261 case 1: /* (d)ate */
1262 BrowserSort = SORT_DATE;
1265 case 2: /* (a)lpha */
1266 BrowserSort = SORT_SUBJECT;
1269 case 3: /* si(z)e */
1270 BrowserSort = SORT_SIZE;
1273 case 4: /* do(n)'t sort */
1274 BrowserSort = SORT_ORDER;
1279 BrowserSort |= reverse ? SORT_REVERSE : 0;
1280 browser_sort (&state);
1281 menu->redraw = REDRAW_FULL;
1286 case OP_TOGGLE_MAILBOXES:
1290 destroy_state (&state);
1295 if (examine_mailboxes (menu, &state) == -1)
1299 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1300 init_state (&state, NULL);
1301 state.imap_browse = 1;
1302 imap_browse (LastDir, &state);
1303 menu->data = state.entry;
1306 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1308 init_menu (&state, menu, title, sizeof (title), buffy);
1315 case OP_BROWSER_NEW_FILE:
1317 snprintf (buf, sizeof (buf), "%s/", LastDir);
1318 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1320 strfcpy (f, buf, flen);
1321 destroy_state (&state);
1322 mutt_menuDestroy (&menu);
1325 MAYBE_REDRAW (menu->redraw);
1328 case OP_BROWSER_VIEW_FILE:
1329 if (!state.entrylen) {
1330 mutt_error _("No files match the file mask");
1336 if (state.entry[menu->current].selectable) {
1337 strfcpy (f, state.entry[menu->current].name, flen);
1338 destroy_state (&state);
1339 mutt_menuDestroy (&menu);
1344 if (S_ISDIR (state.entry[menu->current].mode) ||
1345 (S_ISLNK (state.entry[menu->current].mode) &&
1346 link_is_dir (LastDir, state.entry[menu->current].name))) {
1347 mutt_error _("Can't view a directory");
1353 char buf[_POSIX_PATH_MAX];
1355 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
1357 b = mutt_make_file_attach (buf);
1359 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1360 mutt_free_body (&b);
1361 menu->redraw = REDRAW_FULL;
1364 mutt_error _("Error trying to view file");
1371 if (option (OPTNEWS)) {
1372 struct folder_file *f = &state.entry[menu->current];
1375 if (i == OP_CATCHUP)
1376 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1378 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1382 struct folder_file ff;
1383 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1386 folder.ff->name = f->name;
1387 folder.ff->st = NULL;
1388 folder.ff->is_new = nd->new;
1390 mem_free (&f->desc);
1391 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1392 newsgroup_format_str, (unsigned long) &folder,
1393 M_FORMAT_ARROWCURSOR);
1394 f->desc = str_dup (buffer); */
1395 if (menu->current + 1 < menu->max)
1397 menu->redraw = REDRAW_MOTION_RESYNCH;
1402 case OP_LOAD_ACTIVE:
1403 if (!option (OPTNEWS))
1410 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1411 if ((data = (NNTP_DATA *) tmp->data))
1415 nntp_get_active (CurrentNewsSrv);
1417 destroy_state (&state);
1419 examine_mailboxes (menu, &state);
1421 examine_directory (menu, &state, NULL, NULL);
1422 init_menu (&state, menu, title, sizeof (title), buffy);
1424 #endif /* USE_NNTP */
1426 #if defined USE_IMAP || defined USE_NNTP
1427 case OP_BROWSER_SUBSCRIBE:
1428 case OP_BROWSER_UNSUBSCRIBE:
1431 case OP_SUBSCRIBE_PATTERN:
1432 case OP_UNSUBSCRIBE_PATTERN:
1433 if (option (OPTNEWS)) {
1434 regex_t *rx = (regex_t *) mem_malloc (sizeof (regex_t));
1436 int j = menu->current;
1438 NNTP_SERVER *news = CurrentNewsSrv;
1440 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1445 if (i == OP_SUBSCRIBE_PATTERN)
1446 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1448 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1449 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1454 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1455 regerror (err, rx, buf, sizeof (buf));
1458 mutt_error ("%s", buf);
1461 menu->redraw = REDRAW_FULL;
1464 else if (!state.entrylen) {
1465 mutt_error _("No newsgroups match the mask");
1470 for (; j < state.entrylen; j++) {
1471 struct folder_file *f = &state.entry[j];
1473 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1474 regexec (rx, f->name, 0, NULL, 0) == 0) {
1475 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1476 nd = mutt_newsgroup_subscribe (news, f->name);
1478 nd = mutt_newsgroup_unsubscribe (news, f->name);
1482 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1484 folder.name = f->name;
1486 folder.new = nd->new;
1488 mem_free (&f->desc);
1489 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1490 newsgroup_format_str, (unsigned long) &folder,
1491 M_FORMAT_ARROWCURSOR);
1492 f->desc = str_dup (buffer);
1495 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1496 if (menu->current + 1 < menu->max)
1498 menu->redraw = REDRAW_MOTION_RESYNCH;
1502 if (i == OP_SUBSCRIBE_PATTERN) {
1503 LIST *grouplist = NULL;
1506 grouplist = news->list;
1507 for (; grouplist; grouplist = grouplist->next) {
1508 nd = (NNTP_DATA *) grouplist->data;
1509 if (nd && nd->group && !nd->subscribed) {
1510 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1511 mutt_newsgroup_subscribe (news, nd->group);
1512 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1516 init_menu (&state, menu, title, sizeof (title), buffy);
1518 mutt_newsrc_update (news);
1519 nntp_clear_cacheindex (news);
1520 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1526 #endif /* USE_IMAP && USE_NNTP */
1527 #endif /* USE_NNTP */
1530 if (i == OP_BROWSER_SUBSCRIBE)
1531 imap_subscribe (state.entry[menu->current].name, 1);
1533 imap_subscribe (state.entry[menu->current].name, 0);
1535 #endif /* USE_IMAP */
1542 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));