d5434a94793268557b9251071d604563c8a624af
[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 *
96 sidebar_number_format(char* dest, ssize_t destlen,
97                       char op, const char* src, const char* fmt,
98                       const char* ifstr, const char* elstr,
99                       anytype data, format_flag flags)
100 {
101   char tmp[STRING];
102   BUFFY* b = Incoming.arr[data.i];
103   int opt = flags & M_FORMAT_OPTIONAL;
104   int c = Context && !m_strcmp(Context->path, b->path);
105
106   switch (op) {
107     /* deleted */
108     case 'd':
109       if (!opt) {
110         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
111         snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
112       } else if ((c && Context->deleted == 0) || !c)
113         opt = 0;
114       break;
115     /* flagged */
116     case 'F':
117     case 'f':                   /* for compatibility */
118       if (!opt) {
119         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
120         snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
121       } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
122         opt = 0;
123       break;
124     /* total */
125     case 'c':                   /* for compatibility */
126     case 'm':
127       if (!opt) {
128         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
129         snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
130       } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
131         opt = 0;
132       break;
133     /* total shown, i.e. not hidden by limit */
134     case 'M':
135       if (!opt) {
136         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
137         snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
138       } else if ((c && Context->vcount == 0) || !c)
139         opt = 0;
140       break;
141     /* new */
142     case 'n':
143       if (!opt) {
144         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
145         snprintf (dest, destlen, tmp, c ? Context->new : b->new);
146       } else if ((c && Context->new == 0) || (!c && b->new == 0))
147         opt = 0;
148       break;
149     /* unread */
150     case 'u':
151       if (!opt) {
152         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
153         snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
154       } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
155         opt = 0;
156       break;
157     /* tagged */
158     case 't':
159       if (!opt) {
160         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
161         snprintf (dest, destlen, tmp, c ? Context->tagged : 0);
162       } else if ((c && Context->tagged == 0) || !c)
163         opt = 0;
164       break;
165   }
166
167   if (flags & M_FORMAT_OPTIONAL)
168     m_strformat(dest, destlen, 0, opt ? ifstr : elstr,
169                 sidebar_number_format, data, flags);
170   return src;
171 }
172
173 int sidebar_need_count (void) {
174   if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
175       !SidebarNumberFormat || !*SidebarNumberFormat)
176     return (0);
177   return (1);
178 }
179
180 /* print single item
181  * returns:
182  *      0       item was not printed ('cause of $sidebar_newmail_only)
183  *      1       item was printed
184  */
185 static int make_sidebar_entry (char* sbox, int idx, ssize_t len)
186 {
187   int shortened = 0, lencnt = 0;
188   char no[STRING], entry[STRING];
189   int l_m = m_strlen(Maildir);
190
191   if (SidebarWidth > COLS)
192     SidebarWidth = COLS;
193
194   if (option (OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path && 
195       m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0)
196     /* if $sidebar_newmail_only is set, don't display the
197      * box only if it's not the currently opened
198      * (i.e. always display the currently opened) */
199     return 0;
200
201   m_strformat(no, len, SidebarWidth, SidebarNumberFormat, sidebar_number_format, idx, 0);
202   lencnt = m_strlen(no);
203
204   if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 && 
205       m_strlen(sbox) > l_m) {
206     sbox += l_m;
207     if (Maildir[strlen(Maildir)-1]!='/') {
208       sbox += 1;
209     }
210   } else
211     sbox = basename (sbox);
212
213   if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len-lencnt-1) {
214     sbox = shortened_hierarchy (sbox, len-lencnt-1);
215     shortened = 1;
216   }
217
218   snprintf(entry, sizeof(entry), "%*s", (int)len, no);
219   memcpy(entry, sbox, MIN(len - 1, m_strlen(sbox)));
220   addnstr(entry, len);
221
222   if (shortened)
223     p_delete(&sbox);
224
225   return 1;
226 }
227
228 /* returns folder name of currently 
229  * selected folder for <sidebar-open>
230  */
231 const char* sidebar_get_current (void) {
232   if (!Incoming.len)
233     return (NULL);
234   return Incoming.arr[CurBuffy]->path;
235 }
236
237 /* internally sets item to buf */
238 void sidebar_set_current (const char* buf) {
239   int i = buffy_lookup (buf);
240   if (i >= 0) {
241     CurBuffy = i;
242     calc_boundaries();
243   }
244 }
245
246 /* fix counters for a context
247  * FIXME since ctx must not be of our business, move it elsewhere
248  */
249 void sidebar_set_buffystats (CONTEXT* curContext) {
250   int i = 0;
251   BUFFY* tmp = NULL;
252   if (!curContext || !Incoming.len || (i = buffy_lookup (curContext->path)) < 0)
253     return;
254   tmp = Incoming.arr[i];
255   tmp->new = curContext->new;
256   tmp->msg_unread = curContext->unread;
257   tmp->msgcount = curContext->msgcount;
258   tmp->msg_flagged = curContext->flagged;
259 }
260
261 void sidebar_draw_frames (void) {
262   ssize_t i,delim_len;
263
264   if (!option(OPTMBOXPANE) || SidebarWidth==0) 
265     return;
266
267   delim_len=m_strlen(NONULL(SidebarDelim));
268
269   /* draw vertical delimiter */
270   SETCOLOR (MT_COLOR_SIDEBAR);
271   for (i = 0; i < LINES-1; i++) {
272     move (i, SidebarWidth - delim_len);
273     if (!m_strcmp(SidebarDelim, "|"))
274       addch (ACS_VLINE);
275     else
276       addstr (NONULL (SidebarDelim));
277   }
278
279   /* fill "gaps" at top+bottom */
280   SETCOLOR(MT_COLOR_STATUS);
281   for (i=0; i<SidebarWidth; i++) {
282     /*
283      * if we don't have $status_on_top and have $help, fill top
284      * gap with spaces to get bg color
285      */
286     if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
287       move(0,i);
288       addch(' ');
289     }
290     /*
291       * if we don't have $status_on_top or we have $help, fill bottom
292       * gap with spaces to get bg color
293       */
294     if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
295       move(LINES-2,i);
296       addch(' ');
297     }
298   }
299   SETCOLOR (MT_COLOR_NORMAL);
300 }
301
302 /* actually draws something
303  * FIXME this needs some clue when to do it
304  */
305 int sidebar_draw (void) {
306   int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
307       last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
308       i = 0,line;
309   BUFFY *tmp;
310   ssize_t delim_len = m_strlen(SidebarDelim);
311   char blank[STRING];
312
313   known_lines=last_line-first_line;
314
315   /* initialize first time */
316   if (!initialized) {
317     prev_show_value = option (OPTMBOXPANE);
318     initialized = 1;
319   }
320
321   if (TopBuffy==0 || CurBuffy==0)
322     calc_boundaries();
323
324   /* save or restore the value SidebarWidth */
325   if (prev_show_value != option (OPTMBOXPANE)) {
326     if (!prev_show_value && option (OPTMBOXPANE)) {
327       /* after toggle: force recounting of all mail */
328       buffy_check (2);
329     }
330     prev_show_value = option (OPTMBOXPANE);
331   }
332
333   if (SidebarWidth > 0 && option (OPTMBOXPANE)
334       && m_strlen(SidebarDelim) >= SidebarWidth) {
335     mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
336     sleep (2);
337     unset_option (OPTMBOXPANE);
338     return (0);
339   }
340
341   if (SidebarWidth == 0 || !option (OPTMBOXPANE))
342     return 0;
343
344   sidebar_draw_frames();
345
346   if (!Incoming.len)
347     return 0;
348
349   /* actually print items */
350   for (i = TopBuffy, line=first_line; i < Incoming.len && line < last_line; i++) {
351     tmp = Incoming.arr[i];
352
353     if (i == CurBuffy)
354       SETCOLOR (MT_COLOR_INDICATOR);
355     else if (tmp->new > 0)
356       SETCOLOR (MT_COLOR_NEW);
357     else if (tmp->msg_flagged > 0)
358       SETCOLOR (MT_COLOR_FLAGGED);
359     else
360       SETCOLOR (MT_COLOR_NORMAL);
361
362     move (line, 0);
363     line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
364   }
365
366   SETCOLOR (MT_COLOR_NORMAL);
367
368   /* fill with blanks to bottom */
369   memset(&blank, ' ', sizeof(blank));
370   for (; line < last_line; line++) {
371     move (line, 0);
372     addnstr (blank, SidebarWidth-delim_len);
373   }
374   return 0;
375 }
376
377 /* returns index of new item with new mail or -1 */
378 static int exist_next_new () {
379   int i = 0;
380   if (!Incoming.len)
381     return (-1);
382   i = CurBuffy + 1;
383   while (i < Incoming.len)
384     if (Incoming.arr[i++]->new > 0)
385       return (i-1);
386   return (-1);
387 }
388
389 /* returns index of prev item with new mail or -1 */
390 static int exist_prev_new () {
391   int i = 0;
392   if (!Incoming.len)
393     return (-1);
394   i = CurBuffy - 1;
395   while (i >= 0)
396     if (Incoming.arr[i--]->new > 0)
397       return (i+1);
398   return (-1);
399 }
400
401 void sidebar_scroll (int op) {
402   int i = 0;
403
404   if (!SidebarWidth || !Incoming.len)
405     return;
406
407   switch (op) {
408   case OP_SIDEBAR_NEXT:
409     if (!option (OPTSIDEBARNEWMAILONLY)) {
410       if (CurBuffy + 1 == Incoming.len) {
411         mutt_error (_("You are on the last mailbox."));
412         return;
413       }
414       CurBuffy++;
415       break;
416     }                           /* the fall-through is intentional */
417   case OP_SIDEBAR_NEXT_NEW:
418     if ((i = exist_next_new ()) < 0) {
419       mutt_error (_("No next mailboxes with new mail."));
420       return;
421     }
422     else
423       CurBuffy = i;
424     break;
425   case OP_SIDEBAR_PREV:
426     if (!option (OPTSIDEBARNEWMAILONLY)) {
427       if (CurBuffy == 0) {
428         mutt_error (_("You are on the first mailbox."));
429         return;
430       }
431       CurBuffy--;
432       break;
433     }                           /* the fall-through is intentional */
434   case OP_SIDEBAR_PREV_NEW:
435     if ((i = exist_prev_new ()) < 0) {
436       mutt_error (_("No previous mailbox with new mail."));
437       return;
438     }
439     else
440       CurBuffy = i;
441     break;
442
443   case OP_SIDEBAR_SCROLL_UP:
444     if (CurBuffy == 0) {
445       mutt_error (_("You are on the first mailbox."));
446       return;
447     }
448     CurBuffy -= known_lines;
449     if (CurBuffy < 0)
450       CurBuffy = 0;
451     break;
452   case OP_SIDEBAR_SCROLL_DOWN:
453     if (CurBuffy + 1 == Incoming.len) {
454       mutt_error (_("You are on the last mailbox."));
455       return;
456     }
457     CurBuffy += known_lines;
458     if (CurBuffy >= Incoming.len)
459       CurBuffy = Incoming.len - 1;
460     break;
461   default:
462     return;
463   }
464   calc_boundaries ();
465   sidebar_draw ();
466 }