Skip to content
Permalink
Browse files
Add API to QgsProjectStorage to determine matching project storage
implementation from a file path
  • Loading branch information
nyalldawson committed Jul 15, 2021
1 parent dc33847 commit 1f31a38dd31fbf2a57679ba1762330026d16a06f
@@ -47,6 +47,19 @@ Metadata associated with a project
%Docstring
Unique identifier of the project storage type. If :py:func:`~QgsProjectStorage.type` returns "memory", all project file names
starting with "memory:" will have read/write redirected through that storage implementation.
%End

virtual bool isSupportedUri( const QString &uri ) const;
%Docstring
Returns ``True`` if the specified ``uri`` is supported by the storage provider.

.. note::

This method does not actually test whether the ``uri`` contains projects, but
rather it is a quick test to determine if it is possible that the uri may
contain projects.

.. versionadded:: 3.22
%End

virtual QStringList listProjects( const QString &uri ) = 0;
@@ -15,6 +15,11 @@

#include "qgsprojectstorage.h"

bool QgsProjectStorage::isSupportedUri( const QString & ) const
{
return false;
}

QString QgsProjectStorage::filePath( const QString &uri )
{
Q_UNUSED( uri );
@@ -59,6 +59,17 @@ class CORE_EXPORT QgsProjectStorage
*/
virtual QString type() = 0;

/**
* Returns TRUE if the specified \a uri is supported by the storage provider.
*
* \note This method does not actually test whether the \a uri contains projects, but
* rather it is a quick test to determine if it is possible that the uri may
* contain projects.
*
* \since QGIS 3.22
*/
virtual bool isSupportedUri( const QString &uri ) const;

//! Returns list of all projects for given URI (specific to each storage backend)
virtual QStringList listProjects( const QString &uri ) = 0;

@@ -37,6 +37,14 @@ QgsProjectStorage *QgsProjectStorageRegistry::projectStorageFromUri( const QStri
if ( uri.startsWith( scheme ) )
return storage;
}

// second chance -- use isSupportedUri to determine if a uri is supported by a backend
for ( auto it = mBackends.constBegin(); it != mBackends.constEnd(); ++it )
{
QgsProjectStorage *storage = it.value();
if ( storage->isSupportedUri( uri ) )
return storage;
}
return nullptr;
}

@@ -97,6 +97,12 @@ static bool _projectsTableExists( const QString &database )
return ok;
}

bool QgsGeoPackageProjectStorage::isSupportedUri( const QString &uri ) const
{
const QFileInfo fi( uri );
return fi.isFile() && fi.suffix().compare( QLatin1String( "gpkg" ), Qt::CaseInsensitive ) == 0;
}

QStringList QgsGeoPackageProjectStorage::listProjects( const QString &uri )
{
QStringList lst;
@@ -41,6 +41,7 @@ class CORE_EXPORT QgsGeoPackageProjectStorage : public QgsProjectStorage
// QgsProjectStorage interface
public:
QString type() override { return QStringLiteral( "geopackage" ); }
bool isSupportedUri( const QString &uri ) const override;
QStringList listProjects( const QString &uri ) override;
bool readProject( const QString &uri, QIODevice *device, QgsReadWriteContext &context ) override;
bool writeProject( const QString &uri, QIODevice *device, QgsReadWriteContext &context ) override;
@@ -34,6 +34,7 @@ class TestQgsProjectStorage : public QObject
void cleanup();// will be called after every testfunction.

void testMemoryStorage();
void testSupportedUri();
};

void TestQgsProjectStorage::init()
@@ -241,6 +242,17 @@ void TestQgsProjectStorage::testMemoryStorage()
QgsApplication::projectStorageRegistry()->unregisterProjectStorage( memStorage );
}

void TestQgsProjectStorage::testSupportedUri()
{
QgsProjectStorage *gpkgStorage = QgsApplication::projectStorageRegistry()->projectStorageFromType( QStringLiteral( "geopackage" ) );
QVERIFY( gpkgStorage );

QVERIFY( gpkgStorage->isSupportedUri( QStringLiteral( "%1/mixed_layers.gpkg" ).arg( TEST_DATA_DIR ) ) );
QVERIFY( !gpkgStorage->isSupportedUri( QStringLiteral( "%1/mixed_types.TAB" ).arg( TEST_DATA_DIR ) ) );

QCOMPARE( QgsApplication::projectStorageRegistry()->projectStorageFromUri( QStringLiteral( "%1/mixed_layers.gpkg" ).arg( TEST_DATA_DIR ) )->type(), QStringLiteral( "geopackage" ) );
}


QGSTEST_MAIN( TestQgsProjectStorage )
#include "testqgsprojectstorage.moc"

0 comments on commit 1f31a38

Please sign in to comment.