/src/libreoffice/sfx2/source/appl/flatpak.cxx
Line | Count | Source |
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. |
4 | | * |
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 | | */ |
9 | | |
10 | | #include <sal/config.h> |
11 | | |
12 | | #include <cassert> |
13 | | #include <cstdlib> |
14 | | #include <cstring> |
15 | | |
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> |
26 | | |
27 | 0 | bool flatpak::isFlatpak() { |
28 | 0 | static auto const flatpak = [] { return std::getenv("LIBO_FLATPAK") != nullptr; }(); |
29 | 0 | return flatpak; |
30 | 0 | } |
31 | | |
32 | | namespace { |
33 | | |
34 | | // Must only be accessed with SolarMutex locked: |
35 | | struct { |
36 | | bool created = false; |
37 | | OUString url; |
38 | | } temporaryHtmlDirectoryStatus; |
39 | | |
40 | | } |
41 | | |
42 | 0 | bool flatpak::createTemporaryHtmlDirectory(OUString ** url) { |
43 | 0 | assert(url != nullptr); |
44 | 0 | DBG_TESTSOLARMUTEX(); |
45 | 0 | if (!temporaryHtmlDirectoryStatus.created) { |
46 | | // coverity[tainted_return_value] - we trust the contents of this variable |
47 | 0 | auto const env = std::getenv("XDG_CACHE_HOME"); |
48 | 0 | if (env == nullptr) { |
49 | 0 | SAL_WARN("sfx.appl", "LIBO_FLATPAK mode but unset XDG_CACHE_HOME"); |
50 | 0 | return false; |
51 | 0 | } |
52 | 0 | OUString path; |
53 | 0 | if (!rtl_convertStringToUString( |
54 | 0 | &path.pData, env, std::strlen(env), osl_getThreadTextEncoding(), |
55 | 0 | (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
56 | 0 | | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))) |
57 | 0 | { |
58 | 0 | SAL_WARN( |
59 | 0 | "sfx.appl", |
60 | 0 | "LIBO_FLATPAK mode failure converting XDG_CACHE_HOME \"" << env << "\" encoding"); |
61 | 0 | return false; |
62 | 0 | } |
63 | 0 | OUString parent; |
64 | 0 | auto const err = osl::FileBase::getFileURLFromSystemPath(path, parent); |
65 | 0 | if (err != osl::FileBase::E_None) { |
66 | 0 | SAL_WARN( |
67 | 0 | "sfx.appl", |
68 | 0 | "LIBO_FLATPAK mode failure converting XDG_CACHE_HOME \"" << path << "\" to URL: " |
69 | 0 | << err); |
70 | 0 | return false; |
71 | 0 | } |
72 | 0 | if (!parent.endsWith("/")) { |
73 | 0 | parent += "/"; |
74 | 0 | } |
75 | 0 | temporaryHtmlDirectoryStatus.url = utl::CreateTempURL(&parent, true); |
76 | 0 | if (temporaryHtmlDirectoryStatus.url.isEmpty()) { |
77 | 0 | SAL_WARN( |
78 | 0 | "sfx.appl", "LIBO_FLATPAK mode failure creating temp dir at <" << parent << ">"); |
79 | 0 | return false; |
80 | 0 | } |
81 | 0 | temporaryHtmlDirectoryStatus.created = true; |
82 | 0 | } |
83 | 0 | *url = &temporaryHtmlDirectoryStatus.url; |
84 | 0 | return true; |
85 | 0 | } |
86 | | |
87 | 0 | void flatpak::removeTemporaryHtmlDirectory() { |
88 | 0 | DBG_TESTSOLARMUTEX(); |
89 | 0 | if (temporaryHtmlDirectoryStatus.created) { |
90 | 0 | if (!utl::UCBContentHelper::Kill(temporaryHtmlDirectoryStatus.url)) { |
91 | 0 | SAL_INFO( |
92 | 0 | "sfx.appl", |
93 | 0 | "LIBO_FLATPAK mode failure removing directory <" |
94 | 0 | << temporaryHtmlDirectoryStatus.url << ">"); |
95 | 0 | } |
96 | 0 | } |
97 | 0 | } |
98 | | |
99 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |