3838#include " qgswebview.h"
3939#include " qgswebframe.h"
4040#include " qgsstringutils.h"
41+ #include " qgsfiledownloader.h"
4142
4243#include < QCloseEvent>
4344#include < QLabel>
5556#include < QMessageBox>
5657#include < QComboBox>
5758#include < QTextDocument>
59+ #include < QNetworkRequest>
60+ #include < QNetworkReply>
61+ #include < QFileDialog>
62+ #include < QFileInfo>
63+ #include < QRegExp>
5864
5965// graph
6066#include < qwt_plot.h>
@@ -68,13 +74,63 @@ QgsIdentifyResultsWebView::QgsIdentifyResultsWebView( QWidget *parent ) : QgsWeb
6874 setSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
6975 page ()->setNetworkAccessManager ( QgsNetworkAccessManager::instance () );
7076 // page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
77+ page ()->setForwardUnsupportedContent ( true );
7178 page ()->setLinkDelegationPolicy ( QWebPage::DontDelegateLinks );
7279 settings ()->setAttribute ( QWebSettings::LocalContentCanAccessRemoteUrls, true );
7380 settings ()->setAttribute ( QWebSettings::JavascriptCanOpenWindows, true );
7481 settings ()->setAttribute ( QWebSettings::PluginsEnabled, true );
7582#ifdef QGISDEBUG
7683 settings ()->setAttribute ( QWebSettings::DeveloperExtrasEnabled, true );
7784#endif
85+ connect ( page (), SIGNAL ( downloadRequested ( QNetworkRequest ) ), this , SLOT ( downloadRequested ( QNetworkRequest ) ) );
86+ connect ( page (), SIGNAL ( unsupportedContent ( QNetworkReply* ) ), this , SLOT ( unsupportedContent ( QNetworkReply* ) ) );
87+ }
88+
89+
90+ void QgsIdentifyResultsWebView::downloadRequested ( const QNetworkRequest &request )
91+ {
92+ qDebug () << " Download Requested: " << request.url ();
93+ handleDownload ( request.url () );
94+ }
95+
96+ void QgsIdentifyResultsWebView::unsupportedContent ( QNetworkReply * reply )
97+ {
98+ qDebug () << " Unsupported Content: " << reply->url ();
99+ handleDownload ( reply->url () );
100+ }
101+
102+ void QgsIdentifyResultsWebView::handleDownload ( QUrl url )
103+ {
104+ qDebug () << " Downloading: " << url;
105+ if ( ! url.isValid () )
106+ {
107+ QMessageBox::warning ( this , tr ( " Invalid URL" ), tr ( " The download URL is not valid: %1" ).arg ( url.toString ( ) ) );
108+ }
109+ else
110+ {
111+ const QString DOWNLOADER_LAST_DIR_KEY ( " Qgis/fileDownloaderLastDir" );
112+ QSettings settings;
113+ // Try to get some information from the URL
114+ QFileInfo info ( url.toString ( ) );
115+ QString savePath = settings.value ( DOWNLOADER_LAST_DIR_KEY ).toString ( );
116+ QString fileName = info.fileName ().replace ( QRegExp ( " [^A-z0-9\\ -_\\ .]" ), " _" );
117+ if ( ! savePath.isEmpty () && ! fileName.isEmpty ( ) )
118+ {
119+ savePath = QDir::cleanPath ( savePath + QDir::separator () + fileName );
120+ }
121+ QString targetFile = QFileDialog::getSaveFileName ( this ,
122+ tr ( " Save as" ),
123+ savePath,
124+ info.suffix ( ).isEmpty () ? QString ( ) : " *." + info.suffix ( )
125+ );
126+ if ( ! targetFile.isEmpty () )
127+ {
128+ settings.setValue ( DOWNLOADER_LAST_DIR_KEY, QFileInfo ( targetFile ).dir ().absolutePath ( ) );
129+ // Start the download
130+ qDebug () << " Start the download: " << url;
131+ new QgsFileDownloader ( url, targetFile );
132+ }
133+ }
78134}
79135
80136void QgsIdentifyResultsWebView::print ()
0 commit comments