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