* Paint a styled progressbar instead of our home-brown one.
[kdenetwork.git] / kget / core / transfer.cpp
blobe85499a96ff54d47aca7c19310fb1882347066e7
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; version 2
8 of the License.
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(), i18n("Stopped"), SmallIcon("process-stop"));
42 Transfer::~Transfer()
44 if(status() == Job::Delayed)
45 m_scheduler->stopDelayTimer(this);
47 delete(m_handler);
50 void Transfer::setDelay(int seconds)
52 m_scheduler->startDelayTimer(this, seconds);
54 setStatus(Job::Delayed, i18n("Delayed"), SmallIcon("history"));
56 setTransferChange(Tc_Status, true);
59 void Transfer::delayTimerEvent()
61 setStatus(Job::Stopped, i18n("Stopped"), SmallIcon("process-stop"));
63 setTransferChange(Tc_Status, true);
66 void Transfer::setLog(const QString& message, LogLevel level)
68 QString msg("<font color=\"blue\">" + QTime::currentTime().toString() + "</font> : ");
69 if (level == error)
71 msg += "<font color=\"red\">" + message + "</font>";
73 if (level == warning)
75 msg += "<font color=\"yellow\">" + message + "</font>";
76 } else {
77 msg += message;
79 m_log << msg;
82 TransferHandler * Transfer::handler()
84 if(!m_handler)
85 m_handler = m_factory->createTransferHandler(this, scheduler());
87 return m_handler;
90 TransferTreeModel * Transfer::model()
92 return group()->model();
95 void Transfer::save(QDomElement e) // krazy:exclude=passbyvalue
97 e.setAttribute("Source", m_source.url());
98 e.setAttribute("Dest", m_dest.url());
99 e.setAttribute("TotalSize", (qulonglong) m_totalSize);
100 e.setAttribute("ProcessedSize", (qulonglong) m_processedSize);
103 void Transfer::load(const QDomElement &e)
105 m_source = KUrl(e.attribute("Source"));
106 m_dest = KUrl(e.attribute("Dest"));
108 m_totalSize = e.attribute("TotalSize").toInt();
109 m_processedSize = e.attribute("ProcessedSize").toInt();
111 if( m_totalSize != 0)
112 m_percent = (int)((100.0 * m_processedSize) / m_totalSize);
113 else
114 m_percent = 0;
116 if((m_totalSize == m_processedSize) && (m_totalSize != 0))
118 setStatus(Job::Finished, i18n("Finished"), SmallIcon("ok"));
120 else
122 setStatus(status(), i18n("Stopped"), SmallIcon("process-stop"));
126 void Transfer::setStatus(Job::Status jobStatus, const QString &text, const QPixmap &pix)
128 //If a job is finished don't let it to be changed
129 if((status() == Job::Finished) && (jobStatus != Job::Finished))
130 return;
132 m_statusText = text;
133 m_statusPixmap = pix;
136 * It's important to call job::setStatus AFTER having changed the
137 * icon or the text or whatever.
138 * This because this function also notifies about this change
139 * the scheduler which could also decide to change it another time
140 * as well. For example if a job status is set to Aborted, the scheduler
141 * could mark it to Delayed. This could trigger another icon or text
142 * change which would be the right one since the status of the Job
143 * has changed. If we set the icon or text after calling setStatus(),
144 * we can overwrite the last icon or text change.
146 Job::setStatus(jobStatus);
149 void Transfer::setTransferChange(ChangesFlags change, bool postEvent)
151 handler()->setTransferChange(change, postEvent);