Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <osl/mutex.hxx>
21 : #include <osl/diagnose.h>
22 :
23 : #include <uno/mapping.hxx>
24 : #include <comphelper/processfactory.hxx>
25 : #include <cppuhelper/factory.hxx>
26 : #include <cppuhelper/implbase1.hxx>
27 :
28 : #include <tools/urlobj.hxx>
29 : #include <ucbhelper/content.hxx>
30 : #include <unotools/streamwrap.hxx>
31 : #include <tools/stream.hxx>
32 :
33 : #include <com/sun/star/beans/Property.hpp>
34 : #include <com/sun/star/container/XChild.hpp>
35 : #include <com/sun/star/io/XActiveDataSink.hpp>
36 : #include <com/sun/star/io/XActiveDataSource.hpp>
37 : #include <com/sun/star/io/XActiveDataStreamer.hpp>
38 : #include <com/sun/star/sdbc/XResultSet.hpp>
39 : #include <com/sun/star/ucb/CommandFailedException.hpp>
40 : #include <com/sun/star/ucb/ContentInfo.hpp>
41 : #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
42 : #include <com/sun/star/ucb/InsertCommandArgument.hpp>
43 : #include <com/sun/star/ucb/InteractiveIOException.hpp>
44 : #include <com/sun/star/ucb/NameClash.hpp>
45 : #include <com/sun/star/ucb/NameClashException.hpp>
46 : #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
47 : #include <com/sun/star/ucb/OpenMode.hpp>
48 : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
49 : #include <com/sun/star/ucb/XContent.hpp>
50 : #include <com/sun/star/ucb/XContentAccess.hpp>
51 : #include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
52 : #include <com/sun/star/util/theMacroExpander.hpp>
53 :
54 : #include <vector>
55 :
56 : #include "FileAccess.hxx"
57 :
58 : #define SERVICE_NAME "com.sun.star.ucb.SimpleFileAccess"
59 :
60 : using namespace ::com::sun::star::uno;
61 : using namespace ::com::sun::star::lang;
62 : using namespace ::com::sun::star::io;
63 : using namespace ::com::sun::star::ucb;
64 : using namespace ::com::sun::star::sdbc;
65 : using namespace ::com::sun::star::task;
66 : using namespace ::com::sun::star::util;
67 : using namespace ::com::sun::star::beans;
68 : using namespace ::com::sun::star::registry;
69 : using namespace ::com::sun::star::container;
70 :
71 : using ::std::vector;
72 :
73 : namespace
74 : {
75 :
76 : // Implementation XSimpleFileAccess
77 : typedef cppu::WeakImplHelper1< XSimpleFileAccess3 > FileAccessHelper;
78 : class OCommandEnvironment;
79 :
80 43466 : class OFileAccess : public FileAccessHelper
81 : {
82 : Reference< XComponentContext > m_xContext;
83 : Reference< XCommandEnvironment > mxEnvironment;
84 : OCommandEnvironment* mpEnvironment;
85 :
86 : void transferImpl( const OUString& rSource, const OUString& rDest, bool bMoveData )
87 : throw(CommandAbortedException, Exception, RuntimeException);
88 : bool createNewFile( const OUString & rParentURL,
89 : const OUString & rTitle,
90 : const Reference< XInputStream >& data )
91 : throw ( Exception );
92 :
93 : public:
94 21992 : OFileAccess( const Reference< XComponentContext > & xContext )
95 21992 : : m_xContext( xContext), mpEnvironment( NULL ) {}
96 : // Methods
97 : virtual void SAL_CALL copy( const OUString& SourceURL, const OUString& DestURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
98 : virtual void SAL_CALL move( const OUString& SourceURL, const OUString& DestURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
99 : virtual void SAL_CALL kill( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
100 : virtual sal_Bool SAL_CALL isFolder( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
101 : virtual sal_Bool SAL_CALL isReadOnly( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
102 : virtual void SAL_CALL setReadOnly( const OUString& FileURL, sal_Bool bReadOnly ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
103 : virtual void SAL_CALL createFolder( const OUString& NewFolderURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
104 : virtual sal_Int32 SAL_CALL getSize( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
105 : virtual OUString SAL_CALL getContentType( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
106 : virtual ::com::sun::star::util::DateTime SAL_CALL getDateTimeModified( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
107 : virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getFolderContents( const OUString& FolderURL, sal_Bool bIncludeFolders ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
108 : virtual sal_Bool SAL_CALL exists( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
109 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL openFileRead( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
110 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL openFileWrite( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
111 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream > SAL_CALL openFileReadWrite( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
112 : virtual void SAL_CALL setInteractionHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
113 : virtual void SAL_CALL writeFile( const OUString& FileURL, const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& data ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
114 : virtual sal_Bool SAL_CALL isHidden( const OUString& FileURL ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
115 : virtual void SAL_CALL setHidden( const OUString& FileURL, sal_Bool bHidden ) throw(::com::sun::star::ucb::CommandAbortedException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
116 : };
117 :
118 : // Implementation XActiveDataSink
119 : typedef cppu::WeakImplHelper1< XActiveDataSink > ActiveDataSinkHelper;
120 :
121 18699 : class OActiveDataSink : public ActiveDataSinkHelper
122 : {
123 : Reference< XInputStream > mxStream;
124 :
125 : public:
126 :
127 : // Methods
128 : virtual void SAL_CALL setInputStream( const Reference< XInputStream >& aStream )
129 : throw(RuntimeException, std::exception) SAL_OVERRIDE;
130 : virtual Reference< XInputStream > SAL_CALL getInputStream( )
131 : throw(RuntimeException, std::exception) SAL_OVERRIDE;
132 : };
133 :
134 : // Implementation XActiveDataStreamer
135 : typedef cppu::WeakImplHelper1< XActiveDataStreamer > ActiveDataStreamerHelper;
136 :
137 8352 : class OActiveDataStreamer : public ActiveDataStreamerHelper
138 : {
139 : Reference< XStream > mxStream;
140 :
141 : public:
142 :
143 : // Methods
144 : virtual void SAL_CALL setStream( const Reference< XStream >& aStream )
145 : throw(RuntimeException, std::exception) SAL_OVERRIDE;
146 : virtual Reference< XStream > SAL_CALL getStream()
147 : throw(RuntimeException, std::exception) SAL_OVERRIDE;
148 : };
149 :
150 : // Implementation XCommandEnvironment
151 : typedef cppu::WeakImplHelper1< XCommandEnvironment > CommandEnvironmentHelper;
152 :
153 0 : class OCommandEnvironment : public CommandEnvironmentHelper
154 : {
155 : Reference< XInteractionHandler > mxInteraction;
156 :
157 : public:
158 0 : void setHandler( Reference< XInteractionHandler > xInteraction_ )
159 : {
160 0 : mxInteraction = xInteraction_;
161 0 : }
162 :
163 : // Methods
164 : virtual Reference< XInteractionHandler > SAL_CALL getInteractionHandler()
165 : throw(RuntimeException, std::exception) SAL_OVERRIDE;
166 : virtual Reference< XProgressHandler > SAL_CALL getProgressHandler()
167 : throw(RuntimeException, std::exception) SAL_OVERRIDE;
168 : };
169 :
170 5615 : void OActiveDataSink::setInputStream( const Reference< XInputStream >& aStream )
171 : throw(RuntimeException, std::exception)
172 : {
173 5615 : mxStream = aStream;
174 5615 : }
175 :
176 5615 : Reference< XInputStream > OActiveDataSink::getInputStream()
177 : throw(RuntimeException, std::exception)
178 : {
179 5615 : return mxStream;
180 : }
181 :
182 2782 : void OActiveDataStreamer::setStream( const Reference< XStream >& aStream )
183 : throw(RuntimeException, std::exception)
184 : {
185 2782 : mxStream = aStream;
186 2782 : }
187 :
188 2623 : Reference< XStream > OActiveDataStreamer::getStream()
189 : throw(RuntimeException, std::exception)
190 : {
191 2623 : return mxStream;
192 : }
193 :
194 0 : Reference< XInteractionHandler > OCommandEnvironment::getInteractionHandler()
195 : throw(RuntimeException, std::exception)
196 : {
197 0 : return mxInteraction;
198 : }
199 :
200 0 : Reference< XProgressHandler > OCommandEnvironment::getProgressHandler()
201 : throw(RuntimeException, std::exception)
202 : {
203 0 : Reference< XProgressHandler > xRet;
204 0 : return xRet;
205 : }
206 :
207 21 : void OFileAccess::transferImpl( const OUString& rSource,
208 : const OUString& rDest,
209 : bool bMoveData )
210 : throw(CommandAbortedException, Exception, RuntimeException)
211 : {
212 : // SfxContentHelper::Transfer_Impl
213 21 : INetURLObject aSourceObj( rSource, INET_PROT_FILE );
214 42 : INetURLObject aDestObj( rDest, INET_PROT_FILE );
215 : OUString aName = aDestObj.getName(
216 42 : INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
217 42 : OUString aDestURL;
218 42 : OUString aSourceURL = aSourceObj.GetMainURL( INetURLObject::NO_DECODE );
219 21 : if ( aDestObj.removeSegment() )
220 : {
221 : // hierarchical URL.
222 :
223 21 : aDestObj.setFinalSlash();
224 21 : aDestURL = aDestObj.GetMainURL( INetURLObject::NO_DECODE );
225 : }
226 : else
227 : {
228 : // non-hierachical URL
229 :
230 : // #i29648#
231 :
232 :
233 0 : if ( aDestObj.GetProtocol() == INET_PROT_VND_SUN_STAR_EXPAND )
234 : {
235 : // Hack: Expand destination URL using Macro Expander and try again
236 : // with the hopefully hierarchical expanded URL...
237 :
238 : try
239 : {
240 0 : Reference< XMacroExpander > xExpander = theMacroExpander::get(m_xContext);
241 :
242 0 : aDestURL = xExpander->expandMacros(
243 0 : aDestObj.GetURLPath( INetURLObject::DECODE_WITH_CHARSET ) );
244 : }
245 0 : catch ( Exception const & )
246 : {
247 : throw RuntimeException(
248 : "OFileAccess::transferrImpl - Unable to obtain destination folder URL!",
249 0 : static_cast< cppu::OWeakObject * >( this ) );
250 : }
251 :
252 0 : transferImpl( rSource, aDestURL, bMoveData );
253 21 : return;
254 : }
255 :
256 : throw RuntimeException(
257 : "OFileAccess::transferrImpl - Unable to obtain destination folder URL!",
258 0 : static_cast< cppu::OWeakObject * >( this ) );
259 :
260 : }
261 :
262 42 : ucbhelper::Content aDestPath( aDestURL, mxEnvironment, comphelper::getProcessComponentContext() );
263 42 : ucbhelper::Content aSrc ( aSourceURL, mxEnvironment, comphelper::getProcessComponentContext() );
264 :
265 : try
266 : {
267 : aDestPath.transferContent( aSrc,
268 : bMoveData
269 : ? ucbhelper::InsertOperation_MOVE
270 : : ucbhelper::InsertOperation_COPY,
271 : aName,
272 21 : ::com::sun::star::ucb::NameClash::OVERWRITE );
273 : }
274 0 : catch ( ::com::sun::star::ucb::CommandFailedException const & )
275 : {
276 : // Interaction Handler already handled the error that has occurred...
277 21 : }
278 : }
279 :
280 20 : void OFileAccess::copy( const OUString& SourceURL, const OUString& DestURL )
281 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
282 : {
283 20 : transferImpl( SourceURL, DestURL, false );
284 20 : }
285 :
286 1 : void OFileAccess::move( const OUString& SourceURL, const OUString& DestURL )
287 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
288 : {
289 1 : transferImpl( SourceURL, DestURL, true );
290 1 : }
291 :
292 1949 : void OFileAccess::kill( const OUString& FileURL )
293 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
294 : {
295 : // SfxContentHelper::Kill
296 1949 : INetURLObject aDeleteObj( FileURL, INET_PROT_FILE );
297 3898 : ucbhelper::Content aCnt( aDeleteObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
298 : try
299 : {
300 2366 : aCnt.executeCommand( OUString("delete" ), makeAny( true ) );
301 : }
302 0 : catch ( ::com::sun::star::ucb::CommandFailedException const & )
303 : {
304 : // Interaction Handler already handled the error that has occurred...
305 1949 : }
306 1532 : }
307 :
308 25436 : sal_Bool OFileAccess::isFolder( const OUString& FileURL )
309 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
310 : {
311 25436 : bool bRet = false;
312 : try
313 : {
314 25436 : INetURLObject aURLObj( FileURL, INET_PROT_FILE );
315 29470 : ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
316 50872 : bRet = aCnt.isFolder();
317 : }
318 21402 : catch (const Exception &) {}
319 25436 : return bRet;
320 : }
321 :
322 0 : sal_Bool OFileAccess::isReadOnly( const OUString& FileURL )
323 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
324 : {
325 0 : INetURLObject aURLObj( FileURL, INET_PROT_FILE );
326 0 : ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
327 0 : Any aRetAny = aCnt.getPropertyValue("IsReadOnly");
328 0 : bool bRet = false;
329 0 : aRetAny >>= bRet;
330 0 : return bRet;
331 : }
332 :
333 0 : void OFileAccess::setReadOnly( const OUString& FileURL, sal_Bool bReadOnly )
334 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
335 : {
336 0 : INetURLObject aURLObj( FileURL, INET_PROT_FILE );
337 0 : ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
338 0 : Any aAny;
339 0 : aAny <<= bReadOnly;
340 0 : aCnt.setPropertyValue("IsReadOnly", aAny );
341 0 : }
342 :
343 0 : void OFileAccess::createFolder( const OUString& NewFolderURL )
344 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
345 : {
346 : // Does the folder already exist?
347 0 : if( NewFolderURL.isEmpty() || isFolder( NewFolderURL ) )
348 0 : return;
349 :
350 : // SfxContentHelper::MakeFolder
351 0 : INetURLObject aURL( NewFolderURL, INET_PROT_FILE );
352 0 : OUString aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
353 0 : if ( !aTitle.isEmpty() )
354 : {
355 0 : aURL.removeSegment();
356 :
357 : // Does the base folder exist? Otherwise create it first
358 0 : OUString aBaseFolderURLStr = aURL.GetMainURL( INetURLObject::NO_DECODE );
359 0 : if( !isFolder( aBaseFolderURLStr ) )
360 : {
361 0 : createFolder( aBaseFolderURLStr );
362 0 : }
363 : }
364 :
365 0 : ucbhelper::Content aCnt( aURL.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
366 :
367 0 : Sequence< ContentInfo > aInfo = aCnt.queryCreatableContentsInfo();
368 0 : sal_Int32 nCount = aInfo.getLength();
369 0 : if ( nCount == 0 )
370 0 : return;
371 :
372 0 : for ( sal_Int32 i = 0; i < nCount; ++i )
373 : {
374 : // Simply look for the first KIND_FOLDER...
375 0 : const ContentInfo & rCurr = aInfo[i];
376 0 : if ( rCurr.Attributes & ContentInfoAttribute::KIND_FOLDER )
377 : {
378 : // Make sure the only required bootstrap property is "Title",
379 0 : const Sequence< Property > & rProps = rCurr.Properties;
380 0 : if ( rProps.getLength() != 1 )
381 0 : continue;
382 :
383 0 : if ( rProps[ 0 ].Name != "Title" )
384 0 : continue;
385 :
386 0 : Sequence<OUString> aNames(1);
387 0 : OUString* pNames = aNames.getArray();
388 0 : pNames[0] = "Title";
389 0 : Sequence< Any > aValues(1);
390 0 : Any* pValues = aValues.getArray();
391 0 : pValues[0] = makeAny( OUString( aTitle ) );
392 :
393 0 : ucbhelper::Content aNew;
394 : try
395 : {
396 0 : if ( !aCnt.insertNewContent( rCurr.Type, aNames, aValues, aNew ) )
397 0 : continue;
398 :
399 : // Success. We're done.
400 0 : return;
401 : }
402 0 : catch ( ::com::sun::star::ucb::CommandFailedException const & )
403 : {
404 : // Interaction Handler already handled the error that has occurred...
405 0 : continue;
406 0 : }
407 : }
408 0 : }
409 : }
410 :
411 0 : sal_Int32 OFileAccess::getSize( const OUString& FileURL )
412 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
413 : {
414 : // SfxContentHelper::GetSize
415 0 : sal_Int32 nSize = 0;
416 0 : sal_Int64 nTemp = 0;
417 0 : INetURLObject aObj( FileURL, INET_PROT_FILE );
418 0 : ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
419 0 : aCnt.getPropertyValue( "Size" ) >>= nTemp;
420 0 : nSize = (sal_Int32)nTemp;
421 0 : return nSize;
422 : }
423 :
424 0 : OUString OFileAccess::getContentType( const OUString& FileURL )
425 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
426 : {
427 0 : INetURLObject aObj( FileURL, INET_PROT_FILE );
428 0 : ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
429 :
430 0 : Reference< XContent > xContent = aCnt.get();
431 0 : OUString aTypeStr = xContent->getContentType();
432 0 : return aTypeStr;
433 : }
434 :
435 0 : ::com::sun::star::util::DateTime OFileAccess::getDateTimeModified( const OUString& FileURL )
436 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
437 : {
438 0 : INetURLObject aFileObj( FileURL, INET_PROT_FILE );
439 0 : ::com::sun::star::util::DateTime aDateTime;
440 :
441 0 : Reference< XCommandEnvironment > aCmdEnv;
442 0 : ucbhelper::Content aYoung( aFileObj.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv, comphelper::getProcessComponentContext() );
443 0 : aYoung.getPropertyValue("DateModified") >>= aDateTime;
444 0 : return aDateTime;
445 : }
446 :
447 : typedef vector< OUString* > StringList_Impl;
448 :
449 128 : Sequence< OUString > OFileAccess::getFolderContents( const OUString& FolderURL, sal_Bool bIncludeFolders )
450 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
451 : {
452 : // SfxContentHelper::GetFolderContents
453 :
454 128 : StringList_Impl* pFiles = NULL;
455 128 : INetURLObject aFolderObj( FolderURL, INET_PROT_FILE );
456 :
457 256 : ucbhelper::Content aCnt( aFolderObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
458 256 : Reference< XResultSet > xResultSet;
459 256 : Sequence< OUString > aProps(0);
460 :
461 128 : ucbhelper::ResultSetInclude eInclude = bIncludeFolders ? ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ucbhelper::INCLUDE_DOCUMENTS_ONLY;
462 :
463 : try
464 : {
465 128 : xResultSet = aCnt.createCursor( aProps, eInclude );
466 : }
467 0 : catch ( ::com::sun::star::ucb::CommandFailedException const & )
468 : {
469 : // Interaction Handler already handled the error that has occurred...
470 : }
471 :
472 128 : if ( xResultSet.is() )
473 : {
474 128 : pFiles = new StringList_Impl();
475 128 : Reference< com::sun::star::ucb::XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
476 :
477 476 : while ( xResultSet->next() )
478 : {
479 220 : OUString aId = xContentAccess->queryContentIdentifierString();
480 440 : INetURLObject aURL( aId, INET_PROT_FILE );
481 220 : OUString* pFile = new OUString( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
482 220 : pFiles->push_back( pFile );
483 348 : }
484 : }
485 :
486 128 : if ( pFiles )
487 : {
488 128 : size_t nCount = pFiles->size();
489 128 : Sequence < OUString > aRet( nCount );
490 128 : OUString* pRet = aRet.getArray();
491 348 : for ( size_t i = 0; i < nCount; ++i )
492 : {
493 220 : OUString* pFile = pFiles->at( i );
494 220 : pRet[i] = *( pFile );
495 220 : delete pFile;
496 : }
497 128 : pFiles->clear();
498 128 : delete pFiles;
499 128 : return aRet;
500 : }
501 : else
502 128 : return Sequence < OUString > ();
503 : }
504 :
505 328 : sal_Bool OFileAccess::exists( const OUString& FileURL )
506 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
507 : {
508 328 : bool bRet = false;
509 : try
510 : {
511 328 : bRet = isFolder( FileURL );
512 328 : if( !bRet )
513 : {
514 328 : Reference< XInputStream > xStream = openFileRead( FileURL );
515 156 : bRet = xStream.is();
516 156 : if( bRet )
517 156 : xStream->closeInput();
518 : }
519 : }
520 172 : catch (const Exception &) {}
521 328 : return bRet;
522 : }
523 :
524 6233 : Reference< XInputStream > OFileAccess::openFileRead( const OUString& FileURL )
525 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
526 : {
527 6233 : Reference< XInputStream > xRet;
528 12466 : INetURLObject aObj( FileURL, INET_PROT_FILE );
529 12466 : ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
530 :
531 12466 : Reference< XActiveDataSink > xSink = (XActiveDataSink*)(new OActiveDataSink());
532 :
533 : try
534 : {
535 6233 : bool bRet = aCnt.openStream( xSink );
536 5615 : if( bRet )
537 5615 : xRet = xSink->getInputStream();
538 : }
539 0 : catch ( ::com::sun::star::ucb::CommandFailedException const & )
540 : {
541 : // Interaction Handler already handled the error that has occurred...
542 : }
543 :
544 11230 : return xRet;
545 : }
546 :
547 1714 : Reference< XOutputStream > OFileAccess::openFileWrite( const OUString& FileURL )
548 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
549 : {
550 1714 : Reference< XOutputStream > xRet;
551 3426 : Reference< XStream > xStream = OFileAccess::openFileReadWrite( FileURL );
552 1712 : if( xStream.is() )
553 1712 : xRet = xStream->getOutputStream();
554 3424 : return xRet;
555 : }
556 :
557 2784 : Reference< XStream > OFileAccess::openFileReadWrite( const OUString& FileURL )
558 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
559 : {
560 2784 : Reference< XActiveDataStreamer > xSink = (XActiveDataStreamer*)new OActiveDataStreamer();
561 5568 : Reference< XInterface > xSinkIface = Reference< XInterface >::query( xSink );
562 :
563 5568 : OpenCommandArgument2 aArg;
564 2784 : aArg.Mode = OpenMode::DOCUMENT;
565 2784 : aArg.Priority = 0; // unused
566 2784 : aArg.Sink = xSink;
567 2784 : aArg.Properties = Sequence< Property >( 0 ); // unused
568 :
569 5568 : Any aCmdArg;
570 2784 : aCmdArg <<= aArg;
571 :
572 5568 : INetURLObject aFileObj( FileURL, INET_PROT_FILE );
573 5568 : ucbhelper::Content aCnt( aFileObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
574 :
575 : // Be silent...
576 5564 : Reference< XInteractionHandler > xIH;
577 2782 : if ( mpEnvironment )
578 : {
579 0 : xIH = mpEnvironment->getInteractionHandler();
580 0 : mpEnvironment->setHandler( 0 );
581 : }
582 :
583 : try
584 : {
585 2941 : aCnt.executeCommand( OUString("open" ), aCmdArg );
586 : }
587 159 : catch ( InteractiveIOException const & e )
588 : {
589 159 : if ( xIH.is() && mpEnvironment )
590 0 : mpEnvironment->setHandler( xIH );
591 :
592 159 : if ( e.Code == IOErrorCode_NOT_EXISTING )
593 : {
594 : // Create file...
595 159 : SvMemoryStream aStream(0,0);
596 159 : ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream );
597 318 : Reference< XInputStream > xInput( pInput );
598 318 : InsertCommandArgument aInsertArg;
599 159 : aInsertArg.Data = xInput;
600 159 : aInsertArg.ReplaceExisting = sal_False;
601 :
602 159 : aCmdArg <<= aInsertArg;
603 159 : aCnt.executeCommand( OUString("insert" ), aCmdArg );
604 :
605 : // Retry...
606 318 : return openFileReadWrite( FileURL );
607 : }
608 :
609 0 : throw;
610 : }
611 :
612 2623 : if ( xIH.is() && mpEnvironment )
613 0 : mpEnvironment->setHandler( xIH );
614 :
615 5246 : Reference< XStream > xRet = xSink->getStream();
616 5407 : return xRet;
617 : }
618 :
619 0 : void OFileAccess::setInteractionHandler( const Reference< XInteractionHandler >& Handler )
620 : throw(RuntimeException, std::exception)
621 : {
622 0 : if( !mpEnvironment )
623 : {
624 0 : mpEnvironment = new OCommandEnvironment();
625 0 : mxEnvironment = (XCommandEnvironment*)mpEnvironment;
626 : }
627 0 : mpEnvironment->setHandler( Handler );
628 0 : }
629 :
630 0 : bool OFileAccess::createNewFile( const OUString & rParentURL,
631 : const OUString & rTitle,
632 : const Reference< XInputStream >& data )
633 : throw ( Exception )
634 : {
635 0 : ucbhelper::Content aParentCnt( rParentURL, mxEnvironment, comphelper::getProcessComponentContext() );
636 :
637 0 : Sequence< ContentInfo > aInfo = aParentCnt.queryCreatableContentsInfo();
638 0 : sal_Int32 nCount = aInfo.getLength();
639 0 : if ( nCount == 0 )
640 0 : return false;
641 :
642 0 : for ( sal_Int32 i = 0; i < nCount; ++i )
643 : {
644 0 : const ContentInfo & rCurr = aInfo[i];
645 0 : if ( ( rCurr.Attributes
646 0 : & ContentInfoAttribute::KIND_DOCUMENT ) &&
647 : ( rCurr.Attributes
648 0 : & ContentInfoAttribute::INSERT_WITH_INPUTSTREAM ) )
649 : {
650 : // Make sure the only required bootstrap property is
651 : // "Title",
652 0 : const Sequence< Property > & rProps = rCurr.Properties;
653 0 : if ( rProps.getLength() != 1 )
654 0 : continue;
655 :
656 0 : if ( rProps[ 0 ].Name != "Title" )
657 0 : continue;
658 :
659 0 : Sequence<OUString> aNames(1);
660 0 : OUString* pNames = aNames.getArray();
661 0 : pNames[0] = "Title";
662 0 : Sequence< Any > aValues(1);
663 0 : Any* pValues = aValues.getArray();
664 0 : pValues[0] = makeAny( OUString( rTitle ) );
665 :
666 : try
667 : {
668 0 : ucbhelper::Content aNew;
669 0 : if ( aParentCnt.insertNewContent(
670 0 : rCurr.Type, aNames, aValues, data, aNew ) )
671 0 : return true; // success.
672 : else
673 0 : continue;
674 : }
675 0 : catch ( CommandFailedException const & )
676 : {
677 : // Interaction Handler already handled the
678 : // error that has occurred...
679 0 : continue;
680 0 : }
681 : }
682 : }
683 :
684 0 : return false;
685 : }
686 :
687 2 : void SAL_CALL OFileAccess::writeFile( const OUString& FileURL,
688 : const Reference< XInputStream >& data )
689 : throw ( Exception, RuntimeException, std::exception )
690 : {
691 2 : INetURLObject aURL( FileURL, INET_PROT_FILE );
692 : try
693 : {
694 : ucbhelper::Content aCnt(
695 : aURL.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment,
696 2 : comphelper::getProcessComponentContext() );
697 :
698 : try
699 : {
700 2 : aCnt.writeStream( data, true /* bReplaceExisting */ );
701 : }
702 0 : catch ( CommandFailedException const & )
703 : {
704 : // Interaction Handler already handled the error that has occurred...
705 2 : }
706 : }
707 0 : catch ( ContentCreationException const & e )
708 : {
709 : // Most probably file does not exist. Try to create.
710 0 : if ( e.eError == ContentCreationError_CONTENT_CREATION_FAILED )
711 : {
712 0 : INetURLObject aParentURLObj( aURL );
713 0 : if ( aParentURLObj.removeSegment() )
714 : {
715 : OUString aParentURL
716 0 : = aParentURLObj.GetMainURL( INetURLObject::NO_DECODE );
717 :
718 : // ensure all parent folders exist.
719 0 : createFolder( aParentURL );
720 :
721 : // create the new file...
722 : OUString aTitle
723 : = aURL.getName( INetURLObject::LAST_SEGMENT,
724 : true,
725 0 : INetURLObject::DECODE_WITH_CHARSET );
726 0 : if ( createNewFile( aParentURL, aTitle, data ) )
727 : {
728 : // success
729 2 : return;
730 0 : }
731 0 : }
732 : }
733 :
734 0 : throw;
735 2 : }
736 : }
737 :
738 0 : sal_Bool OFileAccess::isHidden( const OUString& FileURL )
739 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
740 : {
741 0 : INetURLObject aURLObj( FileURL, INET_PROT_FILE );
742 0 : ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
743 0 : Any aRetAny = aCnt.getPropertyValue("IsHidden");
744 0 : bool bRet = false;
745 0 : aRetAny >>= bRet;
746 0 : return bRet;
747 : }
748 :
749 0 : void OFileAccess::setHidden( const OUString& FileURL, sal_Bool bHidden )
750 : throw(CommandAbortedException, Exception, RuntimeException, std::exception)
751 : {
752 0 : INetURLObject aURLObj( FileURL, INET_PROT_FILE );
753 0 : ucbhelper::Content aCnt( aURLObj.GetMainURL( INetURLObject::NO_DECODE ), mxEnvironment, comphelper::getProcessComponentContext() );
754 0 : Any aAny;
755 0 : aAny <<= bHidden;
756 0 : aCnt.setPropertyValue("IsHidden", aAny );
757 0 : }
758 :
759 : }; // namespace end
760 :
761 21992 : Reference< XInterface > SAL_CALL FileAccess_CreateInstance( const Reference< XMultiServiceFactory > & xSMgr )
762 : {
763 21992 : return Reference < XInterface >( ( cppu::OWeakObject * ) new OFileAccess( comphelper::getComponentContext(xSMgr) ) );
764 : }
765 :
766 258 : Sequence< OUString > FileAccess_getSupportedServiceNames()
767 : {
768 258 : Sequence< OUString > seqNames(1);
769 258 : seqNames.getArray()[0] = SERVICE_NAME;
770 258 : return seqNames;
771 : }
772 :
773 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|