Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] Fix double escaping of colon in QgsMimeDataUtils #7261

Merged
merged 4 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/qgsmimedatautils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,13 @@ QByteArray QgsMimeDataUtils::layerTreeNodesToUriList( const QList<QgsLayerTreeNo
QString QgsMimeDataUtils::encode( const QStringList &items )
{
QString encoded;
// Do not escape colon twice
QRegExp re( "([^\\\\]):" );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the recommended replacement QRegularExpression instead.

This could possibly be made a bit cleaner by a negative look behind.

Q_FOREACH ( const QString &item, items )
{
QString str = item;
str.replace( '\\', QLatin1String( "\\\\" ) );
str.replace( ':', QLatin1String( "\\:" ) );
str.replace( re, QLatin1String( "\\1\\:" ) );
encoded += str + ':';
}
return encoded.left( encoded.length() - 1 );
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsmimedatautils.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class CORE_EXPORT QgsMimeDataUtils
static QStringList decode( const QString &encoded );
static QByteArray uriListToByteArray( const UriList &layers );


friend class TestQgsMimeDataUtils;

};

Q_DECLARE_METATYPE( QgsMimeDataUtils::UriList )
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ SET(TESTS
testqgsmeshlayerrenderer.cpp
testqgslayerdefinition.cpp
testqgssqliteutils.cpp
testqgsmimedatautils.cpp
)

IF(WITH_QTWEBKIT)
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgslayerdefinition.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/***************************************************************************
testqgsfilefiledownloader.cpp
testqgslayerdefinition.cpp
--------------------------------------
Date : 07.06.2018
Copyright : (C) 2018 Alessandro Pasotti
Expand Down Expand Up @@ -37,7 +37,7 @@ class TestQgsLayerDefinition: public QObject
void cleanup(); // will be called after every testfunction.

/**
* test that findLayers()
* test findLayers()
*/
void testFindLayers();

Expand Down
108 changes: 108 additions & 0 deletions tests/src/core/testqgsmimedatautils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/***************************************************************************
testqgsmimedatautils.cpp
--------------------------------------
Date : 18.06.2018
Copyright : (C) 2018 Alessandro Pasotti
Email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "qgstest.h"
#include <QObject>

#include <qgsmimedatautils.h>

const QString TEST_ENCODED_DATA( "raster:wms:A Fancy WMS From Ciriè City:crs=EPSG\\:2036&dpiMode=7&format=image/png&layers=lidar&styles=default"
"&url=https\\://geoegl.msp.gouv.qc.:EPSG\\\\:2036\\:EPSG\\\\:3857:image/tiff\\:image/jpeg" );

class TestQgsMimeDataUtils: public QObject
{
Q_OBJECT
public:
TestQgsMimeDataUtils() = default;

private slots:
void initTestCase(); // will be called before the first testfunction is executed.
void cleanupTestCase(); // will be called after the last testfunction was executed.
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.

void testEncodeDecode();

};


void TestQgsMimeDataUtils::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();

}

void TestQgsMimeDataUtils::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsMimeDataUtils::init()
{
}


void TestQgsMimeDataUtils::cleanup()
{
}

void TestQgsMimeDataUtils::testEncodeDecode()
{

QgsMimeDataUtils::Uri uri;
uri.layerType = QStringLiteral( "raster" );
uri.name = QStringLiteral( "A Fancy WMS From Ciriè City" );
uri.providerKey = QStringLiteral( "wms" );
uri.supportedCrs << QStringLiteral( "EPSG:2036" ) << QStringLiteral( "EPSG:3857" ) ;
uri.supportedFormats << QStringLiteral( "image/tiff" ) << QStringLiteral( "image/jpeg" );
uri.uri = QStringLiteral( "crs=EPSG:2036&dpiMode=7&format=image/png&layers=lidar&styles=default&url=https://geoegl.msp.gouv.qc." );

QgsMimeDataUtils::UriList uriList;
uriList << uri;

QMimeData *mimeData = QgsMimeDataUtils::encodeUriList( uriList );

QgsMimeDataUtils::Uri uriDecoded( QgsMimeDataUtils::decodeUriList( mimeData )[0] );

QCOMPARE( uriDecoded.name, uri.name );
QCOMPARE( uriDecoded.providerKey, uri.providerKey );
QCOMPARE( uriDecoded.supportedFormats, uri.supportedFormats );
QCOMPARE( uriDecoded.uri, uri.uri );
QCOMPARE( uriDecoded.supportedCrs, uri.supportedCrs );

QgsMimeDataUtils::decodeUriList( mimeData );

// Encode representation:
QString data( uri.data() );

QCOMPARE( data, TEST_ENCODED_DATA );

QStringList fragments( QgsMimeDataUtils::decode( data ) );

QCOMPARE( fragments[0], QString( "raster" ) );
QCOMPARE( fragments[1], QString( "wms" ) );
QCOMPARE( fragments[2], QString( "A Fancy WMS From Ciriè City" ) );
QCOMPARE( fragments[3], QString( "crs=EPSG:2036&dpiMode=7&format=image/png&layers=lidar&styles=default&url=https://geoegl.msp.gouv.qc." ) );
QCOMPARE( fragments[4], QString( "EPSG\\:2036:EPSG\\:3857" ) );

}


QGSTEST_MAIN( TestQgsMimeDataUtils )
#include "testqgsmimedatautils.moc"