1 /* This file is part of the KDE project
2 
3    Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
4 
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 */
10 
11 #include "core/transferhandler.h"
12 
13 #include "core/job.h"
14 #include "core/jobqueue.h"
15 #include "core/transfergroup.h"
16 #include "core/transfergrouphandler.h"
17 #include "core/transfertreemodel.h"
18 #include "core/plugin/transferfactory.h"
19 #include "settings.h"
20 #include "mainwindow.h"
21 #include "kgetkjobadapter.h"
22 
23 #include "kget_debug.h"
24 #include <QDebug>
25 #include <KLocalizedString>
26 #include <QAction>
27 #include <QVariant>
28 
TransferHandler(Transfer * parent,Scheduler * scheduler)29 TransferHandler::TransferHandler(Transfer * parent, Scheduler * scheduler)
30   : Handler(scheduler, parent),
31     m_transfer(parent),
32     m_changesFlags(Transfer::Tc_None)
33 {
34     static int dBusObjIdx = 0;
35     m_dBusObjectPath = "/KGet/Transfers/" + QString::number(dBusObjIdx++);
36 
37     m_kjobAdapter = new KGetKJobAdapter(this, this);
38 
39     connect(m_transfer, &Transfer::capabilitiesChanged, this, &TransferHandler::capabilitiesChanged);
40 }
41 
~TransferHandler()42 TransferHandler::~TransferHandler()
43 {
44 }
45 
capabilities() const46 Transfer::Capabilities TransferHandler::capabilities() const
47 {
48     return m_transfer->capabilities();
49 }
50 
start()51 void TransferHandler::start()
52 {
53     if(m_transfer->group()->status() == JobQueue::Running)
54     {
55         m_transfer->setPolicy(Job::None);
56         KGet::model()->moveTransfer(m_transfer, m_transfer->group());
57     }
58     else
59     {
60         m_transfer->setPolicy(Job::Start);
61     }
62 }
63 
stop()64 void TransferHandler::stop()
65 {
66     if (m_transfer->group()->status() == JobQueue::Stopped)
67     {
68         m_transfer->setPolicy(Job::None);
69     }
70     else
71     {
72         m_transfer->setPolicy(Job::Stop);
73     }
74 }
75 
elapsedTime() const76 int TransferHandler::elapsedTime() const
77 {
78     return m_transfer->elapsedTime();
79 }
80 
remainingTime() const81 int TransferHandler::remainingTime() const
82 {
83     return m_transfer->remainingTime();
84 }
85 
totalSize() const86 KIO::filesize_t TransferHandler::totalSize() const
87 {
88     return m_transfer->totalSize();
89 }
90 
downloadedSize() const91 KIO::filesize_t TransferHandler::downloadedSize() const
92 {
93     return m_transfer->downloadedSize();
94 }
95 
uploadedSize() const96 KIO::filesize_t TransferHandler::uploadedSize() const
97 {
98     return m_transfer->uploadedSize();
99 }
100 
percent() const101 int TransferHandler::percent() const
102 {
103     return m_transfer->percent();
104 }
105 
downloadSpeed() const106 int TransferHandler::downloadSpeed() const
107 {
108     return m_transfer->downloadSpeed();
109 }
110 
averageDownloadSped() const111 int TransferHandler::averageDownloadSped() const
112 {
113     return m_transfer->averageDownloadSpeed();
114 }
115 
uploadSpeed() const116 int TransferHandler::uploadSpeed() const
117 {
118     return m_transfer->uploadSpeed();
119 }
120 
data(int column)121 QVariant TransferHandler::data(int column)
122 {
123 //     qCDebug(KGET_DEBUG) << "TransferHandler::data(" << column << ")";
124 
125     switch(column)
126     {
127         case 0:
128             return dest().fileName();
129         case 1:
130             if (status() == Job::Aborted && !error().text.isEmpty())
131                 return error().text;
132             return statusText();
133         case 2:
134             if (totalSize() != 0)
135                 return KIO::convertSize(totalSize());
136             else
137                 return i18nc("not available", "n/a");
138         case 3:
139 //             return QString::number(percent())+'%'; // display progressbar instead
140             return QVariant();
141         case 4:
142             if (downloadSpeed() == 0)
143             {
144                 if (m_transfer->isStalled())
145                     return i18n("Stalled");
146                 else
147                     return QString();
148             }
149             else
150                 return i18n("%1/s", KIO::convertSize(downloadSpeed()));
151         case 5:
152             if (status() == Job::Running && downloadSpeed() != 0)
153                 return KIO::convertSeconds(remainingTime());
154             else
155                 return QString();
156         default:
157             return QVariant();
158     }
159 }
160 
setSelected(bool select)161 void TransferHandler::setSelected( bool select )
162 {
163     if( (select && !isSelected()) || (!select && isSelected()) )
164     {
165         m_transfer->m_isSelected = select;
166         setTransferChange( Transfer::Tc_Selection, true );
167     }
168 }
169 
isSelected() const170 bool TransferHandler::isSelected() const
171 {
172     return m_transfer->m_isSelected;
173 }
174 
changesFlags() const175 Transfer::ChangesFlags TransferHandler::changesFlags() const
176 {
177     return m_changesFlags;
178 }
179 
resetChangesFlags()180 void TransferHandler::resetChangesFlags()
181 {
182     m_changesFlags = 0;
183 }
184 
destroy()185 void TransferHandler::destroy()
186 {
187     qCDebug(KGET_DEBUG) << "TransferHandler::destroy() ENTERING";
188 
189     qCDebug(KGET_DEBUG) << "TransferHandler::destroy() LEAVING";
190 }
191 
setTransferChange(ChangesFlags change,bool notifyModel)192 void TransferHandler::setTransferChange(ChangesFlags change, bool notifyModel)
193 {
194     m_changesFlags |= change;
195 
196     if (notifyModel)
197     {
198         // Notify the TransferTreeModel
199         m_transfer->model()->postDataChangedEvent(this);
200 
201         m_kjobAdapter->slotUpdateDescription();
202     }
203 }
204 
contextActions()205 QList<QAction*> TransferHandler::contextActions()
206 {
207     QList<QAction*> actions;
208     if (status() != Job::Finished)
209     {
210         actions << KGet::actionCollection()->action("start_selected_download")
211                 << KGet::actionCollection()->action("stop_selected_download");
212     }
213     actions << KGet::actionCollection()->action("delete_selected_download")
214             << KGet::actionCollection()->action("redownload_selected_download")
215             << KGet::actionCollection()->action("select_all");
216 
217     return actions;
218 }
219 
factoryActions()220 QList<QAction*> TransferHandler::factoryActions()
221 {
222     QList<QAction*> actions;
223     foreach(QAction *action, m_transfer->factory()->actions(this))
224         actions.append(action);
225     return actions;
226 }
227