fixes in the sidebar format, was completely braindead.
[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                                           unsigned long data, format_flag flags) {
99   char tmp[STRING];
100   BUFFY* b = Incoming.arr[data];
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, NONULL(SidebarNumberFormat), sidebar_number_format,
201               idx, 0);
202   lencnt = m_strlen(no);
203
204   if (l > 0 && m_strncmp(sbox, ImapHomeNamespace, l) == 0 && 
205       m_strlen(sbox) > l)
206     sbox += l + 1; /* we're trimming the ImapHomeNamespace, the "+ 1" is for the separator */
207   else
208   if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 && 
209       m_strlen(sbox) > l_m) {
210     sbox += l_m;
211     if (Maildir[strlen(Maildir)-1]!='/') {
212       sbox += 1;
213     }
214   } else
215     sbox = basename (sbox);
216
217   if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len-lencnt-1) {
218     sbox = shortened_hierarchy (sbox, len-lencnt-1);
219     shortened = 1;
220   }
221
222   snprintf(entry, sizeof(entry), "%*s", (int)len, no);
223   memcpy(entry, sbox, MIN(len - 1, m_strlen(sbox)));
224   addnstr(entry, len);
225
226   if (shortened)
227     p_delete(&sbox);
228
229   return 1;
230 }
231
232 /* returns folder name of currently 
233  * selected folder for <sidebar-open>
234  */
235 const char* sidebar_get_current (void) {
236   if (!Incoming.len)
237     return (NULL);
238   return Incoming.arr[CurBuffy]->path;
239 }
240
241 /* internally sets item to buf */
242 void sidebar_set_current (const char* buf) {
243   int i = buffy_lookup (buf);
244   if (i >= 0) {
245     CurBuffy = i;
246     calc_boundaries();
247   }
248 }
249
250 /* fix counters for a context
251  * FIXME since ctx must not be of our business, move it elsewhere
252  */
253 void sidebar_set_buffystats (CONTEXT* curContext) {
254   int i = 0;
255   BUFFY* tmp = NULL;
256   if (!curContext || !Incoming.len || (i = buffy_lookup (curContext->path)) < 0)
257     return;
258   tmp = Incoming.arr[i];
259   tmp->new = curContext->new;
260   tmp->msg_unread = curContext->unread;
261   tmp->msgcount = curContext->msgcount;
262   tmp->msg_flagged = curContext->flagged;
263 }
264
265 void sidebar_draw_frames (void) {
266   ssize_t i,delim_len;
267
268   if (!option(OPTMBOXPANE) || SidebarWidth==0) 
269     return;
270
271   delim_len=m_strlen(NONULL(SidebarDelim));
272
273   /* draw vertical delimiter */
274   SETCOLOR (MT_COLOR_SIDEBAR);
275   for (i = 0; i < LINES-1; i++) {
276     move (i, SidebarWidth - delim_len);
277     if (option (OPTASCIICHARS))
278       addstr (NONULL (SidebarDelim));
279     else if (!option (OPTASCIICHARS) && !m_strcmp(SidebarDelim, "|"))
280       addch (ACS_VLINE);
281     else if ((Charset_is_utf8) && !m_strcmp(SidebarDelim, "|"))
282       addstr ("\342\224\202");
283     else
284       addstr (NONULL (SidebarDelim));
285   }
286
287   /* fill "gaps" at top+bottom */
288   SETCOLOR(MT_COLOR_STATUS);
289   for (i=0; i<SidebarWidth; i++) {
290     /*
291      * if we don't have $status_on_top and have $help, fill top
292      * gap with spaces to get bg color
293      */
294     if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
295       move(0,i);
296       addch(' ');
297     }
298     /*
299       * if we don't have $status_on_top or we have $help, fill bottom
300       * gap with spaces to get bg color
301       */
302     if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
303       move(LINES-2,i);
304       addch(' ');
305     }
306   }
307   SETCOLOR (MT_COLOR_NORMAL);
308 }
309
310 /* actually draws something
311  * FIXME this needs some clue when to do it
312  */
313 int sidebar_draw (void) {
314   int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
315       last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
316       i = 0,line;
317   BUFFY *tmp;
318   ssize_t delim_len = m_strlen(SidebarDelim);
319   char blank[STRING];
320
321   known_lines=last_line-first_line;
322
323   /* initialize first time */
324   if (!initialized) {
325     prev_show_value = option (OPTMBOXPANE);
326     initialized = 1;
327   }
328
329   if (TopBuffy==0 || CurBuffy==0)
330     calc_boundaries();
331
332   /* save or restore the value SidebarWidth */
333   if (prev_show_value != option (OPTMBOXPANE)) {
334     if (!prev_show_value && option (OPTMBOXPANE)) {
335       /* after toggle: force recounting of all mail */
336       buffy_check (2);
337     }
338     prev_show_value = option (OPTMBOXPANE);
339   }
340
341   if (SidebarWidth > 0 && option (OPTMBOXPANE)
342       && m_strlen(SidebarDelim) >= SidebarWidth) {
343     mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
344     sleep (2);
345     unset_option (OPTMBOXPANE);
346     return (0);
347   }
348
349   if (SidebarWidth == 0 || !option (OPTMBOXPANE))
350     return 0;
351
352   sidebar_draw_frames();
353
354   if (!Incoming.len)
355     return 0;
356
357   /* actually print items */
358   for (i = TopBuffy, line=first_line; i < Incoming.len && line < last_line; i++) {
359     tmp = Incoming.arr[i];
360
361     if (i == CurBuffy)
362       SETCOLOR (MT_COLOR_INDICATOR);
363     else if (tmp->new > 0)
364       SETCOLOR (MT_COLOR_NEW);
365     else if (tmp->msg_flagged > 0)
366       SETCOLOR (MT_COLOR_FLAGGED);
367     else
368       SETCOLOR (MT_COLOR_NORMAL);
369
370     move (line, 0);
371     line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
372   }
373
374   SETCOLOR (MT_COLOR_NORMAL);
375
376   /* fill with blanks to bottom */
377   memset(&blank, ' ', sizeof(blank));
378   for (; line < last_line; line++) {
379     move (line, 0);
380     addnstr (blank, SidebarWidth-delim_len);
381   }
382   return 0;
383 }
384
385 /* returns index of new item with new mail or -1 */
386 static int exist_next_new () {
387   int i = 0;
388   if (!Incoming.len)
389     return (-1);
390   i = CurBuffy + 1;
391   while (i < Incoming.len)
392     if (Incoming.arr[i++]->new > 0)
393       return (i-1);
394   return (-1);
395 }
396
397 /* returns index of prev item with new mail or -1 */
398 static int exist_prev_new () {
399   int i = 0;
400   if (!Incoming.len)
401     return (-1);
402   i = CurBuffy - 1;
403   while (i >= 0)
404     if (Incoming.arr[i--]->new > 0)
405       return (i+1);
406   return (-1);
407 }
408
409 void sidebar_scroll (int op) {
410   int i = 0;
411
412   if (!SidebarWidth || !Incoming.len)
413     return;
414
415   switch (op) {
416   case OP_SIDEBAR_NEXT:
417     if (!option (OPTSIDEBARNEWMAILONLY)) {
418       if (CurBuffy + 1 == Incoming.len) {
419         mutt_error (_("You are on the last mailbox."));
420         return;
421       }
422       CurBuffy++;
423       break;
424     }                           /* the fall-through is intentional */
425   case OP_SIDEBAR_NEXT_NEW:
426     if ((i = exist_next_new ()) < 0) {
427       mutt_error (_("No next mailboxes with new mail."));
428       return;
429     }
430     else
431       CurBuffy = i;
432     break;
433   case OP_SIDEBAR_PREV:
434     if (!option (OPTSIDEBARNEWMAILONLY)) {
435       if (CurBuffy == 0) {
436         mutt_error (_("You are on the first mailbox."));
437         return;
438       }
439       CurBuffy--;
440       break;
441     }                           /* the fall-through is intentional */
442   case OP_SIDEBAR_PREV_NEW:
443     if ((i = exist_prev_new ()) < 0) {
444       mutt_error (_("No previous mailbox with new mail."));
445       return;
446     }
447     else
448       CurBuffy = i;
449     break;
450
451   case OP_SIDEBAR_SCROLL_UP:
452     if (CurBuffy == 0) {
453       mutt_error (_("You are on the first mailbox."));
454       return;
455     }
456     CurBuffy -= known_lines;
457     if (CurBuffy < 0)
458       CurBuffy = 0;
459     break;
460   case OP_SIDEBAR_SCROLL_DOWN:
461     if (CurBuffy + 1 == Incoming.len) {
462       mutt_error (_("You are on the last mailbox."));
463       return;
464     }
465     CurBuffy += known_lines;
466     if (CurBuffy >= Incoming.len)
467       CurBuffy = Incoming.len - 1;
468     break;
469   default:
470     return;
471   }
472   calc_boundaries ();
473   sidebar_draw ();
474 }