2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 #include "mutt_curses.h"
25 #include "mutt_menu.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
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++)
83 FREE (&((state->entry)[c].name));
84 FREE (&((state->entry)[c].desc));
85 FREE (&((state->entry)[c].st));
88 FREE (&state->folder);
93 static int browser_compare_subject (const void *a, const void *b)
95 struct folder_file *pa = (struct folder_file *) a;
96 struct folder_file *pb = (struct folder_file *) b;
98 int r = mutt_strcoll (pa->name, pb->name);
100 return ((BrowserSort & SORT_REVERSE) ? -r : r);
103 static int browser_compare_date (const void *a, const void *b)
105 struct folder_file *pa = (struct folder_file *) a;
106 struct folder_file *pb = (struct folder_file *) b;
108 int r = pa->mtime - pb->mtime;
110 return ((BrowserSort & SORT_REVERSE) ? -r : r);
113 static int browser_compare_size (const void *a, const void *b)
115 struct folder_file *pa = (struct folder_file *) a;
116 struct folder_file *pb = (struct folder_file *) b;
118 int r = pa->size - pb->size;
120 return ((BrowserSort & SORT_REVERSE) ? -r : r);
123 static void browser_sort (struct browser_state *state)
125 int (*f) (const void *, const void *);
127 switch (BrowserSort & SORT_MASK)
133 if (option (OPTNEWS))
136 f = browser_compare_date;
140 if (option (OPTNEWS))
143 f = browser_compare_size;
147 f = browser_compare_subject;
150 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
153 static int link_is_dir (const char *folder, const char *path)
156 char fullpath[_POSIX_PATH_MAX];
158 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
160 if (stat (fullpath, &st) == 0)
161 return (S_ISDIR (st.st_mode));
167 folder_format_str (char *dest, size_t destlen, char op, const char *src,
168 const char *fmt, const char *ifstring, const char *elsestring,
169 unsigned long data, format_flag flags)
171 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
172 char date[16], *t_fmt;
174 FOLDER *folder = (FOLDER *) data;
177 int optional = (flags & M_FORMAT_OPTIONAL);
182 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
183 snprintf (dest, destlen, tmp, folder->num + 1);
187 if (folder->ff->st != NULL)
190 t_fmt = tnow - folder->ff->st->st_mtime < 31536000 ? "%b %d %H:%M" : "%b %d %Y";
191 strftime (date, sizeof (date), t_fmt, localtime (&folder->ff->st->st_mtime));
192 mutt_format_s (dest, destlen, fmt, date);
195 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) != 0 ? "*" : ""))) : "");
213 mutt_format_s (dest, destlen, fmt, fn);
217 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->st_mode) ? 'd' : (S_ISLNK(folder->ff->st->st_mode) ? 'l' : '-'),
221 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r': '-',
222 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
223 (folder->ff->st->st_mode & S_ISUID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) != 0 ? 'x': '-',
224 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
225 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
226 (folder->ff->st->st_mode & S_ISGID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) != 0 ? 'x': '-',
227 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
228 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
229 (folder->ff->st->st_mode & S_ISVTX) != 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) != 0 ? 'x': '-');
230 mutt_format_s (dest, destlen, fmt, permission);
233 else if (folder->ff->imap)
235 /* mark folders with subfolders AND mail */
236 snprintf (permission, sizeof (permission), "IMAP %c",
237 (folder->ff->inferiors && folder->ff->selectable) ? '+' : ' ');
238 mutt_format_s (dest, destlen, fmt, permission);
242 mutt_format_s (dest, destlen, fmt, "");
246 if (folder->ff->st != NULL)
248 if ((gr = getgrgid (folder->ff->st->st_gid)))
249 mutt_format_s (dest, destlen, fmt, gr->gr_name);
252 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
253 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
257 mutt_format_s (dest, destlen, fmt, "");
261 if (folder->ff->st != NULL)
263 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
264 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
267 mutt_format_s (dest, destlen, fmt, "");
272 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)
291 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
292 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
295 mutt_format_s (dest, destlen, fmt, "");
299 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
300 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
304 if (folder->ff->st != NULL)
306 if ((pw = getpwuid (folder->ff->st->st_uid)))
307 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);
334 newsgroup_format_str (char *dest, size_t destlen, char op, const char *src,
335 const char *fmt, const char *ifstring, const char *elsestring,
336 unsigned long data, format_flag flags)
338 char fn[SHORT_STRING], tmp[SHORT_STRING];
339 FOLDER *folder = (FOLDER *) data;
344 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
345 snprintf (dest, destlen, tmp, folder->num + 1);
349 strncpy (fn, folder->ff->name, sizeof(fn) - 1);
350 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
351 snprintf (dest, destlen, tmp, fn);
355 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
356 if (folder->ff->nd->subscribed)
357 snprintf (dest, destlen, tmp, ' ');
359 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
363 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
364 if (folder->ff->nd->deleted)
365 snprintf (dest, destlen, tmp, 'D');
367 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
371 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)
382 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
383 snprintf (dest, destlen, tmp, Context->unread);
387 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
388 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
393 if (Context && Context->data == folder->ff->nd)
395 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
396 snprintf (dest, destlen, tmp, Context->new);
398 else if (option (OPTMARKOLD) &&
399 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
400 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage)
402 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
403 snprintf (dest, destlen, tmp, folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
407 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
408 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
413 if (folder->ff->nd->desc != NULL)
415 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
416 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
420 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
421 snprintf (dest, destlen, tmp, "");
427 #endif /* USE_NNTP */
429 static void add_folder (MUTTMENU *m, struct browser_state *state,
430 const char *name, const struct stat *s,
433 if (state->entrylen == state->entrymax)
435 /* need to allocate more space */
436 safe_realloc (&state->entry,
437 sizeof (struct folder_file) * (state->entrymax += 256));
438 memset (&state->entry[state->entrylen], 0,
439 sizeof (struct folder_file) * 256);
441 m->data = state->entry;
446 (state->entry)[state->entrylen].mode = s->st_mode;
447 (state->entry)[state->entrylen].mtime = s->st_mtime;
448 (state->entry)[state->entrylen].size = s->st_size;
450 (state->entry)[state->entrylen].st = safe_malloc (sizeof (struct stat));
451 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
454 (state->entry)[state->entrylen].new = new;
455 (state->entry)[state->entrylen].name = safe_strdup (name);
456 (state->entry)[state->entrylen].desc = safe_strdup (name);
458 (state->entry)[state->entrylen].imap = 0;
461 if (option (OPTNEWS))
462 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
467 static void init_state (struct browser_state *state, MUTTMENU *menu)
470 state->entrymax = 256;
471 state->entry = (struct folder_file *) safe_calloc (state->entrymax, sizeof (struct folder_file));
473 state->imap_browse = 0;
476 menu->data = state->entry;
479 /* get list of all files/newsgroups with mask */
480 static int examine_directory (MUTTMENU *menu, struct browser_state *state,
481 char *d, const char *prefix)
484 if (option (OPTNEWS))
488 NNTP_SERVER *news = CurrentNewsSrv;
490 /* mutt_buffy_check (0); */
491 init_state (state, menu);
493 for (tmp = news->list; tmp; tmp = tmp->next)
495 if (!(data = (NNTP_DATA *)tmp->data))
497 if (prefix && *prefix && strncmp (prefix, data->group,
498 strlen (prefix)) != 0)
500 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
502 add_folder (menu, state, data->group, NULL, data, data->new);
506 #endif /* USE_NNTP */
511 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
514 while (stat (d, &s) == -1)
518 /* The last used directory is deleted, try to use the parent dir. */
519 char *c = strrchr (d, '/');
531 if (!S_ISDIR (s.st_mode))
533 mutt_error (_("%s is not a directory."), d);
537 mutt_buffy_check (0);
539 if ((dp = opendir (d)) == NULL)
545 init_state (state, menu);
547 while ((de = readdir (dp)) != NULL)
549 if (mutt_strcmp (de->d_name, ".") == 0)
550 continue; /* we don't need . */
552 if (prefix && *prefix && mutt_strncmp (prefix, de->d_name, mutt_strlen (prefix)) != 0)
554 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
557 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
558 if (lstat (buffer, &s) == -1)
561 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
562 (! S_ISLNK (s.st_mode)))
566 while (tmp && mutt_strcmp (buffer, tmp->path))
568 add_folder (menu, state, de->d_name, &s, NULL, (tmp) ? tmp->new : 0);
572 draw_sidebar (CurrentMenu);
573 browser_sort (state);
577 /* get list of mailboxes/subscribed newsgroups */
578 static int examine_mailboxes (MUTTMENU *menu, struct browser_state *state)
581 char buffer[LONG_STRING];
584 if (option (OPTNEWS))
588 NNTP_SERVER *news = CurrentNewsSrv;
590 /* mutt_buffy_check (0); */
591 init_state (state, menu);
593 for (tmp = news->list; tmp; tmp = tmp->next)
595 if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
596 (data->subscribed && (!option (OPTSHOWONLYUNREAD) || data->unread))))
597 add_folder (menu, state, data->group, NULL, data, data->new);
603 BUFFY *tmp = Incoming;
607 mutt_buffy_check (0);
609 init_state (state, menu);
614 if (mx_is_imap (tmp->path))
616 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
621 if (mx_is_pop (tmp->path))
623 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
628 if (mx_is_nntp (tmp->path))
630 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
634 if (lstat (tmp->path, &s) == -1)
637 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
638 (! S_ISLNK (s.st_mode)))
641 strfcpy (buffer, NONULL(tmp->path), sizeof (buffer));
642 mutt_pretty_mailbox (buffer);
644 add_folder (menu, state, buffer, &s, NULL, tmp->new);
646 while ((tmp = tmp->next));
648 browser_sort (state);
652 static int select_file_search (MUTTMENU *menu, regex_t *re, int n)
655 if (option (OPTNEWS))
656 return (regexec (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
658 return (regexec (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
661 static void folder_entry (char *s, size_t slen, MUTTMENU *menu, int num)
665 folder.ff = &((struct folder_file *) menu->data)[num];
669 if (option (OPTNEWS))
670 mutt_FormatString (s, slen, NONULL(GroupFormat), newsgroup_format_str,
671 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
674 mutt_FormatString (s, slen, NONULL(FolderFormat), folder_format_str,
675 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
678 static void init_menu (struct browser_state *state, MUTTMENU *menu, char *title,
679 size_t titlelen, int buffy)
681 char path[_POSIX_PATH_MAX];
683 menu->max = state->entrylen;
685 if(menu->current >= menu->max)
686 menu->current = menu->max - 1;
687 if (menu->current < 0)
689 if (menu->top > menu->current)
695 if (option (OPTNEWS))
698 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
700 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
701 CurrentNewsSrv->conn->account.host);
706 snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
709 strfcpy (path, LastDir, sizeof (path));
710 mutt_pretty_mailbox (path);
712 if (state->imap_browse && option (OPTIMAPLSUB))
713 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
714 path, NONULL (Mask.pattern));
717 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
718 path, NONULL(Mask.pattern));
720 menu->redraw = REDRAW_FULL;
723 static int file_tag (MUTTMENU *menu, int n, int m)
725 struct folder_file *ff = &(((struct folder_file *)menu->data)[n]);
727 if (S_ISDIR (ff->mode) || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name)))
729 mutt_error _("Can't attach a directory!");
734 ff->tagged = (m >= 0 ? m : !ff->tagged);
736 return ff->tagged - ot;
739 void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *numfiles)
741 char buf[_POSIX_PATH_MAX];
742 char prefix[_POSIX_PATH_MAX] = "";
743 char helpstr[SHORT_STRING];
745 struct browser_state state;
748 int i, killPrefix = 0;
749 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
750 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
751 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
753 buffy = buffy && folder;
755 memset (&state, 0, sizeof (struct browser_state));
758 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
761 if (option (OPTNEWS))
764 strfcpy (prefix, f, sizeof (prefix));
769 /* default state for news reader mode is browse subscribed newsgroups */
771 for (list = CurrentNewsSrv->list; list; list = list->next)
773 NNTP_DATA *data = (NNTP_DATA *) list->data;
775 if (data && data->subscribed)
787 mutt_expand_path (f, flen);
791 init_state (&state, NULL);
792 state.imap_browse = 1;
793 imap_browse (f, &state);
794 strfcpy (LastDir, state.folder, sizeof (LastDir));
799 for (i = mutt_strlen (f) - 1; i > 0 && f[i] != '/' ; i--);
804 if (i > sizeof (LastDir) - 1) i = sizeof (LastDir) - 1;
805 strncpy (LastDir, f, i);
810 getcwd (LastDir, sizeof (LastDir));
811 safe_strcat (LastDir, sizeof (LastDir), "/");
812 safe_strncat (LastDir, sizeof (LastDir), f, i);
818 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
820 getcwd (LastDir, sizeof (LastDir));
823 if (i <= 0 && f[0] != '/')
824 strfcpy (prefix, f, sizeof (prefix));
826 strfcpy (prefix, f + i + 1, sizeof (prefix));
835 getcwd (LastDir, sizeof (LastDir));
836 else if (!LastDir[0])
837 strfcpy (LastDir, NONULL(Maildir), sizeof (LastDir));
840 if (!buffy && mx_is_imap (LastDir))
842 init_state (&state, NULL);
843 state.imap_browse = 1;
844 imap_browse (LastDir, &state);
853 if (examine_mailboxes (NULL, &state) == -1)
858 if (!state.imap_browse)
860 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
863 menu = mutt_new_menu ();
864 menu->menu = MENU_FOLDER;
865 menu->make_entry = folder_entry;
866 menu->search = select_file_search;
868 menu->data = state.entry;
870 menu->tag = file_tag;
872 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
874 (option (OPTNEWS)) ? FolderNewsHelp :
878 init_menu (&state, menu, title, sizeof (title), buffy);
882 switch (i = mutt_menuLoop (menu))
884 case OP_GENERIC_SELECT_ENTRY:
888 mutt_error _("No files match the file mask");
892 if (S_ISDIR (state.entry[menu->current].mode) ||
893 (S_ISLNK (state.entry[menu->current].mode) &&
894 link_is_dir (LastDir, state.entry[menu->current].name))
896 || state.entry[menu->current].inferiors
900 /* make sure this isn't a MH or maildir mailbox */
903 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
904 mutt_expand_path (buf, sizeof (buf));
907 else if (state.imap_browse)
909 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
913 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
915 if ((mx_get_magic (buf) <= 0)
917 || state.entry[menu->current].inferiors
921 char OldLastDir[_POSIX_PATH_MAX];
923 /* save the old directory */
924 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
926 if (mutt_strcmp (state.entry[menu->current].name, "..") == 0)
928 if (mutt_strcmp ("..", LastDir + mutt_strlen (LastDir) - 2) == 0)
929 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
932 char *p = strrchr (LastDir + 1, '/');
938 if (LastDir[0] == '/')
941 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
947 strfcpy (LastDir, state.entry[menu->current].name, sizeof (LastDir));
948 mutt_expand_path (LastDir, sizeof (LastDir));
951 else if (state.imap_browse)
956 strfcpy (LastDir, state.entry[menu->current].name,
958 /* tack on delimiter here */
959 n = strlen (LastDir)+1;
961 /* special case "" needs no delimiter */
962 url_parse_ciss (&url, state.entry[menu->current].name);
964 (state.entry[menu->current].delim != '\0') &&
965 (n < sizeof (LastDir)))
968 LastDir[n-1] = state.entry[menu->current].delim;
974 char tmp[_POSIX_PATH_MAX];
975 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name, sizeof (tmp));
976 strfcpy (LastDir, tmp, sizeof (LastDir));
979 destroy_state (&state);
987 if (state.imap_browse)
989 init_state (&state, NULL);
990 state.imap_browse = 1;
991 imap_browse (LastDir, &state);
992 menu->data = state.entry;
996 if (examine_directory (menu, &state, LastDir, prefix) == -1)
998 /* try to restore the old values */
999 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
1000 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1002 strfcpy (LastDir, NONULL(Homedir), sizeof (LastDir));
1008 init_menu (&state, menu, title, sizeof (title), buffy);
1014 if (buffy || option (OPTNEWS)) /* news have not path */
1019 strfcpy (f, state.entry[menu->current].name, flen);
1020 mutt_expand_path (f, flen);
1023 else if (state.imap_browse)
1024 strfcpy (f, state.entry[menu->current].name, flen);
1027 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
1029 /* Fall through to OP_EXIT */
1040 *numfiles = menu->tagged;
1041 tfiles = safe_calloc (*numfiles, sizeof (char *));
1042 for (i = 0, j = 0; i < state.entrylen; i++)
1044 struct folder_file ff = state.entry[i];
1045 char full[_POSIX_PATH_MAX];
1048 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1049 mutt_expand_path (full, sizeof (full));
1050 tfiles[j++] = safe_strdup (full);
1055 else if (f[0]) /* no tagged entries. return selected entry */
1058 tfiles = safe_calloc (*numfiles, sizeof (char *));
1059 mutt_expand_path (f, flen);
1060 tfiles[0] = safe_strdup (f);
1065 destroy_state (&state);
1066 mutt_menuDestroy (&menu);
1069 case OP_BROWSER_TELL:
1071 mutt_message("%s", state.entry[menu->current].name);
1075 case OP_BROWSER_TOGGLE_LSUB:
1076 if (option (OPTIMAPLSUB))
1078 unset_option (OPTIMAPLSUB);
1082 set_option (OPTIMAPLSUB);
1084 mutt_ungetch (0, OP_CHECK_NEW);
1087 case OP_CREATE_MAILBOX:
1088 if (!state.imap_browse)
1089 mutt_error (_("Create is only supported for IMAP mailboxes"));
1092 imap_mailbox_create (LastDir);
1093 /* TODO: find a way to detect if the new folder would appear in
1094 * this window, and insert it without starting over. */
1095 destroy_state (&state);
1096 init_state (&state, NULL);
1097 state.imap_browse = 1;
1098 imap_browse (LastDir, &state);
1099 menu->data = state.entry;
1102 init_menu (&state, menu, title, sizeof (title), buffy);
1103 MAYBE_REDRAW (menu->redraw);
1107 case OP_RENAME_MAILBOX:
1108 if (!state.entry[menu->current].imap)
1109 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1112 int nentry = menu->current;
1114 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1115 destroy_state (&state);
1116 init_state (&state, NULL);
1117 state.imap_browse = 1;
1118 imap_browse (LastDir, &state);
1119 menu->data = state.entry;
1122 init_menu (&state, menu, title, sizeof (title), buffy);
1123 MAYBE_REDRAW (menu->redraw);
1128 case OP_DELETE_MAILBOX:
1129 if (!state.entry[menu->current].imap)
1130 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1133 char msg[SHORT_STRING];
1135 int nentry = menu->current;
1137 imap_parse_path (state.entry[nentry].name, &mx);
1138 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1140 if (mutt_yesorno (msg, M_NO) == M_YES)
1142 if (!imap_delete_mailbox (Context, mx))
1144 /* free the mailbox from the browser */
1145 FREE (&((state.entry)[nentry].name));
1146 FREE (&((state.entry)[nentry].desc));
1147 /* and move all other entries up */
1148 if (nentry+1 < state.entrylen)
1149 memmove (state.entry + nentry, state.entry + nentry + 1,
1150 sizeof (struct folder_file) * (state.entrylen - (nentry+1)));
1152 mutt_message _("Mailbox deleted.");
1153 init_menu (&state, menu, title, sizeof (title), buffy);
1154 MAYBE_REDRAW (menu->redraw);
1158 mutt_message _("Mailbox not deleted.");
1164 case OP_CHANGE_DIRECTORY:
1167 if (option (OPTNEWS))
1171 strfcpy (buf, LastDir, sizeof (buf));
1173 if (!state.imap_browse)
1176 /* add '/' at the end of the directory name if not already there */
1177 int len=mutt_strlen(LastDir);
1178 if (len && LastDir[len-1] != '/' && sizeof (buf) > len)
1182 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1186 mutt_expand_path (buf, sizeof (buf));
1188 if (mx_is_imap (buf))
1190 strfcpy (LastDir, buf, sizeof (LastDir));
1191 destroy_state (&state);
1192 init_state (&state, NULL);
1193 state.imap_browse = 1;
1194 imap_browse (LastDir, &state);
1195 menu->data = state.entry;
1198 init_menu (&state, menu, title, sizeof (title), buffy);
1202 if (stat (buf, &st) == 0)
1204 if (S_ISDIR (st.st_mode))
1206 destroy_state (&state);
1207 if (examine_directory (menu, &state, buf, prefix) == 0)
1208 strfcpy (LastDir, buf, sizeof (LastDir));
1211 mutt_error _("Error scanning directory.");
1212 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1214 mutt_menuDestroy (&menu);
1220 init_menu (&state, menu, title, sizeof (title), buffy);
1223 mutt_error (_("%s is not a directory."), buf);
1228 MAYBE_REDRAW (menu->redraw);
1233 strfcpy (buf, NONULL(Mask.pattern), sizeof (buf));
1234 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0)
1236 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1241 /* assume that the user wants to see everything */
1243 strfcpy (buf, ".", sizeof (buf));
1252 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1254 regerror (err, rx, buf, sizeof (buf));
1257 mutt_error ("%s", buf);
1261 mutt_str_replace (&Mask.pattern, buf);
1267 destroy_state (&state);
1269 if (state.imap_browse)
1271 init_state (&state, NULL);
1272 state.imap_browse = 1;
1273 imap_browse (LastDir, &state);
1274 menu->data = state.entry;
1275 init_menu (&state, menu, title, sizeof (title), buffy);
1279 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1280 init_menu (&state, menu, title, sizeof (title), buffy);
1283 mutt_error _("Error scanning directory.");
1284 mutt_menuDestroy (&menu);
1288 if (!state.entrylen)
1290 mutt_error _("No files match the file mask");
1295 MAYBE_REDRAW (menu->redraw);
1299 case OP_SORT_REVERSE:
1303 int reverse = (i == OP_SORT_REVERSE);
1305 switch (mutt_multi_choice ((reverse) ?
1306 _("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ") :
1307 _("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1310 case -1: /* abort */
1314 case 1: /* (d)ate */
1315 BrowserSort = SORT_DATE;
1318 case 2: /* (a)lpha */
1319 BrowserSort = SORT_SUBJECT;
1322 case 3: /* si(z)e */
1323 BrowserSort = SORT_SIZE;
1326 case 4: /* do(n)'t sort */
1327 BrowserSort = SORT_ORDER;
1333 BrowserSort |= reverse ? SORT_REVERSE : 0;
1334 browser_sort (&state);
1335 menu->redraw = REDRAW_FULL;
1340 case OP_TOGGLE_MAILBOXES:
1344 destroy_state (&state);
1350 if (examine_mailboxes (menu, &state) == -1)
1354 else if (mx_is_imap (LastDir))
1356 init_state (&state, NULL);
1357 state.imap_browse = 1;
1358 imap_browse (LastDir, &state);
1359 menu->data = state.entry;
1362 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1364 init_menu (&state, menu, title, sizeof (title), buffy);
1371 case OP_BROWSER_NEW_FILE:
1373 snprintf (buf, sizeof (buf), "%s/", LastDir);
1374 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) == 0)
1376 strfcpy (f, buf, flen);
1377 destroy_state (&state);
1378 mutt_menuDestroy (&menu);
1381 MAYBE_REDRAW (menu->redraw);
1384 case OP_BROWSER_VIEW_FILE:
1385 if (!state.entrylen)
1387 mutt_error _("No files match the file mask");
1392 if (state.entry[menu->current].selectable)
1394 strfcpy (f, state.entry[menu->current].name, flen);
1395 destroy_state (&state);
1396 mutt_menuDestroy (&menu);
1401 if (S_ISDIR (state.entry[menu->current].mode) ||
1402 (S_ISLNK (state.entry[menu->current].mode) &&
1403 link_is_dir (LastDir, state.entry[menu->current].name)))
1405 mutt_error _("Can't view a directory");
1411 char buf[_POSIX_PATH_MAX];
1413 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
1414 b = mutt_make_file_attach (buf);
1417 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1418 mutt_free_body (&b);
1419 menu->redraw = REDRAW_FULL;
1422 mutt_error _("Error trying to view file");
1429 if (option (OPTNEWS))
1431 struct folder_file *f = &state.entry[menu->current];
1434 if (i == OP_CATCHUP)
1435 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1437 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1442 struct folder_file ff;
1443 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1446 folder.ff->name = f->name;
1447 folder.ff->st = NULL;
1448 folder.ff->is_new = nd->new;
1451 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1452 newsgroup_format_str, (unsigned long) &folder,
1453 M_FORMAT_ARROWCURSOR);
1454 f->desc = safe_strdup (buffer); */
1455 if (menu->current + 1 < menu->max)
1457 menu->redraw = REDRAW_MOTION_RESYNCH;
1462 case OP_LOAD_ACTIVE:
1463 if (!option (OPTNEWS))
1470 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next)
1472 if ((data = (NNTP_DATA *)tmp->data))
1476 nntp_get_active (CurrentNewsSrv);
1478 destroy_state (&state);
1480 examine_mailboxes (menu, &state);
1482 examine_directory (menu, &state, NULL, NULL);
1483 init_menu (&state, menu, title, sizeof (title), buffy);
1485 #endif /* USE_NNTP */
1487 #if defined USE_IMAP || defined USE_NNTP
1488 case OP_BROWSER_SUBSCRIBE:
1489 case OP_BROWSER_UNSUBSCRIBE:
1492 case OP_SUBSCRIBE_PATTERN:
1493 case OP_UNSUBSCRIBE_PATTERN:
1494 if (option (OPTNEWS))
1496 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1498 int j = menu->current;
1500 NNTP_SERVER *news = CurrentNewsSrv;
1502 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN)
1508 if (i == OP_SUBSCRIBE_PATTERN)
1509 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1511 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1512 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0])
1518 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1520 regerror (err, rx, buf, sizeof (buf));
1523 mutt_error ("%s", buf);
1526 menu->redraw = REDRAW_FULL;
1529 else if (!state.entrylen)
1531 mutt_error _("No newsgroups match the mask");
1535 for ( ; j < state.entrylen; j++)
1537 struct folder_file *f = &state.entry[j];
1539 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1540 regexec (rx, f->name, 0, NULL, 0) == 0)
1542 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1543 nd = mutt_newsgroup_subscribe (news, f->name);
1545 nd = mutt_newsgroup_unsubscribe (news, f->name);
1549 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1551 folder.name = f->name;
1553 folder.new = nd->new;
1556 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1557 newsgroup_format_str, (unsigned long) &folder,
1558 M_FORMAT_ARROWCURSOR);
1559 f->desc = safe_strdup (buffer);
1562 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE)
1564 if (menu->current + 1 < menu->max)
1566 menu->redraw = REDRAW_MOTION_RESYNCH;
1570 if (i == OP_SUBSCRIBE_PATTERN)
1572 LIST *grouplist = NULL;
1575 grouplist = news->list;
1576 for (; grouplist; grouplist = grouplist->next)
1578 nd = (NNTP_DATA *) grouplist->data;
1579 if (nd && nd->group && !nd->subscribed)
1581 if (regexec (rx, nd->group, 0, NULL, 0) == 0)
1583 mutt_newsgroup_subscribe (news, nd->group);
1584 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1588 init_menu (&state, menu, title, sizeof (title), buffy);
1590 mutt_newsrc_update (news);
1591 nntp_clear_cacheindex (news);
1592 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1598 #endif /* USE_IMAP && USE_NNTP */
1599 #endif /* USE_NNTP */
1602 if (i == OP_BROWSER_SUBSCRIBE)
1603 imap_subscribe (state.entry[menu->current].name, 1);
1605 imap_subscribe (state.entry[menu->current].name, 0);
1607 #endif /* USE_IMAP */
1614 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));