1 #include "downloaditem.h"
2 #include "networkaccess.h"
5 #include <QDesktopServices>
13 NetworkAccess* http();
16 DownloadItem::DownloadItem(Video *video, QUrl url, QString filename, QObject *parent)
19 , m_startedSaving(false)
20 , m_finishedDownloading(false)
27 speedCheckTimer = new QTimer(this);
28 speedCheckTimer->setInterval(2000);
29 speedCheckTimer->setSingleShot(true);
30 connect(speedCheckTimer, SIGNAL(timeout()), SLOT(speedCheck()));
33 DownloadItem::~DownloadItem() {
44 void DownloadItem::start() {
45 m_reply = The::http()->simpleGet(m_url);
49 void DownloadItem::init() {
58 m_startedSaving = false;
59 m_finishedDownloading = false;
61 // attach to the m_reply
62 m_url = m_reply->url();
63 connect(m_reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
64 connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
65 this, SLOT(error(QNetworkReply::NetworkError)));
66 connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)),
67 this, SLOT(downloadProgress(qint64, qint64)));
68 connect(m_reply, SIGNAL(metaDataChanged()),
69 this, SLOT(metaDataChanged()));
70 connect(m_reply, SIGNAL(finished()),
71 this, SLOT(requestFinished()));
73 // start timer for the download estimation
74 m_downloadTime.start();
75 speedCheckTimer->start();
77 if (m_reply->error() != QNetworkReply::NoError) {
78 error(m_reply->error());
84 void DownloadItem::stop() {
86 m_reply->disconnect();
88 m_reply->deleteLater();
95 void DownloadItem::open() {
96 QFileInfo info(m_file);
97 QUrl url = QUrl::fromLocalFile(info.absoluteFilePath());
98 QDesktopServices::openUrl(url);
101 void DownloadItem::openFolder() {
102 QFileInfo info(m_file);
104 mac::showInFinder(info.absoluteFilePath());
106 QUrl url = QUrl::fromLocalFile(info.absolutePath());
107 QDesktopServices::openUrl(url);
111 void DownloadItem::tryAgain() {
116 void DownloadItem::downloadReadyRead() {
117 if (!m_reply) return;
119 if (!m_file.isOpen()) {
120 if (!m_file.open(QIODevice::ReadWrite)) {
121 qWarning() << QString("Error opening output file: %1").arg(m_file.errorString());
123 emit statusChanged();
126 emit statusChanged();
129 if (-1 == m_file.write(m_reply->readAll())) {
130 qWarning() << "Error saving." << m_file.errorString();
133 m_startedSaving = true;
135 // if (m_finishedDownloading) requestFinished();
139 void DownloadItem::error(QNetworkReply::NetworkError) {
142 qWarning() << m_reply->errorString() << m_reply->url().toEncoded();
143 m_errorMessage = m_reply->errorString();
152 QString DownloadItem::errorMessage() const {
153 return m_errorMessage;
156 void DownloadItem::metaDataChanged() {
157 if (!m_reply) return;
158 QVariant locationHeader = m_reply->header(QNetworkRequest::LocationHeader);
159 if (locationHeader.isValid()) {
160 m_url = locationHeader.toUrl();
161 qDebug() << "Redirecting to" << m_url;
166 #ifdef DOWNLOADMANAGER_DEBUG
167 qDebug() << "DownloadItem::" << __FUNCTION__ << "not handled.";
171 int DownloadItem::initialBufferSize() {
172 // qDebug() << video->getDefinitionCode();
173 switch (video->getDefinitionCode()) {
184 void DownloadItem::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
186 // qDebug() << bytesReceived << bytesTotal << m_downloadTime.elapsed();
188 if (m_lastProgressTime.elapsed() < 150) return;
189 m_lastProgressTime.start();
191 m_bytesReceived = bytesReceived;
193 if (m_status != Downloading) {
195 int neededBytes = (int) (bytesTotal * .005);
196 int bufferSize = initialBufferSize();
197 if (bufferSize > bytesTotal) bufferSize = bytesTotal;
198 // qDebug() << bytesReceived << bytesTotal << neededBytes << bufferSize << m_downloadTime.elapsed();
199 if (bytesReceived > bufferSize
200 && bytesReceived > neededBytes
201 && m_downloadTime.elapsed() > 2000) {
202 emit bufferProgress(100);
203 m_status = Downloading;
204 emit statusChanged();
206 int bufferPercent = bytesReceived * 100 / qMax(bufferSize, neededBytes);
207 emit bufferProgress(bufferPercent);
212 if (bytesTotal > 0) {
213 int percent = bytesReceived * 100 / bytesTotal;
214 if (percent != this->percent) {
215 this->percent = percent;
216 emit progress(percent);
223 void DownloadItem::speedCheck() {
224 if (!m_reply) return;
225 int bytesTotal = m_reply->size();
226 int bufferSize = initialBufferSize();
227 if (bufferSize > bytesTotal) bufferSize = 0;
228 if (m_bytesReceived < bufferSize / 3) {
232 qDebug() << "Retrying...";
233 connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)), Qt::UniqueConnection);
234 video->loadStreamUrl();
238 void DownloadItem::gotStreamUrl(QUrl /*streamUrl*/) {
240 Video *video = static_cast<Video *>(sender());
242 qDebug() << "Cannot get sender";
245 video->disconnect(this);
247 m_url = video->getStreamUrl();
251 qint64 DownloadItem::bytesTotal() const {
252 if (!m_reply) return 0;
253 return m_reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
256 qint64 DownloadItem::bytesReceived() const {
257 return m_bytesReceived;
260 double DownloadItem::remainingTime() const {
261 if (m_finishedDownloading)
264 double timeRemaining = ((double)(bytesTotal() - bytesReceived())) / currentSpeed();
266 // When downloading the eta should never be 0
267 if (timeRemaining == 0)
270 return timeRemaining;
273 double DownloadItem::currentSpeed() const {
274 if (m_finishedDownloading)
277 return m_bytesReceived * 1000.0 / m_downloadTime.elapsed();
280 void DownloadItem::requestFinished() {
281 if (!m_startedSaving) {
282 qDebug() << "Request finished but never started saving";
287 if (m_bytesReceived <= 0) {
288 qDebug() << "Request finished but saved 0 bytes";
293 m_finishedDownloading = true;
295 if (m_status == Starting) {
296 m_status = Downloading;
297 emit statusChanged();
301 emit statusChanged();
303 m_reply->deleteLater();
307 QString DownloadItem::formattedFilesize(qint64 size) {
311 } else if (size < 1024*1024) {
318 return QString(QLatin1String("%1 %2")).arg(size).arg(unit);
321 QString DownloadItem::formattedSpeed(double speed) {
322 int speedInt = (int) speed;
324 if (speedInt < 1024) {
325 unit = tr("bytes/sec");
326 } else if (speedInt < 1024*1024) {
330 speedInt /= 1024*1024;
333 return QString(QLatin1String("%1 %2")).arg(speedInt).arg(unit);
336 QString DownloadItem::formattedTime(double timeRemaining) {
337 QString timeRemainingString = tr("seconds");
338 if (timeRemaining > 60) {
339 timeRemaining = timeRemaining / 60;
340 timeRemainingString = tr("minutes");
342 timeRemaining = floor(timeRemaining);
343 return tr("%4 %5 remaining")
345 .arg(timeRemainingString);