3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "downloadmanager.h"
22 #include "downloaditem.h"
23 #include "downloadmodel.h"
25 #include "constants.h"
26 #include "mainwindow.h"
28 #include "activation.h"
33 #include "datautils.h"
35 static DownloadManager *downloadManagerInstance = 0;
37 DownloadManager::DownloadManager(QWidget *parent) :
39 downloadModel(new DownloadModel(this, this))
42 DownloadManager* DownloadManager::instance() {
43 if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
44 return downloadManagerInstance;
47 void DownloadManager::clear() {
50 updateStatusMessage();
53 int DownloadManager::activeItems() {
55 foreach (DownloadItem *item, items) {
56 if (item->status() == Downloading || item->status() == Starting) num++;
61 DownloadItem* DownloadManager::itemForVideo(Video* video) {
62 foreach (DownloadItem *item, items) {
63 if (item->getVideo()->id() == video->id()) return item;
68 void DownloadManager::addItem(Video *video) {
69 // qDebug() << __FUNCTION__ << video->title();
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.")
80 msgBox.setModal(true);
81 // make it a "sheet" on the Mac
82 msgBox.setWindowModality(Qt::WindowModal);
84 msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
85 QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
89 if (msgBox.clickedButton() == buyButton) {
90 MainWindow::instance()->showActivationView();
98 DownloadItem *item = itemForVideo(video);
100 if (item->status() == Failed || item->status() == Idle) {
101 qDebug() << "Restarting download" << video->title();
104 qDebug() << "Already downloading video" << video->title();
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();
114 // see you in gotStreamUrl()
117 void DownloadManager::gotStreamUrl(QUrl url) {
119 Video *video = static_cast<Video*>(sender());
121 qDebug() << "Cannot get video in" << __FUNCTION__;
125 video->disconnect(this);
127 QString basename = DataUtils::stringToFilename(video->title());
128 if (basename.isEmpty()) basename = video->id();
130 QString filename = currentDownloadFolder() + "/" + basename + ".mp4";
132 Video *videoCopy = video->clone();
133 DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
135 downloadModel->beginInsertRows(QModelIndex(), 0, 0);
137 downloadModel->endInsertRows();
139 // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
140 connect(item, SIGNAL(finished()), SLOT(itemFinished()));
143 updateStatusMessage();
146 void DownloadManager::itemFinished() {
147 if (activeItems() == 0) emit finished();
149 DownloadItem *item = static_cast<DownloadItem*>(sender());
151 qDebug() << "Cannot get item in" << __FUNCTION__;
154 Video *video = item->getVideo();
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);
163 void DownloadManager::updateStatusMessage() {
164 QString message = tr("%n Download(s)", "", activeItems());
165 emit statusMessageChanged(message);
168 QString DownloadManager::defaultDownloadFolder() {
169 // download in the Movies system folder
170 #if QT_VERSION >= 0x050000
171 QString path = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
173 QString path = QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
176 QDir moviesDir(path);
177 if (!moviesDir.exists()) {
178 // fallback to Desktop
179 #if QT_VERSION >= 0x050000
180 path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
182 path = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
185 QDir desktopDir(path);
186 if (!desktopDir.exists()) {
188 #if QT_VERSION >= 0x050000
189 path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
191 path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
198 QString DownloadManager::currentDownloadFolder() {
200 QString path = settings.value("downloadFolder").toString();
201 if (path.isEmpty()) path = defaultDownloadFolder();