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"
34 #include "compatibility/pathsservice.h"
35 #include "iconutils.h"
37 static DownloadManager *downloadManagerInstance = 0;
39 DownloadManager::DownloadManager(QWidget *parent) :
41 downloadModel(new DownloadModel(this, this))
44 DownloadManager* DownloadManager::instance() {
45 if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
46 return downloadManagerInstance;
49 void DownloadManager::clear() {
52 updateStatusMessage();
55 int DownloadManager::activeItems() {
57 foreach (DownloadItem *item, items) {
58 if (item->status() == Downloading || item->status() == Starting) num++;
63 DownloadItem* DownloadManager::itemForVideo(Video* video) {
64 foreach (DownloadItem *item, items) {
65 if (item->getVideo()->id() == video->id()) return item;
70 void DownloadManager::addItem(Video *video) {
71 // qDebug() << __FUNCTION__ << video->title();
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.")
82 msgBox.setModal(true);
83 // make it a "sheet" on the Mac
84 msgBox.setWindowModality(Qt::WindowModal);
86 msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
87 QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
91 if (msgBox.clickedButton() == buyButton) {
92 MainWindow::instance()->showActivationView();
100 DownloadItem *item = itemForVideo(video);
102 if (item->status() == Failed || item->status() == Idle) {
103 qDebug() << "Restarting download" << video->title();
106 qDebug() << "Already downloading video" << video->title();
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();
116 // see you in gotStreamUrl()
119 void DownloadManager::gotStreamUrl(QUrl url) {
121 Video *video = static_cast<Video*>(sender());
123 qDebug() << "Cannot get video in" << __FUNCTION__;
127 video->disconnect(this);
129 QString basename = DataUtils::stringToFilename(video->title());
130 if (basename.isEmpty()) basename = video->id();
132 QString filename = currentDownloadFolder() + "/" + basename + ".mp4";
134 Video *videoCopy = video->clone();
135 DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
137 downloadModel->beginInsertRows(QModelIndex(), 0, 0);
139 downloadModel->endInsertRows();
141 // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
142 connect(item, SIGNAL(finished()), SLOT(itemFinished()));
145 updateStatusMessage();
148 void DownloadManager::itemFinished() {
149 if (activeItems() == 0) emit finished();
151 DownloadItem *item = static_cast<DownloadItem*>(sender());
153 qDebug() << "Cannot get item in" << __FUNCTION__;
156 Video *video = item->getVideo();
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);
165 void DownloadManager::updateStatusMessage() {
166 QString message = tr("%n Download(s)", "", activeItems());
167 emit statusMessageChanged(message);
170 QString DownloadManager::defaultDownloadFolder() {
171 // download in the Movies system folder
172 QString path = Paths::getMoviesLocation();
174 const QDir moviesDir(path);
175 if (!moviesDir.exists()) {
176 // fallback to Desktop
177 path = Paths::getDesktopLocation();
179 const QDir desktopDir(path);
180 if (!desktopDir.exists()) {
182 path = Paths::getHomeLocation();
188 QString DownloadManager::currentDownloadFolder() {
190 QString path = settings.value("downloadFolder").toString();
191 if (path.isEmpty()) path = defaultDownloadFolder();