|
| 1 | +/*************************************************************************** |
| 2 | + qgsarchive.cpp |
| 3 | + ---------------- |
| 4 | +
|
| 5 | + begin : July 07, 2017 |
| 6 | + copyright : (C) 2017 by Paul Blottiere |
| 7 | + email : paul.blottiere@oslandia.com |
| 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 | + |
| 23 | +QgsArchive::QgsArchive() |
| 24 | + : mDir( new QTemporaryDir() ) |
| 25 | +{ |
| 26 | +} |
| 27 | + |
| 28 | +QgsArchive::~QgsArchive() |
| 29 | +{ |
| 30 | +} |
| 31 | + |
| 32 | +QString QgsArchive::dir() const |
| 33 | +{ |
| 34 | + return mDir->path(); |
| 35 | +} |
| 36 | + |
| 37 | +void QgsArchive::clear() |
| 38 | +{ |
| 39 | + mDir.reset( new QTemporaryDir() ); |
| 40 | + mFilename.clear(); |
| 41 | + mFiles.clear(); |
| 42 | +} |
| 43 | + |
| 44 | +bool QgsArchive::zip( const QString &filename ) |
| 45 | +{ |
| 46 | + // create a temporary path |
| 47 | + QTemporaryFile tmpFile; |
| 48 | + tmpFile.open(); |
| 49 | + tmpFile.close(); |
| 50 | + |
| 51 | + // zip content |
| 52 | + if ( ! QgsZipUtils::zip( tmpFile.fileName(), mFiles ) ) |
| 53 | + { |
| 54 | + QString err = QObject::tr( "Unable to zip content" ); |
| 55 | + QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) ); |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + // remove existing zip file |
| 60 | + if ( QFile::exists( filename ) ) |
| 61 | + QFile::remove( filename ); |
| 62 | + |
| 63 | + // save zip archive |
| 64 | + if ( ! tmpFile.rename( filename ) ) |
| 65 | + { |
| 66 | + QString err = QObject::tr( "Unable to save zip file '%1'" ).arg( filename ); |
| 67 | + QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) ); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + // keep the zip filename |
| 72 | + tmpFile.setAutoRemove( false ); |
| 73 | + mFilename = filename; |
| 74 | + |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
| 78 | +bool QgsArchive::unzip( const QString &filename ) |
| 79 | +{ |
| 80 | + clear(); |
| 81 | + |
| 82 | + QgsZipUtils::unzip( filename, mDir->path(), mFiles ); |
| 83 | + mFilename = filename; |
| 84 | + |
| 85 | + return ! projectFile().isEmpty(); |
| 86 | +} |
| 87 | + |
| 88 | +void QgsArchive::addFile( const QString &file ) |
| 89 | +{ |
| 90 | + mFiles.append( file ); |
| 91 | +} |
| 92 | + |
| 93 | +QString QgsArchive::filename() const |
| 94 | +{ |
| 95 | + return mFilename; |
| 96 | +} |
| 97 | + |
| 98 | +QString QgsArchive::projectFile() const |
| 99 | +{ |
| 100 | + Q_FOREACH ( const QString &file, mFiles ) |
| 101 | + { |
| 102 | + QFileInfo fileInfo( file ); |
| 103 | + if ( "qgs" == fileInfo.suffix().toLower() ) |
| 104 | + return file; |
| 105 | + } |
| 106 | + |
| 107 | + return QString(); |
| 108 | +} |
| 109 | + |
| 110 | +QStringList QgsArchive::files() const |
| 111 | +{ |
| 112 | + return mFiles; |
| 113 | +} |
0 commit comments