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/XStringResourceSupplier.hpp>
30 #include <sfx2/dispatch.hxx>
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::beans
;
39 using namespace ::com::sun::star::resource
;
44 OUString
const aDot(".");
45 OUString
const aEsc("&");
46 OUString
const aSemi(";");
50 LocalizationMgr::LocalizationMgr(
52 ScriptDocument
const& rDocument
,
53 OUString
const& aLibName
,
54 Reference
<XStringResourceManager
> const& xStringResourceManager
56 m_xStringResourceManager(xStringResourceManager
),
58 m_aDocument(rDocument
),
62 bool LocalizationMgr::isLibraryLocalized ()
64 if (m_xStringResourceManager
.is())
65 return m_xStringResourceManager
->getLocales().getLength() > 0;
69 void LocalizationMgr::handleTranslationbar ()
71 static const char aLayoutManagerName
[] = "LayoutManager";
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
< ::com::sun::star::frame::XLayoutManager
> xLayoutManager
;
79 uno::Any a
= xFrameProps
->getPropertyValue( aLayoutManagerName
);
81 if ( xLayoutManager
.is() )
83 if ( !isLibraryLocalized() )
85 xLayoutManager
->destroyElement( aToolBarResName
);
89 xLayoutManager
->createElement( aToolBarResName
);
90 xLayoutManager
->requestElement( aToolBarResName
);
97 // TODO: -> export from toolkit
105 bool isLanguageDependentProperty( const OUString
& aName
)
110 sal_Int32 nNameLength
;
118 { "CurrencySymbol", 14 },
119 { "StringItemList", 14 },
123 for (Prop
const* pProp
= vProp
; pProp
->sName
; ++pProp
)
124 if (aName
.equalsAsciiL(pProp
->sName
, pProp
->nNameLength
))
130 void LocalizationMgr::implEnableDisableResourceForAllLibraryDialogs( HandleResourceMode eMode
)
132 Sequence
< OUString
> aDlgNames
= m_aDocument
.getObjectNames( E_DIALOGS
, m_aLibName
);
133 sal_Int32 nDlgCount
= aDlgNames
.getLength();
134 const OUString
* pDlgNames
= aDlgNames
.getConstArray();
136 Reference
< XStringResourceResolver
> xDummyStringResolver
;
137 for( sal_Int32 i
= 0 ; i
< nDlgCount
; i
++ )
139 OUString aDlgName
= pDlgNames
[ i
];
140 if (DialogWindow
* pWin
= m_pShell
->FindDlgWin(m_aDocument
, m_aLibName
, aDlgName
))
142 Reference
< container::XNameContainer
> xDialog
= pWin
->GetDialog();
145 // Handle dialog itself as control
147 aDialogCtrl
<<= xDialog
;
148 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
149 OUString(), m_xStringResourceManager
, xDummyStringResolver
, eMode
);
151 // Handle all controls
152 Sequence
< OUString
> aNames
= xDialog
->getElementNames();
153 const OUString
* pNames
= aNames
.getConstArray();
154 sal_Int32 nCtrls
= aNames
.getLength();
155 for( sal_Int32 j
= 0 ; j
< nCtrls
; ++j
)
157 OUString
aCtrlName( pNames
[j
] );
158 Any aCtrl
= xDialog
->getByName( aCtrlName
);
159 implHandleControlResourceProperties( aCtrl
, aDlgName
,
160 aCtrlName
, m_xStringResourceManager
, xDummyStringResolver
, eMode
);
168 OUString implCreatePureResourceId
169 ( const OUString
& aDialogName
, const OUString
& aCtrlName
,
170 const OUString
& aPropName
,
171 Reference
< XStringResourceManager
> xStringResourceManager
)
173 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
174 OUString aPureIdStr
= OUString::number( nUniqueId
);
176 aPureIdStr
+= aDialogName
;
178 if( !aCtrlName
.isEmpty() )
180 aPureIdStr
+= aCtrlName
;
183 aPureIdStr
+= aPropName
;
187 // Works on xStringResourceManager's current language for SET_IDS/RESET_IDS,
188 // anyway only one language should exist when calling this method then,
189 // either the first one for mode SET_IDS or the last one for mode RESET_IDS
190 sal_Int32
LocalizationMgr::implHandleControlResourceProperties
191 (const Any
& rControlAny
, const OUString
& aDialogName
, const OUString
& aCtrlName
,
192 Reference
< XStringResourceManager
> xStringResourceManager
,
193 Reference
< XStringResourceResolver
> xSourceStringResolver
, HandleResourceMode eMode
)
195 sal_Int32 nChangedCount
= 0;
197 Reference
< XPropertySet
> xPropertySet
;
198 rControlAny
>>= xPropertySet
;
199 if( xPropertySet
.is() && xStringResourceManager
.is())
201 Sequence
< Locale
> aLocaleSeq
= xStringResourceManager
->getLocales();
202 sal_Int32 nLocaleCount
= aLocaleSeq
.getLength();
203 if( nLocaleCount
== 0 )
206 Reference
< XPropertySetInfo
> xPropertySetInfo
= xPropertySet
->getPropertySetInfo();
207 if( xPropertySetInfo
.is() )
209 // get sequence of control properties
210 Sequence
< Property
> aPropSeq
= xPropertySetInfo
->getProperties();
211 const Property
* pProps
= aPropSeq
.getConstArray();
212 sal_Int32 nCtrlProps
= aPropSeq
.getLength();
214 // create a map of tab indices and control names, sorted by tab index
215 for( sal_Int32 j
= 0 ; j
< nCtrlProps
; ++j
)
217 const Property
& rProp
= pProps
[j
];
218 OUString aPropName
= rProp
.Name
;
219 TypeClass eType
= rProp
.Type
.getTypeClass();
220 bool bLanguageDependentProperty
=
221 (eType
== TypeClass_STRING
|| eType
== TypeClass_SEQUENCE
)
222 && isLanguageDependentProperty( aPropName
);
223 if( !bLanguageDependentProperty
)
226 if( eType
== TypeClass_STRING
)
228 Any aPropAny
= xPropertySet
->getPropertyValue( aPropName
);
230 aPropAny
>>= aPropStr
;
232 // Replace string by id, add id+string to StringResource
233 if( eMode
== SET_IDS
)
235 bool bEscAlreadyExisting
= aPropStr
.startsWith("&");
236 if( bEscAlreadyExisting
)
239 OUString aPureIdStr
= implCreatePureResourceId
240 ( aDialogName
, aCtrlName
, aPropName
, xStringResourceManager
);
242 // Set Id for all locales
243 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
244 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
246 const Locale
& rLocale
= pLocales
[ i
];
247 xStringResourceManager
->setStringForLocale( aPureIdStr
, aPropStr
, rLocale
);
250 OUString aPropIdStr
= aEsc
;
251 aPropIdStr
+= aPureIdStr
;
252 // TODO?: Change here and in toolkit
254 aPropAny
<<= aPropIdStr
;
255 xPropertySet
->setPropertyValue( aPropName
, aPropAny
);
257 // Replace id by string from StringResource
258 else if( eMode
== RESET_IDS
)
260 if( aPropStr
.getLength() > 1 )
262 OUString aPureIdStr
= aPropStr
.copy( 1 );
263 OUString aNewPropStr
= aPropStr
;
266 aNewPropStr
= xStringResourceManager
->resolveString( aPureIdStr
);
268 catch(const MissingResourceException
&)
271 aPropAny
<<= aNewPropStr
;
272 xPropertySet
->setPropertyValue( aPropName
, aPropAny
);
275 // Remove Id for all locales
276 else if( eMode
== REMOVE_IDS_FROM_RESOURCE
)
278 if( aPropStr
.getLength() > 1 )
280 OUString aPureIdStr
= aPropStr
.copy( 1 );
282 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
283 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
285 const Locale
& rLocale
= pLocales
[ i
];
288 xStringResourceManager
->removeIdForLocale( aPureIdStr
, rLocale
);
290 catch(const MissingResourceException
&)
296 // Rename resource id
297 else if( eMode
== RENAME_DIALOG_IDS
|| eMode
== RENAME_CONTROL_IDS
)
299 OUString aSourceIdStr
= aPropStr
;
300 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
302 OUString aPureIdStr
= implCreatePureResourceId
303 ( aDialogName
, aCtrlName
, aPropName
, xStringResourceManager
);
305 // Set new Id and remove old one for all locales
306 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
307 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
309 const Locale
& rLocale
= pLocales
[ i
];
313 aResStr
= xStringResourceManager
->resolveStringForLocale
314 ( aPureSourceIdStr
, rLocale
);
315 xStringResourceManager
->removeIdForLocale( aPureSourceIdStr
, rLocale
);
316 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
318 catch(const MissingResourceException
&)
322 OUString aPropIdStr
= aEsc
;
323 aPropIdStr
+= aPureIdStr
;
324 // TODO?: Change here and in toolkit
326 aPropAny
<<= aPropIdStr
;
327 xPropertySet
->setPropertyValue( aPropName
, aPropAny
);
329 // Replace string by string from source StringResourceResolver
330 else if( eMode
== MOVE_RESOURCES
&& xSourceStringResolver
.is() )
332 OUString aSourceIdStr
= aPropStr
;
333 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
335 OUString aPureIdStr
= implCreatePureResourceId
336 ( aDialogName
, aCtrlName
, aPropName
, xStringResourceManager
);
338 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
340 // Set Id for all locales
341 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
342 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
344 const Locale
& rLocale
= pLocales
[ i
];
348 aResStr
= xSourceStringResolver
->resolveStringForLocale
349 ( aPureSourceIdStr
, rLocale
);
351 catch(const MissingResourceException
&)
353 aResStr
= xSourceStringResolver
->resolveStringForLocale
354 ( aPureSourceIdStr
, rDefaultLocale
);
356 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
359 OUString aPropIdStr
= aEsc
;
360 aPropIdStr
+= aPureIdStr
;
361 // TODO?: Change here and in toolkit
363 aPropAny
<<= aPropIdStr
;
364 xPropertySet
->setPropertyValue( aPropName
, aPropAny
);
366 // Copy string from source to target resource
367 else if( eMode
== COPY_RESOURCES
&& xSourceStringResolver
.is() )
369 OUString aSourceIdStr
= aPropStr
;
370 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
372 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
374 // Copy Id for all locales
375 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
376 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
378 const Locale
& rLocale
= pLocales
[ i
];
382 aResStr
= xSourceStringResolver
->resolveStringForLocale
383 ( aPureSourceIdStr
, rLocale
);
385 catch(const MissingResourceException
&)
387 aResStr
= xSourceStringResolver
->resolveStringForLocale
388 ( aPureSourceIdStr
, rDefaultLocale
);
390 xStringResourceManager
->setStringForLocale( aPureSourceIdStr
, aResStr
, rLocale
);
396 // Listbox / Combobox
397 else if( eType
== TypeClass_SEQUENCE
)
399 Any aPropAny
= xPropertySet
->getPropertyValue( aPropName
);
400 Sequence
< OUString
> aPropStrings
;
401 aPropAny
>>= aPropStrings
;
403 const OUString
* pPropStrings
= aPropStrings
.getConstArray();
404 sal_Int32 nPropStringCount
= aPropStrings
.getLength();
405 if( nPropStringCount
== 0 )
408 // Replace string by id, add id+string to StringResource
409 if( eMode
== SET_IDS
)
411 Sequence
< OUString
> aIdStrings
;
412 aIdStrings
.realloc( nPropStringCount
);
413 OUString
* pIdStrings
= aIdStrings
.getArray();
415 OUString aIdStrBase
= aDot
;
416 aIdStrBase
+= aCtrlName
;
418 aIdStrBase
+= aPropName
;
420 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
422 for ( i
= 0; i
< nPropStringCount
; ++i
)
424 OUString aPropStr
= pPropStrings
[i
];
425 bool bEscAlreadyExisting
= aPropStr
.startsWith("&");
426 if( bEscAlreadyExisting
)
428 pIdStrings
[i
] = aPropStr
;
432 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
433 OUString aPureIdStr
= OUString::number( nUniqueId
);
434 aPureIdStr
+= aIdStrBase
;
436 // Set Id for all locales
437 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
439 const Locale
& rLocale
= pLocales
[ iLocale
];
440 xStringResourceManager
->setStringForLocale( aPureIdStr
, aPropStr
, rLocale
);
443 OUString aPropIdStr
= aEsc
;
444 aPropIdStr
+= aPureIdStr
;
445 pIdStrings
[i
] = aPropIdStr
;
447 aPropAny
<<= aIdStrings
;
448 xPropertySet
->setPropertyValue( aPropName
, aPropAny
);
450 // Replace id by string from StringResource
451 else if( eMode
== RESET_IDS
)
453 Sequence
< OUString
> aNewPropStrings
;
454 aNewPropStrings
.realloc( nPropStringCount
);
455 OUString
* pNewPropStrings
= aNewPropStrings
.getArray();
458 for ( i
= 0; i
< nPropStringCount
; ++i
)
460 OUString aIdStr
= pPropStrings
[i
];
461 OUString aNewPropStr
= aIdStr
;
462 if( aIdStr
.getLength() > 1 )
464 OUString aPureIdStr
= aIdStr
.copy( 1 );
467 aNewPropStr
= xStringResourceManager
->resolveString( aPureIdStr
);
469 catch(const MissingResourceException
&)
473 pNewPropStrings
[i
] = aNewPropStr
;
475 aPropAny
<<= aNewPropStrings
;
476 xPropertySet
->setPropertyValue( aPropName
, aPropAny
);
478 // Remove Id for all locales
479 else if( eMode
== REMOVE_IDS_FROM_RESOURCE
)
481 Sequence
< OUString
> aNewPropStrings
;
482 aNewPropStrings
.realloc( nPropStringCount
);
484 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
486 for ( i
= 0; i
< nPropStringCount
; ++i
)
488 OUString aIdStr
= pPropStrings
[i
];
489 if( aIdStr
.getLength() > 1 )
491 OUString aPureIdStr
= aIdStr
.copy( 1 );
493 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
495 const Locale
& rLocale
= pLocales
[iLocale
];
498 xStringResourceManager
->removeIdForLocale( aPureIdStr
, rLocale
);
500 catch(const MissingResourceException
&)
507 // Rename resource id
508 else if( eMode
== RENAME_CONTROL_IDS
)
510 Sequence
< OUString
> aIdStrings
;
511 aIdStrings
.realloc( nPropStringCount
);
512 OUString
* pIdStrings
= aIdStrings
.getArray();
514 OUString aIdStrBase
= aDot
;
515 aIdStrBase
+= aCtrlName
;
517 aIdStrBase
+= aPropName
;
519 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
521 for ( i
= 0; i
< nPropStringCount
; ++i
)
523 OUString aSourceIdStr
= pPropStrings
[i
];
524 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
526 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
527 OUString aPureIdStr
= OUString::number( nUniqueId
);
528 aPureIdStr
+= aIdStrBase
;
530 // Set Id for all locales
531 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
533 const Locale
& rLocale
= pLocales
[ iLocale
];
538 aResStr
= xStringResourceManager
->resolveStringForLocale
539 ( aPureSourceIdStr
, rLocale
);
540 xStringResourceManager
->removeIdForLocale( aPureSourceIdStr
, rLocale
);
541 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
543 catch(const MissingResourceException
&)
547 OUString aPropIdStr
= aEsc
;
548 aPropIdStr
+= aPureIdStr
;
549 pIdStrings
[i
] = aPropIdStr
;
551 aPropAny
<<= aIdStrings
;
552 xPropertySet
->setPropertyValue( aPropName
, aPropAny
);
554 // Replace string by string from source StringResourceResolver
555 else if( eMode
== MOVE_RESOURCES
&& xSourceStringResolver
.is() )
557 Sequence
< OUString
> aIdStrings
;
558 aIdStrings
.realloc( nPropStringCount
);
559 OUString
* pIdStrings
= aIdStrings
.getArray();
561 OUString aIdStrBase
= aDot
;
562 aIdStrBase
+= aCtrlName
;
564 aIdStrBase
+= aPropName
;
566 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
568 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
570 for ( i
= 0; i
< nPropStringCount
; ++i
)
572 OUString aSourceIdStr
= pPropStrings
[i
];
573 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
575 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
576 OUString aPureIdStr
= OUString::number( nUniqueId
);
577 aPureIdStr
+= aIdStrBase
;
579 // Set Id for all locales
580 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
582 const Locale
& rLocale
= pLocales
[ iLocale
];
587 aResStr
= xSourceStringResolver
->resolveStringForLocale
588 ( aPureSourceIdStr
, rLocale
);
590 catch(const MissingResourceException
&)
592 aResStr
= xSourceStringResolver
->resolveStringForLocale
593 ( aPureSourceIdStr
, rDefaultLocale
);
595 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
598 OUString aPropIdStr
= aEsc
;
599 aPropIdStr
+= aPureIdStr
;
600 pIdStrings
[i
] = aPropIdStr
;
602 aPropAny
<<= aIdStrings
;
603 xPropertySet
->setPropertyValue( aPropName
, aPropAny
);
605 // Copy string from source to target resource
606 else if( eMode
== COPY_RESOURCES
&& xSourceStringResolver
.is() )
608 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
610 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
612 for ( i
= 0; i
< nPropStringCount
; ++i
)
614 OUString aSourceIdStr
= pPropStrings
[i
];
615 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
617 // Set Id for all locales
618 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
620 const Locale
& rLocale
= pLocales
[ iLocale
];
625 aResStr
= xSourceStringResolver
->resolveStringForLocale
626 ( aPureSourceIdStr
, rLocale
);
628 catch(const MissingResourceException
&)
630 aResStr
= xSourceStringResolver
->resolveStringForLocale
631 ( aPureSourceIdStr
, rDefaultLocale
);
633 xStringResourceManager
->setStringForLocale( aPureSourceIdStr
, aResStr
, rLocale
);
642 return nChangedCount
;
646 void LocalizationMgr::handleAddLocales( const Sequence
< Locale
>& aLocaleSeq
)
648 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
649 sal_Int32 nLocaleCount
= aLocaleSeq
.getLength();
651 if( isLibraryLocalized() )
653 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
655 const Locale
& rLocale
= pLocales
[ i
];
656 m_xStringResourceManager
->newLocale( rLocale
);
661 DBG_ASSERT( nLocaleCount
==1, "LocalizationMgr::handleAddLocales(): Only one first locale allowed" );
663 const Locale
& rLocale
= pLocales
[ 0 ];
664 m_xStringResourceManager
->newLocale( rLocale
);
665 enableResourceForAllLibraryDialogs();
668 MarkDocumentModified( m_aDocument
);
670 // update locale toolbar
671 if (SfxBindings
* pBindings
= GetBindingsPtr())
672 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
674 handleTranslationbar();
678 void LocalizationMgr::handleRemoveLocales( const Sequence
< Locale
>& aLocaleSeq
)
680 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
681 sal_Int32 nLocaleCount
= aLocaleSeq
.getLength();
682 bool bConsistant
= true;
683 bool bModified
= false;
685 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
687 const Locale
& rLocale
= pLocales
[ i
];
690 // Check if last locale
691 Sequence
< Locale
> aResLocaleSeq
= m_xStringResourceManager
->getLocales();
692 if( aResLocaleSeq
.getLength() == 1 )
694 const Locale
& rLastResLocale
= aResLocaleSeq
.getConstArray()[ 0 ];
695 if( localesAreEqual( rLocale
, rLastResLocale
) )
697 disableResourceForAllLibraryDialogs();
701 // Inconsistancy, keep last locale
711 m_xStringResourceManager
->removeLocale( rLocale
);
714 catch(const IllegalArgumentException
&)
722 MarkDocumentModified( m_aDocument
);
725 if (SfxBindings
* pBindings
= GetBindingsPtr())
727 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
728 pBindings
->Invalidate( SID_BASICIDE_MANAGE_LANG
);
731 handleTranslationbar();
734 DBG_ASSERT( bConsistant
,
735 "LocalizationMgr::handleRemoveLocales(): sequence contains unsupported locales" );
739 void LocalizationMgr::handleSetDefaultLocale(const Locale
& rLocale
)
741 if( m_xStringResourceManager
.is() )
745 m_xStringResourceManager
->setDefaultLocale(rLocale
);
747 catch(const IllegalArgumentException
&)
749 OSL_FAIL( "LocalizationMgr::handleSetDefaultLocale: Invalid locale" );
752 // update locale toolbar
753 if (SfxBindings
* pBindings
= GetBindingsPtr())
754 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
758 void LocalizationMgr::handleSetCurrentLocale(const css::lang::Locale
& rLocale
)
760 if( m_xStringResourceManager
.is() )
764 m_xStringResourceManager
->setCurrentLocale(rLocale
, false);
766 catch(const IllegalArgumentException
&)
768 OSL_FAIL( "LocalizationMgr::handleSetCurrentLocale: Invalid locale" );
771 // update locale toolbar
772 if (SfxBindings
* pBindings
= GetBindingsPtr())
773 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
775 if (DialogWindow
* pDlgWin
= dynamic_cast<DialogWindow
*>(m_pShell
->GetCurWindow()))
776 if (!pDlgWin
->IsSuspended())
777 pDlgWin
->GetEditor().UpdatePropertyBrowserDelayed();
781 void LocalizationMgr::handleBasicStarted()
783 if( m_xStringResourceManager
.is() )
784 m_aLocaleBeforeBasicStart
= m_xStringResourceManager
->getCurrentLocale();
787 void LocalizationMgr::handleBasicStopped()
791 if( m_xStringResourceManager
.is() )
792 m_xStringResourceManager
->setCurrentLocale( m_aLocaleBeforeBasicStart
, true );
794 catch(const IllegalArgumentException
&)
800 DialogWindow
* FindDialogWindowForEditor( DlgEditor
* pEditor
)
802 Shell::WindowTable
const& aWindowTable
= GetShell()->GetWindowTable();
803 for (Shell::WindowTableIt it
= aWindowTable
.begin(); it
!= aWindowTable
.end(); ++it
)
805 BaseWindow
* pWin
= it
->second
;
806 if (!pWin
->IsSuspended())
807 if (DialogWindow
* pDlgWin
= dynamic_cast<DialogWindow
*>(pWin
))
809 if (&pDlgWin
->GetEditor() == pEditor
)
817 void LocalizationMgr::setControlResourceIDsForNewEditorObject( DlgEditor
* pEditor
,
818 const Any
& rControlAny
, const OUString
& aCtrlName
)
820 // Get library for DlgEditor
821 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
824 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
825 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::setControlResourceIDsForNewEditorObject: invalid document!" );
826 if ( !aDocument
.isValid() )
828 const OUString
& rLibName
= pDlgWin
->GetLibName();
829 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
830 Reference
< XStringResourceManager
> xStringResourceManager
=
831 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
833 // Set resource property
834 if( !xStringResourceManager
.is() || xStringResourceManager
->getLocales().getLength() == 0 )
837 OUString aDialogName
= pDlgWin
->GetName();
838 Reference
< XStringResourceResolver
> xDummyStringResolver
;
839 sal_Int32 nChangedCount
= implHandleControlResourceProperties
840 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
841 xDummyStringResolver
, SET_IDS
);
844 MarkDocumentModified( aDocument
);
847 void LocalizationMgr::renameControlResourceIDsForEditorObject( DlgEditor
* pEditor
,
848 const css::uno::Any
& rControlAny
, const OUString
& aNewCtrlName
)
850 // Get library for DlgEditor
851 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
854 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
855 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::renameControlResourceIDsForEditorObject: invalid document!" );
856 if ( !aDocument
.isValid() )
858 const OUString
& rLibName
= pDlgWin
->GetLibName();
859 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
860 Reference
< XStringResourceManager
> xStringResourceManager
=
861 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
863 // Set resource property
864 if( !xStringResourceManager
.is() || xStringResourceManager
->getLocales().getLength() == 0 )
867 OUString aDialogName
= pDlgWin
->GetName();
868 Reference
< XStringResourceResolver
> xDummyStringResolver
;
869 implHandleControlResourceProperties
870 ( rControlAny
, aDialogName
, aNewCtrlName
, xStringResourceManager
,
871 xDummyStringResolver
, RENAME_CONTROL_IDS
);
875 void LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( DlgEditor
* pEditor
,
876 const Any
& rControlAny
, const OUString
& aCtrlName
)
878 // Get library for DlgEditor
879 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
882 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
883 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject: invalid document!" );
884 if ( !aDocument
.isValid() )
886 const OUString
& rLibName
= pDlgWin
->GetLibName();
887 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
888 Reference
< XStringResourceManager
> xStringResourceManager
=
889 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
891 OUString aDialogName
= pDlgWin
->GetName();
892 Reference
< XStringResourceResolver
> xDummyStringResolver
;
893 sal_Int32 nChangedCount
= implHandleControlResourceProperties
894 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
895 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
898 MarkDocumentModified( aDocument
);
901 void LocalizationMgr::setStringResourceAtDialog( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
902 const OUString
& aDlgName
, Reference
< container::XNameContainer
> xDialogModel
)
904 static const char aResourceResolverPropName
[] = "ResourceResolver";
907 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
908 Reference
< XStringResourceManager
> xStringResourceManager
=
909 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
911 // Set resource property
912 if( xStringResourceManager
.is() )
914 // Not very elegant as dialog may or may not be localized yet
915 // TODO: Find better place, where dialog is created
916 if( xStringResourceManager
->getLocales().getLength() > 0 )
919 aDialogCtrl
<<= xDialogModel
;
920 Reference
< XStringResourceResolver
> xDummyStringResolver
;
921 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
922 OUString(), xStringResourceManager
,
923 xDummyStringResolver
, SET_IDS
);
926 Reference
< beans::XPropertySet
> xDlgPSet( xDialogModel
, UNO_QUERY
);
927 Any aStringResourceManagerAny
;
928 aStringResourceManagerAny
<<= xStringResourceManager
;
929 xDlgPSet
->setPropertyValue( aResourceResolverPropName
, aStringResourceManagerAny
);
933 void LocalizationMgr::renameStringResourceIDs( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
934 const OUString
& aDlgName
, Reference
< container::XNameContainer
> xDialogModel
)
937 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
938 Reference
< XStringResourceManager
> xStringResourceManager
=
939 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
940 if( !xStringResourceManager
.is() )
944 aDialogCtrl
<<= xDialogModel
;
945 Reference
< XStringResourceResolver
> xDummyStringResolver
;
946 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
947 OUString(), xStringResourceManager
,
948 xDummyStringResolver
, RENAME_DIALOG_IDS
);
950 // Handle all controls
951 Sequence
< OUString
> aNames
= xDialogModel
->getElementNames();
952 const OUString
* pNames
= aNames
.getConstArray();
953 sal_Int32 nCtrls
= aNames
.getLength();
954 for( sal_Int32 i
= 0 ; i
< nCtrls
; ++i
)
956 OUString
aCtrlName( pNames
[i
] );
957 Any aCtrl
= xDialogModel
->getByName( aCtrlName
);
958 implHandleControlResourceProperties( aCtrl
, aDlgName
,
959 aCtrlName
, xStringResourceManager
,
960 xDummyStringResolver
, RENAME_DIALOG_IDS
);
964 void LocalizationMgr::removeResourceForDialog( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
965 const OUString
& aDlgName
, Reference
< container::XNameContainer
> xDialogModel
)
968 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
969 Reference
< XStringResourceManager
> xStringResourceManager
=
970 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
971 if( !xStringResourceManager
.is() )
975 aDialogCtrl
<<= xDialogModel
;
976 Reference
< XStringResourceResolver
> xDummyStringResolver
;
977 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
978 OUString(), xStringResourceManager
,
979 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
981 // Handle all controls
982 Sequence
< OUString
> aNames
= xDialogModel
->getElementNames();
983 const OUString
* pNames
= aNames
.getConstArray();
984 sal_Int32 nCtrls
= aNames
.getLength();
985 for( sal_Int32 i
= 0 ; i
< nCtrls
; ++i
)
987 OUString
aCtrlName( pNames
[i
] );
988 Any aCtrl
= xDialogModel
->getByName( aCtrlName
);
989 implHandleControlResourceProperties( aCtrl
, aDlgName
,
990 aCtrlName
, xStringResourceManager
,
991 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
995 void LocalizationMgr::resetResourceForDialog( Reference
< container::XNameContainer
> xDialogModel
,
996 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
, RESET_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
, RESET_IDS
);
1022 void LocalizationMgr::setResourceIDsForDialog( Reference
< container::XNameContainer
> xDialogModel
,
1023 Reference
< XStringResourceManager
> xStringResourceManager
)
1025 if( !xStringResourceManager
.is() )
1028 // Dialog as control
1029 OUString aDummyName
;
1031 aDialogCtrl
<<= xDialogModel
;
1032 Reference
< XStringResourceResolver
> xDummyStringResolver
;
1033 implHandleControlResourceProperties( aDialogCtrl
, aDummyName
,
1034 aDummyName
, xStringResourceManager
, xDummyStringResolver
, SET_IDS
);
1036 // Handle all controls
1037 Sequence
< OUString
> aNames
= xDialogModel
->getElementNames();
1038 const OUString
* pNames
= aNames
.getConstArray();
1039 sal_Int32 nCtrls
= aNames
.getLength();
1040 for( sal_Int32 i
= 0 ; i
< nCtrls
; ++i
)
1042 OUString
aCtrlName( pNames
[i
] );
1043 Any aCtrl
= xDialogModel
->getByName( aCtrlName
);
1044 implHandleControlResourceProperties( aCtrl
, aDummyName
,
1045 aCtrlName
, xStringResourceManager
, xDummyStringResolver
, SET_IDS
);
1049 void LocalizationMgr::copyResourcesForPastedEditorObject( DlgEditor
* pEditor
,
1050 const Any
& rControlAny
, const OUString
& aCtrlName
,
1051 Reference
< XStringResourceResolver
> xSourceStringResolver
)
1053 // Get library for DlgEditor
1054 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
1057 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
1058 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::copyResourcesForPastedEditorObject: invalid document!" );
1059 if ( !aDocument
.isValid() )
1061 const OUString
& rLibName
= pDlgWin
->GetLibName();
1062 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
1063 Reference
< XStringResourceManager
> xStringResourceManager
=
1064 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
1066 // Set resource property
1067 if( !xStringResourceManager
.is() || xStringResourceManager
->getLocales().getLength() == 0 )
1070 OUString aDialogName
= pDlgWin
->GetName();
1071 implHandleControlResourceProperties
1072 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
1073 xSourceStringResolver
, MOVE_RESOURCES
);
1076 void LocalizationMgr::copyResourceForDroppedDialog( Reference
< container::XNameContainer
> xDialogModel
,
1077 const OUString
& aDialogName
, Reference
< XStringResourceManager
> xStringResourceManager
,
1078 Reference
< XStringResourceResolver
> xSourceStringResolver
)
1080 if( !xStringResourceManager
.is() )
1083 // Dialog as control
1084 OUString aDummyName
;
1086 aDialogCtrl
<<= xDialogModel
;
1087 implHandleControlResourceProperties( aDialogCtrl
, aDialogName
,
1088 aDummyName
, xStringResourceManager
, xSourceStringResolver
, MOVE_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
, aDialogName
,
1099 aCtrlName
, xStringResourceManager
, xSourceStringResolver
, MOVE_RESOURCES
);
1103 void LocalizationMgr::copyResourceForDialog(
1104 const Reference
< container::XNameContainer
>& xDialogModel
,
1105 const Reference
< XStringResourceResolver
>& xSourceStringResolver
,
1106 const Reference
< XStringResourceManager
>& xTargetStringResourceManager
)
1108 if( !xDialogModel
.is() || !xSourceStringResolver
.is() || !xTargetStringResourceManager
.is() )
1111 OUString aDummyName
;
1113 aDialogCtrl
<<= xDialogModel
;
1114 implHandleControlResourceProperties
1115 ( aDialogCtrl
, aDummyName
, aDummyName
, xTargetStringResourceManager
,
1116 xSourceStringResolver
, COPY_RESOURCES
);
1118 // Handle all controls
1119 Sequence
< OUString
> aNames
= xDialogModel
->getElementNames();
1120 const OUString
* pNames
= aNames
.getConstArray();
1121 sal_Int32 nCtrls
= aNames
.getLength();
1122 for( sal_Int32 i
= 0 ; i
< nCtrls
; ++i
)
1124 OUString
aCtrlName( pNames
[i
] );
1125 Any aCtrl
= xDialogModel
->getByName( aCtrlName
);
1126 implHandleControlResourceProperties( aCtrl
, aDummyName
, aDummyName
,
1127 xTargetStringResourceManager
, xSourceStringResolver
, COPY_RESOURCES
);
1131 Reference
< XStringResourceManager
> LocalizationMgr::getStringResourceFromDialogLibrary
1132 ( Reference
< container::XNameContainer
> xDialogLib
)
1134 Reference
< XStringResourceManager
> xStringResourceManager
;
1135 if( xDialogLib
.is() )
1137 Reference
< resource::XStringResourceSupplier
> xStringResourceSupplier( xDialogLib
, UNO_QUERY
);
1138 if( xStringResourceSupplier
.is() )
1140 Reference
< resource::XStringResourceResolver
>
1141 xStringResourceResolver
= xStringResourceSupplier
->getStringResource();
1143 xStringResourceManager
=
1144 Reference
< resource::XStringResourceManager
>( xStringResourceResolver
, UNO_QUERY
);
1147 return xStringResourceManager
;
1150 } // namespace basctl
1152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */