Line data Source code
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 :
21 : #include "bookmarkcontainer.hxx"
22 : #include "dbastrings.hrc"
23 : #include "apitools.hxx"
24 : #include "core_resource.hxx"
25 : #include "core_resource.hrc"
26 :
27 : #include <tools/debug.hxx>
28 : #include <osl/diagnose.h>
29 : #include <comphelper/sequence.hxx>
30 : #include <comphelper/enumhelper.hxx>
31 : #include <comphelper/extract.hxx>
32 : #include <com/sun/star/lang/XComponent.hpp>
33 : #include <comphelper/types.hxx>
34 :
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;
42 :
43 : namespace dbaccess
44 : {
45 :
46 : //==========================================================================
47 : //= OBookmarkContainer
48 : //==========================================================================
49 : DBG_NAME(OBookmarkContainer)
50 :
51 0 : OBookmarkContainer::OBookmarkContainer(OWeakObject& _rParent, Mutex& _rMutex)
52 : :m_rParent(_rParent)
53 : ,m_aContainerListeners(_rMutex)
54 0 : ,m_rMutex(_rMutex)
55 : {
56 : DBG_CTOR(OBookmarkContainer, NULL);
57 0 : }
58 :
59 0 : void OBookmarkContainer::dispose()
60 : {
61 0 : MutexGuard aGuard(m_rMutex);
62 :
63 : // say goodbye to our listeners
64 0 : EventObject aEvt(*this);
65 0 : m_aContainerListeners.disposeAndClear(aEvt);
66 :
67 : // remove our elements
68 0 : m_aBookmarksIndexed.clear();
69 0 : m_aBookmarks.clear();
70 0 : }
71 :
72 0 : void SAL_CALL OBookmarkContainer::acquire( ) throw()
73 : {
74 0 : m_rParent.acquire();
75 0 : }
76 :
77 0 : void SAL_CALL OBookmarkContainer::release( ) throw()
78 : {
79 0 : m_rParent.release();
80 0 : }
81 :
82 0 : OBookmarkContainer::~OBookmarkContainer()
83 : {
84 : DBG_DTOR(OBookmarkContainer, NULL);
85 0 : }
86 :
87 : // XServiceInfo
88 0 : ::rtl::OUString SAL_CALL OBookmarkContainer::getImplementationName( ) throw(RuntimeException)
89 : {
90 0 : return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.dba.OBookmarkContainer"));
91 : }
92 :
93 0 : sal_Bool SAL_CALL OBookmarkContainer::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
94 : {
95 0 : MutexGuard aGuard(m_rMutex);
96 0 : checkValid(sal_False);
97 0 : return findValue(getSupportedServiceNames(), _rServiceName, sal_True).getLength() != 0;
98 : }
99 :
100 0 : Sequence< ::rtl::OUString > SAL_CALL OBookmarkContainer::getSupportedServiceNames( ) throw(RuntimeException)
101 : {
102 0 : Sequence< ::rtl::OUString > aReturn(1);
103 0 : aReturn.getArray()[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.DefinitionContainer"));
104 0 : return aReturn;
105 : }
106 :
107 : // XNameContainer
108 0 : void SAL_CALL OBookmarkContainer::insertByName( const ::rtl::OUString& _rName, const Any& aElement ) throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
109 : {
110 0 : MutexGuard aGuard(m_rMutex);
111 0 : checkValid(sal_True);
112 :
113 0 : if (checkExistence(_rName))
114 0 : throw ElementExistException();
115 :
116 0 : if (_rName.isEmpty())
117 0 : throw IllegalArgumentException();
118 :
119 : // approve the new object
120 0 : ::rtl::OUString sNewLink;
121 0 : if (!(aElement >>= sNewLink))
122 0 : throw IllegalArgumentException();
123 :
124 :
125 0 : implAppend(_rName, sNewLink);
126 :
127 : // notify the listeners
128 0 : if (m_aContainerListeners.getLength())
129 : {
130 0 : ContainerEvent aEvent(*this, makeAny(_rName), makeAny(sNewLink), Any());
131 0 : OInterfaceIteratorHelper aListenerIterator(m_aContainerListeners);
132 0 : while (aListenerIterator.hasMoreElements())
133 0 : static_cast< XContainerListener* >(aListenerIterator.next())->elementInserted(aEvent);
134 0 : }
135 0 : }
136 :
137 0 : void SAL_CALL OBookmarkContainer::removeByName( const ::rtl::OUString& _rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
138 : {
139 0 : ::rtl::OUString sOldBookmark;
140 : {
141 0 : MutexGuard aGuard(m_rMutex);
142 0 : checkValid(sal_True);
143 :
144 : // check the arguments
145 0 : if (_rName.isEmpty())
146 0 : throw IllegalArgumentException();
147 :
148 0 : if (!checkExistence(_rName))
149 0 : throw NoSuchElementException();
150 :
151 : // the old element (for the notifications)
152 0 : sOldBookmark = m_aBookmarks[_rName];
153 :
154 : // do the removal
155 0 : implRemove(_rName);
156 : }
157 :
158 : // notify the listeners
159 0 : if (m_aContainerListeners.getLength())
160 : {
161 0 : ContainerEvent aEvent(*this, makeAny(_rName), makeAny(sOldBookmark), Any());
162 0 : OInterfaceIteratorHelper aListenerIterator(m_aContainerListeners);
163 0 : while (aListenerIterator.hasMoreElements())
164 0 : static_cast< XContainerListener* >(aListenerIterator.next())->elementRemoved(aEvent);
165 0 : }
166 0 : }
167 :
168 : // XNameReplace
169 0 : void SAL_CALL OBookmarkContainer::replaceByName( const ::rtl::OUString& _rName, const Any& aElement ) throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
170 : {
171 0 : ClearableMutexGuard aGuard(m_rMutex);
172 0 : checkValid(sal_True);
173 :
174 : // check the arguments
175 0 : if (_rName.isEmpty())
176 0 : throw IllegalArgumentException();
177 :
178 : // do we have such an element?
179 0 : if (!checkExistence(_rName))
180 0 : throw NoSuchElementException();
181 :
182 : // approve the new object
183 0 : ::rtl::OUString sNewLink;
184 0 : if (!(aElement >>= sNewLink))
185 0 : throw IllegalArgumentException();
186 :
187 : // the old element (for the notifications)
188 0 : ::rtl::OUString sOldLink = m_aBookmarks[_rName];
189 :
190 : // do the replace
191 0 : implReplace(_rName, sNewLink);
192 :
193 : // notify the listeners
194 0 : aGuard.clear();
195 0 : if (m_aContainerListeners.getLength())
196 : {
197 0 : ContainerEvent aEvent(*this, makeAny(_rName), makeAny(sNewLink), makeAny(sOldLink));
198 0 : OInterfaceIteratorHelper aListenerIterator(m_aContainerListeners);
199 0 : while (aListenerIterator.hasMoreElements())
200 0 : static_cast< XContainerListener* >(aListenerIterator.next())->elementReplaced(aEvent);
201 0 : }
202 0 : }
203 :
204 0 : void SAL_CALL OBookmarkContainer::addContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
205 : {
206 0 : MutexGuard aGuard(m_rMutex);
207 0 : if (_rxListener.is())
208 0 : m_aContainerListeners.addInterface(_rxListener);
209 0 : }
210 :
211 0 : void SAL_CALL OBookmarkContainer::removeContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
212 : {
213 0 : MutexGuard aGuard(m_rMutex);
214 0 : if (_rxListener.is())
215 0 : m_aContainerListeners.removeInterface(_rxListener);
216 0 : }
217 :
218 : // XElementAccess
219 0 : Type SAL_CALL OBookmarkContainer::getElementType( ) throw (RuntimeException)
220 : {
221 0 : MutexGuard aGuard(m_rMutex);
222 0 : checkValid(sal_False);
223 0 : return ::getCppuType( static_cast< ::rtl::OUString* >(NULL) );
224 : }
225 :
226 0 : sal_Bool SAL_CALL OBookmarkContainer::hasElements( ) throw (RuntimeException)
227 : {
228 0 : MutexGuard aGuard(m_rMutex);
229 0 : checkValid(sal_False);
230 0 : return !m_aBookmarks.empty();
231 : }
232 :
233 : // XEnumerationAccess
234 0 : Reference< XEnumeration > SAL_CALL OBookmarkContainer::createEnumeration( ) throw(RuntimeException)
235 : {
236 0 : MutexGuard aGuard(m_rMutex);
237 0 : checkValid(sal_False);
238 0 : return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess*>(this));
239 : }
240 :
241 : // XIndexAccess
242 0 : sal_Int32 SAL_CALL OBookmarkContainer::getCount( ) throw(RuntimeException)
243 : {
244 0 : MutexGuard aGuard(m_rMutex);
245 0 : checkValid(sal_False);
246 0 : return m_aBookmarks.size();
247 : }
248 :
249 0 : Any SAL_CALL OBookmarkContainer::getByIndex( sal_Int32 _nIndex ) throw(IndexOutOfBoundsException, WrappedTargetException, RuntimeException)
250 : {
251 0 : MutexGuard aGuard(m_rMutex);
252 0 : checkValid(sal_False);
253 :
254 0 : if ((_nIndex < 0) || (_nIndex >= (sal_Int32)m_aBookmarksIndexed.size()))
255 0 : throw IndexOutOfBoundsException();
256 :
257 0 : return makeAny(m_aBookmarksIndexed[_nIndex]->second);
258 : }
259 :
260 0 : Any SAL_CALL OBookmarkContainer::getByName( const ::rtl::OUString& _rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
261 : {
262 0 : MutexGuard aGuard(m_rMutex);
263 0 : checkValid(sal_False);
264 :
265 0 : if (!checkExistence(_rName))
266 0 : throw NoSuchElementException();
267 :
268 0 : return makeAny(m_aBookmarks[_rName]);
269 : }
270 :
271 0 : Sequence< ::rtl::OUString > SAL_CALL OBookmarkContainer::getElementNames( ) throw(RuntimeException)
272 : {
273 0 : MutexGuard aGuard(m_rMutex);
274 0 : checkValid(sal_False);
275 :
276 0 : Sequence< ::rtl::OUString > aNames(m_aBookmarks.size());
277 0 : ::rtl::OUString* pNames = aNames.getArray();
278 : ;
279 0 : for ( ConstMapIteratorVectorIterator aNameIter = m_aBookmarksIndexed.begin();
280 0 : aNameIter != m_aBookmarksIndexed.end();
281 : ++pNames, ++aNameIter
282 : )
283 : {
284 0 : *pNames = (*aNameIter)->first;
285 : }
286 :
287 0 : return aNames;
288 : }
289 :
290 0 : sal_Bool SAL_CALL OBookmarkContainer::hasByName( const ::rtl::OUString& _rName ) throw(RuntimeException)
291 : {
292 0 : MutexGuard aGuard(m_rMutex);
293 0 : checkValid(sal_False);
294 :
295 0 : return checkExistence(_rName);
296 : }
297 :
298 0 : void OBookmarkContainer::implRemove(const ::rtl::OUString& _rName)
299 : {
300 0 : MutexGuard aGuard(m_rMutex);
301 :
302 : // look for the name in the index access vector
303 0 : MapString2StringIterator aMapPos = m_aBookmarks.end();
304 0 : for ( MapIteratorVectorIterator aSearch = m_aBookmarksIndexed.begin();
305 0 : aSearch != m_aBookmarksIndexed.end();
306 : ++aSearch
307 : )
308 : {
309 : #ifdef DBG_UTIL
310 : ::rtl::OUString sName = (*aSearch)->first;
311 : #endif
312 0 : if ((*aSearch)->first == _rName)
313 : {
314 0 : aMapPos = *aSearch;
315 0 : m_aBookmarksIndexed.erase(aSearch);
316 0 : break;
317 : }
318 : }
319 :
320 0 : if (m_aBookmarks.end() == aMapPos)
321 : {
322 : OSL_FAIL("OBookmarkContainer::implRemove: inconsistence!");
323 0 : return;
324 : }
325 :
326 : // remove the map entries
327 0 : m_aBookmarks.erase(aMapPos);
328 : }
329 :
330 0 : void OBookmarkContainer::implAppend(const ::rtl::OUString& _rName, const ::rtl::OUString& _rDocumentLocation)
331 : {
332 0 : MutexGuard aGuard(m_rMutex);
333 :
334 : OSL_ENSURE(m_aBookmarks.find(_rName) == m_aBookmarks.end(),"Bookmark already known!");
335 0 : m_aBookmarksIndexed.push_back(m_aBookmarks.insert( MapString2String::value_type(_rName,_rDocumentLocation)).first);
336 0 : }
337 :
338 0 : void OBookmarkContainer::implReplace(const ::rtl::OUString& _rName, const ::rtl::OUString& _rNewLink)
339 : {
340 0 : MutexGuard aGuard(m_rMutex);
341 : OSL_ENSURE(checkExistence(_rName), "OBookmarkContainer::implReplace : invalid name !");
342 :
343 0 : m_aBookmarks[_rName] = _rNewLink;
344 0 : }
345 :
346 0 : void OBookmarkContainer::checkValid(sal_Bool /*_bIntendWriteAccess*/) const throw (RuntimeException, DisposedException)
347 : {
348 0 : }
349 :
350 0 : Reference< XInterface > SAL_CALL OBookmarkContainer::getParent( ) throw (RuntimeException)
351 : {
352 0 : return m_rParent;
353 : }
354 :
355 0 : void SAL_CALL OBookmarkContainer::setParent( const Reference< XInterface >& /*Parent*/ ) throw (NoSupportException, RuntimeException)
356 : {
357 0 : throw NoSupportException();
358 : }
359 :
360 : } // namespace dbaccess
361 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|