let m_strformat accept NULL formats.
[apps/madmutt.git] / lib-ui / sidebar.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) ????-2004 Justin Hibbits <jrh29@po.cwru.edu>
4  * Copyright (C) 2004 Thomer M. Gil <mutt@thomer.com>
5  *
6  * Parts were written/modified by:
7  * Rocco Rutte <pdmef@cs.tu-berlin.de>
8  * Nico Golde <nico@ngolde.de>
9  *
10  * This file is part of mutt-ng, see http://www.muttng.org/.
11  * It's licensed under the GNU General Public License,
12  * please see the file GPL in the top level source directory.
13  */
14
15 #include <lib-lib/lib-lib.h>
16 #include <libgen.h>
17
18 #include <lib-ui/curses.h>
19 #include <lib-ui/menu.h>
20 #include <lib-ui/sidebar.h>
21
22 #include "mutt.h"
23 #include "charset.h"
24 #include "buffy.h"
25 #include "keymap.h"
26
27 static int TopBuffy = 0;
28 static int CurBuffy = 0;
29 static int known_lines = 0;
30 static short initialized = 0;
31 static short prev_show_value;
32
33 /* computes first entry to be shown */
34 static void calc_boundaries (void) {
35   if (!Incoming.len)
36     return;
37   if (CurBuffy < 0 || CurBuffy >= Incoming.len)
38     CurBuffy = 0;
39   if (TopBuffy < 0 || TopBuffy >= Incoming.len)
40     TopBuffy = 0;
41
42   if (option (OPTSIDEBARNEWMAILONLY)) {
43     int i = CurBuffy;
44     TopBuffy = CurBuffy - 1;
45     while (i >= 0) {
46       if (Incoming.arr[i]->new > 0)
47         TopBuffy = i;
48       i--;
49     }
50   } else if (known_lines>0)
51     TopBuffy = CurBuffy - (CurBuffy % known_lines);
52   if (TopBuffy < 0)
53     TopBuffy = 0;
54 }
55
56 static char *shortened_hierarchy (char *hbox, int maxlen)
57 {
58   int dots = 0;
59   char *last_dot = NULL;
60   int i, j, len = m_strlen(hbox);
61   char *new_box;
62
63   if (!SidebarBoundary || !*SidebarBoundary)
64     return (m_strdup(hbox));
65
66   for (i = 0; i < len; ++i) {
67     if (strchr (SidebarBoundary, hbox[i])) {
68       ++dots;
69       last_dot = &hbox[i];
70     }
71   }
72
73   if (last_dot) {
74     ++last_dot;
75     new_box = p_new(char, maxlen + 1);
76     new_box[0] = hbox[0];
77     for (i = 1, j = 1; j < maxlen && i < len; ++i) {
78       if (strchr (SidebarBoundary, hbox[i])) {
79         new_box[j++] = hbox[i];
80         new_box[j] = 0;
81         if (&hbox[i + 1] != last_dot || j + m_strlen(last_dot) > maxlen) {
82           new_box[j++] = hbox[i + 1];
83           new_box[j] = 0;
84         } else {
85           m_strcat(&new_box[j], maxlen + 1, last_dot);
86           break;
87         }
88       }
89     }
90     return new_box;
91   }
92   return m_strdup(hbox);
93 }
94
95 static const char* sidebar_number_format (char* dest, ssize_t destlen, char op,
96                                           const char* src, const char* fmt,
97                                           const char* ifstr, const char* elstr,
98                                           anytype data, format_flag flags) {
99   char tmp[STRING];
100   BUFFY* b = Incoming.arr[data.i];
101   int opt = flags & M_FORMAT_OPTIONAL;
102   int c = Context && !m_strcmp(Context->path, b->path);
103
104   switch (op) {
105     /* deleted */
106     case 'd':
107       if (!opt) {
108         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
109         snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
110       } else if ((c && Context->deleted == 0) || !c)
111         opt = 0;
112       break;
113     /* flagged */
114     case 'F':
115     case 'f':                   /* for compatibility */
116       if (!opt) {
117         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
118         snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
119       } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
120         opt = 0;
121       break;
122     /* total */
123     case 'c':                   /* for compatibility */
124     case 'm':
125       if (!opt) {
126         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
127         snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
128       } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
129         opt = 0;
130       break;
131     /* total shown, i.e. not hidden by limit */
132     case 'M':
133       if (!opt) {
134         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
135         snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
136       } else if ((c && Context->vcount == 0) || !c)
137         opt = 0;
138       break;
139     /* new */
140     case 'n':
141       if (!opt) {
142         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
143         snprintf (dest, destlen, tmp, c ? Context->new : b->new);
144       } else if ((c && Context->new == 0) || (!c && b->new == 0))
145         opt = 0;
146       break;
147     /* unread */
148     case 'u':
149       if (!opt) {
150         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
151         snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
152       } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
153         opt = 0;
154       break;
155     /* tagged */
156     case 't':
157       if (!opt) {
158         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
159         snprintf (dest, destlen, tmp, c ? Context->tagged : 0);
160       } else if ((c && Context->tagged == 0) || !c)
161         opt = 0;
162       break;
163   }
164
165   if (flags & M_FORMAT_OPTIONAL)
166     m_strformat(dest, destlen, opt ? ifstr : elstr, sidebar_number_format,
167                 data, flags);
168   return src;
169 }
170
171 int sidebar_need_count (void) {
172   if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
173       !SidebarNumberFormat || !*SidebarNumberFormat)
174     return (0);
175   return (1);
176 }
177
178 /* print single item
179  * returns:
180  *      0       item was not printed ('cause of $sidebar_newmail_only)
181  *      1       item was printed
182  */
183 static int make_sidebar_entry (char* sbox, int idx, ssize_t len)
184 {
185   int shortened = 0, lencnt = 0;
186   char no[STRING], entry[STRING];
187   int l = m_strlen(ImapHomeNamespace);
188   int l_m = m_strlen(Maildir);
189
190   if (SidebarWidth > COLS)
191     SidebarWidth = COLS;
192
193   if (option (OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path && 
194       m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0)
195     /* if $sidebar_newmail_only is set, don't display the
196      * box only if it's not the currently opened
197      * (i.e. always display the currently opened) */
198     return 0;
199
200   m_strformat(no, len, SidebarNumberFormat, sidebar_number_format, idx, 0);
201   lencnt = m_strlen(no);
202
203   if (l > 0 && m_strncmp(sbox, ImapHomeNamespace, l) == 0 && 
204       m_strlen(sbox) > l)
205     sbox += l + 1; /* we're trimming the ImapHomeNamespace, the "+ 1" is for the separator */
206   else
207   if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 && 
208       m_strlen(sbox) > l_m) {
209     sbox += l_m;
210     if (Maildir[strlen(Maildir)-1]!='/') {
211       sbox += 1;
212     }
213   } else
214     sbox = basename (sbox);
215
216   if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len-lencnt-1) {
217     sbox = shortened_hierarchy (sbox, len-lencnt-1);
218     shortened = 1;
219   }
220
221   snprintf(entry, sizeof(entry), "%*s", (int)len, no);
222   memcpy(entry, sbox, MIN(len - 1, m_strlen(sbox)));
223   addnstr(entry, len);
224
225   if (shortened)
226     p_delete(&sbox);
227
228   return 1;
229 }
230
231 /* returns folder name of currently 
232  * selected folder for <sidebar-open>
233  */
234 const char* sidebar_get_current (void) {
235   if (!Incoming.len)
236     return (NULL);
237   return Incoming.arr[CurBuffy]->path;
238 }
239
240 /* internally sets item to buf */
241 void sidebar_set_current (const char* buf) {
242   int i = buffy_lookup (buf);
243   if (i >= 0) {
244     CurBuffy = i;
245     calc_boundaries();
246   }
247 }
248
249 /* fix counters for a context
250  * FIXME since ctx must not be of our business, move it elsewhere
251  */
252 void sidebar_set_buffystats (CONTEXT* curContext) {
253   int i = 0;
254   BUFFY* tmp = NULL;
255   if (!curContext || !Incoming.len || (i = buffy_lookup (curContext->path)) < 0)
256     return;
257   tmp = Incoming.arr[i];
258   tmp->new = curContext->new;
259   tmp->msg_unread = curContext->unread;
260   tmp->msgcount = curContext->msgcount;
261   tmp->msg_flagged = curContext->flagged;
262 }
263
264 void sidebar_draw_frames (void) {
265   ssize_t i,delim_len;
266
267   if (!option(OPTMBOXPANE) || SidebarWidth==0) 
268     return;
269
270   delim_len=m_strlen(NONULL(SidebarDelim));
271
272   /* draw vertical delimiter */
273   SETCOLOR (MT_COLOR_SIDEBAR);
274   for (i = 0; i < LINES-1; i++) {
275     move (i, SidebarWidth - delim_len);
276     if (option (OPTASCIICHARS))
277       addstr (NONULL (SidebarDelim));
278     else if (!option (OPTASCIICHARS) && !m_strcmp(SidebarDelim, "|"))
279       addch (ACS_VLINE);
280     else if ((Charset_is_utf8) && !m_strcmp(SidebarDelim, "|"))
281       addstr ("\342\224\202");
282     else
283       addstr (NONULL (SidebarDelim));
284   }
285
286   /* fill "gaps" at top+bottom */
287   SETCOLOR(MT_COLOR_STATUS);
288   for (i=0; i<SidebarWidth; i++) {
289     /*
290      * if we don't have $status_on_top and have $help, fill top
291      * gap with spaces to get bg color
292      */
293     if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
294       move(0,i);
295       addch(' ');
296     }
297     /*
298       * if we don't have $status_on_top or we have $help, fill bottom
299       * gap with spaces to get bg color
300       */
301     if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
302       move(LINES-2,i);
303       addch(' ');
304     }
305   }
306   SETCOLOR (MT_COLOR_NORMAL);
307 }
308
309 /* actually draws something
310  * FIXME this needs some clue when to do it
311  */
312 int sidebar_draw (void) {
313   int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
314       last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
315       i = 0,line;
316   BUFFY *tmp;
317   ssize_t delim_len = m_strlen(SidebarDelim);
318   char blank[STRING];
319
320   known_lines=last_line-first_line;
321
322   /* initialize first time */
323   if (!initialized) {
324     prev_show_value = option (OPTMBOXPANE);
325     initialized = 1;
326   }
327
328   if (TopBuffy==0 || CurBuffy==0)
329     calc_boundaries();
330
331   /* save or restore the value SidebarWidth */
332   if (prev_show_value != option (OPTMBOXPANE)) {
333     if (!prev_show_value && option (OPTMBOXPANE)) {
334       /* after toggle: force recounting of all mail */
335       buffy_check (2);
336     }
337     prev_show_value = option (OPTMBOXPANE);
338   }
339
340   if (SidebarWidth > 0 && option (OPTMBOXPANE)
341       && m_strlen(SidebarDelim) >= SidebarWidth) {
342     mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
343     sleep (2);
344     unset_option (OPTMBOXPANE);
345     return (0);
346   }
347
348   if (SidebarWidth == 0 || !option (OPTMBOXPANE))
349     return 0;
350
351   sidebar_draw_frames();
352
353   if (!Incoming.len)
354     return 0;
355
356   /* actually print items */
357   for (i = TopBuffy, line=first_line; i < Incoming.len && line < last_line; i++) {
358     tmp = Incoming.arr[i];
359
360     if (i == CurBuffy)
361       SETCOLOR (MT_COLOR_INDICATOR);
362     else if (tmp->new > 0)
363       SETCOLOR (MT_COLOR_NEW);
364     else if (tmp->msg_flagged > 0)
365       SETCOLOR (MT_COLOR_FLAGGED);
366     else
367       SETCOLOR (MT_COLOR_NORMAL);
368
369     move (line, 0);
370     line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
371   }
372
373   SETCOLOR (MT_COLOR_NORMAL);
374
375   /* fill with blanks to bottom */
376   memset(&blank, ' ', sizeof(blank));
377   for (; line < last_line; line++) {
378     move (line, 0);
379     addnstr (blank, SidebarWidth-delim_len);
380   }
381   return 0;
382 }
383
384 /* returns index of new item with new mail or -1 */
385 static int exist_next_new () {
386   int i = 0;
387   if (!Incoming.len)
388     return (-1);
389   i = CurBuffy + 1;
390   while (i < Incoming.len)
391     if (Incoming.arr[i++]->new > 0)
392       return (i-1);
393   return (-1);
394 }
395
396 /* returns index of prev item with new mail or -1 */
397 static int exist_prev_new () {
398   int i = 0;
399   if (!Incoming.len)
400     return (-1);
401   i = CurBuffy - 1;
402   while (i >= 0)
403     if (Incoming.arr[i--]->new > 0)
404       return (i+1);
405   return (-1);
406 }
407
408 void sidebar_scroll (int op) {
409   int i = 0;
410
411   if (!SidebarWidth || !Incoming.len)
412     return;
413
414   switch (op) {
415   case OP_SIDEBAR_NEXT:
416     if (!option (OPTSIDEBARNEWMAILONLY)) {
417       if (CurBuffy + 1 == Incoming.len) {
418         mutt_error (_("You are on the last mailbox."));
419         return;
420       }
421       CurBuffy++;
422       break;
423     }                           /* the fall-through is intentional */
424   case OP_SIDEBAR_NEXT_NEW:
425     if ((i = exist_next_new ()) < 0) {
426       mutt_error (_("No next mailboxes with new mail."));
427       return;
428     }
429     else
430       CurBuffy = i;
431     break;
432   case OP_SIDEBAR_PREV:
433     if (!option (OPTSIDEBARNEWMAILONLY)) {
434       if (CurBuffy == 0) {
435         mutt_error (_("You are on the first mailbox."));
436         return;
437       }
438       CurBuffy--;
439       break;
440     }                           /* the fall-through is intentional */
441   case OP_SIDEBAR_PREV_NEW:
442     if ((i = exist_prev_new ()) < 0) {
443       mutt_error (_("No previous mailbox with new mail."));
444       return;
445     }
446     else
447       CurBuffy = i;
448     break;
449
450   case OP_SIDEBAR_SCROLL_UP:
451     if (CurBuffy == 0) {
452       mutt_error (_("You are on the first mailbox."));
453       return;
454     }
455     CurBuffy -= known_lines;
456     if (CurBuffy < 0)
457       CurBuffy = 0;
458     break;
459   case OP_SIDEBAR_SCROLL_DOWN:
460     if (CurBuffy + 1 == Incoming.len) {
461       mutt_error (_("You are on the last mailbox."));
462       return;
463     }
464     CurBuffy += known_lines;
465     if (CurBuffy >= Incoming.len)
466       CurBuffy = Incoming.len - 1;
467     break;
468   default:
469     return;
470   }
471   calc_boundaries ();
472   sidebar_draw ();
473 }