1 #include "downloadmanager.h"
2 #include "downloaditem.h"
3 #include "downloadmodel.h"
7 static DownloadManager *downloadManagerInstance = 0;
9 DownloadManager::DownloadManager(QObject *parent) :
11 downloadModel(new DownloadModel(this, this))
14 DownloadManager* DownloadManager::instance() {
15 if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
16 return downloadManagerInstance;
19 void DownloadManager::clear() {
22 updateStatusMessage();
25 int DownloadManager::activeItems() {
27 foreach (DownloadItem *item, items) {
28 if (item->status() == Downloading || item->status() == Starting) num++;
33 DownloadItem* DownloadManager::itemForVideo(Video* video) {
34 foreach (DownloadItem *item, items) {
35 if (item->getVideo()->id() == video->id()) return item;
40 void DownloadManager::addItem(Video *video) {
41 // qDebug() << __FUNCTION__ << video->title();
44 if (video->duration() >= 60*4) {
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.")
51 msgBox.setModal(true);
53 QPushButton *quitButton = msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
54 QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
58 if (msgBox.clickedButton() == buyButton) {
59 QDesktopServices::openUrl(QString(Constants::WEBSITE) + "#download");
67 DownloadItem *item = itemForVideo(video);
69 if (item->status() == Failed || item->status() == Idle) {
70 qDebug() << "Restarting download" << video->title();
73 qDebug() << "Already downloading video" << video->title();
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();
83 // see you in gotStreamUrl()
86 void DownloadManager::gotStreamUrl(QUrl url) {
88 Video *video = static_cast<Video*>(sender());
90 qDebug() << "Cannot get video in" << __FUNCTION__;
94 video->disconnect(this);
96 QString path = currentDownloadFolder();
98 // TODO ensure all chars are filename compatible
99 QString basename = video->title();
100 basename.replace('(', '[');
101 basename.replace(')', ']');
102 basename.replace('/', ' ');
103 basename.replace('\\', ' ');
104 basename.replace('<', ' ');
105 basename.replace('>', ' ');
106 basename.replace(':', ' ');
107 basename.replace('"', ' ');
108 basename.replace('|', ' ');
109 basename.replace('?', ' ');
110 basename.replace('*', ' ');
111 basename = basename.simplified();
113 QString filename = path + "/" + basename + ".mp4";
115 Video *videoCopy = video->clone();
116 DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
118 int row = items.count();
119 downloadModel->beginInsertRows(QModelIndex(), row, row);
121 downloadModel->endInsertRows();
123 // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
124 connect(item, SIGNAL(finished()), SLOT(itemFinished()));
127 updateStatusMessage();
130 void DownloadManager::itemFinished() {
131 if (activeItems() == 0) emit finished();
134 void DownloadManager::updateStatusMessage() {
135 QString message = tr("%n Download(s)", "", items.size());
136 emit statusMessageChanged(message);
139 QString DownloadManager::defaultDownloadFolder() {
140 // download in the Movies system folder
141 QString path = QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
142 QDir moviesDir(path);
143 if (!moviesDir.exists()) {
144 // fallback to Desktop
145 path = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
150 QString DownloadManager::currentDownloadFolder() {
152 QString path = settings.value("downloadFolder").toString();
153 if (path.isEmpty()) path = defaultDownloadFolder();