QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  mFiles = other.mFiles;
47 
48  return *this;
49 }
50 
51 QString QgsArchive::dir() const
52 {
53  return mDir->path();
54 }
55 
57 {
58  mDir.reset( new QTemporaryDir() );
59  mFiles.clear();
60 }
61 
62 bool QgsArchive::zip( const QString &filename )
63 {
64  QString tempPath = QStandardPaths::standardLocations( QStandardPaths::TempLocation ).at( 0 );
65  QString uuid = QUuid::createUuid().toString();
66  QFile tmpFile( tempPath + QDir::separator() + uuid );
67 
68  // zip content
69  if ( ! QgsZipUtils::zip( tmpFile.fileName(), mFiles ) )
70  {
71  QString err = QObject::tr( "Unable to zip content" );
72  QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) );
73  return false;
74  }
75 
76  // remove existing zip file
77  if ( QFile::exists( filename ) )
78  QFile::remove( filename );
79 
80 #ifdef Q_OS_WIN
81  // Clear temporary flag (see GH #32118)
82  DWORD dwAttrs;
83  dwAttrs = GetFileAttributes( tmpFile.fileName().toLocal8Bit( ).data( ) );
84  SetFileAttributes( tmpFile.fileName().toLocal8Bit( ).data( ), dwAttrs & ~ FILE_ATTRIBUTE_TEMPORARY );
85 #endif // Q_OS_WIN
86 
87  // save zip archive
88  if ( ! tmpFile.rename( filename ) )
89  {
90  QString err = QObject::tr( "Unable to save zip file '%1'" ).arg( filename );
91  QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) );
92  return false;
93  }
94 
95  return true;
96 }
97 
98 bool QgsArchive::unzip( const QString &filename )
99 {
100  clear();
101  return QgsZipUtils::unzip( filename, mDir->path(), mFiles );
102 }
103 
104 void QgsArchive::addFile( const QString &file )
105 {
106  mFiles.append( file );
107 }
108 
109 bool QgsArchive::removeFile( const QString &file )
110 {
111  bool rc = false;
112 
113  if ( !file.isEmpty() && mFiles.contains( file ) && QFile::exists( file ) )
114  rc = QFile::remove( file );
115 
116  mFiles.removeOne( file );
117 
118  return rc;
119 }
120 
121 QStringList QgsArchive::files() const
122 {
123  return mFiles;
124 }
125 
127 {
128  const auto constFiles = files();
129  for ( const QString &file : constFiles )
130  {
131  QFileInfo fileInfo( file );
132  if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 )
133  return file;
134  }
135 
136  return QString();
137 }
138 
139 bool QgsProjectArchive::unzip( const QString &filename )
140 {
141  if ( QgsArchive::unzip( filename ) )
142  return ! projectFile().isEmpty();
143  else
144  return false;
145 }
146 
148 {
149  return removeFile( projectFile() );
150 }
151 
153 {
154  const QString extension = QgsAuxiliaryStorage::extension();
155 
156  const QStringList fileList = files();
157  for ( const QString &file : fileList )
158  {
159  const QFileInfo fileInfo( file );
160  if ( fileInfo.suffix().compare( extension, Qt::CaseInsensitive ) == 0 )
161  return file;
162  }
163 
164  return QString();
165 }
Class allowing to manage the zip/unzip actions.
Definition: qgsarchive.h:35
bool unzip(const QString &zipFilename) override
Clear the current content of this archive and unzip.
Definition: qgsarchive.cpp:139
QgsArchive()
Constructor.
Definition: qgsarchive.cpp:32
void clear()
Clear the current content of this archive and create a new temporary directory.
Definition: qgsarchive.cpp:56
bool removeFile(const QString &filename)
Remove a file from this archive and from the filesystem.
Definition: qgsarchive.cpp:109
bool zip(const QString &zipFilename)
Zip the content of this archive.
Definition: qgsarchive.cpp:62
static QString extension()
Returns the extension used for auxiliary databases.
CORE_EXPORT bool unzip(const QString &zip, const QString &dir, QStringList &files)
Unzip a zip file in an output directory.
Definition: qgsziputils.cpp:34
QString projectFile() const
Returns the current .qgs project file or an empty string if there&#39;s none.
Definition: qgsarchive.cpp:126
virtual bool unzip(const QString &zipFilename)
Clear the current content of this archive and unzip.
Definition: qgsarchive.cpp:98
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QgsArchive & operator=(const QgsArchive &other)
Definition: qgsarchive.cpp:43
void addFile(const QString &filename)
Add a new file to this archive.
Definition: qgsarchive.cpp:104
QStringList files() const
Returns the list of files within this archive.
Definition: qgsarchive.cpp:121
QString auxiliaryStorageFile() const
Returns the current .qgd auxiliary storage file or an empty string if there&#39;s none.
Definition: qgsarchive.cpp:152
bool clearProjectFile()
Remove the current .qgs project file from the temporary directory.
Definition: qgsarchive.cpp:147
CORE_EXPORT bool zip(const QString &zip, const QStringList &files)
Zip the list of files in the zip file.
QString dir() const
Returns the current temporary directory.
Definition: qgsarchive.cpp:51