21
21
#include " qgsmessagelog.h"
22
22
#include " qgsexpression.h"
23
23
#include " qgslogger.h"
24
+ #include " qgsnetworkcontentfetcher.h"
24
25
25
26
#include < QCoreApplication>
26
27
#include < QPainter>
@@ -108,58 +109,6 @@ void QgsComposerHtml::setEvaluateExpressions( bool evaluateExpressions )
108
109
loadHtml ();
109
110
}
110
111
111
- QString QgsComposerHtml::fetchHtml ( QUrl url )
112
- {
113
- QUrl nextUrlToFetch = url;
114
- QNetworkReply* reply = 0 ;
115
-
116
- // loop until fetched valid html
117
- while ( 1 )
118
- {
119
- // set contents
120
- QNetworkRequest request ( nextUrlToFetch );
121
- reply = QgsNetworkAccessManager::instance ()->get ( request );
122
- connect ( reply, SIGNAL ( finished () ), this , SLOT ( frameLoaded () ) );
123
- // pause until HTML fetch
124
- mLoaded = false ;
125
- while ( !mLoaded )
126
- {
127
- qApp->processEvents ();
128
- }
129
-
130
- if ( reply->error () != QNetworkReply::NoError )
131
- {
132
- QgsMessageLog::logMessage ( tr ( " HTML fetch %1 failed with error %2" ).arg ( reply->url ().toString () ).arg ( reply->errorString () ) );
133
- reply->deleteLater ();
134
- return QString ();
135
- }
136
-
137
- QVariant redirect = reply->attribute ( QNetworkRequest::RedirectionTargetAttribute );
138
- if ( redirect.isNull () )
139
- {
140
- // no error or redirect, got target
141
- break ;
142
- }
143
-
144
- // redirect, so fetch redirect target
145
- nextUrlToFetch = redirect.toUrl ();
146
- reply->deleteLater ();
147
- }
148
-
149
- QVariant status = reply->attribute ( QNetworkRequest::HttpStatusCodeAttribute );
150
- if ( !status.isNull () && status.toInt () >= 400 )
151
- {
152
- QgsMessageLog::logMessage ( tr ( " HTML fetch %1 failed with error %2" ).arg ( reply->url ().toString () ).arg ( status.toString () ) );
153
- reply->deleteLater ();
154
- return QString ();
155
- }
156
-
157
- QByteArray array = reply->readAll ();
158
- reply->deleteLater ();
159
- mFetchedHtml = QString ( array );
160
- return mFetchedHtml ;
161
- }
162
-
163
112
void QgsComposerHtml::loadHtml ()
164
113
{
165
114
if ( !mWebPage )
@@ -195,6 +144,7 @@ void QgsComposerHtml::loadHtml()
195
144
{
196
145
loadedHtml = mFetchedHtml ;
197
146
}
147
+
198
148
break ;
199
149
}
200
150
case QgsComposerHtml::ManualHtml:
@@ -210,7 +160,7 @@ void QgsComposerHtml::loadHtml()
210
160
211
161
mLoaded = false ;
212
162
// set html, using the specified url as base if in Url mode
213
- mWebPage ->mainFrame ()->setHtml ( loadedHtml, mContentMode == QgsComposerHtml::Url ? QUrl ( mLastFetchedUrl ) : QUrl () );
163
+ mWebPage ->mainFrame ()->setHtml ( loadedHtml, mContentMode == QgsComposerHtml::Url ? QUrl ( mActualFetchedUrl ) : QUrl () );
214
164
215
165
// set user stylesheet
216
166
QWebSettings* settings = mWebPage ->settings ();
@@ -279,6 +229,24 @@ void QgsComposerHtml::renderCachedImage()
279
229
painter.end ();
280
230
}
281
231
232
+ QString QgsComposerHtml::fetchHtml ( QUrl url )
233
+ {
234
+ QgsNetworkContentFetcher fetcher;
235
+ // pause until HTML fetch
236
+ mLoaded = false ;
237
+ fetcher.fetchContent ( url );
238
+ connect ( &fetcher, SIGNAL ( finished () ), this , SLOT ( frameLoaded () ) );
239
+
240
+ while ( !mLoaded )
241
+ {
242
+ qApp->processEvents ();
243
+ }
244
+
245
+ mFetchedHtml = fetcher.contentAsString ();
246
+ mActualFetchedUrl = fetcher.reply ()->url ().toString ();
247
+ return mFetchedHtml ;
248
+ }
249
+
282
250
QSizeF QgsComposerHtml::totalSize () const
283
251
{
284
252
return mSize ;
0 commit comments