File indexing completed on 2024-02-18 04:58:18

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
0004    Copyright (C) 2008 Lukas Appelhans <l.appelhans@gmx.de>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 */
0011 
0012 #include "core/transfergrouphandler.h"
0013 
0014 #include "core/kget.h"
0015 #include "core/kgetkjobadapter.h"
0016 #include "core/transfer.h"
0017 #include "core/transferhandler.h"
0018 #include "core/transfertreemodel.h"
0019 
0020 #include "kget_debug.h"
0021 #include <KLocalizedString>
0022 #include <QAction>
0023 #include <QDebug>
0024 #include <QIcon>
0025 
0026 TransferGroupHandler::TransferGroupHandler(Scheduler *scheduler, TransferGroup *parent)
0027     : Handler(scheduler, parent)
0028     , m_group(parent)
0029     , m_changesFlags(Transfer::Tc_None)
0030 {
0031 }
0032 
0033 TransferGroupHandler::~TransferGroupHandler()
0034 {
0035 }
0036 
0037 void TransferGroupHandler::start()
0038 {
0039     qCDebug(KGET_DEBUG) << "TransferGroupHandler::start()";
0040     m_group->setStatus(JobQueue::Running);
0041 }
0042 
0043 void TransferGroupHandler::stop()
0044 {
0045     qCDebug(KGET_DEBUG) << "TransferGroupHandler::stop()";
0046     m_group->setStatus(JobQueue::Stopped);
0047 }
0048 
0049 void TransferGroupHandler::move(QList<TransferHandler *> transfers, TransferHandler *after)
0050 {
0051     // Check that the given transfer (after) belongs to this group
0052     if (after && (after->group() != this))
0053         return;
0054 
0055     QList<TransferHandler *>::iterator it = transfers.begin();
0056     QList<TransferHandler *>::iterator itEnd = transfers.end();
0057 
0058     for (; it != itEnd; ++it) {
0059         // Move the transfers in the JobQueue
0060         if (after)
0061             m_group->move((*it)->m_transfer, after->m_transfer);
0062         else
0063             m_group->move((*it)->m_transfer, nullptr);
0064 
0065         after = *it;
0066     }
0067 }
0068 
0069 TransferHandler *TransferGroupHandler::operator[](int i)
0070 {
0071     //     qCDebug(KGET_DEBUG) << "TransferGroupHandler::operator[" << i << "]";
0072 
0073     return (*m_group)[i]->handler();
0074 }
0075 
0076 void TransferGroupHandler::setName(const QString &name)
0077 {
0078     m_group->setName(name);
0079 }
0080 
0081 QVariant TransferGroupHandler::data(int column)
0082 {
0083     //     qCDebug(KGET_DEBUG) << "TransferGroupHandler::data(" << column << ")";
0084 
0085     switch (column) {
0086     case 0:
0087         /*if (!m_group->supportsSpeedLimits() &&
0088                          (m_group->downloadLimit(Transfer::VisibleSpeedLimit) != 0 || m_group->uploadLimit(Transfer::VisibleSpeedLimit) != 0))
0089             return name() + " - Does not supports SpeedLimits";//FIXME: Do a better text here
0090         else*/
0091         return name();
0092     case 2:
0093         if (m_group->size())
0094             return i18np("1 Item", "%1 Items", m_group->size());
0095         else
0096             return QString();
0097         /*            if (totalSize() != 0)
0098                         return KIO::convertSize(totalSize());
0099                     else
0100                         return i18nc("not available", "n/a");*/
0101     case 3:
0102         //             return QString::number(percent())+'%'; // display progressbar instead
0103         return QVariant();
0104     case 4:
0105         if (downloadSpeed() == 0) {
0106             return QString();
0107         } else
0108             return i18n("%1/s", KIO::convertSize(downloadSpeed()));
0109     default:
0110         return QVariant();
0111     }
0112 }
0113 
0114 TransferGroup::ChangesFlags TransferGroupHandler::changesFlags()
0115 {
0116     return m_changesFlags;
0117 }
0118 
0119 void TransferGroupHandler::resetChangesFlags()
0120 {
0121     m_changesFlags = 0;
0122 }
0123 
0124 int TransferGroupHandler::indexOf(TransferHandler *transfer)
0125 {
0126     return m_group->indexOf(transfer->m_transfer);
0127 }
0128 
0129 const QList<TransferHandler *> TransferGroupHandler::transfers()
0130 {
0131     QList<TransferHandler *> transfers;
0132 
0133     TransferGroup::iterator it = m_group->begin();
0134     TransferGroup::iterator itEnd = m_group->end();
0135 
0136     for (; it != itEnd; ++it) {
0137         transfers.append((static_cast<Transfer *>(*it))->handler());
0138     }
0139     return transfers;
0140 }
0141 
0142 const QList<QAction *> &TransferGroupHandler::actions()
0143 {
0144     createActions();
0145 
0146     return m_actions;
0147 }
0148 
0149 void TransferGroupHandler::setGroupChange(ChangesFlags change, bool notifyModel)
0150 {
0151     m_changesFlags |= change;
0152 
0153     if (notifyModel)
0154         m_group->model()->postDataChangedEvent(this);
0155 }
0156 
0157 void TransferGroupHandler::createActions()
0158 {
0159     if (!m_actions.empty())
0160         return;
0161 
0162     QAction *startAction = KGet::actionCollection()->addAction("transfer_group_start");
0163     startAction->setText(i18nc("start transfergroup downloads", "Start"));
0164     startAction->setIcon(QIcon::fromTheme("media-playback-start"));
0165     QObject::connect(startAction, SIGNAL(triggered()), SLOT(start()));
0166     m_actions.append(startAction);
0167 
0168     QAction *stopAction = KGet::actionCollection()->addAction("transfer_group_stop");
0169     stopAction->setText(i18nc("stop transfergroup downloads", "Stop"));
0170     stopAction->setIcon(QIcon::fromTheme("media-playback-pause"));
0171     QObject::connect(stopAction, SIGNAL(triggered()), SLOT(stop()));
0172     m_actions.append(stopAction);
0173 }
0174 
0175 #include "moc_transfergrouphandler.cpp"