LibreOffice Module dbaccess (master) 1
bookmarkcontainer.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
20#include <bookmarkcontainer.hxx>
21
22#include <o3tl/safeint.hxx>
23#include <osl/diagnose.h>
25#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26#include <com/sun/star/lang/NoSupportException.hpp>
28
29using namespace ::com::sun::star::uno;
30using namespace ::com::sun::star::lang;
31using namespace ::com::sun::star::container;
32using namespace ::osl;
33using namespace ::comphelper;
34using namespace ::cppu;
35
36namespace dbaccess
37{
38
39// OBookmarkContainer
40
41OBookmarkContainer::OBookmarkContainer(OWeakObject& _rParent, Mutex& _rMutex)
42 :m_rParent(_rParent)
43 ,m_aContainerListeners(_rMutex)
44 ,m_rMutex(_rMutex)
45{
46}
47
48
49void SAL_CALL OBookmarkContainer::acquire( ) noexcept
50{
52}
53
54void SAL_CALL OBookmarkContainer::release( ) noexcept
55{
57}
58
60{
61}
62
63// XServiceInfo
65{
66 return "com.sun.star.comp.dba.OBookmarkContainer";
67}
68
69sal_Bool SAL_CALL OBookmarkContainer::supportsService( const OUString& _rServiceName )
70{
71 return cppu::supportsService(this, _rServiceName);
72}
73
74Sequence< OUString > SAL_CALL OBookmarkContainer::getSupportedServiceNames( )
75{
76 return { "com.sun.star.sdb.DefinitionContainer" };
77}
78
79// XNameContainer
80void SAL_CALL OBookmarkContainer::insertByName( const OUString& _rName, const Any& aElement )
81{
82 MutexGuard aGuard(m_rMutex);
83
84 if (checkExistence(_rName))
85 throw ElementExistException();
86
87 if (_rName.isEmpty())
88 throw IllegalArgumentException();
89
90 // approve the new object
91 OUString sNewLink;
92 if (!(aElement >>= sNewLink))
93 throw IllegalArgumentException();
94
95 implAppend(_rName, sNewLink);
96
97 // notify the listeners
99 {
100 ContainerEvent aEvent(*this, Any(_rName), Any(sNewLink), Any());
101 m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
102 }
103}
104
105void SAL_CALL OBookmarkContainer::removeByName( const OUString& _rName )
106{
107 OUString sOldBookmark;
108 {
109 MutexGuard aGuard(m_rMutex);
110
111 // check the arguments
112 if (_rName.isEmpty())
113 throw IllegalArgumentException();
114
115 if (!checkExistence(_rName))
116 throw NoSuchElementException();
117
118 // the old element (for the notifications)
119 sOldBookmark = m_aBookmarks[_rName];
120
121 // do the removal
122 implRemove(_rName);
123 }
124
125 // notify the listeners
127 {
128 ContainerEvent aEvent(*this, Any(_rName), Any(sOldBookmark), Any());
129 m_aContainerListeners.notifyEach( &XContainerListener::elementRemoved, aEvent );
130 }
131}
132
133// XNameReplace
134void SAL_CALL OBookmarkContainer::replaceByName( const OUString& _rName, const Any& aElement )
135{
136 ClearableMutexGuard aGuard(m_rMutex);
137
138 // check the arguments
139 if (_rName.isEmpty())
140 throw IllegalArgumentException();
141
142 // do we have such an element?
143 if (!checkExistence(_rName))
144 throw NoSuchElementException();
145
146 // approve the new object
147 OUString sNewLink;
148 if (!(aElement >>= sNewLink))
149 throw IllegalArgumentException();
150
151 // the old element (for the notifications)
152 OUString sOldLink = m_aBookmarks[_rName];
153
154 // do the replace
155 implReplace(_rName, sNewLink);
156
157 // notify the listeners
158 aGuard.clear();
160 {
161 ContainerEvent aEvent(*this, Any(_rName), Any(sNewLink), Any(sOldLink));
162 m_aContainerListeners.notifyEach( &XContainerListener::elementReplaced, aEvent );
163 }
164}
165
166void SAL_CALL OBookmarkContainer::addContainerListener( const Reference< XContainerListener >& _rxListener )
167{
168 MutexGuard aGuard(m_rMutex);
169 if (_rxListener.is())
171}
172
173void SAL_CALL OBookmarkContainer::removeContainerListener( const Reference< XContainerListener >& _rxListener )
174{
175 MutexGuard aGuard(m_rMutex);
176 if (_rxListener.is())
178}
179
180// XElementAccess
182{
183 return ::cppu::UnoType<OUString>::get();
184}
185
187{
188 MutexGuard aGuard(m_rMutex);
189 return !m_aBookmarks.empty();
190}
191
192// XEnumerationAccess
193Reference< XEnumeration > SAL_CALL OBookmarkContainer::createEnumeration( )
194{
195 MutexGuard aGuard(m_rMutex);
196 return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess*>(this));
197}
198
199// XIndexAccess
200sal_Int32 SAL_CALL OBookmarkContainer::getCount( )
201{
202 MutexGuard aGuard(m_rMutex);
203 return m_aBookmarks.size();
204}
205
206Any SAL_CALL OBookmarkContainer::getByIndex( sal_Int32 _nIndex )
207{
208 MutexGuard aGuard(m_rMutex);
209
210 if ((_nIndex < 0) || (o3tl::make_unsigned(_nIndex) >= m_aBookmarksIndexed.size()))
211 throw IndexOutOfBoundsException();
212
213 return Any(m_aBookmarksIndexed[_nIndex]->second);
214}
215
216Any SAL_CALL OBookmarkContainer::getByName( const OUString& _rName )
217{
218 MutexGuard aGuard(m_rMutex);
219
220 if (!checkExistence(_rName))
221 throw NoSuchElementException();
222
223 return Any(m_aBookmarks[_rName]);
224}
225
226Sequence< OUString > SAL_CALL OBookmarkContainer::getElementNames( )
227{
228 MutexGuard aGuard(m_rMutex);
229
230 Sequence< OUString > aNames(m_aBookmarks.size());
231 OUString* pNames = aNames.getArray();
232
233 for (auto const& bookmarkIndexed : m_aBookmarksIndexed)
234 {
235 *pNames = bookmarkIndexed->first;
236 ++pNames;
237 }
238
239 return aNames;
240}
241
242sal_Bool SAL_CALL OBookmarkContainer::hasByName( const OUString& _rName )
243{
244 MutexGuard aGuard(m_rMutex);
245
246 return checkExistence(_rName);
247}
248
249void OBookmarkContainer::implRemove(const OUString& _rName)
250{
251 MutexGuard aGuard(m_rMutex);
252
253 // look for the name in the index access vector
254 MapString2String::const_iterator aMapPos = m_aBookmarks.end();
255 auto aSearch = std::find_if(m_aBookmarksIndexed.begin(), m_aBookmarksIndexed.end(),
256 [&_rName](const MapString2String::iterator& rIter) { return rIter->first == _rName; });
257 if (aSearch != m_aBookmarksIndexed.end())
258 {
259 aMapPos = *aSearch;
260 m_aBookmarksIndexed.erase(aSearch);
261 }
262
263 if (m_aBookmarks.end() == aMapPos)
264 {
265 OSL_FAIL("OBookmarkContainer::implRemove: inconsistence!");
266 return;
267 }
268
269 // remove the map entries
270 m_aBookmarks.erase(aMapPos);
271}
272
273void OBookmarkContainer::implAppend(const OUString& _rName, const OUString& _rDocumentLocation)
274{
275 MutexGuard aGuard(m_rMutex);
276
277 OSL_ENSURE(m_aBookmarks.find(_rName) == m_aBookmarks.end(),"Bookmark already known!");
278 m_aBookmarksIndexed.push_back(m_aBookmarks.emplace( _rName,_rDocumentLocation).first);
279}
280
281void OBookmarkContainer::implReplace(const OUString& _rName, const OUString& _rNewLink)
282{
283 MutexGuard aGuard(m_rMutex);
284 OSL_ENSURE(checkExistence(_rName), "OBookmarkContainer::implReplace : invalid name !");
285
286 m_aBookmarks[_rName] = _rNewLink;
287}
288
289Reference< XInterface > SAL_CALL OBookmarkContainer::getParent( )
290{
291 return m_rParent;
292}
293
294void SAL_CALL OBookmarkContainer::setParent( const Reference< XInterface >& /*Parent*/ )
295{
296 throw NoSupportException();
297}
298
299} // namespace dbaccess
300
301/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XSLTFilter & m_rParent
AnyEventRef aEvent
sal_Int32 addInterface(const css::uno::Reference< ListenerT > &rxIFace)
sal_Int32 removeInterface(const css::uno::Reference< ListenerT > &rxIFace)
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
virtual void SAL_CALL acquire() SAL_NOEXCEPT SAL_OVERRIDE
virtual void SAL_CALL release() SAL_NOEXCEPT SAL_OVERRIDE
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 _nIndex) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
virtual void SAL_CALL replaceByName(const OUString &_rName, const css::uno::Any &aElement) override
::cppu::OWeakObject & m_rParent
bool checkExistence(const OUString &_rName)
quickly checks if there already is an element with a given name.
void implAppend(const OUString &_rName, const OUString &_rDocumentLocation)
virtual sal_Bool SAL_CALL hasElements() override
::comphelper::OInterfaceContainerHelper3< css::container::XContainerListener > m_aContainerListeners
void implReplace(const OUString &_rName, const OUString &_rNewLink)
virtual void SAL_CALL removeByName(const OUString &_rName) override
MapIteratorVector m_aBookmarksIndexed
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
virtual void SAL_CALL setParent(const css::uno::Reference< css::uno::XInterface > &Parent) override
virtual css::uno::Type SAL_CALL getElementType() override
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
virtual void SAL_CALL release() noexcept override
virtual void SAL_CALL removeContainerListener(const css::uno::Reference< css::container::XContainerListener > &xListener) override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
void implRemove(const OUString &_rName)
virtual void SAL_CALL addContainerListener(const css::uno::Reference< css::container::XContainerListener > &xListener) override
virtual void SAL_CALL acquire() noexcept override
virtual sal_Int32 SAL_CALL getCount() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL insertByName(const OUString &_rName, const css::uno::Any &aElement) override
OBookmarkContainer(::cppu::OWeakObject &_rParent, ::osl::Mutex &_rMutex)
constructs the container.
virtual ~OBookmarkContainer() override
looks like the dtor ...
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL getParent() override
::osl::Mutex & m_rMutex
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
unsigned char sal_Bool