From 3b7a54797c8009ec02c1f1575cea277c02774a72 Mon Sep 17 00:00:00 2001 From: Flavio Date: Wed, 26 Oct 2011 11:35:01 +0200 Subject: [PATCH] Temp files --- src/temporary.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++ src/temporary.h | 18 +++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/temporary.cpp create mode 100644 src/temporary.h diff --git a/src/temporary.cpp b/src/temporary.cpp new file mode 100644 index 0000000..a979cbd --- /dev/null +++ b/src/temporary.cpp @@ -0,0 +1,50 @@ +#include "temporary.h" +#include "constants.h" + +static QList paths; +#ifdef Q_WS_X11 +static QString userName; +#endif + +Temporary::Temporary() { } + +QString Temporary::filename() { + + static const QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation); + + QString tempFile = tempDir + "/" + Constants::UNIX_NAME + "-" + QString::number(qrand()); + +#ifdef Q_WS_X11 + if (userName.isNull()) { + userName = QString(getenv("USERNAME")); + if (userName.isEmpty()) + userName = QString(getenv("USER")); + } + if (!userName.isEmpty()) + tempFile += "-" + userName; +#endif + + if (QFile::exists(tempFile) && !QFile::remove(tempFile)) { + qDebug() << "Cannot remove temp file" << tempFile; + } + + paths << tempFile; + + if (paths.size() > 1) { + QString removedFile = paths.takeFirst(); + if (QFile::exists(removedFile) && !QFile::remove(removedFile)) { + qDebug() << "Cannot remove temp file" << removedFile; + } + } + + return tempFile; + +} + +void Temporary::deleteAll() { + foreach(QString path, paths) { + if (QFile::exists(path) && !QFile::remove(path)) { + qDebug() << "Cannot remove temp file" << path; + } + } +} diff --git a/src/temporary.h b/src/temporary.h new file mode 100644 index 0000000..50b9633 --- /dev/null +++ b/src/temporary.h @@ -0,0 +1,18 @@ +#ifndef TEMPORARY_H +#define TEMPORARY_H + +#include +#include + +class Temporary { + +public: + static QString filename(); + static void deleteAll(); + +private: + Temporary(); + +}; + +#endif // TEMPORARY_H -- 2.39.5