-
Notifications
You must be signed in to change notification settings - Fork 0
/
downloadsmanager.cpp
107 lines (91 loc) · 2.57 KB
/
downloadsmanager.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <QDir>
#include "downloadsmanager.h"
#include "filedownloader.h"
#include "storage.h"
DownloadsManager::DownloadsManager(QObject *parent) :
QObject(parent)
{
}
DownloadsManager::~DownloadsManager()
{
Storage::writeDownloadDir(downloadDir.path());
}
void DownloadsManager::initManager()
{
// TODO: Get Download locaation from storage
QString dir = Storage::getDownloadDir();
if (dir!="")
downloadDir.setPath(dir);
else
downloadDir.setPath(downloadDir.currentPath());
qDebug() << downloadDir.path();
emit downloadDirChanged(downloadDir.path());
}
void DownloadsManager::changeDownloadDirTo(QString newDir)
{
downloadDir.setPath(newDir);
emit downloadDirChanged(downloadDir.path());
}
void DownloadsManager::downloadFile(QString token, QString id, QString fileName)
{
if (!downloadDir.exists(fileName)) {
FileDownloader* t = new FileDownloader(fileName, this);
connect(t, SIGNAL(fileDownloaded()), this, SLOT(fileDownloaded()));
t->startDownload(token, id);
}
else fileDownloaded();
}
void DownloadsManager::changeToDir(QString folderName)
{
folderName.replace(QRegExp("[<|>|:|\"|/|\\|||?|*]"), "");
if (!downloadDir.cd(folderName)) {
downloadDir.mkdir(folderName);
downloadDir.cd(folderName);
}
}
void DownloadsManager::removeDir(QString dirName)
{
dirName.replace(QRegExp("[<|>|:|\"|/|\\|||?|*]"), "");
changeToDir(dirName);
foreach (QString entry, downloadDir.entryList(QDir::NoDotAndDotDot|QDir::Dirs|QDir::Files))
if (!downloadDir.remove(entry))
removeDir(entry);
upDir();
downloadDir.rmdir(dirName);
}
void DownloadsManager::removeFile(QString fileName)
{
downloadDir.remove(fileName);
}
void DownloadsManager::stackDownload(QString id, QString fileName)
{
if (!downloadDir.exists(fileName))
downloads.append(QPair<QString, QString>(id, downloadDir.path()+"/"+fileName));
}
void DownloadsManager::executeDownloads(QString token)
{
numDownloads = downloads.size();
downloadsDone = 0;
if (numDownloads==0) {
numDownloads++;
emit fileDownloaded();
}
else
for (;!downloads.empty();downloads.pop_back()) {
qDebug() << downloads.last().second;
downloadFile(token, downloads.last().first, downloads.last().second);
}
}
void DownloadsManager::upDir()
{
downloadDir.cdUp();
}
QString DownloadsManager::dir()
{
return downloadDir.path();
}
void DownloadsManager::fileDownloaded()
{
downloadsDone++;
emit progress(downloadsDone*100/numDownloads);
}