File indexing completed on 2024-04-28 04:57:38

0001 /***************************************************************************
0002  *   Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net>                     *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "mirrorsettings.h"
0021 #include "core/transferhandler.h"
0022 
0023 #include <QSortFilterProxyModel>
0024 
0025 #include <KLocalizedString>
0026 #include <KStandardGuiItem>
0027 
0028 MirrorAddDlg::MirrorAddDlg(MirrorModel *model, QWidget *parent, Qt::WindowFlags flags)
0029     : QDialog(parent, flags)
0030     , m_model(model)
0031     , m_countryModel(nullptr)
0032 {
0033     init();
0034 }
0035 
0036 MirrorAddDlg::MirrorAddDlg(MirrorModel *model, QSortFilterProxyModel *countryModel, QWidget *parent, Qt::WindowFlags flags)
0037     : QDialog(parent, flags)
0038     , m_model(model)
0039     , m_countryModel(countryModel)
0040 {
0041     init();
0042 }
0043 
0044 QSize MirrorAddDlg::sizeHint() const
0045 {
0046     QSize sh = QDialog::sizeHint();
0047     sh.setHeight(minimumSize().height());
0048     sh.setWidth(sh.width() * 1.5);
0049     return sh;
0050 }
0051 
0052 void MirrorAddDlg::init()
0053 {
0054     setWindowTitle(i18n("Add mirror"));
0055     ui.setupUi(this);
0056 
0057     if (m_countryModel) {
0058         ui.location->setModel(m_countryModel);
0059         ui.location->setCurrentIndex(-1);
0060     }
0061 
0062     KGuiItem::assign(ui.buttonBox->button(QDialogButtonBox::Yes), KStandardGuiItem::add());
0063 
0064     updateButton();
0065 
0066     connect(ui.url, &QLineEdit::textChanged, this, &MirrorAddDlg::updateButton);
0067     connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &MirrorAddDlg::addMirror);
0068     connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0069 }
0070 
0071 void MirrorAddDlg::showItem(MirrorItem::DataType type, bool show)
0072 {
0073     switch (type) {
0074     case MirrorItem::Connections:
0075         ui.labelConnections->setVisible(show);
0076         ui.numConnections->setVisible(show);
0077         break;
0078 
0079     case MirrorItem::Priority:
0080         ui.labelPriority->setVisible(show);
0081         ui.priority->setVisible(show);
0082         break;
0083 
0084     case MirrorItem::Country:
0085         ui.labelLocation->setVisible(show);
0086         ui.location->setVisible(show);
0087         break;
0088 
0089     default:
0090         break;
0091     }
0092     update();
0093 }
0094 
0095 void MirrorAddDlg::updateButton(const QString &text)
0096 {
0097     bool enabled = false;
0098     QUrl url(text);
0099     if (url.isValid() && !url.scheme().isEmpty() && !url.path().isEmpty()) {
0100         enabled = true;
0101     }
0102     ui.buttonBox->button(QDialogButtonBox::Yes)->setEnabled(enabled);
0103 }
0104 
0105 void MirrorAddDlg::addMirror()
0106 {
0107     const int numConnections = ui.numConnections->isVisible() ? ui.numConnections->value() : 0;
0108     const int priority = ui.priority->isVisible() ? ui.priority->value() : 0;
0109     const QString countryCode = ui.location->itemData(ui.location->currentIndex()).toString();
0110     m_model->addMirror(QUrl(ui.url->text()), numConnections, priority, countryCode);
0111     if (m_countryModel) {
0112         ui.location->setCurrentIndex(-1);
0113     }
0114     this->accept();
0115 }
0116 
0117 MirrorSettings::MirrorSettings(QWidget *parent, TransferHandler *handler, const QUrl &file)
0118     : KGetSaveSizeDialog("MirrorSettings", parent)
0119     , m_transfer(handler)
0120     , m_file(file)
0121 {
0122     m_model = new MirrorModel(this);
0123     m_model->setMirrors(m_transfer->availableMirrors(m_file));
0124     m_proxy = new MirrorProxyModel(this);
0125     m_proxy->setSourceModel(m_model);
0126 
0127     ui.setupUi(this);
0128     KGuiItem::assign(ui.add, KStandardGuiItem::add());
0129     KGuiItem::assign(ui.remove, KStandardGuiItem::remove());
0130     KGuiItem::assign(ui.closeButton, KStandardGuiItem::close());
0131     ui.treeView->setModel(m_proxy);
0132     ui.treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
0133     ui.treeView->hideColumn(MirrorItem::Priority);
0134     ui.treeView->hideColumn(MirrorItem::Country);
0135     ui.treeView->setItemDelegate(new MirrorDelegate(this));
0136 
0137     updateButton();
0138 
0139     connect(ui.treeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MirrorSettings::updateButton);
0140     connect(ui.add, &QPushButton::clicked, this, &MirrorSettings::addClicked);
0141     connect(ui.remove, &QPushButton::clicked, this, &MirrorSettings::removeMirror);
0142     connect(this, &MirrorSettings::finished, this, &MirrorSettings::save);
0143 
0144     setWindowTitle(i18n("Modify the used mirrors"));
0145 
0146     connect(ui.closeButton, &QPushButton::clicked, this, &QDialog::accept);
0147 }
0148 
0149 QSize MirrorSettings::sizeHint() const
0150 {
0151     return QSize(700, 400);
0152 }
0153 
0154 void MirrorSettings::updateButton()
0155 {
0156     ui.remove->setEnabled(ui.treeView->selectionModel()->hasSelection());
0157 }
0158 
0159 void MirrorSettings::addClicked()
0160 {
0161     auto *dialog = new MirrorAddDlg(m_model, this);
0162     dialog->showItem(MirrorItem::Priority, false);
0163     dialog->showItem(MirrorItem::Country, false);
0164     dialog->show();
0165 }
0166 
0167 void MirrorSettings::removeMirror()
0168 {
0169     while (ui.treeView->selectionModel()->hasSelection()) {
0170         const QModelIndex index = ui.treeView->selectionModel()->selectedRows().first();
0171         m_model->removeRow(m_proxy->mapToSource(index).row());
0172     }
0173 }
0174 
0175 void MirrorSettings::save()
0176 {
0177     m_transfer->setAvailableMirrors(m_file, m_model->availableMirrors());
0178 }
0179 
0180 #include "moc_mirrorsettings.cpp"