QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsarchive.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsarchive.cpp
3  ----------------
4 
5  begin : July 07, 2017
6  copyright : (C) 2017 by Paul Blottiere
7  email : [email protected]
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "qgsarchive.h"
20 #include "qgsziputils.h"
21 #include "qgsmessagelog.h"
22 #include "qgsauxiliarystorage.h"
23 
24 
25 #ifdef Q_OS_WIN
26 #include <windows.h>
27 #endif
28 
29 #include <QStandardPaths>
30 #include <QUuid>
31 
33  : mDir( new QTemporaryDir() )
34 {
35 }
36 
38  : mFiles( other.mFiles )
39  , mDir( new QTemporaryDir() )
40 {
41 }
42 
44 {
45  if ( this != &other )
46  {
47  mFiles = other.mFiles;
48  mDir.reset( new QTemporaryDir() );
49  }
50 
51  return *this;
52 }
53 
54 QString QgsArchive::dir() const
55 {
56  return mDir->path();
57 }
58 
60 {
61  mDir.reset( new QTemporaryDir() );
62  mFiles.clear();
63 }
64 
65 bool QgsArchive::zip( const QString &filename )
66 {
67  const QString tempPath = QStandardPaths::standardLocations( QStandardPaths::TempLocation ).at( 0 );
68  const QString uuid = QUuid::createUuid().toString();
69  QFile tmpFile( tempPath + QDir::separator() + uuid );
70 
71  // zip content
72  if ( ! QgsZipUtils::zip( tmpFile.fileName(), mFiles ) )
73  {
74  const QString err = QObject::tr( "Unable to zip content" );
75  QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) );
76  return false;
77  }
78 
79  // remove existing zip file
80  if ( QFile::exists( filename ) )
81  QFile::remove( filename );
82 
83 #ifdef Q_OS_WIN
84  // Clear temporary flag (see GH #32118)
85  DWORD dwAttrs;
86  dwAttrs = GetFileAttributes( tmpFile.fileName().toLocal8Bit( ).data( ) );
87  SetFileAttributes( tmpFile.fileName().toLocal8Bit( ).data( ), dwAttrs & ~ FILE_ATTRIBUTE_TEMPORARY );
88 #endif // Q_OS_WIN
89 
90  // save zip archive
91  if ( ! tmpFile.rename( filename ) )
92  {
93  const QString err = QObject::tr( "Unable to save zip file '%1'" ).arg( filename );
94  QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) );
95  return false;
96  }
97 
98  return true;
99 }
100 
101 bool QgsArchive::unzip( const QString &filename )
102 {
103  clear();
104  return QgsZipUtils::unzip( filename, mDir->path(), mFiles );
105 }
106 
107 void QgsArchive::addFile( const QString &file )
108 {
109  mFiles.append( file );
110 }
111 
112 bool QgsArchive::removeFile( const QString &file )
113 {
114  bool rc = false;
115 
116  if ( !file.isEmpty() && mFiles.contains( file ) && QFile::exists( file ) )
117  rc = QFile::remove( file );
118 
119  mFiles.removeOne( file );
120 
121  return rc;
122 }
123 
124 QStringList QgsArchive::files() const
125 {
126  return mFiles;
127 }
128 
129 bool QgsArchive::exists() const
130 {
131  return QFileInfo::exists( mDir->path() );
132 }
133 
135 {
136  const auto constFiles = files();
137  for ( const QString &file : constFiles )
138  {
139  const QFileInfo fileInfo( file );
140  if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 )
141  return file;
142  }
143 
144  return QString();
145 }
146 
147 bool QgsProjectArchive::unzip( const QString &filename )
148 {
149  if ( QgsArchive::unzip( filename ) )
150  return ! projectFile().isEmpty();
151  else
152  return false;
153 }
154 
156 {
157  return removeFile( projectFile() );
158 }
159 
161 {
162  const QString extension = QgsAuxiliaryStorage::extension();
163 
164  const QStringList fileList = files();
165  for ( const QString &file : fileList )
166  {
167  const QFileInfo fileInfo( file );
168  if ( fileInfo.suffix().compare( extension, Qt::CaseInsensitive ) == 0 )
169  return file;
170  }
171 
172  return QString();
173 }
Class allowing to manage the zip/unzip actions.
Definition: qgsarchive.h:36
QgsArchive()
Constructor.
Definition: qgsarchive.cpp:32
QgsArchive & operator=(const QgsArchive &other)
Definition: qgsarchive.cpp:43
virtual bool unzip(const QString &zipFilename)
Clear the current content of this archive and unzip.
Definition: qgsarchive.cpp:101
bool zip(const QString &zipFilename)
Zip the content of this archive.
Definition: qgsarchive.cpp:65
void clear()
Clear the current content of this archive and create a new temporary directory.
Definition: qgsarchive.cpp:59
bool exists() const
Returns true if the archive exists on the filesystem, false otherwise.
Definition: qgsarchive.cpp:129
void addFile(const QString &filename)
Add a new file to this archive.
Definition: qgsarchive.cpp:107
bool removeFile(const QString &filename)
Remove a file from this archive and from the filesystem.
Definition: qgsarchive.cpp:112
QString dir() const
Returns the current temporary directory.
Definition: qgsarchive.cpp:54
QStringList files() const
Returns the list of files within this archive.
Definition: qgsarchive.cpp:124
static QString extension()
Returns the extension used for auxiliary databases.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QString projectFile() const
Returns the current .qgs project file or an empty string if there's none.
Definition: qgsarchive.cpp:134
QString auxiliaryStorageFile() const
Returns the current .qgd auxiliary storage file or an empty string if there's none.
Definition: qgsarchive.cpp:160
bool clearProjectFile()
Remove the current .qgs project file from the temporary directory.
Definition: qgsarchive.cpp:155
bool unzip(const QString &zipFilename) override
Clear the current content of this archive and unzip.
Definition: qgsarchive.cpp:147
CORE_EXPORT bool unzip(const QString &zip, const QString &dir, QStringList &files)
Unzip a zip file in an output directory.
Definition: qgsziputils.cpp:34
CORE_EXPORT bool zip(const QString &zip, const QStringList &files)
Zip the list of files in the zip file.