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