move using the vcl file dialog in headless mode to the point of creation

instead of setting and unsetting the config option at start and exit
because a document that crashes in headless mode leaves my config
in an unwanted built-in file dialog state.

Change-Id: Ib5fcc5994a08c78bffdf57cb5b252dc469167ba2
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150126
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
index 7267449..169330d 100644
--- a/desktop/source/app/app.cxx
+++ b/desktop/source/app/app.cxx
@@ -1227,14 +1227,12 @@ struct ExecuteGlobals
{
    Reference < css::document::XDocumentEventListener > xGlobalBroadcaster;
    bool bRestartRequested;
    bool bUseSystemFileDialog;
    std::unique_ptr<SvtCTLOptions> pCTLLanguageOptions;
    std::unique_ptr<SvtPathOptions> pPathOptions;
    rtl::Reference< JVMloadThread > xJVMloadThread;

    ExecuteGlobals()
    : bRestartRequested( false )
    , bUseSystemFileDialog( true )
    {}
};

@@ -1511,18 +1509,6 @@ int Desktop::Main()
        }
    }

    if ( rCmdLineArgs.IsHeadless() )
    {
        // Ensure that we use not the system file dialogs as
        // headless mode relies on Application::EnableHeadlessMode()
        // which does only work for VCL dialogs!!
        pExecGlobals->bUseSystemFileDialog = officecfg::Office::Common::Misc::UseSystemFileDialog::get();
        std::shared_ptr< comphelper::ConfigurationChanges > xChanges(
                comphelper::ConfigurationChanges::create());
        officecfg::Office::Common::Misc::UseSystemFileDialog::set( false, xChanges );
        xChanges->commit();
    }

    pExecGlobals->bRestartRequested = xRestartManager->isRestartRequested(true);
    if ( !pExecGlobals->bRestartRequested )
    {
@@ -1635,16 +1621,7 @@ int Desktop::doShutdown()
    if ( pExecGlobals->bRestartRequested )
        SetRestartState();

    // Restore old value
    const CommandLineArgs& rCmdLineArgs = GetCommandLineArgs();
    if ( rCmdLineArgs.IsHeadless() )
    {
        std::shared_ptr< comphelper::ConfigurationChanges > xChanges(
                comphelper::ConfigurationChanges::create());
        officecfg::Office::Common::Misc::UseSystemFileDialog::set( pExecGlobals->bUseSystemFileDialog, xChanges );
        xChanges->commit();
    }

    OUString pidfileName = rCmdLineArgs.GetPidfileName();
    if ( !pidfileName.isEmpty() )
    {
diff --git a/postprocess/qa/services.cxx b/postprocess/qa/services.cxx
index 94bd760..c332402 100644
--- a/postprocess/qa/services.cxx
+++ b/postprocess/qa/services.cxx
@@ -365,14 +365,10 @@ void Test::createInstance(
#endif
    } else if (name == "com.sun.star.ui.dialogs.FolderPicker") {
        // FolderPicker is a wrapper returning either a platform-specific or the
        // generic OfficeFolderPicker:
#if defined(_WIN32)
        expImpl = "com.sun.star.ui.dialogs.Win32FolderPicker";
        expServs = {"com.sun.star.ui.dialogs.SystemFolderPicker"};
#else
        // generic OfficeFolderPicker. In headless mode it is always the
        // generic one.
        expImpl = "com.sun.star.svtools.OfficeFolderPicker";
        expServs = {"com.sun.star.ui.dialogs.OfficeFolderPicker"};
#endif
    } else if (expImpl == "com.sun.star.comp.Calc.SpreadsheetDocument") {
        expImpl = "ScModelObj";
    } else if (expImpl == "com.sun.star.comp.Draw.DrawingDocument"
diff --git a/sfx2/source/appl/shutdownicon.cxx b/sfx2/source/appl/shutdownicon.cxx
index ae21484..fca7e56 100644
--- a/sfx2/source/appl/shutdownicon.cxx
+++ b/sfx2/source/appl/shutdownicon.cxx
@@ -138,11 +138,15 @@ void ShutdownIcon::deInitSystray()
    m_bInitialized = false;
}

static bool UseSystemFileDialog()
{
    return !Application::IsHeadlessModeEnabled() && officecfg::Office::Common::Misc::UseSystemFileDialog::get();
}

ShutdownIcon::ShutdownIcon( css::uno::Reference< XComponentContext > xContext ) :
    m_bVeto ( false ),
    m_bListenForTermination ( false ),
    m_bSystemDialogs(officecfg::Office::Common::Misc::UseSystemFileDialog::get()),
    m_bSystemDialogs(UseSystemFileDialog()),
    m_xContext(std::move( xContext )),
    m_bInitialized( false )
{
@@ -241,7 +245,7 @@ void ShutdownIcon::StartFileDialog()
{
    ::SolarMutexGuard aGuard;

    bool bDirty = ( m_bSystemDialogs != officecfg::Office::Common::Misc::UseSystemFileDialog::get() );
    bool bDirty = m_bSystemDialogs != UseSystemFileDialog();

    if ( m_pFileDlg && bDirty )
    {
@@ -382,7 +386,7 @@ IMPL_LINK( ShutdownIcon, DialogClosedHdl_Impl, FileDialogHelper*, /*unused*/, vo
    // This fix is dependent on the dialog settings. Destroying the dialog here will
    // crash the non-native dialog implementation! Therefore make this dependent on
    // the settings.
    if ( officecfg::Office::Common::Misc::UseSystemFileDialog::get() )
    if (UseSystemFileDialog())
    {
        m_pFileDlg.reset();
    }
diff --git a/svtools/source/uno/fpicker.cxx b/svtools/source/uno/fpicker.cxx
index dbb69d60..e02d8ab 100644
--- a/svtools/source/uno/fpicker.cxx
+++ b/svtools/source/uno/fpicker.cxx
@@ -48,6 +48,13 @@ static OUString FilePicker_getSystemPickerServiceName()
#endif
}

// Ensure that we use not the system file dialogs as headless mode relies on
// Application::EnableHeadlessMode() which only works for VCL dialogs
static bool UseSystemFileDialog()
{
    return !Application::IsHeadlessModeEnabled() && officecfg::Office::Common::Misc::UseSystemFileDialog::get();
}

Reference< css::uno::XInterface > FilePicker_CreateInstance (
    Reference< css::uno::XComponentContext > const & context)
{
@@ -57,7 +64,7 @@ Reference< css::uno::XInterface > FilePicker_CreateInstance (
        return xResult;

    Reference< css::lang::XMultiComponentFactory > xFactory (context->getServiceManager());
    if (xFactory.is() && officecfg::Office::Common::Misc::UseSystemFileDialog::get())
    if (xFactory.is() && UseSystemFileDialog())
    {
        xResult.set( Application::createFilePicker( context ) );

@@ -125,7 +132,7 @@ Reference< css::uno::XInterface > FolderPicker_CreateInstance (
        return xResult;

    Reference< css::lang::XMultiComponentFactory > xFactory (context->getServiceManager());
    if (xFactory.is() && officecfg::Office::Common::Misc::UseSystemFileDialog::get())
    if (xFactory.is() && UseSystemFileDialog())
    {
        xResult.set( Application::createFolderPicker( context ) );
        if (!xResult.is())