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