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
31 /*BUFFY *CurBuffy = 0;*/
32 static BUFFY *TopBuffy = 0;
33 static BUFFY *BottomBuffy = 0;
34 static int known_lines = 0;
35
36 static int quick_log10(int n)
37 {
38         char string[32];
39         sprintf(string, "%d", n);
40         return strlen(string);
41 }
42
43 void calc_boundaries (int menu)
44 {
45         BUFFY *tmp = Incoming;
46
47         if ( known_lines != LINES ) {
48                 TopBuffy = BottomBuffy = 0;
49                 known_lines = LINES;
50         }
51         for ( ; tmp->next != 0; tmp = tmp->next )
52                 tmp->next->prev = tmp;
53
54         if ( TopBuffy == 0 && BottomBuffy == 0 )
55                 TopBuffy = Incoming;
56         if ( BottomBuffy == 0 ) {
57                 int count = LINES - 2 - (menu != MENU_PAGER);
58                 BottomBuffy = TopBuffy;
59                 while ( --count && BottomBuffy->next )
60                         BottomBuffy = BottomBuffy->next;
61         }
62         else if ( TopBuffy == CurBuffy->next ) {
63                 int count = LINES - 2 - (menu != MENU_PAGER);
64                 BottomBuffy = CurBuffy;
65                 tmp = BottomBuffy;
66                 while ( --count && tmp->prev)
67                         tmp = tmp->prev;
68                 TopBuffy = tmp;
69         }
70         else if ( BottomBuffy == CurBuffy->prev ) {
71                 int count = LINES - 2 - (menu != MENU_PAGER);
72                 TopBuffy = CurBuffy;
73                 tmp = TopBuffy;
74                 while ( --count && tmp->next )
75                         tmp = tmp->next;
76                 BottomBuffy = tmp;
77         }
78 }
79
80 static char * shortened_hierarchy(char * box) {
81   int dots = 0;
82   char * last_dot;
83   int i,j;
84   char * new_box;
85   for (i=0;i<strlen(box);++i)
86     if (box[i] == '.') ++dots;
87   last_dot = strrchr(box,'.');
88   if (last_dot) {
89     ++last_dot;
90     new_box = safe_malloc(strlen(last_dot)+2*dots+1);
91     new_box[0] = box[0];
92     for (i=1,j=1;i<strlen(box);++i) {
93       if (box[i] == '.') {
94         new_box[j++] = '.';
95         new_box[j] = 0;
96         if (&box[i+1] != last_dot) {
97           new_box[j++] = box[i+1];
98           new_box[j] = 0;
99         } else {
100           strcat(&new_box[j],last_dot);
101           break;
102         }
103       }
104     }
105     return new_box;
106   }
107   return safe_strdup(box);
108 }
109
110 char *make_sidebar_entry(char *box, int size, int new)
111 {
112         static char *entry = 0;
113         char *c;
114         int i = 0;
115
116         c = realloc(entry, SidebarWidth + 1);
117         if ( c ) entry = c;
118         entry[SidebarWidth] = 0;
119         for (; i < SidebarWidth; entry[i++] = ' ' );
120 #if USE_IMAP
121         if (ImapHomeNamespace && strlen(ImapHomeNamespace)>0) {
122     if (strncmp(box,ImapHomeNamespace,strlen(ImapHomeNamespace))==0 && strcmp(box,ImapHomeNamespace)!=0) {
123       box+=strlen(ImapHomeNamespace)+1;
124     }
125   }
126 #endif
127   if (option(OPTSHORTENHIERARCHY)) {
128     box = shortened_hierarchy(box);
129   }
130         i = strlen(box);
131         strncpy( entry, box, i < SidebarWidth ? i :SidebarWidth );
132
133         if ( new ) 
134                 sprintf(
135                         entry + SidebarWidth - 5 - quick_log10(size) - quick_log10(new),
136                         "% d(%d)", size, new);
137         else
138                 sprintf( entry + SidebarWidth - 3 - quick_log10(size), "% d", size);
139   if (option(OPTSHORTENHIERARCHY)) {
140     free(box);
141   }
142         return entry;
143 }
144
145 void set_curbuffy(char buf[LONG_STRING])
146 {
147   BUFFY* tmp = CurBuffy = Incoming;
148
149   if (!Incoming)
150     return;
151
152   while(1) {
153     if(!strcmp(tmp->path, buf)) {
154       CurBuffy = tmp;
155       break;
156     }
157
158     if(tmp->next)
159       tmp = tmp->next;
160     else
161       break;
162   }
163 }
164
165 int draw_sidebar(int menu) {
166
167         int lines = option(OPTHELP) ? 1 : 0;
168         BUFFY *tmp;
169 #ifndef USE_SLANG_CURSES
170         attr_t attrs;
171 #endif
172         short color_pair;
173
174         static bool initialized = false;
175         static int prev_show_value;
176         static short saveSidebarWidth;
177
178         /* initialize first time */
179         if(!initialized) {
180                 prev_show_value = option(OPTMBOXPANE);
181                 saveSidebarWidth = SidebarWidth;
182                 if(!option(OPTMBOXPANE)) SidebarWidth = 0;
183                 initialized = true;
184         }
185
186         /* save or restore the value SidebarWidth */
187         if(prev_show_value != option(OPTMBOXPANE)) {
188                 if(prev_show_value && !option(OPTMBOXPANE)) {
189                         saveSidebarWidth = SidebarWidth;
190                         SidebarWidth = 0;
191                 } else if(!prev_show_value && option(OPTMBOXPANE)) {
192                         SidebarWidth = saveSidebarWidth;
193                 }
194                 prev_show_value = option(OPTMBOXPANE);
195         }
196
197
198         if ( SidebarWidth == 0 ) return 0;
199
200         /* get attributes for divider */
201         SETCOLOR(MT_COLOR_STATUS);
202 #ifndef USE_SLANG_CURSES
203         attr_get(&attrs, &color_pair, 0);
204 #else
205         color_pair = attr_get();
206 #endif
207         SETCOLOR(MT_COLOR_NORMAL);
208
209         /* draw the divider */
210
211         for ( ; lines < LINES-2-(menu != MENU_PAGER); lines++ ) {
212                 move(lines, SidebarWidth - 1);
213                 addch('|');
214  #ifndef USE_SLANG_CURSES
215                 mvchgat(lines, SidebarWidth - 1, 1, 0, color_pair, NULL);
216  #endif
217         }
218         if ( Incoming == 0 ) return 0;
219         lines = option(OPTHELP) ? 1 : 0; /* go back to the top */
220
221         if ( known_lines != LINES || TopBuffy == 0 || BottomBuffy == 0 ) 
222                 calc_boundaries(menu);
223         if ( CurBuffy == 0 ) CurBuffy = Incoming;
224
225         tmp = TopBuffy;
226
227         SETCOLOR(MT_COLOR_NORMAL);
228
229         for ( ; tmp && lines < LINES-1 - (menu != MENU_PAGER); tmp = tmp->next ) {
230                 if ( tmp == CurBuffy )
231                         SETCOLOR(MT_COLOR_INDICATOR);
232                 else if ( tmp->msg_unread > 0 )
233                         SETCOLOR(MT_COLOR_NEW);
234                 else
235                         SETCOLOR(MT_COLOR_NORMAL);
236
237                 move( lines, 0 );
238                 if ( Context && !strcmp( tmp->path, Context->path ) ) {
239                         printw( "%.*s", SidebarWidth,
240                                 make_sidebar_entry(basename(tmp->path), Context->msgcount,
241                                 Context->unread));
242                         tmp->msg_unread = Context->unread;
243                         tmp->msgcount = Context->msgcount;
244                 }
245                 else
246                         printw( "%.*s", SidebarWidth,
247                                 make_sidebar_entry(basename(tmp->path), tmp->msgcount,
248                                 tmp->msg_unread));
249                 lines++;
250         }
251         SETCOLOR(MT_COLOR_NORMAL);
252         for ( ; lines < LINES - 2 - (menu != MENU_PAGER); lines++ ) {
253                 int i = 0;
254                 move( lines, 0 );
255                 for ( ; i < SidebarWidth - 1; i++ )
256                         addch(' ');
257         }
258         return 0;
259 }
260
261 void scroll_sidebar(int op, int menu)
262 {
263         if(!SidebarWidth) return;
264         if(!CurBuffy) return;
265
266         switch (op) {
267                 case OP_SIDEBAR_NEXT:
268                         if ( CurBuffy->next == NULL ) return;
269                         CurBuffy = CurBuffy->next;
270                         break;
271                 case OP_SIDEBAR_PREV:
272                         if ( CurBuffy == Incoming ) return;
273                         {
274                                 BUFFY *tmp = Incoming;
275                                 while ( tmp->next && strcmp(tmp->next->path, CurBuffy->path) ) tmp = tmp->next;
276                                 CurBuffy = tmp;
277                         }
278                         break;
279                 case OP_SIDEBAR_SCROLL_UP:
280                         CurBuffy = TopBuffy;
281                         if ( CurBuffy != Incoming ) {
282                                 calc_boundaries(menu);
283                                 CurBuffy = CurBuffy->prev;
284                         }
285                         break;
286                 case OP_SIDEBAR_SCROLL_DOWN:
287                         CurBuffy = BottomBuffy;
288                         if ( CurBuffy->next ) {
289                                 calc_boundaries(menu);
290                                 CurBuffy = CurBuffy->next;
291                         }
292                         break;
293                 default:
294                         return;
295         }
296         calc_boundaries(menu);
297         draw_sidebar(menu);
298 }