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