File indexing completed on 2024-12-01 13:40:25

0001 /*
0002     SPDX-FileCopyrightText: 2013, 2014 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 "gsmwidget.h"
0008 #include "ui_gsm.h"
0009 
0010 #include <KLocalizedString>
0011 
0012 #include <NetworkManagerQt/GsmSetting>
0013 
0014 GsmWidget::GsmWidget(const NetworkManager::Setting::Ptr &setting, QWidget *parent, Qt::WindowFlags f)
0015     : SettingWidget(setting, parent, f)
0016     , m_ui(new Ui::GsmWidget)
0017 {
0018     m_ui->setupUi(this);
0019 
0020     // Network ID not supported yet in NM
0021     m_ui->labelNetworkId->setHidden(true);
0022     m_ui->networkId->setHidden(true);
0023 
0024     m_ui->password->setPasswordOptionsEnabled(true);
0025     m_ui->password->setPasswordNotRequiredEnabled(true);
0026     m_ui->pin->setPasswordOptionsEnabled(true);
0027     m_ui->pin->setPasswordNotRequiredEnabled(true);
0028 
0029     // Connect for setting check
0030     watchChangedSetting();
0031 
0032     // Connect for validity check
0033     connect(m_ui->apn, &KLineEdit::textChanged, this, &GsmWidget::slotWidgetChanged);
0034     connect(m_ui->password, &PasswordField::textChanged, this, &GsmWidget::slotWidgetChanged);
0035     connect(m_ui->password, &PasswordField::passwordOptionChanged, this, &GsmWidget::slotWidgetChanged);
0036     connect(m_ui->pin, &PasswordField::textChanged, this, &GsmWidget::slotWidgetChanged);
0037     connect(m_ui->pin, &PasswordField::passwordOptionChanged, this, &GsmWidget::slotWidgetChanged);
0038     connect(m_ui->username, &KLineEdit::textChanged, this, &GsmWidget::slotWidgetChanged);
0039 
0040     KAcceleratorManager::manage(this);
0041 
0042     if (setting) {
0043         loadConfig(setting);
0044     }
0045 }
0046 
0047 GsmWidget::~GsmWidget()
0048 {
0049     delete m_ui;
0050 }
0051 
0052 void GsmWidget::loadConfig(const NetworkManager::Setting::Ptr &setting)
0053 {
0054     NetworkManager::GsmSetting::Ptr gsmSetting = setting.staticCast<NetworkManager::GsmSetting>();
0055 
0056     const QString number = gsmSetting->number();
0057     if (!number.isEmpty())
0058         m_ui->number->setText(number);
0059     m_ui->username->setText(gsmSetting->username());
0060     if (gsmSetting->passwordFlags().testFlag(NetworkManager::Setting::None)) {
0061         m_ui->password->setPasswordOption(PasswordField::StoreForAllUsers);
0062     } else if (gsmSetting->passwordFlags().testFlag(NetworkManager::Setting::AgentOwned)) {
0063         m_ui->password->setPasswordOption(PasswordField::StoreForUser);
0064     } else if (gsmSetting->passwordFlags().testFlag(NetworkManager::Setting::NotSaved)) {
0065         m_ui->password->setPasswordOption(PasswordField::AlwaysAsk);
0066     } else {
0067         m_ui->password->setPasswordOption(PasswordField::NotRequired);
0068     }
0069 
0070     m_ui->apn->setText(gsmSetting->apn());
0071     m_ui->networkId->setText(gsmSetting->networkId());
0072     m_ui->roaming->setChecked(!gsmSetting->homeOnly());
0073     if (gsmSetting->pinFlags().testFlag(NetworkManager::Setting::None)) {
0074         m_ui->pin->setPasswordOption(PasswordField::StoreForAllUsers);
0075     } else if (gsmSetting->pinFlags().testFlag(NetworkManager::Setting::AgentOwned)) {
0076         m_ui->pin->setPasswordOption(PasswordField::StoreForUser);
0077     } else if (gsmSetting->pinFlags().testFlag(NetworkManager::Setting::NotSaved)) {
0078         m_ui->pin->setPasswordOption(PasswordField::AlwaysAsk);
0079     } else {
0080         m_ui->pin->setPasswordOption(PasswordField::NotRequired);
0081     }
0082 
0083     loadSecrets(setting);
0084 }
0085 
0086 void GsmWidget::loadSecrets(const NetworkManager::Setting::Ptr &setting)
0087 {
0088     NetworkManager::GsmSetting::Ptr gsmSetting = setting.staticCast<NetworkManager::GsmSetting>();
0089 
0090     if (gsmSetting) {
0091         const QString password = gsmSetting->password();
0092         if (!password.isEmpty()) {
0093             m_ui->password->setText(password);
0094         }
0095 
0096         const QString pin = gsmSetting->pin();
0097         if (!pin.isEmpty()) {
0098             m_ui->pin->setText(pin);
0099         }
0100     }
0101 }
0102 
0103 QVariantMap GsmWidget::setting() const
0104 {
0105     NetworkManager::GsmSetting gsmSetting;
0106     if (!m_ui->number->text().isEmpty())
0107         gsmSetting.setNumber(m_ui->number->text());
0108     if (!m_ui->username->text().isEmpty())
0109         gsmSetting.setUsername(m_ui->username->text());
0110     if (!m_ui->password->text().isEmpty())
0111         gsmSetting.setPassword(m_ui->password->text());
0112     if (m_ui->password->passwordOption() == PasswordField::StoreForAllUsers) {
0113         gsmSetting.setPasswordFlags(NetworkManager::Setting::None);
0114     } else if (m_ui->password->passwordOption() == PasswordField::StoreForUser) {
0115         gsmSetting.setPasswordFlags(NetworkManager::Setting::AgentOwned);
0116     } else if (m_ui->password->passwordOption() == PasswordField::AlwaysAsk) {
0117         gsmSetting.setPasswordFlags(NetworkManager::Setting::NotSaved);
0118     } else {
0119         gsmSetting.setPasswordFlags(NetworkManager::Setting::NotRequired);
0120     }
0121 
0122     if (!m_ui->apn->text().isEmpty())
0123         gsmSetting.setApn(m_ui->apn->text());
0124     if (!m_ui->networkId->text().isEmpty())
0125         gsmSetting.setNetworkId(m_ui->networkId->text());
0126     gsmSetting.setHomeOnly(!m_ui->roaming->isChecked());
0127     if (!m_ui->pin->text().isEmpty())
0128         gsmSetting.setPin(m_ui->pin->text());
0129     if (m_ui->pin->passwordOption() == PasswordField::StoreForAllUsers) {
0130         gsmSetting.setPinFlags(NetworkManager::Setting::None);
0131     } else if (m_ui->pin->passwordOption() == PasswordField::StoreForUser) {
0132         gsmSetting.setPinFlags(NetworkManager::Setting::AgentOwned);
0133     } else if (m_ui->pin->passwordOption() == PasswordField::AlwaysAsk) {
0134         gsmSetting.setPinFlags(NetworkManager::Setting::NotSaved);
0135     } else {
0136         gsmSetting.setPinFlags(NetworkManager::Setting::NotRequired);
0137     }
0138 
0139     return gsmSetting.toMap();
0140 }
0141 
0142 bool GsmWidget::isValid() const
0143 {
0144     bool passwordUserValid = true;
0145     bool pinValid = true;
0146 
0147     if (m_ui->password->passwordOption() == PasswordField::StoreForUser //
0148         || m_ui->password->passwordOption() == PasswordField::StoreForAllUsers) {
0149         passwordUserValid = !m_ui->username->text().isEmpty() && !m_ui->password->text().isEmpty();
0150     } else if (m_ui->password->passwordOption() == PasswordField::AlwaysAsk) {
0151         passwordUserValid = !m_ui->username->text().isEmpty();
0152     }
0153 
0154     if (m_ui->pin->passwordOption() == PasswordField::StoreForUser || m_ui->pin->passwordOption() == PasswordField::StoreForAllUsers) {
0155         pinValid = !m_ui->pin->text().isEmpty();
0156     }
0157 
0158     return !m_ui->apn->text().isEmpty() && passwordUserValid && pinValid;
0159 }