24
|
1 |
#include <stdio.h> |
|
2 |
#include <stdlib.h> |
|
3 |
#include <string.h> |
|
4 |
#include <ncurses.h> |
|
5 |
#include <panel.h> |
|
6 |
#include <time.h> |
|
7 |
#include <ctype.h> |
|
8 |
#include <locale.h> |
232
|
9 |
#include <langinfo.h> |
24
|
10 |
|
|
11 |
#include "screen.h" |
81
|
12 |
#include "hbuf.h" |
47
|
13 |
#include "commands.h" |
95
|
14 |
#include "compl.h" |
81
|
15 |
#include "roster.h" |
180
|
16 |
#include "histolog.h" |
24
|
17 |
#include "parsecfg.h" |
81
|
18 |
#include "utils.h" |
24
|
19 |
#include "list.h" |
|
20 |
|
|
21 |
#define window_entry(n) list_entry(n, window_entry_t, list) |
|
22 |
|
151
|
23 |
inline void check_offset(int); |
|
24 |
|
24
|
25 |
LIST_HEAD(window_list); |
|
26 |
|
|
27 |
typedef struct _window_entry_t { |
|
28 |
WINDOW *win; |
108
|
29 |
PANEL *panel; |
|
30 |
char *name; |
|
31 |
GList *hbuf; |
181
|
32 |
GList *top; // If top is NULL, we'll display the last lines |
|
33 |
char cleared; // For ex, user has issued a /clear command... |
24
|
34 |
struct list_head list; |
|
35 |
} window_entry_t; |
|
36 |
|
|
37 |
|
|
38 |
static WINDOW *rosterWnd, *chatWnd, *inputWnd; |
|
39 |
static WINDOW *logWnd, *logWnd_border; |
|
40 |
static PANEL *rosterPanel, *chatPanel, *inputPanel; |
|
41 |
static PANEL *logPanel, *logPanel_border; |
|
42 |
static int maxY, maxX; |
|
43 |
static window_entry_t *currentWindow; |
|
44 |
|
|
45 |
static int chatmode; |
238
|
46 |
static int multimode; |
|
47 |
static char *multiline; |
30
|
48 |
int update_roster; |
232
|
49 |
int utf8_mode = 0; |
24
|
50 |
|
174
|
51 |
static char inputLine[INPUTLINE_LENGTH+1]; |
|
52 |
static char *ptr_inputline; |
|
53 |
static short int inputline_offset; |
|
54 |
static int completion_started; |
173
|
55 |
static GList *cmdhisto; |
|
56 |
static GList *cmdhisto_cur; |
174
|
57 |
static char cmdhisto_backup[INPUTLINE_LENGTH+1]; |
24
|
58 |
|
|
59 |
|
99
|
60 |
/* Functions */ |
24
|
61 |
|
74
|
62 |
int scr_WindowWidth(WINDOW * win) |
24
|
63 |
{ |
|
64 |
int x, y; |
|
65 |
getmaxyx(win, y, x); |
|
66 |
return x; |
|
67 |
} |
|
68 |
|
74
|
69 |
void scr_clear_box(WINDOW *win, int y, int x, int height, int width, int Color) |
|
70 |
{ |
|
71 |
int i, j; |
|
72 |
|
|
73 |
wattrset(win, COLOR_PAIR(Color)); |
|
74 |
for (i = 0; i < height; i++) { |
|
75 |
wmove(win, y + i, x); |
|
76 |
for (j = 0; j < width; j++) |
|
77 |
wprintw(win, " "); |
|
78 |
} |
|
79 |
} |
|
80 |
|
24
|
81 |
void scr_draw_box(WINDOW * win, int y, int x, int height, int width, |
|
82 |
int Color, chtype box, chtype border) |
|
83 |
{ |
|
84 |
int i, j; |
|
85 |
|
|
86 |
wattrset(win, COLOR_PAIR(Color)); |
|
87 |
for (i = 0; i < height; i++) { |
|
88 |
wmove(win, y + i, x); |
|
89 |
for (j = 0; j < width; j++) |
|
90 |
if (!i && !j) |
|
91 |
waddch(win, border | ACS_ULCORNER); |
|
92 |
else if (i == height - 1 && !j) |
|
93 |
waddch(win, border | ACS_LLCORNER); |
|
94 |
else if (!i && j == width - 1) |
|
95 |
waddch(win, box | ACS_URCORNER); |
|
96 |
else if (i == height - 1 && j == width - 1) |
|
97 |
waddch(win, box | ACS_LRCORNER); |
|
98 |
else if (!i) |
|
99 |
waddch(win, border | ACS_HLINE); |
|
100 |
else if (i == height - 1) |
|
101 |
waddch(win, box | ACS_HLINE); |
|
102 |
else if (!j) |
|
103 |
waddch(win, border | ACS_VLINE); |
|
104 |
else if (j == width - 1) |
|
105 |
waddch(win, box | ACS_VLINE); |
|
106 |
else |
|
107 |
waddch(win, box | ' '); |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
int FindColor(char *name) |
|
112 |
{ |
|
113 |
if (!strcmp(name, "default")) |
|
114 |
return -1; |
|
115 |
if (!strcmp(name, "black")) |
|
116 |
return COLOR_BLACK; |
|
117 |
if (!strcmp(name, "red")) |
|
118 |
return COLOR_RED; |
|
119 |
if (!strcmp(name, "green")) |
|
120 |
return COLOR_GREEN; |
|
121 |
if (!strcmp(name, "yellow")) |
|
122 |
return COLOR_YELLOW; |
|
123 |
if (!strcmp(name, "blue")) |
|
124 |
return COLOR_BLUE; |
|
125 |
if (!strcmp(name, "magenta")) |
|
126 |
return COLOR_MAGENTA; |
|
127 |
if (!strcmp(name, "cyan")) |
|
128 |
return COLOR_CYAN; |
|
129 |
if (!strcmp(name, "white")) |
|
130 |
return COLOR_WHITE; |
|
131 |
|
|
132 |
return -1; |
|
133 |
} |
|
134 |
|
|
135 |
void ParseColors(void) |
|
136 |
{ |
|
137 |
char *colors[11] = { |
|
138 |
"", "", |
|
139 |
"borderlines", |
|
140 |
"jidonline", |
139
|
141 |
"newmsg", |
24
|
142 |
"jidofflineselected", |
|
143 |
"jidoffline", |
|
144 |
"text", |
|
145 |
NULL |
|
146 |
}; |
|
147 |
|
|
148 |
char *tmp = malloc(1024); |
|
149 |
char *color1; |
|
150 |
char *background = cfg_read("color_background"); |
|
151 |
char *backselected = cfg_read("color_backselected"); |
|
152 |
int i = 0; |
|
153 |
|
|
154 |
while (colors[i]) { |
|
155 |
sprintf(tmp, "color_%s", colors[i]); |
|
156 |
color1 = cfg_read(tmp); |
|
157 |
|
|
158 |
switch (i + 1) { |
|
159 |
case 1: |
|
160 |
init_pair(1, COLOR_BLACK, COLOR_WHITE); |
|
161 |
break; |
|
162 |
case 2: |
|
163 |
init_pair(2, COLOR_WHITE, COLOR_BLACK); |
|
164 |
break; |
|
165 |
case 3: |
|
166 |
init_pair(3, FindColor(color1), FindColor(background)); |
|
167 |
break; |
|
168 |
case 4: |
|
169 |
init_pair(4, FindColor(color1), FindColor(backselected)); |
|
170 |
break; |
|
171 |
case 5: |
|
172 |
init_pair(5, FindColor(color1), FindColor(background)); |
|
173 |
break; |
|
174 |
case 6: |
|
175 |
init_pair(6, FindColor(color1), FindColor(backselected)); |
|
176 |
break; |
|
177 |
case 7: |
|
178 |
init_pair(7, FindColor(color1), FindColor(background)); |
|
179 |
break; |
|
180 |
case 8: |
|
181 |
init_pair(8, FindColor(color1), FindColor(background)); |
|
182 |
break; |
|
183 |
} |
|
184 |
i++; |
|
185 |
} |
|
186 |
} |
|
187 |
|
|
188 |
|
151
|
189 |
window_entry_t *scr_CreateBuddyPanel(const char *title, int dont_show) |
24
|
190 |
{ |
151
|
191 |
int x; |
|
192 |
int y; |
|
193 |
int lines; |
|
194 |
int cols; |
153
|
195 |
window_entry_t *tmp; |
|
196 |
|
|
197 |
do { |
|
198 |
tmp = calloc(1, sizeof(window_entry_t)); |
|
199 |
} while (!tmp); |
24
|
200 |
|
151
|
201 |
// Dimensions |
|
202 |
x = ROSTER_WIDTH; |
|
203 |
y = 0; |
|
204 |
lines = CHAT_WIN_HEIGHT; |
|
205 |
cols = maxX - ROSTER_WIDTH; |
|
206 |
|
24
|
207 |
tmp->win = newwin(lines, cols, y, x); |
154
|
208 |
while (!tmp->win) { |
|
209 |
usleep(250); |
|
210 |
tmp->win = newwin(lines, cols, y, x); |
|
211 |
} |
168
|
212 |
wbkgd(tmp->win, COLOR_PAIR(COLOR_GENERAL)); |
24
|
213 |
tmp->panel = new_panel(tmp->win); |
153
|
214 |
tmp->name = (char *) calloc(1, 96); |
|
215 |
strncpy(tmp->name, title, 96); |
24
|
216 |
|
143
|
217 |
if (!dont_show) { |
24
|
218 |
currentWindow = tmp; |
|
219 |
} else { |
|
220 |
if (currentWindow) |
|
221 |
top_panel(currentWindow->panel); |
|
222 |
else |
|
223 |
top_panel(chatPanel); |
|
224 |
} |
143
|
225 |
update_panels(); |
24
|
226 |
|
181
|
227 |
// Load buddy history from file (if enabled) |
185
|
228 |
hlog_read_history(title, &tmp->hbuf, maxX - ROSTER_WIDTH - PREFIX_WIDTH); |
181
|
229 |
|
24
|
230 |
list_add_tail(&tmp->list, &window_list); |
|
231 |
|
|
232 |
return tmp; |
|
233 |
} |
|
234 |
|
50
|
235 |
window_entry_t *scr_SearchWindow(const char *winId) |
24
|
236 |
{ |
|
237 |
struct list_head *pos, *n; |
|
238 |
window_entry_t *search_entry = NULL; |
|
239 |
|
|
240 |
list_for_each_safe(pos, n, &window_list) { |
|
241 |
search_entry = window_entry(pos); |
|
242 |
if (search_entry->name) { |
|
243 |
if (!strcasecmp(search_entry->name, winId)) { |
|
244 |
return search_entry; |
|
245 |
} |
|
246 |
} |
|
247 |
} |
|
248 |
return NULL; |
|
249 |
} |
|
250 |
|
143
|
251 |
// scr_UpdateWindow() |
|
252 |
// (Re-)Display the given chat window. |
74
|
253 |
void scr_UpdateWindow(window_entry_t *win_entry) |
|
254 |
{ |
|
255 |
int n; |
|
256 |
int width; |
184
|
257 |
hbb_line **lines, *line; |
74
|
258 |
GList *hbuf_head; |
184
|
259 |
char date[32]; |
74
|
260 |
|
108
|
261 |
width = scr_WindowWidth(win_entry->win); |
|
262 |
|
|
263 |
// Should the window be empty? |
|
264 |
if (win_entry->cleared) { |
168
|
265 |
werase(win_entry->win); |
108
|
266 |
return; |
|
267 |
} |
|
268 |
|
105
|
269 |
// win_entry->top is the top message of the screen. If it set to NULL, we |
|
270 |
// are displaying the last messages. |
|
271 |
|
74
|
272 |
// We will show the last CHAT_WIN_HEIGHT lines. |
|
273 |
// Let's find out where it begins. |
105
|
274 |
if (!win_entry->top || |
|
275 |
(g_list_position(g_list_first(win_entry->hbuf), win_entry->top) == -1)) { |
|
276 |
// Move up CHAT_WIN_HEIGHT lines |
|
277 |
win_entry->hbuf = g_list_last(win_entry->hbuf); |
|
278 |
hbuf_head = win_entry->hbuf; |
|
279 |
win_entry->top = NULL; // (Just to make sure) |
|
280 |
n = 0; |
|
281 |
while (hbuf_head && (n < CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head)) { |
|
282 |
hbuf_head = g_list_previous(hbuf_head); |
|
283 |
n++; |
|
284 |
} |
|
285 |
} else |
|
286 |
hbuf_head = win_entry->top; |
74
|
287 |
|
|
288 |
// Get the last CHAT_WIN_HEIGHT lines. |
|
289 |
lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT); |
|
290 |
|
|
291 |
// Display these lines |
|
292 |
for (n = 0; n < CHAT_WIN_HEIGHT; n++) { |
168
|
293 |
wmove(win_entry->win, n, 0); |
184
|
294 |
line = *(lines+n); |
185
|
295 |
// NOTE: update PREFIX_WIDTH if you change the date format!! |
|
296 |
// You need to set it to the whole prefix length + 1 |
184
|
297 |
if (line) { |
|
298 |
if (line->timestamp) { |
185
|
299 |
strftime(date, 35, "%m-%d %H:%M", localtime(&line->timestamp)); |
184
|
300 |
} else |
185
|
301 |
strcpy(date, " "); |
197
|
302 |
if (line->flags & HBB_PREFIX_INFO) { |
|
303 |
char dir = '*'; |
|
304 |
if (line->flags & HBB_PREFIX_IN) |
|
305 |
dir = '<'; |
|
306 |
else if (line->flags & HBB_PREFIX_OUT) |
|
307 |
dir = '>'; |
|
308 |
wprintw(win_entry->win, "%.11s *%c* ", date, dir); |
|
309 |
} else if (line->flags & HBB_PREFIX_IN) |
185
|
310 |
wprintw(win_entry->win, "%.11s <== ", date); |
184
|
311 |
else if (line->flags & HBB_PREFIX_OUT) |
185
|
312 |
wprintw(win_entry->win, "%.11s --> ", date); |
75
|
313 |
else { |
185
|
314 |
wprintw(win_entry->win, "%.11s ", date); |
75
|
315 |
} |
184
|
316 |
wprintw(win_entry->win, "%s", line->text); // line |
168
|
317 |
wclrtoeol(win_entry->win); |
184
|
318 |
g_free(line->text); |
168
|
319 |
} else { |
|
320 |
wclrtobot(win_entry->win); |
|
321 |
break; |
75
|
322 |
} |
74
|
323 |
} |
|
324 |
g_free(lines); |
|
325 |
} |
|
326 |
|
143
|
327 |
// scr_ShowWindow() |
|
328 |
// Display the chat window with the given identifier. |
50
|
329 |
void scr_ShowWindow(const char *winId) |
24
|
330 |
{ |
74
|
331 |
window_entry_t *win_entry = scr_SearchWindow(winId); |
|
332 |
|
181
|
333 |
if (!win_entry) |
180
|
334 |
win_entry = scr_CreateBuddyPanel(winId, FALSE); |
74
|
335 |
|
180
|
336 |
top_panel(win_entry->panel); |
|
337 |
currentWindow = win_entry; |
|
338 |
chatmode = TRUE; |
|
339 |
roster_msg_setflag(winId, FALSE); |
|
340 |
roster_setflags(winId, ROSTER_FLAG_LOCK, TRUE); |
|
341 |
update_roster = TRUE; |
74
|
342 |
|
180
|
343 |
// Refresh the window |
|
344 |
scr_UpdateWindow(win_entry); |
|
345 |
|
|
346 |
// Finished :) |
|
347 |
update_panels(); |
142
|
348 |
|
|
349 |
top_panel(inputPanel); |
24
|
350 |
} |
|
351 |
|
143
|
352 |
// scr_ShowBuddyWindow() |
|
353 |
// Display the chat window buffer for the current buddy. |
24
|
354 |
void scr_ShowBuddyWindow(void) |
|
355 |
{ |
105
|
356 |
const gchar *jid; |
140
|
357 |
|
105
|
358 |
if (!current_buddy) |
140
|
359 |
jid = NULL; |
|
360 |
else |
|
361 |
jid = CURRENT_JID; |
|
362 |
|
|
363 |
if (!jid) { |
|
364 |
top_panel(chatPanel); |
143
|
365 |
top_panel(inputPanel); |
140
|
366 |
currentWindow = NULL; |
105
|
367 |
return; |
140
|
368 |
} |
|
369 |
|
105
|
370 |
scr_ShowWindow(jid); |
24
|
371 |
} |
|
372 |
|
|
373 |
|
143
|
374 |
// scr_WriteInWindow() |
|
375 |
// Write some text in the winId window (this usually is a jid). |
|
376 |
// Lines are splitted when they are too long to fit in the chat window. |
|
377 |
// If this window doesn't exist, it is created. |
184
|
378 |
void scr_WriteInWindow(const char *winId, const char *text, time_t timestamp, |
|
379 |
unsigned int prefix_flags, int force_show) |
24
|
380 |
{ |
74
|
381 |
window_entry_t *win_entry; |
24
|
382 |
int dont_show = FALSE; |
|
383 |
|
74
|
384 |
// Look for the window entry. |
|
385 |
win_entry = scr_SearchWindow(winId); |
|
386 |
|
|
387 |
// Do we have to really show the window? |
24
|
388 |
if (!chatmode) |
|
389 |
dont_show = TRUE; |
74
|
390 |
else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry)))) |
24
|
391 |
dont_show = TRUE; |
|
392 |
|
74
|
393 |
// If the window entry doesn't exist yet, let's create it. |
|
394 |
if (win_entry == NULL) { |
151
|
395 |
win_entry = scr_CreateBuddyPanel(winId, dont_show); |
24
|
396 |
} |
|
397 |
|
220
|
398 |
// The message must be displayed -> update top pointer |
|
399 |
if (win_entry->cleared) |
|
400 |
win_entry->top = g_list_last(win_entry->hbuf); |
|
401 |
|
184
|
402 |
hbuf_add_line(&win_entry->hbuf, text, timestamp, prefix_flags, |
185
|
403 |
maxX - ROSTER_WIDTH - PREFIX_WIDTH); |
74
|
404 |
|
108
|
405 |
if (win_entry->cleared) { |
220
|
406 |
win_entry->cleared = FALSE; |
|
407 |
if (g_list_next(win_entry->top)) |
|
408 |
win_entry->top = g_list_next(win_entry->top); |
|
409 |
} |
|
410 |
|
|
411 |
// Make sure the last line appears in the window; update top if necessary |
|
412 |
if (win_entry->top) { |
|
413 |
int dist; |
|
414 |
GList *first = g_list_first(win_entry->hbuf); |
|
415 |
dist = g_list_position(first, g_list_last(win_entry->hbuf)) - |
|
416 |
g_list_position(first, win_entry->top); |
|
417 |
if (dist >= CHAT_WIN_HEIGHT) |
|
418 |
win_entry->top = NULL; |
108
|
419 |
} |
|
420 |
|
24
|
421 |
if (!dont_show) { |
74
|
422 |
// Show and refresh the window |
|
423 |
top_panel(win_entry->panel); |
|
424 |
scr_UpdateWindow(win_entry); |
142
|
425 |
top_panel(inputPanel); |
24
|
426 |
update_panels(); |
|
427 |
doupdate(); |
|
428 |
} else { |
148
|
429 |
roster_msg_setflag(winId, TRUE); |
30
|
430 |
update_roster = TRUE; |
24
|
431 |
} |
|
432 |
} |
|
433 |
|
|
434 |
void scr_InitCurses(void) |
|
435 |
{ |
|
436 |
initscr(); |
|
437 |
noecho(); |
|
438 |
raw(); |
35
|
439 |
halfdelay(5); |
24
|
440 |
start_color(); |
|
441 |
use_default_colors(); |
|
442 |
|
|
443 |
ParseColors(); |
|
444 |
|
|
445 |
getmaxyx(stdscr, maxY, maxX); |
167
|
446 |
if (maxY < LOG_WIN_HEIGHT+2) |
|
447 |
maxY = LOG_WIN_HEIGHT+2; |
24
|
448 |
inputLine[0] = 0; |
|
449 |
ptr_inputline = inputLine; |
|
450 |
|
35
|
451 |
setlocale(LC_CTYPE, ""); |
232
|
452 |
utf8_mode = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0); |
24
|
453 |
|
|
454 |
return; |
|
455 |
} |
|
456 |
|
81
|
457 |
void scr_TerminateCurses(void) |
|
458 |
{ |
|
459 |
clear(); |
|
460 |
refresh(); |
|
461 |
endwin(); |
|
462 |
return; |
|
463 |
} |
|
464 |
|
151
|
465 |
// scr_DrawMainWindow() |
157
|
466 |
// Set fullinit to TRUE to also create panels. Set it to FALSE for a resize. |
151
|
467 |
// |
|
468 |
// I think it could be improved a _lot_ but I'm really not an ncurses |
|
469 |
// expert... :-\ Mikael. |
|
470 |
// |
|
471 |
void scr_DrawMainWindow(unsigned int fullinit) |
24
|
472 |
{ |
157
|
473 |
if (fullinit) { |
|
474 |
/* Create windows */ |
|
475 |
rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0); |
|
476 |
chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH); |
|
477 |
logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0); |
|
478 |
logWnd = newwin(LOG_WIN_HEIGHT-2, maxX-2, CHAT_WIN_HEIGHT+1, 1); |
|
479 |
inputWnd = newwin(1, maxX, maxY-1, 0); |
168
|
480 |
wbkgd(rosterWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
481 |
wbkgd(chatWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
482 |
wbkgd(logWnd_border, COLOR_PAIR(COLOR_GENERAL)); |
|
483 |
wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL)); |
157
|
484 |
} else { |
|
485 |
/* Resize windows */ |
|
486 |
wresize(rosterWnd, CHAT_WIN_HEIGHT, ROSTER_WIDTH); |
|
487 |
wresize(chatWnd, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH); |
|
488 |
|
|
489 |
wresize(logWnd_border, LOG_WIN_HEIGHT, maxX); |
|
490 |
wresize(logWnd, LOG_WIN_HEIGHT-2, maxX-2); |
|
491 |
mvwin(logWnd_border, CHAT_WIN_HEIGHT, 0); |
|
492 |
mvwin(logWnd, CHAT_WIN_HEIGHT+1, 1); |
|
493 |
|
|
494 |
wresize(inputWnd, 1, maxX); |
|
495 |
mvwin(inputWnd, maxY-1, 0); |
168
|
496 |
|
|
497 |
werase(chatWnd); |
157
|
498 |
} |
151
|
499 |
|
|
500 |
/* Draw/init windows */ |
|
501 |
|
74
|
502 |
mvwprintw(chatWnd, 0, 0, "This is the status window"); |
24
|
503 |
|
151
|
504 |
// - Draw/clear the log window |
24
|
505 |
scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0); |
157
|
506 |
// Auto-scrolling in log window |
74
|
507 |
scrollok(logWnd, TRUE); |
24
|
508 |
|
|
509 |
|
151
|
510 |
if (fullinit) { |
157
|
511 |
// Enable keypad (+ special keys) |
|
512 |
keypad(inputWnd, TRUE); |
|
513 |
|
151
|
514 |
// Create panels |
|
515 |
rosterPanel = new_panel(rosterWnd); |
|
516 |
chatPanel = new_panel(chatWnd); |
|
517 |
logPanel_border = new_panel(logWnd_border); |
|
518 |
logPanel = new_panel(logWnd); |
|
519 |
inputPanel = new_panel(inputWnd); |
232
|
520 |
|
|
521 |
if (utf8_mode) |
|
522 |
scr_LogPrint("WARNING: UTF-8 not yet supported!"); |
157
|
523 |
} else { |
|
524 |
// Update panels |
|
525 |
replace_panel(rosterPanel, rosterWnd); |
|
526 |
replace_panel(chatPanel, chatWnd); |
|
527 |
replace_panel(logPanel, logWnd); |
|
528 |
replace_panel(logPanel_border, logWnd_border); |
|
529 |
replace_panel(inputPanel, inputWnd); |
151
|
530 |
} |
|
531 |
|
|
532 |
// We'll need to redraw the roster |
149
|
533 |
update_roster = TRUE; |
24
|
534 |
return; |
|
535 |
} |
|
536 |
|
151
|
537 |
// scr_Resize() |
|
538 |
// Function called when the window is resized. |
157
|
539 |
// - Resize windows |
151
|
540 |
// - Rewrap lines in each buddy buffer |
|
541 |
void scr_Resize() |
|
542 |
{ |
|
543 |
struct list_head *pos, *n; |
|
544 |
window_entry_t *search_entry; |
|
545 |
int x, y, lines, cols; |
|
546 |
|
|
547 |
// First, update the global variables |
|
548 |
getmaxyx(stdscr, maxY, maxX); |
167
|
549 |
if (maxY < LOG_WIN_HEIGHT+2) |
|
550 |
maxY = LOG_WIN_HEIGHT+2; |
151
|
551 |
// Make sure the cursor stays inside the window |
|
552 |
check_offset(0); |
|
553 |
|
157
|
554 |
// Resize windows and update panels |
151
|
555 |
scr_DrawMainWindow(FALSE); |
|
556 |
|
|
557 |
// Resize all buddy windows |
|
558 |
x = ROSTER_WIDTH; |
|
559 |
y = 0; |
|
560 |
lines = CHAT_WIN_HEIGHT; |
|
561 |
cols = maxX - ROSTER_WIDTH; |
|
562 |
|
|
563 |
list_for_each_safe(pos, n, &window_list) { |
|
564 |
search_entry = window_entry(pos); |
|
565 |
if (search_entry->win) { |
189
|
566 |
GList *rescue_top; |
157
|
567 |
// Resize buddy window (no need to move it) |
|
568 |
wresize(search_entry->win, lines, cols); |
168
|
569 |
werase(search_entry->win); |
151
|
570 |
// If a panel exists, replace the old window with the new |
|
571 |
if (search_entry->panel) { |
|
572 |
replace_panel(search_entry->panel, search_entry->win); |
|
573 |
} |
|
574 |
// Redo line wrapping |
189
|
575 |
rescue_top = hbuf_previous_persistent(search_entry->top); |
151
|
576 |
hbuf_rebuild(&search_entry->hbuf, |
185
|
577 |
maxX - ROSTER_WIDTH - PREFIX_WIDTH); |
189
|
578 |
if (g_list_position(g_list_first(search_entry->hbuf), search_entry->top) == -1) |
|
579 |
search_entry->top = rescue_top; |
151
|
580 |
} |
|
581 |
} |
|
582 |
|
|
583 |
// Refresh current buddy window |
157
|
584 |
if (chatmode) |
151
|
585 |
scr_ShowBuddyWindow(); |
|
586 |
} |
|
587 |
|
143
|
588 |
// scr_DrawRoster() |
|
589 |
// Actually, display the buddylist on the screen. |
81
|
590 |
void scr_DrawRoster(void) |
24
|
591 |
{ |
81
|
592 |
static guint offset = 0; |
|
593 |
char name[ROSTER_WIDTH]; |
|
594 |
int maxx, maxy; |
|
595 |
GList *buddy; |
|
596 |
int i, n; |
|
597 |
int rOffset; |
164
|
598 |
enum imstatus currentstatus = jb_getstatus(); |
81
|
599 |
|
123
|
600 |
// We can reset update_roster |
|
601 |
update_roster = FALSE; |
|
602 |
|
81
|
603 |
getmaxyx(rosterWnd, maxy, maxx); |
|
604 |
maxx --; // last char is for vertical border |
|
605 |
name[ROSTER_WIDTH-7] = 0; |
|
606 |
|
|
607 |
// cleanup of roster window |
168
|
608 |
werase(rosterWnd); |
|
609 |
// Redraw the vertical line (not very good...) |
81
|
610 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL)); |
168
|
611 |
for (i=0 ; i < CHAT_WIN_HEIGHT ; i++) |
|
612 |
mvwaddch(rosterWnd, i, ROSTER_WIDTH-1, ACS_VLINE); |
81
|
613 |
|
|
614 |
// Leave now if buddylist is empty |
|
615 |
if (!buddylist) { |
|
616 |
offset = 0; |
123
|
617 |
update_panels(); |
|
618 |
doupdate(); |
81
|
619 |
return; |
|
620 |
} |
|
621 |
|
84
|
622 |
// Update offset if necessary |
|
623 |
i = g_list_position(buddylist, current_buddy); |
|
624 |
if (i == -1) { // This is bad |
|
625 |
scr_LogPrint("Doh! Can't find current selected buddy!!"); |
|
626 |
return; |
|
627 |
} else if (i < offset) { |
|
628 |
offset = i; |
|
629 |
} else if (i+1 > offset + maxy) { |
|
630 |
offset = i + 1 - maxy; |
|
631 |
} |
81
|
632 |
|
|
633 |
buddy = buddylist; |
|
634 |
rOffset = offset; |
|
635 |
|
84
|
636 |
for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) { |
81
|
637 |
|
|
638 |
char status = '?'; |
|
639 |
char pending = ' '; |
|
640 |
enum imstatus budstate; |
149
|
641 |
unsigned short ismsg = buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG; |
|
642 |
unsigned short isgrp = buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP; |
|
643 |
unsigned short ishid = buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_HIDE; |
81
|
644 |
|
|
645 |
if (rOffset > 0) { |
|
646 |
rOffset--; |
|
647 |
continue; |
|
648 |
} |
|
649 |
|
149
|
650 |
// Display message notice if there is a message flag, but not |
|
651 |
// for unfolded groups. |
|
652 |
if (ismsg && (!isgrp || ishid)) { |
81
|
653 |
pending = '#'; |
|
654 |
} |
|
655 |
|
|
656 |
budstate = buddy_getstatus(BUDDATA(buddy)); |
164
|
657 |
if (budstate >= 0 && budstate < imstatus_size && currentstatus != offline) |
81
|
658 |
status = imstatus2char[budstate]; |
|
659 |
if (buddy == current_buddy) { |
|
660 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL)); |
|
661 |
// The 3 following lines aim to color the whole line |
|
662 |
wmove(rosterWnd, i, 0); |
|
663 |
for (n = 0; n < maxx; n++) |
|
664 |
waddch(rosterWnd, ' '); |
|
665 |
} else { |
149
|
666 |
if (pending == '#') |
139
|
667 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_NMSG)); |
|
668 |
else |
|
669 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES)); |
81
|
670 |
} |
|
671 |
|
|
672 |
strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7); |
149
|
673 |
if (isgrp) { |
133
|
674 |
char *sep; |
149
|
675 |
if (ishid) |
133
|
676 |
sep = "+++"; |
|
677 |
else |
|
678 |
sep = "---"; |
|
679 |
mvwprintw(rosterWnd, i, 0, " %c%s %s", pending, sep, name); |
|
680 |
} |
126
|
681 |
else |
|
682 |
mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name); |
84
|
683 |
|
|
684 |
i++; |
81
|
685 |
} |
|
686 |
|
142
|
687 |
top_panel(inputPanel); |
81
|
688 |
update_panels(); |
|
689 |
doupdate(); |
24
|
690 |
} |
|
691 |
|
184
|
692 |
void scr_WriteMessage(const char *jid, const char *text, time_t timestamp, |
|
693 |
guint prefix_flags) |
24
|
694 |
{ |
184
|
695 |
if (!timestamp) timestamp = time(NULL); |
|
696 |
|
|
697 |
scr_WriteInWindow(jid, text, timestamp, prefix_flags, FALSE); |
47
|
698 |
} |
|
699 |
|
190
|
700 |
// If prefix is NULL, HBB_PREFIX_IN is supposed. |
184
|
701 |
void scr_WriteIncomingMessage(const char *jidfrom, const char *text, |
190
|
702 |
time_t timestamp, guint prefix) |
47
|
703 |
{ |
190
|
704 |
if (!prefix) prefix = HBB_PREFIX_IN; |
75
|
705 |
// FIXME expand tabs / filter out special chars... |
190
|
706 |
scr_WriteMessage(jidfrom, text, timestamp, prefix); |
24
|
707 |
update_panels(); |
|
708 |
doupdate(); |
|
709 |
} |
|
710 |
|
50
|
711 |
void scr_WriteOutgoingMessage(const char *jidto, const char *text) |
47
|
712 |
{ |
184
|
713 |
scr_WriteMessage(jidto, text, 0, HBB_PREFIX_OUT); |
47
|
714 |
scr_ShowWindow(jidto); |
|
715 |
} |
|
716 |
|
24
|
717 |
int scr_Getch(void) |
|
718 |
{ |
|
719 |
int ch; |
|
720 |
ch = wgetch(inputWnd); |
|
721 |
return ch; |
|
722 |
} |
|
723 |
|
|
724 |
WINDOW *scr_GetRosterWindow(void) |
|
725 |
{ |
|
726 |
return rosterWnd; |
|
727 |
} |
|
728 |
|
|
729 |
WINDOW *scr_GetStatusWindow(void) |
|
730 |
{ |
|
731 |
return chatWnd; |
|
732 |
} |
|
733 |
|
|
734 |
WINDOW *scr_GetInputWindow(void) |
|
735 |
{ |
|
736 |
return inputWnd; |
|
737 |
} |
|
738 |
|
143
|
739 |
// scr_RosterTop() |
|
740 |
// Go to the first buddy in the buddylist |
105
|
741 |
void scr_RosterTop(void) |
104
|
742 |
{ |
143
|
743 |
enum imstatus prev_st = imstatus_size; // undef |
119
|
744 |
|
|
745 |
if (current_buddy) { |
|
746 |
prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
|
747 |
if (chatmode) |
|
748 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
|
749 |
} |
104
|
750 |
current_buddy = buddylist; |
119
|
751 |
if (chatmode && current_buddy) |
|
752 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
753 |
|
|
754 |
// We should rebuild the buddylist but not everytime |
143
|
755 |
// Here we check if we were locking a buddy who is actually offline, |
|
756 |
// and hide_offline_buddies is TRUE. In which case we need to rebuild. |
120
|
757 |
if (current_buddy && prev_st == offline && |
|
758 |
buddylist_get_hide_offline_buddies()) |
119
|
759 |
buddylist_build(); |
104
|
760 |
if (chatmode) |
|
761 |
scr_ShowBuddyWindow(); |
143
|
762 |
update_roster = TRUE; |
104
|
763 |
} |
|
764 |
|
143
|
765 |
// scr_RosterBottom() |
|
766 |
// Go to the last buddy in the buddylist |
105
|
767 |
void scr_RosterBottom(void) |
104
|
768 |
{ |
143
|
769 |
enum imstatus prev_st = imstatus_size; // undef |
119
|
770 |
|
|
771 |
if (current_buddy) { |
|
772 |
prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
|
773 |
if (chatmode) |
|
774 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
|
775 |
} |
104
|
776 |
current_buddy = g_list_last(buddylist); |
143
|
777 |
// Lock the buddy in the buddylist if we're in chat mode |
119
|
778 |
if (chatmode && current_buddy) |
|
779 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
780 |
|
|
781 |
// We should rebuild the buddylist but not everytime |
143
|
782 |
// Here we check if we were locking a buddy who is actually offline, |
|
783 |
// and hide_offline_buddies is TRUE. In which case we need to rebuild. |
120
|
784 |
if (current_buddy && prev_st == offline && |
|
785 |
buddylist_get_hide_offline_buddies()) |
119
|
786 |
buddylist_build(); |
143
|
787 |
|
104
|
788 |
if (chatmode) |
|
789 |
scr_ShowBuddyWindow(); |
143
|
790 |
update_roster = TRUE; |
104
|
791 |
} |
|
792 |
|
143
|
793 |
// scr_RosterUp() |
|
794 |
// Go to the previous buddy in the buddylist |
105
|
795 |
void scr_RosterUp(void) |
81
|
796 |
{ |
143
|
797 |
enum imstatus prev_st = imstatus_size; // undef |
119
|
798 |
|
81
|
799 |
if (current_buddy) { |
|
800 |
if (g_list_previous(current_buddy)) { |
120
|
801 |
prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
119
|
802 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
81
|
803 |
current_buddy = g_list_previous(current_buddy); |
143
|
804 |
// Lock the buddy in the buddylist if we're in chat mode |
119
|
805 |
if (chatmode) |
|
806 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
807 |
// We should rebuild the buddylist but not everytime |
143
|
808 |
// Here we check if we were locking a buddy who is actually offline, |
|
809 |
// and hide_offline_buddies is TRUE. In which case we need to rebuild. |
120
|
810 |
if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
119
|
811 |
buddylist_build(); |
143
|
812 |
update_roster = TRUE; |
81
|
813 |
} |
|
814 |
} |
104
|
815 |
|
|
816 |
if (chatmode) |
|
817 |
scr_ShowBuddyWindow(); |
81
|
818 |
} |
|
819 |
|
143
|
820 |
// scr_RosterDown() |
|
821 |
// Go to the next buddy in the buddylist |
105
|
822 |
void scr_RosterDown(void) |
81
|
823 |
{ |
143
|
824 |
enum imstatus prev_st = imstatus_size; // undef |
119
|
825 |
|
81
|
826 |
if (current_buddy) { |
|
827 |
if (g_list_next(current_buddy)) { |
120
|
828 |
prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
119
|
829 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
81
|
830 |
current_buddy = g_list_next(current_buddy); |
119
|
831 |
if (chatmode) |
|
832 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
833 |
// We should rebuild the buddylist but not everytime |
143
|
834 |
// Here we check if we were locking a buddy who is actually offline, |
|
835 |
// and hide_offline_buddies is TRUE. In which case we need to rebuild. |
120
|
836 |
if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
119
|
837 |
buddylist_build(); |
143
|
838 |
update_roster = TRUE; |
81
|
839 |
} |
|
840 |
} |
104
|
841 |
|
|
842 |
if (chatmode) |
|
843 |
scr_ShowBuddyWindow(); |
81
|
844 |
} |
|
845 |
|
236
|
846 |
// scr_RosterUnreadMessage(next) |
|
847 |
// Go to a new message. If next is not null, try to go to the next new |
|
848 |
// message. If it is not possible or if next is NULL, go to the first new |
|
849 |
// message from unread_list. |
|
850 |
void scr_RosterUnreadMessage(int next) |
|
851 |
{ |
|
852 |
enum imstatus prev_st = imstatus_size; // undef |
|
853 |
|
|
854 |
if (current_buddy) { |
|
855 |
gpointer unread_ptr; |
|
856 |
gpointer refbuddata; |
|
857 |
gpointer ngroup; |
|
858 |
GList *nbuddy; |
|
859 |
|
|
860 |
if (next) refbuddata = BUDDATA(current_buddy); |
|
861 |
else refbuddata = NULL; |
|
862 |
|
|
863 |
unread_ptr = unread_msg(refbuddata); |
|
864 |
if (!unread_ptr) return; |
|
865 |
|
|
866 |
// If buddy is in a folded group, we need to expand it |
|
867 |
ngroup = buddy_getgroup(unread_ptr); |
|
868 |
if (buddy_getflags(ngroup) & ROSTER_FLAG_HIDE) { |
|
869 |
buddy_setflags(ngroup, ROSTER_FLAG_HIDE, FALSE); |
|
870 |
buddylist_build(); |
|
871 |
} |
|
872 |
|
|
873 |
nbuddy = g_list_find(buddylist, unread_ptr); |
|
874 |
if (nbuddy) { |
|
875 |
prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
|
876 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
|
877 |
current_buddy = nbuddy; |
|
878 |
if (chatmode) |
|
879 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
880 |
// We should rebuild the buddylist but not everytime |
|
881 |
// Here we check if we were locking a buddy who is actually offline, |
|
882 |
// and hide_offline_buddies is TRUE. In which case we need to rebuild. |
|
883 |
if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
|
884 |
buddylist_build(); |
|
885 |
update_roster = TRUE; |
|
886 |
|
|
887 |
if (chatmode) scr_ShowBuddyWindow(); |
|
888 |
} else scr_LogPrint("Error: nbuddy == NULL"); |
|
889 |
} |
|
890 |
} |
|
891 |
|
143
|
892 |
// scr_ScrollUp() |
|
893 |
// Scroll up the current buddy window, half a screen. |
105
|
894 |
void scr_ScrollUp(void) |
|
895 |
{ |
|
896 |
const gchar *jid; |
|
897 |
window_entry_t *win_entry; |
|
898 |
int n, nblines; |
|
899 |
GList *hbuf_top; |
|
900 |
|
|
901 |
// Get win_entry |
|
902 |
if (!current_buddy) |
|
903 |
return; |
|
904 |
jid = CURRENT_JID; |
|
905 |
if (!jid) |
|
906 |
return; |
|
907 |
win_entry = scr_SearchWindow(jid); |
|
908 |
if (!win_entry) |
|
909 |
return; |
|
910 |
|
|
911 |
// Scroll up half a screen (or less) |
|
912 |
nblines = CHAT_WIN_HEIGHT/2-1; |
|
913 |
hbuf_top = win_entry->top; |
|
914 |
if (!hbuf_top) { |
|
915 |
hbuf_top = g_list_last(win_entry->hbuf); |
109
|
916 |
if (!win_entry->cleared) |
|
917 |
nblines *= 3; |
|
918 |
else |
|
919 |
win_entry->cleared = FALSE; |
105
|
920 |
} |
|
921 |
|
|
922 |
n = 0; |
|
923 |
while (hbuf_top && n < nblines && g_list_previous(hbuf_top)) { |
|
924 |
hbuf_top = g_list_previous(hbuf_top); |
|
925 |
n++; |
|
926 |
} |
|
927 |
win_entry->top = hbuf_top; |
|
928 |
|
|
929 |
// Refresh the window |
|
930 |
scr_UpdateWindow(win_entry); |
|
931 |
|
|
932 |
// Finished :) |
|
933 |
update_panels(); |
|
934 |
doupdate(); |
|
935 |
} |
|
936 |
|
143
|
937 |
// scr_ScrollDown() |
|
938 |
// Scroll down the current buddy window, half a screen. |
105
|
939 |
void scr_ScrollDown(void) |
|
940 |
{ |
|
941 |
const gchar *jid; |
|
942 |
window_entry_t *win_entry; |
|
943 |
int n, nblines; |
|
944 |
GList *hbuf_top; |
|
945 |
|
|
946 |
// Get win_entry |
|
947 |
if (!current_buddy) |
|
948 |
return; |
|
949 |
jid = CURRENT_JID; |
|
950 |
if (!jid) |
|
951 |
return; |
|
952 |
win_entry = scr_SearchWindow(jid); |
|
953 |
if (!win_entry) |
|
954 |
return; |
|
955 |
|
|
956 |
// Scroll down half a screen (or less) |
|
957 |
nblines = CHAT_WIN_HEIGHT/2-1; |
|
958 |
hbuf_top = win_entry->top; |
|
959 |
|
|
960 |
for (n=0 ; hbuf_top && n < nblines ; n++) |
|
961 |
hbuf_top = g_list_next(hbuf_top); |
|
962 |
win_entry->top = hbuf_top; |
|
963 |
// Check if we are at the bottom |
|
964 |
for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++) |
|
965 |
hbuf_top = g_list_next(hbuf_top); |
|
966 |
if (!hbuf_top) |
|
967 |
win_entry->top = NULL; // End reached |
|
968 |
|
|
969 |
// Refresh the window |
|
970 |
scr_UpdateWindow(win_entry); |
|
971 |
|
|
972 |
// Finished :) |
|
973 |
update_panels(); |
|
974 |
doupdate(); |
|
975 |
} |
|
976 |
|
143
|
977 |
// scr_Clear() |
|
978 |
// Clear the current buddy window (used for the /clear command) |
108
|
979 |
void scr_Clear(void) |
|
980 |
{ |
|
981 |
const gchar *jid; |
|
982 |
window_entry_t *win_entry; |
|
983 |
|
|
984 |
// Get win_entry |
|
985 |
if (!current_buddy) |
|
986 |
return; |
|
987 |
jid = CURRENT_JID; |
|
988 |
if (!jid) |
|
989 |
return; |
|
990 |
win_entry = scr_SearchWindow(jid); |
|
991 |
if (!win_entry) |
|
992 |
return; |
|
993 |
|
|
994 |
win_entry->cleared = TRUE; |
109
|
995 |
win_entry->top = NULL; |
108
|
996 |
|
|
997 |
// Refresh the window |
|
998 |
scr_UpdateWindow(win_entry); |
|
999 |
|
|
1000 |
// Finished :) |
|
1001 |
update_panels(); |
|
1002 |
doupdate(); |
|
1003 |
} |
|
1004 |
|
187
|
1005 |
// scr_BufferTop() |
|
1006 |
// Jump to the head of the current buddy window |
|
1007 |
void scr_BufferTop(void) |
|
1008 |
{ |
|
1009 |
const gchar *jid; |
|
1010 |
window_entry_t *win_entry; |
|
1011 |
|
|
1012 |
// Get win_entry |
|
1013 |
if (!current_buddy) return; |
|
1014 |
jid = CURRENT_JID; |
|
1015 |
if (!jid) return; |
|
1016 |
win_entry = scr_SearchWindow(jid); |
|
1017 |
|
|
1018 |
if (!win_entry) return; |
|
1019 |
|
|
1020 |
win_entry->cleared = FALSE; |
|
1021 |
win_entry->top = g_list_first(win_entry->hbuf); |
|
1022 |
|
|
1023 |
// Refresh the window |
|
1024 |
scr_UpdateWindow(win_entry); |
|
1025 |
|
|
1026 |
// Finished :) |
|
1027 |
update_panels(); |
|
1028 |
doupdate(); |
|
1029 |
} |
|
1030 |
|
|
1031 |
// scr_BufferBottom() |
|
1032 |
// Jump to the end of the current buddy window |
|
1033 |
void scr_BufferBottom(void) |
|
1034 |
{ |
|
1035 |
const gchar *jid; |
|
1036 |
window_entry_t *win_entry; |
|
1037 |
|
|
1038 |
// Get win_entry |
|
1039 |
if (!current_buddy) return; |
|
1040 |
jid = CURRENT_JID; |
|
1041 |
if (!jid) return; |
|
1042 |
win_entry = scr_SearchWindow(jid); |
|
1043 |
|
|
1044 |
if (!win_entry) return; |
|
1045 |
|
|
1046 |
win_entry->cleared = FALSE; |
|
1047 |
win_entry->top = NULL; |
|
1048 |
|
|
1049 |
// Refresh the window |
|
1050 |
scr_UpdateWindow(win_entry); |
|
1051 |
|
|
1052 |
// Finished :) |
|
1053 |
update_panels(); |
|
1054 |
doupdate(); |
|
1055 |
} |
|
1056 |
|
44
|
1057 |
// scr_LogPrint(...) |
|
1058 |
// Display a message in the log window. |
24
|
1059 |
void scr_LogPrint(const char *fmt, ...) |
|
1060 |
{ |
|
1061 |
time_t timestamp; |
|
1062 |
char *buffer; |
|
1063 |
va_list ap; |
|
1064 |
|
153
|
1065 |
do { |
157
|
1066 |
buffer = (char *) calloc(1, 1024); |
153
|
1067 |
} while (!buffer); |
24
|
1068 |
|
|
1069 |
timestamp = time(NULL); |
|
1070 |
strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp)); |
|
1071 |
wprintw(logWnd, "\n%s", buffer); |
|
1072 |
|
|
1073 |
va_start(ap, fmt); |
153
|
1074 |
vsnprintf(buffer, 1024, fmt, ap); |
24
|
1075 |
va_end(ap); |
|
1076 |
|
|
1077 |
wprintw(logWnd, "%s", buffer); |
|
1078 |
free(buffer); |
|
1079 |
|
|
1080 |
update_panels(); |
|
1081 |
doupdate(); |
|
1082 |
} |
|
1083 |
|
143
|
1084 |
// scr_set_chatmode() |
|
1085 |
// Public fonction to (un)set chatmode... |
129
|
1086 |
inline void scr_set_chatmode(int enable) |
|
1087 |
{ |
|
1088 |
chatmode = enable; |
|
1089 |
} |
|
1090 |
|
238
|
1091 |
// scr_get_multimode() |
|
1092 |
// Public fonction to get multimode status... |
|
1093 |
inline int scr_get_multimode() |
|
1094 |
{ |
|
1095 |
return multimode; |
|
1096 |
} |
|
1097 |
|
|
1098 |
// scr_set_multimode() |
|
1099 |
// Public fonction to (un)set multimode... |
|
1100 |
inline void scr_set_multimode(int enable) |
|
1101 |
{ |
|
1102 |
if (multiline) { |
|
1103 |
g_free(multiline); |
|
1104 |
multiline = NULL; |
|
1105 |
} |
|
1106 |
if (enable) |
|
1107 |
multimode = TRUE; |
|
1108 |
else |
|
1109 |
multimode = FALSE; |
|
1110 |
} |
|
1111 |
|
|
1112 |
// scr_get_multiline() |
|
1113 |
// Public fonction to get multimode status... |
|
1114 |
inline const char *scr_get_multiline() |
|
1115 |
{ |
|
1116 |
if (multimode && multiline) |
|
1117 |
return multiline; |
|
1118 |
else |
|
1119 |
return ""; |
|
1120 |
} |
|
1121 |
|
|
1122 |
// scr_append_multiline(line) |
|
1123 |
// Public function to append a line to the current multi-line message. |
|
1124 |
// Skip empty leading lines. |
|
1125 |
void scr_append_multiline(const char *line) |
|
1126 |
{ |
|
1127 |
static int num; |
|
1128 |
|
|
1129 |
if (!multimode) { |
|
1130 |
scr_LogPrint("Error: Not in multi-line message mode!"); |
|
1131 |
return; |
|
1132 |
} |
|
1133 |
if (multiline) { |
|
1134 |
int len = strlen(multiline)+strlen(line)+2; |
252
|
1135 |
if (len >= HBB_BLOCKSIZE - 1) { |
238
|
1136 |
// We don't handle single messages with size > HBB_BLOCKSIZE |
|
1137 |
// (see hbuf) |
|
1138 |
scr_LogPrint("Your multi-line message is too big, this line has " |
|
1139 |
"not been added."); |
|
1140 |
scr_LogPrint("Please send this part now..."); |
|
1141 |
return; |
|
1142 |
} |
|
1143 |
multiline = g_renew(char, multiline, len); |
|
1144 |
strcat(multiline, "\n"); |
|
1145 |
strcat(multiline, line); |
|
1146 |
num++; |
|
1147 |
} else { |
|
1148 |
// First message line (we skip leading empty lines) |
|
1149 |
num = 0; |
|
1150 |
if (line[0]) { |
|
1151 |
multiline = g_new(char, strlen(line)+1); |
|
1152 |
strcpy(multiline, line); |
|
1153 |
num++; |
|
1154 |
} else |
|
1155 |
return; |
|
1156 |
} |
|
1157 |
scr_LogPrint("Multi-line mode: line #%d added [%.25s...", num, line); |
|
1158 |
} |
|
1159 |
|
173
|
1160 |
// scr_cmdhisto_addline() |
|
1161 |
// Add a line to the inputLine history |
|
1162 |
inline void scr_cmdhisto_addline(char *line) |
|
1163 |
{ |
|
1164 |
if (!line || !*line) return; |
|
1165 |
|
|
1166 |
cmdhisto = g_list_append(cmdhisto, g_strdup(line)); |
|
1167 |
} |
|
1168 |
|
|
1169 |
// scr_cmdhisto_prev() |
|
1170 |
// Look for previous line beginning w/ the given mask in the inputLine history |
175
|
1171 |
// Returns NULL if none found |
173
|
1172 |
const char *scr_cmdhisto_prev(char *mask, guint len) |
|
1173 |
{ |
|
1174 |
GList *hl; |
|
1175 |
if (!cmdhisto_cur) { |
|
1176 |
hl = g_list_last(cmdhisto); |
174
|
1177 |
if (hl) { // backup current line |
|
1178 |
strncpy(cmdhisto_backup, mask, INPUTLINE_LENGTH); |
|
1179 |
} |
173
|
1180 |
} else { |
|
1181 |
hl = g_list_previous(cmdhisto_cur); |
|
1182 |
} |
|
1183 |
while (hl) { |
|
1184 |
if (!strncmp((char*)hl->data, mask, len)) { |
|
1185 |
// Found a match |
|
1186 |
cmdhisto_cur = hl; |
|
1187 |
return (const char*)hl->data; |
|
1188 |
} |
|
1189 |
hl = g_list_previous(hl); |
|
1190 |
} |
|
1191 |
return NULL; |
|
1192 |
} |
|
1193 |
|
|
1194 |
// scr_cmdhisto_next() |
|
1195 |
// Look for next line beginning w/ the given mask in the inputLine history |
175
|
1196 |
// Returns NULL if none found |
173
|
1197 |
const char *scr_cmdhisto_next(char *mask, guint len) |
|
1198 |
{ |
|
1199 |
GList *hl; |
|
1200 |
if (!cmdhisto_cur) return NULL; |
|
1201 |
hl = cmdhisto_cur; |
|
1202 |
while ((hl = g_list_next(hl)) != NULL) |
|
1203 |
if (!strncmp((char*)hl->data, mask, len)) { |
|
1204 |
// Found a match |
|
1205 |
cmdhisto_cur = hl; |
|
1206 |
return (const char*)hl->data; |
|
1207 |
} |
175
|
1208 |
// If the "backuped" line matches, we'll use it |
|
1209 |
if (strncmp(cmdhisto_backup, mask, len)) return NULL; // No match |
174
|
1210 |
cmdhisto_cur = NULL; |
|
1211 |
return cmdhisto_backup; |
173
|
1212 |
} |
|
1213 |
|
195
|
1214 |
// readline_transpose_chars() |
|
1215 |
// Drag the character before point forward over the character at |
|
1216 |
// point, moving point forward as well. If point is at the end of |
|
1217 |
// the line, then this transposes the two characters before point. |
|
1218 |
void readline_transpose_chars() |
|
1219 |
{ |
|
1220 |
char swp; |
|
1221 |
|
|
1222 |
if (ptr_inputline == inputLine) return; |
|
1223 |
|
|
1224 |
if (!*ptr_inputline) { // We're at EOL |
|
1225 |
// If line is only 1 char long, nothing to do... |
|
1226 |
if (ptr_inputline == inputLine+1) return; |
|
1227 |
// Transpose the two previous characters |
|
1228 |
swp = *(ptr_inputline-2); |
|
1229 |
*(ptr_inputline-2) = *(ptr_inputline-1); |
|
1230 |
*(ptr_inputline-1) = swp; |
|
1231 |
} else { |
196
|
1232 |
// Swap the two characters before the cursor and move right. |
195
|
1233 |
swp = *(ptr_inputline-1); |
|
1234 |
*(ptr_inputline-1) = *ptr_inputline; |
|
1235 |
*ptr_inputline++ = swp; |
|
1236 |
check_offset(1); |
|
1237 |
} |
|
1238 |
} |
|
1239 |
|
|
1240 |
// readline_backward_kill_word() |
194
|
1241 |
// Kill the word before the cursor, in input line |
195
|
1242 |
void readline_backward_kill_word() |
194
|
1243 |
{ |
|
1244 |
char *c, *old = ptr_inputline; |
|
1245 |
int spaceallowed = 1; |
|
1246 |
|
|
1247 |
if (ptr_inputline == inputLine) return; |
|
1248 |
|
|
1249 |
for (c = ptr_inputline-1 ; c > inputLine ; c--) |
|
1250 |
if (!isalnum(*c)) { |
|
1251 |
if (*c == ' ') |
|
1252 |
if (!spaceallowed) break; |
|
1253 |
} else spaceallowed = 0; |
|
1254 |
|
|
1255 |
if (c != inputLine || *c != ' ') |
|
1256 |
if ((c < ptr_inputline-1) && (!isalnum(*c))) |
|
1257 |
c++; |
|
1258 |
|
|
1259 |
// Modify the line |
|
1260 |
ptr_inputline = c; |
|
1261 |
for (;;) { |
|
1262 |
*c = *old++; |
|
1263 |
if (!*c++) break; |
|
1264 |
} |
195
|
1265 |
check_offset(-1); |
194
|
1266 |
} |
|
1267 |
|
98
|
1268 |
// which_row() |
|
1269 |
// Tells which row our cursor is in, in the command line. |
|
1270 |
// -1 -> normal text |
|
1271 |
// 0 -> command |
|
1272 |
// 1 -> parameter 1 (etc.) |
102
|
1273 |
// If > 0, then *p_row is set to the beginning of the row |
|
1274 |
int which_row(char **p_row) |
98
|
1275 |
{ |
|
1276 |
int row = -1; |
|
1277 |
char *p; |
|
1278 |
int quote = FALSE; |
|
1279 |
|
|
1280 |
// Not a command? |
|
1281 |
if ((ptr_inputline == inputLine) || (inputLine[0] != '/')) |
|
1282 |
return -1; |
|
1283 |
|
|
1284 |
// This is a command |
|
1285 |
row = 0; |
|
1286 |
for (p = inputLine ; p < ptr_inputline ; p++) { |
|
1287 |
if (quote) { |
|
1288 |
if (*p == '"' && *(p-1) != '\\') |
|
1289 |
quote = FALSE; |
|
1290 |
continue; |
|
1291 |
} |
|
1292 |
if (*p == '"' && *(p-1) != '\\') { |
|
1293 |
quote = TRUE; |
121
|
1294 |
} else if (*p == ' ') { |
|
1295 |
if (*(p-1) != ' ') |
|
1296 |
row++; |
102
|
1297 |
*p_row = p+1; |
|
1298 |
} |
98
|
1299 |
} |
|
1300 |
return row; |
|
1301 |
} |
|
1302 |
|
143
|
1303 |
// scr_insert_text() |
|
1304 |
// Insert the given text at the current cursor position. |
|
1305 |
// The cursor is moved. We don't check if the cursor still is in the screen |
|
1306 |
// after, the caller should do that. |
98
|
1307 |
void scr_insert_text(const char *text) |
|
1308 |
{ |
|
1309 |
char tmpLine[INPUTLINE_LENGTH+1]; |
|
1310 |
int len = strlen(text); |
|
1311 |
// Check the line isn't too long |
|
1312 |
if (strlen(inputLine) + len >= INPUTLINE_LENGTH) { |
|
1313 |
scr_LogPrint("Cannot insert text, line too long."); |
|
1314 |
return; |
|
1315 |
} |
|
1316 |
|
|
1317 |
strcpy(tmpLine, ptr_inputline); |
|
1318 |
strcpy(ptr_inputline, text); ptr_inputline += len; |
|
1319 |
strcpy(ptr_inputline, tmpLine); |
|
1320 |
} |
|
1321 |
|
143
|
1322 |
// scr_handle_tab() |
|
1323 |
// Function called when tab is pressed. |
|
1324 |
// Initiate or continue a completion... |
98
|
1325 |
void scr_handle_tab(void) |
|
1326 |
{ |
102
|
1327 |
int nrow; |
|
1328 |
char *row; |
|
1329 |
const char *cchar; |
103
|
1330 |
guint compl_categ; |
98
|
1331 |
|
102
|
1332 |
nrow = which_row(&row); |
98
|
1333 |
|
103
|
1334 |
// a) No completion if no leading slash ('cause not a command) |
|
1335 |
// b) We can't have more than 2 parameters (we use 2 flags) |
|
1336 |
if (nrow < 0 || nrow > 2) return; |
102
|
1337 |
|
103
|
1338 |
if (nrow == 0) { // Command completion |
|
1339 |
row = &inputLine[1]; |
|
1340 |
compl_categ = COMPL_CMD; |
|
1341 |
} else { // Other completion, depending on the command |
|
1342 |
cmd *com = cmd_get(inputLine); |
|
1343 |
if (!com || !row) { |
|
1344 |
scr_LogPrint("I cannot complete that..."); |
|
1345 |
return; |
|
1346 |
} |
|
1347 |
compl_categ = com->completion_flags[nrow-1]; |
|
1348 |
} |
|
1349 |
|
|
1350 |
if (!completion_started) { |
|
1351 |
GSList *list = compl_get_category_list(compl_categ); |
|
1352 |
if (list) { |
|
1353 |
char *prefix = g_strndup(row, ptr_inputline-row); |
|
1354 |
// Init completion |
|
1355 |
new_completion(prefix, list); |
|
1356 |
g_free(prefix); |
|
1357 |
// Now complete |
98
|
1358 |
cchar = complete(); |
|
1359 |
if (cchar) |
|
1360 |
scr_insert_text(cchar); |
103
|
1361 |
completion_started = TRUE; |
98
|
1362 |
} |
103
|
1363 |
} else { // Completion already initialized |
|
1364 |
char *c; |
|
1365 |
guint back = cancel_completion(); |
|
1366 |
// Remove $back chars |
|
1367 |
ptr_inputline -= back; |
|
1368 |
c = ptr_inputline; |
|
1369 |
for ( ; *c ; c++) |
|
1370 |
*c = *(c+back); |
|
1371 |
// Now complete again |
|
1372 |
cchar = complete(); |
|
1373 |
if (cchar) |
|
1374 |
scr_insert_text(cchar); |
102
|
1375 |
} |
98
|
1376 |
} |
|
1377 |
|
|
1378 |
void scr_cancel_current_completion(void) |
|
1379 |
{ |
|
1380 |
char *c; |
|
1381 |
guint back = cancel_completion(); |
|
1382 |
// Remove $back chars |
|
1383 |
ptr_inputline -= back; |
|
1384 |
c = ptr_inputline; |
|
1385 |
for ( ; *c ; c++) |
|
1386 |
*c = *(c+back); |
|
1387 |
} |
|
1388 |
|
|
1389 |
void scr_end_current_completion(void) |
|
1390 |
{ |
|
1391 |
done_completion(); |
|
1392 |
completion_started = FALSE; |
|
1393 |
} |
|
1394 |
|
24
|
1395 |
// check_offset(int direction) |
|
1396 |
// Check inputline_offset value, and make sure the cursor is inside the |
|
1397 |
// screen. |
|
1398 |
inline void check_offset(int direction) |
|
1399 |
{ |
|
1400 |
// Left side |
|
1401 |
if (inputline_offset && direction <= 0) { |
|
1402 |
while (ptr_inputline <= (char*)&inputLine + inputline_offset) { |
|
1403 |
if (inputline_offset) { |
|
1404 |
inputline_offset -= 5; |
|
1405 |
if (inputline_offset < 0) |
|
1406 |
inputline_offset = 0; |
|
1407 |
} |
|
1408 |
} |
|
1409 |
} |
|
1410 |
// Right side |
|
1411 |
if (direction >= 0) { |
|
1412 |
while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX) |
|
1413 |
inputline_offset += 5; |
|
1414 |
} |
|
1415 |
} |
|
1416 |
|
44
|
1417 |
// process_key(key) |
|
1418 |
// Handle the pressed key, in the command line (bottom). |
29
|
1419 |
int process_key(int key) |
24
|
1420 |
{ |
|
1421 |
if (isprint(key)) { |
|
1422 |
char tmpLine[INPUTLINE_LENGTH+1]; |
|
1423 |
|
|
1424 |
// Check the line isn't too long |
|
1425 |
if (strlen(inputLine) >= INPUTLINE_LENGTH) |
|
1426 |
return 0; |
|
1427 |
|
|
1428 |
// Insert char |
|
1429 |
strcpy(tmpLine, ptr_inputline); |
|
1430 |
*ptr_inputline++ = key; |
|
1431 |
strcpy(ptr_inputline, tmpLine); |
|
1432 |
check_offset(1); |
|
1433 |
} else { |
|
1434 |
switch(key) { |
232
|
1435 |
case 8: // Ctrl-h |
|
1436 |
case 127: // Backspace too |
24
|
1437 |
case KEY_BACKSPACE: |
|
1438 |
if (ptr_inputline != (char*)&inputLine) { |
42
|
1439 |
char *c = --ptr_inputline; |
|
1440 |
for ( ; *c ; c++) |
|
1441 |
*c = *(c+1); |
24
|
1442 |
check_offset(-1); |
|
1443 |
} |
|
1444 |
break; |
238
|
1445 |
case KEY_DC:// Del |
24
|
1446 |
if (*ptr_inputline) |
|
1447 |
strcpy(ptr_inputline, ptr_inputline+1); |
|
1448 |
break; |
|
1449 |
case KEY_LEFT: |
|
1450 |
if (ptr_inputline != (char*)&inputLine) { |
|
1451 |
ptr_inputline--; |
|
1452 |
check_offset(-1); |
|
1453 |
} |
|
1454 |
break; |
|
1455 |
case KEY_RIGHT: |
|
1456 |
if (*ptr_inputline) |
|
1457 |
ptr_inputline++; |
|
1458 |
check_offset(1); |
|
1459 |
break; |
98
|
1460 |
case 7: // Ctrl-g |
|
1461 |
scr_cancel_current_completion(); |
|
1462 |
scr_end_current_completion(); |
|
1463 |
check_offset(-1); |
|
1464 |
break; |
24
|
1465 |
case 9: // Tab |
98
|
1466 |
scr_handle_tab(); |
|
1467 |
check_offset(0); |
24
|
1468 |
break; |
|
1469 |
case '\n': // Enter |
29
|
1470 |
if (process_line(inputLine)) |
24
|
1471 |
return 255; |
173
|
1472 |
// Add line to history |
|
1473 |
scr_cmdhisto_addline(inputLine); |
|
1474 |
cmdhisto_cur = NULL; |
|
1475 |
// Reset the line |
24
|
1476 |
ptr_inputline = inputLine; |
|
1477 |
*ptr_inputline = 0; |
|
1478 |
inputline_offset = 0; |
|
1479 |
break; |
|
1480 |
case KEY_UP: |
175
|
1481 |
{ |
|
1482 |
const char *l = scr_cmdhisto_prev(inputLine, |
|
1483 |
ptr_inputline-inputLine); |
|
1484 |
if (l) { |
|
1485 |
strcpy(inputLine, l); |
|
1486 |
} |
|
1487 |
} |
24
|
1488 |
break; |
|
1489 |
case KEY_DOWN: |
175
|
1490 |
{ |
|
1491 |
const char *l = scr_cmdhisto_next(inputLine, |
|
1492 |
ptr_inputline-inputLine); |
|
1493 |
if (l) { |
|
1494 |
strcpy(inputLine, l); |
|
1495 |
} |
|
1496 |
} |
24
|
1497 |
break; |
|
1498 |
case KEY_PPAGE: |
175
|
1499 |
scr_RosterUp(); |
24
|
1500 |
break; |
|
1501 |
case KEY_NPAGE: |
175
|
1502 |
scr_RosterDown(); |
24
|
1503 |
break; |
|
1504 |
case KEY_HOME: |
|
1505 |
case 1: |
|
1506 |
ptr_inputline = inputLine; |
|
1507 |
inputline_offset = 0; |
|
1508 |
break; |
|
1509 |
case KEY_END: |
|
1510 |
case 5: |
|
1511 |
for (; *ptr_inputline; ptr_inputline++) ; |
|
1512 |
check_offset(1); |
|
1513 |
break; |
|
1514 |
case 21: // Ctrl-u |
|
1515 |
strcpy(inputLine, ptr_inputline); |
|
1516 |
ptr_inputline = inputLine; |
|
1517 |
inputline_offset = 0; |
|
1518 |
break; |
|
1519 |
case KEY_EOL: |
|
1520 |
case 11: // Ctrl-k |
|
1521 |
*ptr_inputline = 0; |
|
1522 |
break; |
|
1523 |
case 16: // Ctrl-p |
175
|
1524 |
scr_ScrollUp(); |
24
|
1525 |
break; |
|
1526 |
case 14: // Ctrl-n |
175
|
1527 |
scr_ScrollDown(); |
24
|
1528 |
break; |
196
|
1529 |
case 17: // Ctrl-q |
236
|
1530 |
scr_RosterUnreadMessage(1); // next unread message |
196
|
1531 |
break; |
195
|
1532 |
case 20: // Ctrl-t |
|
1533 |
readline_transpose_chars(); |
|
1534 |
break; |
194
|
1535 |
case 23: // Ctrl-w |
195
|
1536 |
readline_backward_kill_word(); |
194
|
1537 |
break; |
24
|
1538 |
case 27: // ESC |
|
1539 |
currentWindow = NULL; |
|
1540 |
chatmode = FALSE; |
119
|
1541 |
if (current_buddy) |
|
1542 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
24
|
1543 |
top_panel(chatPanel); |
|
1544 |
top_panel(inputPanel); |
157
|
1545 |
update_panels(); |
24
|
1546 |
break; |
151
|
1547 |
case 12: // Ctrl-l |
|
1548 |
case KEY_RESIZE: |
|
1549 |
scr_Resize(); |
|
1550 |
break; |
24
|
1551 |
default: |
|
1552 |
scr_LogPrint("Unkown key=%d", key); |
232
|
1553 |
if (utf8_mode) |
|
1554 |
scr_LogPrint("WARNING: UTF-8 not yet supported!"); |
24
|
1555 |
} |
|
1556 |
} |
157
|
1557 |
if (completion_started && key != 9 && key != KEY_RESIZE) |
98
|
1558 |
scr_end_current_completion(); |
24
|
1559 |
mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); |
|
1560 |
wclrtoeol(inputWnd); |
|
1561 |
if (*ptr_inputline) { |
|
1562 |
wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); |
|
1563 |
} |
157
|
1564 |
if (!update_roster) { |
|
1565 |
//update_panels(); |
|
1566 |
doupdate(); |
|
1567 |
} |
24
|
1568 |
return 0; |
|
1569 |
} |