File indexing completed on 2024-10-06 08:04:10

0001 /*
0002     SPDX-FileCopyrightText: 2013-2018 Jan Grulich <jgrulich@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "editorproxymodel.h"
0008 #include "networkmodel.h"
0009 #include "networkmodelitem.h"
0010 #include "uiutils.h"
0011 
0012 EditorProxyModel::EditorProxyModel(QObject *parent)
0013     : QSortFilterProxyModel(parent)
0014 {
0015     setDynamicSortFilter(true);
0016     setFilterRole(NetworkModel::NameRole);
0017     setFilterCaseSensitivity(Qt::CaseInsensitive);
0018     setSortCaseSensitivity(Qt::CaseInsensitive);
0019     setSortLocaleAware(true);
0020     sort(0, Qt::DescendingOrder);
0021 }
0022 
0023 EditorProxyModel::~EditorProxyModel() = default;
0024 
0025 bool EditorProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0026 {
0027     const QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
0028 
0029     // slaves are always filtered-out
0030     const bool isSlave = sourceModel()->data(index, NetworkModel::SlaveRole).toBool();
0031     const bool isDuplicate = sourceModel()->data(index, NetworkModel::DuplicateRole).toBool();
0032 
0033     if (isSlave || isDuplicate) {
0034         return false;
0035     }
0036 
0037     const NetworkManager::ConnectionSettings::ConnectionType type =
0038         (NetworkManager::ConnectionSettings::ConnectionType)sourceModel()->data(index, NetworkModel::TypeRole).toUInt();
0039     if (!UiUtils::isConnectionTypeSupported(type)) {
0040         return false;
0041     }
0042 
0043     NetworkModelItem::ItemType itemType = (NetworkModelItem::ItemType)sourceModel()->data(index, NetworkModel::ItemTypeRole).toUInt();
0044     if (itemType == NetworkModelItem::AvailableAccessPoint) {
0045         return false;
0046     }
0047 
0048     return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
0049 }
0050 
0051 bool EditorProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0052 {
0053     const bool leftConnected = sourceModel()->data(left, NetworkModel::ConnectionStateRole).toUInt() == NetworkManager::ActiveConnection::Activated;
0054     const QString leftName = sourceModel()->data(left, NetworkModel::NameRole).toString();
0055     const UiUtils::SortedConnectionType leftType =
0056         UiUtils::connectionTypeToSortedType((NetworkManager::ConnectionSettings::ConnectionType)sourceModel()->data(left, NetworkModel::TypeRole).toUInt());
0057     const QString leftVpnType = sourceModel()->data(left, NetworkModel::VpnType).toString();
0058     const QDateTime leftDate = sourceModel()->data(left, NetworkModel::TimeStampRole).toDateTime();
0059 
0060     const bool rightConnected = sourceModel()->data(right, NetworkModel::ConnectionStateRole).toUInt() == NetworkManager::ActiveConnection::Activated;
0061     const QString rightName = sourceModel()->data(right, NetworkModel::NameRole).toString();
0062     const UiUtils::SortedConnectionType rightType =
0063         UiUtils::connectionTypeToSortedType((NetworkManager::ConnectionSettings::ConnectionType)sourceModel()->data(right, NetworkModel::TypeRole).toUInt());
0064     const QString rightVpnType = sourceModel()->data(right, NetworkModel::VpnType).toString();
0065     const QDateTime rightDate = sourceModel()->data(right, NetworkModel::TimeStampRole).toDateTime();
0066 
0067     if (leftType < rightType) {
0068         return false;
0069     } else if (leftType > rightType) {
0070         return true;
0071     }
0072 
0073     if (leftType == rightType && leftType == UiUtils::Vpn) {
0074         if (QString::localeAwareCompare(leftVpnType, rightVpnType) < 0) {
0075             return false;
0076         } else if (QString::localeAwareCompare(leftVpnType, rightVpnType) > 0) {
0077             return true;
0078         }
0079     }
0080 
0081     if (leftConnected < rightConnected) {
0082         return true;
0083     } else if (leftConnected > rightConnected) {
0084         return false;
0085     }
0086 
0087     if (leftDate > rightDate) {
0088         return false;
0089     } else if (leftDate < rightDate) {
0090         return true;
0091     }
0092 
0093     if (QString::localeAwareCompare(leftName, rightName) > 0) {
0094         return true;
0095     } else {
0096         return false;
0097     }
0098 }
0099 
0100 #include "moc_editorproxymodel.cpp"