1 /************************************************************************
2 **
3 **  Copyright (C) 2015-2020 Kevin B. Hendricks, Stratford Ontario Canada
4 **  Copyright (C) 2009-2011 Strahinja Markovic  <strahinja.markovic@gmail.com>
5 **
6 **  This file is part of Sigil.
7 **
8 **  Sigil is free software: you can redistribute it and/or modify
9 **  it under the terms of the GNU General Public License as published by
10 **  the Free Software Foundation, either version 3 of the License, or
11 **  (at your option) any later version.
12 **
13 **  Sigil is distributed in the hope that it will be useful,
14 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 **  GNU General Public License for more details.
17 **
18 **  You should have received a copy of the GNU General Public License
19 **  along with Sigil.  If not, see <http://www.gnu.org/licenses/>.
20 **
21 *************************************************************************/
22 
23 #include <QDir>
24 #include <QtConcurrent>
25 #include <QDebug>
26 
27 #include "Misc/TempFolder.h"
28 #include "Misc/SettingsStore.h"
29 
TempFolder()30 TempFolder::TempFolder()
31     : m_tempDir(GetNewTempFolderTemplate())
32 {
33     // verify m_tempDir was properly created
34     if (!m_tempDir.isValid()) {
35         qDebug() << "Error: Invalid m_tempDir" << m_tempDir.path();
36     }
37 
38     // will be cleaned manually in the destructor
39     m_tempDir.setAutoRemove(false);
40 }
41 
~TempFolder()42 TempFolder::~TempFolder()
43 {
44     // To be super safe here ...
45     // only manually delete things if the temp directory is actually valid
46     if (m_tempDir.isValid()) {
47         QtConcurrent::run(DeleteFolderAndFiles, m_tempDir.path());
48     }
49 }
50 
51 
GetPath()52 QString TempFolder::GetPath()
53 {
54     return m_tempDir.path();
55 }
56 
57 
GetPathToSigilScratchpad()58 QString TempFolder::GetPathToSigilScratchpad()
59 {
60     SettingsStore ss;
61     QString temp_path = ss.tempFolderHome();
62     if (temp_path == "<SIGIL_DEFAULT_TEMP_HOME>") {
63         return QDir::tempPath();
64     }
65     if (!QDir(temp_path).exists()) {
66         return QDir::tempPath();
67     }
68     return temp_path;
69 }
70 
71 
GetNewTempFolderTemplate()72 QString TempFolder::GetNewTempFolderTemplate()
73 {
74     SettingsStore ss;
75     QString temp_path = ss.tempFolderHome();
76     if (temp_path == "<SIGIL_DEFAULT_TEMP_HOME>") {
77         return QDir::tempPath() + "/Sigil-XXXXXX";;
78     }
79     if (!QDir(temp_path).exists()) {
80         return QDir::tempPath() + "/Sigil-XXXXXX";;
81     }
82     return temp_path + "/Sigil-XXXXXX";
83 }
84 
85 
DeleteFolderAndFiles(const QString & fullfolderpath)86 bool TempFolder::DeleteFolderAndFiles(const QString &fullfolderpath)
87 {
88     QDir folder(fullfolderpath);
89     return folder.removeRecursively();
90 }
91 
92 
93 
94