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 /* CurBuffy should contain a valid buffy
50 * mailbox before calling this function!!! */
51 void calc_boundaries (int menu)
53 BUFFY *tmp = Incoming;
55 int i,count, mailbox_position;
57 /* correct known_lines if it has changed because of a window resize */
58 if ( known_lines != LINES ) {
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;
66 /* calculate the position of the current mailbox */
69 while (tmp != CurBuffy)
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 */
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;
88 static char * shortened_hierarchy(char * 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));
98 last_dot = strrchr(box,'.');
101 new_box = safe_malloc(strlen(last_dot)+2*dots+1);
103 for (i=1,j=1;i<strlen(box);++i) {
107 if (&box[i+1] != last_dot) {
108 new_box[j++] = box[i+1];
111 strcat(&new_box[j],last_dot);
118 return safe_strdup(box);
121 char *make_sidebar_entry(char *box, int size, int new, int flagged)
123 int i = 0, dlen, max, shortened = 0;
126 if (SidebarWidth > COLS)
129 dlen = mutt_strlen(SidebarDelim);
130 max = SidebarWidth - dlen - 1;
132 safe_realloc(&entry, SidebarWidth + 1);
133 entry[SidebarWidth] = 0;
134 for (; i < SidebarWidth; entry[i++] = ' ' );
136 if (ImapHomeNamespace && strlen(ImapHomeNamespace)>0) {
137 if (strncmp(box,ImapHomeNamespace,strlen(ImapHomeNamespace))==0 && strcmp(box,ImapHomeNamespace)!=0) {
138 box+=strlen(ImapHomeNamespace)+1;
142 max -= quick_log10(size);
144 max -= quick_log10(new) + 2;
146 max -= quick_log10(flagged) + 2;
147 if (option(OPTSHORTENHIERARCHY) && mutt_strlen(box) > max) {
148 box = shortened_hierarchy(box);
152 strncpy( entry, box, i < SidebarWidth - dlen ? i :SidebarWidth - dlen);
156 offset = SidebarWidth - 5 - quick_log10(size) - dlen - quick_log10(new) - quick_log10(flagged);
157 if (offset<0) offset = 0;
158 snprintf(entry + offset, SidebarWidth - dlen - offset + 1,
159 "% d(%d)[%d]", size, new, flagged);
161 offset = SidebarWidth - 3 - quick_log10(size) - dlen - quick_log10(new);
162 if (offset<0) offset = 0;
163 snprintf(entry + offset, SidebarWidth - dlen - offset + 1,
164 "% d(%d)", size, new);
168 offset = SidebarWidth - 3 - quick_log10(size) - dlen - quick_log10(flagged);
169 if (offset<0) offset = 0;
170 snprintf( entry + offset, SidebarWidth - dlen - offset + 1,
171 "% d[%d]", size,flagged);
173 offset = SidebarWidth - 1 - quick_log10(size) - dlen;
174 if (offset<0) offset = 0;
175 snprintf( entry + offset, SidebarWidth - dlen - offset + 1,
180 if (option(OPTSHORTENHIERARCHY) && shortened) {
186 void set_curbuffy(char buf[LONG_STRING])
188 BUFFY* tmp = CurBuffy = Incoming;
194 if(!strcmp(tmp->path, buf)) {
206 void set_buffystats (CONTEXT* Context)
208 BUFFY* tmp = Incoming;
213 if (strcmp (tmp->path, Context->path) == 0)
215 tmp->msg_unread = Context->unread;
216 tmp->msgcount = Context->msgcount;
217 tmp->msg_flagged = Context->flagged;
224 int draw_sidebar(int menu) {
226 int lines = option(OPTHELP) ? 1 : 0;
228 short delim_len = mutt_strlen (SidebarDelim);
230 /* initialize first time */
232 prev_show_value = option(OPTMBOXPANE);
233 saveSidebarWidth = SidebarWidth;
234 if(!option(OPTMBOXPANE)) SidebarWidth = 0;
238 /* save or restore the value SidebarWidth */
239 if(prev_show_value != option(OPTMBOXPANE)) {
240 if(prev_show_value && !option(OPTMBOXPANE)) {
241 saveSidebarWidth = SidebarWidth;
243 } else if(!prev_show_value && option(OPTMBOXPANE)) {
244 SidebarWidth = saveSidebarWidth;
245 /* after toggle: force recounting of all mail */
248 prev_show_value = option(OPTMBOXPANE);
251 if ( SidebarWidth == 0 ) return 0;
253 /* draw the divider */
254 //SETCOLOR(MT_COLOR_STATUS);
255 SETCOLOR(MT_COLOR_SIDEBAR);
256 for (lines = option (OPTSTATUSONTOP) ? 0 : 1;
257 lines < LINES-1-(menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++ ) {
258 move(lines, SidebarWidth - delim_len);
259 addstr (NONULL (SidebarDelim));
261 SETCOLOR(MT_COLOR_NORMAL);
263 if ( Incoming == 0 ) return 0;
264 lines = option(OPTHELP) ? 1 : 0; /* go back to the top */
266 if ( CurBuffy == 0 ) CurBuffy = Incoming;
267 if ( known_lines != LINES || TopBuffy == 0 || BottomBuffy == 0 )
268 calc_boundaries(menu);
272 for ( ; tmp && lines < LINES-1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); tmp = tmp->next ) {
273 if ( tmp == CurBuffy )
274 SETCOLOR(MT_COLOR_INDICATOR);
275 else if ( tmp->msg_flagged > 0 )
276 SETCOLOR(MT_COLOR_FLAGGED);
277 else if ( tmp->msg_unread > 0 )
278 SETCOLOR(MT_COLOR_NEW);
280 SETCOLOR(MT_COLOR_NORMAL);
283 if ( Context && !strcmp( tmp->path, Context->path ) ) {
284 printw( "%.*s", SidebarWidth - delim_len,
285 make_sidebar_entry(basename(tmp->path),
286 Context->msgcount, Context->unread, Context->flagged));
287 tmp->msg_unread = Context->unread;
288 tmp->msgcount = Context->msgcount;
289 tmp->msg_flagged = Context->flagged;
292 printw( "%.*s", SidebarWidth - delim_len,
293 make_sidebar_entry(basename(tmp->path),
294 tmp->msgcount,tmp->msg_unread, tmp->msg_flagged));
297 SETCOLOR(MT_COLOR_NORMAL);
298 for ( ; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++ ) {
301 for ( ; i < SidebarWidth - delim_len; i++ )
307 BUFFY * exist_next_new()
309 BUFFY *tmp = CurBuffy;
310 if(tmp == NULL) return NULL;
311 while (tmp->next != NULL)
314 if(tmp->msg_unread) return tmp;
319 BUFFY * exist_prev_new()
321 BUFFY *tmp = CurBuffy;
322 if(tmp == NULL) return NULL;
323 while (tmp->prev != NULL)
326 if(tmp->msg_unread) return tmp;
332 void scroll_sidebar(int op, int menu)
336 if(!SidebarWidth) return;
337 if(!CurBuffy) return;
340 case OP_SIDEBAR_NEXT:
341 if ( CurBuffy->next == NULL ) {
342 mutt_error (_("You are on the last mailbox."));
345 CurBuffy = CurBuffy->next;
347 case OP_SIDEBAR_NEXT_NEW:
348 if ( (tmp = exist_next_new()) == NULL) {
349 mutt_error (_("No next mailboxes with new mail."));
354 case OP_SIDEBAR_PREV:
355 if ( CurBuffy->prev == NULL ) {
356 mutt_error (_("You are on the first mailbox."));
359 CurBuffy = CurBuffy->prev;
361 case OP_SIDEBAR_PREV_NEW:
362 if ( (tmp = exist_prev_new()) == NULL) {
363 mutt_error (_("No previous mailbox with new mail."));
369 case OP_SIDEBAR_SCROLL_UP:
371 if ( CurBuffy != Incoming ) {
372 calc_boundaries(menu);
373 CurBuffy = CurBuffy->prev;
376 case OP_SIDEBAR_SCROLL_DOWN:
377 CurBuffy = BottomBuffy;
378 if ( CurBuffy->next ) {
379 calc_boundaries(menu);
380 CurBuffy = CurBuffy->next;
386 calc_boundaries(menu);