QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  QString tempPath = QStandardPaths::standardLocations( QStandardPaths::TempLocation ).at( 0 );
68  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  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  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 
130 {
131  const auto constFiles = files();
132  for ( const QString &file : constFiles )
133  {
134  QFileInfo fileInfo( file );
135  if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 )
136  return file;
137  }
138 
139  return QString();
140 }
141 
142 bool QgsProjectArchive::unzip( const QString &filename )
143 {
144  if ( QgsArchive::unzip( filename ) )
145  return ! projectFile().isEmpty();
146  else
147  return false;
148 }
149 
151 {
152  return removeFile( projectFile() );
153 }
154 
156 {
157  const QString extension = QgsAuxiliaryStorage::extension();
158 
159  const QStringList fileList = files();
160  for ( const QString &file : fileList )
161  {
162  const QFileInfo fileInfo( file );
163  if ( fileInfo.suffix().compare( extension, Qt::CaseInsensitive ) == 0 )
164  return file;
165  }
166 
167  return QString();
168 }
qgsziputils.h
QgsProjectArchive::unzip
bool unzip(const QString &zipFilename) override
Clear the current content of this archive and unzip.
Definition: qgsarchive.cpp:142
QgsArchive::unzip
virtual bool unzip(const QString &zipFilename)
Clear the current content of this archive and unzip.
Definition: qgsarchive.cpp:101
QgsArchive::zip
bool zip(const QString &zipFilename)
Zip the content of this archive.
Definition: qgsarchive.cpp:65
QgsArchive::dir
QString dir() const
Returns the current temporary directory.
Definition: qgsarchive.cpp:54
QgsArchive::clear
void clear()
Clear the current content of this archive and create a new temporary directory.
Definition: qgsarchive.cpp:59
QgsAuxiliaryStorage::extension
static QString extension()
Returns the extension used for auxiliary databases.
Definition: qgsauxiliarystorage.cpp:677
qgsauxiliarystorage.h
QgsArchive::QgsArchive
QgsArchive()
Constructor.
Definition: qgsarchive.cpp:32
QgsArchive::addFile
void addFile(const QString &filename)
Add a new file to this archive.
Definition: qgsarchive.cpp:107
QgsProjectArchive::projectFile
QString projectFile() const
Returns the current .qgs project file or an empty string if there's none.
Definition: qgsarchive.cpp:129
QgsArchive::files
QStringList files() const
Returns the list of files within this archive.
Definition: qgsarchive.cpp:124
QgsZipUtils::unzip
CORE_EXPORT bool unzip(const QString &zip, const QString &dir, QStringList &files)
Unzip a zip file in an output directory.
Definition: qgsziputils.cpp:34
QgsMessageLog::logMessage
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).
Definition: qgsmessagelog.cpp:27
QgsProjectArchive::clearProjectFile
bool clearProjectFile()
Remove the current .qgs project file from the temporary directory.
Definition: qgsarchive.cpp:150
QgsArchive::operator=
QgsArchive & operator=(const QgsArchive &other)
Definition: qgsarchive.cpp:43
QgsArchive
Class allowing to manage the zip/unzip actions.
Definition: qgsarchive.h:36
QgsProjectArchive::auxiliaryStorageFile
QString auxiliaryStorageFile() const
Returns the current .qgd auxiliary storage file or an empty string if there's none.
Definition: qgsarchive.cpp:155
qgsarchive.h
QgsArchive::removeFile
bool removeFile(const QString &filename)
Remove a file from this archive and from the filesystem.
Definition: qgsarchive.cpp:112
QgsZipUtils::zip
CORE_EXPORT bool zip(const QString &zip, const QStringList &files)
Zip the list of files in the zip file.
Definition: qgsziputils.cpp:132
qgsmessagelog.h