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