LCOV - code coverage report
Current view: top level - unotools/source/ucbhelper - localfilehelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 53 70 75.7 %
Date: 2014-04-11 Functions: 5 7 71.4 %
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             : #include <com/sun/star/sdbc/XResultSet.hpp>
      21             : #include <com/sun/star/ucb/XContentAccess.hpp>
      22             : #include <com/sun/star/ucb/CommandAbortedException.hpp>
      23             : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
      24             : #include <comphelper/processfactory.hxx>
      25             : #include <unotools/localfilehelper.hxx>
      26             : #include <ucbhelper/fileidentifierconverter.hxx>
      27             : #include <rtl/ustring.hxx>
      28             : #include <osl/file.hxx>
      29             : #include <tools/urlobj.hxx>
      30             : #include <ucbhelper/content.hxx>
      31             : #include <vector>
      32             : 
      33             : using namespace ::osl;
      34             : using namespace ::com::sun::star::uno;
      35             : using namespace ::com::sun::star::ucb;
      36             : 
      37             : namespace utl
      38             : {
      39             : 
      40          20 : bool LocalFileHelper::ConvertSystemPathToURL( const OUString& rName, const OUString& rBaseURL, OUString& rReturn )
      41             : {
      42          20 :     rReturn = "";
      43             : 
      44             :     Reference< XUniversalContentBroker > pBroker(
      45             :         UniversalContentBroker::create(
      46          20 :             comphelper::getProcessComponentContext() ) );
      47             :     try
      48             :     {
      49          20 :         rReturn = ::ucbhelper::getFileURLFromSystemPath( pBroker, rBaseURL, rName );
      50             :     }
      51           0 :     catch ( ::com::sun::star::uno::RuntimeException& )
      52             :     {
      53           0 :         return false;
      54             :     }
      55             : 
      56          20 :     return !rReturn.isEmpty();
      57             : }
      58             : 
      59           0 : bool LocalFileHelper::ConvertURLToSystemPath( const OUString& rName, OUString& rReturn )
      60             : {
      61           0 :     rReturn = "";
      62             :     Reference< XUniversalContentBroker > pBroker(
      63             :         UniversalContentBroker::create(
      64           0 :             comphelper::getProcessComponentContext() ) );
      65             :     try
      66             :     {
      67           0 :         rReturn = ::ucbhelper::getSystemPathFromFileURL( pBroker, rName );
      68             :     }
      69           0 :     catch ( ::com::sun::star::uno::RuntimeException& )
      70             :     {
      71             :     }
      72             : 
      73           0 :     return !rReturn.isEmpty();
      74             : }
      75             : 
      76       22762 : bool LocalFileHelper::ConvertPhysicalNameToURL(const OUString& rName, OUString& rReturn)
      77             : {
      78       22762 :     rReturn = OUString();
      79             :     Reference< XUniversalContentBroker > pBroker(
      80             :         UniversalContentBroker::create(
      81       22762 :             comphelper::getProcessComponentContext() ) );
      82             :     try
      83             :     {
      84       22762 :         OUString aBase( ::ucbhelper::getLocalFileURL() );
      85       22762 :         rReturn = ::ucbhelper::getFileURLFromSystemPath( pBroker, aBase, rName );
      86             :     }
      87           0 :     catch (const ::com::sun::star::uno::RuntimeException&)
      88             :     {
      89             :     }
      90             : 
      91       22762 :     return !rReturn.isEmpty();
      92             : }
      93             : 
      94       32272 : bool LocalFileHelper::ConvertURLToPhysicalName(const OUString& rName, OUString& rReturn)
      95             : {
      96       32272 :     rReturn = "";
      97             :     Reference< XUniversalContentBroker > pBroker(
      98             :         UniversalContentBroker::create(
      99       32272 :             comphelper::getProcessComponentContext() ) );
     100             :     try
     101             :     {
     102       32272 :         INetURLObject aObj( rName );
     103       64544 :         INetURLObject aLocal( ::ucbhelper::getLocalFileURL() );
     104       32272 :         if ( aObj.GetProtocol() == aLocal.GetProtocol() )
     105       62595 :             rReturn = ::ucbhelper::getSystemPathFromFileURL( pBroker, rName );
     106             :     }
     107           0 :     catch (const ::com::sun::star::uno::RuntimeException&)
     108             :     {
     109             :     }
     110             : 
     111       32272 :     return !rReturn.isEmpty();
     112             : }
     113             : 
     114       27120 : bool LocalFileHelper::IsLocalFile(const OUString& rName)
     115             : {
     116       27120 :     OUString aTmp;
     117       27120 :     return ConvertURLToPhysicalName(rName, aTmp);
     118             : }
     119             : 
     120           0 : bool LocalFileHelper::IsFileContent(const OUString& rName)
     121             : {
     122           0 :     OUString aTmp;
     123           0 :     return ConvertURLToSystemPath(rName, aTmp);
     124             : }
     125             : 
     126             : typedef ::std::vector< OUString* > StringList_Impl;
     127             : 
     128          42 : ::com::sun::star::uno::Sequence < OUString > LocalFileHelper::GetFolderContents( const OUString& rFolder, bool bFolder )
     129             : {
     130          42 :     StringList_Impl* pFiles = NULL;
     131             :     try
     132             :     {
     133             :         ::ucbhelper::Content aCnt(
     134             :             rFolder, Reference< XCommandEnvironment >(),
     135          42 :             comphelper::getProcessComponentContext() );
     136          84 :         Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
     137          84 :         ::com::sun::star::uno::Sequence< OUString > aProps(1);
     138          42 :         OUString* pProps = aProps.getArray();
     139          42 :         pProps[0] = "Url";
     140             : 
     141             :         try
     142             :         {
     143          42 :             ::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
     144          42 :             xResultSet = aCnt.createCursor( aProps, eInclude );
     145             :         }
     146           0 :         catch( ::com::sun::star::ucb::CommandAbortedException& )
     147             :         {
     148             :         }
     149          19 :         catch( Exception& )
     150             :         {
     151             :         }
     152             : 
     153          42 :         if ( xResultSet.is() )
     154             :         {
     155          23 :             pFiles = new StringList_Impl;
     156          23 :             Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
     157             :             try
     158             :             {
     159         139 :                 while ( xResultSet->next() )
     160             :                 {
     161          93 :                     OUString aId = xContentAccess->queryContentIdentifierString();
     162          93 :                     OUString* pFile = new OUString( aId );
     163          93 :                     pFiles->push_back( pFile );
     164          93 :                 }
     165             :             }
     166           0 :             catch( ::com::sun::star::ucb::CommandAbortedException& )
     167             :             {
     168             :             }
     169           0 :             catch( Exception& )
     170             :             {
     171          23 :             }
     172          42 :         }
     173             :     }
     174           0 :     catch( Exception& )
     175             :     {
     176             :     }
     177             : 
     178          42 :     if ( pFiles )
     179             :     {
     180          23 :         size_t nCount = pFiles->size();
     181          23 :         Sequence < OUString > aRet( nCount );
     182          23 :         OUString* pRet = aRet.getArray();
     183         116 :         for ( size_t i = 0; i < nCount; ++i )
     184             :         {
     185          93 :             OUString* pFile = (*pFiles)[ i ];
     186          93 :             pRet[i] = *( pFile );
     187          93 :             delete pFile;
     188             :         }
     189          23 :         delete pFiles;
     190          23 :         return aRet;
     191             :     }
     192             :     else
     193          19 :         return Sequence < OUString > ();
     194             : }
     195             : 
     196             : }
     197             : 
     198             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10