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
)
55 void OBookmarkContainer::dispose()
57 MutexGuard
aGuard(m_rMutex
);
59 // say goodbye to our listeners
60 EventObject
aEvt(*this);
61 m_aContainerListeners
.disposeAndClear(aEvt
);
63 // remove our elements
64 m_aBookmarksIndexed
.clear();
68 void SAL_CALL
OBookmarkContainer::acquire( ) throw()
73 void SAL_CALL
OBookmarkContainer::release( ) throw()
78 OBookmarkContainer::~OBookmarkContainer()
83 OUString SAL_CALL
OBookmarkContainer::getImplementationName( ) throw(RuntimeException
, std::exception
)
85 return OUString("com.sun.star.comp.dba.OBookmarkContainer");
88 sal_Bool SAL_CALL
OBookmarkContainer::supportsService( const OUString
& _rServiceName
) throw (RuntimeException
, std::exception
)
90 return cppu::supportsService(this, _rServiceName
);
93 Sequence
< OUString
> SAL_CALL
OBookmarkContainer::getSupportedServiceNames( ) throw(RuntimeException
, std::exception
)
95 Sequence
< OUString
> aReturn(1);
96 aReturn
.getArray()[0] = "com.sun.star.sdb.DefinitionContainer";
101 void SAL_CALL
OBookmarkContainer::insertByName( const OUString
& _rName
, const Any
& aElement
) throw(IllegalArgumentException
, ElementExistException
, WrappedTargetException
, RuntimeException
, std::exception
)
103 MutexGuard
aGuard(m_rMutex
);
105 if (checkExistence(_rName
))
106 throw ElementExistException();
108 if (_rName
.isEmpty())
109 throw IllegalArgumentException();
111 // approve the new object
113 if (!(aElement
>>= sNewLink
))
114 throw IllegalArgumentException();
116 implAppend(_rName
, sNewLink
);
118 // notify the listeners
119 if (m_aContainerListeners
.getLength())
121 ContainerEvent
aEvent(*this, makeAny(_rName
), makeAny(sNewLink
), Any());
122 OInterfaceIteratorHelper
aListenerIterator(m_aContainerListeners
);
123 while (aListenerIterator
.hasMoreElements())
124 static_cast< XContainerListener
* >(aListenerIterator
.next())->elementInserted(aEvent
);
128 void SAL_CALL
OBookmarkContainer::removeByName( const OUString
& _rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
130 OUString sOldBookmark
;
132 MutexGuard
aGuard(m_rMutex
);
134 // check the arguments
135 if (_rName
.isEmpty())
136 throw IllegalArgumentException();
138 if (!checkExistence(_rName
))
139 throw NoSuchElementException();
141 // the old element (for the notifications)
142 sOldBookmark
= m_aBookmarks
[_rName
];
148 // notify the listeners
149 if (m_aContainerListeners
.getLength())
151 ContainerEvent
aEvent(*this, makeAny(_rName
), makeAny(sOldBookmark
), Any());
152 OInterfaceIteratorHelper
aListenerIterator(m_aContainerListeners
);
153 while (aListenerIterator
.hasMoreElements())
154 static_cast< XContainerListener
* >(aListenerIterator
.next())->elementRemoved(aEvent
);
159 void SAL_CALL
OBookmarkContainer::replaceByName( const OUString
& _rName
, const Any
& aElement
) throw(IllegalArgumentException
, NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
161 ClearableMutexGuard
aGuard(m_rMutex
);
163 // check the arguments
164 if (_rName
.isEmpty())
165 throw IllegalArgumentException();
167 // do we have such an element?
168 if (!checkExistence(_rName
))
169 throw NoSuchElementException();
171 // approve the new object
173 if (!(aElement
>>= sNewLink
))
174 throw IllegalArgumentException();
176 // the old element (for the notifications)
177 OUString sOldLink
= m_aBookmarks
[_rName
];
180 implReplace(_rName
, sNewLink
);
182 // notify the listeners
184 if (m_aContainerListeners
.getLength())
186 ContainerEvent
aEvent(*this, makeAny(_rName
), makeAny(sNewLink
), makeAny(sOldLink
));
187 OInterfaceIteratorHelper
aListenerIterator(m_aContainerListeners
);
188 while (aListenerIterator
.hasMoreElements())
189 static_cast< XContainerListener
* >(aListenerIterator
.next())->elementReplaced(aEvent
);
193 void SAL_CALL
OBookmarkContainer::addContainerListener( const Reference
< XContainerListener
>& _rxListener
) throw(RuntimeException
, std::exception
)
195 MutexGuard
aGuard(m_rMutex
);
196 if (_rxListener
.is())
197 m_aContainerListeners
.addInterface(_rxListener
);
200 void SAL_CALL
OBookmarkContainer::removeContainerListener( const Reference
< XContainerListener
>& _rxListener
) throw(RuntimeException
, std::exception
)
202 MutexGuard
aGuard(m_rMutex
);
203 if (_rxListener
.is())
204 m_aContainerListeners
.removeInterface(_rxListener
);
208 Type SAL_CALL
OBookmarkContainer::getElementType( ) throw (RuntimeException
, std::exception
)
210 MutexGuard
aGuard(m_rMutex
);
211 return ::cppu::UnoType
<OUString
>::get();
214 sal_Bool SAL_CALL
OBookmarkContainer::hasElements( ) throw (RuntimeException
, std::exception
)
216 MutexGuard
aGuard(m_rMutex
);
217 return !m_aBookmarks
.empty();
220 // XEnumerationAccess
221 Reference
< XEnumeration
> SAL_CALL
OBookmarkContainer::createEnumeration( ) throw(RuntimeException
, std::exception
)
223 MutexGuard
aGuard(m_rMutex
);
224 return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess
*>(this));
228 sal_Int32 SAL_CALL
OBookmarkContainer::getCount( ) throw(RuntimeException
, std::exception
)
230 MutexGuard
aGuard(m_rMutex
);
231 return m_aBookmarks
.size();
234 Any SAL_CALL
OBookmarkContainer::getByIndex( sal_Int32 _nIndex
) throw(IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
236 MutexGuard
aGuard(m_rMutex
);
238 if ((_nIndex
< 0) || (_nIndex
>= (sal_Int32
)m_aBookmarksIndexed
.size()))
239 throw IndexOutOfBoundsException();
241 return makeAny(m_aBookmarksIndexed
[_nIndex
]->second
);
244 Any SAL_CALL
OBookmarkContainer::getByName( const OUString
& _rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
246 MutexGuard
aGuard(m_rMutex
);
248 if (!checkExistence(_rName
))
249 throw NoSuchElementException();
251 return makeAny(m_aBookmarks
[_rName
]);
254 Sequence
< OUString
> SAL_CALL
OBookmarkContainer::getElementNames( ) throw(RuntimeException
, std::exception
)
256 MutexGuard
aGuard(m_rMutex
);
258 Sequence
< OUString
> aNames(m_aBookmarks
.size());
259 OUString
* pNames
= aNames
.getArray();
261 for ( MapIteratorVector::const_iterator aNameIter
= m_aBookmarksIndexed
.begin();
262 aNameIter
!= m_aBookmarksIndexed
.end();
263 ++pNames
, ++aNameIter
266 *pNames
= (*aNameIter
)->first
;
272 sal_Bool SAL_CALL
OBookmarkContainer::hasByName( const OUString
& _rName
) throw(RuntimeException
, std::exception
)
274 MutexGuard
aGuard(m_rMutex
);
276 return checkExistence(_rName
);
279 void OBookmarkContainer::implRemove(const OUString
& _rName
)
281 MutexGuard
aGuard(m_rMutex
);
283 // look for the name in the index access vector
284 MapString2String::iterator aMapPos
= m_aBookmarks
.end();
285 for ( MapIteratorVector::iterator aSearch
= m_aBookmarksIndexed
.begin();
286 aSearch
!= m_aBookmarksIndexed
.end();
290 if ((*aSearch
)->first
== _rName
)
293 m_aBookmarksIndexed
.erase(aSearch
);
298 if (m_aBookmarks
.end() == aMapPos
)
300 OSL_FAIL("OBookmarkContainer::implRemove: inconsistence!");
304 // remove the map entries
305 m_aBookmarks
.erase(aMapPos
);
308 void OBookmarkContainer::implAppend(const OUString
& _rName
, const OUString
& _rDocumentLocation
)
310 MutexGuard
aGuard(m_rMutex
);
312 OSL_ENSURE(m_aBookmarks
.find(_rName
) == m_aBookmarks
.end(),"Bookmark already known!");
313 m_aBookmarksIndexed
.push_back(m_aBookmarks
.insert( MapString2String::value_type(_rName
,_rDocumentLocation
)).first
);
316 void OBookmarkContainer::implReplace(const OUString
& _rName
, const OUString
& _rNewLink
)
318 MutexGuard
aGuard(m_rMutex
);
319 OSL_ENSURE(checkExistence(_rName
), "OBookmarkContainer::implReplace : invalid name !");
321 m_aBookmarks
[_rName
] = _rNewLink
;
324 Reference
< XInterface
> SAL_CALL
OBookmarkContainer::getParent( ) throw (RuntimeException
, std::exception
)
329 void SAL_CALL
OBookmarkContainer::setParent( const Reference
< XInterface
>& /*Parent*/ ) throw (NoSupportException
, RuntimeException
, std::exception
)
331 throw NoSupportException();
334 } // namespace dbaccess
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */