sc: use SAL_RET_MAYBENULL in GetDocument()
[LibreOffice.git] / basctl / source / basicide / localizationmgr.cxx
blobe63d17eba3871ed2bd78eb0b3090785c5c6c9198
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( u"LayoutManager"_ustr );
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 Reference< XStringResourceResolver > xDummyStringResolver;
139 for (auto& aDlgName : m_aDocument.getObjectNames(E_DIALOGS, m_aLibName))
141 if (VclPtr<DialogWindow> pWin = m_pShell->FindDlgWin(m_aDocument, m_aLibName, aDlgName))
143 Reference< container::XNameContainer > xDialog = pWin->GetDialog();
144 if( xDialog.is() )
146 // Handle dialog itself as control
147 Any aDialogCtrl;
148 aDialogCtrl <<= xDialog;
149 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
150 std::u16string_view(), m_xStringResourceManager, xDummyStringResolver, eMode );
152 // Handle all controls
153 for (auto& aCtrlName : xDialog->getElementNames())
155 Any aCtrl = xDialog->getByName( aCtrlName );
156 implHandleControlResourceProperties( aCtrl, aDlgName,
157 aCtrlName, m_xStringResourceManager, xDummyStringResolver, eMode );
165 static OUString implCreatePureResourceId
166 ( std::u16string_view aDialogName, std::u16string_view aCtrlName,
167 std::u16string_view aPropName,
168 const Reference< XStringResourceManager >& xStringResourceManager )
170 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
171 OUString aPureIdStr = OUString::number( nUniqueId )
172 + aDot
173 + aDialogName
174 + aDot;
175 if( !aCtrlName.empty() )
177 aPureIdStr += aCtrlName + aDot;
179 aPureIdStr += aPropName;
180 return aPureIdStr;
183 // Works on xStringResourceManager's current language for SET_IDS/RESET_IDS,
184 // anyway only one language should exist when calling this method then,
185 // either the first one for mode SET_IDS or the last one for mode RESET_IDS
186 sal_Int32 LocalizationMgr::implHandleControlResourceProperties
187 (const Any& rControlAny, std::u16string_view aDialogName, std::u16string_view aCtrlName,
188 const Reference< XStringResourceManager >& xStringResourceManager,
189 const Reference< XStringResourceResolver >& xSourceStringResolver, HandleResourceMode eMode )
191 sal_Int32 nChangedCount = 0;
193 Reference< XPropertySet > xPropertySet;
194 rControlAny >>= xPropertySet;
195 if( xPropertySet.is() && xStringResourceManager.is())
197 Sequence< Locale > aLocaleSeq = xStringResourceManager->getLocales();
198 if (!aLocaleSeq.hasElements())
199 return 0;
201 Reference< XPropertySetInfo > xPropertySetInfo = xPropertySet->getPropertySetInfo();
202 if( xPropertySetInfo.is() )
204 // get sequence of control properties
205 // create a map of tab indices and control names, sorted by tab index
206 for (auto& rProp : xPropertySetInfo->getProperties())
208 OUString aPropName = rProp.Name;
209 TypeClass eType = rProp.Type.getTypeClass();
210 bool bLanguageDependentProperty =
211 (eType == TypeClass_STRING || eType == TypeClass_SEQUENCE)
212 && isLanguageDependentProperty( aPropName );
213 if( !bLanguageDependentProperty )
214 continue;
216 if( eType == TypeClass_STRING )
218 Any aPropAny = xPropertySet->getPropertyValue( aPropName );
219 OUString aPropStr;
220 aPropAny >>= aPropStr;
222 // Replace string by id, add id+string to StringResource
223 if( eMode == SET_IDS )
225 bool bEscAlreadyExisting = aPropStr.startsWith("&");
226 if( bEscAlreadyExisting )
227 continue;
229 OUString aPureIdStr = implCreatePureResourceId
230 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
232 // Set Id for all locales
233 for (auto& rLocale : aLocaleSeq)
235 xStringResourceManager->setStringForLocale( aPureIdStr, aPropStr, rLocale );
238 OUString aPropIdStr = aEsc + aPureIdStr;
239 // TODO?: Change here and in toolkit
240 (void)aSemi;
241 xPropertySet->setPropertyValue( aPropName, Any(aPropIdStr) );
243 // Replace id by string from StringResource
244 else if( eMode == RESET_IDS )
246 if( aPropStr.getLength() > 1 )
248 OUString aPureIdStr = aPropStr.copy( 1 );
249 OUString aNewPropStr = aPropStr;
252 aNewPropStr = xStringResourceManager->resolveString( aPureIdStr );
254 catch(const MissingResourceException&)
257 xPropertySet->setPropertyValue( aPropName, Any(aNewPropStr) );
260 // Remove Id for all locales
261 else if( eMode == REMOVE_IDS_FROM_RESOURCE )
263 if( aPropStr.getLength() > 1 )
265 OUString aPureIdStr = aPropStr.copy( 1 );
267 for (auto& rLocale : aLocaleSeq)
271 xStringResourceManager->removeIdForLocale( aPureIdStr, rLocale );
273 catch(const MissingResourceException&)
279 // Rename resource id
280 else if( eMode == RENAME_DIALOG_IDS || eMode == RENAME_CONTROL_IDS )
282 OUString aPureSourceIdStr = aPropStr.copy( 1 );
284 OUString aPureIdStr = implCreatePureResourceId
285 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
287 // Set new Id and remove old one for all locales
288 for (auto& rLocale : aLocaleSeq)
292 OUString aResStr = xStringResourceManager->resolveStringForLocale
293 ( aPureSourceIdStr, rLocale );
294 xStringResourceManager->removeIdForLocale( aPureSourceIdStr, rLocale );
295 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
297 catch(const MissingResourceException&)
301 OUString aPropIdStr = aEsc + aPureIdStr;
302 // TODO?: Change here and in toolkit
303 (void)aSemi;
304 xPropertySet->setPropertyValue( aPropName, Any(aPropIdStr) );
306 // Replace string by string from source StringResourceResolver
307 else if( eMode == MOVE_RESOURCES && xSourceStringResolver.is() )
309 OUString aPureSourceIdStr = aPropStr.copy( 1 );
311 OUString aPureIdStr = implCreatePureResourceId
312 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
314 const Locale aDefaultLocale = xSourceStringResolver->getDefaultLocale();
316 // Set Id for all locales
317 for (auto& rLocale : aLocaleSeq)
319 OUString aResStr;
322 aResStr = xSourceStringResolver->resolveStringForLocale
323 ( aPureSourceIdStr, rLocale );
325 catch(const MissingResourceException&)
327 aResStr = xSourceStringResolver->resolveStringForLocale
328 ( aPureSourceIdStr, aDefaultLocale );
330 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
333 OUString aPropIdStr = aEsc + aPureIdStr;
334 // TODO?: Change here and in toolkit
335 (void)aSemi;
336 xPropertySet->setPropertyValue( aPropName, Any(aPropIdStr) );
338 // Copy string from source to target resource
339 else if( eMode == COPY_RESOURCES && xSourceStringResolver.is() )
341 OUString aPureSourceIdStr = aPropStr.copy( 1 );
343 const Locale aDefaultLocale = xSourceStringResolver->getDefaultLocale();
345 // Copy Id for all locales
346 for (auto& rLocale : aLocaleSeq)
348 OUString aResStr;
351 aResStr = xSourceStringResolver->resolveStringForLocale
352 ( aPureSourceIdStr, rLocale );
354 catch(const MissingResourceException&)
356 aResStr = xSourceStringResolver->resolveStringForLocale
357 ( aPureSourceIdStr, aDefaultLocale );
359 xStringResourceManager->setStringForLocale( aPureSourceIdStr, aResStr, rLocale );
362 nChangedCount++;
365 // Listbox / Combobox
366 else if( eType == TypeClass_SEQUENCE )
368 Any aPropAny = xPropertySet->getPropertyValue( aPropName );
369 Sequence< OUString > aPropStrings;
370 aPropAny >>= aPropStrings;
372 sal_Int32 nPropStringCount = aPropStrings.getLength();
373 if( nPropStringCount == 0 )
374 continue;
376 // Replace string by id, add id+string to StringResource
377 if( eMode == SET_IDS )
379 Sequence< OUString > aIdStrings(nPropStringCount);
380 OUString* pIdStrings = aIdStrings.getArray();
382 OUString aIdStrBase = aDot
383 + aCtrlName
384 + aDot
385 + aPropName;
387 sal_Int32 i;
388 for ( i = 0; i < nPropStringCount; ++i )
390 const OUString& aPropStr = aPropStrings[i];
391 bool bEscAlreadyExisting = aPropStr.startsWith("&");
392 if( bEscAlreadyExisting )
394 pIdStrings[i] = aPropStr;
395 continue;
398 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
399 OUString aPureIdStr = OUString::number( nUniqueId )
400 + aIdStrBase;
402 // Set Id for all locales
403 for (auto& rLocale : aLocaleSeq)
405 xStringResourceManager->setStringForLocale( aPureIdStr, aPropStr, rLocale );
408 pIdStrings[i] = aEsc + aPureIdStr;
410 xPropertySet->setPropertyValue( aPropName, Any(aIdStrings) );
412 // Replace id by string from StringResource
413 else if( eMode == RESET_IDS )
415 Sequence<OUString> aNewPropStrings(nPropStringCount);
416 OUString* pNewPropStrings = aNewPropStrings.getArray();
418 for (sal_Int32 i = 0; i < nPropStringCount; ++i)
420 const OUString& aIdStr = aPropStrings[i];
421 OUString aNewPropStr = aIdStr;
422 if( aIdStr.getLength() > 1 )
424 OUString aPureIdStr = aIdStr.copy( 1 );
427 aNewPropStr = xStringResourceManager->resolveString( aPureIdStr );
429 catch(const MissingResourceException&)
433 pNewPropStrings[i] = aNewPropStr;
435 xPropertySet->setPropertyValue( aPropName, Any(aNewPropStrings) );
437 // Remove Id for all locales
438 else if( eMode == REMOVE_IDS_FROM_RESOURCE )
440 for (auto& aIdStr : aPropStrings)
442 if( aIdStr.getLength() > 1 )
444 OUString aPureIdStr = aIdStr.copy( 1 );
446 for (auto& rLocale : aLocaleSeq)
450 xStringResourceManager->removeIdForLocale( aPureIdStr, rLocale );
452 catch(const MissingResourceException&)
459 // Rename resource id
460 else if( eMode == RENAME_CONTROL_IDS )
462 Sequence<OUString> aIdStrings(nPropStringCount);
463 OUString* pIdStrings = aIdStrings.getArray();
465 OUString aIdStrBase = aDot
466 + aCtrlName
467 + aDot
468 + aPropName;
470 for (sal_Int32 i = 0; i < nPropStringCount; ++i)
472 OUString aPureSourceIdStr = aPropStrings[i].copy( 1 );
474 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
475 OUString aPureIdStr = OUString::number( nUniqueId )
476 + aIdStrBase;
478 // Set Id for all locales
479 for (auto& rLocale : aLocaleSeq)
483 OUString aResStr = xStringResourceManager->resolveStringForLocale
484 ( aPureSourceIdStr, rLocale );
485 xStringResourceManager->removeIdForLocale( aPureSourceIdStr, rLocale );
486 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
488 catch(const MissingResourceException&)
492 pIdStrings[i] = aEsc + aPureIdStr;
494 xPropertySet->setPropertyValue( aPropName, Any(aIdStrings) );
496 // Replace string by string from source StringResourceResolver
497 else if( eMode == MOVE_RESOURCES && xSourceStringResolver.is() )
499 Sequence<OUString> aIdStrings(nPropStringCount);
500 OUString* pIdStrings = aIdStrings.getArray();
502 OUString aIdStrBase = aDot
503 + aCtrlName
504 + aDot
505 + aPropName;
507 const Locale aDefaultLocale = xSourceStringResolver->getDefaultLocale();
509 for (sal_Int32 i = 0; i < nPropStringCount; ++i)
511 OUString aPureSourceIdStr = aPropStrings[i].copy( 1 );
513 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
514 OUString aPureIdStr = OUString::number( nUniqueId )
515 + aIdStrBase;
517 // Set Id for all locales
518 for (auto& rLocale : aLocaleSeq)
520 OUString aResStr;
523 aResStr = xSourceStringResolver->resolveStringForLocale
524 ( aPureSourceIdStr, rLocale );
526 catch(const MissingResourceException&)
528 aResStr = xSourceStringResolver->resolveStringForLocale
529 ( aPureSourceIdStr, aDefaultLocale );
531 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
534 pIdStrings[i] = aEsc + aPureIdStr;
536 xPropertySet->setPropertyValue( aPropName, Any(aIdStrings) );
538 // Copy string from source to target resource
539 else if( eMode == COPY_RESOURCES && xSourceStringResolver.is() )
541 const Locale aDefaultLocale = xSourceStringResolver->getDefaultLocale();
543 for (auto& aSourceIdStr : aPropStrings)
545 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
547 // Set Id for all locales
548 for (auto& rLocale : aLocaleSeq)
550 OUString aResStr;
553 aResStr = xSourceStringResolver->resolveStringForLocale
554 ( aPureSourceIdStr, rLocale );
556 catch(const MissingResourceException&)
558 aResStr = xSourceStringResolver->resolveStringForLocale
559 ( aPureSourceIdStr, aDefaultLocale );
561 xStringResourceManager->setStringForLocale( aPureSourceIdStr, aResStr, rLocale );
565 nChangedCount++;
570 return nChangedCount;
574 void LocalizationMgr::handleAddLocales( const Sequence< Locale >& aLocaleSeq )
576 if( isLibraryLocalized() )
578 for (auto& rLocale : aLocaleSeq)
580 m_xStringResourceManager->newLocale( rLocale );
583 else
585 DBG_ASSERT( aLocaleSeq.getLength()==1, "LocalizationMgr::handleAddLocales(): Only one first locale allowed" );
587 const Locale& rLocale = aLocaleSeq[0];
588 m_xStringResourceManager->newLocale( rLocale );
589 enableResourceForAllLibraryDialogs();
592 MarkDocumentModified( m_aDocument );
594 // update locale toolbar
595 if (SfxBindings* pBindings = GetBindingsPtr())
596 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
598 handleTranslationbar();
602 void LocalizationMgr::handleRemoveLocales( const Sequence< Locale >& aLocaleSeq )
604 bool bConsistent = true;
605 bool bModified = false;
607 for (auto& rLocale : aLocaleSeq)
609 bool bRemove = true;
611 // Check if last locale
612 Sequence< Locale > aResLocaleSeq = m_xStringResourceManager->getLocales();
613 if( aResLocaleSeq.getLength() == 1 )
615 const Locale& rLastResLocale = aResLocaleSeq[0];
616 if( localesAreEqual( rLocale, rLastResLocale ) )
618 disableResourceForAllLibraryDialogs();
620 else
622 // Inconsistency, keep last locale
623 bConsistent = false;
624 bRemove = false;
628 if( bRemove )
632 m_xStringResourceManager->removeLocale( rLocale );
633 bModified = true;
635 catch(const IllegalArgumentException&)
637 bConsistent = false;
641 if( bModified )
643 MarkDocumentModified( m_aDocument );
645 // update slots
646 if (SfxBindings* pBindings = GetBindingsPtr())
648 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
649 pBindings->Invalidate( SID_BASICIDE_MANAGE_LANG );
652 handleTranslationbar();
655 DBG_ASSERT( bConsistent,
656 "LocalizationMgr::handleRemoveLocales(): sequence contains unsupported locales" );
659 void LocalizationMgr::handleSetDefaultLocale(const Locale& rLocale)
661 if( !m_xStringResourceManager.is() )
662 return;
666 m_xStringResourceManager->setDefaultLocale(rLocale);
668 catch(const IllegalArgumentException&)
670 OSL_FAIL( "LocalizationMgr::handleSetDefaultLocale: Invalid locale" );
673 // update locale toolbar
674 if (SfxBindings* pBindings = GetBindingsPtr())
675 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
678 void LocalizationMgr::handleSetCurrentLocale(const css::lang::Locale& rLocale)
680 if( !m_xStringResourceManager.is() )
681 return;
685 m_xStringResourceManager->setCurrentLocale(rLocale, false);
687 catch(const IllegalArgumentException&)
689 OSL_FAIL( "LocalizationMgr::handleSetCurrentLocale: Invalid locale" );
692 // update locale toolbar
693 if (SfxBindings* pBindings = GetBindingsPtr())
694 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
696 if (DialogWindow* pDlgWin = dynamic_cast<DialogWindow*>(m_pShell->GetCurWindow()))
697 if (!pDlgWin->IsSuspended())
698 pDlgWin->GetEditor().UpdatePropertyBrowserDelayed();
701 void LocalizationMgr::handleBasicStarted()
703 if( m_xStringResourceManager.is() )
704 m_aLocaleBeforeBasicStart = m_xStringResourceManager->getCurrentLocale();
707 void LocalizationMgr::handleBasicStopped()
711 if( m_xStringResourceManager.is() )
712 m_xStringResourceManager->setCurrentLocale( m_aLocaleBeforeBasicStart, true );
714 catch(const IllegalArgumentException&)
720 static DialogWindow* FindDialogWindowForEditor( DlgEditor const * pEditor )
722 Shell::WindowTable const& aWindowTable = GetShell()->GetWindowTable();
723 for (auto const& window : aWindowTable)
725 BaseWindow* pWin = window.second;
726 if (!pWin->IsSuspended())
727 if (DialogWindow* pDlgWin = dynamic_cast<DialogWindow*>(pWin))
729 if (&pDlgWin->GetEditor() == pEditor)
730 return pDlgWin;
733 return nullptr;
737 void LocalizationMgr::setControlResourceIDsForNewEditorObject( DlgEditor const * pEditor,
738 const Any& rControlAny, std::u16string_view aCtrlName )
740 // Get library for DlgEditor
741 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
742 if( !pDlgWin )
743 return;
744 ScriptDocument aDocument( pDlgWin->GetDocument() );
745 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::setControlResourceIDsForNewEditorObject: invalid document!" );
746 if ( !aDocument.isValid() )
747 return;
748 const OUString& rLibName = pDlgWin->GetLibName();
749 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
750 Reference< XStringResourceManager > xStringResourceManager =
751 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
753 // Set resource property
754 if( !xStringResourceManager.is() || !xStringResourceManager->getLocales().hasElements() )
755 return;
757 OUString aDialogName = pDlgWin->GetName();
758 Reference< XStringResourceResolver > xDummyStringResolver;
759 sal_Int32 nChangedCount = implHandleControlResourceProperties
760 ( rControlAny, aDialogName, aCtrlName, xStringResourceManager,
761 xDummyStringResolver, SET_IDS );
763 if( nChangedCount )
764 MarkDocumentModified( aDocument );
767 void LocalizationMgr::renameControlResourceIDsForEditorObject( DlgEditor const * pEditor,
768 const css::uno::Any& rControlAny, std::u16string_view aNewCtrlName )
770 // Get library for DlgEditor
771 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
772 if( !pDlgWin )
773 return;
774 ScriptDocument aDocument( pDlgWin->GetDocument() );
775 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::renameControlResourceIDsForEditorObject: invalid document!" );
776 if ( !aDocument.isValid() )
777 return;
778 const OUString& rLibName = pDlgWin->GetLibName();
779 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
780 Reference< XStringResourceManager > xStringResourceManager =
781 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
783 // Set resource property
784 if( !xStringResourceManager.is() || !xStringResourceManager->getLocales().hasElements() )
785 return;
787 OUString aDialogName = pDlgWin->GetName();
788 Reference< XStringResourceResolver > xDummyStringResolver;
789 implHandleControlResourceProperties
790 ( rControlAny, aDialogName, aNewCtrlName, xStringResourceManager,
791 xDummyStringResolver, RENAME_CONTROL_IDS );
795 void LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( DlgEditor const * pEditor,
796 const Any& rControlAny, std::u16string_view aCtrlName )
798 // Get library for DlgEditor
799 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
800 if( !pDlgWin )
801 return;
802 ScriptDocument aDocument( pDlgWin->GetDocument() );
803 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject: invalid document!" );
804 if ( !aDocument.isValid() )
805 return;
806 const OUString& rLibName = pDlgWin->GetLibName();
807 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
808 Reference< XStringResourceManager > xStringResourceManager =
809 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
811 OUString aDialogName = pDlgWin->GetName();
812 Reference< XStringResourceResolver > xDummyStringResolver;
813 sal_Int32 nChangedCount = implHandleControlResourceProperties
814 ( rControlAny, aDialogName, aCtrlName, xStringResourceManager,
815 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
817 if( nChangedCount )
818 MarkDocumentModified( aDocument );
821 void LocalizationMgr::setStringResourceAtDialog( const ScriptDocument& rDocument, const OUString& aLibName,
822 std::u16string_view aDlgName, const Reference< container::XNameContainer >& xDialogModel )
824 // Get library
825 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, true ) );
826 Reference< XStringResourceManager > xStringResourceManager =
827 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
829 // Set resource property
830 if( !xStringResourceManager.is() )
831 return;
833 // Not very elegant as dialog may or may not be localized yet
834 // TODO: Find better place, where dialog is created
835 if( xStringResourceManager->getLocales().hasElements() )
837 Any aDialogCtrl;
838 aDialogCtrl <<= xDialogModel;
839 Reference< XStringResourceResolver > xDummyStringResolver;
840 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
841 std::u16string_view(), xStringResourceManager,
842 xDummyStringResolver, SET_IDS );
845 Reference< beans::XPropertySet > xDlgPSet( xDialogModel, UNO_QUERY );
846 xDlgPSet->setPropertyValue( u"ResourceResolver"_ustr, Any(xStringResourceManager) );
849 void LocalizationMgr::renameStringResourceIDs( const ScriptDocument& rDocument, const OUString& aLibName,
850 std::u16string_view aDlgName, const Reference< container::XNameContainer >& xDialogModel )
852 // Get library
853 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, true ) );
854 Reference< XStringResourceManager > xStringResourceManager =
855 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
856 if( !xStringResourceManager.is() )
857 return;
859 Any aDialogCtrl;
860 aDialogCtrl <<= xDialogModel;
861 Reference< XStringResourceResolver > xDummyStringResolver;
862 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
863 std::u16string_view(), xStringResourceManager,
864 xDummyStringResolver, RENAME_DIALOG_IDS );
866 // Handle all controls
867 for(const auto& rCtrlName : xDialogModel->getElementNames()) {
868 Any aCtrl = xDialogModel->getByName( rCtrlName );
869 implHandleControlResourceProperties( aCtrl, aDlgName,
870 rCtrlName, xStringResourceManager,
871 xDummyStringResolver, RENAME_DIALOG_IDS );
875 void LocalizationMgr::removeResourceForDialog( const ScriptDocument& rDocument, const OUString& aLibName,
876 std::u16string_view aDlgName, const Reference< container::XNameContainer >& xDialogModel )
878 // Get library
879 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, true ) );
880 Reference< XStringResourceManager > xStringResourceManager =
881 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
882 if( !xStringResourceManager.is() )
883 return;
885 Any aDialogCtrl;
886 aDialogCtrl <<= xDialogModel;
887 Reference< XStringResourceResolver > xDummyStringResolver;
888 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
889 std::u16string_view(), xStringResourceManager,
890 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
892 // Handle all controls
893 for(const auto& rCtrlName : xDialogModel->getElementNames()) {
894 Any aCtrl = xDialogModel->getByName( rCtrlName );
895 implHandleControlResourceProperties( aCtrl, aDlgName,
896 rCtrlName, xStringResourceManager,
897 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
901 void LocalizationMgr::resetResourceForDialog( const Reference< container::XNameContainer >& xDialogModel,
902 const Reference< XStringResourceManager >& xStringResourceManager )
904 if( !xStringResourceManager.is() )
905 return;
907 // Dialog as control
908 std::u16string_view aDummyName;
909 Any aDialogCtrl;
910 aDialogCtrl <<= xDialogModel;
911 Reference< XStringResourceResolver > xDummyStringResolver;
912 implHandleControlResourceProperties( aDialogCtrl, aDummyName,
913 aDummyName, xStringResourceManager, xDummyStringResolver, RESET_IDS );
915 // Handle all controls
916 for(const auto& rCtrlName : xDialogModel->getElementNames()){
917 Any aCtrl = xDialogModel->getByName( rCtrlName );
918 implHandleControlResourceProperties( aCtrl, aDummyName,
919 rCtrlName, xStringResourceManager, xDummyStringResolver, RESET_IDS );
923 void LocalizationMgr::setResourceIDsForDialog( const Reference< container::XNameContainer >& xDialogModel,
924 const Reference< XStringResourceManager >& xStringResourceManager )
926 if( !xStringResourceManager.is() )
927 return;
929 // Dialog as control
930 std::u16string_view aDummyName;
931 Any aDialogCtrl;
932 aDialogCtrl <<= xDialogModel;
933 Reference< XStringResourceResolver > xDummyStringResolver;
934 implHandleControlResourceProperties( aDialogCtrl, aDummyName,
935 aDummyName, xStringResourceManager, xDummyStringResolver, SET_IDS );
937 // Handle all controls
938 for(const auto& rCtrlName : xDialogModel->getElementNames()) {
939 Any aCtrl = xDialogModel->getByName( rCtrlName );
940 implHandleControlResourceProperties( aCtrl, aDummyName,
941 rCtrlName, xStringResourceManager, xDummyStringResolver, SET_IDS );
945 void LocalizationMgr::copyResourcesForPastedEditorObject( DlgEditor const * pEditor,
946 const Any& rControlAny, std::u16string_view aCtrlName,
947 const Reference< XStringResourceResolver >& xSourceStringResolver )
949 // Get library for DlgEditor
950 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
951 if( !pDlgWin )
952 return;
953 ScriptDocument aDocument( pDlgWin->GetDocument() );
954 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::copyResourcesForPastedEditorObject: invalid document!" );
955 if ( !aDocument.isValid() )
956 return;
957 const OUString& rLibName = pDlgWin->GetLibName();
958 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
959 Reference< XStringResourceManager > xStringResourceManager =
960 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
962 // Set resource property
963 if( !xStringResourceManager.is() || !xStringResourceManager->getLocales().hasElements() )
964 return;
966 OUString aDialogName = pDlgWin->GetName();
967 implHandleControlResourceProperties
968 ( rControlAny, aDialogName, aCtrlName, xStringResourceManager,
969 xSourceStringResolver, MOVE_RESOURCES );
972 void LocalizationMgr::copyResourceForDroppedDialog( const Reference< container::XNameContainer >& xDialogModel,
973 std::u16string_view aDialogName,
974 const Reference< XStringResourceManager >& xStringResourceManager,
975 const Reference< XStringResourceResolver >& xSourceStringResolver )
977 if( !xStringResourceManager.is() )
978 return;
980 // Dialog as control
981 Any aDialogCtrl;
982 aDialogCtrl <<= xDialogModel;
983 implHandleControlResourceProperties( aDialogCtrl, aDialogName,
984 std::u16string_view(), xStringResourceManager, xSourceStringResolver, MOVE_RESOURCES );
986 // Handle all controls
987 for(const auto& rCtrlName : xDialogModel->getElementNames()) {
988 Any aCtrl = xDialogModel->getByName( rCtrlName );
989 implHandleControlResourceProperties( aCtrl, aDialogName,
990 rCtrlName, xStringResourceManager, xSourceStringResolver, MOVE_RESOURCES );
994 void LocalizationMgr::copyResourceForDialog(
995 const Reference< container::XNameContainer >& xDialogModel,
996 const Reference< XStringResourceResolver >& xSourceStringResolver,
997 const Reference< XStringResourceManager >& xTargetStringResourceManager )
999 if( !xDialogModel.is() || !xSourceStringResolver.is() || !xTargetStringResourceManager.is() )
1000 return;
1002 std::u16string_view aDummyName;
1003 Any aDialogCtrl;
1004 aDialogCtrl <<= xDialogModel;
1005 implHandleControlResourceProperties
1006 ( aDialogCtrl, aDummyName, aDummyName, xTargetStringResourceManager,
1007 xSourceStringResolver, COPY_RESOURCES );
1009 // Handle all controls
1010 for(const auto& rCtrlName : xDialogModel->getElementNames()) {
1011 Any aCtrl = xDialogModel->getByName( rCtrlName );
1012 implHandleControlResourceProperties( aCtrl, aDummyName, aDummyName,
1013 xTargetStringResourceManager, xSourceStringResolver, COPY_RESOURCES );
1017 Reference< XStringResourceManager > LocalizationMgr::getStringResourceFromDialogLibrary
1018 ( const Reference< container::XNameContainer >& xDialogLib )
1020 Reference< XStringResourceManager > xStringResourceManager;
1021 if( xDialogLib.is() )
1023 Reference< resource::XStringResourceSupplier > xStringResourceSupplier( xDialogLib, UNO_QUERY );
1024 if( xStringResourceSupplier.is() )
1026 Reference< resource::XStringResourceResolver >
1027 xStringResourceResolver = xStringResourceSupplier->getStringResource();
1029 xStringResourceManager =
1030 Reference< resource::XStringResourceManager >( xStringResourceResolver, UNO_QUERY );
1033 return xStringResourceManager;
1036 } // namespace basctl
1038 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */