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

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