Skip to content

Commit

Permalink
Add API to QgsProjectStorage to determine matching project storage
Browse files Browse the repository at this point in the history
implementation from a file path
  • Loading branch information
nyalldawson committed Jul 15, 2021
1 parent dc33847 commit 1f31a38
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python/core/auto_generated/project/qgsprojectstorage.sip.in
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/core/project/qgsprojectstorage.cpp
Expand Up @@ -15,6 +15,11 @@

#include "qgsprojectstorage.h"

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

QString QgsProjectStorage::filePath( const QString &uri )
{
Q_UNUSED( uri );
Expand Down
11 changes: 11 additions & 0 deletions src/core/project/qgsprojectstorage.h
Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions src/core/project/qgsprojectstorageregistry.cpp
Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions src/core/providers/ogr/qgsgeopackageprojectstorage.cpp
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/core/providers/ogr/qgsgeopackageprojectstorage.h
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions tests/src/core/testqgsprojectstorage.cpp
Expand Up @@ -34,6 +34,7 @@ class TestQgsProjectStorage : public QObject
void cleanup();// will be called after every testfunction.

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

void TestQgsProjectStorage::init()
Expand Down Expand Up @@ -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.