Nico Golde:
[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 int prev_show_value;
34 static short saveSidebarWidth;
35 static char *entry = 0;
36
37 /* computes how many digets a number has;
38  * FIXME move out to library?
39  */
40 static int quick_log10 (int n) {
41   int len = 0;
42
43   for (; n > 9; len++, n /= 10);
44   return (++len);
45 }
46
47 /* computes first entry to be shown */
48 void calc_boundaries (int menu)
49 {
50   int lines = 0;
51
52   if (list_empty(Incoming))
53     return;
54   /* correct known_lines if it has changed because of a window resize */
55   if (known_lines != LINES)
56     known_lines = LINES;
57   lines = LINES - 2 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
58   TopBuffy = CurBuffy - (CurBuffy % lines);
59   if (TopBuffy < 0)
60     TopBuffy = 0;
61 }
62
63 static char *shortened_hierarchy (char *box)
64 {
65   int dots = 0;
66   char *last_dot;
67   int i, j;
68   char *new_box;
69
70   for (i = 0; i < mutt_strlen (box); ++i) {
71     if (box[i] == '.')
72       ++dots;
73     else if (isupper (box[i]))
74       return (safe_strdup (box));
75   }
76   last_dot = strrchr (box, '.');
77   if (last_dot) {
78     ++last_dot;
79     new_box = safe_malloc (mutt_strlen (last_dot) + 2 * dots + 1);
80     new_box[0] = box[0];
81     for (i = 1, j = 1; i < mutt_strlen (box); ++i) {
82       if (box[i] == '.') {
83         new_box[j++] = '.';
84         new_box[j] = 0;
85         if (&box[i + 1] != last_dot) {
86           new_box[j++] = box[i + 1];
87           new_box[j] = 0;
88         }
89         else {
90           strcat (&new_box[j], last_dot);
91           break;
92         }
93       }
94     }
95     return new_box;
96   }
97   return safe_strdup (box);
98 }
99
100 /* print single item
101  * FIXME this is completely fucked up right now
102  */
103 char *make_sidebar_entry (char *box, int size, int new, int flagged)
104 {
105   int i = 0, dlen, max, shortened = 0;
106   int offset;
107
108   if (SidebarWidth > COLS)
109     SidebarWidth = COLS;
110
111   dlen = mutt_strlen (SidebarDelim);
112   max = SidebarWidth - dlen - 1;
113
114   safe_realloc (&entry, SidebarWidth + 1);
115   entry[SidebarWidth] = 0;
116   for (; i < SidebarWidth; entry[i++] = ' ');
117 #if USE_IMAP
118   if (ImapHomeNamespace && mutt_strlen (ImapHomeNamespace) > 0) {
119     if (strncmp (box, ImapHomeNamespace, mutt_strlen (ImapHomeNamespace)) == 0
120         && mutt_strcmp (box, ImapHomeNamespace) != 0) {
121       box += mutt_strlen (ImapHomeNamespace) + 1;
122     }
123   }
124 #endif
125   max -= quick_log10 (size);
126   if (new)
127     max -= quick_log10 (new) + 2;
128   if (flagged > 0)
129     max -= quick_log10 (flagged) + 2;
130   if (option (OPTSHORTENHIERARCHY) && mutt_strlen (box) > max) {
131     box = shortened_hierarchy (box);
132     shortened = 1;
133   }
134   i = mutt_strlen (box);
135   strncpy (entry, box, i < SidebarWidth - dlen ? i : SidebarWidth - dlen);
136
137   if (new) {
138     if (flagged > 0) {
139       offset =
140         SidebarWidth - 5 - quick_log10 (size) - dlen - quick_log10 (new) -
141         quick_log10 (flagged);
142       if (offset < 0)
143         offset = 0;
144       snprintf (entry + offset, SidebarWidth - dlen - offset + 1,
145                 "% d(%d)[%d]", size, new, flagged);
146     }
147     else {
148       offset =
149         SidebarWidth - 3 - quick_log10 (size) - dlen - quick_log10 (new);
150       if (offset < 0)
151         offset = 0;
152       snprintf (entry + offset, SidebarWidth - dlen - offset + 1,
153                 "% d(%d)", size, new);
154     }
155   }
156   else {
157     if (flagged > 0) {
158       offset =
159         SidebarWidth - 3 - quick_log10 (size) - dlen - quick_log10 (flagged);
160       if (offset < 0)
161         offset = 0;
162       snprintf (entry + offset, SidebarWidth - dlen - offset + 1,
163                 "% d[%d]", size, flagged);
164     }
165     else {
166       offset = SidebarWidth - 1 - quick_log10 (size) - dlen;
167       if (offset < 0)
168         offset = 0;
169       snprintf (entry + offset, SidebarWidth - dlen - offset + 1,
170                 "% d", size);
171     }
172   }
173
174   if (option (OPTSHORTENHIERARCHY) && shortened) {
175     free (box);
176   }
177   return entry;
178 }
179
180 /* returns folder name of currently 
181  * selected folder for <sidebar-open>
182  */
183 const char* sidebar_get_current (void) {
184   if (list_empty(Incoming))
185     return (NULL);
186   return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
187 }
188
189 /* internally sets item to buf */
190 void sidebar_set_current (const char* buf) {
191   int i = buffy_lookup (buf);
192   if (i >= 0)
193     CurBuffy = i;
194 }
195
196 /* fix counters for a context
197  * FIXME since ctx must not be of our business, move it elsewhere
198  */
199 void sidebar_set_buffystats (CONTEXT* Context) {
200   int i = 0;
201   BUFFY* tmp = NULL;
202   if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
203     return;
204   tmp = (BUFFY*) Incoming->data[i];
205   tmp->new = Context->new;
206   tmp->msg_unread = Context->unread;
207   tmp->msgcount = Context->msgcount;
208   tmp->msg_flagged = Context->flagged;
209 }
210
211 /* actually draws something
212  * FIXME this needs some clue when to do it
213  * FIXME this is completely fucked up right now
214  */
215 int sidebar_draw (int menu)
216 {
217
218   int lines = option (OPTHELP) ? 1 : 0;
219   BUFFY *tmp;
220   int i = 0;
221   short delim_len = mutt_strlen (SidebarDelim);
222
223   /* initialize first time */
224   if (!initialized) {
225     prev_show_value = option (OPTMBOXPANE);
226     saveSidebarWidth = SidebarWidth;
227     if (!option (OPTMBOXPANE))
228       SidebarWidth = 0;
229     initialized = 1;
230   }
231
232   /* save or restore the value SidebarWidth */
233   if (prev_show_value != option (OPTMBOXPANE)) {
234     if (prev_show_value && !option (OPTMBOXPANE)) {
235       saveSidebarWidth = SidebarWidth;
236       SidebarWidth = 0;
237     }
238     else if (!prev_show_value && option (OPTMBOXPANE)) {
239       SidebarWidth = saveSidebarWidth;
240       /* after toggle: force recounting of all mail */
241       mutt_buffy_check (2);
242     }
243     prev_show_value = option (OPTMBOXPANE);
244   }
245
246   if (SidebarWidth > 0 && option (OPTMBOXPANE)
247       && mutt_strlen (SidebarDelim) >= SidebarWidth) {
248     mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
249     sleep (2);
250     unset_option (OPTMBOXPANE);
251     return (0);
252   }
253
254   if (SidebarWidth == 0 || !option (OPTMBOXPANE))
255     return 0;
256
257   /* draw the divider */
258   SETCOLOR (MT_COLOR_SIDEBAR);
259   for (lines = 1;
260        lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
261        lines++) {
262     move (lines, SidebarWidth - delim_len);
263     if (option (OPTASCIICHARS))
264       addstr (NONULL (SidebarDelim));
265     else if (!option (OPTASCIICHARS) && !mutt_strcmp (SidebarDelim, "|"))
266       addch (ACS_VLINE);
267     else if ((Charset_is_utf8) && !mutt_strcmp (SidebarDelim, "|"))
268       addstr ("\342\224\202");
269     else
270       addstr (NONULL (SidebarDelim));
271   }
272   SETCOLOR (MT_COLOR_NORMAL);
273
274   if (list_empty(Incoming))
275     return 0;
276
277   lines = option (OPTHELP) ? 1 : 0;     /* go back to the top */
278
279   calc_boundaries (menu);
280
281   for (i = TopBuffy; i < Incoming->length && lines < LINES - 1 - 
282        (menu != MENU_PAGER || option (OPTSTATUSONTOP)); i++) {
283     tmp = (BUFFY*) Incoming->data[i];
284     if (i == CurBuffy)
285       SETCOLOR (MT_COLOR_INDICATOR);
286     else if (tmp->msg_flagged > 0)
287       SETCOLOR (MT_COLOR_FLAGGED);
288     else if (tmp->msg_unread > 0)
289       SETCOLOR (MT_COLOR_NEW);
290     else
291       SETCOLOR (MT_COLOR_NORMAL);
292
293     move (lines, 0);
294     if (option (OPTSIDEBARNEWMAILONLY)) {
295       if (tmp->msg_unread > 0) {
296         if (Context && !mutt_strcmp (tmp->path, Context->path)) {
297           printw ("%.*s", SidebarWidth - delim_len,
298                   make_sidebar_entry (basename (tmp->path),
299                                       Context->msgcount, Context->unread,
300                                       Context->flagged));
301           tmp->msg_unread = Context->unread;
302           tmp->msgcount = Context->msgcount;
303           tmp->msg_flagged = Context->flagged;
304         }
305         else
306           printw ("%.*s", SidebarWidth - delim_len,
307                   make_sidebar_entry (basename (tmp->path),
308                                       tmp->msgcount, tmp->msg_unread,
309                                       tmp->msg_flagged));
310         lines++;
311       }
312     }
313     else {
314       if (Context && !mutt_strcmp (tmp->path, Context->path)) {
315         printw ("%.*s", SidebarWidth - delim_len,
316                 make_sidebar_entry (basename (tmp->path),
317                                     Context->msgcount, Context->unread,
318                                     Context->flagged));
319         tmp->msg_unread = Context->unread;
320         tmp->msgcount = Context->msgcount;
321         tmp->msg_flagged = Context->flagged;
322       }
323       else
324         printw ("%.*s", SidebarWidth - delim_len,
325                 make_sidebar_entry (basename (tmp->path),
326                                     tmp->msgcount, tmp->msg_unread,
327                                     tmp->msg_flagged));
328       lines++;
329     }
330   }
331   SETCOLOR (MT_COLOR_NORMAL);
332   for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
333        lines++) {
334     int i = 0;
335
336     move (lines, 0);
337     for (; i < SidebarWidth - delim_len; i++)
338       addch (' ');
339   }
340   return 0;
341 }
342
343 /* returns index of new item with new mail or -1 */
344 static int exist_next_new () {
345   int i = 0;
346   if (list_empty(Incoming))
347     return (-1);
348   i = CurBuffy + 1;
349   while (i < Incoming->length)
350     if (((BUFFY*) Incoming->data[i++])->msg_unread)
351       return (i-1);
352   return (-1);
353 }
354
355 /* returns index of prev item with new mail or -1 */
356 static int exist_prev_new () {
357   int i = 0;
358   if (list_empty(Incoming))
359     return (-1);
360   i = CurBuffy - 1;
361   while (i >= 0)
362     if (((BUFFY*) Incoming->data[i--])->msg_unread)
363       return (i+1);
364   return (-1);
365 }
366
367 void sidebar_scroll (int op, int menu) {
368   int i = 0;
369
370   if (!SidebarWidth || list_empty(Incoming))
371     return;
372
373   switch (op) {
374   case OP_SIDEBAR_NEXT:
375     if (!option (OPTSIDEBARNEWMAILONLY)) {
376       if (CurBuffy + 1 == Incoming->length) {
377         mutt_error (_("You are on the last mailbox."));
378         return;
379       }
380       CurBuffy++;
381       break;
382     }                           /* the fall-through is intentional */
383   case OP_SIDEBAR_NEXT_NEW:
384     if ((i = exist_next_new ()) < 0) {
385       mutt_error (_("No next mailboxes with new mail."));
386       return;
387     }
388     else
389       CurBuffy = i;
390     break;
391   case OP_SIDEBAR_PREV:
392     if (!option (OPTSIDEBARNEWMAILONLY)) {
393       if (CurBuffy == 0) {
394         mutt_error (_("You are on the first mailbox."));
395         return;
396       }
397       CurBuffy--;
398       break;
399     }                           /* the fall-through is intentional */
400   case OP_SIDEBAR_PREV_NEW:
401     if ((i = exist_prev_new ()) < 0) {
402       mutt_error (_("No previous mailbox with new mail."));
403       return;
404     }
405     else
406       CurBuffy = i;
407     break;
408
409   case OP_SIDEBAR_SCROLL_UP:
410     if (TopBuffy == 0) {
411       mutt_error (_("You are on the first mailbox."));
412       return;
413     }
414     CurBuffy -= known_lines;
415     if (CurBuffy < 0)
416       CurBuffy = 0;
417     break;
418   case OP_SIDEBAR_SCROLL_DOWN:
419     if (TopBuffy + known_lines >= Incoming->length) {
420       mutt_error (_("You are on the last mailbox."));
421       return;
422     }
423     CurBuffy += known_lines;
424     if (CurBuffy >= Incoming->length)
425       CurBuffy = Incoming->length;
426     break;
427   default:
428     return;
429   }
430   calc_boundaries (menu);
431   sidebar_draw (menu);
432 }