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