LCOV - code coverage report
Current view: top level - libreoffice/unotools/source/ucbhelper - localfilehelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 17 70 24.3 %
Date: 2012-12-27 Functions: 3 7 42.9 %
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           0 : sal_Bool LocalFileHelper::ConvertSystemPathToURL( const String& rName, const String& rBaseURL, String& rReturn )
      41             : {
      42           0 :     rReturn = ::rtl::OUString();
      43             : 
      44             :     Reference< XUniversalContentBroker > pBroker(
      45             :         UniversalContentBroker::create(
      46           0 :             comphelper::getProcessComponentContext() ) );
      47             :     try
      48             :     {
      49           0 :         rReturn = ::ucbhelper::getFileURLFromSystemPath( pBroker, rBaseURL, rName );
      50             :     }
      51           0 :     catch ( ::com::sun::star::uno::RuntimeException& )
      52             :     {
      53           0 :         return sal_False;
      54             :     }
      55             : 
      56           0 :     return ( rReturn.Len() != 0 );
      57             : }
      58             : 
      59           0 : sal_Bool LocalFileHelper::ConvertURLToSystemPath( const String& rName, String& rReturn )
      60             : {
      61           0 :     rReturn = ::rtl::OUString();
      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.Len() != 0 );
      74             : }
      75             : 
      76        2514 : bool LocalFileHelper::ConvertPhysicalNameToURL(const rtl::OUString& rName, rtl::OUString& rReturn)
      77             : {
      78        2514 :     rReturn = ::rtl::OUString();
      79             :     Reference< XUniversalContentBroker > pBroker(
      80             :         UniversalContentBroker::create(
      81        2514 :             comphelper::getProcessComponentContext() ) );
      82             :     try
      83             :     {
      84        2514 :         rtl::OUString aBase( ::ucbhelper::getLocalFileURL() );
      85        2514 :         rReturn = ::ucbhelper::getFileURLFromSystemPath( pBroker, aBase, rName );
      86             :     }
      87           0 :     catch (const ::com::sun::star::uno::RuntimeException&)
      88             :     {
      89             :     }
      90             : 
      91        2514 :     return !rReturn.isEmpty();
      92             : }
      93             : 
      94        3099 : bool LocalFileHelper::ConvertURLToPhysicalName(const rtl::OUString& rName, rtl::OUString& rReturn)
      95             : {
      96        3099 :     rReturn = ::rtl::OUString();
      97             :     Reference< XUniversalContentBroker > pBroker(
      98             :         UniversalContentBroker::create(
      99        3099 :             comphelper::getProcessComponentContext() ) );
     100             :     try
     101             :     {
     102        3099 :         INetURLObject aObj( rName );
     103        3099 :         INetURLObject aLocal( ::ucbhelper::getLocalFileURL() );
     104        3099 :         if ( aObj.GetProtocol() == aLocal.GetProtocol() )
     105        2744 :             rReturn = ::ucbhelper::getSystemPathFromFileURL( pBroker, rName );
     106             :     }
     107           0 :     catch (const ::com::sun::star::uno::RuntimeException&)
     108             :     {
     109             :     }
     110             : 
     111        3099 :     return !rReturn.isEmpty();
     112             : }
     113             : 
     114        2429 : sal_Bool LocalFileHelper::IsLocalFile(const rtl::OUString& rName)
     115             : {
     116        2429 :     rtl::OUString aTmp;
     117        2429 :     return ConvertURLToPhysicalName(rName, aTmp);
     118             : }
     119             : 
     120           0 : sal_Bool LocalFileHelper::IsFileContent(const rtl::OUString& rName)
     121             : {
     122           0 :     String aTmp;
     123           0 :     return ConvertURLToSystemPath(rName, aTmp);
     124             : }
     125             : 
     126             : typedef ::std::vector< ::rtl::OUString* > StringList_Impl;
     127             : 
     128           0 : ::com::sun::star::uno::Sequence < ::rtl::OUString > LocalFileHelper::GetFolderContents( const ::rtl::OUString& rFolder, sal_Bool bFolder )
     129             : {
     130           0 :     StringList_Impl* pFiles = NULL;
     131             :     try
     132             :     {
     133             :         ::ucbhelper::Content aCnt(
     134             :             rFolder, Reference< XCommandEnvironment >(),
     135           0 :             comphelper::getProcessComponentContext() );
     136           0 :         Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
     137           0 :         ::com::sun::star::uno::Sequence< ::rtl::OUString > aProps(1);
     138           0 :         ::rtl::OUString* pProps = aProps.getArray();
     139           0 :         pProps[0] = ::rtl::OUString("Url");
     140             : 
     141             :         try
     142             :         {
     143           0 :             ::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
     144           0 :             xResultSet = aCnt.createCursor( aProps, eInclude );
     145             :         }
     146           0 :         catch( ::com::sun::star::ucb::CommandAbortedException& )
     147             :         {
     148             :         }
     149           0 :         catch( Exception& )
     150             :         {
     151             :         }
     152             : 
     153           0 :         if ( xResultSet.is() )
     154             :         {
     155           0 :             pFiles = new StringList_Impl;
     156           0 :             Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
     157             :             try
     158             :             {
     159           0 :                 while ( xResultSet->next() )
     160             :                 {
     161           0 :                     ::rtl::OUString aId = xContentAccess->queryContentIdentifierString();
     162           0 :                     ::rtl::OUString* pFile = new ::rtl::OUString( aId );
     163           0 :                     pFiles->push_back( pFile );
     164           0 :                 }
     165             :             }
     166           0 :             catch( ::com::sun::star::ucb::CommandAbortedException& )
     167             :             {
     168             :             }
     169           0 :             catch( Exception& )
     170             :             {
     171           0 :             }
     172           0 :         }
     173             :     }
     174           0 :     catch( Exception& )
     175             :     {
     176             :     }
     177             : 
     178           0 :     if ( pFiles )
     179             :     {
     180           0 :         size_t nCount = pFiles->size();
     181           0 :         Sequence < ::rtl::OUString > aRet( nCount );
     182           0 :         ::rtl::OUString* pRet = aRet.getArray();
     183           0 :         for ( size_t i = 0; i < nCount; ++i )
     184             :         {
     185           0 :             ::rtl::OUString* pFile = (*pFiles)[ i ];
     186           0 :             pRet[i] = *( pFile );
     187           0 :             delete pFile;
     188             :         }
     189           0 :         delete pFiles;
     190           0 :         return aRet;
     191             :     }
     192             :     else
     193           0 :         return Sequence < ::rtl::OUString > ();
     194             : }
     195             : 
     196             : }
     197             : 
     198             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10