Link Here
|
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 |
#if HAVE_CONFIG_H |
22 |
# include "config.h" |
23 |
#endif |
24 |
|
25 |
#include "mutt.h" |
26 |
#include "mutt_menu.h" |
27 |
#include "mutt_curses.h" |
28 |
#include "sidebar.h" |
29 |
#include "buffy.h" |
30 |
#include <libgen.h> |
31 |
#include "keymap.h" |
32 |
#include <stdbool.h> |
33 |
|
34 |
/*BUFFY *CurBuffy = 0;*/ |
35 |
static BUFFY *TopBuffy = 0; |
36 |
static BUFFY *BottomBuffy = 0; |
37 |
static int known_lines = 0; |
38 |
|
39 |
void calc_boundaries() { |
40 |
|
41 |
BUFFY *tmp = Incoming; |
42 |
|
43 |
int count = LINES - 2 - (option(OPTHELP) ? 1 : 0); |
44 |
|
45 |
if ( known_lines != LINES ) { |
46 |
TopBuffy = BottomBuffy = 0; |
47 |
known_lines = LINES; |
48 |
} |
49 |
for ( ; tmp->next != 0; tmp = tmp->next ) |
50 |
tmp->next->prev = tmp; |
51 |
|
52 |
if ( TopBuffy == 0 && BottomBuffy == 0 ) |
53 |
TopBuffy = Incoming; |
54 |
if ( BottomBuffy == 0 ) { |
55 |
BottomBuffy = TopBuffy; |
56 |
while ( --count && BottomBuffy->next ) |
57 |
BottomBuffy = BottomBuffy->next; |
58 |
} |
59 |
else if ( TopBuffy == CurBuffy->next ) { |
60 |
BottomBuffy = CurBuffy; |
61 |
tmp = BottomBuffy; |
62 |
while ( --count && tmp->prev) |
63 |
tmp = tmp->prev; |
64 |
TopBuffy = tmp; |
65 |
} |
66 |
else if ( BottomBuffy == CurBuffy->prev ) { |
67 |
TopBuffy = CurBuffy; |
68 |
tmp = TopBuffy; |
69 |
while ( --count && tmp->next ) |
70 |
tmp = tmp->next; |
71 |
BottomBuffy = tmp; |
72 |
} |
73 |
} |
74 |
|
75 |
static const char * |
76 |
sidebar_format_str (char *dest, |
77 |
size_t destlen, |
78 |
size_t col, |
79 |
char op, |
80 |
const char *src, |
81 |
const char *prefix, |
82 |
const char *ifstring, |
83 |
const char *elsestring, |
84 |
unsigned long data, |
85 |
format_flag flags) |
86 |
{ |
87 |
/* casting from unsigned long - srsly?! */ |
88 |
struct sidebar_entry *sbe = (struct sidebar_entry *) data; |
89 |
unsigned int optional; |
90 |
char fmt[SHORT_STRING], buf[SHORT_STRING]; |
91 |
|
92 |
optional = flags & M_FORMAT_OPTIONAL; |
93 |
|
94 |
switch(op) { |
95 |
case 'F': |
96 |
if(!optional) { |
97 |
snprintf (fmt, sizeof (fmt), "%%%sd", prefix); |
98 |
snprintf (dest, destlen, fmt, sbe->flagged); |
99 |
} else if(sbe->flagged == 0) { |
100 |
optional = 0; |
101 |
} |
102 |
break; |
103 |
|
104 |
case '!': |
105 |
if(sbe->flagged == 0) |
106 |
mutt_format_s(dest, destlen, prefix, ""); |
107 |
if(sbe->flagged == 1) |
108 |
mutt_format_s(dest, destlen, prefix, "!"); |
109 |
if(sbe->flagged == 2) |
110 |
mutt_format_s(dest, destlen, prefix, "!!"); |
111 |
if(sbe->flagged > 2) { |
112 |
snprintf (buf, sizeof (buf), "%d!", sbe->flagged); |
113 |
mutt_format_s(dest, destlen, prefix, buf); |
114 |
} |
115 |
break; |
116 |
|
117 |
case 'S': |
118 |
if(!optional) { |
119 |
snprintf (fmt, sizeof (fmt), "%%%sd", prefix); |
120 |
snprintf (dest, destlen, fmt, sbe->size); |
121 |
} else if (sbe->size == 0) { |
122 |
optional = 0; |
123 |
} |
124 |
break; |
125 |
|
126 |
case 'N': |
127 |
if(!optional) { |
128 |
snprintf (fmt, sizeof (fmt), "%%%sd", prefix); |
129 |
snprintf (dest, destlen, fmt, sbe->new); |
130 |
} else if(sbe->new == 0) { |
131 |
optional = 0; |
132 |
} |
133 |
break; |
134 |
|
135 |
case 'B': |
136 |
mutt_format_s(dest, destlen, prefix, sbe->box); |
137 |
break; |
138 |
} |
139 |
|
140 |
if(optional) |
141 |
mutt_FormatString (dest, destlen, col, ifstring, sidebar_format_str, (unsigned long) sbe, flags); |
142 |
else if (flags & M_FORMAT_OPTIONAL) |
143 |
mutt_FormatString (dest, destlen, col, elsestring, sidebar_format_str, (unsigned long) sbe, flags); |
144 |
|
145 |
return (src); |
146 |
} |
147 |
|
148 |
char *make_sidebar_entry(char *box, unsigned int size, unsigned int new, unsigned int flagged) { |
149 |
static char *entry = 0; |
150 |
struct sidebar_entry sbe; |
151 |
int SBvisual; |
152 |
|
153 |
SBvisual = SidebarWidth - strlen(SidebarDelim); |
154 |
if (SBvisual < 1) |
155 |
return NULL; |
156 |
|
157 |
sbe.new = new; |
158 |
sbe.flagged = flagged; |
159 |
sbe.size = size; |
160 |
strncpy(sbe.box, box, SHORT_STRING-1); |
161 |
|
162 |
safe_realloc(&entry, SBvisual + 2); |
163 |
entry[SBvisual + 1] = '\0'; |
164 |
|
165 |
mutt_FormatString (entry, SBvisual+1, 0, SidebarFormat, sidebar_format_str, (unsigned long) &sbe, 0); |
166 |
|
167 |
return entry; |
168 |
} |
169 |
|
170 |
void set_curbuffy(char buf[LONG_STRING]) |
171 |
{ |
172 |
BUFFY* tmp = CurBuffy = Incoming; |
173 |
|
174 |
if (!Incoming) |
175 |
return; |
176 |
|
177 |
while(1) { |
178 |
if(!strcmp(tmp->path, buf) || !strcmp(tmp->realpath, buf)) { |
179 |
CurBuffy = tmp; |
180 |
break; |
181 |
} |
182 |
|
183 |
if(tmp->next) |
184 |
tmp = tmp->next; |
185 |
else |
186 |
break; |
187 |
} |
188 |
} |
189 |
|
190 |
int draw_sidebar(int menu) { |
191 |
|
192 |
BUFFY *tmp; |
193 |
#ifndef USE_SLANG_CURSES |
194 |
attr_t attrs; |
195 |
#endif |
196 |
short delim_len = strlen(SidebarDelim); |
197 |
short color_pair; |
198 |
|
199 |
static bool initialized = false; |
200 |
static int prev_show_value; |
201 |
static short saveSidebarWidth; |
202 |
int lines = 0; |
203 |
int SidebarHeight; |
204 |
|
205 |
if(option(OPTSTATUSONTOP) || option(OPTHELP)) |
206 |
lines++; /* either one will occupy the first line */ |
207 |
|
208 |
/* initialize first time */ |
209 |
if(!initialized) { |
210 |
prev_show_value = option(OPTSIDEBAR); |
211 |
saveSidebarWidth = SidebarWidth; |
212 |
if(!option(OPTSIDEBAR)) SidebarWidth = 0; |
213 |
initialized = true; |
214 |
} |
215 |
|
216 |
/* save or restore the value SidebarWidth */ |
217 |
if(prev_show_value != option(OPTSIDEBAR)) { |
218 |
if(prev_show_value && !option(OPTSIDEBAR)) { |
219 |
saveSidebarWidth = SidebarWidth; |
220 |
SidebarWidth = 0; |
221 |
} else if(!prev_show_value && option(OPTSIDEBAR)) { |
222 |
mutt_buffy_check(1); /* we probably have bad or no numbers */ |
223 |
SidebarWidth = saveSidebarWidth; |
224 |
} |
225 |
prev_show_value = option(OPTSIDEBAR); |
226 |
} |
227 |
|
228 |
|
229 |
/* if ( SidebarWidth == 0 ) return 0; */ |
230 |
if (SidebarWidth > 0 && option (OPTSIDEBAR) |
231 |
&& delim_len >= SidebarWidth) { |
232 |
unset_option (OPTSIDEBAR); |
233 |
/* saveSidebarWidth = SidebarWidth; */ |
234 |
if (saveSidebarWidth > delim_len) { |
235 |
SidebarWidth = saveSidebarWidth; |
236 |
mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar.")); |
237 |
sleep (2); |
238 |
} else { |
239 |
SidebarWidth = 0; |
240 |
mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar. Please set your sidebar_width to a sane value.")); |
241 |
sleep (4); /* the advise to set a sane value should be seen long enough */ |
242 |
} |
243 |
saveSidebarWidth = 0; |
244 |
return (0); |
245 |
} |
246 |
|
247 |
if ( SidebarWidth == 0 || !option(OPTSIDEBAR)) { |
248 |
if (SidebarWidth > 0) { |
249 |
saveSidebarWidth = SidebarWidth; |
250 |
SidebarWidth = 0; |
251 |
} |
252 |
unset_option(OPTSIDEBAR); |
253 |
return 0; |
254 |
} |
255 |
|
256 |
/* get attributes for divider */ |
257 |
SETCOLOR(MT_COLOR_STATUS); |
258 |
#ifndef USE_SLANG_CURSES |
259 |
attr_get(&attrs, &color_pair, 0); |
260 |
#else |
261 |
color_pair = attr_get(); |
262 |
#endif |
263 |
SETCOLOR(MT_COLOR_NORMAL); |
264 |
|
265 |
/* draw the divider */ |
266 |
|
267 |
SidebarHeight = LINES - 1; |
268 |
if(option(OPTHELP) || !option(OPTSTATUSONTOP)) |
269 |
SidebarHeight--; |
270 |
|
271 |
for ( ; lines < SidebarHeight; lines++ ) { |
272 |
move(lines, SidebarWidth - delim_len); |
273 |
addstr(NONULL(SidebarDelim)); |
274 |
#ifndef USE_SLANG_CURSES |
275 |
mvchgat(lines, SidebarWidth - delim_len, delim_len, 0, color_pair, NULL); |
276 |
#endif |
277 |
} |
278 |
|
279 |
if ( Incoming == 0 ) return 0; |
280 |
lines = 0; |
281 |
if(option(OPTSTATUSONTOP) || option(OPTHELP)) |
282 |
lines++; /* either one will occupy the first line */ |
283 |
|
284 |
if ( known_lines != LINES || TopBuffy == 0 || BottomBuffy == 0 ) |
285 |
calc_boundaries(menu); |
286 |
if ( CurBuffy == 0 ) CurBuffy = Incoming; |
287 |
|
288 |
tmp = TopBuffy; |
289 |
|
290 |
SETCOLOR(MT_COLOR_NORMAL); |
291 |
|
292 |
for ( ; tmp && lines < SidebarHeight; tmp = tmp->next ) { |
293 |
if ( tmp == CurBuffy ) |
294 |
SETCOLOR(MT_COLOR_INDICATOR); |
295 |
else if ( tmp->msg_unread > 0 ) |
296 |
SETCOLOR(MT_COLOR_NEW); |
297 |
else if ( tmp->msg_flagged > 0 ) |
298 |
SETCOLOR(MT_COLOR_FLAGGED); |
299 |
else |
300 |
SETCOLOR(MT_COLOR_NORMAL); |
301 |
|
302 |
move( lines, 0 ); |
303 |
if ( Context && Context->path && |
304 |
(!strcmp(tmp->path, Context->path)|| |
305 |
!strcmp(tmp->realpath, Context->path)) ) { |
306 |
tmp->msg_unread = Context->unread; |
307 |
tmp->msgcount = Context->msgcount; |
308 |
tmp->msg_flagged = Context->flagged; |
309 |
} |
310 |
/* check whether Maildir is a prefix of the current folder's path */ |
311 |
short maildir_is_prefix = 0; |
312 |
if ( (strlen(tmp->path) > strlen(Maildir)) && |
313 |
(strncmp(Maildir, tmp->path, strlen(Maildir)) == 0) ) |
314 |
maildir_is_prefix = 1; |
315 |
/* calculate depth of current folder and generate its display name with indented spaces */ |
316 |
int sidebar_folder_depth = 0; |
317 |
char *sidebar_folder_name; |
318 |
sidebar_folder_name = option(OPTSIDEBARSHORTPATH) ? mutt_basename(tmp->path) : tmp->path + maildir_is_prefix*(strlen(Maildir) + ((Maildir[strlen(Maildir) - 1] == '/' || Maildir[strlen(Maildir) - 1] == '}') ? 0 : 1)); |
319 |
/* sidebar_folder_name = option(OPTSIDEBARSHORTPATH) ? mutt_basename(tmp->path) : tmp->path + maildir_is_prefix*(strlen(Maildir) + 1); */ |
320 |
if ( maildir_is_prefix && option(OPTSIDEBARFOLDERINDENT) ) { |
321 |
char *tmp_folder_name; |
322 |
char *tmp_folder_name_dot; |
323 |
char *tmp_folder_name_slash; |
324 |
int i; |
325 |
tmp_folder_name = tmp->path + strlen(Maildir) + 1; |
326 |
for (i = 0; i < strlen(tmp->path) - strlen(Maildir); i++) { |
327 |
if (tmp_folder_name[i] == '/' || tmp_folder_name[i] == '.') sidebar_folder_depth++; |
328 |
} |
329 |
if (sidebar_folder_depth > 0) { |
330 |
if (option(OPTSIDEBARSHORTPATH)) { |
331 |
tmp_folder_name_dot = strrchr(tmp->path, '.'); |
332 |
tmp_folder_name_slash = strrchr(tmp->path, '/'); |
333 |
if (tmp_folder_name_dot == NULL && tmp_folder_name_slash == NULL) |
334 |
tmp_folder_name = mutt_basename(tmp->path); |
335 |
tmp_folder_name = strrchr(tmp->path, '.'); |
336 |
if (tmp_folder_name == NULL) |
337 |
tmp_folder_name = mutt_basename(tmp->path); |
338 |
else if (tmp_folder_name_dot > tmp_folder_name_slash) |
339 |
tmp_folder_name = tmp_folder_name_dot + 1; |
340 |
else |
341 |
tmp_folder_name = tmp_folder_name_slash + 1; |
342 |
} |
343 |
else |
344 |
tmp_folder_name = tmp->path + strlen(Maildir) + ((Maildir[strlen(Maildir) - 1] == '/' || Maildir[strlen(Maildir) - 1] == '}') ? 0 : 1); |
345 |
sidebar_folder_name = malloc(strlen(tmp_folder_name) + sidebar_folder_depth*strlen(NONULL(SidebarIndentStr)) + 1); |
346 |
sidebar_folder_name[0]=0; |
347 |
for (i=0; i < sidebar_folder_depth; i++) |
348 |
strncat(sidebar_folder_name, NONULL(SidebarIndentStr), strlen(NONULL(SidebarIndentStr))); |
349 |
strncat(sidebar_folder_name, tmp_folder_name, strlen(tmp_folder_name)); |
350 |
} |
351 |
} |
352 |
printw( "%.*s", SidebarWidth - delim_len + 1, |
353 |
make_sidebar_entry(sidebar_folder_name, tmp->msgcount, |
354 |
tmp->msg_unread, tmp->msg_flagged)); |
355 |
if (sidebar_folder_depth > 0) |
356 |
free(sidebar_folder_name); |
357 |
lines++; |
358 |
} |
359 |
SETCOLOR(MT_COLOR_NORMAL); |
360 |
for ( ; lines < SidebarHeight; lines++ ) { |
361 |
int i = 0; |
362 |
move( lines, 0 ); |
363 |
for ( ; i < SidebarWidth - delim_len; i++ ) |
364 |
addch(' '); |
365 |
} |
366 |
return 0; |
367 |
} |
368 |
|
369 |
|
370 |
void set_buffystats(CONTEXT* Context) |
371 |
{ |
372 |
BUFFY *tmp = Incoming; |
373 |
while(tmp) { |
374 |
if(Context && (!strcmp(tmp->path, Context->path) || |
375 |
!strcmp(tmp->realpath, Context->path))) { |
376 |
tmp->msg_unread = Context->unread; |
377 |
tmp->msgcount = Context->msgcount; |
378 |
tmp->msg_flagged = Context->flagged; |
379 |
break; |
380 |
} |
381 |
tmp = tmp->next; |
382 |
} |
383 |
} |
384 |
|
385 |
void scroll_sidebar(int op, int menu) |
386 |
{ |
387 |
if(!SidebarWidth) return; |
388 |
if(!CurBuffy) return; |
389 |
|
390 |
switch (op) { |
391 |
case OP_SIDEBAR_NEXT: |
392 |
if ( CurBuffy->next == NULL ) return; |
393 |
CurBuffy = CurBuffy->next; |
394 |
break; |
395 |
case OP_SIDEBAR_PREV: |
396 |
if ( CurBuffy->prev == NULL ) return; |
397 |
CurBuffy = CurBuffy->prev; |
398 |
break; |
399 |
case OP_SIDEBAR_SCROLL_UP: |
400 |
CurBuffy = TopBuffy; |
401 |
if ( CurBuffy != Incoming ) { |
402 |
calc_boundaries(menu); |
403 |
CurBuffy = CurBuffy->prev; |
404 |
} |
405 |
break; |
406 |
case OP_SIDEBAR_SCROLL_DOWN: |
407 |
CurBuffy = BottomBuffy; |
408 |
if ( CurBuffy->next ) { |
409 |
calc_boundaries(menu); |
410 |
CurBuffy = CurBuffy->next; |
411 |
} |
412 |
break; |
413 |
default: |
414 |
return; |
415 |
} |
416 |
calc_boundaries(menu); |
417 |
draw_sidebar(menu); |
418 |
} |
419 |
|