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 <bookmarkcontainer.hxx>
22 #include <o3tl/safeint.hxx>
23 #include <osl/diagnose.h>
24 #include <comphelper/enumhelper.hxx>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <com/sun/star/lang/NoSupportException.hpp>
27 #include <cppuhelper/supportsservice.hxx>
29 using namespace ::com::sun::star::uno
;
30 using namespace ::com::sun::star::lang
;
31 using namespace ::com::sun::star::container
;
32 using namespace ::osl
;
33 using namespace ::comphelper
;
34 using namespace ::cppu
;
41 OBookmarkContainer::OBookmarkContainer(OWeakObject
& _rParent
, Mutex
& _rMutex
)
43 ,m_aContainerListeners(_rMutex
)
49 void SAL_CALL
OBookmarkContainer::acquire( ) noexcept
54 void SAL_CALL
OBookmarkContainer::release( ) noexcept
59 OBookmarkContainer::~OBookmarkContainer()
64 OUString SAL_CALL
OBookmarkContainer::getImplementationName( )
66 return u
"com.sun.star.comp.dba.OBookmarkContainer"_ustr
;
69 sal_Bool SAL_CALL
OBookmarkContainer::supportsService( const OUString
& _rServiceName
)
71 return cppu::supportsService(this, _rServiceName
);
74 Sequence
< OUString
> SAL_CALL
OBookmarkContainer::getSupportedServiceNames( )
76 return { u
"com.sun.star.sdb.DefinitionContainer"_ustr
};
80 void SAL_CALL
OBookmarkContainer::insertByName( const OUString
& _rName
, const Any
& aElement
)
82 MutexGuard
aGuard(m_rMutex
);
84 if (checkExistence(_rName
))
85 throw ElementExistException();
88 throw IllegalArgumentException();
90 // approve the new object
92 if (!(aElement
>>= sNewLink
))
93 throw IllegalArgumentException();
95 implAppend(_rName
, sNewLink
);
97 // notify the listeners
98 if (m_aContainerListeners
.getLength())
100 ContainerEvent
aEvent(*this, Any(_rName
), Any(sNewLink
), Any());
101 m_aContainerListeners
.notifyEach( &XContainerListener::elementInserted
, aEvent
);
105 void SAL_CALL
OBookmarkContainer::removeByName( const OUString
& _rName
)
107 OUString sOldBookmark
;
109 MutexGuard
aGuard(m_rMutex
);
111 // check the arguments
112 if (_rName
.isEmpty())
113 throw IllegalArgumentException();
115 if (!checkExistence(_rName
))
116 throw NoSuchElementException();
118 // the old element (for the notifications)
119 sOldBookmark
= m_aBookmarks
[_rName
];
125 // notify the listeners
126 if (m_aContainerListeners
.getLength())
128 ContainerEvent
aEvent(*this, Any(_rName
), Any(sOldBookmark
), Any());
129 m_aContainerListeners
.notifyEach( &XContainerListener::elementRemoved
, aEvent
);
134 void SAL_CALL
OBookmarkContainer::replaceByName( const OUString
& _rName
, const Any
& aElement
)
136 ClearableMutexGuard
aGuard(m_rMutex
);
138 // check the arguments
139 if (_rName
.isEmpty())
140 throw IllegalArgumentException();
142 // do we have such an element?
143 if (!checkExistence(_rName
))
144 throw NoSuchElementException();
146 // approve the new object
148 if (!(aElement
>>= sNewLink
))
149 throw IllegalArgumentException();
151 // the old element (for the notifications)
152 OUString sOldLink
= m_aBookmarks
[_rName
];
155 implReplace(_rName
, sNewLink
);
157 // notify the listeners
159 if (m_aContainerListeners
.getLength())
161 ContainerEvent
aEvent(*this, Any(_rName
), Any(sNewLink
), Any(sOldLink
));
162 m_aContainerListeners
.notifyEach( &XContainerListener::elementReplaced
, aEvent
);
166 void SAL_CALL
OBookmarkContainer::addContainerListener( const Reference
< XContainerListener
>& _rxListener
)
168 MutexGuard
aGuard(m_rMutex
);
169 if (_rxListener
.is())
170 m_aContainerListeners
.addInterface(_rxListener
);
173 void SAL_CALL
OBookmarkContainer::removeContainerListener( const Reference
< XContainerListener
>& _rxListener
)
175 MutexGuard
aGuard(m_rMutex
);
176 if (_rxListener
.is())
177 m_aContainerListeners
.removeInterface(_rxListener
);
181 Type SAL_CALL
OBookmarkContainer::getElementType( )
183 return ::cppu::UnoType
<OUString
>::get();
186 sal_Bool SAL_CALL
OBookmarkContainer::hasElements( )
188 MutexGuard
aGuard(m_rMutex
);
189 return !m_aBookmarks
.empty();
192 // XEnumerationAccess
193 Reference
< XEnumeration
> SAL_CALL
OBookmarkContainer::createEnumeration( )
195 MutexGuard
aGuard(m_rMutex
);
196 return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess
*>(this));
200 sal_Int32 SAL_CALL
OBookmarkContainer::getCount( )
202 MutexGuard
aGuard(m_rMutex
);
203 return m_aBookmarks
.size();
206 Any SAL_CALL
OBookmarkContainer::getByIndex( sal_Int32 _nIndex
)
208 MutexGuard
aGuard(m_rMutex
);
210 if ((_nIndex
< 0) || (o3tl::make_unsigned(_nIndex
) >= m_aBookmarksIndexed
.size()))
211 throw IndexOutOfBoundsException();
213 return Any(m_aBookmarksIndexed
[_nIndex
]->second
);
216 Any SAL_CALL
OBookmarkContainer::getByName( const OUString
& _rName
)
218 MutexGuard
aGuard(m_rMutex
);
220 if (!checkExistence(_rName
))
221 throw NoSuchElementException();
223 return Any(m_aBookmarks
[_rName
]);
226 Sequence
< OUString
> SAL_CALL
OBookmarkContainer::getElementNames( )
228 MutexGuard
aGuard(m_rMutex
);
230 Sequence
< OUString
> aNames(m_aBookmarks
.size());
231 OUString
* pNames
= aNames
.getArray();
233 for (auto const& bookmarkIndexed
: m_aBookmarksIndexed
)
235 *pNames
= bookmarkIndexed
->first
;
242 sal_Bool SAL_CALL
OBookmarkContainer::hasByName( const OUString
& _rName
)
244 MutexGuard
aGuard(m_rMutex
);
246 return checkExistence(_rName
);
249 void OBookmarkContainer::implRemove(const OUString
& _rName
)
251 MutexGuard
aGuard(m_rMutex
);
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())
260 m_aBookmarksIndexed
.erase(aSearch
);
263 if (m_aBookmarks
.end() == aMapPos
)
265 OSL_FAIL("OBookmarkContainer::implRemove: inconsistence!");
269 // remove the map entries
270 m_aBookmarks
.erase(aMapPos
);
273 void OBookmarkContainer::implAppend(const OUString
& _rName
, const OUString
& _rDocumentLocation
)
275 MutexGuard
aGuard(m_rMutex
);
277 OSL_ENSURE(m_aBookmarks
.find(_rName
) == m_aBookmarks
.end(),"Bookmark already known!");
278 m_aBookmarksIndexed
.push_back(m_aBookmarks
.emplace( _rName
,_rDocumentLocation
).first
);
281 void OBookmarkContainer::implReplace(const OUString
& _rName
, const OUString
& _rNewLink
)
283 MutexGuard
aGuard(m_rMutex
);
284 OSL_ENSURE(checkExistence(_rName
), "OBookmarkContainer::implReplace : invalid name !");
286 m_aBookmarks
[_rName
] = _rNewLink
;
289 Reference
< XInterface
> SAL_CALL
OBookmarkContainer::getParent( )
294 void SAL_CALL
OBookmarkContainer::setParent( const Reference
< XInterface
>& /*Parent*/ )
296 throw NoSupportException();
299 } // namespace dbaccess
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */