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