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