summary refs log tree commit diff stats
path: root/doc/examples
diff options
context:
space:
mode:
authorhut <hut@lepus.uberspace.de>2015-04-13 12:49:49 +0200
committerhut <hut@lepus.uberspace.de>2015-04-13 12:49:49 +0200
commitc0f2fc72eccb4127fba5f48ce4b422487d6ec752 (patch)
tree025f72dde6f7a7e5cdca82637da4e5dcef0ddd62 /doc/examples
parenta9bbdc440c2ea33ccc4470e00949ffa16ce2300e (diff)
downloadranger-c0f2fc72eccb4127fba5f48ce4b422487d6ec752.tar.gz
moved "doc/examples" to "examples" for more visibility
Diffstat (limited to 'doc/examples')
-rw-r--r--doc/examples/README8
-rw-r--r--doc/examples/bash_automatic_cd.sh21
-rw-r--r--doc/examples/bash_subshell_notice.sh7
-rw-r--r--doc/examples/plugin_chmod_keybindings.py20
-rw-r--r--doc/examples/plugin_file_filter.py21
-rw-r--r--doc/examples/plugin_hello_world.py23
-rw-r--r--doc/examples/plugin_new_macro.py19
-rw-r--r--doc/examples/plugin_new_sorting_method.py9
-rw-r--r--doc/examples/rifle_different_file_opener.conf9
-rwxr-xr-xdoc/examples/rifle_sxiv.sh48
-rw-r--r--doc/examples/vim_file_chooser.vim36
11 files changed, 0 insertions, 221 deletions
diff --git a/doc/examples/README b/doc/examples/README
deleted file mode 100644
index ca514853..00000000
--- a/doc/examples/README
+++ /dev/null
@@ -1,8 +0,0 @@
-The files in this directory contain applications or extensions of ranger which
-are put here for your inspiration and as references.
-
-In order to use a plugin from this directory, you need to copy it to
-~/.config/ranger/plugins/
-
-Note that if you update ranger to a new minor version (for example,
-from 1.6.* to 1.7.0), your outdated plugins WILL break and crash ranger.
diff --git a/doc/examples/bash_automatic_cd.sh b/doc/examples/bash_automatic_cd.sh
deleted file mode 100644
index 465c9c80..00000000
--- a/doc/examples/bash_automatic_cd.sh
+++ /dev/null
@@ -1,21 +0,0 @@
-# Compatible with ranger 1.4.2 through 1.6.*
-#
-# Automatically change the directory in bash after closing ranger
-#
-# This is a bash function for .bashrc to automatically change the directory to
-# the last visited one after ranger quits.
-# To undo the effect of this function, you can type "cd -" to return to the
-# original directory.
-
-function ranger-cd {
-    tempfile="$(mktemp)"
-    /usr/bin/ranger --choosedir="$tempfile" "${@:-$(pwd)}"
-    test -f "$tempfile" &&
-    if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
-        cd -- "$(cat "$tempfile")"
-    fi
-    rm -f -- "$tempfile"
-}
-
-# This binds Ctrl-O to ranger-cd:
-bind '"\C-o":"ranger-cd\C-m"'
diff --git a/doc/examples/bash_subshell_notice.sh b/doc/examples/bash_subshell_notice.sh
deleted file mode 100644
index bc44d5a8..00000000
--- a/doc/examples/bash_subshell_notice.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-# Compatible with ranger 1.5.3 through 1.6.*
-#
-# Change the prompt when you open a shell from inside ranger
-#
-# Add this line to your .bashrc for it to work.
-
-[ -n "$RANGER_LEVEL" ] && PS1="$PS1"'(in ranger) '
diff --git a/doc/examples/plugin_chmod_keybindings.py b/doc/examples/plugin_chmod_keybindings.py
deleted file mode 100644
index 0ab975ed..00000000
--- a/doc/examples/plugin_chmod_keybindings.py
+++ /dev/null
@@ -1,20 +0,0 @@
-# Compatible with ranger 1.6.*
-#
-# This plugin serves as an example for adding key bindings through a plugin.
-# It could replace the ten lines in the rc.conf that create the key bindings
-# for the "chmod" command.
-
-import ranger.api
-old_hook_init = ranger.api.hook_init
-
-def hook_init(fm):
-    old_hook_init(fm)
-
-    # Generate key bindings for the chmod command
-    command = "map {0}{1}{2} shell -d chmod {1}{0}{2} %s"
-    for mode in list('ugoa') + ['']:
-        for perm in "rwxXst":
-            fm.execute_console(command.format('-', mode, perm))
-            fm.execute_console(command.format('+', mode, perm))
-
-ranger.api.hook_init = hook_init
diff --git a/doc/examples/plugin_file_filter.py b/doc/examples/plugin_file_filter.py
deleted file mode 100644
index b9bea1f3..00000000
--- a/doc/examples/plugin_file_filter.py
+++ /dev/null
@@ -1,21 +0,0 @@
-# Compatible since ranger 1.6.1, git commit c82a8a76989c
-#
-# This plugin hides the directories "/boot", "/sbin", "/proc" and "/sys" unless
-# the "show_hidden" option is activated.
-
-# Save the original filter function
-import ranger.container.directory
-old_accept_file = ranger.container.directory.accept_file
-
-HIDE_FILES = ("/boot", "/sbin", "/proc", "/sys")
-
-# Define a new one
-def custom_accept_file(file, filters):
-    if not file.fm.settings.show_hidden and file.path in HIDE_FILES:
-        return False
-    else:
-        return old_accept_file(file, filters)
-
-# Overwrite the old function
-import ranger.container.directory
-ranger.container.directory.accept_file = custom_accept_file
diff --git a/doc/examples/plugin_hello_world.py b/doc/examples/plugin_hello_world.py
deleted file mode 100644
index a803e21b..00000000
--- a/doc/examples/plugin_hello_world.py
+++ /dev/null
@@ -1,23 +0,0 @@
-# Compatible with ranger 1.6.*
-#
-# This is a sample plugin that displays "Hello World" in ranger's console after
-# it started.
-
-# We are going to extend the hook "ranger.api.hook_ready", so first we need
-# to import ranger.api:
-import ranger.api
-
-# Save the previously existing hook, because maybe another module already
-# extended that hook and we don't want to lose it:
-old_hook_ready = ranger.api.hook_ready
-
-# Create a replacement for the hook that...
-def hook_ready(fm):
-    # ...does the desired action...
-    fm.notify("Hello World")
-    # ...and calls the saved hook.  If you don't care about the return value,
-    # simply return the return value of the previous hook to be safe.
-    return old_hook_ready(fm)
-
-# Finally, "monkey patch" the existing hook_ready function with our replacement:
-ranger.api.hook_ready = hook_ready
diff --git a/doc/examples/plugin_new_macro.py b/doc/examples/plugin_new_macro.py
deleted file mode 100644
index 159a92f2..00000000
--- a/doc/examples/plugin_new_macro.py
+++ /dev/null
@@ -1,19 +0,0 @@
-# Compatible with ranger 1.6.*
-#
-# This plugin adds the new macro %date which is substituted with the current
-# date in commands that allow macros.  You can test it with the command
-# ":shell echo %date; read"
-
-# Save the original macro function
-import ranger.core.actions
-old_get_macros = ranger.core.actions.Actions._get_macros
-
-# Define a new macro function
-import time
-def get_macros_with_date(self):
-       macros = old_get_macros(self)
-       macros['date'] = time.strftime('%m/%d/%Y')
-       return macros
-
-# Overwrite the old one
-ranger.core.actions.Actions._get_macros = get_macros_with_date
diff --git a/doc/examples/plugin_new_sorting_method.py b/doc/examples/plugin_new_sorting_method.py
deleted file mode 100644
index 6b41b0e1..00000000
--- a/doc/examples/plugin_new_sorting_method.py
+++ /dev/null
@@ -1,9 +0,0 @@
-# Compatible with ranger 1.6.*
-#
-# This plugin adds the sorting algorithm called 'random'.  To enable it, type
-# ":set sort=random" or create a key binding with ":map oz set sort=random"
-
-from ranger.container.directory import Directory
-from random import random
-Directory.sort_dict['random'] = lambda path: random()
-
diff --git a/doc/examples/rifle_different_file_opener.conf b/doc/examples/rifle_different_file_opener.conf
deleted file mode 100644
index 4a8250b8..00000000
--- a/doc/examples/rifle_different_file_opener.conf
+++ /dev/null
@@ -1,9 +0,0 @@
-# Compatible with ranger 1.6.*
-#
-# Replace your rifle.conf with this file to use xdg-open as your file opener.
-# This is, of course, adaptable for use with any other file opener.
-else = xdg-open "$1"
-
-# You need an "editor" and "pager" in order to use certain functions in ranger:
-label editor = "$EDITOR" -- "$@"
-label pager  = "$PAGER" -- "$@"
diff --git a/doc/examples/rifle_sxiv.sh b/doc/examples/rifle_sxiv.sh
deleted file mode 100755
index 6307f1c2..00000000
--- a/doc/examples/rifle_sxiv.sh
+++ /dev/null
@@ -1,48 +0,0 @@
-#!/bin/sh
-# Compatible with ranger 1.6.*
-#
-# This script searches image files in a directory, opens them all with sxiv and
-# sets the first argument to the first image displayed by sxiv.
-#
-# This is supposed to be used in rifle.conf as a workaround for the fact that
-# sxiv takes no file name arguments for the first image, just the number.  Copy
-# this file somewhere into your $PATH and add this at the top of rifle.conf:
-#
-#   mime ^image, has sxiv, X, flag f = path/to/this/script -- "$@"
-#
-# Implementation notes: this script is quite slow because of POSIX limitations
-# and portability concerns. First calling the shell function 'abspath' is
-# quicker than calling 'realpath' because it would fork a whole process, which
-# is slow. Second, we need to append a file list to sxiv, which can only be done
-# properly in two ways: arrays (which are not POSIX) or \0 sperated
-# strings. Unfortunately, assigning \0 to a variable is not POSIX either (will
-# not work in dash and others), so we cannot store the result of listfiles to a
-# variable.
-
-if [ $# -eq 0 ]; then
-    echo "Usage: ${0##*/} PICTURES"
-    exit
-fi
-
-[ "$1" == '--' ] && shift
-
-abspath () {
-    case "$1" in
-        /*) printf "%s\n" "$1";;
-        *)  printf "%s\n" "$PWD/$1";;
-    esac
-}
-
-listfiles () {
-    find -L "$(dirname "$target")" -maxdepth 1 -type f -iregex \
-      '.*\(jpe?g\|bmp\|png\|gif\)$' -print0 | sort -z
-}
-
-target="$(abspath "$1")"
-count="$(listfiles | grep -m 1 -ZznF "$target" | cut -d: -f1)"
-
-if [ -n "$count" ]; then
-    listfiles | xargs -0 sxiv -n "$count" --
-else
-    sxiv -- "$@" # fallback
-fi
diff --git a/doc/examples/vim_file_chooser.vim b/doc/examples/vim_file_chooser.vim
deleted file mode 100644
index aa3af763..00000000
--- a/doc/examples/vim_file_chooser.vim
+++ /dev/null
@@ -1,36 +0,0 @@
-" Compatible with ranger 1.4.2 through 1.6.*
-"
-" Add ranger as a file chooser in vim
-"
-" If you add this code to the .vimrc, ranger can be started using the command
-" ":RangerChooser" or the keybinding "<leader>r".  Once you select one or more
-" files, press enter and ranger will quit again and vim will open the selected
-" files.
-
-function! RangeChooser()
-    let temp = tempname()
-    " The option "--choosefiles" was added in ranger 1.5.1. Use the next line
-    " with ranger 1.4.2 through 1.5.0 instead.
-    "exec 'silent !ranger --choosefile=' . shellescape(temp)
-    exec 'silent !ranger --choosefiles=' . shellescape(temp)
-    if !filereadable(temp)
-        redraw!
-        " Nothing to read.
-        return
-    endif
-    let names = readfile(temp)
-    if empty(names)
-        redraw!
-        " Nothing to open.
-        return
-    endif
-    " Edit the first item.
-    exec 'edit ' . fnameescape(names[0])
-    " Add any remaning items to the arg list/buffer list.
-    for name in names[1:]
-        exec 'argadd ' . fnameescape(name)
-    endfor
-    redraw!
-endfunction
-command! -bar RangerChooser call RangeChooser()
-nnoremap <leader>r :<C-U>RangerChooser<CR>
andling code from rosterwin.c' href='/danisanti/profani-tty/commit/src/ui/rosterwin.c?id=6f81f2b8454e4aa668b18b2f7cd9da8a79e5b85e'>6f81f2b8 ^
3f3182a7 ^
abc2f0de ^

6f81f2b8 ^
abc2f0de ^
3f3182a7 ^



f91b21a1 ^

6f81f2b8 ^
f91b21a1 ^

6f81f2b8 ^
8602ede6 ^
f91b21a1 ^





















3f3182a7 ^



5bbb3ef4 ^
3f3182a7 ^
6f81f2b8 ^
3f3182a7 ^



3f3182a7 ^
e816b124 ^
3f3182a7 ^
e816b124 ^
3f3182a7 ^


369aa5e8 ^


6f81f2b8 ^
3f3182a7 ^
369aa5e8 ^

3f3182a7 ^





369aa5e8 ^
3f3182a7 ^


6f81f2b8 ^
abc2f0de ^


6f81f2b8 ^
abc2f0de ^












6f81f2b8 ^
3f3182a7 ^
3f3182a7 ^
369aa5e8 ^

3f3182a7 ^

369aa5e8 ^
6f81f2b8 ^
3f3182a7 ^

369aa5e8 ^

3f3182a7 ^





369aa5e8 ^
3f3182a7 ^


6f81f2b8 ^
3f3182a7 ^



3f3182a7 ^
e816b124 ^
3f3182a7 ^
e816b124 ^
3f3182a7 ^


369aa5e8 ^



3f3182a7 ^
6f81f2b8 ^
3f3182a7 ^
6f81f2b8 ^
3f3182a7 ^

369aa5e8 ^
3f3182a7 ^





369aa5e8 ^
3f3182a7 ^


abc2f0de ^



















































3f3182a7 ^






































































43191312 ^
3f3182a7 ^


48ec7b3b ^
43191312 ^
5c08bea8 ^
c31ba8c0 ^
522b9ab6 ^





c31ba8c0 ^

b73ff08a ^
522b9ab6 ^
5c08bea8 ^




3bc92750 ^
43191312 ^
3bc92750 ^
522b9ab6 ^


43191312 ^
522b9ab6 ^
7a324abc ^


3bc92750 ^
7a324abc ^








3d1299eb ^
3bc92750 ^
7a324abc ^

522b9ab6 ^
522b9ab6 ^


69358621 ^
c31ba8c0 ^
7a324abc ^









3bc92750 ^
7a324abc ^














c31ba8c0 ^
3d1299eb ^
3bc92750 ^
7a324abc ^

c31ba8c0 ^
c31ba8c0 ^



3f3182a7 ^

0e7e9315 ^
0f22179e ^

b44ea030 ^

43191312 ^

0f22179e ^

b44ea030 ^
aab6a52e ^
0e7e9315 ^
b44ea030 ^
0f22179e ^
c4e8af0b ^


0f22179e ^

c4e8af0b ^
b44ea030 ^
8e84d160 ^
b4482b9f ^
8e84d160 ^
aab6a52e ^
cce01f1d ^

aab6a52e ^

cce01f1d ^
aab6a52e ^
3bc92750 ^
0f22179e ^
b44ea030 ^

0e7e9315 ^
522b9ab6 ^
43191312 ^
0e7e9315 ^
43191312 ^

0f22179e ^
aab6a52e ^

cce01f1d ^

aab6a52e ^









cce01f1d ^
aab6a52e ^
0f22179e ^



b44ea030 ^
0f22179e ^



43191312 ^
0f22179e ^
















0f22179e ^




43191312 ^
0f22179e ^



b44ea030 ^
522b9ab6 ^
b44ea030 ^
1d9d893e ^
aab6a52e ^


cce01f1d ^

aab6a52e ^






cce01f1d ^
43191312 ^
aab6a52e ^


cce01f1d ^

aab6a52e ^








cce01f1d ^
b44ea030 ^
aab6a52e ^
b44ea030 ^




6f81f2b8 ^
b44ea030 ^
3f3182a7 ^









cce01f1d ^
3f3182a7 ^
aab6a52e ^
3f3182a7 ^
7c4a63f3 ^

3f3182a7 ^

6f81f2b8 ^
806afcc0 ^
3f3182a7 ^



48ec7b3b ^

fbe8c1e3 ^
3f3182a7 ^
f91b21a1 ^
052eee3c ^
f91b21a1 ^
6f81f2b8 ^
f91b21a1 ^
8602ede6 ^

f91b21a1 ^
8602ede6 ^

f91b21a1 ^
8602ede6 ^
f91b21a1 ^

dd250c67 ^
8602ede6 ^


dd250c67 ^

f91b21a1 ^




8602ede6 ^


f91b21a1 ^


8602ede6 ^
f91b21a1 ^

8602ede6 ^

f91b21a1 ^

6f81f2b8 ^
f91b21a1 ^
8602ede6 ^
f91b21a1 ^
48ec7b3b ^
8602ede6 ^


48ec7b3b ^

3f3182a7 ^
22a14e12 ^


fcbd2592 ^
0ae975c2 ^




cd2458c0 ^
fcbd2592 ^
cd2458c0 ^
fcbd2592 ^

22a14e12 ^








2a8da892 ^
22a14e12 ^



e58be44f ^



f91b21a1 ^





4c7396e7 ^







f91b21a1 ^
f887a35c ^
e58be44f ^
22a14e12 ^

e58be44f ^
22a14e12 ^




fcbd2592 ^
0ae975c2 ^




cd2458c0 ^
fcbd2592 ^
cd2458c0 ^
fcbd2592 ^
67abfaa2 ^
6d8a3e20 ^
















67abfaa2 ^
67abfaa2 ^
6d8a3e20 ^







67abfaa2 ^
67abfaa2 ^
6d8a3e20 ^



67abfaa2 ^
a00095c8 ^
6d8a3e20 ^


67abfaa2 ^
08c1e62e ^

67abfaa2 ^
6d8a3e20 ^



67abfaa2 ^
08c1e62e ^
67abfaa2 ^
08c1e62e ^



67abfaa2 ^
08c1e62e ^
7cacee87 ^
08c1e62e ^
6d8a3e20 ^
08c1e62e ^
6d8a3e20 ^

08c1e62e ^



6d8a3e20 ^

67abfaa2 ^

6d8a3e20 ^
67abfaa2 ^

6d8a3e20 ^
22a14e12 ^

3f3182a7 ^
dd250c67 ^
7cacee87 ^
dd250c67 ^












7cacee87 ^
f17ecb77 ^
7cacee87 ^





















f17ecb77 ^
2a8da892 ^



7cacee87 ^






08c1e62e ^









7cacee87 ^
08c1e62e ^
7cacee87 ^
08c1e62e ^
7cacee87 ^

f17ecb77 ^
7cacee87 ^
08c1e62e ^


7cacee87 ^








3f3182a7 ^

22a14e12 ^
3f3182a7 ^




7cacee87 ^
22a14e12 ^

3f3182a7 ^

20659b98 ^
3f3182a7 ^
20659b98 ^

3f3182a7 ^

48ec7b3b ^
3f3182a7 ^





cf80fdc3 ^
38ed9188 ^
5bbb3ef4 ^

6f81f2b8 ^
abc2f0de ^
6f81f2b8 ^
abc2f0de ^










































6f81f2b8 ^
5bbb3ef4 ^
6f81f2b8 ^
5bbb3ef4 ^












































f17ecb77 ^

6f81f2b8 ^
f17ecb77 ^
6f81f2b8 ^
f17ecb77 ^




f91b21a1 ^
f17ecb77 ^














f17ecb77 ^
a0f43adf ^










f17ecb77 ^
a0f43adf ^


f17ecb77 ^
a0f43adf ^
f17ecb77 ^




























































369aa5e8 ^



369aa5e8 ^










1c65c36c ^



369aa5e8 ^
1c65c36c ^



369aa5e8 ^
369aa5e8 ^
369aa5e8 ^
1c65c36c ^


369aa5e8 ^
1c65c36c ^
369aa5e8 ^

369aa5e8 ^










369aa5e8 ^








1c65c36c ^

369aa5e8 ^










369aa5e8 ^











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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259


              
                                                            













                                                                       
                                                                      















                                                                                
                   
                   
 
                               
                  
                      
                           
                             
                         
 





                          





                                                                                                              

                                                                          
                                                                                        
                                                                                                  



                                                                                               


                                                                                        
                                                                         
 
                                                                                         
                                                                             
 

                                                                                            











                                                                                               
                                                               







                                                                

                                                                                      

                                                                   
                                                

                                       
                                                     
                               
         





















                                                                                




                                                    





                                                                                  
                                                 

                                                
                                
                                                                       
                                                     
             
                                           
                                                       
                
                                            
         

                                                          
                                             
         



                                                                                     

                                                                   
                                                

                                       
                                                     
                               





















                                                                                



                                
 
           
                                                



                                                      
                                            
                                                            
            
                                                        


                             


                                                           
                                                                    
 

                                                 





                                                      
                                    


           
                                                 


                                               
                                                     












                                                      
                                                                                                 
 
                                                                 

                                                                                   

                                                                     
                                                                    
                                                                     

     

                                                 





                                                      
                                    


           
                                                                  



                                                      
                                            
                                                                
            
                                                            


                             



                                                                    
                    
                                                                         
                
                                                                              

         
                                                 





                                                      
                                    


           



















































                                                                           






































































                                                                                                  
                                                                      


     
           
                                                                                      
                       
 





                                                              

                                                          
                          
 




                                                             
                                                        
                                                                      
 


                                                              
 
                                                         


                                                                
                                                                                     








                                                              
                                                     
                                                                                     

                                                              
             


                                                                 
                                                                                                              
                                                     









                                                                         
                                                                                 














                                                                         
             
                                                 
                                                                                 

                                                          
         



           

                                                                                                                      
 

                                                                 

                                                                  

                                           

                                                      
                                                                                              
                                                                                                     
 
                                                                           
                                            


                                                       

                                          
             
                                                 
                                                          
                                                                       
             
 

                                                                     

                                                             
                                         
 
                                                                
                                                                    

                                                                            
 
                                                                                                   
                                                                                                 
             

                                   
                

                                                                

                                                                     









                                                                                           
                                         
 



                                                                     
 



                                                                                                  
                                                                                                             
















                                                                               




                                                                                                       
                                                                                                     



                                                           
         
                                                                                                  
                                                           
                                                       


                                                                               

                                                                 






                                                                                       
                                     
                                                                      


                                                            

                                                                 








                                                                                       
                                     
     
 




                           
                                                                    
 









                                                                                                               
             
                                     
         
                                           

     

                                                                    
                                                             
 



                                                     

         
 
                              
 
 
           
                                                    
 

                               
                           

                                   
                                        
 

                                                                                                         
         


                                 

     




                                           


                                       


                                                                       
                                                                                      

             

                                     

         
                                                          
 
                                               
                                                 
     


                                     

 
           


                                                            
 




                                                                        
                                                                       
            
                                                                

     








                                                   
                                           



                                              



                                                                      





                                                              







                                                                           
     
                                  
                                                                     

                                                             
                                 




                                                                         
 




                                                                         
                                                                        
            
                                                                 
     
 
















                                                               
             
 







                                                        
             
 



                                                                               
 
                                                      


                                                          
 

                                                                        
 



                                                                              
 
                                             
 



                                                                             
 
                                
                                      
                                                                            
                    
                                                                            

             



                                                                                     

                                         

         
                           

     
                                

 
           
                                                                            
 












                                                           
                                                        
                                                 





















                                                                               
                                                      



                                                          






                                                                              









                                                                                    
                                      
                                                                            
                    
                                                                            

             
                                                                
 


                                                                                     








                                         

                                                                          
 




                                                                                    
     

 

                                                 
 
                                             

 

                                                   
 





                                                 
     
 

           
                                                                    
 
                                         










































                                                                            
                                                                                              
 
                                         












































                                                                            

           
                                                                           
 
                                         




                                                 
                                   














                                                                      
 










                                                                          
             


                                        
         
 




























































                                                                            



                                  










                                                                              



                                                               
 



                                                                                 
                                                                                   
                 
 


                                                                               
             
                                      

         










                                                                            








                                                                                  

                                            










                                                                                   











                                                                              
/*
 * rosterwin.c
 *
 * Copyright (C) 2012 - 2019 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 <https://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "config/preferences.h"
#include "ui/ui.h"
#include "ui/window.h"
#include "ui/window_list.h"
#include "xmpp/roster_list.h"
#include "xmpp/contact.h"

typedef enum {
    ROSTER_CONTACT,
    ROSTER_CONTACT_ACTIVE,
    ROSTER_CONTACT_UNREAD
} roster_contact_theme_t;

static void _rosterwin_contacts_all(ProfLayoutSplit *layout);
static void _rosterwin_contacts_by_presence(ProfLayoutSplit *layout, const char *const presence, char *title);
static void _rosterwin_contacts_by_group(ProfLayoutSplit *layout, char *group);
static void _rosteriwin_unsubscribed(ProfLayoutSplit *layout);
static void _rosterwin_contacts_header(ProfLayoutSplit *layout, const char *title, GSList *contacts);
static void _rosterwin_unsubscribed_header(ProfLayoutSplit *layout, GList *wins);

static void _rosterwin_contact(ProfLayoutSplit *layout, PContact contact);
static void _rosterwin_unsubscribed_item(ProfLayoutSplit *layout, ProfChatWin *chatwin);
static void _rosterwin_presence(ProfLayoutSplit *layout, const char *presence, const char *status,
    int current_indent);
static void _rosterwin_resources(ProfLayoutSplit *layout, PContact contact, int current_indent,
    roster_contact_theme_t theme_type, int unread);

static void _rosterwin_rooms(ProfLayoutSplit *layout, char *title, GList *rooms);
static void _rosterwin_rooms_by_service(ProfLayoutSplit *layout);
static void _rosterwin_rooms_header(ProfLayoutSplit *layout, GList *rooms, char *title);
static void _rosterwin_room(ProfLayoutSplit *layout, ProfMucWin *mucwin);

static void _rosterwin_private_chats(ProfLayoutSplit *layout, GList *orphaned_privchats);
static void _rosterwin_private_header(ProfLayoutSplit *layout, GList *privs);

static GSList* _filter_contacts(GSList *contacts);
static GSList* _filter_contacts_with_presence(GSList *contacts, const char *const presence);
static theme_item_t _get_roster_theme(roster_contact_theme_t theme_type, const char *presence);
static int _compare_rooms_name(ProfMucWin *a, ProfMucWin *b);
static int _compare_rooms_unread(ProfMucWin *a, ProfMucWin *b);

void
rosterwin_roster(void)
{
    ProfWin *console = wins_get_console();
    if (!console) {
        return;
    }

    jabber_conn_status_t conn_status = connection_get_status();
    if (conn_status != JABBER_CONNECTED) {
        return;
    }

    ProfLayoutSplit *layout = (ProfLayoutSplit*)console->layout;
    assert(layout->memcheck == LAYOUT_SPLIT_MEMCHECK);
    werase(layout->subwin);

    char *roomspos = prefs_get_string(PREF_ROSTER_ROOMS_POS);
    if (prefs_get_boolean(PREF_ROSTER_ROOMS) && (g_strcmp0(roomspos, "first") == 0)) {
        char *roomsbypref = prefs_get_string(PREF_ROSTER_ROOMS_BY);
        if (g_strcmp0(roomsbypref, "service") == 0) {
            _rosterwin_rooms_by_service(layout);
        } else {
            GList *rooms = muc_rooms();
            _rosterwin_rooms(layout, "Rooms", rooms);
            g_list_free(rooms);
        }
        prefs_free_string(roomsbypref);

        GList *orphaned_privchats = NULL;
        GList *privchats = wins_get_private_chats(NULL);
        GList *curr = privchats;
        while (curr) {
            ProfPrivateWin *privwin = curr->data;
            Jid *jidp = jid_create(privwin->fulljid);
            if (!muc_active(jidp->barejid)) {
                orphaned_privchats = g_list_append(orphaned_privchats, privwin);
            }
            jid_destroy(jidp);
            curr = g_list_next(curr);
        }

        char *privpref = prefs_get_string(PREF_ROSTER_PRIVATE);
        if (g_strcmp0(privpref, "group") == 0 || orphaned_privchats) {
            _rosterwin_private_chats(layout, orphaned_privchats);
        }
        prefs_free_string(privpref);
        g_list_free(orphaned_privchats);

    }

    if (prefs_get_boolean(PREF_ROSTER_CONTACTS)) {
        char *by = prefs_get_string(PREF_ROSTER_BY);
        if (g_strcmp0(by, "presence") == 0) {
            _rosterwin_contacts_by_presence(layout, "chat", "Available for chat");
            _rosterwin_contacts_by_presence(layout, "online", "Online");
            _rosterwin_contacts_by_presence(layout, "away", "Away");
            _rosterwin_contacts_by_presence(layout, "xa", "Extended Away");
            _rosterwin_contacts_by_presence(layout, "dnd", "Do not disturb");
            _rosterwin_contacts_by_presence(layout, "offline", "Offline");
        } else if (g_strcmp0(by, "group") == 0) {
            GList *groups = roster_get_groups();
            GList *curr_group = groups;
            while (curr_group) {
                _rosterwin_contacts_by_group(layout, curr_group->data);
                curr_group = g_list_next(curr_group);
            }
            g_list_free_full(groups, free);
            _rosterwin_contacts_by_group(layout, NULL);
        } else {
            _rosterwin_contacts_all(layout);
        }

        if (prefs_get_boolean(PREF_ROSTER_UNSUBSCRIBED)) {
            _rosteriwin_unsubscribed(layout);
        }
        prefs_free_string(by);
    }

    if (prefs_get_boolean(PREF_ROSTER_ROOMS) && (g_strcmp0(roomspos, "last") == 0)) {
        char *roomsbypref = prefs_get_string(PREF_ROSTER_ROOMS_BY);
        if (g_strcmp0(roomsbypref, "service") == 0) {
            _rosterwin_rooms_by_service(layout);
        } else {
            GList *rooms = muc_rooms();
            _rosterwin_rooms(layout, "Rooms", rooms);
            g_list_free(rooms);
        }
        prefs_free_string(roomsbypref);

        GList *orphaned_privchats = NULL;
        GList *privchats = wins_get_private_chats(NULL);
        GList *curr = privchats;
        while (curr) {
            ProfPrivateWin *privwin = curr->data;
            Jid *jidp = jid_create(privwin->fulljid);
            if (!muc_active(jidp->barejid)) {
                orphaned_privchats = g_list_append(orphaned_privchats, privwin);
            }
            jid_destroy(jidp);
            curr = g_list_next(curr);
        }

        char *privpref = prefs_get_string(PREF_ROSTER_PRIVATE);
        if (g_strcmp0(privpref, "group") == 0 || orphaned_privchats) {
            _rosterwin_private_chats(layout, orphaned_privchats);
        }
        prefs_free_string(privpref);
        g_list_free(orphaned_privchats);
    }

    prefs_free_string(roomspos);
}

static void
_rosterwin_contacts_all(ProfLayoutSplit *layout)
{
    GSList *contacts = NULL;

    char *order = prefs_get_string(PREF_ROSTER_ORDER);
    if (g_strcmp0(order, "presence") == 0) {
        contacts = roster_get_contacts(ROSTER_ORD_PRESENCE);
    } else {
        contacts = roster_get_contacts(ROSTER_ORD_NAME);
    }
    prefs_free_string(order);

    GSList *filtered_contacts = _filter_contacts(contacts);
    g_slist_free(contacts);

    _rosterwin_contacts_header(layout, "Roster", filtered_contacts);

    if (filtered_contacts) {
        GSList *curr_contact = filtered_contacts;
        while (curr_contact) {
            PContact contact = curr_contact->data;
            _rosterwin_contact(layout, contact);
            curr_contact = g_slist_next(curr_contact);
        }
    }
    g_slist_free(filtered_contacts);
}

static void
_rosteriwin_unsubscribed(ProfLayoutSplit *layout)
{
    GList *wins = wins_get_chat_unsubscribed();
    if (wins) {
        _rosterwin_unsubscribed_header(layout, wins);
    }

    GList *curr = wins;
    while (curr) {
        ProfChatWin *chatwin = curr->data;
        _rosterwin_unsubscribed_item(layout, chatwin);
        curr = g_list_next(curr);
    }

    g_list_free(wins);
}

static void
_rosterwin_contacts_by_presence(ProfLayoutSplit *layout, const char *const presence, char *title)
{
    GSList *contacts = roster_get_contacts_by_presence(presence);
    GSList *filtered_contacts = _filter_contacts_with_presence(contacts, presence);
    g_slist_free(contacts);

    // if this group has contacts, or if we want to show empty groups
    if (filtered_contacts || prefs_get_boolean(PREF_ROSTER_EMPTY)) {
        _rosterwin_contacts_header(layout, title, filtered_contacts);
    }

    if (filtered_contacts) {
        GSList *curr_contact = filtered_contacts;
        while (curr_contact) {
            PContact contact = curr_contact->data;
            _rosterwin_contact(layout, contact);
            curr_contact = g_slist_next(curr_contact);
        }
    }
    g_slist_free(filtered_contacts);
}

static void
_rosterwin_contacts_by_group(ProfLayoutSplit *layout, char *group)
{
    GSList *contacts = NULL;

    char *order = prefs_get_string(PREF_ROSTER_ORDER);
    if (g_strcmp0(order, "presence") == 0) {
        contacts = roster_get_group(group, ROSTER_ORD_PRESENCE);
    } else {
        contacts = roster_get_group(group, ROSTER_ORD_NAME);
    }
    prefs_free_string(order);

    GSList *filtered_contacts = _filter_contacts(contacts);
    g_slist_free(contacts);

    if (filtered_contacts || prefs_get_boolean(PREF_ROSTER_EMPTY)) {
        if (group) {
            _rosterwin_contacts_header(layout, group, filtered_contacts);
        } else {
            _rosterwin_contacts_header(layout, "no group", filtered_contacts);
        }

        GSList *curr_contact = filtered_contacts;
        while (curr_contact) {
            PContact contact = curr_contact->data;
            _rosterwin_contact(layout, contact);
            curr_contact = g_slist_next(curr_contact);
        }
    }
    g_slist_free(filtered_contacts);
}

static void
_rosterwin_unsubscribed_item(ProfLayoutSplit *layout, ProfChatWin *chatwin)
{
    const char *const name = chatwin->barejid;
    const char *const presence = "offline";
    int unread = 0;

    roster_contact_theme_t theme_type = ROSTER_CONTACT;
    if (chatwin->unread > 0) {
        theme_type = ROSTER_CONTACT_UNREAD;
        unread = chatwin->unread;
    } else {
        theme_type = ROSTER_CONTACT_ACTIVE;
    }

    theme_item_t presence_colour = _get_roster_theme(theme_type, presence);

    wattron(layout->subwin, theme_attrs(presence_colour));
    GString *msg = g_string_new(" ");
    int indent = prefs_get_roster_contact_indent();
    int current_indent = 0;
    if (indent > 0) {
        current_indent += indent;
        while (indent > 0) {
            g_string_append(msg, " ");
            indent--;
        }
    }
    char ch = prefs_get_roster_contact_char();
    if (ch) {
        g_string_append_printf(msg, "%c", ch);
    }

    char *unreadpos = prefs_get_string(PREF_ROSTER_UNREAD);
    if ((g_strcmp0(unreadpos, "before") == 0) && unread > 0) {
        g_string_append_printf(msg, "(%d) ", unread);
        unread = 0;
    }
    g_string_append(msg, name);
    if ((g_strcmp0(unreadpos, "after") == 0) && unread > 0) {
        g_string_append_printf(msg, " (%d)", unread);
        unread = 0;
    }
    prefs_free_string(unreadpos);

    win_sub_newline_lazy(layout->subwin);
    gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);
    win_sub_print(layout->subwin, msg->str, FALSE, wrap, current_indent);
    g_string_free(msg, TRUE);
    wattroff(layout->subwin, theme_attrs(presence_colour));
}

static void
_rosterwin_contact(ProfLayoutSplit *layout, PContact contact)
{
    const char *name = p_contact_name_or_jid(contact);
    const char *presence = p_contact_presence(contact);
    const char *status = p_contact_status(contact);
    const char *barejid = p_contact_barejid(contact);
    int unread = 0;

    roster_contact_theme_t theme_type = ROSTER_CONTACT;
    ProfChatWin *chatwin = wins_get_chat(barejid);
    if (chatwin) {
        if (chatwin->unread > 0) {
            theme_type = ROSTER_CONTACT_UNREAD;
            unread = chatwin->unread;
        } else {
            theme_type = ROSTER_CONTACT_ACTIVE;
        }
    }

    theme_item_t presence_colour = _get_roster_theme(theme_type, presence);

    wattron(layout->subwin, theme_attrs(presence_colour));
    GString *msg = g_string_new(" ");
    int indent = prefs_get_roster_contact_indent();
    int current_indent = 0;
    if (indent > 0) {
        current_indent += indent;
        while (indent > 0) {
            g_string_append(msg, " ");
            indent--;
        }
    }
    char ch = prefs_get_roster_contact_char();
    if (ch) {
        g_string_append_printf(msg, "%c", ch);
    }

    char *unreadpos = prefs_get_string(PREF_ROSTER_UNREAD);
    if ((g_strcmp0(unreadpos, "before") == 0) && unread > 0) {
        g_string_append_printf(msg, "(%d) ", unread);
        unread = 0;
    }
    g_string_append(msg, name);
    if (g_strcmp0(unreadpos, "after") == 0) {
        if (!prefs_get_boolean(PREF_ROSTER_RESOURCE)) {
            if (unread > 0) {
                g_string_append_printf(msg, " (%d)", unread);
            }
            unread = 0;
        }
    }
    prefs_free_string(unreadpos);

    win_sub_newline_lazy(layout->subwin);
    gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);
    win_sub_print(layout->subwin, msg->str, FALSE, wrap, current_indent);
    g_string_free(msg, TRUE);
    wattroff(layout->subwin, theme_attrs(presence_colour));

    if (prefs_get_boolean(PREF_ROSTER_RESOURCE)) {
        _rosterwin_resources(layout, contact, current_indent, theme_type, unread);
    } else if (prefs_get_boolean(PREF_ROSTER_PRESENCE) || prefs_get_boolean(PREF_ROSTER_STATUS)) {
        if (unread > 0) {
            GString *unreadmsg = g_string_new("");
            g_string_append_printf(unreadmsg, " (%d)", unread);
            wattron(layout->subwin, theme_attrs(presence_colour));
            win_sub_print(layout->subwin, unreadmsg->str, FALSE, wrap, current_indent);
            g_string_free(unreadmsg, TRUE);
            wattroff(layout->subwin, theme_attrs(presence_colour));
        }

        _rosterwin_presence(layout, presence, status, current_indent);
    }
}

static void
_rosterwin_presence(ProfLayoutSplit *layout, const char *presence, const char *status,
    int current_indent)
{
    // don't show presence for offline contacts
    gboolean is_offline = g_strcmp0(presence, "offline") == 0;
    if (is_offline) {
        return;
    }

    char *by = prefs_get_string(PREF_ROSTER_BY);
    gboolean by_presence = g_strcmp0(by, "presence") == 0;
    prefs_free_string(by);

    int presence_indent = prefs_get_roster_presence_indent();
    if (presence_indent > 0) {
        current_indent += presence_indent;
    }

    gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);
    theme_item_t colour = _get_roster_theme(ROSTER_CONTACT, presence);

    // show only status when grouped by presence
    if (by_presence) {
        if (status && prefs_get_boolean(PREF_ROSTER_STATUS)) {

            wattron(layout->subwin, theme_attrs(colour));
            if (presence_indent == -1) {
                GString *msg = g_string_new("");
                g_string_append_printf(msg, ": \"%s\"", status);
                win_sub_print(layout->subwin, msg->str, FALSE, wrap, current_indent);
                g_string_free(msg, TRUE);
                wattroff(layout->subwin, theme_attrs(colour));
            } else {
                GString *msg = g_string_new(" ");
                while (current_indent > 0) {
                    g_string_append(msg, " ");
                    current_indent--;
                }
                g_string_append_printf(msg, "\"%s\"", status);
                win_sub_newline_lazy(layout->subwin);
                win_sub_print(layout->subwin, msg->str, FALSE, wrap, current_indent);
                g_string_free(msg, TRUE);
                wattroff(layout->subwin, theme_attrs(colour));
            }
        }

    // show both presence and status when not grouped by presence
    } else if (prefs_get_boolean(PREF_ROSTER_PRESENCE) || (status && prefs_get_boolean(PREF_ROSTER_STATUS))) {
        wattron(layout->subwin, theme_attrs(colour));
        if (presence_indent == -1) {
            GString *msg = g_string_new("");
            if (prefs_get_boolean(PREF_ROSTER_PRESENCE)) {
                g_string_append_printf(msg, ": %s", presence);
                if (status && prefs_get_boolean(PREF_ROSTER_STATUS)) {
                    g_string_append_printf(msg, " \"%s\"", status);
                }
            } else if (status && prefs_get_boolean(PREF_ROSTER_STATUS)) {
                g_string_append_printf(msg, ": \"%s\"", status);
            }
            win_sub_print(layout->subwin, msg->str, FALSE, wrap, current_indent);
            g_string_free(msg, TRUE);
            wattroff(layout->subwin, theme_attrs(colour));
        } else {
            GString *msg = g_string_new(" ");
            while (current_indent > 0) {
                g_string_append(msg, " ");
                current_indent--;
            }
            if (prefs_get_boolean(PREF_ROSTER_PRESENCE)) {
                g_string_append(msg, presence);
                if (status && prefs_get_boolean(PREF_ROSTER_STATUS)) {
                    g_string_append_printf(msg, " \"%s\"", status);
                }
            } else if (status && prefs_get_boolean(PREF_ROSTER_STATUS)) {
                g_string_append_printf(msg, "\"%s\"", status);
            }
            win_sub_newline_lazy(layout->subwin);
            win_sub_print(layout->subwin, msg->str, FALSE, wrap, current_indent);
            g_string_free(msg, TRUE);
            wattroff(layout->subwin, theme_attrs(colour));
        }
    }
}

static void
_rosterwin_resources(ProfLayoutSplit *layout, PContact contact, int current_indent, roster_contact_theme_t theme_type,
    int unread)
{
    gboolean join = prefs_get_boolean(PREF_ROSTER_RESOURCE_JOIN);

    GList *resources = p_contact_get_available_resources(contact);
    if (resources) {

        // resource on same line as contact
        if (join && (g_list_length(resources) == 1)) {
            Resource *resource = resources->data;
            const char *resource_presence = string_from_resource_presence(resource->presence);
            theme_item_t resource_presence_colour = _get_roster_theme(theme_type, resource_presence);

            wattron(layout->subwin, theme_attrs(resource_presence_colour));
            GString *msg = g_string_new("");
            char ch = prefs_get_roster_resource_char();
            if (ch) {
                g_string_append_printf(msg, "%c", ch);
            } else {
                g_string_append(msg, " ");
            }
            g_string_append(msg, resource->name);
            if (prefs_get_boolean(PREF_ROSTER_PRIORITY)) {
                g_string_append_printf(msg, " %d", resource->priority);
            }

            char *unreadpos = prefs_get_string(PREF_ROSTER_UNREAD);
            if ((g_strcmp0(unreadpos, "after") == 0) && unread > 0) {
                g_string_append_printf(msg, " (%d)", unread);
            }
            prefs_free_string(unreadpos);

            gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);
            win_sub_print(layout->subwin, msg->str, FALSE, wrap, 0);
            g_string_free(msg, TRUE);
            wattroff(layout->subwin, theme_attrs(resource_presence_colour));

            if (prefs_get_boolean(PREF_ROSTER_PRESENCE) || prefs_get_boolean(PREF_ROSTER_STATUS)) {
                _rosterwin_presence(layout, resource_presence, resource->status, current_indent);
            }

        // resource(s) on new lines
        } else {
            gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);

            char *unreadpos = prefs_get_string(PREF_ROSTER_UNREAD);
            if ((g_strcmp0(unreadpos, "after") == 0) && unread > 0) {
                GString *unreadmsg = g_string_new("");
                g_string_append_printf(unreadmsg, " (%d)", unread);

                const char *presence = p_contact_presence(contact);
                theme_item_t presence_colour = _get_roster_theme(theme_type, presence);

                wattron(layout->subwin, theme_attrs(presence_colour));
                win_sub_print(layout->subwin, unreadmsg->str, FALSE, wrap, current_indent);
                wattroff(layout->subwin, theme_attrs(presence_colour));
            }
            prefs_free_string(unreadpos);

            int resource_indent = prefs_get_roster_resource_indent();
            if (resource_indent > 0) {
                current_indent += resource_indent;
            }

            GList *curr_resource = resources;
            while (curr_resource) {
                Resource *resource = curr_resource->data;
                const char *resource_presence = string_from_resource_presence(resource->presence);
                theme_item_t resource_presence_colour = _get_roster_theme(ROSTER_CONTACT, resource_presence);

                wattron(layout->subwin, theme_attrs(resource_presence_colour));
                GString *msg = g_string_new(" ");
                int this_indent = current_indent;
                while (this_indent > 0) {
                    g_string_append(msg, " ");
                    this_indent--;
                }
                char ch = prefs_get_roster_resource_char();
                if (ch) {
                    g_string_append_printf(msg, "%c", ch);
                }
                g_string_append(msg, resource->name);
                if (prefs_get_boolean(PREF_ROSTER_PRIORITY)) {
                    g_string_append_printf(msg, " %d", resource->priority);
                }
                win_sub_newline_lazy(layout->subwin);
                win_sub_print(layout->subwin, msg->str, FALSE, wrap, current_indent);
                g_string_free(msg, TRUE);
                wattroff(layout->subwin, theme_attrs(resource_presence_colour));

                if (prefs_get_boolean(PREF_ROSTER_PRESENCE) || prefs_get_boolean(PREF_ROSTER_STATUS)) {
                    _rosterwin_presence(layout, resource_presence, resource->status, current_indent);
                }

                curr_resource = g_list_next(curr_resource);
            }
        }
    } else if (prefs_get_boolean(PREF_ROSTER_PRESENCE) || prefs_get_boolean(PREF_ROSTER_STATUS)) {
        const char *presence = p_contact_presence(contact);
        const char *status = p_contact_status(contact);
        theme_item_t presence_colour = _get_roster_theme(theme_type, presence);
        gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);

        char *unreadpos = prefs_get_string(PREF_ROSTER_UNREAD);
        if ((g_strcmp0(unreadpos, "after") == 0) && unread > 0) {
            GString *unreadmsg = g_string_new("");
            g_string_append_printf(unreadmsg, " (%d)", unread);

            wattron(layout->subwin, theme_attrs(presence_colour));
            win_sub_print(layout->subwin, unreadmsg->str, FALSE, wrap, current_indent);
            wattroff(layout->subwin, theme_attrs(presence_colour));
        }
        prefs_free_string(unreadpos);
        _rosterwin_presence(layout, presence, status, current_indent);
    } else {
        gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);

        char *unreadpos = prefs_get_string(PREF_ROSTER_UNREAD);
        if ((g_strcmp0(unreadpos, "after") == 0) && unread > 0) {
            GString *unreadmsg = g_string_new("");
            g_string_append_printf(unreadmsg, " (%d)", unread);
            const char *presence = p_contact_presence(contact);
            theme_item_t presence_colour = _get_roster_theme(theme_type, presence);

            wattron(layout->subwin, theme_attrs(presence_colour));
            win_sub_print(layout->subwin, unreadmsg->str, FALSE, wrap, current_indent);
            wattroff(layout->subwin, theme_attrs(presence_colour));
        }
        prefs_free_string(unreadpos);
    }

    g_list_free(resources);

}

static void
_rosterwin_rooms(ProfLayoutSplit *layout, char *title, GList *rooms)
{
    GList *rooms_sorted = NULL;
    GList *curr_room = rooms;
    while (curr_room) {
        ProfMucWin *mucwin = wins_get_muc(curr_room->data);
        if (mucwin) {
            char *order = prefs_get_string(PREF_ROSTER_ROOMS_ORDER);
            if (g_strcmp0(order, "unread") == 0) {
                rooms_sorted = g_list_insert_sorted(rooms_sorted, mucwin, (GCompareFunc)_compare_rooms_unread);
            } else {
                rooms_sorted = g_list_insert_sorted(rooms_sorted, mucwin, (GCompareFunc)_compare_rooms_name);
            }
            prefs_free_string(order);
        }
        curr_room = g_list_next(curr_room);
    }

    // if there are active rooms, or if we want to show empty groups
    if (rooms_sorted || prefs_get_boolean(PREF_ROSTER_EMPTY)) {
        _rosterwin_rooms_header(layout, rooms_sorted, title);

        GList *curr_room = rooms_sorted;
        while (curr_room) {
            _rosterwin_room(layout, curr_room->data);
            curr_room = g_list_next(curr_room);
        }
    }

    g_list_free(rooms_sorted);
}

static void
_rosterwin_rooms_by_service(ProfLayoutSplit *layout)
{
    GList *rooms = muc_rooms();
    GList *curr = rooms;
    GList *services = NULL;
    while (curr) {
        char *roomjid = curr->data;
        Jid *jidp = jid_create(roomjid);

        if (!g_list_find_custom(services, jidp->domainpart, (GCompareFunc)g_strcmp0)) {
            services = g_list_insert_sorted(services, strdup(jidp->domainpart), (GCompareFunc)g_strcmp0);
        }

        jid_destroy(jidp);
        curr = g_list_next(curr);
    }

    GList *curr_service = services;
    while (curr_service) {
        char *service = curr_service->data;
        GList *filtered_rooms = NULL;

        curr = rooms;
        while (curr) {
            char *roomjid = curr->data;
            Jid *jidp = jid_create(roomjid);

            if (g_strcmp0(curr_service->data, jidp->domainpart) == 0) {
                filtered_rooms = g_list_append(filtered_rooms, strdup(jidp->barejid));
            }

            jid_destroy(jidp);
            curr = g_list_next(curr);
        }

        _rosterwin_rooms(layout, service, filtered_rooms);

        g_list_free_full(filtered_rooms, free);
        curr_service = g_list_next(curr_service);
    }

    g_list_free(rooms);
    g_list_free_full(services, free);
}

static void
_rosterwin_room(ProfLayoutSplit *layout, ProfMucWin *mucwin)
{
    GString *msg = g_string_new(" ");

    if (mucwin->unread_mentions) {
        wattron(layout->subwin, theme_attrs(THEME_ROSTER_ROOM_MENTION));
    } else if (mucwin->unread_triggers) {
        wattron(layout->subwin, theme_attrs(THEME_ROSTER_ROOM_TRIGGER));
    } else if (mucwin->unread > 0) {
        wattron(layout->subwin, theme_attrs(THEME_ROSTER_ROOM_UNREAD));
    } else {
        wattron(layout->subwin, theme_attrs(THEME_ROSTER_ROOM));
    }

    int indent = prefs_get_roster_contact_indent();
    int current_indent = 0;
    if (indent > 0) {
        current_indent += indent;
        while (indent > 0) {
            g_string_append(msg, " ");
            indent--;
        }
    }
    char ch = prefs_get_roster_room_char();
    if (ch) {
        g_string_append_printf(msg, "%c", ch);
    }

    char *unreadpos = prefs_get_string(PREF_ROSTER_ROOMS_UNREAD);
    if ((g_strcmp0(unreadpos, "before") == 0) && mucwin->unread > 0) {
        g_string_append_printf(msg, "(%d) ", mucwin->unread);
    }
    char *roombypref = prefs_get_string(PREF_ROSTER_ROOMS_BY);
    if (g_strcmp0(roombypref, "service") == 0) {
        Jid *jidp = jid_create(mucwin->roomjid);
        g_string_append(msg, jidp->localpart);
        jid_destroy(jidp);
    } else {
        gboolean show_server = prefs_get_boolean(PREF_ROSTER_ROOMS_SERVER);
        if (show_server) {
            g_string_append(msg, mucwin->roomjid);
        } else {
            Jid *jidp = jid_create(mucwin->roomjid);
            g_string_append(msg, jidp->localpart);
            jid_destroy(jidp);
        }
    }
    prefs_free_string(roombypref);
    if ((g_strcmp0(unreadpos, "after") == 0) && mucwin->unread > 0) {
        g_string_append_printf(msg, " (%d)", mucwin->unread);
    }
    prefs_free_string(unreadpos);

    win_sub_newline_lazy(layout->subwin);
    gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);
    win_sub_print(layout->subwin, msg->str, FALSE, wrap, current_indent);
    g_string_free(msg, TRUE);

    if (mucwin->unread_mentions) {
        wattroff(layout->subwin, theme_attrs(THEME_ROSTER_ROOM_MENTION));
    } else if (mucwin->unread_triggers) {
        wattroff(layout->subwin, theme_attrs(THEME_ROSTER_ROOM_TRIGGER));
    } else if (mucwin->unread > 0) {
        wattroff(layout->subwin, theme_attrs(THEME_ROSTER_ROOM_UNREAD));
    } else {
        wattroff(layout->subwin, theme_attrs(THEME_ROSTER_ROOM));
    }

    char *privpref = prefs_get_string(PREF_ROSTER_PRIVATE);
    if (g_strcmp0(privpref, "room") == 0) {
        GList *privs = wins_get_private_chats(mucwin->roomjid);
        GList *curr = privs;
        while (curr) {
            ProfPrivateWin *privwin = curr->data;
            win_sub_newline_lazy(layout->subwin);

            GString *privmsg = g_string_new(" ");
            indent = prefs_get_roster_contact_indent();
            current_indent = 0;
            if (indent > 0) {
                current_indent += indent;
                while (indent > 0) {
                    g_string_append(privmsg, " ");
                    indent--;
                }
            }

            // TODO add preference
            indent = prefs_get_roster_resource_indent();
            if (indent > 0) {
                current_indent += indent;
                while (indent > 0) {
                    g_string_append(privmsg, " ");
                    indent--;
                }
            }

            unreadpos = prefs_get_string(PREF_ROSTER_ROOMS_UNREAD);
            if ((g_strcmp0(unreadpos, "before") == 0) && privwin->unread > 0) {
                g_string_append_printf(privmsg, "(%d) ", privwin->unread);
            }

            ch = prefs_get_roster_room_private_char();
            if (ch) {
                g_string_append_printf(privmsg, "%c", ch);
            }

            char *nick = privwin->fulljid + strlen(mucwin->roomjid) + 1;
            g_string_append(privmsg, nick);

            if ((g_strcmp0(unreadpos, "after") == 0) && privwin->unread > 0) {
                g_string_append_printf(privmsg, " (%d)", privwin->unread);
            }
            prefs_free_string(unreadpos);

            const char *presence = "offline";

            Occupant *occupant = muc_roster_item(mucwin->roomjid, nick);
            if (occupant) {
                presence = string_from_resource_presence(occupant->presence);
            }

            theme_item_t colour;
            if (privwin->unread > 0) {
                colour = _get_roster_theme(ROSTER_CONTACT_UNREAD, presence);
            } else {
                colour = _get_roster_theme(ROSTER_CONTACT_ACTIVE, presence);
            }

            wattron(layout->subwin, theme_attrs(colour));
            win_sub_print(layout->subwin, privmsg->str, FALSE, wrap, current_indent);
            wattroff(layout->subwin, theme_attrs(colour));

            g_string_free(privmsg, TRUE);
            curr = g_list_next(curr);
        }

        g_list_free(privs);
    }

    prefs_free_string(privpref);
}

static void
_rosterwin_private_chats(ProfLayoutSplit *layout, GList *orphaned_privchats)
{
    GList *privs = NULL;

    char *privpref = prefs_get_string(PREF_ROSTER_PRIVATE);
    if (g_strcmp0(privpref, "group") == 0) {
        privs = wins_get_private_chats(NULL);
    } else {
        GList *curr = orphaned_privchats;
        while (curr) {
            privs = g_list_append(privs, curr->data);
            curr = g_list_next(curr);
        }
    }

    if (privs || prefs_get_boolean(PREF_ROSTER_EMPTY)) {
        _rosterwin_private_header(layout, privs);

        GList *curr = privs;
        while (curr) {
            ProfPrivateWin *privwin = curr->data;
            win_sub_newline_lazy(layout->subwin);

            GString *privmsg = g_string_new(" ");
            int indent = prefs_get_roster_contact_indent();
            int current_indent = 0;
            if (indent > 0) {
                current_indent += indent;
                while (indent > 0) {
                    g_string_append(privmsg, " ");
                    indent--;
                }
            }

            char *unreadpos = prefs_get_string(PREF_ROSTER_ROOMS_UNREAD);
            if ((g_strcmp0(unreadpos, "before") == 0) && privwin->unread > 0) {
                g_string_append_printf(privmsg, "(%d) ", privwin->unread);
            }

            char ch = prefs_get_roster_private_char();
            if (ch) {
                g_string_append_printf(privmsg, "%c", ch);
            }

            g_string_append(privmsg, privwin->fulljid);

            if ((g_strcmp0(unreadpos, "after") == 0) && privwin->unread > 0) {
                g_string_append_printf(privmsg, " (%d)", privwin->unread);
            }
            prefs_free_string(unreadpos);

            Jid *jidp = jid_create(privwin->fulljid);
            Occupant *occupant = muc_roster_item(jidp->barejid, jidp->resourcepart);
            jid_destroy(jidp);

            const char *presence = "offline";
            if (occupant) {
                presence = string_from_resource_presence(occupant->presence);
            }

            theme_item_t colour;
            if (privwin->unread > 0) {
                colour = _get_roster_theme(ROSTER_CONTACT_UNREAD, presence);
            } else {
                colour = _get_roster_theme(ROSTER_CONTACT_ACTIVE, presence);
            }

            gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);

            wattron(layout->subwin, theme_attrs(colour));
            win_sub_print(layout->subwin, privmsg->str, FALSE, wrap, current_indent);
            wattroff(layout->subwin, theme_attrs(colour));

            g_string_free(privmsg, TRUE);
            curr = g_list_next(curr);
        }

        g_list_free(privs);
    }
}

static theme_item_t
_get_roster_theme(roster_contact_theme_t theme_type, const char *presence)
{
    switch (theme_type) {
    case ROSTER_CONTACT:        return theme_roster_presence_attrs(presence);
    case ROSTER_CONTACT_ACTIVE: return theme_roster_active_presence_attrs(presence);
    case ROSTER_CONTACT_UNREAD: return theme_roster_unread_presence_attrs(presence);
    default:                    return theme_roster_presence_attrs(presence);
    }
}

static int
_compare_rooms_name(ProfMucWin *a, ProfMucWin *b)
{
    return g_strcmp0(a->roomjid, b->roomjid);
}

static int
_compare_rooms_unread(ProfMucWin *a, ProfMucWin *b)
{
    if (a->unread > b->unread) {
        return -1;
    } else if (a->unread == b->unread) {
        return g_strcmp0(a->roomjid, b->roomjid);
    } else {
        return 1;
    }
}

static void
_rosterwin_unsubscribed_header(ProfLayoutSplit *layout, GList *wins)
{
    win_sub_newline_lazy(layout->subwin);

    GString *header = g_string_new(" ");
    char ch = prefs_get_roster_header_char();
    if (ch) {
        g_string_append_printf(header, "%c", ch);
    }

    g_string_append(header, "Unsubscribed");

    char *countpref = prefs_get_string(PREF_ROSTER_COUNT);
    if (g_strcmp0(countpref, "items") == 0) {
        int itemcount = g_list_length(wins);
        if (itemcount == 0 && prefs_get_boolean(PREF_ROSTER_COUNT_ZERO)) {
            g_string_append_printf(header, " (%d)", itemcount);
        } else {
            g_string_append_printf(header, " (%d)", itemcount);
        }
    } else if (g_strcmp0(countpref, "unread") == 0) {
        int unreadcount = 0;
        GList *curr = wins;
        while (curr) {
            ProfChatWin *chatwin = curr->data;
            unreadcount += chatwin->unread;
            curr = g_list_next(curr);
        }
        if (unreadcount == 0 && prefs_get_boolean(PREF_ROSTER_COUNT_ZERO)) {
            g_string_append_printf(header, " (%d)", unreadcount);
        } else if (unreadcount > 0) {
            g_string_append_printf(header, " (%d)", unreadcount);
        }
    }
    prefs_free_string(countpref);

    gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);

    wattron(layout->subwin, theme_attrs(THEME_ROSTER_HEADER));
    win_sub_print(layout->subwin, header->str, FALSE, wrap, 1);
    wattroff(layout->subwin, theme_attrs(THEME_ROSTER_HEADER));

    g_string_free(header, TRUE);
}

static void
_rosterwin_contacts_header(ProfLayoutSplit *layout, const char *const title, GSList *contacts)
{
    win_sub_newline_lazy(layout->subwin);

    GString *header = g_string_new(" ");
    char ch = prefs_get_roster_header_char();
    if (ch) {
        g_string_append_printf(header, "%c", ch);
    }

    g_string_append(header, title);

    char *countpref = prefs_get_string(PREF_ROSTER_COUNT);
    if (g_strcmp0(countpref, "items") == 0) {
        int itemcount = g_slist_length(contacts);
        if (itemcount == 0 && prefs_get_boolean(PREF_ROSTER_COUNT_ZERO)) {
            g_string_append_printf(header, " (%d)", itemcount);
        } else {
            g_string_append_printf(header, " (%d)", itemcount);
        }
    } else if (g_strcmp0(countpref, "unread") == 0) {
        int unreadcount = 0;
        GSList *curr = contacts;
        while (curr) {
            PContact contact = curr->data;
            const char *barejid = p_contact_barejid(contact);
            ProfChatWin *chatwin = wins_get_chat(barejid);
            if (chatwin) {
                unreadcount += chatwin->unread;
            }
            curr = g_slist_next(curr);
        }
        if (unreadcount == 0 && prefs_get_boolean(PREF_ROSTER_COUNT_ZERO)) {
            g_string_append_printf(header, " (%d)", unreadcount);
        } else if (unreadcount > 0) {
            g_string_append_printf(header, " (%d)", unreadcount);
        }
    }
    prefs_free_string(countpref);

    gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);

    wattron(layout->subwin, theme_attrs(THEME_ROSTER_HEADER));
    win_sub_print(layout->subwin, header->str, FALSE, wrap, 1);
    wattroff(layout->subwin, theme_attrs(THEME_ROSTER_HEADER));

    g_string_free(header, TRUE);
}

static void
_rosterwin_rooms_header(ProfLayoutSplit *layout, GList *rooms, char *title)
{
    win_sub_newline_lazy(layout->subwin);
    GString *header = g_string_new(" ");
    char ch = prefs_get_roster_header_char();
    if (ch) {
        g_string_append_printf(header, "%c", ch);
    }
    g_string_append(header, title);

    char *countpref = prefs_get_string(PREF_ROSTER_COUNT);
    if (g_strcmp0(countpref, "items") == 0) {
        int count = g_list_length(rooms);
        if (count == 0 && prefs_get_boolean(PREF_ROSTER_COUNT_ZERO)) {
            g_string_append_printf(header, " (%d)", count);
        } else if (count > 0) {
            g_string_append_printf(header, " (%d)", count);
        }
    } else if (g_strcmp0(countpref, "unread") == 0) {
        int unread = 0;
        GList *curr = rooms;
        while (curr) {
            ProfMucWin *mucwin = curr->data;
            unread += mucwin->unread;

            // include private chats
            char *prefpriv = prefs_get_string(PREF_ROSTER_PRIVATE);
            if (g_strcmp0(prefpriv, "room") == 0) {
                GList *privwins = wins_get_private_chats(mucwin->roomjid);
                GList *curr_priv = privwins;
                while (curr_priv) {
                    ProfPrivateWin *privwin = curr_priv->data;
                    unread += privwin->unread;
                    curr_priv = g_list_next(curr_priv);
                }
                g_list_free(privwins);
            }
            prefs_free_string(prefpriv);

            curr = g_list_next(curr);
        }

        if (unread == 0 && prefs_get_boolean(PREF_ROSTER_COUNT_ZERO)) {
            g_string_append_printf(header, " (%d)", unread);
        } else if (unread > 0) {
            g_string_append_printf(header, " (%d)", unread);
        }
    }
    prefs_free_string(countpref);

    gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);

    wattron(layout->subwin, theme_attrs(THEME_ROSTER_HEADER));
    win_sub_print(layout->subwin, header->str, FALSE, wrap, 1);
    wattroff(layout->subwin, theme_attrs(THEME_ROSTER_HEADER));

    g_string_free(header, TRUE);
}

static void
_rosterwin_private_header(ProfLayoutSplit *layout, GList *privs)
{
    win_sub_newline_lazy(layout->subwin);

    GString *title_str = g_string_new(" ");
    char ch = prefs_get_roster_header_char();
    if (ch) {
        g_string_append_printf(title_str, "%c", ch);
    }
    g_string_append(title_str, "Private chats");

    char *countpref = prefs_get_string(PREF_ROSTER_COUNT);
    if (g_strcmp0(countpref, "items") == 0) {
        int itemcount = g_list_length(privs);
        if (itemcount == 0 && prefs_get_boolean(PREF_ROSTER_COUNT_ZERO)) {
            g_string_append_printf(title_str, " (%d)", itemcount);
        } else if (itemcount > 0) {
            g_string_append_printf(title_str, " (%d)", itemcount);
        }
    } else if (g_strcmp0(countpref, "unread") == 0) {
        int unreadcount = 0;
        GList *curr = privs;
        while (curr) {
            ProfPrivateWin *privwin = curr->data;
            unreadcount += privwin->unread;
            curr = g_list_next(curr);
        }
        if (unreadcount == 0 && prefs_get_boolean(PREF_ROSTER_COUNT_ZERO)) {
            g_string_append_printf(title_str, " (%d)", unreadcount);
        } else if (unreadcount > 0) {
            g_string_append_printf(title_str, " (%d)", unreadcount);
        }
    }
    prefs_free_string(countpref);

    gboolean wrap = prefs_get_boolean(PREF_ROSTER_WRAP);

    wattron(layout->subwin, theme_attrs(THEME_ROSTER_HEADER));
    win_sub_print(layout->subwin, title_str->str, FALSE, wrap, 1);
    wattroff(layout->subwin, theme_attrs(THEME_ROSTER_HEADER));

    g_string_free(title_str, TRUE);
}

static GSList*
_filter_contacts(GSList *contacts)
{
    GSList *filtered_contacts = NULL;

    // if show offline, include all contacts
    if (prefs_get_boolean(PREF_ROSTER_OFFLINE)) {
        GSList *curr = contacts;
        while (curr) {
            filtered_contacts = g_slist_append(filtered_contacts, curr->data);
            curr = g_slist_next(curr);
        }
    // if dont show offline
    } else {
        GSList *curr = contacts;
        while (curr) {
            PContact contact = curr->data;
            const char *presence = p_contact_presence(contact);

            // include if offline and unread messages
            if (g_strcmp0(presence, "offline") == 0) {
                ProfChatWin *chatwin = wins_get_chat(p_contact_barejid(contact));
                if (chatwin && chatwin->unread > 0) {
                    filtered_contacts = g_slist_append(filtered_contacts, contact);
                }

            // include if not offline
            } else {
                filtered_contacts = g_slist_append(filtered_contacts, contact);
            }
            curr = g_slist_next(curr);
        }
    }

    return filtered_contacts;
}

static GSList*
_filter_contacts_with_presence(GSList *contacts, const char *const presence)
{
    GSList *filtered_contacts = NULL;

    // handling offline contacts
    if (g_strcmp0(presence, "offline") == 0) {

        // if show offline, include all contacts
        if (prefs_get_boolean(PREF_ROSTER_OFFLINE)) {
            GSList *curr = contacts;
            while (curr) {
                filtered_contacts = g_slist_append(filtered_contacts, curr->data);
                curr = g_slist_next(curr);
            }

        // otherwise show if unread messages
        } else {
            GSList *curr = contacts;
            while (curr) {
                PContact contact = curr->data;
                ProfChatWin *chatwin = wins_get_chat(p_contact_barejid(contact));
                if (chatwin && chatwin->unread > 0) {
                    filtered_contacts = g_slist_append(filtered_contacts, contact);
                }
                curr = g_slist_next(curr);
            }
        }

    // any other presence, include all
    } else {
        GSList *curr = contacts;
        while (curr) {
            filtered_contacts = g_slist_append(filtered_contacts, curr->data);
            curr = g_slist_next(curr);
        }
    }

    return filtered_contacts;
}