]> git.sur5r.net Git - minitube/blob - src/downloadmanager.cpp
Upload 3.9.3-2 to unstable
[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 #include "datautils.h"
34 #include "iconutils.h"
35
36 static DownloadManager *downloadManagerInstance = 0;
37
38 DownloadManager::DownloadManager(QWidget *parent) :
39     QObject(parent),
40     downloadModel(new DownloadModel(this, this))
41 { }
42
43 DownloadManager* DownloadManager::instance() {
44     if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
45     return downloadManagerInstance;
46 }
47
48 void DownloadManager::clear() {
49     qDeleteAll(items);
50     items.clear();
51     updateStatusMessage();
52 }
53
54 int DownloadManager::activeItems() {
55     int num = 0;
56     for (DownloadItem *item : items) {
57         if (item->status() == Downloading || item->status() == Starting) num++;
58     }
59     return num;
60 }
61
62 DownloadItem* DownloadManager::itemForVideo(Video* video) {
63     for (DownloadItem *item : items) {
64         if (item->getVideo()->getId() == video->getId()) return item;
65     }
66     return 0;
67 }
68
69 void DownloadManager::addItem(Video *video) {
70     // qDebug() << __FUNCTION__ << video->title();
71
72     DownloadItem *item = itemForVideo(video);
73     if (item != 0) {
74         if (item->status() == Failed || item->status() == Idle) {
75             qDebug() << "Restarting download" << video->getTitle();
76             item->tryAgain();
77         } else {
78             qDebug() << "Already downloading video" << video->getTitle();
79         }
80         return;
81     }
82
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();
87
88     // see you in gotStreamUrl()
89 }
90
91 void DownloadManager::gotStreamUrl(QUrl url) {
92
93     Video *video = static_cast<Video*>(sender());
94     if (!video) {
95         qDebug() << "Cannot get video in" << __FUNCTION__;
96         return;
97     }
98
99     video->disconnect(this);
100
101     QString basename = DataUtils::stringToFilename(video->getTitle());
102     if (basename.isEmpty()) basename = video->getId();
103
104     QString filename = currentDownloadFolder() + "/" + basename + ".mp4";
105
106     Video *videoCopy = video->clone();
107     DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
108
109     downloadModel->beginInsertRows(QModelIndex(), 0, 0);
110     items.prepend(item);
111     downloadModel->endInsertRows();
112
113     // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
114     connect(item, SIGNAL(finished()), SLOT(itemFinished()));
115     item->start();
116
117     updateStatusMessage();
118 }
119
120 void DownloadManager::itemFinished() {
121     if (activeItems() == 0) emit finished();
122 #ifdef APP_EXTRA
123     DownloadItem *item = static_cast<DownloadItem*>(sender());
124     if (!item) {
125         qDebug() << "Cannot get item in" << __FUNCTION__;
126         return;
127     }
128     Video *video = item->getVideo();
129     if (!video) return;
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);
134 #endif
135 }
136
137 void DownloadManager::updateStatusMessage() {
138     QString message = tr("%n Download(s)", "", activeItems());
139     emit statusMessageChanged(message);
140 }
141
142 QString DownloadManager::defaultDownloadFolder() {
143     // download in the Movies system folder
144     QString path = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
145
146     const QDir moviesDir(path);
147     if (!moviesDir.exists()) {
148         // fallback to Desktop
149         path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
150
151         const QDir desktopDir(path);
152         if (!desktopDir.exists()) {
153             // fallback to Home
154             path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
155         }
156     }
157     return path;
158 }
159
160 QString DownloadManager::currentDownloadFolder() {
161     QSettings settings;
162     QString path = settings.value("downloadFolder").toString();
163     if (path.isEmpty()) path = defaultDownloadFolder();
164     return path;
165 }