SafeMode: Restart goes into safe mode again

Looks like the XRestartManager keeps all command line arguments when
restarting, so it also keeps --safe-mode.

Solution is to add a flag file when restarting from safe mode,
to prevent going into safe mode again.

Change-Id: I9820d3ccbddf98b0bf6132f254c989f52ea5e808
Reviewed-on: https://gerrit.libreoffice.org/31913
Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
Tested-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
index eba8bf6..437ff51 100644
--- a/desktop/source/app/app.cxx
+++ b/desktop/source/app/app.cxx
@@ -560,7 +560,10 @@ void Desktop::Init()

    // Check whether safe mode is enabled
    CommandLineArgs& rCmdLine = GetCommandLineArgs();
    if (rCmdLine.IsSafeMode() || sfx2::SafeMode::hasFlag())
    // Check if we are restarting from safe mode - in that case we don't want to enter it again
    if (sfx2::SafeMode::hasRestartFlag())
        sfx2::SafeMode::removeRestartFlag();
    else if (rCmdLine.IsSafeMode() || sfx2::SafeMode::hasFlag())
        Application::EnableSafeMode();

    // When we are in SafeMode we need to do changes before the configuration
diff --git a/include/sfx2/safemode.hxx b/include/sfx2/safemode.hxx
index 03f8590..47034f6 100644
--- a/include/sfx2/safemode.hxx
+++ b/include/sfx2/safemode.hxx
@@ -40,9 +40,31 @@ public:
     */
    static bool removeFlag();

    /**
     * Write a flag to the user profile indicating that we are currently restarting from safe mode -
     * that means we don't want to enter safe mode again.
     *
     * @return Whether the file could be written successfully
     */
    static bool putRestartFlag();

    /**
     * Check the existence of the restart flag file.
     *
     * @return Whether the restart flag file exists
     */
    static bool hasRestartFlag();

    /**
     * Remove the restart flag file.
     *
     * @return Whether the flag file could be removed successfully
     */
    static bool removeRestartFlag();

private:
    /** Returns the path of the safe mode flag file.*/
    static OUString getFileName();
    static OUString getFilePath(const OUString& sFilename);
};

}
diff --git a/sfx2/source/safemode/safemode.cxx b/sfx2/source/safemode/safemode.cxx
index 3e84f7ed0..6d8dcf9 100644
--- a/sfx2/source/safemode/safemode.cxx
+++ b/sfx2/source/safemode/safemode.cxx
@@ -20,7 +20,7 @@ namespace sfx2 {

bool SafeMode::putFlag()
{
    File safeModeFile(getFileName());
    File safeModeFile(getFilePath("safemode"));
    if (safeModeFile.open(osl_File_OpenFlag_Create) == FileBase::E_None)
    {
        safeModeFile.close();
@@ -30,7 +30,7 @@ bool SafeMode::putFlag()
}
bool SafeMode::hasFlag()
{
    File safeModeFile(getFileName());
    File safeModeFile(getFilePath("safemode"));
    if (safeModeFile.open(osl_File_OpenFlag_Read) == FileBase::E_None)
    {
        safeModeFile.close();
@@ -40,17 +40,42 @@ bool SafeMode::hasFlag()
}
bool SafeMode::removeFlag()
{
    return File::remove(getFileName()) == FileBase::E_None;
    return File::remove(getFilePath("safemode")) == FileBase::E_None;
}

OUString SafeMode::getFileName()
bool SafeMode::putRestartFlag()
{
    File restartFile(getFilePath("safemode_restart"));
    if (restartFile.open(osl_File_OpenFlag_Create) == FileBase::E_None)
    {
        restartFile.close();
        return true;
    }
    return false;
}
bool SafeMode::hasRestartFlag()
{
    File restartFile(getFilePath("safemode_restart"));
    if (restartFile.open(osl_File_OpenFlag_Read) == FileBase::E_None)
    {
        restartFile.close();
        return true;
    }
    return false;
}
bool SafeMode::removeRestartFlag()
{
    return File::remove(getFilePath("safemode_restart")) == FileBase::E_None;
}

OUString SafeMode::getFilePath(const OUString& sFilename)
{
    OUString url("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/");
    rtl::Bootstrap::expandMacros(url);

    OUString aProfilePath;
    FileBase::getSystemPathFromFileURL(url, aProfilePath);
    FileBase::getAbsoluteFileURL(url, "safemode", aProfilePath);
    FileBase::getAbsoluteFileURL(url, sFilename, aProfilePath);
    return aProfilePath;
}

diff --git a/svx/source/dialog/SafeModeDialog.cxx b/svx/source/dialog/SafeModeDialog.cxx
index ac9c494..b77b83f 100644
--- a/svx/source/dialog/SafeModeDialog.cxx
+++ b/svx/source/dialog/SafeModeDialog.cxx
@@ -326,6 +326,7 @@ IMPL_LINK(SafeModeDialog, DialogBtnHdl, Button*, pBtn, void)
    }
    else if (pBtn == mpBtnRestart.get())
    {
        sfx2::SafeMode::putRestartFlag();
        Close();
        applyChanges();
    }