6f35e48cc4672463a27b0610acea7f020fd11bdd
[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 CurBuffy = 0;
27
28 /* computes first entry to be shown */
29 static int calc_boundaries(void)
30 {
31     if (CurBuffy < 0 || CurBuffy >= Incoming.len)
32         CurBuffy = 0;
33
34     if (option(OPTSIDEBARNEWMAILONLY)) {
35         int n = 0;
36
37         for (int i = 0; i < CurBuffy; i++) {
38             n += Incoming.arr[i]->new > 0;
39         }
40
41         n %= (LINES - 3);
42         if (!n)
43             return CurBuffy;
44
45         for (int i = CurBuffy - 1; i >= 0; i--) {
46             if (Incoming.arr[i]->new > 0 && --n <= 0) {
47                 return i;
48             }
49         }
50
51         return 0;
52     } else {
53         return CurBuffy - (CurBuffy % (LINES - 3));
54     }
55 }
56
57 static void shortened_hierarchy(char *dst, char *hbox, int maxlen)
58 {
59     int dots = 0, last_dot = 0, len = m_strlen(hbox);
60
61     if (m_strisempty(SidebarBoundary)) {
62         memcpy(dst, hbox, MIN(len, maxlen));
63         return;
64     }
65
66     for (int i = 0; i < len; ++i) {
67         if (strchr(SidebarBoundary, hbox[i])) {
68             ++dots;
69             last_dot = i;
70         }
71     }
72
73     if (dots == 0) {
74         memcpy(dst, hbox, MIN(len, maxlen));
75         return;
76     }
77
78     dst[0] = hbox[0];
79     for (int i = 1, j = 1; i < len && j < maxlen - 1; ++i) {
80         if (!strchr(SidebarBoundary, hbox[i]))
81             continue;
82
83         if (len - i <= maxlen - j) {
84             memcpy(dst, hbox + i, len - i);
85             return;
86         }
87
88         if (i == last_dot) {
89             memcpy(dst, hbox + i, maxlen - j);
90             return;
91         }
92
93         dst[j++] = hbox[i];
94         dst[j++] = hbox[i + 1];
95     }
96 }
97
98 static const char *
99 sidebar_number_format(char* dest, ssize_t destlen,
100                       char op, const char* src, const char* fmt,
101                       const char* ifstr, const char* elstr,
102                       anytype data, format_flag flags)
103 {
104   char tmp[STRING];
105   BUFFY* b = Incoming.arr[data.i];
106   int opt = flags & M_FORMAT_OPTIONAL;
107   int c = Context && !m_strcmp(Context->path, b->path);
108
109   switch (op) {
110     /* deleted */
111     case 'd':
112       if (!opt) {
113         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
114         snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
115       } else if ((c && Context->deleted == 0) || !c)
116         opt = 0;
117       break;
118     /* flagged */
119     case 'F':
120     case 'f':                   /* for compatibility */
121       if (!opt) {
122         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
123         snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
124       } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
125         opt = 0;
126       break;
127     /* total */
128     case 'c':                   /* for compatibility */
129     case 'm':
130       if (!opt) {
131         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
132         snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
133       } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
134         opt = 0;
135       break;
136     /* total shown, i.e. not hidden by limit */
137     case 'M':
138       if (!opt) {
139         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
140         snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
141       } else if ((c && Context->vcount == 0) || !c)
142         opt = 0;
143       break;
144     /* new */
145     case 'n':
146       if (!opt) {
147         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
148         snprintf (dest, destlen, tmp, c ? Context->new : b->new);
149       } else if ((c && Context->new == 0) || (!c && b->new == 0))
150         opt = 0;
151       break;
152     /* unread */
153     case 'u':
154       if (!opt) {
155         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
156         snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
157       } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
158         opt = 0;
159       break;
160     /* tagged */
161     case 't':
162       if (!opt) {
163         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
164         snprintf (dest, destlen, tmp, c ? Context->tagged : 0);
165       } else if ((c && Context->tagged == 0) || !c)
166         opt = 0;
167       break;
168   }
169
170   if (flags & M_FORMAT_OPTIONAL)
171     m_strformat(dest, destlen, 0, opt ? ifstr : elstr,
172                 sidebar_number_format, data, flags);
173   return src;
174 }
175
176 int sidebar_need_count(void)
177 {
178     return sidebar_w && !m_strisempty(SidebarNumberFormat);
179 }
180
181 /* print single item
182  * returns:
183  *      0       item was not printed ('cause of $sidebar_newmail_only)
184  *      1       item was printed
185  */
186 static int make_sidebar_entry(WINDOW *sw, char *sbox, int idx, ssize_t len)
187 {
188     int lencnt = 0;
189     char no[STRING], entry[STRING];
190     int l_m = m_strlen(Maildir);
191
192     if (option(OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path
193     &&  m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0
194     &&  idx != CurBuffy)
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 && m_strlen(sbox) > l_m)
202     {
203         sbox += l_m;
204         if (Maildir[strlen(Maildir) - 1] != '/') {
205             sbox += 1;
206         }
207     } else {
208         sbox = basename(sbox);
209     }
210
211     snprintf(entry, sizeof(entry), "%*s", (int)len, no);
212     if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len - lencnt - 1) {
213         shortened_hierarchy(entry, sbox, len - lencnt - 1);
214     } else {
215         memcpy(entry, sbox, MIN(len - lencnt - 1, m_strlen(sbox)));
216     }
217     waddnstr(sw, entry, len);
218
219     return 1;
220 }
221
222 /* returns folder name of currently 
223  * selected folder for <sidebar-open>
224  */
225 const char *sidebar_get_current(void)
226 {
227     if (0 <= CurBuffy && CurBuffy < Incoming.len)
228         return Incoming.arr[CurBuffy]->path;
229     return NULL;
230 }
231
232 /* internally sets item to buf */
233 void sidebar_set_current(const char* buf)
234 {
235     int i = buffy_lookup(buf);
236     if (i >= 0) {
237         CurBuffy = i;
238     }
239 }
240
241 /* fix counters for a context
242  * FIXME since ctx must not be of our business, move it elsewhere
243  */
244 void sidebar_set_buffystats(CONTEXT *curContext)
245 {
246     int i = 0;
247     BUFFY *tmp;
248
249     if (!curContext || (i = buffy_lookup(curContext->path)) < 0)
250         return;
251     tmp = Incoming.arr[i];
252     tmp->new         = curContext->new;
253     tmp->msg_unread  = curContext->unread;
254     tmp->msgcount    = curContext->msgcount;
255     tmp->msg_flagged = curContext->flagged;
256 }
257
258 int sidebar_draw(void)
259 {
260     static short prev_show_value = -1;
261     int x, y, line;
262     char blank[STRING];
263     WINDOW *sw;
264
265     /* initialize first time */
266     if (prev_show_value != option(OPTMBOXPANE)) {
267         if ((prev_show_value = option(OPTMBOXPANE))) {
268             /* after toggle: force recounting of all mail */
269             buffy_check(2);
270         }
271     }
272
273     sw = ui_layout_sidebar_w();
274     if (!sw)
275         return 0;
276     getmaxyx(sw, y, x);
277
278     memset(&blank, ' ', sizeof(blank));
279
280     wmove(sw, 0, 0);
281     WSETCOLOR(sw, MT_COLOR_STATUS);
282     waddnstr(sw, blank, x);
283
284     line = 1;
285     for (int i = calc_boundaries(); i < Incoming.len && line < y - 1; i++) {
286         BUFFY *tmp = Incoming.arr[i];
287
288         if (i == CurBuffy)
289             WSETCOLOR(sw, MT_COLOR_INDICATOR);
290         else if (tmp->new > 0)
291             WSETCOLOR(sw, MT_COLOR_NEW);
292         else if (tmp->msg_flagged > 0)
293             WSETCOLOR(sw, MT_COLOR_FLAGGED);
294         else
295             WSETCOLOR(sw, MT_COLOR_NORMAL);
296
297         if (make_sidebar_entry(sw, tmp->path, i, x - 1)) {
298             WSETCOLOR(sw, MT_COLOR_SIDEBAR);
299             waddch(sw, ACS_VLINE);
300             line++;
301         }
302     }
303
304     while (line < y - 1) {
305         WSETCOLOR(sw, MT_COLOR_NORMAL);
306         waddnstr(sw, blank, x - 1);
307         WSETCOLOR(sw, MT_COLOR_SIDEBAR);
308         waddch(sw, ACS_VLINE);
309         line++;
310     }
311
312     WSETCOLOR(sw, MT_COLOR_STATUS);
313     waddnstr(sw, blank, x);
314     WSETCOLOR(sw, MT_COLOR_NORMAL);
315     return 0;
316 }
317
318 void sidebar_scroll(int op)
319 {
320     if (!Incoming.len)
321         return;
322
323     switch (op) {
324       case OP_SIDEBAR_NEXT:
325         if (!option(OPTSIDEBARNEWMAILONLY)) {
326             CurBuffy = (CurBuffy + 1) % Incoming.len;
327             break;
328         }
329         /* FALLTHROUGH */
330       case OP_SIDEBAR_NEXT_NEW:
331         for (int i = 1; i < Incoming.len; i++) {
332             if (Incoming.arr[(CurBuffy + i) % Incoming.len]->new > 0) {
333                 CurBuffy = (CurBuffy + i) % Incoming.len;
334                 break;
335             }
336         }
337         break;
338
339       case OP_SIDEBAR_PREV:
340         if (!option(OPTSIDEBARNEWMAILONLY)) {
341             if (!CurBuffy) {
342                 CurBuffy = Incoming.len;
343             }
344             CurBuffy--;
345             break;
346         }
347         /* FALLTHROUGH */
348       case OP_SIDEBAR_PREV_NEW:
349         for (int i = Incoming.len - 1; i > 0; i--) {
350             if (Incoming.arr[(CurBuffy + i) % Incoming.len]->new > 0) {
351                 CurBuffy = (CurBuffy + i) % Incoming.len;
352                 break;
353             }
354         }
355         break;
356
357       case OP_SIDEBAR_SCROLL_UP:
358         CurBuffy -= LINES - 3;
359         if (CurBuffy < 0)
360             CurBuffy = 0;
361         break;
362       case OP_SIDEBAR_SCROLL_DOWN:
363         CurBuffy += LINES - 3;
364         if (CurBuffy >= Incoming.len)
365             CurBuffy = Incoming.len - 1;
366         break;
367       default:
368         return;
369     }
370
371     sidebar_draw();
372 }