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