LibreOffice Module comphelper (master) 1
lok.cxx
Go to the documentation of this file.
1/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <comphelper/lok.hxx>
11#include <osl/process.h>
13#include <sal/log.hxx>
14
15#include <iostream>
16
18{
19
20static bool g_bActive(false);
21
22static bool g_bPartInInvalidation(false);
23
24static bool g_bTiledPainting(false);
25
26static bool g_bDialogPainting(false);
27
28static bool g_bTiledAnnotations(true);
29
30static bool g_bRangeHeaders(false);
31
33
34static bool g_bLocalRendering(false);
35
37
38namespace
39{
40
41class LanguageAndLocale
42{
43private:
46
47public:
48
49 LanguageAndLocale()
52 {}
53
54 const LanguageTag& getLanguage() const
55 {
56 return maLanguageTag;
57 }
58
59 void setLanguage(const LanguageTag& rLanguageTag)
60 {
61 if (maLanguageTag != rLanguageTag)
62 {
63 SAL_INFO("comphelper.lok", "Setting language from " << maLanguageTag.getBcp47() << " to " << rLanguageTag.getBcp47());
64 maLanguageTag = rLanguageTag;
65 }
66 }
67
68 const LanguageTag& getLocale() const
69 {
71 }
72
73 void setLocale(const LanguageTag& rLocaleLanguageTag)
74 {
75 if (maLocaleLanguageTag != rLocaleLanguageTag)
76 {
77 SAL_INFO("comphelper.lok", "Setting locale from " << maLocaleLanguageTag.getBcp47() << " to " << rLocaleLanguageTag.getBcp47());
78 maLocaleLanguageTag = rLocaleLanguageTag;
79 }
80 }
81
82};
83
84}
85
86static LanguageAndLocale g_aLanguageAndLocale;
87
89static double g_fDPIScale(1.0);
90
91void setActive(bool bActive)
92{
93 g_bActive = bActive;
94}
95
97{
98 return g_bActive;
99}
100
101void setPartInInvalidation(bool bPartInInvalidation)
102{
103 g_bPartInInvalidation = bPartInInvalidation;
104}
105
107{
109}
110
111void setTiledPainting(bool bTiledPainting)
112{
113 g_bTiledPainting = bTiledPainting;
114}
115
117{
118 return g_bTiledPainting;
119}
120
121void setDialogPainting(bool bDialogPainting)
122{
123 g_bDialogPainting = bDialogPainting;
124}
125
127{
128 return g_bDialogPainting;
129}
130
131void setDPIScale(double fDPIScale)
132{
133 g_fDPIScale = fDPIScale;
134}
135
137{
138 return g_fDPIScale;
139}
140
141void setTiledAnnotations(bool bTiledAnnotations)
142{
143 g_bTiledAnnotations = bTiledAnnotations;
144}
145
147{
148 return g_bTiledAnnotations;
149}
150
151void setRangeHeaders(bool bRangeHeaders)
152{
153 g_bRangeHeaders = bRangeHeaders;
154}
155
156void setViewIdForVisCursorInvalidation(bool bViewIdForVisCursorInvalidation)
157{
158 g_bViewIdForVisCursorInvalidation = bViewIdForVisCursorInvalidation;
159}
160
162{
164}
165
167{
168 return g_bRangeHeaders;
169}
170
171void setLocalRendering(bool bLocalRendering)
172{
173 g_bLocalRendering = bLocalRendering;
174}
175
177{
178 return g_bLocalRendering;
179}
180
181void setCompatFlag(Compat flag) { g_eCompatFlags = static_cast<Compat>(g_eCompatFlags | flag); }
182
183bool isCompatFlagSet(Compat flag) { return (g_eCompatFlags & flag) == flag; }
184
186
187void setLocale(const LanguageTag& rLanguageTag)
188{
189 g_aLanguageAndLocale.setLocale(rLanguageTag);
190}
191
193{
194 const LanguageTag& rLocale = g_aLanguageAndLocale.getLocale();
195 SAL_INFO_IF(rLocale.getLanguageType() == LANGUAGE_NONE, "comphelper.lok", "Locale not set");
196 return rLocale;
197}
198
199void setLanguageTag(const LanguageTag& rLanguageTag)
200{
201 g_aLanguageAndLocale.setLanguage(rLanguageTag);
202}
203
205{
206 const LanguageTag& rLanguage = g_aLanguageAndLocale.getLanguage();
207 SAL_INFO_IF(rLanguage.getLanguageType() == LANGUAGE_NONE, "comphelper.lok", "Language not set");
208 return rLanguage;
209}
210
211bool isAllowlistedLanguage(const OUString& lang)
212{
213 if (!isActive())
214 return true;
215
216#if defined ANDROID || defined IOS
217 (void) lang;
218 return true;
219#else
220 static const std::vector<OUString> aAllowlist = [] {
221 std::vector<OUString> aList;
222 // coverity[tainted_data] - we trust the contents of this variable
223 const char* pAllowlist = getenv("LOK_ALLOWLIST_LANGUAGES");
224 if (pAllowlist)
225 {
226 std::stringstream stream(pAllowlist);
227 std::string s;
228
229 std::cerr << "Allowlisted languages: ";
230 while (getline(stream, s, ' ')) {
231 if (s.length() == 0)
232 continue;
233
234 std::cerr << s << " ";
235 aList.emplace_back(OStringToOUString(s, RTL_TEXTENCODING_UTF8));
236 }
237 std::cerr << std::endl;
238 }
239
240 if (aList.empty())
241 std::cerr << "No language allowlisted, turning off the language support." << std::endl;
242
243 return aList;
244 }();
245
246 if (aAllowlist.empty())
247 return false;
248
249 for (const auto& entry : aAllowlist)
250 {
251 if (lang.startsWith(entry))
252 return true;
253 if (lang.startsWith(entry.replace('_', '-')))
254 return true;
255 }
256
257 return false;
258#endif
259}
260
261void setTimezone(bool isSet, const OUString& rTimezone)
262{
263 if (isSet)
264 {
265 // Set the given timezone, even if empty.
266 osl_setEnvironment(OUString("TZ").pData, rTimezone.pData);
267 }
268 else
269 {
270 // Unset and empty aren't the same.
271 // When unset, it means default to the system configured timezone.
272 osl_clearEnvironment(OUString("TZ").pData);
273 }
274
275 // Update the timezone data.
276 ::tzset();
277}
278
279static void (*pStatusIndicatorCallback)(void *data, statusIndicatorCallbackType type, int percent, const char* pText)(nullptr);
280static void *pStatusIndicatorCallbackData(nullptr);
281
282void setStatusIndicatorCallback(void (*callback)(void *data, statusIndicatorCallbackType type, int percent, const char* pText), void *data)
283{
284 pStatusIndicatorCallback = callback;
286}
287
288void statusIndicatorStart(const OUString& sText)
289{
292}
293
294void statusIndicatorSetValue(int percent)
295{
298}
299
301{
304}
305
306} // namespace
307
308/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
LanguageType getLanguageType(bool bResolveSystem=true) const
const OUString & getBcp47(bool bResolveSystem=true) const
Reference< XOutputStream > stream
#define LANGUAGE_NONE
#define SAL_INFO_IF(condition, area, stream)
#define SAL_INFO(area, stream)
LanguageTag maLocaleLanguageTag
Definition: lok.cxx:45
LanguageTag maLanguageTag
Definition: lok.cxx:44
std::unique_ptr< sal_Int32[]> pData
void statusIndicatorStart(const OUString &sText)
Definition: lok.cxx:288
void statusIndicatorFinish()
Definition: lok.cxx:300
void setTiledPainting(bool bTiledPainting)
Set if we are doing tiled painting.
Definition: lok.cxx:111
static bool g_bLocalRendering(false)
void setCompatFlag(Compat flag)
Set compatibility flags.
Definition: lok.cxx:181
double getDPIScale()
Get the DPI scale for rendering for HiDPI displays.
Definition: lok.cxx:136
static Compat g_eCompatFlags(Compat::none)
void setDialogPainting(bool bDialogPainting)
Set if we are painting the dialog.
Definition: lok.cxx:121
static bool g_bPartInInvalidation(false)
bool isDialogPainting()
Check if we are painting the dialog.
Definition: lok.cxx:126
bool isTiledPainting()
Check if we are doing tiled painting.
Definition: lok.cxx:116
void setTimezone(bool isSet, const OUString &rTimezone)
Update the current LOK's timezone.
Definition: lok.cxx:261
static bool g_bTiledPainting(false)
bool isTiledAnnotations()
Check if annotations rendering is turned off.
Definition: lok.cxx:146
void statusIndicatorSetValue(int percent)
Definition: lok.cxx:294
static bool g_bViewIdForVisCursorInvalidation(false)
static void * pStatusIndicatorCallbackData(nullptr)
static bool g_bTiledAnnotations(true)
bool isAllowlistedLanguage(const OUString &lang)
If the language name should be used for this LOK instance.
Definition: lok.cxx:211
bool isCompatFlagSet(Compat flag)
Get compatibility flags.
Definition: lok.cxx:183
bool isPartInInvalidation()
Check whether clients want a part number in an invalidation payload.
Definition: lok.cxx:106
const LanguageTag & getLocale()
Get the current LOK's locale.
Definition: lok.cxx:192
bool isRangeHeaders()
Check if range based header data is enabled.
Definition: lok.cxx:166
static bool g_bRangeHeaders(false)
void setRangeHeaders(bool bRangeHeaders)
Set if we want range based header data.
Definition: lok.cxx:151
static double g_fDPIScale(1.0)
Scaling of the cairo canvas painting for hi-dpi.
const LanguageTag & getLanguageTag()
Get the current LOK's language.
Definition: lok.cxx:204
void setPartInInvalidation(bool bPartInInvalidation)
Set whether clients want a part number in an invalidation payload.
Definition: lok.cxx:101
static LanguageAndLocale g_aLanguageAndLocale
Definition: lok.cxx:86
void resetCompatFlag()
Reset compatibility flags.
Definition: lok.cxx:185
static bool g_bDialogPainting(false)
bool isViewIdForVisCursorInvalidation()
Check whether clients want viewId in visible cursor invalidation payload.
Definition: lok.cxx:161
void setLocale(const LanguageTag &rLanguageTag)
Update the current LOK's locale.
Definition: lok.cxx:187
void setTiledAnnotations(bool bTiledAnnotations)
Set if we want no annotations rendering.
Definition: lok.cxx:141
void setLanguageTag(const LanguageTag &rLanguageTag)
Update the current LOK's language.
Definition: lok.cxx:199
void setStatusIndicatorCallback(void(*callback)(void *data, statusIndicatorCallbackType type, int percent, const char *pText), void *data)
Definition: lok.cxx:282
void setLocalRendering(bool bLocalRendering)
Shift the coordinates before rendering each bitmap.
Definition: lok.cxx:171
void setActive(bool bActive)
Definition: lok.cxx:91
void setViewIdForVisCursorInvalidation(bool bViewIdForVisCursorInvalidation)
Set whether clients want viewId in visible cursor invalidation payload.
Definition: lok.cxx:156
void setDPIScale(double fDPIScale)
Set the DPI scale for rendering for HiDPI displays.
Definition: lok.cxx:131
static bool g_bActive(false)
static void(* pStatusIndicatorCallback)(void *data, statusIndicatorCallbackType type, int percent, const char *pText)(nullptr)
Definition: lok.cxx:279
ResultType type