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