Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / basctl / source / basicide / localizationmgr.cxx
blob4d248c16dc9dd2972b1f1c8e19426ed970ddf9d4
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 <localizationmgr.hxx>
22 #include <basidesh.hxx>
23 #include <baside3.hxx>
24 #include <iderdll.hxx>
25 #include <dlged.hxx>
26 #include <managelang.hxx>
28 #include <com/sun/star/frame/XLayoutManager.hpp>
29 #include <com/sun/star/resource/MissingResourceException.hpp>
30 #include <com/sun/star/resource/XStringResourceSupplier.hpp>
31 #include <sfx2/dispatch.hxx>
33 namespace basctl
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::resource;
42 namespace
45 OUString const aDot(".");
46 OUString const aEsc("&");
47 OUString const aSemi(";");
49 } // namespace
51 LocalizationMgr::LocalizationMgr(
52 Shell* pShell,
53 ScriptDocument const& rDocument,
54 OUString const& aLibName,
55 Reference<XStringResourceManager> const& xStringResourceManager
56 ) :
57 m_xStringResourceManager(xStringResourceManager),
58 m_pShell(pShell),
59 m_aDocument(rDocument),
60 m_aLibName(aLibName)
61 { }
63 bool LocalizationMgr::isLibraryLocalized ()
65 if (m_xStringResourceManager.is())
66 return m_xStringResourceManager->getLocales().getLength() > 0;
67 return false;
70 void LocalizationMgr::handleTranslationbar ()
72 static const char aToolBarResName[] = "private:resource/toolbar/translationbar";
74 Reference< beans::XPropertySet > xFrameProps
75 ( m_pShell->GetViewFrame()->GetFrame().GetFrameInterface(), uno::UNO_QUERY );
76 if ( xFrameProps.is() )
78 Reference< css::frame::XLayoutManager > xLayoutManager;
79 uno::Any a = xFrameProps->getPropertyValue( "LayoutManager" );
80 a >>= xLayoutManager;
81 if ( xLayoutManager.is() )
83 if ( !isLibraryLocalized() )
85 xLayoutManager->destroyElement( aToolBarResName );
87 else
89 xLayoutManager->createElement( aToolBarResName );
90 xLayoutManager->requestElement( aToolBarResName );
97 // TODO: -> export from toolkit
100 bool isLanguageDependentProperty( const OUString& aName )
102 static struct Prop
104 const char* sName;
105 sal_Int32 nNameLength;
107 const vProp[] =
109 { "Text", 4 },
110 { "Label", 5 },
111 { "Title", 5 },
112 { "HelpText", 8 },
113 { "CurrencySymbol", 14 },
114 { "StringItemList", 14 },
115 { nullptr, 0 }
118 for (Prop const* pProp = vProp; pProp->sName; ++pProp)
119 if (aName.equalsAsciiL(pProp->sName, pProp->nNameLength))
120 return true;
121 return false;
125 void LocalizationMgr::implEnableDisableResourceForAllLibraryDialogs( HandleResourceMode eMode )
127 Sequence< OUString > aDlgNames = m_aDocument.getObjectNames( E_DIALOGS, m_aLibName );
128 sal_Int32 nDlgCount = aDlgNames.getLength();
129 const OUString* pDlgNames = aDlgNames.getConstArray();
131 Reference< XStringResourceResolver > xDummyStringResolver;
132 for( sal_Int32 i = 0 ; i < nDlgCount ; i++ )
134 OUString aDlgName = pDlgNames[ i ];
135 if (VclPtr<DialogWindow> pWin = m_pShell->FindDlgWin(m_aDocument, m_aLibName, aDlgName))
137 Reference< container::XNameContainer > xDialog = pWin->GetDialog();
138 if( xDialog.is() )
140 // Handle dialog itself as control
141 Any aDialogCtrl;
142 aDialogCtrl <<= xDialog;
143 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
144 OUString(), m_xStringResourceManager, xDummyStringResolver, eMode );
146 // Handle all controls
147 Sequence< OUString > aNames = xDialog->getElementNames();
148 const OUString* pNames = aNames.getConstArray();
149 sal_Int32 nCtrls = aNames.getLength();
150 for( sal_Int32 j = 0 ; j < nCtrls ; ++j )
152 OUString aCtrlName( pNames[j] );
153 Any aCtrl = xDialog->getByName( aCtrlName );
154 implHandleControlResourceProperties( aCtrl, aDlgName,
155 aCtrlName, m_xStringResourceManager, xDummyStringResolver, eMode );
163 OUString implCreatePureResourceId
164 ( const OUString& aDialogName, const OUString& aCtrlName,
165 const OUString& aPropName,
166 const Reference< XStringResourceManager >& xStringResourceManager )
168 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
169 OUString aPureIdStr = OUString::number( nUniqueId )
170 + aDot
171 + aDialogName
172 + aDot;
173 if( !aCtrlName.isEmpty() )
175 aPureIdStr += aCtrlName;
176 aPureIdStr += aDot;
178 aPureIdStr += aPropName;
179 return aPureIdStr;
182 // Works on xStringResourceManager's current language for SET_IDS/RESET_IDS,
183 // anyway only one language should exist when calling this method then,
184 // either the first one for mode SET_IDS or the last one for mode RESET_IDS
185 sal_Int32 LocalizationMgr::implHandleControlResourceProperties
186 (const Any& rControlAny, const OUString& aDialogName, const OUString& aCtrlName,
187 const Reference< XStringResourceManager >& xStringResourceManager,
188 const Reference< XStringResourceResolver >& xSourceStringResolver, HandleResourceMode eMode )
190 sal_Int32 nChangedCount = 0;
192 Reference< XPropertySet > xPropertySet;
193 rControlAny >>= xPropertySet;
194 if( xPropertySet.is() && xStringResourceManager.is())
196 Sequence< Locale > aLocaleSeq = xStringResourceManager->getLocales();
197 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
198 if( nLocaleCount == 0 )
199 return 0;
201 Reference< XPropertySetInfo > xPropertySetInfo = xPropertySet->getPropertySetInfo();
202 if( xPropertySetInfo.is() )
204 // get sequence of control properties
205 Sequence< Property > aPropSeq = xPropertySetInfo->getProperties();
206 const Property* pProps = aPropSeq.getConstArray();
207 sal_Int32 nCtrlProps = aPropSeq.getLength();
209 // create a map of tab indices and control names, sorted by tab index
210 for( sal_Int32 j = 0 ; j < nCtrlProps ; ++j )
212 const Property& rProp = pProps[j];
213 OUString aPropName = rProp.Name;
214 TypeClass eType = rProp.Type.getTypeClass();
215 bool bLanguageDependentProperty =
216 (eType == TypeClass_STRING || eType == TypeClass_SEQUENCE)
217 && isLanguageDependentProperty( aPropName );
218 if( !bLanguageDependentProperty )
219 continue;
221 if( eType == TypeClass_STRING )
223 Any aPropAny = xPropertySet->getPropertyValue( aPropName );
224 OUString aPropStr;
225 aPropAny >>= aPropStr;
227 // Replace string by id, add id+string to StringResource
228 if( eMode == SET_IDS )
230 bool bEscAlreadyExisting = aPropStr.startsWith("&");
231 if( bEscAlreadyExisting )
232 continue;
234 OUString aPureIdStr = implCreatePureResourceId
235 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
237 // Set Id for all locales
238 const Locale* pLocales = aLocaleSeq.getConstArray();
239 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
241 const Locale& rLocale = pLocales[ i ];
242 xStringResourceManager->setStringForLocale( aPureIdStr, aPropStr, rLocale );
245 OUString aPropIdStr = aEsc + aPureIdStr;
246 // TODO?: Change here and in toolkit
247 (void)aSemi;
248 xPropertySet->setPropertyValue( aPropName, Any(aPropIdStr) );
250 // Replace id by string from StringResource
251 else if( eMode == RESET_IDS )
253 if( aPropStr.getLength() > 1 )
255 OUString aPureIdStr = aPropStr.copy( 1 );
256 OUString aNewPropStr = aPropStr;
259 aNewPropStr = xStringResourceManager->resolveString( aPureIdStr );
261 catch(const MissingResourceException&)
264 xPropertySet->setPropertyValue( aPropName, Any(aNewPropStr) );
267 // Remove Id for all locales
268 else if( eMode == REMOVE_IDS_FROM_RESOURCE )
270 if( aPropStr.getLength() > 1 )
272 OUString aPureIdStr = aPropStr.copy( 1 );
274 const Locale* pLocales = aLocaleSeq.getConstArray();
275 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
277 const Locale& rLocale = pLocales[ i ];
280 xStringResourceManager->removeIdForLocale( aPureIdStr, rLocale );
282 catch(const MissingResourceException&)
288 // Rename resource id
289 else if( eMode == RENAME_DIALOG_IDS || eMode == RENAME_CONTROL_IDS )
291 OUString aPureSourceIdStr = aPropStr.copy( 1 );
293 OUString aPureIdStr = implCreatePureResourceId
294 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
296 // Set new Id and remove old one for all locales
297 const Locale* pLocales = aLocaleSeq.getConstArray();
298 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
300 const Locale& rLocale = pLocales[ i ];
301 OUString aResStr;
304 aResStr = xStringResourceManager->resolveStringForLocale
305 ( aPureSourceIdStr, rLocale );
306 xStringResourceManager->removeIdForLocale( aPureSourceIdStr, rLocale );
307 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
309 catch(const MissingResourceException&)
313 OUString aPropIdStr = aEsc + aPureIdStr;
314 // TODO?: Change here and in toolkit
315 (void)aSemi;
316 xPropertySet->setPropertyValue( aPropName, Any(aPropIdStr) );
318 // Replace string by string from source StringResourceResolver
319 else if( eMode == MOVE_RESOURCES && xSourceStringResolver.is() )
321 OUString aPureSourceIdStr = aPropStr.copy( 1 );
323 OUString aPureIdStr = implCreatePureResourceId
324 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
326 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
328 // Set Id for all locales
329 const Locale* pLocales = aLocaleSeq.getConstArray();
330 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
332 const Locale& rLocale = pLocales[ i ];
333 OUString aResStr;
336 aResStr = xSourceStringResolver->resolveStringForLocale
337 ( aPureSourceIdStr, rLocale );
339 catch(const MissingResourceException&)
341 aResStr = xSourceStringResolver->resolveStringForLocale
342 ( aPureSourceIdStr, rDefaultLocale );
344 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
347 OUString aPropIdStr = aEsc + aPureIdStr;
348 // TODO?: Change here and in toolkit
349 (void)aSemi;
350 xPropertySet->setPropertyValue( aPropName, Any(aPropIdStr) );
352 // Copy string from source to target resource
353 else if( eMode == COPY_RESOURCES && xSourceStringResolver.is() )
355 OUString aPureSourceIdStr = aPropStr.copy( 1 );
357 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
359 // Copy Id for all locales
360 const Locale* pLocales = aLocaleSeq.getConstArray();
361 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
363 const Locale& rLocale = pLocales[ i ];
364 OUString aResStr;
367 aResStr = xSourceStringResolver->resolveStringForLocale
368 ( aPureSourceIdStr, rLocale );
370 catch(const MissingResourceException&)
372 aResStr = xSourceStringResolver->resolveStringForLocale
373 ( aPureSourceIdStr, rDefaultLocale );
375 xStringResourceManager->setStringForLocale( aPureSourceIdStr, aResStr, rLocale );
378 nChangedCount++;
381 // Listbox / Combobox
382 else if( eType == TypeClass_SEQUENCE )
384 Any aPropAny = xPropertySet->getPropertyValue( aPropName );
385 Sequence< OUString > aPropStrings;
386 aPropAny >>= aPropStrings;
388 const OUString* pPropStrings = aPropStrings.getConstArray();
389 sal_Int32 nPropStringCount = aPropStrings.getLength();
390 if( nPropStringCount == 0 )
391 continue;
393 // Replace string by id, add id+string to StringResource
394 if( eMode == SET_IDS )
396 Sequence< OUString > aIdStrings;
397 aIdStrings.realloc( nPropStringCount );
398 OUString* pIdStrings = aIdStrings.getArray();
400 OUString aIdStrBase = aDot
401 + aCtrlName
402 + aDot
403 + aPropName;
405 const Locale* pLocales = aLocaleSeq.getConstArray();
406 sal_Int32 i;
407 for ( i = 0; i < nPropStringCount; ++i )
409 OUString aPropStr = pPropStrings[i];
410 bool bEscAlreadyExisting = aPropStr.startsWith("&");
411 if( bEscAlreadyExisting )
413 pIdStrings[i] = aPropStr;
414 continue;
417 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
418 OUString aPureIdStr = OUString::number( nUniqueId )
419 + aIdStrBase;
421 // Set Id for all locales
422 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
424 const Locale& rLocale = pLocales[ iLocale ];
425 xStringResourceManager->setStringForLocale( aPureIdStr, aPropStr, rLocale );
428 OUString aPropIdStr = aEsc + aPureIdStr;
429 pIdStrings[i] = aPropIdStr;
431 xPropertySet->setPropertyValue( aPropName, Any(aIdStrings) );
433 // Replace id by string from StringResource
434 else if( eMode == RESET_IDS )
436 Sequence< OUString > aNewPropStrings;
437 aNewPropStrings.realloc( nPropStringCount );
438 OUString* pNewPropStrings = aNewPropStrings.getArray();
440 sal_Int32 i;
441 for ( i = 0; i < nPropStringCount; ++i )
443 OUString aIdStr = pPropStrings[i];
444 OUString aNewPropStr = aIdStr;
445 if( aIdStr.getLength() > 1 )
447 OUString aPureIdStr = aIdStr.copy( 1 );
450 aNewPropStr = xStringResourceManager->resolveString( aPureIdStr );
452 catch(const MissingResourceException&)
456 pNewPropStrings[i] = aNewPropStr;
458 xPropertySet->setPropertyValue( aPropName, Any(aNewPropStrings) );
460 // Remove Id for all locales
461 else if( eMode == REMOVE_IDS_FROM_RESOURCE )
463 const Locale* pLocales = aLocaleSeq.getConstArray();
464 sal_Int32 i;
465 for ( i = 0; i < nPropStringCount; ++i )
467 OUString aIdStr = pPropStrings[i];
468 if( aIdStr.getLength() > 1 )
470 OUString aPureIdStr = aIdStr.copy( 1 );
472 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
474 const Locale& rLocale = pLocales[iLocale];
477 xStringResourceManager->removeIdForLocale( aPureIdStr, rLocale );
479 catch(const MissingResourceException&)
486 // Rename resource id
487 else if( eMode == RENAME_CONTROL_IDS )
489 Sequence< OUString > aIdStrings;
490 aIdStrings.realloc( nPropStringCount );
491 OUString* pIdStrings = aIdStrings.getArray();
493 OUString aIdStrBase = aDot
494 + aCtrlName
495 + aDot
496 + aPropName;
498 const Locale* pLocales = aLocaleSeq.getConstArray();
499 sal_Int32 i;
500 for ( i = 0; i < nPropStringCount; ++i )
502 OUString aSourceIdStr = pPropStrings[i];
503 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
505 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
506 OUString aPureIdStr = OUString::number( nUniqueId )
507 + aIdStrBase;
509 // Set Id for all locales
510 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
512 const Locale& rLocale = pLocales[ iLocale ];
514 OUString aResStr;
517 aResStr = xStringResourceManager->resolveStringForLocale
518 ( aPureSourceIdStr, rLocale );
519 xStringResourceManager->removeIdForLocale( aPureSourceIdStr, rLocale );
520 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
522 catch(const MissingResourceException&)
526 OUString aPropIdStr = aEsc + aPureIdStr;
527 pIdStrings[i] = aPropIdStr;
529 xPropertySet->setPropertyValue( aPropName, Any(aIdStrings) );
531 // Replace string by string from source StringResourceResolver
532 else if( eMode == MOVE_RESOURCES && xSourceStringResolver.is() )
534 Sequence< OUString > aIdStrings;
535 aIdStrings.realloc( nPropStringCount );
536 OUString* pIdStrings = aIdStrings.getArray();
538 OUString aIdStrBase = aDot
539 + aCtrlName
540 + aDot
541 + aPropName;
543 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
545 const Locale* pLocales = aLocaleSeq.getConstArray();
546 sal_Int32 i;
547 for ( i = 0; i < nPropStringCount; ++i )
549 OUString aSourceIdStr = pPropStrings[i];
550 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
552 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
553 OUString aPureIdStr = OUString::number( nUniqueId )
554 + aIdStrBase;
556 // Set Id for all locales
557 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
559 const Locale& rLocale = pLocales[ iLocale ];
561 OUString aResStr;
564 aResStr = xSourceStringResolver->resolveStringForLocale
565 ( aPureSourceIdStr, rLocale );
567 catch(const MissingResourceException&)
569 aResStr = xSourceStringResolver->resolveStringForLocale
570 ( aPureSourceIdStr, rDefaultLocale );
572 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
575 OUString aPropIdStr = aEsc + aPureIdStr;
576 pIdStrings[i] = aPropIdStr;
578 xPropertySet->setPropertyValue( aPropName, Any(aIdStrings) );
580 // Copy string from source to target resource
581 else if( eMode == COPY_RESOURCES && xSourceStringResolver.is() )
583 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
585 const Locale* pLocales = aLocaleSeq.getConstArray();
586 sal_Int32 i;
587 for ( i = 0; i < nPropStringCount; ++i )
589 OUString aSourceIdStr = pPropStrings[i];
590 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
592 // Set Id for all locales
593 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
595 const Locale& rLocale = pLocales[ iLocale ];
597 OUString aResStr;
600 aResStr = xSourceStringResolver->resolveStringForLocale
601 ( aPureSourceIdStr, rLocale );
603 catch(const MissingResourceException&)
605 aResStr = xSourceStringResolver->resolveStringForLocale
606 ( aPureSourceIdStr, rDefaultLocale );
608 xStringResourceManager->setStringForLocale( aPureSourceIdStr, aResStr, rLocale );
612 nChangedCount++;
617 return nChangedCount;
621 void LocalizationMgr::handleAddLocales( const Sequence< Locale >& aLocaleSeq )
623 const Locale* pLocales = aLocaleSeq.getConstArray();
624 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
626 if( isLibraryLocalized() )
628 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
630 const Locale& rLocale = pLocales[ i ];
631 m_xStringResourceManager->newLocale( rLocale );
634 else
636 DBG_ASSERT( nLocaleCount==1, "LocalizationMgr::handleAddLocales(): Only one first locale allowed" );
638 const Locale& rLocale = pLocales[ 0 ];
639 m_xStringResourceManager->newLocale( rLocale );
640 enableResourceForAllLibraryDialogs();
643 MarkDocumentModified( m_aDocument );
645 // update locale toolbar
646 if (SfxBindings* pBindings = GetBindingsPtr())
647 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
649 handleTranslationbar();
653 void LocalizationMgr::handleRemoveLocales( const Sequence< Locale >& aLocaleSeq )
655 const Locale* pLocales = aLocaleSeq.getConstArray();
656 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
657 bool bConsistant = true;
658 bool bModified = false;
660 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
662 const Locale& rLocale = pLocales[ i ];
663 bool bRemove = true;
665 // Check if last locale
666 Sequence< Locale > aResLocaleSeq = m_xStringResourceManager->getLocales();
667 if( aResLocaleSeq.getLength() == 1 )
669 const Locale& rLastResLocale = aResLocaleSeq.getConstArray()[ 0 ];
670 if( localesAreEqual( rLocale, rLastResLocale ) )
672 disableResourceForAllLibraryDialogs();
674 else
676 // Inconsistency, keep last locale
677 bConsistant = false;
678 bRemove = false;
682 if( bRemove )
686 m_xStringResourceManager->removeLocale( rLocale );
687 bModified = true;
689 catch(const IllegalArgumentException&)
691 bConsistant = false;
695 if( bModified )
697 MarkDocumentModified( m_aDocument );
699 // update slots
700 if (SfxBindings* pBindings = GetBindingsPtr())
702 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
703 pBindings->Invalidate( SID_BASICIDE_MANAGE_LANG );
706 handleTranslationbar();
709 DBG_ASSERT( bConsistant,
710 "LocalizationMgr::handleRemoveLocales(): sequence contains unsupported locales" );
713 void LocalizationMgr::handleSetDefaultLocale(const Locale& rLocale)
715 if( m_xStringResourceManager.is() )
719 m_xStringResourceManager->setDefaultLocale(rLocale);
721 catch(const IllegalArgumentException&)
723 OSL_FAIL( "LocalizationMgr::handleSetDefaultLocale: Invalid locale" );
726 // update locale toolbar
727 if (SfxBindings* pBindings = GetBindingsPtr())
728 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
732 void LocalizationMgr::handleSetCurrentLocale(const css::lang::Locale& rLocale)
734 if( m_xStringResourceManager.is() )
738 m_xStringResourceManager->setCurrentLocale(rLocale, false);
740 catch(const IllegalArgumentException&)
742 OSL_FAIL( "LocalizationMgr::handleSetCurrentLocale: Invalid locale" );
745 // update locale toolbar
746 if (SfxBindings* pBindings = GetBindingsPtr())
747 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
749 if (DialogWindow* pDlgWin = dynamic_cast<DialogWindow*>(m_pShell->GetCurWindow()))
750 if (!pDlgWin->IsSuspended())
751 pDlgWin->GetEditor().UpdatePropertyBrowserDelayed();
755 void LocalizationMgr::handleBasicStarted()
757 if( m_xStringResourceManager.is() )
758 m_aLocaleBeforeBasicStart = m_xStringResourceManager->getCurrentLocale();
761 void LocalizationMgr::handleBasicStopped()
765 if( m_xStringResourceManager.is() )
766 m_xStringResourceManager->setCurrentLocale( m_aLocaleBeforeBasicStart, true );
768 catch(const IllegalArgumentException&)
774 DialogWindow* FindDialogWindowForEditor( DlgEditor const * pEditor )
776 Shell::WindowTable const& aWindowTable = GetShell()->GetWindowTable();
777 for (auto const& window : aWindowTable)
779 BaseWindow* pWin = window.second;
780 if (!pWin->IsSuspended())
781 if (DialogWindow* pDlgWin = dynamic_cast<DialogWindow*>(pWin))
783 if (&pDlgWin->GetEditor() == pEditor)
784 return pDlgWin;
787 return nullptr;
791 void LocalizationMgr::setControlResourceIDsForNewEditorObject( DlgEditor const * pEditor,
792 const Any& rControlAny, const OUString& aCtrlName )
794 // Get library for DlgEditor
795 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
796 if( !pDlgWin )
797 return;
798 ScriptDocument aDocument( pDlgWin->GetDocument() );
799 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::setControlResourceIDsForNewEditorObject: invalid document!" );
800 if ( !aDocument.isValid() )
801 return;
802 const OUString& rLibName = pDlgWin->GetLibName();
803 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
804 Reference< XStringResourceManager > xStringResourceManager =
805 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
807 // Set resource property
808 if( !xStringResourceManager.is() || xStringResourceManager->getLocales().getLength() == 0 )
809 return;
811 OUString aDialogName = pDlgWin->GetName();
812 Reference< XStringResourceResolver > xDummyStringResolver;
813 sal_Int32 nChangedCount = implHandleControlResourceProperties
814 ( rControlAny, aDialogName, aCtrlName, xStringResourceManager,
815 xDummyStringResolver, SET_IDS );
817 if( nChangedCount )
818 MarkDocumentModified( aDocument );
821 void LocalizationMgr::renameControlResourceIDsForEditorObject( DlgEditor const * pEditor,
822 const css::uno::Any& rControlAny, const OUString& aNewCtrlName )
824 // Get library for DlgEditor
825 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
826 if( !pDlgWin )
827 return;
828 ScriptDocument aDocument( pDlgWin->GetDocument() );
829 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::renameControlResourceIDsForEditorObject: invalid document!" );
830 if ( !aDocument.isValid() )
831 return;
832 const OUString& rLibName = pDlgWin->GetLibName();
833 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
834 Reference< XStringResourceManager > xStringResourceManager =
835 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
837 // Set resource property
838 if( !xStringResourceManager.is() || xStringResourceManager->getLocales().getLength() == 0 )
839 return;
841 OUString aDialogName = pDlgWin->GetName();
842 Reference< XStringResourceResolver > xDummyStringResolver;
843 implHandleControlResourceProperties
844 ( rControlAny, aDialogName, aNewCtrlName, xStringResourceManager,
845 xDummyStringResolver, RENAME_CONTROL_IDS );
849 void LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( DlgEditor const * pEditor,
850 const Any& rControlAny, const OUString& aCtrlName )
852 // Get library for DlgEditor
853 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
854 if( !pDlgWin )
855 return;
856 ScriptDocument aDocument( pDlgWin->GetDocument() );
857 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject: invalid document!" );
858 if ( !aDocument.isValid() )
859 return;
860 const OUString& rLibName = pDlgWin->GetLibName();
861 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
862 Reference< XStringResourceManager > xStringResourceManager =
863 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
865 OUString aDialogName = pDlgWin->GetName();
866 Reference< XStringResourceResolver > xDummyStringResolver;
867 sal_Int32 nChangedCount = implHandleControlResourceProperties
868 ( rControlAny, aDialogName, aCtrlName, xStringResourceManager,
869 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
871 if( nChangedCount )
872 MarkDocumentModified( aDocument );
875 void LocalizationMgr::setStringResourceAtDialog( const ScriptDocument& rDocument, const OUString& aLibName,
876 const OUString& 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 );
883 // Set resource property
884 if( xStringResourceManager.is() )
886 // Not very elegant as dialog may or may not be localized yet
887 // TODO: Find better place, where dialog is created
888 if( xStringResourceManager->getLocales().getLength() > 0 )
890 Any aDialogCtrl;
891 aDialogCtrl <<= xDialogModel;
892 Reference< XStringResourceResolver > xDummyStringResolver;
893 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
894 OUString(), xStringResourceManager,
895 xDummyStringResolver, SET_IDS );
898 Reference< beans::XPropertySet > xDlgPSet( xDialogModel, UNO_QUERY );
899 xDlgPSet->setPropertyValue( "ResourceResolver", Any(xStringResourceManager) );
903 void LocalizationMgr::renameStringResourceIDs( const ScriptDocument& rDocument, const OUString& aLibName,
904 const OUString& aDlgName, const Reference< container::XNameContainer >& xDialogModel )
906 // Get library
907 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, true ) );
908 Reference< XStringResourceManager > xStringResourceManager =
909 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
910 if( !xStringResourceManager.is() )
911 return;
913 Any aDialogCtrl;
914 aDialogCtrl <<= xDialogModel;
915 Reference< XStringResourceResolver > xDummyStringResolver;
916 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
917 OUString(), xStringResourceManager,
918 xDummyStringResolver, RENAME_DIALOG_IDS );
920 // Handle all controls
921 Sequence< OUString > aNames = xDialogModel->getElementNames();
922 const OUString* pNames = aNames.getConstArray();
923 sal_Int32 nCtrls = aNames.getLength();
924 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
926 OUString aCtrlName( pNames[i] );
927 Any aCtrl = xDialogModel->getByName( aCtrlName );
928 implHandleControlResourceProperties( aCtrl, aDlgName,
929 aCtrlName, xStringResourceManager,
930 xDummyStringResolver, RENAME_DIALOG_IDS );
934 void LocalizationMgr::removeResourceForDialog( const ScriptDocument& rDocument, const OUString& aLibName,
935 const OUString& 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 OUString(), xStringResourceManager,
949 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
951 // Handle all controls
952 Sequence< OUString > aNames = xDialogModel->getElementNames();
953 const OUString* pNames = aNames.getConstArray();
954 sal_Int32 nCtrls = aNames.getLength();
955 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
957 OUString aCtrlName( pNames[i] );
958 Any aCtrl = xDialogModel->getByName( aCtrlName );
959 implHandleControlResourceProperties( aCtrl, aDlgName,
960 aCtrlName, xStringResourceManager,
961 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
965 void LocalizationMgr::resetResourceForDialog( const Reference< container::XNameContainer >& xDialogModel,
966 const Reference< XStringResourceManager >& xStringResourceManager )
968 if( !xStringResourceManager.is() )
969 return;
971 // Dialog as control
972 OUString aDummyName;
973 Any aDialogCtrl;
974 aDialogCtrl <<= xDialogModel;
975 Reference< XStringResourceResolver > xDummyStringResolver;
976 implHandleControlResourceProperties( aDialogCtrl, aDummyName,
977 aDummyName, xStringResourceManager, xDummyStringResolver, RESET_IDS );
979 // Handle all controls
980 Sequence< OUString > aNames = xDialogModel->getElementNames();
981 const OUString* pNames = aNames.getConstArray();
982 sal_Int32 nCtrls = aNames.getLength();
983 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
985 OUString aCtrlName( pNames[i] );
986 Any aCtrl = xDialogModel->getByName( aCtrlName );
987 implHandleControlResourceProperties( aCtrl, aDummyName,
988 aCtrlName, xStringResourceManager, xDummyStringResolver, RESET_IDS );
992 void LocalizationMgr::setResourceIDsForDialog( const Reference< container::XNameContainer >& xDialogModel,
993 const Reference< XStringResourceManager >& xStringResourceManager )
995 if( !xStringResourceManager.is() )
996 return;
998 // Dialog as control
999 OUString aDummyName;
1000 Any aDialogCtrl;
1001 aDialogCtrl <<= xDialogModel;
1002 Reference< XStringResourceResolver > xDummyStringResolver;
1003 implHandleControlResourceProperties( aDialogCtrl, aDummyName,
1004 aDummyName, xStringResourceManager, xDummyStringResolver, SET_IDS );
1006 // Handle all controls
1007 Sequence< OUString > aNames = xDialogModel->getElementNames();
1008 const OUString* pNames = aNames.getConstArray();
1009 sal_Int32 nCtrls = aNames.getLength();
1010 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1012 OUString aCtrlName( pNames[i] );
1013 Any aCtrl = xDialogModel->getByName( aCtrlName );
1014 implHandleControlResourceProperties( aCtrl, aDummyName,
1015 aCtrlName, xStringResourceManager, xDummyStringResolver, SET_IDS );
1019 void LocalizationMgr::copyResourcesForPastedEditorObject( DlgEditor const * pEditor,
1020 const Any& rControlAny, const OUString& aCtrlName,
1021 const Reference< XStringResourceResolver >& xSourceStringResolver )
1023 // Get library for DlgEditor
1024 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
1025 if( !pDlgWin )
1026 return;
1027 ScriptDocument aDocument( pDlgWin->GetDocument() );
1028 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::copyResourcesForPastedEditorObject: invalid document!" );
1029 if ( !aDocument.isValid() )
1030 return;
1031 const OUString& rLibName = pDlgWin->GetLibName();
1032 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
1033 Reference< XStringResourceManager > xStringResourceManager =
1034 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
1036 // Set resource property
1037 if( !xStringResourceManager.is() || xStringResourceManager->getLocales().getLength() == 0 )
1038 return;
1040 OUString aDialogName = pDlgWin->GetName();
1041 implHandleControlResourceProperties
1042 ( rControlAny, aDialogName, aCtrlName, xStringResourceManager,
1043 xSourceStringResolver, MOVE_RESOURCES );
1046 void LocalizationMgr::copyResourceForDroppedDialog( const Reference< container::XNameContainer >& xDialogModel,
1047 const OUString& aDialogName, const Reference< XStringResourceManager >& xStringResourceManager,
1048 const Reference< XStringResourceResolver >& xSourceStringResolver )
1050 if( !xStringResourceManager.is() )
1051 return;
1053 // Dialog as control
1054 Any aDialogCtrl;
1055 aDialogCtrl <<= xDialogModel;
1056 implHandleControlResourceProperties( aDialogCtrl, aDialogName,
1057 OUString(), xStringResourceManager, xSourceStringResolver, MOVE_RESOURCES );
1059 // Handle all controls
1060 Sequence< OUString > aNames = xDialogModel->getElementNames();
1061 const OUString* pNames = aNames.getConstArray();
1062 sal_Int32 nCtrls = aNames.getLength();
1063 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1065 OUString aCtrlName( pNames[i] );
1066 Any aCtrl = xDialogModel->getByName( aCtrlName );
1067 implHandleControlResourceProperties( aCtrl, aDialogName,
1068 aCtrlName, xStringResourceManager, xSourceStringResolver, MOVE_RESOURCES );
1072 void LocalizationMgr::copyResourceForDialog(
1073 const Reference< container::XNameContainer >& xDialogModel,
1074 const Reference< XStringResourceResolver >& xSourceStringResolver,
1075 const Reference< XStringResourceManager >& xTargetStringResourceManager )
1077 if( !xDialogModel.is() || !xSourceStringResolver.is() || !xTargetStringResourceManager.is() )
1078 return;
1080 OUString aDummyName;
1081 Any aDialogCtrl;
1082 aDialogCtrl <<= xDialogModel;
1083 implHandleControlResourceProperties
1084 ( aDialogCtrl, aDummyName, aDummyName, xTargetStringResourceManager,
1085 xSourceStringResolver, COPY_RESOURCES );
1087 // Handle all controls
1088 Sequence< OUString > aNames = xDialogModel->getElementNames();
1089 const OUString* pNames = aNames.getConstArray();
1090 sal_Int32 nCtrls = aNames.getLength();
1091 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1093 OUString aCtrlName( pNames[i] );
1094 Any aCtrl = xDialogModel->getByName( aCtrlName );
1095 implHandleControlResourceProperties( aCtrl, aDummyName, aDummyName,
1096 xTargetStringResourceManager, xSourceStringResolver, COPY_RESOURCES );
1100 Reference< XStringResourceManager > LocalizationMgr::getStringResourceFromDialogLibrary
1101 ( const Reference< container::XNameContainer >& xDialogLib )
1103 Reference< XStringResourceManager > xStringResourceManager;
1104 if( xDialogLib.is() )
1106 Reference< resource::XStringResourceSupplier > xStringResourceSupplier( xDialogLib, UNO_QUERY );
1107 if( xStringResourceSupplier.is() )
1109 Reference< resource::XStringResourceResolver >
1110 xStringResourceResolver = xStringResourceSupplier->getStringResource();
1112 xStringResourceManager =
1113 Reference< resource::XStringResourceManager >( xStringResourceResolver, UNO_QUERY );
1116 return xStringResourceManager;
1119 } // namespace basctl
1121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */