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