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