Add "Safe mode" which temporarily disables configs and addons (#16858)

* Add safe-mode which starts freecad in a clean environment

* Integrate safe-mode

* Show "(Safe Mode)" in main window title

* Show safe mode info when safe mode is started

* Created Safe Mode Icon

* Use new icon

* Add PreCompiled.h

* Add exports for windows

---------

Co-authored-by: Gabriel <gaz082@gmail.com>
This commit is contained in:
Benjamin Nauck
2024-10-21 18:08:52 +02:00
committed by GitHub
parent 66002908c7
commit 4eb7d5d09c
9 changed files with 394 additions and 1 deletions

View File

@@ -137,6 +137,8 @@
#include <App/TestScript.h>
#include <App/CMakeScript.h>
#include "SafeMode.h"
#ifdef _MSC_VER // New handler for Microsoft Visual C++ compiler
# pragma warning( disable : 4535 )
# if !defined(_DEBUG) && defined(HAVE_SEH)
@@ -1742,6 +1744,7 @@ void Application::destruct()
Base::InterpreterSingleton::Destruct();
Base::Type::destruct();
ParameterManager::Terminate();
SafeMode::Destruct();
}
void Application::destructObserver()
@@ -2230,6 +2233,7 @@ void parseProgramOptions(int ac, char ** av, const string& exe, variables_map& v
("module-path,M", value< vector<string> >()->composing(),"Additional module paths")
("python-path,P", value< vector<string> >()->composing(),"Additional python paths")
("single-instance", "Allow to run a single instance of the application")
("safe-mode", "Force enable safe mode")
("pass", value< vector<string> >()->multitoken(), "Ignores the following arguments and pass them through to be used by a script")
;
@@ -2557,6 +2561,10 @@ void Application::initConfig(int argc, char ** argv)
// extract home paths
ExtractUserPath();
if (vm.count("safe-mode")) {
SafeMode::StartSafeMode();
}
# ifdef FC_DEBUG
mConfig["Debug"] = "1";

View File

@@ -288,6 +288,7 @@ SET(FreeCADApp_CPP_SRCS
Metadata.cpp
MetadataPyImp.cpp
ElementNamingUtils.cpp
SafeMode.cpp
StringHasher.cpp
StringHasherPyImp.cpp
StringIDPyImp.cpp

79
src/App/SafeMode.cpp Normal file
View File

@@ -0,0 +1,79 @@
/***************************************************************************
* Copyright (c) 2024 Benjamin Nauck <benjamin@nauck.se> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* FreeCAD is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with FreeCAD; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#include <QTemporaryDir>
#include "Application.h"
#include "FCConfig.h"
#include "SafeMode.h"
static QTemporaryDir * tempDir = nullptr;
static bool _createTemporaryBaseDir() {
tempDir = new QTemporaryDir();
if (!tempDir->isValid()) {
delete tempDir;
tempDir = nullptr;
}
return tempDir;
}
static void _replaceDirs()
{
auto &config = App::GetApplication().Config();
auto const temp_base = tempDir->path().toStdString();
auto const dirs = {
"UserAppData",
"UserConfigPath",
"UserCachePath",
"AppTempPath",
"UserMacroPath",
"UserHomePath",
};
for (auto const d : dirs) {
auto const path = temp_base + PATHSEP + d + PATHSEP;
auto const qpath = QString::fromStdString(path);
QDir().mkpath(qpath);
config[d] = path;
}
}
void SafeMode::StartSafeMode()
{
if (_createTemporaryBaseDir()) {
_replaceDirs();
}
}
bool SafeMode::SafeModeEnabled() {
return tempDir;
}
void SafeMode::Destruct() {
delete tempDir;
}

30
src/App/SafeMode.h Normal file
View File

@@ -0,0 +1,30 @@
/***************************************************************************
* Copyright (c) 2024 Benjamin Nauck <benjamin@nauck.se> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* FreeCAD is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with FreeCAD; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
* *
***************************************************************************/
#pragma once
namespace SafeMode {
AppExport bool SafeModeEnabled();
AppExport void StartSafeMode();
AppExport void Destruct();
};

View File

@@ -29,6 +29,9 @@
# include <QRegularExpression>
# include <QRegularExpressionMatch>
# include <QWhatsThis>
# include <QAbstractButton>
# include <QTimer>
# include <QProcess>
#endif
#include <App/Document.h>
@@ -319,6 +322,53 @@ void StdCmdWhatsThis::activated(int iMsg)
QWhatsThis::enterWhatsThisMode();
}
//===========================================================================
// Std_RestartInSafeMode
//===========================================================================
DEF_STD_CMD(StdCmdRestartInSafeMode)
StdCmdRestartInSafeMode::StdCmdRestartInSafeMode()
:Command("Std_RestartInSafeMode")
{
sGroup = "Help";
sMenuText = QT_TR_NOOP("Restart in safe mode");
sToolTipText = QT_TR_NOOP("Restart in safe mode");
sWhatsThis = "Std_RestartInSafeMode";
sStatusTip = QT_TR_NOOP("Restart in safe mode");
sPixmap = "safe-mode-restart";
eType = 0;
}
void StdCmdRestartInSafeMode::activated(int iMsg)
{
Q_UNUSED(iMsg);
QMessageBox restartBox;
restartBox.setIcon(QMessageBox::Warning);
restartBox.setWindowTitle(QObject::tr("Restart in safe mode"));
restartBox.setText(QObject::tr("Are you sure you want to restart FreeCAD and enter safe mode?"));
restartBox.setInformativeText(QObject::tr("Safe mode temporarily disables your configuration and addons."));
restartBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
restartBox.setDefaultButton(QMessageBox::No);
if (restartBox.exec() == QMessageBox::Yes) {
//restart FreeCAD after a delay to give time to this dialog to close
const int ms = 1000;
QTimer::singleShot(ms, []()
{
QStringList args = QApplication::arguments();
args.pop_front();
auto const safeModeArgument = QString::fromLatin1("--safe-mode");
if (!args.contains(safeModeArgument)) {
args.append(safeModeArgument);
}
if (getMainWindow()->close()) {
QProcess::startDetached(QApplication::applicationFilePath(), args);
}
});
}
}
//===========================================================================
// Std_DlgParameter
//===========================================================================
@@ -938,6 +988,7 @@ void CreateStdCommands()
rcCmdMgr.addCommand(new StdCmdRecentFiles());
rcCmdMgr.addCommand(new StdCmdRecentMacros());
rcCmdMgr.addCommand(new StdCmdWhatsThis());
rcCmdMgr.addCommand(new StdCmdRestartInSafeMode());
rcCmdMgr.addCommand(new StdCmdPythonHelp());
rcCmdMgr.addCommand(new StdCmdOnlineHelp());
rcCmdMgr.addCommand(new StdCmdOnlineHelpWebsite());

View File

@@ -135,6 +135,7 @@
<file>internet-web-browser.svg</file>
<file>InTray.svg</file>
<file>InTray_missed_notifications.svg</file>
<file>safe-mode-restart.svg</file>
<file>view-select.svg</file>
<file>view-unselectable.svg</file>
<file>view-refresh.svg</file>
@@ -391,6 +392,7 @@
<file>accessories-text-editor.svg</file>
<file>accessories-calculator.svg</file>
<file>internet-web-browser.svg</file>
<file>safe-mode-restart.svg</file>
<file>view-select.svg</file>
<file>view-unselectable.svg</file>
<file>view-refresh.svg</file>

View File

@@ -0,0 +1,200 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="64"
height="64"
id="svg2869"
version="1.1"
viewBox="0 0 64 64"
sodipodi:docname="freecad-safe-mode.svg"
inkscape:version="1.3.2 (091e20ef0f, 2023-11-25, custom)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<title
id="title1">Safe Mode Icon</title>
<sodipodi:namedview
id="namedview1"
pagecolor="#ffffff"
bordercolor="#111111"
borderopacity="1"
inkscape:showpageshadow="0"
inkscape:pageopacity="0"
inkscape:pagecheckerboard="1"
inkscape:deskcolor="#d1d1d1"
inkscape:zoom="11.313709"
inkscape:cx="43.884815"
inkscape:cy="27.665553"
inkscape:window-width="1920"
inkscape:window-height="975"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="svg2869" />
<defs
id="defs2871">
<linearGradient
id="linearGradient5">
<stop
style="stop-color:#ef2929;stop-opacity:1;"
offset="0"
id="stop19" />
<stop
style="stop-color:#ef2929;stop-opacity:0;"
offset="1"
id="stop20" />
</linearGradient>
<linearGradient
id="swatch18">
<stop
style="stop-color:#ef2929;stop-opacity:1;"
offset="0"
id="stop18" />
</linearGradient>
<linearGradient
id="swatch15">
<stop
style="stop-color:#3d0000;stop-opacity:1;"
offset="0"
id="stop15" />
</linearGradient>
<linearGradient
id="linearGradient5-1">
<stop
style="stop-color:#ef2929;stop-opacity:1;"
offset="0"
id="stop5" />
<stop
style="stop-color:#ef2929;stop-opacity:0;"
offset="1"
id="stop6" />
</linearGradient>
<linearGradient
id="linearGradient3836-9">
<stop
style="stop-color:#a40000;stop-opacity:1"
offset="0"
id="stop3838-8" />
<stop
style="stop-color:#ef2929;stop-opacity:1"
offset="1"
id="stop3840-1" />
</linearGradient>
<linearGradient
id="linearGradient3836-9-3">
<stop
style="stop-color:#a40000;stop-opacity:1"
offset="0"
id="stop3838-8-5" />
<stop
style="stop-color:#ef2929;stop-opacity:1"
offset="1"
id="stop3840-1-6" />
</linearGradient>
<linearGradient
id="linearGradient2">
<stop
style="stop-color:#73d216;stop-opacity:1;"
offset="0"
id="stop1" />
<stop
style="stop-color:#8ae234;stop-opacity:1;"
offset="1"
id="stop2" />
</linearGradient>
<linearGradient
xlink:href="#linearGradient5-8"
id="linearGradient4"
x1="-0.46279547"
y1="-9.6076241"
x2="0.91240209"
y2="-7.7955847"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(8.4654504,0,0,8.4688188,91.099985,166.20721)" />
<linearGradient
id="linearGradient5-8">
<stop
style="stop-color:#8ae234;stop-opacity:1"
offset="0"
id="stop1-7" />
<stop
style="stop-color:#4e9a06;stop-opacity:1"
offset="1"
id="stop2-6" />
</linearGradient>
<linearGradient
xlink:href="#linearGradient8"
id="linearGradient9-6"
x1="90.842628"
y1="89.354988"
x2="99.443108"
y2="98.956932"
gradientUnits="userSpaceOnUse" />
<linearGradient
id="linearGradient8">
<stop
style="stop-color:#ffffff;stop-opacity:1"
offset="0"
id="stop8" />
<stop
style="stop-color:#babdb6;stop-opacity:1"
offset="1"
id="stop9" />
</linearGradient>
</defs>
<metadata
id="metadata2874">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:relation>https://www.freecad.org/wiki/index.php?title=Artwork</dc:relation>
<dc:publisher>
<cc:Agent>
<dc:title>FreeCAD</dc:title>
</cc:Agent>
</dc:publisher>
<dc:rights>
<cc:Agent>
<dc:title>FreeCAD LGPL2+</dc:title>
</cc:Agent>
</dc:rights>
<dc:creator>
<cc:Agent>
<dc:title>Gabriel A. Zorril</dc:title>
</cc:Agent>
</dc:creator>
<dc:date>September 26, 2024</dc:date>
<dc:title>Safe Mode Icon</dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
id="layer3"
style="display:inline">
<g
id="g11"
transform="matrix(3.7795276,0,0,3.7795276,-327.50758,-326.66261)">
<path
d="m 101.42832,96.903456 c 0,2.151154 -2.102879,4.033414 -6.308611,5.646794 -4.205733,-1.61338 -6.308603,-3.49564 -6.308603,-5.646794 0,-2.151154 0,-4.840101 0,-8.066841 2.826362,-1.075568 4.929223,-1.613361 6.308603,-1.613361 1.37937,0 3.48225,0.537793 6.308611,1.613361 0,3.22674 0,5.915687 0,8.066841 z"
id="path1-4"
style="display:inline;fill:#8ae234;fill-opacity:1;stroke:#17230b;stroke-width:0.529166;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1" />
<path
d="m 95.119141,87.751953 c 1.255627,0 3.149703,0.472658 5.779299,1.453125 v 7.699219 c 0,1.788278 -1.781815,3.510143 -5.779299,5.076173 -3.997486,-1.56603 -5.779297,-3.287895 -5.779297,-5.076173 v -7.699219 c 2.629594,-0.980467 4.523659,-1.453125 5.779297,-1.453125 z"
id="path2"
style="display:inline;fill:url(#linearGradient4);fill-opacity:1;stroke:#8ae234;stroke-width:0.529166;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1" />
<path
id="path1880"
style="color:#000000;display:block;overflow:visible;visibility:visible;fill:url(#linearGradient9-6);fill-opacity:1;fill-rule:nonzero;stroke:#17230b;stroke-width:0.529166;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;marker:none"
d="m 94.257694,89.521531 c -2.15801,0.002 -3.708676,1.519832 -3.170866,4.048848 -0.04051,-2.920011 3.296068,-3.066458 5.901448,-1.107943 l -1.138948,1.26969 3.422013,0.0036 0.0217,-3.886585 -1.091923,1.189075 C 96.86977,89.996462 95.468284,89.520421 94.257694,89.521531 Z m -3.289722,5.048787 -0.0217,3.886585 1.091923,-1.189075 c 3.704631,2.898842 7.953803,1.415588 7.114294,-2.532145 0.04051,2.920012 -3.296068,3.066974 -5.901448,1.108459 l 1.138948,-1.270206 z" />
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

@@ -70,6 +70,7 @@
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <App/DocumentObjectGroup.h>
#include <App/SafeMode.h>
#include <Base/ConsoleObserver.h>
#include <Base/Parameter.h>
#include <Base/Exception.h>
@@ -1654,6 +1655,22 @@ void MainWindow::delayedStartup()
if (hGrp->GetBool("RecoveryEnabled", true)) {
Application::Instance->checkForPreviousCrashes();
}
if (SafeMode::SafeModeEnabled()) {
auto safeModePopup = QMessageBox(
QMessageBox::Information,
tr("Safe mode enabled"),
tr("FreeCAD is now running in safe mode."),
QMessageBox::Ok
);
safeModePopup.setInformativeText(
tr(
"Safe mode temporarily disables your configurations and addons."
" Restart the application to exit safe mode."
)
);
safeModePopup.exec();
}
}
void MainWindow::appendRecentFile(const QString& filename)
@@ -2610,6 +2627,10 @@ void MainWindow::setWindowTitle(const QString& string)
title = appname;
}
if (SafeMode::SafeModeEnabled()) {
title = QString::fromUtf8("%1 (%2)").arg(title, tr("Safe Mode"));
}
if (!string.isEmpty()) {
title = QString::fromUtf8("[*] %1 - %2").arg(string, title);
}

View File

@@ -761,7 +761,8 @@ MenuItem* StdWorkbench::setupMenuBar() const
*help << "Std_OnlineHelp" << "Std_FreeCADWebsite" << "Std_FreeCADDonation"
<< "Std_FreeCADUserHub" << "Std_FreeCADPowerUserHub"
<< "Std_PythonHelp" << "Std_FreeCADForum" << "Std_FreeCADFAQ"
<< "Std_ReportBug" << "Std_About" << "Std_WhatsThis";
<< "Std_ReportBug" << "Std_About" << "Std_WhatsThis"
<< "Std_RestartInSafeMode";
return menuBar;
}