@@ -320,6 +320,7 @@ VpnModel::VpnModel(QObject *parent)
320
320
, provisioningOutputPath_(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral(" /system/privileged/vpn-provisioning" ))
321
321
, bestState_(VpnModel::Idle)
322
322
, autoConnect_(false )
323
+ , orderByConnected_(false )
323
324
{
324
325
qDBusRegisterMetaType<PathProperties>();
325
326
qDBusRegisterMetaType<PathPropertiesArray>();
@@ -400,6 +401,30 @@ bool VpnModel::autoConnect() const
400
401
return autoConnect_;
401
402
}
402
403
404
+ bool VpnModel::orderByConnected () const
405
+ {
406
+ return orderByConnected_;
407
+ }
408
+
409
+ void VpnModel::setOrderByConnected (bool orderByConnected)
410
+ {
411
+ if (orderByConnected != orderByConnected_) {
412
+ orderByConnected_ = orderByConnected;
413
+
414
+ // Update the ordering; only the connected connections need to move
415
+ // In practice only one VPN can be connected, so full sort is overkill
416
+ const int itemCount (count ());
417
+ for (int index = 0 ; index < itemCount; ++index) {
418
+ VpnConnection *conn = get<VpnConnection>(index);
419
+ if (conn->connected ()) {
420
+ reorderConnection (conn);
421
+ }
422
+ }
423
+
424
+ emit orderByConnectedChanged ();
425
+ }
426
+ }
427
+
403
428
void VpnModel::createConnection (const QVariantMap &createProperties)
404
429
{
405
430
const QString path (createProperties.value (QString (" path" )).toString ());
@@ -851,6 +876,7 @@ void VpnModel::updateConnection(VpnConnection *conn, const QVariantMap &updatePr
851
876
}
852
877
853
878
int oldState (conn->state ());
879
+ bool connectionChanged = false ;
854
880
855
881
if (updateItem (conn, properties)) {
856
882
itemChanged (conn);
@@ -859,6 +885,10 @@ void VpnModel::updateConnection(VpnConnection *conn, const QVariantMap &updatePr
859
885
860
886
if (conn->state () != oldState) {
861
887
emit connectionStateChanged (conn->path (), static_cast <int >(conn->state ()));
888
+ if ((conn->state () == VpnModel::Ready) != (oldState == VpnModel::Ready)) {
889
+ emit conn->connectedChanged ();
890
+ connectionChanged = true ;
891
+ }
862
892
863
893
// Check to see if the best state has changed
864
894
ConnectionState maxState = Idle;
@@ -874,23 +904,14 @@ void VpnModel::updateConnection(VpnConnection *conn, const QVariantMap &updatePr
874
904
}
875
905
}
876
906
877
-
878
- // Keep the items sorted by name. So sort only when updateProperties map contains
879
- // a name e.i. not when "autoConnect" changes. In practice this means that sorting
880
- // is only allowed when a VPN is created. When modifying name of a VPN, the VPN
907
+ // Keep the items sorted by name and possibly connected status. So sort
908
+ // only when the connection status has changed, or the updateProperties
909
+ // map contains a name i.e. not when "autoConnect" changes. In practice
910
+ // this means that if orderByConnected_ is false then sorting is only
911
+ // allowed when a VPN is created. When modifying name of a VPN, the VPN
881
912
// will be first removed and then recreated.
882
- if (itemCount > 1 && updateProperties.contains (QStringLiteral (" name" ))) {
883
- int index = 0 ;
884
- for ( ; index < itemCount; ++index) {
885
- const VpnConnection *existing = get<VpnConnection>(index);
886
- if (existing->name () > conn->name ()) {
887
- break ;
888
- }
889
- }
890
- const int currentIndex = indexOf (conn);
891
- if (index != currentIndex && (index - 1 ) != currentIndex) {
892
- moveItem (currentIndex, (currentIndex < index ? (index - 1 ) : index));
893
- }
913
+ if (updateProperties.contains (QStringLiteral (" name" )) || (orderByConnected_ && connectionChanged)) {
914
+ reorderConnection (conn);
894
915
}
895
916
}
896
917
@@ -905,6 +926,29 @@ void VpnModel::updateConnection(VpnConnection *conn, const QVariantMap &updatePr
905
926
}
906
927
}
907
928
929
+ void VpnModel::reorderConnection (VpnConnection * conn)
930
+ {
931
+ const int itemCount (count ());
932
+
933
+ if (itemCount > 1 ) {
934
+ int index = 0 ;
935
+ for ( ; index < itemCount; ++index) {
936
+ const VpnConnection *existing = get<VpnConnection>(index);
937
+ // Scenario 1 orderByConnected == true: order first by connected, second by name
938
+ // Scenario 2 orderByConnected == false: order only by name
939
+ if ((orderByConnected_ && (existing->connected () < conn->connected ()))
940
+ || ((!orderByConnected_ || (existing->connected () == conn->connected ()))
941
+ && (existing->name () > conn->name ()))) {
942
+ break ;
943
+ }
944
+ }
945
+ const int currentIndex = indexOf (conn);
946
+ if (index != currentIndex && (index - 1 ) != currentIndex) {
947
+ moveItem (currentIndex, (currentIndex < index ? (index - 1 ) : index));
948
+ }
949
+ }
950
+ }
951
+
908
952
QVariantMap VpnModel::processOpenVpnProvisioningFile (QFile &provisioningFile)
909
953
{
910
954
QVariantMap rv;
0 commit comments