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.
16 #include "mutt_curses.h"
17 #include "mutt_menu.h"
26 #include "imap/mx_imap.h"
46 static struct mapping_t FolderHelp[] = {
47 {N_("Exit"), OP_EXIT},
48 {N_("Chdir"), OP_CHANGE_DIRECTORY},
49 {N_("Mask"), OP_ENTER_MASK},
50 {N_("Help"), OP_HELP},
55 static struct mapping_t FolderNewsHelp[] = {
56 {N_("Exit"), OP_EXIT},
57 {N_("List"), OP_TOGGLE_MAILBOXES},
58 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
59 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
60 {N_("Catchup"), OP_CATCHUP},
61 {N_("Mask"), OP_ENTER_MASK},
62 {N_("Help"), OP_HELP},
67 typedef struct folder_t {
68 struct folder_file *ff;
72 static char LastDir[_POSIX_PATH_MAX] = "";
73 static char LastDirBackup[_POSIX_PATH_MAX] = "";
75 /* Frees up the memory allocated for the local-global variables. */
76 static void destroy_state (struct browser_state *state)
80 for (c = 0; c < state->entrylen; c++) {
81 mem_free (&((state->entry)[c].name));
82 mem_free (&((state->entry)[c].desc));
83 mem_free (&((state->entry)[c].st));
86 mem_free (&state->folder);
88 mem_free (&state->entry);
91 static int browser_compare_subject (const void *a, const void *b)
93 struct folder_file *pa = (struct folder_file *) a;
94 struct folder_file *pb = (struct folder_file *) b;
96 int r = str_coll (pa->name, pb->name);
98 return ((BrowserSort & SORT_REVERSE) ? -r : r);
101 static int browser_compare_date (const void *a, const void *b)
103 struct folder_file *pa = (struct folder_file *) a;
104 struct folder_file *pb = (struct folder_file *) b;
106 int r = pa->mtime - pb->mtime;
108 return ((BrowserSort & SORT_REVERSE) ? -r : r);
111 static int browser_compare_size (const void *a, const void *b)
113 struct folder_file *pa = (struct folder_file *) a;
114 struct folder_file *pb = (struct folder_file *) b;
116 int r = pa->size - pb->size;
118 return ((BrowserSort & SORT_REVERSE) ? -r : r);
121 static void browser_sort (struct browser_state *state)
123 int (*f) (const void *, const void *);
125 switch (BrowserSort & SORT_MASK) {
130 if (option (OPTNEWS))
133 f = browser_compare_date;
137 if (option (OPTNEWS))
140 f = browser_compare_size;
144 f = browser_compare_subject;
147 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
150 static int link_is_dir (const char *folder, const char *path)
153 char fullpath[_POSIX_PATH_MAX];
155 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
157 if (stat (fullpath, &st) == 0)
158 return (S_ISDIR (st.st_mode));
163 static const char *folder_format_str (char *dest, size_t destlen, char op,
164 const char *src, const char *fmt,
165 const char *ifstring,
166 const char *elsestring,
167 unsigned long data, format_flag flags)
169 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
170 char date[16], *t_fmt;
172 FOLDER *folder = (FOLDER *) data;
175 int optional = (flags & M_FORMAT_OPTIONAL);
179 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
180 snprintf (dest, destlen, tmp, folder->num + 1);
184 if (folder->ff->st != NULL) {
187 tnow - folder->ff->st->st_mtime <
188 31536000 ? "%b %d %H:%M" : "%b %d %Y";
189 strftime (date, sizeof (date), t_fmt,
190 localtime (&folder->ff->st->st_mtime));
191 mutt_format_s (dest, destlen, fmt, date);
194 mutt_format_s (dest, destlen, fmt, "");
202 if (folder->ff->imap)
203 s = NONULL (folder->ff->desc);
206 s = NONULL (folder->ff->name);
208 snprintf (fn, sizeof (fn), "%s%s", s,
209 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
210 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
211 ((folder->ff->st->st_mode & S_IXUSR) !=
212 0 ? "*" : ""))) : "");
214 mutt_format_s (dest, destlen, fmt, fn);
218 if (folder->ff->st != NULL) {
219 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
220 S_ISDIR (folder->ff->st->
221 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
222 st_mode) ? 'l' : '-'),
223 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
224 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
225 (folder->ff->st->st_mode & S_ISUID) !=
226 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
228 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
229 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
230 (folder->ff->st->st_mode & S_ISGID) !=
231 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
233 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
234 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
235 (folder->ff->st->st_mode & S_ISVTX) !=
236 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
238 mutt_format_s (dest, destlen, fmt, permission);
241 else if (folder->ff->imap) {
242 /* mark folders with subfolders AND mail */
243 snprintf (permission, sizeof (permission), "IMAP %c",
244 (folder->ff->inferiors
245 && folder->ff->selectable) ? '+' : ' ');
246 mutt_format_s (dest, destlen, fmt, permission);
250 mutt_format_s (dest, destlen, fmt, "");
254 if (folder->ff->st != NULL) {
255 if ((gr = getgrgid (folder->ff->st->st_gid)))
256 mutt_format_s (dest, destlen, fmt, gr->gr_name);
258 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
259 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
263 mutt_format_s (dest, destlen, fmt, "");
267 if (folder->ff->st != NULL) {
268 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
269 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
272 mutt_format_s (dest, destlen, fmt, "");
277 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
279 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
280 snprintf (dest, destlen, tmp, folder->ff->new);
282 else if (!folder->ff->new)
287 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
288 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
292 if (folder->ff->st != NULL) {
293 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
294 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
297 mutt_format_s (dest, destlen, fmt, "");
301 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
302 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
306 if (folder->ff->st != NULL) {
307 if ((pw = getpwuid (folder->ff->st->st_uid)))
308 mutt_format_s (dest, destlen, fmt, pw->pw_name);
310 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
311 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
315 mutt_format_s (dest, destlen, fmt, "");
319 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
320 snprintf (dest, destlen, tmp, op);
325 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
326 else if (flags & M_FORMAT_OPTIONAL)
327 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
333 static const char *newsgroup_format_str (char *dest, size_t destlen, char op,
334 const char *src, const char *fmt,
335 const char *ifstring,
336 const char *elsestring,
340 char fn[SHORT_STRING], tmp[SHORT_STRING];
341 FOLDER *folder = (FOLDER *) data;
345 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
346 snprintf (dest, destlen, tmp, folder->num + 1);
350 strncpy (fn, folder->ff->name, sizeof (fn) - 1);
351 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
352 snprintf (dest, destlen, tmp, fn);
356 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
357 if (folder->ff->nd->subscribed)
358 snprintf (dest, destlen, tmp, ' ');
360 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
364 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
365 if (folder->ff->nd->deleted)
366 snprintf (dest, destlen, tmp, 'D');
368 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
372 if (flags & M_FORMAT_OPTIONAL) {
373 if (folder->ff->nd->unread != 0)
374 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
377 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
380 else if (Context && Context->data == folder->ff->nd) {
381 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
382 snprintf (dest, destlen, tmp, Context->unread);
385 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
386 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
391 if (Context && Context->data == folder->ff->nd) {
392 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
393 snprintf (dest, destlen, tmp, Context->new);
395 else if (option (OPTMARKOLD) &&
396 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
397 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
398 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
399 snprintf (dest, destlen, tmp,
400 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) {
410 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
411 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
414 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
415 snprintf (dest, destlen, tmp, "");
421 #endif /* USE_NNTP */
423 static void add_folder (MUTTMENU * m, struct browser_state *state,
424 const char *name, const struct stat *s,
427 if (state->entrylen == state->entrymax) {
428 /* need to allocate more space */
429 mem_realloc (&state->entry,
430 sizeof (struct folder_file) * (state->entrymax += 256));
431 memset (&state->entry[state->entrylen], 0,
432 sizeof (struct folder_file) * 256);
434 m->data = state->entry;
438 (state->entry)[state->entrylen].mode = s->st_mode;
439 (state->entry)[state->entrylen].mtime = s->st_mtime;
440 (state->entry)[state->entrylen].size = s->st_size;
442 (state->entry)[state->entrylen].st = mem_malloc (sizeof (struct stat));
443 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
446 (state->entry)[state->entrylen].new = new;
447 (state->entry)[state->entrylen].name = str_dup (name);
448 (state->entry)[state->entrylen].desc = str_dup (name);
450 (state->entry)[state->entrylen].imap = 0;
453 if (option (OPTNEWS))
454 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
459 static void init_state (struct browser_state *state, MUTTMENU * menu)
462 state->entrymax = 256;
464 (struct folder_file *) mem_calloc (state->entrymax,
465 sizeof (struct folder_file));
467 state->imap_browse = 0;
470 menu->data = state->entry;
473 /* get list of all files/newsgroups with mask */
474 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
475 char *d, const char *prefix)
478 if (option (OPTNEWS)) {
481 NNTP_SERVER *news = CurrentNewsSrv;
483 /* buffy_check (0); */
484 init_state (state, menu);
486 for (tmp = news->list; tmp; tmp = tmp->next) {
487 if (!(data = (NNTP_DATA *) tmp->data))
489 if (prefix && *prefix && strncmp (prefix, data->group,
490 str_len (prefix)) != 0)
492 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
494 add_folder (menu, state, data->group, NULL, data, data->new);
498 #endif /* USE_NNTP */
503 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
506 while (stat (d, &s) == -1) {
507 if (errno == ENOENT) {
508 /* The last used directory is deleted, try to use the parent dir. */
509 char *c = strrchr (d, '/');
520 if (!S_ISDIR (s.st_mode)) {
521 mutt_error (_("%s is not a directory."), d);
527 if ((dp = opendir (d)) == NULL) {
532 init_state (state, menu);
534 while ((de = readdir (dp)) != NULL) {
535 if (str_cmp (de->d_name, ".") == 0)
536 continue; /* we don't need . */
538 if (prefix && *prefix
539 && str_ncmp (prefix, de->d_name, str_len (prefix)) != 0)
541 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
544 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
545 if (lstat (buffer, &s) == -1)
548 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
549 (!S_ISLNK (s.st_mode)))
552 i = buffy_lookup (buffer);
553 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
557 sidebar_draw (CurrentMenu);
558 browser_sort (state);
562 /* get list of mailboxes/subscribed newsgroups */
563 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
566 char buffer[LONG_STRING];
569 if (option (OPTNEWS)) {
572 NNTP_SERVER *news = CurrentNewsSrv;
574 /* buffy_check (0); */
575 init_state (state, menu);
577 for (tmp = news->list; tmp; tmp = tmp->next) {
578 if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
584 add_folder (menu, state, data->group, NULL, data, data->new);
597 init_state (state, menu);
599 for (i = 0; i < Incoming->length; i++) {
600 tmp = (BUFFY*) Incoming->data[i];
601 tmp->magic = mx_get_magic (tmp->path);
603 if (tmp->magic == M_IMAP) {
604 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
609 if (tmp->magic == M_POP) {
610 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
615 if (tmp->magic == M_NNTP) {
616 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
620 if (lstat (tmp->path, &s) == -1)
623 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
624 (!S_ISLNK (s.st_mode)))
627 strfcpy (buffer, NONULL (tmp->path), sizeof (buffer));
628 mutt_pretty_mailbox (buffer);
630 add_folder (menu, state, buffer, &s, NULL, tmp->new);
633 browser_sort (state);
637 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
640 if (option (OPTNEWS))
642 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
645 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
648 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
652 folder.ff = &((struct folder_file *) menu->data)[num];
656 if (option (OPTNEWS))
657 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
658 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
661 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
662 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
665 static void init_menu (struct browser_state *state, MUTTMENU * menu,
666 char *title, size_t titlelen, int buffy)
668 char path[_POSIX_PATH_MAX];
670 menu->max = state->entrylen;
672 if (menu->current >= menu->max)
673 menu->current = menu->max - 1;
674 if (menu->current < 0)
676 if (menu->top > menu->current)
682 if (option (OPTNEWS)) {
684 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
686 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
687 CurrentNewsSrv->conn->account.host);
692 snprintf (title, titlelen, _("Mailboxes [%d]"), buffy_check (0));
694 strfcpy (path, LastDir, sizeof (path));
695 mutt_pretty_mailbox (path);
697 if (state->imap_browse && option (OPTIMAPLSUB))
698 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
699 path, NONULL (Mask.pattern));
702 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
703 path, NONULL (Mask.pattern));
705 menu->redraw = REDRAW_FULL;
708 static int file_tag (MUTTMENU * menu, int n, int m)
710 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
713 if (S_ISDIR (ff->mode)
714 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
715 mutt_error _("Can't attach a directory!");
721 ff->tagged = (m >= 0 ? m : !ff->tagged);
723 return ff->tagged - ot;
726 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
729 char buf[_POSIX_PATH_MAX];
730 char prefix[_POSIX_PATH_MAX] = "";
731 char helpstr[SHORT_STRING];
733 struct browser_state state;
736 int i, killPrefix = 0;
737 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
738 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
739 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
741 buffy = buffy && folder;
743 memset (&state, 0, sizeof (struct browser_state));
746 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
749 if (option (OPTNEWS)) {
751 strfcpy (prefix, f, sizeof (prefix));
755 /* default state for news reader mode is browse subscribed newsgroups */
757 for (list = CurrentNewsSrv->list; list; list = list->next) {
758 NNTP_DATA *data = (NNTP_DATA *) list->data;
760 if (data && data->subscribed) {
770 mutt_expand_path (f, flen);
772 if (imap_is_magic (f, NULL) == M_IMAP) {
773 init_state (&state, NULL);
774 state.imap_browse = 1;
775 if (!imap_browse (f, &state))
776 strfcpy (LastDir, state.folder, sizeof (LastDir));
780 for (i = str_len (f) - 1; i > 0 && f[i] != '/'; i--);
783 if (i > sizeof (LastDir) - 1)
784 i = sizeof (LastDir) - 1;
785 strncpy (LastDir, f, i);
789 getcwd (LastDir, sizeof (LastDir));
790 str_cat (LastDir, sizeof (LastDir), "/");
791 str_ncat (LastDir, sizeof (LastDir), f, i);
796 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
798 getcwd (LastDir, sizeof (LastDir));
801 if (i <= 0 && f[0] != '/')
802 strfcpy (prefix, f, sizeof (prefix));
804 strfcpy (prefix, f + i + 1, sizeof (prefix));
812 getcwd (LastDir, sizeof (LastDir));
813 else if (!LastDir[0])
814 strfcpy (LastDir, NONULL (Maildir), sizeof (LastDir));
817 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
818 init_state (&state, NULL);
819 state.imap_browse = 1;
820 imap_browse (LastDir, &state);
828 if (examine_mailboxes (NULL, &state) == -1)
833 if (!state.imap_browse)
835 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
838 menu = mutt_new_menu ();
839 menu->menu = MENU_FOLDER;
840 menu->make_entry = folder_entry;
841 menu->search = select_file_search;
843 menu->data = state.entry;
845 menu->tag = file_tag;
847 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
849 (option (OPTNEWS)) ? FolderNewsHelp :
853 init_menu (&state, menu, title, sizeof (title), buffy);
856 switch (i = mutt_menuLoop (menu)) {
857 case OP_GENERIC_SELECT_ENTRY:
859 if (!state.entrylen) {
860 mutt_error _("No files match the file mask");
865 if (S_ISDIR (state.entry[menu->current].mode) ||
866 (S_ISLNK (state.entry[menu->current].mode) &&
867 link_is_dir (LastDir, state.entry[menu->current].name))
869 || state.entry[menu->current].inferiors
872 /* make sure this isn't a MH or maildir mailbox */
874 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
875 mutt_expand_path (buf, sizeof (buf));
878 else if (state.imap_browse) {
879 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
883 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
886 if ((mx_get_magic (buf) <= 0)
888 || state.entry[menu->current].inferiors
891 char OldLastDir[_POSIX_PATH_MAX];
893 /* save the old directory */
894 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
896 if (str_cmp (state.entry[menu->current].name, "..") == 0) {
897 if (str_cmp ("..", LastDir + str_len (LastDir) - 2) == 0)
898 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
900 char *p = strrchr (LastDir + 1, '/');
905 if (LastDir[0] == '/')
908 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
913 strfcpy (LastDir, state.entry[menu->current].name,
915 mutt_expand_path (LastDir, sizeof (LastDir));
918 else if (state.imap_browse) {
922 strfcpy (LastDir, state.entry[menu->current].name,
924 /* tack on delimiter here */
925 n = str_len (LastDir) + 1;
927 /* special case "" needs no delimiter */
928 url_parse_ciss (&url, state.entry[menu->current].name);
930 (state.entry[menu->current].delim != '\0') &&
931 (n < sizeof (LastDir))) {
933 LastDir[n - 1] = state.entry[menu->current].delim;
938 char tmp[_POSIX_PATH_MAX];
940 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name,
942 strfcpy (LastDir, tmp, sizeof (LastDir));
945 destroy_state (&state);
952 if (state.imap_browse) {
953 init_state (&state, NULL);
954 state.imap_browse = 1;
955 imap_browse (LastDir, &state);
956 menu->data = state.entry;
960 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
961 /* try to restore the old values */
962 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
963 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
964 strfcpy (LastDir, NONULL (Homedir), sizeof (LastDir));
970 init_menu (&state, menu, title, sizeof (title), buffy);
976 if (buffy || option (OPTNEWS)) /* news have not path */
981 strfcpy (f, state.entry[menu->current].name, flen);
982 mutt_expand_path (f, flen);
985 else if (state.imap_browse)
986 strfcpy (f, state.entry[menu->current].name, flen);
989 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
991 /* Fall through to OP_EXIT */
1000 *numfiles = menu->tagged;
1001 tfiles = mem_calloc (*numfiles, sizeof (char *));
1002 for (i = 0, j = 0; i < state.entrylen; i++) {
1003 struct folder_file ff = state.entry[i];
1004 char full[_POSIX_PATH_MAX];
1007 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1008 mutt_expand_path (full, sizeof (full));
1009 tfiles[j++] = str_dup (full);
1014 else if (f[0]) { /* no tagged entries. return selected entry */
1016 tfiles = mem_calloc (*numfiles, sizeof (char *));
1017 mutt_expand_path (f, flen);
1018 tfiles[0] = str_dup (f);
1023 destroy_state (&state);
1024 mutt_menuDestroy (&menu);
1027 case OP_BROWSER_TELL:
1029 mutt_message ("%s", state.entry[menu->current].name);
1033 case OP_BROWSER_TOGGLE_LSUB:
1034 if (option (OPTIMAPLSUB)) {
1035 unset_option (OPTIMAPLSUB);
1038 set_option (OPTIMAPLSUB);
1040 mutt_ungetch (0, OP_CHECK_NEW);
1043 case OP_CREATE_MAILBOX:
1044 if (!state.imap_browse)
1045 mutt_error (_("Create is only supported for IMAP mailboxes"));
1047 imap_mailbox_create (LastDir);
1048 /* TODO: find a way to detect if the new folder would appear in
1049 * this window, and insert it without starting over. */
1050 destroy_state (&state);
1051 init_state (&state, NULL);
1052 state.imap_browse = 1;
1053 imap_browse (LastDir, &state);
1054 menu->data = state.entry;
1057 init_menu (&state, menu, title, sizeof (title), buffy);
1058 MAYBE_REDRAW (menu->redraw);
1062 case OP_RENAME_MAILBOX:
1063 if (!state.entry[menu->current].imap)
1064 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1066 int nentry = menu->current;
1068 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1069 destroy_state (&state);
1070 init_state (&state, NULL);
1071 state.imap_browse = 1;
1072 imap_browse (LastDir, &state);
1073 menu->data = state.entry;
1076 init_menu (&state, menu, title, sizeof (title), buffy);
1077 MAYBE_REDRAW (menu->redraw);
1082 case OP_DELETE_MAILBOX:
1083 if (!state.entry[menu->current].imap)
1084 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1086 char msg[SHORT_STRING];
1088 int nentry = menu->current;
1090 imap_parse_path (state.entry[nentry].name, &mx);
1091 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1093 if (mutt_yesorno (msg, M_NO) == M_YES) {
1094 if (!imap_delete_mailbox (Context, mx)) {
1095 /* free the mailbox from the browser */
1096 mem_free (&((state.entry)[nentry].name));
1097 mem_free (&((state.entry)[nentry].desc));
1098 /* and move all other entries up */
1099 if (nentry + 1 < state.entrylen)
1100 memmove (state.entry + nentry, state.entry + nentry + 1,
1101 sizeof (struct folder_file) * (state.entrylen -
1104 mutt_message _("Mailbox deleted.");
1106 init_menu (&state, menu, title, sizeof (title), buffy);
1107 MAYBE_REDRAW (menu->redraw);
1111 mutt_message _("Mailbox not deleted.");
1112 mem_free (&mx.mbox);
1117 case OP_CHANGE_DIRECTORY:
1120 if (option (OPTNEWS))
1124 strfcpy (buf, LastDir, sizeof (buf));
1126 if (!state.imap_browse)
1129 /* add '/' at the end of the directory name if not already there */
1130 int len = str_len (LastDir);
1132 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1136 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1139 mutt_expand_path (buf, sizeof (buf));
1141 if (imap_is_magic (buf, NULL) == M_IMAP) {
1142 strfcpy (LastDir, buf, sizeof (LastDir));
1143 destroy_state (&state);
1144 init_state (&state, NULL);
1145 state.imap_browse = 1;
1146 imap_browse (LastDir, &state);
1147 menu->data = state.entry;
1150 init_menu (&state, menu, title, sizeof (title), buffy);
1154 if (stat (buf, &st) == 0) {
1155 if (S_ISDIR (st.st_mode)) {
1156 destroy_state (&state);
1157 if (examine_directory (menu, &state, buf, prefix) == 0)
1158 strfcpy (LastDir, buf, sizeof (LastDir));
1160 mutt_error _("Error scanning directory.");
1162 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1163 mutt_menuDestroy (&menu);
1169 init_menu (&state, menu, title, sizeof (title), buffy);
1172 mutt_error (_("%s is not a directory."), buf);
1177 MAYBE_REDRAW (menu->redraw);
1182 strfcpy (buf, NONULL (Mask.pattern), sizeof (buf));
1183 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1184 regex_t *rx = (regex_t *) mem_malloc (sizeof (regex_t));
1189 /* assume that the user wants to see everything */
1191 strfcpy (buf, ".", sizeof (buf));
1199 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1200 regerror (err, rx, buf, sizeof (buf));
1203 mutt_error ("%s", buf);
1206 str_replace (&Mask.pattern, buf);
1208 mem_free (&Mask.rx);
1212 destroy_state (&state);
1214 if (state.imap_browse) {
1215 init_state (&state, NULL);
1216 state.imap_browse = 1;
1217 imap_browse (LastDir, &state);
1218 menu->data = state.entry;
1219 init_menu (&state, menu, title, sizeof (title), buffy);
1223 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1224 init_menu (&state, menu, title, sizeof (title), buffy);
1226 mutt_error _("Error scanning directory.");
1228 mutt_menuDestroy (&menu);
1232 if (!state.entrylen) {
1233 mutt_error _("No files match the file mask");
1239 MAYBE_REDRAW (menu->redraw);
1243 case OP_SORT_REVERSE:
1247 int reverse = (i == OP_SORT_REVERSE);
1249 switch (mutt_multi_choice ((reverse) ?
1251 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1254 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1256 case -1: /* abort */
1260 case 1: /* (d)ate */
1261 BrowserSort = SORT_DATE;
1264 case 2: /* (a)lpha */
1265 BrowserSort = SORT_SUBJECT;
1268 case 3: /* si(z)e */
1269 BrowserSort = SORT_SIZE;
1272 case 4: /* do(n)'t sort */
1273 BrowserSort = SORT_ORDER;
1278 BrowserSort |= reverse ? SORT_REVERSE : 0;
1279 browser_sort (&state);
1280 menu->redraw = REDRAW_FULL;
1285 case OP_TOGGLE_MAILBOXES:
1289 destroy_state (&state);
1294 if (examine_mailboxes (menu, &state) == -1)
1298 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1299 init_state (&state, NULL);
1300 state.imap_browse = 1;
1301 imap_browse (LastDir, &state);
1302 menu->data = state.entry;
1305 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1307 init_menu (&state, menu, title, sizeof (title), buffy);
1314 case OP_BROWSER_NEW_FILE:
1316 snprintf (buf, sizeof (buf), "%s/", LastDir);
1317 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1319 strfcpy (f, buf, flen);
1320 destroy_state (&state);
1321 mutt_menuDestroy (&menu);
1324 MAYBE_REDRAW (menu->redraw);
1327 case OP_BROWSER_VIEW_FILE:
1328 if (!state.entrylen) {
1329 mutt_error _("No files match the file mask");
1335 if (state.entry[menu->current].selectable) {
1336 strfcpy (f, state.entry[menu->current].name, flen);
1337 destroy_state (&state);
1338 mutt_menuDestroy (&menu);
1343 if (S_ISDIR (state.entry[menu->current].mode) ||
1344 (S_ISLNK (state.entry[menu->current].mode) &&
1345 link_is_dir (LastDir, state.entry[menu->current].name))) {
1346 mutt_error _("Can't view a directory");
1352 char buf[_POSIX_PATH_MAX];
1354 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
1356 b = mutt_make_file_attach (buf);
1358 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1359 mutt_free_body (&b);
1360 menu->redraw = REDRAW_FULL;
1363 mutt_error _("Error trying to view file");
1370 if (option (OPTNEWS)) {
1371 struct folder_file *f = &state.entry[menu->current];
1374 if (i == OP_CATCHUP)
1375 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1377 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1381 struct folder_file ff;
1382 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1385 folder.ff->name = f->name;
1386 folder.ff->st = NULL;
1387 folder.ff->is_new = nd->new;
1389 mem_free (&f->desc);
1390 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1391 newsgroup_format_str, (unsigned long) &folder,
1392 M_FORMAT_ARROWCURSOR);
1393 f->desc = str_dup (buffer); */
1394 if (menu->current + 1 < menu->max)
1396 menu->redraw = REDRAW_MOTION_RESYNCH;
1401 case OP_LOAD_ACTIVE:
1402 if (!option (OPTNEWS))
1409 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1410 if ((data = (NNTP_DATA *) tmp->data))
1414 nntp_get_active (CurrentNewsSrv);
1416 destroy_state (&state);
1418 examine_mailboxes (menu, &state);
1420 examine_directory (menu, &state, NULL, NULL);
1421 init_menu (&state, menu, title, sizeof (title), buffy);
1423 #endif /* USE_NNTP */
1425 #if defined USE_IMAP || defined USE_NNTP
1426 case OP_BROWSER_SUBSCRIBE:
1427 case OP_BROWSER_UNSUBSCRIBE:
1430 case OP_SUBSCRIBE_PATTERN:
1431 case OP_UNSUBSCRIBE_PATTERN:
1432 if (option (OPTNEWS)) {
1433 regex_t *rx = (regex_t *) mem_malloc (sizeof (regex_t));
1435 int j = menu->current;
1437 NNTP_SERVER *news = CurrentNewsSrv;
1439 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1444 if (i == OP_SUBSCRIBE_PATTERN)
1445 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1447 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1448 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1453 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1454 regerror (err, rx, buf, sizeof (buf));
1457 mutt_error ("%s", buf);
1460 menu->redraw = REDRAW_FULL;
1463 else if (!state.entrylen) {
1464 mutt_error _("No newsgroups match the mask");
1469 for (; j < state.entrylen; j++) {
1470 struct folder_file *f = &state.entry[j];
1472 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1473 regexec (rx, f->name, 0, NULL, 0) == 0) {
1474 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1475 nd = mutt_newsgroup_subscribe (news, f->name);
1477 nd = mutt_newsgroup_unsubscribe (news, f->name);
1481 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1483 folder.name = f->name;
1485 folder.new = nd->new;
1487 mem_free (&f->desc);
1488 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1489 newsgroup_format_str, (unsigned long) &folder,
1490 M_FORMAT_ARROWCURSOR);
1491 f->desc = str_dup (buffer);
1494 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1495 if (menu->current + 1 < menu->max)
1497 menu->redraw = REDRAW_MOTION_RESYNCH;
1501 if (i == OP_SUBSCRIBE_PATTERN) {
1502 LIST *grouplist = NULL;
1505 grouplist = news->list;
1506 for (; grouplist; grouplist = grouplist->next) {
1507 nd = (NNTP_DATA *) grouplist->data;
1508 if (nd && nd->group && !nd->subscribed) {
1509 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1510 mutt_newsgroup_subscribe (news, nd->group);
1511 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1515 init_menu (&state, menu, title, sizeof (title), buffy);
1517 mutt_newsrc_update (news);
1518 nntp_clear_cacheindex (news);
1519 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1525 #endif /* USE_IMAP && USE_NNTP */
1526 #endif /* USE_NNTP */
1529 if (i == OP_BROWSER_SUBSCRIBE)
1530 imap_subscribe (state.entry[menu->current].name, 1);
1532 imap_subscribe (state.entry[menu->current].name, 0);
1534 #endif /* USE_IMAP */
1541 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));