-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadManager.cpp
executable file
·123 lines (106 loc) · 3.45 KB
/
DownloadManager.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "DownloadManager.h"
DownloadManager::DownloadManager()
{
//Creates a connection from signal in sender object to functor, and returns a handle to the connection
connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(downloadFinished(QNetworkReply*)));
}
void DownloadManager::doDownload(const QUrl &url)
{
QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request);
currentDownloads.append(reply);
}
QString DownloadManager::saveFileName(const QUrl &url)
{
/*
QString path = url.path();
QString basename = QFileInfo(path).fileName();
if (basename.isEmpty())
basename = "stockinfo";
// already exists, don't overwrite
if (QFile::exists(basename))
{
//removes the ".txt" from the end of the basename
basename.chop(4);
//Increments through to find an available file name
int i = 1;
while (QFile::exists(basename + QString::number(i) + ".txt"))
{
++i;
}
//Adds on the proper number and file extension to the file
basename += QString::number(i) + ".txt";
}
return basename;
*/
return "stockInfo.txt";
}
bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
{
fprintf(stderr, "Could not open %s for writing: %s\n",
qPrintable(filename),
qPrintable(file.errorString()));
return false;
}
file.write(data->readAll());
file.close();
return true;
}
//Not used, but kept in case we want to simulate multi-threading
void DownloadManager::execute()
{
QStringList args = QCoreApplication::instance()->arguments();
args.takeFirst(); // skip the first argument, which is the program's name
if (args.isEmpty())
{
/*
printf("Qt Download example - downloads all URLs in parallel\n"
"Usage: download url1 [url2... urlN]\n"
"\n"
"Downloads the URLs passed in the command-line to the local directory\n"
"If the target file already exists, a .0, .1, .2, etc. is appended to\n"
"differentiate.\n");
*/
QCoreApplication::instance()->quit();
return;
}
foreach (QString arg, args) {
QUrl url = QUrl::fromEncoded(arg.toLocal8Bit());
doDownload(url);
}
}
void DownloadManager::downloadFinished(QNetworkReply *reply)
{
QUrl url = reply->url();
if (reply->error())
{
fprintf(stderr, "Download of %s failed: %s\n",
url.toEncoded().constData(),
qPrintable(reply->errorString()));
fflush(stderr);
}
else
{
QString filename = saveFileName(url);
//Does the actually downloading
if (saveToDisk(filename, reply))
{
//Prints out status of the download
fprintf(stdout, "Download of %s succeded (saved to %s)\n",
url.toEncoded().constData(),
qPrintable(filename));
fflush(stdout);
}
}
//cout << currentDownloads.empty() << endl;
currentDownloads.removeAll(reply);
//cout << currentDownloads.empty() << endl;
reply->deleteLater();
}
bool DownloadManager::canProceed()
{
return (currentDownloads.empty() == 1);
}