File indexing completed on 2024-05-05 04:59:14

0001 /***************************************************************************
0002  *   Copyright (C) 2008 by Joris Guisson and Ivan Vasic                    *
0003  *   joris.guisson@gmail.com                                               *
0004  *   ivasic@gmail.com                                                      *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, write to the                         *
0018  *   Free Software Foundation, Inc.,                                       *
0019  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0020  ***************************************************************************/
0021 #include "webseedstab.h"
0022 
0023 #include <QHeaderView>
0024 
0025 #include <KConfigGroup>
0026 #include <KMessageBox>
0027 
0028 #include "webseedsmodel.h"
0029 #include <interfaces/torrentinterface.h>
0030 #include <interfaces/webseedinterface.h>
0031 
0032 using namespace bt;
0033 
0034 namespace kt
0035 {
0036 
0037 WebSeedsTab::WebSeedsTab(QWidget *parent)
0038     : QWidget(parent)
0039     , curr_tc(nullptr)
0040 {
0041     setupUi(this);
0042     connect(m_add, &QAbstractButton::clicked, this, &WebSeedsTab::addWebSeed);
0043     connect(m_remove, &QAbstractButton::clicked, this, &WebSeedsTab::removeWebSeed);
0044     m_add->setIcon(QIcon::fromTheme("list-add"));
0045     m_remove->setIcon(QIcon::fromTheme("list-remove"));
0046     m_add->setEnabled(false);
0047     m_remove->setEnabled(false);
0048     m_webseed_list->setEnabled(false);
0049     model = new WebSeedsModel(this);
0050     proxy_model = new QSortFilterProxyModel(this);
0051     proxy_model->setSourceModel(model);
0052     proxy_model->setSortRole(Qt::UserRole);
0053     m_webseed_list->setModel(proxy_model);
0054     m_webseed_list->setSortingEnabled(true);
0055 
0056     connect(m_webseed_list->selectionModel(),
0057             SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
0058             this,
0059             SLOT(selectionChanged(QItemSelection, QItemSelection)));
0060 
0061     connect(m_webseed, &QLineEdit::textChanged, this, &WebSeedsTab::onWebSeedTextChanged);
0062 }
0063 
0064 WebSeedsTab::~WebSeedsTab()
0065 {
0066 }
0067 
0068 void WebSeedsTab::changeTC(bt::TorrentInterface *tc)
0069 {
0070     curr_tc = tc;
0071     model->changeTC(tc);
0072     m_add->setEnabled(curr_tc != nullptr);
0073     m_remove->setEnabled(curr_tc != nullptr);
0074     m_webseed_list->setEnabled(curr_tc != nullptr);
0075     m_webseed->setEnabled(curr_tc != nullptr);
0076     onWebSeedTextChanged(m_webseed->text());
0077 
0078     // see if we need to enable or disable the remove button
0079     if (curr_tc)
0080         selectionChanged(m_webseed_list->selectionModel()->selectedRows());
0081 }
0082 
0083 void WebSeedsTab::addWebSeed()
0084 {
0085     if (!curr_tc)
0086         return;
0087 
0088     QUrl url(m_webseed->text());
0089     if (curr_tc != nullptr && url.isValid() && url.scheme() == "http") {
0090         if (curr_tc->addWebSeed(url)) {
0091             model->changeTC(curr_tc);
0092             m_webseed->clear();
0093         } else {
0094             KMessageBox::error(this, i18n("Cannot add the webseed %1, it is already part of the list of webseeds.", url.toDisplayString()));
0095         }
0096     }
0097 }
0098 
0099 void WebSeedsTab::removeWebSeed()
0100 {
0101     if (!curr_tc)
0102         return;
0103 
0104     QModelIndexList idx_list = m_webseed_list->selectionModel()->selectedRows();
0105     foreach (const QModelIndex &idx, idx_list) {
0106         const WebSeedInterface *ws = curr_tc->getWebSeed(proxy_model->mapToSource(idx).row());
0107         if (ws && ws->isUserCreated()) {
0108             if (!curr_tc->removeWebSeed(ws->getUrl()))
0109                 KMessageBox::error(this, i18n("Cannot remove webseed %1, it is part of the torrent.", ws->getUrl().toDisplayString()));
0110         }
0111     }
0112 
0113     model->changeTC(curr_tc);
0114 }
0115 
0116 void WebSeedsTab::selectionChanged(const QModelIndexList &indexes)
0117 {
0118     foreach (const QModelIndex &idx, indexes) {
0119         const WebSeedInterface *ws = curr_tc->getWebSeed(proxy_model->mapToSource(idx).row());
0120         if (ws && ws->isUserCreated()) {
0121             m_remove->setEnabled(true);
0122             return;
0123         }
0124     }
0125 
0126     m_remove->setEnabled(false);
0127 }
0128 
0129 void WebSeedsTab::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
0130 {
0131     Q_UNUSED(deselected)
0132     if (!curr_tc)
0133         return;
0134 
0135     selectionChanged(selected.indexes());
0136 }
0137 
0138 void WebSeedsTab::onWebSeedTextChanged(const QString &ws)
0139 {
0140     QUrl url(ws);
0141     m_add->setEnabled(curr_tc != nullptr && url.isValid() && url.scheme() == "http");
0142 }
0143 
0144 void WebSeedsTab::update()
0145 {
0146     if (model->update())
0147         proxy_model->invalidate();
0148 }
0149 
0150 void WebSeedsTab::saveState(KSharedConfigPtr cfg)
0151 {
0152     KConfigGroup g = cfg->group("WebSeedsTab");
0153     QByteArray s = m_webseed_list->header()->saveState();
0154     g.writeEntry("state", s.toBase64());
0155 }
0156 
0157 void WebSeedsTab::loadState(KSharedConfigPtr cfg)
0158 {
0159     KConfigGroup g = cfg->group("WebSeedsTab");
0160     QByteArray s = QByteArray::fromBase64(g.readEntry("state", QByteArray()));
0161     if (!s.isNull())
0162         m_webseed_list->header()->restoreState(s);
0163 }
0164 }
0165 
0166 #include "moc_webseedstab.cpp"