1 /* ============================================================
2 * StatusBarIcons - Extra icons in statusbar for Falkon
3 * Copyright (C) 2013-2016 David Rosca <nowrep@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #include "sbi_networkicondialog.h"
19 #include "sbi_networkmanager.h"
20 #include "sbi_networkproxy.h"
21 #include "ui_sbi_networkicondialog.h"
22
23 #include <QInputDialog>
24 #include <QMessageBox>
25
SBI_NetworkIconDialog(QWidget * parent)26 SBI_NetworkIconDialog::SBI_NetworkIconDialog(QWidget* parent)
27 : QDialog(parent)
28 , ui(new Ui::SBI_NetworkIconDialog)
29 {
30 setAttribute(Qt::WA_DeleteOnClose);
31
32 ui->setupUi(this);
33
34 ui->addButton->setIcon(QIcon::fromTheme(QLatin1String("document-new"), QIcon(QLatin1String(":sbi/data/add.png"))));
35 ui->removeButton->setIcon(QIcon::fromTheme(QLatin1String("edit-delete"), QIcon(QLatin1String(":sbi/data/remove.png"))));
36
37 const QHash<QString, SBI_NetworkProxy*> &proxies = SBINetManager->proxies();
38
39 QHashIterator<QString, SBI_NetworkProxy*> it(proxies);
40 while (it.hasNext()) {
41 it.next();
42 ui->comboBox->addItem(it.key());
43 }
44
45 updateWidgets();
46 showProxy(ui->comboBox->currentText());
47
48 connect(ui->addButton, &QAbstractButton::clicked, this, &SBI_NetworkIconDialog::addProxy);
49 connect(ui->removeButton, &QAbstractButton::clicked, this, &SBI_NetworkIconDialog::removeProxy);
50 connect(ui->comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(showProxy(QString)));
51 connect(ui->proxyButtonBox, &QDialogButtonBox::accepted, this, &SBI_NetworkIconDialog::saveProxy);
52 connect(ui->closeButton, &QDialogButtonBox::clicked, this, &QWidget::close);
53 }
54
addProxy()55 void SBI_NetworkIconDialog::addProxy()
56 {
57 const QString name = QInputDialog::getText(this, tr("Add proxy"), tr("Name of proxy:"));
58 if (name.isEmpty() || ui->comboBox->findText(name) > -1) {
59 return;
60 }
61
62 ui->comboBox->addItem(name);
63 ui->comboBox->setCurrentIndex(ui->comboBox->count() - 1);
64
65 updateWidgets();
66 }
67
removeProxy()68 void SBI_NetworkIconDialog::removeProxy()
69 {
70 QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Remove current proxy"), tr("Are you sure you want to remove current proxy?"),
71 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
72
73 if (button != QMessageBox::Yes) {
74 return;
75 }
76
77 int index = ui->comboBox->currentIndex();
78 if (index < 0) {
79 return;
80 }
81
82 SBINetManager->removeProxy(ui->comboBox->currentText());
83 ui->comboBox->removeItem(index);
84
85 updateWidgets();
86 }
87
saveProxy()88 void SBI_NetworkIconDialog::saveProxy()
89 {
90 SBINetManager->saveProxy(ui->comboBox->currentText(), ui->proxyWidget->getProxy());
91 }
92
showProxy(const QString & name)93 void SBI_NetworkIconDialog::showProxy(const QString &name)
94 {
95 SBI_NetworkProxy* proxy = SBINetManager->proxies()[name];
96
97 ui->proxyWidget->clear();
98
99 if (proxy) {
100 ui->proxyWidget->setProxy(*proxy);
101 }
102 }
103
updateWidgets()104 void SBI_NetworkIconDialog::updateWidgets()
105 {
106 ui->removeButton->setEnabled(ui->comboBox->count() > 0);
107 ui->noProxiesLabel->setVisible(ui->comboBox->count() == 0);
108 ui->proxyWidget->setVisible(ui->comboBox->count() > 0);
109 }
110
~SBI_NetworkIconDialog()111 SBI_NetworkIconDialog::~SBI_NetworkIconDialog()
112 {
113 delete ui;
114 }
115