Revert "Related tdf#158739: blacklist Nvidia RTX 4050 for Skia hardware"
[LibreOffice.git] / basctl / source / basicide / localizationmgr.cxx
blob4d311741b4bbfcc9b5df2aa59de8ff811f2e8d1b
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <string_view>
24 #include <localizationmgr.hxx>
26 #include <basidesh.hxx>
27 #include <baside3.hxx>
28 #include <basobj.hxx>
29 #include <iderdll.hxx>
30 #include <dlged.hxx>
31 #include <managelang.hxx>
33 #include <com/sun/star/frame/XLayoutManager.hpp>
34 #include <com/sun/star/resource/MissingResourceException.hpp>
35 #include <com/sun/star/resource/XStringResourceSupplier.hpp>
36 #include <sfx2/bindings.hxx>
37 #include <sfx2/sfxsids.hrc>
38 #include <sfx2/viewfrm.hxx>
39 #include <tools/debug.hxx>
40 #include <utility>
41 #include <osl/diagnose.h>
42 #include <o3tl/string_view.hxx>
44 namespace basctl
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::beans;
51 using namespace ::com::sun::star::resource;
53 namespace
56 constexpr OUString aDot(u"."_ustr);
57 constexpr OUString aEsc(u"&"_ustr);
58 constexpr OUString aSemi(u";"_ustr);
60 } // namespace
62 LocalizationMgr::LocalizationMgr(
63 Shell* pShell,
64 ScriptDocument aDocument,
65 OUString aLibName,
66 Reference<XStringResourceManager> const& xStringResourceManager
67 ) :
68 m_xStringResourceManager(xStringResourceManager),
69 m_pShell(pShell),
70 m_aDocument(std::move(aDocument)),
71 m_aLibName(std::move(aLibName))
72 { }
74 bool LocalizationMgr::isLibraryLocalized ()
76 if (m_xStringResourceManager.is())
77 return m_xStringResourceManager->getLocales().hasElements();
78 return false;
81 void LocalizationMgr::handleTranslationbar ()
83 static constexpr OUString aToolBarResName = u"private:resource/toolbar/translationbar"_ustr;
85 Reference< beans::XPropertySet > xFrameProps
86 ( m_pShell->GetViewFrame().GetFrame().GetFrameInterface(), uno::UNO_QUERY );
87 if ( !xFrameProps.is() )
88 return;
90 Reference< css::frame::XLayoutManager > xLayoutManager;
91 uno::Any a = xFrameProps->getPropertyValue( "LayoutManager" );
92 a >>= xLayoutManager;
93 if ( xLayoutManager.is() )
95 if ( !isLibraryLocalized() )
97 xLayoutManager->destroyElement( aToolBarResName );
99 else
101 xLayoutManager->createElement( aToolBarResName );
102 xLayoutManager->requestElement( aToolBarResName );
108 // TODO: -> export from toolkit
111 static bool isLanguageDependentProperty( std::u16string_view aName )
113 static struct Prop
115 const char* sName;
116 sal_Int32 nNameLength;
118 const vProp[] =
120 { "Text", 4 },
121 { "Label", 5 },
122 { "Title", 5 },
123 { "HelpText", 8 },
124 { "CurrencySymbol", 14 },
125 { "StringItemList", 14 },
126 { nullptr, 0 }
129 for (Prop const* pProp = vProp; pProp->sName; ++pProp)
130 if (o3tl::equalsAscii(aName, std::string_view(pProp->sName, pProp->nNameLength)))
131 return true;
132 return false;
136 void LocalizationMgr::implEnableDisableResourceForAllLibraryDialogs( HandleResourceMode eMode )
138 Sequence< OUString > aDlgNames = m_aDocument.getObjectNames( E_DIALOGS, m_aLibName );
139 sal_Int32 nDlgCount = aDlgNames.getLength();
140 const OUString* pDlgNames = aDlgNames.getConstArray();
142 Reference< XStringResourceResolver > xDummyStringResolver;
143 for( sal_Int32 i = 0 ; i < nDlgCount ; i++ )
145 OUString aDlgName = pDlgNames[ i ];
146 if (VclPtr<DialogWindow> pWin = m_pShell->FindDlgWin(m_aDocument, m_aLibName, aDlgName))
148 Reference< container::XNameContainer > xDialog = pWin->GetDialog();
149 if( xDialog.is() )
151 // Handle dialog itself as control
152 Any aDialogCtrl;
153 aDialogCtrl <<= xDialog;
154 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
155 std::u16string_view(), m_xStringResourceManager, xDummyStringResolver, eMode );
157 // Handle all controls
158 Sequence< OUString > aNames = xDialog->getElementNames();
159 const OUString* pNames = aNames.getConstArray();
160 sal_Int32 nCtrls = aNames.getLength();
161 for( sal_Int32 j = 0 ; j < nCtrls ; ++j )
163 OUString aCtrlName( pNames[j] );
164 Any aCtrl = xDialog->getByName( aCtrlName );
165 implHandleControlResourceProperties( aCtrl, aDlgName,
166 aCtrlName, m_xStringResourceManager, xDummyStringResolver, eMode );
174 static OUString implCreatePureResourceId
175 ( std::u16string_view aDialogName, std::u16string_view aCtrlName,
176 std::u16string_view aPropName,
177 const Reference< XStringResourceManager >& xStringResourceManager )
179 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
180 OUString aPureIdStr = OUString::number( nUniqueId )
181 + aDot
182 + aDialogName
183 + aDot;
184 if( !aCtrlName.empty() )
186 aPureIdStr += aCtrlName + aDot;
188 aPureIdStr += aPropName;
189 return aPureIdStr;
192 // Works on xStringResourceManager's current language for SET_IDS/RESET_IDS,
193 // anyway only one language should exist when calling this method then,
194 // either the first one for mode SET_IDS or the last one for mode RESET_IDS
195 sal_Int32 LocalizationMgr::implHandleControlResourceProperties
196 (const Any& rControlAny, std::u16string_view aDialogName, std::u16string_view aCtrlName,
197 const Reference< XStringResourceManager >& xStringResourceManager,
198 const Reference< XStringResourceResolver >& xSourceStringResolver, HandleResourceMode eMode )
200 sal_Int32 nChangedCount = 0;
202 Reference< XPropertySet > xPropertySet;
203 rControlAny >>= xPropertySet;
204 if( xPropertySet.is() && xStringResourceManager.is())
206 Sequence< Locale > aLocaleSeq = xStringResourceManager->getLocales();
207 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
208 if( nLocaleCount == 0 )
209 return 0;
211 Reference< XPropertySetInfo > xPropertySetInfo = xPropertySet->getPropertySetInfo();
212 if( xPropertySetInfo.is() )
214 // get sequence of control properties
215 Sequence< Property > aPropSeq = xPropertySetInfo->getProperties();
216 const Property* pProps = aPropSeq.getConstArray();
217 sal_Int32 nCtrlProps = aPropSeq.getLength();
219 // create a map of tab indices and control names, sorted by tab index
220 for( sal_Int32 j = 0 ; j < nCtrlProps ; ++j )
222 const Property& rProp = pProps[j];
223 OUString aPropName = rProp.Name;
224 TypeClass eType = rProp.Type.getTypeClass();
225 bool bLanguageDependentProperty =
226 (eType == TypeClass_STRING || eType == TypeClass_SEQUENCE)
227 && isLanguageDependentProperty( aPropName );
228 if( !bLanguageDependentProperty )
229 continue;
231 if( eType == TypeClass_STRING )
233 Any aPropAny = xPropertySet->getPropertyValue( aPropName );
234 OUString aPropStr;
235 aPropAny >>= aPropStr;
237 // Replace string by id, add id+string to StringResource
238 if( eMode == SET_IDS )
240 bool bEscAlreadyExisting = aPropStr.startsWith("&");
241 if( bEscAlreadyExisting )
242 continue;
244 OUString aPureIdStr = implCreatePureResourceId
245 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
247 // Set Id for all locales
248 const Locale* pLocales = aLocaleSeq.getConstArray();
249 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
251 const Locale& rLocale = pLocales[ i ];
252 xStringResourceManager->setStringForLocale( aPureIdStr, aPropStr, rLocale );
255 OUString aPropIdStr = aEsc + aPureIdStr;
256 // TODO?: Change here and in toolkit
257 (void)aSemi;
258 xPropertySet->setPropertyValue( aPropName, Any(aPropIdStr) );
260 // Replace id by string from StringResource
261 else if( eMode == RESET_IDS )
263 if( aPropStr.getLength() > 1 )
265 OUString aPureIdStr = aPropStr.copy( 1 );
266 OUString aNewPropStr = aPropStr;
269 aNewPropStr = xStringResourceManager->resolveString( aPureIdStr );
271 catch(const MissingResourceException&)
274 xPropertySet->setPropertyValue( aPropName, Any(aNewPropStr) );
277 // Remove Id for all locales
278 else if( eMode == REMOVE_IDS_FROM_RESOURCE )
280 if( aPropStr.getLength() > 1 )
282 OUString aPureIdStr = aPropStr.copy( 1 );
284 const Locale* pLocales = aLocaleSeq.getConstArray();
285 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
287 const Locale& rLocale = pLocales[ i ];
290 xStringResourceManager->removeIdForLocale( aPureIdStr, rLocale );
292 catch(const MissingResourceException&)
298 // Rename resource id
299 else if( eMode == RENAME_DIALOG_IDS || eMode == RENAME_CONTROL_IDS )
301 OUString aPureSourceIdStr = aPropStr.copy( 1 );
303 OUString aPureIdStr = implCreatePureResourceId
304 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
306 // Set new Id and remove old one for all locales
307 const Locale* pLocales = aLocaleSeq.getConstArray();
308 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
310 const Locale& rLocale = pLocales[ i ];
313 OUString aResStr = xStringResourceManager->resolveStringForLocale
314 ( aPureSourceIdStr, rLocale );
315 xStringResourceManager->removeIdForLocale( aPureSourceIdStr, rLocale );
316 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
318 catch(const MissingResourceException&)
322 OUString aPropIdStr = aEsc + aPureIdStr;
323 // TODO?: Change here and in toolkit
324 (void)aSemi;
325 xPropertySet->setPropertyValue( aPropName, Any(aPropIdStr) );
327 // Replace string by string from source StringResourceResolver
328 else if( eMode == MOVE_RESOURCES && xSourceStringResolver.is() )
330 OUString aPureSourceIdStr = aPropStr.copy( 1 );
332 OUString aPureIdStr = implCreatePureResourceId
333 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
335 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
337 // Set Id for all locales
338 const Locale* pLocales = aLocaleSeq.getConstArray();
339 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
341 const Locale& rLocale = pLocales[ i ];
342 OUString aResStr;
345 aResStr = xSourceStringResolver->resolveStringForLocale
346 ( aPureSourceIdStr, rLocale );
348 catch(const MissingResourceException&)
350 aResStr = xSourceStringResolver->resolveStringForLocale
351 ( aPureSourceIdStr, rDefaultLocale );
353 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
356 OUString aPropIdStr = aEsc + aPureIdStr;
357 // TODO?: Change here and in toolkit
358 (void)aSemi;
359 xPropertySet->setPropertyValue( aPropName, Any(aPropIdStr) );
361 // Copy string from source to target resource
362 else if( eMode == COPY_RESOURCES && xSourceStringResolver.is() )
364 OUString aPureSourceIdStr = aPropStr.copy( 1 );
366 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
368 // Copy Id for all locales
369 const Locale* pLocales = aLocaleSeq.getConstArray();
370 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
372 const Locale& rLocale = pLocales[ i ];
373 OUString aResStr;
376 aResStr = xSourceStringResolver->resolveStringForLocale
377 ( aPureSourceIdStr, rLocale );
379 catch(const MissingResourceException&)
381 aResStr = xSourceStringResolver->resolveStringForLocale
382 ( aPureSourceIdStr, rDefaultLocale );
384 xStringResourceManager->setStringForLocale( aPureSourceIdStr, aResStr, rLocale );
387 nChangedCount++;
390 // Listbox / Combobox
391 else if( eType == TypeClass_SEQUENCE )
393 Any aPropAny = xPropertySet->getPropertyValue( aPropName );
394 Sequence< OUString > aPropStrings;
395 aPropAny >>= aPropStrings;
397 const OUString* pPropStrings = aPropStrings.getConstArray();
398 sal_Int32 nPropStringCount = aPropStrings.getLength();
399 if( nPropStringCount == 0 )
400 continue;
402 // Replace string by id, add id+string to StringResource
403 if( eMode == SET_IDS )
405 Sequence< OUString > aIdStrings;
406 aIdStrings.realloc( nPropStringCount );
407 OUString* pIdStrings = aIdStrings.getArray();
409 OUString aIdStrBase = aDot
410 + aCtrlName
411 + aDot
412 + aPropName;
414 const Locale* pLocales = aLocaleSeq.getConstArray();
415 sal_Int32 i;
416 for ( i = 0; i < nPropStringCount; ++i )
418 OUString aPropStr = pPropStrings[i];
419 bool bEscAlreadyExisting = aPropStr.startsWith("&");
420 if( bEscAlreadyExisting )
422 pIdStrings[i] = aPropStr;
423 continue;
426 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
427 OUString aPureIdStr = OUString::number( nUniqueId )
428 + aIdStrBase;
430 // Set Id for all locales
431 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
433 const Locale& rLocale = pLocales[ iLocale ];
434 xStringResourceManager->setStringForLocale( aPureIdStr, aPropStr, rLocale );
437 pIdStrings[i] = aEsc + aPureIdStr;
439 xPropertySet->setPropertyValue( aPropName, Any(aIdStrings) );
441 // Replace id by string from StringResource
442 else if( eMode == RESET_IDS )
444 Sequence< OUString > aNewPropStrings;
445 aNewPropStrings.realloc( nPropStringCount );
446 OUString* pNewPropStrings = aNewPropStrings.getArray();
448 sal_Int32 i;
449 for ( i = 0; i < nPropStringCount; ++i )
451 OUString aIdStr = pPropStrings[i];
452 OUString aNewPropStr = aIdStr;
453 if( aIdStr.getLength() > 1 )
455 OUString aPureIdStr = aIdStr.copy( 1 );
458 aNewPropStr = xStringResourceManager->resolveString( aPureIdStr );
460 catch(const MissingResourceException&)
464 pNewPropStrings[i] = aNewPropStr;
466 xPropertySet->setPropertyValue( aPropName, Any(aNewPropStrings) );
468 // Remove Id for all locales
469 else if( eMode == REMOVE_IDS_FROM_RESOURCE )
471 const Locale* pLocales = aLocaleSeq.getConstArray();
472 sal_Int32 i;
473 for ( i = 0; i < nPropStringCount; ++i )
475 OUString aIdStr = pPropStrings[i];
476 if( aIdStr.getLength() > 1 )
478 OUString aPureIdStr = aIdStr.copy( 1 );
480 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
482 const Locale& rLocale = pLocales[iLocale];
485 xStringResourceManager->removeIdForLocale( aPureIdStr, rLocale );
487 catch(const MissingResourceException&)
494 // Rename resource id
495 else if( eMode == RENAME_CONTROL_IDS )
497 Sequence< OUString > aIdStrings;
498 aIdStrings.realloc( nPropStringCount );
499 OUString* pIdStrings = aIdStrings.getArray();
501 OUString aIdStrBase = aDot
502 + aCtrlName
503 + aDot
504 + aPropName;
506 const Locale* pLocales = aLocaleSeq.getConstArray();
507 sal_Int32 i;
508 for ( i = 0; i < nPropStringCount; ++i )
510 OUString aSourceIdStr = pPropStrings[i];
511 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
513 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
514 OUString aPureIdStr = OUString::number( nUniqueId )
515 + aIdStrBase;
517 // Set Id for all locales
518 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
520 const Locale& rLocale = pLocales[ iLocale ];
524 OUString aResStr = xStringResourceManager->resolveStringForLocale
525 ( aPureSourceIdStr, rLocale );
526 xStringResourceManager->removeIdForLocale( aPureSourceIdStr, rLocale );
527 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
529 catch(const MissingResourceException&)
533 pIdStrings[i] = aEsc + aPureIdStr;
535 xPropertySet->setPropertyValue( aPropName, Any(aIdStrings) );
537 // Replace string by string from source StringResourceResolver
538 else if( eMode == MOVE_RESOURCES && xSourceStringResolver.is() )
540 Sequence< OUString > aIdStrings;
541 aIdStrings.realloc( nPropStringCount );
542 OUString* pIdStrings = aIdStrings.getArray();
544 OUString aIdStrBase = aDot
545 + aCtrlName
546 + aDot
547 + aPropName;
549 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
551 const Locale* pLocales = aLocaleSeq.getConstArray();
552 sal_Int32 i;
553 for ( i = 0; i < nPropStringCount; ++i )
555 OUString aSourceIdStr = pPropStrings[i];
556 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
558 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
559 OUString aPureIdStr = OUString::number( nUniqueId )
560 + aIdStrBase;
562 // Set Id for all locales
563 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
565 const Locale& rLocale = pLocales[ iLocale ];
567 OUString aResStr;
570 aResStr = xSourceStringResolver->resolveStringForLocale
571 ( aPureSourceIdStr, rLocale );
573 catch(const MissingResourceException&)
575 aResStr = xSourceStringResolver->resolveStringForLocale
576 ( aPureSourceIdStr, rDefaultLocale );
578 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
581 pIdStrings[i] = aEsc + aPureIdStr;
583 xPropertySet->setPropertyValue( aPropName, Any(aIdStrings) );
585 // Copy string from source to target resource
586 else if( eMode == COPY_RESOURCES && xSourceStringResolver.is() )
588 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
590 const Locale* pLocales = aLocaleSeq.getConstArray();
591 sal_Int32 i;
592 for ( i = 0; i < nPropStringCount; ++i )
594 OUString aSourceIdStr = pPropStrings[i];
595 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
597 // Set Id for all locales
598 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
600 const Locale& rLocale = pLocales[ iLocale ];
602 OUString aResStr;
605 aResStr = xSourceStringResolver->resolveStringForLocale
606 ( aPureSourceIdStr, rLocale );
608 catch(const MissingResourceException&)
610 aResStr = xSourceStringResolver->resolveStringForLocale
611 ( aPureSourceIdStr, rDefaultLocale );
613 xStringResourceManager->setStringForLocale( aPureSourceIdStr, aResStr, rLocale );
617 nChangedCount++;
622 return nChangedCount;
626 void LocalizationMgr::handleAddLocales( const Sequence< Locale >& aLocaleSeq )
628 const Locale* pLocales = aLocaleSeq.getConstArray();
629 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
631 if( isLibraryLocalized() )
633 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
635 const Locale& rLocale = pLocales[ i ];
636 m_xStringResourceManager->newLocale( rLocale );
639 else
641 DBG_ASSERT( nLocaleCount==1, "LocalizationMgr::handleAddLocales(): Only one first locale allowed" );
643 const Locale& rLocale = pLocales[ 0 ];
644 m_xStringResourceManager->newLocale( rLocale );
645 enableResourceForAllLibraryDialogs();
648 MarkDocumentModified( m_aDocument );
650 // update locale toolbar
651 if (SfxBindings* pBindings = GetBindingsPtr())
652 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
654 handleTranslationbar();
658 void LocalizationMgr::handleRemoveLocales( const Sequence< Locale >& aLocaleSeq )
660 const Locale* pLocales = aLocaleSeq.getConstArray();
661 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
662 bool bConsistent = true;
663 bool bModified = false;
665 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
667 const Locale& rLocale = pLocales[ i ];
668 bool bRemove = true;
670 // Check if last locale
671 Sequence< Locale > aResLocaleSeq = m_xStringResourceManager->getLocales();
672 if( aResLocaleSeq.getLength() == 1 )
674 const Locale& rLastResLocale = aResLocaleSeq.getConstArray()[ 0 ];
675 if( localesAreEqual( rLocale, rLastResLocale ) )
677 disableResourceForAllLibraryDialogs();
679 else
681 // Inconsistency, keep last locale
682 bConsistent = false;
683 bRemove = false;
687 if( bRemove )
691 m_xStringResourceManager->removeLocale( rLocale );
692 bModified = true;
694 catch(const IllegalArgumentException&)
696 bConsistent = false;
700 if( bModified )
702 MarkDocumentModified( m_aDocument );
704 // update slots
705 if (SfxBindings* pBindings = GetBindingsPtr())
707 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
708 pBindings->Invalidate( SID_BASICIDE_MANAGE_LANG );
711 handleTranslationbar();
714 DBG_ASSERT( bConsistent,
715 "LocalizationMgr::handleRemoveLocales(): sequence contains unsupported locales" );
718 void LocalizationMgr::handleSetDefaultLocale(const Locale& rLocale)
720 if( !m_xStringResourceManager.is() )
721 return;
725 m_xStringResourceManager->setDefaultLocale(rLocale);
727 catch(const IllegalArgumentException&)
729 OSL_FAIL( "LocalizationMgr::handleSetDefaultLocale: Invalid locale" );
732 // update locale toolbar
733 if (SfxBindings* pBindings = GetBindingsPtr())
734 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
737 void LocalizationMgr::handleSetCurrentLocale(const css::lang::Locale& rLocale)
739 if( !m_xStringResourceManager.is() )
740 return;
744 m_xStringResourceManager->setCurrentLocale(rLocale, false);
746 catch(const IllegalArgumentException&)
748 OSL_FAIL( "LocalizationMgr::handleSetCurrentLocale: Invalid locale" );
751 // update locale toolbar
752 if (SfxBindings* pBindings = GetBindingsPtr())
753 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
755 if (DialogWindow* pDlgWin = dynamic_cast<DialogWindow*>(m_pShell->GetCurWindow()))
756 if (!pDlgWin->IsSuspended())
757 pDlgWin->GetEditor().UpdatePropertyBrowserDelayed();
760 void LocalizationMgr::handleBasicStarted()
762 if( m_xStringResourceManager.is() )
763 m_aLocaleBeforeBasicStart = m_xStringResourceManager->getCurrentLocale();
766 void LocalizationMgr::handleBasicStopped()
770 if( m_xStringResourceManager.is() )
771 m_xStringResourceManager->setCurrentLocale( m_aLocaleBeforeBasicStart, true );
773 catch(const IllegalArgumentException&)
779 static DialogWindow* FindDialogWindowForEditor( DlgEditor const * pEditor )
781 Shell::WindowTable const& aWindowTable = GetShell()->GetWindowTable();
782 for (auto const& window : aWindowTable)
784 BaseWindow* pWin = window.second;
785 if (!pWin->IsSuspended())
786 if (DialogWindow* pDlgWin = dynamic_cast<DialogWindow*>(pWin))
788 if (&pDlgWin->GetEditor() == pEditor)
789 return pDlgWin;
792 return nullptr;
796 void LocalizationMgr::setControlResourceIDsForNewEditorObject( DlgEditor const * pEditor,
797 const Any& rControlAny, std::u16string_view aCtrlName )
799 // Get library for DlgEditor
800 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
801 if( !pDlgWin )
802 return;
803 ScriptDocument aDocument( pDlgWin->GetDocument() );
804 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::setControlResourceIDsForNewEditorObject: invalid document!" );
805 if ( !aDocument.isValid() )
806 return;
807 const OUString& rLibName = pDlgWin->GetLibName();
808 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
809 Reference< XStringResourceManager > xStringResourceManager =
810 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
812 // Set resource property
813 if( !xStringResourceManager.is() || !xStringResourceManager->getLocales().hasElements() )
814 return;
816 OUString aDialogName = pDlgWin->GetName();
817 Reference< XStringResourceResolver > xDummyStringResolver;
818 sal_Int32 nChangedCount = implHandleControlResourceProperties
819 ( rControlAny, aDialogName, aCtrlName, xStringResourceManager,
820 xDummyStringResolver, SET_IDS );
822 if( nChangedCount )
823 MarkDocumentModified( aDocument );
826 void LocalizationMgr::renameControlResourceIDsForEditorObject( DlgEditor const * pEditor,
827 const css::uno::Any& rControlAny, std::u16string_view aNewCtrlName )
829 // Get library for DlgEditor
830 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
831 if( !pDlgWin )
832 return;
833 ScriptDocument aDocument( pDlgWin->GetDocument() );
834 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::renameControlResourceIDsForEditorObject: invalid document!" );
835 if ( !aDocument.isValid() )
836 return;
837 const OUString& rLibName = pDlgWin->GetLibName();
838 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
839 Reference< XStringResourceManager > xStringResourceManager =
840 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
842 // Set resource property
843 if( !xStringResourceManager.is() || !xStringResourceManager->getLocales().hasElements() )
844 return;
846 OUString aDialogName = pDlgWin->GetName();
847 Reference< XStringResourceResolver > xDummyStringResolver;
848 implHandleControlResourceProperties
849 ( rControlAny, aDialogName, aNewCtrlName, xStringResourceManager,
850 xDummyStringResolver, RENAME_CONTROL_IDS );
854 void LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( DlgEditor const * pEditor,
855 const Any& rControlAny, std::u16string_view aCtrlName )
857 // Get library for DlgEditor
858 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
859 if( !pDlgWin )
860 return;
861 ScriptDocument aDocument( pDlgWin->GetDocument() );
862 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject: invalid document!" );
863 if ( !aDocument.isValid() )
864 return;
865 const OUString& rLibName = pDlgWin->GetLibName();
866 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
867 Reference< XStringResourceManager > xStringResourceManager =
868 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
870 OUString aDialogName = pDlgWin->GetName();
871 Reference< XStringResourceResolver > xDummyStringResolver;
872 sal_Int32 nChangedCount = implHandleControlResourceProperties
873 ( rControlAny, aDialogName, aCtrlName, xStringResourceManager,
874 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
876 if( nChangedCount )
877 MarkDocumentModified( aDocument );
880 void LocalizationMgr::setStringResourceAtDialog( const ScriptDocument& rDocument, const OUString& aLibName,
881 std::u16string_view aDlgName, const Reference< container::XNameContainer >& xDialogModel )
883 // Get library
884 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, true ) );
885 Reference< XStringResourceManager > xStringResourceManager =
886 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
888 // Set resource property
889 if( !xStringResourceManager.is() )
890 return;
892 // Not very elegant as dialog may or may not be localized yet
893 // TODO: Find better place, where dialog is created
894 if( xStringResourceManager->getLocales().hasElements() )
896 Any aDialogCtrl;
897 aDialogCtrl <<= xDialogModel;
898 Reference< XStringResourceResolver > xDummyStringResolver;
899 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
900 std::u16string_view(), xStringResourceManager,
901 xDummyStringResolver, SET_IDS );
904 Reference< beans::XPropertySet > xDlgPSet( xDialogModel, UNO_QUERY );
905 xDlgPSet->setPropertyValue( "ResourceResolver", Any(xStringResourceManager) );
908 void LocalizationMgr::renameStringResourceIDs( const ScriptDocument& rDocument, const OUString& aLibName,
909 std::u16string_view aDlgName, const Reference< container::XNameContainer >& xDialogModel )
911 // Get library
912 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, true ) );
913 Reference< XStringResourceManager > xStringResourceManager =
914 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
915 if( !xStringResourceManager.is() )
916 return;
918 Any aDialogCtrl;
919 aDialogCtrl <<= xDialogModel;
920 Reference< XStringResourceResolver > xDummyStringResolver;
921 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
922 std::u16string_view(), xStringResourceManager,
923 xDummyStringResolver, RENAME_DIALOG_IDS );
925 // Handle all controls
926 for(const auto& rCtrlName : xDialogModel->getElementNames()) {
927 Any aCtrl = xDialogModel->getByName( rCtrlName );
928 implHandleControlResourceProperties( aCtrl, aDlgName,
929 rCtrlName, xStringResourceManager,
930 xDummyStringResolver, RENAME_DIALOG_IDS );
934 void LocalizationMgr::removeResourceForDialog( const ScriptDocument& rDocument, const OUString& aLibName,
935 std::u16string_view aDlgName, const Reference< container::XNameContainer >& xDialogModel )
937 // Get library
938 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, true ) );
939 Reference< XStringResourceManager > xStringResourceManager =
940 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
941 if( !xStringResourceManager.is() )
942 return;
944 Any aDialogCtrl;
945 aDialogCtrl <<= xDialogModel;
946 Reference< XStringResourceResolver > xDummyStringResolver;
947 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
948 std::u16string_view(), xStringResourceManager,
949 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
951 // Handle all controls
952 for(const auto& rCtrlName : xDialogModel->getElementNames()) {
953 Any aCtrl = xDialogModel->getByName( rCtrlName );
954 implHandleControlResourceProperties( aCtrl, aDlgName,
955 rCtrlName, xStringResourceManager,
956 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
960 void LocalizationMgr::resetResourceForDialog( const Reference< container::XNameContainer >& xDialogModel,
961 const Reference< XStringResourceManager >& xStringResourceManager )
963 if( !xStringResourceManager.is() )
964 return;
966 // Dialog as control
967 std::u16string_view aDummyName;
968 Any aDialogCtrl;
969 aDialogCtrl <<= xDialogModel;
970 Reference< XStringResourceResolver > xDummyStringResolver;
971 implHandleControlResourceProperties( aDialogCtrl, aDummyName,
972 aDummyName, xStringResourceManager, xDummyStringResolver, RESET_IDS );
974 // Handle all controls
975 for(const auto& rCtrlName : xDialogModel->getElementNames()){
976 Any aCtrl = xDialogModel->getByName( rCtrlName );
977 implHandleControlResourceProperties( aCtrl, aDummyName,
978 rCtrlName, xStringResourceManager, xDummyStringResolver, RESET_IDS );
982 void LocalizationMgr::setResourceIDsForDialog( const Reference< container::XNameContainer >& xDialogModel,
983 const Reference< XStringResourceManager >& xStringResourceManager )
985 if( !xStringResourceManager.is() )
986 return;
988 // Dialog as control
989 std::u16string_view aDummyName;
990 Any aDialogCtrl;
991 aDialogCtrl <<= xDialogModel;
992 Reference< XStringResourceResolver > xDummyStringResolver;
993 implHandleControlResourceProperties( aDialogCtrl, aDummyName,
994 aDummyName, xStringResourceManager, xDummyStringResolver, SET_IDS );
996 // Handle all controls
997 for(const auto& rCtrlName : xDialogModel->getElementNames()) {
998 Any aCtrl = xDialogModel->getByName( rCtrlName );
999 implHandleControlResourceProperties( aCtrl, aDummyName,
1000 rCtrlName, xStringResourceManager, xDummyStringResolver, SET_IDS );
1004 void LocalizationMgr::copyResourcesForPastedEditorObject( DlgEditor const * pEditor,
1005 const Any& rControlAny, std::u16string_view aCtrlName,
1006 const Reference< XStringResourceResolver >& xSourceStringResolver )
1008 // Get library for DlgEditor
1009 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
1010 if( !pDlgWin )
1011 return;
1012 ScriptDocument aDocument( pDlgWin->GetDocument() );
1013 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::copyResourcesForPastedEditorObject: invalid document!" );
1014 if ( !aDocument.isValid() )
1015 return;
1016 const OUString& rLibName = pDlgWin->GetLibName();
1017 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
1018 Reference< XStringResourceManager > xStringResourceManager =
1019 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
1021 // Set resource property
1022 if( !xStringResourceManager.is() || !xStringResourceManager->getLocales().hasElements() )
1023 return;
1025 OUString aDialogName = pDlgWin->GetName();
1026 implHandleControlResourceProperties
1027 ( rControlAny, aDialogName, aCtrlName, xStringResourceManager,
1028 xSourceStringResolver, MOVE_RESOURCES );
1031 void LocalizationMgr::copyResourceForDroppedDialog( const Reference< container::XNameContainer >& xDialogModel,
1032 std::u16string_view aDialogName,
1033 const Reference< XStringResourceManager >& xStringResourceManager,
1034 const Reference< XStringResourceResolver >& xSourceStringResolver )
1036 if( !xStringResourceManager.is() )
1037 return;
1039 // Dialog as control
1040 Any aDialogCtrl;
1041 aDialogCtrl <<= xDialogModel;
1042 implHandleControlResourceProperties( aDialogCtrl, aDialogName,
1043 std::u16string_view(), xStringResourceManager, xSourceStringResolver, MOVE_RESOURCES );
1045 // Handle all controls
1046 for(const auto& rCtrlName : xDialogModel->getElementNames()) {
1047 Any aCtrl = xDialogModel->getByName( rCtrlName );
1048 implHandleControlResourceProperties( aCtrl, aDialogName,
1049 rCtrlName, xStringResourceManager, xSourceStringResolver, MOVE_RESOURCES );
1053 void LocalizationMgr::copyResourceForDialog(
1054 const Reference< container::XNameContainer >& xDialogModel,
1055 const Reference< XStringResourceResolver >& xSourceStringResolver,
1056 const Reference< XStringResourceManager >& xTargetStringResourceManager )
1058 if( !xDialogModel.is() || !xSourceStringResolver.is() || !xTargetStringResourceManager.is() )
1059 return;
1061 std::u16string_view aDummyName;
1062 Any aDialogCtrl;
1063 aDialogCtrl <<= xDialogModel;
1064 implHandleControlResourceProperties
1065 ( aDialogCtrl, aDummyName, aDummyName, xTargetStringResourceManager,
1066 xSourceStringResolver, COPY_RESOURCES );
1068 // Handle all controls
1069 for(const auto& rCtrlName : xDialogModel->getElementNames()) {
1070 Any aCtrl = xDialogModel->getByName( rCtrlName );
1071 implHandleControlResourceProperties( aCtrl, aDummyName, aDummyName,
1072 xTargetStringResourceManager, xSourceStringResolver, COPY_RESOURCES );
1076 Reference< XStringResourceManager > LocalizationMgr::getStringResourceFromDialogLibrary
1077 ( const Reference< container::XNameContainer >& xDialogLib )
1079 Reference< XStringResourceManager > xStringResourceManager;
1080 if( xDialogLib.is() )
1082 Reference< resource::XStringResourceSupplier > xStringResourceSupplier( xDialogLib, UNO_QUERY );
1083 if( xStringResourceSupplier.is() )
1085 Reference< resource::XStringResourceResolver >
1086 xStringResourceResolver = xStringResourceSupplier->getStringResource();
1088 xStringResourceManager =
1089 Reference< resource::XStringResourceManager >( xStringResourceResolver, UNO_QUERY );
1092 return xStringResourceManager;
1095 } // namespace basctl
1097 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */