Skip to content

Commit

Permalink
Keep order for external storage items
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Sep 29, 2021
1 parent 77be95f commit 818e398
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
18 changes: 14 additions & 4 deletions src/core/externalstorage/qgsexternalstorageregistry.cpp
Expand Up @@ -32,20 +32,30 @@ QgsExternalStorageRegistry::~QgsExternalStorageRegistry()

QgsExternalStorage *QgsExternalStorageRegistry::externalStorageFromType( const QString &type ) const
{
return mBackends.value( type );
auto it = std::find_if( mBackends.begin(), mBackends.end(), [ = ]( QgsExternalStorage * storage )
{
return storage->type() == type;
} );

return it != mBackends.end() ? *it : nullptr;
}

QList<QgsExternalStorage *> QgsExternalStorageRegistry::externalStorages() const
{
return mBackends.values();
return mBackends;
}

void QgsExternalStorageRegistry::registerExternalStorage( QgsExternalStorage *storage )
{
mBackends.insert( storage->type(), storage );
if ( !mBackends.contains( storage ) )
mBackends.append( storage );
}

void QgsExternalStorageRegistry::unregisterExternalStorage( QgsExternalStorage *storage )
{
delete mBackends.take( storage->type() );
const int index = mBackends.indexOf( storage );
if ( index >= 0 )
{
delete mBackends.takeAt( index );
}
}
2 changes: 1 addition & 1 deletion src/core/externalstorage/qgsexternalstorageregistry.h
Expand Up @@ -69,7 +69,7 @@ class CORE_EXPORT QgsExternalStorageRegistry
void unregisterExternalStorage( QgsExternalStorage *storage );

private:
QHash<QString, QgsExternalStorage *> mBackends;
QList< QgsExternalStorage * > mBackends;
};

#endif // QGSEXTERNALSTORAGEREGISTRY_H
16 changes: 12 additions & 4 deletions tests/src/python/test_qgsexternalstorage_base.py
Expand Up @@ -60,16 +60,17 @@ def setUpClass(cls):
assert(cls.authm.storeAuthenticationConfig(cls.auth_config)[0])
assert cls.auth_config.isValid()

registry = QgsApplication.instance().externalStorageRegistry()
assert registry
cls.registry = QgsApplication.instance().externalStorageRegistry()
assert cls.registry

cls.storage = registry.externalStorageFromType(cls.storageType)
cls.storage = cls.registry.externalStorageFromType(cls.storageType)
assert cls.storage

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
pass
cls.registry.unregisterExternalStorage(cls.storage)
assert not cls.storageType in cls.registry.externalStorages()

def setUp(self):
"""Run before each test."""
Expand All @@ -92,6 +93,13 @@ def checkContent(self, file_path, content):
self.assertTrue(f.read(), b"New content")
f.close()

def testStorageList(self):
"""
Check that storage list in in correct order
"""
self.assertEqual([storage.type() for storage in self.registry.externalStorages()],
["SimpleCopy", "WebDAV"])

def testStoreFetchFileLater(self):
"""
Test file storing and fetching (Later mode)
Expand Down

0 comments on commit 818e398

Please sign in to comment.