]> git.sur5r.net Git - minitube/blob - src/temporary.cpp
a979cbd6b44c78da639ef54f378d622bcd05dd31
[minitube] / src / temporary.cpp
1 #include "temporary.h"
2 #include "constants.h"
3
4 static QList<QString> paths;
5 #ifdef Q_WS_X11
6 static QString userName;
7 #endif
8
9 Temporary::Temporary() { }
10
11 QString Temporary::filename() {
12
13     static const QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
14
15     QString tempFile = tempDir + "/" + Constants::UNIX_NAME + "-" + QString::number(qrand());
16
17 #ifdef Q_WS_X11
18     if (userName.isNull()) {
19         userName = QString(getenv("USERNAME"));
20         if (userName.isEmpty())
21             userName = QString(getenv("USER"));
22     }
23     if (!userName.isEmpty())
24         tempFile += "-" + userName;
25 #endif
26
27     if (QFile::exists(tempFile) && !QFile::remove(tempFile)) {
28         qDebug() << "Cannot remove temp file" << tempFile;
29     }
30
31     paths << tempFile;
32
33     if (paths.size() > 1) {
34         QString removedFile = paths.takeFirst();
35         if (QFile::exists(removedFile) && !QFile::remove(removedFile)) {
36             qDebug() << "Cannot remove temp file" << removedFile;
37         }
38     }
39
40     return tempFile;
41
42 }
43
44 void Temporary::deleteAll() {
45     foreach(QString path, paths) {
46         if (QFile::exists(path) && !QFile::remove(path)) {
47             qDebug() << "Cannot remove temp file" << path;
48         }
49     }
50 }