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