Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add QgsProject::attachmentIdentifier and QgsProject::resolveAttacheme…
…ntIdentifier
  • Loading branch information
manisandro committed Jul 27, 2021
1 parent 6967406 commit 6209193
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
24 changes: 24 additions & 0 deletions python/core/auto_generated/project/qgsproject.sip.in
Expand Up @@ -1295,6 +1295,30 @@ Removes the attached file

.. seealso:: :py:func:`createAttachedFile`

.. versionadded:: 3.22
%End

QString attachmentIdentifier( const QString &attachedFile ) const;
%Docstring
Returns an identifier for an attachment file path
An attachment identifier is a string which does not depend on the project archive
storage location.

:param attachedFile: An attachment file path

:return: An identifier for the attached file

.. versionadded:: 3.22
%End

QString resolveAttachmentIdentifier( const QString &identifier ) const;
%Docstring
Resolves an attachment identifier to a attachment file path

:param identifier: An attachment identifier

:return: The attachment file path, or an empty string if the identifier is invalid

.. versionadded:: 3.22
%End

Expand Down
14 changes: 14 additions & 0 deletions src/core/project/qgsproject.cpp
Expand Up @@ -3700,6 +3700,20 @@ bool QgsProject::removeAttachedFile( const QString &path )
return mArchive->removeFile( path );
}

QString QgsProject::attachmentIdentifier( const QString &attachedFile ) const
{
return QStringLiteral( "attachment:///%1" ).arg( QFileInfo( attachedFile ).fileName() );
}

QString QgsProject::resolveAttachmentIdentifier( const QString &identifier ) const
{
if ( identifier.startsWith( "attachment:///" ) )
{
return QDir( mArchive->dir() ).absoluteFilePath( identifier.mid( 14 ) );
}
return QString();
}

const QgsProjectMetadata &QgsProject::metadata() const
{
return mMetadata;
Expand Down
18 changes: 18 additions & 0 deletions src/core/project/qgsproject.h
Expand Up @@ -1362,6 +1362,24 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
bool removeAttachedFile( const QString &path );

/**
* Returns an identifier for an attachment file path
* An attachment identifier is a string which does not depend on the project archive
* storage location.
* \param attachedFile An attachment file path
* \return An identifier for the attached file
* \since QGIS 3.22
*/
QString attachmentIdentifier( const QString &attachedFile ) const;

/**
* Resolves an attachment identifier to a attachment file path
* \param identifier An attachment identifier
* \return The attachment file path, or an empty string if the identifier is invalid
* \since QGIS 3.22
*/
QString resolveAttachmentIdentifier( const QString &identifier ) const;

/**
* Returns a reference to the project's metadata store.
* \see setMetadata()
Expand Down
20 changes: 20 additions & 0 deletions tests/src/core/testqgsproject.cpp
Expand Up @@ -58,6 +58,7 @@ class TestQgsProject : public QObject
void testDefaultRelativePaths();
void testAttachmentsQgs();
void testAttachmentsQgz();
void testAttachmentIdentifier();
};

void TestQgsProject::init()
Expand Down Expand Up @@ -884,6 +885,25 @@ void TestQgsProject::testAttachmentsQgz()

}

void TestQgsProject::testAttachmentIdentifier()
{
// Verify attachment identifiers
{
QTemporaryFile projFile( QDir::temp().absoluteFilePath( "XXXXXX_test.qgz" ) );
projFile.open();

QgsProject p;
QString attachmentFileName = p.createAttachedFile( "test.jpg" );
QString attachmentId = p.attachmentIdentifier( attachmentFileName );
QCOMPARE( p.resolveAttachmentIdentifier( attachmentId ), attachmentFileName );
p.write( projFile.fileName() );

QgsProject p2;
p2.read( projFile.fileName() );
QVERIFY( QFile( p2.resolveAttachmentIdentifier( attachmentId ) ).exists() );
}
}


QGSTEST_MAIN( TestQgsProject )
#include "testqgsproject.moc"

0 comments on commit 6209193

Please sign in to comment.