1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: moduleuicfgsupplier.cxx,v $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
33 #include <uiconfiguration/moduleuicfgsupplier.hxx>
34 #include <threadhelp/resetableguard.hxx>
37 //_________________________________________________________________________________________________________________
39 //_________________________________________________________________________________________________________________
40 #include <com/sun/star/lang/DisposedException.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/container/XNameAccess.hpp>
43 #include <com/sun/star/embed/ElementModes.hpp>
44 #include <com/sun/star/io/XOutputStream.hpp>
45 #include <com/sun/star/io/XInputStream.hpp>
46 #include <com/sun/star/io/XSeekable.hpp>
47 #include <com/sun/star/embed/XPackageStructureCreator.hpp>
49 //_________________________________________________________________________________________________________________
51 //_________________________________________________________________________________________________________________
52 #include <rtl/logfile.hxx>
53 #include <cppuhelper/implbase1.hxx>
54 #include <vcl/svapp.hxx>
56 using namespace com::sun::star::uno
;
57 using namespace com::sun::star::io
;
58 using namespace com::sun::star::lang
;
59 using namespace com::sun::star::container
;
60 using namespace com::sun::star::beans
;
61 using namespace com::sun::star::embed
;
62 using namespace ::com::sun::star::ui
;
63 using namespace ::com::sun::star::frame
;
68 class RootStorageWrapper
: public ::cppu::WeakImplHelper1
< com::sun::star::embed::XTransactedObject
>
71 // XInterface, XTypeProvider
72 RootStorageWrapper( const Reference
< XTransactedObject
>& xRootCommit
) : m_xRootCommit( xRootCommit
) {}
73 virtual ~RootStorageWrapper() {}
76 virtual void SAL_CALL
commit() throw ( com::sun::star::io::IOException
, com::sun::star::lang::WrappedTargetException
)
78 m_xRootCommit
->commit();
81 virtual void SAL_CALL
revert() throw ( com::sun::star::io::IOException
, com::sun::star::lang::WrappedTargetException
)
83 m_xRootCommit
->revert();
87 Reference
< XTransactedObject
> m_xRootCommit
;
90 //*****************************************************************************************************************
91 // XInterface, XTypeProvider, XServiceInfo
92 //*****************************************************************************************************************
93 DEFINE_XINTERFACE_4 ( ModuleUIConfigurationManagerSupplier
,
95 DIRECT_INTERFACE( css::lang::XTypeProvider
),
96 DIRECT_INTERFACE( css::lang::XServiceInfo
),
97 DIRECT_INTERFACE( css::lang::XComponent
),
98 DIRECT_INTERFACE( ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier
)
101 DEFINE_XTYPEPROVIDER_4 ( ModuleUIConfigurationManagerSupplier
,
102 css::lang::XTypeProvider
,
103 css::lang::XServiceInfo
,
104 css::lang::XComponent
,
105 ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier
108 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE ( ModuleUIConfigurationManagerSupplier
,
109 ::cppu::OWeakObject
,
110 SERVICENAME_MODULEUICONFIGURATIONMANAGERSUPPLIER
,
111 IMPLEMENTATIONNAME_MODULEUICONFIGURATIONMANAGERSUPPLIER
114 DEFINE_INIT_SERVICE ( ModuleUIConfigurationManagerSupplier
, {} )
118 void ModuleUIConfigurationManagerSupplier::impl_initStorages()
120 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ModuleUIConfigurationManagerSupplier::impl_initStorages" );
123 RTL_LOGFILE_CONTEXT( aLog, "framework (cd100003) ::ModuleUIConfigurationManagerSupplier::impl_initStorages" );
125 rtl::OUString aFinalSlash( RTL_CONSTASCII_USTRINGPARAM( "/" ));
126 rtl::OUString aConfigRootFolder( RTL_CONSTASCII_USTRINGPARAM( "soffice.cfg/modules" ));
127 rtl::OUString aConfigSubFolder( RTL_CONSTASCII_USTRINGPARAM( "soffice.cfg/modules/soffice.cfg" ));
128 rtl::OUString aConfigRootFolder( RTL_CONSTASCII_USTRINGPARAM( "soffice.cfg" ));
129 rtl::OUString aConfigSubFolder( RTL_CONSTASCII_USTRINGPARAM( "soffice.cfg/soffice.cfg" ));
130 rtl::OUString aConfigFileName( RTL_CONSTASCII_USTRINGPARAM( "soffice.cfg/uiconfig.zip" ));
132 Reference< XPropertySet > xPathSettings( m_xServiceManager->createInstance(
133 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.PathSettings" ))),
136 Any a = xPathSettings->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UIConfig" )));
137 a >>= m_aDefaultConfigURL;
138 a = xPathSettings->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UserConfig" )));
139 a >>= m_aUserConfigURL;
141 // Use only the first entry from "UIConfig"
142 sal_Int32 nIndex = m_aDefaultConfigURL.indexOf( ';' );
144 m_aDefaultConfigURL = m_aDefaultConfigURL.copy( 0, nIndex );
146 rtl::OUString aDefaultConfigFolderURL( m_aDefaultConfigURL );
148 nIndex = m_aDefaultConfigURL.lastIndexOf( '/' );
149 if (( nIndex > 0 ) && ( nIndex != ( m_aDefaultConfigURL.getLength()-1 )))
151 m_aDefaultConfigURL += aFinalSlash;
152 aDefaultConfigFolderURL += aFinalSlash;
155 nIndex = m_aUserConfigURL.lastIndexOf( '/' );
156 if (( nIndex > 0 ) && ( nIndex != ( m_aUserConfigURL.getLength()-1 )))
157 m_aUserConfigURL += aFinalSlash;
159 // aDefaultConfigFolderURL += aConfigRootFolder;
161 // Create root storages for user interface configuration data (default and customizable)
162 Reference< XSingleServiceFactory > xStorageFactory( m_xServiceManager->createInstance(
163 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.embed.StorageFactory" ))),
166 Sequence< Any > aArgs( 2 );
168 // Default root storage (READ-ACCESS)
169 aArgs[0] <<= m_aDefaultConfigURL + aConfigFileName; //aConfigSubFolder;
170 aArgs[1] <<= ElementModes::READ;
171 m_xDefaultCfgRootStorage = Reference< XStorage >( xStorageFactory->createInstanceWithArguments( aArgs ), UNO_QUERY_THROW );
173 Reference < XOutputStream > xTempOut( m_xServiceManager->createInstance (
174 ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
177 Reference< XPackageStructureCreator > xPackageStructCreator( m_xServiceManager->createInstance(
178 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.embed.PackageStructureCreator" ))),
181 RTL_LOGFILE_CONTEXT_TRACE( aLog, "{ convertToPackage" );
182 xPackageStructCreator->convertToPackage( aDefaultConfigFolderURL, xTempOut );
183 RTL_LOGFILE_CONTEXT_TRACE( aLog, "} convertToPackage" );
185 xTempOut->closeOutput();
186 Reference< XInputStream > xTempIn( xTempOut, UNO_QUERY );
187 Reference< XSeekable > xTempSeek( xTempOut, UNO_QUERY );
189 // Default root storage (READ-ACCESS)
190 xTempSeek->seek( 0 );
192 aArgs[0] <<= xTempIn;
193 aArgs[1] <<= ElementModes::READ;
194 m_xDefaultCfgRootStorage = Reference< XStorage >( xStorageFactory->createInstanceWithArguments( aArgs ), UNO_QUERY_THROW );
196 // Customizable root storage (READWRITE-ACCESS)
197 aArgs[0] <<= m_aUserConfigURL + aConfigSubFolder;
198 aArgs[1] <<= ElementModes::READWRITE;
199 m_xUserCfgRootStorage = Reference< XStorage >( xStorageFactory->createInstanceWithArguments( aArgs ), UNO_QUERY );
201 // Create wrapper object for module user interface configuration managers, so they are able to call commit/revert on
202 // root storage and nothing more (saftey)!
203 RootStorageWrapper* pUserRootStorageWrapper = new RootStorageWrapper( Reference< XTransactedObject >( m_xUserCfgRootStorage, UNO_QUERY ));
204 m_xUserRootCommit = Reference< XTransactedObject>( static_cast< OWeakObject *>( pUserRootStorageWrapper ), UNO_QUERY );
212 ModuleUIConfigurationManagerSupplier::ModuleUIConfigurationManagerSupplier( const Reference
< XMultiServiceFactory
>& xServiceManager
) :
213 ThreadHelpBase( &Application::GetSolarMutex() )
214 , m_bDisposed( false )
215 //TODO_AS , m_bInit( false )
216 , m_xModuleMgr( Reference
< XModuleManager
>( xServiceManager
->createInstance( SERVICENAME_MODULEMANAGER
), UNO_QUERY
))
217 , m_xServiceManager( xServiceManager
)
218 , m_aListenerContainer( m_aLock
.getShareableOslMutex() )
220 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ModuleUIConfigurationManagerSupplier::ModuleUIConfigurationManagerSupplier" );
221 // Retrieve known modules and insert them into our hash_map to speed-up access time.
222 Reference
< XNameAccess
> xNameAccess( m_xModuleMgr
, UNO_QUERY
);
223 const Sequence
< ::rtl::OUString
> aNameSeq
= xNameAccess
->getElementNames();
224 const ::rtl::OUString
* pNameSeq
= aNameSeq
.getConstArray();
225 for ( sal_Int32 n
= 0; n
< aNameSeq
.getLength(); n
++ )
226 m_aModuleToModuleUICfgMgrMap
.insert( ModuleToModuleCfgMgr::value_type( pNameSeq
[n
], Reference
< XUIConfigurationManager
>() ));
229 ModuleUIConfigurationManagerSupplier::~ModuleUIConfigurationManagerSupplier()
231 m_xUserRootCommit
.clear();
233 // dispose all our module user interface configuration managers
234 ModuleToModuleCfgMgr::iterator pIter
= m_aModuleToModuleUICfgMgrMap
.begin();
235 while ( pIter
!= m_aModuleToModuleUICfgMgrMap
.end() )
237 Reference
< XComponent
> xComponent( pIter
->second
, UNO_QUERY
);
238 if ( xComponent
.is() )
239 xComponent
->dispose();
243 // Dispose our root configuration storages
244 if ( m_xDefaultCfgRootStorage.is() )
246 Reference< XComponent > xComponent( m_xDefaultCfgRootStorage, UNO_QUERY );
247 xComponent->dispose();
250 if ( m_xUserCfgRootStorage.is() )
252 Reference< XComponent > xComponent( m_xUserCfgRootStorage, UNO_QUERY );
253 xComponent->dispose();
259 void SAL_CALL
ModuleUIConfigurationManagerSupplier::dispose()
260 throw ( RuntimeException
)
262 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ModuleUIConfigurationManagerSupplier::dispose" );
263 Reference
< XComponent
> xThis( static_cast< OWeakObject
* >(this), UNO_QUERY
);
265 css::lang::EventObject
aEvent( xThis
);
266 m_aListenerContainer
.disposeAndClear( aEvent
);
269 ResetableGuard
aGuard( m_aLock
);
274 void SAL_CALL
ModuleUIConfigurationManagerSupplier::addEventListener( const Reference
< XEventListener
>& xListener
)
275 throw ( RuntimeException
)
277 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ModuleUIConfigurationManagerSupplier::addEventListener" );
279 ResetableGuard
aGuard( m_aLock
);
281 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
283 throw DisposedException();
286 m_aListenerContainer
.addInterface( ::getCppuType( ( const Reference
< XEventListener
>* ) NULL
), xListener
);
289 void SAL_CALL
ModuleUIConfigurationManagerSupplier::removeEventListener( const Reference
< XEventListener
>& xListener
)
290 throw ( RuntimeException
)
292 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ModuleUIConfigurationManagerSupplier::removeEventListener" );
293 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
294 m_aListenerContainer
.removeInterface( ::getCppuType( ( const Reference
< XEventListener
>* ) NULL
), xListener
);
297 // XModuleUIConfigurationManagerSupplier
298 Reference
< XUIConfigurationManager
> SAL_CALL
ModuleUIConfigurationManagerSupplier::getUIConfigurationManager( const ::rtl::OUString
& ModuleIdentifier
)
299 throw ( NoSuchElementException
, RuntimeException
)
301 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ModuleUIConfigurationManagerSupplier::getUIConfigurationManager" );
302 ResetableGuard
aGuard( m_aLock
);
304 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
306 throw DisposedException();
308 ModuleToModuleCfgMgr::iterator pIter
= m_aModuleToModuleUICfgMgrMap
.find( ModuleIdentifier
);
309 if ( pIter
== m_aModuleToModuleUICfgMgrMap
.end() )
310 throw NoSuchElementException();
311 //TODO_AS impl_initStorages();
313 // Create instance on demand
314 if ( !pIter
->second
.is() )
317 Reference< XStorage > xDefaultConfigModuleStorage;
318 Reference< XStorage > xUserConfigModuleStorage;
322 xDefaultConfigModuleStorage = Reference< XStorage >( m_xDefaultCfgRootStorage->openStorageElement(
323 sShort, ElementModes::READ ), UNO_QUERY_THROW );
325 if ( m_xUserCfgRootStorage.is() )
329 xUserConfigModuleStorage = Reference< XStorage >( m_xUserCfgRootStorage->openStorageElement(
330 sShort, ElementModes::READWRITE ), UNO_QUERY );
332 catch( ::com::sun::star::io::IOException& )
336 xUserConfigModuleStorage = Reference< XStorage >( m_xUserCfgRootStorage->openStorageElement(
337 sShort, ElementModes::READ ), UNO_QUERY );
339 catch( com::sun::star::uno::Exception& )
345 catch ( com::sun::star::uno::Exception& )
349 Sequence< Any > aArgs( 5 );
350 aArg.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ModuleIdentifier" ));
351 aArg.Value <<= ModuleIdentifier;
353 aArg.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultConfigStorage" ));
354 aArg.Value <<= xDefaultConfigModuleStorage;
356 aArg.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UserConfigStorage" ));
357 aArg.Value <<= xUserConfigModuleStorage;
359 aArg.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UserRootCommit" ));
360 aArg.Value <<= m_xUserRootCommit;
363 ::rtl::OUString sShort
;
366 Sequence
< PropertyValue
> lProps
;
367 Reference
< XNameAccess
> xCont(m_xModuleMgr
, UNO_QUERY
);
368 xCont
->getByName(ModuleIdentifier
) >>= lProps
;
369 for (sal_Int32 i
=0; i
<lProps
.getLength(); ++i
)
371 if (lProps
[i
].Name
.equalsAscii("ooSetupFactoryShortName"))
373 lProps
[i
].Value
>>= sShort
;
380 sShort
= ::rtl::OUString();
383 if (!sShort
.getLength())
384 throw NoSuchElementException();
386 Sequence
< Any
> aArgs( 2 );
387 aArg
.Name
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ModuleShortName" ));
388 aArg
.Value
<<= sShort
;
390 aArg
.Name
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ModuleIdentifier" ));
391 aArg
.Value
<<= ModuleIdentifier
;
394 pIter
->second
.set( m_xServiceManager
->createInstanceWithArguments(SERVICENAME_MODULEUICONFIGURATIONMANAGER
, aArgs
),UNO_QUERY
);
397 return pIter
->second
;
400 } // namespace framework