Description: Switch TempFolder to QTemporaryDir
 Refactor TempFolder to use QTemporaryDir internally, instead of
 manually creating directories ourselves (which is not race-free).
 QTemporaryDir gives us a race-free temporary directory, although still
 cleaned manually for performance reasons.
 .
 The extra hierarchy (Sigil/scratchpad) in the system temporary
 directory is no more used, as the result now is unique enough, and not
 shared by all the users on the same machine. As a consequence, there is
 no more need to fix the permissions of the scratchpad.
Origin: upstram, a3febd6228aca7392f41ae6e7ef796c1317b35a9
Author: Pino Toscano <toscano.pino@tiscali.it>
Last-Update: 2015-12-02

diff --git a/src/Misc/TempFolder.cpp b/src/Misc/TempFolder.cpp
index 65e3e72..55a4b3c 100644
--- a/src/Misc/TempFolder.cpp
+++ b/src/Misc/TempFolder.cpp
@@ -19,71 +19,46 @@
 **
 *************************************************************************/
 
-#include <QtCore/QtCore>
 #include <QtCore/QDir>
-#include <QtCore/QFileInfo>
 #include <QtConcurrent/QtConcurrent>
 
 #include "Misc/TempFolder.h"
-#include "Misc/Utility.h"
-
-const QString PATH_SUFFIX = "/Sigil";
 
 TempFolder::TempFolder()
-    : m_PathToFolder(GetNewTempFolderPath())
+    : m_tempDir(GetNewTempFolderTemplate())
 {
-    QDir folder(m_PathToFolder);
-    folder.mkpath(folder.absolutePath());
+    // will be cleaned manually in the destructor
+    m_tempDir.setAutoRemove(false);
 }
 
 TempFolder::~TempFolder()
 {
-    QtConcurrent::run(DeleteFolderAndFiles, m_PathToFolder);
+    QtConcurrent::run(DeleteFolderAndFiles, m_tempDir.path());
 }
 
 
 QString TempFolder::GetPath()
 {
-    return m_PathToFolder;
+    return m_tempDir.path();
 }
 
 
 QString TempFolder::GetPathToSigilScratchpad()
 {
-    return QDir::tempPath() + PATH_SUFFIX + "/scratchpad";
+    return QDir::tempPath();
 }
 
 
-QString TempFolder::GetNewTempFolderPath()
+QString TempFolder::GetNewTempFolderTemplate()
 {
-    QString token = Utility::CreateUUID();
-    return GetPathToSigilScratchpad() + "/" + token;
+    return QDir::tempPath() + "/Sigil-XXXXXX";
 }
 
 
 bool TempFolder::DeleteFolderAndFiles(const QString &fullfolderpath)
 {
-    // Make sure the path exists, otherwise very
-    // bad things could happen
-    if (!QFileInfo(fullfolderpath).exists()) {
-        return false;
-    }
-
     QDir folder(fullfolderpath);
-    // Erase all the files in this folder
-    foreach(QFileInfo file, folder.entryInfoList(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot|QDir::Hidden)) {
-        // If it's a file, delete it
-        if (file.isFile()) {
-            folder.remove(file.fileName());
-        }
-        // Else it's a directory, delete it recursively
-        else {
-            DeleteFolderAndFiles(file.absoluteFilePath());
-        }
-    }
-    // Delete the folder after it's empty
-    folder.rmdir(folder.absolutePath());
-    return true;
+    return folder.removeRecursively();
 }
 
 
diff --git a/src/Misc/TempFolder.h b/src/Misc/TempFolder.h
index b12e44c..8d129d1 100644
--- a/src/Misc/TempFolder.h
+++ b/src/Misc/TempFolder.h
@@ -24,6 +24,7 @@
 #define TEMPFOLDER_H
 
 #include <QtCore/QString>
+#include <QtCore/QTemporaryDir>
 
 /**
  * A RAII wrapper around a temp folder. Creating an object of this
@@ -70,12 +71,12 @@ private:
     TempFolder(const TempFolder &);
 
     /**
-     * Provides a full path to a new temp folder.
-     * It is the callers responsibility to create the folder.
+     * Provides the template for QTemporaryDir for new temporary
+     * folders.
      *
-     * @return Full path to a new temp folder.
+     * @return Absolute path to a new temp folder template.
      */
-    static QString GetNewTempFolderPath();
+    static QString GetNewTempFolderTemplate();
 
     /**
      * Deletes the folder specified and all the files
@@ -94,7 +95,7 @@ private:
     /**
      * The full path to the temp folder.
      */
-    QString m_PathToFolder;
+    QTemporaryDir m_tempDir;
 };
 
 
diff --git a/src/main.cpp b/src/main.cpp
index c6be44e..6651ee0 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -189,19 +189,6 @@ void MessageHandler(QtMsgType type, const QMessageLogContext &context, const QSt
 }
 
 
-/**
- * Creates (or modifies, if it already exists) the Sigil temp folder so that it
- * can be read and modified by anyone.
- */
-void CreateTempFolderWithCorrectPermissions()
-{
-    QString temp_path = TempFolder::GetPathToSigilScratchpad();
-    QDir(temp_path).mkpath(temp_path);
-    QFile::setPermissions(temp_path, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
-                          QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup |
-                          QFile::ReadOther | QFile::WriteOther | QFile::ExeOther);
-}
-
 void VerifyPlugins()
 {
     PluginDB *pdb = PluginDB::instance();
@@ -258,12 +245,6 @@ int main(int argc, char *argv[])
 #if !defined(Q_OS_WIN32) && !defined(Q_OS_MAC)
         app.setWindowIcon(GetApplicationIcon());
 #endif
-        // On Unix systems, we make sure that the temp folder we
-        // create is accessible by all users. On Windows, there's
-        // a temp folder per user.
-#ifndef Q_OS_WIN32
-        CreateTempFolderWithCorrectPermissions();
-#endif
         // Needs to be created on the heap so that
         // the reply has time to return.
         UpdateChecker *checker = new UpdateChecker(&app);
