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