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();
46 // this will also delete this NetworkReply as the QNetworkReply is its parent
47 networkReply->deleteLater();
50 void NetworkReply::requestError(QNetworkReply::NetworkError code) {
51 emit error(networkReply);
54 /* --- NetworkAccess --- */
56 NetworkAccess::NetworkAccess( QObject* parent) : QObject( parent ) {}
58 QNetworkReply* NetworkAccess::simpleGet(QUrl url) {
60 QNetworkAccessManager *manager = The::networkAccessManager();
62 QNetworkRequest request(url);
63 request.setRawHeader("User-Agent", Constants::USER_AGENT.toUtf8());
64 request.setRawHeader("Connection", "Keep-Alive");
65 qDebug() << "GET" << url.toString();
66 QNetworkReply *networkReply = manager->get(request);
69 connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
70 this, SLOT(error(QNetworkReply::NetworkError)));
76 NetworkReply* NetworkAccess::get(const QUrl url) {
78 QNetworkReply *networkReply = simpleGet(url);
79 NetworkReply *reply = new NetworkReply(networkReply);
81 // handle redirections
82 connect(networkReply, SIGNAL(metaDataChanged()),
83 reply, SLOT(metaDataChanged()), Qt::QueuedConnection);
86 connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
87 reply, SLOT(requestError(QNetworkReply::NetworkError)));
89 // when the request is finished we'll invoke the target method
90 connect(networkReply, SIGNAL(finished()), reply, SLOT(finished()), Qt::QueuedConnection);
96 QNetworkReply* NetworkAccess::syncGet(QUrl url) {
100 networkReply = simpleGet(url);
101 connect(networkReply, SIGNAL(metaDataChanged()),
102 this, SLOT(syncMetaDataChanged()), Qt::QueuedConnection);
103 connect(networkReply, SIGNAL(finished()),
104 this, SLOT(syncFinished()), Qt::QueuedConnection);
105 connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
106 this, SLOT(error(QNetworkReply::NetworkError)));
108 // A little trick to make this function blocking
110 // Do something else, maybe even network processing events
111 qApp->processEvents();
114 networkReply->deleteLater();
119 void NetworkAccess::syncMetaDataChanged() {
121 QUrl redirection = networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
122 if (redirection.isValid()) {
124 qDebug() << "Redirect" << redirection;
125 networkReply->deleteLater();
126 syncGet(redirection);
129 QNetworkAccessManager *manager = The::networkAccessManager();
130 networkReply->deleteLater();
131 networkReply = manager->get(QNetworkRequest(redirection));
132 connect(networkReply, SIGNAL(metaDataChanged()),
133 this, SLOT(metaDataChanged()), Qt::QueuedConnection);
134 connect(networkReply, SIGNAL(finished()),
135 this, SLOT(finished()), Qt::QueuedConnection);
141 void NetworkAccess::syncFinished() {
146 void NetworkAccess::error(QNetworkReply::NetworkError code) {
147 // get the QNetworkReply that sent the signal
148 QNetworkReply *networkReply = static_cast<QNetworkReply *>(sender());
150 qDebug() << "Cannot get sender";
154 // report the error in the status bar
155 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
156 if (mainWindow) mainWindow->statusBar()->showMessage(
157 tr("Network error: %1").arg(networkReply->errorString()));
159 qDebug() << "Network error:" << networkReply->errorString() << code;
161 networkReply->deleteLater();
164 QByteArray NetworkAccess::syncGetBytes(QUrl url) {
165 return syncGet(url)->readAll();
168 QString NetworkAccess::syncGetString(QUrl url) {
169 return QString::fromUtf8(syncGetBytes(url));