LCOV - code coverage report
Current view: top level - ucb/source/core - FileAccess.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 307 0.0 %
Date: 2014-04-14 Functions: 0 42 0.0 %
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 <osl/mutex.hxx>
      21             : #include <osl/diagnose.h>
      22             : 
      23             : #include <uno/mapping.hxx>
      24             : #include <comphelper/processfactory.hxx>
      25             : #include <cppuhelper/factory.hxx>
      26             : #include <cppuhelper/implbase1.hxx>
      27             : 
      28             : #include <tools/urlobj.hxx>
      29             : #include <ucbhelper/content.hxx>
      30             : #include <unotools/streamwrap.hxx>
      31             : #include <tools/stream.hxx>
      32             : 
      33             : #include <com/sun/star/beans/Property.hpp>
      34             : #include <com/sun/star/container/XChild.hpp>
      35             : #include <com/sun/star/io/XActiveDataSink.hpp>
      36             : #include <com/sun/star/io/XActiveDataSource.hpp>
      37             : #include <com/sun/star/io/XActiveDataStreamer.hpp>
      38             : #include <com/sun/star/sdbc/XResultSet.hpp>
      39             : #include <com/sun/star/ucb/CommandFailedException.hpp>
      40             : #include <com/sun/star/ucb/ContentInfo.hpp>
      41             : #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
      42             : #include <com/sun/star/ucb/InsertCommandArgument.hpp>
      43             : #include <com/sun/star/ucb/InteractiveIOException.hpp>
      44             : #include <com/sun/star/ucb/NameClash.hpp>
      45             : #include <com/sun/star/ucb/NameClashException.hpp>
      46             : #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
      47             : #include <com/sun/star/ucb/OpenMode.hpp>
      48             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      49             : #include <com/sun/star/ucb/XContent.hpp>
      50             : #include <com/sun/star/ucb/XContentAccess.hpp>
      51             : #include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
      52             : #include <com/sun/star/util/theMacroExpander.hpp>
      53             : 
      54             : #include <vector>
      55             : 
      56             : #include "FileAccess.hxx"
      57             : 
      58             : #define SERVICE_NAME "com.sun.star.ucb.SimpleFileAccess"
      59             : 
      60             : using namespace ::com::sun::star::uno;
      61             : using namespace ::com::sun::star::lang;
      62             : using namespace ::com::sun::star::io;
      63             : using namespace ::com::sun::star::ucb;
      64             : using namespace ::com::sun::star::sdbc;
      65             : using namespace ::com::sun::star::task;
      66             : using namespace ::com::sun::star::util;
      67             : using namespace ::com::sun::star::beans;
      68             : using namespace ::com::sun::star::registry;
      69             : using namespace ::com::sun::star::container;
      70             : 
      71             : using ::std::vector;
      72             : 
      73             : namespace
      74             : {
      75             : 
      76             : // Implementation XSimpleFileAccess
      77             : typedef cppu::WeakImplHelper1< XSimpleFileAccess3 > FileAccessHelper;
      78             : class OCommandEnvironment;
      79             : 
      80           0 : class OFileAccess : public FileAccessHelper
      81             : {
      82             :     Reference< XComponentContext > m_xContext;
      83             :     Reference< XCommandEnvironment > mxEnvironment;
      84             :     OCommandEnvironment* mpEnvironment;
      85             : 
      86             :     void transferImpl( const OUString& rSource, const OUString& rDest, sal_Bool bMoveData )
      87             :         throw(CommandAbortedException, Exception, RuntimeException);
      88             :     bool createNewFile( const OUString & rParentURL,
      89             :                         const OUString & rTitle,
      90             :                         const Reference< XInputStream >& data )
      91             :         throw ( Exception );
      92             : 
      93             : public:
      94           0 :     OFileAccess( const Reference< XComponentContext > & xContext )
      95           0 :         : m_xContext( xContext), mpEnvironment( NULL ) {}
      96             :     // Methods
      97             :     virtual void SAL_CALL copy( const OUString& SourceURL, const OUString& DestURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      98             :     virtual void SAL_CALL move( const OUString& SourceURL, const OUString& DestURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      99             :     virtual void SAL_CALL kill( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     100             :     virtual sal_Bool SAL_CALL isFolder( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     101             :     virtual sal_Bool SAL_CALL isReadOnly( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     102             :     virtual void SAL_CALL setReadOnly( const OUString& FileURL, sal_Bool bReadOnly ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     103             :     virtual void SAL_CALL createFolder( const OUString& NewFolderURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     104             :     virtual sal_Int32 SAL_CALL getSize( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     105             :     virtual OUString SAL_CALL getContentType( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     106             :     virtual ::com::sun::star::util::DateTime SAL_CALL getDateTimeModified( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     107             :     virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getFolderContents( const OUString& FolderURL, sal_Bool bIncludeFolders ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     108             :     virtual sal_Bool SAL_CALL exists( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     109             :     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL openFileRead( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     110             :     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL openFileWrite( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     111             :     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream > SAL_CALL openFileReadWrite( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     112             :     virtual void SAL_CALL setInteractionHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     113             :     virtual void SAL_CALL writeFile( const OUString& FileURL, const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& data ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     114             :     virtual sal_Bool SAL_CALL isHidden( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     115             :     virtual void SAL_CALL setHidden( const OUString& FileURL, sal_Bool bHidden ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     116             : };
     117             : 
     118             : // Implementation XActiveDataSink
     119             : typedef cppu::WeakImplHelper1< XActiveDataSink > ActiveDataSinkHelper;
     120             : 
     121           0 : class OActiveDataSink : public ActiveDataSinkHelper
     122             : {
     123             :     Reference< XInputStream > mxStream;
     124             : 
     125             : public:
     126             : 
     127             :     // Methods
     128             :     virtual void SAL_CALL setInputStream( const Reference< XInputStream >& aStream )
     129             :         throw(RuntimeException, std::exception) SAL_OVERRIDE;
     130             :     virtual Reference< XInputStream > SAL_CALL getInputStream(  )
     131             :         throw(RuntimeException, std::exception) SAL_OVERRIDE;
     132             : };
     133             : 
     134             : // Implementation XActiveDataStreamer
     135             : typedef cppu::WeakImplHelper1< XActiveDataStreamer > ActiveDataStreamerHelper;
     136             : 
     137           0 : class OActiveDataStreamer : public ActiveDataStreamerHelper
     138             : {
     139             :     Reference< XStream > mxStream;
     140             : 
     141             : public:
     142             : 
     143             :     // Methods
     144             :     virtual void SAL_CALL setStream( const Reference< XStream >& aStream )
     145             :         throw(RuntimeException, std::exception) SAL_OVERRIDE;
     146             :     virtual Reference< XStream > SAL_CALL getStream()
     147             :         throw(RuntimeException, std::exception) SAL_OVERRIDE;
     148             : };
     149             : 
     150             : // Implementation XCommandEnvironment
     151             : typedef cppu::WeakImplHelper1< XCommandEnvironment > CommandEnvironmentHelper;
     152             : 
     153           0 : class OCommandEnvironment : public CommandEnvironmentHelper
     154             : {
     155             :     Reference< XInteractionHandler > mxInteraction;
     156             : 
     157             : public:
     158           0 :     void setHandler( Reference< XInteractionHandler > xInteraction_ )
     159             :     {
     160           0 :         mxInteraction = xInteraction_;
     161           0 :     }
     162             : 
     163             :     // Methods
     164             :     virtual Reference< XInteractionHandler > SAL_CALL getInteractionHandler()
     165             :         throw(RuntimeException, std::exception) SAL_OVERRIDE;
     166             :     virtual Reference< XProgressHandler > SAL_CALL getProgressHandler()
     167             :         throw(RuntimeException, std::exception) SAL_OVERRIDE;
     168             : };
     169             : 
     170           0 : void OActiveDataSink::setInputStream( const Reference< XInputStream >& aStream )
     171             :     throw(RuntimeException, std::exception)
     172             : {
     173           0 :     mxStream = aStream;
     174           0 : }
     175             : 
     176           0 : Reference< XInputStream > OActiveDataSink::getInputStream()
     177             :     throw(RuntimeException, std::exception)
     178             : {
     179           0 :     return mxStream;
     180             : }
     181             : 
     182           0 : void OActiveDataStreamer::setStream( const Reference< XStream >& aStream )
     183             :     throw(RuntimeException, std::exception)
     184             : {
     185           0 :     mxStream = aStream;
     186           0 : }
     187             : 
     188           0 : Reference< XStream > OActiveDataStreamer::getStream()
     189             :     throw(RuntimeException, std::exception)
     190             : {
     191           0 :     return mxStream;
     192             : }
     193             : 
     194           0 : Reference< XInteractionHandler > OCommandEnvironment::getInteractionHandler()
     195             :     throw(RuntimeException, std::exception)
     196             : {
     197           0 :     return mxInteraction;
     198             : }
     199             : 
     200           0 : Reference< XProgressHandler > OCommandEnvironment::getProgressHandler()
     201             :     throw(RuntimeException, std::exception)
     202             : {
     203           0 :     Reference< XProgressHandler > xRet;
     204           0 :     return xRet;
     205             : }
     206             : 
     207           0 : void OFileAccess::transferImpl( const OUString& rSource,
     208             :                                 const OUString& rDest,
     209             :                                 sal_Bool bMoveData )
     210             :     throw(CommandAbortedException, Exception, RuntimeException)
     211             : {
     212             :     // SfxContentHelper::Transfer_Impl
     213           0 :     INetURLObject aSourceObj( rSource, INET_PROT_FILE );
     214           0 :     INetURLObject aDestObj( rDest, INET_PROT_FILE );
     215             :     OUString aName = aDestObj.getName(
     216           0 :         INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
     217           0 :     OUString aDestURL;
     218           0 :     OUString aSourceURL = aSourceObj.GetMainURL( INetURLObject::NO_DECODE );
     219           0 :     if ( aDestObj.removeSegment() )
     220             :     {
     221             :         // hierarchical URL.
     222             : 
     223           0 :         aDestObj.setFinalSlash();
     224           0 :         aDestURL = aDestObj.GetMainURL( INetURLObject::NO_DECODE );
     225             :     }
     226             :     else
     227             :     {
     228             :         // non-hierachical URL
     229             : 
     230             :         // #i29648#
     231             : 
     232             : 
     233           0 :         if ( aDestObj.GetProtocol() == INET_PROT_VND_SUN_STAR_EXPAND )
     234             :         {
     235             :             // Hack: Expand destination URL using Macro Expander and try again
     236             :             //       with the hopefully hierarchical expanded URL...
     237             : 
     238             :             try
     239             :             {
     240           0 :                 Reference< XMacroExpander > xExpander = theMacroExpander::get(m_xContext);
     241             : 
     242           0 :                 aDestURL = xExpander->expandMacros(
     243           0 :                     aDestObj.GetURLPath( INetURLObject::DECODE_WITH_CHARSET ) );
     244             :             }
     245           0 :             catch ( Exception const & )
     246             :             {
     247             :                 throw RuntimeException(
     248             :                     OUString( "OFileAccess::transferrImpl - Unable to obtain "
     249             :                             "destination folder URL!"  ),
     250           0 :                     static_cast< cppu::OWeakObject * >( this ) );
     251             :             }
     252             : 
     253           0 :             transferImpl( rSource, aDestURL, bMoveData );
     254           0 :             return;
     255             :         }
     256             : 
     257             :         throw RuntimeException(
     258             :             OUString( "OFileAccess::transferrImpl - Unable to obtain "
     259             :                     "destination folder URL!"  ),
     260           0 :                 static_cast< cppu::OWeakObject * >( this ) );
     261             : 
     262             :     }
     263             : 
     264           0 :     ucbhelper::Content aDestPath( aDestURL,   mxEnvironment, comphelper::getProcessComponentContext() );
     265           0 :     ucbhelper::Content aSrc     ( aSourceURL, mxEnvironment, comphelper::getProcessComponentContext() );
     266             : 
     267             :     try
     268             :     {
     269             :         aDestPath.transferContent( aSrc,
     270             :                                    bMoveData
     271             :                                     ? ucbhelper::InsertOperation_MOVE
     272             :                                     : ucbhelper::InsertOperation_COPY,
     273             :                                    aName,
     274           0 :                                    ::com::sun::star::ucb::NameClash::OVERWRITE );
     275             :     }
     276           0 :     catch ( ::com::sun::star::ucb::CommandFailedException const & )
     277             :     {
     278             :         // Interaction Handler already handled the error that has occurred...
     279           0 :     }
     280             : }
     281             : 
     282           0 : void OFileAccess::copy( const OUString& SourceURL, const OUString& DestURL )
     283             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     284             : {
     285           0 :     transferImpl( SourceURL, DestURL, sal_False );
     286           0 : }
     287             : 
     288           0 : void OFileAccess::move( const OUString& SourceURL, const OUString& DestURL )
     289             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     290             : {
     291           0 :     transferImpl( SourceURL, DestURL, sal_True );
     292           0 : }
     293             : 
     294           0 : void OFileAccess::kill( const OUString& FileURL )
     295             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     296             : {
     297             :     // SfxContentHelper::Kill
     298           0 :     INetURLObject aDeleteObj( FileURL, INET_PROT_FILE );
     299           0 :     ucbhelper::Content aCnt( aDeleteObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     300             :     try
     301             :     {
     302           0 :         aCnt.executeCommand( OUString("delete" ), makeAny( sal_Bool( sal_True ) ) );
     303             :     }
     304           0 :     catch ( ::com::sun::star::ucb::CommandFailedException const & )
     305             :     {
     306             :         // Interaction Handler already handled the error that has occurred...
     307           0 :     }
     308           0 : }
     309             : 
     310           0 : sal_Bool OFileAccess::isFolder( const OUString& FileURL )
     311             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     312             : {
     313           0 :     sal_Bool bRet = sal_False;
     314             :     try
     315             :     {
     316           0 :         INetURLObject aURLObj( FileURL, INET_PROT_FILE );
     317           0 :         ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     318           0 :         bRet = aCnt.isFolder();
     319             :     }
     320           0 :     catch (const Exception &) {}
     321           0 :     return bRet;
     322             : }
     323             : 
     324           0 : sal_Bool OFileAccess::isReadOnly( const OUString& FileURL )
     325             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     326             : {
     327           0 :     INetURLObject aURLObj( FileURL, INET_PROT_FILE );
     328           0 :     ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     329           0 :     Any aRetAny = aCnt.getPropertyValue("IsReadOnly");
     330           0 :     sal_Bool bRet = sal_False;
     331           0 :     aRetAny >>= bRet;
     332           0 :     return bRet;
     333             : }
     334             : 
     335           0 : void OFileAccess::setReadOnly( const OUString& FileURL, sal_Bool bReadOnly )
     336             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     337             : {
     338           0 :     INetURLObject aURLObj( FileURL, INET_PROT_FILE );
     339           0 :     ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     340           0 :     Any aAny;
     341           0 :     aAny <<= bReadOnly;
     342           0 :     aCnt.setPropertyValue("IsReadOnly", aAny );
     343           0 : }
     344             : 
     345           0 : void OFileAccess::createFolder( const OUString& NewFolderURL )
     346             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     347             : {
     348             :     // Does the folder already exist?
     349           0 :     if( NewFolderURL.isEmpty() || isFolder( NewFolderURL ) )
     350           0 :         return;
     351             : 
     352             :     // SfxContentHelper::MakeFolder
     353           0 :     INetURLObject aURL( NewFolderURL, INET_PROT_FILE );
     354           0 :     OUString aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
     355           0 :     if ( !aTitle.isEmpty() )
     356             :     {
     357           0 :         aURL.removeSegment();
     358             : 
     359             :         // Does the base folder exist? Otherwise create it first
     360           0 :         OUString aBaseFolderURLStr = aURL.GetMainURL( INetURLObject::NO_DECODE );
     361           0 :         if( !isFolder( aBaseFolderURLStr ) )
     362             :         {
     363           0 :             createFolder( aBaseFolderURLStr );
     364           0 :         }
     365             :     }
     366             : 
     367           0 :     ucbhelper::Content aCnt( aURL.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     368             : 
     369           0 :     Sequence< ContentInfo > aInfo = aCnt.queryCreatableContentsInfo();
     370           0 :     sal_Int32 nCount = aInfo.getLength();
     371           0 :     if ( nCount == 0 )
     372           0 :         return;
     373             : 
     374           0 :     for ( sal_Int32 i = 0; i < nCount; ++i )
     375             :     {
     376             :         // Simply look for the first KIND_FOLDER...
     377           0 :         const ContentInfo & rCurr = aInfo[i];
     378           0 :         if ( rCurr.Attributes & ContentInfoAttribute::KIND_FOLDER )
     379             :         {
     380             :             // Make sure the only required bootstrap property is "Title",
     381           0 :             const Sequence< Property > & rProps = rCurr.Properties;
     382           0 :             if ( rProps.getLength() != 1 )
     383           0 :                 continue;
     384             : 
     385           0 :             if ( rProps[ 0 ].Name != "Title" )
     386           0 :                 continue;
     387             : 
     388           0 :             Sequence<OUString> aNames(1);
     389           0 :             OUString* pNames = aNames.getArray();
     390           0 :             pNames[0] = "Title";
     391           0 :             Sequence< Any > aValues(1);
     392           0 :             Any* pValues = aValues.getArray();
     393           0 :             pValues[0] = makeAny( OUString( aTitle ) );
     394             : 
     395           0 :             ucbhelper::Content aNew;
     396             :             try
     397             :             {
     398           0 :                 if ( !aCnt.insertNewContent( rCurr.Type, aNames, aValues, aNew ) )
     399           0 :                     continue;
     400             : 
     401             :                 // Success. We're done.
     402           0 :                 return;
     403             :             }
     404           0 :             catch ( ::com::sun::star::ucb::CommandFailedException const & )
     405             :             {
     406             :                 // Interaction Handler already handled the error that has occurred...
     407           0 :                 continue;
     408           0 :             }
     409             :         }
     410           0 :     }
     411             : }
     412             : 
     413           0 : sal_Int32 OFileAccess::getSize( const OUString& FileURL )
     414             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     415             : {
     416             :     // SfxContentHelper::GetSize
     417           0 :     sal_Int32 nSize = 0;
     418           0 :     sal_Int64 nTemp = 0;
     419           0 :     INetURLObject aObj( FileURL, INET_PROT_FILE );
     420           0 :     ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     421           0 :     aCnt.getPropertyValue( "Size" ) >>= nTemp;
     422           0 :     nSize = (sal_Int32)nTemp;
     423           0 :     return nSize;
     424             : }
     425             : 
     426           0 : OUString OFileAccess::getContentType( const OUString& FileURL )
     427             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     428             : {
     429           0 :     INetURLObject aObj( FileURL, INET_PROT_FILE );
     430           0 :     ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     431             : 
     432           0 :     Reference< XContent > xContent = aCnt.get();
     433           0 :     OUString aTypeStr = xContent->getContentType();
     434           0 :     return aTypeStr;
     435             : }
     436             : 
     437           0 : DateTime OFileAccess::getDateTimeModified( const OUString& FileURL )
     438             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     439             : {
     440           0 :     INetURLObject aFileObj( FileURL, INET_PROT_FILE );
     441           0 :     DateTime aDateTime;
     442             : 
     443           0 :     Reference< XCommandEnvironment > aCmdEnv;
     444           0 :     ucbhelper::Content aYoung( aFileObj.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv, comphelper::getProcessComponentContext() );
     445           0 :     aYoung.getPropertyValue("DateModified") >>= aDateTime;
     446           0 :     return aDateTime;
     447             : }
     448             : 
     449             : typedef vector< OUString* > StringList_Impl;
     450             : 
     451           0 : Sequence< OUString > OFileAccess::getFolderContents( const OUString& FolderURL, sal_Bool bIncludeFolders )
     452             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     453             : {
     454             :     // SfxContentHelper::GetFolderContents
     455             : 
     456           0 :     StringList_Impl* pFiles = NULL;
     457           0 :     INetURLObject aFolderObj( FolderURL, INET_PROT_FILE );
     458             : 
     459           0 :     ucbhelper::Content aCnt( aFolderObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     460           0 :     Reference< XResultSet > xResultSet;
     461           0 :     Sequence< OUString > aProps(0);
     462             : 
     463           0 :     ucbhelper::ResultSetInclude eInclude = bIncludeFolders ? ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ucbhelper::INCLUDE_DOCUMENTS_ONLY;
     464             : 
     465             :     try
     466             :     {
     467           0 :         xResultSet = aCnt.createCursor( aProps, eInclude );
     468             :     }
     469           0 :     catch ( ::com::sun::star::ucb::CommandFailedException const & )
     470             :     {
     471             :         // Interaction Handler already handled the error that has occurred...
     472             :     }
     473             : 
     474           0 :     if ( xResultSet.is() )
     475             :     {
     476           0 :         pFiles = new StringList_Impl();
     477           0 :         Reference< com::sun::star::ucb::XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
     478             : 
     479           0 :         while ( xResultSet->next() )
     480             :         {
     481           0 :             OUString aId = xContentAccess->queryContentIdentifierString();
     482           0 :             INetURLObject aURL( aId, INET_PROT_FILE );
     483           0 :             OUString* pFile = new OUString( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
     484           0 :             pFiles->push_back( pFile );
     485           0 :         }
     486             :     }
     487             : 
     488           0 :     if ( pFiles )
     489             :     {
     490           0 :         size_t nCount = pFiles->size();
     491           0 :         Sequence < OUString > aRet( nCount );
     492           0 :         OUString* pRet = aRet.getArray();
     493           0 :         for ( size_t i = 0; i < nCount; ++i )
     494             :         {
     495           0 :             OUString* pFile = pFiles->at( i );
     496           0 :             pRet[i] = *( pFile );
     497           0 :             delete pFile;
     498             :         }
     499           0 :         pFiles->clear();
     500           0 :         delete pFiles;
     501           0 :         return aRet;
     502             :     }
     503             :     else
     504           0 :         return Sequence < OUString > ();
     505             : }
     506             : 
     507           0 : sal_Bool OFileAccess::exists( const OUString& FileURL )
     508             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     509             : {
     510           0 :     sal_Bool bRet = sal_False;
     511             :     try
     512             :     {
     513           0 :         bRet = isFolder( FileURL );
     514           0 :         if( !bRet )
     515             :         {
     516           0 :             Reference< XInputStream > xStream = openFileRead( FileURL );
     517           0 :             bRet = xStream.is();
     518           0 :             if( bRet )
     519           0 :                 xStream->closeInput();
     520             :         }
     521             :     }
     522           0 :     catch (const Exception &) {}
     523           0 :     return bRet;
     524             : }
     525             : 
     526           0 : Reference< XInputStream > OFileAccess::openFileRead( const OUString& FileURL )
     527             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     528             : {
     529           0 :     Reference< XInputStream > xRet;
     530           0 :     INetURLObject aObj( FileURL, INET_PROT_FILE );
     531           0 :     ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     532             : 
     533           0 :     Reference< XActiveDataSink > xSink = (XActiveDataSink*)(new OActiveDataSink());
     534             : 
     535             :     try
     536             :     {
     537           0 :         sal_Bool bRet = aCnt.openStream( xSink );
     538           0 :         if( bRet )
     539           0 :             xRet = xSink->getInputStream();
     540             :     }
     541           0 :     catch ( ::com::sun::star::ucb::CommandFailedException const & )
     542             :     {
     543             :         // Interaction Handler already handled the error that has occurred...
     544             :     }
     545             : 
     546           0 :     return xRet;
     547             : }
     548             : 
     549           0 : Reference< XOutputStream > OFileAccess::openFileWrite( const OUString& FileURL )
     550             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     551             : {
     552           0 :     Reference< XOutputStream > xRet;
     553           0 :     Reference< XStream > xStream = OFileAccess::openFileReadWrite( FileURL );
     554           0 :     if( xStream.is() )
     555           0 :         xRet = xStream->getOutputStream();
     556           0 :     return xRet;
     557             : }
     558             : 
     559           0 : Reference< XStream > OFileAccess::openFileReadWrite( const OUString& FileURL )
     560             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     561             : {
     562           0 :     Reference< XActiveDataStreamer > xSink = (XActiveDataStreamer*)new OActiveDataStreamer();
     563           0 :     Reference< XInterface > xSinkIface = Reference< XInterface >::query( xSink );
     564             : 
     565           0 :     OpenCommandArgument2 aArg;
     566           0 :     aArg.Mode       = OpenMode::DOCUMENT;
     567           0 :     aArg.Priority   = 0; // unused
     568           0 :     aArg.Sink       = xSink;
     569           0 :     aArg.Properties = Sequence< Property >( 0 ); // unused
     570             : 
     571           0 :     Any aCmdArg;
     572           0 :     aCmdArg <<= aArg;
     573             : 
     574           0 :     INetURLObject aFileObj( FileURL, INET_PROT_FILE );
     575           0 :     ucbhelper::Content aCnt( aFileObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     576             : 
     577             :     // Be silent...
     578           0 :     Reference< XInteractionHandler > xIH;
     579           0 :     if ( mpEnvironment )
     580             :     {
     581           0 :         xIH = mpEnvironment->getInteractionHandler();
     582           0 :         mpEnvironment->setHandler( 0 );
     583             :     }
     584             : 
     585             :     try
     586             :     {
     587           0 :         aCnt.executeCommand( OUString("open" ), aCmdArg );
     588             :     }
     589           0 :     catch ( InteractiveIOException const & e )
     590             :     {
     591           0 :         if ( xIH.is() && mpEnvironment )
     592           0 :             mpEnvironment->setHandler( xIH );
     593             : 
     594           0 :         if ( e.Code == IOErrorCode_NOT_EXISTING )
     595             :         {
     596             :             // Create file...
     597           0 :             SvMemoryStream aStream(0,0);
     598           0 :             ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
     599           0 :             Reference< XInputStream > xInput( pInput );
     600           0 :             InsertCommandArgument aInsertArg;
     601           0 :             aInsertArg.Data = xInput;
     602           0 :             aInsertArg.ReplaceExisting = sal_False;
     603             : 
     604           0 :             aCmdArg <<= aInsertArg;
     605           0 :             aCnt.executeCommand( OUString("insert" ), aCmdArg );
     606             : 
     607             :             // Retry...
     608           0 :             return openFileReadWrite( FileURL );
     609             :         }
     610             : 
     611           0 :         throw;
     612             :     }
     613             : 
     614           0 :     if ( xIH.is() && mpEnvironment )
     615           0 :         mpEnvironment->setHandler( xIH );
     616             : 
     617           0 :     Reference< XStream > xRet = xSink->getStream();
     618           0 :     return xRet;
     619             : }
     620             : 
     621           0 : void OFileAccess::setInteractionHandler( const Reference< XInteractionHandler >& Handler )
     622             :     throw(RuntimeException, std::exception)
     623             : {
     624           0 :     if( !mpEnvironment )
     625             :     {
     626           0 :         mpEnvironment = new OCommandEnvironment();
     627           0 :         mxEnvironment = (XCommandEnvironment*)mpEnvironment;
     628             :     }
     629           0 :     mpEnvironment->setHandler( Handler );
     630           0 : }
     631             : 
     632           0 : bool OFileAccess::createNewFile( const OUString & rParentURL,
     633             :                                  const OUString & rTitle,
     634             :                                  const Reference< XInputStream >& data )
     635             :     throw ( Exception )
     636             : {
     637           0 :     ucbhelper::Content aParentCnt( rParentURL, mxEnvironment, comphelper::getProcessComponentContext() );
     638             : 
     639           0 :     Sequence< ContentInfo > aInfo = aParentCnt.queryCreatableContentsInfo();
     640           0 :     sal_Int32 nCount = aInfo.getLength();
     641           0 :     if ( nCount == 0 )
     642           0 :         return false;
     643             : 
     644           0 :     for ( sal_Int32 i = 0; i < nCount; ++i )
     645             :     {
     646           0 :         const ContentInfo & rCurr = aInfo[i];
     647           0 :         if ( ( rCurr.Attributes
     648           0 :                & ContentInfoAttribute::KIND_DOCUMENT ) &&
     649             :              ( rCurr.Attributes
     650           0 :                & ContentInfoAttribute::INSERT_WITH_INPUTSTREAM ) )
     651             :         {
     652             :             // Make sure the only required bootstrap property is
     653             :             // "Title",
     654           0 :             const Sequence< Property > & rProps = rCurr.Properties;
     655           0 :             if ( rProps.getLength() != 1 )
     656           0 :                 continue;
     657             : 
     658           0 :             if ( rProps[ 0 ].Name != "Title" )
     659           0 :                 continue;
     660             : 
     661           0 :             Sequence<OUString> aNames(1);
     662           0 :             OUString* pNames = aNames.getArray();
     663           0 :             pNames[0] = "Title";
     664           0 :             Sequence< Any > aValues(1);
     665           0 :             Any* pValues = aValues.getArray();
     666           0 :             pValues[0] = makeAny( OUString( rTitle ) );
     667             : 
     668             :             try
     669             :             {
     670           0 :                 ucbhelper::Content aNew;
     671           0 :                 if ( aParentCnt.insertNewContent(
     672           0 :                          rCurr.Type, aNames, aValues, data, aNew ) )
     673           0 :                     return true; // success.
     674             :                 else
     675           0 :                     continue;
     676             :             }
     677           0 :             catch ( CommandFailedException const & )
     678             :             {
     679             :                 // Interaction Handler already handled the
     680             :                 // error that has occurred...
     681           0 :                 continue;
     682           0 :             }
     683             :         }
     684             :     }
     685             : 
     686           0 :     return false;
     687             : }
     688             : 
     689           0 : void SAL_CALL OFileAccess::writeFile( const OUString& FileURL,
     690             :                                       const Reference< XInputStream >& data )
     691             :     throw ( Exception, RuntimeException, std::exception )
     692             : {
     693           0 :     INetURLObject aURL( FileURL, INET_PROT_FILE );
     694             :     try
     695             :     {
     696             :         ucbhelper::Content aCnt(
     697             :             aURL.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment,
     698           0 :             comphelper::getProcessComponentContext() );
     699             : 
     700             :         try
     701             :         {
     702           0 :             aCnt.writeStream( data, true /* bReplaceExisting */ );
     703             :         }
     704           0 :         catch ( CommandFailedException const & )
     705             :         {
     706             :             // Interaction Handler already handled the error that has occurred...
     707           0 :         }
     708             :     }
     709           0 :     catch ( ContentCreationException const & e )
     710             :     {
     711             :         // Most probably file does not exist. Try to create.
     712           0 :         if ( e.eError == ContentCreationError_CONTENT_CREATION_FAILED )
     713             :         {
     714           0 :             INetURLObject aParentURLObj( aURL );
     715           0 :             if ( aParentURLObj.removeSegment() )
     716             :             {
     717             :                 OUString aParentURL
     718           0 :                     = aParentURLObj.GetMainURL( INetURLObject::NO_DECODE );
     719             : 
     720             :                 // ensure all parent folders exist.
     721           0 :                 createFolder( aParentURL );
     722             : 
     723             :                 // create the new file...
     724             :                 OUString aTitle
     725             :                     = aURL.getName( INetURLObject::LAST_SEGMENT,
     726             :                                     true,
     727           0 :                                     INetURLObject::DECODE_WITH_CHARSET );
     728           0 :                 if ( createNewFile( aParentURL, aTitle, data ) )
     729             :                 {
     730             :                     // success
     731           0 :                     return;
     732           0 :                 }
     733           0 :             }
     734             :         }
     735             : 
     736           0 :         throw;
     737           0 :     }
     738             : }
     739             : 
     740           0 : sal_Bool OFileAccess::isHidden( const OUString& FileURL )
     741             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     742             : {
     743           0 :     INetURLObject aURLObj( FileURL, INET_PROT_FILE );
     744           0 :     ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     745           0 :     Any aRetAny = aCnt.getPropertyValue("IsHidden");
     746           0 :     sal_Bool bRet = sal_False;
     747           0 :     aRetAny >>= bRet;
     748           0 :     return bRet;
     749             : }
     750             : 
     751           0 : void OFileAccess::setHidden( const OUString& FileURL, sal_Bool bHidden )
     752             :     throw(CommandAbortedException, Exception, RuntimeException, std::exception)
     753             : {
     754           0 :     INetURLObject aURLObj( FileURL, INET_PROT_FILE );
     755           0 :     ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
     756           0 :     Any aAny;
     757           0 :     aAny <<= bHidden;
     758           0 :     aCnt.setPropertyValue("IsHidden", aAny );
     759           0 : }
     760             : 
     761             : }; // namespace end
     762             : 
     763           0 : Reference< XInterface > SAL_CALL FileAccess_CreateInstance( const Reference< XMultiServiceFactory > & xSMgr )
     764             : {
     765           0 :     return Reference < XInterface >( ( cppu::OWeakObject * ) new OFileAccess( comphelper::getComponentContext(xSMgr) ) );
     766             : }
     767             : 
     768           0 : Sequence< OUString > FileAccess_getSupportedServiceNames()
     769             : {
     770           0 :     Sequence< OUString > seqNames(1);
     771           0 :     seqNames.getArray()[0] = SERVICE_NAME;
     772           0 :     return seqNames;
     773             : }
     774             : 
     775             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10