QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 #include <QStandardPaths>
25 #include <QUuid>
26 
28  : mDir( new QTemporaryDir() )
29 {
30 }
31 
33  : mFiles( other.mFiles )
34  , mDir( new QTemporaryDir() )
35 {
36 }
37 
39 {
40  if ( this != &other )
41  mFiles = other.mFiles;
42 
43  return *this;
44 }
45 
46 QString QgsArchive::dir() const
47 {
48  return mDir->path();
49 }
50 
52 {
53  mDir.reset( new QTemporaryDir() );
54  mFiles.clear();
55 }
56 
57 bool QgsArchive::zip( const QString &filename )
58 {
59  QString tempPath = QStandardPaths::standardLocations( QStandardPaths::TempLocation ).at( 0 );
60  QString uuid = QUuid::createUuid().toString();
61  QFile tmpFile( tempPath + QDir::separator() + uuid );
62 
63  // zip content
64  if ( ! QgsZipUtils::zip( tmpFile.fileName(), mFiles ) )
65  {
66  QString err = QObject::tr( "Unable to zip content" );
67  QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) );
68  return false;
69  }
70 
71  // remove existing zip file
72  if ( QFile::exists( filename ) )
73  QFile::remove( filename );
74 
75  // save zip archive
76  if ( ! tmpFile.rename( filename ) )
77  {
78  QString err = QObject::tr( "Unable to save zip file '%1'" ).arg( filename );
79  QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) );
80  return false;
81  }
82 
83  return true;
84 }
85 
86 bool QgsArchive::unzip( const QString &filename )
87 {
88  clear();
89  return QgsZipUtils::unzip( filename, mDir->path(), mFiles );
90 }
91 
92 void QgsArchive::addFile( const QString &file )
93 {
94  mFiles.append( file );
95 }
96 
97 bool QgsArchive::removeFile( const QString &file )
98 {
99  bool rc = false;
100 
101  if ( !file.isEmpty() && mFiles.contains( file ) && QFile::exists( file ) )
102  rc = QFile::remove( file );
103 
104  mFiles.removeOne( file );
105 
106  return rc;
107 }
108 
109 QStringList QgsArchive::files() const
110 {
111  return mFiles;
112 }
113 
115 {
116  const auto constFiles = files();
117  for ( const QString &file : constFiles )
118  {
119  QFileInfo fileInfo( file );
120  if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 )
121  return file;
122  }
123 
124  return QString();
125 }
126 
127 bool QgsProjectArchive::unzip( const QString &filename )
128 {
129  if ( QgsArchive::unzip( filename ) )
130  return ! projectFile().isEmpty();
131  else
132  return false;
133 }
134 
136 {
137  return removeFile( projectFile() );
138 }
139 
141 {
142  const QString extension = QgsAuxiliaryStorage::extension();
143 
144  const QStringList fileList = files();
145  for ( const QString &file : fileList )
146  {
147  const QFileInfo fileInfo( file );
148  if ( fileInfo.suffix().compare( extension, Qt::CaseInsensitive ) == 0 )
149  return file;
150  }
151 
152  return QString();
153 }
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:127
QgsArchive()
Constructor.
Definition: qgsarchive.cpp:27
void clear()
Clear the current content of this archive and create a new temporary directory.
Definition: qgsarchive.cpp:51
bool removeFile(const QString &filename)
Remove a file from this archive and from the filesystem.
Definition: qgsarchive.cpp:97
bool zip(const QString &zipFilename)
Zip the content of this archive.
Definition: qgsarchive.cpp:57
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:114
virtual bool unzip(const QString &zipFilename)
Clear the current content of this archive and unzip.
Definition: qgsarchive.cpp:86
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:38
void addFile(const QString &filename)
Add a new file to this archive.
Definition: qgsarchive.cpp:92
QStringList files() const
Returns the list of files within this archive.
Definition: qgsarchive.cpp:109
QString auxiliaryStorageFile() const
Returns the current .qgd auxiliary storage file or an empty string if there&#39;s none.
Definition: qgsarchive.cpp:140
bool clearProjectFile()
Remove the current .qgs project file from the temporary directory.
Definition: qgsarchive.cpp:135
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:46