Files
libreoffice/unotools/source/ucbhelper/localfilehelper.cxx

256 lines
7.7 KiB
C++
Raw Normal View History

2000-12-01 10:54:53 +00:00
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
2000-12-01 10:54:53 +00:00
*
* $RCSfile: localfilehelper.cxx,v $
2000-12-01 10:54:53 +00:00
*
* $Revision: 1.15 $
2000-12-01 10:54:53 +00:00
*
* last change: $Author: rt $ $Date: 2005-09-09 09:50:32 $
2000-12-01 10:54:53 +00:00
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
2000-12-01 10:54:53 +00:00
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
2000-12-01 10:54:53 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
2000-12-01 10:54:53 +00:00
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
2000-12-01 10:54:53 +00:00
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
2000-12-01 10:54:53 +00:00
*
************************************************************************/
#ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
#include <com/sun/star/sdbc/XResultSet.hpp>
#endif
#ifndef _COM_SUN_STAR_UCB_XCONTENTACCESS_HPP_
#include <com/sun/star/ucb/XContentAccess.hpp>
#endif
#ifndef _COM_SUN_STAR_UCB_COMMANDABORTEDEXCEPTION_HPP_
#include <com/sun/star/ucb/CommandAbortedException.hpp>
#endif
2000-12-01 10:54:53 +00:00
#include <unotools/localfilehelper.hxx>
#include <ucbhelper/fileidentifierconverter.hxx>
#include <ucbhelper/contentbroker.hxx>
#include <rtl/ustring.hxx>
#include <osl/file.hxx>
#include <tools/debug.hxx>
#include <tools/list.hxx>
2001-07-25 09:10:31 +00:00
#include <tools/urlobj.hxx>
2000-12-01 10:54:53 +00:00
#include <ucbhelper/content.hxx>
2001-07-25 09:10:31 +00:00
using namespace ::osl;
2000-12-01 10:54:53 +00:00
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::ucb;
namespace utl
{
sal_Bool LocalFileHelper::ConvertSystemPathToURL( const String& rName, const String& rBaseURL, String& rReturn )
{
2001-07-25 09:10:31 +00:00
rReturn = ::rtl::OUString();
::ucb::ContentBroker* pBroker = ::ucb::ContentBroker::get();
if ( !pBroker )
{
2001-07-25 09:10:31 +00:00
rtl::OUString aRet;
if ( FileBase::getFileURLFromSystemPath( rName, aRet ) == FileBase::E_None )
rReturn = aRet;
}
else
{
::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
pBroker->getContentProviderManagerInterface();
try
{
2001-07-25 09:10:31 +00:00
rReturn = ::ucb::getFileURLFromSystemPath( xManager, rBaseURL, rName );
}
catch ( ::com::sun::star::uno::RuntimeException& )
{
2001-07-18 10:11:27 +00:00
return sal_False;
}
}
2001-07-25 09:10:31 +00:00
return ( rReturn.Len() != 0 );
}
sal_Bool LocalFileHelper::ConvertURLToSystemPath( const String& rName, String& rReturn )
{
2001-07-25 09:10:31 +00:00
rReturn = ::rtl::OUString();
::ucb::ContentBroker* pBroker = ::ucb::ContentBroker::get();
if ( !pBroker )
{
2001-07-25 09:10:31 +00:00
rtl::OUString aRet;
if( FileBase::getSystemPathFromFileURL( rName, aRet ) == FileBase::E_None )
rReturn = aRet;
}
else
{
::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
pBroker->getContentProviderManagerInterface();
try
{
2001-07-25 09:10:31 +00:00
rReturn = ::ucb::getSystemPathFromFileURL( xManager, rName );
}
catch ( ::com::sun::star::uno::RuntimeException& )
{
}
}
2001-07-25 09:10:31 +00:00
return ( rReturn.Len() != 0 );
}
2000-12-01 10:54:53 +00:00
sal_Bool LocalFileHelper::ConvertPhysicalNameToURL( const String& rName, String& rReturn )
{
2001-07-25 09:10:31 +00:00
rReturn = ::rtl::OUString();
2000-12-01 10:54:53 +00:00
::ucb::ContentBroker* pBroker = ::ucb::ContentBroker::get();
2000-12-14 16:13:34 +00:00
if ( !pBroker )
2000-12-01 10:54:53 +00:00
{
2001-07-25 09:10:31 +00:00
rtl::OUString aRet;
if ( FileBase::getFileURLFromSystemPath( rName, aRet ) == FileBase::E_None )
rReturn = aRet;
2000-12-01 10:54:53 +00:00
}
else
{
::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
pBroker->getContentProviderManagerInterface();
try
{
rtl::OUString aBase( ::ucb::getLocalFileURL( xManager ) );
2001-07-25 09:10:31 +00:00
rReturn = ::ucb::getFileURLFromSystemPath( xManager, aBase, rName );
}
catch ( ::com::sun::star::uno::RuntimeException& )
{
}
2000-12-01 10:54:53 +00:00
}
2001-07-25 09:10:31 +00:00
return ( rReturn.Len() != 0 );
2000-12-01 10:54:53 +00:00
}
sal_Bool LocalFileHelper::ConvertURLToPhysicalName( const String& rName, String& rReturn )
{
2001-07-25 09:10:31 +00:00
rReturn = ::rtl::OUString();
2000-12-01 10:54:53 +00:00
::ucb::ContentBroker* pBroker = ::ucb::ContentBroker::get();
2000-12-14 16:13:34 +00:00
if ( !pBroker )
2000-12-01 10:54:53 +00:00
{
2001-07-25 09:10:31 +00:00
::rtl::OUString aRet;
if ( FileBase::getSystemPathFromFileURL( rName, aRet ) == FileBase::E_None )
rReturn = aRet;
2000-12-01 10:54:53 +00:00
}
else
{
::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
pBroker->getContentProviderManagerInterface();
try
{
2001-07-25 09:10:31 +00:00
INetURLObject aObj( rName );
INetURLObject aLocal( ::ucb::getLocalFileURL( xManager ) );
if ( aObj.GetProtocol() == aLocal.GetProtocol() )
rReturn = ::ucb::getSystemPathFromFileURL( xManager, rName );
}
catch ( ::com::sun::star::uno::RuntimeException& )
{
}
2000-12-01 10:54:53 +00:00
}
2001-07-25 09:10:31 +00:00
return ( rReturn.Len() != 0 );
2000-12-01 10:54:53 +00:00
}
sal_Bool LocalFileHelper::IsLocalFile( const String& rName )
{
String aTmp;
2001-07-18 10:11:27 +00:00
return ConvertURLToPhysicalName( rName, aTmp );
2000-12-01 10:54:53 +00:00
}
sal_Bool LocalFileHelper::IsFileContent( const String& rName )
{
String aTmp;
return ConvertURLToSystemPath( rName, aTmp );
}
2000-12-04 08:40:31 +00:00
DECLARE_LIST( StringList_Impl, ::rtl::OUString* )
2000-12-01 10:54:53 +00:00
::com::sun::star::uno::Sequence < ::rtl::OUString > LocalFileHelper::GetFolderContents( const ::rtl::OUString& rFolder, sal_Bool bFolder )
{
StringList_Impl* pFiles = NULL;
try
{
::ucb::Content aCnt( rFolder, Reference< XCommandEnvironment > () );
Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
::com::sun::star::uno::Sequence< ::rtl::OUString > aProps(1);
::rtl::OUString* pProps = aProps.getArray();
pProps[0] == ::rtl::OUString::createFromAscii( "Url" );
try
{
::ucb::ResultSetInclude eInclude = bFolder ? ::ucb::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucb::INCLUDE_DOCUMENTS_ONLY;
xResultSet = aCnt.createCursor( aProps, eInclude );
}
catch( ::com::sun::star::ucb::CommandAbortedException& )
{
}
catch( Exception& )
2000-12-01 10:54:53 +00:00
{
}
if ( xResultSet.is() )
{
pFiles = new StringList_Impl;
Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
try
{
while ( xResultSet->next() )
{
::rtl::OUString aId = xContentAccess->queryContentIdentifierString();
::rtl::OUString* pFile = new ::rtl::OUString( aId );
pFiles->Insert( pFile, LIST_APPEND );
}
}
catch( ::com::sun::star::ucb::CommandAbortedException& )
{
}
catch( Exception& )
2000-12-01 10:54:53 +00:00
{
}
}
}
catch( Exception& )
2000-12-01 10:54:53 +00:00
{
}
if ( pFiles )
{
ULONG nCount = pFiles->Count();
Sequence < ::rtl::OUString > aRet( nCount );
::rtl::OUString* pRet = aRet.getArray();
for ( USHORT i = 0; i < nCount; ++i )
{
::rtl::OUString* pFile = pFiles->GetObject(i);
pRet[i] = *( pFile );
delete pFile;
}
delete pFiles;
return aRet;
}
else
return Sequence < ::rtl::OUString > ();
}
};