bump product version to 4.1.6.2
[LibreOffice.git] / basctl / source / basicide / localizationmgr.cxx
blob996767f34e2b1d49d0926e7f4e9675e19afe9126
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "localizationmgr.hxx"
22 #include "basidesh.hxx"
23 #include "baside3.hxx"
24 #include "basobj.hxx"
25 #include "iderdll.hxx"
26 #include "dlged.hxx"
28 #include <com/sun/star/resource/XStringResourceSupplier.hpp>
29 #include <com/sun/star/frame/XLayoutManager.hpp>
30 #include <sfx2/dispatch.hxx>
32 namespace basctl
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;
41 namespace
44 OUString const aDot(".");
45 OUString const aEsc("&");
46 OUString const aSemi(";");
48 } // namespace
50 LocalizationMgr::LocalizationMgr(
51 Shell* pShell,
52 ScriptDocument const& rDocument,
53 OUString const& aLibName,
54 Reference<XStringResourceManager> const& xStringResourceManager
55 ) :
56 m_xStringResourceManager(xStringResourceManager),
57 m_pShell(pShell),
58 m_aDocument(rDocument),
59 m_aLibName(aLibName)
60 { }
62 bool LocalizationMgr::isLibraryLocalized ()
64 if (m_xStringResourceManager.is())
65 return m_xStringResourceManager->getLocales().getLength() > 0;
66 return false;
69 void LocalizationMgr::handleTranslationbar ()
71 static OUString const aLayoutManagerName("LayoutManager");
72 static OUString const 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 );
80 a >>= xLayoutManager;
81 if ( xLayoutManager.is() )
83 if ( !isLibraryLocalized() )
85 xLayoutManager->destroyElement( aToolBarResName );
87 else
89 xLayoutManager->createElement( aToolBarResName );
90 xLayoutManager->requestElement( aToolBarResName );
97 // TODO: -> export from toolkit
99 namespace
105 bool isLanguageDependentProperty( OUString aName )
107 static struct Prop
109 const char* sName;
110 sal_Int32 nNameLength;
112 const vProp[] =
114 { "Text", 4 },
115 { "Label", 5 },
116 { "Title", 5 },
117 { "HelpText", 8 },
118 { "CurrencySymbol", 14 },
119 { "StringItemList", 14 },
120 { 0, 0 }
123 for (Prop const* pProp = vProp; pProp->sName; ++pProp)
124 if (aName.equalsAsciiL(pProp->sName, pProp->nNameLength))
125 return true;
126 return false;
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 String aDlgName = pDlgNames[ i ];
140 if (DialogWindow* pWin = m_pShell->FindDlgWin(m_aDocument, m_aLibName, aDlgName))
142 Reference< container::XNameContainer > xDialog = pWin->GetDialog();
143 if( xDialog.is() )
145 // Handle dialog itself as control
146 Any aDialogCtrl;
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::valueOf( nUniqueId );
175 aPureIdStr += aDot;
176 aPureIdStr += aDialogName;
177 aPureIdStr += aDot;
178 if( !aCtrlName.isEmpty() )
180 aPureIdStr += aCtrlName;
181 aPureIdStr += aDot;
183 aPureIdStr += aPropName;
184 return aPureIdStr;
187 extern bool localesAreEqual( const ::com::sun::star::lang::Locale& rLocaleLeft,
188 const ::com::sun::star::lang::Locale& rLocaleRight );
190 // Works on xStringResourceManager's current language for SET_IDS/RESET_IDS,
191 // anyway only one language should exist when calling this method then,
192 // either the first one for mode SET_IDS or the last one for mode RESET_IDS
193 sal_Int32 LocalizationMgr::implHandleControlResourceProperties
194 ( Any aControlAny, const OUString& aDialogName, const OUString& aCtrlName,
195 Reference< XStringResourceManager > xStringResourceManager,
196 Reference< XStringResourceResolver > xSourceStringResolver, HandleResourceMode eMode )
198 sal_Int32 nChangedCount = 0;
200 Reference< XPropertySet > xPropertySet;
201 aControlAny >>= xPropertySet;
202 if( xPropertySet.is() )
204 Sequence< Locale > aLocaleSeq = xStringResourceManager->getLocales();
205 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
206 if( nLocaleCount == 0 )
207 return 0;
209 Reference< XPropertySetInfo > xPropertySetInfo = xPropertySet->getPropertySetInfo();
210 if( xPropertySetInfo.is() )
212 // get sequence of control properties
213 Sequence< Property > aPropSeq = xPropertySetInfo->getProperties();
214 const Property* pProps = aPropSeq.getConstArray();
215 sal_Int32 nCtrlProps = aPropSeq.getLength();
217 // create a map of tab indices and control names, sorted by tab index
218 for( sal_Int32 j = 0 ; j < nCtrlProps ; ++j )
220 const Property& rProp = pProps[j];
221 OUString aPropName = rProp.Name;
222 TypeClass eType = rProp.Type.getTypeClass();
223 bool bLanguageDependentProperty =
224 (eType == TypeClass_STRING || eType == TypeClass_SEQUENCE)
225 && isLanguageDependentProperty( aPropName );
226 if( !bLanguageDependentProperty )
227 continue;
229 if( eType == TypeClass_STRING )
231 Any aPropAny = xPropertySet->getPropertyValue( aPropName );
232 OUString aPropStr;
233 aPropAny >>= aPropStr;
235 // Replace string by id, add id+string to StringResource
236 if( eMode == SET_IDS )
238 bool bEscAlreadyExisting = (!aPropStr.isEmpty() && aPropStr.getStr()[0] == '&' );
239 if( bEscAlreadyExisting )
240 continue;
242 OUString aPureIdStr = implCreatePureResourceId
243 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
245 // Set Id for all locales
246 const Locale* pLocales = aLocaleSeq.getConstArray();
247 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
249 const Locale& rLocale = pLocales[ i ];
250 xStringResourceManager->setStringForLocale( aPureIdStr, aPropStr, rLocale );
253 OUString aPropIdStr = aEsc;
254 aPropIdStr += aPureIdStr;
255 // TODO?: Change here and in toolkit
256 (void)aSemi;
257 aPropAny <<= aPropIdStr;
258 xPropertySet->setPropertyValue( aPropName, aPropAny );
260 // Replace id by string from StringResource
261 else if( eMode == RESET_IDS )
263 if( aPropStr.getLength() > 1 )
265 OUString aPureIdStr = aPropStr.copy( 1 );
266 OUString aNewPropStr = aPropStr;
269 aNewPropStr = xStringResourceManager->resolveString( aPureIdStr );
271 catch(const MissingResourceException&)
274 aPropAny <<= aNewPropStr;
275 xPropertySet->setPropertyValue( aPropName, aPropAny );
278 // Remove Id for all locales
279 else if( eMode == REMOVE_IDS_FROM_RESOURCE )
281 if( aPropStr.getLength() > 1 )
283 OUString aPureIdStr = aPropStr.copy( 1 );
285 const Locale* pLocales = aLocaleSeq.getConstArray();
286 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
288 const Locale& rLocale = pLocales[ i ];
291 xStringResourceManager->removeIdForLocale( aPureIdStr, rLocale );
293 catch(const MissingResourceException&)
299 // Rename resource id
300 else if( eMode == RENAME_DIALOG_IDS || eMode == RENAME_CONTROL_IDS )
302 OUString aSourceIdStr = aPropStr;
303 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
305 OUString aPureIdStr = implCreatePureResourceId
306 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
308 // Set new Id and remove old one for all locales
309 const Locale* pLocales = aLocaleSeq.getConstArray();
310 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
312 const Locale& rLocale = pLocales[ i ];
313 OUString aResStr;
316 aResStr = xStringResourceManager->resolveStringForLocale
317 ( aPureSourceIdStr, rLocale );
318 xStringResourceManager->removeIdForLocale( aPureSourceIdStr, rLocale );
319 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
321 catch(const MissingResourceException&)
325 OUString aPropIdStr = aEsc;
326 aPropIdStr += aPureIdStr;
327 // TODO?: Change here and in toolkit
328 (void)aSemi;
329 aPropAny <<= aPropIdStr;
330 xPropertySet->setPropertyValue( aPropName, aPropAny );
332 // Replace string by string from source StringResourceResolver
333 else if( eMode == MOVE_RESOURCES && xSourceStringResolver.is() )
335 OUString aSourceIdStr = aPropStr;
336 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
338 OUString aPureIdStr = implCreatePureResourceId
339 ( aDialogName, aCtrlName, aPropName, xStringResourceManager );
341 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
343 // Set Id for all locales
344 const Locale* pLocales = aLocaleSeq.getConstArray();
345 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
347 const Locale& rLocale = pLocales[ i ];
348 OUString aResStr;
351 aResStr = xSourceStringResolver->resolveStringForLocale
352 ( aPureSourceIdStr, rLocale );
354 catch(const MissingResourceException&)
356 aResStr = xSourceStringResolver->resolveStringForLocale
357 ( aPureSourceIdStr, rDefaultLocale );
359 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
362 OUString aPropIdStr = aEsc;
363 aPropIdStr += aPureIdStr;
364 // TODO?: Change here and in toolkit
365 (void)aSemi;
366 aPropAny <<= aPropIdStr;
367 xPropertySet->setPropertyValue( aPropName, aPropAny );
369 // Copy string from source to target resource
370 else if( eMode == COPY_RESOURCES && xSourceStringResolver.is() )
372 OUString aSourceIdStr = aPropStr;
373 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
375 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
377 // Copy Id for all locales
378 const Locale* pLocales = aLocaleSeq.getConstArray();
379 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
381 const Locale& rLocale = pLocales[ i ];
382 OUString aResStr;
385 aResStr = xSourceStringResolver->resolveStringForLocale
386 ( aPureSourceIdStr, rLocale );
388 catch(const MissingResourceException&)
390 aResStr = xSourceStringResolver->resolveStringForLocale
391 ( aPureSourceIdStr, rDefaultLocale );
393 xStringResourceManager->setStringForLocale( aPureSourceIdStr, aResStr, rLocale );
396 nChangedCount++;
399 // Listbox / Combobox
400 else if( eType == TypeClass_SEQUENCE )
402 Any aPropAny = xPropertySet->getPropertyValue( aPropName );
403 Sequence< OUString > aPropStrings;
404 aPropAny >>= aPropStrings;
406 const OUString* pPropStrings = aPropStrings.getConstArray();
407 sal_Int32 nPropStringCount = aPropStrings.getLength();
408 if( nPropStringCount == 0 )
409 continue;
411 // Replace string by id, add id+string to StringResource
412 if( eMode == SET_IDS )
414 Sequence< OUString > aIdStrings;
415 aIdStrings.realloc( nPropStringCount );
416 OUString* pIdStrings = aIdStrings.getArray();
418 OUString aIdStrBase = aDot;
419 aIdStrBase += aCtrlName;
420 aIdStrBase += aDot;
421 aIdStrBase += aPropName;
423 const Locale* pLocales = aLocaleSeq.getConstArray();
424 sal_Int32 i;
425 for ( i = 0; i < nPropStringCount; ++i )
427 OUString aPropStr = pPropStrings[i];
428 bool bEscAlreadyExisting = (!aPropStr.isEmpty() && aPropStr.getStr()[0] == '&' );
429 if( bEscAlreadyExisting )
431 pIdStrings[i] = aPropStr;
432 continue;
435 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
436 OUString aPureIdStr = OUString::valueOf( nUniqueId );
437 aPureIdStr += aIdStrBase;
439 // Set Id for all locales
440 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
442 const Locale& rLocale = pLocales[ iLocale ];
443 xStringResourceManager->setStringForLocale( aPureIdStr, aPropStr, rLocale );
446 OUString aPropIdStr = aEsc;
447 aPropIdStr += aPureIdStr;
448 pIdStrings[i] = aPropIdStr;
450 aPropAny <<= aIdStrings;
451 xPropertySet->setPropertyValue( aPropName, aPropAny );
453 // Replace id by string from StringResource
454 else if( eMode == RESET_IDS )
456 Sequence< OUString > aNewPropStrings;
457 aNewPropStrings.realloc( nPropStringCount );
458 OUString* pNewPropStrings = aNewPropStrings.getArray();
460 sal_Int32 i;
461 for ( i = 0; i < nPropStringCount; ++i )
463 OUString aIdStr = pPropStrings[i];
464 OUString aNewPropStr = aIdStr;
465 if( aIdStr.getLength() > 1 )
467 OUString aPureIdStr = aIdStr.copy( 1 );
470 aNewPropStr = xStringResourceManager->resolveString( aPureIdStr );
472 catch(const MissingResourceException&)
476 pNewPropStrings[i] = aNewPropStr;
478 aPropAny <<= aNewPropStrings;
479 xPropertySet->setPropertyValue( aPropName, aPropAny );
481 // Remove Id for all locales
482 else if( eMode == REMOVE_IDS_FROM_RESOURCE )
484 Sequence< OUString > aNewPropStrings;
485 aNewPropStrings.realloc( nPropStringCount );
487 const Locale* pLocales = aLocaleSeq.getConstArray();
488 sal_Int32 i;
489 for ( i = 0; i < nPropStringCount; ++i )
491 OUString aIdStr = pPropStrings[i];
492 if( aIdStr.getLength() > 1 )
494 OUString aPureIdStr = aIdStr.copy( 1 );
496 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
498 const Locale& rLocale = pLocales[iLocale];
501 xStringResourceManager->removeIdForLocale( aPureIdStr, rLocale );
503 catch(const MissingResourceException&)
510 // Rename resource id
511 else if( eMode == RENAME_CONTROL_IDS )
513 Sequence< OUString > aIdStrings;
514 aIdStrings.realloc( nPropStringCount );
515 OUString* pIdStrings = aIdStrings.getArray();
517 OUString aIdStrBase = aDot;
518 aIdStrBase += aCtrlName;
519 aIdStrBase += aDot;
520 aIdStrBase += aPropName;
522 const Locale* pLocales = aLocaleSeq.getConstArray();
523 sal_Int32 i;
524 for ( i = 0; i < nPropStringCount; ++i )
526 OUString aSourceIdStr = pPropStrings[i];
527 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
529 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
530 OUString aPureIdStr = OUString::valueOf( nUniqueId );
531 aPureIdStr += aIdStrBase;
533 // Set Id for all locales
534 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
536 const Locale& rLocale = pLocales[ iLocale ];
538 OUString aResStr;
541 aResStr = xStringResourceManager->resolveStringForLocale
542 ( aPureSourceIdStr, rLocale );
543 xStringResourceManager->removeIdForLocale( aPureSourceIdStr, rLocale );
544 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
546 catch(const MissingResourceException&)
550 OUString aPropIdStr = aEsc;
551 aPropIdStr += aPureIdStr;
552 pIdStrings[i] = aPropIdStr;
554 aPropAny <<= aIdStrings;
555 xPropertySet->setPropertyValue( aPropName, aPropAny );
557 // Replace string by string from source StringResourceResolver
558 else if( eMode == MOVE_RESOURCES && xSourceStringResolver.is() )
560 Sequence< OUString > aIdStrings;
561 aIdStrings.realloc( nPropStringCount );
562 OUString* pIdStrings = aIdStrings.getArray();
564 OUString aIdStrBase = aDot;
565 aIdStrBase += aCtrlName;
566 aIdStrBase += aDot;
567 aIdStrBase += aPropName;
569 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
571 const Locale* pLocales = aLocaleSeq.getConstArray();
572 sal_Int32 i;
573 for ( i = 0; i < nPropStringCount; ++i )
575 OUString aSourceIdStr = pPropStrings[i];
576 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
578 sal_Int32 nUniqueId = xStringResourceManager->getUniqueNumericId();
579 OUString aPureIdStr = OUString::valueOf( nUniqueId );
580 aPureIdStr += aIdStrBase;
582 // Set Id for all locales
583 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
585 const Locale& rLocale = pLocales[ iLocale ];
587 OUString aResStr;
590 aResStr = xSourceStringResolver->resolveStringForLocale
591 ( aPureSourceIdStr, rLocale );
593 catch(const MissingResourceException&)
595 aResStr = xSourceStringResolver->resolveStringForLocale
596 ( aPureSourceIdStr, rDefaultLocale );
598 xStringResourceManager->setStringForLocale( aPureIdStr, aResStr, rLocale );
601 OUString aPropIdStr = aEsc;
602 aPropIdStr += aPureIdStr;
603 pIdStrings[i] = aPropIdStr;
605 aPropAny <<= aIdStrings;
606 xPropertySet->setPropertyValue( aPropName, aPropAny );
608 // Copy string from source to target resource
609 else if( eMode == COPY_RESOURCES && xSourceStringResolver.is() )
611 const Locale& rDefaultLocale = xSourceStringResolver->getDefaultLocale();
613 const Locale* pLocales = aLocaleSeq.getConstArray();
614 sal_Int32 i;
615 for ( i = 0; i < nPropStringCount; ++i )
617 OUString aSourceIdStr = pPropStrings[i];
618 OUString aPureSourceIdStr = aSourceIdStr.copy( 1 );
620 // Set Id for all locales
621 for( sal_Int32 iLocale = 0 ; iLocale < nLocaleCount ; iLocale++ )
623 const Locale& rLocale = pLocales[ iLocale ];
625 OUString aResStr;
628 aResStr = xSourceStringResolver->resolveStringForLocale
629 ( aPureSourceIdStr, rLocale );
631 catch(const MissingResourceException&)
633 aResStr = xSourceStringResolver->resolveStringForLocale
634 ( aPureSourceIdStr, rDefaultLocale );
636 xStringResourceManager->setStringForLocale( aPureSourceIdStr, aResStr, rLocale );
640 nChangedCount++;
645 return nChangedCount;
649 void LocalizationMgr::handleAddLocales( Sequence< Locale > aLocaleSeq )
651 const Locale* pLocales = aLocaleSeq.getConstArray();
652 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
654 if( isLibraryLocalized() )
656 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
658 const Locale& rLocale = pLocales[ i ];
659 m_xStringResourceManager->newLocale( rLocale );
662 else
664 DBG_ASSERT( nLocaleCount==1, "LocalizationMgr::handleAddLocales(): Only one first locale allowed" );
666 const Locale& rLocale = pLocales[ 0 ];
667 m_xStringResourceManager->newLocale( rLocale );
668 enableResourceForAllLibraryDialogs();
671 MarkDocumentModified( m_aDocument );
673 // update locale toolbar
674 if (SfxBindings* pBindings = GetBindingsPtr())
675 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
677 handleTranslationbar();
681 void LocalizationMgr::handleRemoveLocales( Sequence< Locale > aLocaleSeq )
683 const Locale* pLocales = aLocaleSeq.getConstArray();
684 sal_Int32 nLocaleCount = aLocaleSeq.getLength();
685 bool bConsistant = true;
686 bool bModified = false;
688 for( sal_Int32 i = 0 ; i < nLocaleCount ; i++ )
690 const Locale& rLocale = pLocales[ i ];
691 bool bRemove = true;
693 // Check if last locale
694 Sequence< Locale > aResLocaleSeq = m_xStringResourceManager->getLocales();
695 if( aResLocaleSeq.getLength() == 1 )
697 const Locale& rLastResLocale = aResLocaleSeq.getConstArray()[ 0 ];
698 if( localesAreEqual( rLocale, rLastResLocale ) )
700 disableResourceForAllLibraryDialogs();
702 else
704 // Inconsistancy, keep last locale
705 bConsistant = false;
706 bRemove = false;
710 if( bRemove )
714 m_xStringResourceManager->removeLocale( rLocale );
715 bModified = true;
717 catch(const IllegalArgumentException&)
719 bConsistant = false;
723 if( bModified )
725 MarkDocumentModified( m_aDocument );
727 // update slots
728 if (SfxBindings* pBindings = GetBindingsPtr())
730 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
731 pBindings->Invalidate( SID_BASICIDE_MANAGE_LANG );
734 handleTranslationbar();
737 DBG_ASSERT( bConsistant,
738 "LocalizationMgr::handleRemoveLocales(): sequence contains unsupported locales" );
739 (void)bConsistant;
742 void LocalizationMgr::handleSetDefaultLocale( Locale aLocale )
744 if( m_xStringResourceManager.is() )
748 m_xStringResourceManager->setDefaultLocale( aLocale );
750 catch(const IllegalArgumentException&)
752 OSL_FAIL( "LocalizationMgr::handleSetDefaultLocale: Invalid locale" );
755 // update locale toolbar
756 if (SfxBindings* pBindings = GetBindingsPtr())
757 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
761 void LocalizationMgr::handleSetCurrentLocale( ::com::sun::star::lang::Locale aLocale )
763 if( m_xStringResourceManager.is() )
767 m_xStringResourceManager->setCurrentLocale( aLocale, false );
769 catch(const IllegalArgumentException&)
771 OSL_FAIL( "LocalizationMgr::handleSetCurrentLocale: Invalid locale" );
774 // update locale toolbar
775 if (SfxBindings* pBindings = GetBindingsPtr())
776 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
778 if (DialogWindow* pDlgWin = dynamic_cast<DialogWindow*>(m_pShell->GetCurWindow()))
779 if (!pDlgWin->IsSuspended())
780 pDlgWin->GetEditor().UpdatePropertyBrowserDelayed();
784 void LocalizationMgr::handleBasicStarted( void )
786 if( m_xStringResourceManager.is() )
787 m_aLocaleBeforeBasicStart = m_xStringResourceManager->getCurrentLocale();
790 void LocalizationMgr::handleBasicStopped( void )
794 if( m_xStringResourceManager.is() )
795 m_xStringResourceManager->setCurrentLocale( m_aLocaleBeforeBasicStart, true );
797 catch(const IllegalArgumentException&)
803 DialogWindow* FindDialogWindowForEditor( DlgEditor* pEditor )
805 Shell::WindowTable const& aWindowTable = GetShell()->GetWindowTable();
806 for (Shell::WindowTableIt it = aWindowTable.begin(); it != aWindowTable.end(); ++it )
808 BaseWindow* pWin = it->second;
809 if (!pWin->IsSuspended())
810 if (DialogWindow* pDlgWin = dynamic_cast<DialogWindow*>(pWin))
812 if (&pDlgWin->GetEditor() == pEditor)
813 return pDlgWin;
816 return 0;
820 void LocalizationMgr::setControlResourceIDsForNewEditorObject( DlgEditor* pEditor,
821 Any aControlAny, const OUString& aCtrlName )
823 // Get library for DlgEditor
824 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
825 if( !pDlgWin )
826 return;
827 ScriptDocument aDocument( pDlgWin->GetDocument() );
828 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::setControlResourceIDsForNewEditorObject: invalid document!" );
829 if ( !aDocument.isValid() )
830 return;
831 const String& rLibName = pDlgWin->GetLibName();
832 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
833 Reference< XStringResourceManager > xStringResourceManager =
834 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
836 // Set resource property
837 if( !xStringResourceManager.is() || xStringResourceManager->getLocales().getLength() == 0 )
838 return;
840 OUString aDialogName = pDlgWin->GetName();
841 Reference< XStringResourceResolver > xDummyStringResolver;
842 sal_Int32 nChangedCount = implHandleControlResourceProperties
843 ( aControlAny, aDialogName, aCtrlName, xStringResourceManager,
844 xDummyStringResolver, SET_IDS );
846 if( nChangedCount )
847 MarkDocumentModified( aDocument );
850 void LocalizationMgr::renameControlResourceIDsForEditorObject( DlgEditor* pEditor,
851 ::com::sun::star::uno::Any aControlAny, const OUString& aNewCtrlName )
853 // Get library for DlgEditor
854 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
855 if( !pDlgWin )
856 return;
857 ScriptDocument aDocument( pDlgWin->GetDocument() );
858 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::renameControlResourceIDsForEditorObject: invalid document!" );
859 if ( !aDocument.isValid() )
860 return;
861 const String& rLibName = pDlgWin->GetLibName();
862 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
863 Reference< XStringResourceManager > xStringResourceManager =
864 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
866 // Set resource property
867 if( !xStringResourceManager.is() || xStringResourceManager->getLocales().getLength() == 0 )
868 return;
870 OUString aDialogName = pDlgWin->GetName();
871 Reference< XStringResourceResolver > xDummyStringResolver;
872 implHandleControlResourceProperties
873 ( aControlAny, aDialogName, aNewCtrlName, xStringResourceManager,
874 xDummyStringResolver, RENAME_CONTROL_IDS );
878 void LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( DlgEditor* pEditor,
879 Any aControlAny, const OUString& aCtrlName )
881 // Get library for DlgEditor
882 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
883 if( !pDlgWin )
884 return;
885 ScriptDocument aDocument( pDlgWin->GetDocument() );
886 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject: invalid document!" );
887 if ( !aDocument.isValid() )
888 return;
889 const String& rLibName = pDlgWin->GetLibName();
890 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
891 Reference< XStringResourceManager > xStringResourceManager =
892 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
894 OUString aDialogName = pDlgWin->GetName();
895 Reference< XStringResourceResolver > xDummyStringResolver;
896 sal_Int32 nChangedCount = implHandleControlResourceProperties
897 ( aControlAny, aDialogName, aCtrlName, xStringResourceManager,
898 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
900 if( nChangedCount )
901 MarkDocumentModified( aDocument );
904 void LocalizationMgr::setStringResourceAtDialog( const ScriptDocument& rDocument, const OUString& aLibName,
905 const OUString& aDlgName, Reference< container::XNameContainer > xDialogModel )
907 static OUString aResourceResolverPropName( "ResourceResolver" );
909 // Get library
910 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, true ) );
911 Reference< XStringResourceManager > xStringResourceManager =
912 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
914 // Set resource property
915 if( xStringResourceManager.is() )
917 // Not very elegant as dialog may or may not be localized yet
918 // TODO: Find better place, where dialog is created
919 if( xStringResourceManager->getLocales().getLength() > 0 )
921 Any aDialogCtrl;
922 aDialogCtrl <<= xDialogModel;
923 Reference< XStringResourceResolver > xDummyStringResolver;
924 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
925 OUString(), xStringResourceManager,
926 xDummyStringResolver, SET_IDS );
929 Reference< beans::XPropertySet > xDlgPSet( xDialogModel, UNO_QUERY );
930 Any aStringResourceManagerAny;
931 aStringResourceManagerAny <<= xStringResourceManager;
932 xDlgPSet->setPropertyValue( aResourceResolverPropName, aStringResourceManagerAny );
936 void LocalizationMgr::renameStringResourceIDs( const ScriptDocument& rDocument, const OUString& aLibName,
937 const OUString& aDlgName, Reference< container::XNameContainer > xDialogModel )
939 // Get library
940 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, true ) );
941 Reference< XStringResourceManager > xStringResourceManager =
942 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
943 if( !xStringResourceManager.is() )
944 return;
946 Any aDialogCtrl;
947 aDialogCtrl <<= xDialogModel;
948 Reference< XStringResourceResolver > xDummyStringResolver;
949 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
950 OUString(), xStringResourceManager,
951 xDummyStringResolver, RENAME_DIALOG_IDS );
953 // Handle all controls
954 Sequence< OUString > aNames = xDialogModel->getElementNames();
955 const OUString* pNames = aNames.getConstArray();
956 sal_Int32 nCtrls = aNames.getLength();
957 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
959 OUString aCtrlName( pNames[i] );
960 Any aCtrl = xDialogModel->getByName( aCtrlName );
961 implHandleControlResourceProperties( aCtrl, aDlgName,
962 aCtrlName, xStringResourceManager,
963 xDummyStringResolver, RENAME_DIALOG_IDS );
967 void LocalizationMgr::removeResourceForDialog( const ScriptDocument& rDocument, const OUString& aLibName,
968 const OUString& aDlgName, Reference< container::XNameContainer > xDialogModel )
970 // Get library
971 Reference< container::XNameContainer > xDialogLib( rDocument.getLibrary( E_DIALOGS, aLibName, true ) );
972 Reference< XStringResourceManager > xStringResourceManager =
973 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
974 if( !xStringResourceManager.is() )
975 return;
977 Any aDialogCtrl;
978 aDialogCtrl <<= xDialogModel;
979 Reference< XStringResourceResolver > xDummyStringResolver;
980 implHandleControlResourceProperties( aDialogCtrl, aDlgName,
981 OUString(), xStringResourceManager,
982 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
984 // Handle all controls
985 Sequence< OUString > aNames = xDialogModel->getElementNames();
986 const OUString* pNames = aNames.getConstArray();
987 sal_Int32 nCtrls = aNames.getLength();
988 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
990 OUString aCtrlName( pNames[i] );
991 Any aCtrl = xDialogModel->getByName( aCtrlName );
992 implHandleControlResourceProperties( aCtrl, aDlgName,
993 aCtrlName, xStringResourceManager,
994 xDummyStringResolver, REMOVE_IDS_FROM_RESOURCE );
998 void LocalizationMgr::resetResourceForDialog( Reference< container::XNameContainer > xDialogModel,
999 Reference< XStringResourceManager > xStringResourceManager )
1001 if( !xStringResourceManager.is() )
1002 return;
1004 // Dialog as control
1005 OUString aDummyName;
1006 Any aDialogCtrl;
1007 aDialogCtrl <<= xDialogModel;
1008 Reference< XStringResourceResolver > xDummyStringResolver;
1009 implHandleControlResourceProperties( aDialogCtrl, aDummyName,
1010 aDummyName, xStringResourceManager, xDummyStringResolver, RESET_IDS );
1012 // Handle all controls
1013 Sequence< OUString > aNames = xDialogModel->getElementNames();
1014 const OUString* pNames = aNames.getConstArray();
1015 sal_Int32 nCtrls = aNames.getLength();
1016 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1018 OUString aCtrlName( pNames[i] );
1019 Any aCtrl = xDialogModel->getByName( aCtrlName );
1020 implHandleControlResourceProperties( aCtrl, aDummyName,
1021 aCtrlName, xStringResourceManager, xDummyStringResolver, RESET_IDS );
1025 void LocalizationMgr::setResourceIDsForDialog( Reference< container::XNameContainer > xDialogModel,
1026 Reference< XStringResourceManager > xStringResourceManager )
1028 if( !xStringResourceManager.is() )
1029 return;
1031 // Dialog as control
1032 OUString aDummyName;
1033 Any aDialogCtrl;
1034 aDialogCtrl <<= xDialogModel;
1035 Reference< XStringResourceResolver > xDummyStringResolver;
1036 implHandleControlResourceProperties( aDialogCtrl, aDummyName,
1037 aDummyName, xStringResourceManager, xDummyStringResolver, SET_IDS );
1039 // Handle all controls
1040 Sequence< OUString > aNames = xDialogModel->getElementNames();
1041 const OUString* pNames = aNames.getConstArray();
1042 sal_Int32 nCtrls = aNames.getLength();
1043 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1045 OUString aCtrlName( pNames[i] );
1046 Any aCtrl = xDialogModel->getByName( aCtrlName );
1047 implHandleControlResourceProperties( aCtrl, aDummyName,
1048 aCtrlName, xStringResourceManager, xDummyStringResolver, SET_IDS );
1052 void LocalizationMgr::copyResourcesForPastedEditorObject( DlgEditor* pEditor,
1053 Any aControlAny, const OUString& aCtrlName,
1054 Reference< XStringResourceResolver > xSourceStringResolver )
1056 // Get library for DlgEditor
1057 DialogWindow* pDlgWin = FindDialogWindowForEditor( pEditor );
1058 if( !pDlgWin )
1059 return;
1060 ScriptDocument aDocument( pDlgWin->GetDocument() );
1061 DBG_ASSERT( aDocument.isValid(), "LocalizationMgr::copyResourcesForPastedEditorObject: invalid document!" );
1062 if ( !aDocument.isValid() )
1063 return;
1064 const String& rLibName = pDlgWin->GetLibName();
1065 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, rLibName, true ) );
1066 Reference< XStringResourceManager > xStringResourceManager =
1067 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
1069 // Set resource property
1070 if( !xStringResourceManager.is() || xStringResourceManager->getLocales().getLength() == 0 )
1071 return;
1073 OUString aDialogName = pDlgWin->GetName();
1074 implHandleControlResourceProperties
1075 ( aControlAny, aDialogName, aCtrlName, xStringResourceManager,
1076 xSourceStringResolver, MOVE_RESOURCES );
1079 void LocalizationMgr::copyResourceForDroppedDialog( Reference< container::XNameContainer > xDialogModel,
1080 const OUString& aDialogName, Reference< XStringResourceManager > xStringResourceManager,
1081 Reference< XStringResourceResolver > xSourceStringResolver )
1083 if( !xStringResourceManager.is() )
1084 return;
1086 // Dialog as control
1087 OUString aDummyName;
1088 Any aDialogCtrl;
1089 aDialogCtrl <<= xDialogModel;
1090 implHandleControlResourceProperties( aDialogCtrl, aDialogName,
1091 aDummyName, xStringResourceManager, xSourceStringResolver, MOVE_RESOURCES );
1093 // Handle all controls
1094 Sequence< OUString > aNames = xDialogModel->getElementNames();
1095 const OUString* pNames = aNames.getConstArray();
1096 sal_Int32 nCtrls = aNames.getLength();
1097 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1099 OUString aCtrlName( pNames[i] );
1100 Any aCtrl = xDialogModel->getByName( aCtrlName );
1101 implHandleControlResourceProperties( aCtrl, aDialogName,
1102 aCtrlName, xStringResourceManager, xSourceStringResolver, MOVE_RESOURCES );
1106 void LocalizationMgr::copyResourceForDialog(
1107 const Reference< container::XNameContainer >& xDialogModel,
1108 const Reference< XStringResourceResolver >& xSourceStringResolver,
1109 const Reference< XStringResourceManager >& xTargetStringResourceManager )
1111 if( !xDialogModel.is() || !xSourceStringResolver.is() || !xTargetStringResourceManager.is() )
1112 return;
1114 OUString aDummyName;
1115 Any aDialogCtrl;
1116 aDialogCtrl <<= xDialogModel;
1117 implHandleControlResourceProperties
1118 ( aDialogCtrl, aDummyName, aDummyName, xTargetStringResourceManager,
1119 xSourceStringResolver, COPY_RESOURCES );
1121 // Handle all controls
1122 Sequence< OUString > aNames = xDialogModel->getElementNames();
1123 const OUString* pNames = aNames.getConstArray();
1124 sal_Int32 nCtrls = aNames.getLength();
1125 for( sal_Int32 i = 0 ; i < nCtrls ; ++i )
1127 OUString aCtrlName( pNames[i] );
1128 Any aCtrl = xDialogModel->getByName( aCtrlName );
1129 implHandleControlResourceProperties( aCtrl, aDummyName, aDummyName,
1130 xTargetStringResourceManager, xSourceStringResolver, COPY_RESOURCES );
1134 Reference< XStringResourceManager > LocalizationMgr::getStringResourceFromDialogLibrary
1135 ( Reference< container::XNameContainer > xDialogLib )
1137 Reference< XStringResourceManager > xStringResourceManager;
1138 if( xDialogLib.is() )
1140 Reference< resource::XStringResourceSupplier > xStringResourceSupplier( xDialogLib, UNO_QUERY );
1141 if( xStringResourceSupplier.is() )
1143 Reference< resource::XStringResourceResolver >
1144 xStringResourceResolver = xStringResourceSupplier->getStringResource();
1146 xStringResourceManager =
1147 Reference< resource::XStringResourceManager >( xStringResourceResolver, UNO_QUERY );
1150 return xStringResourceManager;
1153 } // namespace basctl
1155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */