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