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 *************************************************************************/
29 #include "osl/diagnose.h"
30 #include "ucbhelper/contentidentifier.hxx"
32 #include "tdoc_datasupplier.hxx"
33 #include "tdoc_content.hxx"
35 using namespace com::sun::star
;
36 using namespace tdoc_ucp
;
43 // struct ResultListEntry.
47 struct ResultListEntry
50 uno::Reference
< ucb::XContentIdentifier
> xId
;
51 uno::Reference
< ucb::XContent
> xContent
;
52 uno::Reference
< sdbc::XRow
> xRow
;
54 ResultListEntry( const OUString
& rURL
) : aURL( rURL
) {}
63 typedef std::vector
< ResultListEntry
* > ResultList
;
67 // struct DataSupplier_Impl.
71 struct DataSupplier_Impl
74 ResultList m_aResults
;
75 rtl::Reference
< Content
> m_xContent
;
76 uno::Reference
< uno::XComponentContext
> m_xContext
;
77 uno::Sequence
< OUString
> * m_pNamesOfChildren
;
78 sal_Int32 m_nOpenMode
;
80 bool m_bThrowException
;
83 const uno::Reference
< uno::XComponentContext
>& rxContext
,
84 const rtl::Reference
< Content
>& rContent
,
86 : m_xContent( rContent
), m_xContext( rxContext
),
87 m_pNamesOfChildren( 0 ), m_nOpenMode( nOpenMode
),
88 m_bCountFinal( false ), m_bThrowException( false )
94 DataSupplier_Impl::~DataSupplier_Impl()
96 ResultList::const_iterator it
= m_aResults
.begin();
97 ResultList::const_iterator end
= m_aResults
.end();
105 delete m_pNamesOfChildren
;
110 // DataSupplier Implementation.
111 ResultSetDataSupplier::ResultSetDataSupplier(
112 const uno::Reference
< uno::XComponentContext
>& rxContext
,
113 const rtl::Reference
< Content
>& rContent
,
114 sal_Int32 nOpenMode
)
115 : m_pImpl( new DataSupplier_Impl( rxContext
, rContent
, nOpenMode
) )
120 ResultSetDataSupplier::~ResultSetDataSupplier()
127 ResultSetDataSupplier::queryContentIdentifierString( sal_uInt32 nIndex
)
129 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
131 if ( nIndex
< m_pImpl
->m_aResults
.size() )
133 OUString aId
= m_pImpl
->m_aResults
[ nIndex
]->aURL
;
134 if ( !aId
.isEmpty() )
141 if ( getResult( nIndex
) )
143 // Note: getResult fills m_pImpl->m_aResults[ nIndex ]->aURL.
144 return m_pImpl
->m_aResults
[ nIndex
]->aURL
;
150 uno::Reference
< ucb::XContentIdentifier
>
151 ResultSetDataSupplier::queryContentIdentifier( sal_uInt32 nIndex
)
153 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
155 if ( nIndex
< m_pImpl
->m_aResults
.size() )
157 uno::Reference
< ucb::XContentIdentifier
> xId
158 = m_pImpl
->m_aResults
[ nIndex
]->xId
;
166 OUString aId
= queryContentIdentifierString( nIndex
);
167 if ( !aId
.isEmpty() )
169 uno::Reference
< ucb::XContentIdentifier
> xId
170 = new ::ucbhelper::ContentIdentifier( aId
);
171 m_pImpl
->m_aResults
[ nIndex
]->xId
= xId
;
174 return uno::Reference
< ucb::XContentIdentifier
>();
178 uno::Reference
< ucb::XContent
>
179 ResultSetDataSupplier::queryContent( sal_uInt32 nIndex
)
181 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
183 if ( nIndex
< m_pImpl
->m_aResults
.size() )
185 uno::Reference
< ucb::XContent
> xContent
186 = m_pImpl
->m_aResults
[ nIndex
]->xContent
;
194 uno::Reference
< ucb::XContentIdentifier
> xId
195 = queryContentIdentifier( nIndex
);
200 uno::Reference
< ucb::XContent
> xContent
201 = m_pImpl
->m_xContent
->getProvider()->queryContent( xId
);
202 m_pImpl
->m_aResults
[ nIndex
]->xContent
= xContent
;
206 catch ( ucb::IllegalIdentifierException
const & )
210 return uno::Reference
< ucb::XContent
>();
214 bool ResultSetDataSupplier::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.
224 // Result not (yet) present.
226 if ( m_pImpl
->m_bCountFinal
)
229 // Try to obtain result...
231 sal_uInt32 nOldCount
= m_pImpl
->m_aResults
.size();
234 if ( queryNamesOfChildren() )
236 for ( sal_uInt32 n
= nOldCount
;
237 n
< sal::static_int_cast
<sal_uInt32
>(
238 m_pImpl
->m_pNamesOfChildren
->getLength());
241 const OUString
& rName
242 = m_pImpl
->m_pNamesOfChildren
->getConstArray()[ n
];
244 if ( rName
.isEmpty() )
246 OSL_FAIL( "ResultDataSupplier::getResult - Empty name!" );
250 // Assemble URL for child.
251 OUString aURL
= assembleChildURL( rName
);
253 m_pImpl
->m_aResults
.push_back( new ResultListEntry( aURL
) );
265 m_pImpl
->m_bCountFinal
= true;
267 rtl::Reference
< ::ucbhelper::ResultSet
> xResultSet
= getResultSet().get();
268 if ( xResultSet
.is() )
273 if ( nOldCount
< m_pImpl
->m_aResults
.size() )
274 xResultSet
->rowCountChanged( nOldCount
, m_pImpl
->m_aResults
.size() );
276 if ( m_pImpl
->m_bCountFinal
)
277 xResultSet
->rowCountFinal();
284 sal_uInt32
ResultSetDataSupplier::totalCount()
286 osl::ClearableGuard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
288 if ( m_pImpl
->m_bCountFinal
)
289 return m_pImpl
->m_aResults
.size();
291 sal_uInt32 nOldCount
= m_pImpl
->m_aResults
.size();
293 if ( queryNamesOfChildren() )
295 for ( sal_uInt32 n
= nOldCount
;
296 n
< sal::static_int_cast
<sal_uInt32
>(
297 m_pImpl
->m_pNamesOfChildren
->getLength());
300 const OUString
& rName
301 = m_pImpl
->m_pNamesOfChildren
->getConstArray()[ n
];
303 if ( rName
.isEmpty() )
305 OSL_FAIL( "ResultDataSupplier::getResult - Empty name!" );
309 // Assemble URL for child.
310 OUString aURL
= assembleChildURL( rName
);
312 m_pImpl
->m_aResults
.push_back( new ResultListEntry( aURL
) );
316 m_pImpl
->m_bCountFinal
= true;
318 rtl::Reference
< ::ucbhelper::ResultSet
> xResultSet
= getResultSet().get();
319 if ( xResultSet
.is() )
324 if ( nOldCount
< m_pImpl
->m_aResults
.size() )
325 xResultSet
->rowCountChanged( nOldCount
, m_pImpl
->m_aResults
.size() );
327 xResultSet
->rowCountFinal();
330 return m_pImpl
->m_aResults
.size();
334 sal_uInt32
ResultSetDataSupplier::currentCount()
336 return m_pImpl
->m_aResults
.size();
340 bool ResultSetDataSupplier::isCountFinal()
342 return m_pImpl
->m_bCountFinal
;
346 uno::Reference
< sdbc::XRow
>
347 ResultSetDataSupplier::queryPropertyValues( sal_uInt32 nIndex
)
349 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
351 if ( nIndex
< m_pImpl
->m_aResults
.size() )
353 uno::Reference
< sdbc::XRow
> xRow
= m_pImpl
->m_aResults
[ nIndex
]->xRow
;
361 if ( getResult( nIndex
) )
363 uno::Reference
< sdbc::XRow
> xRow
= Content::getPropertyValues(
365 getResultSet()->getProperties(),
366 m_pImpl
->m_xContent
->getContentProvider().get(),
367 queryContentIdentifierString( nIndex
) );
368 m_pImpl
->m_aResults
[ nIndex
]->xRow
= xRow
;
372 return uno::Reference
< sdbc::XRow
>();
376 void ResultSetDataSupplier::releasePropertyValues( sal_uInt32 nIndex
)
378 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
380 if ( nIndex
< m_pImpl
->m_aResults
.size() )
381 m_pImpl
->m_aResults
[ nIndex
]->xRow
= uno::Reference
< sdbc::XRow
>();
385 void ResultSetDataSupplier::close()
390 void ResultSetDataSupplier::validate()
391 throw( ucb::ResultSetException
)
393 if ( m_pImpl
->m_bThrowException
)
394 throw ucb::ResultSetException();
397 bool ResultSetDataSupplier::queryNamesOfChildren()
399 osl::Guard
< osl::Mutex
> aGuard( m_pImpl
->m_aMutex
);
401 if ( m_pImpl
->m_pNamesOfChildren
== 0 )
403 uno::Sequence
< OUString
> * pNamesOfChildren
404 = new uno::Sequence
< OUString
>();
406 if ( !m_pImpl
->m_xContent
->getContentProvider()->queryNamesOfChildren(
407 m_pImpl
->m_xContent
->getIdentifier()->getContentIdentifier(),
408 *pNamesOfChildren
) )
410 OSL_FAIL( "Got no list of children!" );
411 delete pNamesOfChildren
;
412 m_pImpl
->m_bThrowException
= true;
417 m_pImpl
->m_pNamesOfChildren
= pNamesOfChildren
;
424 ResultSetDataSupplier::assembleChildURL( const OUString
& aName
)
427 = m_pImpl
->m_xContent
->getIdentifier()->getContentIdentifier();
428 OUString
aURL( aContURL
);
430 sal_Int32 nUrlEnd
= aURL
.lastIndexOf( '/' );
431 if ( nUrlEnd
!= aURL
.getLength() - 1 )
438 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */