]> git.sur5r.net Git - minitube/blob - src/downloadmanager.cpp
Imported Upstream version 1.2
[minitube] / src / downloadmanager.cpp
1 #include "downloadmanager.h"
2 #include "downloaditem.h"
3 #include "downloadmodel.h"
4 #include "video.h"
5 #include "constants.h"
6
7 static DownloadManager *downloadManagerInstance = 0;
8
9 DownloadManager::DownloadManager(QObject *parent) :
10         QObject(parent),
11         downloadModel(new DownloadModel(this, this))
12 { }
13
14 DownloadManager* DownloadManager::instance() {
15     if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
16     return downloadManagerInstance;
17 }
18
19 void DownloadManager::clear() {
20     qDeleteAll(items);
21     items.clear();
22     updateStatusMessage();
23 }
24
25 int DownloadManager::activeItems() {
26     int num = 0;
27     foreach (DownloadItem *item, items) {
28         if (item->status() == Downloading || item->status() == Starting) num++;
29     }
30     return num;
31 }
32
33 DownloadItem* DownloadManager::itemForVideo(Video* video) {
34     foreach (DownloadItem *item, items) {
35         if (item->getVideo()->id() == video->id()) return item;
36     }
37     return 0;
38 }
39
40 void DownloadManager::addItem(Video *video) {
41     // qDebug() << __FUNCTION__ << video->title();
42     
43 #ifdef APP_DEMO
44     if (video->duration() >= 60*4) {
45         QMessageBox msgBox;
46         msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
47         msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::APP_NAME));
48         msgBox.setInformativeText(
49                 tr("It can only download videos shorter than %1 minutes so you can test the download functionality.")
50                 .arg(4));
51         msgBox.setModal(true);
52
53         QPushButton *quitButton = msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
54         QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
55
56         msgBox.exec();
57
58         if (msgBox.clickedButton() == buyButton) {
59             QDesktopServices::openUrl(QString(Constants::WEBSITE) + "#download");
60         }
61
62         return;
63     }
64 #endif
65     
66
67     DownloadItem *item = itemForVideo(video);
68     if (item != 0) {
69         if (item->status() == Failed || item->status() == Idle) {
70             qDebug() << "Restarting download" << video->title();
71             item->tryAgain();
72         } else {
73             qDebug() << "Already downloading video" << video->title();
74         }
75         return;
76     }
77
78     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
79     // TODO handle signal errors
80     // connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
81     video->loadStreamUrl();
82
83     // see you in gotStreamUrl()
84 }
85
86 void DownloadManager::gotStreamUrl(QUrl url) {
87
88     Video *video = static_cast<Video*>(sender());
89     if (!video) {
90         qDebug() << "Cannot get video in" << __FUNCTION__;
91         return;
92     }
93
94     video->disconnect(this);
95
96     QString path = currentDownloadFolder();
97
98     // TODO ensure all chars are filename compatible
99     QString basename = video->title().simplified();
100     basename.replace('(', '[');
101     basename.replace(')', ']');
102     basename.replace('/', '-');
103     basename.replace('\\', '-');
104     QString filename = path + "/" + basename + ".mp4";
105
106     Video *videoCopy = video->clone();
107     DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
108
109     int row = items.count();
110     downloadModel->beginInsertRows(QModelIndex(), row, row);
111     items.append(item);
112     downloadModel->endInsertRows();
113
114     // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
115     connect(item, SIGNAL(finished()), SLOT(itemFinished()));
116     item->start();
117
118     updateStatusMessage();
119 }
120
121 void DownloadManager::itemFinished() {
122     if (activeItems() == 0) emit finished();
123 }
124
125 void DownloadManager::updateStatusMessage() {
126     QString message = tr("%n Download(s)", "", items.size());
127     emit statusMessageChanged(message);
128 }
129
130 QString DownloadManager::defaultDownloadFolder() {
131     // download in the Movies system folder
132     QString path = QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
133     QDir moviesDir(path);
134     if (!moviesDir.exists()) {
135         // fallback to Desktop
136         path = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
137     }
138     return path;
139 }
140
141 QString DownloadManager::currentDownloadFolder() {
142     QSettings settings;
143     QString path = settings.value("downloadFolder").toString();
144     if (path.isEmpty()) path = defaultDownloadFolder();
145     return path;
146 }