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 <i18nlangtag/languagetag.hxx>
12 #include <sal/log.hxx>
13 
14 #include <iostream>
15 
16 namespace comphelper::LibreOfficeKit
17 {
18 
19 static bool g_bActive(false);
20 
21 static bool g_bPartInInvalidation(false);
22 
23 static bool g_bTiledPainting(false);
24 
25 static bool g_bDialogPainting(false);
26 
27 static bool g_bTiledAnnotations(true);
28 
29 static bool g_bRangeHeaders(false);
30 
31 static bool g_bViewIdForVisCursorInvalidation(false);
32 
33 static bool g_bLocalRendering(false);
34 
35 static Compat g_eCompatFlags(Compat::none);
36 
37 namespace
38 {
39 
40 class LanguageAndLocale
41 {
42 private:
43     LanguageTag maLanguageTag;
44     LanguageTag maLocaleLanguageTag;
45 
46 public:
47 
LanguageAndLocale()48     LanguageAndLocale()
49         : maLanguageTag(LANGUAGE_NONE)
50         , maLocaleLanguageTag(LANGUAGE_NONE)
51     {}
52 
getLanguage() const53     const LanguageTag& getLanguage() const
54     {
55         return maLanguageTag;
56     }
57 
setLanguage(const LanguageTag & rLanguageTag)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 
getLocale() const67     const LanguageTag& getLocale() const
68     {
69         return maLocaleLanguageTag;
70     }
71 
setLocale(const LanguageTag & rLocaleLanguageTag)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 
85 static LanguageAndLocale g_aLanguageAndLocale;
86 
87 /// Scaling of the cairo canvas painting for hi-dpi
88 static double g_fDPIScale(1.0);
89 
setActive(bool bActive)90 void setActive(bool bActive)
91 {
92     g_bActive = bActive;
93 }
94 
isActive()95 bool isActive()
96 {
97     return g_bActive;
98 }
99 
setPartInInvalidation(bool bPartInInvalidation)100 void setPartInInvalidation(bool bPartInInvalidation)
101 {
102     g_bPartInInvalidation = bPartInInvalidation;
103 }
104 
isPartInInvalidation()105 bool isPartInInvalidation()
106 {
107     return g_bPartInInvalidation;
108 }
109 
setTiledPainting(bool bTiledPainting)110 void setTiledPainting(bool bTiledPainting)
111 {
112     g_bTiledPainting = bTiledPainting;
113 }
114 
isTiledPainting()115 bool isTiledPainting()
116 {
117     return g_bTiledPainting;
118 }
119 
setDialogPainting(bool bDialogPainting)120 void setDialogPainting(bool bDialogPainting)
121 {
122     g_bDialogPainting = bDialogPainting;
123 }
124 
isDialogPainting()125 bool isDialogPainting()
126 {
127     return g_bDialogPainting;
128 }
129 
setDPIScale(double fDPIScale)130 void setDPIScale(double fDPIScale)
131 {
132     g_fDPIScale = fDPIScale;
133 }
134 
getDPIScale()135 double getDPIScale()
136 {
137     return g_fDPIScale;
138 }
139 
setTiledAnnotations(bool bTiledAnnotations)140 void setTiledAnnotations(bool bTiledAnnotations)
141 {
142     g_bTiledAnnotations = bTiledAnnotations;
143 }
144 
isTiledAnnotations()145 bool isTiledAnnotations()
146 {
147     return g_bTiledAnnotations;
148 }
149 
setRangeHeaders(bool bRangeHeaders)150 void setRangeHeaders(bool bRangeHeaders)
151 {
152     g_bRangeHeaders = bRangeHeaders;
153 }
154 
setViewIdForVisCursorInvalidation(bool bViewIdForVisCursorInvalidation)155 void setViewIdForVisCursorInvalidation(bool bViewIdForVisCursorInvalidation)
156 {
157     g_bViewIdForVisCursorInvalidation = bViewIdForVisCursorInvalidation;
158 }
159 
isViewIdForVisCursorInvalidation()160 bool isViewIdForVisCursorInvalidation()
161 {
162     return g_bViewIdForVisCursorInvalidation;
163 }
164 
isRangeHeaders()165 bool isRangeHeaders()
166 {
167     return g_bRangeHeaders;
168 }
169 
setLocalRendering(bool bLocalRendering)170 void setLocalRendering(bool bLocalRendering)
171 {
172     g_bLocalRendering = bLocalRendering;
173 }
174 
isLocalRendering()175 bool isLocalRendering()
176 {
177     return g_bLocalRendering;
178 }
179 
setCompatFlag(Compat flag)180 void setCompatFlag(Compat flag) { g_eCompatFlags = static_cast<Compat>(g_eCompatFlags | flag); }
181 
isCompatFlagSet(Compat flag)182 bool isCompatFlagSet(Compat flag) { return (g_eCompatFlags & flag) == flag; }
183 
setLocale(const LanguageTag & rLanguageTag)184 void setLocale(const LanguageTag& rLanguageTag)
185 {
186     g_aLanguageAndLocale.setLocale(rLanguageTag);
187 }
188 
getLocale()189 const LanguageTag& getLocale()
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 
setLanguageTag(const LanguageTag & rLanguageTag)196 void setLanguageTag(const LanguageTag& rLanguageTag)
197 {
198     g_aLanguageAndLocale.setLanguage(rLanguageTag);
199 }
200 
getLanguageTag()201 const LanguageTag& getLanguageTag()
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 
isAllowlistedLanguage(const OUString & lang)208 bool 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 
258 static void (*pStatusIndicatorCallback)(void *data, statusIndicatorCallbackType type, int percent)(nullptr);
259 static void *pStatusIndicatorCallbackData(nullptr);
260 
setStatusIndicatorCallback(void (* callback)(void * data,statusIndicatorCallbackType type,int percent),void * data)261 void setStatusIndicatorCallback(void (*callback)(void *data, statusIndicatorCallbackType type, int percent), void *data)
262 {
263     pStatusIndicatorCallback = callback;
264     pStatusIndicatorCallbackData = data;
265 }
266 
statusIndicatorStart()267 void statusIndicatorStart()
268 {
269     if (pStatusIndicatorCallback)
270         pStatusIndicatorCallback(pStatusIndicatorCallbackData, statusIndicatorCallbackType::Start, 0);
271 }
272 
statusIndicatorSetValue(int percent)273 void statusIndicatorSetValue(int percent)
274 {
275     if (pStatusIndicatorCallback)
276         pStatusIndicatorCallback(pStatusIndicatorCallbackData, statusIndicatorCallbackType::SetValue, percent);
277 }
278 
statusIndicatorFinish()279 void statusIndicatorFinish()
280 {
281     if (pStatusIndicatorCallback)
282         pStatusIndicatorCallback(pStatusIndicatorCallbackData, statusIndicatorCallbackType::Finish, 0);
283 }
284 
285 } // namespace
286 
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
288