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 <com/sun/star/beans/PropertyValue.hpp>
21 : #include <com/sun/star/embed/ElementModes.hpp>
22 : #include <com/sun/star/embed/XTransactedObject.hpp>
23 : #include <com/sun/star/ucb/NameClash.hpp>
24 : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
25 : #include <com/sun/star/ucb/XProgressHandler.hpp>
26 : #include <com/sun/star/ucb/XContentAccess.hpp>
27 : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
28 :
29 : #include <com/sun/star/ucb/InteractiveIOException.hpp>
30 : #include <com/sun/star/ucb/IOErrorCode.hpp>
31 : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
32 : #include <com/sun/star/container/XEnumerationAccess.hpp>
33 : #include <com/sun/star/container/XNamed.hpp>
34 : #include <com/sun/star/util/XChangesBatch.hpp>
35 : #include <com/sun/star/util/XCloneable.hpp>
36 : #include <com/sun/star/lang/XUnoTunnel.hpp>
37 : #include <com/sun/star/lang/XComponent.hpp>
38 : #include <com/sun/star/lang/DisposedException.hpp>
39 : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
40 : #include <com/sun/star/io/IOException.hpp>
41 : #include <com/sun/star/io/XTruncate.hpp>
42 : #include <com/sun/star/io/TempFile.hpp>
43 : #include <com/sun/star/sdbc/XResultSet.hpp>
44 : #include <com/sun/star/sdbc/XRow.hpp>
45 :
46 :
47 : #include <comphelper/processfactory.hxx>
48 : #include <comphelper/storagehelper.hxx>
49 : #include <cppuhelper/typeprovider.hxx>
50 : #include <cppuhelper/exc_hlp.hxx>
51 :
52 : #include <tools/urlobj.hxx>
53 : #include <unotools/ucbhelper.hxx>
54 : #include <unotools/ucbstreamhelper.hxx>
55 : #include <unotools/streamwrap.hxx>
56 : #include <ucbhelper/fileidentifierconverter.hxx>
57 : #include <ucbhelper/content.hxx>
58 :
59 : #include "fsstorage.hxx"
60 : #include "oinputstreamcontainer.hxx"
61 : #include "ostreamcontainer.hxx"
62 :
63 : using namespace ::com::sun::star;
64 :
65 : //=========================================================
66 :
67 : // TODO: move to a standard helper
68 0 : sal_Bool isLocalFile_Impl( OUString aURL )
69 : {
70 0 : OUString aSystemPath;
71 :
72 : try
73 : {
74 0 : aSystemPath = ::ucbhelper::getSystemPathFromFileURL(
75 : ucb::UniversalContentBroker::create(
76 : comphelper::getProcessComponentContext() ),
77 0 : aURL );
78 : }
79 0 : catch ( uno::Exception& )
80 : {
81 : }
82 :
83 0 : return ( !aSystemPath.isEmpty() );
84 : }
85 :
86 :
87 : //=========================================================
88 :
89 : struct FSStorage_Impl
90 : {
91 : OUString m_aURL;
92 :
93 : ::ucbhelper::Content* m_pContent;
94 : sal_Int32 m_nMode;
95 :
96 : ::cppu::OInterfaceContainerHelper* m_pListenersContainer; // list of listeners
97 : ::cppu::OTypeCollection* m_pTypeCollection;
98 :
99 : uno::Reference< uno::XComponentContext > m_xContext;
100 :
101 :
102 : FSStorage_Impl( const OUString& aURL, sal_Int32 nMode, uno::Reference< uno::XComponentContext > xContext )
103 : : m_aURL( aURL )
104 : , m_pContent( NULL )
105 : , m_nMode( nMode )
106 : , m_pListenersContainer( NULL )
107 : , m_pTypeCollection( NULL )
108 : , m_xContext( xContext )
109 : {
110 : OSL_ENSURE( !m_aURL.isEmpty(), "The URL must not be empty" );
111 : }
112 :
113 1212 : FSStorage_Impl( const ::ucbhelper::Content& aContent, sal_Int32 nMode, uno::Reference< uno::XComponentContext > xContext )
114 1212 : : m_aURL( aContent.getURL() )
115 1212 : , m_pContent( new ::ucbhelper::Content( aContent ) )
116 : , m_nMode( nMode )
117 : , m_pListenersContainer( NULL )
118 : , m_pTypeCollection( NULL )
119 2424 : , m_xContext( xContext )
120 : {
121 : OSL_ENSURE( !m_aURL.isEmpty(), "The URL must not be empty" );
122 1212 : }
123 :
124 : ~FSStorage_Impl();
125 : };
126 :
127 : //=========================================================
128 :
129 2356 : FSStorage_Impl::~FSStorage_Impl()
130 : {
131 1178 : if ( m_pListenersContainer )
132 0 : delete m_pListenersContainer;
133 1178 : if ( m_pTypeCollection )
134 0 : delete m_pTypeCollection;
135 1178 : if ( m_pContent )
136 1178 : delete m_pContent;
137 1178 : }
138 :
139 : //=====================================================
140 : // FSStorage implementation
141 : //=====================================================
142 :
143 : //-----------------------------------------------
144 1212 : FSStorage::FSStorage( const ::ucbhelper::Content& aContent,
145 : sal_Int32 nMode,
146 : uno::Reference< uno::XComponentContext > xContext )
147 1212 : : m_pImpl( new FSStorage_Impl( aContent, nMode, xContext ) )
148 : {
149 : // TODO: use properties
150 1212 : if ( !xContext.is() )
151 0 : throw uno::RuntimeException();
152 :
153 1212 : GetContent();
154 1212 : }
155 :
156 : //-----------------------------------------------
157 3534 : FSStorage::~FSStorage()
158 : {
159 : {
160 1178 : ::osl::MutexGuard aGuard( m_aMutex );
161 1178 : m_refCount++; // to call dispose
162 : try {
163 1178 : dispose();
164 : }
165 0 : catch( uno::RuntimeException& )
166 1178 : {}
167 : }
168 2356 : }
169 :
170 : //-----------------------------------------------
171 306 : sal_Bool FSStorage::MakeFolderNoUI( const OUString& rFolder )
172 : {
173 306 : INetURLObject aURL( rFolder );
174 612 : OUString aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
175 306 : aURL.removeSegment();
176 612 : ::ucbhelper::Content aParent;
177 612 : ::ucbhelper::Content aResultContent;
178 :
179 612 : if ( ::ucbhelper::Content::create( aURL.GetMainURL( INetURLObject::NO_DECODE ),
180 : uno::Reference< ucb::XCommandEnvironment >(),
181 : comphelper::getProcessComponentContext(),
182 612 : aParent ) )
183 306 : return ::utl::UCBContentHelper::MakeFolder( aParent, aTitle, aResultContent, sal_False );
184 :
185 306 : return sal_False;
186 : }
187 :
188 : //-----------------------------------------------
189 32853 : ::ucbhelper::Content* FSStorage::GetContent()
190 : {
191 32853 : ::osl::MutexGuard aGuard( m_aMutex );
192 32853 : if ( !m_pImpl->m_pContent )
193 : {
194 0 : uno::Reference< ucb::XCommandEnvironment > xDummyEnv;
195 :
196 : try
197 : {
198 0 : m_pImpl->m_pContent = new ::ucbhelper::Content( m_pImpl->m_aURL, xDummyEnv, comphelper::getProcessComponentContext() );
199 : }
200 0 : catch( uno::Exception& )
201 : {
202 0 : }
203 : }
204 :
205 32853 : return m_pImpl->m_pContent;
206 : }
207 :
208 : //-----------------------------------------------
209 0 : void FSStorage::CopyStreamToSubStream( const OUString& aSourceURL,
210 : const uno::Reference< embed::XStorage >& xDest,
211 : const OUString& aNewEntryName )
212 : {
213 0 : if ( !xDest.is() )
214 0 : throw uno::RuntimeException();
215 :
216 0 : uno::Reference< ucb::XCommandEnvironment > xDummyEnv;
217 0 : ::ucbhelper::Content aSourceContent( aSourceURL, xDummyEnv, comphelper::getProcessComponentContext() );
218 0 : uno::Reference< io::XInputStream > xSourceInput = aSourceContent.openStream();
219 :
220 0 : if ( !xSourceInput.is() )
221 0 : throw io::IOException(); // TODO: error handling
222 :
223 0 : uno::Reference< io::XStream > xSubStream = xDest->openStreamElement(
224 : aNewEntryName,
225 0 : embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
226 0 : if ( !xSubStream.is() )
227 0 : throw uno::RuntimeException();
228 :
229 0 : uno::Reference< io::XOutputStream > xDestOutput = xSubStream->getOutputStream();
230 0 : if ( !xDestOutput.is() )
231 0 : throw uno::RuntimeException();
232 :
233 0 : ::comphelper::OStorageHelper::CopyInputToOutput( xSourceInput, xDestOutput );
234 0 : xDestOutput->closeOutput();
235 0 : }
236 :
237 : //-----------------------------------------------
238 0 : void FSStorage::CopyContentToStorage_Impl( ::ucbhelper::Content* pContent, const uno::Reference< embed::XStorage >& xDest )
239 : {
240 0 : if ( !pContent )
241 0 : throw uno::RuntimeException();
242 :
243 : // get list of contents of the Content
244 : // create cursor for access to children
245 0 : uno::Sequence< OUString > aProps( 2 );
246 0 : OUString* pProps = aProps.getArray();
247 0 : pProps[0] = OUString("TargetURL");
248 0 : pProps[1] = OUString("IsFolder");
249 0 : ::ucbhelper::ResultSetInclude eInclude = ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS;
250 :
251 : try
252 : {
253 0 : uno::Reference< sdbc::XResultSet > xResultSet = pContent->createCursor( aProps, eInclude );
254 0 : uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
255 0 : uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
256 0 : if ( xResultSet.is() )
257 : {
258 : // go through the list: insert files as streams, insert folders as substorages using recursion
259 0 : while ( xResultSet->next() )
260 : {
261 0 : OUString aSourceURL( xRow->getString( 1 ) );
262 0 : sal_Bool bIsFolder( xRow->getBoolean(2) );
263 :
264 : // TODO/LATER: not sure whether the entry name must be encoded
265 : OUString aNewEntryName( INetURLObject( aSourceURL ).getName( INetURLObject::LAST_SEGMENT,
266 : true,
267 0 : INetURLObject::NO_DECODE ) );
268 0 : if ( bIsFolder )
269 : {
270 0 : uno::Reference< embed::XStorage > xSubStorage = xDest->openStorageElement( aNewEntryName,
271 0 : embed::ElementModes::READWRITE );
272 0 : if ( !xSubStorage.is() )
273 0 : throw uno::RuntimeException();
274 :
275 0 : uno::Reference< ucb::XCommandEnvironment > xDummyEnv;
276 0 : ::ucbhelper::Content aSourceContent( aSourceURL, xDummyEnv, comphelper::getProcessComponentContext() );
277 0 : CopyContentToStorage_Impl( &aSourceContent, xSubStorage );
278 : }
279 : else
280 : {
281 0 : CopyStreamToSubStream( aSourceURL, xDest, aNewEntryName );
282 : }
283 0 : }
284 : }
285 :
286 0 : uno::Reference< embed::XTransactedObject > xTransact( xDest, uno::UNO_QUERY );
287 0 : if ( xTransact.is() )
288 0 : xTransact->commit();
289 : }
290 0 : catch( ucb::InteractiveIOException& r )
291 : {
292 0 : if ( r.Code == ucb::IOErrorCode_NOT_EXISTING )
293 : OSL_FAIL( "The folder does not exist!\n" );
294 : else
295 0 : throw;
296 0 : }
297 0 : }
298 :
299 : //____________________________________________________________________________________________________
300 : // XInterface
301 : //____________________________________________________________________________________________________
302 :
303 : //-----------------------------------------------
304 2846 : uno::Any SAL_CALL FSStorage::queryInterface( const uno::Type& rType )
305 : throw( uno::RuntimeException )
306 : {
307 2846 : uno::Any aReturn;
308 5692 : aReturn <<= ::cppu::queryInterface
309 : ( rType
310 : , static_cast<lang::XTypeProvider*> ( this )
311 : , static_cast<embed::XStorage*> ( this )
312 : , static_cast<embed::XHierarchicalStorageAccess*> ( this )
313 : , static_cast<container::XNameAccess*> ( this )
314 : , static_cast<container::XElementAccess*> ( this )
315 : , static_cast<lang::XComponent*> ( this )
316 2846 : , static_cast<beans::XPropertySet*> ( this ) );
317 :
318 2846 : if ( aReturn.hasValue() == sal_True )
319 1907 : return aReturn ;
320 :
321 939 : return OWeakObject::queryInterface( rType );
322 : }
323 :
324 : //-----------------------------------------------
325 33294 : void SAL_CALL FSStorage::acquire() throw()
326 : {
327 33294 : OWeakObject::acquire();
328 33294 : }
329 :
330 : //-----------------------------------------------
331 33235 : void SAL_CALL FSStorage::release() throw()
332 : {
333 33235 : OWeakObject::release();
334 33235 : }
335 :
336 : //____________________________________________________________________________________________________
337 : // XTypeProvider
338 : //____________________________________________________________________________________________________
339 :
340 : //-----------------------------------------------
341 0 : uno::Sequence< uno::Type > SAL_CALL FSStorage::getTypes()
342 : throw( uno::RuntimeException )
343 : {
344 0 : if ( m_pImpl->m_pTypeCollection == NULL )
345 : {
346 0 : ::osl::MutexGuard aGuard( m_aMutex );
347 :
348 0 : if ( m_pImpl->m_pTypeCollection == NULL )
349 : {
350 : m_pImpl->m_pTypeCollection = new ::cppu::OTypeCollection
351 : ( ::getCppuType( ( const uno::Reference< lang::XTypeProvider >* )NULL )
352 : , ::getCppuType( ( const uno::Reference< embed::XStorage >* )NULL )
353 : , ::getCppuType( ( const uno::Reference< embed::XHierarchicalStorageAccess >* )NULL )
354 0 : , ::getCppuType( ( const uno::Reference< beans::XPropertySet >* )NULL ) );
355 0 : }
356 : }
357 :
358 0 : return m_pImpl->m_pTypeCollection->getTypes() ;
359 : }
360 :
361 : //-----------------------------------------------
362 0 : uno::Sequence< sal_Int8 > SAL_CALL FSStorage::getImplementationId()
363 : throw( uno::RuntimeException )
364 : {
365 : static ::cppu::OImplementationId* pID = NULL ;
366 :
367 0 : if ( pID == NULL )
368 : {
369 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ) ;
370 :
371 0 : if ( pID == NULL )
372 : {
373 0 : static ::cppu::OImplementationId aID( sal_False ) ;
374 0 : pID = &aID ;
375 0 : }
376 : }
377 :
378 0 : return pID->getImplementationId() ;
379 :
380 : }
381 :
382 : //____________________________________________________________________________________________________
383 : // XStorage
384 : //____________________________________________________________________________________________________
385 :
386 : //-----------------------------------------------
387 0 : void SAL_CALL FSStorage::copyToStorage( const uno::Reference< embed::XStorage >& xDest )
388 : throw ( embed::InvalidStorageException,
389 : io::IOException,
390 : lang::IllegalArgumentException,
391 : embed::StorageWrappedTargetException,
392 : uno::RuntimeException )
393 : {
394 0 : ::osl::MutexGuard aGuard( m_aMutex );
395 :
396 0 : if ( !m_pImpl )
397 0 : throw lang::DisposedException();
398 :
399 0 : if ( !xDest.is() || xDest == uno::Reference< uno::XInterface >( static_cast< OWeakObject*> ( this ), uno::UNO_QUERY ) )
400 0 : throw lang::IllegalArgumentException(); // TODO:
401 :
402 0 : if ( !GetContent() )
403 0 : throw io::IOException(); // TODO: error handling
404 :
405 : try
406 : {
407 0 : CopyContentToStorage_Impl( GetContent(), xDest );
408 : }
409 0 : catch( embed::InvalidStorageException& )
410 : {
411 0 : throw;
412 : }
413 0 : catch( lang::IllegalArgumentException& )
414 : {
415 0 : throw;
416 : }
417 0 : catch( embed::StorageWrappedTargetException& )
418 : {
419 0 : throw;
420 : }
421 0 : catch( io::IOException& )
422 : {
423 0 : throw;
424 : }
425 0 : catch( uno::RuntimeException& )
426 : {
427 0 : throw;
428 : }
429 0 : catch( uno::Exception& )
430 : {
431 0 : uno::Any aCaught( ::cppu::getCaughtException() );
432 : throw embed::StorageWrappedTargetException("Can't copy raw stream",
433 : uno::Reference< io::XInputStream >(),
434 0 : aCaught );
435 0 : }
436 0 : }
437 :
438 : //-----------------------------------------------
439 252 : uno::Reference< io::XStream > SAL_CALL FSStorage::openStreamElement(
440 : const OUString& aStreamName, sal_Int32 nOpenMode )
441 : throw ( embed::InvalidStorageException,
442 : lang::IllegalArgumentException,
443 : packages::WrongPasswordException,
444 : io::IOException,
445 : embed::StorageWrappedTargetException,
446 : uno::RuntimeException )
447 : {
448 252 : ::osl::MutexGuard aGuard( m_aMutex );
449 :
450 252 : if ( !m_pImpl )
451 0 : throw lang::DisposedException();
452 :
453 252 : if ( !GetContent() )
454 0 : throw io::IOException(); // TODO: error handling
455 :
456 : // TODO/LATER: may need possibility to create folder if it was removed, since the folder can not be locked
457 504 : INetURLObject aFileURL( m_pImpl->m_aURL );
458 252 : aFileURL.Append( aStreamName );
459 :
460 252 : if ( ::utl::UCBContentHelper::IsFolder( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
461 0 : throw io::IOException();
462 :
463 1008 : if ( ( nOpenMode & embed::ElementModes::NOCREATE )
464 756 : && !::utl::UCBContentHelper::IsDocument( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
465 0 : throw io::IOException(); // TODO:
466 :
467 504 : uno::Reference< ucb::XCommandEnvironment > xDummyEnv; // TODO: provide InteractionHandler if any
468 252 : uno::Reference< io::XStream > xResult;
469 : try
470 : {
471 252 : if ( nOpenMode & embed::ElementModes::WRITE )
472 : {
473 0 : if ( isLocalFile_Impl( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
474 : {
475 : uno::Reference<ucb::XSimpleFileAccess3> xSimpleFileAccess(
476 0 : ucb::SimpleFileAccess::create( m_pImpl->m_xContext ) );
477 0 : xResult = xSimpleFileAccess->openFileReadWrite( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) );
478 : }
479 : else
480 : {
481 : // TODO: test whether it really works for http and fwp
482 : SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( aFileURL.GetMainURL( INetURLObject::NO_DECODE ),
483 0 : STREAM_STD_WRITE );
484 0 : if ( pStream )
485 : {
486 0 : if ( !pStream->GetError() )
487 0 : xResult = uno::Reference < io::XStream >( new ::utl::OStreamWrapper( *pStream ) );
488 : else
489 0 : delete pStream;
490 : }
491 : }
492 :
493 0 : if ( !xResult.is() )
494 0 : throw io::IOException();
495 :
496 0 : if ( ( nOpenMode & embed::ElementModes::TRUNCATE ) )
497 : {
498 0 : uno::Reference< io::XTruncate > xTrunc( xResult->getOutputStream(), uno::UNO_QUERY_THROW );
499 0 : xTrunc->truncate();
500 : }
501 : }
502 : else
503 : {
504 756 : if ( ( nOpenMode & embed::ElementModes::TRUNCATE )
505 1008 : || !::utl::UCBContentHelper::IsDocument( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
506 47 : throw io::IOException(); // TODO: access denied
507 :
508 205 : ::ucbhelper::Content aResultContent( aFileURL.GetMainURL( INetURLObject::NO_DECODE ), xDummyEnv, comphelper::getProcessComponentContext() );
509 410 : uno::Reference< io::XInputStream > xInStream = aResultContent.openStream();
510 410 : xResult = static_cast< io::XStream* >( new OFSInputStreamContainer( xInStream ) );
511 : }
512 : }
513 0 : catch( embed::InvalidStorageException& )
514 : {
515 0 : throw;
516 : }
517 0 : catch( lang::IllegalArgumentException& )
518 : {
519 0 : throw;
520 : }
521 0 : catch( packages::WrongPasswordException& )
522 : {
523 0 : throw;
524 : }
525 0 : catch( embed::StorageWrappedTargetException& )
526 : {
527 0 : throw;
528 : }
529 94 : catch( io::IOException& )
530 : {
531 47 : throw;
532 : }
533 0 : catch( uno::RuntimeException& )
534 : {
535 0 : throw;
536 : }
537 0 : catch( uno::Exception& )
538 : {
539 0 : uno::Any aCaught( ::cppu::getCaughtException() );
540 : throw embed::StorageWrappedTargetException("Can't copy raw stream",
541 : uno::Reference< io::XInputStream >(),
542 0 : aCaught );
543 : }
544 :
545 457 : return xResult;
546 : }
547 :
548 : //-----------------------------------------------
549 0 : uno::Reference< io::XStream > SAL_CALL FSStorage::openEncryptedStreamElement(
550 : const OUString&, sal_Int32, const OUString& )
551 : throw ( embed::InvalidStorageException,
552 : lang::IllegalArgumentException,
553 : packages::NoEncryptionException,
554 : packages::WrongPasswordException,
555 : io::IOException,
556 : embed::StorageWrappedTargetException,
557 : uno::RuntimeException )
558 : {
559 0 : throw packages::NoEncryptionException();
560 : }
561 :
562 : //-----------------------------------------------
563 1107 : uno::Reference< embed::XStorage > SAL_CALL FSStorage::openStorageElement(
564 : const OUString& aStorName, sal_Int32 nStorageMode )
565 : throw ( embed::InvalidStorageException,
566 : lang::IllegalArgumentException,
567 : io::IOException,
568 : embed::StorageWrappedTargetException,
569 : uno::RuntimeException )
570 : {
571 1107 : ::osl::MutexGuard aGuard( m_aMutex );
572 :
573 1107 : if ( !m_pImpl )
574 0 : throw lang::DisposedException();
575 :
576 1107 : if ( !GetContent() )
577 0 : throw io::IOException(); // TODO: error handling
578 :
579 1107 : if ( ( nStorageMode & embed::ElementModes::WRITE )
580 513 : && !( m_pImpl->m_nMode & embed::ElementModes::WRITE ) )
581 0 : throw io::IOException(); // TODO: error handling
582 :
583 : // TODO/LATER: may need possibility to create folder if it was removed, since the folder can not be locked
584 2214 : INetURLObject aFolderURL( m_pImpl->m_aURL );
585 1107 : aFolderURL.Append( aStorName );
586 :
587 1107 : sal_Bool bFolderExists = ::utl::UCBContentHelper::IsFolder( aFolderURL.GetMainURL( INetURLObject::NO_DECODE ) );
588 1107 : if ( !bFolderExists && ::utl::UCBContentHelper::IsDocument( aFolderURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
589 0 : throw io::IOException(); // TODO:
590 :
591 1107 : if ( ( nStorageMode & embed::ElementModes::NOCREATE ) && !bFolderExists )
592 7 : throw io::IOException(); // TODO:
593 :
594 2200 : uno::Reference< ucb::XCommandEnvironment > xDummyEnv; // TODO: provide InteractionHandler if any
595 1100 : uno::Reference< embed::XStorage > xResult;
596 : try
597 : {
598 1100 : if ( nStorageMode & embed::ElementModes::WRITE )
599 : {
600 513 : if ( ( nStorageMode & embed::ElementModes::TRUNCATE ) && bFolderExists )
601 : {
602 0 : ::utl::UCBContentHelper::Kill( aFolderURL.GetMainURL( INetURLObject::NO_DECODE ) );
603 : bFolderExists =
604 0 : MakeFolderNoUI( aFolderURL.GetMainURL( INetURLObject::NO_DECODE ) ); // TODO: not atomic :(
605 : }
606 513 : else if ( !bFolderExists )
607 : {
608 : bFolderExists =
609 247 : MakeFolderNoUI( aFolderURL.GetMainURL( INetURLObject::NO_DECODE ) ); // TODO: not atomic :(
610 : }
611 : }
612 587 : else if ( ( nStorageMode & embed::ElementModes::TRUNCATE ) )
613 0 : throw io::IOException(); // TODO: access denied
614 :
615 1100 : if ( !bFolderExists )
616 6 : throw io::IOException(); // there is no such folder
617 :
618 1094 : ::ucbhelper::Content aResultContent( aFolderURL.GetMainURL( INetURLObject::NO_DECODE ), xDummyEnv, comphelper::getProcessComponentContext() );
619 2188 : xResult = uno::Reference< embed::XStorage >(
620 : static_cast< OWeakObject* >( new FSStorage( aResultContent,
621 : nStorageMode,
622 2188 : m_pImpl->m_xContext ) ),
623 2188 : uno::UNO_QUERY );
624 : }
625 0 : catch( embed::InvalidStorageException& )
626 : {
627 0 : throw;
628 : }
629 0 : catch( lang::IllegalArgumentException& )
630 : {
631 0 : throw;
632 : }
633 0 : catch( embed::StorageWrappedTargetException& )
634 : {
635 0 : throw;
636 : }
637 12 : catch( io::IOException& )
638 : {
639 6 : throw;
640 : }
641 0 : catch( uno::RuntimeException& )
642 : {
643 0 : throw;
644 : }
645 0 : catch( uno::Exception& )
646 : {
647 0 : uno::Any aCaught( ::cppu::getCaughtException() );
648 : throw embed::StorageWrappedTargetException("Can't copy raw stream",
649 : uno::Reference< io::XInputStream >(),
650 0 : aCaught );
651 : }
652 :
653 2201 : return xResult;
654 : }
655 :
656 : //-----------------------------------------------
657 0 : uno::Reference< io::XStream > SAL_CALL FSStorage::cloneStreamElement( const OUString& aStreamName )
658 : throw ( embed::InvalidStorageException,
659 : lang::IllegalArgumentException,
660 : packages::WrongPasswordException,
661 : io::IOException,
662 : embed::StorageWrappedTargetException,
663 : uno::RuntimeException )
664 : {
665 0 : ::osl::MutexGuard aGuard( m_aMutex );
666 :
667 0 : if ( !m_pImpl )
668 0 : throw lang::DisposedException();
669 :
670 0 : if ( !GetContent() )
671 0 : throw io::IOException(); // TODO: error handling
672 :
673 : // TODO/LATER: may need possibility to create folder if it was removed, since the folder can not be locked
674 0 : INetURLObject aFileURL( m_pImpl->m_aURL );
675 0 : aFileURL.Append( aStreamName );
676 :
677 0 : uno::Reference < io::XStream > xTempResult;
678 : try
679 : {
680 0 : uno::Reference< ucb::XCommandEnvironment > xDummyEnv;
681 0 : ::ucbhelper::Content aResultContent( aFileURL.GetMainURL( INetURLObject::NO_DECODE ), xDummyEnv, comphelper::getProcessComponentContext() );
682 0 : uno::Reference< io::XInputStream > xInStream = aResultContent.openStream();
683 :
684 0 : xTempResult = io::TempFile::create(m_pImpl->m_xContext);
685 0 : uno::Reference < io::XOutputStream > xTempOut = xTempResult->getOutputStream();
686 0 : uno::Reference < io::XInputStream > xTempIn = xTempResult->getInputStream();
687 :
688 0 : if ( !xTempOut.is() || !xTempIn.is() )
689 0 : throw io::IOException();
690 :
691 0 : ::comphelper::OStorageHelper::CopyInputToOutput( xInStream, xTempOut );
692 0 : xTempOut->closeOutput();
693 : }
694 0 : catch( embed::InvalidStorageException& )
695 : {
696 0 : throw;
697 : }
698 0 : catch( lang::IllegalArgumentException& )
699 : {
700 0 : throw;
701 : }
702 0 : catch( packages::WrongPasswordException& )
703 : {
704 0 : throw;
705 : }
706 0 : catch( io::IOException& )
707 : {
708 0 : throw;
709 : }
710 0 : catch( embed::StorageWrappedTargetException& )
711 : {
712 0 : throw;
713 : }
714 0 : catch( uno::RuntimeException& )
715 : {
716 0 : throw;
717 : }
718 0 : catch( uno::Exception& )
719 : {
720 0 : uno::Any aCaught( ::cppu::getCaughtException() );
721 : throw embed::StorageWrappedTargetException("Can't copy raw stream",
722 : uno::Reference< io::XInputStream >(),
723 0 : aCaught );
724 : }
725 :
726 0 : return xTempResult;
727 : }
728 :
729 : //-----------------------------------------------
730 0 : uno::Reference< io::XStream > SAL_CALL FSStorage::cloneEncryptedStreamElement(
731 : const OUString&,
732 : const OUString& )
733 : throw ( embed::InvalidStorageException,
734 : lang::IllegalArgumentException,
735 : packages::NoEncryptionException,
736 : packages::WrongPasswordException,
737 : io::IOException,
738 : embed::StorageWrappedTargetException,
739 : uno::RuntimeException )
740 : {
741 0 : throw packages::NoEncryptionException();
742 : }
743 :
744 : //-----------------------------------------------
745 0 : void SAL_CALL FSStorage::copyLastCommitTo(
746 : const uno::Reference< embed::XStorage >& xTargetStorage )
747 : throw ( embed::InvalidStorageException,
748 : lang::IllegalArgumentException,
749 : io::IOException,
750 : embed::StorageWrappedTargetException,
751 : uno::RuntimeException )
752 : {
753 0 : copyToStorage( xTargetStorage );
754 0 : }
755 :
756 : //-----------------------------------------------
757 0 : void SAL_CALL FSStorage::copyStorageElementLastCommitTo(
758 : const OUString& aStorName,
759 : const uno::Reference< embed::XStorage >& xTargetStorage )
760 : throw ( embed::InvalidStorageException,
761 : lang::IllegalArgumentException,
762 : io::IOException,
763 : embed::StorageWrappedTargetException,
764 : uno::RuntimeException )
765 : {
766 0 : ::osl::MutexGuard aGuard( m_aMutex );
767 :
768 0 : if ( !m_pImpl )
769 0 : throw lang::DisposedException();
770 :
771 0 : uno::Reference< embed::XStorage > xSourceStor( openStorageElement( aStorName, embed::ElementModes::READ ),
772 0 : uno::UNO_QUERY_THROW );
773 0 : xSourceStor->copyToStorage( xTargetStorage );
774 0 : }
775 :
776 : //-----------------------------------------------
777 0 : sal_Bool SAL_CALL FSStorage::isStreamElement( const OUString& aElementName )
778 : throw ( embed::InvalidStorageException,
779 : lang::IllegalArgumentException,
780 : container::NoSuchElementException,
781 : uno::RuntimeException )
782 : {
783 0 : ::osl::MutexGuard aGuard( m_aMutex );
784 :
785 0 : if ( !m_pImpl )
786 0 : throw lang::DisposedException();
787 :
788 0 : if ( !GetContent() )
789 0 : throw embed::InvalidStorageException(); // TODO: error handling
790 :
791 0 : INetURLObject aURL( m_pImpl->m_aURL );
792 0 : aURL.Append( aElementName );
793 :
794 0 : return !::utl::UCBContentHelper::IsFolder( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
795 : }
796 :
797 : //-----------------------------------------------
798 0 : sal_Bool SAL_CALL FSStorage::isStorageElement( const OUString& aElementName )
799 : throw ( embed::InvalidStorageException,
800 : lang::IllegalArgumentException,
801 : container::NoSuchElementException,
802 : uno::RuntimeException )
803 : {
804 0 : ::osl::MutexGuard aGuard( m_aMutex );
805 :
806 0 : if ( !m_pImpl )
807 0 : throw lang::DisposedException();
808 :
809 0 : if ( !GetContent() )
810 0 : throw embed::InvalidStorageException(); // TODO: error handling
811 :
812 0 : INetURLObject aURL( m_pImpl->m_aURL );
813 0 : aURL.Append( aElementName );
814 :
815 0 : return ::utl::UCBContentHelper::IsFolder( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
816 : }
817 :
818 : //-----------------------------------------------
819 0 : void SAL_CALL FSStorage::removeElement( const OUString& aElementName )
820 : throw ( embed::InvalidStorageException,
821 : lang::IllegalArgumentException,
822 : container::NoSuchElementException,
823 : io::IOException,
824 : embed::StorageWrappedTargetException,
825 : uno::RuntimeException )
826 : {
827 0 : ::osl::MutexGuard aGuard( m_aMutex );
828 :
829 0 : if ( !m_pImpl )
830 0 : throw lang::DisposedException();
831 :
832 0 : if ( !GetContent() )
833 0 : throw io::IOException(); // TODO: error handling
834 :
835 0 : INetURLObject aURL( m_pImpl->m_aURL );
836 0 : aURL.Append( aElementName );
837 :
838 0 : if ( !::utl::UCBContentHelper::IsFolder( aURL.GetMainURL( INetURLObject::NO_DECODE ) )
839 0 : && !::utl::UCBContentHelper::IsDocument( aURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
840 0 : throw container::NoSuchElementException(); // TODO:
841 :
842 0 : ::utl::UCBContentHelper::Kill( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
843 0 : }
844 :
845 : //-----------------------------------------------
846 0 : void SAL_CALL FSStorage::renameElement( const OUString& aElementName, const OUString& aNewName )
847 : throw ( embed::InvalidStorageException,
848 : lang::IllegalArgumentException,
849 : container::NoSuchElementException,
850 : container::ElementExistException,
851 : io::IOException,
852 : embed::StorageWrappedTargetException,
853 : uno::RuntimeException )
854 : {
855 0 : ::osl::MutexGuard aGuard( m_aMutex );
856 :
857 0 : if ( !m_pImpl )
858 0 : throw lang::DisposedException();
859 :
860 0 : if ( !GetContent() )
861 0 : throw io::IOException(); // TODO: error handling
862 :
863 0 : INetURLObject aOldURL( m_pImpl->m_aURL );
864 0 : aOldURL.Append( aElementName );
865 :
866 0 : INetURLObject aNewURL( m_pImpl->m_aURL );
867 0 : aNewURL.Append( aNewName );
868 :
869 0 : if ( !::utl::UCBContentHelper::IsFolder( aOldURL.GetMainURL( INetURLObject::NO_DECODE ) )
870 0 : && !::utl::UCBContentHelper::IsDocument( aOldURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
871 0 : throw container::NoSuchElementException(); // TODO:
872 :
873 0 : if ( ::utl::UCBContentHelper::IsFolder( aNewURL.GetMainURL( INetURLObject::NO_DECODE ) )
874 0 : || ::utl::UCBContentHelper::IsDocument( aNewURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
875 0 : throw container::ElementExistException(); // TODO:
876 :
877 : try
878 : {
879 0 : uno::Reference< ucb::XCommandEnvironment > xDummyEnv;
880 0 : ::ucbhelper::Content aSourceContent( aOldURL.GetMainURL( INetURLObject::NO_DECODE ), xDummyEnv, comphelper::getProcessComponentContext() );
881 :
882 0 : if ( !GetContent()->transferContent( aSourceContent,
883 : ::ucbhelper::InsertOperation_MOVE,
884 : aNewName,
885 0 : ucb::NameClash::ERROR ) )
886 0 : throw io::IOException(); // TODO: error handling
887 : }
888 0 : catch( embed::InvalidStorageException& )
889 : {
890 0 : throw;
891 : }
892 0 : catch( lang::IllegalArgumentException& )
893 : {
894 0 : throw;
895 : }
896 0 : catch( container::NoSuchElementException& )
897 : {
898 0 : throw;
899 : }
900 0 : catch( container::ElementExistException& )
901 : {
902 0 : throw;
903 : }
904 0 : catch( io::IOException& )
905 : {
906 0 : throw;
907 : }
908 0 : catch( embed::StorageWrappedTargetException& )
909 : {
910 0 : throw;
911 : }
912 0 : catch( uno::RuntimeException& )
913 : {
914 0 : throw;
915 : }
916 0 : catch( uno::Exception& )
917 : {
918 0 : uno::Any aCaught( ::cppu::getCaughtException() );
919 : throw embed::StorageWrappedTargetException("Can't copy raw stream",
920 : uno::Reference< io::XInputStream >(),
921 0 : aCaught );
922 0 : }
923 0 : }
924 :
925 : //-----------------------------------------------
926 0 : void SAL_CALL FSStorage::copyElementTo( const OUString& aElementName,
927 : const uno::Reference< embed::XStorage >& xDest,
928 : const OUString& aNewName )
929 : throw ( embed::InvalidStorageException,
930 : lang::IllegalArgumentException,
931 : container::NoSuchElementException,
932 : container::ElementExistException,
933 : io::IOException,
934 : embed::StorageWrappedTargetException,
935 : uno::RuntimeException )
936 : {
937 0 : ::osl::MutexGuard aGuard( m_aMutex );
938 :
939 0 : if ( !m_pImpl )
940 0 : throw lang::DisposedException();
941 :
942 0 : if ( !xDest.is() )
943 0 : throw uno::RuntimeException();
944 :
945 0 : if ( !GetContent() )
946 0 : throw io::IOException(); // TODO: error handling
947 :
948 0 : INetURLObject aOwnURL( m_pImpl->m_aURL );
949 0 : aOwnURL.Append( aElementName );
950 :
951 0 : if ( xDest->hasByName( aNewName ) )
952 0 : throw container::ElementExistException(); // TODO:
953 :
954 : try
955 : {
956 0 : uno::Reference< ucb::XCommandEnvironment > xDummyEnv;
957 0 : if ( ::utl::UCBContentHelper::IsFolder( aOwnURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
958 : {
959 0 : ::ucbhelper::Content aSourceContent( aOwnURL.GetMainURL( INetURLObject::NO_DECODE ), xDummyEnv, comphelper::getProcessComponentContext() );
960 : uno::Reference< embed::XStorage > xDestSubStor(
961 0 : xDest->openStorageElement( aNewName, embed::ElementModes::READWRITE ),
962 0 : uno::UNO_QUERY_THROW );
963 :
964 0 : CopyContentToStorage_Impl( &aSourceContent, xDestSubStor );
965 : }
966 0 : else if ( ::utl::UCBContentHelper::IsDocument( aOwnURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
967 : {
968 0 : CopyStreamToSubStream( aOwnURL.GetMainURL( INetURLObject::NO_DECODE ), xDest, aNewName );
969 : }
970 : else
971 0 : throw container::NoSuchElementException(); // TODO:
972 : }
973 0 : catch( embed::InvalidStorageException& )
974 : {
975 0 : throw;
976 : }
977 0 : catch( lang::IllegalArgumentException& )
978 : {
979 0 : throw;
980 : }
981 0 : catch( container::NoSuchElementException& )
982 : {
983 0 : throw;
984 : }
985 0 : catch( container::ElementExistException& )
986 : {
987 0 : throw;
988 : }
989 0 : catch( embed::StorageWrappedTargetException& )
990 : {
991 0 : throw;
992 : }
993 0 : catch( io::IOException& )
994 : {
995 0 : throw;
996 : }
997 0 : catch( uno::RuntimeException& )
998 : {
999 0 : throw;
1000 : }
1001 0 : catch( uno::Exception& )
1002 : {
1003 0 : uno::Any aCaught( ::cppu::getCaughtException() );
1004 : throw embed::StorageWrappedTargetException("Can't copy raw stream",
1005 : uno::Reference< io::XInputStream >(),
1006 0 : aCaught );
1007 0 : }
1008 0 : }
1009 :
1010 : //-----------------------------------------------
1011 0 : void SAL_CALL FSStorage::moveElementTo( const OUString& aElementName,
1012 : const uno::Reference< embed::XStorage >& xDest,
1013 : const OUString& aNewName )
1014 : throw ( embed::InvalidStorageException,
1015 : lang::IllegalArgumentException,
1016 : container::NoSuchElementException,
1017 : container::ElementExistException,
1018 : io::IOException,
1019 : embed::StorageWrappedTargetException,
1020 : uno::RuntimeException )
1021 : {
1022 0 : ::osl::MutexGuard aGuard( m_aMutex );
1023 0 : copyElementTo( aElementName, xDest, aNewName );
1024 :
1025 0 : INetURLObject aOwnURL( m_pImpl->m_aURL );
1026 0 : aOwnURL.Append( aElementName );
1027 0 : if ( !::utl::UCBContentHelper::Kill( aOwnURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
1028 0 : throw io::IOException(); // TODO: error handling
1029 0 : }
1030 :
1031 : //____________________________________________________________________________________________________
1032 : // XNameAccess
1033 : //____________________________________________________________________________________________________
1034 :
1035 : //-----------------------------------------------
1036 224 : uno::Any SAL_CALL FSStorage::getByName( const OUString& aName )
1037 : throw ( container::NoSuchElementException,
1038 : lang::WrappedTargetException,
1039 : uno::RuntimeException )
1040 : {
1041 224 : ::osl::MutexGuard aGuard( m_aMutex );
1042 :
1043 224 : if ( !m_pImpl )
1044 0 : throw lang::DisposedException();
1045 :
1046 224 : if ( !GetContent() )
1047 0 : throw io::IOException(); // TODO: error handling
1048 :
1049 224 : if ( aName.isEmpty() )
1050 0 : throw lang::IllegalArgumentException();
1051 :
1052 448 : INetURLObject aURL( m_pImpl->m_aURL );
1053 224 : aURL.Append( aName );
1054 :
1055 224 : uno::Any aResult;
1056 : try
1057 : {
1058 224 : if ( ::utl::UCBContentHelper::IsFolder( aURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
1059 : {
1060 224 : aResult <<= openStorageElement( aName, embed::ElementModes::READ );
1061 : }
1062 0 : else if ( ::utl::UCBContentHelper::IsDocument( aURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
1063 : {
1064 0 : aResult <<= openStreamElement( aName, embed::ElementModes::READ );
1065 : }
1066 : else
1067 0 : throw container::NoSuchElementException(); // TODO:
1068 : }
1069 0 : catch( container::NoSuchElementException& )
1070 : {
1071 0 : throw;
1072 : }
1073 0 : catch( lang::WrappedTargetException& )
1074 : {
1075 0 : throw;
1076 : }
1077 0 : catch( uno::RuntimeException& )
1078 : {
1079 0 : throw;
1080 : }
1081 0 : catch ( uno::Exception& )
1082 : {
1083 0 : uno::Any aCaught( ::cppu::getCaughtException() );
1084 : throw lang::WrappedTargetException( OUString("Can not open element!\n"),
1085 : uno::Reference< uno::XInterface >( static_cast< OWeakObject* >( this ),
1086 : uno::UNO_QUERY ),
1087 0 : aCaught );
1088 : }
1089 :
1090 448 : return aResult;
1091 : }
1092 :
1093 :
1094 : //-----------------------------------------------
1095 14765 : uno::Sequence< OUString > SAL_CALL FSStorage::getElementNames()
1096 : throw ( uno::RuntimeException )
1097 : {
1098 14765 : ::osl::MutexGuard aGuard( m_aMutex );
1099 :
1100 14765 : if ( !m_pImpl )
1101 0 : throw lang::DisposedException();
1102 :
1103 14765 : if ( !GetContent() )
1104 0 : throw io::IOException(); // TODO: error handling
1105 :
1106 29530 : uno::Sequence< OUString > aProps( 1 );
1107 14765 : OUString* pProps = aProps.getArray();
1108 14765 : pProps[0] = OUString("Title");
1109 14765 : ::ucbhelper::ResultSetInclude eInclude = ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS;
1110 :
1111 14765 : uno::Sequence< OUString > aResult;
1112 14765 : sal_Int32 nSize = 0;
1113 :
1114 : try
1115 : {
1116 14765 : uno::Reference< sdbc::XResultSet > xResultSet = GetContent()->createCursor( aProps, eInclude );
1117 29530 : uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
1118 29530 : uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
1119 14765 : if ( xResultSet.is() )
1120 : {
1121 : // go through the list
1122 33124 : while ( xResultSet->next() )
1123 : {
1124 3594 : OUString aName( xRow->getString( 1 ) );
1125 3594 : aResult.realloc( ++nSize );
1126 3594 : aResult[nSize-1] = aName;
1127 3594 : }
1128 14765 : }
1129 : }
1130 0 : catch( const ucb::InteractiveIOException& r )
1131 : {
1132 0 : if ( r.Code == ucb::IOErrorCode_NOT_EXISTING )
1133 : OSL_FAIL( "The folder does not exist!\n" );
1134 : else
1135 : {
1136 0 : uno::Any aCaught( ::cppu::getCaughtException() );
1137 : throw lang::WrappedTargetRuntimeException( OUString("Can not open storage!\n"),
1138 : uno::Reference< uno::XInterface >( static_cast< OWeakObject* >( this ),
1139 : uno::UNO_QUERY ),
1140 0 : aCaught );
1141 : }
1142 : }
1143 0 : catch( uno::RuntimeException& )
1144 : {
1145 0 : throw;
1146 : }
1147 0 : catch ( uno::Exception& )
1148 : {
1149 0 : uno::Any aCaught( ::cppu::getCaughtException() );
1150 : throw lang::WrappedTargetRuntimeException( OUString("Can not open storage!\n"),
1151 : uno::Reference< uno::XInterface >( static_cast< OWeakObject* >( this ),
1152 : uno::UNO_QUERY ),
1153 0 : aCaught );
1154 : }
1155 :
1156 29530 : return aResult;
1157 : }
1158 :
1159 :
1160 : //-----------------------------------------------
1161 528 : sal_Bool SAL_CALL FSStorage::hasByName( const OUString& aName )
1162 : throw ( uno::RuntimeException )
1163 : {
1164 528 : ::osl::MutexGuard aGuard( m_aMutex );
1165 :
1166 528 : if ( !m_pImpl )
1167 0 : throw lang::DisposedException();
1168 :
1169 : try
1170 : {
1171 528 : if ( !GetContent() )
1172 0 : throw io::IOException(); // TODO: error handling
1173 :
1174 528 : if ( aName.isEmpty() )
1175 0 : throw lang::IllegalArgumentException();
1176 : }
1177 0 : catch( uno::RuntimeException& )
1178 : {
1179 0 : throw;
1180 : }
1181 0 : catch ( uno::Exception& )
1182 : {
1183 0 : uno::Any aCaught( ::cppu::getCaughtException() );
1184 : throw lang::WrappedTargetRuntimeException( OUString("Can not open storage!\n"),
1185 : uno::Reference< uno::XInterface >( static_cast< OWeakObject* >( this ),
1186 : uno::UNO_QUERY ),
1187 0 : aCaught );
1188 : }
1189 :
1190 1056 : INetURLObject aURL( m_pImpl->m_aURL );
1191 528 : aURL.Append( aName );
1192 :
1193 2112 : return ( ::utl::UCBContentHelper::IsFolder( aURL.GetMainURL( INetURLObject::NO_DECODE ) )
1194 2112 : || ::utl::UCBContentHelper::IsDocument( aURL.GetMainURL( INetURLObject::NO_DECODE ) ) );
1195 : }
1196 :
1197 : //-----------------------------------------------
1198 0 : uno::Type SAL_CALL FSStorage::getElementType()
1199 : throw ( uno::RuntimeException )
1200 : {
1201 0 : ::osl::MutexGuard aGuard( m_aMutex );
1202 :
1203 0 : if ( !m_pImpl )
1204 0 : throw lang::DisposedException();
1205 :
1206 : // it is a multitype container
1207 0 : return uno::Type();
1208 : }
1209 :
1210 : //-----------------------------------------------
1211 0 : sal_Bool SAL_CALL FSStorage::hasElements()
1212 : throw ( uno::RuntimeException )
1213 : {
1214 0 : ::osl::MutexGuard aGuard( m_aMutex );
1215 :
1216 0 : if ( !m_pImpl )
1217 0 : throw lang::DisposedException();
1218 :
1219 0 : if ( !GetContent() )
1220 0 : throw io::IOException(); // TODO: error handling
1221 :
1222 0 : uno::Sequence< OUString > aProps( 1 );
1223 0 : aProps[0] = OUString("TargetURL");
1224 0 : ::ucbhelper::ResultSetInclude eInclude = ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS;
1225 :
1226 : try
1227 : {
1228 0 : uno::Reference< sdbc::XResultSet > xResultSet = GetContent()->createCursor( aProps, eInclude );
1229 0 : return ( xResultSet.is() && xResultSet->next() );
1230 : }
1231 0 : catch( uno::Exception& )
1232 : {
1233 0 : throw uno::RuntimeException();
1234 0 : }
1235 : }
1236 :
1237 :
1238 : //____________________________________________________________________________________________________
1239 : // XDisposable
1240 : //____________________________________________________________________________________________________
1241 :
1242 : //-----------------------------------------------
1243 1178 : void SAL_CALL FSStorage::dispose()
1244 : throw ( uno::RuntimeException )
1245 : {
1246 1178 : ::osl::MutexGuard aGuard( m_aMutex );
1247 :
1248 1178 : if ( !m_pImpl )
1249 0 : throw lang::DisposedException();
1250 :
1251 1178 : if ( m_pImpl->m_pListenersContainer )
1252 : {
1253 0 : lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
1254 0 : m_pImpl->m_pListenersContainer->disposeAndClear( aSource );
1255 : }
1256 :
1257 1178 : delete m_pImpl;
1258 1178 : m_pImpl = NULL;
1259 1178 : }
1260 :
1261 : //-----------------------------------------------
1262 0 : void SAL_CALL FSStorage::addEventListener(
1263 : const uno::Reference< lang::XEventListener >& xListener )
1264 : throw ( uno::RuntimeException )
1265 : {
1266 0 : ::osl::MutexGuard aGuard( m_aMutex );
1267 :
1268 0 : if ( !m_pImpl )
1269 0 : throw lang::DisposedException();
1270 :
1271 0 : if ( !m_pImpl->m_pListenersContainer )
1272 0 : m_pImpl->m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
1273 :
1274 0 : m_pImpl->m_pListenersContainer->addInterface( xListener );
1275 0 : }
1276 :
1277 : //-----------------------------------------------
1278 0 : void SAL_CALL FSStorage::removeEventListener(
1279 : const uno::Reference< lang::XEventListener >& xListener )
1280 : throw ( uno::RuntimeException )
1281 : {
1282 0 : ::osl::MutexGuard aGuard( m_aMutex );
1283 :
1284 0 : if ( !m_pImpl )
1285 0 : throw lang::DisposedException();
1286 :
1287 0 : if ( m_pImpl->m_pListenersContainer )
1288 0 : m_pImpl->m_pListenersContainer->removeInterface( xListener );
1289 0 : }
1290 :
1291 : //____________________________________________________________________________________________________
1292 : // XPropertySet
1293 : //____________________________________________________________________________________________________
1294 :
1295 : //-----------------------------------------------
1296 0 : uno::Reference< beans::XPropertySetInfo > SAL_CALL FSStorage::getPropertySetInfo()
1297 : throw ( uno::RuntimeException )
1298 : {
1299 0 : ::osl::MutexGuard aGuard( m_aMutex );
1300 :
1301 0 : if ( !m_pImpl )
1302 0 : throw lang::DisposedException();
1303 :
1304 : //TODO:
1305 0 : return uno::Reference< beans::XPropertySetInfo >();
1306 : }
1307 :
1308 :
1309 : //-----------------------------------------------
1310 0 : void SAL_CALL FSStorage::setPropertyValue( const OUString& aPropertyName, const uno::Any& )
1311 : throw ( beans::UnknownPropertyException,
1312 : beans::PropertyVetoException,
1313 : lang::IllegalArgumentException,
1314 : lang::WrappedTargetException,
1315 : uno::RuntimeException )
1316 : {
1317 0 : ::osl::MutexGuard aGuard( m_aMutex );
1318 :
1319 0 : if ( !m_pImpl )
1320 0 : throw lang::DisposedException();
1321 :
1322 0 : if ( aPropertyName == "URL" || aPropertyName == "OpenMode" )
1323 0 : throw beans::PropertyVetoException(); // TODO
1324 : else
1325 0 : throw beans::UnknownPropertyException(); // TODO
1326 : }
1327 :
1328 :
1329 : //-----------------------------------------------
1330 150 : uno::Any SAL_CALL FSStorage::getPropertyValue( const OUString& aPropertyName )
1331 : throw ( beans::UnknownPropertyException,
1332 : lang::WrappedTargetException,
1333 : uno::RuntimeException )
1334 : {
1335 150 : ::osl::MutexGuard aGuard( m_aMutex );
1336 :
1337 150 : if ( !m_pImpl )
1338 0 : throw lang::DisposedException();
1339 :
1340 150 : if ( aPropertyName == "URL" )
1341 0 : return uno::makeAny( m_pImpl->m_aURL );
1342 150 : else if ( aPropertyName == "OpenMode" )
1343 150 : return uno::makeAny( m_pImpl->m_nMode );
1344 :
1345 0 : throw beans::UnknownPropertyException(); // TODO
1346 : }
1347 :
1348 :
1349 : //-----------------------------------------------
1350 0 : void SAL_CALL FSStorage::addPropertyChangeListener(
1351 : const OUString& /*aPropertyName*/,
1352 : const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
1353 : throw ( beans::UnknownPropertyException,
1354 : lang::WrappedTargetException,
1355 : uno::RuntimeException )
1356 : {
1357 0 : ::osl::MutexGuard aGuard( m_aMutex );
1358 :
1359 0 : if ( !m_pImpl )
1360 0 : throw lang::DisposedException();
1361 :
1362 : //TODO:
1363 0 : }
1364 :
1365 :
1366 : //-----------------------------------------------
1367 0 : void SAL_CALL FSStorage::removePropertyChangeListener(
1368 : const OUString& /*aPropertyName*/,
1369 : const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
1370 : throw ( beans::UnknownPropertyException,
1371 : lang::WrappedTargetException,
1372 : uno::RuntimeException )
1373 : {
1374 0 : ::osl::MutexGuard aGuard( m_aMutex );
1375 :
1376 0 : if ( !m_pImpl )
1377 0 : throw lang::DisposedException();
1378 :
1379 : //TODO:
1380 0 : }
1381 :
1382 :
1383 : //-----------------------------------------------
1384 0 : void SAL_CALL FSStorage::addVetoableChangeListener(
1385 : const OUString& /*PropertyName*/,
1386 : const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
1387 : throw ( beans::UnknownPropertyException,
1388 : lang::WrappedTargetException,
1389 : uno::RuntimeException )
1390 : {
1391 0 : ::osl::MutexGuard aGuard( m_aMutex );
1392 :
1393 0 : if ( !m_pImpl )
1394 0 : throw lang::DisposedException();
1395 :
1396 : //TODO:
1397 0 : }
1398 :
1399 :
1400 : //-----------------------------------------------
1401 0 : void SAL_CALL FSStorage::removeVetoableChangeListener(
1402 : const OUString& /*PropertyName*/,
1403 : const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
1404 : throw ( beans::UnknownPropertyException,
1405 : lang::WrappedTargetException,
1406 : uno::RuntimeException )
1407 : {
1408 0 : ::osl::MutexGuard aGuard( m_aMutex );
1409 :
1410 0 : if ( !m_pImpl )
1411 0 : throw lang::DisposedException();
1412 :
1413 : //TODO:
1414 0 : }
1415 :
1416 : //____________________________________________________________________________________________________
1417 : // XHierarchicalStorageAccess
1418 : //____________________________________________________________________________________________________
1419 : //-----------------------------------------------
1420 0 : uno::Reference< embed::XExtendedStorageStream > SAL_CALL FSStorage::openStreamElementByHierarchicalName( const OUString& sStreamPath, ::sal_Int32 nOpenMode )
1421 : throw ( embed::InvalidStorageException,
1422 : lang::IllegalArgumentException,
1423 : packages::WrongPasswordException,
1424 : io::IOException,
1425 : embed::StorageWrappedTargetException,
1426 : uno::RuntimeException )
1427 : {
1428 0 : ::osl::MutexGuard aGuard( m_aMutex );
1429 :
1430 0 : if ( !m_pImpl )
1431 0 : throw lang::DisposedException();
1432 :
1433 0 : if ( sStreamPath.toChar() == '/' )
1434 0 : throw lang::IllegalArgumentException();
1435 :
1436 0 : if ( !GetContent() )
1437 0 : throw io::IOException(); // TODO: error handling
1438 :
1439 0 : INetURLObject aBaseURL( m_pImpl->m_aURL );
1440 0 : if ( !aBaseURL.setFinalSlash() )
1441 0 : throw uno::RuntimeException();
1442 :
1443 : INetURLObject aFileURL = INetURLObject::GetAbsURL(
1444 : aBaseURL.GetMainURL( INetURLObject::NO_DECODE ),
1445 0 : sStreamPath );
1446 :
1447 0 : if ( ::utl::UCBContentHelper::IsFolder( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
1448 0 : throw io::IOException();
1449 :
1450 0 : if ( ( nOpenMode & embed::ElementModes::NOCREATE )
1451 0 : && !::utl::UCBContentHelper::IsDocument( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
1452 0 : throw io::IOException(); // TODO:
1453 :
1454 0 : uno::Reference< ucb::XCommandEnvironment > xDummyEnv; // TODO: provide InteractionHandler if any
1455 0 : uno::Reference< io::XStream > xResult;
1456 : try
1457 : {
1458 0 : if ( nOpenMode & embed::ElementModes::WRITE )
1459 : {
1460 0 : if ( isLocalFile_Impl( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
1461 : {
1462 : uno::Reference<ucb::XSimpleFileAccess3> xSimpleFileAccess(
1463 0 : ucb::SimpleFileAccess::create( m_pImpl->m_xContext ) );
1464 : uno::Reference< io::XStream > xStream =
1465 0 : xSimpleFileAccess->openFileReadWrite( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) );
1466 :
1467 0 : xResult = static_cast< io::XStream* >( new OFSStreamContainer( xStream ) );
1468 : }
1469 : else
1470 : {
1471 : // TODO: test whether it really works for http and fwp
1472 : SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( aFileURL.GetMainURL( INetURLObject::NO_DECODE ),
1473 0 : STREAM_STD_WRITE );
1474 0 : if ( pStream )
1475 : {
1476 0 : if ( !pStream->GetError() )
1477 : {
1478 : uno::Reference< io::XStream > xStream =
1479 0 : uno::Reference < io::XStream >( new ::utl::OStreamWrapper( *pStream ) );
1480 0 : xResult = static_cast< io::XStream* >( new OFSStreamContainer( xStream ) );
1481 : }
1482 : else
1483 0 : delete pStream;
1484 : }
1485 : }
1486 :
1487 0 : if ( !xResult.is() )
1488 0 : throw io::IOException();
1489 :
1490 0 : if ( ( nOpenMode & embed::ElementModes::TRUNCATE ) )
1491 : {
1492 0 : uno::Reference< io::XTruncate > xTrunc( xResult->getOutputStream(), uno::UNO_QUERY_THROW );
1493 0 : xTrunc->truncate();
1494 : }
1495 : }
1496 : else
1497 : {
1498 0 : if ( ( nOpenMode & embed::ElementModes::TRUNCATE )
1499 0 : || !::utl::UCBContentHelper::IsDocument( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
1500 0 : throw io::IOException(); // TODO: access denied
1501 :
1502 0 : ::ucbhelper::Content aResultContent( aFileURL.GetMainURL( INetURLObject::NO_DECODE ), xDummyEnv, comphelper::getProcessComponentContext() );
1503 0 : uno::Reference< io::XInputStream > xInStream = aResultContent.openStream();
1504 0 : xResult = static_cast< io::XStream* >( new OFSInputStreamContainer( xInStream ) );
1505 : }
1506 : }
1507 0 : catch( embed::InvalidStorageException& )
1508 : {
1509 0 : throw;
1510 : }
1511 0 : catch( lang::IllegalArgumentException& )
1512 : {
1513 0 : throw;
1514 : }
1515 0 : catch( packages::WrongPasswordException& )
1516 : {
1517 0 : throw;
1518 : }
1519 0 : catch( embed::StorageWrappedTargetException& )
1520 : {
1521 0 : throw;
1522 : }
1523 0 : catch( io::IOException& )
1524 : {
1525 0 : throw;
1526 : }
1527 0 : catch( uno::RuntimeException& )
1528 : {
1529 0 : throw;
1530 : }
1531 0 : catch( uno::Exception& )
1532 : {
1533 0 : uno::Any aCaught( ::cppu::getCaughtException() );
1534 : throw embed::StorageWrappedTargetException("Can't copy raw stream",
1535 : uno::Reference< io::XInputStream >(),
1536 0 : aCaught );
1537 : }
1538 :
1539 0 : return uno::Reference< embed::XExtendedStorageStream >( xResult, uno::UNO_QUERY_THROW );
1540 : }
1541 :
1542 : //-----------------------------------------------
1543 0 : uno::Reference< embed::XExtendedStorageStream > SAL_CALL FSStorage::openEncryptedStreamElementByHierarchicalName( const OUString& /*sStreamName*/, ::sal_Int32 /*nOpenMode*/, const OUString& /*sPassword*/ )
1544 : throw ( embed::InvalidStorageException,
1545 : lang::IllegalArgumentException,
1546 : packages::NoEncryptionException,
1547 : packages::WrongPasswordException,
1548 : io::IOException,
1549 : embed::StorageWrappedTargetException,
1550 : uno::RuntimeException )
1551 : {
1552 0 : throw packages::NoEncryptionException();
1553 : }
1554 :
1555 : //-----------------------------------------------
1556 0 : void SAL_CALL FSStorage::removeStreamElementByHierarchicalName( const OUString& sStreamPath )
1557 : throw ( embed::InvalidStorageException,
1558 : lang::IllegalArgumentException,
1559 : container::NoSuchElementException,
1560 : io::IOException,
1561 : embed::StorageWrappedTargetException,
1562 : uno::RuntimeException )
1563 : {
1564 0 : ::osl::MutexGuard aGuard( m_aMutex );
1565 :
1566 0 : if ( !m_pImpl )
1567 0 : throw lang::DisposedException();
1568 :
1569 0 : if ( !GetContent() )
1570 0 : throw io::IOException(); // TODO: error handling
1571 :
1572 : // TODO/LATER: may need possibility to create folder if it was removed, since the folder can not be locked
1573 0 : INetURLObject aBaseURL( m_pImpl->m_aURL );
1574 0 : if ( !aBaseURL.setFinalSlash() )
1575 0 : throw uno::RuntimeException();
1576 :
1577 : INetURLObject aFileURL = INetURLObject::GetAbsURL(
1578 : aBaseURL.GetMainURL( INetURLObject::NO_DECODE ),
1579 0 : sStreamPath );
1580 :
1581 0 : if ( !::utl::UCBContentHelper::IsDocument( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
1582 : {
1583 0 : if ( ::utl::UCBContentHelper::IsFolder( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
1584 0 : throw lang::IllegalArgumentException();
1585 : else
1586 0 : throw container::NoSuchElementException(); // TODO:
1587 : }
1588 :
1589 0 : if ( !::utl::UCBContentHelper::Kill( aFileURL.GetMainURL( INetURLObject::NO_DECODE ) ) )
1590 0 : throw io::IOException(); // TODO: error handling
1591 0 : }
1592 :
1593 :
1594 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|