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