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)
22 speedCheckTimer = new QTimer(this);
23 speedCheckTimer->setInterval(2000);
24 speedCheckTimer->setSingleShot(true);
25 connect(speedCheckTimer, SIGNAL(timeout()), SLOT(speedCheck()));
28 DownloadItem::~DownloadItem() {
29 if (m_reply) delete m_reply;
30 if (video) delete video;
33 void DownloadItem::start() {
34 m_reply = The::http()->simpleGet(m_url);
38 void DownloadItem::init() {
47 m_startedSaving = false;
48 m_finishedDownloading = false;
50 // attach to the m_reply
51 m_url = m_reply->url();
52 connect(m_reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
53 connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
54 this, SLOT(error(QNetworkReply::NetworkError)));
55 connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)),
56 this, SLOT(downloadProgress(qint64, qint64)));
57 connect(m_reply, SIGNAL(metaDataChanged()),
58 this, SLOT(metaDataChanged()));
59 connect(m_reply, SIGNAL(finished()),
60 this, SLOT(requestFinished()));
62 // start timer for the download estimation
63 m_downloadTime.start();
64 speedCheckTimer->start();
66 if (m_reply->error() != QNetworkReply::NoError) {
67 error(m_reply->error());
73 void DownloadItem::stop() {
80 void DownloadItem::open() {
81 QFileInfo info(m_file);
82 QUrl url = QUrl::fromLocalFile(info.absoluteFilePath());
83 QDesktopServices::openUrl(url);
86 void DownloadItem::openFolder() {
87 QFileInfo info(m_file);
88 QUrl url = QUrl::fromLocalFile(info.absolutePath());
89 QDesktopServices::openUrl(url);
92 void DownloadItem::tryAgain() {
99 m_reply = The::http()->simpleGet(m_url);
101 emit statusChanged();
104 void DownloadItem::downloadReadyRead() {
106 if (!m_file.isOpen()) {
107 if (!m_file.open(QIODevice::ReadWrite)) {
108 qDebug() << QString("Error opening output file: %1").arg(m_file.errorString());
110 emit statusChanged();
113 emit statusChanged();
116 if (-1 == m_file.write(m_reply->readAll())) {
118 downloadInfoLabel->setText(tr("Error saving: %1")
119 .arg(m_output.errorString()));
123 m_startedSaving = true;
124 if (m_status != Downloading) {
125 // m_status = Downloading;
126 // emit statusChanged();
127 } else if (m_finishedDownloading)
132 void DownloadItem::error(QNetworkReply::NetworkError) {
134 #ifdef DOWNLOADMANAGER_DEBUG
135 qDebug() << "DownloadItem::" << __FUNCTION__ << m_reply->errorString() << m_url;
139 qDebug() << m_reply->errorString();
140 m_errorMessage = m_reply->errorString();
149 QString DownloadItem::errorMessage() const {
150 return m_errorMessage;
153 void DownloadItem::metaDataChanged() {
154 QVariant locationHeader = m_reply->header(QNetworkRequest::LocationHeader);
155 if (locationHeader.isValid()) {
156 m_url = locationHeader.toUrl();
157 // qDebug() << "Redirecting to" << m_url;
158 m_reply->deleteLater();
159 m_reply = The::http()->simpleGet(m_url);
164 #ifdef DOWNLOADMANAGER_DEBUG
165 qDebug() << "DownloadItem::" << __FUNCTION__ << "not handled.";
169 int DownloadItem::initialBufferSize() {
170 // qDebug() << video->getDefinitionCode();
171 switch (video->getDefinitionCode()) {
182 void DownloadItem::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
184 if (m_lastProgressTime.elapsed() < 150) return;
185 m_lastProgressTime.start();
187 m_bytesReceived = bytesReceived;
189 if (m_status != Downloading) {
191 int neededBytes = (int) (bytesTotal * .001);
192 // qDebug() << bytesReceived << bytesTotal << neededBytes << m_downloadTime.elapsed();
193 int bufferSize = initialBufferSize();
194 if (bytesReceived > bufferSize
195 && bytesReceived > neededBytes
196 && m_downloadTime.elapsed() > 1000 ) {
197 emit bufferProgress(100);
198 m_status = Downloading;
199 emit statusChanged();
201 int bufferPercent = bytesReceived * 100 / qMax(bufferSize, neededBytes);
202 emit bufferProgress(bufferPercent);
207 if (bytesTotal > 0) {
208 int percent = bytesReceived * 100 / bytesTotal;
209 if (percent != this->percent) {
210 this->percent = percent;
211 emit progress(percent);
218 void DownloadItem::speedCheck() {
219 if (!m_reply) return;
220 if (m_bytesReceived < initialBufferSize() / 3) {
221 m_reply->disconnect();
223 m_reply->deleteLater();
227 qDebug() << "Retrying...";
228 connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
229 video->loadStreamUrl();
233 void DownloadItem::gotStreamUrl(QUrl streamUrl) {
235 Video *video = static_cast<Video *>(sender());
237 qDebug() << "Cannot get sender";
240 video->disconnect(this);
242 m_reply = The::http()->simpleGet(video->getStreamUrl());
246 qint64 DownloadItem::bytesTotal() const {
247 if (!m_reply) return 0;
248 return m_reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
251 qint64 DownloadItem::bytesReceived() const {
252 return m_bytesReceived;
255 double DownloadItem::remainingTime() const {
256 if (m_finishedDownloading)
259 double timeRemaining = ((double)(bytesTotal() - bytesReceived())) / currentSpeed();
261 // When downloading the eta should never be 0
262 if (timeRemaining == 0)
265 return timeRemaining;
268 double DownloadItem::currentSpeed() const {
269 if (m_finishedDownloading)
272 return m_bytesReceived * 1000.0 / m_downloadTime.elapsed();
275 void DownloadItem::requestFinished() {
277 m_finishedDownloading = true;
278 if (!m_startedSaving) {
279 qDebug() << "Request finished but never started saving";
284 emit statusChanged();
288 QString DownloadItem::formattedFilesize(qint64 size) {
290 if (size < 1024) return tr("%1 bytes").arg(size);
291 else if (size < 1024*1024) return tr("%1 KB").arg(size/1024);
292 else if (size < 1024*1024*1024) return tr("%1 MB").arg(size/1024/1024);
293 else return tr("%1 GB").arg(size/1024/1024/1024);
298 } else if (size < 1024*1024) {
305 return QString(QLatin1String("%1 %2")).arg(size).arg(unit);
308 QString DownloadItem::formattedSpeed(double speed) {
310 static const int K = 1024;
311 if (speed < K) return tr("%1 bytes/s").arg(speed);
312 else if (speed < K*K) return tr("%1 KB/s").arg(speed/K);
313 else if (speed < K*K*K) return tr("%1 MB/s").arg(speed/K/K);
314 else return tr("%1 GB/s").arg(speed/K/K/K);
316 int speedInt = (int) speed;
318 if (speedInt < 1024) {
319 unit = tr("bytes/sec");
320 } else if (speedInt < 1024*1024) {
324 speedInt /= 1024*1024;
327 return QString(QLatin1String("%1 %2")).arg(speedInt).arg(unit);
330 QString DownloadItem::formattedTime(double timeRemaining) {
331 QString timeRemainingString = tr("seconds");
332 if (timeRemaining > 60) {
333 timeRemaining = timeRemaining / 60;
334 timeRemainingString = tr("minutes");
336 timeRemaining = floor(timeRemaining);
337 return tr("%4 %5 remaining")
339 .arg(timeRemainingString);