New feature for KGet: Webinterface.
[kdenetwork.git] / kget / core / transfer.cpp
blobd26c78a25e80104803cbe7a09bff1e98593540cd
1 /* This file is part of the KDE project
3 Copyright (C) 2004 Dario Massarin <nekkar@libero.it>
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 */
11 #include "core/transfer.h"
13 #include "core/transferhandler.h"
14 #include "core/plugin/transferfactory.h"
15 #include "core/transfergroup.h"
16 #include "core/scheduler.h"
18 #include <klocale.h>
19 #include <kiconloader.h>
21 #include <QDomElement>
22 #include <QTime>
24 Transfer::Transfer(TransferGroup * parent, TransferFactory * factory,
25 Scheduler * scheduler, const KUrl & source, const KUrl & dest,
26 const QDomElement * e)
27 : Job(parent, scheduler),
28 m_source(source), m_dest(dest),
29 m_totalSize(0), m_processedSize(0),
30 m_percent(0), m_speed(0),
31 m_isSelected(false),
32 m_handler(0), m_factory(factory)
34 if( e )
35 load( *e );
36 else
38 setStatus(status(), i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
42 Transfer::~Transfer()
44 if(status() == Job::Delayed)
45 m_scheduler->stopDelayTimer(this);
47 delete(m_handler);
50 void Transfer::setVisibleUploadLimit(int visibleUlLimit)
52 m_visibleUlLimit = visibleUlLimit;
54 setUploadLimit(m_visibleUlLimit);
57 void Transfer::setVisibleDownloadLimit(int visibleDlLimit)
59 m_visibleDlLimit = visibleDlLimit;
61 setDownloadLimit(m_visibleDlLimit);
64 void Transfer::setDelay(int seconds)
66 m_scheduler->startDelayTimer(this, seconds);
68 setStatus(Job::Delayed, i18nc("transfer state: delayed", "Delayed"), SmallIcon("view-history"));
70 setTransferChange(Tc_Status, true);
73 void Transfer::delayTimerEvent()
75 setStatus(Job::Stopped, i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
77 setTransferChange(Tc_Status, true);
80 void Transfer::setLog(const QString& message, LogLevel level)
82 QString msg("<font color=\"blue\">" + QTime::currentTime().toString() + "</font> : ");
83 if (level == error)
85 msg += "<font color=\"red\">" + message + "</font>";
87 if (level == warning)
89 msg += "<font color=\"yellow\">" + message + "</font>";
90 } else {
91 msg += message;
93 m_log << msg;
96 TransferHandler * Transfer::handler()
98 if(!m_handler)
99 m_handler = m_factory->createTransferHandler(this, scheduler());
101 return m_handler;
104 TransferTreeModel * Transfer::model()
106 return group()->model();
109 void Transfer::save(const QDomElement &element)
111 QDomElement e = element;
112 e.setAttribute("Source", m_source.url());
113 e.setAttribute("Dest", m_dest.url());
114 e.setAttribute("TotalSize", m_totalSize);
115 e.setAttribute("ProcessedSize", m_processedSize);
116 e.setAttribute("DownloadLimit", m_visibleDlLimit);
117 e.setAttribute("UploadLimit", m_visibleUlLimit);
120 void Transfer::load(const QDomElement &e)
122 m_source = KUrl(e.attribute("Source"));
123 m_dest = KUrl(e.attribute("Dest"));
125 m_totalSize = e.attribute("TotalSize").toULongLong();
126 m_processedSize = e.attribute("ProcessedSize").toULongLong();
128 if( m_totalSize != 0)
129 m_percent = (int)((100.0 * m_processedSize) / m_totalSize);
130 else
131 m_percent = 0;
133 if((m_totalSize == m_processedSize) && (m_totalSize != 0))
135 setStatus(Job::Finished, i18nc("transfer state: finished", "Finished"), SmallIcon("dialog-ok"));
137 else
139 setStatus(status(), i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
141 setVisibleUploadLimit(e.attribute("UploadLimit").toInt());
142 setVisibleDownloadLimit(e.attribute("DownloadLimit").toInt());
145 void Transfer::setStatus(Job::Status jobStatus, const QString &text, const QPixmap &pix)
147 //If a job is finished don't let it to be changed
148 if((status() == Job::Finished) && (jobStatus != Job::Finished))
149 return;
151 m_statusText = text;
152 m_statusPixmap = pix;
155 * It's important to call job::setStatus AFTER having changed the
156 * icon or the text or whatever.
157 * This because this function also notifies about this change
158 * the scheduler which could also decide to change it another time
159 * as well. For example if a job status is set to Aborted, the scheduler
160 * could mark it to Delayed. This could trigger another icon or text
161 * change which would be the right one since the status of the Job
162 * has changed. If we set the icon or text after calling setStatus(),
163 * we can overwrite the last icon or text change.
165 Job::setStatus(jobStatus);
168 void Transfer::setTransferChange(ChangesFlags change, bool postEvent)
170 handler()->setTransferChange(change, postEvent);