1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #ifndef _DBA_CORE_BOOKMARKCONTAINER_HXX_
28 #include "bookmarkcontainer.hxx"
29 #endif
30 #ifndef DBACCESS_SHARED_DBASTRINGS_HRC
31 #include "dbastrings.hrc"
32 #endif
33 #ifndef _DBASHARED_APITOOLS_HXX_
34 #include "apitools.hxx"
35 #endif
36 #ifndef _DBA_CORE_RESOURCE_HXX_
37 #include "core_resource.hxx"
38 #endif
39 #ifndef _DBA_CORE_RESOURCE_HRC_
40 #include "core_resource.hrc"
41 #endif
42 
43 #ifndef _TOOLS_DEBUG_HXX
44 #include <tools/debug.hxx>
45 #endif
46 #ifndef _COMPHELPER_SEQUENCE_HXX_
47 #include <comphelper/sequence.hxx>
48 #endif
49 #ifndef _COMPHELPER_ENUMHELPER_HXX_
50 #include <comphelper/enumhelper.hxx>
51 #endif
52 #ifndef _COMPHELPER_EXTRACT_HXX_
53 #include <comphelper/extract.hxx>
54 #endif
55 #ifndef _COM_SUN_STAR_LANG_XCOMPONENT_HPP_
56 #include <com/sun/star/lang/XComponent.hpp>
57 #endif
58 #ifndef _COMPHELPER_TYPES_HXX_
59 #include <comphelper/types.hxx>
60 #endif
61 
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::lang;
64 using namespace ::com::sun::star::beans;
65 using namespace ::com::sun::star::container;
66 using namespace ::osl;
67 using namespace ::comphelper;
68 using namespace ::cppu;
69 
70 //........................................................................
71 namespace dbaccess
72 {
73 //........................................................................
74 
75 //==========================================================================
76 //= OBookmarkContainer
77 //==========================================================================
DBG_NAME(OBookmarkContainer)78 DBG_NAME(OBookmarkContainer)
79 //--------------------------------------------------------------------------
80 OBookmarkContainer::OBookmarkContainer(OWeakObject& _rParent, Mutex& _rMutex)
81 	:m_rParent(_rParent)
82 	,m_aContainerListeners(_rMutex)
83 	,m_rMutex(_rMutex)
84 {
85 	DBG_CTOR(OBookmarkContainer, NULL);
86 }
87 
88 //--------------------------------------------------------------------------
dispose()89 void OBookmarkContainer::dispose()
90 {
91 	MutexGuard aGuard(m_rMutex);
92 
93 	// say our listeners goobye
94 	EventObject aEvt(*this);
95 	m_aContainerListeners.disposeAndClear(aEvt);
96 
97 	// remove our elements
98 	m_aBookmarksIndexed.clear();
99 	m_aBookmarks.clear();
100 }
101 
102 //--------------------------------------------------------------------------
acquire()103 void SAL_CALL OBookmarkContainer::acquire(  ) throw()
104 {
105 	m_rParent.acquire();
106 }
107 
108 //--------------------------------------------------------------------------
release()109 void SAL_CALL OBookmarkContainer::release(  ) throw()
110 {
111 	m_rParent.release();
112 }
113 
114 //--------------------------------------------------------------------------
~OBookmarkContainer()115 OBookmarkContainer::~OBookmarkContainer()
116 {
117 	DBG_DTOR(OBookmarkContainer, NULL);
118 }
119 
120 // XServiceInfo
121 //--------------------------------------------------------------------------
getImplementationName()122 ::rtl::OUString SAL_CALL OBookmarkContainer::getImplementationName(  ) throw(RuntimeException)
123 {
124 	return ::rtl::OUString::createFromAscii("com.sun.star.comp.dba.OBookmarkContainer");
125 }
126 
127 //--------------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)128 sal_Bool SAL_CALL OBookmarkContainer::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
129 {
130 	MutexGuard aGuard(m_rMutex);
131 	checkValid(sal_False);
132 	return findValue(getSupportedServiceNames(), _rServiceName, sal_True).getLength() != 0;
133 }
134 
135 //--------------------------------------------------------------------------
getSupportedServiceNames()136 Sequence< ::rtl::OUString > SAL_CALL OBookmarkContainer::getSupportedServiceNames(  ) throw(RuntimeException)
137 {
138 	Sequence< ::rtl::OUString > aReturn(1);
139 	aReturn.getArray()[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdb.DefinitionContainer");
140 	return aReturn;
141 }
142 
143 // XNameContainer
144 //--------------------------------------------------------------------------
insertByName(const::rtl::OUString & _rName,const Any & aElement)145 void SAL_CALL OBookmarkContainer::insertByName( const ::rtl::OUString& _rName, const Any& aElement ) throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
146 {
147 	MutexGuard aGuard(m_rMutex);
148 	checkValid(sal_True);
149 
150 	if (checkExistence(_rName))
151 		throw ElementExistException();
152 
153 	if (0 == _rName.getLength())
154 		throw IllegalArgumentException();
155 
156 	// approve the new object
157 	::rtl::OUString sNewLink;
158 	if (!(aElement >>= sNewLink))
159 		throw IllegalArgumentException();
160 
161 
162 	implAppend(_rName, sNewLink);
163 
164 	// notify the listeners
165 	if (m_aContainerListeners.getLength())
166 	{
167 		ContainerEvent aEvent(*this, makeAny(_rName), makeAny(sNewLink), Any());
168 		OInterfaceIteratorHelper aListenerIterator(m_aContainerListeners);
169 		while (aListenerIterator.hasMoreElements())
170 			static_cast< XContainerListener* >(aListenerIterator.next())->elementInserted(aEvent);
171 	}
172 }
173 
174 //--------------------------------------------------------------------------
removeByName(const::rtl::OUString & _rName)175 void SAL_CALL OBookmarkContainer::removeByName( const ::rtl::OUString& _rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
176 {
177 	::rtl::OUString sOldBookmark;
178 	{
179 		MutexGuard aGuard(m_rMutex);
180 		checkValid(sal_True);
181 
182 		// check the arguments
183 		if (!_rName.getLength())
184 			throw IllegalArgumentException();
185 
186 		if (!checkExistence(_rName))
187 			throw NoSuchElementException();
188 
189 		// the old element (for the notifications)
190 		sOldBookmark = m_aBookmarks[_rName];
191 
192 		// do the removal
193 		implRemove(_rName);
194 	}
195 
196 	// notify the listeners
197 	if (m_aContainerListeners.getLength())
198 	{
199 		ContainerEvent aEvent(*this, makeAny(_rName), makeAny(sOldBookmark), Any());
200 		OInterfaceIteratorHelper aListenerIterator(m_aContainerListeners);
201 		while (aListenerIterator.hasMoreElements())
202 			static_cast< XContainerListener* >(aListenerIterator.next())->elementRemoved(aEvent);
203 	}
204 }
205 
206 // XNameReplace
207 //--------------------------------------------------------------------------
replaceByName(const::rtl::OUString & _rName,const Any & aElement)208 void SAL_CALL OBookmarkContainer::replaceByName( const ::rtl::OUString& _rName, const Any& aElement ) throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
209 {
210 	ClearableMutexGuard aGuard(m_rMutex);
211 	checkValid(sal_True);
212 
213 	// check the arguments
214 	if (!_rName.getLength())
215 		throw IllegalArgumentException();
216 
217 	// do we have such an element?
218 	if (!checkExistence(_rName))
219 		throw NoSuchElementException();
220 
221 	// approve the new object
222 	::rtl::OUString sNewLink;
223 	if (!(aElement >>= sNewLink))
224 		throw IllegalArgumentException();
225 
226 	// the old element (for the notifications)
227 	::rtl::OUString sOldLink = m_aBookmarks[_rName];
228 
229 	// do the replace
230 	implReplace(_rName, sNewLink);
231 
232 	// notify the listeners
233 	aGuard.clear();
234 	if (m_aContainerListeners.getLength())
235 	{
236 		ContainerEvent aEvent(*this, makeAny(_rName), makeAny(sNewLink), makeAny(sOldLink));
237 		OInterfaceIteratorHelper aListenerIterator(m_aContainerListeners);
238 		while (aListenerIterator.hasMoreElements())
239 			static_cast< XContainerListener* >(aListenerIterator.next())->elementReplaced(aEvent);
240 	}
241 }
242 
243 //--------------------------------------------------------------------------
addContainerListener(const Reference<XContainerListener> & _rxListener)244 void SAL_CALL OBookmarkContainer::addContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
245 {
246 	MutexGuard aGuard(m_rMutex);
247 	if (_rxListener.is())
248 		m_aContainerListeners.addInterface(_rxListener);
249 }
250 
251 //--------------------------------------------------------------------------
removeContainerListener(const Reference<XContainerListener> & _rxListener)252 void SAL_CALL OBookmarkContainer::removeContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
253 {
254 	MutexGuard aGuard(m_rMutex);
255 	if (_rxListener.is())
256 		m_aContainerListeners.removeInterface(_rxListener);
257 }
258 
259 // XElementAccess
260 //--------------------------------------------------------------------------
getElementType()261 Type SAL_CALL OBookmarkContainer::getElementType( ) throw (RuntimeException)
262 {
263 	MutexGuard aGuard(m_rMutex);
264 	checkValid(sal_False);
265 	return ::getCppuType( static_cast< ::rtl::OUString* >(NULL) );
266 }
267 
268 //--------------------------------------------------------------------------
hasElements()269 sal_Bool SAL_CALL OBookmarkContainer::hasElements( ) throw (RuntimeException)
270 {
271 	MutexGuard aGuard(m_rMutex);
272 	checkValid(sal_False);
273 	return !m_aBookmarks.empty();
274 }
275 
276 // XEnumerationAccess
277 //--------------------------------------------------------------------------
createEnumeration()278 Reference< XEnumeration > SAL_CALL OBookmarkContainer::createEnumeration(  ) throw(RuntimeException)
279 {
280 	MutexGuard aGuard(m_rMutex);
281 	checkValid(sal_False);
282 	return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess*>(this));
283 }
284 
285 //--------------------------------------------------------------------------
286 // XIndexAccess
getCount()287 sal_Int32 SAL_CALL OBookmarkContainer::getCount(  ) throw(RuntimeException)
288 {
289 	MutexGuard aGuard(m_rMutex);
290 	checkValid(sal_False);
291 	return m_aBookmarks.size();
292 }
293 
294 //--------------------------------------------------------------------------
getByIndex(sal_Int32 _nIndex)295 Any SAL_CALL OBookmarkContainer::getByIndex( sal_Int32 _nIndex ) throw(IndexOutOfBoundsException, WrappedTargetException, RuntimeException)
296 {
297 	MutexGuard aGuard(m_rMutex);
298 	checkValid(sal_False);
299 
300 	if ((_nIndex < 0) || (_nIndex >= (sal_Int32)m_aBookmarksIndexed.size()))
301 		throw IndexOutOfBoundsException();
302 
303 	return makeAny(m_aBookmarksIndexed[_nIndex]->second);
304 }
305 
306 //--------------------------------------------------------------------------
getByName(const::rtl::OUString & _rName)307 Any SAL_CALL OBookmarkContainer::getByName( const ::rtl::OUString& _rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
308 {
309 	MutexGuard aGuard(m_rMutex);
310 	checkValid(sal_False);
311 
312 	if (!checkExistence(_rName))
313 		throw NoSuchElementException();
314 
315 	return makeAny(m_aBookmarks[_rName]);
316 }
317 
318 //--------------------------------------------------------------------------
getElementNames()319 Sequence< ::rtl::OUString > SAL_CALL OBookmarkContainer::getElementNames(  ) throw(RuntimeException)
320 {
321 	MutexGuard aGuard(m_rMutex);
322 	checkValid(sal_False);
323 
324 	Sequence< ::rtl::OUString > aNames(m_aBookmarks.size());
325 	::rtl::OUString* pNames = aNames.getArray();
326 	;
327 	for	(	ConstMapIteratorVectorIterator aNameIter = m_aBookmarksIndexed.begin();
328 			aNameIter != m_aBookmarksIndexed.end();
329 			++pNames, ++aNameIter
330 		)
331 	{
332 		*pNames = (*aNameIter)->first;
333 	}
334 
335 	return aNames;
336 }
337 
338 //--------------------------------------------------------------------------
hasByName(const::rtl::OUString & _rName)339 sal_Bool SAL_CALL OBookmarkContainer::hasByName( const ::rtl::OUString& _rName ) throw(RuntimeException)
340 {
341 	MutexGuard aGuard(m_rMutex);
342 	checkValid(sal_False);
343 
344 	return checkExistence(_rName);
345 }
346 
347 //--------------------------------------------------------------------------
implRemove(const::rtl::OUString & _rName)348 void OBookmarkContainer::implRemove(const ::rtl::OUString& _rName)
349 {
350 	MutexGuard aGuard(m_rMutex);
351 
352 	// look for the name in the index access vector
353 	MapString2StringIterator aMapPos = m_aBookmarks.end();
354 	for (	MapIteratorVectorIterator aSearch = m_aBookmarksIndexed.begin();
355 			aSearch != m_aBookmarksIndexed.end();
356 			++aSearch
357 		)
358 	{
359 #ifdef DBG_UTIL
360 		::rtl::OUString sName = (*aSearch)->first;
361 #endif
362 		if ((*aSearch)->first == _rName)
363 		{
364 			aMapPos = *aSearch;
365 			m_aBookmarksIndexed.erase(aSearch);
366 			break;
367 		}
368 	}
369 
370 	if (m_aBookmarks.end() == aMapPos)
371 	{
372 		DBG_ERROR("OBookmarkContainer::implRemove: inconsistence!");
373 		return;
374 	}
375 
376 	// remove the map entries
377 	m_aBookmarks.erase(aMapPos);
378 }
379 
380 //--------------------------------------------------------------------------
implAppend(const::rtl::OUString & _rName,const::rtl::OUString & _rDocumentLocation)381 void OBookmarkContainer::implAppend(const ::rtl::OUString& _rName, const ::rtl::OUString& _rDocumentLocation)
382 {
383 	MutexGuard aGuard(m_rMutex);
384 
385 	OSL_ENSURE(m_aBookmarks.find(_rName) == m_aBookmarks.end(),"Bookmark already known!");
386 	m_aBookmarksIndexed.push_back(m_aBookmarks.insert(	MapString2String::value_type(_rName,_rDocumentLocation)).first);
387 }
388 
389 //--------------------------------------------------------------------------
implReplace(const::rtl::OUString & _rName,const::rtl::OUString & _rNewLink)390 void OBookmarkContainer::implReplace(const ::rtl::OUString& _rName, const ::rtl::OUString& _rNewLink)
391 {
392 	MutexGuard aGuard(m_rMutex);
393 	DBG_ASSERT(checkExistence(_rName), "OBookmarkContainer::implReplace : invalid name !");
394 
395 	m_aBookmarks[_rName] = _rNewLink;
396 }
397 
398 //--------------------------------------------------------------------------
checkValid(sal_Bool) const399 void OBookmarkContainer::checkValid(sal_Bool /*_bIntendWriteAccess*/) const throw (RuntimeException, DisposedException)
400 {
401 }
402 
403 //--------------------------------------------------------------------------
getParent()404 Reference< XInterface > SAL_CALL OBookmarkContainer::getParent(  ) throw (RuntimeException)
405 {
406 	return m_rParent;
407 }
408 
409 //--------------------------------------------------------------------------
setParent(const Reference<XInterface> &)410 void SAL_CALL OBookmarkContainer::setParent( const Reference< XInterface >& /*Parent*/ ) throw (NoSupportException, RuntimeException)
411 {
412 	throw NoSupportException();
413 }
414 
415 //........................................................................
416 }	// namespace dbaccess
417 //........................................................................
418