1 /* This file is part of the KDE project
3 Copyright (C) 2004 Dario Massarin <nekkar@libero.it>
4 Copyright (C) 2008 Lukas Appelhans <l.appelhans@gmx.de>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
12 #include "core/transfer.h"
16 #include "core/transferhandler.h"
17 #include "core/plugin/transferfactory.h"
18 #include "core/scheduler.h"
20 #include <kiconloader.h>
23 #include <QDomElement>
27 #include "nepomukhandler.h"
30 Transfer::Transfer(TransferGroup
* parent
, TransferFactory
* factory
,
31 Scheduler
* scheduler
, const KUrl
& source
, const KUrl
& dest
,
32 const QDomElement
* e
)
33 : Job(parent
, scheduler
),
34 m_source(source
), m_dest(dest
),
35 m_totalSize(0), m_downloadedSize(0), m_uploadedSize(0),
36 m_percent(0), m_downloadSpeed(0), m_uploadSpeed(0),
37 m_uploadLimit(0), m_downloadLimit(0), m_isSelected(false),
38 m_visibleUploadLimit(0), m_visibleDownloadLimit(0), m_ratio(0),
39 m_handler(0), m_factory(factory
)
42 m_nepomukHandler
= new NepomukHandler(this, 0);
49 setStatus(status(), i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
55 if(status() == Job::Delayed
)
56 m_scheduler
->stopDelayTimer(this);
61 int Transfer::elapsedTime() const
63 if (status() == Job::Running
)
64 return m_runningTime
.elapsed() / 1000;
66 return m_runningSeconds
;
69 void Transfer::setUploadLimit(int ulLimit
, SpeedLimit limit
)
71 if (limit
== Transfer::VisibleSpeedLimit
)
72 m_visibleUploadLimit
= ulLimit
;
73 if (ulLimit
< m_uploadLimit
|| m_uploadLimit
== 0)
74 m_uploadLimit
= ulLimit
;
76 m_uploadLimit
= ulLimit
;
78 setSpeedLimits(m_uploadLimit
, m_downloadLimit
);
81 void Transfer::setDownloadLimit(int dlLimit
, SpeedLimit limit
)
83 if (limit
== Transfer::VisibleSpeedLimit
)
84 m_visibleDownloadLimit
= dlLimit
;
85 if (dlLimit
< m_downloadLimit
|| m_downloadLimit
== 0)
86 m_downloadLimit
= dlLimit
;
88 m_downloadLimit
= dlLimit
;
90 setSpeedLimits(m_uploadLimit
, m_downloadLimit
);
93 int Transfer::uploadLimit(SpeedLimit limit
) const
95 if (limit
== Transfer::VisibleSpeedLimit
)
96 return m_visibleUploadLimit
;
101 int Transfer::downloadLimit(SpeedLimit limit
) const
103 if (limit
== Transfer::VisibleSpeedLimit
)
104 return m_visibleDownloadLimit
;
106 return m_downloadLimit
;
109 void Transfer::setMaximumShareRatio(double ratio
)
115 void Transfer::checkShareRatio()
117 if (m_downloadedSize
== 0 || m_ratio
== 0)
120 if (m_uploadedSize
/ m_downloadedSize
>= m_ratio
)
121 setDownloadLimit(1, Transfer::InvisibleSpeedLimit
);//If we set it to 0 we would have no limit xD
123 setDownloadLimit(0, Transfer::InvisibleSpeedLimit
);
126 void Transfer::setDelay(int seconds
)
128 m_scheduler
->startDelayTimer(this, seconds
);
130 setStatus(Job::Delayed
, i18nc("transfer state: delayed", "Delayed"), SmallIcon("view-history"));
132 setTransferChange(Tc_Status
, true);
135 void Transfer::delayTimerEvent()
137 setStatus(Job::Stopped
, i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
139 setTransferChange(Tc_Status
, true);
142 void Transfer::setLog(const QString
& message
, LogLevel level
)
144 QString
msg("<font color=\"blue\">" + QTime::currentTime().toString() + "</font> : ");
147 msg
+= "<font color=\"red\">" + message
+ "</font>";
149 if (level
== warning
)
151 msg
+= "<font color=\"yellow\">" + message
+ "</font>";
158 TransferHandler
* Transfer::handler()
161 m_handler
= m_factory
->createTransferHandler(this, scheduler());
166 TransferTreeModel
* Transfer::model()
168 return group()->model();
171 void Transfer::save(const QDomElement
&element
)
173 QDomElement e
= element
;
174 e
.setAttribute("Source", m_source
.url());
175 e
.setAttribute("Dest", m_dest
.url());
176 e
.setAttribute("TotalSize", m_totalSize
);
177 e
.setAttribute("DownloadedSize", m_downloadedSize
);
178 e
.setAttribute("UploadedSize", m_uploadedSize
);
179 e
.setAttribute("DownloadLimit", m_visibleDownloadLimit
);
180 e
.setAttribute("UploadLimit", m_visibleUploadLimit
);
181 e
.setAttribute("ElapsedTime", status() == Job::Running
? m_runningTime
.elapsed() / 1000 : m_runningSeconds
);
182 e
.setAttribute("Policy", policy() == Job::Start
? "Start" : (policy() == Job::Stop
? "Stop" : "None"));
185 void Transfer::load(const QDomElement
&e
)
187 m_source
= KUrl(e
.attribute("Source"));
188 m_dest
= KUrl(e
.attribute("Dest"));
190 m_totalSize
= e
.attribute("TotalSize").toULongLong();
191 m_downloadedSize
= e
.attribute("DownloadedSize").toULongLong();
192 m_uploadedSize
= e
.attribute("UploadedSize").toULongLong();
194 if( m_totalSize
!= 0)
195 m_percent
= (int)((100.0 * m_downloadedSize
) / m_totalSize
);
199 if((m_totalSize
== m_downloadedSize
) && (m_totalSize
!= 0))
201 setStatus(Job::Finished
, i18nc("transfer state: finished", "Finished"), SmallIcon("dialog-ok"));
205 setStatus(status(), i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
207 setUploadLimit(e
.attribute("UploadLimit").toInt(), Transfer::VisibleSpeedLimit
);
208 setDownloadLimit(e
.attribute("DownloadLimit").toInt(), Transfer::VisibleSpeedLimit
);
209 m_runningSeconds
= e
.attribute("ElapsedTime").toInt();
210 if (Settings::startupAction() == 1)
212 setPolicy(Job::Start
);
214 else if (Settings::startupAction() == 2)
216 setPolicy(Job::Stop
);
220 if (e
.attribute("Policy") == "Start")
221 setPolicy(Job::Start
);
222 else if (e
.attribute("Policy") == "Stop")
223 setPolicy(Job::Stop
);
225 setPolicy(Job::None
);
229 void Transfer::setStatus(Job::Status jobStatus
, const QString
&text
, const QPixmap
&pix
)
231 //If a job is finished don't let it to be changed
232 if((status() == Job::Finished
) && (jobStatus
!= Job::Finished
))
236 m_statusPixmap
= pix
;
239 if (jobStatus
== Job::Finished
)
240 m_nepomukHandler
->saveFileProperties();
243 if (jobStatus
== Job::Running
&& status() != Job::Running
)
245 m_runningTime
.restart();
246 m_runningTime
.addSecs(m_runningSeconds
);
248 if (jobStatus
!= Job::Running
&& status() == Job::Running
)
249 m_runningSeconds
= m_runningTime
.elapsed() / 1000;
251 * It's important to call job::setStatus AFTER having changed the
252 * icon or the text or whatever.
253 * This because this function also notifies about this change
254 * the scheduler which could also decide to change it another time
255 * as well. For example if a job status is set to Aborted, the scheduler
256 * could mark it to Delayed. This could trigger another icon or text
257 * change which would be the right one since the status of the Job
258 * has changed. If we set the icon or text after calling setStatus(),
259 * we can overwrite the last icon or text change.
261 Job::setStatus(jobStatus
);
264 void Transfer::setTransferChange(ChangesFlags change
, bool postEvent
)
266 if (change
& Tc_DownloadedSize
|| change
& Tc_Status
) {
267 change
= change
| Tc_RemainingTime
;
269 handler()->setTransferChange(change
, postEvent
);