1 #include "downloadmanager.h"
2 #include "downloaditem.h"
3 #include "downloadmodel.h"
6 #include "mainwindow.h"
8 #include "activation.h"
14 static DownloadManager *downloadManagerInstance = 0;
16 DownloadManager::DownloadManager(QWidget *parent) :
18 downloadModel(new DownloadModel(this, this))
21 DownloadManager* DownloadManager::instance() {
22 if (!downloadManagerInstance) downloadManagerInstance = new DownloadManager();
23 return downloadManagerInstance;
26 void DownloadManager::clear() {
29 updateStatusMessage();
32 int DownloadManager::activeItems() {
34 foreach (DownloadItem *item, items) {
35 if (item->status() == Downloading || item->status() == Starting) num++;
40 DownloadItem* DownloadManager::itemForVideo(Video* video) {
41 foreach (DownloadItem *item, items) {
42 if (item->getVideo()->id() == video->id()) return item;
47 void DownloadManager::addItem(Video *video) {
48 // qDebug() << __FUNCTION__ << video->title();
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.")
59 msgBox.setModal(true);
60 // make it a "sheet" on the Mac
61 msgBox.setWindowModality(Qt::WindowModal);
63 msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
64 QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
68 if (msgBox.clickedButton() == buyButton) {
69 MainWindow::instance()->showActivationView();
77 DownloadItem *item = itemForVideo(video);
79 if (item->status() == Failed || item->status() == Idle) {
80 qDebug() << "Restarting download" << video->title();
83 qDebug() << "Already downloading video" << video->title();
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();
93 // see you in gotStreamUrl()
96 void DownloadManager::gotStreamUrl(QUrl url) {
98 Video *video = static_cast<Video*>(sender());
100 qDebug() << "Cannot get video in" << __FUNCTION__;
104 video->disconnect(this);
106 QString path = currentDownloadFolder();
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();
123 QString filename = path + "/" + basename + ".mp4";
125 Video *videoCopy = video->clone();
126 DownloadItem *item = new DownloadItem(videoCopy, url, filename, this);
128 downloadModel->beginInsertRows(QModelIndex(), 0, 0);
130 downloadModel->endInsertRows();
132 // connect(item, SIGNAL(statusChanged()), SLOT(updateStatusMessage()));
133 connect(item, SIGNAL(finished()), SLOT(itemFinished()));
136 updateStatusMessage();
139 void DownloadManager::itemFinished() {
140 if (activeItems() == 0) emit finished();
142 if (mac::canNotify()) {
143 DownloadItem *item = static_cast<DownloadItem*>(sender());
145 qDebug() << "Cannot get item in" << __FUNCTION__;
148 Video *video = item->getVideo();
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);
158 void DownloadManager::updateStatusMessage() {
159 QString message = tr("%n Download(s)", "", items.size());
160 emit statusMessageChanged(message);
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);
171 QDir desktopDir(path);
172 if (!desktopDir.exists()) {
174 path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
180 QString DownloadManager::currentDownloadFolder() {
182 QString path = settings.value("downloadFolder").toString();
183 if (path.isEmpty()) path = defaultDownloadFolder();