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