LibreOffice Module sd (master) 1
Configuration.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
23
24#include <com/sun/star/drawing/framework/ConfigurationChangeEvent.hpp>
25#include <com/sun/star/drawing/framework/XConfigurationControllerBroadcaster.hpp>
28#include <rtl/ustrbuf.hxx>
29#include <sal/log.hxx>
30
31using namespace ::com::sun::star;
32using namespace ::com::sun::star::uno;
34using ::sd::framework::FrameworkHelper;
35
36namespace {
40class XResourceIdLess
41{
42public:
43 bool operator () (const Reference<XResourceId>& rId1, const Reference<XResourceId>& rId2) const
44 {
45 return rId1->compareTo(rId2) == -1;
46 }
47};
48
49} // end of anonymous namespace
50
51namespace sd::framework {
52
54 : public ::std::set<Reference<XResourceId>, XResourceIdLess>
55{
56public:
58};
59
60//===== Configuration =========================================================
61
63 const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster,
64 bool bBroadcastRequestEvents)
65 : mpResourceContainer(new ResourceContainer()),
66 mxBroadcaster(rxBroadcaster),
67 mbBroadcastRequestEvents(bBroadcastRequestEvents)
68{
69}
70
72 const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster,
73 bool bBroadcastRequestEvents,
74 const ResourceContainer& rResourceContainer)
75 : mpResourceContainer(new ResourceContainer(rResourceContainer)),
76 mxBroadcaster(rxBroadcaster),
77 mbBroadcastRequestEvents(bBroadcastRequestEvents)
78{
79}
80
82{
83}
84
85void Configuration::disposing(std::unique_lock<std::mutex>&)
86{
87 mpResourceContainer->clear();
88 mxBroadcaster = nullptr;
89}
90
91//----- XConfiguration --------------------------------------------------------
92
93void SAL_CALL Configuration::addResource (const Reference<XResourceId>& rxResourceId)
94{
96
97 if ( ! rxResourceId.is() || rxResourceId->getResourceURL().isEmpty())
98 throw css::lang::IllegalArgumentException();
99
100 if (mpResourceContainer->insert(rxResourceId).second)
101 {
102 SAL_INFO("sd.fwk", __func__ << ": Configuration::addResource() " <<
104 PostEvent(rxResourceId, true);
105 }
106}
107
108void SAL_CALL Configuration::removeResource (const Reference<XResourceId>& rxResourceId)
109{
111
112 if ( ! rxResourceId.is() || rxResourceId->getResourceURL().isEmpty())
113 throw css::lang::IllegalArgumentException();
114
115 ResourceContainer::iterator iResource (mpResourceContainer->find(rxResourceId));
116 if (iResource != mpResourceContainer->end())
117 {
118 SAL_INFO("sd.fwk", __func__ << ": Configuration::removeResource() " <<
120 PostEvent(rxResourceId,false);
121 mpResourceContainer->erase(iResource);
122 }
123}
124
125Sequence<Reference<XResourceId> > SAL_CALL Configuration::getResources (
126 const Reference<XResourceId>& rxAnchorId,
127 const OUString& rsResourceURLPrefix,
128 AnchorBindingMode eMode)
129{
130 std::unique_lock aGuard (m_aMutex);
132
133 const bool bFilterResources (!rsResourceURLPrefix.isEmpty());
134
135 // Collect the matching resources in a vector.
136 ::std::vector<Reference<XResourceId> > aResources;
137 for (const auto& rxResource : *mpResourceContainer)
138 {
139 if ( ! rxResource->isBoundTo(rxAnchorId,eMode))
140 continue;
141
142 if (bFilterResources)
143 {
144 // Apply the given resource prefix as filter.
145
146 // Make sure that the resource is bound directly to the anchor.
147 if (eMode != AnchorBindingMode_DIRECT
148 && ! rxResource->isBoundTo(rxAnchorId, AnchorBindingMode_DIRECT))
149 {
150 continue;
151 }
152
153 // Make sure that the resource URL matches the given prefix.
154 if ( ! rxResource->getResourceURL().match(rsResourceURLPrefix))
155 {
156 continue;
157 }
158 }
159
160 aResources.push_back(rxResource);
161 }
162
163 return comphelper::containerToSequence(aResources);
164}
165
166sal_Bool SAL_CALL Configuration::hasResource (const Reference<XResourceId>& rxResourceId)
167{
168 std::unique_lock aGuard (m_aMutex);
170
171 return rxResourceId.is()
172 && mpResourceContainer->find(rxResourceId) != mpResourceContainer->end();
173}
174
175//----- XCloneable ------------------------------------------------------------
176
177Reference<util::XCloneable> SAL_CALL Configuration::createClone()
178{
179 std::unique_lock aGuard (m_aMutex);
181
182 return new Configuration(
186}
187
188//----- XNamed ----------------------------------------------------------------
189
190OUString SAL_CALL Configuration::getName()
191{
192 std::unique_lock aGuard (m_aMutex);
193 OUStringBuffer aString;
194
195 if (m_bDisposed)
196 aString.append("DISPOSED ");
197 aString.append("Configuration[");
198
199 ResourceContainer::const_iterator iResource;
200 for (iResource=mpResourceContainer->begin();
201 iResource!=mpResourceContainer->end();
202 ++iResource)
203 {
204 if (iResource != mpResourceContainer->begin())
205 aString.append(", ");
206 aString.append(FrameworkHelper::ResourceIdToString(*iResource));
207 }
208 aString.append("]");
209
210 return aString.makeStringAndClear();
211}
212
213void SAL_CALL Configuration::setName (const OUString&)
214{
215 // ignored.
216}
217
219 const Reference<XResourceId>& rxResourceId,
220 const bool bActivation)
221{
222 OSL_ASSERT(rxResourceId.is());
223
224 if (!mxBroadcaster.is())
225 return;
226
227 ConfigurationChangeEvent aEvent;
228 aEvent.ResourceId = rxResourceId;
229 if (bActivation)
232 else
234 else
237 else
239 aEvent.Configuration = this;
240
241 mxBroadcaster->notifyEvent(aEvent);
242}
243
245{
246 if (m_bDisposed)
247 {
248 throw lang::DisposedException ("Configuration object has already been disposed",
249 const_cast<uno::XWeak*>(static_cast<const uno::XWeak*>(this)));
250 }
251}
252
254 const Reference<XConfiguration>& rxConfiguration1,
255 const Reference<XConfiguration>& rxConfiguration2)
256{
257 if (rxConfiguration1.is() != rxConfiguration2.is())
258 return false;
259 if ( ! rxConfiguration1.is() && ! rxConfiguration2.is())
260 return true;
261
262 // Get the lists of resources from the two given configurations.
263 const Sequence<Reference<XResourceId> > aResources1(
264 rxConfiguration1->getResources(
265 nullptr, OUString(), AnchorBindingMode_INDIRECT));
266 const Sequence<Reference<XResourceId> > aResources2(
267 rxConfiguration2->getResources(
268 nullptr, OUString(), AnchorBindingMode_INDIRECT));
269
270 // When the number of resources differ then the configurations can not
271 // be equivalent.
272 // Comparison of the two lists of resource ids relies on their
273 // ordering.
274 return std::equal(aResources1.begin(), aResources1.end(), aResources2.begin(), aResources2.end(),
275 [](const Reference<XResourceId>& a, const Reference<XResourceId>& b) {
276 if (a.is() && b.is())
277 return a->compareTo(b) == 0;
278 return a.is() == b.is();
279 });
280}
281
282} // end of namespace sd::framework
283
284
285/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
virtual css::uno::Sequence< css::uno::Reference< css::drawing::framework::XResourceId > > SAL_CALL getResources(const css::uno::Reference< css::drawing::framework::XResourceId > &rxAnchorId, const OUString &rsResourceURLPrefix, css::drawing::framework::AnchorBindingMode eMode) override
virtual OUString SAL_CALL getName() override
Return a human readable string representation.
std::unique_ptr< ResourceContainer > mpResourceContainer
The resource container holds the URLs of unique resource and of resource linked to unique resources.
virtual void SAL_CALL removeResource(const css::uno::Reference< css::drawing::framework::XResourceId > &rxResourceId) override
virtual void SAL_CALL addResource(const css::uno::Reference< css::drawing::framework::XResourceId > &rxResourceId) override
void ThrowIfDisposed() const
When the called object has already been disposed this method throws an exception and does not return.
virtual ~Configuration() override
void PostEvent(const css::uno::Reference< css::drawing::framework::XResourceId > &rxResourceId, const bool bActivation)
Send an event to all interested listeners that a resource has been added or removed.
css::uno::Reference< css::drawing::framework::XConfigurationControllerBroadcaster > mxBroadcaster
The broadcaster used for notifying listeners of requests for configuration changes.
Configuration(const css::uno::Reference< css::drawing::framework::XConfigurationControllerBroadcaster > &rxBroadcaster, bool bBroadcastRequestEvents)
Create a new configuration with a broadcaster that is used to send events about requested configurati...
virtual sal_Bool SAL_CALL hasResource(const css::uno::Reference< css::drawing::framework::XResourceId > &rxResourceId) override
virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone() override
virtual void disposing(std::unique_lock< std::mutex > &) override
virtual void SAL_CALL setName(const OUString &rName) override
This call is ignored because the XNamed interface is (mis)used to give access to a human readable nam...
static constexpr OUStringLiteral msResourceDeactivationEvent
static constexpr OUStringLiteral msResourceActivationRequestEvent
static OUString ResourceIdToString(const css::uno::Reference< css::drawing::framework::XResourceId > &rxResourceId)
Return a string representation of the given XResourceId object.
static constexpr OUStringLiteral msResourceDeactivationRequestEvent
static constexpr OUStringLiteral msResourceActivationEvent
Mode eMode
uno_Any a
#define SAL_INFO(area, stream)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
bool AreConfigurationsEquivalent(const Reference< XConfiguration > &rxConfiguration1, const Reference< XConfiguration > &rxConfiguration2)
unsigned char sal_Bool