Skip to content

Commit

Permalink
Add a QgsProjectArchive class to keep QgsArchive generic
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Jul 31, 2017
1 parent 26d3845 commit 576afe5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 38 deletions.
43 changes: 32 additions & 11 deletions python/core/qgsarchive.sip
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ class QgsArchive
:rtype: bool
%End

bool unzip( const QString &zipFilename );
virtual bool unzip( const QString &zipFilename );
%Docstring
Clear the current content of this archive and unzip. If a project file
is found in the content, then this archive may be considered as a valid
one. Files are unzipped in the temporary directory.
Clear the current content of this archive and unzip. Files are unzipped
in the temporary directory.
\param zipFilename The zip file to unzip
:return: true if a project file has been found, false otherwise
:return: true if unzip action is a success, false otherwise
:rtype: bool
%End

Expand Down Expand Up @@ -97,12 +96,6 @@ class QgsArchive
:rtype: str
%End

QString projectFile() const;
%Docstring
Returns the current .qgs project file or an empty string if there's none
:rtype: str
%End

QStringList files() const;
%Docstring
Returns the list of files within this archive
Expand All @@ -115,6 +108,34 @@ class QgsArchive
:rtype: str
%End

protected:

};

class QgsProjectArchive : QgsArchive
{

%TypeHeaderCode
#include "qgsarchive.h"
%End
public:

virtual bool unzip( const QString &zipFilename );

%Docstring
Clear the current content of this archive and unzip. If a project file
is found in the content, then this archive may be considered as a valid
one. Files are unzipped in the temporary directory.
\param zipFilename The zip file to unzip
:return: true if a project file has been found, false otherwise
:rtype: bool
%End

QString projectFile() const;
%Docstring
Returns the current .qgs project file or an empty string if there's none
:rtype: str
%End
};

/************************************************************************
Expand Down
24 changes: 15 additions & 9 deletions src/core/qgsarchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
#include "qgsarchive.h"
#include "qgsziputils.h"
#include "qgsmessagelog.h"
#include <iostream>

QgsArchive::QgsArchive()
: mDir( new QTemporaryDir() )
{
}

QgsArchive::QgsArchive( const QgsArchive &other )
: mDir( new QTemporaryDir() )
, mFiles( other.mFiles )
: mFiles( other.mFiles )
, mDir( new QTemporaryDir() )
, mFilename( other.mFilename )
{
}
Expand Down Expand Up @@ -108,11 +109,8 @@ bool QgsArchive::unzip()
bool QgsArchive::unzip( const QString &filename )
{
clear();

QgsZipUtils::unzip( filename, mDir->path(), mFiles );
mFilename = filename;

return ! projectFile().isEmpty();
return QgsZipUtils::unzip( filename, mDir->path(), mFiles );
}

void QgsArchive::addFile( const QString &file )
Expand All @@ -130,7 +128,12 @@ void QgsArchive::setFileName( const QString &filename )
mFilename = filename;
}

QString QgsArchive::projectFile() const
QStringList QgsArchive::files() const
{
return mFiles;
}

QString QgsProjectArchive::projectFile() const
{
Q_FOREACH ( const QString &file, mFiles )
{
Expand All @@ -142,7 +145,10 @@ QString QgsArchive::projectFile() const
return QString();
}

QStringList QgsArchive::files() const
bool QgsProjectArchive::unzip( const QString &filename )
{
return mFiles;
if ( QgsArchive::unzip( filename ) )
return ! projectFile().isEmpty();
else
return false;
}
40 changes: 27 additions & 13 deletions src/core/qgsarchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ class CORE_EXPORT QgsArchive
bool zip();

/**
* Clear the current content of this archive and unzip. If a project file
* is found in the content, then this archive may be considered as a valid
* one. Files are unzipped in the temporary directory.
* Clear the current content of this archive and unzip. Files are unzipped
* in the temporary directory.
* \param zipFilename The zip file to unzip
* \returns true if a project file has been found, false otherwise
* \returns true if unzip action is a success, false otherwise
*/
bool unzip( const QString &zipFilename );
virtual bool unzip( const QString &zipFilename );

/**
* Clear the current content of this archive and unzip. If a project file
Expand Down Expand Up @@ -107,11 +106,6 @@ class CORE_EXPORT QgsArchive
*/
QString filename() const;

/**
* Returns the current .qgs project file or an empty string if there's none
*/
QString projectFile() const;

/**
* Returns the list of files within this archive
*/
Expand All @@ -122,17 +116,37 @@ class CORE_EXPORT QgsArchive
*/
QString dir() const;

protected:
// content of the archive
QStringList mFiles;

private:
#ifndef SIP_RUN
// used when unzip is performed
std::unique_ptr<QTemporaryDir> mDir;

// content of the archive
QStringList mFiles;

// zip filename
QString mFilename;
#endif
};

class CORE_EXPORT QgsProjectArchive : public QgsArchive
{
public:

/**
* Clear the current content of this archive and unzip. If a project file
* is found in the content, then this archive may be considered as a valid
* one. Files are unzipped in the temporary directory.
* \param zipFilename The zip file to unzip
* \returns true if a project file has been found, false otherwise
*/
bool unzip( const QString &zipFilename ) override;

/**
* Returns the current .qgs project file or an empty string if there's none
*/
QString projectFile() const;
};

#endif
8 changes: 4 additions & 4 deletions src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ QgsProject::QgsProject( QObject *parent )
, mLayoutManager( new QgsLayoutManager( this ) )
, mRootGroup( new QgsLayerTree )
, mLabelingEngineSettings( new QgsLabelingEngineSettings )
, mArchive( new QgsArchive() )
, mArchive( new QgsProjectArchive() )
, mAutoTransaction( false )
, mEvaluateDefaultValues( false )
, mDirty( false )
Expand Down Expand Up @@ -2083,7 +2083,7 @@ bool QgsProject::unzip()
bool QgsProject::unzip( const QString &filename )
{
clearError();
std::unique_ptr<QgsArchive> archive( new QgsArchive() );
std::unique_ptr<QgsProjectArchive> archive( new QgsProjectArchive() );

// unzip the archive
if ( !archive->unzip( filename ) )
Expand Down Expand Up @@ -2131,7 +2131,7 @@ bool QgsProject::zip( const QString &filename )
clearError();

// save the current project in a temporary .qgs file
std::unique_ptr<QgsArchive> archive( new QgsArchive() );
std::unique_ptr<QgsProjectArchive> archive( new QgsProjectArchive() );
const QString baseName = QFileInfo( filename ).baseName();
const QString qgsFileName = QString( "%1.qgs" ).arg( baseName );
QFile qgsFile( QDir( archive->dir() ).filePath( qgsFileName ) );
Expand Down Expand Up @@ -2186,7 +2186,7 @@ QString QgsProject::zipFileName() const

void QgsProject::setZipFileName( const QString &filename )
{
mArchive.reset( new QgsArchive() );
mArchive.reset( new QgsProjectArchive() );
mArchive->setFileName( filename );
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

QString mPathResolverBaseName;

std::unique_ptr<QgsArchive> mArchive;
std::unique_ptr<QgsProjectArchive> mArchive;
bool mUnzipping;

QFile mFile; // current physical project file
Expand Down

0 comments on commit 576afe5

Please sign in to comment.