From a1a29d136b491f0b6a82d2a3a8eff5f05d29d3a0 Mon Sep 17 00:00:00 2001 From: revenmartin Date: Fri, 28 May 2021 14:25:23 +0800 Subject: [PATCH] Network: add type filter https://github.com/cutefishos/settings/issues/3 --- networkmanagement/CMakeLists.txt | 3 - networkmanagement/appletproxymodel.cpp | 39 ++++++ networkmanagement/appletproxymodel.h | 18 +++ networkmanagement/qmlplugins.cpp | 2 - networkmanagement/technologyproxymodel.cpp | 144 --------------------- networkmanagement/technologyproxymodel.h | 75 ----------- 6 files changed, 57 insertions(+), 224 deletions(-) delete mode 100644 networkmanagement/technologyproxymodel.cpp delete mode 100644 networkmanagement/technologyproxymodel.h diff --git a/networkmanagement/CMakeLists.txt b/networkmanagement/CMakeLists.txt index c514dd4..56da6ae 100644 --- a/networkmanagement/CMakeLists.txt +++ b/networkmanagement/CMakeLists.txt @@ -17,9 +17,6 @@ set(NETWORKMGR_SRCS wiressitemsettings.cpp wiressitemsettings.h - technologyproxymodel.cpp - technologyproxymodel.h - uiutils.cpp uiutils.h diff --git a/networkmanagement/appletproxymodel.cpp b/networkmanagement/appletproxymodel.cpp index 2b89388..cc7d6a9 100644 --- a/networkmanagement/appletproxymodel.cpp +++ b/networkmanagement/appletproxymodel.cpp @@ -1,5 +1,6 @@ /* Copyright 2013-2014 Jan Grulich + Copyright 2021 Reven Martin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -22,6 +23,22 @@ #include "networkmodel.h" #include "uiutils.h" +static NetworkManager::ConnectionSettings::ConnectionType convertType(AppletProxyModel::Type type) +{ + switch (type) { + case AppletProxyModel::UnknownType: + return NetworkManager::ConnectionSettings::ConnectionType::Unknown; + case AppletProxyModel::WiredType: + return NetworkManager::ConnectionSettings::ConnectionType::Wired; + case AppletProxyModel::WirelessType: + return NetworkManager::ConnectionSettings::ConnectionType::Wireless; + default: + break; + } + + return NetworkManager::ConnectionSettings::ConnectionType::Unknown; +} + AppletProxyModel::AppletProxyModel(QObject *parent) : QSortFilterProxyModel(parent) { @@ -34,6 +51,24 @@ AppletProxyModel::~AppletProxyModel() { } +AppletProxyModel::Type AppletProxyModel::type() const +{ + return m_type; +} + +void AppletProxyModel::setType(Type type) +{ + if (m_type == type) + return; + m_type = type; + Q_EMIT typeChanged(); + + if (type == UnknownType) + setFilterRole(0); + else + setFilterRole(NetworkModel::TypeRole); +} + bool AppletProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { const QModelIndex index = sourceModel()->index(source_row, 0, source_parent); @@ -49,6 +84,10 @@ bool AppletProxyModel::filterAcceptsRow(int source_row, const QModelIndex &sourc return false; } + // Type Filter + if (m_type == UnknownType || type != convertType(m_type)) + return false; + NetworkModelItem::ItemType itemType = (NetworkModelItem::ItemType)sourceModel()->data(index, NetworkModel::ItemTypeRole).toUInt(); if (itemType != NetworkModelItem::AvailableConnection && diff --git a/networkmanagement/appletproxymodel.h b/networkmanagement/appletproxymodel.h index b614c82..514688c 100644 --- a/networkmanagement/appletproxymodel.h +++ b/networkmanagement/appletproxymodel.h @@ -1,5 +1,6 @@ /* Copyright 2013-2014 Jan Grulich + Copyright 2021 Reven Martin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -29,15 +30,32 @@ class NETWORKMANAGER_EXPORT AppletProxyModel : public QSortFilterProxyModel { Q_OBJECT + Q_PROPERTY(Type type READ type WRITE setType NOTIFY typeChanged) Q_PROPERTY(QAbstractItemModel * sourceModel READ sourceModel WRITE setSourceModel) public: explicit AppletProxyModel(QObject *parent = nullptr); ~AppletProxyModel() override; + enum Type { + UnknownType = 0, + WiredType, + WirelessType + }; + Q_ENUM(Type) + + Type type() const; + void setType(Type type); + +signals: + void typeChanged(); + protected: bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; + +private: + Type m_type = UnknownType; }; #endif // APPLETPROXYMODEL_H diff --git a/networkmanagement/qmlplugins.cpp b/networkmanagement/qmlplugins.cpp index 140339a..cc4b428 100644 --- a/networkmanagement/qmlplugins.cpp +++ b/networkmanagement/qmlplugins.cpp @@ -2,7 +2,6 @@ #include "networkmodel.h" #include "networkmodelitem.h" #include "appletproxymodel.h" -#include "technologyproxymodel.h" #include "wiressitemsettings.h" #include "connectionicon.h" #include "network.h" @@ -21,7 +20,6 @@ void QmlPlugins::registerTypes(const char* uri) QLatin1String("Cannot instantiate NetworkModelItem")); qmlRegisterType(uri, 1, 0, "AppletProxyModel"); qmlRegisterType(uri, 1, 0, "NetworkModel"); - qmlRegisterType(uri, 1, 0, "TechnologyProxyModel"); qmlRegisterType(uri, 1, 0, "WirelessItemSettings"); qmlRegisterType(uri, 1, 0, "ConnectionIcon"); qmlRegisterType(uri, 1, 0, "Network"); diff --git a/networkmanagement/technologyproxymodel.cpp b/networkmanagement/technologyproxymodel.cpp deleted file mode 100644 index e98bab7..0000000 --- a/networkmanagement/technologyproxymodel.cpp +++ /dev/null @@ -1,144 +0,0 @@ - -/**************************************************************************** - * - * Copyright (C) 2017 Pier Luigi Fiorini - * - * $BEGIN_LICENSE:LGPLv3+$ - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * $END_LICENSE$ - ***************************************************************************/ - -#include "networkmodel.h" -#include "networkmodelitem.h" -#include "technologyproxymodel.h" - -static NetworkManager::ConnectionSettings::ConnectionType convertType(TechnologyProxyModel::Type type) -{ - switch (type) { - case TechnologyProxyModel::UnknownType: - return NetworkManager::ConnectionSettings::ConnectionType::Unknown; - case TechnologyProxyModel::AdslType: - return NetworkManager::ConnectionSettings::ConnectionType::Adsl; - case TechnologyProxyModel::BluetoothType: - return NetworkManager::ConnectionSettings::ConnectionType::Bluetooth; - case TechnologyProxyModel::CdmaType: - return NetworkManager::ConnectionSettings::ConnectionType::Cdma; - case TechnologyProxyModel::GsmType: - return NetworkManager::ConnectionSettings::ConnectionType::Gsm; - case TechnologyProxyModel::OLPCMeshType: - return NetworkManager::ConnectionSettings::ConnectionType::OLPCMesh; - case TechnologyProxyModel::PppoeType: - return NetworkManager::ConnectionSettings::ConnectionType::Pppoe; - case TechnologyProxyModel::VpnType: - return NetworkManager::ConnectionSettings::ConnectionType::Vpn; - case TechnologyProxyModel::WimaxType: - return NetworkManager::ConnectionSettings::ConnectionType::Wimax; - case TechnologyProxyModel::WiredType: - return NetworkManager::ConnectionSettings::ConnectionType::Wired; - case TechnologyProxyModel::WirelessType: - return NetworkManager::ConnectionSettings::ConnectionType::Wireless; - default: - break; - } - - return NetworkManager::ConnectionSettings::ConnectionType::Unknown; -} - -TechnologyProxyModel::TechnologyProxyModel(QObject *parent) - : QSortFilterProxyModel(parent) -{ - setDynamicSortFilter(true); - setSortCaseSensitivity(Qt::CaseInsensitive); - setSortLocaleAware(true); - sort(0, Qt::DescendingOrder); -} - -TechnologyProxyModel::Type TechnologyProxyModel::type() const -{ - return m_type; -} - -void TechnologyProxyModel::setType(Type type) -{ - if (m_type == type) - return; - m_type = type; - Q_EMIT typeChanged(); - - if (type == UnknownType) - setFilterRole(0); - else - setFilterRole(NetworkModel::TypeRole); -} - -bool TechnologyProxyModel::showInactiveConnections() const -{ - return m_showInactiveConnections; -} - -void TechnologyProxyModel::setShowInactiveConnections(bool value) -{ - if (m_showInactiveConnections == value) - return; - - m_showInactiveConnections = value; - Q_EMIT showInactiveConnectionsChanged(); -} - -bool TechnologyProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const -{ - const QModelIndex index = sourceModel()->index(source_row, 0, source_parent); - - // Filter out slaves and duplicates - const bool isSlave = sourceModel()->data(index, NetworkModel::SlaveRole).toBool(); - const bool isDuplicate = sourceModel()->data(index, NetworkModel::DuplicateRole).toBool(); - if (isSlave || isDuplicate) - return false; - - // Connection and item type - const NetworkManager::ConnectionSettings::ConnectionType type = - static_cast(sourceModel()->data(index, NetworkModel::TypeRole).toUInt()); - const NetworkModelItem::ItemType itemType = - static_cast(sourceModel()->data(index, NetworkModel::ItemTypeRole).toUInt()); - - // Filter-out certain connection types we are not interested in - if (m_type != UnknownType) { - if (type != convertType(m_type)) - return false; - } - - // Filter-out access points - if (itemType != NetworkModelItem::AvailableConnection && - itemType != NetworkModelItem::AvailableAccessPoint) - return false; - - // Filter by state - const NetworkManager::ActiveConnection::State state = - static_cast(sourceModel()->data(index, NetworkModel::ConnectionStateRole).toUInt()); - if (!m_showInactiveConnections && state == NetworkManager::ActiveConnection::Deactivated) - return false; - - // Filter on connection name - const QString pattern = filterRegExp().pattern(); - if (!pattern.isEmpty()) { - QString data = sourceModel()->data(index, Qt::DisplayRole).toString(); - if (data.isEmpty()) - data = sourceModel()->data(index, NetworkModel::NameRole).toString(); - return data.contains(pattern, Qt::CaseInsensitive); - } - - return true; -} diff --git a/networkmanagement/technologyproxymodel.h b/networkmanagement/technologyproxymodel.h deleted file mode 100644 index 26abcbe..0000000 --- a/networkmanagement/technologyproxymodel.h +++ /dev/null @@ -1,75 +0,0 @@ -/**************************************************************************** - * - * Copyright (C) 2017 Pier Luigi Fiorini - * - * $BEGIN_LICENSE:LGPLv3+$ - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * $END_LICENSE$ - ***************************************************************************/ - -#ifndef TECHNOLOGYPROXYMODEL_H -#define TECHNOLOGYPROXYMODEL_H - -#include - -#include - -#include - -class NETWORKMANAGER_EXPORT TechnologyProxyModel : public QSortFilterProxyModel -{ - Q_OBJECT - Q_PROPERTY(Type type READ type WRITE setType NOTIFY typeChanged) - Q_PROPERTY(bool showInactiveConnections READ showInactiveConnections WRITE setShowInactiveConnections NOTIFY showInactiveConnectionsChanged) - Q_PROPERTY(QAbstractItemModel *sourceModel READ sourceModel WRITE setSourceModel) - -public: - enum Type { - UnknownType = 0, - AdslType, - BluetoothType, - CdmaType, - GsmType, - OLPCMeshType, - PppoeType, - VpnType, - WimaxType, - WiredType, - WirelessType - }; - Q_ENUM(Type) - - TechnologyProxyModel(QObject *parent = nullptr); - - Type type() const; - void setType(Type type); - - bool showInactiveConnections() const; - void setShowInactiveConnections(bool value); - -Q_SIGNALS: - void typeChanged(); - void showInactiveConnectionsChanged(); - -protected: - bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; - -private: - Type m_type = UnknownType; - bool m_showInactiveConnections = false; -}; - -#endif //TECHNOLOGYPROXYMODEL_H