File indexing completed on 2024-12-08 08:02:31

0001 /*
0002     SPDX-FileCopyrightText: 2013 Lukas Tinkl <ltinkl@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 "pppoewidget.h"
0008 #include "ui_pppoe.h"
0009 
0010 #include <NetworkManagerQt/PppoeSetting>
0011 
0012 PppoeWidget::PppoeWidget(const NetworkManager::Setting::Ptr &setting, QWidget *parent, Qt::WindowFlags f)
0013     : SettingWidget(setting, parent, f)
0014     , m_ui(new Ui::PppoeWidget)
0015 {
0016     m_ui->setupUi(this);
0017 
0018     KAcceleratorManager::manage(this);
0019 
0020     m_ui->password->setPasswordOptionsEnabled(true);
0021     m_ui->password->setPasswordNotRequiredEnabled(true);
0022 
0023     // Connect for setting check
0024     watchChangedSetting();
0025 
0026     // Connect for validity check
0027     connect(m_ui->service, &KLineEdit::textChanged, this, &PppoeWidget::slotWidgetChanged);
0028     connect(m_ui->username, &KLineEdit::textChanged, this, &PppoeWidget::slotWidgetChanged);
0029     connect(m_ui->password, &PasswordField::textChanged, this, &PppoeWidget::slotWidgetChanged);
0030     connect(m_ui->password, &PasswordField::passwordOptionChanged, this, &PppoeWidget::slotWidgetChanged);
0031 
0032     if (setting && !setting->isNull()) {
0033         loadConfig(setting);
0034     }
0035 }
0036 
0037 PppoeWidget::~PppoeWidget()
0038 {
0039     delete m_ui;
0040 }
0041 
0042 void PppoeWidget::loadConfig(const NetworkManager::Setting::Ptr &setting)
0043 {
0044     NetworkManager::PppoeSetting::Ptr pppoeSetting = setting.staticCast<NetworkManager::PppoeSetting>();
0045 
0046     m_ui->service->setText(pppoeSetting->service());
0047     m_ui->username->setText(pppoeSetting->username());
0048     if (pppoeSetting->passwordFlags().testFlag(NetworkManager::Setting::None)) {
0049         m_ui->password->setPasswordOption(PasswordField::StoreForAllUsers);
0050     } else if (pppoeSetting->passwordFlags().testFlag(NetworkManager::Setting::AgentOwned)) {
0051         m_ui->password->setPasswordOption(PasswordField::StoreForUser);
0052     } else if (pppoeSetting->passwordFlags().testFlag(NetworkManager::Setting::NotSaved)) {
0053         m_ui->password->setPasswordOption(PasswordField::AlwaysAsk);
0054     } else {
0055         m_ui->password->setPasswordOption(PasswordField::NotRequired);
0056     }
0057 
0058     loadSecrets(setting);
0059 }
0060 
0061 void PppoeWidget::loadSecrets(const NetworkManager::Setting::Ptr &setting)
0062 {
0063     NetworkManager::PppoeSetting::Ptr pppoeSetting = setting.staticCast<NetworkManager::PppoeSetting>();
0064 
0065     if (pppoeSetting) {
0066         const QString password = pppoeSetting->password();
0067         if (!password.isEmpty()) {
0068             m_ui->password->setText(password);
0069         }
0070     }
0071 }
0072 
0073 QVariantMap PppoeWidget::setting() const
0074 {
0075     NetworkManager::PppoeSetting pppoeSetting;
0076     if (!m_ui->service->text().isEmpty()) {
0077         pppoeSetting.setService(m_ui->service->text());
0078     }
0079 
0080     if (!m_ui->username->text().isEmpty()) {
0081         pppoeSetting.setUsername(m_ui->username->text());
0082     }
0083 
0084     if (!m_ui->password->text().isEmpty()) {
0085         pppoeSetting.setPassword(m_ui->password->text());
0086     }
0087 
0088     if (m_ui->password->passwordOption() == PasswordField::StoreForAllUsers) {
0089         pppoeSetting.setPasswordFlags(NetworkManager::Setting::None);
0090     } else if (m_ui->password->passwordOption() == PasswordField::StoreForUser) {
0091         pppoeSetting.setPasswordFlags(NetworkManager::Setting::AgentOwned);
0092     } else if (m_ui->password->passwordOption() == PasswordField::AlwaysAsk) {
0093         pppoeSetting.setPasswordFlags(NetworkManager::Setting::NotSaved);
0094     } else {
0095         pppoeSetting.setPasswordFlags(NetworkManager::Setting::NotRequired);
0096     }
0097 
0098     return pppoeSetting.toMap();
0099 }
0100 
0101 bool PppoeWidget::isValid() const
0102 {
0103     bool passwordUserValid = true;
0104 
0105     if (m_ui->password->passwordOption() == PasswordField::StoreForUser //
0106         || m_ui->password->passwordOption() == PasswordField::StoreForAllUsers) {
0107         passwordUserValid = !m_ui->username->text().isEmpty() && !m_ui->password->text().isEmpty();
0108     } else if (m_ui->password->passwordOption() == PasswordField::AlwaysAsk) {
0109         passwordUserValid = !m_ui->username->text().isEmpty();
0110     }
0111 
0112     return passwordUserValid;
0113 }
0114 
0115 #include "moc_pppoewidget.cpp"