1 | /* $Id: VirtualBoxTranslator.cpp 90881 2021-08-25 13:28:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Translator class.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <iprt/asm.h>
|
---|
19 | #include <iprt/ctype.h>
|
---|
20 | #include <iprt/err.h>
|
---|
21 | #include <iprt/locale.h>
|
---|
22 | #include <iprt/once.h>
|
---|
23 | #include <iprt/path.h>
|
---|
24 | #include <iprt/string.h>
|
---|
25 | #include <iprt/thread.h>
|
---|
26 | #include <iprt/strcache.h>
|
---|
27 |
|
---|
28 | #include "Global.h"
|
---|
29 | #include "VirtualBoxBase.h"
|
---|
30 | #include "QMTranslator.h"
|
---|
31 | #include "VirtualBoxTranslator.h"
|
---|
32 |
|
---|
33 | #define TRANSLATOR_CACHE_SIZE 32
|
---|
34 |
|
---|
35 | /** Init once for the critical section. */
|
---|
36 | static RTONCE g_Once = RTONCE_INITIALIZER;
|
---|
37 | RTCRITSECTRW VirtualBoxTranslator::s_instanceRwLock;
|
---|
38 | VirtualBoxTranslator *VirtualBoxTranslator::s_pInstance = NULL;
|
---|
39 | static RTTLS g_idxTls = NIL_RTTLS;
|
---|
40 |
|
---|
41 | typedef std::pair<const char *, const char *> LastTranslation;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * @callback_method_impl{FNRTONCE}
|
---|
45 | */
|
---|
46 | static DECLCALLBACK(int32_t) initLock(void *pvUser)
|
---|
47 | {
|
---|
48 | RT_NOREF(pvUser);
|
---|
49 | return VirtualBoxTranslator::initCritSect();
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * @callback_method_impl{FNRTTLSDTOR, Destroys the cache during thread termination.}
|
---|
55 | */
|
---|
56 | static DECLCALLBACK(void) freeThreadCache(void *pvValue) RT_NOTHROW_DEF
|
---|
57 | {
|
---|
58 | if (pvValue != NULL)
|
---|
59 | {
|
---|
60 | LastTranslation *pCache = (LastTranslation *)pvValue;
|
---|
61 | delete pCache;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | VirtualBoxTranslator::VirtualBoxTranslator()
|
---|
67 | : util::RWLockHandle(VBoxLockingClass::LOCKCLASS_TRANSLATOR)
|
---|
68 | , m_cInstanceRefs(0)
|
---|
69 | , m_pTranslator(NULL)
|
---|
70 | , m_hStrCache(NIL_RTSTRCACHE)
|
---|
71 | {
|
---|
72 | RTTlsAllocEx(&g_idxTls, &freeThreadCache);
|
---|
73 | int rc = RTStrCacheCreate(&m_hStrCache, "API Translation");
|
---|
74 | m_rcCache = rc;
|
---|
75 | if (RT_FAILURE(rc))
|
---|
76 | m_hStrCache = NIL_RTSTRCACHE; /* (loadLanguage will fail) */
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | VirtualBoxTranslator::~VirtualBoxTranslator()
|
---|
81 | {
|
---|
82 | if (g_idxTls != NIL_RTTLS)
|
---|
83 | {
|
---|
84 | void *pvTlsValue = NULL;
|
---|
85 | int rc = RTTlsGetEx(g_idxTls, &pvTlsValue);
|
---|
86 | if (RT_SUCCESS(rc))
|
---|
87 | {
|
---|
88 | freeThreadCache(pvTlsValue);
|
---|
89 | RTTlsSet(g_idxTls, NULL);
|
---|
90 | }
|
---|
91 | RTTlsFree(g_idxTls);
|
---|
92 | g_idxTls = NIL_RTTLS;
|
---|
93 | }
|
---|
94 | if (m_pTranslator)
|
---|
95 | {
|
---|
96 | delete m_pTranslator;
|
---|
97 | m_pTranslator = NULL;
|
---|
98 | }
|
---|
99 | if (m_hStrCache != NIL_RTSTRCACHE)
|
---|
100 | {
|
---|
101 | RTStrCacheDestroy(m_hStrCache);
|
---|
102 | m_hStrCache = NIL_RTSTRCACHE;
|
---|
103 | m_rcCache = VERR_WRONG_ORDER;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Get or create a translator instance (singelton), referenced.
|
---|
110 | *
|
---|
111 | * The main reference is held by the main VBox singelton objects (VirtualBox,
|
---|
112 | * VirtualBoxClient) tying it's lifetime to theirs.
|
---|
113 | */
|
---|
114 | /* static */
|
---|
115 | VirtualBoxTranslator *VirtualBoxTranslator::instance()
|
---|
116 | {
|
---|
117 | int rc = RTOnce(&g_Once, initLock, NULL);
|
---|
118 | if (RT_SUCCESS(rc))
|
---|
119 | {
|
---|
120 | RTCritSectRwEnterShared(&s_instanceRwLock);
|
---|
121 | VirtualBoxTranslator *pInstance = s_pInstance;
|
---|
122 | if (RT_LIKELY(pInstance != NULL))
|
---|
123 | {
|
---|
124 | uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
|
---|
125 | Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
|
---|
126 | RTCritSectRwLeaveShared(&s_instanceRwLock);
|
---|
127 | return pInstance;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /* Maybe create the instance: */
|
---|
131 | RTCritSectRwLeaveShared(&s_instanceRwLock);
|
---|
132 | RTCritSectRwEnterExcl(&s_instanceRwLock);
|
---|
133 | pInstance = s_pInstance;
|
---|
134 | if (pInstance == NULL)
|
---|
135 | s_pInstance = pInstance = new VirtualBoxTranslator();
|
---|
136 | ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
|
---|
137 | RTCritSectRwLeaveExcl(&s_instanceRwLock);
|
---|
138 | return pInstance;
|
---|
139 | }
|
---|
140 | return NULL;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /* static */
|
---|
145 | VirtualBoxTranslator *VirtualBoxTranslator::tryInstance()
|
---|
146 | {
|
---|
147 | int rc = RTOnce(&g_Once, initLock, NULL);
|
---|
148 | if (RT_SUCCESS(rc))
|
---|
149 | {
|
---|
150 | RTCritSectRwEnterShared(&s_instanceRwLock);
|
---|
151 | VirtualBoxTranslator *pInstance = s_pInstance;
|
---|
152 | if (RT_LIKELY(pInstance != NULL))
|
---|
153 | {
|
---|
154 | uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
|
---|
155 | Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
|
---|
156 | }
|
---|
157 | RTCritSectRwLeaveShared(&s_instanceRwLock);
|
---|
158 | return pInstance;
|
---|
159 | }
|
---|
160 | return NULL;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Release translator reference previous obtained via instance() or
|
---|
166 | * i_instance().
|
---|
167 | */
|
---|
168 | void VirtualBoxTranslator::release()
|
---|
169 | {
|
---|
170 | RTCritSectRwEnterShared(&s_instanceRwLock);
|
---|
171 | uint32_t cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
|
---|
172 | Assert(cRefs < _8K);
|
---|
173 | if (RT_LIKELY(cRefs > 0))
|
---|
174 | RTCritSectRwLeaveShared(&s_instanceRwLock);
|
---|
175 | else
|
---|
176 | {
|
---|
177 | /* Looks like we've got the last reference. Must switch to exclusive
|
---|
178 | mode for safe cleanup. */
|
---|
179 | ASMAtomicIncU32(&m_cInstanceRefs);
|
---|
180 | RTCritSectRwLeaveShared(&s_instanceRwLock);
|
---|
181 | RTCritSectRwEnterExcl(&s_instanceRwLock);
|
---|
182 | cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
|
---|
183 | Assert(cRefs < _8K);
|
---|
184 | if (cRefs == 0)
|
---|
185 | {
|
---|
186 | s_pInstance = NULL;
|
---|
187 | delete this;
|
---|
188 | }
|
---|
189 | RTCritSectRwLeaveExcl(&s_instanceRwLock);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | HRESULT VirtualBoxTranslator::loadLanguage(ComPtr<IVirtualBox> aVirtualBox)
|
---|
195 | {
|
---|
196 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
197 |
|
---|
198 | ComPtr<ISystemProperties> pSystemProperties;
|
---|
199 | HRESULT hrc = aVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
|
---|
200 | if (SUCCEEDED(hrc))
|
---|
201 | {
|
---|
202 | com::Bstr bstrLocale;
|
---|
203 | hrc = pSystemProperties->COMGETTER(LanguageId)(bstrLocale.asOutParam());
|
---|
204 | if (SUCCEEDED(hrc))
|
---|
205 | {
|
---|
206 | int vrc = i_loadLanguage(com::Utf8Str(bstrLocale).c_str());
|
---|
207 | if (RT_FAILURE(vrc))
|
---|
208 | hrc = Global::vboxStatusCodeToCOM(vrc);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | return hrc;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | int VirtualBoxTranslator::i_loadLanguage(const char *pszLang)
|
---|
216 | {
|
---|
217 | int rc = VINF_SUCCESS;
|
---|
218 | char szLocale[256];
|
---|
219 | if (pszLang == NULL || *pszLang == '\0')
|
---|
220 | {
|
---|
221 | rc = RTLocaleQueryNormalizedBaseLocaleName(szLocale, sizeof(szLocale));
|
---|
222 | if (RT_SUCCESS(rc))
|
---|
223 | pszLang = szLocale;
|
---|
224 | }
|
---|
225 | else
|
---|
226 | {
|
---|
227 | /* check the pszLang looks like language code, i.e. {ll} or {ll}_{CC} */
|
---|
228 | size_t cbLang = strlen(pszLang);
|
---|
229 | if ( !(cbLang == 1 && pszLang[0] == 'C')
|
---|
230 | && !(cbLang == 2 && RT_C_IS_LOWER(pszLang[0]) && RT_C_IS_LOWER(pszLang[1]))
|
---|
231 | && !(cbLang == 5 && RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszLang)))
|
---|
232 | rc = VERR_INVALID_PARAMETER;
|
---|
233 | }
|
---|
234 | if (RT_SUCCESS(rc))
|
---|
235 | {
|
---|
236 | if (strcmp(pszLang, "C") != 0)
|
---|
237 | {
|
---|
238 | /* Construct the base filename for the translations: */
|
---|
239 | char szNlsPath[RTPATH_MAX];
|
---|
240 | rc = RTPathAppPrivateNoArch(szNlsPath, sizeof(szNlsPath));
|
---|
241 | if (RT_SUCCESS(rc))
|
---|
242 | rc = RTPathAppend(szNlsPath, sizeof(szNlsPath), "nls" RTPATH_SLASH_STR "VirtualBoxAPI_");
|
---|
243 | if (RT_SUCCESS(rc))
|
---|
244 | {
|
---|
245 | size_t const cchNlsBase = strlen(szNlsPath);
|
---|
246 |
|
---|
247 | /* Try load language file on form 'VirtualBoxAPI_ll_CC.qm' if it exists
|
---|
248 | where 'll_CC' could for example be 'en_US' or 'de_CH': */
|
---|
249 | ssize_t cchOkay = RTStrPrintf2(&szNlsPath[cchNlsBase], sizeof(szNlsPath) - cchNlsBase, "%s.qm", pszLang);
|
---|
250 | if (cchOkay > 0)
|
---|
251 | rc = i_setLanguageFile(szNlsPath);
|
---|
252 | else
|
---|
253 | rc = VERR_FILENAME_TOO_LONG;
|
---|
254 | if (RT_FAILURE(rc))
|
---|
255 | {
|
---|
256 | /* No luck, drop the country part, i.e. 'VirtualBoxAPI_de.qm' or 'VirtualBoxAPI_en.qm': */
|
---|
257 | const char *pszDash = strchr(pszLang, '_');
|
---|
258 | if (pszDash && pszDash != pszLang)
|
---|
259 | {
|
---|
260 | cchOkay = RTStrPrintf2(&szNlsPath[cchNlsBase], sizeof(szNlsPath) - cchNlsBase, "%.*s.qm",
|
---|
261 | pszDash - pszLang, pszLang);
|
---|
262 | if (cchOkay > 0)
|
---|
263 | rc = i_setLanguageFile(szNlsPath);
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 | else
|
---|
269 | {
|
---|
270 | /* No translator needed for 'C': */
|
---|
271 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
272 | delete m_pTranslator;
|
---|
273 | m_pTranslator = NULL;
|
---|
274 | }
|
---|
275 | }
|
---|
276 | return rc;
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | int VirtualBoxTranslator::i_setLanguageFile(const char *aFileName)
|
---|
281 | {
|
---|
282 | int rc = m_rcCache;
|
---|
283 | if (m_hStrCache != NIL_RTSTRCACHE)
|
---|
284 | {
|
---|
285 | QMTranslator *pNewTranslator;
|
---|
286 | try { pNewTranslator = new QMTranslator(); }
|
---|
287 | catch (std::bad_alloc &) { pNewTranslator = NULL; }
|
---|
288 | if (pNewTranslator)
|
---|
289 | {
|
---|
290 | rc = pNewTranslator->load(aFileName, m_hStrCache);
|
---|
291 | if (RT_SUCCESS(rc))
|
---|
292 | {
|
---|
293 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
294 |
|
---|
295 | if (m_pTranslator)
|
---|
296 | delete m_pTranslator;
|
---|
297 | m_pTranslator = pNewTranslator;
|
---|
298 | m_strLangFileName = aFileName;
|
---|
299 | }
|
---|
300 | else
|
---|
301 | delete pNewTranslator;
|
---|
302 | }
|
---|
303 | else
|
---|
304 | rc = VERR_NO_MEMORY;
|
---|
305 | }
|
---|
306 | else
|
---|
307 | Assert(RT_FAILURE_NP(rc));
|
---|
308 | return rc;
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | com::Utf8Str VirtualBoxTranslator::i_languageFile()
|
---|
313 | {
|
---|
314 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
315 | return m_strLangFileName;
|
---|
316 | }
|
---|
317 |
|
---|
318 |
|
---|
319 | const char *VirtualBoxTranslator::translate(const char *aContext,
|
---|
320 | const char *aSourceText,
|
---|
321 | const char *aComment,
|
---|
322 | const int aNum)
|
---|
323 | {
|
---|
324 | VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
|
---|
325 | const char *pszTranslation = aSourceText;
|
---|
326 | if (pCurrInstance != NULL)
|
---|
327 | {
|
---|
328 | pszTranslation = pCurrInstance->i_translate(aContext, aSourceText, aComment, aNum);
|
---|
329 | pCurrInstance->release();
|
---|
330 | }
|
---|
331 | return pszTranslation;
|
---|
332 | }
|
---|
333 |
|
---|
334 |
|
---|
335 | static LastTranslation *getTlsEntry() RT_NOEXCEPT
|
---|
336 | {
|
---|
337 | if (RT_LIKELY(g_idxTls != NIL_RTTLS))
|
---|
338 | {
|
---|
339 | LastTranslation *pEntry = (LastTranslation *)RTTlsGet(g_idxTls);
|
---|
340 | if (pEntry != NULL)
|
---|
341 | return pEntry; /* likely */
|
---|
342 |
|
---|
343 | /* Create a new entry. */
|
---|
344 | try
|
---|
345 | {
|
---|
346 | pEntry = new LastTranslation();
|
---|
347 | pEntry->first = pEntry->second = "";
|
---|
348 | int rc = RTTlsSet(g_idxTls, pEntry);
|
---|
349 | if (RT_SUCCESS(rc))
|
---|
350 | return pEntry;
|
---|
351 | delete pEntry;
|
---|
352 | }
|
---|
353 | catch (std::bad_alloc &)
|
---|
354 | {
|
---|
355 | }
|
---|
356 | }
|
---|
357 | return NULL;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | const char *VirtualBoxTranslator::i_translate(const char *aContext,
|
---|
362 | const char *aSourceText,
|
---|
363 | const char *aComment,
|
---|
364 | const int aNum)
|
---|
365 | {
|
---|
366 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
367 | if (!m_pTranslator)
|
---|
368 | return aSourceText;
|
---|
369 | const char *pszTranslation = m_pTranslator->translate(aContext, aSourceText, aComment, aNum);
|
---|
370 | alock.release();
|
---|
371 |
|
---|
372 | LastTranslation *pEntry = getTlsEntry();
|
---|
373 | if (pEntry)
|
---|
374 | {
|
---|
375 | pEntry->first = pszTranslation;
|
---|
376 | pEntry->second = aSourceText;
|
---|
377 | }
|
---|
378 |
|
---|
379 | return pszTranslation;
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | const char *VirtualBoxTranslator::trSource(const char *aTranslation)
|
---|
384 | {
|
---|
385 | const char *pszSource = aTranslation;
|
---|
386 | VirtualBoxTranslator *pCurInstance = VirtualBoxTranslator::tryInstance(); /* paranoia */
|
---|
387 | if (pCurInstance != NULL)
|
---|
388 | {
|
---|
389 | LastTranslation *pEntry = getTlsEntry();
|
---|
390 | if ( pEntry != NULL
|
---|
391 | && ( pEntry->first == aTranslation
|
---|
392 | || strcmp(pEntry->first, aTranslation) == 0) )
|
---|
393 | pszSource = pEntry->second;
|
---|
394 | pCurInstance->release();
|
---|
395 | }
|
---|
396 | return pszSource;
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | int32_t VirtualBoxTranslator::initCritSect()
|
---|
401 | {
|
---|
402 | return RTCritSectRwInit(&s_instanceRwLock);
|
---|
403 | }
|
---|
404 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|