| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2024 Benjamin Nauck <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This program is free software; you can redistribute it and/or modify * |
| 7 |
* it under the terms of the GNU Library General Public License (LGPL) * |
| 8 |
* as published by the Free Software Foundation; either version 2 of * |
| 9 |
* the License, or (at your option) any later version. * |
| 10 |
* for detail see the LICENCE text file. * |
| 11 |
* * |
| 12 |
* FreeCAD is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with FreeCAD; if not, write to the Free Software * |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * |
| 20 |
* USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#include <QTemporaryDir> |
| 26 |
|
| 27 |
#include "Application.h" |
| 28 |
#include "FCConfig.h" |
| 29 |
|
| 30 |
#include "SafeMode.h" |
| 31 |
|
| 32 |
static QTemporaryDir* tempDir = nullptr; |
| 33 |
|
| 34 |
static bool _createTemporaryBaseDir() |
| 35 |
{ |
| 36 |
tempDir = new QTemporaryDir(); |
| 37 |
if (!tempDir->isValid()) { |
| 38 |
delete tempDir; |
| 39 |
tempDir = nullptr; |
| 40 |
} |
| 41 |
return tempDir; |
| 42 |
} |
| 43 |
|
| 44 |
static void _replaceDirs() |
| 45 |
{ |
| 46 |
auto& config = App::GetApplication().Config(); |
| 47 |
|
| 48 |
auto const temp_base = tempDir->path().toStdString(); |
| 49 |
auto const dirs = { |
| 50 |
"UserAppData", |
| 51 |
"UserConfigPath", |
| 52 |
"UserCachePath", |
| 53 |
"AppTempPath", |
| 54 |
"UserMacroPath", |
| 55 |
"UserHomePath", |
| 56 |
}; |
| 57 |
|
| 58 |
for (auto const d : dirs) { |
| 59 |
auto const path = temp_base + PATHSEP + d + PATHSEP; |
| 60 |
auto const qpath = QString::fromStdString(path); |
| 61 |
QDir().mkpath(qpath); |
| 62 |
config[d] = path; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void SafeMode::StartSafeMode() |
| 67 |
{ |
| 68 |
if (_createTemporaryBaseDir()) { |
| 69 |
_replaceDirs(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
bool SafeMode::SafeModeEnabled() |
| 74 |
{ |
| 75 |
return tempDir; |
| 76 |
} |
| 77 |
|
| 78 |
void SafeMode::Destruct() |
| 79 |
{ |
| 80 |
delete tempDir; |
| 81 |
} |