]> git.sur5r.net Git - minitube/blob - src/networkaccess.cpp
Japanese translation credits
[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     emit data(bytes);
44
45     // bye bye my reply
46     // this will also delete this NetworkReply as the QNetworkReply is its parent
47     networkReply->deleteLater();
48 }
49
50 void NetworkReply::requestError(QNetworkReply::NetworkError code) {
51     emit error(networkReply);
52 }
53
54 /* --- NetworkAccess --- */
55
56 NetworkAccess::NetworkAccess( QObject* parent) : QObject( parent ) {}
57
58 QNetworkReply* NetworkAccess::simpleGet(QUrl url) {
59
60     QNetworkAccessManager *manager = The::networkAccessManager();
61
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);
67
68     // error handling
69     connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
70             this, SLOT(error(QNetworkReply::NetworkError)));
71
72     return networkReply;
73
74 }
75
76 NetworkReply* NetworkAccess::get(const QUrl url) {
77
78     QNetworkReply *networkReply = simpleGet(url);
79     NetworkReply *reply = new NetworkReply(networkReply);
80
81     // handle redirections
82     connect(networkReply, SIGNAL(metaDataChanged()),
83             reply, SLOT(metaDataChanged()), Qt::QueuedConnection);
84
85     // error signal
86     connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
87             reply, SLOT(requestError(QNetworkReply::NetworkError)));
88
89     // when the request is finished we'll invoke the target method
90     connect(networkReply, SIGNAL(finished()), reply, SLOT(finished()), Qt::QueuedConnection);
91
92     return reply;
93
94 }
95
96 QNetworkReply* NetworkAccess::syncGet(QUrl url) {
97
98     working = true;
99
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)));
107
108     // A little trick to make this function blocking
109     while (working) {
110         // Do something else, maybe even network processing events
111         qApp->processEvents();
112     }
113
114     networkReply->deleteLater();
115     return networkReply;
116
117 }
118
119 void NetworkAccess::syncMetaDataChanged() {
120
121     QUrl redirection = networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
122     if (redirection.isValid()) {
123
124         qDebug() << "Redirect" << redirection;
125         networkReply->deleteLater();
126         syncGet(redirection);
127
128         /*
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);
136         */
137     }
138
139 }
140
141 void NetworkAccess::syncFinished() {
142     // got it!
143     working = false;
144 }
145
146 void NetworkAccess::error(QNetworkReply::NetworkError code) {
147     // get the QNetworkReply that sent the signal
148     QNetworkReply *networkReply = static_cast<QNetworkReply *>(sender());
149     if (!networkReply) {
150         qDebug() << "Cannot get sender";
151         return;
152     }
153
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()));
158
159     qDebug() << "Network error:" << networkReply->errorString() << code;
160
161     networkReply->deleteLater();
162 }
163
164 QByteArray NetworkAccess::syncGetBytes(QUrl url) {
165     return syncGet(url)->readAll();
166 }
167
168 QString NetworkAccess::syncGetString(QUrl url) {
169     return QString::fromUtf8(syncGetBytes(url));
170 }