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