1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
/*
* windows.c
*
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <string.h>
#include <glib.h>
#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#endif
#include "common.h"
#include "roster_list.h"
#include "config/theme.h"
#include "ui/ui.h"
#include "ui/statusbar.h"
#include "ui/window.h"
#include "ui/windows.h"
#define CONS_WIN_TITLE "_cons"
static GHashTable *windows;
static int current;
static int max_cols;
void
wins_init(void)
{
windows = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
(GDestroyNotify)win_free);
max_cols = getmaxx(stdscr);
int cols = getmaxx(stdscr);
ProfWin *console = win_create(CONS_WIN_TITLE, cols, WIN_CONSOLE);
g_hash_table_insert(windows, GINT_TO_POINTER(1), console);
current = 1;
}
ProfWin *
wins_get_console(void)
{
return g_hash_table_lookup(windows, GINT_TO_POINTER(1));
}
ProfWin *
wins_get_current(void)
{
return g_hash_table_lookup(windows, GINT_TO_POINTER(current));
}
GList *
wins_get_nums(void)
{
return g_hash_table_get_keys(windows);
}
void
wins_set_current_by_num(int i)
{
if (g_hash_table_lookup(windows, GINT_TO_POINTER(i)) != NULL) {
current = i;
}
}
ProfWin *
wins_get_by_num(int i)
{
return g_hash_table_lookup(windows, GINT_TO_POINTER(i));
}
ProfWin *
wins_get_next(void)
{
// get and sort win nums
GList *keys = g_hash_table_get_keys(windows);
keys = g_list_sort(keys, cmp_win_num);
GList *curr = keys;
// find our place in the list
while (curr != NULL) {
if (current == GPOINTER_TO_INT(curr->data)) {
break;
}
curr = g_list_next(curr);
}
// if there is a next window return it
curr = g_list_next(curr);
if (curr != NULL) {
return wins_get_by_num(GPOINTER_TO_INT(curr->data));
// otherwise return the first window (console)
} else {
return wins_get_console();
}
}
ProfWin *
wins_get_previous(void)
{
// get and sort win nums
GList *keys = g_hash_table_get_keys(windows);
keys = g_list_sort(keys, cmp_win_num);
GList *curr = keys;
// find our place in the list
while (curr != NULL) {
if (current == GPOINTER_TO_INT(curr->data)) {
break;
}
curr = g_list_next(curr);
}
// if there is a previous window return it
curr = g_list_previous(curr);
if (curr != NULL) {
return wins_get_by_num(GPOINTER_TO_INT(curr->data));
// otherwise return the last window
} else {
int new_num = GPOINTER_TO_INT(g_list_last(keys)->data);
return wins_get_by_num(new_num);
}
}
ProfWin *
wins_get_by_recipient(const char * const recipient)
{
GList *values = g_hash_table_get_values(windows);
GList *curr = values;
while (curr != NULL) {
ProfWin *window = curr->data;
if (g_strcmp0(window->from, recipient) == 0) {
return window;
}
curr = g_list_next(curr);
}
return NULL;
}
int
wins_get_num(ProfWin *window)
{
GList *keys = g_hash_table_get_keys(windows);
GList *curr = keys;
while (curr != NULL) {
gconstpointer num_p = curr->data;
ProfWin *curr_win = g_hash_table_lookup(windows, num_p);
if (g_strcmp0(curr_win->from, window->from) == 0) {
return GPOINTER_TO_INT(num_p);
}
curr = g_list_next(curr);
}
return -1;
}
int
wins_get_current_num(void)
{
return current;
}
void
wins_close_current(void)
{
wins_close_by_num(current);
}
void
wins_close_by_num(int i)
{
// console cannot be closed
if (i != 1) {
// go to console if closing current window
if (i == current) {
current = 1;
ProfWin *window = wins_get_current();
win_update_virtual(window);
}
g_hash_table_remove(windows, GINT_TO_POINTER(i));
status_bar_inactive(i);
}
}
void
wins_clear_current(void)
{
ProfWin *window = wins_get_current();
werase(window->win);
win_update_virtual(window);
}
gboolean
wins_is_current(ProfWin *window)
{
ProfWin *current_window = wins_get_current();
if (g_strcmp0(current_window->from, window->from) == 0) {
return TRUE;
} else {
return FALSE;
}
}
ProfWin *
wins_new(const char * const from, win_type_t type)
{
GList *keys = g_hash_table_get_keys(windows);
int result = get_next_available_win_num(keys);
int cols = getmaxx(stdscr);
ProfWin *new = win_create(from, cols, type);
g_hash_table_insert(windows, GINT_TO_POINTER(result), new);
return new;
}
int
wins_get_total_unread(void)
{
int result = 0;
GList *values = g_hash_table_get_values(windows);
GList *curr = values;
while (curr != NULL) {
ProfWin *window = curr->data;
result += window->unread;
curr = g_list_next(curr);
}
return result;
}
void
wins_resize_all(void)
{
int rows, cols;
getmaxyx(stdscr, rows, cols);
// only make the pads bigger, to avoid data loss on cropping
if (cols > max_cols) {
max_cols = cols;
GList *values = g_hash_table_get_values(windows);
GList *curr = values;
while (curr != NULL) {
ProfWin *window = curr->data;
wresize(window->win, PAD_SIZE, cols);
curr = g_list_next(curr);
}
}
ProfWin *current_win = wins_get_current();
pnoutrefresh(current_win->win, current_win->y_pos, 0, 1, 0, rows-3, cols-1);
}
gboolean
wins_duck_exists(void)
{
GList *values = g_hash_table_get_values(windows);
GList *curr = values;
while (curr != NULL) {
ProfWin *window = curr->data;
if (window->type == WIN_DUCK)
return TRUE;
curr = g_list_next(curr);
}
return FALSE;
}
GSList *
wins_get_chat_recipients(void)
{
GSList *result = NULL;
GList *values = g_hash_table_get_values(windows);
GList *curr = values;
while (curr != NULL) {
ProfWin *window = curr->data;
if (window->type == WIN_CHAT) {
result = g_slist_append(result, window->from);
}
curr = g_list_next(curr);
}
return result;
}
GSList *
wins_get_prune_recipients(void)
{
GSList *result = NULL;
GList *values = g_hash_table_get_values(windows);
GList *curr = values;
while (curr != NULL) {
ProfWin *window = curr->data;
if (window->unread == 0 && window->type != WIN_MUC && window->type != WIN_CONSOLE) {
result = g_slist_append(result, window->from);
}
curr = g_list_next(curr);
}
return result;
}
void
wins_lost_connection(void)
{
GList *values = g_hash_table_get_values(windows);
GList *curr = values;
while (curr != NULL) {
ProfWin *window = curr->data;
if (window->type != WIN_CONSOLE) {
win_print_time(window, '-');
wattron(window->win, COLOUR_ERROR);
wprintw(window->win, "%s\n", "Lost connection.");
wattroff(window->win, COLOUR_ERROR);
// if current win, set current_win_dirty
if (wins_is_current(window)) {
win_update_virtual(window);
}
}
curr = g_list_next(curr);
}
}
gboolean
wins_tidy(void)
{
gboolean tidy_required = FALSE;
// check for gaps
GList *keys = g_hash_table_get_keys(windows);
keys = g_list_sort(keys, cmp_win_num);
// get last used
GList *last = g_list_last(keys);
int last_num = GPOINTER_TO_INT(last->data);
// find first free num TODO - Will sort again
int next_available = get_next_available_win_num(keys);
// found gap (next available before last window)
if (cmp_win_num(GINT_TO_POINTER(next_available), GINT_TO_POINTER(last_num)) < 0) {
tidy_required = TRUE;
}
if (tidy_required) {
status_bar_set_all_inactive();
GHashTable *new_windows = g_hash_table_new_full(g_direct_hash,
g_direct_equal, NULL, (GDestroyNotify)win_free);
int num = 1;
GList *curr = keys;
while (curr != NULL) {
ProfWin *window = g_hash_table_lookup(windows, curr->data);
if (num == 10) {
g_hash_table_insert(new_windows, GINT_TO_POINTER(0), window);
if (window->unread > 0) {
status_bar_new(0);
} else {
status_bar_active(0);
}
} else {
g_hash_table_insert(new_windows, GINT_TO_POINTER(num), window);
if (window->unread > 0) {
status_bar_new(num);
} else {
status_bar_active(num);
}
}
num++;
curr = g_list_next(curr);
}
windows = new_windows;
current = 1;
ui_switch_win(1);
return TRUE;
} else {
return FALSE;
}
}
GSList *
wins_create_summary(void)
{
GSList *result = NULL;
GList *keys = g_hash_table_get_keys(windows);
keys = g_list_sort(keys, cmp_win_num);
GList *curr = keys;
while (curr != NULL) {
ProfWin *window = g_hash_table_lookup(windows, curr->data);
int ui_index = GPOINTER_TO_INT(curr->data);
GString *chat_string;
GString *priv_string;
GString *muc_string;
GString *duck_string;
switch (window->type)
{
case WIN_CONSOLE:
result = g_slist_append(result, strdup("1: Console"));
break;
case WIN_CHAT:
chat_string = g_string_new("");
PContact contact = roster_get_contact(window->from);
if (contact == NULL) {
g_string_printf(chat_string, "%d: Chat %s", ui_index, window->from);
} else {
const char *display_name = p_contact_name_or_jid(contact);
g_string_printf(chat_string, "%d: Chat %s", ui_index, display_name);
GString *chat_presence = g_string_new("");
g_string_printf(chat_presence, " - %s", p_contact_presence(contact));
g_string_append(chat_string, chat_presence->str);
g_string_free(chat_presence, TRUE);
}
if (window->unread > 0) {
GString *chat_unread = g_string_new("");
g_string_printf(chat_unread, ", %d unread", window->unread);
g_string_append(chat_string, chat_unread->str);
g_string_free(chat_unread, TRUE);
}
result = g_slist_append(result, strdup(chat_string->str));
g_string_free(chat_string, TRUE);
break;
case WIN_PRIVATE:
priv_string = g_string_new("");
g_string_printf(priv_string, "%d: Private %s", ui_index, window->from);
if (window->unread > 0) {
GString *priv_unread = g_string_new("");
g_string_printf(priv_unread, ", %d unread", window->unread);
g_string_append(priv_string, priv_unread->str);
g_string_free(priv_unread, TRUE);
}
result = g_slist_append(result, strdup(priv_string->str));
g_string_free(priv_string, TRUE);
break;
case WIN_MUC:
muc_string = g_string_new("");
g_string_printf(muc_string, "%d: Room %s", ui_index, window->from);
if (window->unread > 0) {
GString *muc_unread = g_string_new("");
g_string_printf(muc_unread, ", %d unread", window->unread);
g_string_append(muc_string, muc_unread->str);
g_string_free(muc_unread, TRUE);
}
result = g_slist_append(result, strdup(muc_string->str));
g_string_free(muc_string, TRUE);
break;
case WIN_DUCK:
duck_string = g_string_new("");
g_string_printf(duck_string, "%d: DuckDuckGo search", ui_index);
result = g_slist_append(result, strdup(duck_string->str));
g_string_free(duck_string, TRUE);
break;
default:
break;
}
curr = g_list_next(curr);
}
return result;
}
void
wins_destroy(void)
{
g_hash_table_destroy(windows);
}
|