]> git.sur5r.net Git - minitube/blob - src/downloadmanager.cpp
011b5bb005001fd09b12d75fcddb35b4c93057f2
[minitube] / src / downloadmanager.cpp
1 #include "downloadmanager.h"
2 #include "downloaditem.h"
3 #include "downloadmodel.h"
4 #include "video.h"
5
6 static DownloadManager *downloadManagerInstance = 0;
7
8 DownloadManager::DownloadManager(QObject *parent) :
9         QObject(parent),
10         downloadModel(new DownloadModel(this, this))
11 { }
12
13 DownloadManager* DownloadManager::instance() {
14     if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
15     return downloadManagerInstance;
16 }
17
18 void DownloadManager::clear() {
19     qDeleteAll(items);
20     items.clear();
21     updateStatusMessage();
22 }
23
24 int DownloadManager::activeItems() {
25     int num = 0;
26     foreach (DownloadItem *item, items) {
27         if (item->status() == Downloading || item->status() == Starting) num++;
28     }
29     return num;
30 }
31
32 DownloadItem* DownloadManager::itemForVideo(Video* video) {
33     foreach (DownloadItem *item, items) {
34         if (item->getVideo()->id() == video->id()) return item;
35     }
36     return 0;
37 }
38
39 void DownloadManager::addItem(Video *video) {
40     // qDebug() << __FUNCTION__ << video->title();
41
42     DownloadItem *item = itemForVideo(video);
43     if (item != 0) {
44         if (item->status() == Failed || item->status() == Idle) {
45             qDebug() << "Restarting download" << video->title();
46             item->tryAgain();
47         } else {
48             qDebug() << "Already downloading video" << video->title();
49         }
50         return;
51     }
52
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();
57
58     // see you in gotStreamUrl()
59 }
60
61 void DownloadManager::gotStreamUrl(QUrl url) {
62
63     Video *video = static_cast<Video*>(sender());
64     if (!video) {
65         qDebug() << "Cannot get video in" << __FUNCTION__;
66         return;
67     }
68
69     video->disconnect(this);
70
71     QString path = currentDownloadFolder();
72
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";
80
81     Video *videoCopy = video->clone();
82     DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
83
84     int row = items.count();
85     downloadModel->beginInsertRows(QModelIndex(), row, row);
86     items.append(item);
87     downloadModel->endInsertRows();
88
89     // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
90     connect(item, SIGNAL(finished()), SLOT(itemFinished()));
91     item->start();
92
93     updateStatusMessage();
94 }
95
96 void DownloadManager::itemFinished() {
97     if (activeItems() == 0) emit finished();
98 }
99
100 void DownloadManager::updateStatusMessage() {
101     QString message = tr("%n Download(s)", "", items.size());
102     emit statusMessageChanged(message);
103 }
104
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);
112     }
113     return path;
114 }
115
116 QString DownloadManager::currentDownloadFolder() {
117     QSettings settings;
118     QString path = settings.value("downloadFolder").toString();
119     if (path.isEmpty()) path = defaultDownloadFolder();
120     return path;
121 }