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