1 #include "downloadmanager.h"
2 #include "downloaditem.h"
3 #include "downloadmodel.h"
6 static DownloadManager *downloadManagerInstance = 0;
8 DownloadManager::DownloadManager(QObject *parent) :
10 downloadModel(new DownloadModel(this, this))
13 DownloadManager* DownloadManager::instance() {
14 if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
15 return downloadManagerInstance;
18 void DownloadManager::clear() {
21 updateStatusMessage();
24 int DownloadManager::activeItems() {
26 foreach (DownloadItem *item, items) {
27 if (item->status() == Downloading || item->status() == Starting) num++;
32 DownloadItem* DownloadManager::itemForVideo(Video* video) {
33 foreach (DownloadItem *item, items) {
34 if (item->getVideo()->id() == video->id()) return item;
39 void DownloadManager::addItem(Video *video) {
40 // qDebug() << __FUNCTION__ << video->title();
42 DownloadItem *item = itemForVideo(video);
44 if (item->status() == Failed || item->status() == Idle) {
45 qDebug() << "Restarting download" << video->title();
48 qDebug() << "Already downloading video" << video->title();
53 connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
54 // TODO handle signal errors
55 // connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
56 video->loadStreamUrl();
58 // see you in gotStreamUrl()
61 void DownloadManager::gotStreamUrl(QUrl url) {
63 Video *video = static_cast<Video*>(sender());
65 qDebug() << "Cannot get video in" << __FUNCTION__;
69 video->disconnect(this);
71 QString path = currentDownloadFolder();
73 // TODO ensure all chars are filename compatible
74 QString basename = video->title().simplified();
75 basename.replace('(', '[');
76 basename.replace(')', ']');
77 basename.replace('/', '-');
78 basename.replace('\\', '-');
79 QString filename = path + "/" + basename + ".mp4";
81 Video *videoCopy = video->clone();
82 DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
84 int row = items.count();
85 downloadModel->beginInsertRows(QModelIndex(), row, row);
87 downloadModel->endInsertRows();
89 // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
90 connect(item, SIGNAL(finished()), SLOT(itemFinished()));
93 updateStatusMessage();
96 void DownloadManager::itemFinished() {
97 if (activeItems() == 0) emit finished();
100 void DownloadManager::updateStatusMessage() {
101 QString message = tr("%n Download(s)", "", items.size());
102 emit statusMessageChanged(message);
105 QString DownloadManager::defaultDownloadFolder() {
106 // download in the Movies system folder
107 QString path = QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
108 QDir moviesDir(path);
109 if (!moviesDir.exists()) {
110 // fallback to Desktop
111 path = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
116 QString DownloadManager::currentDownloadFolder() {
118 QString path = settings.value("downloadFolder").toString();
119 if (path.isEmpty()) path = defaultDownloadFolder();