4242#include " qgswebframe.h"
4343#include " qgsstringutils.h"
4444#include " qgstreewidgetitem.h"
45+ #include " qgsfiledownloader.h"
4546
4647#include < QCloseEvent>
4748#include < QLabel>
5960#include < QMessageBox>
6061#include < QComboBox>
6162#include < QTextDocument>
63+ #include < QNetworkRequest>
64+ #include < QNetworkReply>
65+ #include < QFileDialog>
66+ #include < QFileInfo>
67+ #include < QRegExp>
6268
6369// graph
6470#include < qwt_plot.h>
@@ -72,13 +78,59 @@ QgsIdentifyResultsWebView::QgsIdentifyResultsWebView( QWidget *parent ) : QgsWeb
7278 setSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
7379 page ()->setNetworkAccessManager ( QgsNetworkAccessManager::instance () );
7480 // page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
81+ page ()->setForwardUnsupportedContent ( true );
7582 page ()->setLinkDelegationPolicy ( QWebPage::DontDelegateLinks );
7683 settings ()->setAttribute ( QWebSettings::LocalContentCanAccessRemoteUrls, true );
7784 settings ()->setAttribute ( QWebSettings::JavascriptCanOpenWindows, true );
7885 settings ()->setAttribute ( QWebSettings::PluginsEnabled, true );
7986#ifdef QGISDEBUG
8087 settings ()->setAttribute ( QWebSettings::DeveloperExtrasEnabled, true );
8188#endif
89+ connect ( page (), SIGNAL ( downloadRequested ( QNetworkRequest ) ), this , SLOT ( downloadRequested ( QNetworkRequest ) ) );
90+ connect ( page (), SIGNAL ( unsupportedContent ( QNetworkReply* ) ), this , SLOT ( unsupportedContent ( QNetworkReply* ) ) );
91+ }
92+
93+
94+ void QgsIdentifyResultsWebView::downloadRequested ( const QNetworkRequest &request )
95+ {
96+ handleDownload ( request.url () );
97+ }
98+
99+ void QgsIdentifyResultsWebView::unsupportedContent ( QNetworkReply * reply )
100+ {
101+ handleDownload ( reply->url () );
102+ }
103+
104+ void QgsIdentifyResultsWebView::handleDownload ( QUrl url )
105+ {
106+ if ( ! url.isValid () )
107+ {
108+ QMessageBox::warning ( this , tr ( " Invalid URL" ), tr ( " The download URL is not valid: %1" ).arg ( url.toString ( ) ) );
109+ }
110+ else
111+ {
112+ const QString DOWNLOADER_LAST_DIR_KEY ( " Qgis/fileDownloaderLastDir" );
113+ QSettings settings;
114+ // Try to get some information from the URL
115+ QFileInfo info ( url.toString ( ) );
116+ QString savePath = settings.value ( DOWNLOADER_LAST_DIR_KEY ).toString ( );
117+ QString fileName = info.fileName ().replace ( QRegExp ( " [^A-z0-9\\ -_\\ .]" ), " _" );
118+ if ( ! savePath.isEmpty () && ! fileName.isEmpty ( ) )
119+ {
120+ savePath = QDir::cleanPath ( savePath + QDir::separator () + fileName );
121+ }
122+ QString targetFile = QFileDialog::getSaveFileName ( this ,
123+ tr ( " Save as" ),
124+ savePath,
125+ info.suffix ( ).isEmpty () ? QString ( ) : " *." + info.suffix ( )
126+ );
127+ if ( ! targetFile.isEmpty () )
128+ {
129+ settings.setValue ( DOWNLOADER_LAST_DIR_KEY , QFileInfo ( targetFile ).dir ().absolutePath ( ) );
130+ // Start the download
131+ new QgsFileDownloader ( url, targetFile );
132+ }
133+ }
82134}
83135
84136void QgsIdentifyResultsWebView::print ()
0 commit comments