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 < safe_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 (safe_strlen (last_dot) + 2 * dots + 1);
80     new_box[0] = box[0];
81     for (i = 1, j = 1; i < safe_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 = safe_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 && safe_strlen (ImapHomeNamespace) > 0) {
119     if (strncmp (box, ImapHomeNamespace, safe_strlen (ImapHomeNamespace)) == 0
120         && safe_strcmp (box, ImapHomeNamespace) != 0) {
121       box += safe_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) && safe_strlen (box) > max) {
131     box = shortened_hierarchy (box);
132     shortened = 1;
133   }
134   i = safe_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   int draw_devider=1;
220   BUFFY *tmp;
221   int i = 0;
222   short delim_len = safe_strlen (SidebarDelim);
223
224   /* initialize first time */
225   if (!initialized) {
226     prev_show_value = option (OPTMBOXPANE);
227     saveSidebarWidth = SidebarWidth;
228     if (!option (OPTMBOXPANE)){
229       SidebarWidth = 0;
230       draw_devider = 1;
231     }
232     initialized = 1;
233   }
234
235   /* save or restore the value SidebarWidth */
236   if (prev_show_value != option (OPTMBOXPANE)) {
237     if (prev_show_value && !option (OPTMBOXPANE)) {
238       saveSidebarWidth = SidebarWidth;
239       SidebarWidth = 0;
240     }
241     else if (!prev_show_value && option (OPTMBOXPANE)) {
242       SidebarWidth = saveSidebarWidth;
243       /* after toggle: force recounting of all mail */
244       mutt_buffy_check (2);
245     }
246     prev_show_value = option (OPTMBOXPANE);
247   }
248
249   if (SidebarWidth > 0 && option (OPTMBOXPANE)
250       && safe_strlen (SidebarDelim) >= SidebarWidth) {
251     mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
252     sleep (2);
253     unset_option (OPTMBOXPANE);
254     return (0);
255   }
256
257   if (SidebarWidth == 0 || !option (OPTMBOXPANE))
258     return 0;
259   /* draw devider only if necessary (if the sidebar becomes visible e.g.)*/
260   if (draw_devider == 1){
261     /* draw the divider */
262     SETCOLOR (MT_COLOR_SIDEBAR);
263     for (lines = 1;
264          lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
265          lines++) {
266       move (lines, SidebarWidth - delim_len);
267       if (option (OPTASCIICHARS))
268         addstr (NONULL (SidebarDelim));
269       else if (!option (OPTASCIICHARS) && !safe_strcmp (SidebarDelim, "|"))
270         addch (ACS_VLINE);
271       else if ((Charset_is_utf8) && !safe_strcmp (SidebarDelim, "|"))
272         addstr ("\342\224\202");
273       else
274         addstr (NONULL (SidebarDelim));
275     }
276   }
277   SETCOLOR (MT_COLOR_NORMAL);
278
279   if (list_empty(Incoming))
280     return 0;
281
282   lines = option (OPTHELP) ? 1 : 0;     /* go back to the top */
283
284   calc_boundaries (menu);
285
286   for (i = TopBuffy; i < Incoming->length && lines < LINES - 1 - 
287        (menu != MENU_PAGER || option (OPTSTATUSONTOP)); i++) {
288     tmp = (BUFFY*) Incoming->data[i];
289     if (i == CurBuffy)
290       SETCOLOR (MT_COLOR_INDICATOR);
291     else if (tmp->msg_flagged > 0)
292       SETCOLOR (MT_COLOR_FLAGGED);
293     else if (tmp->msg_unread > 0)
294       SETCOLOR (MT_COLOR_NEW);
295     else
296       SETCOLOR (MT_COLOR_NORMAL);
297
298     move (lines, 0);
299     if (option (OPTSIDEBARNEWMAILONLY)) {
300       if (tmp->msg_unread > 0) {
301         if (Context && !safe_strcmp (tmp->path, Context->path)) {
302           printw ("%.*s", SidebarWidth - delim_len,
303                   make_sidebar_entry (basename (tmp->path),
304                                       Context->msgcount, Context->unread,
305                                       Context->flagged));
306           tmp->msg_unread = Context->unread;
307           tmp->msgcount = Context->msgcount;
308           tmp->msg_flagged = Context->flagged;
309         }
310         else
311           printw ("%.*s", SidebarWidth - delim_len,
312                   make_sidebar_entry (basename (tmp->path),
313                                       tmp->msgcount, tmp->msg_unread,
314                                       tmp->msg_flagged));
315         lines++;
316       }
317     }
318     else {
319       if (Context && !safe_strcmp (tmp->path, Context->path)) {
320         printw ("%.*s", SidebarWidth - delim_len,
321                 make_sidebar_entry (basename (tmp->path),
322                                     Context->msgcount, Context->unread,
323                                     Context->flagged));
324         tmp->msg_unread = Context->unread;
325         tmp->msgcount = Context->msgcount;
326         tmp->msg_flagged = Context->flagged;
327       }
328       else
329         printw ("%.*s", SidebarWidth - delim_len,
330                 make_sidebar_entry (basename (tmp->path),
331                                     tmp->msgcount, tmp->msg_unread,
332                                     tmp->msg_flagged));
333       lines++;
334     }
335   }
336   SETCOLOR (MT_COLOR_NORMAL);
337   for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
338        lines++) {
339     int i = 0;
340
341     move (lines, 0);
342     for (; i < SidebarWidth - delim_len; i++)
343       addch (' ');
344   }
345   return 0;
346 }
347
348 /* returns index of new item with new mail or -1 */
349 static int exist_next_new () {
350   int i = 0;
351   if (list_empty(Incoming))
352     return (-1);
353   i = CurBuffy + 1;
354   while (i < Incoming->length)
355     if (((BUFFY*) Incoming->data[i++])->msg_unread)
356       return (i-1);
357   return (-1);
358 }
359
360 /* returns index of prev item with new mail or -1 */
361 static int exist_prev_new () {
362   int i = 0;
363   if (list_empty(Incoming))
364     return (-1);
365   i = CurBuffy - 1;
366   while (i >= 0)
367     if (((BUFFY*) Incoming->data[i--])->msg_unread)
368       return (i+1);
369   return (-1);
370 }
371
372 void sidebar_scroll (int op, int menu) {
373   int i = 0;
374
375   if (!SidebarWidth || list_empty(Incoming))
376     return;
377
378   switch (op) {
379   case OP_SIDEBAR_NEXT:
380     if (!option (OPTSIDEBARNEWMAILONLY)) {
381       if (CurBuffy + 1 == Incoming->length) {
382         mutt_error (_("You are on the last mailbox."));
383         return;
384       }
385       CurBuffy++;
386       break;
387     }                           /* the fall-through is intentional */
388   case OP_SIDEBAR_NEXT_NEW:
389     if ((i = exist_next_new ()) < 0) {
390       mutt_error (_("No next mailboxes with new mail."));
391       return;
392     }
393     else
394       CurBuffy = i;
395     break;
396   case OP_SIDEBAR_PREV:
397     if (!option (OPTSIDEBARNEWMAILONLY)) {
398       if (CurBuffy == 0) {
399         mutt_error (_("You are on the first mailbox."));
400         return;
401       }
402       CurBuffy--;
403       break;
404     }                           /* the fall-through is intentional */
405   case OP_SIDEBAR_PREV_NEW:
406     if ((i = exist_prev_new ()) < 0) {
407       mutt_error (_("No previous mailbox with new mail."));
408       return;
409     }
410     else
411       CurBuffy = i;
412     break;
413
414   case OP_SIDEBAR_SCROLL_UP:
415     if (TopBuffy == 0) {
416       mutt_error (_("You are on the first mailbox."));
417       return;
418     }
419     CurBuffy -= known_lines;
420     if (CurBuffy < 0)
421       CurBuffy = 0;
422     break;
423   case OP_SIDEBAR_SCROLL_DOWN:
424     if (TopBuffy + known_lines >= Incoming->length) {
425       mutt_error (_("You are on the last mailbox."));
426       return;
427     }
428     CurBuffy += known_lines;
429     if (CurBuffy >= Incoming->length)
430       CurBuffy = Incoming->length;
431     break;
432   default:
433     return;
434   }
435   calc_boundaries (menu);
436   sidebar_draw (menu);
437 }