1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
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
;
45 OUString
const aDot(".");
46 OUString
const aEsc("&");
47 OUString
const aSemi(";");
51 LocalizationMgr::LocalizationMgr(
53 ScriptDocument
const& rDocument
,
54 OUString
const& aLibName
,
55 Reference
<XStringResourceManager
> const& xStringResourceManager
57 m_xStringResourceManager(xStringResourceManager
),
59 m_aDocument(rDocument
),
63 bool LocalizationMgr::isLibraryLocalized ()
65 if (m_xStringResourceManager
.is())
66 return m_xStringResourceManager
->getLocales().getLength() > 0;
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" );
81 if ( xLayoutManager
.is() )
83 if ( !isLibraryLocalized() )
85 xLayoutManager
->destroyElement( aToolBarResName
);
89 xLayoutManager
->createElement( aToolBarResName
);
90 xLayoutManager
->requestElement( aToolBarResName
);
97 // TODO: -> export from toolkit
100 bool isLanguageDependentProperty( const OUString
& aName
)
105 sal_Int32 nNameLength
;
113 { "CurrencySymbol", 14 },
114 { "StringItemList", 14 },
118 for (Prop
const* pProp
= vProp
; pProp
->sName
; ++pProp
)
119 if (aName
.equalsAsciiL(pProp
->sName
, pProp
->nNameLength
))
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();
140 // Handle dialog itself as control
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
)
173 if( !aCtrlName
.isEmpty() )
175 aPureIdStr
+= aCtrlName
;
178 aPureIdStr
+= aPropName
;
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 )
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
)
221 if( eType
== TypeClass_STRING
)
223 Any aPropAny
= xPropertySet
->getPropertyValue( aPropName
);
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
)
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
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
];
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
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
];
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
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
];
367 aResStr
= xSourceStringResolver
->resolveStringForLocale
368 ( aPureSourceIdStr
, rLocale
);
370 catch(const MissingResourceException
&)
372 aResStr
= xSourceStringResolver
->resolveStringForLocale
373 ( aPureSourceIdStr
, rDefaultLocale
);
375 xStringResourceManager
->setStringForLocale( aPureSourceIdStr
, aResStr
, rLocale
);
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 )
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
405 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
407 for ( i
= 0; i
< nPropStringCount
; ++i
)
409 OUString aPropStr
= pPropStrings
[i
];
410 bool bEscAlreadyExisting
= aPropStr
.startsWith("&");
411 if( bEscAlreadyExisting
)
413 pIdStrings
[i
] = aPropStr
;
417 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
418 OUString aPureIdStr
= OUString::number( nUniqueId
)
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();
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 Sequence
< OUString
> aNewPropStrings
;
464 aNewPropStrings
.realloc( nPropStringCount
);
466 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
468 for ( i
= 0; i
< nPropStringCount
; ++i
)
470 OUString aIdStr
= pPropStrings
[i
];
471 if( aIdStr
.getLength() > 1 )
473 OUString aPureIdStr
= aIdStr
.copy( 1 );
475 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
477 const Locale
& rLocale
= pLocales
[iLocale
];
480 xStringResourceManager
->removeIdForLocale( aPureIdStr
, rLocale
);
482 catch(const MissingResourceException
&)
489 // Rename resource id
490 else if( eMode
== RENAME_CONTROL_IDS
)
492 Sequence
< OUString
> aIdStrings
;
493 aIdStrings
.realloc( nPropStringCount
);
494 OUString
* pIdStrings
= aIdStrings
.getArray();
496 OUString aIdStrBase
= aDot
501 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
503 for ( i
= 0; i
< nPropStringCount
; ++i
)
505 OUString aSourceIdStr
= pPropStrings
[i
];
506 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
508 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
509 OUString aPureIdStr
= OUString::number( nUniqueId
)
512 // Set Id for all locales
513 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
515 const Locale
& rLocale
= pLocales
[ iLocale
];
520 aResStr
= xStringResourceManager
->resolveStringForLocale
521 ( aPureSourceIdStr
, rLocale
);
522 xStringResourceManager
->removeIdForLocale( aPureSourceIdStr
, rLocale
);
523 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
525 catch(const MissingResourceException
&)
529 OUString aPropIdStr
= aEsc
+ aPureIdStr
;
530 pIdStrings
[i
] = aPropIdStr
;
532 xPropertySet
->setPropertyValue( aPropName
, Any(aIdStrings
) );
534 // Replace string by string from source StringResourceResolver
535 else if( eMode
== MOVE_RESOURCES
&& xSourceStringResolver
.is() )
537 Sequence
< OUString
> aIdStrings
;
538 aIdStrings
.realloc( nPropStringCount
);
539 OUString
* pIdStrings
= aIdStrings
.getArray();
541 OUString aIdStrBase
= aDot
546 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
548 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
550 for ( i
= 0; i
< nPropStringCount
; ++i
)
552 OUString aSourceIdStr
= pPropStrings
[i
];
553 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
555 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
556 OUString aPureIdStr
= OUString::number( nUniqueId
)
559 // Set Id for all locales
560 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
562 const Locale
& rLocale
= pLocales
[ iLocale
];
567 aResStr
= xSourceStringResolver
->resolveStringForLocale
568 ( aPureSourceIdStr
, rLocale
);
570 catch(const MissingResourceException
&)
572 aResStr
= xSourceStringResolver
->resolveStringForLocale
573 ( aPureSourceIdStr
, rDefaultLocale
);
575 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
578 OUString aPropIdStr
= aEsc
+ aPureIdStr
;
579 pIdStrings
[i
] = aPropIdStr
;
581 xPropertySet
->setPropertyValue( aPropName
, Any(aIdStrings
) );
583 // Copy string from source to target resource
584 else if( eMode
== COPY_RESOURCES
&& xSourceStringResolver
.is() )
586 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
588 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
590 for ( i
= 0; i
< nPropStringCount
; ++i
)
592 OUString aSourceIdStr
= pPropStrings
[i
];
593 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
595 // Set Id for all locales
596 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
598 const Locale
& rLocale
= pLocales
[ iLocale
];
603 aResStr
= xSourceStringResolver
->resolveStringForLocale
604 ( aPureSourceIdStr
, rLocale
);
606 catch(const MissingResourceException
&)
608 aResStr
= xSourceStringResolver
->resolveStringForLocale
609 ( aPureSourceIdStr
, rDefaultLocale
);
611 xStringResourceManager
->setStringForLocale( aPureSourceIdStr
, aResStr
, rLocale
);
620 return nChangedCount
;
624 void LocalizationMgr::handleAddLocales( const Sequence
< Locale
>& aLocaleSeq
)
626 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
627 sal_Int32 nLocaleCount
= aLocaleSeq
.getLength();
629 if( isLibraryLocalized() )
631 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
633 const Locale
& rLocale
= pLocales
[ i
];
634 m_xStringResourceManager
->newLocale( rLocale
);
639 DBG_ASSERT( nLocaleCount
==1, "LocalizationMgr::handleAddLocales(): Only one first locale allowed" );
641 const Locale
& rLocale
= pLocales
[ 0 ];
642 m_xStringResourceManager
->newLocale( rLocale
);
643 enableResourceForAllLibraryDialogs();
646 MarkDocumentModified( m_aDocument
);
648 // update locale toolbar
649 if (SfxBindings
* pBindings
= GetBindingsPtr())
650 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
652 handleTranslationbar();
656 void LocalizationMgr::handleRemoveLocales( const Sequence
< Locale
>& aLocaleSeq
)
658 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
659 sal_Int32 nLocaleCount
= aLocaleSeq
.getLength();
660 bool bConsistant
= true;
661 bool bModified
= false;
663 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
665 const Locale
& rLocale
= pLocales
[ i
];
668 // Check if last locale
669 Sequence
< Locale
> aResLocaleSeq
= m_xStringResourceManager
->getLocales();
670 if( aResLocaleSeq
.getLength() == 1 )
672 const Locale
& rLastResLocale
= aResLocaleSeq
.getConstArray()[ 0 ];
673 if( localesAreEqual( rLocale
, rLastResLocale
) )
675 disableResourceForAllLibraryDialogs();
679 // Inconsistency, keep last locale
689 m_xStringResourceManager
->removeLocale( rLocale
);
692 catch(const IllegalArgumentException
&)
700 MarkDocumentModified( m_aDocument
);
703 if (SfxBindings
* pBindings
= GetBindingsPtr())
705 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
706 pBindings
->Invalidate( SID_BASICIDE_MANAGE_LANG
);
709 handleTranslationbar();
712 DBG_ASSERT( bConsistant
,
713 "LocalizationMgr::handleRemoveLocales(): sequence contains unsupported locales" );
716 void LocalizationMgr::handleSetDefaultLocale(const Locale
& rLocale
)
718 if( m_xStringResourceManager
.is() )
722 m_xStringResourceManager
->setDefaultLocale(rLocale
);
724 catch(const IllegalArgumentException
&)
726 OSL_FAIL( "LocalizationMgr::handleSetDefaultLocale: Invalid locale" );
729 // update locale toolbar
730 if (SfxBindings
* pBindings
= GetBindingsPtr())
731 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
735 void LocalizationMgr::handleSetCurrentLocale(const css::lang::Locale
& rLocale
)
737 if( m_xStringResourceManager
.is() )
741 m_xStringResourceManager
->setCurrentLocale(rLocale
, false);
743 catch(const IllegalArgumentException
&)
745 OSL_FAIL( "LocalizationMgr::handleSetCurrentLocale: Invalid locale" );
748 // update locale toolbar
749 if (SfxBindings
* pBindings
= GetBindingsPtr())
750 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
752 if (DialogWindow
* pDlgWin
= dynamic_cast<DialogWindow
*>(m_pShell
->GetCurWindow()))
753 if (!pDlgWin
->IsSuspended())
754 pDlgWin
->GetEditor().UpdatePropertyBrowserDelayed();
758 void LocalizationMgr::handleBasicStarted()
760 if( m_xStringResourceManager
.is() )
761 m_aLocaleBeforeBasicStart
= m_xStringResourceManager
->getCurrentLocale();
764 void LocalizationMgr::handleBasicStopped()
768 if( m_xStringResourceManager
.is() )
769 m_xStringResourceManager
->setCurrentLocale( m_aLocaleBeforeBasicStart
, true );
771 catch(const IllegalArgumentException
&)
777 DialogWindow
* FindDialogWindowForEditor( DlgEditor
const * pEditor
)
779 Shell::WindowTable
const& aWindowTable
= GetShell()->GetWindowTable();
780 for (Shell::WindowTableIt it
= aWindowTable
.begin(); it
!= aWindowTable
.end(); ++it
)
782 BaseWindow
* pWin
= it
->second
;
783 if (!pWin
->IsSuspended())
784 if (DialogWindow
* pDlgWin
= dynamic_cast<DialogWindow
*>(pWin
))
786 if (&pDlgWin
->GetEditor() == pEditor
)
794 void LocalizationMgr::setControlResourceIDsForNewEditorObject( DlgEditor
const * pEditor
,
795 const Any
& rControlAny
, const OUString
& aCtrlName
)
797 // Get library for DlgEditor
798 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
801 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
802 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::setControlResourceIDsForNewEditorObject: invalid document!" );
803 if ( !aDocument
.isValid() )
805 const OUString
& rLibName
= pDlgWin
->GetLibName();
806 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
807 Reference
< XStringResourceManager
> xStringResourceManager
=
808 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
810 // Set resource property
811 if( !xStringResourceManager
.is() || xStringResourceManager
->getLocales().getLength() == 0 )
814 OUString aDialogName
= pDlgWin
->GetName();
815 Reference
< XStringResourceResolver
> xDummyStringResolver
;
816 sal_Int32 nChangedCount
= implHandleControlResourceProperties
817 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
818 xDummyStringResolver
, SET_IDS
);
821 MarkDocumentModified( aDocument
);
824 void LocalizationMgr::renameControlResourceIDsForEditorObject( DlgEditor
const * pEditor
,
825 const css::uno::Any
& rControlAny
, const OUString
& aNewCtrlName
)
827 // Get library for DlgEditor
828 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
831 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
832 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::renameControlResourceIDsForEditorObject: invalid document!" );
833 if ( !aDocument
.isValid() )
835 const OUString
& rLibName
= pDlgWin
->GetLibName();
836 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
837 Reference
< XStringResourceManager
> xStringResourceManager
=
838 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
840 // Set resource property
841 if( !xStringResourceManager
.is() || xStringResourceManager
->getLocales().getLength() == 0 )
844 OUString aDialogName
= pDlgWin
->GetName();
845 Reference
< XStringResourceResolver
> xDummyStringResolver
;
846 implHandleControlResourceProperties
847 ( rControlAny
, aDialogName
, aNewCtrlName
, xStringResourceManager
,
848 xDummyStringResolver
, RENAME_CONTROL_IDS
);
852 void LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( DlgEditor
const * pEditor
,
853 const Any
& rControlAny
, const OUString
& aCtrlName
)
855 // Get library for DlgEditor
856 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
859 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
860 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject: invalid document!" );
861 if ( !aDocument
.isValid() )
863 const OUString
& rLibName
= pDlgWin
->GetLibName();
864 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
865 Reference
< XStringResourceManager
> xStringResourceManager
=
866 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
868 OUString aDialogName
= pDlgWin
->GetName();
869 Reference
< XStringResourceResolver
> xDummyStringResolver
;
870 sal_Int32 nChangedCount
= implHandleControlResourceProperties
871 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
872 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
875 MarkDocumentModified( aDocument
);
878 void LocalizationMgr::setStringResourceAtDialog( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
879 const OUString
& aDlgName
, const Reference
< container::XNameContainer
>& xDialogModel
)
882 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
883 Reference
< XStringResourceManager
> xStringResourceManager
=
884 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
886 // Set resource property
887 if( xStringResourceManager
.is() )
889 // Not very elegant as dialog may or may not be localized yet
890 // TODO: Find better place, where dialog is created
891 if( xStringResourceManager
->getLocales().getLength() > 0 )
894 aDialogCtrl
<<= xDialogModel
;
895 Reference
< XStringResourceResolver
> xDummyStringResolver
;
896 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
897 OUString(), xStringResourceManager
,
898 xDummyStringResolver
, SET_IDS
);
901 Reference
< beans::XPropertySet
> xDlgPSet( xDialogModel
, UNO_QUERY
);
902 xDlgPSet
->setPropertyValue( "ResourceResolver", Any(xStringResourceManager
) );
906 void LocalizationMgr::renameStringResourceIDs( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
907 const OUString
& aDlgName
, const Reference
< container::XNameContainer
>& xDialogModel
)
910 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
911 Reference
< XStringResourceManager
> xStringResourceManager
=
912 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
913 if( !xStringResourceManager
.is() )
917 aDialogCtrl
<<= xDialogModel
;
918 Reference
< XStringResourceResolver
> xDummyStringResolver
;
919 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
920 OUString(), xStringResourceManager
,
921 xDummyStringResolver
, RENAME_DIALOG_IDS
);
923 // Handle all controls
924 Sequence
< OUString
> aNames
= xDialogModel
->getElementNames();
925 const OUString
* pNames
= aNames
.getConstArray();
926 sal_Int32 nCtrls
= aNames
.getLength();
927 for( sal_Int32 i
= 0 ; i
< nCtrls
; ++i
)
929 OUString
aCtrlName( pNames
[i
] );
930 Any aCtrl
= xDialogModel
->getByName( aCtrlName
);
931 implHandleControlResourceProperties( aCtrl
, aDlgName
,
932 aCtrlName
, xStringResourceManager
,
933 xDummyStringResolver
, RENAME_DIALOG_IDS
);
937 void LocalizationMgr::removeResourceForDialog( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
938 const OUString
& aDlgName
, const Reference
< container::XNameContainer
>& xDialogModel
)
941 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
942 Reference
< XStringResourceManager
> xStringResourceManager
=
943 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
944 if( !xStringResourceManager
.is() )
948 aDialogCtrl
<<= xDialogModel
;
949 Reference
< XStringResourceResolver
> xDummyStringResolver
;
950 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
951 OUString(), xStringResourceManager
,
952 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
954 // Handle all controls
955 Sequence
< OUString
> aNames
= xDialogModel
->getElementNames();
956 const OUString
* pNames
= aNames
.getConstArray();
957 sal_Int32 nCtrls
= aNames
.getLength();
958 for( sal_Int32 i
= 0 ; i
< nCtrls
; ++i
)
960 OUString
aCtrlName( pNames
[i
] );
961 Any aCtrl
= xDialogModel
->getByName( aCtrlName
);
962 implHandleControlResourceProperties( aCtrl
, aDlgName
,
963 aCtrlName
, xStringResourceManager
,
964 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
968 void LocalizationMgr::resetResourceForDialog( const Reference
< container::XNameContainer
>& xDialogModel
,
969 const Reference
< XStringResourceManager
>& xStringResourceManager
)
971 if( !xStringResourceManager
.is() )
977 aDialogCtrl
<<= xDialogModel
;
978 Reference
< XStringResourceResolver
> xDummyStringResolver
;
979 implHandleControlResourceProperties( aDialogCtrl
, aDummyName
,
980 aDummyName
, xStringResourceManager
, xDummyStringResolver
, RESET_IDS
);
982 // Handle all controls
983 Sequence
< OUString
> aNames
= xDialogModel
->getElementNames();
984 const OUString
* pNames
= aNames
.getConstArray();
985 sal_Int32 nCtrls
= aNames
.getLength();
986 for( sal_Int32 i
= 0 ; i
< nCtrls
; ++i
)
988 OUString
aCtrlName( pNames
[i
] );
989 Any aCtrl
= xDialogModel
->getByName( aCtrlName
);
990 implHandleControlResourceProperties( aCtrl
, aDummyName
,
991 aCtrlName
, xStringResourceManager
, xDummyStringResolver
, RESET_IDS
);
995 void LocalizationMgr::setResourceIDsForDialog( const Reference
< container::XNameContainer
>& xDialogModel
,
996 const Reference
< XStringResourceManager
>& xStringResourceManager
)
998 if( !xStringResourceManager
.is() )
1001 // Dialog as control
1002 OUString aDummyName
;
1004 aDialogCtrl
<<= xDialogModel
;
1005 Reference
< XStringResourceResolver
> xDummyStringResolver
;
1006 implHandleControlResourceProperties( aDialogCtrl
, aDummyName
,
1007 aDummyName
, xStringResourceManager
, xDummyStringResolver
, SET_IDS
);
1009 // Handle all controls
1010 Sequence
< OUString
> aNames
= xDialogModel
->getElementNames();
1011 const OUString
* pNames
= aNames
.getConstArray();
1012 sal_Int32 nCtrls
= aNames
.getLength();
1013 for( sal_Int32 i
= 0 ; i
< nCtrls
; ++i
)
1015 OUString
aCtrlName( pNames
[i
] );
1016 Any aCtrl
= xDialogModel
->getByName( aCtrlName
);
1017 implHandleControlResourceProperties( aCtrl
, aDummyName
,
1018 aCtrlName
, xStringResourceManager
, xDummyStringResolver
, SET_IDS
);
1022 void LocalizationMgr::copyResourcesForPastedEditorObject( DlgEditor
const * pEditor
,
1023 const Any
& rControlAny
, const OUString
& aCtrlName
,
1024 const Reference
< XStringResourceResolver
>& xSourceStringResolver
)
1026 // Get library for DlgEditor
1027 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
1030 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
1031 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::copyResourcesForPastedEditorObject: invalid document!" );
1032 if ( !aDocument
.isValid() )
1034 const OUString
& rLibName
= pDlgWin
->GetLibName();
1035 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
1036 Reference
< XStringResourceManager
> xStringResourceManager
=
1037 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
1039 // Set resource property
1040 if( !xStringResourceManager
.is() || xStringResourceManager
->getLocales().getLength() == 0 )
1043 OUString aDialogName
= pDlgWin
->GetName();
1044 implHandleControlResourceProperties
1045 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
1046 xSourceStringResolver
, MOVE_RESOURCES
);
1049 void LocalizationMgr::copyResourceForDroppedDialog( const Reference
< container::XNameContainer
>& xDialogModel
,
1050 const OUString
& aDialogName
, const Reference
< XStringResourceManager
>& xStringResourceManager
,
1051 const Reference
< XStringResourceResolver
>& xSourceStringResolver
)
1053 if( !xStringResourceManager
.is() )
1056 // Dialog as control
1058 aDialogCtrl
<<= xDialogModel
;
1059 implHandleControlResourceProperties( aDialogCtrl
, aDialogName
,
1060 OUString(), xStringResourceManager
, xSourceStringResolver
, MOVE_RESOURCES
);
1062 // Handle all controls
1063 Sequence
< OUString
> aNames
= xDialogModel
->getElementNames();
1064 const OUString
* pNames
= aNames
.getConstArray();
1065 sal_Int32 nCtrls
= aNames
.getLength();
1066 for( sal_Int32 i
= 0 ; i
< nCtrls
; ++i
)
1068 OUString
aCtrlName( pNames
[i
] );
1069 Any aCtrl
= xDialogModel
->getByName( aCtrlName
);
1070 implHandleControlResourceProperties( aCtrl
, aDialogName
,
1071 aCtrlName
, xStringResourceManager
, xSourceStringResolver
, MOVE_RESOURCES
);
1075 void LocalizationMgr::copyResourceForDialog(
1076 const Reference
< container::XNameContainer
>& xDialogModel
,
1077 const Reference
< XStringResourceResolver
>& xSourceStringResolver
,
1078 const Reference
< XStringResourceManager
>& xTargetStringResourceManager
)
1080 if( !xDialogModel
.is() || !xSourceStringResolver
.is() || !xTargetStringResourceManager
.is() )
1083 OUString aDummyName
;
1085 aDialogCtrl
<<= xDialogModel
;
1086 implHandleControlResourceProperties
1087 ( aDialogCtrl
, aDummyName
, aDummyName
, xTargetStringResourceManager
,
1088 xSourceStringResolver
, COPY_RESOURCES
);
1090 // Handle all controls
1091 Sequence
< OUString
> aNames
= xDialogModel
->getElementNames();
1092 const OUString
* pNames
= aNames
.getConstArray();
1093 sal_Int32 nCtrls
= aNames
.getLength();
1094 for( sal_Int32 i
= 0 ; i
< nCtrls
; ++i
)
1096 OUString
aCtrlName( pNames
[i
] );
1097 Any aCtrl
= xDialogModel
->getByName( aCtrlName
);
1098 implHandleControlResourceProperties( aCtrl
, aDummyName
, aDummyName
,
1099 xTargetStringResourceManager
, xSourceStringResolver
, COPY_RESOURCES
);
1103 Reference
< XStringResourceManager
> LocalizationMgr::getStringResourceFromDialogLibrary
1104 ( const Reference
< container::XNameContainer
>& xDialogLib
)
1106 Reference
< XStringResourceManager
> xStringResourceManager
;
1107 if( xDialogLib
.is() )
1109 Reference
< resource::XStringResourceSupplier
> xStringResourceSupplier( xDialogLib
, UNO_QUERY
);
1110 if( xStringResourceSupplier
.is() )
1112 Reference
< resource::XStringResourceResolver
>
1113 xStringResourceResolver
= xStringResourceSupplier
->getStringResource();
1115 xStringResourceManager
=
1116 Reference
< resource::XStringResourceManager
>( xStringResourceResolver
, UNO_QUERY
);
1119 return xStringResourceManager
;
1122 } // namespace basctl
1124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */