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