1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <comphelper/lok.hxx>
11 #include <osl/process.h>
12 #include <i18nlangtag/languagetag.hxx>
13 #include <sal/log.hxx>
17 namespace comphelper::LibreOfficeKit
20 static bool g_bActive(false);
22 static bool g_bPartInInvalidation(false);
24 static bool g_bTiledPainting(false);
26 static bool g_bDialogPainting(false);
28 static bool g_bTiledAnnotations(true);
30 static bool g_bRangeHeaders(false);
32 static bool g_bViewIdForVisCursorInvalidation(false);
34 static bool g_bLocalRendering(false);
36 static Compat
g_eCompatFlags(Compat::none
);
41 class LanguageAndLocale
44 LanguageTag maLanguageTag
;
45 LanguageTag maLocaleLanguageTag
;
50 : maLanguageTag(LANGUAGE_NONE
)
51 , maLocaleLanguageTag(LANGUAGE_NONE
)
54 const LanguageTag
& getLanguage() const
59 void setLanguage(const LanguageTag
& rLanguageTag
)
61 if (maLanguageTag
!= rLanguageTag
)
63 SAL_INFO("comphelper.lok", "Setting language from " << maLanguageTag
.getBcp47() << " to " << rLanguageTag
.getBcp47());
64 maLanguageTag
= rLanguageTag
;
68 const LanguageTag
& getLocale() const
70 return maLocaleLanguageTag
;
73 void setLocale(const LanguageTag
& rLocaleLanguageTag
)
75 if (maLocaleLanguageTag
!= rLocaleLanguageTag
)
77 SAL_INFO("comphelper.lok", "Setting locale from " << maLocaleLanguageTag
.getBcp47() << " to " << rLocaleLanguageTag
.getBcp47());
78 maLocaleLanguageTag
= rLocaleLanguageTag
;
86 static LanguageAndLocale g_aLanguageAndLocale
;
88 /// Scaling of the cairo canvas painting for hi-dpi
89 static double g_fDPIScale(1.0);
91 void setActive(bool bActive
)
101 void setPartInInvalidation(bool bPartInInvalidation
)
103 g_bPartInInvalidation
= bPartInInvalidation
;
106 bool isPartInInvalidation()
108 return g_bPartInInvalidation
;
111 void setTiledPainting(bool bTiledPainting
)
113 g_bTiledPainting
= bTiledPainting
;
116 bool isTiledPainting()
118 return g_bTiledPainting
;
121 void setDialogPainting(bool bDialogPainting
)
123 g_bDialogPainting
= bDialogPainting
;
126 bool isDialogPainting()
128 return g_bDialogPainting
;
131 void setDPIScale(double fDPIScale
)
133 g_fDPIScale
= fDPIScale
;
141 void setTiledAnnotations(bool bTiledAnnotations
)
143 g_bTiledAnnotations
= bTiledAnnotations
;
146 bool isTiledAnnotations()
148 return g_bTiledAnnotations
;
151 void setRangeHeaders(bool bRangeHeaders
)
153 g_bRangeHeaders
= bRangeHeaders
;
156 void setViewIdForVisCursorInvalidation(bool bViewIdForVisCursorInvalidation
)
158 g_bViewIdForVisCursorInvalidation
= bViewIdForVisCursorInvalidation
;
161 bool isViewIdForVisCursorInvalidation()
163 return g_bViewIdForVisCursorInvalidation
;
166 bool isRangeHeaders()
168 return g_bRangeHeaders
;
171 void setLocalRendering(bool bLocalRendering
)
173 g_bLocalRendering
= bLocalRendering
;
176 bool isLocalRendering()
178 return g_bLocalRendering
;
181 void setCompatFlag(Compat flag
) { g_eCompatFlags
= static_cast<Compat
>(g_eCompatFlags
| flag
); }
183 bool isCompatFlagSet(Compat flag
) { return (g_eCompatFlags
& flag
) == flag
; }
185 void resetCompatFlag() { g_eCompatFlags
= Compat::none
; }
187 void setLocale(const LanguageTag
& rLanguageTag
)
189 g_aLanguageAndLocale
.setLocale(rLanguageTag
);
192 const LanguageTag
& getLocale()
194 const LanguageTag
& rLocale
= g_aLanguageAndLocale
.getLocale();
195 SAL_INFO_IF(rLocale
.getLanguageType() == LANGUAGE_NONE
, "comphelper.lok", "Locale not set");
199 void setLanguageTag(const LanguageTag
& rLanguageTag
)
201 g_aLanguageAndLocale
.setLanguage(rLanguageTag
);
204 const LanguageTag
& getLanguageTag()
206 const LanguageTag
& rLanguage
= g_aLanguageAndLocale
.getLanguage();
207 SAL_INFO_IF(rLanguage
.getLanguageType() == LANGUAGE_NONE
, "comphelper.lok", "Language not set");
211 bool isAllowlistedLanguage(const OUString
& lang
)
216 #if defined ANDROID || defined IOS
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");
226 std::stringstream
stream(pAllowlist
);
229 std::cerr
<< "Allowlisted languages: ";
230 while (getline(stream
, s
, ' ')) {
234 std::cerr
<< s
<< " ";
235 aList
.emplace_back(OStringToOUString(s
, RTL_TEXTENCODING_UTF8
));
237 std::cerr
<< std::endl
;
241 std::cerr
<< "No language allowlisted, turning off the language support." << std::endl
;
246 if (aAllowlist
.empty())
249 for (const auto& entry
: aAllowlist
)
251 if (lang
.startsWith(entry
))
253 if (lang
.startsWith(entry
.replace('_', '-')))
261 void setTimezone(bool isSet
, const OUString
& rTimezone
)
265 // Set the given timezone, even if empty.
266 osl_setEnvironment(OUString("TZ").pData
, rTimezone
.pData
);
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
);
275 // Update the timezone data.
279 static void (*pStatusIndicatorCallback
)(void *data
, statusIndicatorCallbackType type
, int percent
, const char* pText
)(nullptr);
280 static void *pStatusIndicatorCallbackData(nullptr);
282 void setStatusIndicatorCallback(void (*callback
)(void *data
, statusIndicatorCallbackType type
, int percent
, const char* pText
), void *data
)
284 pStatusIndicatorCallback
= callback
;
285 pStatusIndicatorCallbackData
= data
;
288 void statusIndicatorStart(const OUString
& sText
)
290 if (pStatusIndicatorCallback
)
291 pStatusIndicatorCallback(pStatusIndicatorCallbackData
, statusIndicatorCallbackType::Start
, 0, sText
.toUtf8().getStr());
294 void statusIndicatorSetValue(int percent
)
296 if (pStatusIndicatorCallback
)
297 pStatusIndicatorCallback(pStatusIndicatorCallbackData
, statusIndicatorCallbackType::SetValue
, percent
, nullptr);
300 void statusIndicatorFinish()
302 if (pStatusIndicatorCallback
)
303 pStatusIndicatorCallback(pStatusIndicatorCallbackData
, statusIndicatorCallbackType::Finish
, 0, nullptr);
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */