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