|
18 | 18 | #include "qgscomposition.h"
|
19 | 19 | #include "qgsaddremovemultiframecommand.h"
|
20 | 20 | #include "qgsnetworkaccessmanager.h"
|
| 21 | +#include "qgsmessagelog.h" |
21 | 22 |
|
22 | 23 | #include <QCoreApplication>
|
23 | 24 | #include <QPainter>
|
24 | 25 | #include <QWebFrame>
|
25 | 26 | #include <QWebPage>
|
26 | 27 | #include <QImage>
|
| 28 | +#include <QNetworkReply> |
27 | 29 |
|
28 | 30 | QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands ): QgsComposerMultiFrame( c, createUndoCommands ),
|
29 | 31 | mContentMode( QgsComposerHtml::Url ),
|
@@ -78,26 +80,81 @@ void QgsComposerHtml::setHtml( const QString html )
|
78 | 80 | mHtml = html;
|
79 | 81 | }
|
80 | 82 |
|
| 83 | +QString QgsComposerHtml::fetchHtml( QUrl url ) |
| 84 | +{ |
| 85 | + QUrl nextUrlToFetch = url; |
| 86 | + QNetworkReply* reply = 0; |
| 87 | + |
| 88 | + //loop until fetched valid html |
| 89 | + while ( 1 ) |
| 90 | + { |
| 91 | + //set contents |
| 92 | + QNetworkRequest request( nextUrlToFetch ); |
| 93 | + reply = QgsNetworkAccessManager::instance()->get( request ); |
| 94 | + connect( reply, SIGNAL( finished() ), this, SLOT( frameLoaded() ) ); |
| 95 | + //pause until HTML fetch |
| 96 | + mLoaded = false; |
| 97 | + while ( !mLoaded ) |
| 98 | + { |
| 99 | + qApp->processEvents(); |
| 100 | + } |
| 101 | + |
| 102 | + if ( reply->error() != QNetworkReply::NoError ) |
| 103 | + { |
| 104 | + QgsMessageLog::logMessage( tr( "HTML fetch %1 failed with error %2" ).arg( reply->url().toString() ).arg( reply->errorString() ) ); |
| 105 | + reply->deleteLater(); |
| 106 | + return QString(); |
| 107 | + } |
| 108 | + |
| 109 | + QVariant redirect = reply->attribute( QNetworkRequest::RedirectionTargetAttribute ); |
| 110 | + if ( redirect.isNull() ) |
| 111 | + { |
| 112 | + //no error or redirect, got target |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + //redirect, so fetch redirect target |
| 117 | + nextUrlToFetch = redirect.toUrl(); |
| 118 | + reply->deleteLater(); |
| 119 | + } |
| 120 | + |
| 121 | + QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); |
| 122 | + if ( !status.isNull() && status.toInt() >= 400 ) |
| 123 | + { |
| 124 | + QgsMessageLog::logMessage( tr( "HTML fetch %1 failed with error %2" ).arg( reply->url().toString() ).arg( status.toString() ) ); |
| 125 | + reply->deleteLater(); |
| 126 | + return QString(); |
| 127 | + } |
| 128 | + |
| 129 | + QByteArray array = reply->readAll(); |
| 130 | + reply->deleteLater(); |
| 131 | + return QString( array ); |
| 132 | +} |
| 133 | + |
81 | 134 | void QgsComposerHtml::loadHtml()
|
82 | 135 | {
|
83 | 136 | if ( !mWebPage || ( mContentMode == QgsComposerHtml::Url && mUrl.isEmpty() ) )
|
84 | 137 | {
|
85 | 138 | return;
|
86 | 139 | }
|
87 | 140 |
|
88 |
| - mLoaded = false; |
89 |
| - //set contents |
| 141 | + QString loadedHtml; |
90 | 142 | switch ( mContentMode )
|
91 | 143 | {
|
92 | 144 | case QgsComposerHtml::Url:
|
93 |
| - mWebPage->mainFrame()->load( mUrl ); |
| 145 | + { |
| 146 | + loadedHtml = fetchHtml( mUrl ); |
94 | 147 | break;
|
| 148 | + } |
95 | 149 | case QgsComposerHtml::ManualHtml:
|
96 |
| - mWebPage->mainFrame()->setHtml( mHtml ); |
| 150 | + loadedHtml = mHtml; |
97 | 151 | break;
|
98 | 152 | }
|
99 | 153 |
|
100 |
| - //pause until HTML loaded |
| 154 | + mLoaded = false; |
| 155 | + //set html, using the specified url as base if in Url mode |
| 156 | + mWebPage->mainFrame()->setHtml( loadedHtml, mContentMode == QgsComposerHtml::Url ? QUrl( mUrl ) : QUrl() ); |
| 157 | + |
101 | 158 | while ( !mLoaded )
|
102 | 159 | {
|
103 | 160 | qApp->processEvents();
|
|
0 commit comments