LCOV - code coverage report
Current view: top level - ucb/source/ucp/package - pkgdatasupplier.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 104 169 61.5 %
Date: 2014-04-11 Functions: 14 19 73.7 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <vector>
      28             : #include <osl/diagnose.h>
      29             : #include <com/sun/star/container/XEnumeration.hpp>
      30             : #include <com/sun/star/container/XNamed.hpp>
      31             : #include <ucbhelper/contentidentifier.hxx>
      32             : #include <ucbhelper/providerhelper.hxx>
      33             : #include "pkgdatasupplier.hxx"
      34             : #include "pkgcontent.hxx"
      35             : #include "pkgprovider.hxx"
      36             : 
      37             : #include "../inc/urihelper.hxx"
      38             : 
      39             : using namespace com::sun::star;
      40             : using namespace package_ucp;
      41             : 
      42             : namespace package_ucp
      43             : {
      44             : 
      45             : 
      46             : 
      47             : // struct ResultListEntry.
      48             : 
      49             : 
      50             : 
      51           8 : struct ResultListEntry
      52             : {
      53             :     OUString                             aURL;
      54             :     uno::Reference< ucb::XContentIdentifier > xId;
      55             :     uno::Reference< ucb::XContent >           xContent;
      56             :     uno::Reference< sdbc::XRow >              xRow;
      57             : 
      58           8 :     ResultListEntry( const OUString& rURL ) : aURL( rURL ) {}
      59             : };
      60             : 
      61             : 
      62             : 
      63             : // ResultList.
      64             : 
      65             : 
      66             : 
      67             : typedef std::vector< ResultListEntry* > ResultList;
      68             : 
      69             : 
      70             : 
      71             : // struct DataSupplier_Impl.
      72             : 
      73             : 
      74             : 
      75             : struct DataSupplier_Impl
      76             : {
      77             :     osl::Mutex                                   m_aMutex;
      78             :     ResultList                                   m_aResults;
      79             :     rtl::Reference< Content >                    m_xContent;
      80             :     uno::Reference< uno::XComponentContext >     m_xContext;
      81             :     uno::Reference< container::XEnumeration >    m_xFolderEnum;
      82             :     sal_Int32                                    m_nOpenMode;
      83             :     sal_Bool                                     m_bCountFinal;
      84             :     sal_Bool                                     m_bThrowException;
      85             : 
      86          17 :     DataSupplier_Impl(
      87             :             const uno::Reference< uno::XComponentContext >& rxContext,
      88             :             const rtl::Reference< Content >& rContent,
      89             :             sal_Int32 nOpenMode )
      90             :     : m_xContent( rContent ), m_xContext( rxContext ),
      91             :       m_xFolderEnum( rContent->getIterator() ), m_nOpenMode( nOpenMode ),
      92          17 :       m_bCountFinal( !m_xFolderEnum.is() ), m_bThrowException( m_bCountFinal )
      93          17 :     {}
      94             :     ~DataSupplier_Impl();
      95             : };
      96             : 
      97             : 
      98          34 : DataSupplier_Impl::~DataSupplier_Impl()
      99             : {
     100          17 :     ResultList::const_iterator it  = m_aResults.begin();
     101          17 :     ResultList::const_iterator end = m_aResults.end();
     102             : 
     103          42 :     while ( it != end )
     104             :     {
     105           8 :         delete (*it);
     106           8 :         ++it;
     107             :     }
     108          17 : }
     109             : 
     110             : }
     111             : 
     112             : 
     113             : 
     114             : 
     115             : // DataSupplier Implementation.
     116             : 
     117             : 
     118             : 
     119             : 
     120          17 : DataSupplier::DataSupplier(
     121             :                 const uno::Reference< uno::XComponentContext >& rxContext,
     122             :                 const rtl::Reference< Content >& rContent,
     123             :                 sal_Int32 nOpenMode )
     124          17 : : m_pImpl( new DataSupplier_Impl( rxContext, rContent, nOpenMode ) )
     125             : {
     126          17 : }
     127             : 
     128             : 
     129             : // virtual
     130          51 : DataSupplier::~DataSupplier()
     131             : {
     132          17 :     delete m_pImpl;
     133          34 : }
     134             : 
     135             : 
     136             : // virtual
     137          16 : OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
     138             : {
     139          16 :     osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
     140             : 
     141          16 :     if ( nIndex < m_pImpl->m_aResults.size() )
     142             :     {
     143          16 :         OUString aId = m_pImpl->m_aResults[ nIndex ]->aURL;
     144          16 :         if ( !aId.isEmpty() )
     145             :         {
     146             :             // Already cached.
     147          16 :             return aId;
     148           0 :         }
     149             :     }
     150             : 
     151           0 :     if ( getResult( nIndex ) )
     152             :     {
     153             :         // Note: getResult fills m_pImpl->m_aResults[ nIndex ]->aURL.
     154           0 :         return m_pImpl->m_aResults[ nIndex ]->aURL;
     155             :     }
     156           0 :     return OUString();
     157             : }
     158             : 
     159             : 
     160             : // virtual
     161             : uno::Reference< ucb::XContentIdentifier >
     162           8 : DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
     163             : {
     164           8 :     osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
     165             : 
     166           8 :     if ( nIndex < m_pImpl->m_aResults.size() )
     167             :     {
     168             :         uno::Reference< ucb::XContentIdentifier > xId
     169           8 :                                 = m_pImpl->m_aResults[ nIndex ]->xId;
     170           8 :         if ( xId.is() )
     171             :         {
     172             :             // Already cached.
     173           0 :             return xId;
     174           8 :         }
     175             :     }
     176             : 
     177          16 :     OUString aId = queryContentIdentifierString( nIndex );
     178           8 :     if ( !aId.isEmpty() )
     179             :     {
     180             :         uno::Reference< ucb::XContentIdentifier > xId
     181           8 :             = new ::ucbhelper::ContentIdentifier( aId );
     182           8 :         m_pImpl->m_aResults[ nIndex ]->xId = xId;
     183           8 :         return xId;
     184             :     }
     185           8 :     return uno::Reference< ucb::XContentIdentifier >();
     186             : }
     187             : 
     188             : 
     189             : // virtual
     190           8 : uno::Reference< ucb::XContent > DataSupplier::queryContent(
     191             :                                                         sal_uInt32 nIndex )
     192             : {
     193           8 :     osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
     194             : 
     195           8 :     if ( nIndex < m_pImpl->m_aResults.size() )
     196             :     {
     197             :         uno::Reference< ucb::XContent > xContent
     198           8 :                                 = m_pImpl->m_aResults[ nIndex ]->xContent;
     199           8 :         if ( xContent.is() )
     200             :         {
     201             :             // Already cached.
     202           0 :             return xContent;
     203           8 :         }
     204             :     }
     205             : 
     206             :     uno::Reference< ucb::XContentIdentifier > xId
     207          16 :         = queryContentIdentifier( nIndex );
     208           8 :     if ( xId.is() )
     209             :     {
     210             :         try
     211             :         {
     212             :             uno::Reference< ucb::XContent > xContent
     213           8 :                 = m_pImpl->m_xContent->getProvider()->queryContent( xId );
     214           8 :             m_pImpl->m_aResults[ nIndex ]->xContent = xContent;
     215           8 :             return xContent;
     216             : 
     217             :         }
     218           0 :         catch ( ucb::IllegalIdentifierException const & )
     219             :         {
     220             :         }
     221             :     }
     222           8 :     return uno::Reference< ucb::XContent >();
     223             : }
     224             : 
     225             : 
     226             : // virtual
     227          33 : bool DataSupplier::getResult( sal_uInt32 nIndex )
     228             : {
     229          33 :     osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
     230             : 
     231          33 :     if ( m_pImpl->m_aResults.size() > nIndex )
     232             :     {
     233             :         // Result already present.
     234           8 :         return true;
     235             :     }
     236             : 
     237             :     // Result not (yet) present.
     238             : 
     239          25 :     if ( m_pImpl->m_bCountFinal )
     240           0 :         return false;
     241             : 
     242             :     // Try to obtain result...
     243             : 
     244          25 :     sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
     245          25 :     bool bFound = false;
     246          25 :     sal_uInt32 nPos = nOldCount;
     247             : 
     248          50 :     while ( m_pImpl->m_xFolderEnum->hasMoreElements() )
     249             :     {
     250             :         try
     251             :         {
     252           8 :             uno::Reference< container::XNamed > xNamed;
     253           8 :             m_pImpl->m_xFolderEnum->nextElement() >>= xNamed;
     254             : 
     255           8 :             if ( !xNamed.is() )
     256             :             {
     257             :                 OSL_FAIL( "DataSupplier::getResult - Got no XNamed!" );
     258           0 :                 break;
     259             :             }
     260             : 
     261           8 :             OUString aName = xNamed->getName();
     262             : 
     263           8 :             if ( aName.isEmpty() )
     264             :             {
     265             :                 OSL_FAIL( "DataSupplier::getResult - Empty name!" );
     266           0 :                 break;
     267             :             }
     268             : 
     269             :             // Assemble URL for child.
     270           8 :             OUString aURL = assembleChildURL( aName );
     271             : 
     272           8 :             m_pImpl->m_aResults.push_back( new ResultListEntry( aURL ) );
     273             : 
     274           8 :             if ( nPos == nIndex )
     275             :             {
     276             :                 // Result obtained.
     277           8 :                 bFound = true;
     278           8 :                 break;
     279             :             }
     280             : 
     281           0 :             nPos++;
     282             :         }
     283           0 :         catch ( container::NoSuchElementException const & )
     284             :         {
     285           0 :             m_pImpl->m_bThrowException = sal_True;
     286           0 :             break;
     287             :         }
     288           0 :         catch ( lang::WrappedTargetException const & )
     289             :         {
     290           0 :             m_pImpl->m_bThrowException = sal_True;
     291           0 :             break;
     292             :         }
     293             :     }
     294             : 
     295          25 :     if ( !bFound )
     296          17 :         m_pImpl->m_bCountFinal = sal_True;
     297             : 
     298          50 :     rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
     299          25 :     if ( xResultSet.is() )
     300             :     {
     301             :         // Callbacks follow!
     302          25 :         aGuard.clear();
     303             : 
     304          25 :         if ( nOldCount < m_pImpl->m_aResults.size() )
     305             :             xResultSet->rowCountChanged(
     306           8 :                                     nOldCount, m_pImpl->m_aResults.size() );
     307             : 
     308          25 :         if ( m_pImpl->m_bCountFinal )
     309          17 :             xResultSet->rowCountFinal();
     310             :     }
     311             : 
     312          58 :     return bFound;
     313             : }
     314             : 
     315             : 
     316             : // virtual
     317           0 : sal_uInt32 DataSupplier::totalCount()
     318             : {
     319           0 :     osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
     320             : 
     321           0 :     if ( m_pImpl->m_bCountFinal )
     322           0 :         return m_pImpl->m_aResults.size();
     323             : 
     324           0 :     sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
     325             : 
     326           0 :     while ( m_pImpl->m_xFolderEnum->hasMoreElements() )
     327             :     {
     328             :         try
     329             :         {
     330           0 :             uno::Reference< container::XNamed > xNamed;
     331           0 :             m_pImpl->m_xFolderEnum->nextElement() >>= xNamed;
     332             : 
     333           0 :             if ( !xNamed.is() )
     334             :             {
     335             :                 OSL_FAIL( "DataSupplier::getResult - Got no XNamed!" );
     336           0 :                 break;
     337             :             }
     338             : 
     339           0 :             OUString aName = xNamed->getName();
     340             : 
     341           0 :             if ( aName.isEmpty() )
     342             :             {
     343             :                 OSL_FAIL( "DataSupplier::getResult - Empty name!" );
     344           0 :                 break;
     345             :             }
     346             : 
     347             :             // Assemble URL for child.
     348           0 :             OUString aURL = assembleChildURL( aName );
     349             : 
     350           0 :             m_pImpl->m_aResults.push_back( new ResultListEntry( aURL ) );
     351             :         }
     352           0 :         catch ( container::NoSuchElementException const & )
     353             :         {
     354           0 :             m_pImpl->m_bThrowException = sal_True;
     355           0 :             break;
     356             :         }
     357           0 :         catch ( lang::WrappedTargetException const & )
     358             :         {
     359           0 :             m_pImpl->m_bThrowException = sal_True;
     360           0 :             break;
     361             :         }
     362             :     }
     363             : 
     364           0 :     m_pImpl->m_bCountFinal = sal_True;
     365             : 
     366           0 :     rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
     367           0 :     if ( xResultSet.is() )
     368             :     {
     369             :         // Callbacks follow!
     370           0 :         aGuard.clear();
     371             : 
     372           0 :         if ( nOldCount < m_pImpl->m_aResults.size() )
     373             :             xResultSet->rowCountChanged(
     374           0 :                                     nOldCount, m_pImpl->m_aResults.size() );
     375             : 
     376           0 :         xResultSet->rowCountFinal();
     377             :     }
     378             : 
     379           0 :     return m_pImpl->m_aResults.size();
     380             : }
     381             : 
     382             : 
     383             : // virtual
     384           0 : sal_uInt32 DataSupplier::currentCount()
     385             : {
     386           0 :     return m_pImpl->m_aResults.size();
     387             : }
     388             : 
     389             : 
     390             : // virtual
     391           0 : bool DataSupplier::isCountFinal()
     392             : {
     393           0 :     return m_pImpl->m_bCountFinal;
     394             : }
     395             : 
     396             : 
     397             : // virtual
     398          32 : uno::Reference< sdbc::XRow > DataSupplier::queryPropertyValues(
     399             :                                                         sal_uInt32 nIndex  )
     400             : {
     401          32 :     osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
     402             : 
     403          32 :     if ( nIndex < m_pImpl->m_aResults.size() )
     404             :     {
     405          32 :         uno::Reference< sdbc::XRow > xRow = m_pImpl->m_aResults[ nIndex ]->xRow;
     406          32 :         if ( xRow.is() )
     407             :         {
     408             :             // Already cached.
     409          24 :             return xRow;
     410           8 :         }
     411             :     }
     412             : 
     413           8 :     if ( getResult( nIndex ) )
     414             :     {
     415             :         uno::Reference< sdbc::XRow > xRow = Content::getPropertyValues(
     416             :                         m_pImpl->m_xContext,
     417          16 :                         getResultSet()->getProperties(),
     418             :                         static_cast< ContentProvider * >(
     419           8 :                             m_pImpl->m_xContent->getProvider().get() ),
     420          24 :                         queryContentIdentifierString( nIndex ) );
     421           8 :         m_pImpl->m_aResults[ nIndex ]->xRow = xRow;
     422           8 :         return xRow;
     423             :     }
     424             : 
     425           0 :     return uno::Reference< sdbc::XRow >();
     426             : }
     427             : 
     428             : 
     429             : // virtual
     430           0 : void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
     431             : {
     432           0 :     osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
     433             : 
     434           0 :     if ( nIndex < m_pImpl->m_aResults.size() )
     435           0 :         m_pImpl->m_aResults[ nIndex ]->xRow = uno::Reference< sdbc::XRow >();
     436           0 : }
     437             : 
     438             : 
     439             : // virtual
     440           0 : void DataSupplier::close()
     441             : {
     442           0 : }
     443             : 
     444             : 
     445             : // virtual
     446          57 : void DataSupplier::validate()
     447             :     throw( ucb::ResultSetException )
     448             : {
     449          57 :     if ( m_pImpl->m_bThrowException )
     450           0 :         throw ucb::ResultSetException();
     451          57 : }
     452             : 
     453             : 
     454           8 : OUString DataSupplier::assembleChildURL( const OUString& aName )
     455             : {
     456           8 :     OUString aURL;
     457             :     OUString aContURL
     458          16 :         = m_pImpl->m_xContent->getIdentifier()->getContentIdentifier();
     459           8 :     sal_Int32 nParam = aContURL.indexOf( '?' );
     460           8 :     if ( nParam >= 0 )
     461             :     {
     462           0 :         aURL = aContURL.copy( 0, nParam );
     463             : 
     464           0 :         sal_Int32 nPackageUrlEnd = aURL.lastIndexOf( '/' );
     465           0 :         if ( nPackageUrlEnd != aURL.getLength() - 1 )
     466           0 :             aURL += "/";
     467             : 
     468           0 :         aURL += ::ucb_impl::urihelper::encodeSegment( aName );
     469           0 :         aURL += aContURL.copy( nParam );
     470             :     }
     471             :     else
     472             :     {
     473           8 :         aURL = aContURL;
     474             : 
     475           8 :         sal_Int32 nPackageUrlEnd = aURL.lastIndexOf( '/' );
     476           8 :         if ( nPackageUrlEnd != aURL.getLength() - 1 )
     477           8 :             aURL += "/";
     478             : 
     479           8 :         aURL += ::ucb_impl::urihelper::encodeSegment( aName );
     480             :     }
     481          16 :     return aURL;
     482             : }
     483             : 
     484             : 
     485             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10