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 "uifactory/factoryconfiguration.hxx"
23 #include "helper/mischelper.hxx"
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/configuration/theDefaultProvider.hpp>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <com/sun/star/container/XNameContainer.hpp>
30 #include <com/sun/star/container/XContainer.hpp>
32 #include <rtl/ustrbuf.hxx>
33 #include <cppuhelper/weak.hxx>
37 using namespace com::sun::star
;
38 using namespace com::sun::star::uno
;
39 using namespace com::sun::star::lang
;
40 using namespace com::sun::star::beans
;
41 using namespace com::sun::star::container
;
47 OUString
getHashKeyFromStrings( const OUString
& aCommandURL
, const OUString
& aModuleName
)
49 OUStringBuffer
aKey( aCommandURL
);
50 aKey
.appendAscii( "-" );
51 aKey
.append( aModuleName
);
52 return aKey
.makeStringAndClear();
55 // XInterface, XTypeProvider
57 ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory( const Reference
< XComponentContext
>& rxContext
, const OUString
& _sRoot
,bool _bAskValue
) :
58 m_aPropCommand( "Command" ),
59 m_aPropModule( "Module" ),
60 m_aPropController( "Controller" ),
61 m_aPropValue( "Value" ),
63 m_bConfigAccessInitialized( false ),
64 m_bAskValue(_bAskValue
)
66 m_xConfigProvider
= configuration::theDefaultProvider::get( rxContext
);
69 ConfigurationAccess_ControllerFactory::~ConfigurationAccess_ControllerFactory()
71 osl::MutexGuard
g(m_mutex
);
73 Reference
< XContainer
> xContainer( m_xConfigAccess
, UNO_QUERY
);
74 if ( xContainer
.is() )
75 xContainer
->removeContainerListener(m_xConfigAccessListener
);
78 OUString
ConfigurationAccess_ControllerFactory::getServiceFromCommandModule( const OUString
& rCommandURL
, const OUString
& rModule
) const
80 osl::MutexGuard
g(m_mutex
);
81 MenuControllerMap::const_iterator pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, rModule
));
83 if ( pIter
!= m_aMenuControllerMap
.end() )
84 return pIter
->second
.m_aImplementationName
;
85 else if ( !rModule
.isEmpty() )
87 // Try to detect if we have a generic popup menu controller
88 pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, OUString() ));
90 if ( pIter
!= m_aMenuControllerMap
.end() )
91 return pIter
->second
.m_aImplementationName
;
96 OUString
ConfigurationAccess_ControllerFactory::getValueFromCommandModule( const OUString
& rCommandURL
, const OUString
& rModule
) const
98 osl::MutexGuard
g(m_mutex
);
100 MenuControllerMap::const_iterator pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, rModule
));
102 if ( pIter
!= m_aMenuControllerMap
.end() )
103 return pIter
->second
.m_aValue
;
104 else if ( !rModule
.isEmpty() )
106 // Try to detect if we have a generic popup menu controller
107 pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, OUString() ));
109 if ( pIter
!= m_aMenuControllerMap
.end() )
110 return pIter
->second
.m_aValue
;
116 void ConfigurationAccess_ControllerFactory::addServiceToCommandModule(
117 const OUString
& rCommandURL
,
118 const OUString
& rModule
,
119 const OUString
& rServiceSpecifier
)
121 osl::MutexGuard
g(m_mutex
);
123 OUString aHashKey
= getHashKeyFromStrings( rCommandURL
, rModule
);
124 m_aMenuControllerMap
.insert( MenuControllerMap::value_type( aHashKey
,ControllerInfo(rServiceSpecifier
,OUString()) ));
127 void ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule(
128 const OUString
& rCommandURL
,
129 const OUString
& rModule
)
131 osl::MutexGuard
g(m_mutex
);
133 OUString aHashKey
= getHashKeyFromStrings( rCommandURL
, rModule
);
134 m_aMenuControllerMap
.erase( aHashKey
);
137 // container.XContainerListener
138 void SAL_CALL
ConfigurationAccess_ControllerFactory::elementInserted( const ContainerEvent
& aEvent
) throw(RuntimeException
, std::exception
)
145 osl::MutexGuard
g(m_mutex
);
147 if ( impl_getElementProps( aEvent
.Element
, aCommand
, aModule
, aService
, aValue
))
149 // Create hash key from command and module as they are together a primary key to
150 // the UNO service that implements the popup menu controller.
151 OUString
aHashKey( getHashKeyFromStrings( aCommand
, aModule
));
152 ControllerInfo
& rControllerInfo
= m_aMenuControllerMap
[ aHashKey
];
153 rControllerInfo
.m_aImplementationName
= aService
;
154 rControllerInfo
.m_aValue
= aValue
;
158 void SAL_CALL
ConfigurationAccess_ControllerFactory::elementRemoved ( const ContainerEvent
& aEvent
) throw(RuntimeException
, std::exception
)
165 osl::MutexGuard
g(m_mutex
);
167 if ( impl_getElementProps( aEvent
.Element
, aCommand
, aModule
, aService
, aValue
))
169 // Create hash key from command and module as they are together a primary key to
170 // the UNO service that implements the popup menu controller.
171 OUString
aHashKey( getHashKeyFromStrings( aCommand
, aModule
));
172 m_aMenuControllerMap
.erase( aHashKey
);
176 void SAL_CALL
ConfigurationAccess_ControllerFactory::elementReplaced( const ContainerEvent
& aEvent
) throw(RuntimeException
, std::exception
)
178 elementInserted(aEvent
);
181 // lang.XEventListener
182 void SAL_CALL
ConfigurationAccess_ControllerFactory::disposing( const EventObject
& ) throw(RuntimeException
, std::exception
)
184 // remove our reference to the config access
185 osl::MutexGuard
g(m_mutex
);
186 m_xConfigAccess
.clear();
189 void ConfigurationAccess_ControllerFactory::readConfigurationData()
192 osl::ClearableMutexGuard
aLock( m_mutex
);
194 if ( !m_bConfigAccessInitialized
)
196 Sequence
< Any
> aArgs( 1 );
197 PropertyValue aPropValue
;
199 aPropValue
.Name
= "nodepath";
200 aPropValue
.Value
<<= m_sRoot
;
201 aArgs
[0] <<= aPropValue
;
205 m_xConfigAccess
= Reference
< XNameAccess
>( m_xConfigProvider
->createInstanceWithArguments(SERVICENAME_CFGREADACCESS
,aArgs
), UNO_QUERY
);
207 catch ( const WrappedTargetException
& )
211 m_bConfigAccessInitialized
= true;
214 if ( m_xConfigAccess
.is() )
216 // Read and update configuration data
217 updateConfigurationData();
219 uno::Reference
< container::XContainer
> xContainer( m_xConfigAccess
, uno::UNO_QUERY
);
223 if ( xContainer
.is() )
225 m_xConfigAccessListener
= new WeakContainerListener(this);
226 xContainer
->addContainerListener(m_xConfigAccessListener
);
231 void ConfigurationAccess_ControllerFactory::updateConfigurationData()
233 osl::MutexGuard
g(m_mutex
);
234 if ( m_xConfigAccess
.is() )
236 Sequence
< OUString
> aPopupMenuControllers
= m_xConfigAccess
->getElementNames();
244 m_aMenuControllerMap
.clear();
245 for ( sal_Int32 i
= 0; i
< aPopupMenuControllers
.getLength(); i
++ )
249 if ( impl_getElementProps( m_xConfigAccess
->getByName( aPopupMenuControllers
[i
] ), aCommand
, aModule
, aService
,aValue
))
251 // Create hash key from command and module as they are together a primary key to
252 // the UNO service that implements the popup menu controller.
253 aHashKey
= getHashKeyFromStrings( aCommand
, aModule
);
254 m_aMenuControllerMap
.insert( MenuControllerMap::value_type( aHashKey
, ControllerInfo(aService
,aValue
) ));
257 catch ( const NoSuchElementException
& )
260 catch ( const WrappedTargetException
& )
267 bool ConfigurationAccess_ControllerFactory::impl_getElementProps( const Any
& aElement
, OUString
& aCommand
, OUString
& aModule
, OUString
& aServiceSpecifier
,OUString
& aValue
) const
269 Reference
< XPropertySet
> xPropertySet
;
270 aElement
>>= xPropertySet
;
272 if ( xPropertySet
.is() )
276 xPropertySet
->getPropertyValue( m_aPropCommand
) >>= aCommand
;
277 xPropertySet
->getPropertyValue( m_aPropModule
) >>= aModule
;
278 xPropertySet
->getPropertyValue( m_aPropController
) >>= aServiceSpecifier
;
280 xPropertySet
->getPropertyValue( m_aPropValue
) >>= aValue
;
282 catch ( const com::sun::star::beans::UnknownPropertyException
& )
286 catch ( const com::sun::star::lang::WrappedTargetException
& )
294 } // namespace framework
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */