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, int flagged)
139 int i = 0, dlen = mutt_strlen (SidebarDelim), max = SidebarWidth-dlen-1,
142 c = realloc(entry, SidebarWidth + 1);
144 entry[SidebarWidth] = 0;
145 for (; i < SidebarWidth; entry[i++] = ' ' );
147 if (ImapHomeNamespace && strlen(ImapHomeNamespace)>0) {
148 if (strncmp(box,ImapHomeNamespace,strlen(ImapHomeNamespace))==0 && strcmp(box,ImapHomeNamespace)!=0) {
149 box+=strlen(ImapHomeNamespace)+1;
153 max -= quick_log10(size);
155 max -= quick_log10(new) + 2;
157 max -= quick_log10(flagged) + 2;
158 if (option(OPTSHORTENHIERARCHY) && mutt_strlen(box) > max) {
159 box = shortened_hierarchy(box);
163 strncpy( entry, box, i < SidebarWidth - dlen ? i :SidebarWidth - dlen);
167 sprintf(entry + SidebarWidth - 5 - quick_log10(size) - dlen - quick_log10(new) - quick_log10(flagged),
168 "% d(%d)[%d]", size, new, flagged);
170 sprintf(entry + SidebarWidth - 3 - quick_log10(size) - dlen - quick_log10(new),
171 "% d(%d)", size, new);
175 sprintf( entry + SidebarWidth - 3 - quick_log10(size) - dlen - quick_log10(flagged), "% d[%d]", size,flagged);
177 sprintf( entry + SidebarWidth - 1 - quick_log10(size) - dlen, "% d", size);
181 if (option(OPTSHORTENHIERARCHY) && shortened) {
187 void set_curbuffy(char buf[LONG_STRING])
189 BUFFY* tmp = CurBuffy = Incoming;
195 if(!strcmp(tmp->path, buf)) {
207 void set_buffystats (CONTEXT* Context)
209 BUFFY* tmp = Incoming;
214 if (strcmp (tmp->path, Context->path) == 0)
216 tmp->msg_unread = Context->unread;
217 tmp->msgcount = Context->msgcount;
218 tmp->msg_flagged = Context->flagged;
225 int draw_sidebar(int menu) {
227 int lines = option(OPTHELP) ? 1 : 0;
229 short delim_len = mutt_strlen (SidebarDelim);
231 /* initialize first time */
233 prev_show_value = option(OPTMBOXPANE);
234 saveSidebarWidth = SidebarWidth;
235 if(!option(OPTMBOXPANE)) SidebarWidth = 0;
239 /* save or restore the value SidebarWidth */
240 if(prev_show_value != option(OPTMBOXPANE)) {
241 if(prev_show_value && !option(OPTMBOXPANE)) {
242 saveSidebarWidth = SidebarWidth;
244 } else if(!prev_show_value && option(OPTMBOXPANE)) {
245 SidebarWidth = saveSidebarWidth;
247 prev_show_value = option(OPTMBOXPANE);
250 if ( SidebarWidth == 0 ) return 0;
252 /* draw the divider */
253 SETCOLOR(MT_COLOR_STATUS);
254 for (lines = option (OPTSTATUSONTOP) ? 0 : 1;
255 lines < LINES-1-(menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++ ) {
256 move(lines, SidebarWidth - delim_len);
257 addstr (NONULL (SidebarDelim));
259 SETCOLOR(MT_COLOR_NORMAL);
261 if ( Incoming == 0 ) return 0;
262 lines = option(OPTHELP) ? 1 : 0; /* go back to the top */
264 if (cur_is_hidden (LINES-1-(menu != MENU_PAGER)))
267 if ( known_lines != LINES || TopBuffy == 0 || BottomBuffy == 0 )
268 calc_boundaries(menu);
269 if ( CurBuffy == 0 ) CurBuffy = Incoming;
273 for ( ; tmp && lines < LINES-1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); tmp = tmp->next ) {
274 if ( tmp == CurBuffy )
275 SETCOLOR(MT_COLOR_INDICATOR);
276 else if ( tmp->msg_flagged > 0 )
277 SETCOLOR(MT_COLOR_FLAGGED);
278 else if ( tmp->msg_unread > 0 )
279 SETCOLOR(MT_COLOR_NEW);
281 SETCOLOR(MT_COLOR_NORMAL);
284 if ( Context && !strcmp( tmp->path, Context->path ) ) {
285 printw( "%.*s", SidebarWidth - delim_len,
286 make_sidebar_entry(basename(tmp->path),
287 Context->msgcount, Context->unread, Context->flagged));
288 tmp->msg_unread = Context->unread;
289 tmp->msgcount = Context->msgcount;
290 tmp->msg_flagged = Context->flagged;
293 printw( "%.*s", SidebarWidth - delim_len,
294 make_sidebar_entry(basename(tmp->path),
295 tmp->msgcount,tmp->msg_unread, tmp->msg_flagged));
298 SETCOLOR(MT_COLOR_NORMAL);
299 for ( ; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++ ) {
302 for ( ; i < SidebarWidth - delim_len; i++ )
308 BUFFY * exist_next_new()
310 BUFFY *tmp = CurBuffy;
311 if(tmp == NULL) return NULL;
312 while (tmp->next != NULL)
315 if(tmp->msg_unread) return tmp;
320 BUFFY * exist_prev_new()
322 BUFFY *tmp = CurBuffy;
323 if(tmp == NULL) return NULL;
324 while (tmp->prev != NULL)
327 if(tmp->msg_unread) return tmp;
333 void scroll_sidebar(int op, int menu)
337 if(!SidebarWidth) return;
338 if(!CurBuffy) return;
341 case OP_SIDEBAR_NEXT:
342 if ( CurBuffy->next == NULL ) return;
343 CurBuffy = CurBuffy->next;
345 case OP_SIDEBAR_NEXT_NEW:
346 if ( (tmp = exist_next_new()) == NULL)
348 if (CurBuffy->next == NULL) return;
349 CurBuffy = CurBuffy->next;
353 case OP_SIDEBAR_PREV:
354 if ( CurBuffy->prev == NULL ) return;
355 CurBuffy = CurBuffy->prev;
357 case OP_SIDEBAR_PREV_NEW:
358 if ( (tmp = exist_prev_new()) == NULL)
360 if(CurBuffy->prev == NULL) return;
361 CurBuffy = CurBuffy->prev;
366 case OP_SIDEBAR_SCROLL_UP:
368 if ( CurBuffy != Incoming ) {
369 calc_boundaries(menu);
370 CurBuffy = CurBuffy->prev;
373 case OP_SIDEBAR_SCROLL_DOWN:
374 CurBuffy = BottomBuffy;
375 if ( CurBuffy->next ) {
376 calc_boundaries(menu);
377 CurBuffy = CurBuffy->next;
383 calc_boundaries(menu);