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