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