1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: bookmarkcontainer.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef _DBA_CORE_BOOKMARKCONTAINER_HXX_
35 #include "bookmarkcontainer.hxx"
37 #ifndef DBACCESS_SHARED_DBASTRINGS_HRC
38 #include "dbastrings.hrc"
40 #ifndef _DBASHARED_APITOOLS_HXX_
41 #include "apitools.hxx"
43 #ifndef _DBA_CORE_RESOURCE_HXX_
44 #include "core_resource.hxx"
46 #ifndef _DBA_CORE_RESOURCE_HRC_
47 #include "core_resource.hrc"
50 #ifndef _TOOLS_DEBUG_HXX
51 #include <tools/debug.hxx>
53 #ifndef _COMPHELPER_SEQUENCE_HXX_
54 #include <comphelper/sequence.hxx>
56 #ifndef _COMPHELPER_ENUMHELPER_HXX_
57 #include <comphelper/enumhelper.hxx>
59 #ifndef _COMPHELPER_EXTRACT_HXX_
60 #include <comphelper/extract.hxx>
62 #ifndef _COM_SUN_STAR_LANG_XCOMPONENT_HPP_
63 #include <com/sun/star/lang/XComponent.hpp>
65 #ifndef _COMPHELPER_TYPES_HXX_
66 #include <comphelper/types.hxx>
69 using namespace ::com::sun::star::uno
;
70 using namespace ::com::sun::star::lang
;
71 using namespace ::com::sun::star::beans
;
72 using namespace ::com::sun::star::container
;
73 using namespace ::osl
;
74 using namespace ::comphelper
;
75 using namespace ::cppu
;
77 //........................................................................
80 //........................................................................
82 //==========================================================================
83 //= OBookmarkContainer
84 //==========================================================================
85 DBG_NAME(OBookmarkContainer
)
86 //--------------------------------------------------------------------------
87 OBookmarkContainer::OBookmarkContainer(OWeakObject
& _rParent
, Mutex
& _rMutex
)
89 ,m_aContainerListeners(_rMutex
)
92 DBG_CTOR(OBookmarkContainer
, NULL
);
95 //--------------------------------------------------------------------------
96 void OBookmarkContainer::dispose()
98 MutexGuard
aGuard(m_rMutex
);
100 // say our listeners goobye
101 EventObject
aEvt(*this);
102 m_aContainerListeners
.disposeAndClear(aEvt
);
104 // remove our elements
105 m_aBookmarksIndexed
.clear();
106 m_aBookmarks
.clear();
109 //--------------------------------------------------------------------------
110 void SAL_CALL
OBookmarkContainer::acquire( ) throw()
115 //--------------------------------------------------------------------------
116 void SAL_CALL
OBookmarkContainer::release( ) throw()
121 //--------------------------------------------------------------------------
122 OBookmarkContainer::~OBookmarkContainer()
124 DBG_DTOR(OBookmarkContainer
, NULL
);
128 //--------------------------------------------------------------------------
129 ::rtl::OUString SAL_CALL
OBookmarkContainer::getImplementationName( ) throw(RuntimeException
)
131 return ::rtl::OUString::createFromAscii("com.sun.star.comp.dba.OBookmarkContainer");
134 //--------------------------------------------------------------------------
135 sal_Bool SAL_CALL
OBookmarkContainer::supportsService( const ::rtl::OUString
& _rServiceName
) throw (RuntimeException
)
137 MutexGuard
aGuard(m_rMutex
);
138 checkValid(sal_False
);
139 return findValue(getSupportedServiceNames(), _rServiceName
, sal_True
).getLength() != 0;
142 //--------------------------------------------------------------------------
143 Sequence
< ::rtl::OUString
> SAL_CALL
OBookmarkContainer::getSupportedServiceNames( ) throw(RuntimeException
)
145 Sequence
< ::rtl::OUString
> aReturn(1);
146 aReturn
.getArray()[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdb.DefinitionContainer");
151 //--------------------------------------------------------------------------
152 void SAL_CALL
OBookmarkContainer::insertByName( const ::rtl::OUString
& _rName
, const Any
& aElement
) throw(IllegalArgumentException
, ElementExistException
, WrappedTargetException
, RuntimeException
)
154 MutexGuard
aGuard(m_rMutex
);
155 checkValid(sal_True
);
157 if (checkExistence(_rName
))
158 throw ElementExistException();
160 if (0 == _rName
.getLength())
161 throw IllegalArgumentException();
163 // approve the new object
164 ::rtl::OUString sNewLink
;
165 if (!(aElement
>>= sNewLink
))
166 throw IllegalArgumentException();
169 implAppend(_rName
, sNewLink
);
171 // notify the listeners
172 if (m_aContainerListeners
.getLength())
174 ContainerEvent
aEvent(*this, makeAny(_rName
), makeAny(sNewLink
), Any());
175 OInterfaceIteratorHelper
aListenerIterator(m_aContainerListeners
);
176 while (aListenerIterator
.hasMoreElements())
177 static_cast< XContainerListener
* >(aListenerIterator
.next())->elementInserted(aEvent
);
181 //--------------------------------------------------------------------------
182 void SAL_CALL
OBookmarkContainer::removeByName( const ::rtl::OUString
& _rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
)
184 ::rtl::OUString sOldBookmark
;
186 MutexGuard
aGuard(m_rMutex
);
187 checkValid(sal_True
);
189 // check the arguments
190 if (!_rName
.getLength())
191 throw IllegalArgumentException();
193 if (!checkExistence(_rName
))
194 throw NoSuchElementException();
196 // the old element (for the notifications)
197 sOldBookmark
= m_aBookmarks
[_rName
];
203 // notify the listeners
204 if (m_aContainerListeners
.getLength())
206 ContainerEvent
aEvent(*this, makeAny(_rName
), makeAny(sOldBookmark
), Any());
207 OInterfaceIteratorHelper
aListenerIterator(m_aContainerListeners
);
208 while (aListenerIterator
.hasMoreElements())
209 static_cast< XContainerListener
* >(aListenerIterator
.next())->elementRemoved(aEvent
);
214 //--------------------------------------------------------------------------
215 void SAL_CALL
OBookmarkContainer::replaceByName( const ::rtl::OUString
& _rName
, const Any
& aElement
) throw(IllegalArgumentException
, NoSuchElementException
, WrappedTargetException
, RuntimeException
)
217 ClearableMutexGuard
aGuard(m_rMutex
);
218 checkValid(sal_True
);
220 // check the arguments
221 if (!_rName
.getLength())
222 throw IllegalArgumentException();
224 // do we have such an element?
225 if (!checkExistence(_rName
))
226 throw NoSuchElementException();
228 // approve the new object
229 ::rtl::OUString sNewLink
;
230 if (!(aElement
>>= sNewLink
))
231 throw IllegalArgumentException();
233 // the old element (for the notifications)
234 ::rtl::OUString sOldLink
= m_aBookmarks
[_rName
];
237 implReplace(_rName
, sNewLink
);
239 // notify the listeners
241 if (m_aContainerListeners
.getLength())
243 ContainerEvent
aEvent(*this, makeAny(_rName
), makeAny(sNewLink
), makeAny(sOldLink
));
244 OInterfaceIteratorHelper
aListenerIterator(m_aContainerListeners
);
245 while (aListenerIterator
.hasMoreElements())
246 static_cast< XContainerListener
* >(aListenerIterator
.next())->elementReplaced(aEvent
);
250 //--------------------------------------------------------------------------
251 void SAL_CALL
OBookmarkContainer::addContainerListener( const Reference
< XContainerListener
>& _rxListener
) throw(RuntimeException
)
253 MutexGuard
aGuard(m_rMutex
);
254 if (_rxListener
.is())
255 m_aContainerListeners
.addInterface(_rxListener
);
258 //--------------------------------------------------------------------------
259 void SAL_CALL
OBookmarkContainer::removeContainerListener( const Reference
< XContainerListener
>& _rxListener
) throw(RuntimeException
)
261 MutexGuard
aGuard(m_rMutex
);
262 if (_rxListener
.is())
263 m_aContainerListeners
.removeInterface(_rxListener
);
267 //--------------------------------------------------------------------------
268 Type SAL_CALL
OBookmarkContainer::getElementType( ) throw (RuntimeException
)
270 MutexGuard
aGuard(m_rMutex
);
271 checkValid(sal_False
);
272 return ::getCppuType( static_cast< ::rtl::OUString
* >(NULL
) );
275 //--------------------------------------------------------------------------
276 sal_Bool SAL_CALL
OBookmarkContainer::hasElements( ) throw (RuntimeException
)
278 MutexGuard
aGuard(m_rMutex
);
279 checkValid(sal_False
);
280 return !m_aBookmarks
.empty();
283 // XEnumerationAccess
284 //--------------------------------------------------------------------------
285 Reference
< XEnumeration
> SAL_CALL
OBookmarkContainer::createEnumeration( ) throw(RuntimeException
)
287 MutexGuard
aGuard(m_rMutex
);
288 checkValid(sal_False
);
289 return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess
*>(this));
292 //--------------------------------------------------------------------------
294 sal_Int32 SAL_CALL
OBookmarkContainer::getCount( ) throw(RuntimeException
)
296 MutexGuard
aGuard(m_rMutex
);
297 checkValid(sal_False
);
298 return m_aBookmarks
.size();
301 //--------------------------------------------------------------------------
302 Any SAL_CALL
OBookmarkContainer::getByIndex( sal_Int32 _nIndex
) throw(IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
)
304 MutexGuard
aGuard(m_rMutex
);
305 checkValid(sal_False
);
307 if ((_nIndex
< 0) || (_nIndex
>= (sal_Int32
)m_aBookmarksIndexed
.size()))
308 throw IndexOutOfBoundsException();
310 return makeAny(m_aBookmarksIndexed
[_nIndex
]->second
);
313 //--------------------------------------------------------------------------
314 Any SAL_CALL
OBookmarkContainer::getByName( const ::rtl::OUString
& _rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
)
316 MutexGuard
aGuard(m_rMutex
);
317 checkValid(sal_False
);
319 if (!checkExistence(_rName
))
320 throw NoSuchElementException();
322 return makeAny(m_aBookmarks
[_rName
]);
325 //--------------------------------------------------------------------------
326 Sequence
< ::rtl::OUString
> SAL_CALL
OBookmarkContainer::getElementNames( ) throw(RuntimeException
)
328 MutexGuard
aGuard(m_rMutex
);
329 checkValid(sal_False
);
331 Sequence
< ::rtl::OUString
> aNames(m_aBookmarks
.size());
332 ::rtl::OUString
* pNames
= aNames
.getArray();
334 for ( ConstMapIteratorVectorIterator aNameIter
= m_aBookmarksIndexed
.begin();
335 aNameIter
!= m_aBookmarksIndexed
.end();
336 ++pNames
, ++aNameIter
339 *pNames
= (*aNameIter
)->first
;
345 //--------------------------------------------------------------------------
346 sal_Bool SAL_CALL
OBookmarkContainer::hasByName( const ::rtl::OUString
& _rName
) throw(RuntimeException
)
348 MutexGuard
aGuard(m_rMutex
);
349 checkValid(sal_False
);
351 return checkExistence(_rName
);
354 //--------------------------------------------------------------------------
355 void OBookmarkContainer::implRemove(const ::rtl::OUString
& _rName
)
357 MutexGuard
aGuard(m_rMutex
);
359 // look for the name in the index access vector
360 MapString2StringIterator aMapPos
= m_aBookmarks
.end();
361 for ( MapIteratorVectorIterator aSearch
= m_aBookmarksIndexed
.begin();
362 aSearch
!= m_aBookmarksIndexed
.end();
367 ::rtl::OUString sName
= (*aSearch
)->first
;
369 if ((*aSearch
)->first
== _rName
)
372 m_aBookmarksIndexed
.erase(aSearch
);
377 if (m_aBookmarks
.end() == aMapPos
)
379 DBG_ERROR("OBookmarkContainer::implRemove: inconsistence!");
383 // remove the map entries
384 m_aBookmarks
.erase(aMapPos
);
387 //--------------------------------------------------------------------------
388 void OBookmarkContainer::implAppend(const ::rtl::OUString
& _rName
, const ::rtl::OUString
& _rDocumentLocation
)
390 MutexGuard
aGuard(m_rMutex
);
392 OSL_ENSURE(m_aBookmarks
.find(_rName
) == m_aBookmarks
.end(),"Bookmark already known!");
393 m_aBookmarksIndexed
.push_back(m_aBookmarks
.insert( MapString2String::value_type(_rName
,_rDocumentLocation
)).first
);
396 //--------------------------------------------------------------------------
397 void OBookmarkContainer::implReplace(const ::rtl::OUString
& _rName
, const ::rtl::OUString
& _rNewLink
)
399 MutexGuard
aGuard(m_rMutex
);
400 DBG_ASSERT(checkExistence(_rName
), "OBookmarkContainer::implReplace : invalid name !");
402 m_aBookmarks
[_rName
] = _rNewLink
;
405 //--------------------------------------------------------------------------
406 void OBookmarkContainer::checkValid(sal_Bool
/*_bIntendWriteAccess*/) const throw (RuntimeException
, DisposedException
)
410 //--------------------------------------------------------------------------
411 Reference
< XInterface
> SAL_CALL
OBookmarkContainer::getParent( ) throw (RuntimeException
)
416 //--------------------------------------------------------------------------
417 void SAL_CALL
OBookmarkContainer::setParent( const Reference
< XInterface
>& /*Parent*/ ) throw (NoSupportException
, RuntimeException
)
419 throw NoSupportException();
422 //........................................................................
423 } // namespace dbaccess
424 //........................................................................