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( std::unique_lock
<std::mutex
>& rResultSetGuard
, sal_uInt32 nIndex
)
65 std::unique_lock
aGuard( m_aMutex
);
66 return queryContentIdentifierStringImpl(rResultSetGuard
, aGuard
, nIndex
);
69 OUString
DataSupplier::queryContentIdentifierStringImpl( std::unique_lock
<std::mutex
>& rResultSetGuard
, 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( rResultSetGuard
, rGuard
, nIndex
) )
83 // Note: getResult fills m_aResults[ nIndex ].aURL.
84 return m_aResults
[ nIndex
].aURL
;
91 uno::Reference
< ucb::XContentIdentifier
>
92 DataSupplier::queryContentIdentifier( std::unique_lock
<std::mutex
>& rResultSetGuard
, sal_uInt32 nIndex
)
94 std::unique_lock
aGuard( m_aMutex
);
95 return queryContentIdentifierImpl(rResultSetGuard
, aGuard
, nIndex
);
98 uno::Reference
< ucb::XContentIdentifier
>
99 DataSupplier::queryContentIdentifierImpl( std::unique_lock
<std::mutex
>& rResultSetGuard
, 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( rResultSetGuard
, 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(
126 std::unique_lock
<std::mutex
>& rResultSetGuard
,
129 std::unique_lock
aGuard( m_aMutex
);
131 if ( nIndex
< m_aResults
.size() )
133 uno::Reference
< ucb::XContent
>& xContent
134 = m_aResults
[ nIndex
].xContent
;
142 uno::Reference
< ucb::XContentIdentifier
> xId
143 = queryContentIdentifierImpl( rResultSetGuard
, aGuard
, nIndex
);
148 uno::Reference
< ucb::XContent
> xContent
149 = m_xContent
->getProvider()->queryContent( xId
);
150 m_aResults
[ nIndex
].xContent
= xContent
;
154 catch ( ucb::IllegalIdentifierException
const & )
158 return uno::Reference
< ucb::XContent
>();
163 bool DataSupplier::getResult( std::unique_lock
<std::mutex
>& rResultSetGuard
, sal_uInt32 nIndex
)
165 std::unique_lock
aGuard( m_aMutex
);
166 return getResultImpl(rResultSetGuard
, aGuard
, nIndex
);
169 bool DataSupplier::getResultImpl( std::unique_lock
<std::mutex
>& rResultSetGuard
, std::unique_lock
<std::mutex
>& rGuard
, sal_uInt32 nIndex
)
171 if ( m_aResults
.size() > nIndex
)
173 // Result already present.
177 // Result not (yet) present.
182 // Try to obtain result...
184 sal_uInt32 nOldCount
= m_aResults
.size();
186 sal_uInt32 nPos
= nOldCount
;
188 while ( m_xFolderEnum
->hasMoreElements() )
192 uno::Reference
< container::XNamed
> xNamed
;
193 m_xFolderEnum
->nextElement() >>= xNamed
;
197 OSL_FAIL( "DataSupplier::getResult - Got no XNamed!" );
201 OUString aName
= xNamed
->getName();
203 if ( aName
.isEmpty() )
205 OSL_FAIL( "DataSupplier::getResult - Empty name!" );
209 // Assemble URL for child.
210 OUString aURL
= assembleChildURL( aName
);
212 m_aResults
.emplace_back(aURL
);
214 if ( nPos
== nIndex
)
223 catch ( container::NoSuchElementException
const & )
225 m_bThrowException
= true;
228 catch ( lang::WrappedTargetException
const & )
230 m_bThrowException
= true;
236 m_bCountFinal
= true;
238 rtl::Reference
< ::ucbhelper::ResultSet
> xResultSet
= getResultSet();
239 if ( xResultSet
.is() )
244 if ( nOldCount
< m_aResults
.size() )
245 xResultSet
->rowCountChanged(rResultSetGuard
,
246 nOldCount
, m_aResults
.size() );
249 xResultSet
->rowCountFinal(rResultSetGuard
);
259 sal_uInt32
DataSupplier::totalCount(std::unique_lock
<std::mutex
>& rResultSetGuard
)
261 std::unique_lock
aGuard( m_aMutex
);
264 return m_aResults
.size();
266 sal_uInt32 nOldCount
= m_aResults
.size();
268 while ( m_xFolderEnum
->hasMoreElements() )
272 uno::Reference
< container::XNamed
> xNamed
;
273 m_xFolderEnum
->nextElement() >>= xNamed
;
277 OSL_FAIL( "DataSupplier::getResult - Got no XNamed!" );
281 OUString aName
= xNamed
->getName();
283 if ( aName
.isEmpty() )
285 OSL_FAIL( "DataSupplier::getResult - Empty name!" );
289 // Assemble URL for child.
290 OUString aURL
= assembleChildURL( aName
);
292 m_aResults
.emplace_back(aURL
);
294 catch ( container::NoSuchElementException
const & )
296 m_bThrowException
= true;
299 catch ( lang::WrappedTargetException
const & )
301 m_bThrowException
= true;
306 m_bCountFinal
= true;
308 rtl::Reference
< ::ucbhelper::ResultSet
> xResultSet
= getResultSet();
309 if ( xResultSet
.is() )
314 if ( nOldCount
< m_aResults
.size() )
315 xResultSet
->rowCountChanged(rResultSetGuard
,
316 nOldCount
, m_aResults
.size() );
318 xResultSet
->rowCountFinal(rResultSetGuard
);
321 return m_aResults
.size();
326 sal_uInt32
DataSupplier::currentCount()
328 return m_aResults
.size();
333 bool DataSupplier::isCountFinal()
335 return m_bCountFinal
;
340 uno::Reference
< sdbc::XRow
> DataSupplier::queryPropertyValues(
341 std::unique_lock
<std::mutex
>& rResultSetGuard
,
344 std::unique_lock
aGuard( m_aMutex
);
346 if ( nIndex
< m_aResults
.size() )
348 uno::Reference
< sdbc::XRow
>& xRow
= m_aResults
[ nIndex
].xRow
;
356 if ( getResultImpl( rResultSetGuard
, aGuard
, nIndex
) )
358 uno::Reference
< sdbc::XRow
> xRow
= Content::getPropertyValues(
360 getResultSet()->getProperties(),
361 static_cast< ContentProvider
* >(
362 m_xContent
->getProvider().get() ),
363 queryContentIdentifierStringImpl( rResultSetGuard
, aGuard
, nIndex
) );
364 m_aResults
[ nIndex
].xRow
= xRow
;
368 return uno::Reference
< sdbc::XRow
>();
373 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex
)
375 std::unique_lock
aGuard( m_aMutex
);
377 if ( nIndex
< m_aResults
.size() )
378 m_aResults
[ nIndex
].xRow
.clear();
383 void DataSupplier::close()
389 void DataSupplier::validate()
391 if ( m_bThrowException
)
392 throw ucb::ResultSetException();
396 OUString
DataSupplier::assembleChildURL( const OUString
& aName
)
400 = m_xContent
->getIdentifier()->getContentIdentifier();
401 sal_Int32 nParam
= aContURL
.indexOf( '?' );
404 aURL
= aContURL
.copy( 0, nParam
);
406 sal_Int32 nPackageUrlEnd
= aURL
.lastIndexOf( '/' );
407 if ( nPackageUrlEnd
!= aURL
.getLength() - 1 )
410 aURL
+= ::ucb_impl::urihelper::encodeSegment( aName
) +
411 aContURL
.subView( nParam
);
417 sal_Int32 nPackageUrlEnd
= aURL
.lastIndexOf( '/' );
418 if ( nPackageUrlEnd
!= aURL
.getLength() - 1 )
421 aURL
+= ::ucb_impl::urihelper::encodeSegment( aName
);
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */