Andreas Krennmair:
[apps/madmutt.git] / status.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  * 
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.
8  * 
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.
13  * 
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.
17  */ 
18
19 #include "mutt.h"
20 #include "mutt_menu.h"
21 #include "mutt_curses.h"
22 #include "sort.h"
23 #include "mapping.h"
24 #include "mx.h"
25
26 #include <string.h>
27 #include <ctype.h>
28 #include <unistd.h>
29
30 static char *get_sort_str (char *buf, size_t buflen, int method)
31 {
32   snprintf (buf, buflen, "%s%s%s",
33             (method & SORT_REVERSE) ? "reverse-" : "",
34             (method & SORT_LAST) ? "last-" : "",
35             mutt_getnamebyvalue (method & SORT_MASK, SortMethods));
36   return buf;
37 }
38
39 /* %b = number of incoming folders with unread messages [option]
40  * %d = number of deleted messages [option]
41  * %f = full mailbox path
42  * %F = number of flagged messages [option]
43  * %h = hostname
44  * %l = length of mailbox (in bytes) [option]
45  * %m = total number of messages [option]
46  * %M = number of messages shown (virutal message count when limiting) [option]
47  * %n = number of new messages [option]
48  * %p = number of postponed messages [option]
49  * %P = percent of way through index
50  * %r = readonly/wontwrite/changed flag
51  * %s = current sorting method ($sort)
52  * %S = current aux sorting method ($sort_aux)
53  * %t = # of tagged messages [option]
54  * %v = Mutt version 
55  * %V = currently active limit pattern [option] */
56 static const char *
57 status_format_str (char *buf, size_t buflen, char op, const char *src,
58                    const char *prefix, const char *ifstring,
59                    const char *elsestring,
60                    unsigned long data, format_flag flags)
61 {
62   char fmt[SHORT_STRING], tmp[SHORT_STRING], *cp;
63   int count, optional = (flags & M_FORMAT_OPTIONAL);
64   MUTTMENU *menu = (MUTTMENU *) data;
65
66   *buf = 0;
67   switch (op)
68   {
69     case 'b':
70       if (!optional)
71       {
72         snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
73         snprintf (buf, buflen, fmt, mutt_buffy_check (0));
74       }
75       else if (!mutt_buffy_check (0))
76         optional = 0;
77       break;
78
79     case 'd':
80       if (!optional)
81       {
82         snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
83         snprintf (buf, buflen, fmt, Context ? Context->deleted : 0);
84       }
85       else if (!Context || !Context->deleted)
86         optional = 0;
87       break;
88
89     case 'h':
90       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
91       snprintf (buf, buflen, fmt, NONULL(Hostname));
92       break;
93
94     case 'f':
95       snprintf (fmt, sizeof(fmt), "%%%ss", prefix);
96       if (Context && Context->path)
97       {
98         strfcpy (tmp, Context->path, sizeof (tmp));
99         mutt_pretty_mailbox (tmp);
100       }
101       else
102         strfcpy (tmp, _("(no mailbox)"), sizeof (tmp));
103       snprintf (buf, buflen, fmt, tmp);
104       break;
105
106     case 'F':
107       if (!optional)
108       {
109         snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
110         snprintf (buf, buflen, fmt, Context ? Context->flagged : 0);
111       }
112       else if (!Context || !Context->flagged)
113         optional = 0;
114       break;
115
116     case 'l':
117       if (!optional)
118       {
119         snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
120         mutt_pretty_size (tmp, sizeof (tmp), Context ? Context->size : 0);
121         snprintf (buf, buflen, fmt, tmp);
122       }
123       else if (!Context || !Context->size)
124         optional = 0;
125       break;
126
127     case 'L':
128       if (!optional)
129       {
130         snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
131         mutt_pretty_size (tmp, sizeof (tmp), Context ? Context->vsize: 0);
132         snprintf (buf, buflen, fmt, tmp);
133       }
134       else if (!Context || !Context->pattern)
135         optional = 0;
136       break;
137
138     case 'm':
139       if (!optional)
140       {
141         snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
142         snprintf (buf, buflen, fmt, Context ? Context->msgcount : 0);
143       }
144       else if (!Context || !Context->msgcount)
145         optional = 0;
146       break;
147
148     case 'M':
149       if (!optional)
150       {
151         snprintf (fmt, sizeof(fmt), "%%%sd", prefix);
152         snprintf (buf, buflen, fmt, Context ? Context->vcount : 0);
153       }
154       else if (!Context || !Context->pattern)
155         optional = 0;
156       break;
157
158     case 'n':
159       if (!optional)
160       {
161         snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
162         snprintf (buf, buflen, fmt, Context ? Context->new : 0);
163       }
164       else if (!Context || !Context->new)
165         optional = 0;
166       break;
167
168     case 'o':
169       if (!optional)
170       {
171         snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
172         snprintf (buf, buflen, fmt, Context ? Context->unread - Context->new : 0);
173       }
174       else if (!Context || !(Context->unread - Context->new))
175         optional = 0;
176       break;
177
178     case 'p':
179       count = mutt_num_postponed (0);
180       if (!optional)
181       {
182         snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
183         snprintf (buf, buflen, fmt, count);
184       }
185       else if (!count)
186         optional = 0;
187       break;
188
189     case 'P':
190       if (menu->top + menu->pagelen >= menu->max)
191         cp = menu->top ? "end" : "all";
192       else
193       {
194         count = (100 * (menu->top + menu->pagelen)) / menu->max;
195         snprintf (tmp, sizeof (tmp), "%d%%", count);
196         cp = tmp;
197       }
198       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
199       snprintf (buf, buflen, fmt, cp);
200       break;
201
202     case 'r':
203     {
204       int i = 0;
205
206       if (Context)
207       {
208         i = option(OPTATTACHMSG) ? 3 : ((Context->readonly ||
209           Context->dontwrite) ? 2 : (Context->changed || (
210 #ifdef USE_IMAP
211         /* deleted doesn't necessarily mean changed in IMAP */
212           Context->magic != M_IMAP &&
213 #endif
214           Context->deleted)) ? 1 : 0);
215       }
216       
217       if (!StChars)
218         buf[0] = 0;
219       else if (i >= mutt_strlen(StChars))
220         buf[0] = StChars[0];
221       else
222         buf[0] = StChars[i];
223
224       buf[1] = 0;
225       break;
226     }
227       
228     case 's':
229       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
230       snprintf (buf, buflen, fmt,
231                 get_sort_str (tmp, sizeof (tmp), Sort));
232       break;
233
234     case 'S':
235       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
236       snprintf (buf, buflen, fmt,
237                 get_sort_str (tmp, sizeof (tmp), SortAux));
238       break;
239
240     case 't':
241       if (!optional)
242       {
243         snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
244         snprintf (buf, buflen, fmt, Context ? Context->tagged : 0);
245       }
246       else if (!Context || !Context->tagged)
247         optional = 0;
248       break;
249
250     case 'u':
251       if (!optional)
252       {
253         snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
254         snprintf (buf, buflen, fmt, Context ? Context->unread : 0);
255       }
256       else if (!Context || !Context->unread)
257         optional = 0;
258       break;
259
260     case 'v':
261       snprintf (fmt, sizeof (fmt), "Mutt %%s");
262       snprintf (buf, buflen, fmt, MUTT_VERSION);
263       break;
264
265     case 'V':
266       if (!optional)
267       {
268         snprintf (fmt, sizeof(fmt), "%%%ss", prefix);
269         snprintf (buf, buflen, fmt, Context ? Context->pattern : 0);
270       }
271       else if (!Context || !Context->pattern)
272         optional = 0;
273       break;
274
275     case 0:
276       *buf = 0;
277       return (src);
278
279     default:
280       snprintf (buf, buflen, "%%%s%c", prefix, op);
281       break;
282   }
283
284   if (optional)
285     menu_status_line (buf, buflen, menu, ifstring);
286   else if (flags & M_FORMAT_OPTIONAL)
287     menu_status_line (buf, buflen, menu, elsestring);
288
289   return (src);
290 }
291
292 void menu_status_line (char *buf, size_t buflen, MUTTMENU *menu, const char *p)
293 {
294   mutt_FormatString (buf, buflen, p, status_format_str, (unsigned long) menu, 0);
295 }