2 * Copyright (C) ????-2004 Justin Hibbits <jrh29@po.cwru.edu>
3 * Copyright (C) 2004 Thomer M. Gil <mutt@thomer.com>
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.
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.
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.
23 #include "mutt_menu.h"
24 #include "mutt_curses.h"
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;
41 static int quick_log10(int n)
44 for (; n > 9; len++, n /= 10)
49 static int cur_is_hidden (int maxline)
52 BUFFY* tmp = TopBuffy;
55 while (tmp && l < maxline && !seen)
57 if (strcmp (tmp->path, CurBuffy->path) == 0)
63 return (seen == 0 || l == maxline);
66 void calc_boundaries (int menu)
68 BUFFY *tmp = Incoming;
70 if ( known_lines != LINES ) {
71 TopBuffy = BottomBuffy = 0;
74 for ( ; tmp->next != 0; tmp = tmp->next )
75 tmp->next->prev = tmp;
77 if ( TopBuffy == 0 && BottomBuffy == 0 )
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;
85 else if ( TopBuffy == CurBuffy->next ) {
86 int count = LINES - 2 - (menu != MENU_PAGER);
87 BottomBuffy = CurBuffy;
89 while ( --count && tmp->prev)
93 else if ( BottomBuffy == CurBuffy->prev ) {
94 int count = LINES - 2 - (menu != MENU_PAGER);
97 while ( --count && tmp->next )
103 static char * shortened_hierarchy(char * 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));
113 last_dot = strrchr(box,'.');
116 new_box = safe_malloc(strlen(last_dot)+2*dots+1);
118 for (i=1,j=1;i<strlen(box);++i) {
122 if (&box[i+1] != last_dot) {
123 new_box[j++] = box[i+1];
126 strcat(&new_box[j],last_dot);
133 return safe_strdup(box);
136 char *make_sidebar_entry(char *box, int size, int new)
139 int i = 0, dlen = mutt_strlen (SidebarDelim);
141 c = realloc(entry, SidebarWidth + 1);
143 entry[SidebarWidth] = 0;
144 for (; i < SidebarWidth; entry[i++] = ' ' );
146 if (ImapHomeNamespace && strlen(ImapHomeNamespace)>0) {
147 if (strncmp(box,ImapHomeNamespace,strlen(ImapHomeNamespace))==0 && strcmp(box,ImapHomeNamespace)!=0) {
148 box+=strlen(ImapHomeNamespace)+1;
152 if (option(OPTSHORTENHIERARCHY)) {
153 box = shortened_hierarchy(box);
156 strncpy( entry, box, i < SidebarWidth - dlen ? i :SidebarWidth - dlen);
159 sprintf(entry + SidebarWidth - 3 - quick_log10(size) - dlen - quick_log10(new),
160 "% d(%d)", size, new);
162 sprintf( entry + SidebarWidth - 1 - quick_log10(size) - dlen, "% d", size);
163 if (option(OPTSHORTENHIERARCHY)) {
169 void set_curbuffy(char buf[LONG_STRING])
171 BUFFY* tmp = CurBuffy = Incoming;
177 if(!strcmp(tmp->path, buf)) {
189 void set_buffystats (CONTEXT* Context)
191 BUFFY* tmp = Incoming;
194 if (strcmp (tmp->path, Context->path) == 0)
196 tmp->msg_unread = Context->unread;
197 tmp->msgcount = Context->msgcount;
204 int draw_sidebar(int menu) {
206 int lines = option(OPTHELP) ? 1 : 0;
208 short delim_len = mutt_strlen (SidebarDelim);
210 /* initialize first time */
212 prev_show_value = option(OPTMBOXPANE);
213 saveSidebarWidth = SidebarWidth;
214 if(!option(OPTMBOXPANE)) SidebarWidth = 0;
218 /* save or restore the value SidebarWidth */
219 if(prev_show_value != option(OPTMBOXPANE)) {
220 if(prev_show_value && !option(OPTMBOXPANE)) {
221 saveSidebarWidth = SidebarWidth;
223 } else if(!prev_show_value && option(OPTMBOXPANE)) {
224 SidebarWidth = saveSidebarWidth;
226 prev_show_value = option(OPTMBOXPANE);
229 if ( SidebarWidth == 0 ) return 0;
231 /* draw the divider */
232 SETCOLOR(MT_COLOR_STATUS);
233 for (lines = option (OPTSTATUSONTOP) ? 0 : 1;
234 lines < LINES-1-(menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++ ) {
235 move(lines, SidebarWidth - delim_len);
236 addstr (NONULL (SidebarDelim));
238 SETCOLOR(MT_COLOR_NORMAL);
240 if ( Incoming == 0 ) return 0;
241 lines = option(OPTHELP) ? 1 : 0; /* go back to the top */
243 if (cur_is_hidden (LINES-1-(menu != MENU_PAGER)))
246 if ( known_lines != LINES || TopBuffy == 0 || BottomBuffy == 0 )
247 calc_boundaries(menu);
248 if ( CurBuffy == 0 ) CurBuffy = Incoming;
252 for ( ; tmp && lines < LINES-1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); tmp = tmp->next ) {
253 if ( tmp == CurBuffy )
254 SETCOLOR(MT_COLOR_INDICATOR);
255 else if ( tmp->msg_unread > 0 )
256 SETCOLOR(MT_COLOR_NEW);
258 SETCOLOR(MT_COLOR_NORMAL);
261 if ( Context && !strcmp( tmp->path, Context->path ) ) {
262 printw( "%.*s", SidebarWidth - delim_len,
263 make_sidebar_entry(basename(tmp->path),
264 Context->msgcount, Context->unread));
265 tmp->msg_unread = Context->unread;
266 tmp->msgcount = Context->msgcount;
269 printw( "%.*s", SidebarWidth - delim_len,
270 make_sidebar_entry(basename(tmp->path),
271 tmp->msgcount,tmp->msg_unread));
274 SETCOLOR(MT_COLOR_NORMAL);
275 for ( ; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++ ) {
278 for ( ; i < SidebarWidth - delim_len; i++ )
284 void scroll_sidebar(int op, int menu)
286 if(!SidebarWidth) return;
287 if(!CurBuffy) return;
290 case OP_SIDEBAR_NEXT:
291 if ( CurBuffy->next == NULL ) return;
292 CurBuffy = CurBuffy->next;
294 case OP_SIDEBAR_PREV:
295 if ( CurBuffy == Incoming ) return;
297 BUFFY *tmp = Incoming;
298 while ( tmp->next && strcmp(tmp->next->path, CurBuffy->path) ) tmp = tmp->next;
302 case OP_SIDEBAR_SCROLL_UP:
304 if ( CurBuffy != Incoming ) {
305 calc_boundaries(menu);
306 CurBuffy = CurBuffy->prev;
309 case OP_SIDEBAR_SCROLL_DOWN:
310 CurBuffy = BottomBuffy;
311 if ( CurBuffy->next ) {
312 calc_boundaries(menu);
313 CurBuffy = CurBuffy->next;
319 calc_boundaries(menu);