3535#include " qgsvectorlayer.h"
3636#include " qgswebview.h"
3737#include " qgswebframe.h"
38+ #include " qgsfiledownloader.h"
3839
3940#include < QCloseEvent>
4041#include < QLabel>
5253#include < QDesktopServices>
5354#include < QMessageBox>
5455#include < QComboBox>
56+ #include < QNetworkRequest>
57+ #include < QNetworkReply>
58+ #include < QFileDialog>
59+ #include < QFileInfo>
60+ #include < QRegExp>
5561
5662// graph
5763#include < qwt_plot.h>
@@ -66,13 +72,59 @@ QgsIdentifyResultsWebView::QgsIdentifyResultsWebView( QWidget *parent ) : QgsWeb
6672 setSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
6773 page ()->setNetworkAccessManager ( QgsNetworkAccessManager::instance () );
6874 // page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
75+ page ()->setForwardUnsupportedContent ( true );
6976 page ()->setLinkDelegationPolicy ( QWebPage::DontDelegateLinks );
7077 settings ()->setAttribute ( QWebSettings::LocalContentCanAccessRemoteUrls, true );
7178 settings ()->setAttribute ( QWebSettings::JavascriptCanOpenWindows, true );
7279 settings ()->setAttribute ( QWebSettings::PluginsEnabled, true );
7380#ifdef QGISDEBUG
7481 settings ()->setAttribute ( QWebSettings::DeveloperExtrasEnabled, true );
7582#endif
83+ connect ( page (), SIGNAL ( downloadRequested ( QNetworkRequest ) ), this , SLOT ( downloadRequested ( QNetworkRequest ) ) );
84+ connect ( page (), SIGNAL ( unsupportedContent ( QNetworkReply* ) ), this , SLOT ( unsupportedContent ( QNetworkReply* ) ) );
85+ }
86+
87+
88+ void QgsIdentifyResultsWebView::downloadRequested ( const QNetworkRequest &request )
89+ {
90+ handleDownload ( request.url () );
91+ }
92+
93+ void QgsIdentifyResultsWebView::unsupportedContent ( QNetworkReply * reply )
94+ {
95+ handleDownload ( reply->url () );
96+ }
97+
98+ void QgsIdentifyResultsWebView::handleDownload ( QUrl url )
99+ {
100+ if ( ! url.isValid () )
101+ {
102+ QMessageBox::warning ( this , tr ( " Invalid URL" ), tr ( " The download URL is not valid: %1" ).arg ( url.toString ( ) ) );
103+ }
104+ else
105+ {
106+ const QString DOWNLOADER_LAST_DIR_KEY ( " Qgis/fileDownloaderLastDir" );
107+ QSettings settings;
108+ // Try to get some information from the URL
109+ QFileInfo info ( url.toString ( ) );
110+ QString savePath = settings.value ( DOWNLOADER_LAST_DIR_KEY ).toString ( );
111+ QString fileName = info.fileName ().replace ( QRegExp ( " [^A-z0-9\\ -_\\ .]" ), " _" );
112+ if ( ! savePath.isEmpty () && ! fileName.isEmpty ( ) )
113+ {
114+ savePath = QDir::cleanPath ( savePath + QDir::separator () + fileName );
115+ }
116+ QString targetFile = QFileDialog::getSaveFileName ( this ,
117+ tr ( " Save as" ),
118+ savePath,
119+ info.suffix ( ).isEmpty () ? QString ( ) : " *." + info.suffix ( )
120+ );
121+ if ( ! targetFile.isEmpty () )
122+ {
123+ settings.setValue ( DOWNLOADER_LAST_DIR_KEY, QFileInfo ( targetFile ).dir ().absolutePath ( ) );
124+ // Start the download
125+ new QgsFileDownloader ( url, targetFile );
126+ }
127+ }
76128}
77129
78130void QgsIdentifyResultsWebView::print ( void )
0 commit comments