File indexing completed on 2024-05-12 08:59:44

0001 /**************************************************************************
0002  *   Copyright (C) 2006 - 2008 Urs Wolfer <uwolfer @ kde.org>              *
0003  *   Copyright (C) 2006 Dario Massarin <nekkar@libero.it>                  *
0004  *   Copyright (C) 2008 - 2009 Lukas Appelhans <l.appelhans@gmx.de>        *
0005  *   Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net>                     *
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or modify  *
0008  *   it under the terms of the GNU General Public License as published by  *
0009  *   the Free Software Foundation; either version 2 of the License, or     *
0010  *   (at your option) any later version.                                   *
0011  *                                                                         *
0012  *   This program is distributed in the hope that it will be useful,       *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0015  *   GNU General Public License for more details.                          *
0016  *                                                                         *
0017  *   You should have received a copy of the GNU General Public License     *
0018  *   along with this program; if not, write to the                         *
0019  *   Free Software Foundation, Inc.,                                       *
0020  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0021  ***************************************************************************/
0022 
0023 #include "dbuskgetwrapper.h"
0024 
0025 #include "core/kget.h"
0026 #include "core/plugin/transferfactory.h"
0027 #include "core/transferhandler.h"
0028 #include "core/transfertreemodel.h"
0029 #include "mainwindow.h"
0030 #include "settings.h"
0031 #include "ui/droptarget.h"
0032 #include "ui/linkview/kget_linkview.h"
0033 #include "ui/newtransferdialog.h"
0034 
0035 #include "kget_debug.h"
0036 
0037 DBusKGetWrapper::DBusKGetWrapper(MainWindow *parent)
0038     : QObject(parent)
0039     , m_mainWindow(parent)
0040 {
0041     foreach (TransferHandler *handler, KGet::allTransfers()) {
0042         m_transfers[handler] = qMakePair(handler->source().toString(), handler->dBusObjectPath());
0043     }
0044 
0045     TransferTreeModel *model = KGet::model();
0046 
0047     connect(model, SIGNAL(transfersAddedEvent(QList<TransferHandler *>)), this, SLOT(slotTransfersAdded(QList<TransferHandler *>)));
0048     connect(model, &TransferTreeModel::transfersRemovedEvent, this, &DBusKGetWrapper::slotTransfersRemoved);
0049 }
0050 
0051 DBusKGetWrapper::~DBusKGetWrapper()
0052 {
0053 }
0054 
0055 QStringList DBusKGetWrapper::addTransfer(const QString &src, const QString &dest, bool start)
0056 {
0057     QStringList dBusPaths;
0058 
0059     QList<QUrl> urls;
0060     const QStringList srcSplit = src.split(";");
0061     for (const QString &s : srcSplit)
0062         urls.append(QUrl(s));
0063     // split src for the case it is a QStringList (e.g. from konqueror plugin)
0064     QList<TransferHandler *> addedTransfers = KGet::addTransfer(urls, dest, QString(), start);
0065 
0066     foreach (TransferHandler *handler, addedTransfers) {
0067         dBusPaths.append(handler->dBusObjectPath());
0068     }
0069 
0070     return dBusPaths;
0071 }
0072 
0073 bool DBusKGetWrapper::delTransfer(const QString &dbusObjectPath)
0074 {
0075     qCDebug(KGET_DEBUG) << "deleting Transfer";
0076 
0077     Transfer *transfer = KGet::model()->findTransferByDBusObjectPath(dbusObjectPath);
0078 
0079     if (transfer) {
0080         return KGet::delTransfer(transfer->handler());
0081     }
0082 
0083     return false;
0084 }
0085 
0086 void DBusKGetWrapper::showNewTransferDialog(const QStringList &urls)
0087 {
0088     QList<QUrl> qurls;
0089     for (const QString &s : urls)
0090         qurls.append(QUrl(s));
0091     NewTransferDialogHandler::showNewTransferDialog(qurls);
0092 }
0093 
0094 bool DBusKGetWrapper::dropTargetVisible() const
0095 {
0096     return m_mainWindow->m_drop->isVisible();
0097 }
0098 
0099 void DBusKGetWrapper::setDropTargetVisible(bool setVisible)
0100 {
0101     if (setVisible != Settings::showDropTarget()) {
0102         m_mainWindow->m_drop->setDropTargetVisible(setVisible);
0103     }
0104 }
0105 
0106 void DBusKGetWrapper::setOfflineMode(bool offline)
0107 {
0108     KGet::setSchedulerRunning(offline);
0109 }
0110 
0111 bool DBusKGetWrapper::offlineMode() const
0112 {
0113     return !KGet::schedulerRunning();
0114 }
0115 
0116 QVariantMap DBusKGetWrapper::transfers() const
0117 {
0118     const QList<QPair<QString, QString>> transfers = m_transfers.values();
0119     QVariantMap result;
0120     for (int i = 0; i < transfers.count(); ++i) {
0121         result.insert(transfers[i].first, transfers[i].second);
0122     }
0123 
0124     return result;
0125 }
0126 
0127 void DBusKGetWrapper::slotTransfersAdded(const QList<TransferHandler *> &transfers)
0128 {
0129     QStringList urls;
0130     QStringList objectPaths;
0131     foreach (TransferHandler *transfer, transfers) {
0132         const QString url = transfer->source().toString();
0133         const QString objectPath = transfer->dBusObjectPath();
0134         urls << url;
0135         objectPaths << objectPath;
0136         m_transfers[transfer] = qMakePair(url, objectPath);
0137     }
0138 
0139     Q_EMIT transfersAdded(urls, objectPaths);
0140 }
0141 
0142 void DBusKGetWrapper::slotTransfersRemoved(const QList<TransferHandler *> &transfers)
0143 {
0144     QStringList urls;
0145     QStringList objectPaths;
0146     foreach (TransferHandler *transfer, transfers) {
0147         const QPair<QString, QString> removed = m_transfers[transfer];
0148         urls << removed.first;
0149         objectPaths << removed.second;
0150     }
0151 
0152     Q_EMIT transfersRemoved(urls, objectPaths);
0153 }
0154 
0155 int DBusKGetWrapper::transfersSpeed() const
0156 {
0157     return 0; // FIXME
0158     // return m_dbusModelObserver->transfersSpeed();
0159 }
0160 
0161 void DBusKGetWrapper::importLinks(const QList<QString> &links)
0162 {
0163     auto *link_view = new KGetLinkView(m_mainWindow);
0164     link_view->setLinks(links);
0165     link_view->show();
0166 }
0167 
0168 bool DBusKGetWrapper::isSupported(const QString &url) const
0169 {
0170     foreach (TransferFactory *factory, KGet::factories()) {
0171         qDebug() << "Check" << factory->objectName() << "for" << url << "it is?" << factory->isSupported(QUrl(url));
0172         if (factory->isSupported(QUrl(url)))
0173             return true;
0174     }
0175     return false;
0176 }
0177 
0178 #include "moc_dbuskgetwrapper.cpp"