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