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 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 = safe_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 + safe_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 && safe_strcmp (Context->path, b->path) == 0;
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 = safe_strlen (ImapHomeNamespace);
159 #endif
160
161   if (SidebarWidth > COLS)
162     SidebarWidth = COLS;
163
164   if (option (OPTSIDEBARNEWMAILONLY) && box && Context && Context->path && 
165       safe_strcmp (Context->path, box) != 0 && 
166       ((BUFFY*) Incoming->data[idx])->new == 0)
167     /* if $sidebar_newmail_only is set, don't display the
168      * box only if it's not the currently opened
169      * (i.e. always display the currently opened) */
170     return (0);
171
172   mutt_FormatString (no, len, NONULL (SidebarNumberFormat),
173                      sidebar_number_format, idx, M_FORMAT_OPTIONAL);
174   lencnt = safe_strlen (no);
175   memset (&entry, ' ', sizeof (entry));
176
177 #if USE_IMAP
178   if (l > 0 && safe_strncmp (box, ImapHomeNamespace, l) == 0 && 
179       safe_strlen (box) > l)
180     box += l + 1;
181   else
182 #endif
183     box = basename (box);
184
185   if (option (OPTSHORTENHIERARCHY) && safe_strlen (box) > len-lencnt-1) {
186     box = shortened_hierarchy (box, len-lencnt-1);
187     shortened = 1;
188   }
189
190   snprintf (entry, len-lencnt, "%s", box);
191   entry[safe_strlen (entry)] = ' ';
192   strncpy (entry + (len - lencnt), no, lencnt);
193
194   addnstr (entry, len);
195
196   if (shortened)
197     FREE(&box);
198
199   return (1);
200 }
201
202 /* returns folder name of currently 
203  * selected folder for <sidebar-open>
204  */
205 const char* sidebar_get_current (void) {
206   if (list_empty(Incoming))
207     return (NULL);
208   return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
209 }
210
211 /* internally sets item to buf */
212 void sidebar_set_current (const char* buf) {
213   int i = buffy_lookup (buf);
214   if (i >= 0)
215     CurBuffy = i;
216 }
217
218 /* fix counters for a context
219  * FIXME since ctx must not be of our business, move it elsewhere
220  */
221 void sidebar_set_buffystats (CONTEXT* Context) {
222   int i = 0;
223   BUFFY* tmp = NULL;
224   if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
225     return;
226   tmp = (BUFFY*) Incoming->data[i];
227   tmp->new = Context->new;
228   tmp->msg_unread = Context->unread;
229   tmp->msgcount = Context->msgcount;
230   tmp->msg_flagged = Context->flagged;
231 }
232
233 /* actually draws something
234  * FIXME this needs some clue when to do it
235  */
236 int sidebar_draw (int menu)
237 {
238
239   int lines = option (OPTHELP) ? 1 : 0, draw_devider = 1, i = 0;
240   BUFFY *tmp;
241   short delim_len = safe_strlen (SidebarDelim);
242   char blank[SHORT_STRING];
243
244   /* initialize first time */
245   if (!initialized) {
246     prev_show_value = option (OPTMBOXPANE);
247     saveSidebarWidth = SidebarWidth;
248     if (!option (OPTMBOXPANE)){
249       SidebarWidth = 0;
250       draw_devider = 1;
251     }
252     initialized = 1;
253   }
254
255   /* save or restore the value SidebarWidth */
256   if (prev_show_value != option (OPTMBOXPANE)) {
257     if (prev_show_value && !option (OPTMBOXPANE)) {
258       saveSidebarWidth = SidebarWidth;
259       SidebarWidth = 0;
260     }
261     else if (!prev_show_value && option (OPTMBOXPANE)) {
262       SidebarWidth = saveSidebarWidth;
263       /* after toggle: force recounting of all mail */
264       buffy_check (2);
265     }
266     prev_show_value = option (OPTMBOXPANE);
267   }
268
269   if (SidebarWidth > 0 && option (OPTMBOXPANE)
270       && safe_strlen (SidebarDelim) >= SidebarWidth) {
271     mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
272     sleep (2);
273     unset_option (OPTMBOXPANE);
274     return (0);
275   }
276
277   if (SidebarWidth == 0 || !option (OPTMBOXPANE))
278     return 0;
279   /* draw devider only if necessary (if the sidebar becomes visible e.g.)*/
280   if (draw_devider == 1){
281     /* draw the divider */
282     SETCOLOR (MT_COLOR_SIDEBAR);
283     for (lines = 1;
284          lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
285          lines++) {
286       move (lines, SidebarWidth - delim_len);
287       if (option (OPTASCIICHARS))
288         addstr (NONULL (SidebarDelim));
289       else if (!option (OPTASCIICHARS) && !safe_strcmp (SidebarDelim, "|"))
290         addch (ACS_VLINE);
291       else if ((Charset_is_utf8) && !safe_strcmp (SidebarDelim, "|"))
292         addstr ("\342\224\202");
293       else
294         addstr (NONULL (SidebarDelim));
295     }
296   }
297   SETCOLOR (MT_COLOR_NORMAL);
298
299   if (list_empty(Incoming))
300     return 0;
301   lines = option (OPTHELP) ? 1 : 0;     /* go back to the top */
302   calc_boundaries (menu);
303
304   /* actually print items */
305   for (i = TopBuffy; i < Incoming->length && lines < LINES - 1 - 
306        (menu != MENU_PAGER || option (OPTSTATUSONTOP)); i++) {
307     tmp = (BUFFY*) Incoming->data[i];
308
309     if (i == CurBuffy)
310       SETCOLOR (MT_COLOR_INDICATOR);
311     else if (tmp->msg_flagged > 0)
312       SETCOLOR (MT_COLOR_FLAGGED);
313     else if (tmp->new > 0)
314       SETCOLOR (MT_COLOR_NEW);
315     else
316       SETCOLOR (MT_COLOR_NORMAL);
317
318     move (lines, 0);
319     lines += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
320   }
321
322   /* fill with blanks to bottom */
323   memset (&blank, ' ', sizeof (blank));
324   SETCOLOR (MT_COLOR_NORMAL);
325   for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++) {
326     move (lines, 0);
327     addnstr (blank, SidebarWidth-delim_len);
328   }
329   return 0;
330 }
331
332 /* returns index of new item with new mail or -1 */
333 static int exist_next_new () {
334   int i = 0;
335   if (list_empty(Incoming))
336     return (-1);
337   i = CurBuffy + 1;
338   while (i < Incoming->length)
339     if (((BUFFY*) Incoming->data[i++])->new > 0)
340       return (i-1);
341   return (-1);
342 }
343
344 /* returns index of prev item with new mail or -1 */
345 static int exist_prev_new () {
346   int i = 0;
347   if (list_empty(Incoming))
348     return (-1);
349   i = CurBuffy - 1;
350   while (i >= 0)
351     if (((BUFFY*) Incoming->data[i--])->new > 0)
352       return (i+1);
353   return (-1);
354 }
355
356 void sidebar_scroll (int op, int menu) {
357   int i = 0;
358
359   if (!SidebarWidth || list_empty(Incoming))
360     return;
361
362   switch (op) {
363   case OP_SIDEBAR_NEXT:
364     if (!option (OPTSIDEBARNEWMAILONLY)) {
365       if (CurBuffy + 1 == Incoming->length) {
366         mutt_error (_("You are on the last mailbox."));
367         return;
368       }
369       CurBuffy++;
370       break;
371     }                           /* the fall-through is intentional */
372   case OP_SIDEBAR_NEXT_NEW:
373     if ((i = exist_next_new ()) < 0) {
374       mutt_error (_("No next mailboxes with new mail."));
375       return;
376     }
377     else
378       CurBuffy = i;
379     break;
380   case OP_SIDEBAR_PREV:
381     if (!option (OPTSIDEBARNEWMAILONLY)) {
382       if (CurBuffy == 0) {
383         mutt_error (_("You are on the first mailbox."));
384         return;
385       }
386       CurBuffy--;
387       break;
388     }                           /* the fall-through is intentional */
389   case OP_SIDEBAR_PREV_NEW:
390     if ((i = exist_prev_new ()) < 0) {
391       mutt_error (_("No previous mailbox with new mail."));
392       return;
393     }
394     else
395       CurBuffy = i;
396     break;
397
398   case OP_SIDEBAR_SCROLL_UP:
399     if (CurBuffy == 0) {
400       mutt_error (_("You are on the first mailbox."));
401       return;
402     }
403     CurBuffy -= known_lines;
404     if (CurBuffy < 0)
405       CurBuffy = 0;
406     break;
407   case OP_SIDEBAR_SCROLL_DOWN:
408     if (CurBuffy + 1 == Incoming->length) {
409       mutt_error (_("You are on the last mailbox."));
410       return;
411     }
412     CurBuffy += known_lines;
413     if (CurBuffy >= Incoming->length)
414       CurBuffy = Incoming->length - 1;
415     break;
416   default:
417     return;
418   }
419   calc_boundaries (menu);
420   sidebar_draw (menu);
421 }