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 "iconutils.h"
36 static DownloadManager *downloadManagerInstance = 0;
38 DownloadManager::DownloadManager(QWidget *parent) :
40 downloadModel(new DownloadModel(this, this))
43 DownloadManager* DownloadManager::instance() {
44 if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
45 return downloadManagerInstance;
48 void DownloadManager::clear() {
51 updateStatusMessage();
54 int DownloadManager::activeItems() {
56 for (DownloadItem *item : items) {
57 if (item->status() == Downloading || item->status() == Starting) num++;
62 DownloadItem* DownloadManager::itemForVideo(Video* video) {
63 for (DownloadItem *item : items) {
64 if (item->getVideo()->getId() == video->getId()) return item;
69 void DownloadManager::addItem(Video *video) {
70 // qDebug() << __FUNCTION__ << video->title();
72 DownloadItem *item = itemForVideo(video);
74 if (item->status() == Failed || item->status() == Idle) {
75 qDebug() << "Restarting download" << video->getTitle();
78 qDebug() << "Already downloading video" << video->getTitle();
83 connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
84 // TODO handle signal errors
85 // connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
86 video->loadStreamUrl();
88 // see you in gotStreamUrl()
91 void DownloadManager::gotStreamUrl(QUrl url) {
93 Video *video = static_cast<Video*>(sender());
95 qDebug() << "Cannot get video in" << __FUNCTION__;
99 video->disconnect(this);
101 QString basename = DataUtils::stringToFilename(video->getTitle());
102 if (basename.isEmpty()) basename = video->getId();
104 QString filename = currentDownloadFolder() + "/" + basename + ".mp4";
106 Video *videoCopy = video->clone();
107 DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
109 downloadModel->beginInsertRows(QModelIndex(), 0, 0);
111 downloadModel->endInsertRows();
113 // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
114 connect(item, SIGNAL(finished()), SLOT(itemFinished()));
117 updateStatusMessage();
120 void DownloadManager::itemFinished() {
121 if (activeItems() == 0) emit finished();
123 DownloadItem *item = static_cast<DownloadItem*>(sender());
125 qDebug() << "Cannot get item in" << __FUNCTION__;
128 Video *video = item->getVideo();
130 QString stats = tr("%1 downloaded in %2").arg(
131 DownloadItem::formattedFilesize(item->bytesTotal()),
132 DownloadItem::formattedTime(item->totalTime(), false));
133 Extra::notify(tr("Download finished"), video->getTitle(), stats);
137 void DownloadManager::updateStatusMessage() {
138 QString message = tr("%n Download(s)", "", activeItems());
139 emit statusMessageChanged(message);
142 QString DownloadManager::defaultDownloadFolder() {
143 // download in the Movies system folder
144 QString path = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
146 const QDir moviesDir(path);
147 if (!moviesDir.exists()) {
148 // fallback to Desktop
149 path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
151 const QDir desktopDir(path);
152 if (!desktopDir.exists()) {
154 path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
160 QString DownloadManager::currentDownloadFolder() {
162 QString path = settings.value("downloadFolder").toString();
163 if (path.isEmpty()) path = defaultDownloadFolder();