File indexing completed on 2024-07-21 07:58:04

0001 /*
0002     SPDX-FileCopyrightText: 2013 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 "connectionwidget.h"
0008 #include "advancedpermissionswidget.h"
0009 #include "ui_connectionwidget.h"
0010 
0011 #include <NetworkManagerQt/Connection>
0012 #include <NetworkManagerQt/Settings>
0013 
0014 #include <KAcceleratorManager>
0015 #include <KLocalizedString>
0016 #include <KUser>
0017 #include <QDBusConnection>
0018 #include <QDialog>
0019 #include <QDialogButtonBox>
0020 #include <QVBoxLayout>
0021 
0022 ConnectionWidget::ConnectionWidget(const NetworkManager::ConnectionSettings::Ptr &settings, QWidget *parent, Qt::WindowFlags f)
0023     : QWidget(parent, f)
0024     , m_widget(new Ui::ConnectionWidget)
0025     , m_type(settings->connectionType())
0026     , m_masterUuid(settings->master())
0027     , m_slaveType(settings->slaveType())
0028 {
0029     m_widget->setupUi(this);
0030 
0031     m_widget->firewallZone->addItems(firewallZones());
0032 
0033     // VPN combo
0034     populateVpnConnections();
0035     if (settings->connectionType() == NetworkManager::ConnectionSettings::Vpn) {
0036         m_widget->autoconnectVpn->setEnabled(false);
0037         m_widget->vpnCombobox->setEnabled(false);
0038         m_widget->autoconnect->setEnabled(false);
0039     } else {
0040         m_widget->autoconnectVpn->setEnabled(true);
0041         m_widget->autoconnect->setEnabled(true);
0042     }
0043 
0044     connect(m_widget->autoconnectVpn, &QCheckBox::toggled, this, &ConnectionWidget::autoVpnToggled);
0045 
0046     if (settings) {
0047         loadConfig(settings);
0048     }
0049 
0050     m_tmpSetting.setPermissions(settings->permissions());
0051 
0052     KAcceleratorManager::manage(this);
0053 
0054     connect(m_widget->autoconnect, &QCheckBox::stateChanged, this, &ConnectionWidget::settingChanged);
0055     connect(m_widget->allUsers, &QCheckBox::stateChanged, this, &ConnectionWidget::settingChanged);
0056     connect(m_widget->allUsers, &QCheckBox::stateChanged, this, &ConnectionWidget::allUsersChanged);
0057     connect(m_widget->autoconnectVpn, &QCheckBox::stateChanged, this, &ConnectionWidget::settingChanged);
0058     connect(m_widget->pushButtonPermissions, &QPushButton::clicked, this, &ConnectionWidget::settingChanged);
0059     connect(m_widget->firewallZone, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ConnectionWidget::settingChanged);
0060     connect(m_widget->firewallZone, &QComboBox::currentTextChanged, this, &ConnectionWidget::settingChanged);
0061     connect(m_widget->vpnCombobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ConnectionWidget::settingChanged);
0062     connect(m_widget->vpnCombobox, &QComboBox::currentTextChanged, this, &ConnectionWidget::settingChanged);
0063     connect(m_widget->prioritySpin, QOverload<int>::of(&QSpinBox::valueChanged), this, &ConnectionWidget::settingChanged);
0064     connect(m_widget->metered, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ConnectionWidget::settingChanged);
0065 
0066     connect(m_widget->pushButtonPermissions, &QPushButton::clicked, this, &ConnectionWidget::openAdvancedPermissions);
0067 }
0068 
0069 ConnectionWidget::~ConnectionWidget()
0070 {
0071     delete m_widget;
0072 }
0073 
0074 void ConnectionWidget::loadConfig(const NetworkManager::ConnectionSettings::Ptr &settings)
0075 {
0076     if (settings->permissions().isEmpty()) {
0077         m_widget->allUsers->setChecked(true);
0078     } else {
0079         m_widget->allUsers->setChecked(false);
0080     }
0081 
0082     const QString zone = settings->zone();
0083     m_widget->firewallZone->setCurrentIndex(m_widget->firewallZone->findText(zone));
0084 
0085     const QStringList secondaries = settings->secondaries();
0086     const QStringList vpnKeys = vpnConnections().keys();
0087     if (!secondaries.isEmpty() && !vpnKeys.isEmpty()) {
0088         for (const QString &vpnKey : vpnKeys) {
0089             if (secondaries.contains(vpnKey)) {
0090                 m_widget->vpnCombobox->setCurrentIndex(m_widget->vpnCombobox->findData(vpnKey));
0091                 m_widget->autoconnectVpn->setChecked(true);
0092                 break;
0093             }
0094         }
0095     } else {
0096         m_widget->autoconnectVpn->setChecked(false);
0097     }
0098 
0099     m_widget->autoconnect->setChecked(settings->autoconnect());
0100 
0101     if (m_widget->prioritySpin->isEnabled()) {
0102         m_widget->prioritySpin->setValue(settings->autoconnectPriority());
0103     }
0104 
0105     m_widget->metered->setCurrentIndex(settings->metered());
0106 }
0107 
0108 NMVariantMapMap ConnectionWidget::setting() const
0109 {
0110     NetworkManager::ConnectionSettings settings;
0111 
0112     settings.setConnectionType(m_type);
0113     settings.setAutoconnect(m_widget->autoconnect->isChecked());
0114     settings.setMaster(m_masterUuid);
0115     settings.setSlaveType(m_slaveType);
0116 
0117     if (m_widget->allUsers->isChecked()) {
0118         settings.setPermissions(QHash<QString, QString>());
0119     } else {
0120         if (m_tmpSetting.permissions().isEmpty()) {
0121             settings.addToPermissions(KUser().loginName(), QString());
0122         } else {
0123             settings.setPermissions(m_tmpSetting.permissions());
0124         }
0125     }
0126 
0127     if (m_widget->autoconnectVpn->isChecked() && m_widget->vpnCombobox->count() > 0) {
0128         settings.setSecondaries(QStringList() << m_widget->vpnCombobox->itemData(m_widget->vpnCombobox->currentIndex()).toString());
0129     }
0130 
0131     const QString zone = m_widget->firewallZone->currentText();
0132     if (!zone.isEmpty()) {
0133         settings.setZone(zone);
0134     }
0135 
0136     if (m_widget->prioritySpin->isEnabled()) {
0137         settings.setAutoconnectPriority(m_widget->prioritySpin->value());
0138     }
0139 
0140     settings.setMetered(static_cast<NetworkManager::ConnectionSettings::Metered>(m_widget->metered->currentIndex()));
0141 
0142     return settings.toMap();
0143 }
0144 
0145 bool ConnectionWidget::allUsers() const
0146 {
0147     return m_widget->allUsers->isChecked();
0148 }
0149 
0150 void ConnectionWidget::autoVpnToggled(bool on)
0151 {
0152     m_widget->vpnCombobox->setEnabled(on);
0153 }
0154 
0155 void ConnectionWidget::openAdvancedPermissions()
0156 {
0157     QPointer<AdvancedPermissionsWidget> dialog = new AdvancedPermissionsWidget(m_tmpSetting.permissions(), this);
0158     dialog->setWindowTitle(i18nc("@title:window advanced permissions editor", "Advanced Permissions Editor"));
0159     if (dialog->exec() == QDialog::Accepted) {
0160         m_tmpSetting.setPermissions(dialog->currentUsers());
0161     }
0162     delete dialog;
0163 }
0164 
0165 NMStringMap ConnectionWidget::vpnConnections() const
0166 {
0167     NetworkManager::Connection::List list = NetworkManager::listConnections();
0168     NMStringMap result;
0169 
0170     for (const NetworkManager::Connection::Ptr &conn : list) {
0171         NetworkManager::ConnectionSettings::Ptr conSet = conn->settings();
0172         if (conSet->connectionType() == NetworkManager::ConnectionSettings::Vpn //
0173             || conSet->connectionType() == NetworkManager::ConnectionSettings::WireGuard) {
0174             // qCDebug(PLASMA_NM_EDITOR_LOG) << "Found VPN" << conSet->id() << conSet->uuid();
0175             result.insert(conSet->uuid(), conSet->id());
0176         }
0177     }
0178 
0179     return result;
0180 }
0181 
0182 QStringList ConnectionWidget::firewallZones() const
0183 {
0184     QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.fedoraproject.FirewallD1"), //
0185                                                       QStringLiteral("/org/fedoraproject/FirewallD1"),
0186                                                       QStringLiteral("org.fedoraproject.FirewallD1.zone"),
0187                                                       QStringLiteral("getZones"));
0188     QDBusPendingReply<QStringList> reply = QDBusConnection::systemBus().asyncCall(msg);
0189     reply.waitForFinished();
0190     if (reply.isValid())
0191         return reply.value();
0192 
0193     return {};
0194 }
0195 
0196 void ConnectionWidget::populateVpnConnections()
0197 {
0198     QMapIterator<QString, QString> it(vpnConnections());
0199     while (it.hasNext()) {
0200         it.next();
0201         m_widget->vpnCombobox->addItem(it.value(), it.key());
0202     }
0203 }
0204 
0205 #include "moc_connectionwidget.cpp"