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 nntp_sync_sidebar (data);
491 if (prefix && *prefix && strncmp (prefix, data->group,
492 str_len (prefix)) != 0)
494 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
496 add_folder (menu, state, data->group, NULL, data, data->new);
498 sidebar_draw (CurrentMenu);
501 #endif /* USE_NNTP */
506 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
509 while (stat (d, &s) == -1) {
510 if (errno == ENOENT) {
511 /* The last used directory is deleted, try to use the parent dir. */
512 char *c = strrchr (d, '/');
523 if (!S_ISDIR (s.st_mode)) {
524 mutt_error (_("%s is not a directory."), d);
530 if ((dp = opendir (d)) == NULL) {
535 init_state (state, menu);
537 while ((de = readdir (dp)) != NULL) {
538 if (str_cmp (de->d_name, ".") == 0)
539 continue; /* we don't need . */
541 if (prefix && *prefix
542 && str_ncmp (prefix, de->d_name, str_len (prefix)) != 0)
544 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
547 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
548 if (lstat (buffer, &s) == -1)
551 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
552 (!S_ISLNK (s.st_mode)))
555 i = buffy_lookup (buffer);
556 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
560 sidebar_draw (CurrentMenu);
561 browser_sort (state);
565 /* get list of mailboxes/subscribed newsgroups */
566 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
569 char buffer[LONG_STRING];
572 if (option (OPTNEWS)) {
575 NNTP_SERVER *news = CurrentNewsSrv;
577 /* buffy_check (0); */
578 init_state (state, menu);
580 for (tmp = news->list; tmp; tmp = tmp->next) {
581 if ((data = (NNTP_DATA*) tmp->data) == NULL)
583 nntp_sync_sidebar (data);
584 if ((data->new || (data->subscribed &&
585 (!option (OPTSHOWONLYUNREAD)|| data->unread))))
586 add_folder (menu, state, data->group, NULL, data, data->new);
588 sidebar_draw (CurrentMenu);
600 init_state (state, menu);
602 for (i = 0; i < Incoming->length; i++) {
603 tmp = (BUFFY*) Incoming->data[i];
604 tmp->magic = mx_get_magic (tmp->path);
606 if (tmp->magic == M_IMAP) {
607 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
612 if (tmp->magic == M_POP) {
613 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
618 if (tmp->magic == M_NNTP) {
619 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
623 if (lstat (tmp->path, &s) == -1)
626 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
627 (!S_ISLNK (s.st_mode)))
630 strfcpy (buffer, NONULL (tmp->path), sizeof (buffer));
631 mutt_pretty_mailbox (buffer);
633 add_folder (menu, state, buffer, &s, NULL, tmp->new);
636 browser_sort (state);
640 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
643 if (option (OPTNEWS))
645 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
648 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
651 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
655 folder.ff = &((struct folder_file *) menu->data)[num];
659 if (option (OPTNEWS))
660 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
661 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
664 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
665 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
668 static void init_menu (struct browser_state *state, MUTTMENU * menu,
669 char *title, size_t titlelen, int buffy)
671 char path[_POSIX_PATH_MAX];
673 menu->max = state->entrylen;
675 if (menu->current >= menu->max)
676 menu->current = menu->max - 1;
677 if (menu->current < 0)
679 if (menu->top > menu->current)
685 if (option (OPTNEWS)) {
687 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
689 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
690 CurrentNewsSrv->conn->account.host);
695 snprintf (title, titlelen, _("Mailboxes [%d]"), buffy_check (0));
697 strfcpy (path, LastDir, sizeof (path));
698 mutt_pretty_mailbox (path);
700 if (state->imap_browse && option (OPTIMAPLSUB))
701 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
702 path, NONULL (Mask.pattern));
705 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
706 path, NONULL (Mask.pattern));
708 menu->redraw = REDRAW_FULL;
711 static int file_tag (MUTTMENU * menu, int n, int m)
713 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
716 if (S_ISDIR (ff->mode)
717 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
718 mutt_error _("Can't attach a directory!");
724 ff->tagged = (m >= 0 ? m : !ff->tagged);
726 return ff->tagged - ot;
729 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
732 char buf[_POSIX_PATH_MAX];
733 char prefix[_POSIX_PATH_MAX] = "";
734 char helpstr[SHORT_STRING];
736 struct browser_state state;
739 int i, killPrefix = 0;
740 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
741 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
742 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
744 buffy = buffy && folder;
746 memset (&state, 0, sizeof (struct browser_state));
749 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
752 if (option (OPTNEWS)) {
754 strfcpy (prefix, f, sizeof (prefix));
758 /* default state for news reader mode is browse subscribed newsgroups */
760 for (list = CurrentNewsSrv->list; list; list = list->next) {
761 NNTP_DATA *data = (NNTP_DATA *) list->data;
763 if (data && data->subscribed) {
773 mutt_expand_path (f, flen);
775 if (imap_is_magic (f, NULL) == M_IMAP) {
776 init_state (&state, NULL);
777 state.imap_browse = 1;
778 if (!imap_browse (f, &state))
779 strfcpy (LastDir, state.folder, sizeof (LastDir));
783 for (i = str_len (f) - 1; i > 0 && f[i] != '/'; i--);
786 if (i > sizeof (LastDir) - 1)
787 i = sizeof (LastDir) - 1;
788 strncpy (LastDir, f, i);
792 getcwd (LastDir, sizeof (LastDir));
793 str_cat (LastDir, sizeof (LastDir), "/");
794 str_ncat (LastDir, sizeof (LastDir), f, i);
799 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
801 getcwd (LastDir, sizeof (LastDir));
804 if (i <= 0 && f[0] != '/')
805 strfcpy (prefix, f, sizeof (prefix));
807 strfcpy (prefix, f + i + 1, sizeof (prefix));
815 getcwd (LastDir, sizeof (LastDir));
816 else if (!LastDir[0])
817 strfcpy (LastDir, NONULL (Maildir), sizeof (LastDir));
820 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
821 init_state (&state, NULL);
822 state.imap_browse = 1;
823 imap_browse (LastDir, &state);
831 if (examine_mailboxes (NULL, &state) == -1)
836 if (!state.imap_browse)
838 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
841 menu = mutt_new_menu ();
842 menu->menu = MENU_FOLDER;
843 menu->make_entry = folder_entry;
844 menu->search = select_file_search;
846 menu->data = state.entry;
848 menu->tag = file_tag;
850 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
852 (option (OPTNEWS)) ? FolderNewsHelp :
856 init_menu (&state, menu, title, sizeof (title), buffy);
859 switch (i = mutt_menuLoop (menu)) {
860 case OP_GENERIC_SELECT_ENTRY:
862 if (!state.entrylen) {
863 mutt_error _("No files match the file mask");
868 if (S_ISDIR (state.entry[menu->current].mode) ||
869 (S_ISLNK (state.entry[menu->current].mode) &&
870 link_is_dir (LastDir, state.entry[menu->current].name))
872 || state.entry[menu->current].inferiors
875 /* make sure this isn't a MH or maildir mailbox */
877 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
878 mutt_expand_path (buf, sizeof (buf));
881 else if (state.imap_browse) {
882 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
886 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
889 if ((mx_get_magic (buf) <= 0)
891 || state.entry[menu->current].inferiors
894 char OldLastDir[_POSIX_PATH_MAX];
896 /* save the old directory */
897 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
899 if (str_cmp (state.entry[menu->current].name, "..") == 0) {
900 if (str_cmp ("..", LastDir + str_len (LastDir) - 2) == 0)
901 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
903 char *p = strrchr (LastDir + 1, '/');
908 if (LastDir[0] == '/')
911 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
916 strfcpy (LastDir, state.entry[menu->current].name,
918 mutt_expand_path (LastDir, sizeof (LastDir));
921 else if (state.imap_browse) {
925 strfcpy (LastDir, state.entry[menu->current].name,
927 /* tack on delimiter here */
928 n = str_len (LastDir) + 1;
930 /* special case "" needs no delimiter */
931 url_parse_ciss (&url, state.entry[menu->current].name);
933 (state.entry[menu->current].delim != '\0') &&
934 (n < sizeof (LastDir))) {
936 LastDir[n - 1] = state.entry[menu->current].delim;
941 char tmp[_POSIX_PATH_MAX];
943 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name,
945 strfcpy (LastDir, tmp, sizeof (LastDir));
948 destroy_state (&state);
955 if (state.imap_browse) {
956 init_state (&state, NULL);
957 state.imap_browse = 1;
958 imap_browse (LastDir, &state);
959 menu->data = state.entry;
963 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
964 /* try to restore the old values */
965 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
966 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
967 strfcpy (LastDir, NONULL (Homedir), sizeof (LastDir));
973 init_menu (&state, menu, title, sizeof (title), buffy);
979 if (buffy || option (OPTNEWS)) /* news have not path */
984 strfcpy (f, state.entry[menu->current].name, flen);
985 mutt_expand_path (f, flen);
988 else if (state.imap_browse)
989 strfcpy (f, state.entry[menu->current].name, flen);
992 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
994 /* Fall through to OP_EXIT */
1003 *numfiles = menu->tagged;
1004 tfiles = mem_calloc (*numfiles, sizeof (char *));
1005 for (i = 0, j = 0; i < state.entrylen; i++) {
1006 struct folder_file ff = state.entry[i];
1007 char full[_POSIX_PATH_MAX];
1010 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1011 mutt_expand_path (full, sizeof (full));
1012 tfiles[j++] = str_dup (full);
1017 else if (f[0]) { /* no tagged entries. return selected entry */
1019 tfiles = mem_calloc (*numfiles, sizeof (char *));
1020 mutt_expand_path (f, flen);
1021 tfiles[0] = str_dup (f);
1026 destroy_state (&state);
1027 mutt_menuDestroy (&menu);
1030 case OP_BROWSER_TELL:
1032 mutt_message ("%s", state.entry[menu->current].name);
1036 case OP_BROWSER_TOGGLE_LSUB:
1037 if (option (OPTIMAPLSUB)) {
1038 unset_option (OPTIMAPLSUB);
1041 set_option (OPTIMAPLSUB);
1043 mutt_ungetch (0, OP_CHECK_NEW);
1046 case OP_CREATE_MAILBOX:
1047 if (!state.imap_browse)
1048 mutt_error (_("Create is only supported for IMAP mailboxes"));
1050 imap_mailbox_create (LastDir);
1051 /* TODO: find a way to detect if the new folder would appear in
1052 * this window, and insert it without starting over. */
1053 destroy_state (&state);
1054 init_state (&state, NULL);
1055 state.imap_browse = 1;
1056 imap_browse (LastDir, &state);
1057 menu->data = state.entry;
1060 init_menu (&state, menu, title, sizeof (title), buffy);
1061 MAYBE_REDRAW (menu->redraw);
1065 case OP_RENAME_MAILBOX:
1066 if (!state.entry[menu->current].imap)
1067 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1069 int nentry = menu->current;
1071 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1072 destroy_state (&state);
1073 init_state (&state, NULL);
1074 state.imap_browse = 1;
1075 imap_browse (LastDir, &state);
1076 menu->data = state.entry;
1079 init_menu (&state, menu, title, sizeof (title), buffy);
1080 MAYBE_REDRAW (menu->redraw);
1085 case OP_DELETE_MAILBOX:
1086 if (!state.entry[menu->current].imap)
1087 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1089 char msg[SHORT_STRING];
1091 int nentry = menu->current;
1093 imap_parse_path (state.entry[nentry].name, &mx);
1094 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1096 if (mutt_yesorno (msg, M_NO) == M_YES) {
1097 if (!imap_delete_mailbox (Context, mx)) {
1098 /* free the mailbox from the browser */
1099 mem_free (&((state.entry)[nentry].name));
1100 mem_free (&((state.entry)[nentry].desc));
1101 /* and move all other entries up */
1102 if (nentry + 1 < state.entrylen)
1103 memmove (state.entry + nentry, state.entry + nentry + 1,
1104 sizeof (struct folder_file) * (state.entrylen -
1107 mutt_message _("Mailbox deleted.");
1109 init_menu (&state, menu, title, sizeof (title), buffy);
1110 MAYBE_REDRAW (menu->redraw);
1114 mutt_message _("Mailbox not deleted.");
1115 mem_free (&mx.mbox);
1120 case OP_CHANGE_DIRECTORY:
1123 if (option (OPTNEWS))
1127 strfcpy (buf, LastDir, sizeof (buf));
1129 if (!state.imap_browse)
1132 /* add '/' at the end of the directory name if not already there */
1133 int len = str_len (LastDir);
1135 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1139 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1142 mutt_expand_path (buf, sizeof (buf));
1144 if (imap_is_magic (buf, NULL) == M_IMAP) {
1145 strfcpy (LastDir, buf, sizeof (LastDir));
1146 destroy_state (&state);
1147 init_state (&state, NULL);
1148 state.imap_browse = 1;
1149 imap_browse (LastDir, &state);
1150 menu->data = state.entry;
1153 init_menu (&state, menu, title, sizeof (title), buffy);
1157 if (stat (buf, &st) == 0) {
1158 if (S_ISDIR (st.st_mode)) {
1159 destroy_state (&state);
1160 if (examine_directory (menu, &state, buf, prefix) == 0)
1161 strfcpy (LastDir, buf, sizeof (LastDir));
1163 mutt_error _("Error scanning directory.");
1165 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1166 mutt_menuDestroy (&menu);
1172 init_menu (&state, menu, title, sizeof (title), buffy);
1175 mutt_error (_("%s is not a directory."), buf);
1180 MAYBE_REDRAW (menu->redraw);
1185 strfcpy (buf, NONULL (Mask.pattern), sizeof (buf));
1186 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1187 regex_t *rx = (regex_t *) mem_malloc (sizeof (regex_t));
1192 /* assume that the user wants to see everything */
1194 strfcpy (buf, ".", sizeof (buf));
1202 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1203 regerror (err, rx, buf, sizeof (buf));
1206 mutt_error ("%s", buf);
1209 str_replace (&Mask.pattern, buf);
1211 mem_free (&Mask.rx);
1215 destroy_state (&state);
1217 if (state.imap_browse) {
1218 init_state (&state, NULL);
1219 state.imap_browse = 1;
1220 imap_browse (LastDir, &state);
1221 menu->data = state.entry;
1222 init_menu (&state, menu, title, sizeof (title), buffy);
1226 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1227 init_menu (&state, menu, title, sizeof (title), buffy);
1229 mutt_error _("Error scanning directory.");
1231 mutt_menuDestroy (&menu);
1235 if (!state.entrylen) {
1236 mutt_error _("No files match the file mask");
1242 MAYBE_REDRAW (menu->redraw);
1246 case OP_SORT_REVERSE:
1250 int reverse = (i == OP_SORT_REVERSE);
1252 switch (mutt_multi_choice ((reverse) ?
1254 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1257 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1259 case -1: /* abort */
1263 case 1: /* (d)ate */
1264 BrowserSort = SORT_DATE;
1267 case 2: /* (a)lpha */
1268 BrowserSort = SORT_SUBJECT;
1271 case 3: /* si(z)e */
1272 BrowserSort = SORT_SIZE;
1275 case 4: /* do(n)'t sort */
1276 BrowserSort = SORT_ORDER;
1281 BrowserSort |= reverse ? SORT_REVERSE : 0;
1282 browser_sort (&state);
1283 menu->redraw = REDRAW_FULL;
1288 case OP_TOGGLE_MAILBOXES:
1292 destroy_state (&state);
1297 if (examine_mailboxes (menu, &state) == -1)
1301 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1302 init_state (&state, NULL);
1303 state.imap_browse = 1;
1304 imap_browse (LastDir, &state);
1305 menu->data = state.entry;
1308 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1310 init_menu (&state, menu, title, sizeof (title), buffy);
1314 if (option (OPTFORCEBUFFYCHECK))
1319 case OP_BROWSER_NEW_FILE:
1321 snprintf (buf, sizeof (buf), "%s/", LastDir);
1322 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1324 strfcpy (f, buf, flen);
1325 destroy_state (&state);
1326 mutt_menuDestroy (&menu);
1329 MAYBE_REDRAW (menu->redraw);
1332 case OP_BROWSER_VIEW_FILE:
1333 if (!state.entrylen) {
1334 mutt_error _("No files match the file mask");
1340 if (state.entry[menu->current].selectable) {
1341 strfcpy (f, state.entry[menu->current].name, flen);
1342 destroy_state (&state);
1343 mutt_menuDestroy (&menu);
1348 if (S_ISDIR (state.entry[menu->current].mode) ||
1349 (S_ISLNK (state.entry[menu->current].mode) &&
1350 link_is_dir (LastDir, state.entry[menu->current].name))) {
1351 mutt_error _("Can't view a directory");
1357 char buf[_POSIX_PATH_MAX];
1359 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
1361 b = mutt_make_file_attach (buf);
1363 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1364 mutt_free_body (&b);
1365 menu->redraw = REDRAW_FULL;
1368 mutt_error _("Error trying to view file");
1375 if (option (OPTNEWS)) {
1376 struct folder_file *f = &state.entry[menu->current];
1379 if (i == OP_CATCHUP)
1380 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1382 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1386 struct folder_file ff;
1387 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1390 folder.ff->name = f->name;
1391 folder.ff->st = NULL;
1392 folder.ff->is_new = nd->new;
1394 mem_free (&f->desc);
1395 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1396 newsgroup_format_str, (unsigned long) &folder,
1397 M_FORMAT_ARROWCURSOR);
1398 f->desc = str_dup (buffer); */
1399 if (menu->current + 1 < menu->max)
1401 menu->redraw = REDRAW_MOTION_RESYNCH;
1406 case OP_LOAD_ACTIVE:
1407 if (!option (OPTNEWS))
1414 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1415 if ((data = (NNTP_DATA *) tmp->data))
1419 nntp_get_active (CurrentNewsSrv);
1421 destroy_state (&state);
1423 examine_mailboxes (menu, &state);
1425 examine_directory (menu, &state, NULL, NULL);
1426 init_menu (&state, menu, title, sizeof (title), buffy);
1428 #endif /* USE_NNTP */
1430 #if defined USE_IMAP || defined USE_NNTP
1431 case OP_BROWSER_SUBSCRIBE:
1432 case OP_BROWSER_UNSUBSCRIBE:
1435 case OP_SUBSCRIBE_PATTERN:
1436 case OP_UNSUBSCRIBE_PATTERN:
1437 if (option (OPTNEWS)) {
1438 regex_t *rx = (regex_t *) mem_malloc (sizeof (regex_t));
1440 int j = menu->current;
1442 NNTP_SERVER *news = CurrentNewsSrv;
1444 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1449 if (i == OP_SUBSCRIBE_PATTERN)
1450 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1452 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1453 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1458 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1459 regerror (err, rx, buf, sizeof (buf));
1462 mutt_error ("%s", buf);
1465 menu->redraw = REDRAW_FULL;
1468 else if (!state.entrylen) {
1469 mutt_error _("No newsgroups match the mask");
1474 for (; j < state.entrylen; j++) {
1475 struct folder_file *f = &state.entry[j];
1477 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1478 regexec (rx, f->name, 0, NULL, 0) == 0) {
1479 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1480 nd = mutt_newsgroup_subscribe (news, f->name);
1482 nd = mutt_newsgroup_unsubscribe (news, f->name);
1486 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1488 folder.name = f->name;
1490 folder.new = nd->new;
1492 mem_free (&f->desc);
1493 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1494 newsgroup_format_str, (unsigned long) &folder,
1495 M_FORMAT_ARROWCURSOR);
1496 f->desc = str_dup (buffer);
1499 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1500 if (menu->current + 1 < menu->max)
1502 menu->redraw = REDRAW_MOTION_RESYNCH;
1506 if (i == OP_SUBSCRIBE_PATTERN) {
1507 LIST *grouplist = NULL;
1510 grouplist = news->list;
1511 for (; grouplist; grouplist = grouplist->next) {
1512 nd = (NNTP_DATA *) grouplist->data;
1513 if (nd && nd->group && !nd->subscribed) {
1514 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1515 mutt_newsgroup_subscribe (news, nd->group);
1516 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1520 init_menu (&state, menu, title, sizeof (title), buffy);
1522 mutt_newsrc_update (news);
1523 nntp_clear_cacheindex (news);
1524 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1530 #endif /* USE_IMAP && USE_NNTP */
1531 #endif /* USE_NNTP */
1534 if (i == OP_BROWSER_SUBSCRIBE)
1535 imap_subscribe (state.entry[menu->current].name, 1);
1537 imap_subscribe (state.entry[menu->current].name, 0);
1539 #endif /* USE_IMAP */
1546 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));