File indexing completed on 2024-07-14 15:48:23

0001 /*
0002     SPDX-FileCopyrightText: 2016 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 "iodinewidget.h"
0008 #include "ui_iodine.h"
0009 
0010 #include "nm-iodine-service.h"
0011 
0012 #include <NetworkManagerQt/Setting>
0013 
0014 #include <QDBusMetaType>
0015 
0016 IodineWidget::IodineWidget(const NetworkManager::VpnSetting::Ptr &setting, QWidget *parent, Qt::WindowFlags f)
0017     : SettingWidget(setting, parent, f)
0018     , m_ui(new Ui::IodineWidget)
0019     , m_setting(setting)
0020 {
0021     qDBusRegisterMetaType<NMStringMap>();
0022 
0023     m_ui->setupUi(this);
0024 
0025     m_ui->le_password->setPasswordOptionsEnabled(true);
0026 
0027     // Connect for setting check
0028     watchChangedSetting();
0029 
0030     // Connect for validity check
0031     connect(m_ui->le_toplevelDomain, &QLineEdit::textChanged, this, &IodineWidget::slotWidgetChanged);
0032 
0033     KAcceleratorManager::manage(this);
0034 
0035     if (setting && !setting->isNull()) {
0036         loadConfig(setting);
0037     }
0038 }
0039 
0040 IodineWidget::~IodineWidget()
0041 {
0042     delete m_ui;
0043 }
0044 
0045 void IodineWidget::loadConfig(const NetworkManager::Setting::Ptr &setting)
0046 {
0047     const NMStringMap data = m_setting->data();
0048 
0049     const QString toplevelDomain = data.value(NM_IODINE_KEY_TOPDOMAIN);
0050     if (!toplevelDomain.isEmpty()) {
0051         m_ui->le_toplevelDomain->setText(toplevelDomain);
0052     }
0053 
0054     const QString nameserver = data.value(NM_IODINE_KEY_NAMESERVER);
0055     if (!nameserver.isEmpty()) {
0056         m_ui->le_nameserver->setText(nameserver);
0057     }
0058 
0059     const NetworkManager::Setting::SecretFlags passwordFlag =
0060         static_cast<NetworkManager::Setting::SecretFlags>(data.value(NM_IODINE_KEY_PASSWORD "-flags").toInt());
0061     if (passwordFlag == NetworkManager::Setting::None) {
0062         m_ui->le_password->setPasswordOption(PasswordField::StoreForAllUsers);
0063     } else if (passwordFlag == NetworkManager::Setting::AgentOwned) {
0064         m_ui->le_password->setPasswordOption(PasswordField::StoreForUser);
0065     } else {
0066         m_ui->le_password->setPasswordOption(PasswordField::AlwaysAsk);
0067     }
0068 
0069     const QString fragSize = data.value(NM_IODINE_KEY_FRAGSIZE);
0070     if (!fragSize.isEmpty()) {
0071         m_ui->sb_fragmentSize->setValue(fragSize.toInt());
0072     }
0073 
0074     loadSecrets(setting);
0075 }
0076 
0077 void IodineWidget::loadSecrets(const NetworkManager::Setting::Ptr &setting)
0078 {
0079     NetworkManager::VpnSetting::Ptr vpnSetting = setting.staticCast<NetworkManager::VpnSetting>();
0080 
0081     if (vpnSetting) {
0082         const NMStringMap secrets = vpnSetting->secrets();
0083 
0084         const QString password = secrets.value(NM_IODINE_KEY_PASSWORD);
0085         if (!password.isEmpty()) {
0086             m_ui->le_password->setText(password);
0087         }
0088     }
0089 }
0090 
0091 QVariantMap IodineWidget::setting() const
0092 {
0093     NetworkManager::VpnSetting setting;
0094     setting.setServiceType(QLatin1String(NM_DBUS_SERVICE_IODINE));
0095     NMStringMap data;
0096     NMStringMap secrets;
0097 
0098     if (!m_ui->le_toplevelDomain->text().isEmpty()) {
0099         data.insert(NM_IODINE_KEY_TOPDOMAIN, m_ui->le_toplevelDomain->text());
0100     }
0101 
0102     if (!m_ui->le_nameserver->text().isEmpty()) {
0103         data.insert(NM_IODINE_KEY_NAMESERVER, m_ui->le_nameserver->text());
0104     }
0105 
0106     if (!m_ui->le_password->text().isEmpty()) {
0107         secrets.insert(NM_IODINE_KEY_PASSWORD, m_ui->le_password->text());
0108     }
0109 
0110     if (m_ui->le_password->passwordOption() == PasswordField::StoreForAllUsers) {
0111         data.insert(NM_IODINE_KEY_PASSWORD "-flags", QString::number(NetworkManager::Setting::None));
0112     } else if (m_ui->le_password->passwordOption() == PasswordField::StoreForUser) {
0113         data.insert(NM_IODINE_KEY_PASSWORD "-flags", QString::number(NetworkManager::Setting::AgentOwned));
0114     } else {
0115         data.insert(NM_IODINE_KEY_PASSWORD "-flags", QString::number(NetworkManager::Setting::NotSaved));
0116     }
0117 
0118     if (m_ui->sb_fragmentSize->value()) {
0119         data.insert(NM_IODINE_KEY_FRAGSIZE, QString::number(m_ui->sb_fragmentSize->value()));
0120     }
0121 
0122     setting.setData(data);
0123     setting.setSecrets(secrets);
0124     return setting.toMap();
0125 }
0126 
0127 bool IodineWidget::isValid() const
0128 {
0129     return !m_ui->le_toplevelDomain->text().isEmpty();
0130 }