summaryrefslogtreecommitdiffstats
path: root/src/downloadManager.cpp
blob: dca99d370278fb973021515944ff9e5eac901ae6 (plain) (blame)
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
#include "downloadManager.h"
#include <QFileInfo>
#include <QByteArray>

int downloadManager::downloaded = 0;
// ----------------------------------------------------------------------------------------
downloadManager::downloadManager()
{
	qnam = new QNetworkAccessManager();
	dip = false;
	downloadDir = QDir(downloadPath);
	/* Check if downloadPath exists, if not create it. */
	if (!downloadDir.exists()){
		if (debug) qDebug() << "Download directory: " << downloadDir.path() << "doesn't exist.";
		QDir::current().mkdir(downloadPath);
		if (downloadDir.exists() && debug)
			qDebug() << "Created download directory: " << downloadDir.path();
	}
	else if (debug) qDebug() << "Download directory: " << downloadDir.path() << "exists.";
}
// ----------------------------------------------------------------------------------------
void downloadManager::downloadFile(QString& filename){
	if (debug) qDebug() << "Received downloadFile signal for:" << filename;
	QUrl fileUrl;
	fileUrl = baseURL.resolved(QUrl(filename));
	if (debug) qDebug() << "fileUrl: " << fileUrl;
	this->processDownloadRequest(fileUrl);
}
// ----------------------------------------------------------------------------------------
void downloadManager::downloadFile(QUrl& fileUrl){
	if (debug) qDebug() << "Received downloadFile signal for:" << fileUrl;
	this->processDownloadRequest(fileUrl);
}
// ----------------------------------------------------------------------------------------
void downloadManager::processDownloadRequest(QUrl& url)
{
	if (url.isEmpty()){
		if (debug) qDebug() << "No URL specified for download.";
		return;
	}
	/* If download in progress, enqueue file and return. */
	if (dip)
	{
		if (debug) qDebug() << "Download in progress! Enqueueing:" << url.toString()
				 << "(" << dlQ.size() << "in queue)";
		dlQ.enqueue(url);
		return;
	}
	/* No running downloads: enqueue and start next download. */
	dlQ.enqueue(url);
	if (debug) qDebug() << "Enqueueing:" << url.toString() << endl;
	startNextDownload();
}
// ----------------------------------------------------------------------------------------
void downloadManager::startNextDownload()
{

	if (dlQ.isEmpty())
	{
		emit downloadQueueEmpty();
		if (debug) qDebug() << "Download manager ready. (1)";
		return;
	}
	if (debug) qDebug() << "Starting next download: " << dlQ.head().toString()
			 << "(" << dlQ.size() << "in queue.)";

	/* Dequeue next URL to download. */
	QUrl url = dlQ.dequeue();

	/* Get temporary filename from URL. */
	QString tmp = url.path();
	tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1);
	if (debug) qDebug() << "Extracted " << tmp << "from " << url.toString();

	outfile.setFileName(downloadPath + "/" + tmp);
	if (!outfile.open(QIODevice::WriteOnly))
	{
		if (debug) qDebug() << "Couldn't open file! Skipping...";
		return;
	}

	QNetworkRequest request(url);
	currentDownload = qnam->get(request);
	if (currentDownload->error() != QNetworkReply::NoError)
	{
		if (debug) qDebug() << "Network reply error, skipping download...";
		return;
	}

	lastProgress = 0;
	dip = true;
	QObject::connect(currentDownload, SIGNAL(readyRead()), this, SLOT(downloadReady()));
	QObject::connect(currentDownload, SIGNAL(downloadProgress(qint64, qint64)),
					 this, SLOT(downloadProgress(qint64, qint64)));
	QObject::connect(currentDownload, SIGNAL(finished()), this, SLOT(downloadFinished()));
}
// ----------------------------------------------------------------------------------------
//                                   Private slots
// ----------------------------------------------------------------------------------------
void downloadManager::downloadReady()
{ 
	/* Data ready, save it */
	outfile.write(currentDownload->readAll());
}
// ----------------------------------------------------------------------------------------
void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal)
{
	/* Update progress only if difference higher than the updateInterval setting */
	int currentProgress = ((bytesIn * 100) / bytesTotal);
	if (currentProgress - lastProgress > updateInterval){
		lastProgress = currentProgress;
		emit updateProgress(currentDownload->url().toString(), currentProgress);
		if (debug) qDebug() << "Download progress of " << currentDownload->url().toString()
				 << ": " << bytesIn << "/" << bytesTotal << "(" << currentProgress << "\%)";
	}
	return;
}
// ----------------------------------------------------------------------------------------
void downloadManager::downloadFinished()
{
	/* Header filename fetching & renaming, old-ish
	const QByteArray cd = "Content-Disposition";
	QByteArray cdc = currentDownload->rawHeader(cd);
	int x = cdc.indexOf("filename=\"") + 10;
	cdc.remove(0, x).chop(1);
	if (!cdc.isEmpty())
		currentTargetFilename = cdc;
	else
		currentTargetFilename = QString("download.\%1").arg(downloaded);

	QString tmp = outfile.fileName();
	tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1);
	qDebug() << "Trying to rename " << tmp << " to --> " << currentTargetFilename;

	if (outfile.rename(downloadPath + "/" + currentTargetFilename)) {
		if (debug) qDebug() << "Renamed file!";
	}
	else {
		if (debug) qDebug() << "Failure to rename file!";
	}
	*/

	outfile.close();
	currentDownload->deleteLater();
	downloaded++;
	dip = false;
	if (debug) qDebug() << "Download of " << currentDownload->url().toString()
							<< "finished. (dlcount = "<< downloaded << ")";
	if (dlQ.isEmpty()){
		emit downloadQueueEmpty();
		if (debug) qDebug() << "Download manager ready. (2)";

		return;
	}
	startNextDownload();
}