VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/VirtualBoxTranslator.cpp@ 92154

Last change on this file since 92154 was 92145, checked in by vboxsync, 3 years ago

Main/VirtualBoxTranslator.cpp: Write lock the object in the destructor to try prevent destroying it while someone is changing the language (com::Shutdown / notification race). Introduced some logging. bugref:1909

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/* $Id: VirtualBoxTranslator.cpp 92145 2021-10-29 13:00:02Z 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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_MAIN_VIRTUALBOXCLIENT /** @todo add separate logging group! */
23#include "LoggingNew.h"
24
25#include <iprt/asm.h>
26#include <iprt/ctype.h>
27#include <iprt/err.h>
28#include <iprt/locale.h>
29#include <iprt/once.h>
30#include <iprt/path.h>
31#include <iprt/string.h>
32#include <iprt/thread.h>
33#include <iprt/strcache.h>
34
35#include "Global.h"
36#include "VirtualBoxBase.h"
37#include "QMTranslator.h"
38#include "VirtualBoxTranslator.h"
39
40
41/*********************************************************************************************************************************
42* Defined Constants And Macros *
43*********************************************************************************************************************************/
44#define TRANSLATOR_CACHE_SIZE 32
45
46
47/*********************************************************************************************************************************
48* Global Variables *
49*********************************************************************************************************************************/
50/** Init once for the critical section. */
51static RTONCE g_Once = RTONCE_INITIALIZER;
52RTCRITSECTRW VirtualBoxTranslator::s_instanceRwLock;
53VirtualBoxTranslator *VirtualBoxTranslator::s_pInstance = NULL;
54/** TLS index that points to the translated text. */
55static RTTLS g_idxTlsTr = NIL_RTTLS;
56/** TLS index that points to the original text. */
57static RTTLS g_idxTlsSrc = NIL_RTTLS;
58
59
60
61/**
62 * @callback_method_impl{FNRTONCE}
63 */
64static DECLCALLBACK(int32_t) initLock(void *pvUser)
65{
66 RT_NOREF(pvUser);
67 return VirtualBoxTranslator::initCritSect();
68}
69
70
71VirtualBoxTranslator::VirtualBoxTranslator()
72 : util::RWLockHandle(util::LOCKCLASS_TRANSLATOR)
73 , m_cInstanceRefs(0)
74 , m_pDefaultComponent(NULL)
75 , m_strLanguage("C")
76 , m_hStrCache(NIL_RTSTRCACHE)
77{
78 g_idxTlsTr = RTTlsAlloc();
79 g_idxTlsSrc = RTTlsAlloc();
80 int rc = RTStrCacheCreate(&m_hStrCache, "API Translation");
81 m_rcCache = rc;
82 if (RT_FAILURE(rc))
83 m_hStrCache = NIL_RTSTRCACHE; /* (loadLanguage will fail) */
84 LogFlowFunc(("m_rcCache=%Rrc g_idxTlsTr=%#x g_idxTlsSrc=%#x\n", m_rcCache, g_idxTlsTr, g_idxTlsSrc));
85}
86
87
88VirtualBoxTranslator::~VirtualBoxTranslator()
89{
90 LogFlowFunc(("enter\n"));
91
92 /* Write-lock the object as we could be racing language change
93 notifications processing during XPCOM shutdown. (risky?) */
94 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
95
96 RTTlsFree(g_idxTlsTr);
97 g_idxTlsTr = NIL_RTTLS;
98 RTTlsFree(g_idxTlsSrc);
99 g_idxTlsSrc = NIL_RTTLS;
100
101 m_pDefaultComponent = NULL;
102
103 for (TranslatorList::iterator it = m_lTranslators.begin();
104 it != m_lTranslators.end();
105 ++it)
106 {
107 if (it->pTranslator != NULL)
108 delete it->pTranslator;
109 it->pTranslator = NULL;
110 }
111 if (m_hStrCache != NIL_RTSTRCACHE)
112 {
113 RTStrCacheDestroy(m_hStrCache);
114 m_hStrCache = NIL_RTSTRCACHE;
115 m_rcCache = VERR_WRONG_ORDER;
116 }
117 LogFlowFunc(("returns\n"));
118}
119
120
121/**
122 * Get or create a translator instance (singelton), referenced.
123 *
124 * The main reference is held by the main VBox singelton objects (VirtualBox,
125 * VirtualBoxClient) tying it's lifetime to theirs.
126 */
127/* static */
128VirtualBoxTranslator *VirtualBoxTranslator::instance()
129{
130 int rc = RTOnce(&g_Once, initLock, NULL);
131 if (RT_SUCCESS(rc))
132 {
133 RTCritSectRwEnterShared(&s_instanceRwLock);
134 VirtualBoxTranslator *pInstance = s_pInstance;
135 if (RT_LIKELY(pInstance != NULL))
136 {
137 uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
138 Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
139 RTCritSectRwLeaveShared(&s_instanceRwLock);
140 return pInstance;
141 }
142
143 /* Maybe create the instance: */
144 RTCritSectRwLeaveShared(&s_instanceRwLock);
145 RTCritSectRwEnterExcl(&s_instanceRwLock);
146 pInstance = s_pInstance;
147 if (pInstance == NULL)
148 s_pInstance = pInstance = new VirtualBoxTranslator();
149 ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
150 RTCritSectRwLeaveExcl(&s_instanceRwLock);
151 return pInstance;
152 }
153 return NULL;
154}
155
156
157/* static */
158VirtualBoxTranslator *VirtualBoxTranslator::tryInstance() RT_NOEXCEPT
159{
160 int rc = RTOnce(&g_Once, initLock, NULL);
161 if (RT_SUCCESS(rc))
162 {
163 RTCritSectRwEnterShared(&s_instanceRwLock);
164 VirtualBoxTranslator *pInstance = s_pInstance;
165 if (RT_LIKELY(pInstance != NULL))
166 {
167 uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
168 Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
169 }
170 RTCritSectRwLeaveShared(&s_instanceRwLock);
171 return pInstance;
172 }
173 return NULL;
174}
175
176
177/**
178 * Release translator reference previous obtained via instance() or
179 * tryinstance().
180 */
181void VirtualBoxTranslator::release()
182{
183 RTCritSectRwEnterShared(&s_instanceRwLock);
184 uint32_t cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
185 Assert(cRefs < _8K);
186 if (RT_LIKELY(cRefs > 0))
187 RTCritSectRwLeaveShared(&s_instanceRwLock);
188 else
189 {
190 /* Looks like we've got the last reference. Must switch to exclusive
191 mode for safe cleanup. */
192 ASMAtomicIncU32(&m_cInstanceRefs);
193 RTCritSectRwLeaveShared(&s_instanceRwLock);
194 RTCritSectRwEnterExcl(&s_instanceRwLock);
195 cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
196 Assert(cRefs < _8K);
197 if (cRefs == 0)
198 {
199 s_pInstance = NULL;
200 delete this;
201 }
202 RTCritSectRwLeaveExcl(&s_instanceRwLock);
203 }
204}
205
206
207HRESULT VirtualBoxTranslator::loadLanguage(ComPtr<IVirtualBox> aVirtualBox)
208{
209 AssertReturn(aVirtualBox, E_INVALIDARG);
210
211 ComPtr<ISystemProperties> pSystemProperties;
212 HRESULT hrc = aVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
213 if (SUCCEEDED(hrc))
214 {
215 com::Bstr bstrLocale;
216 hrc = pSystemProperties->COMGETTER(LanguageId)(bstrLocale.asOutParam());
217 if (SUCCEEDED(hrc))
218 {
219 int vrc = i_loadLanguage(com::Utf8Str(bstrLocale).c_str());
220 if (RT_FAILURE(vrc))
221 hrc = Global::vboxStatusCodeToCOM(vrc);
222 }
223 }
224 return hrc;
225}
226
227
228int VirtualBoxTranslator::i_loadLanguage(const char *pszLang)
229{
230 LogFlowFunc(("pszLang=%s\n", pszLang));
231 int rc = VINF_SUCCESS;
232 char szLocale[256];
233 if (pszLang == NULL || *pszLang == '\0')
234 {
235 rc = RTLocaleQueryNormalizedBaseLocaleName(szLocale, sizeof(szLocale));
236 if (RT_SUCCESS(rc))
237 pszLang = szLocale;
238 }
239 else
240 {
241 /* check the pszLang looks like language code, i.e. {ll} or {ll}_{CC} */
242 size_t cbLang = strlen(pszLang);
243 if ( !(cbLang == 1 && pszLang[0] == 'C')
244 && !(cbLang == 2 && RT_C_IS_LOWER(pszLang[0]) && RT_C_IS_LOWER(pszLang[1]))
245 && !(cbLang == 5 && RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszLang)))
246 rc = VERR_INVALID_PARAMETER;
247 }
248 if (RT_SUCCESS(rc))
249 {
250 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
251
252 m_strLanguage = pszLang;
253
254 for (TranslatorList::iterator it = m_lTranslators.begin();
255 it != m_lTranslators.end();
256 ++it)
257 {
258 /* ignore errors from particular translator allowing the use of others */
259 i_loadLanguageForComponent(&(*it), pszLang);
260 }
261 }
262 return rc;
263}
264
265
266int VirtualBoxTranslator::i_loadLanguageForComponent(TranslatorComponent *aComponent, const char *aLang)
267{
268 AssertReturn(aComponent, VERR_INVALID_PARAMETER);
269 LogFlow(("aComponent=%s aLang=%s\n", aComponent->strPath.c_str(), aLang));
270
271 int rc;
272 if (strcmp(aLang, "C") != 0)
273 {
274 /* Construct the base filename for the translations: */
275 char szNlsPath[RTPATH_MAX];
276 /* Try load language file on form 'VirtualBoxAPI_ll_CC.qm' if it exists
277 where 'll_CC' could for example be 'en_US' or 'de_CH': */
278 ssize_t cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%s.qm",
279 aComponent->strPath.c_str(), aLang);
280 if (cchOkay > 0)
281 rc = i_setLanguageFile(aComponent, szNlsPath);
282 else
283 rc = VERR_FILENAME_TOO_LONG;
284 if (RT_FAILURE(rc))
285 {
286 /* No luck, drop the country part, i.e. 'VirtualBoxAPI_de.qm' or 'VirtualBoxAPI_en.qm': */
287 const char *pszDash = strchr(aLang, '_');
288 if (pszDash && pszDash != aLang)
289 {
290 cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%.*s.qm",
291 aComponent->strPath.c_str(), pszDash - aLang, aLang);
292 if (cchOkay > 0)
293 rc = i_setLanguageFile(aComponent, szNlsPath);
294 }
295 }
296 }
297 else
298 {
299 /* No translator needed for 'C' */
300 delete aComponent->pTranslator;
301 aComponent->pTranslator = NULL;
302 rc = VINF_SUCCESS;
303 }
304 return rc;
305}
306
307
308int VirtualBoxTranslator::i_setLanguageFile(TranslatorComponent *aComponent, const char *aFileName)
309{
310 AssertReturn(aComponent, VERR_INVALID_PARAMETER);
311
312 int rc = m_rcCache;
313 if (m_hStrCache != NIL_RTSTRCACHE)
314 {
315 QMTranslator *pNewTranslator;
316 try { pNewTranslator = new QMTranslator(); }
317 catch (std::bad_alloc &) { pNewTranslator = NULL; }
318 if (pNewTranslator)
319 {
320 rc = pNewTranslator->load(aFileName, m_hStrCache);
321 if (RT_SUCCESS(rc))
322 {
323 if (aComponent->pTranslator)
324 delete aComponent->pTranslator;
325 aComponent->pTranslator = pNewTranslator;
326 }
327 else
328 delete pNewTranslator;
329 }
330 else
331 rc = VERR_NO_MEMORY;
332 }
333 else
334 Assert(RT_FAILURE_NP(rc));
335 return rc;
336}
337
338
339int VirtualBoxTranslator::registerTranslation(const char *aTranslationPath,
340 bool aDefault,
341 PTRCOMPONENT *aComponent)
342{
343 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
344 int rc = VERR_GENERAL_FAILURE;
345 if (pCurrInstance != NULL)
346 {
347 rc = pCurrInstance->i_registerTranslation(aTranslationPath, aDefault, aComponent);
348 pCurrInstance->release();
349 }
350 return rc;
351}
352
353
354int VirtualBoxTranslator::i_registerTranslation(const char *aTranslationPath,
355 bool aDefault,
356 PTRCOMPONENT *aComponent)
357{
358 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
359 TranslatorComponent *pComponent;
360 for (TranslatorList::iterator it = m_lTranslators.begin();
361 it != m_lTranslators.end();
362 ++it)
363 {
364 if (it->strPath == aTranslationPath)
365 {
366 pComponent = &(*it);
367 if (aDefault)
368 m_pDefaultComponent = pComponent;
369 *aComponent = (PTRCOMPONENT)pComponent;
370 return VINF_SUCCESS;
371 }
372 }
373
374 try
375 {
376 m_lTranslators.push_back(TranslatorComponent());
377 pComponent = &m_lTranslators.back();
378 }
379 catch(std::bad_alloc &)
380 {
381 return VERR_NO_MEMORY;
382 }
383
384 pComponent->strPath = aTranslationPath;
385 if (aDefault)
386 m_pDefaultComponent = pComponent;
387 *aComponent = (PTRCOMPONENT)pComponent;
388 /* ignore the error during loading because path
389 * could contain no translation for current language */
390 i_loadLanguageForComponent(pComponent, m_strLanguage.c_str());
391 return VINF_SUCCESS;
392}
393
394
395int VirtualBoxTranslator::unregisterTranslation(PTRCOMPONENT aComponent)
396{
397 int rc;
398 if (aComponent != NULL)
399 {
400 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
401 if (pCurrInstance != NULL)
402 {
403 rc = pCurrInstance->i_unregisterTranslation(aComponent);
404 pCurrInstance->release();
405 }
406 else
407 rc = VERR_GENERAL_FAILURE;
408 }
409 else
410 rc = VWRN_NOT_FOUND;
411 return rc;
412}
413
414
415int VirtualBoxTranslator::i_unregisterTranslation(PTRCOMPONENT aComponent)
416{
417 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
418
419 if (aComponent == m_pDefaultComponent)
420 m_pDefaultComponent = NULL;
421
422 for (TranslatorList::iterator it = m_lTranslators.begin();
423 it != m_lTranslators.end();
424 ++it)
425 {
426 if (&(*it) == aComponent)
427 {
428 delete aComponent->pTranslator;
429 m_lTranslators.erase(it);
430 return VINF_SUCCESS;
431 }
432 }
433
434 return VERR_NOT_FOUND;
435}
436
437
438const char *VirtualBoxTranslator::translate(PTRCOMPONENT aComponent,
439 const char *aContext,
440 const char *aSourceText,
441 const char *aComment,
442 const size_t aNum) RT_NOEXCEPT
443{
444 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
445 const char *pszTranslation = aSourceText;
446 if (pCurrInstance != NULL)
447 {
448 pszTranslation = pCurrInstance->i_translate(aComponent, aContext, aSourceText, aComment, aNum);
449 pCurrInstance->release();
450 }
451 return pszTranslation;
452}
453
454
455const char *VirtualBoxTranslator::i_translate(PTRCOMPONENT aComponent,
456 const char *aContext,
457 const char *aSourceText,
458 const char *aComment,
459 const size_t aNum) RT_NOEXCEPT
460{
461 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
462
463 if (aComponent == NULL)
464 aComponent = m_pDefaultComponent;
465
466 if ( aComponent == NULL
467 || aComponent->pTranslator == NULL)
468 return aSourceText;
469
470 const char *pszSafeSource = NULL;
471 const char *pszTranslation = aComponent->pTranslator->translate(aContext, aSourceText, &pszSafeSource, aComment, aNum);
472 if (pszSafeSource && g_idxTlsSrc != NIL_RTTLS && g_idxTlsTr != NIL_RTTLS)
473 {
474 RTTlsSet(g_idxTlsTr, (void *)pszTranslation);
475 RTTlsSet(g_idxTlsSrc, (void *)pszSafeSource);
476 }
477
478 return pszTranslation;
479}
480
481
482const char *VirtualBoxTranslator::trSource(const char *aTranslation) RT_NOEXCEPT
483{
484 const char *pszSource = aTranslation;
485 VirtualBoxTranslator *pCurInstance = VirtualBoxTranslator::tryInstance(); /* paranoia */
486 if (pCurInstance != NULL)
487 {
488 if (g_idxTlsSrc != NIL_RTTLS && g_idxTlsTr != NIL_RTTLS)
489 {
490 const char * const pszTranslationTls = (const char *)RTTlsGet(g_idxTlsTr);
491 const char * const pszSourceTls = (const char *)RTTlsGet(g_idxTlsSrc);
492 if ( pszSourceTls != NULL
493 && pszTranslationTls != NULL
494 && ( pszTranslationTls == aTranslation
495 || strcmp(pszTranslationTls, aTranslation) == 0))
496 pszSource = pszSourceTls;
497 }
498 pCurInstance->release();
499 }
500 return pszSource;
501}
502
503
504int32_t VirtualBoxTranslator::initCritSect()
505{
506 return RTCritSectRwInit(&s_instanceRwLock);
507}
508/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use