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: Configuration.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 #include "precompiled_sd.hxx"
33 #include "framework/Configuration.hxx"
35 #include "framework/FrameworkHelper.hxx"
36 #include <comphelper/stl_types.hxx>
40 using namespace ::com::sun::star
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star::drawing::framework
;
43 using ::sd::framework::FrameworkHelper
;
44 using ::rtl::OUString
;
49 /** Use the XResourceId::compareTo() method to implement a compare operator
53 : public ::std::binary_function
<Reference
<XResourceId
>, Reference
<XResourceId
>, bool>
56 bool operator () (const Reference
<XResourceId
>& rId1
, const Reference
<XResourceId
>& rId2
) const
58 return rId1
->compareTo(rId2
) == -1;
62 } // end of anonymous namespace
67 namespace sd
{ namespace framework
{
70 class Configuration::ResourceContainer
71 : public ::std::set
<Reference
<XResourceId
>, XResourceIdLess
>
74 ResourceContainer (void) {}
80 //----- Service ---------------------------------------------------------------
82 Reference
<XInterface
> SAL_CALL
Configuration_createInstance (
83 const Reference
<XComponentContext
>& rxContext
)
86 return Reference
<XInterface
>(static_cast<XWeak
*>(new Configuration(NULL
,false)));
92 OUString
Configuration_getImplementationName (void) throw(RuntimeException
)
94 return OUString(RTL_CONSTASCII_USTRINGPARAM(
95 "com.sun.star.comp.Draw.framework.configuration.Configuration"));
101 Sequence
<rtl::OUString
> SAL_CALL
Configuration_getSupportedServiceNames (void)
102 throw (RuntimeException
)
104 static const OUString
sServiceName(OUString::createFromAscii(
105 "com.sun.star.drawing.framework.Configuration"));
106 return Sequence
<rtl::OUString
>(&sServiceName
, 1);
112 //===== Configuration =========================================================
114 Configuration::Configuration (
115 const Reference
<XConfigurationControllerBroadcaster
>& rxBroadcaster
,
116 bool bBroadcastRequestEvents
)
117 : ConfigurationInterfaceBase(MutexOwner::maMutex
),
118 mpResourceContainer(new ResourceContainer()),
119 mxBroadcaster(rxBroadcaster
),
120 mbBroadcastRequestEvents(bBroadcastRequestEvents
)
126 Configuration::Configuration (
127 const Reference
<XConfigurationControllerBroadcaster
>& rxBroadcaster
,
128 bool bBroadcastRequestEvents
,
129 const ResourceContainer
& rResourceContainer
)
130 : ConfigurationInterfaceBase(MutexOwner::maMutex
),
131 mpResourceContainer(new ResourceContainer(rResourceContainer
)),
132 mxBroadcaster(rxBroadcaster
),
133 mbBroadcastRequestEvents(bBroadcastRequestEvents
)
140 Configuration::~Configuration (void)
147 void SAL_CALL
Configuration::disposing (void)
149 ::osl::MutexGuard
aGuard (maMutex
);
150 mpResourceContainer
->clear();
151 mxBroadcaster
= NULL
;
157 //----- XConfiguration --------------------------------------------------------
159 void SAL_CALL
Configuration::addResource (const Reference
<XResourceId
>& rxResourceId
)
160 throw (RuntimeException
)
164 if ( ! rxResourceId
.is() || rxResourceId
->getResourceURL().getLength()==0)
165 throw ::com::sun::star::lang::IllegalArgumentException();
167 if (mpResourceContainer
->find(rxResourceId
) == mpResourceContainer
->end())
170 OSL_TRACE("Configuration::addResource() %s",
172 FrameworkHelper::ResourceIdToString(rxResourceId
), RTL_TEXTENCODING_UTF8
).getStr());
174 mpResourceContainer
->insert(rxResourceId
);
175 PostEvent(rxResourceId
, true);
182 void SAL_CALL
Configuration::removeResource (const Reference
<XResourceId
>& rxResourceId
)
183 throw (RuntimeException
)
187 if ( ! rxResourceId
.is() || rxResourceId
->getResourceURL().getLength()==0)
188 throw ::com::sun::star::lang::IllegalArgumentException();
190 ResourceContainer::iterator
iResource (mpResourceContainer
->find(rxResourceId
));
191 if (iResource
!= mpResourceContainer
->end())
194 OSL_TRACE("Configuration::removeResource() %s",
196 FrameworkHelper::ResourceIdToString(rxResourceId
), RTL_TEXTENCODING_UTF8
).getStr());
198 PostEvent(rxResourceId
,false);
199 mpResourceContainer
->erase(iResource
);
206 Sequence
<Reference
<XResourceId
> > SAL_CALL
Configuration::getResources (
207 const Reference
<XResourceId
>& rxAnchorId
,
208 const ::rtl::OUString
& rsResourceURLPrefix
,
209 AnchorBindingMode eMode
)
210 throw (::com::sun::star::uno::RuntimeException
)
212 ::osl::MutexGuard
aGuard (maMutex
);
215 bool bFilterResources (rsResourceURLPrefix
.getLength() > 0);
217 // Collect the matching resources in a vector.
218 ::std::vector
<Reference
<XResourceId
> > aResources
;
219 ResourceContainer::const_iterator iResource
;
220 for (iResource
=mpResourceContainer
->begin();
221 iResource
!=mpResourceContainer
->end();
224 if ( ! (*iResource
)->isBoundTo(rxAnchorId
,eMode
))
228 if (bFilterResources
)
230 // Apply the given resource prefix as filter.
232 // Make sure that the resource is bound directly to the anchor.
233 if (eMode
!= AnchorBindingMode_DIRECT
234 && ! (*iResource
)->isBoundTo(rxAnchorId
, AnchorBindingMode_DIRECT
))
239 // Make sure that the resource URL matches the given prefix.
240 if ( ! (*iResource
)->getResourceURL().match(rsResourceURLPrefix
))
246 aResources
.push_back(*iResource
);
249 // Copy the resources from the vector into a new sequence.
250 Sequence
<Reference
<XResourceId
> > aResult (aResources
.size());
251 for (sal_uInt32 nIndex
=0; nIndex
<aResources
.size(); ++nIndex
)
252 aResult
[nIndex
] = aResources
[nIndex
];
260 sal_Bool SAL_CALL
Configuration::hasResource (const Reference
<XResourceId
>& rxResourceId
)
261 throw (RuntimeException
)
263 ::osl::MutexGuard
aGuard (maMutex
);
266 return rxResourceId
.is()
267 && mpResourceContainer
->find(rxResourceId
) != mpResourceContainer
->end();
273 //----- XCloneable ------------------------------------------------------------
275 Reference
<util::XCloneable
> SAL_CALL
Configuration::createClone (void)
276 throw (RuntimeException
)
278 ::osl::MutexGuard
aGuard (maMutex
);
281 Configuration
* pConfiguration
= new Configuration(
283 mbBroadcastRequestEvents
,
284 *mpResourceContainer
);
286 return Reference
<util::XCloneable
>(pConfiguration
);
292 //----- XNamed ----------------------------------------------------------------
294 OUString SAL_CALL
Configuration::getName (void)
295 throw (RuntimeException
)
297 ::osl::MutexGuard
aGuard (maMutex
);
300 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
301 aString
+= OUString::createFromAscii("DISPOSED ");
302 aString
+= OUString::createFromAscii("Configuration[");
304 ResourceContainer::const_iterator iResource
;
305 for (iResource
=mpResourceContainer
->begin();
306 iResource
!=mpResourceContainer
->end();
309 if (iResource
!= mpResourceContainer
->begin())
310 aString
+= OUString::createFromAscii(", ");
311 aString
+= FrameworkHelper::ResourceIdToString(*iResource
);
313 aString
+= OUString::createFromAscii("]");
321 void SAL_CALL
Configuration::setName (const OUString
& rsName
)
322 throw (RuntimeException
)
324 (void)rsName
; // rsName is ignored.
331 // ----------------------------------------------------------------------------
333 void Configuration::PostEvent (
334 const Reference
<XResourceId
>& rxResourceId
,
335 const bool bActivation
)
337 OSL_ASSERT(rxResourceId
.is());
339 if (mxBroadcaster
.is())
341 ConfigurationChangeEvent aEvent
;
342 aEvent
.ResourceId
= rxResourceId
;
344 if (mbBroadcastRequestEvents
)
345 aEvent
.Type
= FrameworkHelper::msResourceActivationRequestEvent
;
347 aEvent
.Type
= FrameworkHelper::msResourceActivationEvent
;
349 if (mbBroadcastRequestEvents
)
350 aEvent
.Type
= FrameworkHelper::msResourceDeactivationRequestEvent
;
352 aEvent
.Type
= FrameworkHelper::msResourceDeactivationEvent
;
353 aEvent
.Configuration
= this;
355 mxBroadcaster
->notifyEvent(aEvent
);
362 void Configuration::ThrowIfDisposed (void) const
363 throw (::com::sun::star::lang::DisposedException
)
365 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
367 throw lang::DisposedException (
368 OUString(RTL_CONSTASCII_USTRINGPARAM(
369 "Configuration object has already been disposed")),
370 const_cast<uno::XWeak
*>(static_cast<const uno::XWeak
*>(this)));
377 //=============================================================================
379 bool AreConfigurationsEquivalent (
380 const Reference
<XConfiguration
>& rxConfiguration1
,
381 const Reference
<XConfiguration
>& rxConfiguration2
)
383 if (rxConfiguration1
.is() != rxConfiguration2
.is())
385 if ( ! rxConfiguration1
.is() && ! rxConfiguration2
.is())
388 // Get the lists of resources from the two given configurations.
389 const Sequence
<Reference
<XResourceId
> > aResources1(
390 rxConfiguration1
->getResources(
391 NULL
, OUString(), AnchorBindingMode_INDIRECT
));
392 const Sequence
<Reference
<XResourceId
> > aResources2(
393 rxConfiguration2
->getResources(
394 NULL
, OUString(), AnchorBindingMode_INDIRECT
));
396 // When the number of resources differ then the configurations can not
398 const sal_Int32
nCount (aResources1
.getLength());
399 const sal_Int32
nCount2 (aResources2
.getLength());
400 if (nCount
!= nCount2
)
403 // Comparison of the two lists of resource ids relies on their
405 for (sal_Int32 nIndex
=0; nIndex
<nCount
; ++nIndex
)
407 const Reference
<XResourceId
> xResource1 (aResources1
[nIndex
]);
408 const Reference
<XResourceId
> xResource2 (aResources2
[nIndex
]);
409 if (xResource1
.is() && xResource2
.is())
411 if (xResource1
->compareTo(xResource2
) != 0)
414 else if (xResource1
.is() != xResource2
.is())
423 } } // end of namespace sd::framework