Make the sidebar live in a proper independent window.
[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-ui/lib-ui.h>
16 #include <libgen.h>
17
18 #include <lib-ui/menu.h>
19 #include <lib-ui/sidebar.h>
20
21 #include "mutt.h"
22 #include "charset.h"
23 #include "buffy.h"
24 #include "keymap.h"
25
26 static int TopBuffy = 0;
27 static int CurBuffy = 0;
28 static short initialized = 0;
29 static short prev_show_value;
30
31 /* computes first entry to be shown */
32 static void calc_boundaries(void)
33 {
34     if (!Incoming.len)
35         return;
36     if (CurBuffy < 0 || CurBuffy >= Incoming.len)
37         CurBuffy = 0;
38     if (TopBuffy < 0 || TopBuffy >= Incoming.len)
39         TopBuffy = 0;
40
41     if (option (OPTSIDEBARNEWMAILONLY)) {
42         int i = CurBuffy;
43         TopBuffy = CurBuffy - 1;
44         while (i >= 0) {
45             if (Incoming.arr[i]->new > 0)
46                 TopBuffy = i;
47             i--;
48         }
49     } else {
50         TopBuffy = CurBuffy - (CurBuffy % (LINES - 3));
51     }
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 {
175     return ui_layout_sidebar_w()
176         && !m_strisempty(SidebarNumberFormat);
177 }
178
179 /* print single item
180  * returns:
181  *      0       item was not printed ('cause of $sidebar_newmail_only)
182  *      1       item was printed
183  */
184 static int make_sidebar_entry(WINDOW *sw, char *sbox, int idx, ssize_t len)
185 {
186     int shortened = 0, lencnt = 0;
187     char no[STRING], entry[STRING];
188     int l_m = m_strlen(Maildir);
189
190     if (option(OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path && 
191         m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0)
192         /* if $sidebar_newmail_only is set, don't display the
193          * box only if it's not the currently opened
194          * (i.e. always display the currently opened) */
195         return 0;
196
197     m_strformat(no, sizeof(no), len, SidebarNumberFormat,
198                 sidebar_number_format, idx, 0);
199     lencnt = m_strlen(no);
200
201     if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 && 
202         m_strlen(sbox) > l_m) {
203         sbox += l_m;
204         if (Maildir[strlen(Maildir)-1]!='/') {
205             sbox += 1;
206         }
207     } else {
208         sbox = basename(sbox);
209     }
210
211     if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len - lencnt) {
212         sbox = shortened_hierarchy(sbox, len - lencnt);
213         shortened = 1;
214     }
215
216     snprintf(entry, sizeof(entry), "%*s", (int)len, no);
217     memcpy(entry, sbox, MIN(len, m_strlen(sbox)));
218     waddnstr(sw, entry, len);
219
220     if (shortened)
221         p_delete(&sbox);
222
223     return 1;
224 }
225
226 /* returns folder name of currently 
227  * selected folder for <sidebar-open>
228  */
229 const char* sidebar_get_current (void) {
230   if (!Incoming.len)
231     return (NULL);
232   return Incoming.arr[CurBuffy]->path;
233 }
234
235 /* internally sets item to buf */
236 void sidebar_set_current (const char* buf) {
237   int i = buffy_lookup (buf);
238   if (i >= 0) {
239     CurBuffy = i;
240     calc_boundaries();
241   }
242 }
243
244 /* fix counters for a context
245  * FIXME since ctx must not be of our business, move it elsewhere
246  */
247 void sidebar_set_buffystats (CONTEXT* curContext) {
248   int i = 0;
249   BUFFY* tmp = NULL;
250   if (!curContext || !Incoming.len || (i = buffy_lookup (curContext->path)) < 0)
251     return;
252   tmp = Incoming.arr[i];
253   tmp->new = curContext->new;
254   tmp->msg_unread = curContext->unread;
255   tmp->msgcount = curContext->msgcount;
256   tmp->msg_flagged = curContext->flagged;
257 }
258
259 /* actually draws something
260  * FIXME this needs some clue when to do it
261  */
262 int sidebar_draw(void)
263 {
264     int x, y, i, line;
265     char blank[STRING];
266     WINDOW *sw;
267
268     /* initialize first time */
269     if (!initialized) {
270         prev_show_value = option(OPTMBOXPANE);
271         initialized = 1;
272     }
273
274     if (TopBuffy == 0 || CurBuffy == 0)
275         calc_boundaries();
276
277     /* save or restore the value SidebarWidth */
278     if (prev_show_value != option(OPTMBOXPANE)) {
279         if (!prev_show_value && option(OPTMBOXPANE)) {
280             /* after toggle: force recounting of all mail */
281             buffy_check (2);
282         }
283         prev_show_value = option(OPTMBOXPANE);
284     }
285
286     sw = ui_layout_sidebar_w();
287     if (!sw)
288         return 0;
289     getmaxyx(sw, y, x);
290
291     memset(&blank, ' ', sizeof(blank));
292
293     wmove(sw, 0, 0);
294     WSETCOLOR(sw, MT_COLOR_STATUS);
295     waddnstr(sw, blank, x);
296
297     for (i = TopBuffy, line = 1; i < Incoming.len && line < y; i++) {
298         BUFFY *tmp = Incoming.arr[i];
299
300         if (i == CurBuffy)
301             WSETCOLOR(sw, MT_COLOR_INDICATOR);
302         else if (tmp->new > 0)
303             WSETCOLOR(sw, MT_COLOR_NEW);
304         else if (tmp->msg_flagged > 0)
305             WSETCOLOR(sw, MT_COLOR_FLAGGED);
306         else
307             WSETCOLOR(sw, MT_COLOR_NORMAL);
308
309         if (make_sidebar_entry(sw, tmp->path, i, x - 1)) {
310             WSETCOLOR(sw, MT_COLOR_SIDEBAR);
311             waddch(sw, ACS_VLINE);
312             line++;
313         }
314     }
315
316     while (line < y - 1) {
317         WSETCOLOR(sw, MT_COLOR_NORMAL);
318         waddnstr(sw, blank, x - 1);
319         WSETCOLOR(sw, MT_COLOR_SIDEBAR);
320         waddch(sw, ACS_VLINE);
321         line++;
322     }
323
324     WSETCOLOR(sw, MT_COLOR_STATUS);
325     waddnstr(sw, blank, x);
326     WSETCOLOR(sw, MT_COLOR_NORMAL);
327
328     return 0;
329 }
330
331 /* returns index of new item with new mail or -1 */
332 static int exist_next_new (void) {
333   int i = 0;
334   if (!Incoming.len)
335     return (-1);
336   i = CurBuffy + 1;
337   while (i < Incoming.len)
338     if (Incoming.arr[i++]->new > 0)
339       return (i-1);
340   return (-1);
341 }
342
343 /* returns index of prev item with new mail or -1 */
344 static int exist_prev_new (void) {
345   int i = 0;
346   if (!Incoming.len)
347     return (-1);
348   i = CurBuffy - 1;
349   while (i >= 0)
350     if (Incoming.arr[i--]->new > 0)
351       return (i+1);
352   return (-1);
353 }
354
355 void sidebar_scroll (int op) {
356   int i = 0;
357
358   if (!SidebarWidth || !Incoming.len)
359     return;
360
361   switch (op) {
362   case OP_SIDEBAR_NEXT:
363     if (!option (OPTSIDEBARNEWMAILONLY)) {
364       if (CurBuffy + 1 == Incoming.len) {
365         mutt_error (_("You are on the last mailbox."));
366         return;
367       }
368       CurBuffy++;
369       break;
370     }                           /* the fall-through is intentional */
371   case OP_SIDEBAR_NEXT_NEW:
372     if ((i = exist_next_new ()) < 0) {
373       mutt_error (_("No next mailboxes with new mail."));
374       return;
375     }
376     else
377       CurBuffy = i;
378     break;
379   case OP_SIDEBAR_PREV:
380     if (!option (OPTSIDEBARNEWMAILONLY)) {
381       if (CurBuffy == 0) {
382         mutt_error (_("You are on the first mailbox."));
383         return;
384       }
385       CurBuffy--;
386       break;
387     }                           /* the fall-through is intentional */
388   case OP_SIDEBAR_PREV_NEW:
389     if ((i = exist_prev_new ()) < 0) {
390       mutt_error (_("No previous mailbox with new mail."));
391       return;
392     }
393     else
394       CurBuffy = i;
395     break;
396
397   case OP_SIDEBAR_SCROLL_UP:
398     if (CurBuffy == 0) {
399       mutt_error (_("You are on the first mailbox."));
400       return;
401     }
402     CurBuffy -= LINES - 3;
403     if (CurBuffy < 0)
404       CurBuffy = 0;
405     break;
406   case OP_SIDEBAR_SCROLL_DOWN:
407     if (CurBuffy + 1 == Incoming.len) {
408       mutt_error (_("You are on the last mailbox."));
409       return;
410     }
411     CurBuffy += LINES - 3;
412     if (CurBuffy >= Incoming.len)
413       CurBuffy = Incoming.len - 1;
414     break;
415   default:
416     return;
417   }
418   calc_boundaries ();
419   sidebar_draw ();
420 }