]> git.sur5r.net Git - minitube/commitdiff
Temp files
authorFlavio <flavio@odisseo.local>
Wed, 26 Oct 2011 09:35:01 +0000 (11:35 +0200)
committerFlavio <flavio@odisseo.local>
Wed, 26 Oct 2011 09:35:01 +0000 (11:35 +0200)
src/temporary.cpp [new file with mode: 0644]
src/temporary.h [new file with mode: 0644]

diff --git a/src/temporary.cpp b/src/temporary.cpp
new file mode 100644 (file)
index 0000000..a979cbd
--- /dev/null
@@ -0,0 +1,50 @@
+#include "temporary.h"
+#include "constants.h"
+
+static QList<QString> 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 (file)
index 0000000..50b9633
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef TEMPORARY_H
+#define TEMPORARY_H
+
+#include <QtCore>
+#include <QDesktopServices>
+
+class Temporary {
+
+public:
+    static QString filename();
+    static void deleteAll();
+
+private:
+    Temporary();
+
+};
+
+#endif // TEMPORARY_H