1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/* This file is part of the KDE project
Copyright (C) 2004 Dario Massarin <nekkar@libero.it>
Copyright (C) 2008 - 2011 Lukas Appelhans <l.appelhans@gmx.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#include "core/transfer.h"
#include "settings.h"
#include "core/transferhandler.h"
#include "core/plugin/transferfactory.h"
#include "core/scheduler.h"
#include <kiconloader.h>
#include <klocale.h>
#include <QDomElement>
#include <QTime>
#ifdef HAVE_NEPOMUK
#include "nepomukhandler.h"
#endif
struct StatusStrings
{
const char * context;
const char * name;
};
const StatusStrings STATUSTEXTS[] = {
{"", I18N_NOOP("Downloading....")},
{I18N_NOOP2_NOSTRIP("transfer state: delayed", "Delayed")},
{I18N_NOOP2_NOSTRIP("transfer state: stopped", "Stopped")},
{I18N_NOOP2_NOSTRIP("transfer state: aborted", "Aborted")},
{I18N_NOOP2_NOSTRIP("transfer state: finished", "Finished")},
{"", ""},//TODO: Add FinishedKeepAlive status
{I18N_NOOP2_NOSTRIP("changing the destination of the file", "Changing destination")}
};
const QStringList STATUSICONS = QStringList() << "media-playback-start" << "view-history" << "process-stop" << "dialog-error" << "dialog-ok" << "media-playback-start" << "media-playback-pause";
Transfer::Transfer(TransferGroup * parent, TransferFactory * factory,
Scheduler * scheduler, const KUrl & source, const KUrl & dest,
const QDomElement * e)
: Job(scheduler, parent),
m_source(source), m_dest(dest),
m_totalSize(0), m_downloadedSize(0), m_uploadedSize(0),
m_percent(0), m_downloadSpeed(0), m_uploadSpeed(0),
m_uploadLimit(0), m_downloadLimit(0), m_isSelected(false),
m_capabilities(0), m_visibleUploadLimit(0), m_visibleDownloadLimit(0),
m_ratio(0), m_handler(0), m_factory(factory)
#ifdef HAVE_NEPOMUK
, m_nepomukHandler(0)
#endif
{
Q_UNUSED(e)
}
Transfer::~Transfer()
{
}
void Transfer::setCapabilities(Capabilities capabilities)
{
if (m_capabilities != capabilities) {
m_capabilities = capabilities;
emit capabilitiesChanged();
}
}
void Transfer::create()
{
#ifdef HAVE_NEPOMUK
if (!m_nepomukHandler)
m_nepomukHandler = new NepomukHandler(this);
#endif
init();
}
void Transfer::destroy(DeleteOptions options)
{
deinit(options);
#ifdef HAVE_NEPOMUK
nepomukHandler()->deinit();
#endif //HAVE_NEPOMUK
}
void Transfer::init()//TODO think about e, maybe not have it at all in the constructor?
{
}
#ifdef HAVE_NEPOMUK
void Transfer::setNepomukHandler(NepomukHandler *handler)
{
delete(m_nepomukHandler);
m_nepomukHandler = handler;
}
#endif //HAVE_NEPOMUK
bool Transfer::setDirectory(const KUrl& newDirectory)
{
Q_UNUSED(newDirectory)
//the standard implemention always returns false
return false;
}
int Transfer::elapsedTime() const
{
if (status() == Job::Running)
return m_runningTime.elapsed() / 1000;
return m_runningSeconds;
}
int Transfer::averageDownloadSpeed() const
{
const int runningSeconds = elapsedTime();
if (runningSeconds)
{
return m_totalSize / runningSeconds;
}
return 0;
}
QHash<KUrl, QPair<bool, int> > Transfer::availableMirrors(const KUrl &file) const
{
Q_UNUSED(file)
QHash<KUrl, QPair<bool, int> > available;
available[m_source] = QPair<bool, int>(true, 1);
return available;
}
void Transfer::setUploadLimit(int ulLimit, SpeedLimit limit)
{
if (limit == Transfer::VisibleSpeedLimit)
m_visibleUploadLimit = ulLimit;
if (ulLimit < m_uploadLimit || m_uploadLimit == 0)
m_uploadLimit = ulLimit;
else
m_uploadLimit = ulLimit;
setSpeedLimits(m_uploadLimit, m_downloadLimit);
}
void Transfer::setDownloadLimit(int dlLimit, SpeedLimit limit)
{
if (limit == Transfer::VisibleSpeedLimit)
m_visibleDownloadLimit = dlLimit;
if (dlLimit < m_downloadLimit || m_downloadLimit == 0)
m_downloadLimit = dlLimit;
else
m_downloadLimit = dlLimit;
setSpeedLimits(m_uploadLimit, m_downloadLimit);
}
int Transfer::uploadLimit(SpeedLimit limit) const
{
if (limit == Transfer::VisibleSpeedLimit)
return m_visibleUploadLimit;
return m_uploadLimit;
}
int Transfer::downloadLimit(SpeedLimit limit) const
{
if (limit == Transfer::VisibleSpeedLimit)
return m_visibleDownloadLimit;
return m_downloadLimit;
}
void Transfer::setMaximumShareRatio(double ratio)
{
m_ratio = ratio;
checkShareRatio();
}
void Transfer::checkShareRatio()
{
if (m_downloadedSize == 0 || m_ratio == 0)
return;
if (m_uploadedSize / m_downloadedSize >= m_ratio)
setDownloadLimit(1, Transfer::InvisibleSpeedLimit);//If we set it to 0 we would have no limit xD
else
setDownloadLimit(0, Transfer::InvisibleSpeedLimit);
}
void Transfer::setLog(const QString& message, Transfer::LogLevel level)
{
QString msg("<font color=\"blue\">" + QTime::currentTime().toString() + "</font> : ");
if (level == Log_Error)
{
msg += "<font color=\"red\">" + message + "</font>";
}
if (level == Log_Warning)
{
msg += "<font color=\"yellow\">" + message + "</font>";
} else {
msg += message;
}
m_log << msg;
}
TransferHandler * Transfer::handler()
{
if(!m_handler)
m_handler = m_factory->createTransferHandler(this, scheduler());
return m_handler;
}
TransferTreeModel * Transfer::model()
{
return group()->model();
}
void Transfer::save(const QDomElement &element)
{
QDomElement e = element;
e.setAttribute("Source", m_source.url());
e.setAttribute("Dest", m_dest.url());
e.setAttribute("TotalSize", m_totalSize);
e.setAttribute("DownloadedSize", m_downloadedSize);
e.setAttribute("UploadedSize", m_uploadedSize);
e.setAttribute("DownloadLimit", m_visibleDownloadLimit);
e.setAttribute("UploadLimit", m_visibleUploadLimit);
e.setAttribute("ElapsedTime", status() == Job::Running ? m_runningTime.elapsed() / 1000 : m_runningSeconds);
e.setAttribute("Policy", policy() == Job::Start ? "Start" : (policy() == Job::Stop ? "Stop" : "None"));
}
void Transfer::load(const QDomElement *element)
{
if (!element)
{
setStatus(status(), i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
setStartStatus(status());
return;
}
const QDomElement e = *element;
m_source = KUrl(e.attribute("Source"));
m_dest = KUrl(e.attribute("Dest"));
m_totalSize = e.attribute("TotalSize").toULongLong();
m_downloadedSize = e.attribute("DownloadedSize").toULongLong();
m_uploadedSize = e.attribute("UploadedSize").toULongLong();
m_percent = (m_totalSize ? ((100.0 * m_downloadedSize) / m_totalSize) : 0);
if ((m_totalSize == m_downloadedSize) && (m_totalSize != 0)) {
setStartStatus(Job::Finished);
setStatus(startStatus());
} else {
setStatus(status(), i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
setStartStatus(status());
}
setUploadLimit(e.attribute("UploadLimit").toInt(), Transfer::VisibleSpeedLimit);
setDownloadLimit(e.attribute("DownloadLimit").toInt(), Transfer::VisibleSpeedLimit);
m_runningSeconds = e.attribute("ElapsedTime").toInt();
if (Settings::startupAction() == 1)
{
setPolicy(Job::Start);
}
else if (Settings::startupAction() == 2)
{
setPolicy(Job::Stop);
}
else
{
if (e.attribute("Policy") == "Start")
setPolicy(Job::Start);
else if (e.attribute("Policy") == "Stop")
setPolicy(Job::Stop);
else
setPolicy(Job::None);
}
}
void Transfer::setStatus(Job::Status jobStatus, const QString &text, const QPixmap &pix)
{
const bool statusChanged = (status() != jobStatus);
QString statusText = text;
if (statusText.isEmpty()) {
statusText = i18nc(STATUSTEXTS[jobStatus].context, STATUSTEXTS[jobStatus].name);
}
//always prefer pix, if it is set
if (!pix.isNull()) {
m_statusPixmap = pix;
} else if (statusChanged || m_statusPixmap.isNull()) {
m_statusPixmap = SmallIcon(STATUSICONS[jobStatus]);
}
m_statusText = statusText;
if (jobStatus == Job::Running && status() != Job::Running)
{
m_runningTime.restart();
m_runningTime.addSecs(m_runningSeconds);
}
if (jobStatus != Job::Running && status() == Job::Running)
m_runningSeconds = m_runningTime.elapsed() / 1000;
/**
* It's important to call job::setStatus AFTER having changed the
* icon or the text or whatever.
* This because this function also notifies about this change
* the scheduler which could also decide to change it another time
* as well. For example if a job status is set to Aborted, the scheduler
* could mark it to Delayed. This could trigger another icon or text
* change which would be the right one since the status of the Job
* has changed. If we set the icon or text after calling setStatus(),
* we can overwrite the last icon or text change.
*/
Job::setStatus(jobStatus);
#ifdef HAVE_NEPOMUK
const bool loadedFinished = ((startStatus() == Job::Finished) || (startStatus() == Job::FinishedKeepAlive));
const bool isFinished = ((jobStatus == Job::Finished) || (jobStatus == Job::FinishedKeepAlive));
if (!loadedFinished && statusChanged && isFinished) {
m_nepomukHandler->saveFileProperties();
}
#endif
}
void Transfer::setTransferChange(ChangesFlags change, bool postEvent)
{
if (change & Tc_DownloadedSize || change & Tc_Status) {
change = change | Tc_RemainingTime;
}
handler()->setTransferChange(change, postEvent);
}
QString Transfer::statusText(Job::Status status)
{
return i18nc(STATUSTEXTS[status].context, STATUSTEXTS[status].name);
}
QPixmap Transfer::statusPixmap(Job::Status status)
{
return SmallIcon(STATUSICONS[status]);
}
|