]> git.sur5r.net Git - minitube/blob - src/downloadmanager.cpp
Imported Upstream version 2.3
[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
35 static DownloadManager *downloadManagerInstance = 0;
36
37 DownloadManager::DownloadManager(QWidget *parent) :
38     QObject(parent),
39     downloadModel(new DownloadModel(this, this))
40 { }
41
42 DownloadManager* DownloadManager::instance() {
43     if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
44     return downloadManagerInstance;
45 }
46
47 void DownloadManager::clear() {
48     qDeleteAll(items);
49     items.clear();
50     updateStatusMessage();
51 }
52
53 int DownloadManager::activeItems() {
54     int num = 0;
55     foreach (DownloadItem *item, items) {
56         if (item->status() == Downloading || item->status() == Starting) num++;
57     }
58     return num;
59 }
60
61 DownloadItem* DownloadManager::itemForVideo(Video* video) {
62     foreach (DownloadItem *item, items) {
63         if (item->getVideo()->id() == video->id()) return item;
64     }
65     return 0;
66 }
67
68 void DownloadManager::addItem(Video *video) {
69     // qDebug() << __FUNCTION__ << video->title();
70
71 #ifdef APP_ACTIVATION
72     if (!Activation::instance().isActivated()) {
73         if (video->duration() >= 60*4) {
74             QMessageBox msgBox(MainWindow::instance());
75             msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
76             msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::NAME));
77             msgBox.setInformativeText(
78                         tr("It can only download videos shorter than %1 minutes so you can test the download functionality.")
79                         .arg(4));
80             msgBox.setModal(true);
81             // make it a "sheet" on the Mac
82             msgBox.setWindowModality(Qt::WindowModal);
83
84             msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
85             QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
86
87             msgBox.exec();
88
89             if (msgBox.clickedButton() == buyButton) {
90                 MainWindow::instance()->showActivationView();
91             }
92
93             return;
94         }
95     }
96 #endif
97
98     DownloadItem *item = itemForVideo(video);
99     if (item != 0) {
100         if (item->status() == Failed || item->status() == Idle) {
101             qDebug() << "Restarting download" << video->title();
102             item->tryAgain();
103         } else {
104             qDebug() << "Already downloading video" << video->title();
105         }
106         return;
107     }
108
109     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
110     // TODO handle signal errors
111     // connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
112     video->loadStreamUrl();
113
114     // see you in gotStreamUrl()
115 }
116
117 void DownloadManager::gotStreamUrl(QUrl url) {
118
119     Video *video = static_cast<Video*>(sender());
120     if (!video) {
121         qDebug() << "Cannot get video in" << __FUNCTION__;
122         return;
123     }
124
125     video->disconnect(this);
126
127     QString basename = DataUtils::stringToFilename(video->title());
128     if (basename.isEmpty()) basename = video->id();
129
130     QString filename = currentDownloadFolder() + "/" + basename + ".mp4";
131
132     Video *videoCopy = video->clone();
133     DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
134
135     downloadModel->beginInsertRows(QModelIndex(), 0, 0);
136     items.prepend(item);
137     downloadModel->endInsertRows();
138
139     // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
140     connect(item, SIGNAL(finished()), SLOT(itemFinished()));
141     item->start();
142
143     updateStatusMessage();
144 }
145
146 void DownloadManager::itemFinished() {
147     if (activeItems() == 0) emit finished();
148 #ifdef APP_EXTRA
149     DownloadItem *item = static_cast<DownloadItem*>(sender());
150     if (!item) {
151         qDebug() << "Cannot get item in" << __FUNCTION__;
152         return;
153     }
154     Video *video = item->getVideo();
155     if (!video) return;
156     QString stats = tr("%1 downloaded in %2").arg(
157                 DownloadItem::formattedFilesize(item->bytesTotal()),
158                 DownloadItem::formattedTime(item->totalTime(), false));
159     Extra::notify(tr("Download finished"), video->title(), stats);
160 #endif
161 }
162
163 void DownloadManager::updateStatusMessage() {
164     QString message = tr("%n Download(s)", "", activeItems());
165     emit statusMessageChanged(message);
166 }
167
168 QString DownloadManager::defaultDownloadFolder() {
169     // download in the Movies system folder
170 #if QT_VERSION >= 0x050000
171     QString path = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
172 #else
173     QString path = QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
174 #endif
175
176     QDir moviesDir(path);
177     if (!moviesDir.exists()) {
178         // fallback to Desktop
179 #if QT_VERSION >= 0x050000
180         path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
181 #else
182         path = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
183 #endif
184
185         QDir desktopDir(path);
186         if (!desktopDir.exists()) {
187             // fallback to Home
188 #if QT_VERSION >= 0x050000
189             path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
190 #else
191             path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
192 #endif
193         }
194     }
195     return path;
196 }
197
198 QString DownloadManager::currentDownloadFolder() {
199     QSettings settings;
200     QString path = settings.value("downloadFolder").toString();
201     if (path.isEmpty()) path = defaultDownloadFolder();
202     return path;
203 }