bump product version to 5.0.4.1
[LibreOffice.git] / ucb / source / core / FileAccess.cxx
blob543f00a716211a6dfa09e9355bedf50fd3361d1a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <osl/mutex.hxx>
21 #include <osl/diagnose.h>
23 #include <uno/mapping.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <cppuhelper/factory.hxx>
26 #include <cppuhelper/implbase.hxx>
27 #include <cppuhelper/supportsservice.hxx>
29 #include <tools/urlobj.hxx>
30 #include <ucbhelper/content.hxx>
31 #include <unotools/streamwrap.hxx>
32 #include <tools/stream.hxx>
34 #include <com/sun/star/beans/Property.hpp>
35 #include <com/sun/star/container/XChild.hpp>
36 #include <com/sun/star/io/XActiveDataSink.hpp>
37 #include <com/sun/star/io/XActiveDataSource.hpp>
38 #include <com/sun/star/io/XActiveDataStreamer.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <com/sun/star/sdbc/XResultSet.hpp>
41 #include <com/sun/star/ucb/CommandFailedException.hpp>
42 #include <com/sun/star/ucb/ContentInfo.hpp>
43 #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
44 #include <com/sun/star/ucb/InsertCommandArgument.hpp>
45 #include <com/sun/star/ucb/InteractiveIOException.hpp>
46 #include <com/sun/star/ucb/NameClash.hpp>
47 #include <com/sun/star/ucb/NameClashException.hpp>
48 #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
49 #include <com/sun/star/ucb/OpenMode.hpp>
50 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
51 #include <com/sun/star/ucb/XContent.hpp>
52 #include <com/sun/star/ucb/XContentAccess.hpp>
53 #include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
54 #include <com/sun/star/util/theMacroExpander.hpp>
56 #include <vector>
58 #include "FileAccess.hxx"
60 #define SERVICE_NAME "com.sun.star.ucb.SimpleFileAccess"
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::lang;
64 using namespace ::com::sun::star::io;
65 using namespace ::com::sun::star::ucb;
66 using namespace ::com::sun::star::sdbc;
67 using namespace ::com::sun::star::task;
68 using namespace ::com::sun::star::util;
69 using namespace ::com::sun::star::beans;
70 using namespace ::com::sun::star::registry;
71 using namespace ::com::sun::star::container;
73 using ::std::vector;
75 namespace
78 // Implementation XSimpleFileAccess
79 typedef cppu::WeakImplHelper<XSimpleFileAccess3, css::lang::XServiceInfo>
80 FileAccessHelper;
81 class OCommandEnvironment;
83 class OFileAccess : public FileAccessHelper
85 Reference< XComponentContext > m_xContext;
86 Reference< XCommandEnvironment > mxEnvironment;
87 OCommandEnvironment* mpEnvironment;
89 void transferImpl( const OUString& rSource, const OUString& rDest, bool bMoveData )
90 throw(CommandAbortedException, Exception, RuntimeException);
91 bool createNewFile( const OUString & rParentURL,
92 const OUString & rTitle,
93 const Reference< XInputStream >& data )
94 throw ( Exception );
96 public:
97 OFileAccess( const Reference< XComponentContext > & xContext )
98 : m_xContext( xContext), mpEnvironment( NULL ) {}
99 // Methods
100 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;
101 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;
102 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;
103 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;
104 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;
105 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;
106 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;
107 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;
108 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;
109 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;
110 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;
111 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;
112 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;
113 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;
114 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;
115 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;
116 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;
117 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;
118 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;
120 OUString SAL_CALL getImplementationName()
121 throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
122 { return OUString(IMPLEMENTATION_NAME); }
124 sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
125 throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
126 { return cppu::supportsService(this, ServiceName); }
128 css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
129 throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
130 { return FileAccess_getSupportedServiceNames(); }
133 // Implementation XActiveDataSink
134 typedef cppu::WeakImplHelper1< XActiveDataSink > ActiveDataSinkHelper;
136 class OActiveDataSink : public ActiveDataSinkHelper
138 Reference< XInputStream > mxStream;
140 public:
142 // Methods
143 virtual void SAL_CALL setInputStream( const Reference< XInputStream >& aStream )
144 throw(RuntimeException, std::exception) SAL_OVERRIDE;
145 virtual Reference< XInputStream > SAL_CALL getInputStream( )
146 throw(RuntimeException, std::exception) SAL_OVERRIDE;
149 // Implementation XActiveDataStreamer
150 typedef cppu::WeakImplHelper1< XActiveDataStreamer > ActiveDataStreamerHelper;
152 class OActiveDataStreamer : public ActiveDataStreamerHelper
154 Reference< XStream > mxStream;
156 public:
158 // Methods
159 virtual void SAL_CALL setStream( const Reference< XStream >& aStream )
160 throw(RuntimeException, std::exception) SAL_OVERRIDE;
161 virtual Reference< XStream > SAL_CALL getStream()
162 throw(RuntimeException, std::exception) SAL_OVERRIDE;
165 // Implementation XCommandEnvironment
166 typedef cppu::WeakImplHelper1< XCommandEnvironment > CommandEnvironmentHelper;
168 class OCommandEnvironment : public CommandEnvironmentHelper
170 Reference< XInteractionHandler > mxInteraction;
172 public:
173 void setHandler( Reference< XInteractionHandler > xInteraction_ )
175 mxInteraction = xInteraction_;
178 // Methods
179 virtual Reference< XInteractionHandler > SAL_CALL getInteractionHandler()
180 throw(RuntimeException, std::exception) SAL_OVERRIDE;
181 virtual Reference< XProgressHandler > SAL_CALL getProgressHandler()
182 throw(RuntimeException, std::exception) SAL_OVERRIDE;
185 void OActiveDataSink::setInputStream( const Reference< XInputStream >& aStream )
186 throw(RuntimeException, std::exception)
188 mxStream = aStream;
191 Reference< XInputStream > OActiveDataSink::getInputStream()
192 throw(RuntimeException, std::exception)
194 return mxStream;
197 void OActiveDataStreamer::setStream( const Reference< XStream >& aStream )
198 throw(RuntimeException, std::exception)
200 mxStream = aStream;
203 Reference< XStream > OActiveDataStreamer::getStream()
204 throw(RuntimeException, std::exception)
206 return mxStream;
209 Reference< XInteractionHandler > OCommandEnvironment::getInteractionHandler()
210 throw(RuntimeException, std::exception)
212 return mxInteraction;
215 Reference< XProgressHandler > OCommandEnvironment::getProgressHandler()
216 throw(RuntimeException, std::exception)
218 Reference< XProgressHandler > xRet;
219 return xRet;
222 void OFileAccess::transferImpl( const OUString& rSource,
223 const OUString& rDest,
224 bool bMoveData )
225 throw(CommandAbortedException, Exception, RuntimeException)
227 // SfxContentHelper::Transfer_Impl
228 INetURLObject aSourceObj( rSource, INetProtocol::File );
229 INetURLObject aDestObj( rDest, INetProtocol::File );
230 OUString aName = aDestObj.getName(
231 INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
232 OUString aDestURL;
233 OUString aSourceURL = aSourceObj.GetMainURL( INetURLObject::NO_DECODE );
234 if ( aDestObj.removeSegment() )
236 // hierarchical URL.
238 aDestObj.setFinalSlash();
239 aDestURL = aDestObj.GetMainURL( INetURLObject::NO_DECODE );
241 else
243 // non-hierarchical URL
245 // #i29648#
248 if ( aDestObj.GetProtocol() == INetProtocol::VndSunStarExpand )
250 // Hack: Expand destination URL using Macro Expander and try again
251 // with the hopefully hierarchical expanded URL...
255 Reference< XMacroExpander > xExpander = theMacroExpander::get(m_xContext);
257 aDestURL = xExpander->expandMacros(
258 aDestObj.GetURLPath( INetURLObject::DECODE_WITH_CHARSET ) );
260 catch ( Exception const & )
262 throw RuntimeException(
263 "OFileAccess::transferrImpl - Unable to obtain destination folder URL!",
264 static_cast< cppu::OWeakObject * >( this ) );
267 transferImpl( rSource, aDestURL, bMoveData );
268 return;
271 throw RuntimeException(
272 "OFileAccess::transferrImpl - Unable to obtain destination folder URL!",
273 static_cast< cppu::OWeakObject * >( this ) );
277 ucbhelper::Content aDestPath( aDestURL, mxEnvironment, comphelper::getProcessComponentContext() );
278 ucbhelper::Content aSrc ( aSourceURL, mxEnvironment, comphelper::getProcessComponentContext() );
282 aDestPath.transferContent( aSrc,
283 bMoveData
284 ? ucbhelper::InsertOperation_MOVE
285 : ucbhelper::InsertOperation_COPY,
286 aName,
287 ::com::sun::star::ucb::NameClash::OVERWRITE );
289 catch ( ::com::sun::star::ucb::CommandFailedException const & )
291 // Interaction Handler already handled the error that has occurred...
295 void OFileAccess::copy( const OUString& SourceURL, const OUString& DestURL )
296 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
298 transferImpl( SourceURL, DestURL, false );
301 void OFileAccess::move( const OUString& SourceURL, const OUString& DestURL )
302 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
304 transferImpl( SourceURL, DestURL, true );
307 void OFileAccess::kill( const OUString& FileURL )
308 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
310 // SfxContentHelper::Kill
311 INetURLObject aDeleteObj( FileURL, INetProtocol::File );
312 ucbhelper::Content aCnt( aDeleteObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
315 aCnt.executeCommand( OUString("delete" ), makeAny( true ) );
317 catch ( ::com::sun::star::ucb::CommandFailedException const & )
319 // Interaction Handler already handled the error that has occurred...
323 sal_Bool OFileAccess::isFolder( const OUString& FileURL )
324 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
326 bool bRet = false;
329 INetURLObject aURLObj( FileURL, INetProtocol::File );
330 ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
331 bRet = aCnt.isFolder();
333 catch (const Exception &) {}
334 return bRet;
337 sal_Bool OFileAccess::isReadOnly( const OUString& FileURL )
338 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
340 INetURLObject aURLObj( FileURL, INetProtocol::File );
341 ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
342 Any aRetAny = aCnt.getPropertyValue("IsReadOnly");
343 bool bRet = false;
344 aRetAny >>= bRet;
345 return bRet;
348 void OFileAccess::setReadOnly( const OUString& FileURL, sal_Bool bReadOnly )
349 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
351 INetURLObject aURLObj( FileURL, INetProtocol::File );
352 ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
353 Any aAny;
354 aAny <<= bReadOnly;
355 aCnt.setPropertyValue("IsReadOnly", aAny );
358 void OFileAccess::createFolder( const OUString& NewFolderURL )
359 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
361 // Does the folder already exist?
362 if( NewFolderURL.isEmpty() || isFolder( NewFolderURL ) )
363 return;
365 // SfxContentHelper::MakeFolder
366 INetURLObject aURL( NewFolderURL, INetProtocol::File );
367 OUString aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
368 if ( !aTitle.isEmpty() )
370 aURL.removeSegment();
372 // Does the base folder exist? Otherwise create it first
373 OUString aBaseFolderURLStr = aURL.GetMainURL( INetURLObject::NO_DECODE );
374 if( !isFolder( aBaseFolderURLStr ) )
376 createFolder( aBaseFolderURLStr );
380 ucbhelper::Content aCnt( aURL.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
382 Sequence< ContentInfo > aInfo = aCnt.queryCreatableContentsInfo();
383 sal_Int32 nCount = aInfo.getLength();
384 if ( nCount == 0 )
385 return;
387 for ( sal_Int32 i = 0; i < nCount; ++i )
389 // Simply look for the first KIND_FOLDER...
390 const ContentInfo & rCurr = aInfo[i];
391 if ( rCurr.Attributes & ContentInfoAttribute::KIND_FOLDER )
393 // Make sure the only required bootstrap property is "Title",
394 const Sequence< Property > & rProps = rCurr.Properties;
395 if ( rProps.getLength() != 1 )
396 continue;
398 if ( rProps[ 0 ].Name != "Title" )
399 continue;
401 Sequence<OUString> aNames(1);
402 OUString* pNames = aNames.getArray();
403 pNames[0] = "Title";
404 Sequence< Any > aValues(1);
405 Any* pValues = aValues.getArray();
406 pValues[0] = makeAny( OUString( aTitle ) );
408 ucbhelper::Content aNew;
411 if ( !aCnt.insertNewContent( rCurr.Type, aNames, aValues, aNew ) )
412 continue;
414 // Success. We're done.
415 return;
417 catch ( ::com::sun::star::ucb::CommandFailedException const & )
419 // Interaction Handler already handled the error that has occurred...
420 continue;
426 sal_Int32 OFileAccess::getSize( const OUString& FileURL )
427 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
429 // SfxContentHelper::GetSize
430 sal_Int32 nSize = 0;
431 sal_Int64 nTemp = 0;
432 INetURLObject aObj( FileURL, INetProtocol::File );
433 ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
434 aCnt.getPropertyValue( "Size" ) >>= nTemp;
435 nSize = (sal_Int32)nTemp;
436 return nSize;
439 OUString OFileAccess::getContentType( const OUString& FileURL )
440 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
442 INetURLObject aObj( FileURL, INetProtocol::File );
443 ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
445 Reference< XContent > xContent = aCnt.get();
446 OUString aTypeStr = xContent->getContentType();
447 return aTypeStr;
450 ::com::sun::star::util::DateTime OFileAccess::getDateTimeModified( const OUString& FileURL )
451 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
453 INetURLObject aFileObj( FileURL, INetProtocol::File );
454 ::com::sun::star::util::DateTime aDateTime;
456 Reference< XCommandEnvironment > aCmdEnv;
457 ucbhelper::Content aYoung( aFileObj.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv, comphelper::getProcessComponentContext() );
458 aYoung.getPropertyValue("DateModified") >>= aDateTime;
459 return aDateTime;
462 typedef vector< OUString* > StringList_Impl;
464 Sequence< OUString > OFileAccess::getFolderContents( const OUString& FolderURL, sal_Bool bIncludeFolders )
465 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
467 // SfxContentHelper::GetFolderContents
469 StringList_Impl* pFiles = NULL;
470 INetURLObject aFolderObj( FolderURL, INetProtocol::File );
472 ucbhelper::Content aCnt( aFolderObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
473 Reference< XResultSet > xResultSet;
474 Sequence< OUString > aProps(0);
476 ucbhelper::ResultSetInclude eInclude = bIncludeFolders ? ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ucbhelper::INCLUDE_DOCUMENTS_ONLY;
480 xResultSet = aCnt.createCursor( aProps, eInclude );
482 catch ( ::com::sun::star::ucb::CommandFailedException const & )
484 // Interaction Handler already handled the error that has occurred...
487 if ( xResultSet.is() )
489 pFiles = new StringList_Impl();
490 Reference< com::sun::star::ucb::XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
492 while ( xResultSet->next() )
494 OUString aId = xContentAccess->queryContentIdentifierString();
495 INetURLObject aURL( aId, INetProtocol::File );
496 OUString* pFile = new OUString( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
497 pFiles->push_back( pFile );
501 if ( pFiles )
503 size_t nCount = pFiles->size();
504 Sequence < OUString > aRet( nCount );
505 OUString* pRet = aRet.getArray();
506 for ( size_t i = 0; i < nCount; ++i )
508 OUString* pFile = pFiles->at( i );
509 pRet[i] = *( pFile );
510 delete pFile;
512 pFiles->clear();
513 delete pFiles;
514 return aRet;
516 else
517 return Sequence < OUString > ();
520 sal_Bool OFileAccess::exists( const OUString& FileURL )
521 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
523 bool bRet = false;
526 bRet = isFolder( FileURL );
527 if( !bRet )
529 Reference< XInputStream > xStream = openFileRead( FileURL );
530 bRet = xStream.is();
531 if( bRet )
532 xStream->closeInput();
535 catch (const Exception &) {}
536 return bRet;
539 Reference< XInputStream > OFileAccess::openFileRead( const OUString& FileURL )
540 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
542 Reference< XInputStream > xRet;
543 INetURLObject aObj( FileURL, INetProtocol::File );
544 ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
546 Reference< XActiveDataSink > xSink = (XActiveDataSink*)(new OActiveDataSink());
550 bool bRet = aCnt.openStream( xSink );
551 if( bRet )
552 xRet = xSink->getInputStream();
554 catch ( ::com::sun::star::ucb::CommandFailedException const & )
556 // Interaction Handler already handled the error that has occurred...
559 return xRet;
562 Reference< XOutputStream > OFileAccess::openFileWrite( const OUString& FileURL )
563 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
565 Reference< XOutputStream > xRet;
566 Reference< XStream > xStream = OFileAccess::openFileReadWrite( FileURL );
567 if( xStream.is() )
568 xRet = xStream->getOutputStream();
569 return xRet;
572 Reference< XStream > OFileAccess::openFileReadWrite( const OUString& FileURL )
573 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
575 Reference< XActiveDataStreamer > xSink = (XActiveDataStreamer*)new OActiveDataStreamer();
576 Reference< XInterface > xSinkIface = Reference< XInterface >::query( xSink );
578 OpenCommandArgument2 aArg;
579 aArg.Mode = OpenMode::DOCUMENT;
580 aArg.Priority = 0; // unused
581 aArg.Sink = xSink;
582 aArg.Properties = Sequence< Property >( 0 ); // unused
584 Any aCmdArg;
585 aCmdArg <<= aArg;
587 INetURLObject aFileObj( FileURL, INetProtocol::File );
588 ucbhelper::Content aCnt( aFileObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
590 // Be silent...
591 Reference< XInteractionHandler > xIH;
592 if ( mpEnvironment )
594 xIH = mpEnvironment->getInteractionHandler();
595 mpEnvironment->setHandler( 0 );
600 aCnt.executeCommand( OUString("open" ), aCmdArg );
602 catch ( InteractiveIOException const & e )
604 if ( xIH.is() && mpEnvironment )
605 mpEnvironment->setHandler( xIH );
607 if ( e.Code == IOErrorCode_NOT_EXISTING )
609 // Create file...
610 SvMemoryStream aStream(0,0);
611 ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
612 Reference< XInputStream > xInput( pInput );
613 InsertCommandArgument aInsertArg;
614 aInsertArg.Data = xInput;
615 aInsertArg.ReplaceExisting = sal_False;
617 aCmdArg <<= aInsertArg;
618 aCnt.executeCommand( OUString("insert" ), aCmdArg );
620 // Retry...
621 return openFileReadWrite( FileURL );
624 throw;
627 if ( xIH.is() && mpEnvironment )
628 mpEnvironment->setHandler( xIH );
630 Reference< XStream > xRet = xSink->getStream();
631 return xRet;
634 void OFileAccess::setInteractionHandler( const Reference< XInteractionHandler >& Handler )
635 throw(RuntimeException, std::exception)
637 if( !mpEnvironment )
639 mpEnvironment = new OCommandEnvironment();
640 mxEnvironment = (XCommandEnvironment*)mpEnvironment;
642 mpEnvironment->setHandler( Handler );
645 bool OFileAccess::createNewFile( const OUString & rParentURL,
646 const OUString & rTitle,
647 const Reference< XInputStream >& data )
648 throw ( Exception )
650 ucbhelper::Content aParentCnt( rParentURL, mxEnvironment, comphelper::getProcessComponentContext() );
652 Sequence< ContentInfo > aInfo = aParentCnt.queryCreatableContentsInfo();
653 sal_Int32 nCount = aInfo.getLength();
654 if ( nCount == 0 )
655 return false;
657 for ( sal_Int32 i = 0; i < nCount; ++i )
659 const ContentInfo & rCurr = aInfo[i];
660 if ( ( rCurr.Attributes
661 & ContentInfoAttribute::KIND_DOCUMENT ) &&
662 ( rCurr.Attributes
663 & ContentInfoAttribute::INSERT_WITH_INPUTSTREAM ) )
665 // Make sure the only required bootstrap property is
666 // "Title",
667 const Sequence< Property > & rProps = rCurr.Properties;
668 if ( rProps.getLength() != 1 )
669 continue;
671 if ( rProps[ 0 ].Name != "Title" )
672 continue;
674 Sequence<OUString> aNames(1);
675 OUString* pNames = aNames.getArray();
676 pNames[0] = "Title";
677 Sequence< Any > aValues(1);
678 Any* pValues = aValues.getArray();
679 pValues[0] = makeAny( OUString( rTitle ) );
683 ucbhelper::Content aNew;
684 if ( aParentCnt.insertNewContent(
685 rCurr.Type, aNames, aValues, data, aNew ) )
686 return true; // success.
687 else
688 continue;
690 catch ( CommandFailedException const & )
692 // Interaction Handler already handled the
693 // error that has occurred...
694 continue;
699 return false;
702 void SAL_CALL OFileAccess::writeFile( const OUString& FileURL,
703 const Reference< XInputStream >& data )
704 throw ( Exception, RuntimeException, std::exception )
706 INetURLObject aURL( FileURL, INetProtocol::File );
709 ucbhelper::Content aCnt(
710 aURL.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment,
711 comphelper::getProcessComponentContext() );
715 aCnt.writeStream( data, true /* bReplaceExisting */ );
717 catch ( CommandFailedException const & )
719 // Interaction Handler already handled the error that has occurred...
722 catch ( ContentCreationException const & e )
724 // Most probably file does not exist. Try to create.
725 if ( e.eError == ContentCreationError_CONTENT_CREATION_FAILED )
727 INetURLObject aParentURLObj( aURL );
728 if ( aParentURLObj.removeSegment() )
730 OUString aParentURL
731 = aParentURLObj.GetMainURL( INetURLObject::NO_DECODE );
733 // ensure all parent folders exist.
734 createFolder( aParentURL );
736 // create the new file...
737 OUString aTitle
738 = aURL.getName( INetURLObject::LAST_SEGMENT,
739 true,
740 INetURLObject::DECODE_WITH_CHARSET );
741 if ( createNewFile( aParentURL, aTitle, data ) )
743 // success
744 return;
749 throw;
753 sal_Bool OFileAccess::isHidden( const OUString& FileURL )
754 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
756 INetURLObject aURLObj( FileURL, INetProtocol::File );
757 ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
758 Any aRetAny = aCnt.getPropertyValue("IsHidden");
759 bool bRet = false;
760 aRetAny >>= bRet;
761 return bRet;
764 void OFileAccess::setHidden( const OUString& FileURL, sal_Bool bHidden )
765 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
767 INetURLObject aURLObj( FileURL, INetProtocol::File );
768 ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
769 Any aAny;
770 aAny <<= bHidden;
771 aCnt.setPropertyValue("IsHidden", aAny );
774 }; // namespace end
776 Reference< XInterface > SAL_CALL FileAccess_CreateInstance( const Reference< XMultiServiceFactory > & xSMgr )
778 return Reference < XInterface >( ( cppu::OWeakObject * ) new OFileAccess( comphelper::getComponentContext(xSMgr) ) );
781 Sequence< OUString > FileAccess_getSupportedServiceNames()
783 Sequence< OUString > seqNames(1);
784 seqNames.getArray()[0] = SERVICE_NAME;
785 return seqNames;
788 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */