update dev300-m58
[ooovba.git] / ucb / source / ucp / hierarchy / hierarchydatasupplier.cxx
blobd254d4259be31d596c48eb86e07182e26f4f9750
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hierarchydatasupplier.cxx,v $
10 * $Revision: 1.12 $
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_ucb.hxx"
34 /**************************************************************************
35 TODO
36 **************************************************************************
38 *************************************************************************/
40 #include <vector>
41 #include <ucbhelper/contentidentifier.hxx>
42 #include "hierarchydatasupplier.hxx"
43 #include "hierarchyprovider.hxx"
44 #include "hierarchycontent.hxx"
46 using namespace com::sun::star;
47 using namespace hierarchy_ucp;
49 namespace hierarchy_ucp
52 //=========================================================================
54 // struct ResultListEntry.
56 //=========================================================================
58 struct ResultListEntry
60 rtl::OUString aId;
61 uno::Reference< ucb::XContentIdentifier > xId;
62 uno::Reference< ucb::XContent > xContent;
63 uno::Reference< sdbc::XRow > xRow;
64 HierarchyEntryData aData;
66 ResultListEntry( const HierarchyEntryData& rEntry ) : aData( rEntry ) {}
69 //=========================================================================
71 // ResultList.
73 //=========================================================================
75 typedef std::vector< ResultListEntry* > ResultList;
77 //=========================================================================
79 // struct DataSupplier_Impl.
81 //=========================================================================
83 struct DataSupplier_Impl
85 osl::Mutex m_aMutex;
86 ResultList m_aResults;
87 rtl::Reference< HierarchyContent > m_xContent;
88 uno::Reference< lang::XMultiServiceFactory > m_xSMgr;
89 HierarchyEntry m_aFolder;
90 HierarchyEntry::iterator m_aIterator;
91 sal_Int32 m_nOpenMode;
92 sal_Bool m_bCountFinal;
94 DataSupplier_Impl(
95 const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
96 const rtl::Reference< HierarchyContent >& rContent,
97 sal_Int32 nOpenMode )
98 : m_xContent( rContent ), m_xSMgr( rxSMgr ),
99 m_aFolder( rxSMgr,
100 static_cast< HierarchyContentProvider * >(
101 rContent->getProvider().get() ),
102 rContent->getIdentifier()->getContentIdentifier() ),
103 m_nOpenMode( nOpenMode ), m_bCountFinal( sal_False ) {}
104 ~DataSupplier_Impl();
107 //=========================================================================
108 DataSupplier_Impl::~DataSupplier_Impl()
110 ResultList::const_iterator it = m_aResults.begin();
111 ResultList::const_iterator end = m_aResults.end();
113 while ( it != end )
115 delete (*it);
116 it++;
122 //=========================================================================
123 //=========================================================================
125 // HierarchyResultSetDataSupplier Implementation.
127 //=========================================================================
128 //=========================================================================
130 HierarchyResultSetDataSupplier::HierarchyResultSetDataSupplier(
131 const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
132 const rtl::Reference< HierarchyContent >& rContent,
133 sal_Int32 nOpenMode )
134 : m_pImpl( new DataSupplier_Impl( rxSMgr, rContent, nOpenMode ) )
138 //=========================================================================
139 // virtual
140 HierarchyResultSetDataSupplier::~HierarchyResultSetDataSupplier()
142 delete m_pImpl;
145 //=========================================================================
146 // virtual
147 rtl::OUString HierarchyResultSetDataSupplier::queryContentIdentifierString(
148 sal_uInt32 nIndex )
150 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
152 if ( nIndex < m_pImpl->m_aResults.size() )
154 rtl::OUString aId = m_pImpl->m_aResults[ nIndex ]->aId;
155 if ( aId.getLength() )
157 // Already cached.
158 return aId;
162 if ( getResult( nIndex ) )
164 rtl::OUString aId
165 = m_pImpl->m_xContent->getIdentifier()->getContentIdentifier();
167 if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() )
168 aId += rtl::OUString::createFromAscii( "/" );
170 aId += m_pImpl->m_aResults[ nIndex ]->aData.getName();
172 m_pImpl->m_aResults[ nIndex ]->aId = aId;
173 return aId;
175 return rtl::OUString();
178 //=========================================================================
179 // virtual
180 uno::Reference< ucb::XContentIdentifier >
181 HierarchyResultSetDataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
183 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
185 if ( nIndex < m_pImpl->m_aResults.size() )
187 uno::Reference< ucb::XContentIdentifier > xId
188 = m_pImpl->m_aResults[ nIndex ]->xId;
189 if ( xId.is() )
191 // Already cached.
192 return xId;
196 rtl::OUString aId = queryContentIdentifierString( nIndex );
197 if ( aId.getLength() )
199 uno::Reference< ucb::XContentIdentifier > xId
200 = new ::ucbhelper::ContentIdentifier( aId );
201 m_pImpl->m_aResults[ nIndex ]->xId = xId;
202 return xId;
204 return uno::Reference< ucb::XContentIdentifier >();
207 //=========================================================================
208 // virtual
209 uno::Reference< ucb::XContent >
210 HierarchyResultSetDataSupplier::queryContent( sal_uInt32 nIndex )
212 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
214 if ( nIndex < m_pImpl->m_aResults.size() )
216 uno::Reference< ucb::XContent > xContent
217 = m_pImpl->m_aResults[ nIndex ]->xContent;
218 if ( xContent.is() )
220 // Already cached.
221 return xContent;
225 uno::Reference< ucb::XContentIdentifier > xId
226 = queryContentIdentifier( nIndex );
227 if ( xId.is() )
231 uno::Reference< ucb::XContent > xContent
232 = m_pImpl->m_xContent->getProvider()->queryContent( xId );
233 m_pImpl->m_aResults[ nIndex ]->xContent = xContent;
234 return xContent;
237 catch ( ucb::IllegalIdentifierException const & )
241 return uno::Reference< ucb::XContent >();
244 //=========================================================================
245 // virtual
246 sal_Bool HierarchyResultSetDataSupplier::getResult( sal_uInt32 nIndex )
248 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
250 if ( m_pImpl->m_aResults.size() > nIndex )
252 // Result already present.
253 return sal_True;
256 // Result not (yet) present.
258 if ( m_pImpl->m_bCountFinal )
259 return sal_False;
261 // Try to obtain result...
263 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
264 sal_Bool bFound = sal_False;
265 sal_uInt32 nPos = nOldCount;
267 while ( m_pImpl->m_aFolder.next( m_pImpl->m_aIterator ) )
269 const HierarchyEntryData& rResult = *m_pImpl->m_aIterator;
270 if ( checkResult( rResult ) )
272 m_pImpl->m_aResults.push_back( new ResultListEntry( rResult ) );
274 if ( nPos == nIndex )
276 // Result obtained.
277 bFound = sal_True;
278 break;
281 nPos++;
284 if ( !bFound )
285 m_pImpl->m_bCountFinal = sal_True;
287 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
288 if ( xResultSet.is() )
290 // Callbacks follow!
291 aGuard.clear();
293 if ( nOldCount < m_pImpl->m_aResults.size() )
294 xResultSet->rowCountChanged(
295 nOldCount, m_pImpl->m_aResults.size() );
297 if ( m_pImpl->m_bCountFinal )
298 xResultSet->rowCountFinal();
301 return bFound;
304 //=========================================================================
305 // virtual
306 sal_uInt32 HierarchyResultSetDataSupplier::totalCount()
308 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
310 if ( m_pImpl->m_bCountFinal )
311 return m_pImpl->m_aResults.size();
313 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
315 while ( m_pImpl->m_aFolder.next( m_pImpl->m_aIterator ) )
317 const HierarchyEntryData& rResult = *m_pImpl->m_aIterator;
318 if ( checkResult( rResult ) )
319 m_pImpl->m_aResults.push_back( new ResultListEntry( rResult ) );
322 m_pImpl->m_bCountFinal = sal_True;
324 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
325 if ( xResultSet.is() )
327 // Callbacks follow!
328 aGuard.clear();
330 if ( nOldCount < m_pImpl->m_aResults.size() )
331 xResultSet->rowCountChanged(
332 nOldCount, m_pImpl->m_aResults.size() );
334 xResultSet->rowCountFinal();
337 return m_pImpl->m_aResults.size();
340 //=========================================================================
341 // virtual
342 sal_uInt32 HierarchyResultSetDataSupplier::currentCount()
344 return m_pImpl->m_aResults.size();
347 //=========================================================================
348 // virtual
349 sal_Bool HierarchyResultSetDataSupplier::isCountFinal()
351 return m_pImpl->m_bCountFinal;
354 //=========================================================================
355 // virtual
356 uno::Reference< sdbc::XRow >
357 HierarchyResultSetDataSupplier::queryPropertyValues( sal_uInt32 nIndex )
359 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
361 if ( nIndex < m_pImpl->m_aResults.size() )
363 uno::Reference< sdbc::XRow > xRow
364 = m_pImpl->m_aResults[ nIndex ]->xRow;
365 if ( xRow.is() )
367 // Already cached.
368 return xRow;
372 if ( getResult( nIndex ) )
374 static rtl::OUString aFolderType(
375 rtl::OUString::createFromAscii( HIERARCHY_FOLDER_CONTENT_TYPE ) );
376 static rtl::OUString aLinkType(
377 rtl::OUString::createFromAscii( HIERARCHY_LINK_CONTENT_TYPE ) );
379 HierarchyContentProperties aData(
380 m_pImpl->m_aResults[ nIndex ]->aData );
382 uno::Reference< sdbc::XRow > xRow
383 = HierarchyContent::getPropertyValues(
384 m_pImpl->m_xSMgr,
385 getResultSet()->getProperties(),
386 aData,
387 static_cast< HierarchyContentProvider * >(
388 m_pImpl->m_xContent->getProvider().get() ),
389 queryContentIdentifierString( nIndex ) );
390 m_pImpl->m_aResults[ nIndex ]->xRow = xRow;
391 return xRow;
394 return uno::Reference< sdbc::XRow >();
397 //=========================================================================
398 // virtual
399 void HierarchyResultSetDataSupplier::releasePropertyValues( sal_uInt32 nIndex )
401 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
403 if ( nIndex < m_pImpl->m_aResults.size() )
404 m_pImpl->m_aResults[ nIndex ]->xRow = uno::Reference< sdbc::XRow >();
407 //=========================================================================
408 // virtual
409 void HierarchyResultSetDataSupplier::close()
413 //=========================================================================
414 // virtual
415 void HierarchyResultSetDataSupplier::validate()
416 throw( ucb::ResultSetException )
420 //=========================================================================
421 sal_Bool HierarchyResultSetDataSupplier::checkResult(
422 const HierarchyEntryData& rResult )
424 switch ( m_pImpl->m_nOpenMode )
426 case ucb::OpenMode::FOLDERS:
427 if ( rResult.getType() == HierarchyEntryData::LINK )
429 // Entry is a link.
430 return sal_False;
432 break;
434 case ucb::OpenMode::DOCUMENTS:
435 if ( rResult.getType() == HierarchyEntryData::FOLDER )
437 // Entry is a folder.
438 return sal_False;
440 break;
442 case ucb::OpenMode::ALL:
443 default:
444 break;
447 return sal_True;