29 #include "arcsdownloader.h"
30 #include <QNetworkReply>
31 #include <QNetworkAccessManager>
32 #include <QNetworkRequest>
35 #include <QAuthenticator>
36 #include <QSslCertificate>
38 #include <getpasswd.h>
42 ARCSDownloader::ARCSDownloader(QObject *parent) :
45 manager =
new QNetworkAccessManager(
this);
46 eventLoop =
new QEventLoop(
this);
47 connect(manager,SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
48 this,SLOT(authenticateToProxy(QNetworkProxy,QAuthenticator*)));
49 connect(manager,SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
50 this,SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
51 connect(manager,SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
52 this,SLOT(authenticateToSite(QNetworkReply*,QAuthenticator*)));
53 connect(manager,SIGNAL(finished(QNetworkReply*)),
this,SLOT(replyFinished(QNetworkReply*)));
58 void ARCSDownloader::replyFinished(QNetworkReply *reply)
63 QString urlString = reply->request().url().toString();
65 if (reply->error() != QNetworkReply::NoError)
67 std::cerr <<
"Downloader has encountered an error: "
68 << qPrintable(reply->errorString()) << std::endl;
69 std::cerr <<
"on url: " << qPrintable(urlString) << std::endl;
76 if (!file.open(QIODevice::WriteOnly))
78 std::cerr <<
"Failed to write down file: " << qPrintable(filename) << std::endl;
84 file.write(reply->readAll());
88 void ARCSDownloader::authenticateToSite(QNetworkReply * reply, QAuthenticator * auth)
90 std::cout <<
"The site you try to connect requires you to authenticate yourself." << std::endl;
94 void ARCSDownloader::authenticateToProxy(
const QNetworkProxy &proxy, QAuthenticator *auth)
96 std::cout <<
"Your network proxy requires you to authenticate yourself." << std::endl;
100 void ARCSDownloader::sslErrors(QNetworkReply *reply,
const QList<QSslError> &errors)
102 std::cerr <<
"Your ssl connection has encountered the following errors " << std::endl;
103 for (
int i=0; i < errors.count(); i++)
105 std::cerr <<
" " << qPrintable(errors[i].errorString()) << std::endl;
106 QSslCertificate cert = errors[i].certificate();
107 std::cerr <<
" Certificate issued by " << qPrintable(cert.subjectInfo(QSslCertificate::CommonName))
108 <<
", " << qPrintable(cert.subjectInfo(QSslCertificate::Organization))
109 <<
", " << qPrintable(cert.subjectInfo(QSslCertificate::OrganizationalUnitName))
110 <<
", " << qPrintable(cert.subjectInfo(QSslCertificate::LocalityName))
111 <<
", " << qPrintable(cert.subjectInfo(QSslCertificate::CountryName))
115 std::cerr <<
"Do you wish to continue (y/N) ?";
120 reply->ignoreSslErrors();
121 cachedSslErrors = errors;
125 std::cerr <<
"Ssl transaction has been dropped" << std::endl;
130 void ARCSDownloader::setProgress(qint64 received, qint64 total)
132 qint64 frac=received*40/total;
133 qint64 percent = received*100/total;
135 std::cout <<
"\rDownloading " << qPrintable(filename) <<
"\t[";
136 for (
int i=0; i<40; i++)
148 std::cout <<
"] " << percent <<
"%\t" << std::flush;
149 if (frac==40) std::cout << std::endl;
152 void ARCSDownloader::authenticate(QAuthenticator *auth)
154 std::cout <<
"Username: " ;
158 pwd = getpasswd(
"Password: ");
159 auth->setUser(uname.data());
160 auth->setPassword(pwd.data());
163 void ARCSDownloader::download(QString s)
165 QNetworkReply* reply = manager->get(QNetworkRequest(s));
166 reply->ignoreSslErrors(cachedSslErrors);
167 filename = QFileInfo(reply->request().url().path()).fileName();
168 std::cout <<
"Trying to download " << qPrintable(filename) << std::endl;
169 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(setProgress(qint64,qint64)));