Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / comphelper / source / misc / lok.cxx
blobb5cbcc74cb541d645d2659c876d5cead5fdfd9e9
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <comphelper/lok.hxx>
11 #include <osl/process.h>
12 #include <i18nlangtag/languagetag.hxx>
13 #include <sal/log.hxx>
15 #include <iostream>
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);
38 namespace
41 class LanguageAndLocale
43 private:
44 LanguageTag maLanguageTag;
45 LanguageTag maLocaleLanguageTag;
47 public:
49 LanguageAndLocale()
50 : maLanguageTag(LANGUAGE_NONE)
51 , maLocaleLanguageTag(LANGUAGE_NONE)
54 const LanguageTag& getLanguage() const
56 return maLanguageTag;
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)
93 g_bActive = bActive;
96 bool isActive()
98 return g_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;
136 double getDPIScale()
138 return g_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");
196 return rLocale;
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");
208 return rLanguage;
211 bool isAllowlistedLanguage(const OUString& lang)
213 if (!isActive())
214 return true;
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)
226 std::stringstream stream(pAllowlist);
227 std::string s;
229 std::cerr << "Allowlisted languages: ";
230 while (getline(stream, s, ' ')) {
231 if (s.length() == 0)
232 continue;
234 std::cerr << s << " ";
235 aList.emplace_back(OStringToOUString(s, RTL_TEXTENCODING_UTF8));
237 std::cerr << std::endl;
240 if (aList.empty())
241 std::cerr << "No language allowlisted, turning off the language support." << std::endl;
243 return aList;
244 }();
246 if (aAllowlist.empty())
247 return false;
249 for (const auto& entry : aAllowlist)
251 if (lang.startsWith(entry))
252 return true;
253 if (lang.startsWith(entry.replace('_', '-')))
254 return true;
257 return false;
258 #endif
261 void setTimezone(bool isSet, const OUString& rTimezone)
263 if (isSet)
265 // Set the given timezone, even if empty.
266 osl_setEnvironment(OUString("TZ").pData, rTimezone.pData);
268 else
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.
276 ::tzset();
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);
306 } // namespace
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */