bump product version to 7.2.5.1
[LibreOffice.git] / ucb / source / ucp / hierarchy / hierarchydatasupplier.cxx
blob9a4cae13d2b31ef5903e1673402c354ae168c95a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 /**************************************************************************
22 TODO
23 **************************************************************************
25 *************************************************************************/
27 #include <vector>
29 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
30 #include <com/sun/star/ucb/OpenMode.hpp>
31 #include <ucbhelper/contentidentifier.hxx>
32 #include "hierarchydatasupplier.hxx"
33 #include "hierarchyprovider.hxx"
34 #include "hierarchycontent.hxx"
36 using namespace com::sun::star;
37 using namespace hierarchy_ucp;
39 namespace hierarchy_ucp
43 // struct ResultListEntry.
45 namespace {
47 struct ResultListEntry
49 OUString aId;
50 uno::Reference< ucb::XContentIdentifier > xId;
51 uno::Reference< ucb::XContent > xContent;
52 uno::Reference< sdbc::XRow > xRow;
53 HierarchyEntryData aData;
55 explicit ResultListEntry( const HierarchyEntryData& rEntry ) : aData( rEntry ) {}
60 // ResultList.
63 typedef std::vector< std::unique_ptr<ResultListEntry> > ResultList;
66 // struct DataSupplier_Impl.
69 struct DataSupplier_Impl
71 osl::Mutex m_aMutex;
72 ResultList m_aResults;
73 rtl::Reference< HierarchyContent > m_xContent;
74 uno::Reference< uno::XComponentContext > m_xContext;
75 HierarchyEntry m_aFolder;
76 HierarchyEntry::iterator m_aIterator;
77 sal_Int32 m_nOpenMode;
78 bool m_bCountFinal;
80 DataSupplier_Impl(
81 const uno::Reference< uno::XComponentContext >& rxContext,
82 const rtl::Reference< HierarchyContent >& rContent,
83 sal_Int32 nOpenMode )
84 : m_xContent( rContent ), m_xContext( rxContext ),
85 m_aFolder( rxContext,
86 static_cast< HierarchyContentProvider * >(
87 rContent->getProvider().get() ),
88 rContent->getIdentifier()->getContentIdentifier() ),
89 m_nOpenMode( nOpenMode ), m_bCountFinal( false ) {}
96 // HierarchyResultSetDataSupplier Implementation.
99 HierarchyResultSetDataSupplier::HierarchyResultSetDataSupplier(
100 const uno::Reference< uno::XComponentContext >& rxContext,
101 const rtl::Reference< HierarchyContent >& rContent,
102 sal_Int32 nOpenMode )
103 : m_pImpl( new DataSupplier_Impl( rxContext, rContent, nOpenMode ) )
108 // virtual
109 HierarchyResultSetDataSupplier::~HierarchyResultSetDataSupplier()
114 // virtual
115 OUString HierarchyResultSetDataSupplier::queryContentIdentifierString(
116 sal_uInt32 nIndex )
118 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
120 if ( nIndex < m_pImpl->m_aResults.size() )
122 OUString aId = m_pImpl->m_aResults[ nIndex ]->aId;
123 if ( !aId.isEmpty() )
125 // Already cached.
126 return aId;
130 if ( getResult( nIndex ) )
132 OUString aId
133 = m_pImpl->m_xContent->getIdentifier()->getContentIdentifier();
135 if ( ( aId.lastIndexOf( '/' ) + 1 ) != aId.getLength() )
136 aId += "/";
138 aId += m_pImpl->m_aResults[ nIndex ]->aData.getName();
140 m_pImpl->m_aResults[ nIndex ]->aId = aId;
141 return aId;
143 return OUString();
147 // virtual
148 uno::Reference< ucb::XContentIdentifier >
149 HierarchyResultSetDataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
151 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
153 if ( nIndex < m_pImpl->m_aResults.size() )
155 uno::Reference< ucb::XContentIdentifier > xId
156 = m_pImpl->m_aResults[ nIndex ]->xId;
157 if ( xId.is() )
159 // Already cached.
160 return xId;
164 OUString aId = queryContentIdentifierString( nIndex );
165 if ( !aId.isEmpty() )
167 uno::Reference< ucb::XContentIdentifier > xId
168 = new ::ucbhelper::ContentIdentifier( aId );
169 m_pImpl->m_aResults[ nIndex ]->xId = xId;
170 return xId;
172 return uno::Reference< ucb::XContentIdentifier >();
176 // virtual
177 uno::Reference< ucb::XContent >
178 HierarchyResultSetDataSupplier::queryContent( sal_uInt32 nIndex )
180 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
182 if ( nIndex < m_pImpl->m_aResults.size() )
184 uno::Reference< ucb::XContent > xContent
185 = m_pImpl->m_aResults[ nIndex ]->xContent;
186 if ( xContent.is() )
188 // Already cached.
189 return xContent;
193 uno::Reference< ucb::XContentIdentifier > xId
194 = queryContentIdentifier( nIndex );
195 if ( xId.is() )
199 uno::Reference< ucb::XContent > xContent
200 = m_pImpl->m_xContent->getProvider()->queryContent( xId );
201 m_pImpl->m_aResults[ nIndex ]->xContent = xContent;
202 return xContent;
205 catch ( ucb::IllegalIdentifierException const & )
209 return uno::Reference< ucb::XContent >();
213 // virtual
214 bool HierarchyResultSetDataSupplier::getResult( sal_uInt32 nIndex )
216 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
218 if ( m_pImpl->m_aResults.size() > nIndex )
220 // Result already present.
221 return true;
224 // Result not (yet) present.
226 if ( m_pImpl->m_bCountFinal )
227 return false;
229 // Try to obtain result...
231 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
232 bool bFound = false;
233 sal_uInt32 nPos = nOldCount;
235 while ( m_pImpl->m_aFolder.next( m_pImpl->m_aIterator ) )
237 const HierarchyEntryData& rResult = *m_pImpl->m_aIterator;
238 if ( checkResult( rResult ) )
240 m_pImpl->m_aResults.emplace_back( new ResultListEntry( rResult ) );
242 if ( nPos == nIndex )
244 // Result obtained.
245 bFound = true;
246 break;
249 nPos++;
252 if ( !bFound )
253 m_pImpl->m_bCountFinal = true;
255 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet();
256 if ( xResultSet.is() )
258 // Callbacks follow!
259 aGuard.clear();
261 if ( nOldCount < m_pImpl->m_aResults.size() )
262 xResultSet->rowCountChanged(
263 nOldCount, m_pImpl->m_aResults.size() );
265 if ( m_pImpl->m_bCountFinal )
266 xResultSet->rowCountFinal();
269 return bFound;
273 // virtual
274 sal_uInt32 HierarchyResultSetDataSupplier::totalCount()
276 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
278 if ( m_pImpl->m_bCountFinal )
279 return m_pImpl->m_aResults.size();
281 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
283 while ( m_pImpl->m_aFolder.next( m_pImpl->m_aIterator ) )
285 const HierarchyEntryData& rResult = *m_pImpl->m_aIterator;
286 if ( checkResult( rResult ) )
287 m_pImpl->m_aResults.emplace_back( new ResultListEntry( rResult ) );
290 m_pImpl->m_bCountFinal = true;
292 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet();
293 if ( xResultSet.is() )
295 // Callbacks follow!
296 aGuard.clear();
298 if ( nOldCount < m_pImpl->m_aResults.size() )
299 xResultSet->rowCountChanged(
300 nOldCount, m_pImpl->m_aResults.size() );
302 xResultSet->rowCountFinal();
305 return m_pImpl->m_aResults.size();
309 // virtual
310 sal_uInt32 HierarchyResultSetDataSupplier::currentCount()
312 return m_pImpl->m_aResults.size();
316 // virtual
317 bool HierarchyResultSetDataSupplier::isCountFinal()
319 return m_pImpl->m_bCountFinal;
323 // virtual
324 uno::Reference< sdbc::XRow >
325 HierarchyResultSetDataSupplier::queryPropertyValues( sal_uInt32 nIndex )
327 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
329 if ( nIndex < m_pImpl->m_aResults.size() )
331 uno::Reference< sdbc::XRow > xRow
332 = m_pImpl->m_aResults[ nIndex ]->xRow;
333 if ( xRow.is() )
335 // Already cached.
336 return xRow;
340 if ( getResult( nIndex ) )
342 HierarchyContentProperties aData(
343 m_pImpl->m_aResults[ nIndex ]->aData );
345 uno::Reference< sdbc::XRow > xRow
346 = HierarchyContent::getPropertyValues(
347 m_pImpl->m_xContext,
348 getResultSet()->getProperties(),
349 aData,
350 static_cast< HierarchyContentProvider * >(
351 m_pImpl->m_xContent->getProvider().get() ),
352 queryContentIdentifierString( nIndex ) );
353 m_pImpl->m_aResults[ nIndex ]->xRow = xRow;
354 return xRow;
357 return uno::Reference< sdbc::XRow >();
361 // virtual
362 void HierarchyResultSetDataSupplier::releasePropertyValues( sal_uInt32 nIndex )
364 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
366 if ( nIndex < m_pImpl->m_aResults.size() )
367 m_pImpl->m_aResults[ nIndex ]->xRow.clear();
371 // virtual
372 void HierarchyResultSetDataSupplier::close()
377 // virtual
378 void HierarchyResultSetDataSupplier::validate()
383 bool HierarchyResultSetDataSupplier::checkResult(
384 const HierarchyEntryData& rResult )
386 switch ( m_pImpl->m_nOpenMode )
388 case ucb::OpenMode::FOLDERS:
389 if ( rResult.getType() == HierarchyEntryData::LINK )
391 // Entry is a link.
392 return false;
394 break;
396 case ucb::OpenMode::DOCUMENTS:
397 if ( rResult.getType() == HierarchyEntryData::FOLDER )
399 // Entry is a folder.
400 return false;
402 break;
404 case ucb::OpenMode::ALL:
405 default:
406 break;
409 return true;
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */