Update ooo320-m1
[ooovba.git] / basctl / source / basicide / localizationmgr.cxx
blobe9bccc85934a665f6c95949aa52d86489d3bb4d4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: localizationmgr.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_basctl.hxx"
32 #include <ide_pch.hxx>
34 #include <basidesh.hxx>
35 #include <baside3.hxx>
36 #include <basobj.hxx>
37 #include <iderdll.hxx>
38 #include "dlged.hxx"
40 #include <localizationmgr.hxx>
41 #include <com/sun/star/resource/XStringResourceSupplier.hpp>
42 #include <com/sun/star/frame/XLayoutManager.hpp>
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::beans;
48 using namespace ::com::sun::star::resource;
50 static ::rtl::OUString aDot = ::rtl::OUString::createFromAscii( "." );
51 static ::rtl::OUString aEsc = ::rtl::OUString::createFromAscii( "&" );
52 static ::rtl::OUString aSemi = ::rtl::OUString::createFromAscii( ";" );
55 LocalizationMgr::LocalizationMgr( BasicIDEShell* pIDEShell,
56 const ScriptDocument& rDocument, String aLibName,
57 const Reference< XStringResourceManager >& xStringResourceManager )
58 : m_xStringResourceManager( xStringResourceManager )
59 , m_pIDEShell( pIDEShell )
60 , m_aDocument( rDocument )
61 , m_aLibName( aLibName )
65 bool LocalizationMgr::isLibraryLocalized( void )
67 bool bRet = false;
68 if( m_xStringResourceManager.is() )
70 Sequence< Locale > aLocaleSeq = m_xStringResourceManager->getLocales();
71 bRet = ( aLocaleSeq.getLength() > 0 );
73 return bRet;
76 void LocalizationMgr::handleTranslationbar( void )
78 static ::rtl::OUString aLayoutManagerName = ::rtl::OUString::createFromAscii( "LayoutManager" );
79 static ::rtl::OUString aToolBarResName =
80 ::rtl::OUString::createFromAscii( "private:resource/toolbar/translationbar" );
82 Reference< beans::XPropertySet > xFrameProps
83 ( m_pIDEShell->GetViewFrame()->GetFrame()->GetFrameInterface(), uno::UNO_QUERY );
84 if ( xFrameProps.is() )
86 Reference< ::com::sun::star::frame::XLayoutManager > xLayoutManager;
87 uno::Any a = xFrameProps->getPropertyValue( aLayoutManagerName );
88 a >>= xLayoutManager;
89 if ( xLayoutManager.is() )
91 if ( !isLibraryLocalized() )
93 xLayoutManager->destroyElement( aToolBarResName );
95 else
97 xLayoutManager->createElement( aToolBarResName );
98 xLayoutManager->requestElement( aToolBarResName );
105 //============================================
106 // TODO: -> export from toolkit
108 struct LanguageDependentProp
110 const char* pPropName;
111 sal_Int32 nPropNameLength;
114 static LanguageDependentProp aLanguageDependentProp[] =
116 { "Text", 4 },
117 { "Label", 5 },
118 { "Title", 5 },
119 { "HelpText", 8 },
120 { "CurrencySymbol", 14 },
121 { "StringItemList", 14 },
122 { 0, 0 }
125 bool isLanguageDependentProperty( ::rtl::OUString aName )
127 bool bRet = false;
129 LanguageDependentProp* pLangDepProp = aLanguageDependentProp;
130 while( pLangDepProp->pPropName != 0 )
132 if( aName.equalsAsciiL( pLangDepProp->pPropName, pLangDepProp->nPropNameLength ))
134 bRet = true;
135 break;
137 pLangDepProp++;
139 return bRet;
141 //============================================
143 void LocalizationMgr::implEnableDisableResourceForAllLibraryDialogs( HandleResourceMode eMode )
145 Sequence< ::rtl::OUString > aDlgNames = m_aDocument.getObjectNames( E_DIALOGS, m_aLibName );
146 sal_Int32 nDlgCount = aDlgNames.getLength();
147 const ::rtl::OUString* pDlgNames = aDlgNames.getConstArray();
149 Reference< XStringResourceResolver > xDummyStringResolver;
150 for( sal_Int32 i = 0 ; i < nDlgCount ; i++ )
152 String aDlgName = pDlgNames[ i ];
153 DialogWindow* pWin = m_pIDEShell->FindDlgWin( m_aDocument, m_aLibName, aDlgName, FALSE );
154 if( pWin && pWin->IsA( TYPE( DialogWindow ) ) )
156 DialogWindow* pDialogWin = static_cast< DialogWindow* >( pWin );
157 Reference< container::XNameContainer > xDialog = pDialogWin->GetDialog();
158 if( xDialog.is() )
160 // Handle dialog itself as control
161 Any aDialogCtrl;
162 aDialogCtrl <<= xDialog;
163 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
164 ::rtl::OUString(), m_xStringResourceManager, xDummyStringResolver, eMode );
166 // Handle all controls
167 Sequence< ::rtl::OUString > aNames = xDialog->getElementNames();
168 const ::rtl::OUString* pNames = aNames.getConstArray();
169 sal_Int32 nCtrls = aNames.getLength();
170 for( sal_Int32 j = 0 ; j < nCtrls ; ++j )
172 ::rtl::OUString aCtrlName( pNames[j] );
173 Any aCtrl = xDialog->getByName( aCtrlName );
174 implHandleControlResourceProperties( aCtrl, aDlgName,
175 aCtrlName, m_xStringResourceManager, xDummyStringResolver, eMode );
183 ::rtl::OUString implCreatePureResourceId
184 ( const ::rtl::OUString& aDialogName, const ::rtl::OUString& aCtrlName,
185 const ::rtl::OUString& aPropName,
186 Reference< XStringResourceManager > xStringResourceManager )
188 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
189 ::rtl::OUString aPureIdStr = ::rtl::OUString::valueOf( nUniqueId );
190 aPureIdStr += aDot;
191 aPureIdStr += aDialogName;
192 aPureIdStr += aDot;
193 if( aCtrlName.getLength() )
195 aPureIdStr += aCtrlName;
196 aPureIdStr += aDot;
198 aPureIdStr += aPropName;
199 return aPureIdStr;
202 extern bool localesAreEqual( const ::com::sun::star::lang::Locale& rLocaleLeft,
203 const ::com::sun::star::lang::Locale& rLocaleRight );
205 // Works on xStringResourceManager's current language for SET_IDS/RESET_IDS,
206 // anyway only one language should exist when calling this method then,
207 // either the first one for mode SET_IDS or the last one for mode RESET_IDS
208 sal_Int32 LocalizationMgr::implHandleControlResourceProperties
209 ( Any aControlAny, const ::rtl::OUString& aDialogName, const ::rtl::OUString& aCtrlName,
210 Reference< XStringResourceManager > xStringResourceManager,
211 Reference< XStringResourceResolver > xSourceStringResolver, HandleResourceMode eMode )
213 sal_Int32 nChangedCount = 0;
215 Reference< XPropertySet > xPropertySet;
216 aControlAny >>= xPropertySet;
217 if( xPropertySet.is() )
219 Sequence< Locale > aLocaleSeq = xStringResourceManager->getLocales();
220 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
221 if( nLocaleCount == 0 )
222 return 0;
224 Reference< XPropertySetInfo > xPropertySetInfo = xPropertySet->getPropertySetInfo();
225 if( xPropertySetInfo.is() )
227 // get sequence of control properties
228 Sequence< Property > aPropSeq = xPropertySetInfo->getProperties();
229 const Property* pProps = aPropSeq.getConstArray();
230 sal_Int32 nCtrlProps = aPropSeq.getLength();
232 // create a map of tab indices and control names, sorted by tab index
233 for( sal_Int32 j = 0 ; j < nCtrlProps ; ++j )
235 const Property& rProp = pProps[j];
236 ::rtl::OUString aPropName = rProp.Name;
237 TypeClass eType = rProp.Type.getTypeClass();
238 bool bLanguageDependentProperty =
239 (eType == TypeClass_STRING || eType == TypeClass_SEQUENCE)
240 && isLanguageDependentProperty( aPropName );
241 if( !bLanguageDependentProperty )
242 continue;
244 if( eType == TypeClass_STRING )
246 Any aPropAny = xPropertySet->getPropertyValue( aPropName );
247 ::rtl::OUString aPropStr;
248 aPropAny >>= aPropStr;
250 // Replace string by id, add id+string to StringResource
251 if( eMode == SET_IDS )
253 bool bEscAlreadyExisting = (aPropStr.getLength() && aPropStr.getStr()[0] == '&' );
254 if( bEscAlreadyExisting )
255 continue;
257 ::rtl::OUString aPureIdStr = implCreatePureResourceId
258 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
260 // Set Id for all locales
261 const Locale* pLocales = aLocaleSeq.getConstArray();
262 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
264 const Locale& rLocale = pLocales[ i ];
265 xStringResourceManager->setStringForLocale( aPureIdStr, aPropStr, rLocale );
268 ::rtl::OUString aPropIdStr = aEsc;
269 aPropIdStr += aPureIdStr;
270 // TODO?: Change here and in toolkit
271 //aPropIdStr += aSemi;
272 (void)aSemi;
273 aPropAny <<= aPropIdStr;
274 xPropertySet->setPropertyValue( aPropName, aPropAny );
276 // Replace id by string from StringResource
277 else if( eMode == RESET_IDS )
279 if( aPropStr.getLength() > 1 )
281 ::rtl::OUString aPureIdStr = aPropStr.copy( 1 );
282 ::rtl::OUString aNewPropStr = aPropStr;
285 aNewPropStr = xStringResourceManager->resolveString( aPureIdStr );
287 catch(MissingResourceException&)
290 aPropAny <<= aNewPropStr;
291 xPropertySet->setPropertyValue( aPropName, aPropAny );
294 // Remove Id for all locales
295 else if( eMode == REMOVE_IDS_FROM_RESOURCE )
297 if( aPropStr.getLength() > 1 )
299 ::rtl::OUString aPureIdStr = aPropStr.copy( 1 );
301 const Locale* pLocales = aLocaleSeq.getConstArray();
302 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
304 const Locale& rLocale = pLocales[ i ];
307 xStringResourceManager->removeIdForLocale( aPureIdStr, rLocale );
309 catch(MissingResourceException&)
315 // Rename resource id
316 else if( eMode == RENAME_DIALOG_IDS || eMode == RENAME_CONTROL_IDS )
318 ::rtl::OUString aSourceIdStr = aPropStr;
319 ::rtl::OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
321 ::rtl::OUString aPureIdStr = implCreatePureResourceId
322 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
324 // Set new Id and remove old one for all locales
325 const Locale* pLocales = aLocaleSeq.getConstArray();
326 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
328 const Locale& rLocale = pLocales[ i ];
329 ::rtl::OUString aResStr;
332 aResStr = xStringResourceManager->resolveStringForLocale
333 ( aPureSourceIdStr, rLocale );
334 xStringResourceManager->removeIdForLocale( aPureSourceIdStr, rLocale );
335 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
337 catch(MissingResourceException&)
341 ::rtl::OUString aPropIdStr = aEsc;
342 aPropIdStr += aPureIdStr;
343 // TODO?: Change here and in toolkit
344 //aPropIdStr += aSemi;
345 (void)aSemi;
346 aPropAny <<= aPropIdStr;
347 xPropertySet->setPropertyValue( aPropName, aPropAny );
349 // Replace string by string from source StringResourceResolver
350 else if( eMode == MOVE_RESOURCES && xSourceStringResolver.is() )
352 ::rtl::OUString aSourceIdStr = aPropStr;
353 ::rtl::OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
355 ::rtl::OUString aPureIdStr = implCreatePureResourceId
356 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
358 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
360 // Set Id for all locales
361 const Locale* pLocales = aLocaleSeq.getConstArray();
362 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
364 const Locale& rLocale = pLocales[ i ];
365 ::rtl::OUString aResStr;
368 aResStr = xSourceStringResolver->resolveStringForLocale
369 ( aPureSourceIdStr, rLocale );
371 catch(MissingResourceException&)
373 aResStr = xSourceStringResolver->resolveStringForLocale
374 ( aPureSourceIdStr, rDefaultLocale );
376 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
379 ::rtl::OUString aPropIdStr = aEsc;
380 aPropIdStr += aPureIdStr;
381 // TODO?: Change here and in toolkit
382 //aPropIdStr += aSemi;
383 (void)aSemi;
384 aPropAny <<= aPropIdStr;
385 xPropertySet->setPropertyValue( aPropName, aPropAny );
387 // Copy string from source to target resource
388 else if( eMode == COPY_RESOURCES && xSourceStringResolver.is() )
390 ::rtl::OUString aSourceIdStr = aPropStr;
391 ::rtl::OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
393 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
395 // Copy Id for all locales
396 const Locale* pLocales = aLocaleSeq.getConstArray();
397 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
399 const Locale& rLocale = pLocales[ i ];
400 ::rtl::OUString aResStr;
403 aResStr = xSourceStringResolver->resolveStringForLocale
404 ( aPureSourceIdStr, rLocale );
406 catch(MissingResourceException&)
408 aResStr = xSourceStringResolver->resolveStringForLocale
409 ( aPureSourceIdStr, rDefaultLocale );
411 xStringResourceManager->setStringForLocale( aPureSourceIdStr, aResStr, rLocale );
414 nChangedCount++;
417 // Listbox / Combobox
418 else if( eType == TypeClass_SEQUENCE )
420 Any aPropAny = xPropertySet->getPropertyValue( aPropName );
421 Sequence< ::rtl::OUString > aPropStrings;
422 aPropAny >>= aPropStrings;
424 const ::rtl::OUString* pPropStrings = aPropStrings.getConstArray();
425 sal_Int32 nPropStringCount = aPropStrings.getLength();
426 if( nPropStringCount == 0 )
427 continue;
429 // Replace string by id, add id+string to StringResource
430 if( eMode == SET_IDS )
432 Sequence< ::rtl::OUString > aIdStrings;
433 aIdStrings.realloc( nPropStringCount );
434 ::rtl::OUString* pIdStrings = aIdStrings.getArray();
436 ::rtl::OUString aIdStrBase = aDot;
437 aIdStrBase += aCtrlName;
438 aIdStrBase += aDot;
439 aIdStrBase += aPropName;
441 const Locale* pLocales = aLocaleSeq.getConstArray();
442 sal_Int32 i;
443 for ( i = 0; i < nPropStringCount; ++i )
445 ::rtl::OUString aPropStr = pPropStrings[i];
446 bool bEscAlreadyExisting = (aPropStr.getLength() && aPropStr.getStr()[0] == '&' );
447 if( bEscAlreadyExisting )
449 pIdStrings[i] = aPropStr;
450 continue;
453 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
454 ::rtl::OUString aPureIdStr = ::rtl::OUString::valueOf( nUniqueId );
455 aPureIdStr += aIdStrBase;
457 // Set Id for all locales
458 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
460 const Locale& rLocale = pLocales[ iLocale ];
461 xStringResourceManager->setStringForLocale( aPureIdStr, aPropStr, rLocale );
464 ::rtl::OUString aPropIdStr = aEsc;
465 aPropIdStr += aPureIdStr;
466 pIdStrings[i] = aPropIdStr;
468 aPropAny <<= aIdStrings;
469 xPropertySet->setPropertyValue( aPropName, aPropAny );
471 // Replace id by string from StringResource
472 else if( eMode == RESET_IDS )
474 Sequence< ::rtl::OUString > aNewPropStrings;
475 aNewPropStrings.realloc( nPropStringCount );
476 ::rtl::OUString* pNewPropStrings = aNewPropStrings.getArray();
478 sal_Int32 i;
479 for ( i = 0; i < nPropStringCount; ++i )
481 ::rtl::OUString aIdStr = pPropStrings[i];
482 ::rtl::OUString aNewPropStr = aIdStr;
483 if( aIdStr.getLength() > 1 )
485 ::rtl::OUString aPureIdStr = aIdStr.copy( 1 );
488 aNewPropStr = xStringResourceManager->resolveString( aPureIdStr );
490 catch(MissingResourceException&)
494 pNewPropStrings[i] = aNewPropStr;
496 aPropAny <<= aNewPropStrings;
497 xPropertySet->setPropertyValue( aPropName, aPropAny );
499 // Remove Id for all locales
500 else if( eMode == REMOVE_IDS_FROM_RESOURCE )
502 Sequence< ::rtl::OUString > aNewPropStrings;
503 aNewPropStrings.realloc( nPropStringCount );
505 const Locale* pLocales = aLocaleSeq.getConstArray();
506 sal_Int32 i;
507 for ( i = 0; i < nPropStringCount; ++i )
509 ::rtl::OUString aIdStr = pPropStrings[i];
510 if( aIdStr.getLength() > 1 )
512 ::rtl::OUString aPureIdStr = aIdStr.copy( 1 );
514 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
516 const Locale& rLocale = pLocales[iLocale];
519 xStringResourceManager->removeIdForLocale( aPureIdStr, rLocale );
521 catch(MissingResourceException&)
528 // Rename resource id
529 else if( eMode == RENAME_CONTROL_IDS )
531 Sequence< ::rtl::OUString > aIdStrings;
532 aIdStrings.realloc( nPropStringCount );
533 ::rtl::OUString* pIdStrings = aIdStrings.getArray();
535 ::rtl::OUString aIdStrBase = aDot;
536 aIdStrBase += aCtrlName;
537 aIdStrBase += aDot;
538 aIdStrBase += aPropName;
540 const Locale* pLocales = aLocaleSeq.getConstArray();
541 sal_Int32 i;
542 for ( i = 0; i < nPropStringCount; ++i )
544 ::rtl::OUString aSourceIdStr = pPropStrings[i];
545 ::rtl::OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
547 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
548 ::rtl::OUString aPureIdStr = ::rtl::OUString::valueOf( nUniqueId );
549 aPureIdStr += aIdStrBase;
551 // Set Id for all locales
552 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
554 const Locale& rLocale = pLocales[ iLocale ];
556 ::rtl::OUString aResStr;
559 aResStr = xStringResourceManager->resolveStringForLocale
560 ( aPureSourceIdStr, rLocale );
561 xStringResourceManager->removeIdForLocale( aPureSourceIdStr, rLocale );
562 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
564 catch(MissingResourceException&)
568 ::rtl::OUString aPropIdStr = aEsc;
569 aPropIdStr += aPureIdStr;
570 pIdStrings[i] = aPropIdStr;
572 aPropAny <<= aIdStrings;
573 xPropertySet->setPropertyValue( aPropName, aPropAny );
575 // Replace string by string from source StringResourceResolver
576 else if( eMode == MOVE_RESOURCES && xSourceStringResolver.is() )
578 Sequence< ::rtl::OUString > aIdStrings;
579 aIdStrings.realloc( nPropStringCount );
580 ::rtl::OUString* pIdStrings = aIdStrings.getArray();
582 ::rtl::OUString aIdStrBase = aDot;
583 aIdStrBase += aCtrlName;
584 aIdStrBase += aDot;
585 aIdStrBase += aPropName;
587 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
589 const Locale* pLocales = aLocaleSeq.getConstArray();
590 sal_Int32 i;
591 for ( i = 0; i < nPropStringCount; ++i )
593 ::rtl::OUString aSourceIdStr = pPropStrings[i];
594 ::rtl::OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
596 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
597 ::rtl::OUString aPureIdStr = ::rtl::OUString::valueOf( nUniqueId );
598 aPureIdStr += aIdStrBase;
600 // Set Id for all locales
601 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
603 const Locale& rLocale = pLocales[ iLocale ];
605 ::rtl::OUString aResStr;
608 aResStr = xSourceStringResolver->resolveStringForLocale
609 ( aPureSourceIdStr, rLocale );
611 catch(MissingResourceException&)
613 aResStr = xSourceStringResolver->resolveStringForLocale
614 ( aPureSourceIdStr, rDefaultLocale );
616 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
619 ::rtl::OUString aPropIdStr = aEsc;
620 aPropIdStr += aPureIdStr;
621 pIdStrings[i] = aPropIdStr;
623 aPropAny <<= aIdStrings;
624 xPropertySet->setPropertyValue( aPropName, aPropAny );
626 // Copy string from source to target resource
627 else if( eMode == COPY_RESOURCES && xSourceStringResolver.is() )
629 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
631 const Locale* pLocales = aLocaleSeq.getConstArray();
632 sal_Int32 i;
633 for ( i = 0; i < nPropStringCount; ++i )
635 ::rtl::OUString aSourceIdStr = pPropStrings[i];
636 ::rtl::OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
638 // Set Id for all locales
639 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
641 const Locale& rLocale = pLocales[ iLocale ];
643 ::rtl::OUString aResStr;
646 aResStr = xSourceStringResolver->resolveStringForLocale
647 ( aPureSourceIdStr, rLocale );
649 catch(MissingResourceException&)
651 aResStr = xSourceStringResolver->resolveStringForLocale
652 ( aPureSourceIdStr, rDefaultLocale );
654 xStringResourceManager->setStringForLocale( aPureSourceIdStr, aResStr, rLocale );
658 nChangedCount++;
663 return nChangedCount;
667 void TEST_simulateDialogAddRemoveLocale( bool bAdd )
669 Sequence< Locale > aLocaleSeq( 1 );
670 Locale* pLocales = aLocaleSeq.getArray();
672 ::com::sun::star::lang::Locale aLocale_en;
673 aLocale_en.Language = ::rtl::OUString::createFromAscii( "en" );
674 aLocale_en.Country = ::rtl::OUString::createFromAscii( "US" );
676 ::com::sun::star::lang::Locale aLocale_de;
677 aLocale_de.Language = ::rtl::OUString::createFromAscii( "de" );
678 aLocale_de.Country = ::rtl::OUString::createFromAscii( "DE" );
680 ::com::sun::star::lang::Locale aLocale_fr;
681 aLocale_fr.Language = ::rtl::OUString::createFromAscii( "fr" );
682 aLocale_fr.Country = ::rtl::OUString::createFromAscii( "FR" );
684 int n = 0;
685 if( n == 0 )
686 pLocales[0] = aLocale_en;
687 else if( n == 1 )
688 pLocales[0] = aLocale_de;
689 else if( n == 2 )
690 pLocales[0] = aLocale_fr;
692 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
693 LocalizationMgr* pMgr = pIDEShell->GetCurLocalizationMgr();
694 if( bAdd )
695 pMgr->handleAddLocales( aLocaleSeq );
696 else
697 pMgr->handleRemoveLocales( aLocaleSeq );
700 void TEST_simulateDialogAddLocale( void )
702 TEST_simulateDialogAddRemoveLocale( true );
705 void TEST_simulateDialogRemoveLocale( void )
707 TEST_simulateDialogAddRemoveLocale( false );
711 void LocalizationMgr::handleAddLocales( Sequence< Locale > aLocaleSeq )
713 const Locale* pLocales = aLocaleSeq.getConstArray();
714 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
716 if( isLibraryLocalized() )
718 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
720 const Locale& rLocale = pLocales[ i ];
721 m_xStringResourceManager->newLocale( rLocale );
724 else
726 DBG_ASSERT( nLocaleCount==1, "LocalizationMgr::handleAddLocales(): Only one first locale allowed" );
728 const Locale& rLocale = pLocales[ 0 ];
729 m_xStringResourceManager->newLocale( rLocale );
730 enableResourceForAllLibraryDialogs();
733 BasicIDE::MarkDocumentModified( m_aDocument );
735 // update locale toolbar
736 SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
737 if ( pBindings )
738 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
740 handleTranslationbar();
744 void LocalizationMgr::handleRemoveLocales( Sequence< Locale > aLocaleSeq )
746 const Locale* pLocales = aLocaleSeq.getConstArray();
747 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
748 bool bConsistant = true;
749 bool bModified = false;
751 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
753 const Locale& rLocale = pLocales[ i ];
754 bool bRemove = true;
756 // Check if last locale
757 Sequence< Locale > aResLocaleSeq = m_xStringResourceManager->getLocales();
758 if( aResLocaleSeq.getLength() == 1 )
760 const Locale& rLastResLocale = aResLocaleSeq.getConstArray()[ 0 ];
761 if( localesAreEqual( rLocale, rLastResLocale ) )
763 disableResourceForAllLibraryDialogs();
765 else
767 // Inconsistancy, keep last locale
768 bConsistant = false;
769 bRemove = false;
773 if( bRemove )
777 m_xStringResourceManager->removeLocale( rLocale );
778 bModified = true;
780 catch(IllegalArgumentException&)
782 bConsistant = false;
786 if( bModified )
788 BasicIDE::MarkDocumentModified( m_aDocument );
790 // update slots
791 SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
792 if ( pBindings )
794 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
795 pBindings->Invalidate( SID_BASICIDE_MANAGE_LANG );
798 handleTranslationbar();
801 DBG_ASSERT( bConsistant,
802 "LocalizationMgr::handleRemoveLocales(): sequence contains unsupported locales" );
805 void LocalizationMgr::handleSetDefaultLocale( Locale aLocale )
807 if( m_xStringResourceManager.is() )
811 m_xStringResourceManager->setDefaultLocale( aLocale );
813 catch(IllegalArgumentException&)
815 DBG_ERROR( "LocalizationMgr::handleSetDefaultLocale: Invalid locale" );
818 // update locale toolbar
819 SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
820 if ( pBindings )
821 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
825 void LocalizationMgr::handleSetCurrentLocale( ::com::sun::star::lang::Locale aLocale )
827 if( m_xStringResourceManager.is() )
831 m_xStringResourceManager->setCurrentLocale( aLocale, false );
833 catch(IllegalArgumentException&)
835 DBG_ERROR( "LocalizationMgr::handleSetCurrentLocale: Invalid locale" );
838 // update locale toolbar
839 SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
840 if ( pBindings )
841 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
843 IDEBaseWindow* pCurWin = m_pIDEShell->GetCurWindow();
844 if ( pCurWin && !pCurWin->IsSuspended() && pCurWin->IsA( TYPE( DialogWindow ) ) )
846 DialogWindow* pDlgWin = (DialogWindow*)pCurWin;
847 DlgEditor* pWinEditor = pDlgWin->GetEditor();
848 if( pWinEditor )
849 pWinEditor->UpdatePropertyBrowserDelayed();
854 void LocalizationMgr::handleBasicStarted( void )
856 if( m_xStringResourceManager.is() )
857 m_aLocaleBeforeBasicStart = m_xStringResourceManager->getCurrentLocale();
860 void LocalizationMgr::handleBasicStopped( void )
864 if( m_xStringResourceManager.is() )
865 m_xStringResourceManager->setCurrentLocale( m_aLocaleBeforeBasicStart, true );
867 catch(IllegalArgumentException&)
873 DialogWindow* FindDialogWindowForEditor( DlgEditor* pEditor )
875 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
876 IDEWindowTable& aIDEWindowTable = pIDEShell->GetIDEWindowTable();
877 IDEBaseWindow* pWin = aIDEWindowTable.First();
878 DialogWindow* pFoundDlgWin = NULL;
879 while( pWin )
881 if ( !pWin->IsSuspended() && pWin->IsA( TYPE( DialogWindow ) ) )
883 DialogWindow* pDlgWin = (DialogWindow*)pWin;
884 DlgEditor* pWinEditor = pDlgWin->GetEditor();
885 if( pWinEditor == pEditor )
887 pFoundDlgWin = pDlgWin;
888 break;
891 pWin = aIDEWindowTable.Next();
893 return pFoundDlgWin;
897 void LocalizationMgr::setControlResourceIDsForNewEditorObject( DlgEditor* pEditor,
898 Any aControlAny, const ::rtl::OUString& aCtrlName )
900 // Get library for DlgEditor
901 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
902 if( !pDlgWin )
903 return;
904 ScriptDocument aDocument( pDlgWin->GetDocument() );
905 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::setControlResourceIDsForNewEditorObject: invalid document!" );
906 if ( !aDocument.isValid() )
907 return;
908 const String& rLibName = pDlgWin->GetLibName();
909 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, TRUE ) );
910 Reference< XStringResourceManager > xStringResourceManager =
911 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
913 // Set resource property
914 if( !xStringResourceManager.is() || xStringResourceManager->getLocales().getLength() == 0 )
915 return;
917 ::rtl::OUString aDialogName = pDlgWin->GetName();
918 Reference< XStringResourceResolver > xDummyStringResolver;
919 sal_Int32 nChangedCount = implHandleControlResourceProperties
920 ( aControlAny, aDialogName, aCtrlName, xStringResourceManager,
921 xDummyStringResolver, SET_IDS );
923 if( nChangedCount )
924 BasicIDE::MarkDocumentModified( aDocument );
927 void LocalizationMgr::renameControlResourceIDsForEditorObject( DlgEditor* pEditor,
928 ::com::sun::star::uno::Any aControlAny, const ::rtl::OUString& aNewCtrlName )
930 // Get library for DlgEditor
931 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
932 if( !pDlgWin )
933 return;
934 ScriptDocument aDocument( pDlgWin->GetDocument() );
935 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::renameControlResourceIDsForEditorObject: invalid document!" );
936 if ( !aDocument.isValid() )
937 return;
938 const String& rLibName = pDlgWin->GetLibName();
939 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, TRUE ) );
940 Reference< XStringResourceManager > xStringResourceManager =
941 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
943 // Set resource property
944 if( !xStringResourceManager.is() || xStringResourceManager->getLocales().getLength() == 0 )
945 return;
947 ::rtl::OUString aDialogName = pDlgWin->GetName();
948 Reference< XStringResourceResolver > xDummyStringResolver;
949 implHandleControlResourceProperties
950 ( aControlAny, aDialogName, aNewCtrlName, xStringResourceManager,
951 xDummyStringResolver, RENAME_CONTROL_IDS );
955 void LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( DlgEditor* pEditor,
956 Any aControlAny, const ::rtl::OUString& aCtrlName )
958 // Get library for DlgEditor
959 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
960 if( !pDlgWin )
961 return;
962 ScriptDocument aDocument( pDlgWin->GetDocument() );
963 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject: invalid document!" );
964 if ( !aDocument.isValid() )
965 return;
966 const String& rLibName = pDlgWin->GetLibName();
967 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, TRUE ) );
968 Reference< XStringResourceManager > xStringResourceManager =
969 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
971 ::rtl::OUString aDialogName = pDlgWin->GetName();
972 Reference< XStringResourceResolver > xDummyStringResolver;
973 sal_Int32 nChangedCount = implHandleControlResourceProperties
974 ( aControlAny, aDialogName, aCtrlName, xStringResourceManager,
975 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
977 if( nChangedCount )
978 BasicIDE::MarkDocumentModified( aDocument );
981 void LocalizationMgr::setStringResourceAtDialog( const ScriptDocument& rDocument, const String& aLibName,
982 const String& aDlgName, Reference< container::XNameContainer > xDialogModel )
984 static ::rtl::OUString aResourceResolverPropName = ::rtl::OUString::createFromAscii( "ResourceResolver" );
986 // Get library
987 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, TRUE ) );
988 Reference< XStringResourceManager > xStringResourceManager =
989 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
991 // Set resource property
992 if( xStringResourceManager.is() )
994 // Not very elegant as dialog may or may not be localized yet
995 // TODO: Find better place, where dialog is created
996 if( xStringResourceManager->getLocales().getLength() > 0 )
998 Any aDialogCtrl;
999 aDialogCtrl <<= xDialogModel;
1000 Reference< XStringResourceResolver > xDummyStringResolver;
1001 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
1002 ::rtl::OUString(), xStringResourceManager,
1003 xDummyStringResolver, SET_IDS );
1006 Reference< beans::XPropertySet > xDlgPSet( xDialogModel, UNO_QUERY );
1007 Any aStringResourceManagerAny;
1008 aStringResourceManagerAny <<= xStringResourceManager;
1009 xDlgPSet->setPropertyValue( aResourceResolverPropName, aStringResourceManagerAny );
1013 void LocalizationMgr::renameStringResourceIDs( const ScriptDocument& rDocument, const String& aLibName,
1014 const String& aDlgName, Reference< container::XNameContainer > xDialogModel )
1016 // Get library
1017 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, TRUE ) );
1018 Reference< XStringResourceManager > xStringResourceManager =
1019 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
1020 if( !xStringResourceManager.is() )
1021 return;
1023 Any aDialogCtrl;
1024 aDialogCtrl <<= xDialogModel;
1025 Reference< XStringResourceResolver > xDummyStringResolver;
1026 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
1027 ::rtl::OUString(), xStringResourceManager,
1028 xDummyStringResolver, RENAME_DIALOG_IDS );
1030 // Handle all controls
1031 Sequence< ::rtl::OUString > aNames = xDialogModel->getElementNames();
1032 const ::rtl::OUString* pNames = aNames.getConstArray();
1033 sal_Int32 nCtrls = aNames.getLength();
1034 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1036 ::rtl::OUString aCtrlName( pNames[i] );
1037 Any aCtrl = xDialogModel->getByName( aCtrlName );
1038 implHandleControlResourceProperties( aCtrl, aDlgName,
1039 aCtrlName, xStringResourceManager,
1040 xDummyStringResolver, RENAME_DIALOG_IDS );
1044 void LocalizationMgr::removeResourceForDialog( const ScriptDocument& rDocument, const String& aLibName,
1045 const String& aDlgName, Reference< container::XNameContainer > xDialogModel )
1047 // Get library
1048 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, TRUE ) );
1049 Reference< XStringResourceManager > xStringResourceManager =
1050 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
1051 if( !xStringResourceManager.is() )
1052 return;
1054 Any aDialogCtrl;
1055 aDialogCtrl <<= xDialogModel;
1056 Reference< XStringResourceResolver > xDummyStringResolver;
1057 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
1058 ::rtl::OUString(), xStringResourceManager,
1059 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
1061 // Handle all controls
1062 Sequence< ::rtl::OUString > aNames = xDialogModel->getElementNames();
1063 const ::rtl::OUString* pNames = aNames.getConstArray();
1064 sal_Int32 nCtrls = aNames.getLength();
1065 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1067 ::rtl::OUString aCtrlName( pNames[i] );
1068 Any aCtrl = xDialogModel->getByName( aCtrlName );
1069 implHandleControlResourceProperties( aCtrl, aDlgName,
1070 aCtrlName, xStringResourceManager,
1071 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
1075 void LocalizationMgr::resetResourceForDialog( Reference< container::XNameContainer > xDialogModel,
1076 Reference< XStringResourceManager > xStringResourceManager )
1078 if( !xStringResourceManager.is() )
1079 return;
1081 // Dialog as control
1082 ::rtl::OUString aDummyName;
1083 Any aDialogCtrl;
1084 aDialogCtrl <<= xDialogModel;
1085 Reference< XStringResourceResolver > xDummyStringResolver;
1086 implHandleControlResourceProperties( aDialogCtrl, aDummyName,
1087 aDummyName, xStringResourceManager, xDummyStringResolver, RESET_IDS );
1089 // Handle all controls
1090 Sequence< ::rtl::OUString > aNames = xDialogModel->getElementNames();
1091 const ::rtl::OUString* pNames = aNames.getConstArray();
1092 sal_Int32 nCtrls = aNames.getLength();
1093 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1095 ::rtl::OUString aCtrlName( pNames[i] );
1096 Any aCtrl = xDialogModel->getByName( aCtrlName );
1097 implHandleControlResourceProperties( aCtrl, aDummyName,
1098 aCtrlName, xStringResourceManager, xDummyStringResolver, RESET_IDS );
1102 void LocalizationMgr::setResourceIDsForDialog( Reference< container::XNameContainer > xDialogModel,
1103 Reference< XStringResourceManager > xStringResourceManager )
1105 if( !xStringResourceManager.is() )
1106 return;
1108 // Dialog as control
1109 ::rtl::OUString aDummyName;
1110 Any aDialogCtrl;
1111 aDialogCtrl <<= xDialogModel;
1112 Reference< XStringResourceResolver > xDummyStringResolver;
1113 implHandleControlResourceProperties( aDialogCtrl, aDummyName,
1114 aDummyName, xStringResourceManager, xDummyStringResolver, SET_IDS );
1116 // Handle all controls
1117 Sequence< ::rtl::OUString > aNames = xDialogModel->getElementNames();
1118 const ::rtl::OUString* pNames = aNames.getConstArray();
1119 sal_Int32 nCtrls = aNames.getLength();
1120 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1122 ::rtl::OUString aCtrlName( pNames[i] );
1123 Any aCtrl = xDialogModel->getByName( aCtrlName );
1124 implHandleControlResourceProperties( aCtrl, aDummyName,
1125 aCtrlName, xStringResourceManager, xDummyStringResolver, SET_IDS );
1129 void LocalizationMgr::copyResourcesForPastedEditorObject( DlgEditor* pEditor,
1130 Any aControlAny, const ::rtl::OUString& aCtrlName,
1131 Reference< XStringResourceResolver > xSourceStringResolver )
1133 // Get library for DlgEditor
1134 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
1135 if( !pDlgWin )
1136 return;
1137 ScriptDocument aDocument( pDlgWin->GetDocument() );
1138 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::copyResourcesForPastedEditorObject: invalid document!" );
1139 if ( !aDocument.isValid() )
1140 return;
1141 const String& rLibName = pDlgWin->GetLibName();
1142 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, TRUE ) );
1143 Reference< XStringResourceManager > xStringResourceManager =
1144 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
1146 // Set resource property
1147 if( !xStringResourceManager.is() || xStringResourceManager->getLocales().getLength() == 0 )
1148 return;
1150 ::rtl::OUString aDialogName = pDlgWin->GetName();
1151 implHandleControlResourceProperties
1152 ( aControlAny, aDialogName, aCtrlName, xStringResourceManager,
1153 xSourceStringResolver, MOVE_RESOURCES );
1156 void LocalizationMgr::copyResourceForDroppedDialog( Reference< container::XNameContainer > xDialogModel,
1157 const ::rtl::OUString& aDialogName, Reference< XStringResourceManager > xStringResourceManager,
1158 Reference< XStringResourceResolver > xSourceStringResolver )
1160 if( !xStringResourceManager.is() )
1161 return;
1163 // Dialog as control
1164 ::rtl::OUString aDummyName;
1165 Any aDialogCtrl;
1166 aDialogCtrl <<= xDialogModel;
1167 implHandleControlResourceProperties( aDialogCtrl, aDialogName,
1168 aDummyName, xStringResourceManager, xSourceStringResolver, MOVE_RESOURCES );
1170 // Handle all controls
1171 Sequence< ::rtl::OUString > aNames = xDialogModel->getElementNames();
1172 const ::rtl::OUString* pNames = aNames.getConstArray();
1173 sal_Int32 nCtrls = aNames.getLength();
1174 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1176 ::rtl::OUString aCtrlName( pNames[i] );
1177 Any aCtrl = xDialogModel->getByName( aCtrlName );
1178 implHandleControlResourceProperties( aCtrl, aDialogName,
1179 aCtrlName, xStringResourceManager, xSourceStringResolver, MOVE_RESOURCES );
1183 void LocalizationMgr::copyResourceForDialog(
1184 const Reference< container::XNameContainer >& xDialogModel,
1185 const Reference< XStringResourceResolver >& xSourceStringResolver,
1186 const Reference< XStringResourceManager >& xTargetStringResourceManager )
1188 if( !xDialogModel.is() || !xSourceStringResolver.is() || !xTargetStringResourceManager.is() )
1189 return;
1191 ::rtl::OUString aDummyName;
1192 Any aDialogCtrl;
1193 aDialogCtrl <<= xDialogModel;
1194 implHandleControlResourceProperties
1195 ( aDialogCtrl, aDummyName, aDummyName, xTargetStringResourceManager,
1196 xSourceStringResolver, COPY_RESOURCES );
1198 // Handle all controls
1199 Sequence< ::rtl::OUString > aNames = xDialogModel->getElementNames();
1200 const ::rtl::OUString* pNames = aNames.getConstArray();
1201 sal_Int32 nCtrls = aNames.getLength();
1202 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1204 ::rtl::OUString aCtrlName( pNames[i] );
1205 Any aCtrl = xDialogModel->getByName( aCtrlName );
1206 implHandleControlResourceProperties( aCtrl, aDummyName, aDummyName,
1207 xTargetStringResourceManager, xSourceStringResolver, COPY_RESOURCES );
1211 Reference< XStringResourceManager > LocalizationMgr::getStringResourceFromDialogLibrary
1212 ( Reference< container::XNameContainer > xDialogLib )
1214 Reference< XStringResourceManager > xStringResourceManager;
1215 if( xDialogLib.is() )
1217 Reference< resource::XStringResourceSupplier > xStringResourceSupplier( xDialogLib, UNO_QUERY );
1218 if( xStringResourceSupplier.is() )
1220 Reference< resource::XStringResourceResolver >
1221 xStringResourceResolver = xStringResourceSupplier->getStringResource();
1223 xStringResourceManager =
1224 Reference< resource::XStringResourceManager >( xStringResourceResolver, UNO_QUERY );
1227 return xStringResourceManager;