32#include <com/sun/star/beans/Property.hpp>
33#include <com/sun/star/container/XChild.hpp>
34#include <com/sun/star/io/XActiveDataSink.hpp>
35#include <com/sun/star/io/XActiveDataStreamer.hpp>
36#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
37#include <com/sun/star/lang/XServiceInfo.hpp>
38#include <com/sun/star/sdbc/XResultSet.hpp>
39#include <com/sun/star/ucb/ContentCreationException.hpp>
40#include <com/sun/star/ucb/CommandFailedException.hpp>
41#include <com/sun/star/ucb/ContentInfo.hpp>
42#include <com/sun/star/ucb/ContentInfoAttribute.hpp>
43#include <com/sun/star/ucb/InsertCommandArgument.hpp>
44#include <com/sun/star/ucb/InteractiveIOException.hpp>
45#include <com/sun/star/ucb/NameClash.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>
56constexpr OUStringLiteral
SERVICE_NAME =
u"com.sun.star.ucb.SimpleFileAccess";
75typedef cppu::WeakImplHelper<XSimpleFileAccess3, css::lang::XServiceInfo>
77class OCommandEnvironment;
79class OFileAccess :
public FileAccessHelper
87 void transferImpl(
const OUString& rSource, std::u16string_view rDest,
bool bMoveData );
89 bool createNewFile(
const OUString & rParentURL,
90 const OUString & rTitle,
91 const Reference< XInputStream >& data );
94 explicit OFileAccess(
const Reference< XComponentContext > & xContext )
97 virtual void SAL_CALL
copy(
const OUString& SourceURL,
const OUString& DestURL )
override;
98 virtual void SAL_CALL move(
const OUString& SourceURL,
const OUString& DestURL )
override;
99 virtual void SAL_CALL kill(
const OUString& FileURL )
override;
100 virtual sal_Bool SAL_CALL isFolder(
const OUString& FileURL )
override;
101 virtual sal_Bool SAL_CALL isReadOnly(
const OUString& FileURL )
override;
102 virtual void SAL_CALL setReadOnly(
const OUString& FileURL,
sal_Bool bReadOnly )
override;
103 virtual void SAL_CALL createFolder(
const OUString& NewFolderURL )
override;
104 virtual sal_Int32 SAL_CALL getSize(
const OUString& FileURL )
override;
105 virtual OUString SAL_CALL getContentType(
const OUString& FileURL )
override;
106 virtual css::util::DateTime SAL_CALL getDateTimeModified(
const OUString& FileURL )
override;
107 virtual css::uno::Sequence< OUString > SAL_CALL getFolderContents(
const OUString& FolderURL,
sal_Bool bIncludeFolders )
override;
108 virtual sal_Bool SAL_CALL exists(
const OUString& FileURL )
override;
109 virtual css::uno::Reference< css::io::XInputStream > SAL_CALL openFileRead(
const OUString& FileURL )
override;
110 virtual css::uno::Reference< css::io::XOutputStream > SAL_CALL openFileWrite(
const OUString& FileURL )
override;
111 virtual css::uno::Reference< css::io::XStream > SAL_CALL openFileReadWrite(
const OUString& FileURL )
override;
112 virtual void SAL_CALL setInteractionHandler(
const css::uno::Reference< css::task::XInteractionHandler >& Handler )
override;
113 virtual void SAL_CALL writeFile(
const OUString& FileURL,
const css::uno::Reference< css::io::XInputStream >& data )
override;
114 virtual sal_Bool SAL_CALL isHidden(
const OUString& FileURL )
override;
115 virtual void SAL_CALL setHidden(
const OUString& FileURL,
sal_Bool bHidden )
override;
118 {
return "com.sun.star.comp.ucb.SimpleFileAccess"; }
129class OActiveDataSink :
public cppu::WeakImplHelper< XActiveDataSink >
131 Reference< XInputStream > mxStream;
136 virtual void SAL_CALL setInputStream(
const Reference< XInputStream >& aStream )
override;
137 virtual Reference< XInputStream > SAL_CALL getInputStream( )
override;
142class OActiveDataStreamer :
public cppu::WeakImplHelper< XActiveDataStreamer >
144 Reference< XStream > mxStream;
149 virtual void SAL_CALL setStream(
const Reference< XStream >& aStream )
override;
150 virtual Reference< XStream > SAL_CALL getStream()
override;
155class OCommandEnvironment :
public cppu::WeakImplHelper< XCommandEnvironment >
160 void setHandler(
const Reference< XInteractionHandler >& xInteraction_ )
166 virtual Reference< XInteractionHandler > SAL_CALL getInteractionHandler()
override;
167 virtual Reference< XProgressHandler > SAL_CALL getProgressHandler()
override;
170void OActiveDataSink::setInputStream(
const Reference< XInputStream >& aStream )
175Reference< XInputStream > OActiveDataSink::getInputStream()
180void OActiveDataStreamer::setStream(
const Reference< XStream >& aStream )
185Reference< XStream > OActiveDataStreamer::getStream()
190Reference< XInteractionHandler > OCommandEnvironment::getInteractionHandler()
195Reference< XProgressHandler > OCommandEnvironment::getProgressHandler()
197 Reference< XProgressHandler > xRet;
201void OFileAccess::transferImpl(
const OUString& rSource,
202 std::u16string_view rDest,
226 if ( aDestObj.
GetProtocol() == INetProtocol::VndSunStarExpand )
233 Reference< XMacroExpander > xExpander = theMacroExpander::get(
m_xContext);
235 aDestURL = xExpander->expandMacros(
241 throw css::lang::WrappedTargetRuntimeException(
242 "OFileAccess::transferrImpl - Unable to obtain destination folder URL!",
246 transferImpl( rSource, aDestURL, bMoveData );
251 "OFileAccess::transferrImpl - Unable to obtain destination folder URL!",
266 css::ucb::NameClash::OVERWRITE);
268 catch ( css::ucb::CommandFailedException
const & )
274void OFileAccess::copy(
const OUString& SourceURL,
const OUString& DestURL )
276 transferImpl( SourceURL, DestURL,
false );
279void OFileAccess::move(
const OUString& SourceURL,
const OUString& DestURL )
281 transferImpl( SourceURL, DestURL,
true );
284void OFileAccess::kill(
const OUString& FileURL )
293 catch ( css::ucb::CommandFailedException
const & )
299sal_Bool OFileAccess::isFolder(
const OUString& FileURL )
312sal_Bool OFileAccess::isReadOnly(
const OUString& FileURL )
329void OFileAccess::createFolder(
const OUString& NewFolderURL )
332 if( NewFolderURL.isEmpty() || isFolder( NewFolderURL ) )
338 if ( !aTitle.isEmpty() )
340 aURL.removeSegment();
344 if( !isFolder( aBaseFolderURLStr ) )
346 createFolder( aBaseFolderURLStr );
357 if ( rCurr.Attributes & ContentInfoAttribute::KIND_FOLDER )
360 const Sequence< Property > & rProps = rCurr.Properties;
361 if ( rProps.getLength() != 1 )
364 if ( rProps[ 0 ].
Name !=
"Title" )
370 if ( !aCnt.
insertNewContent( rCurr.Type, {
"Title" }, { Any(aTitle) }, aNew ) )
376 catch ( css::ucb::CommandFailedException
const & )
385sal_Int32 OFileAccess::getSize(
const OUString& FileURL )
393 nSize =
static_cast<sal_Int32
>(nTemp);
397OUString OFileAccess::getContentType(
const OUString& FileURL )
402 Reference< XContent > xContent = aCnt.
get();
403 OUString aTypeStr = xContent->getContentType();
407css::util::DateTime OFileAccess::getDateTimeModified(
const OUString& FileURL )
410 css::util::DateTime aDateTime;
412 Reference< XCommandEnvironment > aCmdEnv;
418Sequence< OUString > OFileAccess::getFolderContents(
const OUString& FolderURL,
sal_Bool bIncludeFolders )
422 std::vector<OUString> aFiles;
426 Reference< XResultSet > xResultSet;
434 catch ( css::ucb::CommandFailedException
const & )
439 if ( xResultSet.is() )
441 Reference< css::ucb::XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
443 while ( xResultSet->next() )
445 OUString aId = xContentAccess->queryContentIdentifierString();
454sal_Bool OFileAccess::exists(
const OUString& FileURL )
459 bRet = isFolder( FileURL );
462 Reference< XInputStream >
xStream = openFileRead( FileURL );
472Reference< XInputStream > OFileAccess::openFileRead(
const OUString& FileURL )
474 Reference< XInputStream > xRet;
478 Reference<XActiveDataSink>
xSink =
new OActiveDataSink;
484 xRet =
xSink->getInputStream();
486 catch ( css::ucb::CommandFailedException
const & )
494Reference< XOutputStream > OFileAccess::openFileWrite(
const OUString& FileURL )
496 Reference< XOutputStream > xRet;
497 Reference< XStream >
xStream = OFileAccess::openFileReadWrite( FileURL );
499 xRet =
xStream->getOutputStream();
503Reference< XStream > OFileAccess::openFileReadWrite(
const OUString& FileURL )
505 Reference<XActiveDataStreamer>
xSink =
new OActiveDataStreamer;
507 OpenCommandArgument2 aArg;
511 aArg.Properties = Sequence< Property >( 0 );
520 Reference< XInteractionHandler > xIH;
521 if ( mxEnvironment.is() )
523 xIH = mxEnvironment->getInteractionHandler();
524 mxEnvironment->setHandler(
nullptr );
531 catch ( InteractiveIOException
const & e )
533 if ( xIH.is() && mxEnvironment.is() )
534 mxEnvironment->setHandler( xIH );
536 if ( e.Code == IOErrorCode_NOT_EXISTING )
541 InsertCommandArgument aInsertArg;
542 aInsertArg.Data = pInput;
543 aInsertArg.ReplaceExisting =
false;
545 aCmdArg <<= aInsertArg;
549 return openFileReadWrite( FileURL );
555 if ( xIH.is() && mxEnvironment.is() )
556 mxEnvironment->setHandler( xIH );
558 Reference< XStream > xRet =
xSink->getStream();
562void OFileAccess::setInteractionHandler(
const Reference< XInteractionHandler >& Handler )
564 if( !mxEnvironment.is() )
566 mxEnvironment =
new OCommandEnvironment;
568 mxEnvironment->setHandler( Handler );
571bool OFileAccess::createNewFile(
const OUString & rParentURL,
572 const OUString & rTitle,
573 const Reference< XInputStream >& data )
581 if ( ( rCurr.Attributes
582 & ContentInfoAttribute::KIND_DOCUMENT ) &&
584 & ContentInfoAttribute::INSERT_WITH_INPUTSTREAM ) )
588 const Sequence< Property > & rProps = rCurr.Properties;
589 if ( rProps.getLength() != 1 )
592 if ( rProps[ 0 ].
Name !=
"Title" )
599 rCurr.Type, {
"Title" }, { Any(rTitle) }, data, aNew ) )
604 catch ( CommandFailedException
const & )
616void SAL_CALL OFileAccess::writeFile(
const OUString& FileURL,
617 const Reference< XInputStream >& data )
630 catch ( CommandFailedException
const & )
635 catch ( ContentCreationException
const & e )
638 if ( e.eError == ContentCreationError_CONTENT_CREATION_FAILED )
647 createFolder( aParentURL );
654 if ( createNewFile( aParentURL, aTitle, data ) )
666sal_Bool OFileAccess::isHidden(
const OUString& FileURL )
676void OFileAccess::setHidden(
const OUString& FileURL,
sal_Bool bHidden )
684extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
685ucb_OFileAccess_get_implementation(
686 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any>
const&)
688 return cppu::acquire(
new OFileAccess(context));
constexpr OUStringLiteral SERVICE_NAME
Reference< XComponentContext > m_xContext
OUString getName(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
bool removeSegment(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true)
OUString GetURLPath(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
INetProtocol GetProtocol() const
css::uno::Any setPropertyValue(const OUString &rPropertyName, const css::uno::Any &rValue)
void transferContent(const Content &rSourceContent, InsertOperation eOperation, const OUString &rTitle, const sal_Int32 nNameClashAction, const OUString &rMimeType=OUString(), bool bMajorVersion=false, const OUString &rCommentVersion=OUString(), OUString *pResultURL=nullptr, const OUString &rDocumentId=OUString()) const
css::uno::Any getPropertyValue(const OUString &rPropertyName)
css::uno::Any executeCommand(const OUString &rCommandName, const css::uno::Any &rCommandArgument)
bool insertNewContent(const OUString &rContentType, const css::uno::Sequence< OUString > &rPropertyNames, const css::uno::Sequence< css::uno::Any > &rPropertyValues, Content &rNewContent)
css::uno::Sequence< css::ucb::ContentInfo > queryCreatableContentsInfo()
css::uno::Reference< css::ucb::XContent > get() const
css::uno::Reference< css::io::XInputStream > openStream()
css::uno::Reference< css::sdbc::XResultSet > createCursor(const css::uno::Sequence< OUString > &rPropertyNames, ResultSetInclude eMode=INCLUDE_FOLDERS_AND_DOCUMENTS)
void writeStream(const css::uno::Reference< css::io::XInputStream > &rStream, bool bReplaceExisting)
tools::SvRef< SvBaseLink > xSink
Reference< task::XInteractionHandler > mxInteraction
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Reference< XComponentContext > getProcessComponentContext()
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Any SAL_CALL getCaughtException()
void copy(const fs::path &src, const fs::path &dest)
INCLUDE_FOLDERS_AND_DOCUMENTS