diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2019-04-30 10:38:38 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2019-05-02 08:40:10 +0200 |
commit | c61e7b9940cb30800d6f1000727f9cfd5de9fa5e (patch) | |
tree | b27094b0ae4bf5d9ab306d92d8478c69a739b4e4 /sfx2 | |
parent | 19976f079800ec4c1d0d5d7e226986cb41f834c2 (diff) |
Support "Preview in Web Browser" in Flatpak mode
...by storing the temporary HTML document in a location that can be accessed by
the browser running outside the Flatpak sandbox. This reuses and extends the
mechanism already in place for the new HTML-based help in Flatpak mode (see
72b936d70b7eaa6d9f5f911b27e3c955382de967 "Enable --help=html for flatpak").
This fixes <https://github.com/flathub/org.libreoffice.LibreOffice/issues/85>
"“Preview in Web Browser” does not work in Flatpak version".
Change-Id: I5f73fd89139ffe6b8ab0dc501154b4f054a0ae5c
Reviewed-on: https://gerrit.libreoffice.org/71570
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'sfx2')
-rw-r--r-- | sfx2/Library_sfx.mk | 1 | ||||
-rw-r--r-- | sfx2/source/appl/flatpak.cxx | 99 | ||||
-rw-r--r-- | sfx2/source/appl/sfxhelp.cxx | 74 | ||||
-rw-r--r-- | sfx2/source/view/viewsh.cxx | 12 |
4 files changed, 113 insertions, 73 deletions
diff --git a/sfx2/Library_sfx.mk b/sfx2/Library_sfx.mk index 6b10dfa021ee..d4fb4cf6b972 100644 --- a/sfx2/Library_sfx.mk +++ b/sfx2/Library_sfx.mk @@ -94,6 +94,7 @@ $(eval $(call gb_Library_add_exception_objects,sfx,\ sfx2/source/appl/childwin \ sfx2/source/appl/childwinimpl \ sfx2/source/appl/fileobj \ + sfx2/source/appl/flatpak \ sfx2/source/appl/fwkhelper \ sfx2/source/appl/helpdispatch \ sfx2/source/appl/helpinterceptor \ diff --git a/sfx2/source/appl/flatpak.cxx b/sfx2/source/appl/flatpak.cxx new file mode 100644 index 000000000000..14411dafc5d0 --- /dev/null +++ b/sfx2/source/appl/flatpak.cxx @@ -0,0 +1,99 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> + +#include <cassert> +#include <cstdlib> +#include <cstring> + +#include <osl/file.hxx> +#include <osl/thread.h> +#include <rtl/textcvt.h> +#include <rtl/ustring.h> +#include <rtl/ustring.hxx> +#include <sal/log.hxx> +#include <sfx2/flatpak.hxx> +#include <tools/debug.hxx> +#include <unotools/tempfile.hxx> +#include <unotools/ucbhelper.hxx> + +bool flatpak::isFlatpak() { + static auto const flatpak = [] { return std::getenv("LIBO_FLATPAK") != nullptr; }(); + return flatpak; +} + +namespace { + +// Must only be accessed with SolarMutex locked: +struct { + bool created = false; + OUString url; +} temporaryHtmlDirectoryStatus; + +} + +bool flatpak::createTemporaryHtmlDirectory(OUString ** url) { + assert(url != nullptr); + DBG_TESTSOLARMUTEX(); + if (!temporaryHtmlDirectoryStatus.created) { + auto const env = std::getenv("XDG_CACHE_HOME"); + if (env == nullptr) { + SAL_WARN("sfx.appl", "LIBO_FLATPAK mode but unset XDG_CACHE_HOME"); + return false; + } + OUString path; + if (!rtl_convertStringToUString( + &path.pData, env, std::strlen(env), osl_getThreadTextEncoding(), + (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR + | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))) + { + SAL_WARN( + "sfx.appl", + "LIBO_FLATPAK mode failure converting XDG_CACHE_HOME \"" << env << "\" encoding"); + return false; + } + OUString parent; + auto const err = osl::FileBase::getFileURLFromSystemPath(path, parent); + if (err != osl::FileBase::E_None) { + SAL_WARN( + "sfx.appl", + "LIBO_FLATPAK mode failure converting XDG_CACHE_HOME \"" << path << "\" to URL: " + << err); + return false; + } + if (!parent.endsWith("/")) { + parent += "/"; + } + auto const tmp = utl::TempFile(&parent, true); + if (!tmp.IsValid()) { + SAL_WARN( + "sfx.appl", "LIBO_FLATPAK mode failure creating temp dir at <" << parent << ">"); + return false; + } + temporaryHtmlDirectoryStatus.url = tmp.GetURL(); + temporaryHtmlDirectoryStatus.created = true; + } + *url = &temporaryHtmlDirectoryStatus.url; + return true; +} + +void flatpak::removeTemporaryHtmlDirectory() { + DBG_TESTSOLARMUTEX(); + if (temporaryHtmlDirectoryStatus.created) { + if (!utl::UCBContentHelper::Kill(temporaryHtmlDirectoryStatus.url)) { + SAL_INFO( + "sfx.appl", + "LIBO_FLATPAK mode failure removing directory <" + << temporaryHtmlDirectoryStatus.url << ">"); + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/sfx2/source/appl/sfxhelp.cxx b/sfx2/source/appl/sfxhelp.cxx index d2593d468129..daeaeddb2b40 100644 --- a/sfx2/source/appl/sfxhelp.cxx +++ b/sfx2/source/appl/sfxhelp.cxx @@ -58,7 +58,6 @@ #include <osl/file.hxx> #include <unotools/bootstrap.hxx> #include <unotools/tempfile.hxx> -#include <unotools/ucbhelper.hxx> #include <rtl/uri.hxx> #include <vcl/commandinfoprovider.hxx> #include <vcl/layout.hxx> @@ -75,6 +74,7 @@ #include "newhelp.hxx" #include <sfx2/objsh.hxx> #include <sfx2/docfac.hxx> +#include <sfx2/flatpak.hxx> #include <sfx2/sfxresid.hxx> #include <helper.hxx> #include <sfx2/strings.hrc> @@ -724,11 +724,6 @@ static bool impl_showOnlineHelp( const OUString& rURL ) namespace { -bool isFlatpak() { - static auto const flatpak = [] { return std::getenv("LIBO_FLATPAK") != nullptr; }(); - return flatpak; -} - bool rewriteFlatpakHelpRootUrl(OUString * helpRootUrl) { assert(helpRootUrl != nullptr); //TODO: this function for now assumes that the passed-in *helpRootUrl references @@ -894,57 +889,6 @@ bool rewriteFlatpakHelpRootUrl(OUString * helpRootUrl) { } } -// Must only be accessed with SolarMutex locked: -static struct { - bool created = false; - OUString url; -} flatpakHelpTemporaryDirectoryStatus; - -bool createFlatpakHelpTemporaryDirectory(OUString ** url) { - assert(url != nullptr); - DBG_TESTSOLARMUTEX(); - if (!flatpakHelpTemporaryDirectoryStatus.created) { - auto const env = std::getenv("XDG_CACHE_HOME"); - if (env == nullptr) { - SAL_WARN("sfx.appl", "LIBO_FLATPAK mode but unset XDG_CACHE_HOME"); - return false; - } - OUString path; - if (!rtl_convertStringToUString( - &path.pData, env, std::strlen(env), osl_getThreadTextEncoding(), - (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR - | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))) - { - SAL_WARN( - "sfx.appl", - "LIBO_FLATPAK mode failure converting XDG_CACHE_HOME \"" << env << "\" encoding"); - return false; - } - OUString parent; - auto const err = osl::FileBase::getFileURLFromSystemPath(path, parent); - if (err != osl::FileBase::E_None) { - SAL_WARN( - "sfx.appl", - "LIBO_FLATPAK mode failure converting XDG_CACHE_HOME \"" << path << "\" to URL: " - << err); - return false; - } - if (!parent.endsWith("/")) { - parent += "/"; - } - auto const tmp = utl::TempFile(&parent, true); - if (!tmp.IsValid()) { - SAL_WARN( - "sfx.appl", "LIBO_FLATPAK mode failure creating temp dir at <" << parent << ">"); - return false; - } - flatpakHelpTemporaryDirectoryStatus.url = tmp.GetURL(); - flatpakHelpTemporaryDirectoryStatus.created = true; - } - *url = &flatpakHelpTemporaryDirectoryStatus.url; - return true; -} - } #define SHTML1 "<!DOCTYPE HTML><html lang=\"en-US\"><head><meta charset=\"UTF-8\">" @@ -957,7 +901,7 @@ static bool impl_showOfflineHelp( const OUString& rURL ) OUString aBaseInstallPath = getHelpRootURL(); // For the flatpak case, find the pathname outside the flatpak sandbox that corresponds to // aBaseInstallPath, because that is what needs to be stored in aTempFile below: - if (isFlatpak() && !rewriteFlatpakHelpRootUrl(&aBaseInstallPath)) { + if (flatpak::isFlatpak() && !rewriteFlatpakHelpRootUrl(&aBaseInstallPath)) { return false; } @@ -970,7 +914,7 @@ static bool impl_showOfflineHelp( const OUString& rURL ) // technical reasons, so that it can be accessed by the browser running outside the sandbox): OUString const aExtension(".html"); OUString * parent = nullptr; - if (isFlatpak() && !createFlatpakHelpTemporaryDirectory(&parent)) { + if (flatpak::isFlatpak() && !flatpak::createTemporaryHtmlDirectory(&parent)) { return false; } ::utl::TempFile aTempFile("NewHelp", true, &aExtension, parent, false ); @@ -1366,16 +1310,4 @@ bool SfxHelp::IsHelpInstalled() return impl_hasHelpInstalled(); } -void SfxHelp::removeFlatpakHelpTemporaryDirectory() { - DBG_TESTSOLARMUTEX(); - if (flatpakHelpTemporaryDirectoryStatus.created) { - if (!utl::UCBContentHelper::Kill(flatpakHelpTemporaryDirectoryStatus.url)) { - SAL_INFO( - "sfx.appl", - "LIBO_FLATPAK mode failure removing directory <" - << flatpakHelpTemporaryDirectoryStatus.url << ">"); - } - } -} - /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/view/viewsh.cxx b/sfx2/source/view/viewsh.cxx index 57677b689784..f2d77e2ed3bb 100644 --- a/sfx2/source/view/viewsh.cxx +++ b/sfx2/source/view/viewsh.cxx @@ -68,6 +68,7 @@ #include <officecfg/Setup.hxx> #include <sfx2/app.hxx> +#include <sfx2/flatpak.hxx> #include <sfx2/viewsh.hxx> #include "viewimp.hxx" #include <sfx2/sfxresid.hxx> @@ -616,8 +617,15 @@ void SfxViewShell::ExecMisc_Impl( SfxRequest &rReq ) OSL_ASSERT( !aFilterName.isEmpty() ); OSL_ASSERT( !aFileName.isEmpty() ); - // Creates a temporary directory to store our predefined file into it. - ::utl::TempFile aTempDir( nullptr, true ); + // Creates a temporary directory to store our predefined file into it (for the + // flatpak case, create it in XDG_CACHE_HOME instead of /tmp for technical reasons, + // so that it can be accessed by the browser running outside the sandbox): + OUString * parent = nullptr; + if (flatpak::isFlatpak() && !flatpak::createTemporaryHtmlDirectory(&parent)) + { + SAL_WARN("sfx.view", "cannot create Flatpak html temp dir"); + } + ::utl::TempFile aTempDir( parent, true ); INetURLObject aFilePathObj( aTempDir.GetURL() ); aFilePathObj.insertName( aFileName ); |