forked from jamulussoftware/jamulus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectdlg.cpp
More file actions
1390 lines (1185 loc) · 55.1 KB
/
connectdlg.cpp
File metadata and controls
1390 lines (1185 loc) · 55.1 KB
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
/******************************************************************************\
* Copyright (c) 2004-2025
*
* Author(s):
* Volker Fischer
*
******************************************************************************
*
* This program 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 2 of the License, or (at your option) any later
* version.
*
* This program 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
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
\******************************************************************************/
#include "connectdlg.h"
/* Implementation *************************************************************/
// mapVersionStr - converts a version number to a sortable string
static QString mapVersionStr ( const QString& versionStr )
{
QString key;
QString x = ">"; // default suffix is later (git, dev, nightly, etc)
// Regex for SemVer: major.minor.patch-suffix
QRegularExpression semVerRegex ( R"(^(\d+)\.(\d+)\.(\d+)-?(.*)$)" );
QRegularExpressionMatch match = semVerRegex.match ( versionStr );
if ( !match.hasMatch() )
{
return versionStr; // fallback: plain text
}
int major = match.captured ( 1 ).toInt();
int minor = match.captured ( 2 ).toInt();
int patch = match.captured ( 3 ).toInt();
QString suffix = match.captured ( 4 ); // may be empty
if ( suffix.isEmpty() )
{
x = "="; // bare version number
}
else if ( suffix.startsWith ( "rc" ) || suffix.startsWith ( "beta" ) || suffix.startsWith ( "alpha" ) )
{
x = "<"; // pre-release version
}
// construct a sortable key mmmnnnpppksuffix, where:
// mmm = major
// nnn = minor
// ppp = patch
// k = sort key to sort alpha, beta, rc before bare version number, and other suffixes after (<, =, >)
// suffix = supplied suffix
key = QString ( "%1%2%3%4%5" )
.arg ( major, 3, 10, QLatin1Char ( '0' ) )
.arg ( minor, 3, 10, QLatin1Char ( '0' ) )
.arg ( patch, 3, 10, QLatin1Char ( '0' ) )
.arg ( x )
.arg ( suffix );
return key;
}
// Subclass of QTreeWidgetItem that allows LVC_VERSION to sort by the UserRole data value
CMappedTreeWidgetItem::CMappedTreeWidgetItem ( QTreeWidget* owner ) : QTreeWidgetItem ( owner ), owner ( owner ) {}
bool CMappedTreeWidgetItem::operator<( const QTreeWidgetItem& other ) const
{
if ( !owner )
return QTreeWidgetItem::operator<( other );
int column = owner->sortColumn();
// we only need this override for comparing server versions
if ( column != CConnectDlg::LVC_VERSION )
return QTreeWidgetItem::operator<( other );
QVariant lhs = data ( column, Qt::UserRole );
QVariant rhs = other.data ( column, Qt::UserRole );
if ( !lhs.isValid() || !rhs.isValid() )
return QTreeWidgetItem::operator<( other );
return lhs.toString() < rhs.toString();
}
CConnectDlg::CConnectDlg ( CClientSettings* pNSetP, const bool bNewShowCompleteRegList, const bool bNEnableIPv6, QWidget* parent ) :
CBaseDlg ( parent, Qt::Dialog ),
pSettings ( pNSetP ),
strSelectedAddress ( "" ),
strSelectedServerName ( "" ),
bShowCompleteRegList ( bNewShowCompleteRegList ),
bServerListReceived ( false ),
bReducedServerListReceived ( false ),
bServerListItemWasChosen ( false ),
bListFilterWasActive ( false ),
bShowAllMusicians ( true ),
bEnableIPv6 ( bNEnableIPv6 )
{
setupUi ( this );
// Add help text to controls -----------------------------------------------
// directory
QString strDirectoryWT = "<b>" + tr ( "Directory" ) + ":</b> " +
tr ( "Shows the servers listed by the selected directory. "
"You can add custom directories in Advanced Settings." );
QString strDirectoryAN = tr ( "Directory combo box" );
lblList->setWhatsThis ( strDirectoryWT );
lblList->setAccessibleName ( strDirectoryAN );
cbxDirectory->setWhatsThis ( strDirectoryWT );
cbxDirectory->setAccessibleName ( strDirectoryAN );
// filter
QString strFilterWT = "<b>" + tr ( "Filter" ) + ":</b> " +
tr ( "Filters the server list by the given text. Note that the filter is case insensitive. "
"A single # character will filter for those servers with at least one person connected." );
QString strFilterAN = tr ( "Filter edit box" );
lblFilter->setWhatsThis ( strFilterWT );
edtFilter->setWhatsThis ( strFilterWT );
lblFilter->setAccessibleName ( strFilterAN );
edtFilter->setAccessibleName ( strFilterAN );
// show all mucisians
chbExpandAll->setWhatsThis ( "<b>" + tr ( "Show All Musicians" ) + ":</b> " +
tr ( "Uncheck to collapse the server list to show just the server details. "
"Check to show everyone on the servers." ) );
chbExpandAll->setAccessibleName ( tr ( "Show all musicians check box" ) );
// server list view
lvwServers->setWhatsThis ( "<b>" + tr ( "Server List" ) + ":</b> " +
tr ( "The Connection Setup window lists the available servers registered with "
"the selected directory. Use the Directory dropdown to change the directory, "
"find the server you want to join in the server list, click on it, and "
"then click the Connect button to connect. Alternatively, double click on "
"the server name to connect." ) +
"<br>" + tr ( "Permanent servers (those that have been listed for longer than 48 hours) are shown in bold." ) +
"<br>" + tr ( "You can add custom directories in Advanced Settings." ) );
lvwServers->setAccessibleName ( tr ( "Server list view" ) );
// server address
QString strServAddrH = "<b>" + tr ( "Server Address" ) + ":</b> " +
tr ( "If you know the server address, you can connect to it "
"using the Server name/Address field. An optional port number can be added after the server "
"address using a colon as a separator, e.g. %1. "
"The field will also show a list of the most recently used server addresses." )
.arg ( QString ( "<tt>example.org:%1</tt>" ).arg ( DEFAULT_PORT_NUMBER ) );
lblServerAddr->setWhatsThis ( strServAddrH );
cbxServerAddr->setWhatsThis ( strServAddrH );
cbxServerAddr->setAccessibleName ( tr ( "Server address edit box" ) );
cbxServerAddr->setAccessibleDescription ( tr ( "Holds the current server address. It also stores old addresses in the combo box list." ) );
tbtDeleteServerAddr->setAccessibleName ( tr ( "Delete server address button" ) );
tbtDeleteServerAddr->setWhatsThis ( "<b>" + tr ( "Delete Server Address" ) + ":</b> " +
tr ( "Click the button to clear the currently selected server address "
"and delete it from the list of stored servers." ) );
tbtDeleteServerAddr->setText ( u8"\u232B" );
UpdateDirectoryComboBox();
// init server address combo box (max MAX_NUM_SERVER_ADDR_ITEMS entries)
cbxServerAddr->setMaxCount ( MAX_NUM_SERVER_ADDR_ITEMS );
cbxServerAddr->setInsertPolicy ( QComboBox::NoInsert );
// set up list view for connected clients (note that the last column size
// must not be specified since this column takes all the remaining space)
#ifdef ANDROID
// for Android we need larger numbers because of the default font size
lvwServers->setColumnWidth ( LVC_NAME, 200 );
lvwServers->setColumnWidth ( LVC_PING, 130 );
lvwServers->setColumnWidth ( LVC_CLIENTS, 100 );
lvwServers->setColumnWidth ( LVC_VERSION, 110 );
#else
lvwServers->setColumnWidth ( LVC_NAME, 180 );
lvwServers->setColumnWidth ( LVC_PING, 75 );
lvwServers->setColumnWidth ( LVC_CLIENTS, 70 );
lvwServers->setColumnWidth ( LVC_LOCATION, 220 );
lvwServers->setColumnWidth ( LVC_VERSION, 95 );
#endif
lvwServers->clear();
// make sure we do not get a too long horizontal scroll bar
lvwServers->header()->setStretchLastSection ( false );
// add invisible columns which are used for sorting the list and storing
// the current/maximum number of clients
// 0: server name
// 1: ping time
// 2: number of musicians (including additional strings like " (full)")
// 3: location
// 4: server version
// 5: minimum ping time (invisible)
// 6: maximum number of clients (invisible)
// (see EConnectListViewColumns in connectdlg.h, which must match the above)
lvwServers->setColumnCount ( LVC_COLUMNS );
lvwServers->hideColumn ( LVC_PING_MIN_HIDDEN );
lvwServers->hideColumn ( LVC_CLIENTS_MAX_HIDDEN );
// per default the root shall not be decorated (to save space)
lvwServers->setRootIsDecorated ( false );
#ifdef USE_ACCESSIBLE_SERVER_LIST
// Create simplified accessible navigation panel for screen readers
// Design: One info label + 2 navigation buttons (Previous/Next) + Toggle button
wAccessibleNavPanel = new QWidget ( this );
wAccessibleNavPanel->setObjectName ( "wAccessibleNavPanel" );
QVBoxLayout* accessibleMainLayout = new QVBoxLayout ( wAccessibleNavPanel );
accessibleMainLayout->setContentsMargins ( 0, 5, 0, 5 );
// Create horizontal layout for navigation buttons (Previous, Next)
QHBoxLayout* navLayout = new QHBoxLayout();
butAccessiblePrevious = new QPushButton ( u8"\u2190 " + tr ( "Previous Server" ), wAccessibleNavPanel );
butAccessiblePrevious->setObjectName ( "butAccessiblePrevious" );
butAccessiblePrevious->setAccessibleName ( tr ( "Go to previous server" ) );
butAccessiblePrevious->setAccessibleDescription ( tr ( "Navigate to the previous server in the list" ) );
butAccessiblePrevious->setShortcut ( QKeySequence ( Qt::ALT | Qt::Key_Up ) );
butAccessiblePrevious->setToolTip ( tr ( "Previous server (Alt+Up)" ) );
navLayout->addWidget ( butAccessiblePrevious, 1 );
butAccessibleNext = new QPushButton ( tr ( "Next Server" ) + u8" \u2192", wAccessibleNavPanel );
butAccessibleNext->setObjectName ( "butAccessibleNext" );
butAccessibleNext->setAccessibleName ( tr ( "Go to next server" ) );
butAccessibleNext->setAccessibleDescription ( tr ( "Navigate to the next server in the list" ) );
butAccessibleNext->setShortcut ( QKeySequence ( Qt::ALT | Qt::Key_Down ) );
butAccessibleNext->setToolTip ( tr ( "Next server (Alt+Down)" ) );
navLayout->addWidget ( butAccessibleNext, 1 );
accessibleMainLayout->addLayout ( navLayout );
// Create read-only, focusable label showing current server information
// Using QLabel with focus policy so screenreaders can read it
lblAccessibleServerInfo = new QLabel ( tr ( "No server selected" ), wAccessibleNavPanel );
lblAccessibleServerInfo->setObjectName ( "lblAccessibleServerInfo" );
lblAccessibleServerInfo->setWordWrap ( true );
lblAccessibleServerInfo->setFrameStyle ( QFrame::Panel | QFrame::Sunken );
lblAccessibleServerInfo->setTextInteractionFlags ( Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard );
lblAccessibleServerInfo->setMinimumHeight ( 50 );
lblAccessibleServerInfo->setFocusPolicy ( Qt::StrongFocus ); // Make it focusable for screen readers
lblAccessibleServerInfo->setAccessibleName ( tr ( "Current server information" ) );
lblAccessibleServerInfo->setAccessibleDescription ( tr ( "Shows details of the currently selected server. Use Previous/Next buttons or Alt+Up/Down to navigate." ) );
accessibleMainLayout->addWidget ( lblAccessibleServerInfo );
// Create toggle button
butToggleAccessible = new QPushButton ( u8"\u25BC " + tr ( "Hide Accessible Controls" ), this );
butToggleAccessible->setObjectName ( "butToggleAccessible" );
butToggleAccessible->setAccessibleName ( tr ( "Toggle accessible controls" ) );
butToggleAccessible->setAccessibleDescription ( tr ( "Show or hide the accessible navigation panel for screen readers" ) );
butToggleAccessible->setCheckable ( true );
butToggleAccessible->setChecked ( true );
butToggleAccessible->setToolTip ( tr ( "Toggle accessible controls" ) );
// Insert the accessible panel and toggle button into the layout right after the tree widget
QVBoxLayout* mainLayout = qobject_cast<QVBoxLayout*> ( layout() );
if ( mainLayout )
{
// Find the tree widget in the layout
bool inserted = false;
for ( int i = 0; i < mainLayout->count(); ++i )
{
QLayoutItem* item = mainLayout->itemAt ( i );
if ( item && item->widget() == lvwServers )
{
mainLayout->insertWidget ( i + 1, butToggleAccessible );
mainLayout->insertWidget ( i + 2, wAccessibleNavPanel );
inserted = true;
break;
}
}
if ( !inserted )
{
qWarning ( "Accessible navigation panel could not be inserted: tree widget not found in layout." );
}
}
else
{
qWarning ( "Accessible navigation panel could not be inserted: main layout cast failed." );
}
// Initially show the accessible panel
wAccessibleNavPanel->setVisible ( true );
#endif
// make sure the connect button has the focus
butConnect->setFocus();
// for "show all servers" mode make sort by click on header possible
if ( bShowCompleteRegList )
{
lvwServers->setSortingEnabled ( true );
lvwServers->sortItems ( LVC_NAME, Qt::AscendingOrder );
}
// set a placeholder text to explain how to filter occupied servers (#397)
edtFilter->setPlaceholderText ( tr ( "Filter text, or # for occupied servers" ) );
// setup timers
TimerInitialSort.setSingleShot ( true ); // only once after list request
#if defined( ANDROID ) || defined( Q_OS_IOS )
// for the Android and iOS version maximize the window
setWindowState ( Qt::WindowMaximized );
#endif
// Connections -------------------------------------------------------------
// list view
QObject::connect ( lvwServers, &QTreeWidget::itemDoubleClicked, this, &CConnectDlg::OnServerListItemDoubleClicked );
// to get default return key behaviour working
QObject::connect ( lvwServers, &QTreeWidget::activated, this, &CConnectDlg::OnConnectClicked );
// connect selection change for accessibility support
QObject::connect ( lvwServers, &QTreeWidget::itemSelectionChanged, this, &CConnectDlg::OnServerListItemSelectionChanged );
// line edit
QObject::connect ( edtFilter, &QLineEdit::textEdited, this, &CConnectDlg::OnFilterTextEdited );
// combo boxes
QObject::connect ( cbxServerAddr, &QComboBox::editTextChanged, this, &CConnectDlg::OnServerAddrEditTextChanged );
QObject::connect ( cbxDirectory, static_cast<void ( QComboBox::* ) ( int )> ( &QComboBox::activated ), this, &CConnectDlg::OnDirectoryChanged );
// check boxes
QObject::connect ( chbExpandAll, &QCheckBox::stateChanged, this, &CConnectDlg::OnExpandAllStateChanged );
// buttons
QObject::connect ( butCancel, &QPushButton::clicked, this, &CConnectDlg::close );
QObject::connect ( butConnect, &QPushButton::clicked, this, &CConnectDlg::OnConnectClicked );
// tool buttons
QObject::connect ( tbtDeleteServerAddr, &QToolButton::clicked, this, &CConnectDlg::OnDeleteServerAddrClicked );
// timers
QObject::connect ( &TimerPing, &QTimer::timeout, this, &CConnectDlg::OnTimerPing );
QObject::connect ( &TimerReRequestServList, &QTimer::timeout, this, &CConnectDlg::OnTimerReRequestServList );
#ifdef USE_ACCESSIBLE_SERVER_LIST
// accessible navigation panel
QObject::connect ( butAccessiblePrevious, &QPushButton::clicked, this, &CConnectDlg::OnAccessiblePreviousClicked );
QObject::connect ( butAccessibleNext, &QPushButton::clicked, this, &CConnectDlg::OnAccessibleNextClicked );
QObject::connect ( butToggleAccessible, &QPushButton::clicked, this, &CConnectDlg::OnToggleAccessibleClicked );
#endif
}
void CConnectDlg::showEvent ( QShowEvent* )
{
// load stored IP addresses in combo box
cbxServerAddr->clear();
cbxServerAddr->clearEditText();
for ( int iLEIdx = 0; iLEIdx < MAX_NUM_SERVER_ADDR_ITEMS; iLEIdx++ )
{
if ( !pSettings->vstrIPAddress[iLEIdx].isEmpty() )
{
cbxServerAddr->addItem ( pSettings->vstrIPAddress[iLEIdx] );
}
}
// on opening the connect dialg, we always want to request a
// new updated server list per definition
RequestServerList();
}
void CConnectDlg::RequestServerList()
{
// reset flags
bServerListReceived = false;
bReducedServerListReceived = false;
bServerListItemWasChosen = false;
bListFilterWasActive = false;
// clear current address and name
strSelectedAddress = "";
strSelectedServerName = "";
// clear server list view
lvwServers->clear();
// update list combo box (disable events to avoid a signal)
cbxDirectory->blockSignals ( true );
if ( pSettings->eDirectoryType == AT_CUSTOM )
{
// iCustomDirectoryIndex is non-zero only if eDirectoryType == AT_CUSTOM
// find the combobox item that corresponds to vstrDirectoryAddress[iCustomDirectoryIndex]
// (the current selected custom directory)
cbxDirectory->setCurrentIndex ( cbxDirectory->findData ( QVariant ( pSettings->iCustomDirectoryIndex ) ) );
}
else
{
cbxDirectory->setCurrentIndex ( static_cast<int> ( pSettings->eDirectoryType ) );
}
cbxDirectory->blockSignals ( false );
// Get the IP address of the directory server (using the ParseNetworAddress
// function) when the connect dialog is opened, this seems to be the correct
// time to do it. Note that in case of custom directories we
// use iCustomDirectoryIndex as an index into the vector.
// Allow IPv4 only for communicating with Directories
if ( NetworkUtil().ParseNetworkAddress (
NetworkUtil::GetDirectoryAddress ( pSettings->eDirectoryType, pSettings->vstrDirectoryAddress[pSettings->iCustomDirectoryIndex] ),
haDirectoryAddress,
false ) )
{
// send the request for the server list
emit ReqServerListQuery ( haDirectoryAddress );
// start timer, if this message did not get any respond to retransmit
// the server list request message
TimerReRequestServList.start ( SERV_LIST_REQ_UPDATE_TIME_MS );
TimerInitialSort.start ( SERV_LIST_REQ_UPDATE_TIME_MS ); // reuse the time value
}
}
void CConnectDlg::hideEvent ( QHideEvent* )
{
// if window is closed, stop timers
TimerPing.stop();
TimerReRequestServList.stop();
}
void CConnectDlg::OnDirectoryChanged ( int iTypeIdx )
{
// store the new directory type and request new list
// if iTypeIdx == AT_CUSTOM, then iCustomDirectoryIndex is the index into the vector holding the user's custom directory servers
// if iTypeIdx != AT_CUSTOM, then iCustomDirectoryIndex MUST be 0;
if ( iTypeIdx >= AT_CUSTOM )
{
// the value for the index into the vector vstrDirectoryAddress is in the user data of the combobox item
pSettings->iCustomDirectoryIndex = cbxDirectory->itemData ( iTypeIdx ).toInt();
iTypeIdx = AT_CUSTOM;
}
else
{
pSettings->iCustomDirectoryIndex = 0;
}
pSettings->eDirectoryType = static_cast<EDirectoryType> ( iTypeIdx );
RequestServerList();
}
void CConnectDlg::OnTimerReRequestServList()
{
// if the server list is not yet received, retransmit the request for the
// server list
if ( !bServerListReceived )
{
// note that this is a connection less message which may get lost
// and therefore it makes sense to re-transmit it
emit ReqServerListQuery ( haDirectoryAddress );
}
}
void CConnectDlg::SetServerList ( const CHostAddress& InetAddr, const CVector<CServerInfo>& vecServerInfo, const bool bIsReducedServerList )
{
// If the normal list was received, we do not accept any further list
// updates (to avoid the reduced list overwrites the normal list (#657)). Also,
// we only accept a server list from the server address we have sent the
// request for this to (note that we cannot use the port number since the
// receive port and send port might be different at the directory server).
if ( bServerListReceived || ( InetAddr.InetAddr != haDirectoryAddress.InetAddr ) )
{
return;
}
// special treatment if a reduced server list was received
if ( bIsReducedServerList )
{
// make sure we only apply the reduced version list once
if ( bReducedServerListReceived )
{
// do nothing
return;
}
else
{
bReducedServerListReceived = true;
}
}
else
{
// set flag and disable timer for resend server list request if full list
// was received (i.e. not the reduced list)
bServerListReceived = true;
TimerReRequestServList.stop();
}
// first clear list
lvwServers->clear();
// add list item for each server in the server list
const int iServerInfoLen = vecServerInfo.Size();
for ( int iIdx = 0; iIdx < iServerInfoLen; iIdx++ )
{
// get the host address, note that for the very first entry which is
// the directory server, we have to use the receive host address
// instead
CHostAddress CurHostAddress;
if ( iIdx > 0 )
{
CurHostAddress = vecServerInfo[iIdx].HostAddr;
}
else
{
// substitute the receive host address for directory server
CurHostAddress = InetAddr;
}
// create new list view item
CMappedTreeWidgetItem* pNewListViewItem = new CMappedTreeWidgetItem ( lvwServers );
// make the entry invisible (will be set to visible on successful ping
// result) if the complete list of registered servers shall not be shown
if ( !bShowCompleteRegList )
{
pNewListViewItem->setHidden ( true );
}
// server name (if empty, show host address instead)
if ( !vecServerInfo[iIdx].strName.isEmpty() )
{
pNewListViewItem->setText ( LVC_NAME, vecServerInfo[iIdx].strName );
}
else
{
// IP address and port (use IP number without last byte)
// Definition: If the port number is the default port number, we do
// not show it.
if ( vecServerInfo[iIdx].HostAddr.iPort == DEFAULT_PORT_NUMBER )
{
// only show IP number, no port number
pNewListViewItem->setText ( LVC_NAME, CurHostAddress.toString ( CHostAddress::SM_IP_NO_LAST_BYTE ) );
}
else
{
// show IP number and port
pNewListViewItem->setText ( LVC_NAME, CurHostAddress.toString ( CHostAddress::SM_IP_NO_LAST_BYTE_PORT ) );
}
}
// in case of all servers shown, add the registration number at the beginning
if ( bShowCompleteRegList )
{
pNewListViewItem->setText ( LVC_NAME, QString ( "%1: " ).arg ( 1 + iIdx, 3 ) + pNewListViewItem->text ( LVC_NAME ) );
}
// show server name in bold font if it is a permanent server
QFont CurServerNameFont = pNewListViewItem->font ( LVC_NAME );
CurServerNameFont.setBold ( vecServerInfo[iIdx].bPermanentOnline );
pNewListViewItem->setFont ( LVC_NAME, CurServerNameFont );
// the ping time shall be shown in bold font
QFont CurPingTimeFont = pNewListViewItem->font ( LVC_PING );
CurPingTimeFont.setBold ( true );
pNewListViewItem->setFont ( LVC_PING, CurPingTimeFont );
// server location (city and country)
QString strLocation = vecServerInfo[iIdx].strCity;
if ( ( !strLocation.isEmpty() ) && ( vecServerInfo[iIdx].eCountry != QLocale::AnyCountry ) )
{
strLocation += ", ";
}
if ( vecServerInfo[iIdx].eCountry != QLocale::AnyCountry )
{
QString strCountryToString = QLocale::countryToString ( vecServerInfo[iIdx].eCountry );
// Qt countryToString does not use spaces in between country name
// parts but they use upper case letters which we can detect and
// insert spaces as a post processing
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
if ( !strCountryToString.contains ( " " ) )
{
QRegularExpressionMatchIterator reMatchIt = QRegularExpression ( "[A-Z][^A-Z]*" ).globalMatch ( strCountryToString );
QStringList slNames;
while ( reMatchIt.hasNext() )
{
slNames << reMatchIt.next().capturedTexts();
}
strCountryToString = slNames.join ( " " );
}
#endif
strLocation += strCountryToString;
}
pNewListViewItem->setText ( LVC_LOCATION, strLocation );
// init the minimum ping time with a large number (note that this number
// must fit in an integer type)
pNewListViewItem->setText ( LVC_PING_MIN_HIDDEN, "99999999" );
// store the maximum number of clients
pNewListViewItem->setText ( LVC_CLIENTS_MAX_HIDDEN, QString().setNum ( vecServerInfo[iIdx].iMaxNumClients ) );
// store host address
pNewListViewItem->setData ( LVC_NAME, Qt::UserRole, CurHostAddress.toString() );
// per default expand the list item (if not "show all servers")
if ( bShowAllMusicians )
{
lvwServers->expandItem ( pNewListViewItem );
}
}
// immediately issue the ping measurements and start the ping timer since
// the server list is filled now
OnTimerPing();
TimerPing.start ( PING_UPDATE_TIME_SERVER_LIST_MS );
}
void CConnectDlg::SetConnClientsList ( const CHostAddress& InetAddr, const CVector<CChannelInfo>& vecChanInfo )
{
// find the server with the correct address
CMappedTreeWidgetItem* pCurListViewItem = FindListViewItem ( InetAddr );
if ( pCurListViewItem )
{
// first remove any existing children
DeleteAllListViewItemChilds ( pCurListViewItem );
// get number of connected clients
const int iNumConnectedClients = vecChanInfo.Size();
for ( int i = 0; i < iNumConnectedClients; i++ )
{
// create new list view item
QTreeWidgetItem* pNewChildListViewItem = new QTreeWidgetItem ( static_cast<QTreeWidgetItem*> ( pCurListViewItem ) );
// child items shall use only one column
pNewChildListViewItem->setFirstColumnSpanned ( true );
// set the clients name
QString sClientText = vecChanInfo[i].strName;
// set the icon: country flag has priority over instrument
bool bCountryFlagIsUsed = false;
if ( vecChanInfo[i].eCountry != QLocale::AnyCountry )
{
// try to load the country flag icon
QPixmap CountryFlagPixmap ( CLocale::GetCountryFlagIconsResourceReference ( vecChanInfo[i].eCountry ) );
// first check if resource reference was valid
if ( !CountryFlagPixmap.isNull() )
{
// set correct picture
pNewChildListViewItem->setIcon ( LVC_NAME, QIcon ( CountryFlagPixmap ) );
bCountryFlagIsUsed = true;
}
}
if ( !bCountryFlagIsUsed )
{
// get the resource reference string for this instrument
const QString strCurResourceRef = CInstPictures::GetResourceReference ( vecChanInfo[i].iInstrument );
// first check if instrument picture is used or not and if it is valid
if ( !( CInstPictures::IsNotUsedInstrument ( vecChanInfo[i].iInstrument ) || strCurResourceRef.isEmpty() ) )
{
// set correct picture
pNewChildListViewItem->setIcon ( LVC_NAME, QIcon ( QPixmap ( strCurResourceRef ) ) );
}
}
// add the instrument information as text
if ( !CInstPictures::IsNotUsedInstrument ( vecChanInfo[i].iInstrument ) )
{
sClientText.append ( " (" + CInstPictures::GetName ( vecChanInfo[i].iInstrument ) + ")" );
}
// apply the client text to the list view item
pNewChildListViewItem->setText ( LVC_NAME, sClientText );
// add the new child to the corresponding server item
pCurListViewItem->addChild ( pNewChildListViewItem );
// at least one server has children now, show decoration to be able
// to show the children
lvwServers->setRootIsDecorated ( true );
}
// the clients list may have changed, update the filter selection
UpdateListFilter();
}
}
void CConnectDlg::OnServerListItemDoubleClicked ( QTreeWidgetItem* Item, int )
{
// if a server list item was double clicked, it is the same as if the
// connect button was clicked
if ( Item != nullptr )
{
OnConnectClicked();
}
}
void CConnectDlg::OnServerAddrEditTextChanged ( const QString& )
{
// in the server address combo box, a text was changed, remove selection
// in the server list (if any)
lvwServers->clearSelection();
}
void CConnectDlg::OnServerListItemSelectionChanged()
{
#ifdef USE_ACCESSIBLE_SERVER_LIST
UpdateAccessibleServerInfo();
#endif
}
#ifdef USE_ACCESSIBLE_SERVER_LIST
void CConnectDlg::UpdateAccessibleServerInfo()
{
QList<QTreeWidgetItem*> selectedItems = lvwServers->selectedItems();
if ( !selectedItems.isEmpty() && selectedItems.first()->parent() == nullptr )
{
// We have a server item selected (not a musician child item)
QTreeWidgetItem* pItem = selectedItems.first();
// Extract server information
QString serverName = pItem->text ( LVC_NAME );
QString pingTime = pItem->text ( LVC_PING );
QString musicians = pItem->text ( LVC_CLIENTS );
QString location = pItem->text ( LVC_LOCATION );
QString version = pItem->text ( LVC_VERSION );
// Build text for screen readers
QString accessibleText = tr ( "Server: %1" ).arg ( serverName );
if ( !pingTime.isEmpty() )
{
accessibleText += tr ( ", Ping: %1" ).arg ( pingTime );
}
if ( !musicians.isEmpty() )
{
accessibleText += tr ( ", Musicians: %1" ).arg ( musicians );
}
if ( !location.isEmpty() )
{
accessibleText += tr ( ", Location: %1" ).arg ( location );
}
if ( !version.isEmpty() )
{
accessibleText += tr ( ", Version: %1" ).arg ( version );
}
// Update label
lblAccessibleServerInfo->setText ( accessibleText );
lblAccessibleServerInfo->setAccessibleName ( tr ( "Selected server information" ) );
lblAccessibleServerInfo->setAccessibleDescription ( accessibleText );
// Update navigation buttons to show previous/next server names
int currentIndex = lvwServers->indexOfTopLevelItem ( pItem );
// Update "Previous" button with previous server name
if ( currentIndex > 0 )
{
QTreeWidgetItem* prevItem = lvwServers->topLevelItem ( currentIndex - 1 );
QString prevName = prevItem->text ( LVC_NAME );
butAccessiblePrevious->setText ( u8"\u2190 " + prevName );
butAccessiblePrevious->setAccessibleName ( tr ( "Previous server: %1" ).arg ( prevName ) );
butAccessiblePrevious->setAccessibleDescription ( tr ( "Go to previous server: %1" ).arg ( prevName ) );
butAccessiblePrevious->setEnabled ( true );
}
else
{
butAccessiblePrevious->setText ( u8"\u2190 " + tr ( "(first)" ) );
butAccessiblePrevious->setAccessibleName ( tr ( "No previous server - at first server" ) );
butAccessiblePrevious->setAccessibleDescription ( tr ( "Cannot go back, already at first server" ) );
butAccessiblePrevious->setEnabled ( false );
}
// Update "Next" button with next server name
if ( currentIndex < lvwServers->topLevelItemCount() - 1 )
{
QTreeWidgetItem* nextItem = lvwServers->topLevelItem ( currentIndex + 1 );
QString nextName = nextItem->text ( LVC_NAME );
butAccessibleNext->setText ( nextName + u8" \u2192" );
butAccessibleNext->setAccessibleName ( tr ( "Next server: %1" ).arg ( nextName ) );
butAccessibleNext->setAccessibleDescription ( tr ( "Go to next server: %1" ).arg ( nextName ) );
butAccessibleNext->setEnabled ( true );
}
else
{
butAccessibleNext->setText ( tr ( "(last)" ) + u8" \u2192" );
butAccessibleNext->setAccessibleName ( tr ( "No next server - at last server" ) );
butAccessibleNext->setAccessibleDescription ( tr ( "Cannot go forward, already at last server" ) );
butAccessibleNext->setEnabled ( false );
}
// Force VoiceOver to announce the change
QAccessible::updateAccessibility ( new QAccessibleValueChangeEvent ( lblAccessibleServerInfo, accessibleText ) );
}
else
{
// Reset navigation buttons
butAccessiblePrevious->setText ( u8"\u2190 " + tr ( "Previous" ) );
butAccessiblePrevious->setAccessibleName ( tr ( "Navigate to previous server" ) );
butAccessiblePrevious->setEnabled ( lvwServers->topLevelItemCount() > 0 );
butAccessibleNext->setText ( tr ( "Next" ) + u8" \u2192" );
butAccessibleNext->setAccessibleName ( tr ( "Navigate to next server" ) );
butAccessibleNext->setEnabled ( lvwServers->topLevelItemCount() > 0 );
// No server selected or musician child selected
lblAccessibleServerInfo->setText ( tr ( "<i>No server selected or in musician selection</i>" ) );
lblAccessibleServerInfo->setAccessibleName ( tr ( "No server selected or in musician selection" ) );
lblAccessibleServerInfo->setAccessibleDescription ( tr ( "No server selected. Use Previous/Next buttons or Alt+Up/Down to navigate servers." ) );
}
}
void CConnectDlg::OnAccessiblePreviousClicked()
{
// Navigate to previous server
QList<QTreeWidgetItem*> selectedItems = lvwServers->selectedItems();
QTreeWidgetItem* currentItem = selectedItems.isEmpty() ? nullptr : selectedItems.first();
// Get current item or first item if none selected
if ( currentItem == nullptr )
{
// Select first item
if ( lvwServers->topLevelItemCount() > 0 )
{
lvwServers->setCurrentItem ( lvwServers->topLevelItem ( 0 ) );
}
return;
}
// If current item is a musician (child), move to parent
if ( currentItem->parent() != nullptr )
{
lvwServers->setCurrentItem ( currentItem->parent() );
return;
}
// Get previous server item
int currentIndex = lvwServers->indexOfTopLevelItem ( currentItem );
if ( currentIndex > 0 )
{
lvwServers->setCurrentItem ( lvwServers->topLevelItem ( currentIndex - 1 ) );
}
}
void CConnectDlg::OnAccessibleNextClicked()
{
// Navigate to next server
QList<QTreeWidgetItem*> selectedItems = lvwServers->selectedItems();
QTreeWidgetItem* currentItem = selectedItems.isEmpty() ? nullptr : selectedItems.first();
// Get current item or first item if none selected
if ( currentItem == nullptr )
{
// Select first item
if ( lvwServers->topLevelItemCount() > 0 )
{
lvwServers->setCurrentItem ( lvwServers->topLevelItem ( 0 ) );
}
return;
}
// If current item is a musician (child), find next server
if ( currentItem->parent() != nullptr )
{
currentItem = currentItem->parent();
}
// Find next server item (skip musician children)
int currentIndex = lvwServers->indexOfTopLevelItem ( currentItem );
if ( currentIndex >= 0 && currentIndex < lvwServers->topLevelItemCount() - 1 )
{
lvwServers->setCurrentItem ( lvwServers->topLevelItem ( currentIndex + 1 ) );
}
}
void CConnectDlg::OnToggleAccessibleClicked()
{
bool isVisible = wAccessibleNavPanel->isVisible();
wAccessibleNavPanel->setVisible ( !isVisible );
if ( isVisible )
{
butToggleAccessible->setText ( u8"\u25B6 " + tr ( "Show Accessible Controls" ) );
butToggleAccessible->setAccessibleDescription ( tr ( "Show the accessible navigation controls" ) );
}
else
{
butToggleAccessible->setText ( u8"\u25BC " + tr ( "Hide Accessible Controls" ) );
butToggleAccessible->setAccessibleDescription ( tr ( "Hide the accessible navigation controls" ) );
}
}
#endif
void CConnectDlg::OnCustomDirectoriesChanged()
{
QString strPreviousSelection = cbxDirectory->currentText();
UpdateDirectoryComboBox();
// after updating the combobox, we must re-select the previous directory selection
if ( pSettings->eDirectoryType == AT_CUSTOM )
{
// check if the currently select custom directory still exists in the now potentially re-ordered vector,
// if so, then change to its new index. (addresses Issue #1899)
int iNewIndex = cbxDirectory->findText ( strPreviousSelection, Qt::MatchExactly );
if ( iNewIndex == INVALID_INDEX )
{
// previously selected custom directory has been deleted. change to default directory
pSettings->eDirectoryType = static_cast<EDirectoryType> ( AT_DEFAULT );
pSettings->iCustomDirectoryIndex = 0;
RequestServerList();
}
else
{
// find previously selected custom directory in the now potentially re-ordered vector
pSettings->eDirectoryType = static_cast<EDirectoryType> ( AT_CUSTOM );
pSettings->iCustomDirectoryIndex = cbxDirectory->itemData ( iNewIndex ).toInt();
cbxDirectory->blockSignals ( true );
cbxDirectory->setCurrentIndex ( cbxDirectory->findData ( QVariant ( pSettings->iCustomDirectoryIndex ) ) );
cbxDirectory->blockSignals ( false );
}
}
else
{
// selected directory was not a custom directory
cbxDirectory->blockSignals ( true );
cbxDirectory->setCurrentIndex ( static_cast<int> ( pSettings->eDirectoryType ) );
cbxDirectory->blockSignals ( false );
}
}
void CConnectDlg::ShowAllMusicians ( const bool bState )
{
bShowAllMusicians = bState;
// update list
if ( bState )
{
lvwServers->expandAll();
}
else
{
lvwServers->collapseAll();
}
// update check box if necessary
if ( ( chbExpandAll->checkState() == Qt::Checked && !bShowAllMusicians ) || ( chbExpandAll->checkState() == Qt::Unchecked && bShowAllMusicians ) )
{
chbExpandAll->setCheckState ( bState ? Qt::Checked : Qt::Unchecked );
}
}
void CConnectDlg::UpdateListFilter()
{
const QString sFilterText = edtFilter->text();
if ( !sFilterText.isEmpty() )
{
bListFilterWasActive = true;
const int iServerListLen = lvwServers->topLevelItemCount();
for ( int iIdx = 0; iIdx < iServerListLen; iIdx++ )
{
CMappedTreeWidgetItem* pCurListViewItem = static_cast<CMappedTreeWidgetItem*> ( lvwServers->topLevelItem ( iIdx ) );
bool bFilterFound = false;
// DEFINITION: if "#" is set at the beginning of the filter text, we show
// occupied servers (#397)
if ( ( sFilterText.indexOf ( "#" ) == 0 ) && ( sFilterText.length() == 1 ) )
{
// special case: filter for occupied servers
if ( pCurListViewItem->childCount() > 0 )
{
bFilterFound = true;
}
}