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.
11 #include "core/transfer.h"
13 #include "core/transferhandler.h"
14 #include "core/plugin/transferfactory.h"
15 #include "core/scheduler.h"
17 #include <kiconloader.h>
20 #include <QDomElement>
23 Transfer::Transfer(TransferGroup
* parent
, TransferFactory
* factory
,
24 Scheduler
* scheduler
, const KUrl
& source
, const KUrl
& dest
,
25 const QDomElement
* e
)
26 : Job(parent
, scheduler
),
27 m_source(source
), m_dest(dest
),
28 m_totalSize(0), m_downloadedSize(0), m_uploadedSize(0),
29 m_percent(0), m_downloadSpeed(0), m_uploadSpeed(0),
30 m_ulLimit(0), m_dlLimit(0), m_isSelected(false),
31 m_visibleUlLimit(0), m_visibleDlLimit(0), m_ratio(0),
32 m_handler(0), m_factory(factory
)
38 setStatus(status(), i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
44 if(status() == Job::Delayed
)
45 m_scheduler
->stopDelayTimer(this);
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::setMaximumShareRatio(double ratio
)
70 void Transfer::checkShareRatio()
72 if (m_downloadedSize
== 0 || m_ratio
== 0)
75 if (m_uploadedSize
/ m_downloadedSize
>= m_ratio
)
76 setDownloadLimit(1);//If we set it to 0 we would have no limit xD
81 void Transfer::setDelay(int seconds
)
83 m_scheduler
->startDelayTimer(this, seconds
);
85 setStatus(Job::Delayed
, i18nc("transfer state: delayed", "Delayed"), SmallIcon("view-history"));
87 setTransferChange(Tc_Status
, true);
90 void Transfer::delayTimerEvent()
92 setStatus(Job::Stopped
, i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
94 setTransferChange(Tc_Status
, true);
97 void Transfer::setLog(const QString
& message
, LogLevel level
)
99 QString
msg("<font color=\"blue\">" + QTime::currentTime().toString() + "</font> : ");
102 msg
+= "<font color=\"red\">" + message
+ "</font>";
104 if (level
== warning
)
106 msg
+= "<font color=\"yellow\">" + message
+ "</font>";
113 TransferHandler
* Transfer::handler()
116 m_handler
= m_factory
->createTransferHandler(this, scheduler());
121 TransferTreeModel
* Transfer::model()
123 return group()->model();
126 void Transfer::save(const QDomElement
&element
)
128 QDomElement e
= element
;
129 e
.setAttribute("Source", m_source
.url());
130 e
.setAttribute("Dest", m_dest
.url());
131 e
.setAttribute("TotalSize", m_totalSize
);
132 e
.setAttribute("DownloadedSize", m_downloadedSize
);
133 e
.setAttribute("UploadedSize", m_uploadedSize
);
134 e
.setAttribute("DownloadLimit", m_visibleDlLimit
);
135 e
.setAttribute("UploadLimit", m_visibleUlLimit
);
138 void Transfer::load(const QDomElement
&e
)
140 m_source
= KUrl(e
.attribute("Source"));
141 m_dest
= KUrl(e
.attribute("Dest"));
143 m_totalSize
= e
.attribute("TotalSize").toULongLong();
144 m_downloadedSize
= e
.attribute("DownloadedSize").toULongLong();
145 m_uploadedSize
= e
.attribute("UploadedSize").toULongLong();
147 if( m_totalSize
!= 0)
148 m_percent
= (int)((100.0 * m_downloadedSize
) / m_totalSize
);
152 if((m_totalSize
== m_downloadedSize
) && (m_totalSize
!= 0))
154 setStatus(Job::Finished
, i18nc("transfer state: finished", "Finished"), SmallIcon("dialog-ok"));
158 setStatus(status(), i18nc("transfer state: stopped", "Stopped"), SmallIcon("process-stop"));
160 setVisibleUploadLimit(e
.attribute("UploadLimit").toInt());
161 setVisibleDownloadLimit(e
.attribute("DownloadLimit").toInt());
164 void Transfer::setStatus(Job::Status jobStatus
, const QString
&text
, const QPixmap
&pix
)
166 //If a job is finished don't let it to be changed
167 if((status() == Job::Finished
) && (jobStatus
!= Job::Finished
))
171 m_statusPixmap
= pix
;
174 * It's important to call job::setStatus AFTER having changed the
175 * icon or the text or whatever.
176 * This because this function also notifies about this change
177 * the scheduler which could also decide to change it another time
178 * as well. For example if a job status is set to Aborted, the scheduler
179 * could mark it to Delayed. This could trigger another icon or text
180 * change which would be the right one since the status of the Job
181 * has changed. If we set the icon or text after calling setStatus(),
182 * we can overwrite the last icon or text change.
184 Job::setStatus(jobStatus
);
187 void Transfer::setTransferChange(ChangesFlags change
, bool postEvent
)
189 handler()->setTransferChange(change
, postEvent
);