1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 /**************************************************************************
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>
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
)
57 DataSupplier::~DataSupplier()
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
;
81 if ( getResultImpl( rGuard
, nIndex
) )
83 // Note: getResult fills m_aResults[ nIndex ].aURL.
84 return m_aResults
[ nIndex
].aURL
;
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
;
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
;
120 return uno::Reference
< ucb::XContentIdentifier
>();
125 uno::Reference
< ucb::XContent
> DataSupplier::queryContent(
128 std::unique_lock
aGuard( m_aMutex
);
130 if ( nIndex
< m_aResults
.size() )
132 uno::Reference
< ucb::XContent
>& xContent
133 = m_aResults
[ nIndex
].xContent
;
141 uno::Reference
< ucb::XContentIdentifier
> xId
142 = queryContentIdentifierImpl( aGuard
, nIndex
);
147 uno::Reference
< ucb::XContent
> xContent
148 = m_xContent
->getProvider()->queryContent( xId
);
149 m_aResults
[ nIndex
].xContent
= xContent
;
153 catch ( ucb::IllegalIdentifierException
const & )
157 return uno::Reference
< ucb::XContent
>();
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.
176 // Result not (yet) present.
181 // Try to obtain result...
183 sal_uInt32 nOldCount
= m_aResults
.size();
185 sal_uInt32 nPos
= nOldCount
;
187 while ( m_xFolderEnum
->hasMoreElements() )
191 uno::Reference
< container::XNamed
> xNamed
;
192 m_xFolderEnum
->nextElement() >>= xNamed
;
196 OSL_FAIL( "DataSupplier::getResult - Got no XNamed!" );
200 OUString aName
= xNamed
->getName();
202 if ( aName
.isEmpty() )
204 OSL_FAIL( "DataSupplier::getResult - Empty name!" );
208 // Assemble URL for child.
209 OUString aURL
= assembleChildURL( aName
);
211 m_aResults
.push_back( ResultListEntry( aURL
) );
213 if ( nPos
== nIndex
)
222 catch ( container::NoSuchElementException
const & )
224 m_bThrowException
= true;
227 catch ( lang::WrappedTargetException
const & )
229 m_bThrowException
= true;
235 m_bCountFinal
= true;
237 rtl::Reference
< ::ucbhelper::ResultSet
> xResultSet
= getResultSet();
238 if ( xResultSet
.is() )
243 if ( nOldCount
< m_aResults
.size() )
244 xResultSet
->rowCountChanged(
245 nOldCount
, m_aResults
.size() );
248 xResultSet
->rowCountFinal();
258 sal_uInt32
DataSupplier::totalCount()
260 std::unique_lock
aGuard( m_aMutex
);
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
;
276 OSL_FAIL( "DataSupplier::getResult - Got no XNamed!" );
280 OUString aName
= xNamed
->getName();
282 if ( aName
.isEmpty() )
284 OSL_FAIL( "DataSupplier::getResult - Empty name!" );
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;
298 catch ( lang::WrappedTargetException
const & )
300 m_bThrowException
= true;
305 m_bCountFinal
= true;
307 rtl::Reference
< ::ucbhelper::ResultSet
> xResultSet
= getResultSet();
308 if ( xResultSet
.is() )
313 if ( nOldCount
< m_aResults
.size() )
314 xResultSet
->rowCountChanged(
315 nOldCount
, m_aResults
.size() );
317 xResultSet
->rowCountFinal();
320 return m_aResults
.size();
325 sal_uInt32
DataSupplier::currentCount()
327 return m_aResults
.size();
332 bool DataSupplier::isCountFinal()
334 return m_bCountFinal
;
339 uno::Reference
< sdbc::XRow
> DataSupplier::queryPropertyValues(
342 std::unique_lock
aGuard( m_aMutex
);
344 if ( nIndex
< m_aResults
.size() )
346 uno::Reference
< sdbc::XRow
>& xRow
= m_aResults
[ nIndex
].xRow
;
354 if ( getResultImpl( aGuard
, nIndex
) )
356 uno::Reference
< sdbc::XRow
> xRow
= Content::getPropertyValues(
358 getResultSet()->getProperties(),
359 static_cast< ContentProvider
* >(
360 m_xContent
->getProvider().get() ),
361 queryContentIdentifierStringImpl( aGuard
, nIndex
) );
362 m_aResults
[ nIndex
].xRow
= xRow
;
366 return uno::Reference
< sdbc::XRow
>();
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();
381 void DataSupplier::close()
387 void DataSupplier::validate()
389 if ( m_bThrowException
)
390 throw ucb::ResultSetException();
394 OUString
DataSupplier::assembleChildURL( const OUString
& aName
)
398 = m_xContent
->getIdentifier()->getContentIdentifier();
399 sal_Int32 nParam
= aContURL
.indexOf( '?' );
402 aURL
= aContURL
.copy( 0, nParam
);
404 sal_Int32 nPackageUrlEnd
= aURL
.lastIndexOf( '/' );
405 if ( nPackageUrlEnd
!= aURL
.getLength() - 1 )
408 aURL
+= ::ucb_impl::urihelper::encodeSegment( aName
) +
409 aContURL
.subView( nParam
);
415 sal_Int32 nPackageUrlEnd
= aURL
.lastIndexOf( '/' );
416 if ( nPackageUrlEnd
!= aURL
.getLength() - 1 )
419 aURL
+= ::ucb_impl::urihelper::encodeSegment( aName
);
425 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */