Skip to content

Commit

Permalink
[External Storage] Add SimpleCopy backend
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Jul 30, 2021
1 parent 82e16cf commit d3534db
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -130,6 +130,8 @@ set(QGIS_CORE_SRCS

externalstorage/qgsexternalstorage.cpp
externalstorage/qgsexternalstorageregistry.cpp
externalstorage/qgssimplecopyexternalstorage.cpp

layertree/qgscolorramplegendnode.cpp
layertree/qgscolorramplegendnodesettings.cpp
layertree/qgslayertreegroup.cpp
Expand Down Expand Up @@ -1744,6 +1746,9 @@ set(QGIS_CORE_PRIVATE_HDRS
qgsspatialindexkdbush_p.h

editform/qgseditformconfig_p.h

externalstorage/qgssimplecopyexternalstorage_p.h

proj/qgscoordinatereferencesystem_p.h
proj/qgscoordinatetransformcontext_p.h
proj/qgscoordinatetransform_p.h
Expand Down
1 change: 1 addition & 0 deletions src/core/externalstorage/qgsexternalstorageregistry.cpp
Expand Up @@ -20,6 +20,7 @@

QgsExternalStorageRegistry::QgsExternalStorageRegistry()
{
registerExternalStorage( new QgsSimpleCopyExternalStorage() );
}

QgsExternalStorageRegistry::~QgsExternalStorageRegistry()
Expand Down
105 changes: 105 additions & 0 deletions src/core/externalstorage/qgssimplecopyexternalstorage.cpp
@@ -0,0 +1,105 @@
/***************************************************************************
qgssimplecopyexternalstorage.cpp
--------------------------------------
Date : March 2021
Copyright : (C) 2021 by Julien Cabieces
Email : julien dot cabieces at oslandia dot com
***************************************************************************
* *
* 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 "qgssimplecopyexternalstorage_p.h"

#include "qgscopyfiletask.h"
#include "qgsapplication.h"

#include <QFileInfo>

QgsSimpleCopyExternalStorageStoredContent::QgsSimpleCopyExternalStorageStoredContent( const QString &filePath, const QString &url, const QString &authcfg )
{
Q_UNUSED( authcfg );

mCopyTask = new QgsCopyFileTask( filePath, url );

QgsApplication::instance()->taskManager()->addTask( mCopyTask );

connect( mCopyTask, &QgsTask::taskCompleted, this, [ = ]
{
mUrl = mCopyTask->destination();
mStatus = Finished;
emit stored();
} );

connect( mCopyTask, &QgsTask::taskTerminated, this, [ = ]
{
reportError( mCopyTask->errorString() );
} );

connect( mCopyTask, &QgsTask::progressChanged, this, [ = ]( double progress )
{
emit progressChanged( progress );
} );

mStatus = OnGoing;
}

void QgsSimpleCopyExternalStorageStoredContent::cancel()
{
if ( !mCopyTask )
return;

disconnect( mCopyTask, &QgsTask::taskTerminated, nullptr, nullptr );
connect( mCopyTask, &QgsTask::taskTerminated, this, [ = ]
{
mStatus = Canceled;
emit canceled();
} );

mCopyTask->cancel();
}

QString QgsSimpleCopyExternalStorageStoredContent::url() const
{
return mUrl;
}

QgsSimpleCopyExternalStorageFetchedContent::QgsSimpleCopyExternalStorageFetchedContent( const QString &filePath )
{
// no fetching process, we read directly from its location
if ( !QFileInfo::exists( filePath ) )
{
reportError( tr( "File '%1' does not exist" ).arg( filePath ) );
}
else
{
mStatus = Finished;
mFilePath = filePath;
}
}

QString QgsSimpleCopyExternalStorageFetchedContent::filePath() const
{
return mFilePath;
}

QString QgsSimpleCopyExternalStorage::type() const
{
return QStringLiteral( "SimpleCopy" );
};

QgsExternalStorageStoredContent *QgsSimpleCopyExternalStorage::store( const QString &filePath, const QString &url, const QString &authcfg ) const
{
return new QgsSimpleCopyExternalStorageStoredContent( filePath, url, authcfg );
};

QgsExternalStorageFetchedContent *QgsSimpleCopyExternalStorage::fetch( const QString &url, const QString &authConfig ) const
{
Q_UNUSED( authConfig );

return new QgsSimpleCopyExternalStorageFetchedContent( url );
}
94 changes: 94 additions & 0 deletions src/core/externalstorage/qgssimplecopyexternalstorage_p.h
@@ -0,0 +1,94 @@
/***************************************************************************
qgssimplecopyexternalstorage.h
--------------------------------------
Date : March 2021
Copyright : (C) 2021 by Julien Cabieces
Email : julien dot cabieces at oslandia dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#ifndef QGSSIMPLECOPYEXTERNALSTORAGE_H
#define QGSSIMPLECOPYEXTERNALSTORAGE_H

#include "externalstorage/qgsexternalstorage.h"
#include "qgis_core.h"
#include "qgis_sip.h"

#include <QPointer>

///@cond PRIVATE
#define SIP_NO_FILE

class QgsCopyFileTask;

/**
* \ingroup core
* \brief External storage implementation which simply copy the given resource
* on a given directory file path.
*
* \since QGIS 3.22
*/
class CORE_EXPORT QgsSimpleCopyExternalStorage : public QgsExternalStorage
{
public:

QString type() const override;

QgsExternalStorageStoredContent *store( const QString &filePath, const QString &url, const QString &authcfg = QString() ) const override;

QgsExternalStorageFetchedContent *fetch( const QString &url, const QString &authConfig = QString() ) const override;
};

/**
* \ingroup core
* \brief Class for Simple copy stored content
*
* \since QGIS 3.22
*/
class QgsSimpleCopyExternalStorageStoredContent : public QgsExternalStorageStoredContent
{
Q_OBJECT

public:

QgsSimpleCopyExternalStorageStoredContent( const QString &filePath, const QString &url, const QString &authcfg = QString() );

void cancel() override;

QString url() const override;

private:

QPointer<QgsCopyFileTask> mCopyTask;
QString mUrl;
};

/**
* \ingroup core
* \brief Class for Simple copy fetched content
*
* \since QGIS 3.22
*/
class QgsSimpleCopyExternalStorageFetchedContent : public QgsExternalStorageFetchedContent
{
Q_OBJECT

public:

QgsSimpleCopyExternalStorageFetchedContent( const QString &filePath );

QString filePath() const override;

private:

QString mFilePath;
};


#endif // QGSSIMPLECOPYEXTERNALSTORAGE_H
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -296,6 +296,7 @@ ADD_PYTHON_TEST(PyQgsSearchWidgetWrapper test_qgssearchwidgetwrapper.py)
ADD_PYTHON_TEST(PyQgsShortcutsManager test_qgsshortcutsmanager.py)
ADD_PYTHON_TEST(PyQgsSimpleFillSymbolLayer test_qgssimplefillsymbollayer.py)
ADD_PYTHON_TEST(PyQgsSimpleLineSymbolLayer test_qgssimplelinesymbollayer.py)
ADD_PYTHON_TEST(PyQgsExternalStorageSimpleCopy test_qgsexternalstorage_simplecopy.py)
ADD_PYTHON_TEST(PyQgsSpatialIndex test_qgsspatialindex.py)
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_provider_spatialite.py)
ADD_PYTHON_TEST(PyQgsSQLStatement test_qgssqlstatement.py)
Expand Down
3 changes: 3 additions & 0 deletions tests/src/python/test_qgsexternalstorage_base.py
Expand Up @@ -106,6 +106,7 @@ def testStoreFetchFile(self):
self.assertEqual(storedContent.status(), QgsExternalStorageOperation.OnGoing)

spyErrorOccurred = QSignalSpy(storedContent.errorOccurred)
spyProgressChanged = QSignalSpy(storedContent.progressChanged)

loop = QEventLoop()
storedContent.stored.connect(loop.quit)
Expand All @@ -116,6 +117,8 @@ def testStoreFetchFile(self):
self.assertEqual(storedContent.url(), url)
self.assertFalse(storedContent.errorString())
self.assertEqual(storedContent.status(), QgsExternalStorageFetchedContent.Finished)
self.assertTrue(len(spyProgressChanged) > 0)
self.assertEqual(spyProgressChanged[-1][0], 100)

# fetch
fetchedContent = self.storage.fetch(self.url + "/" + os.path.basename(f.name), self.auth_config.id())
Expand Down
61 changes: 61 additions & 0 deletions tests/src/python/test_qgsexternalstorage_simplecopy.py
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for Simple copy external storage
.. note:: 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.
"""

__author__ = 'Julien Cabieces'
__date__ = '31/03/2021'
__copyright__ = 'Copyright 2021, The QGIS Project'

from shutil import rmtree
import os
import tempfile
import time

from utilities import unitTestDataPath, waitServer
from test_qgsexternalstorage_base import TestPyQgsExternalStorageBase

from qgis.PyQt.QtCore import QCoreApplication, QEventLoop, QUrl, QTemporaryDir

from qgis.core import (
QgsApplication,
QgsAuthMethodConfig,
QgsExternalStorageFetchedContent)

from qgis.testing import (
start_app,
unittest,
)


class TestPyQgsExternalStorageSimpleCopy(TestPyQgsExternalStorageBase, unittest.TestCase):

storageType = "SimpleCopy"
badUrl = "/nothing/here/"

@classmethod
def setUpClass(cls):
"""Run before all tests:"""

cls.temp_dir = QTemporaryDir()
cls.url = cls.temp_dir.path()

super().setUpClass()

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
super().tearDownClass()
cls.temp_dir = None

def testStoreMissingAuth(self):
"""Override this one because there is authentification for SimpleCopy external storage"""
pass


if __name__ == '__main__':
unittest.main()

0 comments on commit d3534db

Please sign in to comment.