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 
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 
36 
37 namespace
38 {
39 
40 class LanguageAndLocale
41 {
42 private:
45 
46 public:
47 
48  LanguageAndLocale()
49  : maLanguageTag(LANGUAGE_NONE)
50  , maLocaleLanguageTag(LANGUAGE_NONE)
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  {
69  return maLocaleLanguageTag;
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 
85 static LanguageAndLocale g_aLanguageAndLocale;
86 
88 static double g_fDPIScale(1.0);
89 
90 void setActive(bool bActive)
91 {
92  g_bActive = bActive;
93 }
94 
95 bool isActive()
96 {
97  return g_bActive;
98 }
99 
100 void setPartInInvalidation(bool bPartInInvalidation)
101 {
102  g_bPartInInvalidation = bPartInInvalidation;
103 }
104 
106 {
107  return g_bPartInInvalidation;
108 }
109 
110 void setTiledPainting(bool bTiledPainting)
111 {
112  g_bTiledPainting = bTiledPainting;
113 }
114 
116 {
117  return g_bTiledPainting;
118 }
119 
120 void setDialogPainting(bool bDialogPainting)
121 {
122  g_bDialogPainting = bDialogPainting;
123 }
124 
126 {
127  return g_bDialogPainting;
128 }
129 
130 void setDPIScale(double fDPIScale)
131 {
132  g_fDPIScale = fDPIScale;
133 }
134 
135 double getDPIScale()
136 {
137  return g_fDPIScale;
138 }
139 
140 void setTiledAnnotations(bool bTiledAnnotations)
141 {
142  g_bTiledAnnotations = bTiledAnnotations;
143 }
144 
146 {
147  return g_bTiledAnnotations;
148 }
149 
150 void setRangeHeaders(bool bRangeHeaders)
151 {
152  g_bRangeHeaders = bRangeHeaders;
153 }
154 
155 void setViewIdForVisCursorInvalidation(bool bViewIdForVisCursorInvalidation)
156 {
157  g_bViewIdForVisCursorInvalidation = bViewIdForVisCursorInvalidation;
158 }
159 
161 {
163 }
164 
166 {
167  return g_bRangeHeaders;
168 }
169 
170 void setLocalRendering(bool bLocalRendering)
171 {
172  g_bLocalRendering = bLocalRendering;
173 }
174 
176 {
177  return g_bLocalRendering;
178 }
179 
180 void setCompatFlag(Compat flag) { g_eCompatFlags = static_cast<Compat>(g_eCompatFlags | flag); }
181 
182 bool isCompatFlagSet(Compat flag) { return (g_eCompatFlags & flag) == flag; }
183 
184 void 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 
196 void 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 
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 
261 void setStatusIndicatorCallback(void (*callback)(void *data, statusIndicatorCallbackType type, int percent), void *data)
262 {
263  pStatusIndicatorCallback = callback;
265 }
266 
268 {
271 }
272 
273 void statusIndicatorSetValue(int percent)
274 {
277 }
278 
280 {
283 }
284 
285 } // namespace
286 
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define LANGUAGE_NONE
static void * pStatusIndicatorCallbackData(nullptr)
bool isAllowlistedLanguage(const OUString &lang)
If the language name should be used for this LOK instance.
Definition: lok.cxx:208
bool isTiledAnnotations()
Check if annotations rendering is turned off.
Definition: lok.cxx:145
LanguageType getLanguageType(bool bResolveSystem=true) const
#define SAL_INFO_IF(condition, area, stream)
void setViewIdForVisCursorInvalidation(bool bViewIdForVisCursorInvalidation)
Set whether clients want viewId in visible cursor invalidation payload.
Definition: lok.cxx:155
const OUString & getBcp47(bool bResolveSystem=true) const
void statusIndicatorFinish()
Definition: lok.cxx:279
LanguageTag maLocaleLanguageTag
Definition: lok.cxx:44
static Compat g_eCompatFlags(Compat::none)
void setTiledPainting(bool bTiledPainting)
Set if we are doing tiled painting.
Definition: lok.cxx:110
static void(* pStatusIndicatorCallback)(void *data, statusIndicatorCallbackType type, int percent)(nullptr)
Definition: lok.cxx:258
void setDPIScale(double fDPIScale)
Set the DPI scale for rendering for HiDPI displays.
Definition: lok.cxx:130
void setLanguageTag(const LanguageTag &rLanguageTag)
Update the current LOK's language.
Definition: lok.cxx:196
bool isRangeHeaders()
Check if range based header data is enabled.
Definition: lok.cxx:165
bool isCompatFlagSet(Compat flag)
Get compatibility flags.
Definition: lok.cxx:182
static bool g_bTiledPainting(false)
bool isTiledPainting()
Check if we are doing tiled painting.
Definition: lok.cxx:115
bool isDialogPainting()
Check if we are painting the dialog.
Definition: lok.cxx:125
bool isPartInInvalidation()
Check whether clients want a part number in an invalidation payload.
Definition: lok.cxx:105
static LanguageAndLocale g_aLanguageAndLocale
Definition: lok.cxx:85
static bool g_bRangeHeaders(false)
void setRangeHeaders(bool bRangeHeaders)
Set if we want range based header data.
Definition: lok.cxx:150
static bool g_bDialogPainting(false)
void setLocalRendering(bool bLocalRendering)
Shift the coordinates before rendering each bitmap.
Definition: lok.cxx:170
void setStatusIndicatorCallback(void(*callback)(void *data, statusIndicatorCallbackType type, int percent), void *data)
Definition: lok.cxx:261
const LanguageTag & getLanguageTag()
Get the current LOK's language.
Definition: lok.cxx:201
void setDialogPainting(bool bDialogPainting)
Set if we are painting the dialog.
Definition: lok.cxx:120
void statusIndicatorSetValue(int percent)
Definition: lok.cxx:273
static bool g_bLocalRendering(false)
void setCompatFlag(Compat flag)
Set compatibility flags.
Definition: lok.cxx:180
exports com.sun.star.chart2. data
Reference< XOutputStream > stream
const LanguageTag & getLocale()
Get the current LOK's locale.
Definition: lok.cxx:189
bool isViewIdForVisCursorInvalidation()
Check whether clients want viewId in visible cursor invalidation payload.
Definition: lok.cxx:160
static bool g_bViewIdForVisCursorInvalidation(false)
void setTiledAnnotations(bool bTiledAnnotations)
Set if we want no annotations rendering.
Definition: lok.cxx:140
#define SAL_INFO(area, stream)
static bool g_bTiledAnnotations(true)
static double g_fDPIScale(1.0)
Scaling of the cairo canvas painting for hi-dpi.
LanguageTag maLanguageTag
Definition: lok.cxx:43
ResultType type
void setActive(bool bActive)
Definition: lok.cxx:90
void setPartInInvalidation(bool bPartInInvalidation)
Set whether clients want a part number in an invalidation payload.
Definition: lok.cxx:100
static bool g_bActive(false)
double getDPIScale()
Get the DPI scale for rendering for HiDPI displays.
Definition: lok.cxx:135
static bool g_bPartInInvalidation(false)
void setLocale(const LanguageTag &rLanguageTag)
Update the current LOK's locale.
Definition: lok.cxx:184
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo