From 7e871514539152325e28f490bcfa21178fc073e7 Mon Sep 17 00:00:00 2001 From: Flavio Date: Mon, 13 Aug 2012 17:03:53 +0200 Subject: [PATCH] Notification when download finishes --- src/downloaditem.cpp | 7 +++++-- src/downloaditem.h | 4 +++- src/downloadmanager.cpp | 23 +++++++++++++++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/downloaditem.cpp b/src/downloaditem.cpp index 0599537..6baab76 100644 --- a/src/downloaditem.cpp +++ b/src/downloaditem.cpp @@ -71,6 +71,7 @@ void DownloadItem::init() { this, SLOT(requestFinished())); // start timer for the download estimation + m_totalTime = 0; m_downloadTime.start(); speedCheckTimer->start(); @@ -298,6 +299,7 @@ void DownloadItem::requestFinished() { } m_file.close(); m_status = Finished; + m_totalTime = m_downloadTime.elapsed() / 1000.0; emit statusChanged(); emit finished(); m_reply->deleteLater(); @@ -333,14 +335,15 @@ QString DownloadItem::formattedSpeed(double speed) { return QString(QLatin1String("%1 %2")).arg(speedInt).arg(unit); } -QString DownloadItem::formattedTime(double timeRemaining) { +QString DownloadItem::formattedTime(double timeRemaining, bool remaining) { QString timeRemainingString = tr("seconds"); if (timeRemaining > 60) { timeRemaining = timeRemaining / 60; timeRemainingString = tr("minutes"); } timeRemaining = floor(timeRemaining); - return tr("%4 %5 remaining") + QString msg = remaining ? tr("%4 %5 remaining") : "%4 %5"; + return msg .arg(timeRemaining) .arg(timeRemainingString); } diff --git a/src/downloaditem.h b/src/downloaditem.h index 58bb493..bb9245f 100644 --- a/src/downloaditem.h +++ b/src/downloaditem.h @@ -31,6 +31,7 @@ public: qint64 bytesTotal() const; qint64 bytesReceived() const; double remainingTime() const; + double totalTime() { return m_totalTime; } double currentSpeed() const; int currentPercent() const { return percent; } Video* getVideo() const { return video; } @@ -38,7 +39,7 @@ public: DownloadItemStatus status() const { return m_status; } static QString formattedFilesize(qint64 size); static QString formattedSpeed(double speed); - static QString formattedTime(double time); + static QString formattedTime(double time, bool remaining = true); QString errorMessage() const; public slots: @@ -67,6 +68,7 @@ private: bool m_finishedDownloading; QTime m_lastProgressTime; int percent; + double m_totalTime; QUrl m_url; diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp index a4f1c49..ee597d8 100644 --- a/src/downloadmanager.cpp +++ b/src/downloadmanager.cpp @@ -8,8 +8,8 @@ static DownloadManager *downloadManagerInstance = 0; DownloadManager::DownloadManager(QWidget *parent) : - QObject(parent), - downloadModel(new DownloadModel(this, this)) + QObject(parent), + downloadModel(new DownloadModel(this, this)) { } DownloadManager* DownloadManager::instance() { @@ -47,8 +47,8 @@ void DownloadManager::addItem(Video *video) { msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation)); msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::NAME)); msgBox.setInformativeText( - tr("It can only download videos shorter than %1 minutes so you can test the download functionality.") - .arg(4)); + tr("It can only download videos shorter than %1 minutes so you can test the download functionality.") + .arg(4)); msgBox.setModal(true); // make it a "sheet" on the Mac msgBox.setWindowModality(Qt::WindowModal); @@ -130,6 +130,21 @@ void DownloadManager::gotStreamUrl(QUrl url) { void DownloadManager::itemFinished() { if (activeItems() == 0) emit finished(); +#ifdef Q_WS_MAC + if (mac::canNotify()) { + DownloadItem *item = static_cast(sender()); + if (!item) { + qDebug() << "Cannot get item in" << __FUNCTION__; + return; + } + Video *video = item->getVideo(); + if (!video) return; + QString stats = tr("%1 downloaded in %2").arg( + DownloadItem::formattedFilesize(item->bytesTotal()), + DownloadItem::formattedTime(item->totalTime(), false)); + mac::notify(tr("Download finished"), video->title(), stats); + } +#endif } void DownloadManager::updateStatusMessage() { -- 2.39.5