Address PR comments

This commit is contained in:
Alessandro Pasotti 2021-12-07 09:33:04 +01:00 committed by Nyall Dawson
parent f6e2c0dee8
commit a90e34a765
3 changed files with 171 additions and 174 deletions

View File

@ -2,7 +2,7 @@
"name": "url_encode", "name": "url_encode",
"type": "function", "type": "function",
"groups": ["Maps"], "groups": ["Maps"],
"description": "Returns an URL encoded string from a map.<br>The underlying function uses <a href='https://doc.qt.io/qt-5/qurlquery.html#toString'>QUrlQuery::toString</a> with <a href='https://doc.qt.io/qt-5/qurl.html#ComponentFormattingOption-enum'>QUrl::FullyEncoded</a> flags.<br>Note that the plus sign '+' is not converted.", "description": "Returns an URL encoded string from a map. Transforms all characters in their properly-encoded form producing a fully-compliant query string.<br>Note that the plus sign '+' is not converted.",
"arguments": [ "arguments": [
{ {
"arg": "map", "arg": "map",

View File

@ -65,29 +65,7 @@ void QgsAction::run( QgsVectorLayer *layer, const QgsFeature &feature, const Qgs
run( actionContext ); run( actionContext );
} }
void QgsAction::run( const QgsExpressionContext &expressionContext ) const void QgsAction::handleFormSubmitAction( const QString &expandedAction ) const
{
if ( !isValid() )
{
QgsDebugMsg( QStringLiteral( "Invalid action cannot be run" ) );
return;
}
QgsExpressionContextScope *scope = new QgsExpressionContextScope( mExpressionContextScope );
QgsExpressionContext context( expressionContext );
context << scope;
const QString expandedAction = QgsExpression::replaceExpressionText( mCommand, &context );
if ( mType == QgsAction::OpenUrl )
{
const QFileInfo finfo( expandedAction );
if ( finfo.exists() && finfo.isFile() )
QDesktopServices::openUrl( QUrl::fromLocalFile( expandedAction ) );
else
QDesktopServices::openUrl( QUrl( expandedAction, QUrl::TolerantMode ) );
}
else if ( mType == QgsAction::SubmitUrlEncoded || mType == QgsAction::SubmitUrlMultipart )
{ {
QUrl url{ expandedAction }; QUrl url{ expandedAction };
@ -127,8 +105,8 @@ void QgsAction::run( const QgsExpressionContext &expressionContext ) const
else else
{ {
QHttpMultiPart *multiPart = new QHttpMultiPart( QHttpMultiPart::FormDataType ); QHttpMultiPart *multiPart = new QHttpMultiPart( QHttpMultiPart::FormDataType );
const auto queryItems { queryString.queryItems( QUrl::ComponentFormattingOption::FullyDecoded ) }; const QList<QPair<QString, QString>> queryItems { queryString.queryItems( QUrl::ComponentFormattingOption::FullyDecoded ) };
for ( const auto &queryItem : std::as_const( queryItems ) ) for ( const QPair<QString, QString> &queryItem : std::as_const( queryItems ) )
{ {
QHttpPart part; QHttpPart part;
part.setHeader( QNetworkRequest::ContentDispositionHeader, part.setHeader( QNetworkRequest::ContentDispositionHeader,
@ -152,17 +130,17 @@ void QgsAction::run( const QgsExpressionContext &expressionContext ) const
const QByteArray replyData = reply->readAll(); const QByteArray replyData = reply->readAll();
QString filename { "download.bin" }; QString filename { "download.bin" };
if ( const auto header = reply->header( QNetworkRequest::KnownHeaders::ContentDispositionHeader ).toString().toStdString(); ! header.empty() ) if ( const std::string header = reply->header( QNetworkRequest::KnownHeaders::ContentDispositionHeader ).toString().toStdString(); ! header.empty() )
{ {
std::string ascii; std::string ascii;
const std::string q1 { R"(filename=")" }; const std::string q1 { R"(filename=")" };
if ( const auto pos = header.find( q1 ); pos != std::string::npos ) if ( const unsigned long pos = header.find( q1 ); pos != std::string::npos )
{ {
const auto len = pos + q1.size(); const unsigned long len = pos + q1.size();
const std::string q2 { R"(")" }; const std::string q2 { R"(")" };
if ( auto pos = header.find( q2, len ); pos != std::string::npos ) if ( unsigned long pos = header.find( q2, len ); pos != std::string::npos )
{ {
bool escaped = false; bool escaped = false;
while ( pos != std::string::npos && header[pos - 1] == '\\' ) while ( pos != std::string::npos && header[pos - 1] == '\\' )
@ -196,7 +174,7 @@ void QgsAction::run( const QgsExpressionContext &expressionContext ) const
std::string utf8; std::string utf8;
const std::string u { R"(UTF-8'')" }; const std::string u { R"(UTF-8'')" };
if ( const auto pos = header.find( u ); pos != std::string::npos ) if ( const unsigned long pos = header.find( u ); pos != std::string::npos )
{ {
utf8 = header.substr( pos + u.size() ); utf8 = header.substr( pos + u.size() );
} }
@ -252,16 +230,33 @@ void QgsAction::run( const QgsExpressionContext &expressionContext ) const
reply->deleteLater(); reply->deleteLater();
} ); } );
#if 0 // Not sure we need this: error is checked in finished() anyway }
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
QObject::connect( reply, qOverload<QNetworkReply::NetworkError>( QNetworkReply::error error ), reply, [ reply ] {} ); void QgsAction::run( const QgsExpressionContext &expressionContext ) const
#else
QObject::connect( reply, &QNetworkReply::errorOccurred, reply, [ reply ]( QNetworkReply::NetworkError )
{ {
QgsMessageLog::logMessage( reply->errorString(), QStringLiteral( "Form Submit Action" ), Qgis::MessageLevel::Critical ); if ( !isValid() )
} ); {
#endif QgsDebugMsg( QStringLiteral( "Invalid action cannot be run" ) );
#endif return;
}
QgsExpressionContextScope *scope = new QgsExpressionContextScope( mExpressionContextScope );
QgsExpressionContext context( expressionContext );
context << scope;
const QString expandedAction = QgsExpression::replaceExpressionText( mCommand, &context );
if ( mType == QgsAction::OpenUrl )
{
const QFileInfo finfo( expandedAction );
if ( finfo.exists() && finfo.isFile() )
QDesktopServices::openUrl( QUrl::fromLocalFile( expandedAction ) );
else
QDesktopServices::openUrl( QUrl( expandedAction, QUrl::TolerantMode ) );
}
else if ( mType == QgsAction::SubmitUrlEncoded || mType == QgsAction::SubmitUrlMultipart )
{
handleFormSubmitAction( expandedAction );
} }
else if ( mType == QgsAction::GenericPython ) else if ( mType == QgsAction::GenericPython )

View File

@ -257,6 +257,8 @@ class CORE_EXPORT QgsAction
QString html( ) const; QString html( ) const;
private: private:
void handleFormSubmitAction( const QString &expandedAction ) const;
ActionType mType = Generic; ActionType mType = Generic;
QString mDescription; QString mDescription;
QString mShortTitle; QString mShortTitle;