From 9e5925af97ed3b9b9c525a53e8b0420c2b4c0df0 Mon Sep 17 00:00:00 2001 From: cutefishd Date: Sun, 18 Apr 2021 00:32:10 +0800 Subject: [PATCH] NetworkManagement: add IdentityModel --- networkmanagement/CMakeLists.txt | 3 + networkmanagement/identitymodel.cpp | 117 ++++++++++++++++++++++++++++ networkmanagement/identitymodel.h | 48 ++++++++++++ networkmanagement/qmlplugins.cpp | 2 + 4 files changed, 170 insertions(+) create mode 100644 networkmanagement/identitymodel.cpp create mode 100644 networkmanagement/identitymodel.h diff --git a/networkmanagement/CMakeLists.txt b/networkmanagement/CMakeLists.txt index 2041784..48127d1 100644 --- a/networkmanagement/CMakeLists.txt +++ b/networkmanagement/CMakeLists.txt @@ -29,6 +29,9 @@ set(NETWORKMGR_SRCS connectionicon.cpp connectionicon.h + identitymodel.cpp + identitymodel.h + qmlplugins.cpp qmlplugins.h ) diff --git a/networkmanagement/identitymodel.cpp b/networkmanagement/identitymodel.cpp new file mode 100644 index 0000000..789514b --- /dev/null +++ b/networkmanagement/identitymodel.cpp @@ -0,0 +1,117 @@ +/* + Copyright 2013-2018 Jan Grulich + + This library 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 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library 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 library. If not, see . +*/ + +#include "identitymodel.h" +#include "networkmodel.h" +#include "networkmodelitem.h" +#include "uiutils.h" + +#include + +#include +#include + +IdentityModel::IdentityModel(QObject *parent) + : QIdentityProxyModel(parent) +{ + NetworkModel *baseModel = new NetworkModel(this); + setSourceModel(baseModel); +} + +IdentityModel::~IdentityModel() +{ +} + +Qt::ItemFlags IdentityModel::flags(const QModelIndex &index) const +{ + const QModelIndex mappedProxyIndex = index.sibling(index.row(), 0); + return QIdentityProxyModel::flags(mappedProxyIndex) | Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} + +int IdentityModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + + return 3; +} + +QHash IdentityModel::roleNames() const +{ + QHash roles = QIdentityProxyModel::roleNames(); + roles[ConnectionIconRole] = "ConnectionIcon"; + roles[ConnectionTypeRole] = "ConnectionType"; + roles[VpnConnectionExportable] = "VpnConnectionExportable"; + + return roles; +} + +QVariant IdentityModel::data(const QModelIndex &index, int role) const +{ + const QModelIndex sourceIndex = sourceModel()->index(index.row(), 0); + NetworkManager::ConnectionSettings::ConnectionType type = static_cast(sourceModel()->data(sourceIndex, NetworkModel::TypeRole).toInt()); + + NetworkManager::ConnectionSettings::Ptr settings; + NetworkManager::VpnSetting::Ptr vpnSetting ; + if (type == NetworkManager::ConnectionSettings::Vpn) { + settings = NetworkManager::findConnection(sourceModel()->data(sourceIndex, NetworkModel::ConnectionPathRole).toString())->settings(); + if (settings) { + vpnSetting = settings->setting(NetworkManager::Setting::Vpn).staticCast(); + } + } + + QString tooltip; + const QString iconName = UiUtils::iconAndTitleForConnectionSettingsType(type, tooltip); + + if (role == ConnectionIconRole) { + return iconName; + } else if (role == ConnectionTypeRole) { + if (type == NetworkManager::ConnectionSettings::Vpn && vpnSetting) { + return QString("%1 (%2)").arg(tooltip).arg(vpnSetting->serviceType().section('.', -1)); + } + return tooltip; + } else if (role == VpnConnectionExportable) { + if (type == NetworkManager::ConnectionSettings::Vpn && vpnSetting) { + return (vpnSetting->serviceType().endsWith(QLatin1String("vpnc")) || + vpnSetting->serviceType().endsWith(QLatin1String("openvpn")) || + vpnSetting->serviceType().endsWith(QLatin1String("wireguard"))); + } + return false; + } else { + return sourceModel()->data(index, role); + } + + return QVariant(); +} + +QModelIndex IdentityModel::index(int row, int column, const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return createIndex(row, column); +} + +QModelIndex IdentityModel::mapToSource(const QModelIndex &proxyIndex) const +{ + if (proxyIndex.column() > 0) { + return QModelIndex(); + } + + return QIdentityProxyModel::mapToSource(proxyIndex); +} + diff --git a/networkmanagement/identitymodel.h b/networkmanagement/identitymodel.h new file mode 100644 index 0000000..4f8f3d9 --- /dev/null +++ b/networkmanagement/identitymodel.h @@ -0,0 +1,48 @@ +/* + Copyright 2016-2018 Jan Grulich + This library 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 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + This library 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 library. If not, see . +*/ + +#ifndef PLASMA_NM_KCM_IDENTITY_MODEL_H +#define PLASMA_NM_KCM_IDENTITY_MODEL_H + +#include +#include + +class Q_DECL_EXPORT IdentityModel : public QIdentityProxyModel +{ + Q_OBJECT + +public: + explicit IdentityModel(QObject *parent = nullptr); + ~IdentityModel() override; + + enum KcmItemRole { + ConnectionIconRole = Qt::UserRole + 100, + ConnectionTypeRole, + VpnConnectionExportable + }; + + QHash< int, QByteArray > roleNames() const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; + + QModelIndex mapToSource(const QModelIndex &proxyIndex) const override; +}; + +#endif // PLASMA_NM_KCM_IDENTITY_MODEL_H \ No newline at end of file diff --git a/networkmanagement/qmlplugins.cpp b/networkmanagement/qmlplugins.cpp index df2c124..1bfe79a 100644 --- a/networkmanagement/qmlplugins.cpp +++ b/networkmanagement/qmlplugins.cpp @@ -7,6 +7,7 @@ #include "wiressitemsettings.h" #include "connectionicon.h" #include "network.h" +#include "identitymodel.h" #include @@ -21,4 +22,5 @@ void QmlPlugins::registerTypes(const char* uri) qmlRegisterType(uri, 1, 0, "WirelessItemSettings"); qmlRegisterType(uri, 1, 0, "ConnectionIcon"); qmlRegisterType(uri, 1, 0, "Network"); + qmlRegisterType(uri, 1, 0, "IdentityModel"); } \ No newline at end of file