Andreas Krennmair:
[apps/madmutt.git] / sidebar.c
1 /*
2  * Copyright (C) ????-2004 Justin Hibbits <jrh29@po.cwru.edu>
3  * Copyright (C) 2004 Thomer M. Gil <mutt@thomer.com>
4  * 
5  *     This program is free software; you can redistribute it and/or modify
6  *     it under the terms of the GNU General Public License as published by
7  *     the Free Software Foundation; either version 2 of the License, or
8  *     (at your option) any later version.
9  * 
10  *     This program is distributed in the hope that it will be useful,
11  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *     GNU General Public License for more details.
14  * 
15  *     You should have received a copy of the GNU General Public License
16  *     along with this program; if not, write to the Free Software
17  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
18  */ 
19
20
21
22 #include "mutt.h"
23 #include "mutt_menu.h"
24 #include "mutt_curses.h"
25 #include "sidebar.h"
26 #include "buffy.h"
27 #include <libgen.h>
28 #include "keymap.h"
29 #include <stdbool.h>
30 #include <ctype.h>
31
32 /*BUFFY *CurBuffy = 0;*/
33 static BUFFY *TopBuffy = 0;
34 static BUFFY *BottomBuffy = 0;
35 static int known_lines = 0;
36 static bool initialized = false;
37 static int prev_show_value;
38 static short saveSidebarWidth;
39 static char *entry = 0;
40
41 static int quick_log10(int n)
42 {
43   int len = 0;
44   for (; n > 9; len++, n /= 10)
45     ;
46   return (++len);
47 }
48
49 static int cur_is_hidden (int maxline)
50 {
51   int l = 0, seen = 0;
52   BUFFY* tmp = TopBuffy;
53   if (!CurBuffy)
54     return (0);
55   while (tmp && l < maxline && !seen)
56   {
57     if (strcmp (tmp->path, CurBuffy->path) == 0)
58       seen = 1;
59     else
60       tmp = tmp->next;
61     l++;
62   }
63   return (seen == 0 || l == maxline);
64 }
65
66 void calc_boundaries (int menu)
67 {
68   BUFFY *tmp = Incoming;
69
70   if ( known_lines != LINES ) {
71     TopBuffy = BottomBuffy = 0;
72     known_lines = LINES;
73   }
74   for ( ; tmp->next != 0; tmp = tmp->next )
75     tmp->next->prev = tmp;
76
77   if ( TopBuffy == 0 && BottomBuffy == 0 )
78     TopBuffy = Incoming;
79   if ( BottomBuffy == 0 ) {
80     int count = LINES - 2 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
81     BottomBuffy = TopBuffy;
82     while ( --count && BottomBuffy->next )
83       BottomBuffy = BottomBuffy->next;
84   }
85   else if ( TopBuffy == CurBuffy->next ) {
86     int count = LINES - 2 - (menu != MENU_PAGER);
87     BottomBuffy = CurBuffy;
88     tmp = BottomBuffy;
89     while ( --count && tmp->prev)
90       tmp = tmp->prev;
91     TopBuffy = tmp;
92   }
93   else if ( BottomBuffy == CurBuffy->prev ) {
94     int count = LINES - 2 - (menu != MENU_PAGER);
95     TopBuffy = CurBuffy;
96     tmp = TopBuffy;
97     while ( --count && tmp->next )
98       tmp = tmp->next;
99     BottomBuffy = tmp;
100   }
101 }
102
103 static char * shortened_hierarchy(char * box) {
104   int dots = 0;
105   char * last_dot;
106   int i,j;
107   char * new_box;
108   for (i=0;i<strlen(box);++i) {
109     if (box[i] == '.') ++dots;
110     else if (isupper (box[i])) 
111       return (safe_strdup (box));
112   }
113   last_dot = strrchr(box,'.');
114   if (last_dot) {
115     ++last_dot;
116     new_box = safe_malloc(strlen(last_dot)+2*dots+1);
117     new_box[0] = box[0];
118     for (i=1,j=1;i<strlen(box);++i) {
119       if (box[i] == '.') {
120         new_box[j++] = '.';
121         new_box[j] = 0;
122         if (&box[i+1] != last_dot) {
123           new_box[j++] = box[i+1];
124           new_box[j] = 0;
125         } else {
126           strcat(&new_box[j],last_dot);
127           break;
128         }
129       }
130     }
131     return new_box;
132   }
133   return safe_strdup(box);
134 }
135
136 char *make_sidebar_entry(char *box, int size, int new, int tagged)
137 {
138   char *c;
139   int i = 0, dlen = mutt_strlen (SidebarDelim);
140
141   c = realloc(entry, SidebarWidth + 1);
142   if ( c ) entry = c;
143   entry[SidebarWidth] = 0;
144   for (; i < SidebarWidth; entry[i++] = ' ' );
145 #if USE_IMAP
146   if (ImapHomeNamespace && strlen(ImapHomeNamespace)>0) {
147     if (strncmp(box,ImapHomeNamespace,strlen(ImapHomeNamespace))==0 && strcmp(box,ImapHomeNamespace)!=0) {
148       box+=strlen(ImapHomeNamespace)+1;
149     }
150   }
151 #endif
152   if (option(OPTSHORTENHIERARCHY)) {
153     box = shortened_hierarchy(box);
154   }
155   i = strlen(box);
156   strncpy( entry, box, i < SidebarWidth - dlen ? i :SidebarWidth - dlen);
157
158   if ( new ) {
159     if (tagged>0) {
160       sprintf(entry + SidebarWidth - 5 - quick_log10(size) - dlen - quick_log10(new) - quick_log10(tagged),
161               "% d(%d)[%d]", size, new, tagged);
162     } else {
163       sprintf(entry + SidebarWidth - 3 - quick_log10(size) - dlen - quick_log10(new),
164               "% d(%d)", size, new);
165     }
166   } else {
167     if (tagged>0) {
168       sprintf( entry + SidebarWidth - 3 - quick_log10(size) - dlen - quick_log10(tagged), "% d[%d]", size,tagged);
169     } else {
170       sprintf( entry + SidebarWidth - 1 - quick_log10(size) - dlen, "% d", size);
171     }
172
173   }
174   if (option(OPTSHORTENHIERARCHY)) {
175     free(box);
176   }
177   return entry;
178 }
179
180 void set_curbuffy(char buf[LONG_STRING])
181 {
182   BUFFY* tmp = CurBuffy = Incoming;
183
184   if (!Incoming)
185     return;
186
187   while(1) {
188     if(!strcmp(tmp->path, buf)) {
189       CurBuffy = tmp;
190       break;
191     }
192
193     if(tmp->next)
194       tmp = tmp->next;
195     else
196       break;
197   }
198 }
199
200 void set_buffystats (CONTEXT* Context)
201 {
202   BUFFY* tmp = Incoming;
203   while (tmp)
204   {
205     if (strcmp (tmp->path, Context->path) == 0)
206     {
207       tmp->msg_unread = Context->unread;
208       tmp->msgcount = Context->msgcount;
209       tmp->msg_tagged = Context->flagged;
210       break;
211     }
212     tmp = tmp->next;
213   }
214 }
215
216 int draw_sidebar(int menu) {
217
218   int lines = option(OPTHELP) ? 1 : 0;
219   BUFFY *tmp;
220   short delim_len = mutt_strlen (SidebarDelim);
221
222   /* initialize first time */
223   if(!initialized) {
224     prev_show_value = option(OPTMBOXPANE);
225     saveSidebarWidth = SidebarWidth;
226     if(!option(OPTMBOXPANE)) SidebarWidth = 0;
227     initialized = true;
228   }
229
230   /* save or restore the value SidebarWidth */
231   if(prev_show_value != option(OPTMBOXPANE)) {
232     if(prev_show_value && !option(OPTMBOXPANE)) {
233       saveSidebarWidth = SidebarWidth;
234       SidebarWidth = 0;
235     } else if(!prev_show_value && option(OPTMBOXPANE)) {
236       SidebarWidth = saveSidebarWidth;
237     }
238     prev_show_value = option(OPTMBOXPANE);
239   }
240
241   if ( SidebarWidth == 0 ) return 0;
242
243   /* draw the divider */
244   SETCOLOR(MT_COLOR_STATUS);
245   for (lines = option (OPTSTATUSONTOP) ? 0 : 1; 
246        lines < LINES-1-(menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++ ) {
247     move(lines, SidebarWidth - delim_len);
248     addstr (NONULL (SidebarDelim));
249   }
250   SETCOLOR(MT_COLOR_NORMAL);
251
252   if ( Incoming == 0 ) return 0;
253   lines = option(OPTHELP) ? 1 : 0; /* go back to the top */
254
255   if (cur_is_hidden (LINES-1-(menu != MENU_PAGER)))
256     CurBuffy = TopBuffy;
257
258   if ( known_lines != LINES || TopBuffy == 0 || BottomBuffy == 0 ) 
259     calc_boundaries(menu);
260   if ( CurBuffy == 0 ) CurBuffy = Incoming;
261
262   tmp = TopBuffy;
263
264   for ( ; tmp && lines < LINES-1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); tmp = tmp->next ) {
265     if ( tmp == CurBuffy )
266       SETCOLOR(MT_COLOR_INDICATOR);
267     else if ( tmp->msg_unread > 0 )
268       SETCOLOR(MT_COLOR_NEW);
269     else
270       SETCOLOR(MT_COLOR_NORMAL);
271
272     move( lines, 0 );
273     if ( Context && !strcmp( tmp->path, Context->path ) ) {
274       printw( "%.*s", SidebarWidth - delim_len,
275               make_sidebar_entry(basename(tmp->path),
276                                  Context->msgcount, Context->unread, Context->tagged));
277       tmp->msg_unread = Context->unread;
278       tmp->msgcount = Context->msgcount;
279       tmp->msg_tagged = Context->tagged;
280     }
281     else
282       printw( "%.*s", SidebarWidth - delim_len,
283               make_sidebar_entry(basename(tmp->path),
284                                  tmp->msgcount,tmp->msg_unread, tmp->msg_tagged));
285     lines++;
286   }
287   SETCOLOR(MT_COLOR_NORMAL);
288   for ( ; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++ ) {
289     int i = 0;
290     move( lines, 0 );
291     for ( ; i < SidebarWidth - delim_len; i++ )
292       addch(' ');
293   }
294   return 0;
295 }
296
297 void scroll_sidebar(int op, int menu)
298 {
299         if(!SidebarWidth) return;
300         if(!CurBuffy) return;
301
302   switch (op) {
303     case OP_SIDEBAR_NEXT:
304       if ( CurBuffy->next == NULL ) return;
305       CurBuffy = CurBuffy->next;
306       break;
307     case OP_SIDEBAR_PREV:
308       if ( CurBuffy == Incoming ) return;
309       {
310         BUFFY *tmp = Incoming;
311         while ( tmp->next && strcmp(tmp->next->path, CurBuffy->path) ) tmp = tmp->next;
312         CurBuffy = tmp;
313       }
314       break;
315     case OP_SIDEBAR_SCROLL_UP:
316       CurBuffy = TopBuffy;
317       if ( CurBuffy != Incoming ) {
318         calc_boundaries(menu);
319         CurBuffy = CurBuffy->prev;
320       }
321       break;
322     case OP_SIDEBAR_SCROLL_DOWN:
323       CurBuffy = BottomBuffy;
324       if ( CurBuffy->next ) {
325         calc_boundaries(menu);
326         CurBuffy = CurBuffy->next;
327       }
328       break;
329     default:
330       return;
331   }
332   calc_boundaries(menu);
333   draw_sidebar(menu);
334 }