]> git.sur5r.net Git - minitube/blob - src/networkaccess.cpp
Fixed translation loading
[minitube] / src / networkaccess.cpp
1 #include "networkaccess.h"
2 #include "Constants.h"
3 #include <QtGui>
4
5 namespace The {
6     NetworkAccess* http();
7 }
8
9 NetworkReply::NetworkReply(QNetworkReply *networkReply) : QObject(networkReply) {
10     this->networkReply = networkReply;
11 }
12
13 void NetworkReply::metaDataChanged() {
14
15     QUrl redirection = networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
16     if (redirection.isValid()) {
17
18         qDebug() << "Redirect" << redirection;
19
20         QNetworkReply *redirectReply = The::http()->simpleGet(redirection);
21
22         setParent(redirectReply);
23         networkReply->deleteLater();
24         networkReply = redirectReply;
25
26         // handle redirections
27         connect(networkReply, SIGNAL(metaDataChanged()),
28                 this, SLOT(metaDataChanged()), Qt::QueuedConnection);
29
30         // when the request is finished we'll invoke the target method
31         connect(networkReply, SIGNAL(finished()), this, SLOT(finished()), Qt::QueuedConnection);
32
33     }
34 }
35
36 void NetworkReply::finished() {
37
38     emit finished(networkReply);
39
40     // get the HTTP response body
41     QByteArray bytes = networkReply->readAll();
42
43
44     emit data(bytes);
45
46     // bye bye my reply
47     // this will also delete this NetworkReply as the QNetworkReply is its parent
48     networkReply->deleteLater();
49 }
50
51 /* --- NetworkAccess --- */
52
53 NetworkAccess::NetworkAccess( QObject* parent) : QObject( parent ) {}
54
55 QNetworkReply* NetworkAccess::simpleGet(QUrl url) {
56
57     QNetworkAccessManager *manager = The::networkAccessManager();
58
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);
64
65     // error handling
66     connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
67             this, SLOT(error(QNetworkReply::NetworkError)));
68
69     return networkReply;
70
71 }
72
73 NetworkReply* NetworkAccess::get(const QUrl url) {
74
75     QNetworkReply *networkReply = simpleGet(url);
76     NetworkReply *reply = new NetworkReply(networkReply);
77
78     // handle redirections
79     connect(networkReply, SIGNAL(metaDataChanged()),
80             reply, SLOT(metaDataChanged()), Qt::QueuedConnection);
81
82     // when the request is finished we'll invoke the target method
83     connect(networkReply, SIGNAL(finished()), reply, SLOT(finished()), Qt::QueuedConnection);
84
85     return reply;
86
87 }
88
89 QNetworkReply* NetworkAccess::syncGet(QUrl url) {
90
91     working = true;
92
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)));
100
101     // A little trick to make this function blocking
102     while (working) {
103         // Do something else, maybe even network processing events
104         qApp->processEvents();
105     }
106
107     networkReply->deleteLater();
108     return networkReply;
109
110 }
111
112 void NetworkAccess::syncMetaDataChanged() {
113
114     QUrl redirection = networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
115     if (redirection.isValid()) {
116
117         qDebug() << "Redirect" << redirection;
118         networkReply->deleteLater();
119         syncGet(redirection);
120
121         /*
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);
129         */
130     }
131
132 }
133
134 void NetworkAccess::syncFinished() {
135     // got it!
136     working = false;
137 }
138
139 void NetworkAccess::error(QNetworkReply::NetworkError code) {
140     // get the QNetworkReply that sent the signal
141     QNetworkReply *networkReply = static_cast<QNetworkReply *>(sender());
142     if (!networkReply) {
143         qDebug() << "Cannot get sender";
144         return;
145     }
146
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());
150
151     qDebug() << "Network error" << networkReply->errorString() << code;
152     networkReply->deleteLater();
153 }
154
155 QByteArray NetworkAccess::syncGetBytes(QUrl url) {
156     return syncGet(url)->readAll();
157 }
158
159 QString NetworkAccess::syncGetString(QUrl url) {
160     return QString::fromUtf8(syncGetBytes(url));
161 }