File indexing completed on 2024-10-13 13:17:33
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 "kcmidentitymodel.h" 0008 #include "networkmodel.h" 0009 #include "uiutils.h" 0010 0011 #include <NetworkManagerQt/Settings> 0012 0013 #include <QFont> 0014 #include <QIcon> 0015 0016 #include <KLocalizedString> 0017 0018 KcmIdentityModel::KcmIdentityModel(QObject *parent) 0019 : QIdentityProxyModel(parent) 0020 { 0021 auto baseModel = new NetworkModel(this); 0022 setSourceModel(baseModel); 0023 } 0024 0025 KcmIdentityModel::~KcmIdentityModel() = default; 0026 0027 Qt::ItemFlags KcmIdentityModel::flags(const QModelIndex &index) const 0028 { 0029 const QModelIndex mappedProxyIndex = index.sibling(index.row(), 0); 0030 return QIdentityProxyModel::flags(mappedProxyIndex) | Qt::ItemIsEnabled | Qt::ItemIsSelectable; 0031 } 0032 0033 int KcmIdentityModel::columnCount(const QModelIndex &parent) const 0034 { 0035 Q_UNUSED(parent); 0036 0037 return 3; 0038 } 0039 0040 QHash<int, QByteArray> KcmIdentityModel::roleNames() const 0041 { 0042 QHash<int, QByteArray> roles = QIdentityProxyModel::roleNames(); 0043 roles[KcmConnectionIconRole] = "KcmConnectionIcon"; 0044 roles[KcmConnectionTypeRole] = "KcmConnectionType"; 0045 roles[KcmVpnConnectionExportable] = "KcmVpnConnectionExportable"; 0046 0047 return roles; 0048 } 0049 0050 QVariant KcmIdentityModel::data(const QModelIndex &index, int role) const 0051 { 0052 const QModelIndex sourceIndex = sourceModel()->index(index.row(), 0); 0053 NetworkManager::ConnectionSettings::ConnectionType type = 0054 static_cast<NetworkManager::ConnectionSettings::ConnectionType>(sourceModel()->data(sourceIndex, NetworkModel::TypeRole).toInt()); 0055 0056 NetworkManager::ConnectionSettings::Ptr settings; 0057 NetworkManager::VpnSetting::Ptr vpnSetting; 0058 if (type == NetworkManager::ConnectionSettings::Vpn) { 0059 settings = NetworkManager::findConnection(sourceModel()->data(sourceIndex, NetworkModel::ConnectionPathRole).toString())->settings(); 0060 if (settings) { 0061 vpnSetting = settings->setting(NetworkManager::Setting::Vpn).staticCast<NetworkManager::VpnSetting>(); 0062 } 0063 } 0064 0065 QString tooltip; 0066 const QString iconName = UiUtils::iconAndTitleForConnectionSettingsType(type, tooltip); 0067 0068 if (role == KcmConnectionIconRole) { 0069 return iconName; 0070 } else if (role == KcmConnectionTypeRole) { 0071 if (type == NetworkManager::ConnectionSettings::Vpn && vpnSetting) { 0072 return QStringLiteral("%1 (%2)").arg(tooltip, vpnSetting->serviceType().section(QLatin1Char('.'), -1)); 0073 } 0074 return tooltip; 0075 } else if (role == KcmVpnConnectionExportable) { 0076 if (type == NetworkManager::ConnectionSettings::Vpn && vpnSetting) { 0077 return (vpnSetting->serviceType().endsWith(QLatin1String("vpnc")) // 0078 || vpnSetting->serviceType().endsWith(QLatin1String("openvpn")) // 0079 || vpnSetting->serviceType().endsWith(QLatin1String("wireguard"))); 0080 } 0081 return false; 0082 } else { 0083 return sourceModel()->data(index, role); 0084 } 0085 0086 return {}; 0087 } 0088 0089 QModelIndex KcmIdentityModel::index(int row, int column, const QModelIndex &parent) const 0090 { 0091 Q_UNUSED(parent); 0092 return createIndex(row, column); 0093 } 0094 0095 QModelIndex KcmIdentityModel::mapToSource(const QModelIndex &proxyIndex) const 0096 { 0097 if (proxyIndex.column() > 0) { 0098 return {}; 0099 } 0100 0101 return QIdentityProxyModel::mapToSource(proxyIndex); 0102 }