Bump version to 24.04.3.4
[LibreOffice.git] / ucb / source / ucp / package / pkgdatasupplier.cxx
blob436f7023d33751ca55c50669b4c7986cb74f6ac6
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 <osl/diagnose.h>
28 #include <com/sun/star/container/XNamed.hpp>
29 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
30 #include <com/sun/star/ucb/ResultSetException.hpp>
31 #include <ucbhelper/contentidentifier.hxx>
32 #include <ucbhelper/providerhelper.hxx>
33 #include <utility>
34 #include "pkgdatasupplier.hxx"
35 #include "pkgcontent.hxx"
36 #include "pkgprovider.hxx"
38 #include "../inc/urihelper.hxx"
40 using namespace com::sun::star;
41 using namespace package_ucp;
43 // DataSupplier Implementation.
46 DataSupplier::DataSupplier(
47 uno::Reference< uno::XComponentContext > xContext,
48 const rtl::Reference< Content >& rContent )
49 : m_xContent( rContent ), m_xContext(std::move( xContext )),
50 m_xFolderEnum( rContent->getIterator() ),
51 m_bCountFinal( !m_xFolderEnum.is() ), m_bThrowException( m_bCountFinal )
56 // virtual
57 DataSupplier::~DataSupplier()
62 // virtual
63 OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
65 std::unique_lock aGuard( m_aMutex );
66 return queryContentIdentifierStringImpl(aGuard, nIndex);
69 OUString DataSupplier::queryContentIdentifierStringImpl( std::unique_lock<std::mutex>& rGuard, sal_uInt32 nIndex )
71 if ( nIndex < m_aResults.size() )
73 OUString aId = m_aResults[ nIndex ].aURL;
74 if ( !aId.isEmpty() )
76 // Already cached.
77 return aId;
81 if ( getResultImpl( rGuard, nIndex ) )
83 // Note: getResult fills m_aResults[ nIndex ].aURL.
84 return m_aResults[ nIndex ].aURL;
86 return OUString();
90 // virtual
91 uno::Reference< ucb::XContentIdentifier >
92 DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
94 std::unique_lock aGuard( m_aMutex );
95 return queryContentIdentifierImpl(aGuard, nIndex);
98 uno::Reference< ucb::XContentIdentifier >
99 DataSupplier::queryContentIdentifierImpl( std::unique_lock<std::mutex>& rGuard, sal_uInt32 nIndex )
101 if ( nIndex < m_aResults.size() )
103 uno::Reference< ucb::XContentIdentifier >& xId
104 = m_aResults[ nIndex ].xId;
105 if ( xId.is() )
107 // Already cached.
108 return xId;
112 OUString aId = queryContentIdentifierStringImpl( rGuard, nIndex );
113 if ( !aId.isEmpty() )
115 uno::Reference< ucb::XContentIdentifier > xId
116 = new ::ucbhelper::ContentIdentifier( aId );
117 m_aResults[ nIndex ].xId = xId;
118 return xId;
120 return uno::Reference< ucb::XContentIdentifier >();
124 // virtual
125 uno::Reference< ucb::XContent > DataSupplier::queryContent(
126 sal_uInt32 nIndex )
128 std::unique_lock aGuard( m_aMutex );
130 if ( nIndex < m_aResults.size() )
132 uno::Reference< ucb::XContent >& xContent
133 = m_aResults[ nIndex ].xContent;
134 if ( xContent.is() )
136 // Already cached.
137 return xContent;
141 uno::Reference< ucb::XContentIdentifier > xId
142 = queryContentIdentifierImpl( aGuard, nIndex );
143 if ( xId.is() )
147 uno::Reference< ucb::XContent > xContent
148 = m_xContent->getProvider()->queryContent( xId );
149 m_aResults[ nIndex ].xContent = xContent;
150 return xContent;
153 catch ( ucb::IllegalIdentifierException const & )
157 return uno::Reference< ucb::XContent >();
161 // virtual
162 bool DataSupplier::getResult( sal_uInt32 nIndex )
164 std::unique_lock aGuard( m_aMutex );
165 return getResultImpl(aGuard, nIndex);
168 bool DataSupplier::getResultImpl( std::unique_lock<std::mutex>& rGuard, sal_uInt32 nIndex )
170 if ( m_aResults.size() > nIndex )
172 // Result already present.
173 return true;
176 // Result not (yet) present.
178 if ( m_bCountFinal )
179 return false;
181 // Try to obtain result...
183 sal_uInt32 nOldCount = m_aResults.size();
184 bool bFound = false;
185 sal_uInt32 nPos = nOldCount;
187 while ( m_xFolderEnum->hasMoreElements() )
191 uno::Reference< container::XNamed > xNamed;
192 m_xFolderEnum->nextElement() >>= xNamed;
194 if ( !xNamed.is() )
196 OSL_FAIL( "DataSupplier::getResult - Got no XNamed!" );
197 break;
200 OUString aName = xNamed->getName();
202 if ( aName.isEmpty() )
204 OSL_FAIL( "DataSupplier::getResult - Empty name!" );
205 break;
208 // Assemble URL for child.
209 OUString aURL = assembleChildURL( aName );
211 m_aResults.push_back( ResultListEntry( aURL ) );
213 if ( nPos == nIndex )
215 // Result obtained.
216 bFound = true;
217 break;
220 nPos++;
222 catch ( container::NoSuchElementException const & )
224 m_bThrowException = true;
225 break;
227 catch ( lang::WrappedTargetException const & )
229 m_bThrowException = true;
230 break;
234 if ( !bFound )
235 m_bCountFinal = true;
237 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet();
238 if ( xResultSet.is() )
240 // Callbacks follow!
241 rGuard.unlock();
243 if ( nOldCount < m_aResults.size() )
244 xResultSet->rowCountChanged(
245 nOldCount, m_aResults.size() );
247 if ( m_bCountFinal )
248 xResultSet->rowCountFinal();
250 rGuard.lock();
253 return bFound;
257 // virtual
258 sal_uInt32 DataSupplier::totalCount()
260 std::unique_lock aGuard( m_aMutex );
262 if ( m_bCountFinal )
263 return m_aResults.size();
265 sal_uInt32 nOldCount = m_aResults.size();
267 while ( m_xFolderEnum->hasMoreElements() )
271 uno::Reference< container::XNamed > xNamed;
272 m_xFolderEnum->nextElement() >>= xNamed;
274 if ( !xNamed.is() )
276 OSL_FAIL( "DataSupplier::getResult - Got no XNamed!" );
277 break;
280 OUString aName = xNamed->getName();
282 if ( aName.isEmpty() )
284 OSL_FAIL( "DataSupplier::getResult - Empty name!" );
285 break;
288 // Assemble URL for child.
289 OUString aURL = assembleChildURL( aName );
291 m_aResults.push_back( ResultListEntry( aURL ) );
293 catch ( container::NoSuchElementException const & )
295 m_bThrowException = true;
296 break;
298 catch ( lang::WrappedTargetException const & )
300 m_bThrowException = true;
301 break;
305 m_bCountFinal = true;
307 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet();
308 if ( xResultSet.is() )
310 // Callbacks follow!
311 aGuard.unlock();
313 if ( nOldCount < m_aResults.size() )
314 xResultSet->rowCountChanged(
315 nOldCount, m_aResults.size() );
317 xResultSet->rowCountFinal();
320 return m_aResults.size();
324 // virtual
325 sal_uInt32 DataSupplier::currentCount()
327 return m_aResults.size();
331 // virtual
332 bool DataSupplier::isCountFinal()
334 return m_bCountFinal;
338 // virtual
339 uno::Reference< sdbc::XRow > DataSupplier::queryPropertyValues(
340 sal_uInt32 nIndex )
342 std::unique_lock aGuard( m_aMutex );
344 if ( nIndex < m_aResults.size() )
346 uno::Reference< sdbc::XRow >& xRow = m_aResults[ nIndex ].xRow;
347 if ( xRow.is() )
349 // Already cached.
350 return xRow;
354 if ( getResultImpl( aGuard, nIndex ) )
356 uno::Reference< sdbc::XRow > xRow = Content::getPropertyValues(
357 m_xContext,
358 getResultSet()->getProperties(),
359 static_cast< ContentProvider * >(
360 m_xContent->getProvider().get() ),
361 queryContentIdentifierStringImpl( aGuard, nIndex ) );
362 m_aResults[ nIndex ].xRow = xRow;
363 return xRow;
366 return uno::Reference< sdbc::XRow >();
370 // virtual
371 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
373 std::unique_lock aGuard( m_aMutex );
375 if ( nIndex < m_aResults.size() )
376 m_aResults[ nIndex ].xRow.clear();
380 // virtual
381 void DataSupplier::close()
386 // virtual
387 void DataSupplier::validate()
389 if ( m_bThrowException )
390 throw ucb::ResultSetException();
394 OUString DataSupplier::assembleChildURL( const OUString& aName )
396 OUString aURL;
397 OUString aContURL
398 = m_xContent->getIdentifier()->getContentIdentifier();
399 sal_Int32 nParam = aContURL.indexOf( '?' );
400 if ( nParam >= 0 )
402 aURL = aContURL.copy( 0, nParam );
404 sal_Int32 nPackageUrlEnd = aURL.lastIndexOf( '/' );
405 if ( nPackageUrlEnd != aURL.getLength() - 1 )
406 aURL += "/";
408 aURL += ::ucb_impl::urihelper::encodeSegment( aName ) +
409 aContURL.subView( nParam );
411 else
413 aURL = aContURL;
415 sal_Int32 nPackageUrlEnd = aURL.lastIndexOf( '/' );
416 if ( nPackageUrlEnd != aURL.getLength() - 1 )
417 aURL += "/";
419 aURL += ::ucb_impl::urihelper::encodeSegment( aName );
421 return aURL;
425 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */