1 #include "networkaccess.h"
9 NetworkReply::NetworkReply(QNetworkReply *networkReply) : QObject(networkReply) {
10 this->networkReply = networkReply;
13 void NetworkReply::metaDataChanged() {
15 QUrl redirection = networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
16 if (redirection.isValid()) {
18 qDebug() << "Redirect" << redirection;
20 QNetworkReply *redirectReply = The::http()->simpleGet(redirection);
22 setParent(redirectReply);
23 networkReply->deleteLater();
24 networkReply = redirectReply;
26 // handle redirections
27 connect(networkReply, SIGNAL(metaDataChanged()),
28 this, SLOT(metaDataChanged()), Qt::QueuedConnection);
30 // when the request is finished we'll invoke the target method
31 connect(networkReply, SIGNAL(finished()), this, SLOT(finished()), Qt::QueuedConnection);
36 void NetworkReply::finished() {
38 emit finished(networkReply);
40 // get the HTTP response body
41 QByteArray bytes = networkReply->readAll();
47 // this will also delete this NetworkReply as the QNetworkReply is its parent
48 networkReply->deleteLater();
51 /* --- NetworkAccess --- */
53 NetworkAccess::NetworkAccess( QObject* parent) : QObject( parent ) {}
55 QNetworkReply* NetworkAccess::simpleGet(QUrl url) {
57 QNetworkAccessManager *manager = The::networkAccessManager();
59 QNetworkRequest request(url);
60 request.setRawHeader("User-Agent", Constants::USER_AGENT.toUtf8());
61 request.setRawHeader("Connection", "Keep-Alive");
62 qDebug() << "GET" << url.toString();
63 QNetworkReply *networkReply = manager->get(request);
66 connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
67 this, SLOT(error(QNetworkReply::NetworkError)));
73 NetworkReply* NetworkAccess::get(const QUrl url) {
75 QNetworkReply *networkReply = simpleGet(url);
76 NetworkReply *reply = new NetworkReply(networkReply);
78 // handle redirections
79 connect(networkReply, SIGNAL(metaDataChanged()),
80 reply, SLOT(metaDataChanged()), Qt::QueuedConnection);
82 // when the request is finished we'll invoke the target method
83 connect(networkReply, SIGNAL(finished()), reply, SLOT(finished()), Qt::QueuedConnection);
89 QNetworkReply* NetworkAccess::syncGet(QUrl url) {
93 networkReply = simpleGet(url);
94 connect(networkReply, SIGNAL(metaDataChanged()),
95 this, SLOT(syncMetaDataChanged()), Qt::QueuedConnection);
96 connect(networkReply, SIGNAL(finished()),
97 this, SLOT(syncFinished()), Qt::QueuedConnection);
98 connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
99 this, SLOT(error(QNetworkReply::NetworkError)));
101 // A little trick to make this function blocking
103 // Do something else, maybe even network processing events
104 qApp->processEvents();
107 networkReply->deleteLater();
112 void NetworkAccess::syncMetaDataChanged() {
114 QUrl redirection = networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
115 if (redirection.isValid()) {
117 qDebug() << "Redirect" << redirection;
118 networkReply->deleteLater();
119 syncGet(redirection);
122 QNetworkAccessManager *manager = The::networkAccessManager();
123 networkReply->deleteLater();
124 networkReply = manager->get(QNetworkRequest(redirection));
125 connect(networkReply, SIGNAL(metaDataChanged()),
126 this, SLOT(metaDataChanged()), Qt::QueuedConnection);
127 connect(networkReply, SIGNAL(finished()),
128 this, SLOT(finished()), Qt::QueuedConnection);
134 void NetworkAccess::syncFinished() {
139 void NetworkAccess::error(QNetworkReply::NetworkError code) {
140 // get the QNetworkReply that sent the signal
141 QNetworkReply *networkReply = static_cast<QNetworkReply *>(sender());
143 qDebug() << "Cannot get sender";
147 // report the error in the status bar
148 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
149 if (mainWindow) mainWindow->statusBar()->showMessage(networkReply->errorString());
151 qDebug() << "Network error" << networkReply->errorString() << code;\
152 networkReply->deleteLater();
155 QByteArray NetworkAccess::syncGetBytes(QUrl url) {
156 return syncGet(url)->readAll();
159 QString NetworkAccess::syncGetString(QUrl url) {
160 return QString::fromUtf8(syncGetBytes(url));