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 :rtype: bool
%End %End
bool unzip( const QString &zipFilename ); virtual bool unzip( const QString &zipFilename );
%Docstring %Docstring
Clear the current content of this archive and unzip. If a project file Clear the current content of this archive and unzip. Files are unzipped
is found in the content, then this archive may be considered as a valid in the temporary directory.
one. Files are unzipped in the temporary directory.
\param zipFilename The zip file to unzip \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 :rtype: bool
%End %End
@ -97,12 +96,6 @@ class QgsArchive
:rtype: str :rtype: str
%End %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; QStringList files() const;
%Docstring %Docstring
Returns the list of files within this archive Returns the list of files within this archive
@ -115,6 +108,34 @@ class QgsArchive
:rtype: str :rtype: str
%End %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 "qgsarchive.h"
#include "qgsziputils.h" #include "qgsziputils.h"
#include "qgsmessagelog.h" #include "qgsmessagelog.h"
#include <iostream>
QgsArchive::QgsArchive() QgsArchive::QgsArchive()
: mDir( new QTemporaryDir() ) : mDir( new QTemporaryDir() )
@ -26,8 +27,8 @@ QgsArchive::QgsArchive()
} }
QgsArchive::QgsArchive( const QgsArchive &other ) QgsArchive::QgsArchive( const QgsArchive &other )
: mDir( new QTemporaryDir() ) : mFiles( other.mFiles )
, mFiles( other.mFiles ) , mDir( new QTemporaryDir() )
, mFilename( other.mFilename ) , mFilename( other.mFilename )
{ {
} }
@ -108,11 +109,8 @@ bool QgsArchive::unzip()
bool QgsArchive::unzip( const QString &filename ) bool QgsArchive::unzip( const QString &filename )
{ {
clear(); clear();
QgsZipUtils::unzip( filename, mDir->path(), mFiles );
mFilename = filename; mFilename = filename;
return QgsZipUtils::unzip( filename, mDir->path(), mFiles );
return ! projectFile().isEmpty();
} }
void QgsArchive::addFile( const QString &file ) void QgsArchive::addFile( const QString &file )
@ -130,7 +128,12 @@ void QgsArchive::setFileName( const QString &filename )
mFilename = filename; mFilename = filename;
} }
QString QgsArchive::projectFile() const QStringList QgsArchive::files() const
{
return mFiles;
}
QString QgsProjectArchive::projectFile() const
{ {
Q_FOREACH ( const QString &file, mFiles ) Q_FOREACH ( const QString &file, mFiles )
{ {
@ -142,7 +145,10 @@ QString QgsArchive::projectFile() const
return QString(); 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(); bool zip();
/** /**
* Clear the current content of this archive and unzip. If a project file * Clear the current content of this archive and unzip. Files are unzipped
* is found in the content, then this archive may be considered as a valid * in the temporary directory.
* one. Files are unzipped in the temporary directory.
* \param zipFilename The zip file to unzip * \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 * Clear the current content of this archive and unzip. If a project file
@ -107,11 +106,6 @@ class CORE_EXPORT QgsArchive
*/ */
QString filename() const; 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 * Returns the list of files within this archive
*/ */
@ -122,17 +116,37 @@ class CORE_EXPORT QgsArchive
*/ */
QString dir() const; QString dir() const;
protected:
// content of the archive
QStringList mFiles;
private: private:
#ifndef SIP_RUN #ifndef SIP_RUN
// used when unzip is performed // used when unzip is performed
std::unique_ptr<QTemporaryDir> mDir; std::unique_ptr<QTemporaryDir> mDir;
// content of the archive
QStringList mFiles;
// zip filename // zip filename
QString mFilename; QString mFilename;
#endif #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 #endif

View File

@ -332,7 +332,7 @@ QgsProject::QgsProject( QObject *parent )
, mLayoutManager( new QgsLayoutManager( this ) ) , mLayoutManager( new QgsLayoutManager( this ) )
, mRootGroup( new QgsLayerTree ) , mRootGroup( new QgsLayerTree )
, mLabelingEngineSettings( new QgsLabelingEngineSettings ) , mLabelingEngineSettings( new QgsLabelingEngineSettings )
, mArchive( new QgsArchive() ) , mArchive( new QgsProjectArchive() )
, mAutoTransaction( false ) , mAutoTransaction( false )
, mEvaluateDefaultValues( false ) , mEvaluateDefaultValues( false )
, mDirty( false ) , mDirty( false )
@ -2083,7 +2083,7 @@ bool QgsProject::unzip()
bool QgsProject::unzip( const QString &filename ) bool QgsProject::unzip( const QString &filename )
{ {
clearError(); clearError();
std::unique_ptr<QgsArchive> archive( new QgsArchive() ); std::unique_ptr<QgsProjectArchive> archive( new QgsProjectArchive() );
// unzip the archive // unzip the archive
if ( !archive->unzip( filename ) ) if ( !archive->unzip( filename ) )
@ -2131,7 +2131,7 @@ bool QgsProject::zip( const QString &filename )
clearError(); clearError();
// save the current project in a temporary .qgs file // 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 baseName = QFileInfo( filename ).baseName();
const QString qgsFileName = QString( "%1.qgs" ).arg( baseName ); const QString qgsFileName = QString( "%1.qgs" ).arg( baseName );
QFile qgsFile( QDir( archive->dir() ).filePath( qgsFileName ) ); QFile qgsFile( QDir( archive->dir() ).filePath( qgsFileName ) );
@ -2186,7 +2186,7 @@ QString QgsProject::zipFileName() const
void QgsProject::setZipFileName( const QString &filename ) void QgsProject::setZipFileName( const QString &filename )
{ {
mArchive.reset( new QgsArchive() ); mArchive.reset( new QgsProjectArchive() );
mArchive->setFileName( filename ); mArchive->setFileName( filename );
} }

View File

@ -1099,7 +1099,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
QString mPathResolverBaseName; QString mPathResolverBaseName;
std::unique_ptr<QgsArchive> mArchive; std::unique_ptr<QgsProjectArchive> mArchive;
bool mUnzipping; bool mUnzipping;
QFile mFile; // current physical project file QFile mFile; // current physical project file