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

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 "urlwidget.h"
0021 #include "../mirror/mirrormodel.h"
0022 #include "../mirror/mirrorsettings.h"
0023 #include "metalinker.h"
0024 #include <KGuiItem>
0025 #include <KStandardGuiItem>
0026 #include <QSortFilterProxyModel>
0027 
0028 UrlWidget::UrlWidget(QObject *parent)
0029     : QObject(parent)
0030     , m_resources(nullptr)
0031     , m_countrySort(nullptr)
0032     , m_widget(nullptr)
0033 {
0034     m_widget = new QWidget; // TODO inherit from qWidget and set this widget as mainwidget?
0035     ui.setupUi(m_widget);
0036 
0037     m_mirrorModel = new MirrorModel(this);
0038     m_proxy = new MirrorProxyModel(this);
0039     m_proxy->setSourceModel(m_mirrorModel);
0040     ui.used_mirrors->setModel(m_proxy);
0041     ui.used_mirrors->sortByColumn(MirrorItem::Priority, Qt::DescendingOrder);
0042     ui.used_mirrors->resizeColumnToContents(MirrorItem::Priority);
0043     ui.used_mirrors->hideColumn(MirrorItem::Used);
0044     ui.used_mirrors->hideColumn(MirrorItem::Connections);
0045 
0046     KGuiItem::assign(ui.add_mirror, KStandardGuiItem::add());
0047     KGuiItem::assign(ui.remove_mirror, KStandardGuiItem::remove());
0048     ui.remove_mirror->setEnabled(false);
0049     connect(ui.used_mirrors->selectionModel(), &QItemSelectionModel::selectionChanged, this, &UrlWidget::slotUrlClicked);
0050     connect(ui.add_mirror, &QPushButton::clicked, this, &UrlWidget::slotAddMirror);
0051     connect(ui.remove_mirror, &QPushButton::clicked, this, &UrlWidget::slotRemoveMirror);
0052     connect(m_mirrorModel, &MirrorModel::dataChanged, this, &UrlWidget::urlsChanged);
0053     connect(m_mirrorModel, &MirrorModel::rowsInserted, this, &UrlWidget::urlsChanged);
0054     connect(m_mirrorModel, &MirrorModel::rowsRemoved, this, &UrlWidget::urlsChanged);
0055 }
0056 
0057 UrlWidget::~UrlWidget()
0058 {
0059     delete m_widget;
0060 }
0061 
0062 QWidget *UrlWidget::widget()
0063 {
0064     return m_widget;
0065 }
0066 
0067 void UrlWidget::init(KGetMetalink::Resources *resources, QSortFilterProxyModel *countrySort)
0068 {
0069     if (m_resources) {
0070         m_mirrorModel->removeRows(0, m_mirrorModel->rowCount());
0071     }
0072 
0073     m_resources = resources;
0074     m_countrySort = countrySort;
0075 
0076     foreach (const KGetMetalink::Url &url, m_resources->urls) {
0077         m_mirrorModel->addMirror(url.url, 0, url.priority, url.location);
0078     }
0079 
0080     auto *delegate = new MirrorDelegate(m_countrySort, this);
0081     ui.used_mirrors->setItemDelegate(delegate);
0082 }
0083 
0084 bool UrlWidget::hasUrls() const
0085 {
0086     return m_mirrorModel->rowCount();
0087 }
0088 
0089 void UrlWidget::slotUrlClicked()
0090 {
0091     const QModelIndexList selected = ui.used_mirrors->selectionModel()->selectedRows();
0092     ui.remove_mirror->setEnabled(!selected.isEmpty());
0093 }
0094 
0095 void UrlWidget::slotAddMirror()
0096 {
0097     auto *dialog = new MirrorAddDlg(m_mirrorModel, m_countrySort, m_widget);
0098     dialog->showItem(MirrorItem::Connections, false);
0099     dialog->show();
0100 }
0101 
0102 void UrlWidget::slotRemoveMirror()
0103 {
0104     while (ui.used_mirrors->selectionModel()->hasSelection()) {
0105         const QModelIndex index = ui.used_mirrors->selectionModel()->selectedRows().first();
0106         m_mirrorModel->removeRow(m_proxy->mapToSource(index).row());
0107     }
0108 }
0109 
0110 void UrlWidget::save()
0111 {
0112     if (m_resources) {
0113         for (int i = 0; i < m_mirrorModel->rowCount(); ++i) {
0114             KGetMetalink::Url url;
0115             url.url = QUrl(m_mirrorModel->index(i, MirrorItem::Url).data(Qt::UserRole).toUrl());
0116             url.priority = m_mirrorModel->index(i, MirrorItem::Priority).data(Qt::UserRole).toInt();
0117             url.location = m_mirrorModel->index(i, MirrorItem::Country).data(Qt::UserRole).toString();
0118             m_resources->urls.append(url);
0119         }
0120     }
0121 }
0122 
0123 #include "moc_urlwidget.cpp"