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 <comphelper/propertysequence.hxx>
34 #include <cppuhelper/weak.hxx>
38 using namespace com::sun::star
;
39 using namespace com::sun::star::uno
;
40 using namespace com::sun::star::lang
;
41 using namespace com::sun::star::beans
;
42 using namespace com::sun::star::container
;
48 static OUString
getHashKeyFromStrings( const OUString
& aCommandURL
, const OUString
& aModuleName
)
50 return aCommandURL
+ "-" + aModuleName
;
53 // XInterface, XTypeProvider
55 ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory( const Reference
< XComponentContext
>& rxContext
, const OUString
& _sRoot
) :
56 m_aPropCommand( "Command" ),
57 m_aPropModule( "Module" ),
58 m_aPropController( "Controller" ),
59 m_aPropValue( "Value" ),
61 m_bConfigAccessInitialized( false )
63 m_xConfigProvider
= configuration::theDefaultProvider::get( rxContext
);
66 ConfigurationAccess_ControllerFactory::~ConfigurationAccess_ControllerFactory()
68 osl::MutexGuard
g(m_mutex
);
70 Reference
< XContainer
> xContainer( m_xConfigAccess
, UNO_QUERY
);
71 if ( xContainer
.is() )
72 xContainer
->removeContainerListener(m_xConfigAccessListener
);
75 OUString
ConfigurationAccess_ControllerFactory::getServiceFromCommandModule( const OUString
& rCommandURL
, const OUString
& rModule
) const
77 osl::MutexGuard
g(m_mutex
);
78 MenuControllerMap::const_iterator pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, rModule
));
80 if ( pIter
!= m_aMenuControllerMap
.end() )
81 return pIter
->second
.m_aImplementationName
;
82 else if ( !rModule
.isEmpty() )
84 // Try to detect if we have a generic popup menu controller
85 pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, OUString() ));
87 if ( pIter
!= m_aMenuControllerMap
.end() )
88 return pIter
->second
.m_aImplementationName
;
93 OUString
ConfigurationAccess_ControllerFactory::getValueFromCommandModule( const OUString
& rCommandURL
, const OUString
& rModule
) const
95 osl::MutexGuard
g(m_mutex
);
97 MenuControllerMap::const_iterator pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, rModule
));
99 if ( pIter
!= m_aMenuControllerMap
.end() )
100 return pIter
->second
.m_aValue
;
101 else if ( !rModule
.isEmpty() )
103 // Try to detect if we have a generic popup menu controller
104 pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, OUString() ));
106 if ( pIter
!= m_aMenuControllerMap
.end() )
107 return pIter
->second
.m_aValue
;
113 void ConfigurationAccess_ControllerFactory::addServiceToCommandModule(
114 const OUString
& rCommandURL
,
115 const OUString
& rModule
,
116 const OUString
& rServiceSpecifier
)
118 osl::MutexGuard
g(m_mutex
);
120 OUString aHashKey
= getHashKeyFromStrings( rCommandURL
, rModule
);
121 m_aMenuControllerMap
.emplace( aHashKey
,ControllerInfo(rServiceSpecifier
,OUString()) );
124 void ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule(
125 const OUString
& rCommandURL
,
126 const OUString
& rModule
)
128 osl::MutexGuard
g(m_mutex
);
130 OUString aHashKey
= getHashKeyFromStrings( rCommandURL
, rModule
);
131 m_aMenuControllerMap
.erase( aHashKey
);
134 // container.XContainerListener
135 void SAL_CALL
ConfigurationAccess_ControllerFactory::elementInserted( const ContainerEvent
& aEvent
)
142 osl::MutexGuard
g(m_mutex
);
144 if ( impl_getElementProps( aEvent
.Element
, aCommand
, aModule
, aService
, aValue
))
146 // Create hash key from command and module as they are together a primary key to
147 // the UNO service that implements the popup menu controller.
148 OUString
aHashKey( getHashKeyFromStrings( aCommand
, aModule
));
149 ControllerInfo
& rControllerInfo
= m_aMenuControllerMap
[ aHashKey
];
150 rControllerInfo
.m_aImplementationName
= aService
;
151 rControllerInfo
.m_aValue
= aValue
;
155 void SAL_CALL
ConfigurationAccess_ControllerFactory::elementRemoved ( const ContainerEvent
& aEvent
)
162 osl::MutexGuard
g(m_mutex
);
164 if ( impl_getElementProps( aEvent
.Element
, aCommand
, aModule
, aService
, aValue
))
166 // Create hash key from command and module as they are together a primary key to
167 // the UNO service that implements the popup menu controller.
168 OUString
aHashKey( getHashKeyFromStrings( aCommand
, aModule
));
169 m_aMenuControllerMap
.erase( aHashKey
);
173 void SAL_CALL
ConfigurationAccess_ControllerFactory::elementReplaced( const ContainerEvent
& aEvent
)
175 elementInserted(aEvent
);
178 // lang.XEventListener
179 void SAL_CALL
ConfigurationAccess_ControllerFactory::disposing( const EventObject
& )
181 // remove our reference to the config access
182 osl::MutexGuard
g(m_mutex
);
183 m_xConfigAccess
.clear();
186 void ConfigurationAccess_ControllerFactory::readConfigurationData()
189 osl::ClearableMutexGuard
aLock( m_mutex
);
191 if ( !m_bConfigAccessInitialized
)
193 uno::Sequence
<uno::Any
> aArgs(comphelper::InitAnyPropertySequence(
195 {"nodepath", uno::Any(m_sRoot
)}
199 m_xConfigAccess
.set( m_xConfigProvider
->createInstanceWithArguments(SERVICENAME_CFGREADACCESS
,aArgs
), UNO_QUERY
);
201 catch ( const WrappedTargetException
& )
205 m_bConfigAccessInitialized
= true;
208 if ( m_xConfigAccess
.is() )
210 // Read and update configuration data
211 updateConfigurationData();
213 uno::Reference
< container::XContainer
> xContainer( m_xConfigAccess
, uno::UNO_QUERY
);
217 if ( xContainer
.is() )
219 m_xConfigAccessListener
= new WeakContainerListener(this);
220 xContainer
->addContainerListener(m_xConfigAccessListener
);
225 void ConfigurationAccess_ControllerFactory::updateConfigurationData()
227 osl::MutexGuard
g(m_mutex
);
228 if ( m_xConfigAccess
.is() )
230 Sequence
< OUString
> aPopupMenuControllers
= m_xConfigAccess
->getElementNames();
238 m_aMenuControllerMap
.clear();
239 for ( sal_Int32 i
= 0; i
< aPopupMenuControllers
.getLength(); i
++ )
243 if ( impl_getElementProps( m_xConfigAccess
->getByName( aPopupMenuControllers
[i
] ), aCommand
, aModule
, aService
,aValue
))
245 // Create hash key from command and module as they are together a primary key to
246 // the UNO service that implements the popup menu controller.
247 aHashKey
= getHashKeyFromStrings( aCommand
, aModule
);
248 m_aMenuControllerMap
.emplace( aHashKey
, ControllerInfo(aService
,aValue
) );
251 catch ( const NoSuchElementException
& )
254 catch ( const WrappedTargetException
& )
261 bool ConfigurationAccess_ControllerFactory::impl_getElementProps( const Any
& aElement
, OUString
& aCommand
, OUString
& aModule
, OUString
& aServiceSpecifier
,OUString
& aValue
) const
263 Reference
< XPropertySet
> xPropertySet
;
264 aElement
>>= xPropertySet
;
266 if ( xPropertySet
.is() )
270 xPropertySet
->getPropertyValue( m_aPropCommand
) >>= aCommand
;
271 xPropertySet
->getPropertyValue( m_aPropModule
) >>= aModule
;
272 xPropertySet
->getPropertyValue( m_aPropController
) >>= aServiceSpecifier
;
273 xPropertySet
->getPropertyValue( m_aPropValue
) >>= aValue
;
275 catch ( const css::beans::UnknownPropertyException
& )
279 catch ( const css::lang::WrappedTargetException
& )
287 } // namespace framework
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */