| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
/************************************************************************** |
| 22 |
TODO |
| 23 |
************************************************************************** |
| 24 |
|
| 25 |
*************************************************************************/ |
| 26 |
|
| 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" |
| 37 |
|
| 38 |
#include "../inc/urihelper.hxx" |
| 39 |
|
| 40 |
using namespace com::sun::star; |
| 41 |
using namespace package_ucp; |
| 42 |
|
| 43 |
// DataSupplier Implementation. |
| 44 |
|
| 45 |
|
| 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 ) |
| 52 |
{ |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
// virtual |
| 57 |
DataSupplier::~DataSupplier() |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
// virtual |
| 63 |
OUString DataSupplier::queryContentIdentifierString( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex ) |
| 64 |
{ |
| 65 |
std::unique_lock aGuard( m_aMutex ); |
| 66 |
return queryContentIdentifierStringImpl(rResultSetGuard, aGuard, nIndex); |
| 67 |
} |
| 68 |
|
| 69 |
OUString DataSupplier::queryContentIdentifierStringImpl( std::unique_lock<std::mutex>& rResultSetGuard, std::unique_lock<std::mutex>& rGuard, sal_uInt32 nIndex ) |
| 70 |
{ |
| 71 |
if ( nIndex < m_aResults.size() ) |
| 72 |
{ |
| 73 |
OUString aId = m_aResults[ nIndex ].aURL; |
| 74 |
if ( !aId.isEmpty() ) |
| 75 |
{ |
| 76 |
// Already cached. |
| 77 |
return aId; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if ( getResultImpl( rResultSetGuard, rGuard, nIndex ) ) |
| 82 |
{ |
| 83 |
// Note: getResult fills m_aResults[ nIndex ].aURL. |
| 84 |
return m_aResults[ nIndex ].aURL; |
| 85 |
} |
| 86 |
return OUString(); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
// virtual |
| 91 |
uno::Reference< ucb::XContentIdentifier > |
| 92 |
DataSupplier::queryContentIdentifier( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex ) |
| 93 |
{ |
| 94 |
std::unique_lock aGuard( m_aMutex ); |
| 95 |
return queryContentIdentifierImpl(rResultSetGuard, aGuard, nIndex); |
| 96 |
} |
| 97 |
|
| 98 |
uno::Reference< ucb::XContentIdentifier > |
| 99 |
DataSupplier::queryContentIdentifierImpl( std::unique_lock<std::mutex>& rResultSetGuard, std::unique_lock<std::mutex>& rGuard, sal_uInt32 nIndex ) |
| 100 |
{ |
| 101 |
if ( nIndex < m_aResults.size() ) |
| 102 |
{ |
| 103 |
uno::Reference< ucb::XContentIdentifier >& xId |
| 104 |
= m_aResults[ nIndex ].xId; |
| 105 |
if ( xId.is() ) |
| 106 |
{ |
| 107 |
// Already cached. |
| 108 |
return xId; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
OUString aId = queryContentIdentifierStringImpl( rResultSetGuard, rGuard, nIndex ); |
| 113 |
if ( !aId.isEmpty() ) |
| 114 |
{ |
| 115 |
uno::Reference< ucb::XContentIdentifier > xId |
| 116 |
= new ::ucbhelper::ContentIdentifier( aId ); |
| 117 |
m_aResults[ nIndex ].xId = xId; |
| 118 |
return xId; |
| 119 |
} |
| 120 |
return uno::Reference< ucb::XContentIdentifier >(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
// virtual |
| 125 |
uno::Reference< ucb::XContent > DataSupplier::queryContent( |
| 126 |
std::unique_lock<std::mutex>& rResultSetGuard, |
| 127 |
sal_uInt32 nIndex ) |
| 128 |
{ |
| 129 |
std::unique_lock aGuard( m_aMutex ); |
| 130 |
|
| 131 |
if ( nIndex < m_aResults.size() ) |
| 132 |
{ |
| 133 |
uno::Reference< ucb::XContent >& xContent |
| 134 |
= m_aResults[ nIndex ].xContent; |
| 135 |
if ( xContent.is() ) |
| 136 |
{ |
| 137 |
// Already cached. |
| 138 |
return xContent; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
uno::Reference< ucb::XContentIdentifier > xId |
| 143 |
= queryContentIdentifierImpl( rResultSetGuard, aGuard, nIndex ); |
| 144 |
if ( xId.is() ) |
| 145 |
{ |
| 146 |
try |
| 147 |
{ |
| 148 |
uno::Reference< ucb::XContent > xContent |
| 149 |
= m_xContent->getProvider()->queryContent( xId ); |
| 150 |
m_aResults[ nIndex ].xContent = xContent; |
| 151 |
return xContent; |
| 152 |
|
| 153 |
} |
| 154 |
catch ( ucb::IllegalIdentifierException const & ) |
| 155 |
{ |
| 156 |
} |
| 157 |
} |
| 158 |
return uno::Reference< ucb::XContent >(); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
// virtual |
| 163 |
bool DataSupplier::getResult( std::unique_lock<std::mutex>& rResultSetGuard, sal_uInt32 nIndex ) |
| 164 |
{ |
| 165 |
std::unique_lock aGuard( m_aMutex ); |
| 166 |
return getResultImpl(rResultSetGuard, aGuard, nIndex); |
| 167 |
} |
| 168 |
|
| 169 |
bool DataSupplier::getResultImpl( std::unique_lock<std::mutex>& rResultSetGuard, std::unique_lock<std::mutex>& rGuard, sal_uInt32 nIndex ) |
| 170 |
{ |
| 171 |
if ( m_aResults.size() > nIndex ) |
| 172 |
{ |
| 173 |
// Result already present. |
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
// Result not (yet) present. |
| 178 |
|
| 179 |
if ( m_bCountFinal ) |
| 180 |
return false; |
| 181 |
|
| 182 |
// Try to obtain result... |
| 183 |
|
| 184 |
sal_uInt32 nOldCount = m_aResults.size(); |
| 185 |
bool bFound = false; |
| 186 |
sal_uInt32 nPos = nOldCount; |
| 187 |
|
| 188 |
while ( m_xFolderEnum->hasMoreElements() ) |
| 189 |
{ |
| 190 |
try |
| 191 |
{ |
| 192 |
uno::Reference< container::XNamed > xNamed; |
| 193 |
m_xFolderEnum->nextElement() >>= xNamed; |
| 194 |
|
| 195 |
if ( !xNamed.is() ) |
| 196 |
{ |
| 197 |
OSL_FAIL( "DataSupplier::getResult - Got no XNamed!" ); |
| 198 |
break; |
| 199 |
} |
| 200 |
|
| 201 |
OUString aName = xNamed->getName(); |
| 202 |
|
| 203 |
if ( aName.isEmpty() ) |
| 204 |
{ |
| 205 |
OSL_FAIL( "DataSupplier::getResult - Empty name!" ); |
| 206 |
break; |
| 207 |
} |
| 208 |
|
| 209 |
// Assemble URL for child. |
| 210 |
OUString aURL = assembleChildURL( aName ); |
| 211 |
|
| 212 |
m_aResults.emplace_back(aURL); |
| 213 |
|
| 214 |
if ( nPos == nIndex ) |
| 215 |
{ |
| 216 |
// Result obtained. |
| 217 |
bFound = true; |
| 218 |
break; |
| 219 |
} |
| 220 |
|
| 221 |
nPos++; |
| 222 |
} |
| 223 |
catch ( container::NoSuchElementException const & ) |
| 224 |
{ |
| 225 |
m_bThrowException = true; |
| 226 |
break; |
| 227 |
} |
| 228 |
catch ( lang::WrappedTargetException const & ) |
| 229 |
{ |
| 230 |
m_bThrowException = true; |
| 231 |
break; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
if ( !bFound ) |
| 236 |
m_bCountFinal = true; |
| 237 |
|
| 238 |
rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet(); |
| 239 |
if ( xResultSet.is() ) |
| 240 |
{ |
| 241 |
// Callbacks follow! |
| 242 |
rGuard.unlock(); |
| 243 |
|
| 244 |
if ( nOldCount < m_aResults.size() ) |
| 245 |
xResultSet->rowCountChanged(rResultSetGuard, |
| 246 |
nOldCount, m_aResults.size() ); |
| 247 |
|
| 248 |
if ( m_bCountFinal ) |
| 249 |
xResultSet->rowCountFinal(rResultSetGuard); |
| 250 |
|
| 251 |
rGuard.lock(); |
| 252 |
} |
| 253 |
|
| 254 |
return bFound; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
// virtual |
| 259 |
sal_uInt32 DataSupplier::totalCount(std::unique_lock<std::mutex>& rResultSetGuard) |
| 260 |
{ |
| 261 |
std::unique_lock aGuard( m_aMutex ); |
| 262 |
|
| 263 |
if ( m_bCountFinal ) |
| 264 |
return m_aResults.size(); |
| 265 |
|
| 266 |
sal_uInt32 nOldCount = m_aResults.size(); |
| 267 |
|
| 268 |
while ( m_xFolderEnum->hasMoreElements() ) |
| 269 |
{ |
| 270 |
try |
| 271 |
{ |
| 272 |
uno::Reference< container::XNamed > xNamed; |
| 273 |
m_xFolderEnum->nextElement() >>= xNamed; |
| 274 |
|
| 275 |
if ( !xNamed.is() ) |
| 276 |
{ |
| 277 |
OSL_FAIL( "DataSupplier::getResult - Got no XNamed!" ); |
| 278 |
break; |
| 279 |
} |
| 280 |
|
| 281 |
OUString aName = xNamed->getName(); |
| 282 |
|
| 283 |
if ( aName.isEmpty() ) |
| 284 |
{ |
| 285 |
OSL_FAIL( "DataSupplier::getResult - Empty name!" ); |
| 286 |
break; |
| 287 |
} |
| 288 |
|
| 289 |
// Assemble URL for child. |
| 290 |
OUString aURL = assembleChildURL( aName ); |
| 291 |
|
| 292 |
m_aResults.emplace_back(aURL); |
| 293 |
} |
| 294 |
catch ( container::NoSuchElementException const & ) |
| 295 |
{ |
| 296 |
m_bThrowException = true; |
| 297 |
break; |
| 298 |
} |
| 299 |
catch ( lang::WrappedTargetException const & ) |
| 300 |
{ |
| 301 |
m_bThrowException = true; |
| 302 |
break; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
m_bCountFinal = true; |
| 307 |
|
| 308 |
rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet(); |
| 309 |
if ( xResultSet.is() ) |
| 310 |
{ |
| 311 |
// Callbacks follow! |
| 312 |
aGuard.unlock(); |
| 313 |
|
| 314 |
if ( nOldCount < m_aResults.size() ) |
| 315 |
xResultSet->rowCountChanged(rResultSetGuard, |
| 316 |
nOldCount, m_aResults.size() ); |
| 317 |
|
| 318 |
xResultSet->rowCountFinal(rResultSetGuard); |
| 319 |
} |
| 320 |
|
| 321 |
return m_aResults.size(); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
// virtual |
| 326 |
sal_uInt32 DataSupplier::currentCount() |
| 327 |
{ |
| 328 |
return m_aResults.size(); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
// virtual |
| 333 |
bool DataSupplier::isCountFinal() |
| 334 |
{ |
| 335 |
return m_bCountFinal; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
// virtual |
| 340 |
uno::Reference< sdbc::XRow > DataSupplier::queryPropertyValues( |
| 341 |
std::unique_lock<std::mutex>& rResultSetGuard, |
| 342 |
sal_uInt32 nIndex ) |
| 343 |
{ |
| 344 |
std::unique_lock aGuard( m_aMutex ); |
| 345 |
|
| 346 |
if ( nIndex < m_aResults.size() ) |
| 347 |
{ |
| 348 |
uno::Reference< sdbc::XRow >& xRow = m_aResults[ nIndex ].xRow; |
| 349 |
if ( xRow.is() ) |
| 350 |
{ |
| 351 |
// Already cached. |
| 352 |
return xRow; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
if ( getResultImpl( rResultSetGuard, aGuard, nIndex ) ) |
| 357 |
{ |
| 358 |
uno::Reference< sdbc::XRow > xRow = Content::getPropertyValues( |
| 359 |
m_xContext, |
| 360 |
getResultSet()->getProperties(), |
| 361 |
static_cast< ContentProvider * >( |
| 362 |
m_xContent->getProvider().get() ), |
| 363 |
queryContentIdentifierStringImpl( rResultSetGuard, aGuard, nIndex ) ); |
| 364 |
m_aResults[ nIndex ].xRow = xRow; |
| 365 |
return xRow; |
| 366 |
} |
| 367 |
|
| 368 |
return uno::Reference< sdbc::XRow >(); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
// virtual |
| 373 |
void DataSupplier::releasePropertyValues( sal_uInt32 nIndex ) |
| 374 |
{ |
| 375 |
std::unique_lock aGuard( m_aMutex ); |
| 376 |
|
| 377 |
if ( nIndex < m_aResults.size() ) |
| 378 |
m_aResults[ nIndex ].xRow.clear(); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
// virtual |
| 383 |
void DataSupplier::close() |
| 384 |
{ |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
// virtual |
| 389 |
void DataSupplier::validate() |
| 390 |
{ |
| 391 |
if ( m_bThrowException ) |
| 392 |
throw ucb::ResultSetException(); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
OUString DataSupplier::assembleChildURL( const OUString& aName ) |
| 397 |
{ |
| 398 |
OUString aURL; |
| 399 |
OUString aContURL |
| 400 |
= m_xContent->getIdentifier()->getContentIdentifier(); |
| 401 |
sal_Int32 nParam = aContURL.indexOf( '?' ); |
| 402 |
if ( nParam >= 0 ) |
| 403 |
{ |
| 404 |
aURL = aContURL.copy( 0, nParam ); |
| 405 |
|
| 406 |
sal_Int32 nPackageUrlEnd = aURL.lastIndexOf( '/' ); |
| 407 |
if ( nPackageUrlEnd != aURL.getLength() - 1 ) |
| 408 |
aURL += "/"; |
| 409 |
|
| 410 |
aURL += ::ucb_impl::urihelper::encodeSegment( aName ) + |
| 411 |
aContURL.subView( nParam ); |
| 412 |
} |
| 413 |
else |
| 414 |
{ |
| 415 |
aURL = aContURL; |
| 416 |
|
| 417 |
sal_Int32 nPackageUrlEnd = aURL.lastIndexOf( '/' ); |
| 418 |
if ( nPackageUrlEnd != aURL.getLength() - 1 ) |
| 419 |
aURL += "/"; |
| 420 |
|
| 421 |
aURL += ::ucb_impl::urihelper::encodeSegment( aName ); |
| 422 |
} |
| 423 |
return aURL; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |