File indexing completed on 2024-04-21 04:57:13

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2007 - 2010 Lukas Appelhans <l.appelhans@gmx.de>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 */
0010 
0011 #include "bttransferfactory.h"
0012 
0013 // header inclusion order is crucial because of signal Q_EMIT clashes
0014 #include "advanceddetails/btadvanceddetailswidget.h"
0015 #include "btdatasource.h"
0016 #include "btdetailswidget.h"
0017 #include "bttransfer.h"
0018 #include "bttransferhandler.h"
0019 
0020 #include "kget_debug.h"
0021 #include <torrent/job.h>
0022 #include <util/functions.h>
0023 #include <version.h>
0024 
0025 K_PLUGIN_CLASS_WITH_JSON(BTTransferFactory, "kget_bittorrentfactory.json")
0026 
0027 BTTransferFactory::BTTransferFactory(QObject *parent, const QVariantList &args)
0028     : TransferFactory(parent, args)
0029 {
0030     if (!bt::InitLibKTorrent()) {
0031         qCCritical(KGET_DEBUG) << "Failed to initialize libktorrent";
0032         KGet::showNotification(nullptr, "error", i18n("Cannot initialize libktorrent. Torrent support might not work."));
0033     }
0034 }
0035 
0036 BTTransferFactory::~BTTransferFactory()
0037 {
0038 }
0039 
0040 Transfer *BTTransferFactory::createTransfer(const QUrl &srcUrl, const QUrl &destUrl, TransferGroup *parent, Scheduler *scheduler, const QDomElement *e)
0041 {
0042     qCDebug(KGET_DEBUG) << "BTTransferFactory::createTransfer";
0043 
0044     if (isSupported(srcUrl)) {
0045         return new BTTransfer(parent, this, scheduler, srcUrl, destUrl, e);
0046     }
0047     return nullptr;
0048 }
0049 
0050 TransferHandler *BTTransferFactory::createTransferHandler(Transfer *transfer, Scheduler *scheduler)
0051 {
0052     auto *bttransfer = qobject_cast<BTTransfer *>(transfer);
0053 
0054     if (!bttransfer) {
0055         qCCritical(KGET_DEBUG) << "WARNING! passing a non-BTTransfer pointer!!";
0056         return nullptr;
0057     }
0058 
0059     return new BTTransferHandler(bttransfer, scheduler);
0060 }
0061 
0062 QWidget *BTTransferFactory::createDetailsWidget(TransferHandler *transfer)
0063 {
0064     auto *bttransfer = static_cast<BTTransferHandler *>(transfer);
0065 
0066     return new BTDetailsWidget(bttransfer);
0067 }
0068 
0069 const QList<QAction *> BTTransferFactory::actions(TransferHandler *handler)
0070 {
0071     auto *bttransfer = static_cast<BTTransferHandler *>(handler);
0072 
0073     QList<QAction *> actions;
0074     if (bttransfer && bttransfer->torrentControl()) {
0075         auto *openAdvancedDetailsAction = new QAction(QIcon::fromTheme("document-open"), i18n("&Advanced Details"), this);
0076 
0077         connect(openAdvancedDetailsAction, &QAction::triggered, bttransfer, &BTTransferHandler::createAdvancedDetails);
0078 
0079         actions.append(openAdvancedDetailsAction);
0080 
0081         auto *openScanDlg = new QAction(QIcon::fromTheme("document-open"), i18n("&Scan Files"), this);
0082 
0083         connect(openScanDlg, &QAction::triggered, bttransfer, &BTTransferHandler::createScanDlg);
0084 
0085         actions.append(openScanDlg);
0086     }
0087 
0088     if (bttransfer)
0089         return actions;
0090     else
0091         return QList<QAction *>();
0092 }
0093 
0094 TransferDataSource *BTTransferFactory::createTransferDataSource(const QUrl &srcUrl, const QDomElement &type, QObject *parent)
0095 {
0096     Q_UNUSED(srcUrl)
0097     Q_UNUSED(type)
0098     Q_UNUSED(parent)
0099     /*if (srcUrl.fileName().endsWith(".torrent"))
0100         return new BTDataSource();*/
0101     return nullptr;
0102 }
0103 
0104 bool BTTransferFactory::isSupported(const QUrl &url) const
0105 {
0106     return url.url().endsWith(QLatin1String(".torrent"));
0107 }
0108 
0109 #include "bttransferfactory.moc"
0110 #include "moc_bttransferfactory.cpp"