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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
#include "scdlmgr.h"
#include "scdlthread.h"
#include "scpaths.h"
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
ScDLThread::ScDLThread(QObject *parent) : QThread(parent),
downloadedCount(0), totalCount(0)
{
connect(this, SIGNAL(runSignal()), this, SLOT(runSlot()));
}
ScDLThread::~ScDLThread()
{
}
void ScDLThread::run()
{
emit(runSignal());
}
void ScDLThread::addURL(const QUrl &url, bool overwrite, const QString& location)
{
qDebug()<<"ScDLThread::addURL:"<<url;
if (!urlOK(url))
return;
QString l(QDir::cleanPath(location));
if (!l.endsWith("/"))
l += "/";
downloadQueue.enqueue(qMakePair(url, l));
++totalCount;
}
void ScDLThread::addURLs(const QStringList &urlList, bool overwrite, const QString& location)
{
qDebug()<<"ScDLThread::addURLs:"<<urlList;
m_urlList=urlList;
QString l(QDir::cleanPath(location));
if (!l.endsWith("/"))
l += "/";
foreach (QString u, m_urlList)
{
QUrl url(u);
if (!urlOK(u))
return;
downloadQueue.enqueue(qMakePair(url, l));
++totalCount;
}
}
void ScDLThread::startDownloads()
{
if (downloadQueue.isEmpty())
{
qDebug()<<"No more downloads left";
emit finished();
return;
}
start();
}
QString ScDLThread::saveFileName(const QUrl &url, const QString& location, bool overwrite)
{
QDir d(location);
QString path = url.path();
QString basename = QFileInfo(path).fileName();
if (location.isEmpty() || !d.exists())
{
if (basename.isEmpty())
basename = "download";
basename = ScPaths::downloadDir() + basename;
}
else
{
basename = location + basename;
}
if (!overwrite && QFile::exists(basename))
{
// already exists, don't overwrite
int i = 0;
basename += '.';
while (QFile::exists(basename + QString::number(i)))
++i;
basename += QString::number(i);
}
return basename;
}
void ScDLThread::startNextDownload()
{
if (downloadQueue.isEmpty())
{
qDebug()<<downloadedCount<<"/"<<totalCount<<"files downloaded successfully";
downloadedCount=totalCount=0;
emit finished();
return;
}
QPair<QUrl, QString> urlPair=downloadQueue.dequeue();
QString filename = saveFileName(urlPair.first, urlPair.second, true);
if (filename.isEmpty())
{
qDebug()<<"File name empty for url:"<<urlPair.first.toEncoded().constData();
return;
}
output.setFileName(filename);
if (!output.open(QIODevice::WriteOnly))
{
qDebug()<<"Problem opening save file '"<<qPrintable(filename)<<"' for download '"
<<urlPair.first.toEncoded().constData()<<"': "<<qPrintable(output.errorString());
startNextDownload();
return;
}
QNetworkRequest request(urlPair.first);
currentDownload = manager.get(request);
connect(currentDownload, SIGNAL(finished()), SLOT(downloadFinished()));
connect(currentDownload, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
qDebug()<<"Downloading:"<<urlPair.first.toEncoded().constData();
}
void ScDLThread::downloadFinished()
{
output.close();
if (currentDownload->error())
qDebug()<<"Failed: "<<qPrintable(currentDownload->errorString());
else
{
printf("Succeeded.\n");
qDebug()<<"Saving file:"<<qPrintable(output.fileName());
++downloadedCount;
}
emit (received(output.fileName()));
currentDownload->deleteLater();
startNextDownload();
}
void ScDLThread::downloadReadyRead()
{
output.write(currentDownload->readAll());
}
void ScDLThread::runSlot()
{
startNextDownload();
}
bool ScDLThread::urlOK(QUrl url)
{
//TODO: Add some more URL checks
if (!url.isValid() || url.isEmpty() || url.host().isEmpty())
{
qDebug()<<"URL invalid:"<<url;
return false;
}
return true;
}
|