Skip to content

Commit 576afe5

Browse files
committedJul 31, 2017
Add a QgsProjectArchive class to keep QgsArchive generic
1 parent 26d3845 commit 576afe5

File tree

5 files changed

+79
-38
lines changed

5 files changed

+79
-38
lines changed
 

‎python/core/qgsarchive.sip

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ class QgsArchive
5252
:rtype: bool
5353
%End
5454

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

@@ -97,12 +96,6 @@ class QgsArchive
9796
:rtype: str
9897
%End
9998

100-
QString projectFile() const;
101-
%Docstring
102-
Returns the current .qgs project file or an empty string if there's none
103-
:rtype: str
104-
%End
105-
10699
QStringList files() const;
107100
%Docstring
108101
Returns the list of files within this archive
@@ -115,6 +108,34 @@ class QgsArchive
115108
:rtype: str
116109
%End
117110

111+
protected:
112+
113+
};
114+
115+
class QgsProjectArchive : QgsArchive
116+
{
117+
118+
%TypeHeaderCode
119+
#include "qgsarchive.h"
120+
%End
121+
public:
122+
123+
virtual bool unzip( const QString &zipFilename );
124+
125+
%Docstring
126+
Clear the current content of this archive and unzip. If a project file
127+
is found in the content, then this archive may be considered as a valid
128+
one. Files are unzipped in the temporary directory.
129+
\param zipFilename The zip file to unzip
130+
:return: true if a project file has been found, false otherwise
131+
:rtype: bool
132+
%End
133+
134+
QString projectFile() const;
135+
%Docstring
136+
Returns the current .qgs project file or an empty string if there's none
137+
:rtype: str
138+
%End
118139
};
119140

120141
/************************************************************************

‎src/core/qgsarchive.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
#include "qgsarchive.h"
2020
#include "qgsziputils.h"
2121
#include "qgsmessagelog.h"
22+
#include <iostream>
2223

2324
QgsArchive::QgsArchive()
2425
: mDir( new QTemporaryDir() )
2526
{
2627
}
2728

2829
QgsArchive::QgsArchive( const QgsArchive &other )
29-
: mDir( new QTemporaryDir() )
30-
, mFiles( other.mFiles )
30+
: mFiles( other.mFiles )
31+
, mDir( new QTemporaryDir() )
3132
, mFilename( other.mFilename )
3233
{
3334
}
@@ -108,11 +109,8 @@ bool QgsArchive::unzip()
108109
bool QgsArchive::unzip( const QString &filename )
109110
{
110111
clear();
111-
112-
QgsZipUtils::unzip( filename, mDir->path(), mFiles );
113112
mFilename = filename;
114-
115-
return ! projectFile().isEmpty();
113+
return QgsZipUtils::unzip( filename, mDir->path(), mFiles );
116114
}
117115

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

133-
QString QgsArchive::projectFile() const
131+
QStringList QgsArchive::files() const
132+
{
133+
return mFiles;
134+
}
135+
136+
QString QgsProjectArchive::projectFile() const
134137
{
135138
Q_FOREACH ( const QString &file, mFiles )
136139
{
@@ -142,7 +145,10 @@ QString QgsArchive::projectFile() const
142145
return QString();
143146
}
144147

145-
QStringList QgsArchive::files() const
148+
bool QgsProjectArchive::unzip( const QString &filename )
146149
{
147-
return mFiles;
150+
if ( QgsArchive::unzip( filename ) )
151+
return ! projectFile().isEmpty();
152+
else
153+
return false;
148154
}

‎src/core/qgsarchive.h

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@ class CORE_EXPORT QgsArchive
6666
bool zip();
6767

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

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

110-
/**
111-
* Returns the current .qgs project file or an empty string if there's none
112-
*/
113-
QString projectFile() const;
114-
115109
/**
116110
* Returns the list of files within this archive
117111
*/
@@ -122,17 +116,37 @@ class CORE_EXPORT QgsArchive
122116
*/
123117
QString dir() const;
124118

119+
protected:
120+
// content of the archive
121+
QStringList mFiles;
122+
125123
private:
126124
#ifndef SIP_RUN
127125
// used when unzip is performed
128126
std::unique_ptr<QTemporaryDir> mDir;
129127

130-
// content of the archive
131-
QStringList mFiles;
132-
133128
// zip filename
134129
QString mFilename;
135130
#endif
136131
};
137132

133+
class CORE_EXPORT QgsProjectArchive : public QgsArchive
134+
{
135+
public:
136+
137+
/**
138+
* Clear the current content of this archive and unzip. If a project file
139+
* is found in the content, then this archive may be considered as a valid
140+
* one. Files are unzipped in the temporary directory.
141+
* \param zipFilename The zip file to unzip
142+
* \returns true if a project file has been found, false otherwise
143+
*/
144+
bool unzip( const QString &zipFilename ) override;
145+
146+
/**
147+
* Returns the current .qgs project file or an empty string if there's none
148+
*/
149+
QString projectFile() const;
150+
};
151+
138152
#endif

‎src/core/qgsproject.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ QgsProject::QgsProject( QObject *parent )
332332
, mLayoutManager( new QgsLayoutManager( this ) )
333333
, mRootGroup( new QgsLayerTree )
334334
, mLabelingEngineSettings( new QgsLabelingEngineSettings )
335-
, mArchive( new QgsArchive() )
335+
, mArchive( new QgsProjectArchive() )
336336
, mAutoTransaction( false )
337337
, mEvaluateDefaultValues( false )
338338
, mDirty( false )
@@ -2083,7 +2083,7 @@ bool QgsProject::unzip()
20832083
bool QgsProject::unzip( const QString &filename )
20842084
{
20852085
clearError();
2086-
std::unique_ptr<QgsArchive> archive( new QgsArchive() );
2086+
std::unique_ptr<QgsProjectArchive> archive( new QgsProjectArchive() );
20872087

20882088
// unzip the archive
20892089
if ( !archive->unzip( filename ) )
@@ -2131,7 +2131,7 @@ bool QgsProject::zip( const QString &filename )
21312131
clearError();
21322132

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

21872187
void QgsProject::setZipFileName( const QString &filename )
21882188
{
2189-
mArchive.reset( new QgsArchive() );
2189+
mArchive.reset( new QgsProjectArchive() );
21902190
mArchive->setFileName( filename );
21912191
}
21922192

‎src/core/qgsproject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
10991099

11001100
QString mPathResolverBaseName;
11011101

1102-
std::unique_ptr<QgsArchive> mArchive;
1102+
std::unique_ptr<QgsProjectArchive> mArchive;
11031103
bool mUnzipping;
11041104

11051105
QFile mFile; // current physical project file

0 commit comments

Comments
 (0)
Please sign in to comment.