File indexing completed on 2024-04-28 08:49:23

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
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 "core/transferhandler.h"
0012 
0013 #include "core/job.h"
0014 #include "core/jobqueue.h"
0015 #include "core/plugin/transferfactory.h"
0016 #include "core/transfergroup.h"
0017 #include "core/transfergrouphandler.h"
0018 #include "core/transfertreemodel.h"
0019 #include "kgetkjobadapter.h"
0020 #include "mainwindow.h"
0021 #include "settings.h"
0022 
0023 #include "kget_debug.h"
0024 #include <KLocalizedString>
0025 #include <QAction>
0026 #include <QDebug>
0027 
0028 TransferHandler::TransferHandler(Transfer *parent, Scheduler *scheduler)
0029     : Handler(scheduler, parent)
0030     , m_transfer(parent)
0031     , m_changesFlags(Transfer::Tc_None)
0032 {
0033     static int dBusObjIdx = 0;
0034     m_dBusObjectPath = "/KGet/Transfers/" + QString::number(dBusObjIdx++);
0035 
0036     m_kjobAdapter = new KGetKJobAdapter(this, this);
0037 
0038     connect(m_transfer, &Transfer::capabilitiesChanged, this, &TransferHandler::capabilitiesChanged);
0039 }
0040 
0041 TransferHandler::~TransferHandler()
0042 {
0043 }
0044 
0045 Transfer::Capabilities TransferHandler::capabilities() const
0046 {
0047     return m_transfer->capabilities();
0048 }
0049 
0050 void TransferHandler::start()
0051 {
0052     if (m_transfer->group()->status() == JobQueue::Running) {
0053         m_transfer->setPolicy(Job::None);
0054         KGet::model()->moveTransfer(m_transfer, m_transfer->group());
0055     } else {
0056         m_transfer->setPolicy(Job::Start);
0057     }
0058 }
0059 
0060 void TransferHandler::stop()
0061 {
0062     if (m_transfer->group()->status() == JobQueue::Stopped) {
0063         m_transfer->setPolicy(Job::None);
0064     } else {
0065         m_transfer->setPolicy(Job::Stop);
0066     }
0067 }
0068 
0069 int TransferHandler::elapsedTime() const
0070 {
0071     return m_transfer->elapsedTime();
0072 }
0073 
0074 int TransferHandler::remainingTime() const
0075 {
0076     return m_transfer->remainingTime();
0077 }
0078 
0079 KIO::filesize_t TransferHandler::totalSize() const
0080 {
0081     return m_transfer->totalSize();
0082 }
0083 
0084 KIO::filesize_t TransferHandler::downloadedSize() const
0085 {
0086     return m_transfer->downloadedSize();
0087 }
0088 
0089 KIO::filesize_t TransferHandler::uploadedSize() const
0090 {
0091     return m_transfer->uploadedSize();
0092 }
0093 
0094 int TransferHandler::percent() const
0095 {
0096     return m_transfer->percent();
0097 }
0098 
0099 int TransferHandler::downloadSpeed() const
0100 {
0101     return m_transfer->downloadSpeed();
0102 }
0103 
0104 int TransferHandler::averageDownloadSped() const
0105 {
0106     return m_transfer->averageDownloadSpeed();
0107 }
0108 
0109 int TransferHandler::uploadSpeed() const
0110 {
0111     return m_transfer->uploadSpeed();
0112 }
0113 
0114 QVariant TransferHandler::data(int column)
0115 {
0116     //     qCDebug(KGET_DEBUG) << "TransferHandler::data(" << column << ")";
0117 
0118     switch (column) {
0119     case 0:
0120         return dest().fileName();
0121     case 1:
0122         if (status() == Job::Aborted && !error().text.isEmpty())
0123             return error().text;
0124         return statusText();
0125     case 2:
0126         if (totalSize() != 0)
0127             return KIO::convertSize(totalSize());
0128         else
0129             return i18nc("not available", "n/a");
0130     case 3:
0131         //             return QString::number(percent())+'%'; // display progressbar instead
0132         return QVariant();
0133     case 4:
0134         if (downloadSpeed() == 0) {
0135             if (m_transfer->isStalled())
0136                 return i18n("Stalled");
0137             else
0138                 return QString();
0139         } else
0140             return i18n("%1/s", KIO::convertSize(downloadSpeed()));
0141     case 5:
0142         if (status() == Job::Running && downloadSpeed() != 0)
0143             return KIO::convertSeconds(remainingTime());
0144         else
0145             return QString();
0146     default:
0147         return QVariant();
0148     }
0149 }
0150 
0151 void TransferHandler::setSelected(bool select)
0152 {
0153     if (select != isSelected()) {
0154         m_transfer->m_isSelected = select;
0155         setTransferChange(Transfer::Tc_Selection, true);
0156     }
0157 }
0158 
0159 bool TransferHandler::isSelected() const
0160 {
0161     return m_transfer->m_isSelected;
0162 }
0163 
0164 Transfer::ChangesFlags TransferHandler::changesFlags() const
0165 {
0166     return m_changesFlags;
0167 }
0168 
0169 void TransferHandler::resetChangesFlags()
0170 {
0171     m_changesFlags = 0;
0172 }
0173 
0174 void TransferHandler::destroy()
0175 {
0176     qCDebug(KGET_DEBUG) << "TransferHandler::destroy() ENTERING";
0177 
0178     qCDebug(KGET_DEBUG) << "TransferHandler::destroy() LEAVING";
0179 }
0180 
0181 void TransferHandler::setTransferChange(ChangesFlags change, bool notifyModel)
0182 {
0183     m_changesFlags |= change;
0184 
0185     if (notifyModel) {
0186         // Notify the TransferTreeModel
0187         m_transfer->model()->postDataChangedEvent(this);
0188 
0189         m_kjobAdapter->slotUpdateDescription();
0190     }
0191 }
0192 
0193 QList<QAction *> TransferHandler::contextActions()
0194 {
0195     QList<QAction *> actions;
0196     if (status() != Job::Finished) {
0197         actions << KGet::actionCollection()->action("start_selected_download") << KGet::actionCollection()->action("stop_selected_download");
0198     }
0199     actions << KGet::actionCollection()->action("delete_selected_download") << KGet::actionCollection()->action("redownload_selected_download")
0200             << KGet::actionCollection()->action("select_all");
0201 
0202     return actions;
0203 }
0204 
0205 QList<QAction *> TransferHandler::factoryActions()
0206 {
0207     QList<QAction *> actions;
0208     foreach (QAction *action, m_transfer->factory()->actions(this))
0209         actions.append(action);
0210     return actions;
0211 }
0212 
0213 #include "moc_transferhandler.cpp"