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"
34 static DownloadManager *downloadManagerInstance = 0;
36 DownloadManager::DownloadManager(QWidget *parent) :
38 downloadModel(new DownloadModel(this, this))
41 DownloadManager* DownloadManager::instance() {
42 if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
43 return downloadManagerInstance;
46 void DownloadManager::clear() {
49 updateStatusMessage();
52 int DownloadManager::activeItems() {
54 foreach (DownloadItem *item, items) {
55 if (item->status() == Downloading || item->status() == Starting) num++;
60 DownloadItem* DownloadManager::itemForVideo(Video* video) {
61 foreach (DownloadItem *item, items) {
62 if (item->getVideo()->id() == video->id()) return item;
67 void DownloadManager::addItem(Video *video) {
68 // qDebug() << __FUNCTION__ << video->title();
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.")
79 msgBox.setModal(true);
80 // make it a "sheet" on the Mac
81 msgBox.setWindowModality(Qt::WindowModal);
83 msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
84 QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
88 if (msgBox.clickedButton() == buyButton) {
89 MainWindow::instance()->showActivationView();
97 DownloadItem *item = itemForVideo(video);
99 if (item->status() == Failed || item->status() == Idle) {
100 qDebug() << "Restarting download" << video->title();
103 qDebug() << "Already downloading video" << video->title();
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();
113 // see you in gotStreamUrl()
116 void DownloadManager::gotStreamUrl(QUrl url) {
118 Video *video = static_cast<Video*>(sender());
120 qDebug() << "Cannot get video in" << __FUNCTION__;
124 video->disconnect(this);
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();
140 if (!basename.isEmpty() && basename.at(0) == '.')
141 basename = basename.mid(1).trimmed();
143 if (basename.isEmpty()) basename = video->id();
145 QString filename = currentDownloadFolder() + "/" + basename + ".mp4";
147 Video *videoCopy = video->clone();
148 DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
150 downloadModel->beginInsertRows(QModelIndex(), 0, 0);
152 downloadModel->endInsertRows();
154 // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
155 connect(item, SIGNAL(finished()), SLOT(itemFinished()));
158 updateStatusMessage();
161 void DownloadManager::itemFinished() {
162 if (activeItems() == 0) emit finished();
164 DownloadItem *item = static_cast<DownloadItem*>(sender());
166 qDebug() << "Cannot get item in" << __FUNCTION__;
169 Video *video = item->getVideo();
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);
178 void DownloadManager::updateStatusMessage() {
179 QString message = tr("%n Download(s)", "", activeItems());
180 emit statusMessageChanged(message);
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);
191 QDir desktopDir(path);
192 if (!desktopDir.exists()) {
194 path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
200 QString DownloadManager::currentDownloadFolder() {
202 QString path = settings.value("downloadFolder").toString();
203 if (path.isEmpty()) path = defaultDownloadFolder();