-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtubedownloader.cpp
executable file
·68 lines (56 loc) · 2.57 KB
/
youtubedownloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "youtubedownloader.h"
#include "byteshumanizer.h"
#include "default_values.h"
#include <QFileInfo>
#include <QFile>
#include "multidownloader.h"
YoutubeDownloader::YoutubeDownloader(const QUrl & url,const QString & out_file_name,int threads_count,QObject *parent) : DownloaderInterface(out_file_name,parent) {
downloader = new MultiDownloader(url,threads_count,out_file_name);
connect(downloader,SIGNAL(download_completed()),this,SLOT(download_completed()));
connect(downloader,SIGNAL(progress(qint64,int,qint64)),SLOT(download_progress(qint64,int,qint64)));
connect(downloader,SIGNAL(error_occured()),SLOT(download_error()));
connect(downloader,SIGNAL(location_changed(const QUrl &)),SLOT(location_changed(const QUrl &)));
}
YoutubeDownloader::~YoutubeDownloader() {
downloader->quit();
downloader->wait();
delete downloader;
}
void YoutubeDownloader::location_changed(const QUrl & url) {
int threads_count = downloader->threadsCount();
QString out_file_name = downloader->outputName();
downloader->quit();
downloader->wait();
delete downloader;
downloader = new MultiDownloader(url,threads_count,out_file_name);
connect(downloader,SIGNAL(download_completed()),this,SLOT(download_completed()));
connect(downloader,SIGNAL(progress(qint64,int,qint64)),SLOT(download_progress(qint64,int,qint64)));
connect(downloader,SIGNAL(error_occured()),SLOT(download_error()));
connect(downloader,SIGNAL(location_changed(const QUrl &)),SLOT(location_changed(const QUrl &)));
start();
}
void YoutubeDownloader::download_progress(qint64 downloaded,int percents,qint64 speed) {
emit progress(BytesHumanizer(downloaded).value(),BytesHumanizer(downloader->dataLength()).value(),percents,BytesHumanizer(speed).value());
}
void YoutubeDownloader::start() {
QString savePath = downloader->outputName()+".save";
if (QFileInfo(savePath).exists()) {
if (!downloader->continueSaved(savePath)) emit finished(tr("Problem during starting downloader"));
}
else if (!downloader->start()) emit finished(tr("Problem during starting downloader"));
}
void YoutubeDownloader::download_error() {
downloader->saveState(downloader->outputName()+".save");
emit finished(downloader->errorString());
}
void YoutubeDownloader::download_completed() {
emit finished("");
QFile(downloader->outputName()+".save").remove();
}
void YoutubeDownloader::terminate() {
downloader->terminate();
downloader->saveState(downloader->outputName()+".save");
}
bool YoutubeDownloader::isDownloading() const {
return downloader->isStarted();
}