##// END OF EJS Templates
Really basic implementation of Downloader which might replace current...
Really basic implementation of Downloader which might replace current NetworkController It is currently really basic, it only does synchronous DLs with or without authentication. It is written to isolate as much as possible Qt Network classes. Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>

File last commit:

r1342:91cbf8a85daf
r1342:91cbf8a85daf
Show More
Downloader.cpp
69 lines | 2.0 KiB | text/x-c | CppLexer
#include <Network/Downloader.h>
#include <memory>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QAuthenticator>
#include <QVariant>
#include <QHash>
#include <QPair>
#include <QCoreApplication>
class Downloader::p_Downloader
{
using login_pair=QPair<QString,QString>;
QNetworkAccessManager manager;
QHash<QString,login_pair> auth;
public:
explicit p_Downloader()
{
auto login_bambda = [this](QNetworkReply * reply, QAuthenticator * authenticator)
{
if(auth.contains(reply->url().toString()))
{
auto login = auth[reply->url().toString()];
authenticator->setUser(login.first);
authenticator->setPassword(login.second);
}
};
QObject::connect(&manager, &QNetworkAccessManager::authenticationRequired, login_bambda);
}
Response get(const QString& url, const QString &user="", const QString &passwd="")
{
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "SciQLop 1.0");
if(user!="" and passwd!="")
{
//might grow quickly since we can have tons of URLs for the same host
auth[url]=login_pair(user,passwd);
QString login = "Basic "+user+":"+passwd;
request.setRawHeader("Authorization",login.toLocal8Bit());
}
QNetworkReply *reply = manager.get(request);
while (!reply->isFinished())
QCoreApplication::processEvents();
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
return Response(reply->readAll(), status_code.toInt());
}
};
Response Downloader::get(const QString &url)
{
return Downloader::instance().impl->get(url);
}
Response Downloader::get(const QString &url, const QString &user, const QString &passwd)
{
return Downloader::instance().impl->get(url, user, passwd);
}
Downloader::Downloader()
:impl(spimpl::make_unique_impl<p_Downloader>())
{
}