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