1 #include "downloaditem.h"
2 #include "networkaccess.h"
5 #include <QDesktopServices>
11 DownloadItem::DownloadItem(Video *video, QUrl url, QString filename, QObject *parent)
14 , m_startedSaving(false)
15 , m_finishedDownloading(false)
23 void DownloadItem::start() {
24 m_reply = The::http()->simpleGet(m_url);
28 void DownloadItem::init() {
34 m_startedSaving = false;
35 m_finishedDownloading = false;
37 // attach to the m_reply
38 m_url = m_reply->url();
39 connect(m_reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
40 connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
41 this, SLOT(error(QNetworkReply::NetworkError)));
42 connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)),
43 this, SLOT(downloadProgress(qint64, qint64)));
44 connect(m_reply, SIGNAL(metaDataChanged()),
45 this, SLOT(metaDataChanged()));
46 connect(m_reply, SIGNAL(finished()),
47 this, SLOT(requestFinished()));
49 // start timer for the download estimation
50 m_downloadTime.start();
52 if (m_reply->error() != QNetworkReply::NoError) {
53 error(m_reply->error());
59 void DownloadItem::stop() {
66 void DownloadItem::open() {
67 QFileInfo info(m_file);
68 QUrl url = QUrl::fromLocalFile(info.absoluteFilePath());
69 QDesktopServices::openUrl(url);
72 void DownloadItem::openFolder() {
73 QFileInfo info(m_file);
74 QUrl url = QUrl::fromLocalFile(info.absolutePath());
75 QDesktopServices::openUrl(url);
78 void DownloadItem::tryAgain() {
85 m_reply = The::http()->simpleGet(m_url);
90 void DownloadItem::downloadReadyRead() {
92 if (!m_file.isOpen()) {
93 if (!m_file.open(QIODevice::WriteOnly)) {
94 qDebug() << QString("Error opening output file: %1").arg(m_file.errorString());
102 m_status = Downloading;
103 if (-1 == m_file.write(m_reply->readAll())) {
105 downloadInfoLabel->setText(tr("Error saving: %1")
106 .arg(m_output.errorString()));
110 m_startedSaving = true;
111 if (m_finishedDownloading)
116 void DownloadItem::error(QNetworkReply::NetworkError) {
118 #ifdef DOWNLOADMANAGER_DEBUG
119 qDebug() << "DownloadItem::" << __FUNCTION__ << m_reply->errorString() << m_url;
122 m_errorMessage = m_reply->errorString();
129 QString DownloadItem::errorMessage() const {
130 return m_errorMessage;
133 void DownloadItem::metaDataChanged() {
134 QVariant locationHeader = m_reply->header(QNetworkRequest::LocationHeader);
135 if (locationHeader.isValid()) {
136 m_url = locationHeader.toUrl();
137 m_reply->deleteLater();
138 m_reply = The::http()->simpleGet(m_url);
143 #ifdef DOWNLOADMANAGER_DEBUG
144 qDebug() << "DownloadItem::" << __FUNCTION__ << "not handled.";
148 void DownloadItem::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
149 QTime now = QTime::currentTime();
150 if (m_lastProgressTime.msecsTo(now) < 200)
153 m_lastProgressTime = now;
155 m_bytesReceived = bytesReceived;
156 if (bytesTotal > 0) {
157 percent = bytesReceived * 100 / bytesTotal;
160 emit progress(percent);
161 // emit statusChanged();
164 qint64 DownloadItem::bytesTotal() const {
165 if (!m_reply) return 0;
166 return m_reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
169 qint64 DownloadItem::bytesReceived() const {
170 return m_bytesReceived;
173 double DownloadItem::remainingTime() const {
174 if (m_finishedDownloading)
177 double timeRemaining = ((double)(bytesTotal() - bytesReceived())) / currentSpeed();
179 // When downloading the eta should never be 0
180 if (timeRemaining == 0)
183 return timeRemaining;
186 double DownloadItem::currentSpeed() const {
187 if (m_finishedDownloading)
190 return m_bytesReceived * 1000.0 / m_downloadTime.elapsed();
193 void DownloadItem::requestFinished() {
195 m_finishedDownloading = true;
196 if (!m_startedSaving) {
201 emit statusChanged();
205 QString DownloadItem::formattedFilesize(qint64 size) {
207 if (size < 1024) return tr("%1 bytes").arg(size);
208 else if (size < 1024*1024) return tr("%1 KB").arg(size/1024);
209 else if (size < 1024*1024*1024) return tr("%1 MB").arg(size/1024/1024);
210 else return tr("%1 GB").arg(size/1024/1024/1024);
215 } else if (size < 1024*1024) {
222 return QString(QLatin1String("%1 %2")).arg(size).arg(unit);
225 QString DownloadItem::formattedSpeed(double speed) {
227 static const int K = 1024;
228 if (speed < K) return tr("%1 bytes/s").arg(speed);
229 else if (speed < K*K) return tr("%1 KB/s").arg(speed/K);
230 else if (speed < K*K*K) return tr("%1 MB/s").arg(speed/K/K);
231 else return tr("%1 GB/s").arg(speed/K/K/K);
233 int speedInt = (int) speed;
235 if (speedInt < 1024) {
236 unit = tr("bytes/sec");
237 } else if (speedInt < 1024*1024) {
241 speedInt /= 1024*1024;
244 return QString(QLatin1String("%1 %2")).arg(speedInt).arg(unit);
247 QString DownloadItem::formattedTime(double timeRemaining) {
248 QString timeRemainingString = tr("seconds");
249 if (timeRemaining > 60) {
250 timeRemaining = timeRemaining / 60;
251 timeRemainingString = tr("minutes");
253 timeRemaining = floor(timeRemaining);
254 return tr("%4 %5 remaining")
256 .arg(timeRemainingString);