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"
47 static struct mapping_t FolderHelp[] = {
48 { N_("Exit"), OP_EXIT },
49 { N_("Chdir"), OP_CHANGE_DIRECTORY },
50 { N_("Mask"), OP_ENTER_MASK },
51 { N_("Help"), OP_HELP },
56 static struct mapping_t FolderNewsHelp[] = {
57 { N_("Exit"), OP_EXIT },
58 { N_("List"), OP_TOGGLE_MAILBOXES },
59 { N_("Subscribe"), OP_BROWSER_SUBSCRIBE },
60 { N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE },
61 { N_("Catchup"), OP_CATCHUP },
62 { N_("Mask"), OP_ENTER_MASK },
63 { N_("Help"), OP_HELP },
68 typedef struct folder_t
70 struct folder_file *ff;
74 static char LastDir[_POSIX_PATH_MAX] = "";
75 static char LastDirBackup[_POSIX_PATH_MAX] = "";
77 /* Frees up the memory allocated for the local-global variables. */
78 static void destroy_state (struct browser_state *state)
82 for (c = 0; c < state->entrylen; c++)
84 FREE (&((state->entry)[c].name));
85 FREE (&((state->entry)[c].desc));
86 FREE (&((state->entry)[c].st));
89 FREE (&state->folder);
94 static int browser_compare_subject (const void *a, const void *b)
96 struct folder_file *pa = (struct folder_file *) a;
97 struct folder_file *pb = (struct folder_file *) b;
99 int r = mutt_strcoll (pa->name, pb->name);
101 return ((BrowserSort & SORT_REVERSE) ? -r : r);
104 static int browser_compare_date (const void *a, const void *b)
106 struct folder_file *pa = (struct folder_file *) a;
107 struct folder_file *pb = (struct folder_file *) b;
109 int r = pa->mtime - pb->mtime;
111 return ((BrowserSort & SORT_REVERSE) ? -r : r);
114 static int browser_compare_size (const void *a, const void *b)
116 struct folder_file *pa = (struct folder_file *) a;
117 struct folder_file *pb = (struct folder_file *) b;
119 int r = pa->size - pb->size;
121 return ((BrowserSort & SORT_REVERSE) ? -r : r);
124 static void browser_sort (struct browser_state *state)
126 int (*f) (const void *, const void *);
128 switch (BrowserSort & SORT_MASK)
134 if (option (OPTNEWS))
137 f = browser_compare_date;
141 if (option (OPTNEWS))
144 f = browser_compare_size;
148 f = browser_compare_subject;
151 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
154 static int link_is_dir (const char *folder, const char *path)
157 char fullpath[_POSIX_PATH_MAX];
159 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
161 if (stat (fullpath, &st) == 0)
162 return (S_ISDIR (st.st_mode));
168 folder_format_str (char *dest, size_t destlen, char op, const char *src,
169 const char *fmt, const char *ifstring, const char *elsestring,
170 unsigned long data, format_flag flags)
172 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
173 char date[16], *t_fmt;
175 FOLDER *folder = (FOLDER *) data;
178 int optional = (flags & M_FORMAT_OPTIONAL);
183 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
184 snprintf (dest, destlen, tmp, folder->num + 1);
188 if (folder->ff->st != NULL)
191 t_fmt = tnow - folder->ff->st->st_mtime < 31536000 ? "%b %d %H:%M" : "%b %d %Y";
192 strftime (date, sizeof (date), t_fmt, localtime (&folder->ff->st->st_mtime));
193 mutt_format_s (dest, destlen, fmt, date);
196 mutt_format_s (dest, destlen, fmt, "");
203 if (folder->ff->imap)
204 s = NONULL (folder->ff->desc);
207 s = NONULL (folder->ff->name);
209 snprintf (fn, sizeof (fn), "%s%s", s,
210 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
211 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
212 ((folder->ff->st->st_mode & S_IXUSR) != 0 ? "*" : ""))) : "");
214 mutt_format_s (dest, destlen, fmt, fn);
218 if (folder->ff->st != NULL)
220 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
221 S_ISDIR(folder->ff->st->st_mode) ? 'd' : (S_ISLNK(folder->ff->st->st_mode) ? 'l' : '-'),
222 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r': '-',
223 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
224 (folder->ff->st->st_mode & S_ISUID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) != 0 ? 'x': '-',
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) != 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) != 0 ? 'x': '-',
228 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
229 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
230 (folder->ff->st->st_mode & S_ISVTX) != 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) != 0 ? 'x': '-');
231 mutt_format_s (dest, destlen, fmt, permission);
234 else if (folder->ff->imap)
236 /* mark folders with subfolders AND mail */
237 snprintf (permission, sizeof (permission), "IMAP %c",
238 (folder->ff->inferiors && folder->ff->selectable) ? '+' : ' ');
239 mutt_format_s (dest, destlen, fmt, permission);
243 mutt_format_s (dest, destlen, fmt, "");
247 if (folder->ff->st != NULL)
249 if ((gr = getgrgid (folder->ff->st->st_gid)))
250 mutt_format_s (dest, destlen, fmt, gr->gr_name);
253 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
254 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
258 mutt_format_s (dest, destlen, fmt, "");
262 if (folder->ff->st != NULL)
264 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
265 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
268 mutt_format_s (dest, destlen, fmt, "");
273 if (mx_is_imap (folder->ff->desc))
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)
292 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
293 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
296 mutt_format_s (dest, destlen, fmt, "");
300 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
301 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
305 if (folder->ff->st != NULL)
307 if ((pw = getpwuid (folder->ff->st->st_uid)))
308 mutt_format_s (dest, destlen, fmt, pw->pw_name);
311 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
312 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
316 mutt_format_s (dest, destlen, fmt, "");
320 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
321 snprintf (dest, destlen, tmp, op);
326 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
327 else if (flags & M_FORMAT_OPTIONAL)
328 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
335 newsgroup_format_str (char *dest, size_t destlen, char op, const char *src,
336 const char *fmt, const char *ifstring, const char *elsestring,
337 unsigned long data, format_flag flags)
339 char fn[SHORT_STRING], tmp[SHORT_STRING];
340 FOLDER *folder = (FOLDER *) data;
345 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
346 snprintf (dest, destlen, tmp, folder->num + 1);
350 strncpy (fn, folder->ff->name, sizeof(fn) - 1);
351 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
352 snprintf (dest, destlen, tmp, fn);
356 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
357 if (folder->ff->nd->subscribed)
358 snprintf (dest, destlen, tmp, ' ');
360 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
364 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
365 if (folder->ff->nd->deleted)
366 snprintf (dest, destlen, tmp, 'D');
368 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
372 if (flags & M_FORMAT_OPTIONAL)
374 if (folder->ff->nd->unread != 0)
375 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
378 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
381 else if (Context && Context->data == folder->ff->nd)
383 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
384 snprintf (dest, destlen, tmp, Context->unread);
388 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
389 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
394 if (Context && Context->data == folder->ff->nd)
396 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
397 snprintf (dest, destlen, tmp, Context->new);
399 else if (option (OPTMARKOLD) &&
400 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
401 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage)
403 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
404 snprintf (dest, destlen, tmp, folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
408 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
409 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
414 if (folder->ff->nd->desc != NULL)
416 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
417 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
421 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
422 snprintf (dest, destlen, tmp, "");
428 #endif /* USE_NNTP */
430 static void add_folder (MUTTMENU *m, struct browser_state *state,
431 const char *name, const struct stat *s,
434 if (state->entrylen == state->entrymax)
436 /* need to allocate more space */
437 safe_realloc (&state->entry,
438 sizeof (struct folder_file) * (state->entrymax += 256));
439 memset (&state->entry[state->entrylen], 0,
440 sizeof (struct folder_file) * 256);
442 m->data = state->entry;
447 (state->entry)[state->entrylen].mode = s->st_mode;
448 (state->entry)[state->entrylen].mtime = s->st_mtime;
449 (state->entry)[state->entrylen].size = s->st_size;
451 (state->entry)[state->entrylen].st = safe_malloc (sizeof (struct stat));
452 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
455 (state->entry)[state->entrylen].new = new;
456 (state->entry)[state->entrylen].name = safe_strdup (name);
457 (state->entry)[state->entrylen].desc = safe_strdup (name);
459 (state->entry)[state->entrylen].imap = 0;
462 if (option (OPTNEWS))
463 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
468 static void init_state (struct browser_state *state, MUTTMENU *menu)
471 state->entrymax = 256;
472 state->entry = (struct folder_file *) safe_calloc (state->entrymax, sizeof (struct folder_file));
474 state->imap_browse = 0;
477 menu->data = state->entry;
480 /* get list of all files/newsgroups with mask */
481 static int examine_directory (MUTTMENU *menu, struct browser_state *state,
482 char *d, const char *prefix)
485 if (option (OPTNEWS))
489 NNTP_SERVER *news = CurrentNewsSrv;
491 /* mutt_buffy_check (0); */
492 init_state (state, menu);
494 for (tmp = news->list; tmp; tmp = tmp->next)
496 if (!(data = (NNTP_DATA *)tmp->data))
498 if (prefix && *prefix && strncmp (prefix, data->group,
499 strlen (prefix)) != 0)
501 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
503 add_folder (menu, state, data->group, NULL, data, data->new);
507 #endif /* USE_NNTP */
512 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
515 while (stat (d, &s) == -1)
519 /* The last used directory is deleted, try to use the parent dir. */
520 char *c = strrchr (d, '/');
532 if (!S_ISDIR (s.st_mode))
534 mutt_error (_("%s is not a directory."), d);
538 mutt_buffy_check (0);
540 if ((dp = opendir (d)) == NULL)
546 init_state (state, menu);
548 while ((de = readdir (dp)) != NULL)
550 if (mutt_strcmp (de->d_name, ".") == 0)
551 continue; /* we don't need . */
553 if (prefix && *prefix && mutt_strncmp (prefix, de->d_name, mutt_strlen (prefix)) != 0)
555 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
558 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
559 if (lstat (buffer, &s) == -1)
562 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
563 (! S_ISLNK (s.st_mode)))
567 while (tmp && mutt_strcmp (buffer, tmp->path))
569 add_folder (menu, state, de->d_name, &s, NULL, (tmp) ? tmp->new : 0);
573 draw_sidebar (CurrentMenu);
574 browser_sort (state);
578 /* get list of mailboxes/subscribed newsgroups */
579 static int examine_mailboxes (MUTTMENU *menu, struct browser_state *state)
582 char buffer[LONG_STRING];
585 if (option (OPTNEWS))
589 NNTP_SERVER *news = CurrentNewsSrv;
591 /* mutt_buffy_check (0); */
592 init_state (state, menu);
594 for (tmp = news->list; tmp; tmp = tmp->next)
596 if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
597 (data->subscribed && (!option (OPTSHOWONLYUNREAD) || data->unread))))
598 add_folder (menu, state, data->group, NULL, data, data->new);
604 BUFFY *tmp = Incoming;
608 mutt_buffy_check (0);
610 init_state (state, menu);
615 if (mx_is_imap (tmp->path))
617 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
622 if (mx_is_pop (tmp->path))
624 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
629 if (mx_is_nntp (tmp->path))
631 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
635 if (lstat (tmp->path, &s) == -1)
638 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
639 (! S_ISLNK (s.st_mode)))
642 strfcpy (buffer, NONULL(tmp->path), sizeof (buffer));
643 mutt_pretty_mailbox (buffer);
645 add_folder (menu, state, buffer, &s, NULL, tmp->new);
647 while ((tmp = tmp->next));
649 browser_sort (state);
653 static int select_file_search (MUTTMENU *menu, regex_t *re, int n)
656 if (option (OPTNEWS))
657 return (regexec (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
659 return (regexec (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
662 static void folder_entry (char *s, size_t slen, MUTTMENU *menu, int num)
666 folder.ff = &((struct folder_file *) menu->data)[num];
670 if (option (OPTNEWS))
671 mutt_FormatString (s, slen, NONULL(GroupFormat), newsgroup_format_str,
672 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
675 mutt_FormatString (s, slen, NONULL(FolderFormat), folder_format_str,
676 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
679 static void init_menu (struct browser_state *state, MUTTMENU *menu, char *title,
680 size_t titlelen, int buffy)
682 char path[_POSIX_PATH_MAX];
684 menu->max = state->entrylen;
686 if(menu->current >= menu->max)
687 menu->current = menu->max - 1;
688 if (menu->current < 0)
690 if (menu->top > menu->current)
696 if (option (OPTNEWS))
699 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
701 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
702 CurrentNewsSrv->conn->account.host);
707 snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
710 strfcpy (path, LastDir, sizeof (path));
711 mutt_pretty_mailbox (path);
713 if (state->imap_browse && option (OPTIMAPLSUB))
714 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
715 path, NONULL (Mask.pattern));
718 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
719 path, NONULL(Mask.pattern));
721 menu->redraw = REDRAW_FULL;
724 static int file_tag (MUTTMENU *menu, int n, int m)
726 struct folder_file *ff = &(((struct folder_file *)menu->data)[n]);
728 if (S_ISDIR (ff->mode) || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name)))
730 mutt_error _("Can't attach a directory!");
735 ff->tagged = (m >= 0 ? m : !ff->tagged);
737 return ff->tagged - ot;
740 void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *numfiles)
742 char buf[_POSIX_PATH_MAX];
743 char prefix[_POSIX_PATH_MAX] = "";
744 char helpstr[SHORT_STRING];
746 struct browser_state state;
749 int i, killPrefix = 0;
750 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
751 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
752 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
754 buffy = buffy && folder;
756 memset (&state, 0, sizeof (struct browser_state));
759 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
762 if (option (OPTNEWS))
765 strfcpy (prefix, f, sizeof (prefix));
770 /* default state for news reader mode is browse subscribed newsgroups */
772 for (list = CurrentNewsSrv->list; list; list = list->next)
774 NNTP_DATA *data = (NNTP_DATA *) list->data;
776 if (data && data->subscribed)
788 mutt_expand_path (f, flen);
792 init_state (&state, NULL);
793 state.imap_browse = 1;
794 imap_browse (f, &state);
795 strfcpy (LastDir, state.folder, sizeof (LastDir));
800 for (i = mutt_strlen (f) - 1; i > 0 && f[i] != '/' ; i--);
805 if (i > sizeof (LastDir) - 1) i = sizeof (LastDir) - 1;
806 strncpy (LastDir, f, i);
811 getcwd (LastDir, sizeof (LastDir));
812 safe_strcat (LastDir, sizeof (LastDir), "/");
813 safe_strncat (LastDir, sizeof (LastDir), f, i);
819 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
821 getcwd (LastDir, sizeof (LastDir));
824 if (i <= 0 && f[0] != '/')
825 strfcpy (prefix, f, sizeof (prefix));
827 strfcpy (prefix, f + i + 1, sizeof (prefix));
836 getcwd (LastDir, sizeof (LastDir));
837 else if (!LastDir[0])
838 strfcpy (LastDir, NONULL(Maildir), sizeof (LastDir));
841 if (!buffy && mx_is_imap (LastDir))
843 init_state (&state, NULL);
844 state.imap_browse = 1;
845 imap_browse (LastDir, &state);
854 if (examine_mailboxes (NULL, &state) == -1)
859 if (!state.imap_browse)
861 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
864 menu = mutt_new_menu ();
865 menu->menu = MENU_FOLDER;
866 menu->make_entry = folder_entry;
867 menu->search = select_file_search;
869 menu->data = state.entry;
871 menu->tag = file_tag;
873 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
875 (option (OPTNEWS)) ? FolderNewsHelp :
879 init_menu (&state, menu, title, sizeof (title), buffy);
883 switch (i = mutt_menuLoop (menu))
885 case OP_GENERIC_SELECT_ENTRY:
889 mutt_error _("No files match the file mask");
893 if (S_ISDIR (state.entry[menu->current].mode) ||
894 (S_ISLNK (state.entry[menu->current].mode) &&
895 link_is_dir (LastDir, state.entry[menu->current].name))
897 || state.entry[menu->current].inferiors
901 /* make sure this isn't a MH or maildir mailbox */
904 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
905 mutt_expand_path (buf, sizeof (buf));
908 else if (state.imap_browse)
910 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
914 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
916 if ((mx_get_magic (buf) <= 0)
918 || state.entry[menu->current].inferiors
922 char OldLastDir[_POSIX_PATH_MAX];
924 /* save the old directory */
925 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
927 if (mutt_strcmp (state.entry[menu->current].name, "..") == 0)
929 if (mutt_strcmp ("..", LastDir + mutt_strlen (LastDir) - 2) == 0)
930 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
933 char *p = strrchr (LastDir + 1, '/');
939 if (LastDir[0] == '/')
942 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
948 strfcpy (LastDir, state.entry[menu->current].name, sizeof (LastDir));
949 mutt_expand_path (LastDir, sizeof (LastDir));
952 else if (state.imap_browse)
957 strfcpy (LastDir, state.entry[menu->current].name,
959 /* tack on delimiter here */
960 n = strlen (LastDir)+1;
962 /* special case "" needs no delimiter */
963 url_parse_ciss (&url, state.entry[menu->current].name);
965 (state.entry[menu->current].delim != '\0') &&
966 (n < sizeof (LastDir)))
969 LastDir[n-1] = state.entry[menu->current].delim;
975 char tmp[_POSIX_PATH_MAX];
976 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name, sizeof (tmp));
977 strfcpy (LastDir, tmp, sizeof (LastDir));
980 destroy_state (&state);
988 if (state.imap_browse)
990 init_state (&state, NULL);
991 state.imap_browse = 1;
992 imap_browse (LastDir, &state);
993 menu->data = state.entry;
997 if (examine_directory (menu, &state, LastDir, prefix) == -1)
999 /* try to restore the old values */
1000 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
1001 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1003 strfcpy (LastDir, NONULL(Homedir), sizeof (LastDir));
1009 init_menu (&state, menu, title, sizeof (title), buffy);
1015 if (buffy || option (OPTNEWS)) /* news have not path */
1020 strfcpy (f, state.entry[menu->current].name, flen);
1021 mutt_expand_path (f, flen);
1024 else if (state.imap_browse)
1025 strfcpy (f, state.entry[menu->current].name, flen);
1028 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
1030 /* Fall through to OP_EXIT */
1041 *numfiles = menu->tagged;
1042 tfiles = safe_calloc (*numfiles, sizeof (char *));
1043 for (i = 0, j = 0; i < state.entrylen; i++)
1045 struct folder_file ff = state.entry[i];
1046 char full[_POSIX_PATH_MAX];
1049 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1050 mutt_expand_path (full, sizeof (full));
1051 tfiles[j++] = safe_strdup (full);
1056 else if (f[0]) /* no tagged entries. return selected entry */
1059 tfiles = safe_calloc (*numfiles, sizeof (char *));
1060 mutt_expand_path (f, flen);
1061 tfiles[0] = safe_strdup (f);
1066 destroy_state (&state);
1067 mutt_menuDestroy (&menu);
1070 case OP_BROWSER_TELL:
1072 mutt_message("%s", state.entry[menu->current].name);
1076 case OP_BROWSER_TOGGLE_LSUB:
1077 if (option (OPTIMAPLSUB))
1079 unset_option (OPTIMAPLSUB);
1083 set_option (OPTIMAPLSUB);
1085 mutt_ungetch (0, OP_CHECK_NEW);
1088 case OP_CREATE_MAILBOX:
1089 if (!state.imap_browse)
1090 mutt_error (_("Create is only supported for IMAP mailboxes"));
1093 imap_mailbox_create (LastDir);
1094 /* TODO: find a way to detect if the new folder would appear in
1095 * this window, and insert it without starting over. */
1096 destroy_state (&state);
1097 init_state (&state, NULL);
1098 state.imap_browse = 1;
1099 imap_browse (LastDir, &state);
1100 menu->data = state.entry;
1103 init_menu (&state, menu, title, sizeof (title), buffy);
1104 MAYBE_REDRAW (menu->redraw);
1108 case OP_RENAME_MAILBOX:
1109 if (!state.entry[menu->current].imap)
1110 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1113 int nentry = menu->current;
1115 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1116 destroy_state (&state);
1117 init_state (&state, NULL);
1118 state.imap_browse = 1;
1119 imap_browse (LastDir, &state);
1120 menu->data = state.entry;
1123 init_menu (&state, menu, title, sizeof (title), buffy);
1124 MAYBE_REDRAW (menu->redraw);
1129 case OP_DELETE_MAILBOX:
1130 if (!state.entry[menu->current].imap)
1131 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1134 char msg[SHORT_STRING];
1136 int nentry = menu->current;
1138 imap_parse_path (state.entry[nentry].name, &mx);
1139 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1141 if (mutt_yesorno (msg, M_NO) == M_YES)
1143 if (!imap_delete_mailbox (Context, mx))
1145 /* free the mailbox from the browser */
1146 FREE (&((state.entry)[nentry].name));
1147 FREE (&((state.entry)[nentry].desc));
1148 /* and move all other entries up */
1149 if (nentry+1 < state.entrylen)
1150 memmove (state.entry + nentry, state.entry + nentry + 1,
1151 sizeof (struct folder_file) * (state.entrylen - (nentry+1)));
1153 mutt_message _("Mailbox deleted.");
1154 init_menu (&state, menu, title, sizeof (title), buffy);
1155 MAYBE_REDRAW (menu->redraw);
1159 mutt_message _("Mailbox not deleted.");
1165 case OP_CHANGE_DIRECTORY:
1168 if (option (OPTNEWS))
1172 strfcpy (buf, LastDir, sizeof (buf));
1174 if (!state.imap_browse)
1177 /* add '/' at the end of the directory name if not already there */
1178 int len=mutt_strlen(LastDir);
1179 if (len && LastDir[len-1] != '/' && sizeof (buf) > len)
1183 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1187 mutt_expand_path (buf, sizeof (buf));
1189 if (mx_is_imap (buf))
1191 strfcpy (LastDir, buf, sizeof (LastDir));
1192 destroy_state (&state);
1193 init_state (&state, NULL);
1194 state.imap_browse = 1;
1195 imap_browse (LastDir, &state);
1196 menu->data = state.entry;
1199 init_menu (&state, menu, title, sizeof (title), buffy);
1203 if (stat (buf, &st) == 0)
1205 if (S_ISDIR (st.st_mode))
1207 destroy_state (&state);
1208 if (examine_directory (menu, &state, buf, prefix) == 0)
1209 strfcpy (LastDir, buf, sizeof (LastDir));
1212 mutt_error _("Error scanning directory.");
1213 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1215 mutt_menuDestroy (&menu);
1221 init_menu (&state, menu, title, sizeof (title), buffy);
1224 mutt_error (_("%s is not a directory."), buf);
1229 MAYBE_REDRAW (menu->redraw);
1234 strfcpy (buf, NONULL(Mask.pattern), sizeof (buf));
1235 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0)
1237 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1242 /* assume that the user wants to see everything */
1244 strfcpy (buf, ".", sizeof (buf));
1253 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1255 regerror (err, rx, buf, sizeof (buf));
1258 mutt_error ("%s", buf);
1262 mutt_str_replace (&Mask.pattern, buf);
1268 destroy_state (&state);
1270 if (state.imap_browse)
1272 init_state (&state, NULL);
1273 state.imap_browse = 1;
1274 imap_browse (LastDir, &state);
1275 menu->data = state.entry;
1276 init_menu (&state, menu, title, sizeof (title), buffy);
1280 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1281 init_menu (&state, menu, title, sizeof (title), buffy);
1284 mutt_error _("Error scanning directory.");
1285 mutt_menuDestroy (&menu);
1289 if (!state.entrylen)
1291 mutt_error _("No files match the file mask");
1296 MAYBE_REDRAW (menu->redraw);
1300 case OP_SORT_REVERSE:
1304 int reverse = (i == OP_SORT_REVERSE);
1306 switch (mutt_multi_choice ((reverse) ?
1307 _("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ") :
1308 _("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1311 case -1: /* abort */
1315 case 1: /* (d)ate */
1316 BrowserSort = SORT_DATE;
1319 case 2: /* (a)lpha */
1320 BrowserSort = SORT_SUBJECT;
1323 case 3: /* si(z)e */
1324 BrowserSort = SORT_SIZE;
1327 case 4: /* do(n)'t sort */
1328 BrowserSort = SORT_ORDER;
1334 BrowserSort |= reverse ? SORT_REVERSE : 0;
1335 browser_sort (&state);
1336 menu->redraw = REDRAW_FULL;
1341 case OP_TOGGLE_MAILBOXES:
1345 destroy_state (&state);
1351 if (examine_mailboxes (menu, &state) == -1)
1355 else if (mx_is_imap (LastDir))
1357 init_state (&state, NULL);
1358 state.imap_browse = 1;
1359 imap_browse (LastDir, &state);
1360 menu->data = state.entry;
1363 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1365 init_menu (&state, menu, title, sizeof (title), buffy);
1372 case OP_BROWSER_NEW_FILE:
1374 snprintf (buf, sizeof (buf), "%s/", LastDir);
1375 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) == 0)
1377 strfcpy (f, buf, flen);
1378 destroy_state (&state);
1379 mutt_menuDestroy (&menu);
1382 MAYBE_REDRAW (menu->redraw);
1385 case OP_BROWSER_VIEW_FILE:
1386 if (!state.entrylen)
1388 mutt_error _("No files match the file mask");
1393 if (state.entry[menu->current].selectable)
1395 strfcpy (f, state.entry[menu->current].name, flen);
1396 destroy_state (&state);
1397 mutt_menuDestroy (&menu);
1402 if (S_ISDIR (state.entry[menu->current].mode) ||
1403 (S_ISLNK (state.entry[menu->current].mode) &&
1404 link_is_dir (LastDir, state.entry[menu->current].name)))
1406 mutt_error _("Can't view a directory");
1412 char buf[_POSIX_PATH_MAX];
1414 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
1415 b = mutt_make_file_attach (buf);
1418 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1419 mutt_free_body (&b);
1420 menu->redraw = REDRAW_FULL;
1423 mutt_error _("Error trying to view file");
1430 if (option (OPTNEWS))
1432 struct folder_file *f = &state.entry[menu->current];
1435 if (i == OP_CATCHUP)
1436 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1438 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1443 struct folder_file ff;
1444 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1447 folder.ff->name = f->name;
1448 folder.ff->st = NULL;
1449 folder.ff->is_new = nd->new;
1452 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1453 newsgroup_format_str, (unsigned long) &folder,
1454 M_FORMAT_ARROWCURSOR);
1455 f->desc = safe_strdup (buffer); */
1456 if (menu->current + 1 < menu->max)
1458 menu->redraw = REDRAW_MOTION_RESYNCH;
1463 case OP_LOAD_ACTIVE:
1464 if (!option (OPTNEWS))
1471 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next)
1473 if ((data = (NNTP_DATA *)tmp->data))
1477 nntp_get_active (CurrentNewsSrv);
1479 destroy_state (&state);
1481 examine_mailboxes (menu, &state);
1483 examine_directory (menu, &state, NULL, NULL);
1484 init_menu (&state, menu, title, sizeof (title), buffy);
1486 #endif /* USE_NNTP */
1488 #if defined USE_IMAP || defined USE_NNTP
1489 case OP_BROWSER_SUBSCRIBE:
1490 case OP_BROWSER_UNSUBSCRIBE:
1493 case OP_SUBSCRIBE_PATTERN:
1494 case OP_UNSUBSCRIBE_PATTERN:
1495 if (option (OPTNEWS))
1497 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1499 int j = menu->current;
1501 NNTP_SERVER *news = CurrentNewsSrv;
1503 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN)
1509 if (i == OP_SUBSCRIBE_PATTERN)
1510 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1512 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1513 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0])
1519 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1521 regerror (err, rx, buf, sizeof (buf));
1524 mutt_error ("%s", buf);
1527 menu->redraw = REDRAW_FULL;
1530 else if (!state.entrylen)
1532 mutt_error _("No newsgroups match the mask");
1536 for ( ; j < state.entrylen; j++)
1538 struct folder_file *f = &state.entry[j];
1540 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1541 regexec (rx, f->name, 0, NULL, 0) == 0)
1543 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1544 nd = mutt_newsgroup_subscribe (news, f->name);
1546 nd = mutt_newsgroup_unsubscribe (news, f->name);
1550 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1552 folder.name = f->name;
1554 folder.new = nd->new;
1557 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1558 newsgroup_format_str, (unsigned long) &folder,
1559 M_FORMAT_ARROWCURSOR);
1560 f->desc = safe_strdup (buffer);
1563 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE)
1565 if (menu->current + 1 < menu->max)
1567 menu->redraw = REDRAW_MOTION_RESYNCH;
1571 if (i == OP_SUBSCRIBE_PATTERN)
1573 LIST *grouplist = NULL;
1576 grouplist = news->list;
1577 for (; grouplist; grouplist = grouplist->next)
1579 nd = (NNTP_DATA *) grouplist->data;
1580 if (nd && nd->group && !nd->subscribed)
1582 if (regexec (rx, nd->group, 0, NULL, 0) == 0)
1584 mutt_newsgroup_subscribe (news, nd->group);
1585 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1589 init_menu (&state, menu, title, sizeof (title), buffy);
1591 mutt_newsrc_update (news);
1592 nntp_clear_cacheindex (news);
1593 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1599 #endif /* USE_IMAP && USE_NNTP */
1600 #endif /* USE_NNTP */
1603 if (i == OP_BROWSER_SUBSCRIBE)
1604 imap_subscribe (state.entry[menu->current].name, 1);
1606 imap_subscribe (state.entry[menu->current].name, 0);
1608 #endif /* USE_IMAP */
1615 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));