Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UserModel.cpp « mumble « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 474585eb2a80f8b43e3e67566208fa0e0c175e9c (plain)
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
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
// Copyright 2009-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "UserModel.h"

#include "Channel.h"
#include "ClientUser.h"
#include "Database.h"
#include "LCD.h"
#include "Log.h"
#include "MainWindow.h"
#include "Message.h"
#ifdef USE_OVERLAY
#	include "Overlay.h"
#endif
#include "ChannelListener.h"
#include "ServerHandler.h"
#include "Usage.h"
#include "User.h"
#include "Global.h"

#include <QtCore/QMimeData>
#include <QtCore/QStack>
#include <QtGui/QImageReader>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QToolTip>
#include <QtWidgets/QWhatsThis>

QHash< const Channel *, ModelItem * > ModelItem::c_qhChannels;
QHash< const ClientUser *, ModelItem * > ModelItem::c_qhUsers;
QHash< const ClientUser *, QList< ModelItem * > > ModelItem::s_userProxies;
bool ModelItem::bUsersTop = false;

ModelItem::ModelItem(Channel *c) {
	this->cChan      = c;
	this->pUser      = nullptr;
	this->isListener = false;
	bCommentSeen     = true;
	c_qhChannels.insert(c, this);
	parent = c_qhChannels.value(c->cParent);
	iUsers = 0;
}

ModelItem::ModelItem(ClientUser *p, bool isListener) {
	this->cChan      = nullptr;
	this->pUser      = p;
	this->isListener = isListener;
	bCommentSeen     = true;
	if (isListener) {
		// The way operator[] works for a QHash is that it'll insert a default-constructed
		// object first, before returning a reference to it, in case there is no entry for
		// the provided key yet. Thus we never have to worry about explicitly adding an empty
		// list for a new user before accessing it.
		s_userProxies[p] << this;
	} else {
		c_qhUsers.insert(p, this);
	}
	parent = c_qhChannels.value(p->cChannel);
	iUsers = 0;
}

ModelItem::ModelItem(ModelItem *i) {
	// Create a shallow clone
	this->cChan        = i->cChan;
	this->pUser        = i->pUser;
	this->parent       = i->parent;
	this->bCommentSeen = i->bCommentSeen;
	this->isListener   = i->isListener;

	if (pUser) {
		if (isListener) {
			s_userProxies[pUser] << this;
		} else {
			c_qhUsers.insert(pUser, this);
		}
	} else if (cChan)
		c_qhChannels.insert(cChan, this);

	iUsers = i->iUsers;
}

ModelItem::~ModelItem() {
	Q_ASSERT(qlChildren.count() == 0);

	if (cChan && c_qhChannels.value(cChan) == this)
		c_qhChannels.remove(cChan);
	if (pUser) {
		if (isListener) {
			s_userProxies[pUser].removeAll(this);
		} else {
			if (c_qhUsers.value(pUser) == this)
				c_qhUsers.remove(pUser);
		}
	}
}

void ModelItem::wipe() {
	foreach (ModelItem *i, qlChildren) {
		i->wipe();
		delete i;
	}
	qlChildren.clear();
	iUsers = 0;
}

ModelItem *ModelItem::child(int idx) const {
	if (!validRow(idx))
		return nullptr;

	return qlChildren.at(idx);
}

bool ModelItem::validRow(int idx) const {
	return ((idx >= 0) && (idx < qlChildren.count()));
}

ClientUser *ModelItem::userAt(int idx) const {
	if (!validRow(idx))
		return nullptr;
	return qlChildren.at(idx)->pUser;
}

Channel *ModelItem::channelAt(int idx) const {
	if (!validRow(idx))
		return nullptr;
	return qlChildren.at(idx)->cChan;
}

int ModelItem::rowOf(Channel *c) const {
	for (int i = 0; i < qlChildren.count(); i++)
		if (qlChildren.at(i)->cChan == c)
			return i;
	return -1;
}

int ModelItem::rowOf(ClientUser *p, const bool isListener) const {
	for (int i = 0; i < qlChildren.count(); i++)
		if (qlChildren.at(i)->isListener == isListener && qlChildren.at(i)->pUser == p)
			return i;
	return -1;
}

int ModelItem::rowOfSelf() const {
	// Root?
	if (!parent)
		return 0;

	if (pUser)
		return parent->rowOf(pUser, isListener);
	else
		return parent->rowOf(cChan);
}

int ModelItem::rows() const {
	return qlChildren.count();
}

int ModelItem::insertIndex(Channel *c) const {
	QList< Channel * > qlpc;
	ModelItem *item;

	int ocount = 0;

	foreach (item, qlChildren) {
		if (item->cChan) {
			if (item->cChan != c) {
				qlpc << item->cChan;
			}
		} else
			ocount++;
	}
	qlpc << c;
	std::sort(qlpc.begin(), qlpc.end(), Channel::lessThan);
	return qlpc.indexOf(c) + (bUsersTop ? ocount : 0);
}

int ModelItem::insertIndex(ClientUser *p, bool isListener) const {
	QList< ClientUser * > qlclientuser;
	ModelItem *item;

	int ocount        = 0;
	int listenerCount = 0;

	foreach (item, qlChildren) {
		if (item->pUser) {
			if (item->pUser != p) {
				// Make sure listeners and non-listeners are all grouped together and not mixed
				if ((isListener && item->isListener) || (!isListener && !item->isListener)) {
					qlclientuser << item->pUser;
				}
			}

			if (item->isListener) {
				listenerCount++;
			}
		} else {
			ocount++;
		}
	}

	qlclientuser << p;
	std::sort(qlclientuser.begin(), qlclientuser.end(), ClientUser::lessThan);

	// Make sure that the a user is always added to other users either all above or all below
	// sub-channels) and also make sure that listeners are grouped together and directly above
	// normal users.
	return qlclientuser.indexOf(p) + (bUsersTop ? 0 : ocount) + (isListener ? 0 : listenerCount);
}

QString ModelItem::hash() const {
	if (pUser) {
		if (!pUser->qsHash.isEmpty())
			return pUser->qsHash + (isListener ? QLatin1String("l") : QString());
		else
			return QLatin1String(sha1(pUser->qsName + (isListener ? QLatin1String("l") : QString())).toHex());
	} else {
		QCryptographicHash chash(QCryptographicHash::Sha1);

		chash.addData(cChan->qsName.toUtf8());
		chash.addData(QString::number(cChan->iId).toUtf8());
		if (Global::get().sh && Global::get().sh->isRunning()) {
			QString host, user, pw;
			unsigned short port;
			Global::get().sh->getConnectionInfo(host, port, user, pw);
			chash.addData(host.toUtf8());
			chash.addData(QString::number(port).toUtf8());
		}
		return QLatin1String(chash.result().toHex());
	}
}

UserModel::UserModel(QObject *p) : QAbstractItemModel(p) {
	qiTalkingOff      = QIcon(QLatin1String("skin:talking_off.svg"));
	qiTalkingOn       = QIcon(QLatin1String("skin:talking_on.svg"));
	qiTalkingMuted    = QIcon(QLatin1String("skin:talking_muted.svg"));
	qiTalkingShout    = QIcon(QLatin1String("skin:talking_alt.svg"));
	qiTalkingWhisper  = QIcon(QLatin1String("skin:talking_whisper.svg"));
	qiPrioritySpeaker = QIcon(QLatin1String("skin:priority_speaker.svg"));
	qiRecording       = QIcon(QLatin1String("skin:actions/media-record.svg"));
	qiMutedPushToMute.addFile(QLatin1String("skin:muted_pushtomute.svg"));
	qiMutedSelf       = QIcon(QLatin1String("skin:muted_self.svg"));
	qiMutedServer     = QIcon(QLatin1String("skin:muted_server.svg"));
	qiMutedLocal      = QIcon(QLatin1String("skin:muted_local.svg"));
	qiIgnoredLocal    = QIcon(QLatin1String("skin:status/text-missing.svg"));
	qiMutedSuppressed = QIcon(QLatin1String("skin:muted_suppressed.svg"));
	qiDeafenedSelf    = QIcon(QLatin1String("skin:deafened_self.svg"));
	qiDeafenedServer  = QIcon(QLatin1String("skin:deafened_server.svg"));
	qiAuthenticated   = QIcon(QLatin1String("skin:authenticated.svg"));
	qiChannel         = QIcon(QLatin1String("skin:channel.svg"));
	qiActiveChannel   = QIcon(QLatin1String("skin:channel_active.svg"));
	qiLinkedChannel   = QIcon(QLatin1String("skin:channel_linked.svg"));
	qiFriend          = QIcon(QLatin1String("skin:emblems/emblem-favorite.svg"));
	qiComment         = QIcon(QLatin1String("skin:comment.svg"));
	qiCommentSeen     = QIcon(QLatin1String("skin:comment_seen.svg"));
	qiFilter          = QIcon(QLatin1String("skin:filter.svg"));
	qiLock_locked     = QIcon(QLatin1String("skin:lock_locked.svg"));
	qiLock_unlocked   = QIcon(QLatin1String("skin:lock_unlocked.svg"));
	qiEar             = QIcon(QLatin1String("skin:ear.svg"));

	ModelItem::bUsersTop = Global::get().s.bUserTop;

	uiSessionComment    = 0;
	iChannelDescription = -1;
	bClicked            = false;

	miRoot = new ModelItem(Channel::get(0));
}

UserModel::~UserModel() {
	removeAll();
	Q_ASSERT(ModelItem::c_qhUsers.count() == 0);
	Q_ASSERT(ModelItem::c_qhChannels.count() == 1);
	delete miRoot;
}


int UserModel::columnCount(const QModelIndex &) const {
	return 1;
}

QModelIndex UserModel::index(int row, int column, const QModelIndex &p) const {
	ModelItem *item;
	QModelIndex idx = QModelIndex();

	if ((row < 0) || (column < 0) || (column > 1)) {
		return QModelIndex();
	}

	if (!p.isValid()) {
		return createIndex(row, column, miRoot);
	} else {
		item = static_cast< ModelItem * >(p.internalPointer());
	}

	if (!item)
		return idx;

	if (!item->validRow(row))
		return idx;

	idx = createIndex(row, column, item->child(row));

	return idx;
}

QModelIndex UserModel::index(ClientUser *p, int column) const {
	ModelItem *item = ModelItem::c_qhUsers.value(p);
	Q_ASSERT(p);
	Q_ASSERT(item);
	if (!p || !item)
		return QModelIndex();
	QModelIndex idx = createIndex(item->rowOfSelf(), column, item);
	return idx;
}

QModelIndex UserModel::index(Channel *c, int column) const {
	ModelItem *item = ModelItem::c_qhChannels.value(c);
	Q_ASSERT(c);
	Q_ASSERT(item);
	if (!item || !c)
		return QModelIndex();
	QModelIndex idx = createIndex(item->rowOfSelf(), column, item);
	return idx;
}

QModelIndex UserModel::index(ModelItem *item) const {
	return createIndex(item->rowOfSelf(), 0, item);
}

QModelIndex UserModel::channelListenerIndex(const ClientUser *user, const Channel *channel, int column) const {
	QList< ModelItem * > items = ModelItem::s_userProxies.value(user);

	ModelItem *item = nullptr;
	for (ModelItem *currentItem : items) {
		ModelItem *parent = currentItem->parent;
		if (currentItem->isListener && parent && parent->cChan == channel) {
			item = currentItem;
			break;
		}
	}

	Q_ASSERT(user);
	Q_ASSERT(channel);
	Q_ASSERT(item);
	if (!item || !channel || !user) {
		return QModelIndex();
	}

	QModelIndex idx = createIndex(item->rowOfSelf(), column, item);

	return idx;
}

QModelIndex UserModel::parent(const QModelIndex &idx) const {
	if (!idx.isValid())
		return QModelIndex();

	ModelItem *item = static_cast< ModelItem * >(idx.internalPointer());

	ModelItem *parent_item = item ? item->parent : nullptr;

	if (!parent_item)
		return QModelIndex();

	return createIndex(parent_item->rowOfSelf(), 0, parent_item);
}

int UserModel::rowCount(const QModelIndex &p) const {
	ModelItem *item;

	int val = 0;

	if (!p.isValid())
		return 1;
	else
		item = static_cast< ModelItem * >(p.internalPointer());

	if (!item || (p.column() != 0))
		return 0;

	val = item->rows();

	return val;
}

QString UserModel::stringIndex(const QModelIndex &idx) const {
	ModelItem *item = static_cast< ModelItem * >(idx.internalPointer());
	if (!idx.isValid())
		return QLatin1String("invIdx");
	if (!item)
		return QLatin1String("invPtr");
	if (item->pUser)
		return QString::fromLatin1("P:%1 [%2,%3]").arg(item->pUser->qsName).arg(idx.row()).arg(idx.column());
	else
		return QString::fromLatin1("C:%1 [%2,%3]").arg(item->cChan->qsName).arg(idx.row()).arg(idx.column());
}

QModelIndex UserModel::getSelectedIndex() const {
	QTreeView *v = Global::get().mw->qtvUsers;
	if (v) {
		QItemSelectionModel *sel = v->selectionModel();

		return sel->currentIndex();
	}

	return QModelIndex();
}

QVariant UserModel::data(const QModelIndex &idx, int role) const {
	if (!idx.isValid())
		return QVariant();

	ModelItem *item = static_cast< ModelItem * >(idx.internalPointer());

	Channel *c        = item->cChan;
	ClientUser *p     = item->pUser;
	ClientUser *pSelf = ClientUser::get(Global::get().uiSession);

	if (!c && !p) {
		return QVariant();
	}

	QVariant v = otherRoles(idx, role);
	if (v.isValid())
		return v;

	QList< QVariant > l;

	if (p) {
		switch (role) {
			case Qt::DecorationRole:
				if (idx.column() == 0) {
					if (item->isListener) {
						return qiEar;
					} else {
						// Select the talking-state symbol to display
						if (p == pSelf && p->bSelfMute) {
							// This is a workaround for a bug that can lead to the user having muted him/herself but
							// the talking icon is stuck at qiTalkingOn for some reason.
							// Until someone figures out how to fix the root of the problem, we'll have this workaround
							// to cure the symptoms of the bug.
							return qiTalkingOff;
						}

						switch (p->tsState) {
							case Settings::Talking:
								return qiTalkingOn;
							case Settings::MutedTalking:
								return qiTalkingMuted;
							case Settings::Whispering:
								return qiTalkingWhisper;
							case Settings::Shouting:
								return qiTalkingShout;
							case Settings::Passive:
							default:
								return qiTalkingOff;
						}
					}
				}
				break;
			case Qt::FontRole:
				if ((idx.column() == 0) && (p->uiSession == Global::get().uiSession)) {
					QFont f = Global::get().mw->font();
					f.setBold(!f.bold());
					f.setItalic(item->isListener);
					return f;
				}
				if (item->isListener) {
					QFont f = Global::get().mw->font();
					f.setItalic(true);
					return f;
				}
				break;
			case Qt::DisplayRole:
				if (idx.column() == 0) {
					// Get the channel the user/listener is in
					const Channel *parentChannel = item->parent ? item->parent->cChan : nullptr;

					return createDisplayString(*p, item->isListener, parentChannel);
				}

				// Most of the following icons are for non-listeners (as listeners are merely proxies) only
				// but in order to not change the order of the icons, the condition is added to each case
				// individually instead of checking it up front.
				if (!p->qbaCommentHash.isEmpty() && !item->isListener)
					l << (item->bCommentSeen ? qiCommentSeen : qiComment);
				if (p->bPrioritySpeaker && !item->isListener)
					l << qiPrioritySpeaker;
				if (p->bRecording)
					l << qiRecording;
				// ClientUser doesn't contain a push-to-mute
				// state because it isn't sent to the server.
				// We can show the icon only for the local user.
				if (p == pSelf && Global::get().bPushToMute && !item->isListener)
					l << qiMutedPushToMute;
				if (p->bMute || item->isListener)
					l << qiMutedServer;
				if (p->bSuppress && !item->isListener)
					l << qiMutedSuppressed;
				if (p->bSelfMute && !item->isListener)
					l << qiMutedSelf;
				if (p->bLocalMute && !item->isListener)
					l << qiMutedLocal;
				if (p->bLocalIgnore && !item->isListener)
					l << qiIgnoredLocal;
				if (p->bDeaf)
					l << qiDeafenedServer;
				if (p->bSelfDeaf)
					l << qiDeafenedSelf;
				if (p->iId >= 0 && !item->isListener)
					l << qiAuthenticated;
				if (!p->qsFriendName.isEmpty() && !item->isListener)
					l << qiFriend;
				return l;
			default:
				break;
		}
	} else {
		switch (role) {
			case Qt::DecorationRole:
				if (idx.column() == 0) {
					if (Global::get().uiSession && qsLinked.contains(c)) {
						if (ClientUser::get(Global::get().uiSession)->cChannel == c)
							return qiActiveChannel;
						else
							return qiLinkedChannel;
					}
					return qiChannel;
				}
				break;
			case Qt::DisplayRole:
				if (idx.column() == 0) {
					if (!Global::get().s.bShowUserCount || item->iUsers == 0)
						return c->qsName;

					return QString::fromLatin1("%1 (%2)").arg(c->qsName).arg(item->iUsers);
				}
				if (!c->qbaDescHash.isEmpty())
					l << (item->bCommentSeen ? qiCommentSeen : qiComment);

				if (c->bFiltered)
					l << (qiFilter);

				// Show a lock icon for enter restricted channels
				if (c->hasEnterRestrictions.load()) {
					if (c->localUserCanEnter.load()) {
						l << qiLock_unlocked;
					} else {
						l << qiLock_locked;
					}
				}
				return l;
			case Qt::FontRole:
				if (Global::get().uiSession) {
					Channel *home = ClientUser::get(Global::get().uiSession)->cChannel;

					if ((c == home) || qsLinked.contains(c)) {
						QFont f = Global::get().mw->font();
						if (qsLinked.count() > 1)
							f.setItalic(!f.italic());
						if (c == home)
							f.setBold(!f.bold());
						return f;
					}
				}
				break;
			case Qt::BackgroundRole:
				if ((c->iId == 0) && Global::get().sh && Global::get().sh->isStrong()) {
					QColor qc(Qt::green);
					qc.setAlpha(32);
					return qc;
				}
				break;
			default:
				break;
		}
	}
	return QVariant();
}

Qt::ItemFlags UserModel::flags(const QModelIndex &idx) const {
	if (!idx.isValid())
		return Qt::ItemIsDropEnabled;

	if (idx.column() != 0)
		return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled;

	return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}

QVariant UserModel::otherRoles(const QModelIndex &idx, int role) const {
	ModelItem *item = static_cast< ModelItem * >(idx.internalPointer());
	ClientUser *p   = item->pUser;
	Channel *c      = item->cChan;
	int section     = idx.column();
	bool isUser     = p;

	switch (role) {
		case Qt::ToolTipRole:
			const_cast< UserModel * >(this)->uiSessionComment    = 0;
			const_cast< UserModel * >(this)->iChannelDescription = -1;
			const_cast< UserModel * >(this)->bClicked            = false;
			switch (section) {
				case 0: {
					if (isUser) {
						QString qsImage;
						if (!p->qbaTextureHash.isEmpty()) {
							if (p->qbaTexture.isEmpty()) {
								p->qbaTexture = Global::get().db->blob(p->qbaTextureHash);
								if (p->qbaTexture.isEmpty()) {
									MumbleProto::RequestBlob mprb;
									mprb.add_session_texture(p->uiSession);
									Global::get().sh->sendMessage(mprb);
								} else {
#ifdef USE_OVERLAY
									Global::get().o->verifyTexture(p);
#endif
								}
							}
							if (!p->qbaTexture.isEmpty()) {
								QBuffer qb(&p->qbaTexture);
								qb.open(QIODevice::ReadOnly);
								QImageReader qir(&qb, p->qbaTextureFormat);
								QSize sz = qir.size();
								if (sz.width() > 0) {
									qsImage = QString::fromLatin1("<img src=\"data:;base64,");
									qsImage.append(QString::fromLatin1(p->qbaTexture.toBase64().toPercentEncoding()));
									if (sz.width() > 128) {
										int targ = sz.width() / ((sz.width() + 127) / 128);
										qsImage.append(QString::fromLatin1("\" width=\"%1\" />").arg(targ));
									} else {
										qsImage.append(QString::fromLatin1("\" />"));
									}
								}
							}
						}

						if (p->qbaCommentHash.isEmpty()) {
							if (!qsImage.isEmpty())
								return qsImage;
							else
								return p->qsName;
						} else {
							if (p->qsComment.isEmpty()) {
								p->qsComment = QString::fromUtf8(Global::get().db->blob(p->qbaCommentHash));
								if (p->qsComment.isEmpty()) {
									const_cast< UserModel * >(this)->uiSessionComment = p->uiSession;

									MumbleProto::RequestBlob mprb;
									mprb.add_session_comment(p->uiSession);
									Global::get().sh->sendMessage(mprb);
									return QVariant();
								}
							}
							const_cast< UserModel * >(this)->seenComment(idx);
							QString base = Log::validHtml(p->qsComment);
							if (!qsImage.isEmpty())
								return QString::fromLatin1(
										   "<table><tr><td valign=\"top\">%1</td><td>%2</td></tr></table>")
									.arg(qsImage, base);
							return base;
						}
					} else {
						if (c->qbaDescHash.isEmpty()) {
							return c->qsName;
						} else {
							if (c->qsDesc.isEmpty()) {
								c->qsDesc = QString::fromUtf8(Global::get().db->blob(c->qbaDescHash));
								if (c->qsDesc.isEmpty()) {
									const_cast< UserModel * >(this)->iChannelDescription = c->iId;

									MumbleProto::RequestBlob mprb;
									mprb.add_channel_description(c->iId);
									Global::get().sh->sendMessage(mprb);
									return QVariant();
								}
							}

							const_cast< UserModel * >(this)->seenComment(idx);
							return Log::validHtml(c->qsDesc);
						}
					}
				} break;
				case 1:
					return isUser ? p->getFlagsString() : QVariant();
			}
			break;
		case Qt::WhatsThisRole:
			switch (section) {
				case 0:
					if (isUser)
						return QString::fromLatin1("%1"
												   "<table>"
												   "<tr><td><img src=\"skin:talking_on.svg\" height=64 /></td><td "
												   "valign=\"middle\">%2</td></tr>"
												   "<tr><td><img src=\"skin:talking_alt.svg\" height=64 /></td><td "
												   "valign=\"middle\">%3</td></tr>"
												   "<tr><td><img src=\"skin:talking_whisper.svg\" height=64 /></td><td "
												   "valign=\"middle\">%4</td></tr>"
												   "<tr><td><img src=\"skin:talking_off.svg\" height=64 /></td><td "
												   "valign=\"middle\">%5</td></tr>"
												   "<tr><td><img src=\"skin:talking_muted.svg\" height=64 /></td><td "
												   "valign=\"middle\">%6</td></tr>"
												   "<tr><td><img src=\"skin:talking_muted.svg\" height=64 /></td><td "
												   "valign=\"middle\">%6</td></tr>"
												   "<tr><td><img src=\"skin:ear.svg\" height=64 /></td><td "
												   "valign=\"middle\">%7</td></tr>"
												   "</table>")
							.arg(tr("This is a user connected to the server. The icon to the left of the user "
									"indicates whether or not they are talking:"),
								 tr("Talking to your channel."), tr("Shouting directly to your channel."),
								 tr("Whispering directly to you."), tr("Not talking."),
								 tr("Talking while being muted on your end"),
								 tr("This is a channel listener. The corresponding user hears everything you say in "
									"this channel."));
					else
						return QString::fromLatin1("%1"
												   "<table>"
												   "<tr><td><img src=\"skin:channel_active.svg\" height=64 /></td><td "
												   "valign=\"middle\">%2</td></tr>"
												   "<tr><td><img src=\"skin:channel_linked.svg\" height=64 /></td><td "
												   "valign=\"middle\">%3</td></tr>"
												   "<tr><td><img src=\"skin:channel.svg\" height=64 /></td><td "
												   "valign=\"middle\">%4</td></tr>"
												   "</table>")
							.arg(tr("This is a channel on the server. The icon indicates the state of the channel:"),
								 tr("Your current channel."),
								 tr("A channel that is linked with your channel. Linked channels can talk to each "
									"other."),
								 tr("A channel on the server that you are not linked to."));
				case 1:
					if (isUser)
						return QString::fromLatin1("%1"
												   "<table>"
												   "<tr><td><img src=\"skin:emblems/emblem-favorite.svg\" height=64 "
												   "/></td><td valign=\"middle\">%2</td></tr>"
												   "<tr><td><img src=\"skin:authenticated.svg\" height=64 /></td><td "
												   "valign=\"middle\">%3</td></tr>"
												   "<tr><td><img src=\"skin:muted_self.svg\" height=64 /></td><td "
												   "valign=\"middle\">%4</td></tr>"
												   "<tr><td><img src=\"skin:muted_server.svg\" height=64 /></td><td "
												   "valign=\"middle\">%5</td></tr>"
												   "<tr><td><img src=\"skin:muted_suppressed.svg\" height=64 "
												   "/></td><td valign=\"middle\">%6</td></tr>"
												   "<tr><td><img src=\"skin:muted_local.svg\" height=64 /></td><td "
												   "valign=\"middle\">%7</td></tr>"
												   "<tr><td><img src=\"skin:muted_pushtomute.svg\" height=64 "
												   "/></td><td valign=\"middle\">%8</td></tr>"
												   "<tr><td><img src=\"skin:deafened_self.svg\" height=64 /></td><td "
												   "valign=\"middle\">%9</td></tr>"
												   "<tr><td><img src=\"skin:deafened_server.svg\" height=64 /></td><td "
												   "valign=\"middle\">%10</td></tr>"
												   "<tr><td><img src=\"skin:comment.svg\" height=64 /></td><td "
												   "valign=\"middle\">%11</td></tr>"
												   "<tr><td><img src=\"skin:comment_seen.svg\" height=64 /></td><td "
												   "valign=\"middle\">%12</td></tr>"
												   "<tr><td><img src=\"skin:status/text-missing.svg\" height=64 "
												   "/></td><td valign=\"middle\">%13</td></tr>"
												   "</table>")
							.arg(tr("This shows the flags the user has on the server, if any:"),
								 tr("On your friend list"), tr("Authenticated user"),
								 tr("Muted (manually muted by self)"), tr("Muted (manually muted by admin)"),
								 tr("Muted (not allowed to speak in current channel)"),
								 tr("Muted (muted by you, only on your machine)"), tr("Muted (push-to-mute)"))
							.arg(tr("Deafened (by self)"), tr("Deafened (by admin)"),
								 tr("User has a new comment set (click to show)"),
								 tr("User has a comment set, which you've already seen. (click to show)"),
								 tr("Ignoring Text Messages"));
					else
						return QString::fromLatin1("%1"
												   "<table>"
												   "<tr><td><img src=\"skin:comment.svg\" height=64 /></td><td "
												   "valign=\"middle\">%10</td></tr>"
												   "<tr><td><img src=\"skin:comment_seen.svg\" height=64 /></td><td "
												   "valign=\"middle\">%11</td></tr>"
												   "<tr><td><img src=\"skin:filter.svg\" height=64 /></td><td "
												   "valign=\"middle\">%12</td></tr>"
												   "<tr><td><img src=\"skin:lock_locked.svg\" height=64 /></td><td "
												   "valign=\"middle\">%13</td></tr>"
												   "<tr><td><img src=\"skin:lock_unlocked.svg\" height=64 /></td><td "
												   "valign=\"middle\">%14</td></tr>"
												   "</table>")
							.arg(tr("This shows the flags the channel has, if any:"),
								 tr("Channel has a new comment set (click to show)"),
								 tr("Channel has a comment set, which you've already seen. (click to show)"),
								 tr("Channel will be hidden when filtering is enabled"),
								 tr("Channel has access restrictions so that you can't enter it"),
								 tr("Channel has access restrictions but you can enter nonetheless"));
			}
			break;
	}
	return QVariant();
}

QVariant UserModel::headerData(int section, Qt::Orientation orientation, int role) const {
	if (orientation != Qt::Horizontal)
		return QVariant();

	switch (role) {
		case Qt::DisplayRole:
			switch (section) {
				case 0:
					return tr("Name");
				case 1:
					return tr("Flags");
			}
	}

	return QVariant();
}

void UserModel::recursiveClone(const ModelItem *old, ModelItem *item, QModelIndexList &from, QModelIndexList &to) {
	if (old->qlChildren.isEmpty())
		return;

	beginInsertRows(index(item), 0, old->qlChildren.count());

	for (int i = 0; i < old->qlChildren.count(); ++i) {
		ModelItem *o  = old->qlChildren.at(i);
		ModelItem *mi = new ModelItem(o);
		mi->parent    = item;

		item->qlChildren << mi;

		from << createIndex(i, 0, o);
		from << createIndex(i, 1, o);
		to << createIndex(i, 0, mi);
		to << createIndex(i, 1, mi);
	}

	endInsertRows();

	for (int i = 0; i < old->qlChildren.count(); ++i)
		recursiveClone(old->qlChildren.at(i), item->qlChildren.at(i), from, to);
}

ModelItem *UserModel::moveItem(ModelItem *oldparent, ModelItem *newparent, ModelItem *oldItem) {
	// Here's the idea. We insert the item, update persistent indexes, THEN remove it.

	// Get the current position of the item under its parent (aka its "row")
	int oldrow = oldparent->qlChildren.indexOf(oldItem);

	// Get the row of the item at its new position. This depends on whether we're moving a
	// channel or a user.
	int newrow = -1;
	if (oldItem->cChan) {
		newrow = newparent->insertIndex(oldItem->cChan);
	} else {
		newrow = newparent->insertIndex(oldItem->pUser);
	}

	if ((oldparent == newparent) && (newrow == oldrow)) {
		// This is a no-op. We still claim that the data has changed in order
		// to trigger potential event handlers.
		emit dataChanged(index(oldItem), index(oldItem));
		return oldItem;
	}

	// Shallow clone. newItem is the new ModelItem that will be added to newparent
	ModelItem *newItem = new ModelItem(oldItem);

	// Store the index if it's "active".
	// The selection is stored as "from"-"to" pairs, so if we move up in the same channel,
	// we'd move only "from" and select half the channel.

	// Check whether the moved item is currently selected and if so, store it as a persistent
	// model index in active. Also clear the selection as we're going to mess with the active
	// item.
	QTreeView *v             = Global::get().mw->qtvUsers;
	QItemSelectionModel *sel = v->selectionModel();
	QPersistentModelIndex active;
	QModelIndex oindex = createIndex(oldrow, 0, oldItem);
	if (sel->isSelected(oindex) || (oindex == v->currentIndex())) {
		active = index(oldItem);
		v->clearSelection();
		v->setCurrentIndex(QModelIndex());
	}

	// Check whether the oldItem is currently expanded in order to restore the same
	// state once we have moved it.
	bool expanded = v->isExpanded(index(oldItem));

	if (newparent == oldparent) {
		// If the moving happens within the same parent, we have to watch out that we use the correct
		// row indices for our operation here.
		// As we're inserting the new item before remving the old one, newrow has to be the index
		// applicable before the removal (aka "as is" atm) whereas oldrow has to be applicable
		// after we've inserted the new item.
		if (oldrow >= newrow) {
			// The new item will be inserted above the old one. Thus we have to account for that extra
			// item in the used row index.
			oldrow++;
		} else {
			newrow++;
		}
	}

	// Insert the new item to its (new) parent
	beginInsertRows(index(newparent), newrow, newrow);
	newItem->parent = newparent;
	newparent->qlChildren.insert(newrow, newItem);

	if (oldItem->cChan) {
		// When moving a channel, we'll also have to move any sub-channels
		oldparent->cChan->removeChannel(oldItem->cChan);
		newparent->cChan->addChannel(oldItem->cChan);
	} else {
		newparent->cChan->addClientUser(oldItem->pUser);
	}

	endInsertRows();


	QModelIndexList from, to;
	from << createIndex(oldrow, 0, oldItem);
	from << createIndex(oldrow, 1, oldItem);
	to << createIndex(newrow, 0, newItem);
	to << createIndex(newrow, 1, newItem);

	// Clone all children of oldItem and attach them to newItem
	recursiveClone(oldItem, newItem, from, to);

	// Update all persistent model indices that are affected by our action here. This includes (but is in general
	// not limited to) the "active" index we potentially created above.
	changePersistentIndexList(from, to);

	// Now that we have added the new index, it is time to actually remove the old one
	beginRemoveRows(index(oldparent), oldrow, oldrow);
	oldparent->qlChildren.removeAt(oldrow);
	endRemoveRows();

	// oldItem is now longer needed as it is not present in the model anymore and all potential
	// references to it should be updated to point to the new (moved) item instead.
	// Thus we can delete it and all its children (which have been cloned and reference-updated
	// as well.
	oldItem->wipe();
	delete oldItem;

	if (active.isValid()) {
		// If the moved item has been previously selected, we restore that selection to now be the
		// new item using the "active" model index which has been updated to now point to the new
		// item.
		sel->select(active, QItemSelectionModel::SelectCurrent);
		v->setCurrentIndex(active);
	}

	if (expanded) {
		// If the old item (or rather the parent it has been living in) has been expanded,
		// restore that state for the new item.
		v->expand(index(newItem));
	}

	return newItem;
}

void UserModel::expandAll(Channel *c) {
	QStack< Channel * > chans;

	while (c) {
		chans.push(c);
		c = c->cParent;
	}
	while (!chans.isEmpty()) {
		c = chans.pop();
		Global::get().mw->qtvUsers->setExpanded(index(c), true);
	}
}

void UserModel::collapseEmpty(Channel *c) {
	while (c) {
		ModelItem *mi = ModelItem::c_qhChannels.value(c);
		if (mi->iUsers == 0)
			Global::get().mw->qtvUsers->setExpanded(index(c), false);
		else
			break;
		c = c->cParent;
	}
}

void UserModel::ensureSelfVisible() {
	if (!Global::get().uiSession)
		return;

	Global::get().mw->qtvUsers->scrollTo(index(ClientUser::get(Global::get().uiSession)));
}

void UserModel::recheckLinks() {
	if (!Global::get().uiSession)
		return;

	ClientUser *clientUser = ClientUser::get(Global::get().uiSession);
	if (!clientUser)
		return;

	bool bChanged = false;

	Channel *home = clientUser->cChannel;

	QSet< Channel * > all = home->allLinks();

	if (all == qsLinked)
		return;

	QSet< Channel * > changed = (all - qsLinked);
	changed += (qsLinked - all);

	if ((all.count() == 1) || (qsLinked.count() == 1))
		changed += home;

	qsLinked = all;

	foreach (Channel *c, changed) {
		QModelIndex idx = index(c);
		emit dataChanged(idx, idx);
		bChanged = true;
	}
	if (bChanged)
		updateOverlay();
}

ClientUser *UserModel::addUser(unsigned int id, const QString &name) {
	ClientUser *p = ClientUser::add(id, this);
	p->qsName     = name;

	ModelItem *item = new ModelItem(p);

	connect(p, SIGNAL(talkingStateChanged()), this, SLOT(userStateChanged()));
	connect(p, SIGNAL(muteDeafStateChanged()), this, SLOT(userStateChanged()));
	connect(p, SIGNAL(prioritySpeakerStateChanged()), this, SLOT(userStateChanged()));
	connect(p, SIGNAL(recordingStateChanged()), this, SLOT(userStateChanged()));
	connect(p, &ClientUser::localVolumeAdjustmentsChanged, this, &UserModel::userStateChanged);
	connect(p, &ClientUser::localNicknameChanged, this, &UserModel::userStateChanged);

	Channel *c       = Channel::get(0);
	ModelItem *citem = ModelItem::c_qhChannels.value(c);

	item->parent = citem;

	int row = citem->insertIndex(p);

	beginInsertRows(index(citem), row, row);
	citem->qlChildren.insert(row, item);
	c->addClientUser(p);
	endInsertRows();

	while (citem) {
		citem->iUsers++;
		citem = citem->parent;
	}

	updateOverlay();

	emit userAdded(p->uiSession);

	return p;
}

void UserModel::removeUser(ClientUser *p) {
	// First remove all listener proxies this user has at the moment
	removeChannelListener(p);

	if (Global::get().uiSession && p->uiSession == Global::get().uiSession)
		Global::get().uiSession = 0;
	Channel *c       = p->cChannel;
	ModelItem *item  = ModelItem::c_qhUsers.value(p);
	ModelItem *citem = ModelItem::c_qhChannels.value(c);

	int row = citem->qlChildren.indexOf(item);

	beginRemoveRows(index(citem), row, row);
	c->removeUser(p);
	citem->qlChildren.removeAt(row);
	endRemoveRows();

	p->cChannel = nullptr;

	ClientUser::remove(p);
	qmHashes.remove(p->qsHash);

	while (citem) {
		citem->iUsers--;
		citem = citem->parent;
	}

	if (Global::get().s.ceExpand == Settings::ChannelsWithUsers)
		collapseEmpty(c);

	updateOverlay();

	emit userRemoved(p->uiSession);

	delete p;
	delete item;
}

void UserModel::moveUser(ClientUser *p, Channel *np) {
	Channel *oc     = p->cChannel;
	ModelItem *opi  = ModelItem::c_qhChannels.value(oc);
	ModelItem *pi   = ModelItem::c_qhChannels.value(np);
	ModelItem *item = ModelItem::c_qhUsers.value(p);

	item = moveItem(opi, pi, item);

	if (p->uiSession == Global::get().uiSession) {
		ensureSelfVisible();
		recheckLinks();
	}

	while (opi) {
		opi->iUsers--;
		opi = opi->parent;
	}
	while (pi) {
		pi->iUsers++;
		pi = pi->parent;
	}

	if (Global::get().s.ceExpand == Settings::ChannelsWithUsers) {
		expandAll(np);
		collapseEmpty(oc);
	}

	updateOverlay();
}

void UserModel::renameUser(ClientUser *p, const QString &name) {
	Channel *c = p->cChannel;
	p->qsName  = name;

	ModelItem *pi   = ModelItem::c_qhChannels.value(c);
	ModelItem *item = ModelItem::c_qhUsers.value(p);
	moveItem(pi, pi, item);

	updateOverlay();
}

void UserModel::setUserId(ClientUser *p, int id) {
	p->iId          = id;
	QModelIndex idx = index(p, 0);
	emit dataChanged(idx, idx);
}

void UserModel::setHash(ClientUser *p, const QString &hash) {
	if (!p->qsHash.isEmpty())
		qmHashes.remove(p->qsHash);

	p->qsHash = hash;
	qmHashes.insert(p->qsHash, p);
}

void UserModel::setFriendName(ClientUser *p, const QString &name) {
	p->qsFriendName = name;
	QModelIndex idx = index(p, 0);
	emit dataChanged(idx, idx);
}

void UserModel::setComment(ClientUser *cu, const QString &comment) {
	cu->qbaCommentHash = comment.isEmpty() ? QByteArray() : sha1(comment);

	if (comment != cu->qsComment) {
		ModelItem *item = ModelItem::c_qhUsers.value(cu);
		int oldstate    = (cu->qsComment.isEmpty() && cu->qbaCommentHash.isEmpty()) ? 0 : (item->bCommentSeen ? 2 : 1);
		int newstate    = 0;

		cu->qsComment = comment;

		if (!comment.isEmpty()) {
			Global::get().db->setBlob(cu->qbaCommentHash, cu->qsComment.toUtf8());
			if (cu->uiSession == uiSessionComment) {
				uiSessionComment   = 0;
				item->bCommentSeen = false;
				if (bClicked) {
					QRect r = Global::get().mw->qtvUsers->visualRect(index(cu));
					QWhatsThis::showText(Global::get().mw->qtvUsers->viewport()->mapToGlobal(r.bottomRight()),
										 data(index(cu, 0), Qt::ToolTipRole).toString(), Global::get().mw->qtvUsers);
				} else {
					QToolTip::showText(QCursor::pos(), data(index(cu, 0), Qt::ToolTipRole).toString(),
									   Global::get().mw->qtvUsers);
				}
			} else if (cu->uiSession == ~uiSessionComment) {
				uiSessionComment = 0;
				if (cu->uiSession == Global::get().uiSession) {
					QTimer::singleShot(0, Global::get().mw, SLOT(on_qaSelfComment_triggered()));
				} else {
					Global::get().mw->cuContextUser = cu;
					QTimer::singleShot(0, Global::get().mw, SLOT(on_qaUserCommentView_triggered()));
				}
			} else {
				item->bCommentSeen = Global::get().db->seenComment(item->hash(), cu->qbaCommentHash);
				newstate           = item->bCommentSeen ? 2 : 1;
			}
		} else {
			item->bCommentSeen = true;
		}

		if (oldstate != newstate) {
			QModelIndex idx = index(cu, 0);
			emit dataChanged(idx, idx);
		}
	}
}

void UserModel::setCommentHash(ClientUser *cu, const QByteArray &hash) {
	if (hash != cu->qbaCommentHash) {
		ModelItem *item = ModelItem::c_qhUsers.value(cu);
		int oldstate    = (cu->qsComment.isEmpty() && cu->qbaCommentHash.isEmpty()) ? 0 : (item->bCommentSeen ? 2 : 1);
		int newstate;

		cu->qsComment      = QString();
		cu->qbaCommentHash = hash;

		item->bCommentSeen = Global::get().db->seenComment(item->hash(), cu->qbaCommentHash);
		newstate           = item->bCommentSeen ? 2 : 1;

		if (oldstate != newstate) {
			QModelIndex idx = index(cu, 0);
			emit dataChanged(idx, idx);
		}
	}
}

void UserModel::setComment(Channel *c, const QString &comment) {
	c->qbaDescHash = comment.isEmpty() ? QByteArray() : sha1(comment);

	if (comment != c->qsDesc) {
		ModelItem *item = ModelItem::c_qhChannels.value(c);
		int oldstate    = c->qsDesc.isEmpty() ? 0 : (item->bCommentSeen ? 2 : 1);
		int newstate    = 0;

		c->qsDesc = comment;

		if (!comment.isEmpty()) {
			Global::get().db->setBlob(c->qbaDescHash, c->qsDesc.toUtf8());

			if (c->iId == iChannelDescription) {
				iChannelDescription = -1;
				item->bCommentSeen  = false;
				if (bClicked) {
					QRect r = Global::get().mw->qtvUsers->visualRect(index(c));
					QWhatsThis::showText(Global::get().mw->qtvUsers->viewport()->mapToGlobal(r.bottomRight()),
										 data(index(c, 0), Qt::ToolTipRole).toString(), Global::get().mw->qtvUsers);
				} else {
					QToolTip::showText(QCursor::pos(), data(index(c, 0), Qt::ToolTipRole).toString(),
									   Global::get().mw->qtvUsers);
				}
			} else {
				item->bCommentSeen = Global::get().db->seenComment(item->hash(), c->qbaDescHash);
				newstate           = item->bCommentSeen ? 2 : 1;
			}
		} else {
			item->bCommentSeen = true;
		}

		if (oldstate != newstate) {
			QModelIndex idx = index(c, 0);
			emit dataChanged(idx, idx);
		}
	}
}

void UserModel::setCommentHash(Channel *c, const QByteArray &hash) {
	if (hash != c->qbaDescHash) {
		ModelItem *item = ModelItem::c_qhChannels.value(c);
		int oldstate    = (c->qsDesc.isEmpty() && c->qbaDescHash.isEmpty()) ? 0 : (item->bCommentSeen ? 2 : 1);
		int newstate;

		c->qsDesc      = QString();
		c->qbaDescHash = hash;

		item->bCommentSeen = Global::get().db->seenComment(item->hash(), hash);
		newstate           = item->bCommentSeen ? 2 : 1;

		if (oldstate != newstate) {
			QModelIndex idx = index(c, 0);
			emit dataChanged(idx, idx);
		}
	}
}

void UserModel::seenComment(const QModelIndex &idx) {
	ModelItem *item;
	item = static_cast< ModelItem * >(idx.internalPointer());

	if (item->bCommentSeen)
		return;

	item->bCommentSeen = true;

	emit dataChanged(idx, idx);

	if (item->pUser)
		Global::get().db->setSeenComment(item->hash(), item->pUser->qbaCommentHash);
	else
		Global::get().db->setSeenComment(item->hash(), item->cChan->qbaDescHash);
}

void UserModel::renameChannel(Channel *c, const QString &name) {
	c->qsName = name;

	if (c->iId == 0) {
		QModelIndex idx = index(c);
		emit dataChanged(idx, idx);
	} else {
		Channel *pc     = c->cParent;
		ModelItem *pi   = ModelItem::c_qhChannels.value(pc);
		ModelItem *item = ModelItem::c_qhChannels.value(c);

		moveItem(pi, pi, item);
	}

	emit channelRenamed(c->iId);
}

void UserModel::repositionChannel(Channel *c, const int position) {
	c->iPosition = position;

	if (c->iId == 0) {
		QModelIndex idx = index(c);
		emit dataChanged(idx, idx);
	} else {
		Channel *pc     = c->cParent;
		ModelItem *pi   = ModelItem::c_qhChannels.value(pc);
		ModelItem *item = ModelItem::c_qhChannels.value(c);

		moveItem(pi, pi, item);
	}
}

Channel *UserModel::addChannel(int id, Channel *p, const QString &name) {
	Channel *c = Channel::add(id, name);

	if (!c)
		return nullptr;

	ModelItem *item  = new ModelItem(c);
	ModelItem *citem = ModelItem::c_qhChannels.value(p);

	item->parent = citem;

	int row = citem->insertIndex(c);

	beginInsertRows(index(citem), row, row);
	p->addChannel(c);
	citem->qlChildren.insert(row, item);
	endInsertRows();

	if (Global::get().s.ceExpand == Settings::AllChannels)
		Global::get().mw->qtvUsers->setExpanded(index(item), true);


	emit channelAdded(c->iId);

	return c;
}

void UserModel::addChannelListener(ClientUser *p, Channel *c) {
	ModelItem *item  = new ModelItem(p, true);
	ModelItem *citem = ModelItem::c_qhChannels.value(c);

	item->parent = citem;

	int row = citem->insertIndex(p, true);

	beginInsertRows(index(citem), row, row);
	citem->qlChildren.insert(row, item);
	endInsertRows();

	while (citem) {
		citem->iUsers++;
		citem = citem->parent;
	}

	updateOverlay();
}

void UserModel::removeChannelListener(const ClientUser *p, const Channel *c) {
	// The way operator[] works for a QHash is that it'll insert a default-constructed
	// object first, before returning a reference to it, in case there is no entry for
	// the provided key yet. Thus we never have to worry about explicitly adding an empty
	// list for a new user before accessing it.
	const QList< ModelItem * > &items = ModelItem::s_userProxies[p];

	if (items.isEmpty()) {
		return;
	}

	if (c) {
		ModelItem *citem = ModelItem::c_qhChannels.value(c);

		ModelItem *item = nullptr;
		for (int i = 0; i < items.size(); i++) {
			if (citem->qlChildren.contains(items[i])) {
				item = items[i];
				break;
			}
		}

		if (item) {
			removeChannelListener(item, citem);
		} else {
			qCritical("UserModel::removeChannelListener: Can't find item for provided channel");
		}
	} else {
		// remove all items
		foreach (ModelItem *currentItem, items) { removeChannelListener(currentItem); }
	}
}

bool UserModel::isChannelListener(const QModelIndex &idx) const {
	if (!idx.isValid()) {
		return false;
	}

	ModelItem *item;
	item = static_cast< ModelItem * >(idx.internalPointer());

	return item->isListener;
}

void UserModel::setSelectedChannelListener(unsigned int userSession, int channelID) {
	QModelIndex idx = channelListenerIndex(ClientUser::get(userSession), Channel::get(channelID));

	if (!idx.isValid()) {
		return;
	}

	QTreeView *v = Global::get().mw->qtvUsers;
	if (v) {
		v->setCurrentIndex(idx);
	}
}

void UserModel::removeChannelListener(ModelItem *item, ModelItem *citem) {
	if (!citem) {
		citem = item->parent;
	}

	if (!item || !citem) {
		qCritical("UserModel::removeChannelListener: Invalid state encountered");
		return;
	}
	if (!citem->qlChildren.contains(item)) {
		qCritical("UserModel::removeChannelListener: Item does not match parent");
		return;
	}

	ClientUser *p = item->pUser;
	Channel *c    = citem->cChan;

	if (!p) {
		qCritical("UserModel::removeChannelListener: Can't find associated ClientUser");
		return;
	}
	if (!c) {
		qCritical("UserModel::removeChannelListener: Can't find associated Channel");
		return;
	}

	int row = citem->qlChildren.indexOf(item);

	beginRemoveRows(index(citem), row, row);
	citem->qlChildren.removeAt(row);
	endRemoveRows();

	while (citem) {
		citem->iUsers--;
		citem = citem->parent;
	}

	if (Global::get().s.ceExpand == Settings::ChannelsWithUsers)
		collapseEmpty(c);

	updateOverlay();

	delete item;
}

bool UserModel::removeChannel(Channel *c, const bool onlyIfUnoccupied) {
	const ModelItem *item = ModelItem::c_qhChannels.value(c);

	if (onlyIfUnoccupied && item->iUsers != 0)
		return false; // Checks full hierarchy

	foreach (const ModelItem *i, item->qlChildren) {
		if (i->pUser) {
			if (i->isListener) {
				removeChannelListener(i->pUser, c);
			} else {
				removeUser(i->pUser);
			}
		} else {
			removeChannel(i->cChan);
		}
	}

	Channel *p = c->cParent;

	if (!p)
		return true;

	ModelItem *citem = ModelItem::c_qhChannels.value(p);

	int row = citem->rowOf(c);

	beginRemoveRows(index(citem), row, row);
	p->removeChannel(c);
	citem->qlChildren.removeAt(row);
	qsLinked.remove(c);
	endRemoveRows();

	Channel::remove(c);

	emit channelRemoved(c->iId);

	delete item;
	delete c;
	return true;
}

void UserModel::moveChannel(Channel *c, Channel *p) {
	Channel *oc     = c->cParent;
	ModelItem *opi  = ModelItem::c_qhChannels.value(c->cParent);
	ModelItem *pi   = ModelItem::c_qhChannels.value(p);
	ModelItem *item = ModelItem::c_qhChannels.value(c);
	item            = moveItem(opi, pi, item);

	while (opi) {
		opi->iUsers -= item->iUsers;
		opi = opi->parent;
	}
	while (pi) {
		pi->iUsers += item->iUsers;
		pi = pi->parent;
	}

	ensureSelfVisible();

	if (Global::get().s.ceExpand == Settings::ChannelsWithUsers) {
		collapseEmpty(oc);
	}
}

void UserModel::linkChannels(Channel *c, QList< Channel * > links) {
	foreach (Channel *l, links)
		c->link(l);
	recheckLinks();
}

void UserModel::unlinkChannels(Channel *c, QList< Channel * > links) {
	foreach (Channel *l, links)
		c->unlink(l);
	recheckLinks();
}

void UserModel::unlinkAll(Channel *c) {
	c->unlink(nullptr);
	recheckLinks();
}

void UserModel::removeAll() {
	ModelItem *item = miRoot;
	ModelItem *i;

	uiSessionComment    = 0;
	iChannelDescription = -1;
	bClicked            = false;

	// in order to avoid complications, we remove all ChannelListeners first
	foreach (i, item->qlChildren) {
		if (i->pUser && i->isListener) {
			removeChannelListener(i, item);
		}
	}

	foreach (i, item->qlChildren) {
		if (i->pUser)
			removeUser(i->pUser);
		else
			removeChannel(i->cChan);
	}

	qsLinked.clear();

	updateOverlay();
}

ClientUser *UserModel::getUser(const QModelIndex &idx) const {
	if (!idx.isValid())
		return nullptr;

	ModelItem *item;
	item = static_cast< ModelItem * >(idx.internalPointer());

	return item->pUser;
}

ClientUser *UserModel::getUser(const QString &hash) const {
	return qmHashes.value(hash);
}

ClientUser *UserModel::getSelectedUser() const {
	QModelIndex selected = getSelectedIndex();

	if (selected.isValid()) {
		ModelItem *item = static_cast< ModelItem * >(selected.internalPointer());

		return item->pUser;
	}

	return nullptr;
}

void UserModel::setSelectedUser(unsigned int session) {
	QModelIndex idx = index(ClientUser::get(session));

	if (!idx.isValid()) {
		return;
	}

	QTreeView *v = Global::get().mw->qtvUsers;
	if (v) {
		v->setCurrentIndex(idx);
	}
}

Channel *UserModel::getChannel(const QModelIndex &idx) const {
	if (!idx.isValid())
		return nullptr;

	ModelItem *item;
	item = static_cast< ModelItem * >(idx.internalPointer());

	if (item->pUser)
		if (item->parent && item->parent->cChan) {
			return item->parent->cChan;
		} else {
			// Failsafe in case the item does not have a parent
			qWarning("UserModel::getChannel encountered weird program flow - that's a bug (please report)!");
			return item->pUser->cChannel;
		}
	else
		return item->cChan;
}

Channel *UserModel::getSelectedChannel() const {
	QModelIndex selected = getSelectedIndex();

	if (selected.isValid()) {
		ModelItem *item = static_cast< ModelItem * >(selected.internalPointer());

		return item->cChan;
	}

	return nullptr;
}

void UserModel::setSelectedChannel(int id) {
	QModelIndex idx = index(Channel::get(id));

	if (!idx.isValid()) {
		return;
	}

	QTreeView *v = Global::get().mw->qtvUsers;
	if (v) {
		v->setCurrentIndex(idx);
	}
}

Channel *UserModel::getSubChannel(Channel *p, int idx) const {
	ModelItem *item = ModelItem::c_qhChannels.value(p);
	if (!item)
		return nullptr;

	foreach (ModelItem *i, item->qlChildren) {
		if (i->cChan) {
			if (idx == 0)
				return i->cChan;
			idx--;
		}
	}
	return nullptr;
}

void UserModel::userStateChanged() {
	ClientUser *user = qobject_cast< ClientUser * >(sender());

	if (!user)
		return;

	const QModelIndex idx = index(user);
	emit dataChanged(idx, idx);

	updateOverlay();
}

void UserModel::on_channelListenerLocalVolumeAdjustmentChanged(int channelID, float oldValue, float newValue) {
	Q_UNUSED(oldValue);
	Q_UNUSED(newValue);

	const QModelIndex idx = channelListenerIndex(ClientUser::get(Global::get().uiSession), Channel::get(channelID));
	emit dataChanged(idx, idx);
}

void UserModel::toggleChannelFiltered(Channel *c) {
	QModelIndex idx;
	if (c) {
		c->bFiltered = !c->bFiltered;

		ServerHandlerPtr sh = Global::get().sh;
		Global::get().db->setChannelFiltered(sh->qbaDigest, c->iId, c->bFiltered);
		idx = index(c);
	}

	emit dataChanged(idx, idx);

	updateOverlay();
}

Qt::DropActions UserModel::supportedDropActions() const {
	return Qt::MoveAction;
}

QStringList UserModel::mimeTypes() const {
	QStringList sl;
	sl << QLatin1String("mumble/dragentry");
	return sl;
}

QMimeData *UserModel::mimeData(const QModelIndexList &idxs) const {
	QModelIndex idx;
	QByteArray qba;
	QDataStream ds(&qba, QIODevice::WriteOnly);

	foreach (idx, idxs) {
		ClientUser *p = getUser(idx);
		Channel *c    = getChannel(idx);
		if (p) {
			ds << false;
			ds << p->uiSession;
		} else if (c) {
			ds << true;
			ds << c->iId;
		}
	}
	QMimeData *md = new QMimeData();
	md->setData(QLatin1String("mumble/dragentry"), qba);
	return md;
}

bool UserModel::dropMimeData(const QMimeData *md, Qt::DropAction, int row, int column, const QModelIndex &p) {
#define NAMECMPCHANNEL(first, second) (QString::localeAwareCompare(first->qsName, second->qsName) > 0)

	if (!md->hasFormat(mimeTypes().at(0)))
		return false;

	QByteArray qba = md->data(mimeTypes().at(0));
	QDataStream ds(qba);

	bool isChannel;
	int iId                = -1;
	unsigned int uiSession = 0;
	ds >> isChannel;

	if (isChannel)
		ds >> iId;
	else
		ds >> uiSession;

	Channel *c;
	if (!p.isValid()) {
		c = Channel::get(0);
	} else {
		c = getChannel(p);
	}

	if (!c)
		return false;

	expandAll(c);

	if (!isChannel) {
		// User dropped somewhere
		int ret;
		switch (Global::get().s.ceUserDrag) {
			case Settings::Ask:
				ret = QMessageBox::question(Global::get().mw, QLatin1String("Mumble"),
											tr("Are you sure you want to drag this user?"), QMessageBox::Yes,
											QMessageBox::No);

				if (ret == QMessageBox::No)
					return false;
				break;
			case Settings::DoNothing:
				Global::get().l->log(
					Log::Information,
					MainWindow::tr("You have User Dragging set to \"Do Nothing\" so the user wasn't moved."));
				return false;
				break;
			case Settings::Move:
				break;
		}
		MumbleProto::UserState mpus;
		mpus.set_session(uiSession);
		mpus.set_channel_id(c->iId);
		Global::get().sh->sendMessage(mpus);
	} else if (c->iId != iId) {
		// Channel dropped somewhere (not on itself)
		int ret;
		switch (Global::get().s.ceChannelDrag) {
			case Settings::Ask:
				ret = QMessageBox::question(Global::get().mw, QLatin1String("Mumble"),
											tr("Are you sure you want to drag this channel?"), QMessageBox::Yes,
											QMessageBox::No);

				if (ret == QMessageBox::No)
					return false;
				break;
			case Settings::DoNothing:
				Global::get().l->log(
					Log::Information,
					MainWindow::tr("You have Channel Dragging set to \"Do Nothing\" so the channel wasn't moved."));
				return false;
				break;
			case Settings::Move:
				break;
			default:
				Global::get().l->log(Log::CriticalError,
									 MainWindow::tr("Unknown Channel Drag mode in UserModel::dropMimeData."));
				return false;
				break;
		}

		long long inewpos = 0;
		Channel *dropped  = Channel::c_qhChannels.value(iId);

		if (!dropped)
			return false;

		if (p.isValid()) {
			ModelItem *pi = static_cast< ModelItem * >(p.internalPointer());
			if (pi->pUser)
				pi = pi->parent;

			int ifirst = 0;
			int ilast  = pi->rows() - 1;

			if (ilast > 0) {
				while (pi->userAt(ifirst) && ifirst < ilast)
					ifirst++;
				while (pi->userAt(ilast) && ilast > 0)
					ilast--;
			}

			if (row == -1 && column == -1) {
				// Dropped on item
				if (getUser(p)) {
					// Dropped on player
					if (ilast > 0) {
						if (pi->bUsersTop) {
							if (pi->channelAt(ifirst) == dropped || NAMECMPCHANNEL(pi->channelAt(ifirst), dropped)) {
								if (dropped->iPosition == pi->channelAt(ifirst)->iPosition)
									return true;
								inewpos = pi->channelAt(ifirst)->iPosition;
							} else {
								inewpos = static_cast< long long >(pi->channelAt(ifirst)->iPosition) - 20;
							}
						} else {
							if (dropped == pi->channelAt(ilast) || NAMECMPCHANNEL(dropped, pi->channelAt(ilast))) {
								if (pi->channelAt(ilast)->iPosition == dropped->iPosition)
									return true;
								inewpos = pi->channelAt(ilast)->iPosition;
							} else {
								inewpos = static_cast< long long >(pi->channelAt(ilast)->iPosition) + 20;
							}
						}
					}
				}
			} else {
				// Dropped between items
				if (ilast == 0) {
					// No channels in there yet
				} else if (row <= ifirst) {
					if (pi->channelAt(ifirst) == dropped || NAMECMPCHANNEL(pi->channelAt(ifirst), dropped)) {
						if (dropped->iPosition == pi->channelAt(ifirst)->iPosition)
							return true;
						inewpos = pi->channelAt(ifirst)->iPosition;
					} else {
						inewpos = static_cast< long long >(pi->channelAt(ifirst)->iPosition) - 20;
					}
				} else if (row > ilast) {
					if (dropped == pi->channelAt(ilast) || NAMECMPCHANNEL(dropped, pi->channelAt(ilast))) {
						if (pi->channelAt(ilast)->iPosition == dropped->iPosition)
							return true;
						inewpos = pi->channelAt(ilast)->iPosition;
					} else {
						inewpos = static_cast< long long >(pi->channelAt(ilast)->iPosition) + 20;
					}
				} else {
					// Dropped between channels
					Channel *lower = pi->channelAt(row);
					Channel *upper = pi->channelAt(row - 1);

					if (lower->iPosition == upper->iPosition && NAMECMPCHANNEL(lower, dropped)
						&& NAMECMPCHANNEL(dropped, upper)) {
						inewpos = upper->iPosition;
					} else if (lower->iPosition > upper->iPosition && NAMECMPCHANNEL(lower, dropped)) {
						inewpos = lower->iPosition;
					} else if (lower->iPosition > upper->iPosition && NAMECMPCHANNEL(dropped, upper)) {
						inewpos = upper->iPosition;
					} else if (lower == dropped || upper == dropped) {
						return true;
					} else if (abs(lower->iPosition) - abs(upper->iPosition) > 1) {
						inewpos = upper->iPosition + (abs(lower->iPosition) - abs(upper->iPosition)) / 2;
					} else {
						// Not enough space, other channels have to be moved
						if (static_cast< long long >(pi->channelAt(ilast)->iPosition) + 40 > INT_MAX) {
							QMessageBox::critical(Global::get().mw, QLatin1String("Mumble"),
												  tr("Cannot perform this movement automatically, please reset the "
													 "numeric sorting indicators or adjust it manually."));
							return false;
						}
						for (int i = row; i <= ilast; i++) {
							Channel *tmp = pi->channelAt(i);
							if (tmp != dropped) {
								MumbleProto::ChannelState mpcs;
								mpcs.set_channel_id(tmp->iId);
								mpcs.set_position(tmp->iPosition + 40);
								Global::get().sh->sendMessage(mpcs);
							}
						}
						inewpos = upper->iPosition + 20;
					}
				}
			}
		}

		if (inewpos > INT_MAX || inewpos < INT_MIN) {
			QMessageBox::critical(Global::get().mw, QLatin1String("Mumble"),
								  tr("Cannot perform this movement automatically, please reset the numeric sorting "
									 "indicators or adjust it manually."));
			return false;
		}

		MumbleProto::ChannelState mpcs;
		mpcs.set_channel_id(iId);
		if (dropped->parent() != c)
			mpcs.set_parent(c->iId);
		mpcs.set_position(static_cast< int >(inewpos));
		Global::get().sh->sendMessage(mpcs);
	}

	return true;
#undef NAMECMPCHANNEL
}

void UserModel::updateOverlay() const {
#ifdef USE_OVERLAY
	Global::get().o->updateOverlay();
#endif
	Global::get().lcd->updateUserView();
}


QString UserModel::createDisplayString(const ClientUser &user, bool isChannelListener, const Channel *parentChannel) {
	// Get the configured volume adjustment. Depending on whether
	// this display string is for a ChannelListener or a regular user, we have to fetch
	// the volume adjustment differently.
	float volumeAdjustment = 1.0f;
	if (isChannelListener) {
		if (parentChannel && user.uiSession == Global::get().uiSession) {
			// Only the listener of the local user can have a volume adjustment
			volumeAdjustment = ChannelListener::getListenerLocalVolumeAdjustment(parentChannel->iId);
		}
	} else {
		volumeAdjustment = user.getLocalVolumeAdjustments();
	}

	// Transform the adjustment into dB
	// *2 == 6 dB
	int localVolumeDecibel = std::round(log2f(volumeAdjustment) * 6);

	// Create a friend-tag
	QString friendTag;
	if (!user.qsFriendName.isEmpty() && user.qsName.compare(user.qsFriendName, Qt::CaseInsensitive) != 0) {
		friendTag = QString::fromLatin1("(%2)").arg(user.qsFriendName);
	}

	// Create a nickname-tag
	QString nickname = user.getLocalNickname();

	// Create a tag that indicates the volume adjustments
	QString volumeTag;
	if (std::abs(localVolumeDecibel) > 0 && Global::get().s.bShowVolumeAdjustments) {
		volumeTag = QString::asprintf("|%+d|", localVolumeDecibel);
	}

	QString displayString;
	if (!Global::get().s.bShowNicknamesOnly || nickname.isEmpty()) {
		displayString += user.qsName;
	} else {
		displayString += nickname;
	}

	if (!friendTag.isEmpty()) {
		displayString += " " + friendTag;
	}

	if (!Global::get().s.bShowNicknamesOnly && !nickname.isEmpty()
		&& user.qsName.compare(nickname, Qt::CaseInsensitive) != 0) {
		displayString += " " + QString::fromLatin1("[%1]").arg(nickname);
	}

	if (!volumeTag.isEmpty()) {
		displayString += "   " + volumeTag;
	}

	return displayString;
}