Skip to content

Commit

Permalink
OGR: add a download option to the protocol data source
Browse files Browse the repository at this point in the history
Fixes #21091

The issue was due to the /vsicurl/ not
being able to open a streaming endpoint.

“Things are not always what they seem; the first appearance
deceives many; [...]”

― Phaedrus
  • Loading branch information
elpaso committed Jan 25, 2019
1 parent a90fb87 commit cf0d45e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 46 deletions.
46 changes: 41 additions & 5 deletions src/providers/ogr/qgsogrsourceselect.cpp
Expand Up @@ -28,6 +28,7 @@
#include "qgsvectordataprovider.h"
#include "qgssettings.h"
#include "qgsproviderregistry.h"
#include "qgsfiledownloader.h"
#include "ogr/qgsnewogrconnection.h"
#include "ogr/qgsogrhelperfunctions.h"

Expand Down Expand Up @@ -304,6 +305,7 @@ void QgsOgrSourceSelect::setProtocolWidgetsVisibility()
labelKey->show();
mKey->show();
mAuthWarning->show();
mDownloadCheckBox->hide();
}
else
{
Expand All @@ -315,6 +317,7 @@ void QgsOgrSourceSelect::setProtocolWidgetsVisibility()
labelKey->hide();
mKey->hide();
mAuthWarning->hide();
mDownloadCheckBox->show();
}
}

Expand Down Expand Up @@ -397,12 +400,45 @@ void QgsOgrSourceSelect::addButtonClicked()
{
uri = protocolURI->text();
}
if ( cloudType || ! mDownloadCheckBox->isChecked() )
{

mDataSources << createProtocolURI( cmbProtocolTypes->currentText(),
uri,
mAuthSettingsProtocol->configId(),
mAuthSettingsProtocol->username(),
mAuthSettingsProtocol->password() );
}
else
{
QTemporaryFile tempFile;
tempFile.setAutoRemove( false );
tempFile.open();
tempFile.close();
QString filePath { tempFile.fileName() };
QString fileEncoding { encoding() };
QString dsType { dataSourceType() };
QUrl url { uri };
url.setUserName( mAuthSettingsProtocol->username() );
url.setPassword( mAuthSettingsProtocol->password() );
// QgsFileDownloader will self destruct, no leaks here
// As long as the application keeps the data source select dialog
// alive for the whole application lifetime, the lambdas below are
// safe regarding to "this".
QgsFileDownloader *downloader = new QgsFileDownloader( url, filePath, mAuthSettingsProtocol->configId() );
connect( downloader, &QgsFileDownloader::downloadCompleted, [ = ]
{
emit addVectorLayers( QStringList() << filePath, fileEncoding, dsType );
} );
connect( downloader, &QgsFileDownloader::downloadError, [ = ]( QStringList errorMessages )
{
QMessageBox::warning( this,
tr( "Error downloading layer data" ),
errorMessages.join( "<br/>" ) );
} );

}

mDataSources << createProtocolURI( cmbProtocolTypes->currentText(),
uri,
mAuthSettingsProtocol->configId(),
mAuthSettingsProtocol->username(),
mAuthSettingsProtocol->password() );
}
else if ( radioSrcFile->isChecked() )
{
Expand Down
92 changes: 51 additions & 41 deletions src/ui/qgsogrsourceselectbase.ui
Expand Up @@ -9,8 +9,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>450</width>
<height>658</height>
<width>478</width>
<height>756</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -126,29 +126,6 @@
<string>Protocol</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="1">
<widget class="QComboBox" name="cmbProtocolTypes"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Type</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelProtocolURI">
<property name="text">
<string>&amp;URI</string>
</property>
<property name="buddy">
<cstring>protocolURI</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="protocolURI"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="labelBucket">
<property name="text">
Expand All @@ -157,38 +134,48 @@
<property name="buddy">
<cstring>mBucket</cstring>
</property>
</widget>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="mBucket"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="labelKey">
<item row="5" column="0" colspan="2">
<widget class="QLabel" name="mAuthWarning">
<property name="text">
<string>Object key</string>
</property>
<property name="buddy">
<cstring>mKey</cstring>
<string>…</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="mKey"/>
</item>
<item row="4" column="0" colspan="2">
<widget class="QLabel" name="mAuthWarning">
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string></string>
<string>Type</string>
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
<item row="1" column="0">
<widget class="QLabel" name="labelProtocolURI">
<property name="text">
<string>&amp;URI</string>
</property>
<property name="buddy">
<cstring>protocolURI</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="cmbProtocolTypes"/>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="mKey"/>
</item>
<item row="6" column="0" colspan="2">
<widget class="QGroupBox" name="mAuthGroupBox">
<property name="title">
<string>Authentication</string>
Expand Down Expand Up @@ -219,6 +206,29 @@
</layout>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="protocolURI"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="labelKey">
<property name="text">
<string>Object key</string>
</property>
<property name="buddy">
<cstring>mKey</cstring>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="mDownloadCheckBox">
<property name="toolTip">
<string>This option is useful if the service is a streaming service.</string>
</property>
<property name="text">
<string>Download all data before opening</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit cf0d45e

Please sign in to comment.