Add a QgsProjectArchive class to keep QgsArchive generic

This commit is contained in:
Blottiere Paul 2017-07-13 08:45:33 +01:00
parent 26d38459c5
commit 576afe5aa3
5 changed files with 79 additions and 38 deletions

View File

@ -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
@ -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
@ -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
};
/************************************************************************

View File

@ -19,6 +19,7 @@
#include "qgsarchive.h"
#include "qgsziputils.h"
#include "qgsmessagelog.h"
#include <iostream>
QgsArchive::QgsArchive()
: mDir( new QTemporaryDir() )
@ -26,8 +27,8 @@ QgsArchive::QgsArchive()
}
QgsArchive::QgsArchive( const QgsArchive &other )
: mDir( new QTemporaryDir() )
, mFiles( other.mFiles )
: mFiles( other.mFiles )
, mDir( new QTemporaryDir() )
, mFilename( other.mFilename )
{
}
@ -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 )
@ -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 )
{
@ -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;
}

View File

@ -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
@ -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
*/
@ -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

View File

@ -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 )
@ -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 ) )
@ -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 ) );
@ -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 );
}

View File

@ -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