]> git.sur5r.net Git - minitube/blob - src/downloadmanager.cpp
Imported Upstream version 2.0
[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 #include "mainwindow.h"
7 #ifdef APP_ACTIVATION
8 #include "activation.h"
9 #endif
10 #ifdef Q_WS_MAC
11 #include "macutils.h"
12 #endif
13
14 static DownloadManager *downloadManagerInstance = 0;
15
16 DownloadManager::DownloadManager(QWidget *parent) :
17     QObject(parent),
18     downloadModel(new DownloadModel(this, this))
19 { }
20
21 DownloadManager* DownloadManager::instance() {
22     if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
23     return downloadManagerInstance;
24 }
25
26 void DownloadManager::clear() {
27     qDeleteAll(items);
28     items.clear();
29     updateStatusMessage();
30 }
31
32 int DownloadManager::activeItems() {
33     int num = 0;
34     foreach (DownloadItem *item, items) {
35         if (item->status() == Downloading || item->status() == Starting) num++;
36     }
37     return num;
38 }
39
40 DownloadItem* DownloadManager::itemForVideo(Video* video) {
41     foreach (DownloadItem *item, items) {
42         if (item->getVideo()->id() == video->id()) return item;
43     }
44     return 0;
45 }
46
47 void DownloadManager::addItem(Video *video) {
48     // qDebug() << __FUNCTION__ << video->title();
49
50 #ifdef APP_ACTIVATION
51     if (!Activation::instance().isActivated()) {
52         if (video->duration() >= 60*4) {
53             QMessageBox msgBox(MainWindow::instance());
54             msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
55             msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::NAME));
56             msgBox.setInformativeText(
57                         tr("It can only download videos shorter than %1 minutes so you can test the download functionality.")
58                         .arg(4));
59             msgBox.setModal(true);
60             // make it a "sheet" on the Mac
61             msgBox.setWindowModality(Qt::WindowModal);
62
63             msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
64             QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
65
66             msgBox.exec();
67
68             if (msgBox.clickedButton() == buyButton) {
69                 MainWindow::instance()->showActivationView();
70             }
71
72             return;
73         }
74     }
75 #endif
76
77     DownloadItem *item = itemForVideo(video);
78     if (item != 0) {
79         if (item->status() == Failed || item->status() == Idle) {
80             qDebug() << "Restarting download" << video->title();
81             item->tryAgain();
82         } else {
83             qDebug() << "Already downloading video" << video->title();
84         }
85         return;
86     }
87
88     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
89     // TODO handle signal errors
90     // connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
91     video->loadStreamUrl();
92
93     // see you in gotStreamUrl()
94 }
95
96 void DownloadManager::gotStreamUrl(QUrl url) {
97
98     Video *video = static_cast<Video*>(sender());
99     if (!video) {
100         qDebug() << "Cannot get video in" << __FUNCTION__;
101         return;
102     }
103
104     video->disconnect(this);
105
106     QString path = currentDownloadFolder();
107
108     // TODO ensure all chars are filename compatible
109     QString basename = video->title();
110     basename.replace('(', '[');
111     basename.replace(')', ']');
112     basename.replace('/', ' ');
113     basename.replace('\\', ' ');
114     basename.replace('<', ' ');
115     basename.replace('>', ' ');
116     basename.replace(':', ' ');
117     basename.replace('"', ' ');
118     basename.replace('|', ' ');
119     basename.replace('?', ' ');
120     basename.replace('*', ' ');
121     basename = basename.simplified();
122
123     QString filename = path + "/" + basename + ".mp4";
124
125     Video *videoCopy = video->clone();
126     DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
127
128     downloadModel->beginInsertRows(QModelIndex(), 0, 0);
129     items.prepend(item);
130     downloadModel->endInsertRows();
131
132     // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
133     connect(item, SIGNAL(finished()), SLOT(itemFinished()));
134     item->start();
135
136     updateStatusMessage();
137 }
138
139 void DownloadManager::itemFinished() {
140     if (activeItems() == 0) emit finished();
141 #ifdef Q_WS_MAC
142     if (mac::canNotify()) {
143         DownloadItem *item = static_cast<DownloadItem*>(sender());
144         if (!item) {
145             qDebug() << "Cannot get item in" << __FUNCTION__;
146             return;
147         }
148         Video *video = item->getVideo();
149         if (!video) return;
150         QString stats = tr("%1 downloaded in %2").arg(
151                     DownloadItem::formattedFilesize(item->bytesTotal()),
152                     DownloadItem::formattedTime(item->totalTime(), false));
153         mac::notify(tr("Download finished"), video->title(), stats);
154     }
155 #endif
156 }
157
158 void DownloadManager::updateStatusMessage() {
159     QString message = tr("%n Download(s)", "", activeItems());
160     emit statusMessageChanged(message);
161 }
162
163 QString DownloadManager::defaultDownloadFolder() {
164     // download in the Movies system folder
165     QString path = QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
166     QDir moviesDir(path);
167     if (!moviesDir.exists()) {
168         // fallback to Desktop
169         path = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
170
171         QDir desktopDir(path);
172         if (!desktopDir.exists()) {
173             // fallback to Home
174             path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
175         }
176     }
177     return path;
178 }
179
180 QString DownloadManager::currentDownloadFolder() {
181     QSettings settings;
182     QString path = settings.value("downloadFolder").toString();
183     if (path.isEmpty()) path = defaultDownloadFolder();
184     return path;
185 }