Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / ucb / source / core / FileAccess.cxx
blob8c96d49cd2a8639f3f06afe043620adafc1da0c9
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/implbase1.hxx>
28 #include <tools/urlobj.hxx>
29 #include <ucbhelper/content.hxx>
30 #include <unotools/streamwrap.hxx>
31 #include <tools/stream.hxx>
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>
54 #include <vector>
56 #include "FileAccess.hxx"
58 #define SERVICE_NAME "com.sun.star.ucb.SimpleFileAccess"
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;
71 using ::std::vector;
73 namespace
76 // Implementation XSimpleFileAccess
77 typedef cppu::WeakImplHelper1< XSimpleFileAccess3 > FileAccessHelper;
78 class OCommandEnvironment;
80 class OFileAccess : public FileAccessHelper
82 Reference< XComponentContext > m_xContext;
83 Reference< XCommandEnvironment > mxEnvironment;
84 OCommandEnvironment* mpEnvironment;
86 void transferImpl( const OUString& rSource, const OUString& rDest, 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 );
93 public:
94 OFileAccess( const Reference< XComponentContext > & xContext )
95 : 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;
118 // Implementation XActiveDataSink
119 typedef cppu::WeakImplHelper1< XActiveDataSink > ActiveDataSinkHelper;
121 class OActiveDataSink : public ActiveDataSinkHelper
123 Reference< XInputStream > mxStream;
125 public:
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;
134 // Implementation XActiveDataStreamer
135 typedef cppu::WeakImplHelper1< XActiveDataStreamer > ActiveDataStreamerHelper;
137 class OActiveDataStreamer : public ActiveDataStreamerHelper
139 Reference< XStream > mxStream;
141 public:
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;
150 // Implementation XCommandEnvironment
151 typedef cppu::WeakImplHelper1< XCommandEnvironment > CommandEnvironmentHelper;
153 class OCommandEnvironment : public CommandEnvironmentHelper
155 Reference< XInteractionHandler > mxInteraction;
157 public:
158 void setHandler( Reference< XInteractionHandler > xInteraction_ )
160 mxInteraction = xInteraction_;
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;
170 void OActiveDataSink::setInputStream( const Reference< XInputStream >& aStream )
171 throw(RuntimeException, std::exception)
173 mxStream = aStream;
176 Reference< XInputStream > OActiveDataSink::getInputStream()
177 throw(RuntimeException, std::exception)
179 return mxStream;
182 void OActiveDataStreamer::setStream( const Reference< XStream >& aStream )
183 throw(RuntimeException, std::exception)
185 mxStream = aStream;
188 Reference< XStream > OActiveDataStreamer::getStream()
189 throw(RuntimeException, std::exception)
191 return mxStream;
194 Reference< XInteractionHandler > OCommandEnvironment::getInteractionHandler()
195 throw(RuntimeException, std::exception)
197 return mxInteraction;
200 Reference< XProgressHandler > OCommandEnvironment::getProgressHandler()
201 throw(RuntimeException, std::exception)
203 Reference< XProgressHandler > xRet;
204 return xRet;
207 void OFileAccess::transferImpl( const OUString& rSource,
208 const OUString& rDest,
209 bool bMoveData )
210 throw(CommandAbortedException, Exception, RuntimeException)
212 // SfxContentHelper::Transfer_Impl
213 INetURLObject aSourceObj( rSource, INET_PROT_FILE );
214 INetURLObject aDestObj( rDest, INET_PROT_FILE );
215 OUString aName = aDestObj.getName(
216 INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
217 OUString aDestURL;
218 OUString aSourceURL = aSourceObj.GetMainURL( INetURLObject::NO_DECODE );
219 if ( aDestObj.removeSegment() )
221 // hierarchical URL.
223 aDestObj.setFinalSlash();
224 aDestURL = aDestObj.GetMainURL( INetURLObject::NO_DECODE );
226 else
228 // non-hierachical URL
230 // #i29648#
233 if ( aDestObj.GetProtocol() == INET_PROT_VND_SUN_STAR_EXPAND )
235 // Hack: Expand destination URL using Macro Expander and try again
236 // with the hopefully hierarchical expanded URL...
240 Reference< XMacroExpander > xExpander = theMacroExpander::get(m_xContext);
242 aDestURL = xExpander->expandMacros(
243 aDestObj.GetURLPath( INetURLObject::DECODE_WITH_CHARSET ) );
245 catch ( Exception const & )
247 throw RuntimeException(
248 OUString( "OFileAccess::transferrImpl - Unable to obtain "
249 "destination folder URL!" ),
250 static_cast< cppu::OWeakObject * >( this ) );
253 transferImpl( rSource, aDestURL, bMoveData );
254 return;
257 throw RuntimeException(
258 OUString( "OFileAccess::transferrImpl - Unable to obtain "
259 "destination folder URL!" ),
260 static_cast< cppu::OWeakObject * >( this ) );
264 ucbhelper::Content aDestPath( aDestURL, mxEnvironment, comphelper::getProcessComponentContext() );
265 ucbhelper::Content aSrc ( aSourceURL, mxEnvironment, comphelper::getProcessComponentContext() );
269 aDestPath.transferContent( aSrc,
270 bMoveData
271 ? ucbhelper::InsertOperation_MOVE
272 : ucbhelper::InsertOperation_COPY,
273 aName,
274 ::com::sun::star::ucb::NameClash::OVERWRITE );
276 catch ( ::com::sun::star::ucb::CommandFailedException const & )
278 // Interaction Handler already handled the error that has occurred...
282 void OFileAccess::copy( const OUString& SourceURL, const OUString& DestURL )
283 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
285 transferImpl( SourceURL, DestURL, false );
288 void OFileAccess::move( const OUString& SourceURL, const OUString& DestURL )
289 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
291 transferImpl( SourceURL, DestURL, true );
294 void OFileAccess::kill( const OUString& FileURL )
295 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
297 // SfxContentHelper::Kill
298 INetURLObject aDeleteObj( FileURL, INET_PROT_FILE );
299 ucbhelper::Content aCnt( aDeleteObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
302 aCnt.executeCommand( OUString("delete" ), makeAny( true ) );
304 catch ( ::com::sun::star::ucb::CommandFailedException const & )
306 // Interaction Handler already handled the error that has occurred...
310 sal_Bool OFileAccess::isFolder( const OUString& FileURL )
311 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
313 bool bRet = false;
316 INetURLObject aURLObj( FileURL, INET_PROT_FILE );
317 ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
318 bRet = aCnt.isFolder();
320 catch (const Exception &) {}
321 return bRet;
324 sal_Bool OFileAccess::isReadOnly( const OUString& FileURL )
325 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
327 INetURLObject aURLObj( FileURL, INET_PROT_FILE );
328 ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
329 Any aRetAny = aCnt.getPropertyValue("IsReadOnly");
330 bool bRet = false;
331 aRetAny >>= bRet;
332 return bRet;
335 void OFileAccess::setReadOnly( const OUString& FileURL, sal_Bool bReadOnly )
336 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
338 INetURLObject aURLObj( FileURL, INET_PROT_FILE );
339 ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
340 Any aAny;
341 aAny <<= bReadOnly;
342 aCnt.setPropertyValue("IsReadOnly", aAny );
345 void OFileAccess::createFolder( const OUString& NewFolderURL )
346 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
348 // Does the folder already exist?
349 if( NewFolderURL.isEmpty() || isFolder( NewFolderURL ) )
350 return;
352 // SfxContentHelper::MakeFolder
353 INetURLObject aURL( NewFolderURL, INET_PROT_FILE );
354 OUString aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
355 if ( !aTitle.isEmpty() )
357 aURL.removeSegment();
359 // Does the base folder exist? Otherwise create it first
360 OUString aBaseFolderURLStr = aURL.GetMainURL( INetURLObject::NO_DECODE );
361 if( !isFolder( aBaseFolderURLStr ) )
363 createFolder( aBaseFolderURLStr );
367 ucbhelper::Content aCnt( aURL.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
369 Sequence< ContentInfo > aInfo = aCnt.queryCreatableContentsInfo();
370 sal_Int32 nCount = aInfo.getLength();
371 if ( nCount == 0 )
372 return;
374 for ( sal_Int32 i = 0; i < nCount; ++i )
376 // Simply look for the first KIND_FOLDER...
377 const ContentInfo & rCurr = aInfo[i];
378 if ( rCurr.Attributes & ContentInfoAttribute::KIND_FOLDER )
380 // Make sure the only required bootstrap property is "Title",
381 const Sequence< Property > & rProps = rCurr.Properties;
382 if ( rProps.getLength() != 1 )
383 continue;
385 if ( rProps[ 0 ].Name != "Title" )
386 continue;
388 Sequence<OUString> aNames(1);
389 OUString* pNames = aNames.getArray();
390 pNames[0] = "Title";
391 Sequence< Any > aValues(1);
392 Any* pValues = aValues.getArray();
393 pValues[0] = makeAny( OUString( aTitle ) );
395 ucbhelper::Content aNew;
398 if ( !aCnt.insertNewContent( rCurr.Type, aNames, aValues, aNew ) )
399 continue;
401 // Success. We're done.
402 return;
404 catch ( ::com::sun::star::ucb::CommandFailedException const & )
406 // Interaction Handler already handled the error that has occurred...
407 continue;
413 sal_Int32 OFileAccess::getSize( const OUString& FileURL )
414 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
416 // SfxContentHelper::GetSize
417 sal_Int32 nSize = 0;
418 sal_Int64 nTemp = 0;
419 INetURLObject aObj( FileURL, INET_PROT_FILE );
420 ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
421 aCnt.getPropertyValue( "Size" ) >>= nTemp;
422 nSize = (sal_Int32)nTemp;
423 return nSize;
426 OUString OFileAccess::getContentType( const OUString& FileURL )
427 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
429 INetURLObject aObj( FileURL, INET_PROT_FILE );
430 ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
432 Reference< XContent > xContent = aCnt.get();
433 OUString aTypeStr = xContent->getContentType();
434 return aTypeStr;
437 ::com::sun::star::util::DateTime OFileAccess::getDateTimeModified( const OUString& FileURL )
438 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
440 INetURLObject aFileObj( FileURL, INET_PROT_FILE );
441 ::com::sun::star::util::DateTime aDateTime;
443 Reference< XCommandEnvironment > aCmdEnv;
444 ucbhelper::Content aYoung( aFileObj.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv, comphelper::getProcessComponentContext() );
445 aYoung.getPropertyValue("DateModified") >>= aDateTime;
446 return aDateTime;
449 typedef vector< OUString* > StringList_Impl;
451 Sequence< OUString > OFileAccess::getFolderContents( const OUString& FolderURL, sal_Bool bIncludeFolders )
452 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
454 // SfxContentHelper::GetFolderContents
456 StringList_Impl* pFiles = NULL;
457 INetURLObject aFolderObj( FolderURL, INET_PROT_FILE );
459 ucbhelper::Content aCnt( aFolderObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
460 Reference< XResultSet > xResultSet;
461 Sequence< OUString > aProps(0);
463 ucbhelper::ResultSetInclude eInclude = bIncludeFolders ? ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ucbhelper::INCLUDE_DOCUMENTS_ONLY;
467 xResultSet = aCnt.createCursor( aProps, eInclude );
469 catch ( ::com::sun::star::ucb::CommandFailedException const & )
471 // Interaction Handler already handled the error that has occurred...
474 if ( xResultSet.is() )
476 pFiles = new StringList_Impl();
477 Reference< com::sun::star::ucb::XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
479 while ( xResultSet->next() )
481 OUString aId = xContentAccess->queryContentIdentifierString();
482 INetURLObject aURL( aId, INET_PROT_FILE );
483 OUString* pFile = new OUString( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
484 pFiles->push_back( pFile );
488 if ( pFiles )
490 size_t nCount = pFiles->size();
491 Sequence < OUString > aRet( nCount );
492 OUString* pRet = aRet.getArray();
493 for ( size_t i = 0; i < nCount; ++i )
495 OUString* pFile = pFiles->at( i );
496 pRet[i] = *( pFile );
497 delete pFile;
499 pFiles->clear();
500 delete pFiles;
501 return aRet;
503 else
504 return Sequence < OUString > ();
507 sal_Bool OFileAccess::exists( const OUString& FileURL )
508 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
510 bool bRet = false;
513 bRet = isFolder( FileURL );
514 if( !bRet )
516 Reference< XInputStream > xStream = openFileRead( FileURL );
517 bRet = xStream.is();
518 if( bRet )
519 xStream->closeInput();
522 catch (const Exception &) {}
523 return bRet;
526 Reference< XInputStream > OFileAccess::openFileRead( const OUString& FileURL )
527 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
529 Reference< XInputStream > xRet;
530 INetURLObject aObj( FileURL, INET_PROT_FILE );
531 ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
533 Reference< XActiveDataSink > xSink = (XActiveDataSink*)(new OActiveDataSink());
537 bool bRet = aCnt.openStream( xSink );
538 if( bRet )
539 xRet = xSink->getInputStream();
541 catch ( ::com::sun::star::ucb::CommandFailedException const & )
543 // Interaction Handler already handled the error that has occurred...
546 return xRet;
549 Reference< XOutputStream > OFileAccess::openFileWrite( const OUString& FileURL )
550 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
552 Reference< XOutputStream > xRet;
553 Reference< XStream > xStream = OFileAccess::openFileReadWrite( FileURL );
554 if( xStream.is() )
555 xRet = xStream->getOutputStream();
556 return xRet;
559 Reference< XStream > OFileAccess::openFileReadWrite( const OUString& FileURL )
560 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
562 Reference< XActiveDataStreamer > xSink = (XActiveDataStreamer*)new OActiveDataStreamer();
563 Reference< XInterface > xSinkIface = Reference< XInterface >::query( xSink );
565 OpenCommandArgument2 aArg;
566 aArg.Mode = OpenMode::DOCUMENT;
567 aArg.Priority = 0; // unused
568 aArg.Sink = xSink;
569 aArg.Properties = Sequence< Property >( 0 ); // unused
571 Any aCmdArg;
572 aCmdArg <<= aArg;
574 INetURLObject aFileObj( FileURL, INET_PROT_FILE );
575 ucbhelper::Content aCnt( aFileObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
577 // Be silent...
578 Reference< XInteractionHandler > xIH;
579 if ( mpEnvironment )
581 xIH = mpEnvironment->getInteractionHandler();
582 mpEnvironment->setHandler( 0 );
587 aCnt.executeCommand( OUString("open" ), aCmdArg );
589 catch ( InteractiveIOException const & e )
591 if ( xIH.is() && mpEnvironment )
592 mpEnvironment->setHandler( xIH );
594 if ( e.Code == IOErrorCode_NOT_EXISTING )
596 // Create file...
597 SvMemoryStream aStream(0,0);
598 ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
599 Reference< XInputStream > xInput( pInput );
600 InsertCommandArgument aInsertArg;
601 aInsertArg.Data = xInput;
602 aInsertArg.ReplaceExisting = sal_False;
604 aCmdArg <<= aInsertArg;
605 aCnt.executeCommand( OUString("insert" ), aCmdArg );
607 // Retry...
608 return openFileReadWrite( FileURL );
611 throw;
614 if ( xIH.is() && mpEnvironment )
615 mpEnvironment->setHandler( xIH );
617 Reference< XStream > xRet = xSink->getStream();
618 return xRet;
621 void OFileAccess::setInteractionHandler( const Reference< XInteractionHandler >& Handler )
622 throw(RuntimeException, std::exception)
624 if( !mpEnvironment )
626 mpEnvironment = new OCommandEnvironment();
627 mxEnvironment = (XCommandEnvironment*)mpEnvironment;
629 mpEnvironment->setHandler( Handler );
632 bool OFileAccess::createNewFile( const OUString & rParentURL,
633 const OUString & rTitle,
634 const Reference< XInputStream >& data )
635 throw ( Exception )
637 ucbhelper::Content aParentCnt( rParentURL, mxEnvironment, comphelper::getProcessComponentContext() );
639 Sequence< ContentInfo > aInfo = aParentCnt.queryCreatableContentsInfo();
640 sal_Int32 nCount = aInfo.getLength();
641 if ( nCount == 0 )
642 return false;
644 for ( sal_Int32 i = 0; i < nCount; ++i )
646 const ContentInfo & rCurr = aInfo[i];
647 if ( ( rCurr.Attributes
648 & ContentInfoAttribute::KIND_DOCUMENT ) &&
649 ( rCurr.Attributes
650 & ContentInfoAttribute::INSERT_WITH_INPUTSTREAM ) )
652 // Make sure the only required bootstrap property is
653 // "Title",
654 const Sequence< Property > & rProps = rCurr.Properties;
655 if ( rProps.getLength() != 1 )
656 continue;
658 if ( rProps[ 0 ].Name != "Title" )
659 continue;
661 Sequence<OUString> aNames(1);
662 OUString* pNames = aNames.getArray();
663 pNames[0] = "Title";
664 Sequence< Any > aValues(1);
665 Any* pValues = aValues.getArray();
666 pValues[0] = makeAny( OUString( rTitle ) );
670 ucbhelper::Content aNew;
671 if ( aParentCnt.insertNewContent(
672 rCurr.Type, aNames, aValues, data, aNew ) )
673 return true; // success.
674 else
675 continue;
677 catch ( CommandFailedException const & )
679 // Interaction Handler already handled the
680 // error that has occurred...
681 continue;
686 return false;
689 void SAL_CALL OFileAccess::writeFile( const OUString& FileURL,
690 const Reference< XInputStream >& data )
691 throw ( Exception, RuntimeException, std::exception )
693 INetURLObject aURL( FileURL, INET_PROT_FILE );
696 ucbhelper::Content aCnt(
697 aURL.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment,
698 comphelper::getProcessComponentContext() );
702 aCnt.writeStream( data, true /* bReplaceExisting */ );
704 catch ( CommandFailedException const & )
706 // Interaction Handler already handled the error that has occurred...
709 catch ( ContentCreationException const & e )
711 // Most probably file does not exist. Try to create.
712 if ( e.eError == ContentCreationError_CONTENT_CREATION_FAILED )
714 INetURLObject aParentURLObj( aURL );
715 if ( aParentURLObj.removeSegment() )
717 OUString aParentURL
718 = aParentURLObj.GetMainURL( INetURLObject::NO_DECODE );
720 // ensure all parent folders exist.
721 createFolder( aParentURL );
723 // create the new file...
724 OUString aTitle
725 = aURL.getName( INetURLObject::LAST_SEGMENT,
726 true,
727 INetURLObject::DECODE_WITH_CHARSET );
728 if ( createNewFile( aParentURL, aTitle, data ) )
730 // success
731 return;
736 throw;
740 sal_Bool OFileAccess::isHidden( const OUString& FileURL )
741 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
743 INetURLObject aURLObj( FileURL, INET_PROT_FILE );
744 ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
745 Any aRetAny = aCnt.getPropertyValue("IsHidden");
746 bool bRet = false;
747 aRetAny >>= bRet;
748 return bRet;
751 void OFileAccess::setHidden( const OUString& FileURL, sal_Bool bHidden )
752 throw(CommandAbortedException, Exception, RuntimeException, std::exception)
754 INetURLObject aURLObj( FileURL, INET_PROT_FILE );
755 ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
756 Any aAny;
757 aAny <<= bHidden;
758 aCnt.setPropertyValue("IsHidden", aAny );
761 }; // namespace end
763 Reference< XInterface > SAL_CALL FileAccess_CreateInstance( const Reference< XMultiServiceFactory > & xSMgr )
765 return Reference < XInterface >( ( cppu::OWeakObject * ) new OFileAccess( comphelper::getComponentContext(xSMgr) ) );
768 Sequence< OUString > FileAccess_getSupportedServiceNames()
770 Sequence< OUString > seqNames(1);
771 seqNames.getArray()[0] = SERVICE_NAME;
772 return seqNames;
775 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */