Nico Golde:
[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 // CurBuffy should contain a valid buffy mailbox before calling this function!!!
50 void calc_boundaries (int menu)
51 {
52   BUFFY *tmp = Incoming;
53   int position;
54   int i,count, mailbox_position;
55
56   // correct known_lines if it has changed because of a window resize
57   if ( known_lines != LINES ) {
58     known_lines = LINES;
59   }
60   // fix all the prev links on all the mailboxes
61   for ( ; tmp->next != 0; tmp = tmp->next )
62     tmp->next->prev = tmp;
63
64   // calculate the position of the current mailbox
65   position = 1;
66   tmp = Incoming;
67   while (tmp != CurBuffy)
68   {
69           position++;
70           tmp = tmp->next;
71   }
72   // calculate the size of the screen we can use
73   count = LINES - 2 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
74   // calculate the position of the current mailbox on the screen
75   mailbox_position = position%count;
76   // determine topbuffy
77   TopBuffy = CurBuffy;
78   for(i = mailbox_position; i >1; i--) TopBuffy = TopBuffy->prev;
79   // determine bottombuffy
80   BottomBuffy = CurBuffy;
81   for(i = mailbox_position; i <= count && BottomBuffy->next; i++) BottomBuffy = BottomBuffy->next;
82 }
83
84 static char * shortened_hierarchy(char * box) {
85   int dots = 0;
86   char * last_dot;
87   int i,j;
88   char * new_box;
89   for (i=0;i<strlen(box);++i) {
90     if (box[i] == '.') ++dots;
91     else if (isupper (box[i])) 
92       return (safe_strdup (box));
93   }
94   last_dot = strrchr(box,'.');
95   if (last_dot) {
96     ++last_dot;
97     new_box = safe_malloc(strlen(last_dot)+2*dots+1);
98     new_box[0] = box[0];
99     for (i=1,j=1;i<strlen(box);++i) {
100       if (box[i] == '.') {
101         new_box[j++] = '.';
102         new_box[j] = 0;
103         if (&box[i+1] != last_dot) {
104           new_box[j++] = box[i+1];
105           new_box[j] = 0;
106         } else {
107           strcat(&new_box[j],last_dot);
108           break;
109         }
110       }
111     }
112     return new_box;
113   }
114   return safe_strdup(box);
115 }
116
117 char *make_sidebar_entry(char *box, int size, int new, int flagged)
118 {
119   char *c;
120   int i = 0, dlen = mutt_strlen (SidebarDelim), max = SidebarWidth-dlen-1,
121       shortened = 0;
122
123   c = realloc(entry, SidebarWidth + 1);
124   if ( c ) entry = c;
125   entry[SidebarWidth] = 0;
126   for (; i < SidebarWidth; entry[i++] = ' ' );
127 #if USE_IMAP
128   if (ImapHomeNamespace && strlen(ImapHomeNamespace)>0) {
129     if (strncmp(box,ImapHomeNamespace,strlen(ImapHomeNamespace))==0 && strcmp(box,ImapHomeNamespace)!=0) {
130       box+=strlen(ImapHomeNamespace)+1;
131     }
132   }
133 #endif
134   max -= quick_log10(size);
135   if (new)
136     max -= quick_log10(new) + 2;
137   if (flagged > 0)
138     max -= quick_log10(flagged) + 2;
139   if (option(OPTSHORTENHIERARCHY) && mutt_strlen(box) > max) {
140     box = shortened_hierarchy(box);
141     shortened = 1;
142   }
143   i = strlen(box);
144   strncpy( entry, box, i < SidebarWidth - dlen ? i :SidebarWidth - dlen);
145
146   if ( new ) {
147     if (flagged>0) {
148       sprintf(entry + SidebarWidth - 5 - quick_log10(size) - dlen - quick_log10(new) - quick_log10(flagged),
149               "% d(%d)[%d]", size, new, flagged);
150     } else {
151       sprintf(entry + SidebarWidth - 3 - quick_log10(size) - dlen - quick_log10(new),
152               "% d(%d)", size, new);
153     }
154   } else {
155     if (flagged>0) {
156       sprintf( entry + SidebarWidth - 3 - quick_log10(size) - dlen - quick_log10(flagged), "% d[%d]", size,flagged);
157     } else {
158       sprintf( entry + SidebarWidth - 1 - quick_log10(size) - dlen, "% d", size);
159     }
160
161   }
162   if (option(OPTSHORTENHIERARCHY) && shortened) {
163     free(box);
164   }
165   return entry;
166 }
167
168 void set_curbuffy(char buf[LONG_STRING])
169 {
170   BUFFY* tmp = CurBuffy = Incoming;
171
172   if (!Incoming)
173     return;
174
175   while(1) {
176     if(!strcmp(tmp->path, buf)) {
177       CurBuffy = tmp;
178       break;
179     }
180
181     if(tmp->next)
182       tmp = tmp->next;
183     else
184       break;
185   }
186 }
187
188 void set_buffystats (CONTEXT* Context)
189 {
190   BUFFY* tmp = Incoming;
191   if (!Context)
192     return;
193   while (tmp)
194   {
195     if (strcmp (tmp->path, Context->path) == 0)
196     {
197       tmp->msg_unread = Context->unread;
198       tmp->msgcount = Context->msgcount;
199       tmp->msg_flagged = Context->flagged;
200       break;
201     }
202     tmp = tmp->next;
203   }
204 }
205
206 int draw_sidebar(int menu) {
207
208   int lines = option(OPTHELP) ? 1 : 0;
209   BUFFY *tmp;
210   short delim_len = mutt_strlen (SidebarDelim);
211
212   /* initialize first time */
213   if(!initialized) {
214     prev_show_value = option(OPTMBOXPANE);
215     saveSidebarWidth = SidebarWidth;
216     if(!option(OPTMBOXPANE)) SidebarWidth = 0;
217     initialized = true;
218   }
219
220   /* save or restore the value SidebarWidth */
221   if(prev_show_value != option(OPTMBOXPANE)) {
222     if(prev_show_value && !option(OPTMBOXPANE)) {
223       saveSidebarWidth = SidebarWidth;
224       SidebarWidth = 0;
225     } else if(!prev_show_value && option(OPTMBOXPANE)) {
226       SidebarWidth = saveSidebarWidth;
227       /* after toggle: force recounting of all mail */
228       mutt_buffy_check(1);
229     }
230     prev_show_value = option(OPTMBOXPANE);
231   }
232
233   if ( SidebarWidth == 0 ) return 0;
234
235   /* draw the divider */
236   SETCOLOR(MT_COLOR_STATUS);
237   for (lines = option (OPTSTATUSONTOP) ? 0 : 1; 
238        lines < LINES-1-(menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++ ) {
239     move(lines, SidebarWidth - delim_len);
240     addstr (NONULL (SidebarDelim));
241   }
242   SETCOLOR(MT_COLOR_NORMAL);
243
244   if ( Incoming == 0 ) return 0;
245   lines = option(OPTHELP) ? 1 : 0; /* go back to the top */
246
247   if ( CurBuffy == 0 ) CurBuffy = Incoming;
248   if ( known_lines != LINES || TopBuffy == 0 || BottomBuffy == 0 ) 
249     calc_boundaries(menu);
250
251   tmp = TopBuffy;
252
253   for ( ; tmp && lines < LINES-1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); tmp = tmp->next ) {
254     if ( tmp == CurBuffy )
255       SETCOLOR(MT_COLOR_INDICATOR);
256     else if ( tmp->msg_flagged > 0 )
257       SETCOLOR(MT_COLOR_FLAGGED);
258     else if ( tmp->msg_unread > 0 )
259       SETCOLOR(MT_COLOR_NEW);
260     else
261       SETCOLOR(MT_COLOR_NORMAL);
262
263     move( lines, 0 );
264     if ( Context && !strcmp( tmp->path, Context->path ) ) {
265       printw( "%.*s", SidebarWidth - delim_len,
266               make_sidebar_entry(basename(tmp->path),
267                                  Context->msgcount, Context->unread, Context->flagged));
268       tmp->msg_unread = Context->unread;
269       tmp->msgcount = Context->msgcount;
270       tmp->msg_flagged = Context->flagged;
271     }
272     else
273       printw( "%.*s", SidebarWidth - delim_len,
274               make_sidebar_entry(basename(tmp->path),
275                                  tmp->msgcount,tmp->msg_unread, tmp->msg_flagged));
276     lines++;
277   }
278   SETCOLOR(MT_COLOR_NORMAL);
279   for ( ; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++ ) {
280     int i = 0;
281     move( lines, 0 );
282     for ( ; i < SidebarWidth - delim_len; i++ )
283       addch(' ');
284   }
285   return 0;
286 }
287
288 BUFFY * exist_next_new()
289 {
290         BUFFY *tmp = CurBuffy;
291         if(tmp == NULL) return NULL;
292         while (tmp->next != NULL)
293         {
294                 tmp = tmp->next;
295                 if(tmp->msg_unread) return tmp;
296         }
297         return NULL;
298 }
299
300 BUFFY * exist_prev_new()
301 {
302         BUFFY *tmp = CurBuffy;
303         if(tmp == NULL) return NULL;
304         while (tmp->prev != NULL)
305         {
306                 tmp = tmp->prev;
307                 if(tmp->msg_unread) return tmp;
308         }
309         return NULL;
310 }     
311
312
313 void scroll_sidebar(int op, int menu)
314 {
315   BUFFY *tmp;
316
317   if(!SidebarWidth) return;
318   if(!CurBuffy) return;
319
320   switch (op) {
321     case OP_SIDEBAR_NEXT:
322       if ( CurBuffy->next == NULL ) return;
323       CurBuffy = CurBuffy->next;
324       break;
325     case OP_SIDEBAR_NEXT_NEW:
326       if ( (tmp = exist_next_new()) == NULL)
327           return;
328       else CurBuffy = tmp;
329       break;
330     case OP_SIDEBAR_PREV:
331       if ( CurBuffy->prev == NULL ) return;
332       CurBuffy = CurBuffy->prev;
333      break;
334     case OP_SIDEBAR_PREV_NEW:
335       if ( (tmp = exist_prev_new()) == NULL)
336           return;
337       else CurBuffy = tmp;
338       break;
339      
340     case OP_SIDEBAR_SCROLL_UP:
341       CurBuffy = TopBuffy;
342       if ( CurBuffy != Incoming ) {
343         calc_boundaries(menu);
344         CurBuffy = CurBuffy->prev;
345       }
346       break;
347     case OP_SIDEBAR_SCROLL_DOWN:
348       CurBuffy = BottomBuffy;
349       if ( CurBuffy->next ) {
350         calc_boundaries(menu);
351         CurBuffy = CurBuffy->next;
352       }
353       break;
354     default:
355       return;
356   }
357   calc_boundaries(menu);
358   draw_sidebar(menu);
359 }