Skip to content

Commit

Permalink
Try to autofix qgz with last version of qgd file
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Jun 15, 2021
1 parent 6dc608d commit a2168b3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
4 changes: 3 additions & 1 deletion python/core/auto_generated/qgsarchive.sip.in
Expand Up @@ -61,7 +61,7 @@ Clear the current content of this archive and create a new temporary
directory.
%End

void addFile( const QString &filename, bool copy = false );
void addFile( const QString &filename );
%Docstring
Add a new file to this archive. During a zip action, this file will be
part of the resulting zipped file.
Expand All @@ -88,6 +88,8 @@ Returns the list of files within this archive
Returns the current temporary directory.
%End

bool exists() const;

};

class QgsProjectArchive : QgsArchive
Expand Down
20 changes: 15 additions & 5 deletions src/core/project/qgsproject.cpp
Expand Up @@ -3346,13 +3346,23 @@ bool QgsProject::zip( const QString &filename )
if ( ! saveAuxiliaryStorage( asFileName ) )
{
const QString err = mAuxiliaryStorage->errorString();
setError( tr( "Unable to save auxiliary storage file ('%1'). The project is still saved but last changes on auxiliary data are lost." ).arg( err ) );
setError( tr( "Unable to save auxiliary storage file ('%1'). The project is still saved but last changes on auxiliary data are lost. It is recommended to reload the project." ).arg( err ) );
asOk = false;

// try to retrieve the previous version
QgsProjectArchive tmpArchive;
tmpArchive.unzip( mFile.fileName() );
archive->addFile( tmpArchive.auxiliaryStorageFile(), true );
// fixes the current archive and keep the previous version of qgd
if ( !mArchive->exists() )
{
mArchive.reset( new QgsProjectArchive() );
mArchive->unzip( mFile.fileName() );
mArchive->clearProjectFile();

const QString asFile = mArchive->auxiliaryStorageFile();
if ( ! asFile.isEmpty() )
{
archive->addFile( asFile );
mAuxiliaryStorage.reset( new QgsAuxiliaryStorage( asFile, false ) );
}
}
}
else
{
Expand Down
22 changes: 7 additions & 15 deletions src/core/qgsarchive.cpp
Expand Up @@ -104,22 +104,9 @@ bool QgsArchive::unzip( const QString &filename )
return QgsZipUtils::unzip( filename, mDir->path(), mFiles );
}

void QgsArchive::addFile( const QString &file, bool copy )
void QgsArchive::addFile( const QString &file )
{
if ( copy )
{
QFileInfo fi( file );
if ( ! fi.exists() )
return;

const QString newFile = mDir->path() + QDir::separator() + fi.fileName();
QFile::copy( file, newFile );
mFiles.append( newFile );
}
else
{
mFiles.append( file );
}
mFiles.append( file );
}

bool QgsArchive::removeFile( const QString &file )
Expand All @@ -139,6 +126,11 @@ QStringList QgsArchive::files() const
return mFiles;
}

bool QgsArchive::exists() const
{
return QFileInfo::exists( mDir->path() );
}

QString QgsProjectArchive::projectFile() const
{
const auto constFiles = files();
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsarchive.h
Expand Up @@ -79,7 +79,7 @@ class CORE_EXPORT QgsArchive
* part of the resulting zipped file.
* \param filename A file to add when zipping this archive
*/
void addFile( const QString &filename, bool copy = false );
void addFile( const QString &filename );

/**
* Remove a file from this archive and from the filesystem.
Expand All @@ -98,6 +98,8 @@ class CORE_EXPORT QgsArchive
*/
QString dir() const;

bool exists() const;

private:
// content of the archive
QStringList mFiles;
Expand Down

0 comments on commit a2168b3

Please sign in to comment.