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 "framework/Configuration.hxx"
22 #include "framework/FrameworkHelper.hxx"
26 using namespace ::com::sun::star
;
27 using namespace ::com::sun::star::uno
;
28 using namespace ::com::sun::star::drawing::framework
;
29 using ::sd::framework::FrameworkHelper
;
32 /** Use the XResourceId::compareTo() method to implement a compare operator
36 : public ::std::binary_function
<Reference
<XResourceId
>, Reference
<XResourceId
>, bool>
39 bool operator () (const Reference
<XResourceId
>& rId1
, const Reference
<XResourceId
>& rId2
) const
41 return rId1
->compareTo(rId2
) == -1;
45 } // end of anonymous namespace
47 namespace sd
{ namespace framework
{
49 class Configuration::ResourceContainer
50 : public ::std::set
<Reference
<XResourceId
>, XResourceIdLess
>
53 ResourceContainer() {}
56 //===== Configuration =========================================================
58 Configuration::Configuration (
59 const Reference
<XConfigurationControllerBroadcaster
>& rxBroadcaster
,
60 bool bBroadcastRequestEvents
)
61 : ConfigurationInterfaceBase(MutexOwner::maMutex
),
62 mpResourceContainer(new ResourceContainer()),
63 mxBroadcaster(rxBroadcaster
),
64 mbBroadcastRequestEvents(bBroadcastRequestEvents
)
68 Configuration::Configuration (
69 const Reference
<XConfigurationControllerBroadcaster
>& rxBroadcaster
,
70 bool bBroadcastRequestEvents
,
71 const ResourceContainer
& rResourceContainer
)
72 : ConfigurationInterfaceBase(MutexOwner::maMutex
),
73 mpResourceContainer(new ResourceContainer(rResourceContainer
)),
74 mxBroadcaster(rxBroadcaster
),
75 mbBroadcastRequestEvents(bBroadcastRequestEvents
)
79 Configuration::~Configuration()
83 void SAL_CALL
Configuration::disposing()
85 ::osl::MutexGuard
aGuard (maMutex
);
86 mpResourceContainer
->clear();
90 //----- XConfiguration --------------------------------------------------------
92 void SAL_CALL
Configuration::addResource (const Reference
<XResourceId
>& rxResourceId
)
93 throw (RuntimeException
, std::exception
)
97 if ( ! rxResourceId
.is() || rxResourceId
->getResourceURL().isEmpty())
98 throw ::com::sun::star::lang::IllegalArgumentException();
100 if (mpResourceContainer
->find(rxResourceId
) == mpResourceContainer
->end())
102 SAL_INFO("sd.fwk", OSL_THIS_FUNC
<< ": Configuration::addResource() " <<
104 FrameworkHelper::ResourceIdToString(rxResourceId
), RTL_TEXTENCODING_UTF8
).getStr());
105 mpResourceContainer
->insert(rxResourceId
);
106 PostEvent(rxResourceId
, true);
110 void SAL_CALL
Configuration::removeResource (const Reference
<XResourceId
>& rxResourceId
)
111 throw (RuntimeException
, std::exception
)
115 if ( ! rxResourceId
.is() || rxResourceId
->getResourceURL().isEmpty())
116 throw ::com::sun::star::lang::IllegalArgumentException();
118 ResourceContainer::iterator
iResource (mpResourceContainer
->find(rxResourceId
));
119 if (iResource
!= mpResourceContainer
->end())
121 SAL_INFO("sd.fwk", OSL_THIS_FUNC
<< ": Configuration::removeResource() " <<
123 FrameworkHelper::ResourceIdToString(rxResourceId
), RTL_TEXTENCODING_UTF8
).getStr());
124 PostEvent(rxResourceId
,false);
125 mpResourceContainer
->erase(iResource
);
129 Sequence
<Reference
<XResourceId
> > SAL_CALL
Configuration::getResources (
130 const Reference
<XResourceId
>& rxAnchorId
,
131 const OUString
& rsResourceURLPrefix
,
132 AnchorBindingMode eMode
)
133 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
135 ::osl::MutexGuard
aGuard (maMutex
);
138 bool bFilterResources (!rsResourceURLPrefix
.isEmpty());
140 // Collect the matching resources in a vector.
141 ::std::vector
<Reference
<XResourceId
> > aResources
;
142 ResourceContainer::const_iterator iResource
;
143 for (iResource
=mpResourceContainer
->begin();
144 iResource
!=mpResourceContainer
->end();
147 if ( ! (*iResource
)->isBoundTo(rxAnchorId
,eMode
))
150 if (bFilterResources
)
152 // Apply the given resource prefix as filter.
154 // Make sure that the resource is bound directly to the anchor.
155 if (eMode
!= AnchorBindingMode_DIRECT
156 && ! (*iResource
)->isBoundTo(rxAnchorId
, AnchorBindingMode_DIRECT
))
161 // Make sure that the resource URL matches the given prefix.
162 if ( ! (*iResource
)->getResourceURL().match(rsResourceURLPrefix
))
168 aResources
.push_back(*iResource
);
171 // Copy the resources from the vector into a new sequence.
172 Sequence
<Reference
<XResourceId
> > aResult (aResources
.size());
173 for (sal_uInt32 nIndex
=0; nIndex
<aResources
.size(); ++nIndex
)
174 aResult
[nIndex
] = aResources
[nIndex
];
179 sal_Bool SAL_CALL
Configuration::hasResource (const Reference
<XResourceId
>& rxResourceId
)
180 throw (RuntimeException
, std::exception
)
182 ::osl::MutexGuard
aGuard (maMutex
);
185 return rxResourceId
.is()
186 && mpResourceContainer
->find(rxResourceId
) != mpResourceContainer
->end();
189 //----- XCloneable ------------------------------------------------------------
191 Reference
<util::XCloneable
> SAL_CALL
Configuration::createClone()
192 throw (RuntimeException
, std::exception
)
194 ::osl::MutexGuard
aGuard (maMutex
);
197 Configuration
* pConfiguration
= new Configuration(
199 mbBroadcastRequestEvents
,
200 *mpResourceContainer
);
202 return Reference
<util::XCloneable
>(pConfiguration
);
205 //----- XNamed ----------------------------------------------------------------
207 OUString SAL_CALL
Configuration::getName()
208 throw (RuntimeException
, std::exception
)
210 ::osl::MutexGuard
aGuard (maMutex
);
213 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
214 aString
+= "DISPOSED ";
215 aString
+= "Configuration[";
217 ResourceContainer::const_iterator iResource
;
218 for (iResource
=mpResourceContainer
->begin();
219 iResource
!=mpResourceContainer
->end();
222 if (iResource
!= mpResourceContainer
->begin())
224 aString
+= FrameworkHelper::ResourceIdToString(*iResource
);
231 void SAL_CALL
Configuration::setName (const OUString
& rsName
)
232 throw (RuntimeException
, std::exception
)
234 (void)rsName
; // rsName is ignored.
237 OUString
Configuration::getImplementationName()
238 throw (css::uno::RuntimeException
, std::exception
)
241 "com.sun.star.comp.Draw.framework.configuration.Configuration");
244 sal_Bool
Configuration::supportsService(OUString
const & ServiceName
)
245 throw (css::uno::RuntimeException
, std::exception
)
247 return cppu::supportsService(this, ServiceName
);
250 css::uno::Sequence
<OUString
> Configuration::getSupportedServiceNames()
251 throw (css::uno::RuntimeException
, std::exception
)
253 return css::uno::Sequence
<OUString
>{
254 "com.sun.star.drawing.framework.Configuration"};
257 void Configuration::PostEvent (
258 const Reference
<XResourceId
>& rxResourceId
,
259 const bool bActivation
)
261 OSL_ASSERT(rxResourceId
.is());
263 if (mxBroadcaster
.is())
265 ConfigurationChangeEvent aEvent
;
266 aEvent
.ResourceId
= rxResourceId
;
268 if (mbBroadcastRequestEvents
)
269 aEvent
.Type
= FrameworkHelper::msResourceActivationRequestEvent
;
271 aEvent
.Type
= FrameworkHelper::msResourceActivationEvent
;
273 if (mbBroadcastRequestEvents
)
274 aEvent
.Type
= FrameworkHelper::msResourceDeactivationRequestEvent
;
276 aEvent
.Type
= FrameworkHelper::msResourceDeactivationEvent
;
277 aEvent
.Configuration
= this;
279 mxBroadcaster
->notifyEvent(aEvent
);
283 void Configuration::ThrowIfDisposed() const
284 throw (::com::sun::star::lang::DisposedException
)
286 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
288 throw lang::DisposedException ("Configuration object has already been disposed",
289 const_cast<uno::XWeak
*>(static_cast<const uno::XWeak
*>(this)));
293 bool AreConfigurationsEquivalent (
294 const Reference
<XConfiguration
>& rxConfiguration1
,
295 const Reference
<XConfiguration
>& rxConfiguration2
)
297 if (rxConfiguration1
.is() != rxConfiguration2
.is())
299 if ( ! rxConfiguration1
.is() && ! rxConfiguration2
.is())
302 // Get the lists of resources from the two given configurations.
303 const Sequence
<Reference
<XResourceId
> > aResources1(
304 rxConfiguration1
->getResources(
305 NULL
, OUString(), AnchorBindingMode_INDIRECT
));
306 const Sequence
<Reference
<XResourceId
> > aResources2(
307 rxConfiguration2
->getResources(
308 NULL
, OUString(), AnchorBindingMode_INDIRECT
));
310 // When the number of resources differ then the configurations can not
312 const sal_Int32
nCount (aResources1
.getLength());
313 const sal_Int32
nCount2 (aResources2
.getLength());
314 if (nCount
!= nCount2
)
317 // Comparison of the two lists of resource ids relies on their
319 for (sal_Int32 nIndex
=0; nIndex
<nCount
; ++nIndex
)
321 const Reference
<XResourceId
> xResource1 (aResources1
[nIndex
]);
322 const Reference
<XResourceId
> xResource2 (aResources2
[nIndex
]);
323 if (xResource1
.is() && xResource2
.is())
325 if (xResource1
->compareTo(xResource2
) != 0)
328 else if (xResource1
.is() != xResource2
.is())
337 } } // end of namespace sd::framework
340 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface
* SAL_CALL
341 com_sun_star_comp_Draw_framework_configuration_Configuration_get_implementation(
342 ::com::sun::star::uno::XComponentContext
*,
343 ::com::sun::star::uno::Sequence
<css::uno::Any
> const &)
345 return cppu::acquire(new sd::framework::Configuration(NULL
, false));
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */