File indexing completed on 2024-05-19 04:59:18

0001 /* ============================================================
0002 * StatusBarIcons - Extra icons in statusbar for Falkon
0003 * Copyright (C) 2013-2016  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "sbi_networkicondialog.h"
0019 #include "sbi_networkmanager.h"
0020 #include "sbi_networkproxy.h"
0021 #include "ui_sbi_networkicondialog.h"
0022 
0023 #include <QInputDialog>
0024 #include <QMessageBox>
0025 
0026 SBI_NetworkIconDialog::SBI_NetworkIconDialog(QWidget* parent)
0027     : QDialog(parent)
0028     , ui(new Ui::SBI_NetworkIconDialog)
0029 {
0030     setAttribute(Qt::WA_DeleteOnClose);
0031 
0032     ui->setupUi(this);
0033 
0034     ui->addButton->setIcon(QIcon::fromTheme(QLatin1String("document-new"), QIcon(QLatin1String(":sbi/data/add.png"))));
0035     ui->removeButton->setIcon(QIcon::fromTheme(QLatin1String("edit-delete"), QIcon(QLatin1String(":sbi/data/remove.png"))));
0036 
0037     const QHash<QString, SBI_NetworkProxy*> &proxies = SBINetManager->proxies();
0038 
0039     QHashIterator<QString, SBI_NetworkProxy*> it(proxies);
0040     while (it.hasNext()) {
0041         it.next();
0042         ui->comboBox->addItem(it.key());
0043     }
0044 
0045     updateWidgets();
0046     showProxy(ui->comboBox->currentText());
0047 
0048     connect(ui->addButton, &QAbstractButton::clicked, this, &SBI_NetworkIconDialog::addProxy);
0049     connect(ui->removeButton, &QAbstractButton::clicked, this, &SBI_NetworkIconDialog::removeProxy);
0050     connect(ui->comboBox, &QComboBox::currentTextChanged, this, &SBI_NetworkIconDialog::showProxy);
0051     connect(ui->proxyButtonBox, &QDialogButtonBox::accepted, this, &SBI_NetworkIconDialog::saveProxy);
0052     connect(ui->closeButton, &QDialogButtonBox::clicked, this, &QWidget::close);
0053 }
0054 
0055 void SBI_NetworkIconDialog::addProxy()
0056 {
0057     const QString name = QInputDialog::getText(this, tr("Add proxy"), tr("Name of proxy:"));
0058     if (name.isEmpty() || ui->comboBox->findText(name) > -1) {
0059         return;
0060     }
0061 
0062     ui->comboBox->addItem(name);
0063     ui->comboBox->setCurrentIndex(ui->comboBox->count() - 1);
0064 
0065     updateWidgets();
0066 }
0067 
0068 void SBI_NetworkIconDialog::removeProxy()
0069 {
0070     QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Remove current proxy"), tr("Are you sure you want to remove current proxy?"),
0071                                          QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
0072 
0073     if (button != QMessageBox::Yes) {
0074         return;
0075     }
0076 
0077     int index = ui->comboBox->currentIndex();
0078     if (index < 0) {
0079         return;
0080     }
0081 
0082     SBINetManager->removeProxy(ui->comboBox->currentText());
0083     ui->comboBox->removeItem(index);
0084 
0085     updateWidgets();
0086 }
0087 
0088 void SBI_NetworkIconDialog::saveProxy()
0089 {
0090     SBINetManager->saveProxy(ui->comboBox->currentText(), ui->proxyWidget->getProxy());
0091 }
0092 
0093 void SBI_NetworkIconDialog::showProxy(const QString &name)
0094 {
0095     SBI_NetworkProxy* proxy = SBINetManager->proxies()[name];
0096 
0097     ui->proxyWidget->clear();
0098 
0099     if (proxy) {
0100         ui->proxyWidget->setProxy(*proxy);
0101     }
0102 }
0103 
0104 void SBI_NetworkIconDialog::updateWidgets()
0105 {
0106     ui->removeButton->setEnabled(ui->comboBox->count() > 0);
0107     ui->noProxiesLabel->setVisible(ui->comboBox->count() == 0);
0108     ui->proxyWidget->setVisible(ui->comboBox->count() > 0);
0109 }
0110 
0111 SBI_NetworkIconDialog::~SBI_NetworkIconDialog()
0112 {
0113     delete ui;
0114 }