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"
21 #include "dbastrings.hrc"
22 #include "apitools.hxx"
23 #include "core_resource.hxx"
24 #include "core_resource.hrc"
26 #include <tools/debug.hxx>
27 #include <osl/diagnose.h>
28 #include <comphelper/sequence.hxx>
29 #include <comphelper/enumhelper.hxx>
30 #include <comphelper/extract.hxx>
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include <comphelper/types.hxx>
33 #include <cppuhelper/supportsservice.hxx>
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::beans
;
38 using namespace ::com::sun::star::container
;
39 using namespace ::osl
;
40 using namespace ::comphelper
;
41 using namespace ::cppu
;
48 OBookmarkContainer::OBookmarkContainer(OWeakObject
& _rParent
, Mutex
& _rMutex
)
50 ,m_aContainerListeners(_rMutex
)
56 void SAL_CALL
OBookmarkContainer::acquire( ) throw()
61 void SAL_CALL
OBookmarkContainer::release( ) throw()
66 OBookmarkContainer::~OBookmarkContainer()
71 OUString SAL_CALL
OBookmarkContainer::getImplementationName( ) throw(RuntimeException
, std::exception
)
73 return OUString("com.sun.star.comp.dba.OBookmarkContainer");
76 sal_Bool SAL_CALL
OBookmarkContainer::supportsService( const OUString
& _rServiceName
) throw (RuntimeException
, std::exception
)
78 return cppu::supportsService(this, _rServiceName
);
81 Sequence
< OUString
> SAL_CALL
OBookmarkContainer::getSupportedServiceNames( ) throw(RuntimeException
, std::exception
)
83 Sequence
< OUString
> aReturn
{ "com.sun.star.sdb.DefinitionContainer" };
88 void SAL_CALL
OBookmarkContainer::insertByName( const OUString
& _rName
, const Any
& aElement
) throw(IllegalArgumentException
, ElementExistException
, WrappedTargetException
, RuntimeException
, std::exception
)
90 MutexGuard
aGuard(m_rMutex
);
92 if (checkExistence(_rName
))
93 throw ElementExistException();
96 throw IllegalArgumentException();
98 // approve the new object
100 if (!(aElement
>>= sNewLink
))
101 throw IllegalArgumentException();
103 implAppend(_rName
, sNewLink
);
105 // notify the listeners
106 if (m_aContainerListeners
.getLength())
108 ContainerEvent
aEvent(*this, makeAny(_rName
), makeAny(sNewLink
), Any());
109 OInterfaceIteratorHelper2
aListenerIterator(m_aContainerListeners
);
110 while (aListenerIterator
.hasMoreElements())
111 static_cast< XContainerListener
* >(aListenerIterator
.next())->elementInserted(aEvent
);
115 void SAL_CALL
OBookmarkContainer::removeByName( const OUString
& _rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
117 OUString sOldBookmark
;
119 MutexGuard
aGuard(m_rMutex
);
121 // check the arguments
122 if (_rName
.isEmpty())
123 throw IllegalArgumentException();
125 if (!checkExistence(_rName
))
126 throw NoSuchElementException();
128 // the old element (for the notifications)
129 sOldBookmark
= m_aBookmarks
[_rName
];
135 // notify the listeners
136 if (m_aContainerListeners
.getLength())
138 ContainerEvent
aEvent(*this, makeAny(_rName
), makeAny(sOldBookmark
), Any());
139 OInterfaceIteratorHelper2
aListenerIterator(m_aContainerListeners
);
140 while (aListenerIterator
.hasMoreElements())
141 static_cast< XContainerListener
* >(aListenerIterator
.next())->elementRemoved(aEvent
);
146 void SAL_CALL
OBookmarkContainer::replaceByName( const OUString
& _rName
, const Any
& aElement
) throw(IllegalArgumentException
, NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
148 ClearableMutexGuard
aGuard(m_rMutex
);
150 // check the arguments
151 if (_rName
.isEmpty())
152 throw IllegalArgumentException();
154 // do we have such an element?
155 if (!checkExistence(_rName
))
156 throw NoSuchElementException();
158 // approve the new object
160 if (!(aElement
>>= sNewLink
))
161 throw IllegalArgumentException();
163 // the old element (for the notifications)
164 OUString sOldLink
= m_aBookmarks
[_rName
];
167 implReplace(_rName
, sNewLink
);
169 // notify the listeners
171 if (m_aContainerListeners
.getLength())
173 ContainerEvent
aEvent(*this, makeAny(_rName
), makeAny(sNewLink
), makeAny(sOldLink
));
174 OInterfaceIteratorHelper2
aListenerIterator(m_aContainerListeners
);
175 while (aListenerIterator
.hasMoreElements())
176 static_cast< XContainerListener
* >(aListenerIterator
.next())->elementReplaced(aEvent
);
180 void SAL_CALL
OBookmarkContainer::addContainerListener( const Reference
< XContainerListener
>& _rxListener
) throw(RuntimeException
, std::exception
)
182 MutexGuard
aGuard(m_rMutex
);
183 if (_rxListener
.is())
184 m_aContainerListeners
.addInterface(_rxListener
);
187 void SAL_CALL
OBookmarkContainer::removeContainerListener( const Reference
< XContainerListener
>& _rxListener
) throw(RuntimeException
, std::exception
)
189 MutexGuard
aGuard(m_rMutex
);
190 if (_rxListener
.is())
191 m_aContainerListeners
.removeInterface(_rxListener
);
195 Type SAL_CALL
OBookmarkContainer::getElementType( ) throw (RuntimeException
, std::exception
)
197 MutexGuard
aGuard(m_rMutex
);
198 return ::cppu::UnoType
<OUString
>::get();
201 sal_Bool SAL_CALL
OBookmarkContainer::hasElements( ) throw (RuntimeException
, std::exception
)
203 MutexGuard
aGuard(m_rMutex
);
204 return !m_aBookmarks
.empty();
207 // XEnumerationAccess
208 Reference
< XEnumeration
> SAL_CALL
OBookmarkContainer::createEnumeration( ) throw(RuntimeException
, std::exception
)
210 MutexGuard
aGuard(m_rMutex
);
211 return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess
*>(this));
215 sal_Int32 SAL_CALL
OBookmarkContainer::getCount( ) throw(RuntimeException
, std::exception
)
217 MutexGuard
aGuard(m_rMutex
);
218 return m_aBookmarks
.size();
221 Any SAL_CALL
OBookmarkContainer::getByIndex( sal_Int32 _nIndex
) throw(IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
223 MutexGuard
aGuard(m_rMutex
);
225 if ((_nIndex
< 0) || (_nIndex
>= (sal_Int32
)m_aBookmarksIndexed
.size()))
226 throw IndexOutOfBoundsException();
228 return makeAny(m_aBookmarksIndexed
[_nIndex
]->second
);
231 Any SAL_CALL
OBookmarkContainer::getByName( const OUString
& _rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
233 MutexGuard
aGuard(m_rMutex
);
235 if (!checkExistence(_rName
))
236 throw NoSuchElementException();
238 return makeAny(m_aBookmarks
[_rName
]);
241 Sequence
< OUString
> SAL_CALL
OBookmarkContainer::getElementNames( ) throw(RuntimeException
, std::exception
)
243 MutexGuard
aGuard(m_rMutex
);
245 Sequence
< OUString
> aNames(m_aBookmarks
.size());
246 OUString
* pNames
= aNames
.getArray();
248 for ( MapIteratorVector::const_iterator aNameIter
= m_aBookmarksIndexed
.begin();
249 aNameIter
!= m_aBookmarksIndexed
.end();
250 ++pNames
, ++aNameIter
253 *pNames
= (*aNameIter
)->first
;
259 sal_Bool SAL_CALL
OBookmarkContainer::hasByName( const OUString
& _rName
) throw(RuntimeException
, std::exception
)
261 MutexGuard
aGuard(m_rMutex
);
263 return checkExistence(_rName
);
266 void OBookmarkContainer::implRemove(const OUString
& _rName
)
268 MutexGuard
aGuard(m_rMutex
);
270 // look for the name in the index access vector
271 MapString2String::const_iterator aMapPos
= m_aBookmarks
.end();
272 for ( MapIteratorVector::iterator aSearch
= m_aBookmarksIndexed
.begin();
273 aSearch
!= m_aBookmarksIndexed
.end();
277 if ((*aSearch
)->first
== _rName
)
280 m_aBookmarksIndexed
.erase(aSearch
);
285 if (m_aBookmarks
.end() == aMapPos
)
287 OSL_FAIL("OBookmarkContainer::implRemove: inconsistence!");
291 // remove the map entries
292 m_aBookmarks
.erase(aMapPos
);
295 void OBookmarkContainer::implAppend(const OUString
& _rName
, const OUString
& _rDocumentLocation
)
297 MutexGuard
aGuard(m_rMutex
);
299 OSL_ENSURE(m_aBookmarks
.find(_rName
) == m_aBookmarks
.end(),"Bookmark already known!");
300 m_aBookmarksIndexed
.push_back(m_aBookmarks
.insert( MapString2String::value_type(_rName
,_rDocumentLocation
)).first
);
303 void OBookmarkContainer::implReplace(const OUString
& _rName
, const OUString
& _rNewLink
)
305 MutexGuard
aGuard(m_rMutex
);
306 OSL_ENSURE(checkExistence(_rName
), "OBookmarkContainer::implReplace : invalid name !");
308 m_aBookmarks
[_rName
] = _rNewLink
;
311 Reference
< XInterface
> SAL_CALL
OBookmarkContainer::getParent( ) throw (RuntimeException
, std::exception
)
316 void SAL_CALL
OBookmarkContainer::setParent( const Reference
< XInterface
>& /*Parent*/ ) throw (NoSupportException
, RuntimeException
, std::exception
)
318 throw NoSupportException();
321 } // namespace dbaccess
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */