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