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
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 char *make_sidebar_entry(char *box, int size, int new)
81 {
82         static char *entry = 0;
83         char *c;
84         int i = 0;
85
86         c = realloc(entry, SidebarWidth + 1);
87         if ( c ) entry = c;
88         entry[SidebarWidth] = 0;
89         for (; i < SidebarWidth; entry[i++] = ' ' );
90         i = strlen(box);
91         strncpy( entry, box, i < SidebarWidth ? i :SidebarWidth );
92
93         if ( new ) 
94                 sprintf(
95                         entry + SidebarWidth - 5 - quick_log10(size) - quick_log10(new),
96                         "% d(%d)", size, new);
97         else
98                 sprintf( entry + SidebarWidth - 3 - quick_log10(size), "% d", size);
99         return entry;
100 }
101
102 void set_curbuffy(char buf[LONG_STRING])
103 {
104   BUFFY* tmp = CurBuffy = Incoming;
105
106   if (!Incoming)
107     return;
108
109   while(1) {
110     if(!strcmp(tmp->path, buf)) {
111       CurBuffy = tmp;
112       break;
113     }
114
115     if(tmp->next)
116       tmp = tmp->next;
117     else
118       break;
119   }
120 }
121
122 int draw_sidebar(int menu) {
123
124         int lines = option(OPTHELP) ? 1 : 0;
125         BUFFY *tmp;
126 #ifndef USE_SLANG_CURSES
127         attr_t attrs;
128 #endif
129         short color_pair;
130
131         static bool initialized = false;
132         static int prev_show_value;
133         static short saveSidebarWidth;
134
135         /* initialize first time */
136         if(!initialized) {
137                 prev_show_value = option(OPTMBOXPANE);
138                 saveSidebarWidth = SidebarWidth;
139                 if(!option(OPTMBOXPANE)) SidebarWidth = 0;
140                 initialized = true;
141         }
142
143         /* save or restore the value SidebarWidth */
144         if(prev_show_value != option(OPTMBOXPANE)) {
145                 if(prev_show_value && !option(OPTMBOXPANE)) {
146                         saveSidebarWidth = SidebarWidth;
147                         SidebarWidth = 0;
148                 } else if(!prev_show_value && option(OPTMBOXPANE)) {
149                         SidebarWidth = saveSidebarWidth;
150                 }
151                 prev_show_value = option(OPTMBOXPANE);
152         }
153
154
155         if ( SidebarWidth == 0 ) return 0;
156
157         /* get attributes for divider */
158         SETCOLOR(MT_COLOR_STATUS);
159 #ifndef USE_SLANG_CURSES
160         attr_get(&attrs, &color_pair, 0);
161 #else
162         color_pair = attr_get();
163 #endif
164         SETCOLOR(MT_COLOR_NORMAL);
165
166         /* draw the divider */
167
168         for ( ; lines < LINES-1-(menu != MENU_PAGER); lines++ ) {
169                 move(lines, SidebarWidth - 1);
170                 addch('|');
171  #ifndef USE_SLANG_CURSES
172                 mvchgat(lines, SidebarWidth - 1, 1, 0, color_pair, NULL);
173  #endif
174         }
175         if ( Incoming == 0 ) return 0;
176         lines = option(OPTHELP) ? 1 : 0; /* go back to the top */
177
178         if ( known_lines != LINES || TopBuffy == 0 || BottomBuffy == 0 ) 
179                 calc_boundaries(menu);
180         if ( CurBuffy == 0 ) CurBuffy = Incoming;
181
182         tmp = TopBuffy;
183
184         SETCOLOR(MT_COLOR_NORMAL);
185
186         for ( ; tmp && lines < LINES-1 - (menu != MENU_PAGER); tmp = tmp->next ) {
187                 if ( tmp == CurBuffy )
188                         SETCOLOR(MT_COLOR_INDICATOR);
189                 else if ( tmp->msg_unread > 0 )
190                         SETCOLOR(MT_COLOR_NEW);
191                 else
192                         SETCOLOR(MT_COLOR_NORMAL);
193
194                 move( lines, 0 );
195                 if ( Context && !strcmp( tmp->path, Context->path ) ) {
196                         printw( "%.*s", SidebarWidth,
197                                 make_sidebar_entry(basename(tmp->path), Context->msgcount,
198                                 Context->unread));
199                         tmp->msg_unread = Context->unread;
200                         tmp->msgcount = Context->msgcount;
201                 }
202                 else
203                         printw( "%.*s", SidebarWidth,
204                                 make_sidebar_entry(basename(tmp->path), tmp->msgcount,
205                                 tmp->msg_unread));
206                 lines++;
207         }
208         SETCOLOR(MT_COLOR_NORMAL);
209         for ( ; lines < LINES - 1 - (menu != MENU_PAGER); lines++ ) {
210                 int i = 0;
211                 move( lines, 0 );
212                 for ( ; i < SidebarWidth - 1; i++ )
213                         addch(' ');
214         }
215         return 0;
216 }
217
218 void scroll_sidebar(int op, int menu)
219 {
220         if(!SidebarWidth) return;
221         if(!CurBuffy) return;
222
223         switch (op) {
224                 case OP_SIDEBAR_NEXT:
225                         if ( CurBuffy->next == NULL ) return;
226                         CurBuffy = CurBuffy->next;
227                         break;
228                 case OP_SIDEBAR_PREV:
229                         if ( CurBuffy == Incoming ) return;
230                         {
231                                 BUFFY *tmp = Incoming;
232                                 while ( tmp->next && strcmp(tmp->next->path, CurBuffy->path) ) tmp = tmp->next;
233                                 CurBuffy = tmp;
234                         }
235                         break;
236                 case OP_SIDEBAR_SCROLL_UP:
237                         CurBuffy = TopBuffy;
238                         if ( CurBuffy != Incoming ) {
239                                 calc_boundaries(menu);
240                                 CurBuffy = CurBuffy->prev;
241                         }
242                         break;
243                 case OP_SIDEBAR_SCROLL_DOWN:
244                         CurBuffy = BottomBuffy;
245                         if ( CurBuffy->next ) {
246                                 calc_boundaries(menu);
247                                 CurBuffy = CurBuffy->next;
248                         }
249                         break;
250                 default:
251                         return;
252         }
253         calc_boundaries(menu);
254         draw_sidebar(menu);
255 }