summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElvis Angelaccio <elvis.angelaccio@kde.org>2016-11-12 16:52:39 (GMT)
committerElvis Angelaccio <elvis.angelaccio@kde.org>2016-11-12 17:07:08 (GMT)
commita2a39db8a665b0c6e63b3c82955f22fc414610b7 (patch)
tree6977f2fbd4aa7ed030dbdc4dbd8ef8e45fb6ad13
parent0025a4f2529f2bf1e541a89e4519a907754a6e67 (diff)
cliinterface: use QScopedPointer for temp folders/files
This simplifies the code and also ensures that the temp folders are always deleted.
-rw-r--r--kerfuffle/cliinterface.cpp36
-rw-r--r--kerfuffle/cliinterface.h8
-rw-r--r--plugins/clizipplugin/cliplugin.cpp8
3 files changed, 19 insertions, 33 deletions
diff --git a/kerfuffle/cliinterface.cpp b/kerfuffle/cliinterface.cpp
index f2aa946..f7988ba 100644
--- a/kerfuffle/cliinterface.cpp
+++ b/kerfuffle/cliinterface.cpp
@@ -74,7 +74,6 @@ CliInterface::CliInterface(QObject *parent, const QVariantList & args)
CliInterface::~CliInterface()
{
Q_ASSERT(!m_process);
- delete m_commentTempFile;
}
void CliInterface::setListEmptyLines(bool emptyLines)
@@ -124,9 +123,7 @@ bool CliInterface::extractFiles(const QVector<Archive::Entry*> &files, const QSt
const bool useTmpExtractDir = options.isDragAndDropEnabled() || options.alwaysUseTempDir();
if (useTmpExtractDir) {
-
- Q_ASSERT(!m_extractTempDir);
- m_extractTempDir = new QTemporaryDir(QApplication::applicationName() + QLatin1Char('-'));
+ m_extractTempDir.reset(new QTemporaryDir(QApplication::applicationName() + QLatin1Char('-')));
qCDebug(ARK) << "Using temporary extraction dir:" << m_extractTempDir->path();
if (!m_extractTempDir->isValid()) {
@@ -162,7 +159,7 @@ bool CliInterface::addFiles(const QVector<Archive::Entry*> &files, const Archive
qCDebug(ARK) << "Adding" << files.count() << "file(s) to destination:" << destinationPath;
if (!destinationPath.isEmpty()) {
- m_extractTempDir = new QTemporaryDir();
+ m_extractTempDir.reset(new QTemporaryDir());
const QString absoluteDestinationPath = m_extractTempDir->path() + QLatin1Char('/') + destinationPath;
QDir qDir;
@@ -182,8 +179,6 @@ bool CliInterface::addFiles(const QVector<Archive::Entry*> &files, const Archive
qCDebug(ARK) << "Symlink's created:" << filePath << newFilePath;
} else {
qCDebug(ARK) << "Can't create symlink" << filePath << newFilePath;
- delete m_extractTempDir;
- m_extractTempDir = Q_NULLPTR;
emit finished(false);
return false;
}
@@ -235,9 +230,9 @@ bool CliInterface::moveFiles(const QVector<Archive::Entry*> &files, Archive::Ent
bool CliInterface::copyFiles(const QVector<Archive::Entry*> &files, Archive::Entry *destination, const CompressionOptions &options)
{
m_oldWorkingDir = QDir::currentPath();
- m_tempExtractDir = new QTemporaryDir();
- m_tempAddDir = new QTemporaryDir();
- QDir::setCurrent(m_tempExtractDir->path());
+ m_tempWorkingDir.reset(new QTemporaryDir());
+ m_tempAddDir.reset(new QTemporaryDir());
+ QDir::setCurrent(m_tempWorkingDir->path());
m_passedFiles = files;
m_passedDestination = destination;
m_passedOptions = options;
@@ -340,10 +335,6 @@ void CliInterface::processFinished(int exitCode, QProcess::ExitStatus exitStatus
}
if (m_operationMode == Add && !isMultiVolume()) {
- if (m_extractTempDir) {
- delete m_extractTempDir;
- m_extractTempDir = Q_NULLPTR;
- }
list();
} else if (m_operationMode == List && isCorrupt()) {
Kerfuffle::LoadCorruptQuery query(filename());
@@ -546,10 +537,7 @@ void CliInterface::cleanUpExtracting()
QDir::setCurrent(m_oldWorkingDir);
}
- if (m_extractTempDir) {
- delete m_extractTempDir;
- m_extractTempDir = Q_NULLPTR;
- }
+ m_extractTempDir.reset();
}
void CliInterface::finishCopying(bool result)
@@ -735,10 +723,8 @@ void CliInterface::cleanUp()
qDeleteAll(m_tempAddedFiles);
m_tempAddedFiles.clear();
QDir::setCurrent(m_oldWorkingDir);
- delete m_tempExtractDir;
- m_tempExtractDir = Q_NULLPTR;
- delete m_tempAddDir;
- m_tempAddDir = Q_NULLPTR;
+ m_tempWorkingDir.reset();
+ m_tempAddDir.reset();
}
void CliInterface::readStdout(bool handleAll)
@@ -825,7 +811,7 @@ bool CliInterface::setAddedFiles()
{
QDir::setCurrent(m_tempAddDir->path());
foreach (const Archive::Entry *file, m_passedFiles) {
- const QString oldPath = m_tempExtractDir->path() + QLatin1Char('/') + file->fullPath(NoTrailingSlash);
+ const QString oldPath = m_tempWorkingDir->path() + QLatin1Char('/') + file->fullPath(NoTrailingSlash);
const QString newPath = m_tempAddDir->path() + QLatin1Char('/') + file->name();
if (!QFile::rename(oldPath, newPath)) {
return false;
@@ -1064,14 +1050,14 @@ bool CliInterface::addComment(const QString &comment)
{
m_operationMode = Comment;
- m_commentTempFile = new QTemporaryFile;
+ m_commentTempFile.reset(new QTemporaryFile());
if (!m_commentTempFile->open()) {
qCWarning(ARK) << "Failed to create temporary file for comment";
emit finished(false);
return false;
}
- QTextStream stream(m_commentTempFile);
+ QTextStream stream(m_commentTempFile.data());
stream << comment << endl;
m_commentTempFile->close();
diff --git a/kerfuffle/cliinterface.h b/kerfuffle/cliinterface.h
index 9947a1a..6d456b0 100644
--- a/kerfuffle/cliinterface.h
+++ b/kerfuffle/cliinterface.h
@@ -139,8 +139,8 @@ protected:
CliProperties *m_cliProps = Q_NULLPTR;
QString m_oldWorkingDir;
- QTemporaryDir *m_tempExtractDir = Q_NULLPTR;
- QTemporaryDir *m_tempAddDir = Q_NULLPTR;
+ QScopedPointer<QTemporaryDir> m_tempWorkingDir;
+ QScopedPointer<QTemporaryDir> m_tempAddDir;
OperationMode m_subOperation = List;
QVector<Archive::Entry*> m_passedFiles;
QVector<Archive::Entry*> m_tempAddedFiles;
@@ -211,8 +211,8 @@ private:
ExtractionOptions m_extractionOptions;
QString m_extractDestDir;
- QTemporaryDir *m_extractTempDir = Q_NULLPTR;
- QTemporaryFile *m_commentTempFile = Q_NULLPTR;
+ QScopedPointer<QTemporaryDir> m_extractTempDir;
+ QScopedPointer<QTemporaryFile> m_commentTempFile;
QVector<Archive::Entry*> m_extractedFiles;
uint m_archiveSizeOnDisk = 0;
uint m_listedSize = 0;
diff --git a/plugins/clizipplugin/cliplugin.cpp b/plugins/clizipplugin/cliplugin.cpp
index 4665d04..a61cdaf 100644
--- a/plugins/clizipplugin/cliplugin.cpp
+++ b/plugins/clizipplugin/cliplugin.cpp
@@ -192,9 +192,9 @@ bool CliPlugin::moveFiles(const QVector<Archive::Entry*> &files, Archive::Entry
qCDebug(ARK) << "Moving" << files.count() << "file(s) to destination:" << destination;
m_oldWorkingDir = QDir::currentPath();
- m_tempExtractDir = new QTemporaryDir();
- m_tempAddDir = new QTemporaryDir();
- QDir::setCurrent(m_tempExtractDir->path());
+ m_tempWorkingDir.reset(new QTemporaryDir());
+ m_tempAddDir.reset(new QTemporaryDir());
+ QDir::setCurrent(m_tempWorkingDir->path());
m_passedFiles = files;
m_passedDestination = destination;
m_passedOptions = options;
@@ -248,7 +248,7 @@ bool CliPlugin::setMovingAddedFiles()
QDir::setCurrent(m_tempAddDir->path());
const Archive::Entry *file = m_passedFiles.at(0);
- const QString oldPath = m_tempExtractDir->path() + QLatin1Char('/') + file->fullPath(NoTrailingSlash);
+ const QString oldPath = m_tempWorkingDir->path() + QLatin1Char('/') + file->fullPath(NoTrailingSlash);
const QString newPath = m_tempAddDir->path() + QLatin1Char('/') + m_passedDestination->name();
if (!QFile::rename(oldPath, newPath)) {
return false;