2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
16 #include "mutt_curses.h"
17 #include "mutt_menu.h"
24 #include "imap/mx_imap.h"
44 static struct mapping_t FolderHelp[] = {
45 {N_("Exit"), OP_EXIT},
46 {N_("Chdir"), OP_CHANGE_DIRECTORY},
47 {N_("Mask"), OP_ENTER_MASK},
48 {N_("Help"), OP_HELP},
53 static struct mapping_t FolderNewsHelp[] = {
54 {N_("Exit"), OP_EXIT},
55 {N_("List"), OP_TOGGLE_MAILBOXES},
56 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
57 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
58 {N_("Catchup"), OP_CATCHUP},
59 {N_("Mask"), OP_ENTER_MASK},
60 {N_("Help"), OP_HELP},
65 typedef struct folder_t {
66 struct folder_file *ff;
70 static char LastDir[_POSIX_PATH_MAX] = "";
71 static char LastDirBackup[_POSIX_PATH_MAX] = "";
73 /* Frees up the memory allocated for the local-global variables. */
74 static void destroy_state (struct browser_state *state)
78 for (c = 0; c < state->entrylen; c++) {
79 FREE (&((state->entry)[c].name));
80 FREE (&((state->entry)[c].desc));
81 FREE (&((state->entry)[c].st));
84 FREE (&state->folder);
89 static int browser_compare_subject (const void *a, const void *b)
91 struct folder_file *pa = (struct folder_file *) a;
92 struct folder_file *pb = (struct folder_file *) b;
94 int r = safe_strcoll (pa->name, pb->name);
96 return ((BrowserSort & SORT_REVERSE) ? -r : r);
99 static int browser_compare_date (const void *a, const void *b)
101 struct folder_file *pa = (struct folder_file *) a;
102 struct folder_file *pb = (struct folder_file *) b;
104 int r = pa->mtime - pb->mtime;
106 return ((BrowserSort & SORT_REVERSE) ? -r : r);
109 static int browser_compare_size (const void *a, const void *b)
111 struct folder_file *pa = (struct folder_file *) a;
112 struct folder_file *pb = (struct folder_file *) b;
114 int r = pa->size - pb->size;
116 return ((BrowserSort & SORT_REVERSE) ? -r : r);
119 static void browser_sort (struct browser_state *state)
121 int (*f) (const void *, const void *);
123 switch (BrowserSort & SORT_MASK) {
128 if (option (OPTNEWS))
131 f = browser_compare_date;
135 if (option (OPTNEWS))
138 f = browser_compare_size;
142 f = browser_compare_subject;
145 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
148 static int link_is_dir (const char *folder, const char *path)
151 char fullpath[_POSIX_PATH_MAX];
153 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
155 if (stat (fullpath, &st) == 0)
156 return (S_ISDIR (st.st_mode));
161 static const char *folder_format_str (char *dest, size_t destlen, char op,
162 const char *src, const char *fmt,
163 const char *ifstring,
164 const char *elsestring,
165 unsigned long data, format_flag flags)
167 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
168 char date[16], *t_fmt;
170 FOLDER *folder = (FOLDER *) data;
173 int optional = (flags & M_FORMAT_OPTIONAL);
177 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
178 snprintf (dest, destlen, tmp, folder->num + 1);
182 if (folder->ff->st != NULL) {
185 tnow - folder->ff->st->st_mtime <
186 31536000 ? "%b %d %H:%M" : "%b %d %Y";
187 strftime (date, sizeof (date), t_fmt,
188 localtime (&folder->ff->st->st_mtime));
189 mutt_format_s (dest, destlen, fmt, date);
192 mutt_format_s (dest, destlen, fmt, "");
200 if (folder->ff->imap)
201 s = NONULL (folder->ff->desc);
204 s = NONULL (folder->ff->name);
206 snprintf (fn, sizeof (fn), "%s%s", s,
207 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
208 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
209 ((folder->ff->st->st_mode & S_IXUSR) !=
210 0 ? "*" : ""))) : "");
212 mutt_format_s (dest, destlen, fmt, fn);
216 if (folder->ff->st != NULL) {
217 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
218 S_ISDIR (folder->ff->st->
219 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
220 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) !=
224 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
226 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
227 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
228 (folder->ff->st->st_mode & S_ISGID) !=
229 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
231 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
232 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
233 (folder->ff->st->st_mode & S_ISVTX) !=
234 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
236 mutt_format_s (dest, destlen, fmt, permission);
239 else if (folder->ff->imap) {
240 /* mark folders with subfolders AND mail */
241 snprintf (permission, sizeof (permission), "IMAP %c",
242 (folder->ff->inferiors
243 && folder->ff->selectable) ? '+' : ' ');
244 mutt_format_s (dest, destlen, fmt, permission);
248 mutt_format_s (dest, destlen, fmt, "");
252 if (folder->ff->st != NULL) {
253 if ((gr = getgrgid (folder->ff->st->st_gid)))
254 mutt_format_s (dest, destlen, fmt, gr->gr_name);
256 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
257 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
261 mutt_format_s (dest, destlen, fmt, "");
265 if (folder->ff->st != NULL) {
266 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
267 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
270 mutt_format_s (dest, destlen, fmt, "");
275 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
277 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
278 snprintf (dest, destlen, tmp, folder->ff->new);
280 else if (!folder->ff->new)
285 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
286 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
290 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) {
305 if ((pw = getpwuid (folder->ff->st->st_uid)))
306 mutt_format_s (dest, destlen, fmt, pw->pw_name);
308 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
309 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
313 mutt_format_s (dest, destlen, fmt, "");
317 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
318 snprintf (dest, destlen, tmp, op);
323 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
324 else if (flags & M_FORMAT_OPTIONAL)
325 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
331 static const char *newsgroup_format_str (char *dest, size_t destlen, char op,
332 const char *src, const char *fmt,
333 const char *ifstring,
334 const char *elsestring,
338 char fn[SHORT_STRING], tmp[SHORT_STRING];
339 FOLDER *folder = (FOLDER *) data;
343 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
344 snprintf (dest, destlen, tmp, folder->num + 1);
348 strncpy (fn, folder->ff->name, sizeof (fn) - 1);
349 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
350 snprintf (dest, destlen, tmp, fn);
354 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
355 if (folder->ff->nd->subscribed)
356 snprintf (dest, destlen, tmp, ' ');
358 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
362 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
363 if (folder->ff->nd->deleted)
364 snprintf (dest, destlen, tmp, 'D');
366 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
370 if (flags & M_FORMAT_OPTIONAL) {
371 if (folder->ff->nd->unread != 0)
372 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
375 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
378 else if (Context && Context->data == folder->ff->nd) {
379 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
380 snprintf (dest, destlen, tmp, Context->unread);
383 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
384 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
389 if (Context && Context->data == folder->ff->nd) {
390 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
391 snprintf (dest, destlen, tmp, Context->new);
393 else if (option (OPTMARKOLD) &&
394 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
395 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
396 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
397 snprintf (dest, destlen, tmp,
398 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
401 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
402 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
407 if (folder->ff->nd->desc != NULL) {
408 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
409 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
412 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
413 snprintf (dest, destlen, tmp, "");
419 #endif /* USE_NNTP */
421 static void add_folder (MUTTMENU * m, struct browser_state *state,
422 const char *name, const struct stat *s,
425 if (state->entrylen == state->entrymax) {
426 /* need to allocate more space */
427 safe_realloc (&state->entry,
428 sizeof (struct folder_file) * (state->entrymax += 256));
429 memset (&state->entry[state->entrylen], 0,
430 sizeof (struct folder_file) * 256);
432 m->data = state->entry;
436 (state->entry)[state->entrylen].mode = s->st_mode;
437 (state->entry)[state->entrylen].mtime = s->st_mtime;
438 (state->entry)[state->entrylen].size = s->st_size;
440 (state->entry)[state->entrylen].st = safe_malloc (sizeof (struct stat));
441 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
444 (state->entry)[state->entrylen].new = new;
445 (state->entry)[state->entrylen].name = safe_strdup (name);
446 (state->entry)[state->entrylen].desc = safe_strdup (name);
448 (state->entry)[state->entrylen].imap = 0;
451 if (option (OPTNEWS))
452 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
457 static void init_state (struct browser_state *state, MUTTMENU * menu)
460 state->entrymax = 256;
462 (struct folder_file *) safe_calloc (state->entrymax,
463 sizeof (struct folder_file));
465 state->imap_browse = 0;
468 menu->data = state->entry;
471 /* get list of all files/newsgroups with mask */
472 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
473 char *d, const char *prefix)
476 if (option (OPTNEWS)) {
479 NNTP_SERVER *news = CurrentNewsSrv;
481 /* buffy_check (0); */
482 init_state (state, menu);
484 for (tmp = news->list; tmp; tmp = tmp->next) {
485 if (!(data = (NNTP_DATA *) tmp->data))
487 if (prefix && *prefix && strncmp (prefix, data->group,
488 safe_strlen (prefix)) != 0)
490 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
492 add_folder (menu, state, data->group, NULL, data, data->new);
496 #endif /* USE_NNTP */
501 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
504 while (stat (d, &s) == -1) {
505 if (errno == ENOENT) {
506 /* The last used directory is deleted, try to use the parent dir. */
507 char *c = strrchr (d, '/');
518 if (!S_ISDIR (s.st_mode)) {
519 mutt_error (_("%s is not a directory."), d);
525 if ((dp = opendir (d)) == NULL) {
530 init_state (state, menu);
532 while ((de = readdir (dp)) != NULL) {
533 if (safe_strcmp (de->d_name, ".") == 0)
534 continue; /* we don't need . */
536 if (prefix && *prefix
537 && safe_strncmp (prefix, de->d_name, safe_strlen (prefix)) != 0)
539 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
542 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
543 if (lstat (buffer, &s) == -1)
546 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
547 (!S_ISLNK (s.st_mode)))
550 i = buffy_lookup (buffer);
551 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
555 sidebar_draw (CurrentMenu);
556 browser_sort (state);
560 /* get list of mailboxes/subscribed newsgroups */
561 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
564 char buffer[LONG_STRING];
567 if (option (OPTNEWS)) {
570 NNTP_SERVER *news = CurrentNewsSrv;
572 /* buffy_check (0); */
573 init_state (state, menu);
575 for (tmp = news->list; tmp; tmp = tmp->next) {
576 if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
582 add_folder (menu, state, data->group, NULL, data, data->new);
595 init_state (state, menu);
597 for (i = 0; i < Incoming->length; i++) {
598 tmp = (BUFFY*) Incoming->data[i];
599 tmp->magic = mx_get_magic (tmp->path);
601 if (tmp->magic == M_IMAP) {
602 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
607 if (tmp->magic == M_POP) {
608 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
613 if (tmp->magic == M_NNTP) {
614 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
618 if (lstat (tmp->path, &s) == -1)
621 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
622 (!S_ISLNK (s.st_mode)))
625 strfcpy (buffer, NONULL (tmp->path), sizeof (buffer));
626 mutt_pretty_mailbox (buffer);
628 add_folder (menu, state, buffer, &s, NULL, tmp->new);
631 browser_sort (state);
635 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
638 if (option (OPTNEWS))
640 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
643 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
646 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
650 folder.ff = &((struct folder_file *) menu->data)[num];
654 if (option (OPTNEWS))
655 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
656 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
659 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
660 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
663 static void init_menu (struct browser_state *state, MUTTMENU * menu,
664 char *title, size_t titlelen, int buffy)
666 char path[_POSIX_PATH_MAX];
668 menu->max = state->entrylen;
670 if (menu->current >= menu->max)
671 menu->current = menu->max - 1;
672 if (menu->current < 0)
674 if (menu->top > menu->current)
680 if (option (OPTNEWS)) {
682 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
684 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
685 CurrentNewsSrv->conn->account.host);
690 snprintf (title, titlelen, _("Mailboxes [%d]"), buffy_check (0));
692 strfcpy (path, LastDir, sizeof (path));
693 mutt_pretty_mailbox (path);
695 if (state->imap_browse && option (OPTIMAPLSUB))
696 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
697 path, NONULL (Mask.pattern));
700 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
701 path, NONULL (Mask.pattern));
703 menu->redraw = REDRAW_FULL;
706 static int file_tag (MUTTMENU * menu, int n, int m)
708 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
711 if (S_ISDIR (ff->mode)
712 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
713 mutt_error _("Can't attach a directory!");
719 ff->tagged = (m >= 0 ? m : !ff->tagged);
721 return ff->tagged - ot;
724 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
727 char buf[_POSIX_PATH_MAX];
728 char prefix[_POSIX_PATH_MAX] = "";
729 char helpstr[SHORT_STRING];
731 struct browser_state state;
734 int i, killPrefix = 0;
735 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
736 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
737 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
739 buffy = buffy && folder;
741 memset (&state, 0, sizeof (struct browser_state));
744 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
747 if (option (OPTNEWS)) {
749 strfcpy (prefix, f, sizeof (prefix));
753 /* default state for news reader mode is browse subscribed newsgroups */
755 for (list = CurrentNewsSrv->list; list; list = list->next) {
756 NNTP_DATA *data = (NNTP_DATA *) list->data;
758 if (data && data->subscribed) {
768 mutt_expand_path (f, flen);
770 if (imap_is_magic (f, NULL) == M_IMAP) {
771 init_state (&state, NULL);
772 state.imap_browse = 1;
773 imap_browse (f, &state);
774 strfcpy (LastDir, state.folder, sizeof (LastDir));
778 for (i = safe_strlen (f) - 1; i > 0 && f[i] != '/'; i--);
781 if (i > sizeof (LastDir) - 1)
782 i = sizeof (LastDir) - 1;
783 strncpy (LastDir, f, i);
787 getcwd (LastDir, sizeof (LastDir));
788 safe_strcat (LastDir, sizeof (LastDir), "/");
789 safe_strncat (LastDir, sizeof (LastDir), f, i);
794 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
796 getcwd (LastDir, sizeof (LastDir));
799 if (i <= 0 && f[0] != '/')
800 strfcpy (prefix, f, sizeof (prefix));
802 strfcpy (prefix, f + i + 1, sizeof (prefix));
810 getcwd (LastDir, sizeof (LastDir));
811 else if (!LastDir[0])
812 strfcpy (LastDir, NONULL (Maildir), sizeof (LastDir));
815 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
816 init_state (&state, NULL);
817 state.imap_browse = 1;
818 imap_browse (LastDir, &state);
826 if (examine_mailboxes (NULL, &state) == -1)
831 if (!state.imap_browse)
833 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
836 menu = mutt_new_menu ();
837 menu->menu = MENU_FOLDER;
838 menu->make_entry = folder_entry;
839 menu->search = select_file_search;
841 menu->data = state.entry;
843 menu->tag = file_tag;
845 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
847 (option (OPTNEWS)) ? FolderNewsHelp :
851 init_menu (&state, menu, title, sizeof (title), buffy);
854 switch (i = mutt_menuLoop (menu)) {
855 case OP_GENERIC_SELECT_ENTRY:
857 if (!state.entrylen) {
858 mutt_error _("No files match the file mask");
863 if (S_ISDIR (state.entry[menu->current].mode) ||
864 (S_ISLNK (state.entry[menu->current].mode) &&
865 link_is_dir (LastDir, state.entry[menu->current].name))
867 || state.entry[menu->current].inferiors
870 /* make sure this isn't a MH or maildir mailbox */
872 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
873 mutt_expand_path (buf, sizeof (buf));
876 else if (state.imap_browse) {
877 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
881 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
884 if ((mx_get_magic (buf) <= 0)
886 || state.entry[menu->current].inferiors
889 char OldLastDir[_POSIX_PATH_MAX];
891 /* save the old directory */
892 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
894 if (safe_strcmp (state.entry[menu->current].name, "..") == 0) {
895 if (safe_strcmp ("..", LastDir + safe_strlen (LastDir) - 2) == 0)
896 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
898 char *p = strrchr (LastDir + 1, '/');
903 if (LastDir[0] == '/')
906 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
911 strfcpy (LastDir, state.entry[menu->current].name,
913 mutt_expand_path (LastDir, sizeof (LastDir));
916 else if (state.imap_browse) {
920 strfcpy (LastDir, state.entry[menu->current].name,
922 /* tack on delimiter here */
923 n = safe_strlen (LastDir) + 1;
925 /* special case "" needs no delimiter */
926 url_parse_ciss (&url, state.entry[menu->current].name);
928 (state.entry[menu->current].delim != '\0') &&
929 (n < sizeof (LastDir))) {
931 LastDir[n - 1] = state.entry[menu->current].delim;
936 char tmp[_POSIX_PATH_MAX];
938 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name,
940 strfcpy (LastDir, tmp, sizeof (LastDir));
943 destroy_state (&state);
950 if (state.imap_browse) {
951 init_state (&state, NULL);
952 state.imap_browse = 1;
953 imap_browse (LastDir, &state);
954 menu->data = state.entry;
958 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
959 /* try to restore the old values */
960 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
961 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
962 strfcpy (LastDir, NONULL (Homedir), sizeof (LastDir));
968 init_menu (&state, menu, title, sizeof (title), buffy);
974 if (buffy || option (OPTNEWS)) /* news have not path */
979 strfcpy (f, state.entry[menu->current].name, flen);
980 mutt_expand_path (f, flen);
983 else if (state.imap_browse)
984 strfcpy (f, state.entry[menu->current].name, flen);
987 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
989 /* Fall through to OP_EXIT */
998 *numfiles = menu->tagged;
999 tfiles = safe_calloc (*numfiles, sizeof (char *));
1000 for (i = 0, j = 0; i < state.entrylen; i++) {
1001 struct folder_file ff = state.entry[i];
1002 char full[_POSIX_PATH_MAX];
1005 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1006 mutt_expand_path (full, sizeof (full));
1007 tfiles[j++] = safe_strdup (full);
1012 else if (f[0]) { /* no tagged entries. return selected entry */
1014 tfiles = safe_calloc (*numfiles, sizeof (char *));
1015 mutt_expand_path (f, flen);
1016 tfiles[0] = safe_strdup (f);
1021 destroy_state (&state);
1022 mutt_menuDestroy (&menu);
1025 case OP_BROWSER_TELL:
1027 mutt_message ("%s", state.entry[menu->current].name);
1031 case OP_BROWSER_TOGGLE_LSUB:
1032 if (option (OPTIMAPLSUB)) {
1033 unset_option (OPTIMAPLSUB);
1036 set_option (OPTIMAPLSUB);
1038 mutt_ungetch (0, OP_CHECK_NEW);
1041 case OP_CREATE_MAILBOX:
1042 if (!state.imap_browse)
1043 mutt_error (_("Create is only supported for IMAP mailboxes"));
1045 imap_mailbox_create (LastDir);
1046 /* TODO: find a way to detect if the new folder would appear in
1047 * this window, and insert it without starting over. */
1048 destroy_state (&state);
1049 init_state (&state, NULL);
1050 state.imap_browse = 1;
1051 imap_browse (LastDir, &state);
1052 menu->data = state.entry;
1055 init_menu (&state, menu, title, sizeof (title), buffy);
1056 MAYBE_REDRAW (menu->redraw);
1060 case OP_RENAME_MAILBOX:
1061 if (!state.entry[menu->current].imap)
1062 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1064 int nentry = menu->current;
1066 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1067 destroy_state (&state);
1068 init_state (&state, NULL);
1069 state.imap_browse = 1;
1070 imap_browse (LastDir, &state);
1071 menu->data = state.entry;
1074 init_menu (&state, menu, title, sizeof (title), buffy);
1075 MAYBE_REDRAW (menu->redraw);
1080 case OP_DELETE_MAILBOX:
1081 if (!state.entry[menu->current].imap)
1082 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1084 char msg[SHORT_STRING];
1086 int nentry = menu->current;
1088 imap_parse_path (state.entry[nentry].name, &mx);
1089 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1091 if (mutt_yesorno (msg, M_NO) == M_YES) {
1092 if (!imap_delete_mailbox (Context, mx)) {
1093 /* free the mailbox from the browser */
1094 FREE (&((state.entry)[nentry].name));
1095 FREE (&((state.entry)[nentry].desc));
1096 /* and move all other entries up */
1097 if (nentry + 1 < state.entrylen)
1098 memmove (state.entry + nentry, state.entry + nentry + 1,
1099 sizeof (struct folder_file) * (state.entrylen -
1102 mutt_message _("Mailbox deleted.");
1104 init_menu (&state, menu, title, sizeof (title), buffy);
1105 MAYBE_REDRAW (menu->redraw);
1109 mutt_message _("Mailbox not deleted.");
1115 case OP_CHANGE_DIRECTORY:
1118 if (option (OPTNEWS))
1122 strfcpy (buf, LastDir, sizeof (buf));
1124 if (!state.imap_browse)
1127 /* add '/' at the end of the directory name if not already there */
1128 int len = safe_strlen (LastDir);
1130 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1134 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1137 mutt_expand_path (buf, sizeof (buf));
1139 if (imap_is_magic (buf, NULL) == M_IMAP) {
1140 strfcpy (LastDir, buf, sizeof (LastDir));
1141 destroy_state (&state);
1142 init_state (&state, NULL);
1143 state.imap_browse = 1;
1144 imap_browse (LastDir, &state);
1145 menu->data = state.entry;
1148 init_menu (&state, menu, title, sizeof (title), buffy);
1152 if (stat (buf, &st) == 0) {
1153 if (S_ISDIR (st.st_mode)) {
1154 destroy_state (&state);
1155 if (examine_directory (menu, &state, buf, prefix) == 0)
1156 strfcpy (LastDir, buf, sizeof (LastDir));
1158 mutt_error _("Error scanning directory.");
1160 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1161 mutt_menuDestroy (&menu);
1167 init_menu (&state, menu, title, sizeof (title), buffy);
1170 mutt_error (_("%s is not a directory."), buf);
1175 MAYBE_REDRAW (menu->redraw);
1180 strfcpy (buf, NONULL (Mask.pattern), sizeof (buf));
1181 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1182 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1187 /* assume that the user wants to see everything */
1189 strfcpy (buf, ".", sizeof (buf));
1197 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1198 regerror (err, rx, buf, sizeof (buf));
1201 mutt_error ("%s", buf);
1204 str_replace (&Mask.pattern, buf);
1210 destroy_state (&state);
1212 if (state.imap_browse) {
1213 init_state (&state, NULL);
1214 state.imap_browse = 1;
1215 imap_browse (LastDir, &state);
1216 menu->data = state.entry;
1217 init_menu (&state, menu, title, sizeof (title), buffy);
1221 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1222 init_menu (&state, menu, title, sizeof (title), buffy);
1224 mutt_error _("Error scanning directory.");
1226 mutt_menuDestroy (&menu);
1230 if (!state.entrylen) {
1231 mutt_error _("No files match the file mask");
1237 MAYBE_REDRAW (menu->redraw);
1241 case OP_SORT_REVERSE:
1245 int reverse = (i == OP_SORT_REVERSE);
1247 switch (mutt_multi_choice ((reverse) ?
1249 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1252 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1254 case -1: /* abort */
1258 case 1: /* (d)ate */
1259 BrowserSort = SORT_DATE;
1262 case 2: /* (a)lpha */
1263 BrowserSort = SORT_SUBJECT;
1266 case 3: /* si(z)e */
1267 BrowserSort = SORT_SIZE;
1270 case 4: /* do(n)'t sort */
1271 BrowserSort = SORT_ORDER;
1276 BrowserSort |= reverse ? SORT_REVERSE : 0;
1277 browser_sort (&state);
1278 menu->redraw = REDRAW_FULL;
1283 case OP_TOGGLE_MAILBOXES:
1287 destroy_state (&state);
1292 if (examine_mailboxes (menu, &state) == -1)
1296 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1297 init_state (&state, NULL);
1298 state.imap_browse = 1;
1299 imap_browse (LastDir, &state);
1300 menu->data = state.entry;
1303 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1305 init_menu (&state, menu, title, sizeof (title), buffy);
1312 case OP_BROWSER_NEW_FILE:
1314 snprintf (buf, sizeof (buf), "%s/", LastDir);
1315 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1317 strfcpy (f, buf, flen);
1318 destroy_state (&state);
1319 mutt_menuDestroy (&menu);
1322 MAYBE_REDRAW (menu->redraw);
1325 case OP_BROWSER_VIEW_FILE:
1326 if (!state.entrylen) {
1327 mutt_error _("No files match the file mask");
1333 if (state.entry[menu->current].selectable) {
1334 strfcpy (f, state.entry[menu->current].name, flen);
1335 destroy_state (&state);
1336 mutt_menuDestroy (&menu);
1341 if (S_ISDIR (state.entry[menu->current].mode) ||
1342 (S_ISLNK (state.entry[menu->current].mode) &&
1343 link_is_dir (LastDir, state.entry[menu->current].name))) {
1344 mutt_error _("Can't view a directory");
1350 char buf[_POSIX_PATH_MAX];
1352 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
1354 b = mutt_make_file_attach (buf);
1356 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1357 mutt_free_body (&b);
1358 menu->redraw = REDRAW_FULL;
1361 mutt_error _("Error trying to view file");
1368 if (option (OPTNEWS)) {
1369 struct folder_file *f = &state.entry[menu->current];
1372 if (i == OP_CATCHUP)
1373 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1375 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1379 struct folder_file ff;
1380 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1383 folder.ff->name = f->name;
1384 folder.ff->st = NULL;
1385 folder.ff->is_new = nd->new;
1388 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1389 newsgroup_format_str, (unsigned long) &folder,
1390 M_FORMAT_ARROWCURSOR);
1391 f->desc = safe_strdup (buffer); */
1392 if (menu->current + 1 < menu->max)
1394 menu->redraw = REDRAW_MOTION_RESYNCH;
1399 case OP_LOAD_ACTIVE:
1400 if (!option (OPTNEWS))
1407 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1408 if ((data = (NNTP_DATA *) tmp->data))
1412 nntp_get_active (CurrentNewsSrv);
1414 destroy_state (&state);
1416 examine_mailboxes (menu, &state);
1418 examine_directory (menu, &state, NULL, NULL);
1419 init_menu (&state, menu, title, sizeof (title), buffy);
1421 #endif /* USE_NNTP */
1423 #if defined USE_IMAP || defined USE_NNTP
1424 case OP_BROWSER_SUBSCRIBE:
1425 case OP_BROWSER_UNSUBSCRIBE:
1428 case OP_SUBSCRIBE_PATTERN:
1429 case OP_UNSUBSCRIBE_PATTERN:
1430 if (option (OPTNEWS)) {
1431 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1433 int j = menu->current;
1435 NNTP_SERVER *news = CurrentNewsSrv;
1437 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1442 if (i == OP_SUBSCRIBE_PATTERN)
1443 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1445 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1446 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1451 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1452 regerror (err, rx, buf, sizeof (buf));
1455 mutt_error ("%s", buf);
1458 menu->redraw = REDRAW_FULL;
1461 else if (!state.entrylen) {
1462 mutt_error _("No newsgroups match the mask");
1467 for (; j < state.entrylen; j++) {
1468 struct folder_file *f = &state.entry[j];
1470 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1471 regexec (rx, f->name, 0, NULL, 0) == 0) {
1472 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1473 nd = mutt_newsgroup_subscribe (news, f->name);
1475 nd = mutt_newsgroup_unsubscribe (news, f->name);
1479 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1481 folder.name = f->name;
1483 folder.new = nd->new;
1486 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1487 newsgroup_format_str, (unsigned long) &folder,
1488 M_FORMAT_ARROWCURSOR);
1489 f->desc = safe_strdup (buffer);
1492 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1493 if (menu->current + 1 < menu->max)
1495 menu->redraw = REDRAW_MOTION_RESYNCH;
1499 if (i == OP_SUBSCRIBE_PATTERN) {
1500 LIST *grouplist = NULL;
1503 grouplist = news->list;
1504 for (; grouplist; grouplist = grouplist->next) {
1505 nd = (NNTP_DATA *) grouplist->data;
1506 if (nd && nd->group && !nd->subscribed) {
1507 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1508 mutt_newsgroup_subscribe (news, nd->group);
1509 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1513 init_menu (&state, menu, title, sizeof (title), buffy);
1515 mutt_newsrc_update (news);
1516 nntp_clear_cacheindex (news);
1517 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1523 #endif /* USE_IMAP && USE_NNTP */
1524 #endif /* USE_NNTP */
1527 if (i == OP_BROWSER_SUBSCRIBE)
1528 imap_subscribe (state.entry[menu->current].name, 1);
1530 imap_subscribe (state.entry[menu->current].name, 0);
1532 #endif /* USE_IMAP */
1539 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));