File indexing completed on 2024-12-08 13:24:49

0001 /*
0002     Mobile proxy model - model for displaying netwoks in mobile kcm
0003     SPDX-FileCopyrightText: 2017 Martin Kacej <m.kacej@atlas.sk>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 
0007 */
0008 
0009 #include "mobileproxymodel.h"
0010 #include "networkmodel.h"
0011 #include "networkmodelitem.h"
0012 
0013 MobileProxyModel::MobileProxyModel(QObject *parent)
0014     : QSortFilterProxyModel(parent)
0015 {
0016     setDynamicSortFilter(true);
0017     sort(0, Qt::DescendingOrder);
0018 }
0019 
0020 MobileProxyModel::~MobileProxyModel() = default;
0021 
0022 void MobileProxyModel::setShowSavedMode(bool mode)
0023 {
0024     m_showSavedMode = mode;
0025     Q_EMIT showSavedModeChanged(mode);
0026     invalidate();
0027 }
0028 
0029 bool MobileProxyModel::showSavedMode() const
0030 {
0031     return m_showSavedMode;
0032 }
0033 
0034 bool MobileProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0035 {
0036     const QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
0037 
0038     // slaves are always filtered-out
0039     const bool isSlave = sourceModel()->data(index, NetworkModel::SlaveRole).toBool();
0040 
0041     if (isSlave) {
0042         return false;
0043     }
0044 
0045     const NetworkManager::ConnectionSettings::ConnectionType type =
0046         (NetworkManager::ConnectionSettings::ConnectionType)sourceModel()->data(index, NetworkModel::TypeRole).toUInt();
0047     if (type == NetworkManager::ConnectionSettings::Wireless) {
0048         NetworkModelItem::ItemType itemType = (NetworkModelItem::ItemType)sourceModel()->data(index, NetworkModel::ItemTypeRole).toUInt();
0049         if (showSavedMode()) {
0050             return itemType == NetworkModelItem::UnavailableConnection;
0051         } else {
0052             return itemType >= NetworkModelItem::AvailableConnection;
0053         }
0054     }
0055     return false;
0056 }
0057 
0058 bool MobileProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0059 {
0060     const bool leftAvailable =
0061         (NetworkModelItem::ItemType)sourceModel()->data(left, NetworkModel::ItemTypeRole).toUInt() != NetworkModelItem::UnavailableConnection;
0062     const bool leftConnected = sourceModel()->data(left, NetworkModel::ConnectionStateRole).toUInt() == NetworkManager::ActiveConnection::Activated;
0063     const int leftConnectionState = sourceModel()->data(left, NetworkModel::ConnectionStateRole).toUInt();
0064     const QString leftName = sourceModel()->data(left, NetworkModel::NameRole).toString();
0065     const QString leftUuid = sourceModel()->data(left, NetworkModel::UuidRole).toString();
0066     const int leftSignal = sourceModel()->data(left, NetworkModel::SignalRole).toInt();
0067     const QDateTime leftDate = sourceModel()->data(left, NetworkModel::TimeStampRole).toDateTime();
0068 
0069     const bool rightAvailable =
0070         (NetworkModelItem::ItemType)sourceModel()->data(right, NetworkModel::ItemTypeRole).toUInt() != NetworkModelItem::UnavailableConnection;
0071     const bool rightConnected = sourceModel()->data(right, NetworkModel::ConnectionStateRole).toUInt() == NetworkManager::ActiveConnection::Activated;
0072     const int rightConnectionState = sourceModel()->data(right, NetworkModel::ConnectionStateRole).toUInt();
0073     const QString rightName = sourceModel()->data(right, NetworkModel::NameRole).toString();
0074     const QString rightUuid = sourceModel()->data(right, NetworkModel::UuidRole).toString();
0075     const int rightSignal = sourceModel()->data(right, NetworkModel::SignalRole).toInt();
0076     const QDateTime rightDate = sourceModel()->data(right, NetworkModel::TimeStampRole).toDateTime();
0077 
0078     if (leftAvailable < rightAvailable) {
0079         return true;
0080     } else if (leftAvailable > rightAvailable) {
0081         return false;
0082     }
0083 
0084     if (leftConnected < rightConnected) {
0085         return true;
0086     } else if (leftConnected > rightConnected) {
0087         return false;
0088     }
0089 
0090     if (leftConnectionState > rightConnectionState) {
0091         return true;
0092     } else if (leftConnectionState < rightConnectionState) {
0093         return false;
0094     }
0095 
0096     if (leftUuid.isEmpty() && !rightUuid.isEmpty()) {
0097         return true;
0098     } else if (!leftUuid.isEmpty() && rightUuid.isEmpty()) {
0099         return false;
0100     }
0101 
0102     if (leftDate > rightDate) {
0103         return false;
0104     } else if (leftDate < rightDate) {
0105         return true;
0106     }
0107 
0108     if (leftSignal < rightSignal) {
0109         return true;
0110     } else if (leftSignal > rightSignal) {
0111         return false;
0112     }
0113 
0114     if (QString::localeAwareCompare(leftName, rightName) > 0) {
0115         return true;
0116     } else {
0117         return false;
0118     }
0119 }