QGIS API Documentation  3.6.0-Noosa (5873452)
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 #include <iostream>
24 
25 #include <QStandardPaths>
26 #include <QUuid>
27 
29  : mDir( new QTemporaryDir() )
30 {
31 }
32 
34  : mFiles( other.mFiles )
35  , mDir( new QTemporaryDir() )
36 {
37 }
38 
40 {
41  if ( this != &other )
42  mFiles = other.mFiles;
43 
44  return *this;
45 }
46 
47 QString QgsArchive::dir() const
48 {
49  return mDir->path();
50 }
51 
53 {
54  mDir.reset( new QTemporaryDir() );
55  mFiles.clear();
56 }
57 
58 bool QgsArchive::zip( const QString &filename )
59 {
60  QString tempPath = QStandardPaths::standardLocations( QStandardPaths::TempLocation ).at( 0 );
61  QString uuid = QUuid::createUuid().toString();
62  QFile tmpFile( tempPath + QDir::separator() + uuid );
63 
64  // zip content
65  if ( ! QgsZipUtils::zip( tmpFile.fileName(), mFiles ) )
66  {
67  QString err = QObject::tr( "Unable to zip content" );
68  QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) );
69  return false;
70  }
71 
72  // remove existing zip file
73  if ( QFile::exists( filename ) )
74  QFile::remove( filename );
75 
76  // save zip archive
77  if ( ! tmpFile.rename( filename ) )
78  {
79  QString err = QObject::tr( "Unable to save zip file '%1'" ).arg( filename );
80  QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) );
81  return false;
82  }
83 
84  return true;
85 }
86 
87 bool QgsArchive::unzip( const QString &filename )
88 {
89  clear();
90  return QgsZipUtils::unzip( filename, mDir->path(), mFiles );
91 }
92 
93 void QgsArchive::addFile( const QString &file )
94 {
95  mFiles.append( file );
96 }
97 
98 bool QgsArchive::removeFile( const QString &file )
99 {
100  bool rc = false;
101 
102  if ( !file.isEmpty() && mFiles.contains( file ) && QFile::exists( file ) )
103  rc = QFile::remove( file );
104 
105  mFiles.removeOne( file );
106 
107  return rc;
108 }
109 
110 QStringList QgsArchive::files() const
111 {
112  return mFiles;
113 }
114 
116 {
117  Q_FOREACH ( const QString &file, files() )
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:34
bool unzip(const QString &zipFilename) override
Clear the current content of this archive and unzip.
Definition: qgsarchive.cpp:127
QgsArchive()
Constructor.
Definition: qgsarchive.cpp:28
void clear()
Clear the current content of this archive and create a new temporary directory.
Definition: qgsarchive.cpp:52
bool removeFile(const QString &filename)
Remove a file from this archive and from the filesystem.
Definition: qgsarchive.cpp:98
bool zip(const QString &zipFilename)
Zip the content of this archive.
Definition: qgsarchive.cpp:58
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:115
virtual bool unzip(const QString &zipFilename)
Clear the current content of this archive and unzip.
Definition: qgsarchive.cpp:87
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:39
void addFile(const QString &filename)
Add a new file to this archive.
Definition: qgsarchive.cpp:93
QStringList files() const
Returns the list of files within this archive.
Definition: qgsarchive.cpp:110
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:47