2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
15 #include "mutt_curses.h"
16 #include "mutt_menu.h"
43 static struct mapping_t FolderHelp[] = {
44 {N_("Exit"), OP_EXIT},
45 {N_("Chdir"), OP_CHANGE_DIRECTORY},
46 {N_("Mask"), OP_ENTER_MASK},
47 {N_("Help"), OP_HELP},
52 static struct mapping_t FolderNewsHelp[] = {
53 {N_("Exit"), OP_EXIT},
54 {N_("List"), OP_TOGGLE_MAILBOXES},
55 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
56 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
57 {N_("Catchup"), OP_CATCHUP},
58 {N_("Mask"), OP_ENTER_MASK},
59 {N_("Help"), OP_HELP},
64 typedef struct folder_t {
65 struct folder_file *ff;
69 static char LastDir[_POSIX_PATH_MAX] = "";
70 static char LastDirBackup[_POSIX_PATH_MAX] = "";
72 /* Frees up the memory allocated for the local-global variables. */
73 static void destroy_state (struct browser_state *state)
77 for (c = 0; c < state->entrylen; c++) {
78 FREE (&((state->entry)[c].name));
79 FREE (&((state->entry)[c].desc));
80 FREE (&((state->entry)[c].st));
83 FREE (&state->folder);
88 static int browser_compare_subject (const void *a, const void *b)
90 struct folder_file *pa = (struct folder_file *) a;
91 struct folder_file *pb = (struct folder_file *) b;
93 int r = mutt_strcoll (pa->name, pb->name);
95 return ((BrowserSort & SORT_REVERSE) ? -r : r);
98 static int browser_compare_date (const void *a, const void *b)
100 struct folder_file *pa = (struct folder_file *) a;
101 struct folder_file *pb = (struct folder_file *) b;
103 int r = pa->mtime - pb->mtime;
105 return ((BrowserSort & SORT_REVERSE) ? -r : r);
108 static int browser_compare_size (const void *a, const void *b)
110 struct folder_file *pa = (struct folder_file *) a;
111 struct folder_file *pb = (struct folder_file *) b;
113 int r = pa->size - pb->size;
115 return ((BrowserSort & SORT_REVERSE) ? -r : r);
118 static void browser_sort (struct browser_state *state)
120 int (*f) (const void *, const void *);
122 switch (BrowserSort & SORT_MASK) {
127 if (option (OPTNEWS))
130 f = browser_compare_date;
134 if (option (OPTNEWS))
137 f = browser_compare_size;
141 f = browser_compare_subject;
144 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
147 static int link_is_dir (const char *folder, const char *path)
150 char fullpath[_POSIX_PATH_MAX];
152 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
154 if (stat (fullpath, &st) == 0)
155 return (S_ISDIR (st.st_mode));
160 static const char *folder_format_str (char *dest, size_t destlen, char op,
161 const char *src, const char *fmt,
162 const char *ifstring,
163 const char *elsestring,
164 unsigned long data, format_flag flags)
166 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
167 char date[16], *t_fmt;
169 FOLDER *folder = (FOLDER *) data;
172 int optional = (flags & M_FORMAT_OPTIONAL);
176 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
177 snprintf (dest, destlen, tmp, folder->num + 1);
181 if (folder->ff->st != NULL) {
184 tnow - folder->ff->st->st_mtime <
185 31536000 ? "%b %d %H:%M" : "%b %d %Y";
186 strftime (date, sizeof (date), t_fmt,
187 localtime (&folder->ff->st->st_mtime));
188 mutt_format_s (dest, destlen, fmt, date);
191 mutt_format_s (dest, destlen, fmt, "");
199 if (folder->ff->imap)
200 s = NONULL (folder->ff->desc);
203 s = NONULL (folder->ff->name);
205 snprintf (fn, sizeof (fn), "%s%s", s,
206 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
207 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
208 ((folder->ff->st->st_mode & S_IXUSR) !=
209 0 ? "*" : ""))) : "");
211 mutt_format_s (dest, destlen, fmt, fn);
215 if (folder->ff->st != NULL) {
216 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
217 S_ISDIR (folder->ff->st->
218 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
219 st_mode) ? 'l' : '-'),
220 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
221 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
222 (folder->ff->st->st_mode & S_ISUID) !=
223 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
225 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
226 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
227 (folder->ff->st->st_mode & S_ISGID) !=
228 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
230 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
231 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
232 (folder->ff->st->st_mode & S_ISVTX) !=
233 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
235 mutt_format_s (dest, destlen, fmt, permission);
238 else if (folder->ff->imap) {
239 /* mark folders with subfolders AND mail */
240 snprintf (permission, sizeof (permission), "IMAP %c",
241 (folder->ff->inferiors
242 && folder->ff->selectable) ? '+' : ' ');
243 mutt_format_s (dest, destlen, fmt, permission);
247 mutt_format_s (dest, destlen, fmt, "");
251 if (folder->ff->st != NULL) {
252 if ((gr = getgrgid (folder->ff->st->st_gid)))
253 mutt_format_s (dest, destlen, fmt, gr->gr_name);
255 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
256 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
260 mutt_format_s (dest, destlen, fmt, "");
264 if (folder->ff->st != NULL) {
265 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
266 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
269 mutt_format_s (dest, destlen, fmt, "");
274 if (mx_is_imap (folder->ff->desc)) {
276 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
277 snprintf (dest, destlen, tmp, folder->ff->new);
279 else if (!folder->ff->new)
284 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
285 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
289 if (folder->ff->st != NULL) {
290 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
291 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
294 mutt_format_s (dest, destlen, fmt, "");
298 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
299 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
303 if (folder->ff->st != NULL) {
304 if ((pw = getpwuid (folder->ff->st->st_uid)))
305 mutt_format_s (dest, destlen, fmt, pw->pw_name);
307 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
308 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
312 mutt_format_s (dest, destlen, fmt, "");
316 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
317 snprintf (dest, destlen, tmp, op);
322 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
323 else if (flags & M_FORMAT_OPTIONAL)
324 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
330 static const char *newsgroup_format_str (char *dest, size_t destlen, char op,
331 const char *src, const char *fmt,
332 const char *ifstring,
333 const char *elsestring,
337 char fn[SHORT_STRING], tmp[SHORT_STRING];
338 FOLDER *folder = (FOLDER *) data;
342 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
343 snprintf (dest, destlen, tmp, folder->num + 1);
347 strncpy (fn, folder->ff->name, sizeof (fn) - 1);
348 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
349 snprintf (dest, destlen, tmp, fn);
353 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
354 if (folder->ff->nd->subscribed)
355 snprintf (dest, destlen, tmp, ' ');
357 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
361 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
362 if (folder->ff->nd->deleted)
363 snprintf (dest, destlen, tmp, 'D');
365 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
369 if (flags & M_FORMAT_OPTIONAL) {
370 if (folder->ff->nd->unread != 0)
371 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
374 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
377 else if (Context && Context->data == folder->ff->nd) {
378 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
379 snprintf (dest, destlen, tmp, Context->unread);
382 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
383 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
388 if (Context && Context->data == folder->ff->nd) {
389 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
390 snprintf (dest, destlen, tmp, Context->new);
392 else if (option (OPTMARKOLD) &&
393 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
394 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
395 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
396 snprintf (dest, destlen, tmp,
397 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
400 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
401 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
406 if (folder->ff->nd->desc != NULL) {
407 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
408 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
411 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
412 snprintf (dest, destlen, tmp, "");
418 #endif /* USE_NNTP */
420 static void add_folder (MUTTMENU * m, struct browser_state *state,
421 const char *name, const struct stat *s,
424 if (state->entrylen == state->entrymax) {
425 /* need to allocate more space */
426 safe_realloc (&state->entry,
427 sizeof (struct folder_file) * (state->entrymax += 256));
428 memset (&state->entry[state->entrylen], 0,
429 sizeof (struct folder_file) * 256);
431 m->data = state->entry;
435 (state->entry)[state->entrylen].mode = s->st_mode;
436 (state->entry)[state->entrylen].mtime = s->st_mtime;
437 (state->entry)[state->entrylen].size = s->st_size;
439 (state->entry)[state->entrylen].st = safe_malloc (sizeof (struct stat));
440 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
443 (state->entry)[state->entrylen].new = new;
444 (state->entry)[state->entrylen].name = safe_strdup (name);
445 (state->entry)[state->entrylen].desc = safe_strdup (name);
447 (state->entry)[state->entrylen].imap = 0;
450 if (option (OPTNEWS))
451 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
456 static void init_state (struct browser_state *state, MUTTMENU * menu)
459 state->entrymax = 256;
461 (struct folder_file *) safe_calloc (state->entrymax,
462 sizeof (struct folder_file));
464 state->imap_browse = 0;
467 menu->data = state->entry;
470 /* get list of all files/newsgroups with mask */
471 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
472 char *d, const char *prefix)
475 if (option (OPTNEWS)) {
478 NNTP_SERVER *news = CurrentNewsSrv;
480 /* mutt_buffy_check (0); */
481 init_state (state, menu);
483 for (tmp = news->list; tmp; tmp = tmp->next) {
484 if (!(data = (NNTP_DATA *) tmp->data))
486 if (prefix && *prefix && strncmp (prefix, data->group,
487 mutt_strlen (prefix)) != 0)
489 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
491 add_folder (menu, state, data->group, NULL, data, data->new);
495 #endif /* USE_NNTP */
500 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
503 while (stat (d, &s) == -1) {
504 if (errno == ENOENT) {
505 /* The last used directory is deleted, try to use the parent dir. */
506 char *c = strrchr (d, '/');
517 if (!S_ISDIR (s.st_mode)) {
518 mutt_error (_("%s is not a directory."), d);
522 mutt_buffy_check (0);
524 if ((dp = opendir (d)) == NULL) {
529 init_state (state, menu);
531 while ((de = readdir (dp)) != NULL) {
532 if (mutt_strcmp (de->d_name, ".") == 0)
533 continue; /* we don't need . */
535 if (prefix && *prefix
536 && mutt_strncmp (prefix, de->d_name, mutt_strlen (prefix)) != 0)
538 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
541 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
542 if (lstat (buffer, &s) == -1)
545 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
546 (!S_ISLNK (s.st_mode)))
549 i = buffy_lookup (buffer);
550 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
554 sidebar_draw (CurrentMenu);
555 browser_sort (state);
559 /* get list of mailboxes/subscribed newsgroups */
560 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
563 char buffer[LONG_STRING];
566 if (option (OPTNEWS)) {
569 NNTP_SERVER *news = CurrentNewsSrv;
571 /* mutt_buffy_check (0); */
572 init_state (state, menu);
574 for (tmp = news->list; tmp; tmp = tmp->next) {
575 if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
581 add_folder (menu, state, data->group, NULL, data, data->new);
592 mutt_buffy_check (0);
594 init_state (state, menu);
596 for (i = 0; i < Incoming->length; i++) {
597 tmp = (BUFFY*) Incoming->data[i];
599 if (mx_is_imap (tmp->path)) {
600 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
605 if (mx_is_pop (tmp->path)) {
606 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
611 if (mx_is_nntp (tmp->path)) {
612 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
616 if (lstat (tmp->path, &s) == -1)
619 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
620 (!S_ISLNK (s.st_mode)))
623 strfcpy (buffer, NONULL (tmp->path), sizeof (buffer));
624 mutt_pretty_mailbox (buffer);
626 add_folder (menu, state, buffer, &s, NULL, tmp->new);
629 browser_sort (state);
633 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
636 if (option (OPTNEWS))
638 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
641 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
644 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
648 folder.ff = &((struct folder_file *) menu->data)[num];
652 if (option (OPTNEWS))
653 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
654 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
657 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
658 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
661 static void init_menu (struct browser_state *state, MUTTMENU * menu,
662 char *title, size_t titlelen, int buffy)
664 char path[_POSIX_PATH_MAX];
666 menu->max = state->entrylen;
668 if (menu->current >= menu->max)
669 menu->current = menu->max - 1;
670 if (menu->current < 0)
672 if (menu->top > menu->current)
678 if (option (OPTNEWS)) {
680 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
682 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
683 CurrentNewsSrv->conn->account.host);
688 snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
690 strfcpy (path, LastDir, sizeof (path));
691 mutt_pretty_mailbox (path);
693 if (state->imap_browse && option (OPTIMAPLSUB))
694 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
695 path, NONULL (Mask.pattern));
698 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
699 path, NONULL (Mask.pattern));
701 menu->redraw = REDRAW_FULL;
704 static int file_tag (MUTTMENU * menu, int n, int m)
706 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
709 if (S_ISDIR (ff->mode)
710 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
711 mutt_error _("Can't attach a directory!");
717 ff->tagged = (m >= 0 ? m : !ff->tagged);
719 return ff->tagged - ot;
722 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
725 char buf[_POSIX_PATH_MAX];
726 char prefix[_POSIX_PATH_MAX] = "";
727 char helpstr[SHORT_STRING];
729 struct browser_state state;
732 int i, killPrefix = 0;
733 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
734 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
735 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
737 buffy = buffy && folder;
739 memset (&state, 0, sizeof (struct browser_state));
742 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
745 if (option (OPTNEWS)) {
747 strfcpy (prefix, f, sizeof (prefix));
751 /* default state for news reader mode is browse subscribed newsgroups */
753 for (list = CurrentNewsSrv->list; list; list = list->next) {
754 NNTP_DATA *data = (NNTP_DATA *) list->data;
756 if (data && data->subscribed) {
766 mutt_expand_path (f, flen);
768 if (mx_is_imap (f)) {
769 init_state (&state, NULL);
770 state.imap_browse = 1;
771 imap_browse (f, &state);
772 strfcpy (LastDir, state.folder, sizeof (LastDir));
776 for (i = mutt_strlen (f) - 1; i > 0 && f[i] != '/'; i--);
779 if (i > sizeof (LastDir) - 1)
780 i = sizeof (LastDir) - 1;
781 strncpy (LastDir, f, i);
785 getcwd (LastDir, sizeof (LastDir));
786 safe_strcat (LastDir, sizeof (LastDir), "/");
787 safe_strncat (LastDir, sizeof (LastDir), f, i);
792 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
794 getcwd (LastDir, sizeof (LastDir));
797 if (i <= 0 && f[0] != '/')
798 strfcpy (prefix, f, sizeof (prefix));
800 strfcpy (prefix, f + i + 1, sizeof (prefix));
808 getcwd (LastDir, sizeof (LastDir));
809 else if (!LastDir[0])
810 strfcpy (LastDir, NONULL (Maildir), sizeof (LastDir));
813 if (!buffy && mx_is_imap (LastDir)) {
814 init_state (&state, NULL);
815 state.imap_browse = 1;
816 imap_browse (LastDir, &state);
824 if (examine_mailboxes (NULL, &state) == -1)
829 if (!state.imap_browse)
831 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
834 menu = mutt_new_menu ();
835 menu->menu = MENU_FOLDER;
836 menu->make_entry = folder_entry;
837 menu->search = select_file_search;
839 menu->data = state.entry;
841 menu->tag = file_tag;
843 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
845 (option (OPTNEWS)) ? FolderNewsHelp :
849 init_menu (&state, menu, title, sizeof (title), buffy);
852 switch (i = mutt_menuLoop (menu)) {
853 case OP_GENERIC_SELECT_ENTRY:
855 if (!state.entrylen) {
856 mutt_error _("No files match the file mask");
861 if (S_ISDIR (state.entry[menu->current].mode) ||
862 (S_ISLNK (state.entry[menu->current].mode) &&
863 link_is_dir (LastDir, state.entry[menu->current].name))
865 || state.entry[menu->current].inferiors
868 /* make sure this isn't a MH or maildir mailbox */
870 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
871 mutt_expand_path (buf, sizeof (buf));
874 else if (state.imap_browse) {
875 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
879 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
882 if ((mx_get_magic (buf) <= 0)
884 || state.entry[menu->current].inferiors
887 char OldLastDir[_POSIX_PATH_MAX];
889 /* save the old directory */
890 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
892 if (mutt_strcmp (state.entry[menu->current].name, "..") == 0) {
893 if (mutt_strcmp ("..", LastDir + mutt_strlen (LastDir) - 2) == 0)
894 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
896 char *p = strrchr (LastDir + 1, '/');
901 if (LastDir[0] == '/')
904 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
909 strfcpy (LastDir, state.entry[menu->current].name,
911 mutt_expand_path (LastDir, sizeof (LastDir));
914 else if (state.imap_browse) {
918 strfcpy (LastDir, state.entry[menu->current].name,
920 /* tack on delimiter here */
921 n = mutt_strlen (LastDir) + 1;
923 /* special case "" needs no delimiter */
924 url_parse_ciss (&url, state.entry[menu->current].name);
926 (state.entry[menu->current].delim != '\0') &&
927 (n < sizeof (LastDir))) {
929 LastDir[n - 1] = state.entry[menu->current].delim;
934 char tmp[_POSIX_PATH_MAX];
936 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name,
938 strfcpy (LastDir, tmp, sizeof (LastDir));
941 destroy_state (&state);
948 if (state.imap_browse) {
949 init_state (&state, NULL);
950 state.imap_browse = 1;
951 imap_browse (LastDir, &state);
952 menu->data = state.entry;
956 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
957 /* try to restore the old values */
958 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
959 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
960 strfcpy (LastDir, NONULL (Homedir), sizeof (LastDir));
966 init_menu (&state, menu, title, sizeof (title), buffy);
972 if (buffy || option (OPTNEWS)) /* news have not path */
977 strfcpy (f, state.entry[menu->current].name, flen);
978 mutt_expand_path (f, flen);
981 else if (state.imap_browse)
982 strfcpy (f, state.entry[menu->current].name, flen);
985 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
987 /* Fall through to OP_EXIT */
996 *numfiles = menu->tagged;
997 tfiles = safe_calloc (*numfiles, sizeof (char *));
998 for (i = 0, j = 0; i < state.entrylen; i++) {
999 struct folder_file ff = state.entry[i];
1000 char full[_POSIX_PATH_MAX];
1003 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1004 mutt_expand_path (full, sizeof (full));
1005 tfiles[j++] = safe_strdup (full);
1010 else if (f[0]) { /* no tagged entries. return selected entry */
1012 tfiles = safe_calloc (*numfiles, sizeof (char *));
1013 mutt_expand_path (f, flen);
1014 tfiles[0] = safe_strdup (f);
1019 destroy_state (&state);
1020 mutt_menuDestroy (&menu);
1023 case OP_BROWSER_TELL:
1025 mutt_message ("%s", state.entry[menu->current].name);
1029 case OP_BROWSER_TOGGLE_LSUB:
1030 if (option (OPTIMAPLSUB)) {
1031 unset_option (OPTIMAPLSUB);
1034 set_option (OPTIMAPLSUB);
1036 mutt_ungetch (0, OP_CHECK_NEW);
1039 case OP_CREATE_MAILBOX:
1040 if (!state.imap_browse)
1041 mutt_error (_("Create is only supported for IMAP mailboxes"));
1043 imap_mailbox_create (LastDir);
1044 /* TODO: find a way to detect if the new folder would appear in
1045 * this window, and insert it without starting over. */
1046 destroy_state (&state);
1047 init_state (&state, NULL);
1048 state.imap_browse = 1;
1049 imap_browse (LastDir, &state);
1050 menu->data = state.entry;
1053 init_menu (&state, menu, title, sizeof (title), buffy);
1054 MAYBE_REDRAW (menu->redraw);
1058 case OP_RENAME_MAILBOX:
1059 if (!state.entry[menu->current].imap)
1060 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1062 int nentry = menu->current;
1064 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1065 destroy_state (&state);
1066 init_state (&state, NULL);
1067 state.imap_browse = 1;
1068 imap_browse (LastDir, &state);
1069 menu->data = state.entry;
1072 init_menu (&state, menu, title, sizeof (title), buffy);
1073 MAYBE_REDRAW (menu->redraw);
1078 case OP_DELETE_MAILBOX:
1079 if (!state.entry[menu->current].imap)
1080 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1082 char msg[SHORT_STRING];
1084 int nentry = menu->current;
1086 imap_parse_path (state.entry[nentry].name, &mx);
1087 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1089 if (mutt_yesorno (msg, M_NO) == M_YES) {
1090 if (!imap_delete_mailbox (Context, mx)) {
1091 /* free the mailbox from the browser */
1092 FREE (&((state.entry)[nentry].name));
1093 FREE (&((state.entry)[nentry].desc));
1094 /* and move all other entries up */
1095 if (nentry + 1 < state.entrylen)
1096 memmove (state.entry + nentry, state.entry + nentry + 1,
1097 sizeof (struct folder_file) * (state.entrylen -
1100 mutt_message _("Mailbox deleted.");
1102 init_menu (&state, menu, title, sizeof (title), buffy);
1103 MAYBE_REDRAW (menu->redraw);
1107 mutt_message _("Mailbox not deleted.");
1113 case OP_CHANGE_DIRECTORY:
1116 if (option (OPTNEWS))
1120 strfcpy (buf, LastDir, sizeof (buf));
1122 if (!state.imap_browse)
1125 /* add '/' at the end of the directory name if not already there */
1126 int len = mutt_strlen (LastDir);
1128 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1132 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1135 mutt_expand_path (buf, sizeof (buf));
1137 if (mx_is_imap (buf)) {
1138 strfcpy (LastDir, buf, sizeof (LastDir));
1139 destroy_state (&state);
1140 init_state (&state, NULL);
1141 state.imap_browse = 1;
1142 imap_browse (LastDir, &state);
1143 menu->data = state.entry;
1146 init_menu (&state, menu, title, sizeof (title), buffy);
1150 if (stat (buf, &st) == 0) {
1151 if (S_ISDIR (st.st_mode)) {
1152 destroy_state (&state);
1153 if (examine_directory (menu, &state, buf, prefix) == 0)
1154 strfcpy (LastDir, buf, sizeof (LastDir));
1156 mutt_error _("Error scanning directory.");
1158 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1159 mutt_menuDestroy (&menu);
1165 init_menu (&state, menu, title, sizeof (title), buffy);
1168 mutt_error (_("%s is not a directory."), buf);
1173 MAYBE_REDRAW (menu->redraw);
1178 strfcpy (buf, NONULL (Mask.pattern), sizeof (buf));
1179 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1180 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1185 /* assume that the user wants to see everything */
1187 strfcpy (buf, ".", sizeof (buf));
1195 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1196 regerror (err, rx, buf, sizeof (buf));
1199 mutt_error ("%s", buf);
1202 mutt_str_replace (&Mask.pattern, buf);
1208 destroy_state (&state);
1210 if (state.imap_browse) {
1211 init_state (&state, NULL);
1212 state.imap_browse = 1;
1213 imap_browse (LastDir, &state);
1214 menu->data = state.entry;
1215 init_menu (&state, menu, title, sizeof (title), buffy);
1219 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1220 init_menu (&state, menu, title, sizeof (title), buffy);
1222 mutt_error _("Error scanning directory.");
1224 mutt_menuDestroy (&menu);
1228 if (!state.entrylen) {
1229 mutt_error _("No files match the file mask");
1235 MAYBE_REDRAW (menu->redraw);
1239 case OP_SORT_REVERSE:
1243 int reverse = (i == OP_SORT_REVERSE);
1245 switch (mutt_multi_choice ((reverse) ?
1247 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1250 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1252 case -1: /* abort */
1256 case 1: /* (d)ate */
1257 BrowserSort = SORT_DATE;
1260 case 2: /* (a)lpha */
1261 BrowserSort = SORT_SUBJECT;
1264 case 3: /* si(z)e */
1265 BrowserSort = SORT_SIZE;
1268 case 4: /* do(n)'t sort */
1269 BrowserSort = SORT_ORDER;
1274 BrowserSort |= reverse ? SORT_REVERSE : 0;
1275 browser_sort (&state);
1276 menu->redraw = REDRAW_FULL;
1281 case OP_TOGGLE_MAILBOXES:
1285 destroy_state (&state);
1290 if (examine_mailboxes (menu, &state) == -1)
1294 else if (mx_is_imap (LastDir)) {
1295 init_state (&state, NULL);
1296 state.imap_browse = 1;
1297 imap_browse (LastDir, &state);
1298 menu->data = state.entry;
1301 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1303 init_menu (&state, menu, title, sizeof (title), buffy);
1310 case OP_BROWSER_NEW_FILE:
1312 snprintf (buf, sizeof (buf), "%s/", LastDir);
1313 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1315 strfcpy (f, buf, flen);
1316 destroy_state (&state);
1317 mutt_menuDestroy (&menu);
1320 MAYBE_REDRAW (menu->redraw);
1323 case OP_BROWSER_VIEW_FILE:
1324 if (!state.entrylen) {
1325 mutt_error _("No files match the file mask");
1331 if (state.entry[menu->current].selectable) {
1332 strfcpy (f, state.entry[menu->current].name, flen);
1333 destroy_state (&state);
1334 mutt_menuDestroy (&menu);
1339 if (S_ISDIR (state.entry[menu->current].mode) ||
1340 (S_ISLNK (state.entry[menu->current].mode) &&
1341 link_is_dir (LastDir, state.entry[menu->current].name))) {
1342 mutt_error _("Can't view a directory");
1348 char buf[_POSIX_PATH_MAX];
1350 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
1352 b = mutt_make_file_attach (buf);
1354 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1355 mutt_free_body (&b);
1356 menu->redraw = REDRAW_FULL;
1359 mutt_error _("Error trying to view file");
1366 if (option (OPTNEWS)) {
1367 struct folder_file *f = &state.entry[menu->current];
1370 if (i == OP_CATCHUP)
1371 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1373 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1377 struct folder_file ff;
1378 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1381 folder.ff->name = f->name;
1382 folder.ff->st = NULL;
1383 folder.ff->is_new = nd->new;
1386 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1387 newsgroup_format_str, (unsigned long) &folder,
1388 M_FORMAT_ARROWCURSOR);
1389 f->desc = safe_strdup (buffer); */
1390 if (menu->current + 1 < menu->max)
1392 menu->redraw = REDRAW_MOTION_RESYNCH;
1397 case OP_LOAD_ACTIVE:
1398 if (!option (OPTNEWS))
1405 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1406 if ((data = (NNTP_DATA *) tmp->data))
1410 nntp_get_active (CurrentNewsSrv);
1412 destroy_state (&state);
1414 examine_mailboxes (menu, &state);
1416 examine_directory (menu, &state, NULL, NULL);
1417 init_menu (&state, menu, title, sizeof (title), buffy);
1419 #endif /* USE_NNTP */
1421 #if defined USE_IMAP || defined USE_NNTP
1422 case OP_BROWSER_SUBSCRIBE:
1423 case OP_BROWSER_UNSUBSCRIBE:
1426 case OP_SUBSCRIBE_PATTERN:
1427 case OP_UNSUBSCRIBE_PATTERN:
1428 if (option (OPTNEWS)) {
1429 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1431 int j = menu->current;
1433 NNTP_SERVER *news = CurrentNewsSrv;
1435 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1440 if (i == OP_SUBSCRIBE_PATTERN)
1441 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1443 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1444 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1449 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1450 regerror (err, rx, buf, sizeof (buf));
1453 mutt_error ("%s", buf);
1456 menu->redraw = REDRAW_FULL;
1459 else if (!state.entrylen) {
1460 mutt_error _("No newsgroups match the mask");
1465 for (; j < state.entrylen; j++) {
1466 struct folder_file *f = &state.entry[j];
1468 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1469 regexec (rx, f->name, 0, NULL, 0) == 0) {
1470 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1471 nd = mutt_newsgroup_subscribe (news, f->name);
1473 nd = mutt_newsgroup_unsubscribe (news, f->name);
1477 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1479 folder.name = f->name;
1481 folder.new = nd->new;
1484 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1485 newsgroup_format_str, (unsigned long) &folder,
1486 M_FORMAT_ARROWCURSOR);
1487 f->desc = safe_strdup (buffer);
1490 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1491 if (menu->current + 1 < menu->max)
1493 menu->redraw = REDRAW_MOTION_RESYNCH;
1497 if (i == OP_SUBSCRIBE_PATTERN) {
1498 LIST *grouplist = NULL;
1501 grouplist = news->list;
1502 for (; grouplist; grouplist = grouplist->next) {
1503 nd = (NNTP_DATA *) grouplist->data;
1504 if (nd && nd->group && !nd->subscribed) {
1505 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1506 mutt_newsgroup_subscribe (news, nd->group);
1507 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1511 init_menu (&state, menu, title, sizeof (title), buffy);
1513 mutt_newsrc_update (news);
1514 nntp_clear_cacheindex (news);
1515 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1521 #endif /* USE_IMAP && USE_NNTP */
1522 #endif /* USE_NNTP */
1525 if (i == OP_BROWSER_SUBSCRIBE)
1526 imap_subscribe (state.entry[menu->current].name, 1);
1528 imap_subscribe (state.entry[menu->current].name, 0);
1530 #endif /* USE_IMAP */
1537 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));