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