Warning, file /plasma/plasma-nm/libs/models/creatableconnectionsmodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2016-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 "creatableconnectionsmodel.h"
0008 #include <KLocalizedString>
0009 #include <KPluginMetaData>
0010 #include <NetworkManagerQt/Manager>
0011 
0012 CreatableConnectionItem::CreatableConnectionItem(const QString &typeName,
0013                                                  const QString &typeSection,
0014                                                  const QString &description,
0015                                                  const QString &icon,
0016                                                  NetworkManager::ConnectionSettings::ConnectionType type,
0017                                                  const QString &vpnType,
0018                                                  const QString &specificType,
0019                                                  bool shared,
0020                                                  QObject *parent)
0021     : QObject(parent)
0022     , m_shared(shared)
0023     , m_connectionType(type)
0024     , m_description(description)
0025     , m_icon(icon)
0026     , m_specificType(specificType)
0027     , m_typeName(typeName)
0028     , m_typeSection(typeSection)
0029     , m_vpnType(vpnType)
0030 {
0031 }
0032 
0033 CreatableConnectionItem::CreatableConnectionItem(QObject *parent)
0034     : QObject(parent)
0035 {
0036 }
0037 
0038 CreatableConnectionItem::~CreatableConnectionItem() = default;
0039 NetworkManager::ConnectionSettings::ConnectionType CreatableConnectionItem::connectionType() const
0040 {
0041     return m_connectionType;
0042 }
0043 
0044 void CreatableConnectionItem::setConnectionType(NetworkManager::ConnectionSettings::ConnectionType type)
0045 {
0046     m_connectionType = type;
0047 }
0048 
0049 QString CreatableConnectionItem::description() const
0050 {
0051     return m_description;
0052 }
0053 
0054 void CreatableConnectionItem::setDescription(const QString &description)
0055 {
0056     m_description = description;
0057 }
0058 
0059 QString CreatableConnectionItem::icon() const
0060 {
0061     return m_icon;
0062 }
0063 
0064 void CreatableConnectionItem::setIcon(const QString &icon)
0065 {
0066     m_icon = icon;
0067 }
0068 
0069 bool CreatableConnectionItem::shared() const
0070 {
0071     return m_shared;
0072 }
0073 
0074 void CreatableConnectionItem::setShared(bool shared)
0075 {
0076     m_shared = shared;
0077 }
0078 
0079 QString CreatableConnectionItem::specificType() const
0080 {
0081     return m_specificType;
0082 }
0083 
0084 void CreatableConnectionItem::setSpecificType(const QString &specificType)
0085 {
0086     m_specificType = specificType;
0087 }
0088 
0089 QString CreatableConnectionItem::typeName() const
0090 {
0091     return m_typeName;
0092 }
0093 
0094 void CreatableConnectionItem::setTypeName(const QString &typeName)
0095 {
0096     m_typeName = typeName;
0097 }
0098 
0099 QString CreatableConnectionItem::typeSection() const
0100 {
0101     return m_typeSection;
0102 }
0103 
0104 void CreatableConnectionItem::setTypeSection(const QString &typeSection)
0105 {
0106     m_typeSection = typeSection;
0107 }
0108 
0109 QString CreatableConnectionItem::vpnType() const
0110 {
0111     return m_vpnType;
0112 }
0113 
0114 void CreatableConnectionItem::setVpnType(const QString &vpnType)
0115 {
0116     m_vpnType = vpnType;
0117 }
0118 
0119 CreatableConnectionsModel::CreatableConnectionsModel(QObject *parent)
0120     : QAbstractListModel(parent)
0121 {
0122     populateModel();
0123 }
0124 
0125 void CreatableConnectionsModel::populateModel()
0126 {
0127     beginResetModel();
0128 
0129     if (!m_list.isEmpty()) {
0130         qDeleteAll(m_list);
0131         m_list.clear();
0132     }
0133 
0134     CreatableConnectionItem *connectionItem{nullptr};
0135     connectionItem = new CreatableConnectionItem(i18n("DSL"),
0136                                                  i18n("Hardware based connections"),
0137                                                  i18n("Some DSL description"),
0138                                                  QStringLiteral("network-modem"),
0139                                                  NetworkManager::ConnectionSettings::Pppoe);
0140     m_list << connectionItem;
0141 
0142     connectionItem = new CreatableConnectionItem(i18n("Infiniband"),
0143                                                  i18n("Hardware based connections"),
0144                                                  i18n("Some infiniband description"),
0145                                                  QStringLiteral("network-wired"),
0146                                                  NetworkManager::ConnectionSettings::Infiniband);
0147     m_list << connectionItem;
0148 
0149     connectionItem = new CreatableConnectionItem(i18n("Mobile Broadband"),
0150                                                  i18n("Hardware based connections"),
0151                                                  i18n("Some mobile broadband description"),
0152                                                  QStringLiteral("smartphone"),
0153                                                  NetworkManager::ConnectionSettings::Gsm);
0154     m_list << connectionItem;
0155 
0156     connectionItem = new CreatableConnectionItem(i18n("Wired Ethernet"),
0157                                                  i18n("Hardware based connections"),
0158                                                  i18n("Some wired ethernet description"),
0159                                                  QStringLiteral("network-wired"),
0160                                                  NetworkManager::ConnectionSettings::Wired);
0161     m_list << connectionItem;
0162 
0163     connectionItem = new CreatableConnectionItem(i18n("Wired Ethernet (shared)"),
0164                                                  i18n("Hardware based connections"),
0165                                                  i18n("Some wired ethernet description"),
0166                                                  QStringLiteral("network-wired"),
0167                                                  NetworkManager::ConnectionSettings::Wired,
0168                                                  QString(),
0169                                                  QString(),
0170                                                  true); // VpnType and SpecificType are empty
0171     m_list << connectionItem;
0172 
0173     connectionItem = new CreatableConnectionItem(i18n("Wi-Fi"),
0174                                                  i18n("Hardware based connections"),
0175                                                  i18n("Some wi-fi description"),
0176                                                  QStringLiteral("network-wireless"),
0177                                                  NetworkManager::ConnectionSettings::Wireless);
0178     m_list << connectionItem;
0179 
0180     connectionItem = new CreatableConnectionItem(i18n("Wi-Fi (shared)"),
0181                                                  i18n("Hardware based connections"),
0182                                                  i18n("Some wi-fi description"),
0183                                                  QStringLiteral("network-wireless"),
0184                                                  NetworkManager::ConnectionSettings::Wireless,
0185                                                  QString(),
0186                                                  QString(),
0187                                                  true); // VpnType and SpecificType are empty
0188     m_list << connectionItem;
0189 
0190     connectionItem = new CreatableConnectionItem(i18n("Bond"),
0191                                                  i18n("Virtual connections"),
0192                                                  i18n("Some bond description"),
0193                                                  QStringLiteral("network-wired"),
0194                                                  NetworkManager::ConnectionSettings::Bond,
0195                                                  QString(),
0196                                                  QString(),
0197                                                  true); // VpnType and SpecificType are empty
0198     m_list << connectionItem;
0199 
0200     connectionItem = new CreatableConnectionItem(i18n("Bridge"),
0201                                                  i18n("Virtual connections"),
0202                                                  i18n("Some bond description"),
0203                                                  QStringLiteral("network-wired"),
0204                                                  NetworkManager::ConnectionSettings::Bridge,
0205                                                  QString(),
0206                                                  QString(),
0207                                                  true); // VpnType and SpecificType are empty
0208     m_list << connectionItem;
0209 
0210     connectionItem = new CreatableConnectionItem(i18n("Team"),
0211                                                  i18n("Virtual connections"),
0212                                                  i18n("Some team description"),
0213                                                  QStringLiteral("network-wired"),
0214                                                  NetworkManager::ConnectionSettings::Team,
0215                                                  QString(),
0216                                                  QString(),
0217                                                  true); // VpnType and SpecificType are empty
0218     m_list << connectionItem;
0219 
0220     connectionItem = new CreatableConnectionItem(i18n("Vlan"),
0221                                                  i18n("Virtual connections"),
0222                                                  i18n("Some vlan description"),
0223                                                  QStringLiteral("network-wired"),
0224                                                  NetworkManager::ConnectionSettings::Vlan,
0225                                                  QString(),
0226                                                  QString(),
0227                                                  true); // VpnType and SpecificType are empty
0228     m_list << connectionItem;
0229 
0230     QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("plasma/network/vpn"));
0231 
0232     std::sort(plugins.begin(), plugins.end(), [](const auto &left, const auto &right) {
0233         return QString::localeAwareCompare(left.name(), right.name()) <= 0;
0234     });
0235 
0236     for (const auto &service : std::as_const(plugins)) {
0237         const QString vpnType = service.value(QStringLiteral("X-NetworkManager-Services"));
0238         const QString vpnSubType = service.value(QStringLiteral("X-NetworkManager-Services-Subtype"));
0239         const QString vpnDescription = service.description();
0240 
0241         connectionItem = new CreatableConnectionItem(service.name(),
0242                                                      i18n("VPN connections"),
0243                                                      vpnDescription,
0244                                                      QStringLiteral("network-vpn"),
0245                                                      NetworkManager::ConnectionSettings::Vpn,
0246                                                      vpnType,
0247                                                      vpnSubType,
0248                                                      false);
0249         m_list << connectionItem;
0250     }
0251 
0252     // WireGuard changed from VPN plugin to primary device in version 1.16 of NetworkManager
0253     if (NetworkManager::checkVersion(1, 16, 0)) {
0254         connectionItem = new CreatableConnectionItem(i18n("WireGuard"),
0255                                                      i18n("VPN connections"),
0256                                                      i18n("WireGuard"),
0257                                                      QStringLiteral("network-vpn"),
0258                                                      NetworkManager::ConnectionSettings::WireGuard,
0259                                                      QStringLiteral("WireGuard"),
0260                                                      QString(),
0261                                                      true); // VpnType and SpecificType are empty
0262         m_list << connectionItem;
0263     }
0264 
0265     // Placeholder for VPN import
0266     connectionItem = new CreatableConnectionItem(i18n("Import VPN connection…"),
0267                                                  i18n("Other"),
0268                                                  i18n("Import a saved configuration file"),
0269                                                  QStringLiteral("document-import"),
0270                                                  NetworkManager::ConnectionSettings::Vpn,
0271                                                  QStringLiteral("imported"),
0272                                                  QStringLiteral("imported"),
0273                                                  false);
0274     m_list << connectionItem;
0275     endResetModel();
0276 }
0277 
0278 CreatableConnectionsModel::~CreatableConnectionsModel() = default;
0279 
0280 QVariant CreatableConnectionsModel::data(const QModelIndex &index, int role) const
0281 {
0282     const int row = index.row();
0283 
0284     if (row >= 0 && row < m_list.count()) {
0285         CreatableConnectionItem *item = m_list.at(row);
0286 
0287         switch (role) {
0288         case ConnectionDescription:
0289             return item->description();
0290         case ConnectionIcon:
0291             return item->icon();
0292         case ConnectionSpeficType:
0293             return item->specificType();
0294         case ConnectionShared:
0295             return item->shared();
0296         case ConnectionType:
0297             return item->connectionType();
0298         case ConnectionTypeName:
0299             return item->typeName();
0300         case ConnectionTypeSection:
0301             return item->typeSection();
0302         case ConnectionVpnType:
0303             return item->vpnType();
0304         default:
0305             break;
0306         }
0307     }
0308 
0309     return {};
0310 }
0311 
0312 QHash<int, QByteArray> CreatableConnectionsModel::roleNames() const
0313 {
0314     QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
0315     roles[ConnectionDescription] = "ConnectionDescription";
0316     roles[ConnectionIcon] = "ConnectionIcon";
0317     roles[ConnectionSpeficType] = "ConnectionSpecificType";
0318     roles[ConnectionShared] = "ConnectionShared";
0319     roles[ConnectionType] = "ConnectionType";
0320     roles[ConnectionTypeName] = "ConnectionTypeName";
0321     roles[ConnectionTypeSection] = "ConnectionTypeSection";
0322     roles[ConnectionVpnType] = "ConnectionVpnType";
0323 
0324     return roles;
0325 }
0326 
0327 int CreatableConnectionsModel::rowCount(const QModelIndex &parent) const
0328 {
0329     Q_UNUSED(parent);
0330     return m_list.count();
0331 }