Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / appl / flatpak.cxx
blob14411dafc5d0c058f7ef2c7b3586d0fb617136b5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <cassert>
13 #include <cstdlib>
14 #include <cstring>
16 #include <osl/file.hxx>
17 #include <osl/thread.h>
18 #include <rtl/textcvt.h>
19 #include <rtl/ustring.h>
20 #include <rtl/ustring.hxx>
21 #include <sal/log.hxx>
22 #include <sfx2/flatpak.hxx>
23 #include <tools/debug.hxx>
24 #include <unotools/tempfile.hxx>
25 #include <unotools/ucbhelper.hxx>
27 bool flatpak::isFlatpak() {
28 static auto const flatpak = [] { return std::getenv("LIBO_FLATPAK") != nullptr; }();
29 return flatpak;
32 namespace {
34 // Must only be accessed with SolarMutex locked:
35 struct {
36 bool created = false;
37 OUString url;
38 } temporaryHtmlDirectoryStatus;
42 bool flatpak::createTemporaryHtmlDirectory(OUString ** url) {
43 assert(url != nullptr);
44 DBG_TESTSOLARMUTEX();
45 if (!temporaryHtmlDirectoryStatus.created) {
46 auto const env = std::getenv("XDG_CACHE_HOME");
47 if (env == nullptr) {
48 SAL_WARN("sfx.appl", "LIBO_FLATPAK mode but unset XDG_CACHE_HOME");
49 return false;
51 OUString path;
52 if (!rtl_convertStringToUString(
53 &path.pData, env, std::strlen(env), osl_getThreadTextEncoding(),
54 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
55 | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
57 SAL_WARN(
58 "sfx.appl",
59 "LIBO_FLATPAK mode failure converting XDG_CACHE_HOME \"" << env << "\" encoding");
60 return false;
62 OUString parent;
63 auto const err = osl::FileBase::getFileURLFromSystemPath(path, parent);
64 if (err != osl::FileBase::E_None) {
65 SAL_WARN(
66 "sfx.appl",
67 "LIBO_FLATPAK mode failure converting XDG_CACHE_HOME \"" << path << "\" to URL: "
68 << err);
69 return false;
71 if (!parent.endsWith("/")) {
72 parent += "/";
74 auto const tmp = utl::TempFile(&parent, true);
75 if (!tmp.IsValid()) {
76 SAL_WARN(
77 "sfx.appl", "LIBO_FLATPAK mode failure creating temp dir at <" << parent << ">");
78 return false;
80 temporaryHtmlDirectoryStatus.url = tmp.GetURL();
81 temporaryHtmlDirectoryStatus.created = true;
83 *url = &temporaryHtmlDirectoryStatus.url;
84 return true;
87 void flatpak::removeTemporaryHtmlDirectory() {
88 DBG_TESTSOLARMUTEX();
89 if (temporaryHtmlDirectoryStatus.created) {
90 if (!utl::UCBContentHelper::Kill(temporaryHtmlDirectoryStatus.url)) {
91 SAL_INFO(
92 "sfx.appl",
93 "LIBO_FLATPAK mode failure removing directory <"
94 << temporaryHtmlDirectoryStatus.url << ">");
99 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */