]> git.sur5r.net Git - minitube/blob - src/downloadmanager.cpp
Imported Upstream version 2.5.1
[minitube] / src / downloadmanager.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "downloadmanager.h"
22 #include "downloaditem.h"
23 #include "downloadmodel.h"
24 #include "video.h"
25 #include "constants.h"
26 #include "mainwindow.h"
27 #ifdef APP_ACTIVATION
28 #include "activation.h"
29 #endif
30 #ifdef APP_EXTRA
31 #include "extra.h"
32 #endif
33 #include "datautils.h"
34 #include "compatibility/pathsservice.h"
35 #include "iconutils.h"
36
37 static DownloadManager *downloadManagerInstance = 0;
38
39 DownloadManager::DownloadManager(QWidget *parent) :
40     QObject(parent),
41     downloadModel(new DownloadModel(this, this))
42 { }
43
44 DownloadManager* DownloadManager::instance() {
45     if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
46     return downloadManagerInstance;
47 }
48
49 void DownloadManager::clear() {
50     qDeleteAll(items);
51     items.clear();
52     updateStatusMessage();
53 }
54
55 int DownloadManager::activeItems() {
56     int num = 0;
57     foreach (DownloadItem *item, items) {
58         if (item->status() == Downloading || item->status() == Starting) num++;
59     }
60     return num;
61 }
62
63 DownloadItem* DownloadManager::itemForVideo(Video* video) {
64     foreach (DownloadItem *item, items) {
65         if (item->getVideo()->id() == video->id()) return item;
66     }
67     return 0;
68 }
69
70 void DownloadManager::addItem(Video *video) {
71     // qDebug() << __FUNCTION__ << video->title();
72
73 #ifdef APP_ACTIVATION
74     if (!Activation::instance().isActivated()) {
75         if (video->duration() >= 60*4) {
76             QMessageBox msgBox(MainWindow::instance());
77             msgBox.setIconPixmap(IconUtils::pixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
78             msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::NAME));
79             msgBox.setInformativeText(
80                         tr("It can only download videos shorter than %1 minutes so you can test the download functionality.")
81                         .arg(4));
82             msgBox.setModal(true);
83             // make it a "sheet" on the Mac
84             msgBox.setWindowModality(Qt::WindowModal);
85
86             msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
87             QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
88
89             msgBox.exec();
90
91             if (msgBox.clickedButton() == buyButton) {
92                 MainWindow::instance()->showActivationView();
93             }
94
95             return;
96         }
97     }
98 #endif
99
100     DownloadItem *item = itemForVideo(video);
101     if (item != 0) {
102         if (item->status() == Failed || item->status() == Idle) {
103             qDebug() << "Restarting download" << video->title();
104             item->tryAgain();
105         } else {
106             qDebug() << "Already downloading video" << video->title();
107         }
108         return;
109     }
110
111     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
112     // TODO handle signal errors
113     // connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
114     video->loadStreamUrl();
115
116     // see you in gotStreamUrl()
117 }
118
119 void DownloadManager::gotStreamUrl(QUrl url) {
120
121     Video *video = static_cast<Video*>(sender());
122     if (!video) {
123         qDebug() << "Cannot get video in" << __FUNCTION__;
124         return;
125     }
126
127     video->disconnect(this);
128
129     QString basename = DataUtils::stringToFilename(video->title());
130     if (basename.isEmpty()) basename = video->id();
131
132     QString filename = currentDownloadFolder() + "/" + basename + ".mp4";
133
134     Video *videoCopy = video->clone();
135     DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
136
137     downloadModel->beginInsertRows(QModelIndex(), 0, 0);
138     items.prepend(item);
139     downloadModel->endInsertRows();
140
141     // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
142     connect(item, SIGNAL(finished()), SLOT(itemFinished()));
143     item->start();
144
145     updateStatusMessage();
146 }
147
148 void DownloadManager::itemFinished() {
149     if (activeItems() == 0) emit finished();
150 #ifdef APP_EXTRA
151     DownloadItem *item = static_cast<DownloadItem*>(sender());
152     if (!item) {
153         qDebug() << "Cannot get item in" << __FUNCTION__;
154         return;
155     }
156     Video *video = item->getVideo();
157     if (!video) return;
158     QString stats = tr("%1 downloaded in %2").arg(
159                 DownloadItem::formattedFilesize(item->bytesTotal()),
160                 DownloadItem::formattedTime(item->totalTime(), false));
161     Extra::notify(tr("Download finished"), video->title(), stats);
162 #endif
163 }
164
165 void DownloadManager::updateStatusMessage() {
166     QString message = tr("%n Download(s)", "", activeItems());
167     emit statusMessageChanged(message);
168 }
169
170 QString DownloadManager::defaultDownloadFolder() {
171     // download in the Movies system folder
172     QString path = Paths::getMoviesLocation();
173
174     const QDir moviesDir(path);
175     if (!moviesDir.exists()) {
176         // fallback to Desktop
177         path = Paths::getDesktopLocation();
178
179         const QDir desktopDir(path);
180         if (!desktopDir.exists()) {
181             // fallback to Home
182             path = Paths::getHomeLocation();
183         }
184     }
185     return path;
186 }
187
188 QString DownloadManager::currentDownloadFolder() {
189     QSettings settings;
190     QString path = settings.value("downloadFolder").toString();
191     if (path.isEmpty()) path = defaultDownloadFolder();
192     return path;
193 }