File indexing completed on 2024-05-12 04:58:12

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2019 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 "protocolhandlerdialog.h"
0019 #include "ui_protocolhandlerdialog.h"
0020 #include "mainapplication.h"
0021 #include "protocolhandlermanager.h"
0022 
0023 ProtocolHandlerDialog::ProtocolHandlerDialog(QWidget* parent)
0024     : QDialog(parent)
0025     , ui(new Ui::ProtocolHandlerDialog)
0026 {
0027     setAttribute(Qt::WA_DeleteOnClose);
0028 
0029     ui->setupUi(this);
0030 
0031     init();
0032 
0033     ui->treeWidget->header()->resizeSection(0, 100);
0034     connect(ui->remove, &QPushButton::clicked, this, &ProtocolHandlerDialog::removeEntry);
0035     connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &ProtocolHandlerDialog::accepted);
0036     connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &ProtocolHandlerDialog::close);
0037 }
0038 
0039 ProtocolHandlerDialog::~ProtocolHandlerDialog()
0040 {
0041     delete ui;
0042 }
0043 
0044 void ProtocolHandlerDialog::init()
0045 {
0046     const auto handlers = mApp->protocolHandlerManager()->protocolHandlers();
0047     for (auto it = handlers.cbegin(); it != handlers.cend(); ++it) {
0048         auto *item = new QTreeWidgetItem(ui->treeWidget);
0049         item->setText(0, it.key());
0050         item->setText(1, it.value().host());
0051         ui->treeWidget->addTopLevelItem(item);
0052     }
0053 }
0054 
0055 void ProtocolHandlerDialog::accepted()
0056 {
0057     auto handlers = mApp->protocolHandlerManager()->protocolHandlers();
0058     for (int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i) {
0059         QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
0060         handlers.remove(item->text(0));
0061     }
0062     for (auto it = handlers.cbegin(); it != handlers.cend(); ++it) {
0063         mApp->protocolHandlerManager()->removeProtocolHandler(it.key());
0064     }
0065     close();
0066 }
0067 
0068 void ProtocolHandlerDialog::removeEntry()
0069 {
0070     delete ui->treeWidget->currentItem();
0071 }
0072