QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  Q_FOREACH ( const QString &file, files() )
117  {
118  QFileInfo fileInfo( file );
119  if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 )
120  return file;
121  }
122 
123  return QString();
124 }
125 
126 bool QgsProjectArchive::unzip( const QString &filename )
127 {
128  if ( QgsArchive::unzip( filename ) )
129  return ! projectFile().isEmpty();
130  else
131  return false;
132 }
133 
135 {
136  return removeFile( projectFile() );
137 }
138 
140 {
141  const QString extension = QgsAuxiliaryStorage::extension();
142 
143  const QStringList fileList = files();
144  for ( const QString &file : fileList )
145  {
146  const QFileInfo fileInfo( file );
147  if ( fileInfo.suffix().compare( extension, Qt::CaseInsensitive ) == 0 )
148  return file;
149  }
150 
151  return QString();
152 }
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:126
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
QString projectFile() const
Returns the current .qgs project file or an empty string if there&#39;s none.
Definition: qgsarchive.cpp:114
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
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).
QStringList files() const
Returns the list of files within this archive.
Definition: qgsarchive.cpp:109
QgsArchive & operator=(const QgsArchive &other)
Definition: qgsarchive.cpp:38
QString auxiliaryStorageFile() const
Returns the current .qgd auxiliary storage file or an empty string if there&#39;s none.
Definition: qgsarchive.cpp:139
QString dir() const
Returns the current temporary directory.
Definition: qgsarchive.cpp:46
void addFile(const QString &filename)
Add a new file to this archive.
Definition: qgsarchive.cpp:92
bool clearProjectFile()
Remove the current .qgs project file from the temporary directory.
Definition: qgsarchive.cpp:134
CORE_EXPORT bool zip(const QString &zip, const QStringList &files)
Zip the list of files in the zip file.