18be892b1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
38be892b1SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
48be892b1SAndrew Rist * or more contributor license agreements. See the NOTICE file
58be892b1SAndrew Rist * distributed with this work for additional information
68be892b1SAndrew Rist * regarding copyright ownership. The ASF licenses this file
78be892b1SAndrew Rist * to you under the Apache License, Version 2.0 (the
88be892b1SAndrew Rist * "License"); you may not use this file except in compliance
98be892b1SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
118be892b1SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
138be892b1SAndrew Rist * Unless required by applicable law or agreed to in writing,
148be892b1SAndrew Rist * software distributed under the License is distributed on an
158be892b1SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
168be892b1SAndrew Rist * KIND, either express or implied. See the License for the
178be892b1SAndrew Rist * specific language governing permissions and limitations
188be892b1SAndrew Rist * under the License.
19cdf0e10cSrcweir *
208be892b1SAndrew Rist *************************************************************/
218be892b1SAndrew Rist
228be892b1SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include <osl/mutex.hxx>
25cdf0e10cSrcweir #include <osl/diagnose.h>
26cdf0e10cSrcweir
273616bdb9Sdamjan #include "fileaccess/dllapi.h"
283616bdb9Sdamjan
29cdf0e10cSrcweir #include <uno/mapping.hxx>
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
32cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
33*cde5ecdaSDamjan Jovanovic #include <cppuhelper/implementationentry.hxx>
34cdf0e10cSrcweir
35cdf0e10cSrcweir #include <tools/ref.hxx>
36cdf0e10cSrcweir #include <tools/urlobj.hxx>
37cdf0e10cSrcweir #include <ucbhelper/content.hxx>
38cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
39cdf0e10cSrcweir #include <tools/stream.hxx>
40cdf0e10cSrcweir
41cdf0e10cSrcweir #include <com/sun/star/beans/Property.hpp>
42cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
43cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
44cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSink.hpp>
45cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
46cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataStreamer.hpp>
47cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
48cdf0e10cSrcweir #include <com/sun/star/ucb/CommandFailedException.hpp>
49cdf0e10cSrcweir #include <com/sun/star/ucb/ContentInfo.hpp>
50cdf0e10cSrcweir #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
51cdf0e10cSrcweir #include <com/sun/star/ucb/InsertCommandArgument.hpp>
52cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveIOException.hpp>
53cdf0e10cSrcweir #include <com/sun/star/ucb/NameClash.hpp>
54cdf0e10cSrcweir #include <com/sun/star/ucb/NameClashException.hpp>
55cdf0e10cSrcweir #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
56cdf0e10cSrcweir #include <com/sun/star/ucb/OpenMode.hpp>
57cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandEnvironment.hpp>
58cdf0e10cSrcweir #include <com/sun/star/ucb/XContent.hpp>
59cdf0e10cSrcweir #include <com/sun/star/ucb/XContentAccess.hpp>
60cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
61cdf0e10cSrcweir #include <com/sun/star/util/XMacroExpander.hpp>
62cdf0e10cSrcweir
63cdf0e10cSrcweir using namespace ::com::sun::star::uno;
64cdf0e10cSrcweir using namespace ::com::sun::star::lang;
65cdf0e10cSrcweir using namespace ::com::sun::star::io;
66cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
67cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
68cdf0e10cSrcweir using namespace ::com::sun::star::task;
69cdf0e10cSrcweir using namespace ::com::sun::star::util;
70cdf0e10cSrcweir using namespace ::com::sun::star::beans;
71cdf0e10cSrcweir using namespace ::com::sun::star::registry;
72cdf0e10cSrcweir using namespace ::com::sun::star::container;
73cdf0e10cSrcweir
74cdf0e10cSrcweir namespace io_FileAccess
75cdf0e10cSrcweir {
76cdf0e10cSrcweir
77cdf0e10cSrcweir
78cdf0e10cSrcweir //===========================================================================
79cdf0e10cSrcweir // Implementation XSimpleFileAccess
80cdf0e10cSrcweir
81cdf0e10cSrcweir typedef cppu::WeakImplHelper1< XSimpleFileAccess3 > FileAccessHelper;
82cdf0e10cSrcweir class OCommandEnvironment;
83cdf0e10cSrcweir
84cdf0e10cSrcweir class OFileAccess : public FileAccessHelper
85cdf0e10cSrcweir {
86*cde5ecdaSDamjan Jovanovic Reference< XComponentContext > mxCtx;
87cdf0e10cSrcweir Reference< XCommandEnvironment > mxEnvironment;
88cdf0e10cSrcweir OCommandEnvironment* mpEnvironment;
89cdf0e10cSrcweir
90cdf0e10cSrcweir void transferImpl( const rtl::OUString& rSource, const rtl::OUString& rDest, sal_Bool bMoveData )
91cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException);
92cdf0e10cSrcweir bool createNewFile( const rtl::OUString & rParentURL,
93cdf0e10cSrcweir const rtl::OUString & rTitle,
94cdf0e10cSrcweir const Reference< XInputStream >& data )
95cdf0e10cSrcweir throw ( Exception );
96cdf0e10cSrcweir
97cdf0e10cSrcweir public:
OFileAccess(const Reference<XComponentContext> & xCtx)98*cde5ecdaSDamjan Jovanovic OFileAccess( const Reference< XComponentContext > & xCtx )
99*cde5ecdaSDamjan Jovanovic : mxCtx( xCtx ), mpEnvironment( NULL ) {}
100cdf0e10cSrcweir
101cdf0e10cSrcweir // Methods
102cdf0e10cSrcweir virtual void SAL_CALL copy( const ::rtl::OUString& SourceURL, const ::rtl::OUString& DestURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
103cdf0e10cSrcweir virtual void SAL_CALL move( const ::rtl::OUString& SourceURL, const ::rtl::OUString& DestURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
104cdf0e10cSrcweir virtual void SAL_CALL kill( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
105cdf0e10cSrcweir virtual sal_Bool SAL_CALL isFolder( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
106cdf0e10cSrcweir virtual sal_Bool SAL_CALL isReadOnly( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
107cdf0e10cSrcweir virtual void SAL_CALL setReadOnly( const ::rtl::OUString& FileURL, sal_Bool bReadOnly ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
108cdf0e10cSrcweir virtual void SAL_CALL createFolder( const ::rtl::OUString& NewFolderURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
109cdf0e10cSrcweir virtual sal_Int32 SAL_CALL getSize( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
110cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getContentType( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
111cdf0e10cSrcweir virtual ::com::sun::star::util::DateTime SAL_CALL getDateTimeModified( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
112cdf0e10cSrcweir virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getFolderContents( const ::rtl::OUString& FolderURL, sal_Bool bIncludeFolders ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
113cdf0e10cSrcweir virtual sal_Bool SAL_CALL exists( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
114cdf0e10cSrcweir virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL openFileRead( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
115cdf0e10cSrcweir virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL openFileWrite( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
116cdf0e10cSrcweir virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream > SAL_CALL openFileReadWrite( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
117cdf0e10cSrcweir virtual void SAL_CALL setInteractionHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler ) throw(::com::sun::star::uno::RuntimeException);
118cdf0e10cSrcweir virtual void SAL_CALL writeFile( const ::rtl::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);
119cdf0e10cSrcweir virtual sal_Bool SAL_CALL isHidden( const ::rtl::OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
120cdf0e10cSrcweir virtual void SAL_CALL setHidden( const ::rtl::OUString& FileURL, sal_Bool bHidden ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
121cdf0e10cSrcweir };
122cdf0e10cSrcweir
123cdf0e10cSrcweir
124cdf0e10cSrcweir //===========================================================================
125cdf0e10cSrcweir // Implementation XActiveDataSink
126cdf0e10cSrcweir
127cdf0e10cSrcweir typedef cppu::WeakImplHelper1< XActiveDataSink > ActiveDataSinkHelper;
128cdf0e10cSrcweir
129cdf0e10cSrcweir class OActiveDataSink : public ActiveDataSinkHelper
130cdf0e10cSrcweir {
131cdf0e10cSrcweir Reference< XInputStream > mxStream;
132cdf0e10cSrcweir
133cdf0e10cSrcweir public:
134cdf0e10cSrcweir
135cdf0e10cSrcweir // Methods
136cdf0e10cSrcweir virtual void SAL_CALL setInputStream( const Reference< XInputStream >& aStream )
137cdf0e10cSrcweir throw(RuntimeException);
138cdf0e10cSrcweir virtual Reference< XInputStream > SAL_CALL getInputStream( )
139cdf0e10cSrcweir throw(RuntimeException);
140cdf0e10cSrcweir };
141cdf0e10cSrcweir
setInputStream(const Reference<XInputStream> & aStream)142cdf0e10cSrcweir void OActiveDataSink::setInputStream( const Reference< XInputStream >& aStream )
143cdf0e10cSrcweir throw(RuntimeException)
144cdf0e10cSrcweir {
145cdf0e10cSrcweir mxStream = aStream;
146cdf0e10cSrcweir }
147cdf0e10cSrcweir
getInputStream()148cdf0e10cSrcweir Reference< XInputStream > OActiveDataSink::getInputStream()
149cdf0e10cSrcweir throw(RuntimeException)
150cdf0e10cSrcweir {
151cdf0e10cSrcweir return mxStream;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir
154cdf0e10cSrcweir
155cdf0e10cSrcweir //===========================================================================
156cdf0e10cSrcweir // Implementation XActiveDataSource
157cdf0e10cSrcweir
158cdf0e10cSrcweir typedef cppu::WeakImplHelper1< XActiveDataSource > ActiveDataSourceHelper;
159cdf0e10cSrcweir
160cdf0e10cSrcweir class OActiveDataSource : public ActiveDataSourceHelper
161cdf0e10cSrcweir {
162cdf0e10cSrcweir Reference< XOutputStream > mxStream;
163cdf0e10cSrcweir
164cdf0e10cSrcweir public:
165cdf0e10cSrcweir
166cdf0e10cSrcweir // Methods
167cdf0e10cSrcweir virtual void SAL_CALL setOutputStream( const Reference< XOutputStream >& aStream )
168cdf0e10cSrcweir throw(RuntimeException);
169cdf0e10cSrcweir virtual Reference< XOutputStream > SAL_CALL getOutputStream()
170cdf0e10cSrcweir throw(RuntimeException);
171cdf0e10cSrcweir };
172cdf0e10cSrcweir
setOutputStream(const Reference<XOutputStream> & aStream)173cdf0e10cSrcweir void OActiveDataSource::setOutputStream( const Reference< XOutputStream >& aStream )
174cdf0e10cSrcweir throw(RuntimeException)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir mxStream = aStream;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir
getOutputStream()179cdf0e10cSrcweir Reference< XOutputStream > OActiveDataSource::getOutputStream()
180cdf0e10cSrcweir throw(RuntimeException)
181cdf0e10cSrcweir {
182cdf0e10cSrcweir return mxStream;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir
185cdf0e10cSrcweir
186cdf0e10cSrcweir //===========================================================================
187cdf0e10cSrcweir // Implementation XActiveDataStreamer
188cdf0e10cSrcweir
189cdf0e10cSrcweir typedef cppu::WeakImplHelper1< XActiveDataStreamer > ActiveDataStreamerHelper;
190cdf0e10cSrcweir
191cdf0e10cSrcweir class OActiveDataStreamer : public ActiveDataStreamerHelper
192cdf0e10cSrcweir {
193cdf0e10cSrcweir Reference< XStream > mxStream;
194cdf0e10cSrcweir
195cdf0e10cSrcweir public:
196cdf0e10cSrcweir
197cdf0e10cSrcweir // Methods
198cdf0e10cSrcweir virtual void SAL_CALL setStream( const Reference< XStream >& aStream )
199cdf0e10cSrcweir throw(RuntimeException);
200cdf0e10cSrcweir virtual Reference< XStream > SAL_CALL getStream()
201cdf0e10cSrcweir throw(RuntimeException);
202cdf0e10cSrcweir };
203cdf0e10cSrcweir
setStream(const Reference<XStream> & aStream)204cdf0e10cSrcweir void OActiveDataStreamer::setStream( const Reference< XStream >& aStream )
205cdf0e10cSrcweir throw(RuntimeException)
206cdf0e10cSrcweir {
207cdf0e10cSrcweir mxStream = aStream;
208cdf0e10cSrcweir }
209cdf0e10cSrcweir
getStream()210cdf0e10cSrcweir Reference< XStream > OActiveDataStreamer::getStream()
211cdf0e10cSrcweir throw(RuntimeException)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir return mxStream;
214cdf0e10cSrcweir }
215cdf0e10cSrcweir
216cdf0e10cSrcweir
217cdf0e10cSrcweir
218cdf0e10cSrcweir //===========================================================================
219cdf0e10cSrcweir // Implementation XCommandEnvironment
220cdf0e10cSrcweir
221cdf0e10cSrcweir typedef cppu::WeakImplHelper1< XCommandEnvironment > CommandEnvironmentHelper;
222cdf0e10cSrcweir
223cdf0e10cSrcweir class OCommandEnvironment : public CommandEnvironmentHelper
224cdf0e10cSrcweir {
225cdf0e10cSrcweir Reference< XInteractionHandler > mxInteraction;
226cdf0e10cSrcweir
227cdf0e10cSrcweir public:
setHandler(Reference<XInteractionHandler> xInteraction_)228cdf0e10cSrcweir void setHandler( Reference< XInteractionHandler > xInteraction_ )
229cdf0e10cSrcweir {
230cdf0e10cSrcweir mxInteraction = xInteraction_;
231cdf0e10cSrcweir }
232cdf0e10cSrcweir
233cdf0e10cSrcweir // Methods
234cdf0e10cSrcweir virtual Reference< XInteractionHandler > SAL_CALL getInteractionHandler()
235cdf0e10cSrcweir throw(RuntimeException);
236cdf0e10cSrcweir virtual Reference< XProgressHandler > SAL_CALL getProgressHandler()
237cdf0e10cSrcweir throw(RuntimeException);
238cdf0e10cSrcweir };
239cdf0e10cSrcweir
getInteractionHandler()240cdf0e10cSrcweir Reference< XInteractionHandler > OCommandEnvironment::getInteractionHandler()
241cdf0e10cSrcweir throw(RuntimeException)
242cdf0e10cSrcweir {
243cdf0e10cSrcweir return mxInteraction;
244cdf0e10cSrcweir }
245cdf0e10cSrcweir
getProgressHandler()246cdf0e10cSrcweir Reference< XProgressHandler > OCommandEnvironment::getProgressHandler()
247cdf0e10cSrcweir throw(RuntimeException)
248cdf0e10cSrcweir {
249cdf0e10cSrcweir Reference< XProgressHandler > xRet;
250cdf0e10cSrcweir return xRet;
251cdf0e10cSrcweir }
252cdf0e10cSrcweir
253cdf0e10cSrcweir //===========================================================================
254cdf0e10cSrcweir
transferImpl(const rtl::OUString & rSource,const rtl::OUString & rDest,sal_Bool bMoveData)255cdf0e10cSrcweir void OFileAccess::transferImpl( const rtl::OUString& rSource,
256cdf0e10cSrcweir const rtl::OUString& rDest,
257cdf0e10cSrcweir sal_Bool bMoveData )
258cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
259cdf0e10cSrcweir {
260cdf0e10cSrcweir // SfxContentHelper::Transfer_Impl
261cdf0e10cSrcweir INetURLObject aSourceObj( rSource, INET_PROT_FILE );
262cdf0e10cSrcweir INetURLObject aDestObj( rDest, INET_PROT_FILE );
263cdf0e10cSrcweir String aName = aDestObj.getName(
264cdf0e10cSrcweir INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
265cdf0e10cSrcweir String aDestURL;
266cdf0e10cSrcweir String aSourceURL = aSourceObj.GetMainURL( INetURLObject::NO_DECODE );
267cdf0e10cSrcweir if ( aDestObj.removeSegment() )
268cdf0e10cSrcweir {
269cdf0e10cSrcweir // hierarchical URL.
270cdf0e10cSrcweir
271cdf0e10cSrcweir aDestObj.setFinalSlash();
272cdf0e10cSrcweir aDestURL = aDestObj.GetMainURL( INetURLObject::NO_DECODE );
273cdf0e10cSrcweir }
274cdf0e10cSrcweir else
275cdf0e10cSrcweir {
276cdf0e10cSrcweir // non-hierachical URL
277cdf0e10cSrcweir
278cdf0e10cSrcweir // #i29648#
279cdf0e10cSrcweir //
280cdf0e10cSrcweir #if 0
28107a3d7f1SPedro Giffuni // Note: A hierarchical UCB content implements interface XChild, which
282cdf0e10cSrcweir // has a method getParent(). Unfortunately this does not always help
283cdf0e10cSrcweir // here, because it is not guaranteed that a content object for a
284cdf0e10cSrcweir // non-existing resource can be created. Thus, it will happen that an
285cdf0e10cSrcweir // exception is thrown when trying to create a UCB content for the
286cdf0e10cSrcweir // destination URL.
287cdf0e10cSrcweir
288cdf0e10cSrcweir try
289cdf0e10cSrcweir {
290cdf0e10cSrcweir ucbhelper::Content aFullDest(
291cdf0e10cSrcweir aDestObj.GetMainURL(
292cdf0e10cSrcweir INetURLObject::NO_DECODE ), mxEnvironment );
293cdf0e10cSrcweir
294cdf0e10cSrcweir Reference< XChild > xChild( aFullDest.get(), UNO_QUERY_THROW );
295cdf0e10cSrcweir Reference< com::sun::star::ucb::XContent >
296cdf0e10cSrcweir xParent( xChild->getParent(), UNO_QUERY_THROW );
297cdf0e10cSrcweir ucbhelper::Content aParent( xParent, mxEnvironment );
298cdf0e10cSrcweir
299cdf0e10cSrcweir aDestURL = aParent.getURL();
300cdf0e10cSrcweir
301cdf0e10cSrcweir rtl::OUString aNameTmp;
302cdf0e10cSrcweir aFullDest.getPropertyValue(
303cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Title" ) ) )
304cdf0e10cSrcweir >>= aNameTmp;
305cdf0e10cSrcweir aName = aNameTmp;
306cdf0e10cSrcweir }
307cdf0e10cSrcweir catch ( Exception const & )
308cdf0e10cSrcweir {
309cdf0e10cSrcweir throw RuntimeException(
310cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
311cdf0e10cSrcweir "OFileAccess::transferrImpl - Unable to "
312cdf0e10cSrcweir "obtain destination folder URL!" ) ),
313cdf0e10cSrcweir static_cast< cppu::OWeakObject * >( this ) );
314cdf0e10cSrcweir }
315cdf0e10cSrcweir #else
316cdf0e10cSrcweir if ( aDestObj.GetProtocol() == INET_PROT_VND_SUN_STAR_EXPAND )
317cdf0e10cSrcweir {
318cdf0e10cSrcweir // Hack: Expand destination URL using Macro Expander and try again
319cdf0e10cSrcweir // with the hopefully hierarchical expanded URL...
320cdf0e10cSrcweir
321cdf0e10cSrcweir try
322cdf0e10cSrcweir {
323cdf0e10cSrcweir Reference< XMacroExpander > xExpander;
324cdf0e10cSrcweir
325*cde5ecdaSDamjan Jovanovic mxCtx->getValueByName(
326cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
327cdf0e10cSrcweir "/singletons/com.sun.star.util.theMacroExpander" ) ) )
328cdf0e10cSrcweir >>= xExpander;
329cdf0e10cSrcweir
330cdf0e10cSrcweir OSL_ENSURE( xExpander.is(),
331cdf0e10cSrcweir "Unable to obtain macro expander singleton!" );
332cdf0e10cSrcweir
333cdf0e10cSrcweir aDestURL = xExpander->expandMacros(
334cdf0e10cSrcweir aDestObj.GetURLPath( INetURLObject::DECODE_WITH_CHARSET ) );
335cdf0e10cSrcweir }
336cdf0e10cSrcweir catch ( Exception const & )
337cdf0e10cSrcweir {
338cdf0e10cSrcweir throw RuntimeException(
339cdf0e10cSrcweir rtl::OUString(
340cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(
341cdf0e10cSrcweir "OFileAccess::transferrImpl - Unable to obtain "
342cdf0e10cSrcweir "destination folder URL!" ) ),
343cdf0e10cSrcweir static_cast< cppu::OWeakObject * >( this ) );
344cdf0e10cSrcweir }
345cdf0e10cSrcweir
346cdf0e10cSrcweir transferImpl( rSource, aDestURL, bMoveData );
347cdf0e10cSrcweir return;
348cdf0e10cSrcweir }
349cdf0e10cSrcweir
350cdf0e10cSrcweir throw RuntimeException(
351cdf0e10cSrcweir rtl::OUString(
352cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(
353cdf0e10cSrcweir "OFileAccess::transferrImpl - Unable to obtain "
354cdf0e10cSrcweir "destination folder URL!" ) ),
355cdf0e10cSrcweir static_cast< cppu::OWeakObject * >( this ) );
356cdf0e10cSrcweir #endif
357cdf0e10cSrcweir }
358cdf0e10cSrcweir
359cdf0e10cSrcweir ucbhelper::Content aDestPath( aDestURL, mxEnvironment );
360cdf0e10cSrcweir ucbhelper::Content aSrc ( aSourceURL, mxEnvironment );
361cdf0e10cSrcweir
362cdf0e10cSrcweir try
363cdf0e10cSrcweir {
364cdf0e10cSrcweir aDestPath.transferContent( aSrc,
365cdf0e10cSrcweir bMoveData
366cdf0e10cSrcweir ? ucbhelper::InsertOperation_MOVE
367cdf0e10cSrcweir : ucbhelper::InsertOperation_COPY,
368cdf0e10cSrcweir aName,
369cdf0e10cSrcweir ::com::sun::star::ucb::NameClash::OVERWRITE );
370cdf0e10cSrcweir }
371cdf0e10cSrcweir catch ( ::com::sun::star::ucb::CommandFailedException const & )
372cdf0e10cSrcweir {
37307a3d7f1SPedro Giffuni // Interaction Handler already handled the error that has occurred...
374cdf0e10cSrcweir }
375cdf0e10cSrcweir }
376cdf0e10cSrcweir
copy(const rtl::OUString & SourceURL,const rtl::OUString & DestURL)377cdf0e10cSrcweir void OFileAccess::copy( const rtl::OUString& SourceURL, const rtl::OUString& DestURL )
378cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
379cdf0e10cSrcweir {
380cdf0e10cSrcweir transferImpl( SourceURL, DestURL, sal_False );
381cdf0e10cSrcweir }
382cdf0e10cSrcweir
move(const rtl::OUString & SourceURL,const rtl::OUString & DestURL)383cdf0e10cSrcweir void OFileAccess::move( const rtl::OUString& SourceURL, const rtl::OUString& DestURL )
384cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
385cdf0e10cSrcweir {
386cdf0e10cSrcweir transferImpl( SourceURL, DestURL, sal_True );
387cdf0e10cSrcweir }
388cdf0e10cSrcweir
kill(const rtl::OUString & FileURL)389cdf0e10cSrcweir void OFileAccess::kill( const rtl::OUString& FileURL )
390cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
391cdf0e10cSrcweir {
392cdf0e10cSrcweir // SfxContentHelper::Kill
393cdf0e10cSrcweir INetURLObject aDeleteObj( FileURL, INET_PROT_FILE );
394cdf0e10cSrcweir ucbhelper::Content aCnt( aDeleteObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
395cdf0e10cSrcweir try
396cdf0e10cSrcweir {
397cdf0e10cSrcweir aCnt.executeCommand( rtl::OUString::createFromAscii( "delete" ), makeAny( sal_Bool( sal_True ) ) );
398cdf0e10cSrcweir }
399cdf0e10cSrcweir catch ( ::com::sun::star::ucb::CommandFailedException const & )
400cdf0e10cSrcweir {
40107a3d7f1SPedro Giffuni // Interaction Handler already handled the error that has occurred...
402cdf0e10cSrcweir }
403cdf0e10cSrcweir }
404cdf0e10cSrcweir
isFolder(const rtl::OUString & FileURL)405cdf0e10cSrcweir sal_Bool OFileAccess::isFolder( const rtl::OUString& FileURL )
406cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
407cdf0e10cSrcweir {
408cdf0e10cSrcweir sal_Bool bRet = sal_False;
409cdf0e10cSrcweir try
410cdf0e10cSrcweir {
411cdf0e10cSrcweir INetURLObject aURLObj( FileURL, INET_PROT_FILE );
412cdf0e10cSrcweir ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
413cdf0e10cSrcweir bRet = aCnt.isFolder();
414cdf0e10cSrcweir }
415cdf0e10cSrcweir catch (Exception &) {}
416cdf0e10cSrcweir return bRet;
417cdf0e10cSrcweir }
418cdf0e10cSrcweir
isReadOnly(const rtl::OUString & FileURL)419cdf0e10cSrcweir sal_Bool OFileAccess::isReadOnly( const rtl::OUString& FileURL )
420cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
421cdf0e10cSrcweir {
422cdf0e10cSrcweir INetURLObject aURLObj( FileURL, INET_PROT_FILE );
423cdf0e10cSrcweir ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
424cdf0e10cSrcweir Any aRetAny = aCnt.getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsReadOnly" ) ) );
425cdf0e10cSrcweir sal_Bool bRet = sal_False;
426cdf0e10cSrcweir aRetAny >>= bRet;
427cdf0e10cSrcweir return bRet;
428cdf0e10cSrcweir }
429cdf0e10cSrcweir
setReadOnly(const rtl::OUString & FileURL,sal_Bool bReadOnly)430cdf0e10cSrcweir void OFileAccess::setReadOnly( const rtl::OUString& FileURL, sal_Bool bReadOnly )
431cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
432cdf0e10cSrcweir {
433cdf0e10cSrcweir INetURLObject aURLObj( FileURL, INET_PROT_FILE );
434cdf0e10cSrcweir ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
435cdf0e10cSrcweir Any aAny;
436cdf0e10cSrcweir aAny <<= bReadOnly;
437cdf0e10cSrcweir aCnt.setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsReadOnly" ) ), aAny );
438cdf0e10cSrcweir }
439cdf0e10cSrcweir
createFolder(const rtl::OUString & NewFolderURL)440cdf0e10cSrcweir void OFileAccess::createFolder( const rtl::OUString& NewFolderURL )
441cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
442cdf0e10cSrcweir {
443cdf0e10cSrcweir // Does the folder already exist?
444cdf0e10cSrcweir if( !NewFolderURL.getLength() || isFolder( NewFolderURL ) )
445cdf0e10cSrcweir return;
446cdf0e10cSrcweir
447cdf0e10cSrcweir // SfxContentHelper::MakeFolder
448cdf0e10cSrcweir INetURLObject aURL( NewFolderURL, INET_PROT_FILE );
449cdf0e10cSrcweir String aNewFolderURLStr = aURL.GetMainURL( INetURLObject::NO_DECODE );
450cdf0e10cSrcweir String aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
451cdf0e10cSrcweir if ( aTitle.Len() )
452cdf0e10cSrcweir {
453cdf0e10cSrcweir aURL.removeSegment();
454cdf0e10cSrcweir
455cdf0e10cSrcweir // Does the base folder exist? Otherwise create it first
456cdf0e10cSrcweir String aBaseFolderURLStr = aURL.GetMainURL( INetURLObject::NO_DECODE );
457cdf0e10cSrcweir if( !isFolder( aBaseFolderURLStr ) )
458cdf0e10cSrcweir {
459cdf0e10cSrcweir createFolder( aBaseFolderURLStr );
460cdf0e10cSrcweir }
461cdf0e10cSrcweir }
462cdf0e10cSrcweir
463cdf0e10cSrcweir ucbhelper::Content aCnt( aURL.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
464cdf0e10cSrcweir
465cdf0e10cSrcweir Sequence< ContentInfo > aInfo = aCnt.queryCreatableContentsInfo();
466cdf0e10cSrcweir sal_Int32 nCount = aInfo.getLength();
467cdf0e10cSrcweir if ( nCount == 0 )
468cdf0e10cSrcweir return;
469cdf0e10cSrcweir
470cdf0e10cSrcweir for ( sal_Int32 i = 0; i < nCount; ++i )
471cdf0e10cSrcweir {
472cdf0e10cSrcweir // Simply look for the first KIND_FOLDER...
473cdf0e10cSrcweir const ContentInfo & rCurr = aInfo[i];
474cdf0e10cSrcweir if ( rCurr.Attributes & ContentInfoAttribute::KIND_FOLDER )
475cdf0e10cSrcweir {
476cdf0e10cSrcweir // Make sure the only required bootstrap property is "Title",
477cdf0e10cSrcweir const Sequence< Property > & rProps = rCurr.Properties;
478cdf0e10cSrcweir if ( rProps.getLength() != 1 )
479cdf0e10cSrcweir continue;
480cdf0e10cSrcweir
481cdf0e10cSrcweir if ( !rProps[ 0 ].Name.equalsAsciiL(
482cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "Title" ) ) )
483cdf0e10cSrcweir continue;
484cdf0e10cSrcweir
485cdf0e10cSrcweir Sequence<rtl::OUString> aNames(1);
486cdf0e10cSrcweir rtl::OUString* pNames = aNames.getArray();
487cdf0e10cSrcweir pNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Title" ) );
488cdf0e10cSrcweir Sequence< Any > aValues(1);
489cdf0e10cSrcweir Any* pValues = aValues.getArray();
490cdf0e10cSrcweir pValues[0] = makeAny( rtl::OUString( aTitle ) );
491cdf0e10cSrcweir
492cdf0e10cSrcweir ucbhelper::Content aNew;
493cdf0e10cSrcweir try
494cdf0e10cSrcweir {
495cdf0e10cSrcweir if ( !aCnt.insertNewContent( rCurr.Type, aNames, aValues, aNew ) )
496cdf0e10cSrcweir continue;
497cdf0e10cSrcweir
498cdf0e10cSrcweir // Success. We're done.
499cdf0e10cSrcweir return;
500cdf0e10cSrcweir }
501cdf0e10cSrcweir catch ( ::com::sun::star::ucb::CommandFailedException const & )
502cdf0e10cSrcweir {
50307a3d7f1SPedro Giffuni // Interaction Handler already handled the error that has occurred...
504cdf0e10cSrcweir continue;
505cdf0e10cSrcweir }
506cdf0e10cSrcweir }
507cdf0e10cSrcweir }
508cdf0e10cSrcweir }
509cdf0e10cSrcweir
getSize(const rtl::OUString & FileURL)510cdf0e10cSrcweir sal_Int32 OFileAccess::getSize( const rtl::OUString& FileURL )
511cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
512cdf0e10cSrcweir {
513cdf0e10cSrcweir // SfxContentHelper::GetSize
514cdf0e10cSrcweir sal_Int32 nSize = 0;
515cdf0e10cSrcweir sal_Int64 nTemp = 0;
516cdf0e10cSrcweir INetURLObject aObj( FileURL, INET_PROT_FILE );
517cdf0e10cSrcweir ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
518cdf0e10cSrcweir aCnt.getPropertyValue( rtl::OUString::createFromAscii( "Size" ) ) >>= nTemp;
519cdf0e10cSrcweir nSize = (sal_Int32)nTemp;
520cdf0e10cSrcweir return nSize;
521cdf0e10cSrcweir }
522cdf0e10cSrcweir
getContentType(const rtl::OUString & FileURL)523cdf0e10cSrcweir rtl::OUString OFileAccess::getContentType( const rtl::OUString& FileURL )
524cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
525cdf0e10cSrcweir {
526cdf0e10cSrcweir INetURLObject aObj( FileURL, INET_PROT_FILE );
527cdf0e10cSrcweir ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
528cdf0e10cSrcweir
529cdf0e10cSrcweir Reference< XContent > xContent = aCnt.get();
530cdf0e10cSrcweir rtl::OUString aTypeStr = xContent->getContentType();
531cdf0e10cSrcweir return aTypeStr;
532cdf0e10cSrcweir }
533cdf0e10cSrcweir
getDateTimeModified(const rtl::OUString & FileURL)534cdf0e10cSrcweir DateTime OFileAccess::getDateTimeModified( const rtl::OUString& FileURL )
535cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
536cdf0e10cSrcweir {
537cdf0e10cSrcweir INetURLObject aFileObj( FileURL, INET_PROT_FILE );
538cdf0e10cSrcweir DateTime aDateTime;
539cdf0e10cSrcweir
540cdf0e10cSrcweir Reference< XCommandEnvironment > aCmdEnv;
541cdf0e10cSrcweir ucbhelper::Content aYoung( aFileObj.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv );
542cdf0e10cSrcweir aYoung.getPropertyValue( rtl::OUString::createFromAscii( "DateModified" ) ) >>= aDateTime;
543cdf0e10cSrcweir return aDateTime;
544cdf0e10cSrcweir }
545cdf0e10cSrcweir
546cdf0e10cSrcweir
DECLARE_LIST(StringList_Impl,rtl::OUString *)547cdf0e10cSrcweir DECLARE_LIST( StringList_Impl, rtl::OUString* )
548cdf0e10cSrcweir
549cdf0e10cSrcweir Sequence< rtl::OUString > OFileAccess::getFolderContents( const rtl::OUString& FolderURL, sal_Bool bIncludeFolders )
550cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
551cdf0e10cSrcweir {
552cdf0e10cSrcweir // SfxContentHelper::GetFolderContents
553cdf0e10cSrcweir
554cdf0e10cSrcweir StringList_Impl* pFiles = NULL;
555cdf0e10cSrcweir INetURLObject aFolderObj( FolderURL, INET_PROT_FILE );
556cdf0e10cSrcweir
557cdf0e10cSrcweir ucbhelper::Content aCnt( aFolderObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
558cdf0e10cSrcweir Reference< XResultSet > xResultSet;
559cdf0e10cSrcweir Sequence< rtl::OUString > aProps(0);
560cdf0e10cSrcweir //Sequence< rtl::OUString > aProps(1);
561cdf0e10cSrcweir //rtl::OUString* pProps = aProps.getArray();
562cdf0e10cSrcweir //pProps[0] == rtl::OUString::createFromAscii( "Url" );
563cdf0e10cSrcweir
564cdf0e10cSrcweir ucbhelper::ResultSetInclude eInclude = bIncludeFolders ? ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ucbhelper::INCLUDE_DOCUMENTS_ONLY;
565cdf0e10cSrcweir
566cdf0e10cSrcweir try
567cdf0e10cSrcweir {
568cdf0e10cSrcweir xResultSet = aCnt.createCursor( aProps, eInclude );
569cdf0e10cSrcweir }
570cdf0e10cSrcweir catch ( ::com::sun::star::ucb::CommandFailedException const & )
571cdf0e10cSrcweir {
57207a3d7f1SPedro Giffuni // Interaction Handler already handled the error that has occurred...
573cdf0e10cSrcweir }
574cdf0e10cSrcweir
575cdf0e10cSrcweir if ( xResultSet.is() )
576cdf0e10cSrcweir {
577cdf0e10cSrcweir pFiles = new StringList_Impl;
578cdf0e10cSrcweir Reference< com::sun::star::ucb::XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
579cdf0e10cSrcweir
580cdf0e10cSrcweir while ( xResultSet->next() )
581cdf0e10cSrcweir {
582cdf0e10cSrcweir rtl::OUString aId = xContentAccess->queryContentIdentifierString();
583cdf0e10cSrcweir INetURLObject aURL( aId, INET_PROT_FILE );
584cdf0e10cSrcweir rtl::OUString* pFile = new rtl::OUString( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
585cdf0e10cSrcweir pFiles->Insert( pFile, LIST_APPEND );
586cdf0e10cSrcweir }
587cdf0e10cSrcweir }
588cdf0e10cSrcweir
589cdf0e10cSrcweir if ( pFiles )
590cdf0e10cSrcweir {
591cdf0e10cSrcweir sal_uIntPtr nCount = pFiles->Count();
592cdf0e10cSrcweir Sequence < rtl::OUString > aRet( nCount );
593cdf0e10cSrcweir rtl::OUString* pRet = aRet.getArray();
594cdf0e10cSrcweir for ( sal_uInt16 i = 0; i < nCount; ++i )
595cdf0e10cSrcweir {
596cdf0e10cSrcweir rtl::OUString* pFile = pFiles->GetObject(i);
597cdf0e10cSrcweir pRet[i] = *( pFile );
598cdf0e10cSrcweir delete pFile;
599cdf0e10cSrcweir }
600cdf0e10cSrcweir delete pFiles;
601cdf0e10cSrcweir return aRet;
602cdf0e10cSrcweir }
603cdf0e10cSrcweir else
604cdf0e10cSrcweir return Sequence < rtl::OUString > ();
605cdf0e10cSrcweir }
606cdf0e10cSrcweir
exists(const rtl::OUString & FileURL)607cdf0e10cSrcweir sal_Bool OFileAccess::exists( const rtl::OUString& FileURL )
608cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
609cdf0e10cSrcweir {
610cdf0e10cSrcweir sal_Bool bRet = sal_False;
611cdf0e10cSrcweir try
612cdf0e10cSrcweir {
613cdf0e10cSrcweir bRet = isFolder( FileURL );
614cdf0e10cSrcweir if( !bRet )
615cdf0e10cSrcweir {
616cdf0e10cSrcweir Reference< XInputStream > xStream = openFileRead( FileURL );
617cdf0e10cSrcweir bRet = xStream.is();
618cdf0e10cSrcweir if( bRet )
619cdf0e10cSrcweir xStream->closeInput();
620cdf0e10cSrcweir }
621cdf0e10cSrcweir }
622cdf0e10cSrcweir catch (Exception &) {}
623cdf0e10cSrcweir return bRet;
624cdf0e10cSrcweir }
625cdf0e10cSrcweir
openFileRead(const rtl::OUString & FileURL)626cdf0e10cSrcweir Reference< XInputStream > OFileAccess::openFileRead( const rtl::OUString& FileURL )
627cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
628cdf0e10cSrcweir {
629cdf0e10cSrcweir Reference< XInputStream > xRet;
630cdf0e10cSrcweir INetURLObject aObj( FileURL, INET_PROT_FILE );
631cdf0e10cSrcweir ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
632cdf0e10cSrcweir
633cdf0e10cSrcweir Reference< XActiveDataSink > xSink = (XActiveDataSink*)(new OActiveDataSink());
634cdf0e10cSrcweir
635cdf0e10cSrcweir try
636cdf0e10cSrcweir {
637cdf0e10cSrcweir sal_Bool bRet = aCnt.openStream( xSink );
638cdf0e10cSrcweir if( bRet )
639cdf0e10cSrcweir xRet = xSink->getInputStream();
640cdf0e10cSrcweir }
641cdf0e10cSrcweir catch ( ::com::sun::star::ucb::CommandFailedException const & )
642cdf0e10cSrcweir {
64307a3d7f1SPedro Giffuni // Interaction Handler already handled the error that has occurred...
644cdf0e10cSrcweir }
645cdf0e10cSrcweir
646cdf0e10cSrcweir return xRet;
647cdf0e10cSrcweir }
648cdf0e10cSrcweir
openFileWrite(const rtl::OUString & FileURL)649cdf0e10cSrcweir Reference< XOutputStream > OFileAccess::openFileWrite( const rtl::OUString& FileURL )
650cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
651cdf0e10cSrcweir {
652cdf0e10cSrcweir Reference< XOutputStream > xRet;
653cdf0e10cSrcweir Reference< XStream > xStream = OFileAccess::openFileReadWrite( FileURL );
654cdf0e10cSrcweir if( xStream.is() )
655cdf0e10cSrcweir xRet = xStream->getOutputStream();
656cdf0e10cSrcweir return xRet;
657cdf0e10cSrcweir }
658cdf0e10cSrcweir
openFileReadWrite(const rtl::OUString & FileURL)659cdf0e10cSrcweir Reference< XStream > OFileAccess::openFileReadWrite( const rtl::OUString& FileURL )
660cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
661cdf0e10cSrcweir {
662cdf0e10cSrcweir Reference< XActiveDataStreamer > xSink = (XActiveDataStreamer*)new OActiveDataStreamer();
663cdf0e10cSrcweir Reference< XInterface > xSinkIface = Reference< XInterface >::query( xSink );
664cdf0e10cSrcweir
665cdf0e10cSrcweir OpenCommandArgument2 aArg;
666cdf0e10cSrcweir aArg.Mode = OpenMode::DOCUMENT;
667cdf0e10cSrcweir aArg.Priority = 0; // unused
668cdf0e10cSrcweir aArg.Sink = xSink;
669cdf0e10cSrcweir aArg.Properties = Sequence< Property >( 0 ); // unused
670cdf0e10cSrcweir
671cdf0e10cSrcweir Any aCmdArg;
672cdf0e10cSrcweir aCmdArg <<= aArg;
673cdf0e10cSrcweir
674cdf0e10cSrcweir INetURLObject aFileObj( FileURL, INET_PROT_FILE );
675cdf0e10cSrcweir ucbhelper::Content aCnt( aFileObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
676cdf0e10cSrcweir
677cdf0e10cSrcweir // Be silent...
678cdf0e10cSrcweir Reference< XInteractionHandler > xIH;
679cdf0e10cSrcweir if ( mpEnvironment )
680cdf0e10cSrcweir {
681cdf0e10cSrcweir xIH = mpEnvironment->getInteractionHandler();
682cdf0e10cSrcweir mpEnvironment->setHandler( 0 );
683cdf0e10cSrcweir }
684cdf0e10cSrcweir
685cdf0e10cSrcweir try
686cdf0e10cSrcweir {
687cdf0e10cSrcweir aCnt.executeCommand( rtl::OUString::createFromAscii( "open" ), aCmdArg );
688cdf0e10cSrcweir }
689cdf0e10cSrcweir catch ( InteractiveIOException const & e )
690cdf0e10cSrcweir {
691cdf0e10cSrcweir if ( xIH.is() )
692cdf0e10cSrcweir mpEnvironment->setHandler( xIH );
693cdf0e10cSrcweir
694cdf0e10cSrcweir if ( e.Code == IOErrorCode_NOT_EXISTING )
695cdf0e10cSrcweir {
696cdf0e10cSrcweir // Create file...
697cdf0e10cSrcweir SvMemoryStream aStream(0,0);
698cdf0e10cSrcweir ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
699cdf0e10cSrcweir Reference< XInputStream > xInput( pInput );
700cdf0e10cSrcweir InsertCommandArgument aInsertArg;
701cdf0e10cSrcweir aInsertArg.Data = xInput;
702cdf0e10cSrcweir aInsertArg.ReplaceExisting = sal_False;
703cdf0e10cSrcweir
704cdf0e10cSrcweir aCmdArg <<= aInsertArg;
705cdf0e10cSrcweir aCnt.executeCommand( rtl::OUString::createFromAscii( "insert" ), aCmdArg );
706cdf0e10cSrcweir
707cdf0e10cSrcweir // Retry...
708cdf0e10cSrcweir return openFileReadWrite( FileURL );
709cdf0e10cSrcweir }
710cdf0e10cSrcweir
711cdf0e10cSrcweir throw;
712cdf0e10cSrcweir }
713cdf0e10cSrcweir
714cdf0e10cSrcweir if ( xIH.is() )
715cdf0e10cSrcweir mpEnvironment->setHandler( xIH );
716cdf0e10cSrcweir
717cdf0e10cSrcweir Reference< XStream > xRet = xSink->getStream();
718cdf0e10cSrcweir return xRet;
719cdf0e10cSrcweir }
720cdf0e10cSrcweir
setInteractionHandler(const Reference<XInteractionHandler> & Handler)721cdf0e10cSrcweir void OFileAccess::setInteractionHandler( const Reference< XInteractionHandler >& Handler )
722cdf0e10cSrcweir throw(RuntimeException)
723cdf0e10cSrcweir {
724cdf0e10cSrcweir if( !mpEnvironment )
725cdf0e10cSrcweir {
726cdf0e10cSrcweir mpEnvironment = new OCommandEnvironment();
727cdf0e10cSrcweir mxEnvironment = (XCommandEnvironment*)mpEnvironment;
728cdf0e10cSrcweir }
729cdf0e10cSrcweir mpEnvironment->setHandler( Handler );
730cdf0e10cSrcweir }
731cdf0e10cSrcweir
createNewFile(const rtl::OUString & rParentURL,const rtl::OUString & rTitle,const Reference<XInputStream> & data)732cdf0e10cSrcweir bool OFileAccess::createNewFile( const rtl::OUString & rParentURL,
733cdf0e10cSrcweir const rtl::OUString & rTitle,
734cdf0e10cSrcweir const Reference< XInputStream >& data )
735cdf0e10cSrcweir throw ( Exception )
736cdf0e10cSrcweir {
737cdf0e10cSrcweir ucbhelper::Content aParentCnt( rParentURL, mxEnvironment );
738cdf0e10cSrcweir
739cdf0e10cSrcweir Sequence< ContentInfo > aInfo = aParentCnt.queryCreatableContentsInfo();
740cdf0e10cSrcweir sal_Int32 nCount = aInfo.getLength();
741cdf0e10cSrcweir if ( nCount == 0 )
742cdf0e10cSrcweir return false;
743cdf0e10cSrcweir
744cdf0e10cSrcweir for ( sal_Int32 i = 0; i < nCount; ++i )
745cdf0e10cSrcweir {
746cdf0e10cSrcweir const ContentInfo & rCurr = aInfo[i];
747cdf0e10cSrcweir if ( ( rCurr.Attributes
748cdf0e10cSrcweir & ContentInfoAttribute::KIND_DOCUMENT ) &&
749cdf0e10cSrcweir ( rCurr.Attributes
750cdf0e10cSrcweir & ContentInfoAttribute::INSERT_WITH_INPUTSTREAM ) )
751cdf0e10cSrcweir {
752cdf0e10cSrcweir // Make sure the only required bootstrap property is
753cdf0e10cSrcweir // "Title",
754cdf0e10cSrcweir const Sequence< Property > & rProps = rCurr.Properties;
755cdf0e10cSrcweir if ( rProps.getLength() != 1 )
756cdf0e10cSrcweir continue;
757cdf0e10cSrcweir
758cdf0e10cSrcweir if ( !rProps[ 0 ].Name.equalsAsciiL(
759cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "Title" ) ) )
760cdf0e10cSrcweir continue;
761cdf0e10cSrcweir
762cdf0e10cSrcweir Sequence<rtl::OUString> aNames(1);
763cdf0e10cSrcweir rtl::OUString* pNames = aNames.getArray();
764cdf0e10cSrcweir pNames[0] = rtl::OUString(
765cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "Title" ) );
766cdf0e10cSrcweir Sequence< Any > aValues(1);
767cdf0e10cSrcweir Any* pValues = aValues.getArray();
768cdf0e10cSrcweir pValues[0] = makeAny( rtl::OUString( rTitle ) );
769cdf0e10cSrcweir
770cdf0e10cSrcweir try
771cdf0e10cSrcweir {
772cdf0e10cSrcweir ucbhelper::Content aNew;
773cdf0e10cSrcweir if ( aParentCnt.insertNewContent(
774cdf0e10cSrcweir rCurr.Type, aNames, aValues, data, aNew ) )
775cdf0e10cSrcweir return true; // success.
776cdf0e10cSrcweir else
777cdf0e10cSrcweir continue;
778cdf0e10cSrcweir }
779cdf0e10cSrcweir catch ( CommandFailedException const & )
780cdf0e10cSrcweir {
781cdf0e10cSrcweir // Interaction Handler already handled the
78207a3d7f1SPedro Giffuni // error that has occurred...
783cdf0e10cSrcweir continue;
784cdf0e10cSrcweir }
785cdf0e10cSrcweir }
786cdf0e10cSrcweir }
787cdf0e10cSrcweir
788cdf0e10cSrcweir return false;
789cdf0e10cSrcweir }
790cdf0e10cSrcweir
writeFile(const rtl::OUString & FileURL,const Reference<XInputStream> & data)791cdf0e10cSrcweir void SAL_CALL OFileAccess::writeFile( const rtl::OUString& FileURL,
792cdf0e10cSrcweir const Reference< XInputStream >& data )
793cdf0e10cSrcweir throw ( Exception, RuntimeException )
794cdf0e10cSrcweir {
795cdf0e10cSrcweir INetURLObject aURL( FileURL, INET_PROT_FILE );
796cdf0e10cSrcweir try
797cdf0e10cSrcweir {
798cdf0e10cSrcweir ucbhelper::Content aCnt(
799cdf0e10cSrcweir aURL.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
800cdf0e10cSrcweir
801cdf0e10cSrcweir try
802cdf0e10cSrcweir {
803cdf0e10cSrcweir aCnt.writeStream( data, sal_True /* bReplaceExisting */ );
804cdf0e10cSrcweir }
805cdf0e10cSrcweir catch ( CommandFailedException const & )
806cdf0e10cSrcweir {
80707a3d7f1SPedro Giffuni // Interaction Handler already handled the error that has occurred...
808cdf0e10cSrcweir }
809cdf0e10cSrcweir }
810cdf0e10cSrcweir catch ( ContentCreationException const & e )
811cdf0e10cSrcweir {
812cdf0e10cSrcweir // Most probably file does not exist. Try to create.
813cdf0e10cSrcweir if ( e.eError == ContentCreationError_CONTENT_CREATION_FAILED )
814cdf0e10cSrcweir {
815cdf0e10cSrcweir INetURLObject aParentURLObj( aURL );
816cdf0e10cSrcweir if ( aParentURLObj.removeSegment() )
817cdf0e10cSrcweir {
818cdf0e10cSrcweir String aParentURL
819cdf0e10cSrcweir = aParentURLObj.GetMainURL( INetURLObject::NO_DECODE );
820cdf0e10cSrcweir
821cdf0e10cSrcweir // ensure all parent folders exist.
822cdf0e10cSrcweir createFolder( aParentURL );
823cdf0e10cSrcweir
824cdf0e10cSrcweir // create the new file...
825cdf0e10cSrcweir String aTitle
826cdf0e10cSrcweir = aURL.getName( INetURLObject::LAST_SEGMENT,
827cdf0e10cSrcweir true,
828cdf0e10cSrcweir INetURLObject::DECODE_WITH_CHARSET );
829cdf0e10cSrcweir if ( createNewFile( aParentURL, aTitle, data ) )
830cdf0e10cSrcweir {
831cdf0e10cSrcweir // success
832cdf0e10cSrcweir return;
833cdf0e10cSrcweir }
834cdf0e10cSrcweir }
835cdf0e10cSrcweir }
836cdf0e10cSrcweir
837cdf0e10cSrcweir throw;
838cdf0e10cSrcweir }
839cdf0e10cSrcweir }
840cdf0e10cSrcweir
isHidden(const::rtl::OUString & FileURL)841cdf0e10cSrcweir sal_Bool OFileAccess::isHidden( const ::rtl::OUString& FileURL )
842cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
843cdf0e10cSrcweir {
844cdf0e10cSrcweir INetURLObject aURLObj( FileURL, INET_PROT_FILE );
845cdf0e10cSrcweir ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
846cdf0e10cSrcweir Any aRetAny = aCnt.getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsHidden" ) ) );
847cdf0e10cSrcweir sal_Bool bRet = sal_False;
848cdf0e10cSrcweir aRetAny >>= bRet;
849cdf0e10cSrcweir return bRet;
850cdf0e10cSrcweir }
851cdf0e10cSrcweir
setHidden(const::rtl::OUString & FileURL,sal_Bool bHidden)852cdf0e10cSrcweir void OFileAccess::setHidden( const ::rtl::OUString& FileURL, sal_Bool bHidden )
853cdf0e10cSrcweir throw(CommandAbortedException, Exception, RuntimeException)
854cdf0e10cSrcweir {
855cdf0e10cSrcweir INetURLObject aURLObj( FileURL, INET_PROT_FILE );
856cdf0e10cSrcweir ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment );
857cdf0e10cSrcweir Any aAny;
858cdf0e10cSrcweir aAny <<= bHidden;
859cdf0e10cSrcweir aCnt.setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsHidden" ) ), aAny );
860cdf0e10cSrcweir }
861cdf0e10cSrcweir
862cdf0e10cSrcweir //==================================================================================================
863cdf0e10cSrcweir //==================================================================================================
864cdf0e10cSrcweir //==================================================================================================
865cdf0e10cSrcweir
FileAccess_CreateInstance(const Reference<XComponentContext> & xCtx)866*cde5ecdaSDamjan Jovanovic Reference< XInterface > SAL_CALL FileAccess_CreateInstance( const Reference< XComponentContext > & xCtx )
867cdf0e10cSrcweir {
868*cde5ecdaSDamjan Jovanovic return Reference < XInterface >( ( cppu::OWeakObject * ) new OFileAccess( xCtx ) );
869cdf0e10cSrcweir }
870cdf0e10cSrcweir
FileAccess_getImplementationName()871*cde5ecdaSDamjan Jovanovic rtl::OUString FileAccess_getImplementationName()
872*cde5ecdaSDamjan Jovanovic {
873*cde5ecdaSDamjan Jovanovic return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.ucb.SimpleFileAccess" ) );
874*cde5ecdaSDamjan Jovanovic }
875cdf0e10cSrcweir
FileAccess_getSupportedServiceNames()876cdf0e10cSrcweir Sequence< rtl::OUString > FileAccess_getSupportedServiceNames()
877cdf0e10cSrcweir {
878cdf0e10cSrcweir static Sequence < rtl::OUString > *pNames = 0;
879cdf0e10cSrcweir if( ! pNames )
880cdf0e10cSrcweir {
881cdf0e10cSrcweir osl::MutexGuard guard( osl::Mutex::getGlobalMutex() );
882cdf0e10cSrcweir if( !pNames )
883cdf0e10cSrcweir {
884cdf0e10cSrcweir static Sequence< rtl::OUString > seqNames(1);
885*cde5ecdaSDamjan Jovanovic seqNames.getArray()[0] = rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" );
886cdf0e10cSrcweir pNames = &seqNames;
887cdf0e10cSrcweir }
888cdf0e10cSrcweir }
889cdf0e10cSrcweir return *pNames;
890cdf0e10cSrcweir }
891cdf0e10cSrcweir
892*cde5ecdaSDamjan Jovanovic struct ::cppu::ImplementationEntry g_component_entries [] =
893*cde5ecdaSDamjan Jovanovic {
894*cde5ecdaSDamjan Jovanovic {
895*cde5ecdaSDamjan Jovanovic FileAccess_CreateInstance,
896*cde5ecdaSDamjan Jovanovic FileAccess_getImplementationName,
897*cde5ecdaSDamjan Jovanovic FileAccess_getSupportedServiceNames,
898*cde5ecdaSDamjan Jovanovic ::cppu::createSingleComponentFactory,
899*cde5ecdaSDamjan Jovanovic 0,
900*cde5ecdaSDamjan Jovanovic 0
901*cde5ecdaSDamjan Jovanovic },
902*cde5ecdaSDamjan Jovanovic { 0, 0, 0, 0, 0, 0 }
903*cde5ecdaSDamjan Jovanovic };
904cdf0e10cSrcweir
905cdf0e10cSrcweir }
906cdf0e10cSrcweir
907cdf0e10cSrcweir //==================================================================================================
908cdf0e10cSrcweir // Component exports
909cdf0e10cSrcweir
910cdf0e10cSrcweir extern "C"
911cdf0e10cSrcweir {
912cdf0e10cSrcweir //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)9133616bdb9Sdamjan FILEACCESS_DLLPUBLIC void SAL_CALL component_getImplementationEnvironment(
914cdf0e10cSrcweir const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
915cdf0e10cSrcweir {
916cdf0e10cSrcweir *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
917cdf0e10cSrcweir }
918cdf0e10cSrcweir //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)9193616bdb9Sdamjan FILEACCESS_DLLPUBLIC void * SAL_CALL component_getFactory(
920*cde5ecdaSDamjan Jovanovic const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
921cdf0e10cSrcweir {
922*cde5ecdaSDamjan Jovanovic return ::cppu::component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey, ::io_FileAccess::g_component_entries );
923cdf0e10cSrcweir }
924cdf0e10cSrcweir }
925