From: Flavio Date: Wed, 26 Oct 2011 09:35:01 +0000 (+0200) Subject: Temp files X-Git-Tag: 1.6~2 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=3b7a54797c8009ec02c1f1575cea277c02774a72;p=minitube Temp files --- 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