]> git.sur5r.net Git - minitube/blobdiff - src/downloadmanager.cpp
Refresh patches
[minitube] / src / downloadmanager.cpp
index 5363d1ea74782a9c08e69c06961292a41f56a761..21115d3ceab2fd759b344e10080ad7366a67696d 100644 (file)
@@ -3,12 +3,19 @@
 #include "downloadmodel.h"
 #include "video.h"
 #include "constants.h"
+#include "mainwindow.h"
+#ifdef APP_ACTIVATION
+#include "activation.h"
+#endif
+#ifdef Q_WS_MAC
+#include "macutils.h"
+#endif
 
 static DownloadManager *downloadManagerInstance = 0;
 
-DownloadManager::DownloadManager(QObject *parent) :
-        QObject(parent),
-        downloadModel(new DownloadModel(this, this))
+DownloadManager::DownloadManager(QWidget *parent) :
+    QObject(parent),
+    downloadModel(new DownloadModel(this, this))
 { }
 
 DownloadManager* DownloadManager::instance() {
@@ -39,30 +46,33 @@ DownloadItem* DownloadManager::itemForVideo(Video* video) {
 
 void DownloadManager::addItem(Video *video) {
     // qDebug() << __FUNCTION__ << video->title();
-    
-#ifdef APP_DEMO
-    if (video->duration() >= 60*4) {
-        QMessageBox msgBox;
-        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::APP_NAME));
-        msgBox.setInformativeText(
-                tr("It can only download videos shorter than %1 minutes so you can test the download functionality.")
-                .arg(4));
-        msgBox.setModal(true);
-
-        QPushButton *quitButton = msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
-        QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
-
-        msgBox.exec();
-
-        if (msgBox.clickedButton() == buyButton) {
-            QDesktopServices::openUrl(QString(Constants::WEBSITE) + "#download");
-        }
 
-        return;
+#ifdef APP_ACTIVATION
+    if (!Activation::instance().isActivated()) {
+        if (video->duration() >= 60*4) {
+            QMessageBox msgBox(MainWindow::instance());
+            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));
+            msgBox.setModal(true);
+            // make it a "sheet" on the Mac
+            msgBox.setWindowModality(Qt::WindowModal);
+
+            msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
+            QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
+
+            msgBox.exec();
+
+            if (msgBox.clickedButton() == buyButton) {
+                MainWindow::instance()->showActivationView();
+            }
+
+            return;
+        }
     }
 #endif
-    
 
     DownloadItem *item = itemForVideo(video);
     if (item != 0) {
@@ -115,9 +125,8 @@ void DownloadManager::gotStreamUrl(QUrl url) {
     Video *videoCopy = video->clone();
     DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
 
-    int row = items.count();
-    downloadModel->beginInsertRows(QModelIndex(), row, row);
-    items.append(item);
+    downloadModel->beginInsertRows(QModelIndex(), 0, 0);
+    items.prepend(item);
     downloadModel->endInsertRows();
 
     // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
@@ -129,10 +138,25 @@ void DownloadManager::gotStreamUrl(QUrl url) {
 
 void DownloadManager::itemFinished() {
     if (activeItems() == 0) emit finished();
+#ifdef Q_WS_MAC
+    if (mac::canNotify()) {
+        DownloadItem *item = static_cast<DownloadItem*>(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() {
-    QString message = tr("%n Download(s)", "", items.size());
+    QString message = tr("%n Download(s)", "", activeItems());
     emit statusMessageChanged(message);
 }
 
@@ -143,6 +167,12 @@ QString DownloadManager::defaultDownloadFolder() {
     if (!moviesDir.exists()) {
         // fallback to Desktop
         path = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
+
+        QDir desktopDir(path);
+        if (!desktopDir.exists()) {
+            // fallback to Home
+            path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
+        }
     }
     return path;
 }