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 .
21 #include "basecontainer.hxx"
23 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
24 #include <com/sun/star/uno/Type.h>
25 #include <comphelper/enumhelper.hxx>
26 #include <comphelper/sequence.hxx>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <osl/diagnose.h>
30 namespace filter::config
{
32 BaseContainer::BaseContainer()
35 GetTheFilterCache().load(FilterCache::E_CONTAINS_STANDARD
);
39 BaseContainer::~BaseContainer()
44 void BaseContainer::init(const OUString
& sImplementationName
,
45 const css::uno::Sequence
< OUString
>& lServiceNames
,
46 FilterCache::EItemType eType
)
48 m_sImplementationName
= sImplementationName
;
49 m_lServiceNames
= lServiceNames
;
54 void BaseContainer::impl_loadOnDemand(std::unique_lock
<std::mutex
>& /*rGuard*/)
56 // A generic container needs all items of a set of our cache!
57 // Of course it can block for a while, till the cache is really filled.
58 // Note: don't load all sets supported by the cache here!
60 FilterCache::EFillState eRequiredState
= FilterCache::E_CONTAINS_NOTHING
;
63 case FilterCache::E_TYPE
:
64 eRequiredState
= FilterCache::E_CONTAINS_TYPES
;
67 case FilterCache::E_FILTER
:
68 eRequiredState
= FilterCache::E_CONTAINS_FILTERS
;
71 case FilterCache::E_FRAMELOADER
:
72 eRequiredState
= FilterCache::E_CONTAINS_FRAMELOADERS
;
75 case FilterCache::E_CONTENTHANDLER
:
76 eRequiredState
= FilterCache::E_CONTAINS_CONTENTHANDLERS
;
80 GetTheFilterCache().load(eRequiredState
);
84 void BaseContainer::impl_initFlushMode(std::unique_lock
<std::mutex
>& /*rGuard*/)
87 m_pFlushCache
= GetTheFilterCache().clone();
89 throw css::uno::RuntimeException( u
"Can not create write copy of internal used cache on demand."_ustr
,
94 FilterCache
* BaseContainer::impl_getWorkingCache(std::unique_lock
<std::mutex
>& /*rGuard*/) const
97 return m_pFlushCache
.get();
99 return &GetTheFilterCache();
103 OUString SAL_CALL
BaseContainer::getImplementationName()
105 return m_sImplementationName
;
109 sal_Bool SAL_CALL
BaseContainer::supportsService(const OUString
& sServiceName
)
111 return cppu::supportsService(this, sServiceName
);
114 css::uno::Sequence
< OUString
> SAL_CALL
BaseContainer::getSupportedServiceNames()
116 return m_lServiceNames
;
120 void SAL_CALL
BaseContainer::insertByName(const OUString
& sItem
,
121 const css::uno::Any
& aValue
)
124 throw css::lang::IllegalArgumentException(u
"empty value not allowed as item name."_ustr
,
125 static_cast< css::container::XNameContainer
* >(this),
133 catch(const css::uno::Exception
& ex
)
135 throw css::lang::IllegalArgumentException(ex
.Message
, static_cast< css::container::XNameContainer
* >(this), 2);
138 // SAFE -> ----------------------------------
139 std::unique_lock
aLock(m_aMutex
);
141 impl_loadOnDemand(aLock
);
143 // create write copy of used cache on demand ...
144 impl_initFlushMode(aLock
);
146 FilterCache
* pCache
= impl_getWorkingCache(aLock
);
147 if (pCache
->hasItem(m_eType
, sItem
))
148 throw css::container::ElementExistException(OUString(), static_cast< css::container::XNameContainer
* >(this));
149 pCache
->setItem(m_eType
, sItem
, aItem
);
150 // <- SAFE ----------------------------------
154 void SAL_CALL
BaseContainer::removeByName(const OUString
& sItem
)
156 // SAFE -> ----------------------------------
157 std::unique_lock
aLock(m_aMutex
);
159 impl_loadOnDemand(aLock
);
161 // create write copy of used cache on demand ...
162 impl_initFlushMode(aLock
);
164 FilterCache
* pCache
= impl_getWorkingCache(aLock
);
165 pCache
->removeItem(m_eType
, sItem
); // throw exceptions automatically
166 // <- SAFE ----------------------------------
170 void SAL_CALL
BaseContainer::replaceByName(const OUString
& sItem
,
171 const css::uno::Any
& aValue
)
174 throw css::lang::IllegalArgumentException(u
"empty value not allowed as item name."_ustr
,
175 static_cast< css::container::XNameContainer
* >(this),
183 catch(const css::uno::Exception
& ex
)
185 throw css::lang::IllegalArgumentException(ex
.Message
, static_cast< css::container::XNameContainer
* >(this), 2);
188 // SAFE -> ----------------------------------
189 std::unique_lock
aLock(m_aMutex
);
191 impl_loadOnDemand(aLock
);
193 // create write copy of used cache on demand ...
194 impl_initFlushMode(aLock
);
196 FilterCache
* pCache
= impl_getWorkingCache(aLock
);
197 if (!pCache
->hasItem(m_eType
, sItem
))
198 throw css::container::NoSuchElementException(OUString(), static_cast< css::container::XNameContainer
* >(this));
199 pCache
->setItem(m_eType
, sItem
, aItem
);
200 // <- SAFE ----------------------------------
204 css::uno::Any SAL_CALL
BaseContainer::getByName(const OUString
& sItem
)
207 throw css::container::NoSuchElementException( u
"An empty item can't be part of this cache!"_ustr
,
208 static_cast< css::container::XNameAccess
* >(this));
210 css::uno::Any aValue
;
213 std::unique_lock
aLock(m_aMutex
);
215 impl_loadOnDemand(aLock
);
219 FilterCache
* pCache
= impl_getWorkingCache(aLock
);
220 aValue
= pCache
->getItemWithStateProps(m_eType
, sItem
);
222 catch(const css::container::NoSuchElementException
&)
226 catch(const css::uno::Exception
&)
228 // TODO invalid cache!? How should it be handled right?
237 css::uno::Sequence
< OUString
> SAL_CALL
BaseContainer::getElementNames()
239 css::uno::Sequence
< OUString
> lNames
;
242 std::unique_lock
aLock(m_aMutex
);
244 impl_loadOnDemand(aLock
);
248 FilterCache
* pCache
= impl_getWorkingCache(aLock
);
249 std::vector
<OUString
> lKeys
= pCache
->getItemNames(m_eType
);
250 lNames
= comphelper::containerToSequence(lKeys
);
252 catch(const css::uno::Exception
&)
264 sal_Bool SAL_CALL
BaseContainer::hasByName(const OUString
& sItem
)
266 bool bHasOne
= false;
269 std::unique_lock
aLock(m_aMutex
);
271 impl_loadOnDemand(aLock
);
275 FilterCache
* pCache
= impl_getWorkingCache(aLock
);
276 bHasOne
= pCache
->hasItem(m_eType
, sItem
);
278 catch(const css::uno::Exception
&)
290 css::uno::Type SAL_CALL
BaseContainer::getElementType()
292 // no lock necessary - because the type of our items
293 // is fix! no internal call or member needed ...
294 return cppu::UnoType
<css::uno::Sequence
< css::beans::PropertyValue
>>::get();
298 sal_Bool SAL_CALL
BaseContainer::hasElements()
300 bool bHasSome
= false;
303 std::unique_lock
aLock(m_aMutex
);
305 impl_loadOnDemand(aLock
);
309 FilterCache
* pCache
= impl_getWorkingCache(aLock
);
310 bHasSome
= pCache
->hasItems(m_eType
);
312 catch(const css::uno::Exception
&)
324 css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
BaseContainer::createSubSetEnumerationByQuery(const OUString
& /* sQuery */ )
326 OSL_FAIL("not pure virtual ... but not really implemented .-)");
328 return new ::comphelper::OEnumerationByName(this, {});
332 css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
BaseContainer::createSubSetEnumerationByProperties(const css::uno::Sequence
< css::beans::NamedValue
>& lProperties
)
334 std::vector
<OUString
> lKeys
;
337 std::unique_lock
aLock(m_aMutex
);
339 impl_loadOnDemand(aLock
);
343 // search the key names of all items, where its properties match
344 // the given ones in its minimum
345 FilterCache
* pCache
= impl_getWorkingCache(aLock
);
346 lKeys
= pCache
->getMatchingItemsByProps(m_eType
, std::span
<const css::beans::NamedValue
>( lProperties
.getConstArray(), lProperties
.getLength() ));
348 catch(const css::uno::Exception
&)
350 // invalid cache, internal failure, wrong conversion ...!?
357 // create a specialized enumeration helper, which
358 // provides the collected information outside.
359 // It hold a reference to us ... and call our container interface directly.
360 // be aware of some direct callbacks if it will be created :-)
362 /* Note: It's not allowed to return NULL. Because an empty enumeration
363 transport the same information but make no trouble outside.
364 Further its easier to work directly with the return value
365 instead of checking of NULL returns! */
367 return new ::comphelper::OEnumerationByName(this, std::move(lKeys
));
371 void SAL_CALL
BaseContainer::flush()
374 std::unique_lock
aLock(m_aMutex
);
377 throw css::lang::WrappedTargetRuntimeException(
378 u
"Can not guarantee cache consistency. Special flush container does not exists!"_ustr
,
384 m_pFlushCache
->flush();
385 // Take over all changes into the global cache and
389 If the global cache gets this information via listener,
390 we should remove this method!
392 GetTheFilterCache().takeOver(*m_pFlushCache
);
394 catch(const css::uno::Exception
& ex
)
396 // Don't remove the clone. May be the outside
397 // user wish to repair it now and calls flush()
400 throw css::lang::WrappedTargetRuntimeException( u
"Flush rejected by internal container."_ustr
,
405 m_pFlushCache
.reset();
407 css::lang::EventObject
aSource (static_cast< css::util::XFlushable
* >(this));
408 m_lListener
.notifyEach( aLock
, &css::util::XFlushListener::flushed
, aSource
);
414 void SAL_CALL
BaseContainer::addFlushListener(const css::uno::Reference
< css::util::XFlushListener
>& xListener
)
416 std::unique_lock
g(m_aMutex
);
417 m_lListener
.addInterface(g
, xListener
);
421 void SAL_CALL
BaseContainer::removeFlushListener(const css::uno::Reference
< css::util::XFlushListener
>& xListener
)
423 std::unique_lock
g(m_aMutex
);
424 m_lListener
.removeInterface(g
, xListener
);
427 } // namespace filter::config
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */