1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mediadescriptor.cxx,v $
10 * $Revision: 1.20.22.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/mediadescriptor.hxx>
35 //_______________________________________________
38 #ifndef __COM_SUN_STAR_UCB_XCONTENT_HPP__
39 #include <com/sun/star/ucb/XContent.hpp>
42 #ifndef __COM_SUN_STAR_UCB_XCOMMANDENVIRONMENT_HPP__
43 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
46 #ifndef __COM_SUN_STAR_TASK_XINTERACTIONHANDLER_HPP__
47 #include <com/sun/star/task/XInteractionHandler.hpp>
50 #ifndef __COM_SUN_STAR_IO_XSTREAM_HPP__
51 #include <com/sun/star/io/XStream.hpp>
53 #include <com/sun/star/io/XActiveDataSink.hpp>
54 #include <com/sun/star/io/XSeekable.hpp>
56 #ifndef __COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP__
57 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
59 #include <com/sun/star/lang/IllegalArgumentException.hpp>
61 #ifndef __COM_SUN_STAR_UTIL_XURLTRANSFORMER_HPP__
62 #include <com/sun/star/util/XURLTransformer.hpp>
65 #ifndef __COM_SUN_STAR_UCB_INTERACTIVEIOEXCEPTION_HPP__
66 #include <com/sun/star/ucb/InteractiveIOException.hpp>
69 #ifndef __COM_SUN_STAR_UCB_UNSUPPORTEDDATASINKEXCEPTION_HPP__
70 #include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
73 #ifndef __COM_SUN_STAR_UCB_COMMANDFAILEDEXCEPTION_HPP__
74 #include <com/sun/star/ucb/CommandFailedException.hpp>
77 #ifndef __COM_SUN_STAR_TASK_XINTERACTIONABORT_HPP__
78 #include <com/sun/star/task/XInteractionAbort.hpp>
81 #ifndef __COM_SUN_STAR_URI_XURIREFERENCEFACTORY_HPP__
82 #include <com/sun/star/uri/XUriReferenceFactory.hpp>
85 #ifndef __COM_SUN_STAR_URI_XURIREFERENCE_HPP__
86 #include <com/sun/star/uri/XUriReference.hpp>
88 #include <com/sun/star/ucb/PostCommandArgument2.hpp>
89 #include <com/sun/star/container/XNameAccess.hpp>
91 #include <ucbhelper/interceptedinteraction.hxx>
92 #include <ucbhelper/content.hxx>
93 #include <ucbhelper/commandenvironment.hxx>
94 #include <ucbhelper/activedatasink.hxx>
95 #include <comphelper/processfactory.hxx>
96 #include <comphelper/configurationhelper.hxx>
99 #ifndef _RTL_USTRBUF_HXX_
100 #include <rtl/ustrbuf.hxx>
104 //_______________________________________________
107 namespace comphelper
{
109 namespace css
= ::com::sun::star
;
111 //_______________________________________________
114 /*-----------------------------------------------
116 -----------------------------------------------*/
117 const ::rtl::OUString
& MediaDescriptor::PROP_ABORTED()
119 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Aborted"));
123 const ::rtl::OUString
& MediaDescriptor::PROP_ASTEMPLATE()
125 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("AsTemplate"));
129 const ::rtl::OUString
& MediaDescriptor::PROP_CHARACTERSET()
131 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("CharacterSet"));
135 const ::rtl::OUString
& MediaDescriptor::PROP_COMPONENTDATA()
137 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("ComponentData"));
141 const ::rtl::OUString
& MediaDescriptor::PROP_DEEPDETECTION()
143 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("DeepDetection"));
147 const ::rtl::OUString
& MediaDescriptor::PROP_DETECTSERVICE()
149 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("DetectService"));
153 const ::rtl::OUString
& MediaDescriptor::PROP_DOCUMENTSERVICE()
155 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("DocumentService"));
159 const ::rtl::OUString
& MediaDescriptor::PROP_EXTENSION()
161 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Extension"));
165 const ::rtl::OUString
& MediaDescriptor::PROP_FILENAME()
167 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("FileName"));
171 const ::rtl::OUString
& MediaDescriptor::PROP_FILTERNAME()
173 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("FilterName"));
177 const ::rtl::OUString
& MediaDescriptor::PROP_FILTEROPTIONS()
179 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("FilterOptions"));
183 const ::rtl::OUString
& MediaDescriptor::PROP_FORMAT()
185 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Format"));
189 const ::rtl::OUString
& MediaDescriptor::PROP_FRAMENAME()
191 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("FrameName"));
195 const ::rtl::OUString
& MediaDescriptor::PROP_HIDDEN()
197 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Hidden"));
201 const ::rtl::OUString
& MediaDescriptor::PROP_INPUTSTREAM()
203 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("InputStream"));
207 const ::rtl::OUString
& MediaDescriptor::PROP_INTERACTIONHANDLER()
209 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("InteractionHandler"));
213 const ::rtl::OUString
& MediaDescriptor::PROP_JUMPMARK()
215 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("JumpMark"));
219 const ::rtl::OUString
& MediaDescriptor::PROP_MACROEXECUTIONMODE()
221 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("MacroExecutionMode"));
225 const ::rtl::OUString
& MediaDescriptor::PROP_MEDIATYPE()
227 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("MediaType"));
231 const ::rtl::OUString
& MediaDescriptor::PROP_MINIMIZED()
233 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Minimized"));
237 const ::rtl::OUString
& MediaDescriptor::PROP_NOAUTOSAVE()
239 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("NoAutoSave"));
243 const ::rtl::OUString
& MediaDescriptor::PROP_OPENNEWVIEW()
245 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("OpenNewView"));
249 const ::rtl::OUString
& MediaDescriptor::PROP_OUTPUTSTREAM()
251 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("OutputStream"));
255 const ::rtl::OUString
& MediaDescriptor::PROP_PATTERN()
257 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Pattern"));
261 const ::rtl::OUString
& MediaDescriptor::PROP_POSSIZE()
263 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("PosSize"));
267 const ::rtl::OUString
& MediaDescriptor::PROP_POSTDATA()
269 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("PostData"));
273 const ::rtl::OUString
& MediaDescriptor::PROP_POSTSTRING()
275 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("PostString"));
279 const ::rtl::OUString
& MediaDescriptor::PROP_PREVIEW()
281 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Preview"));
285 const ::rtl::OUString
& MediaDescriptor::PROP_READONLY()
287 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("ReadOnly"));
291 const ::rtl::OUString
& MediaDescriptor::PROP_REFERRER()
293 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Referer"));
297 const ::rtl::OUString
& MediaDescriptor::PROP_SILENT()
299 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Silent"));
303 const ::rtl::OUString
& MediaDescriptor::PROP_STATUSINDICATOR()
305 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("StatusIndicator"));
309 const ::rtl::OUString
& MediaDescriptor::PROP_STREAM()
311 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Stream"));
315 const ::rtl::OUString
& MediaDescriptor::PROP_STREAMFOROUTPUT()
317 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("StreamForOutput"));
321 const ::rtl::OUString
& MediaDescriptor::PROP_TEMPLATENAME()
323 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("TemplateName"));
327 const ::rtl::OUString
& MediaDescriptor::PROP_TEMPLATEREGIONNAME()
329 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("TemplateRegionName"));
333 const ::rtl::OUString
& MediaDescriptor::PROP_TYPENAME()
335 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("TypeName"));
339 const ::rtl::OUString
& MediaDescriptor::PROP_UCBCONTENT()
341 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("UCBContent"));
345 const ::rtl::OUString
& MediaDescriptor::PROP_UPDATEDOCMODE()
347 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("UpdateDocMode"));
351 const ::rtl::OUString
& MediaDescriptor::PROP_URL()
353 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("URL"));
357 const ::rtl::OUString
& MediaDescriptor::PROP_VERSION()
359 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Version"));
363 const ::rtl::OUString
& MediaDescriptor::PROP_VIEWID()
365 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("ViewId"));
369 const ::rtl::OUString
& MediaDescriptor::PROP_REPAIRPACKAGE()
371 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("RepairPackage"));
375 const ::rtl::OUString
& MediaDescriptor::PROP_DOCUMENTTITLE()
377 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("DocumentTitle"));
381 const ::rtl::OUString
& MediaDescriptor::PROP_MODEL()
383 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Model"));
387 const ::rtl::OUString
& MediaDescriptor::PROP_PASSWORD()
389 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Password"));
393 const ::rtl::OUString
& MediaDescriptor::PROP_TITLE()
395 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("Title"));
399 const ::rtl::OUString
& MediaDescriptor::PROP_SALVAGEDFILE()
401 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("SalvagedFile"));
405 const ::rtl::OUString
& MediaDescriptor::PROP_VIEWONLY()
407 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("ViewOnly"));
411 const ::rtl::OUString
& MediaDescriptor::PROP_DOCUMENTBASEURL()
413 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("DocumentBaseURL"));
417 const ::rtl::OUString
& MediaDescriptor::PROP_VIEWCONTROLLERNAME()
419 static const ::rtl::OUString
sProp(RTL_CONSTASCII_USTRINGPARAM("ViewControllerName"));
422 /*-----------------------------------------------
424 -----------------------------------------------*/
425 MediaDescriptor::MediaDescriptor()
426 : SequenceAsHashMap()
430 /*-----------------------------------------------
432 -----------------------------------------------*/
433 MediaDescriptor::MediaDescriptor(const css::uno::Any
& aSource
)
434 : SequenceAsHashMap(aSource
)
438 /*-----------------------------------------------
440 -----------------------------------------------*/
441 MediaDescriptor::MediaDescriptor(const css::uno::Sequence
< css::beans::PropertyValue
>& lSource
)
442 : SequenceAsHashMap(lSource
)
446 /*-----------------------------------------------
448 -----------------------------------------------*/
449 MediaDescriptor::MediaDescriptor(const css::uno::Sequence
< css::beans::NamedValue
>& lSource
)
450 : SequenceAsHashMap(lSource
)
454 /*-----------------------------------------------
456 -----------------------------------------------*/
457 sal_Bool
MediaDescriptor::isStreamReadOnly() const
459 static ::rtl::OUString CONTENTSCHEME_FILE
= ::rtl::OUString::createFromAscii("file");
460 static ::rtl::OUString CONTENTPROP_ISREADONLY
= ::rtl::OUString::createFromAscii("IsReadOnly");
461 static sal_Bool READONLY_FALLBACK
= sal_False
;
463 sal_Bool bReadOnly
= READONLY_FALLBACK
;
465 // check for explicit readonly state
466 const_iterator pIt
= find(MediaDescriptor::PROP_READONLY());
469 pIt
->second
>>= bReadOnly
;
473 // streams based on post data are readonly by definition
474 pIt
= find(MediaDescriptor::PROP_POSTDATA());
478 // A XStream capsulate XInputStream and XOutputStream ...
479 // If it exists - the file must be open in read/write mode!
480 pIt
= find(MediaDescriptor::PROP_STREAM());
484 // Only file system content provider is able to provide XStream
485 // so for this content impossibility to create XStream triggers
486 // switch to readonly mode.
489 css::uno::Reference
< css::ucb::XContent
> xContent
= getUnpackedValueOrDefault(MediaDescriptor::PROP_UCBCONTENT(), css::uno::Reference
< css::ucb::XContent
>());
492 css::uno::Reference
< css::ucb::XContentIdentifier
> xId(xContent
->getIdentifier(), css::uno::UNO_QUERY
);
493 ::rtl::OUString aScheme
;
495 aScheme
= xId
->getContentProviderScheme();
497 if (aScheme
.equalsIgnoreAsciiCase(CONTENTSCHEME_FILE
))
498 bReadOnly
= sal_True
;
501 ::ucbhelper::Content
aContent(xContent
, css::uno::Reference
< css::ucb::XCommandEnvironment
>());
502 aContent
.getPropertyValue(CONTENTPROP_ISREADONLY
) >>= bReadOnly
;
506 catch(const css::uno::RuntimeException
& exRun
)
508 catch(const css::uno::Exception
&)
514 /*-----------------------------------------------
516 -----------------------------------------------*/
517 sal_Bool
MediaDescriptor::addInputStream()
519 return impl_addInputStream( sal_True
);
522 /*-----------------------------------------------*/
523 sal_Bool
MediaDescriptor::addInputStreamOwnLock()
525 // Own lock file implementation
527 sal_Bool bUseLock
= sal_True
; // the system file locking is used per default
531 css::uno::Reference
< css::uno::XInterface
> xCommonConfig
= ::comphelper::ConfigurationHelper::openConfig(
532 ::comphelper::getProcessServiceFactory(),
533 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Common" ) ),
534 ::comphelper::ConfigurationHelper::E_STANDARD
);
535 if ( !xCommonConfig
.is() )
536 throw css::uno::RuntimeException();
538 ::comphelper::ConfigurationHelper::readRelativeKey(
540 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Misc/" ) ),
541 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UseDocumentSystemFileLocking" ) ) ) >>= bUseLock
;
543 catch( const css::uno::Exception
& )
547 return impl_addInputStream( bUseLock
);
550 /*-----------------------------------------------*/
551 sal_Bool
MediaDescriptor::impl_addInputStream( sal_Bool bLockFile
)
553 // check for an already existing stream item first
554 const_iterator pIt
= find(MediaDescriptor::PROP_INPUTSTREAM());
560 // No stream available - create a new one
561 // a) data comes as PostData ...
562 pIt
= find(MediaDescriptor::PROP_POSTDATA());
565 const css::uno::Any
& rPostData
= pIt
->second
;
566 css::uno::Reference
< css::io::XInputStream
> xPostData
;
567 rPostData
>>= xPostData
;
569 return impl_openStreamWithPostData( xPostData
);
572 // b) ... or we must get it from the given URL
573 ::rtl::OUString sURL
= getUnpackedValueOrDefault(MediaDescriptor::PROP_URL(), ::rtl::OUString());
574 if (!sURL
.getLength())
575 throw css::uno::Exception(
576 ::rtl::OUString::createFromAscii("Found no URL."),
577 css::uno::Reference
< css::uno::XInterface
>());
579 // Parse URL! Only the main part has to be used further. E.g. a jumpmark can make trouble
580 ::rtl::OUString sNormalizedURL
= impl_normalizeURL( sURL
);
581 return impl_openStreamWithURL( sNormalizedURL
, bLockFile
);
583 #if OSL_DEBUG_LEVEL>0
584 catch(const css::uno::Exception
& ex
)
586 ::rtl::OUStringBuffer
sMsg(256);
587 sMsg
.appendAscii("Invalid MediaDescriptor detected:\n");
588 sMsg
.append (ex
.Message
);
589 OSL_ENSURE(sal_False
, ::rtl::OUStringToOString(sMsg
.makeStringAndClear(), RTL_TEXTENCODING_UTF8
).getStr());
592 catch(const css::uno::Exception
&)
599 /*-----------------------------------------------
601 -----------------------------------------------*/
602 sal_Bool
MediaDescriptor::impl_openStreamWithPostData( const css::uno::Reference
< css::io::XInputStream
>& _rxPostData
)
603 throw(::com::sun::star::uno::RuntimeException
)
605 if ( !_rxPostData
.is() )
606 throw css::lang::IllegalArgumentException(
607 ::rtl::OUString::createFromAscii("Found invalid PostData."),
608 css::uno::Reference
< css::uno::XInterface
>(), 1);
610 // PostData can't be used in read/write mode!
611 (*this)[MediaDescriptor::PROP_READONLY()] <<= sal_True
;
613 // prepare the environment
614 css::uno::Reference
< css::task::XInteractionHandler
> xInteraction
= getUnpackedValueOrDefault(
615 MediaDescriptor::PROP_INTERACTIONHANDLER(),
616 css::uno::Reference
< css::task::XInteractionHandler
>());
617 css::uno::Reference
< css::ucb::XProgressHandler
> xProgress
;
618 ::ucbhelper::CommandEnvironment
* pCommandEnv
= new ::ucbhelper::CommandEnvironment(xInteraction
, xProgress
);
619 css::uno::Reference
< css::ucb::XCommandEnvironment
> xCommandEnv(static_cast< css::ucb::XCommandEnvironment
* >(pCommandEnv
), css::uno::UNO_QUERY
);
622 ::rtl::OUString sMediaType
= getUnpackedValueOrDefault(MediaDescriptor::PROP_MEDIATYPE(), ::rtl::OUString());
623 if (!sMediaType
.getLength())
625 sMediaType
= ::rtl::OUString::createFromAscii("application/x-www-form-urlencoded");
626 (*this)[MediaDescriptor::PROP_MEDIATYPE()] <<= sMediaType
;
630 ::rtl::OUString
sURL( getUnpackedValueOrDefault( PROP_URL(), ::rtl::OUString() ) );
632 css::uno::Reference
< css::io::XInputStream
> xResultStream
;
635 // seek PostData stream to the beginning
636 css::uno::Reference
< css::io::XSeekable
> xSeek( _rxPostData
, css::uno::UNO_QUERY
);
640 // a content for the URL
641 ::ucbhelper::Content
aContent( sURL
, xCommandEnv
);
644 css::ucb::PostCommandArgument2 aPostArgument
;
645 aPostArgument
.Source
= _rxPostData
;
646 css::uno::Reference
< css::io::XActiveDataSink
> xSink( new ucbhelper::ActiveDataSink
);
647 aPostArgument
.Sink
= xSink
;
648 aPostArgument
.MediaType
= sMediaType
;
649 aPostArgument
.Referer
= getUnpackedValueOrDefault( PROP_REFERRER(), ::rtl::OUString() );
651 ::rtl::OUString
sCommandName( RTL_CONSTASCII_USTRINGPARAM( "post" ) );
652 aContent
.executeCommand( sCommandName
, css::uno::makeAny( aPostArgument
) );
655 xResultStream
= xSink
->getInputStream();
657 catch( const css::uno::Exception
& )
662 if ( !xResultStream
.is() )
664 OSL_ENSURE( false, "no valid reply to the HTTP-Post" );
668 (*this)[MediaDescriptor::PROP_INPUTSTREAM()] <<= xResultStream
;
672 /*-----------------------------------------------*/
673 class StillReadWriteInteraction
: public ::ucbhelper::InterceptedInteraction
676 static const sal_Int32 HANDLE_INTERACTIVEIOEXCEPTION
= 0;
677 static const sal_Int32 HANDLE_UNSUPPORTEDDATASINKEXCEPTION
= 1;
680 sal_Bool m_bHandledByMySelf
;
681 sal_Bool m_bHandledByInternalHandler
;
684 StillReadWriteInteraction(const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
)
685 : m_bUsed (sal_False
)
686 , m_bHandledByMySelf (sal_False
)
687 , m_bHandledByInternalHandler(sal_False
)
689 ::std::vector
< ::ucbhelper::InterceptedInteraction::InterceptedRequest
> lInterceptions
;
690 ::ucbhelper::InterceptedInteraction::InterceptedRequest aInterceptedRequest
;
692 aInterceptedRequest
.Handle
= HANDLE_INTERACTIVEIOEXCEPTION
;
693 aInterceptedRequest
.Request
<<= css::ucb::InteractiveIOException();
694 aInterceptedRequest
.Continuation
= ::getCppuType(static_cast< css::uno::Reference
< css::task::XInteractionAbort
>* >(0));
695 aInterceptedRequest
.MatchExact
= sal_False
;
696 lInterceptions
.push_back(aInterceptedRequest
);
698 aInterceptedRequest
.Handle
= HANDLE_UNSUPPORTEDDATASINKEXCEPTION
;
699 aInterceptedRequest
.Request
<<= css::ucb::UnsupportedDataSinkException();
700 aInterceptedRequest
.Continuation
= ::getCppuType(static_cast< css::uno::Reference
< css::task::XInteractionAbort
>* >(0));
701 aInterceptedRequest
.MatchExact
= sal_False
;
702 lInterceptions
.push_back(aInterceptedRequest
);
704 setInterceptedHandler(xHandler
);
705 setInterceptions(lInterceptions
);
708 void resetInterceptions()
710 setInterceptions(::std::vector
< ::ucbhelper::InterceptedInteraction::InterceptedRequest
>());
713 void resetErrorStates()
716 m_bHandledByMySelf
= sal_False
;
717 m_bHandledByInternalHandler
= sal_False
;
720 sal_Bool
wasWriteError()
722 return (m_bUsed
&& m_bHandledByMySelf
);
726 virtual ucbhelper::InterceptedInteraction::EInterceptionState
intercepted(const ::ucbhelper::InterceptedInteraction::InterceptedRequest
& aRequest
,
727 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionRequest
>& xRequest
)
732 // check if its a real interception - might some parameters are not the right ones ...
733 sal_Bool bAbort
= sal_False
;
734 switch(aRequest
.Handle
)
736 case HANDLE_INTERACTIVEIOEXCEPTION
:
738 css::ucb::InteractiveIOException exIO
;
739 xRequest
->getRequest() >>= exIO
;
741 (exIO
.Code
== css::ucb::IOErrorCode_ACCESS_DENIED
)
742 || (exIO
.Code
== css::ucb::IOErrorCode_LOCKING_VIOLATION
)
744 // this is a workaround for MAC, on this platform if the file is locked
745 // the returned error code looks to be wrong
746 || (exIO
.Code
== css::ucb::IOErrorCode_GENERAL
)
752 case HANDLE_UNSUPPORTEDDATASINKEXCEPTION
:
759 // handle interaction by ourself
762 m_bHandledByMySelf
= sal_True
;
763 css::uno::Reference
< css::task::XInteractionContinuation
> xAbort
= ::ucbhelper::InterceptedInteraction::extractContinuation(
764 xRequest
->getContinuations(),
765 ::getCppuType(static_cast< css::uno::Reference
< css::task::XInteractionAbort
>* >(0)));
767 return ::ucbhelper::InterceptedInteraction::E_NO_CONTINUATION_FOUND
;
769 return ::ucbhelper::InterceptedInteraction::E_INTERCEPTED
;
772 // Otherwhise use internal handler.
773 if (m_xInterceptedHandler
.is())
775 m_bHandledByInternalHandler
= sal_True
;
776 m_xInterceptedHandler
->handle(xRequest
);
778 return ::ucbhelper::InterceptedInteraction::E_INTERCEPTED
;
782 /*-----------------------------------------------
784 -----------------------------------------------*/
785 sal_Bool
MediaDescriptor::impl_openStreamWithURL( const ::rtl::OUString
& sURL
, sal_Bool bLockFile
)
786 throw(::com::sun::star::uno::RuntimeException
)
788 // prepare the environment
789 css::uno::Reference
< css::task::XInteractionHandler
> xOrgInteraction
= getUnpackedValueOrDefault(
790 MediaDescriptor::PROP_INTERACTIONHANDLER(),
791 css::uno::Reference
< css::task::XInteractionHandler
>());
793 StillReadWriteInteraction
* pInteraction
= new StillReadWriteInteraction(xOrgInteraction
);
794 css::uno::Reference
< css::task::XInteractionHandler
> xInteraction(static_cast< css::task::XInteractionHandler
* >(pInteraction
), css::uno::UNO_QUERY
);
796 css::uno::Reference
< css::ucb::XProgressHandler
> xProgress
;
797 ::ucbhelper::CommandEnvironment
* pCommandEnv
= new ::ucbhelper::CommandEnvironment(xInteraction
, xProgress
);
798 css::uno::Reference
< css::ucb::XCommandEnvironment
> xCommandEnv(static_cast< css::ucb::XCommandEnvironment
* >(pCommandEnv
), css::uno::UNO_QUERY
);
800 // try to create the content
801 // no content -> no stream => return immediatly with FALSE
802 ::ucbhelper::Content aContent
;
803 css::uno::Reference
< css::ucb::XContent
> xContent
;
806 aContent
= ::ucbhelper::Content(sURL
, xCommandEnv
);
807 xContent
= aContent
.get();
809 catch(const css::uno::RuntimeException
&)
811 catch(const css::ucb::ContentCreationException
&)
812 { return sal_False
; } // TODO error handling
813 catch(const css::uno::Exception
&)
814 { return sal_False
; } // TODO error handling
816 // try to open the file in read/write mode
817 // (if its allowed to do so).
818 // But handle errors in a "hidden mode". Because
819 // we try it readonly later - if read/write isnt an option.
820 css::uno::Reference
< css::io::XStream
> xStream
;
821 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
823 sal_Bool bReadOnly
= sal_False
;
824 sal_Bool bModeRequestedExplicitly
= sal_False
;
825 const_iterator pIt
= find(MediaDescriptor::PROP_READONLY());
828 pIt
->second
>>= bReadOnly
;
829 bModeRequestedExplicitly
= sal_True
;
832 if ( !bReadOnly
&& bLockFile
)
836 // TODO: use "special" still interaction to supress error messages
837 xStream
= aContent
.openWriteableStream();
839 xInputStream
= xStream
->getInputStream();
841 catch(const css::uno::RuntimeException
&)
843 catch(const css::uno::Exception
&)
845 // ignore exception, if reason was problem reasoned on
846 // open it in WRITEABLE mode! Then we try it READONLY
847 // later a second time.
848 // All other errors must be handled as real error an
849 // break this method.
850 if (!pInteraction
->wasWriteError() || bModeRequestedExplicitly
)
853 xInputStream
.clear();
857 // If opening of the stream in read/write mode wasnt allowed
858 // or failed by an error - we must try it in readonly mode.
859 if (!xInputStream
.is())
861 rtl::OUString aScheme
;
865 css::uno::Reference
< css::ucb::XContentIdentifier
> xContId(
866 aContent
.get().is() ? aContent
.get()->getIdentifier() : 0 );
869 aScheme
= xContId
->getContentProviderScheme();
871 // Only file system content provider is able to provide XStream
872 // so for this content impossibility to create XStream triggers
873 // switch to readonly mode in case of opening with locking on
874 if( bLockFile
&& aScheme
.equalsIgnoreAsciiCaseAscii( "file" ) )
875 bReadOnly
= sal_True
;
878 sal_Bool bRequestReadOnly
= bReadOnly
;
879 aContent
.getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsReadOnly" ) ) ) >>= bReadOnly
;
880 if ( bReadOnly
&& !bRequestReadOnly
&& bModeRequestedExplicitly
)
881 return sal_False
; // the document is explicitly requested with WRITEABLE mode
884 catch(const css::uno::RuntimeException
&)
886 catch(const css::uno::Exception
&)
887 { /* no error handling if IsReadOnly property does not exist for UCP */ }
890 (*this)[MediaDescriptor::PROP_READONLY()] <<= bReadOnly
;
892 pInteraction
->resetInterceptions();
893 pInteraction
->resetErrorStates();
896 // all the contents except file-URLs should be opened as usual
897 if ( bLockFile
|| !aScheme
.equalsIgnoreAsciiCaseAscii( "file" ) )
898 xInputStream
= aContent
.openStream();
900 xInputStream
= aContent
.openStreamNoLock();
902 catch(const css::uno::RuntimeException
&)
904 catch(const css::uno::Exception
&)
905 { return sal_False
; }
908 // add streams to the descriptor
910 (*this)[MediaDescriptor::PROP_UCBCONTENT()] <<= xContent
;
912 (*this)[MediaDescriptor::PROP_STREAM()] <<= xStream
;
913 if (xInputStream
.is())
914 (*this)[MediaDescriptor::PROP_INPUTSTREAM()] <<= xInputStream
;
916 // At least we need an input stream. The r/w stream is optional ...
917 return xInputStream
.is();
920 /*-----------------------------------------------
922 -----------------------------------------------*/
923 ::rtl::OUString
MediaDescriptor::impl_normalizeURL(const ::rtl::OUString
& sURL
)
925 /* Remove Jumpmarks (fragments) of an URL only here.
926 They are not part of any URL and as a result may be
927 no ucb content can be created then.
928 On the other side arguments must exists ... because
929 they are part of an URL.
931 Do not use the URLTransformer service here. Because
932 it parses the URL in another way. It's main part isnt enough
933 and it's complete part contains the jumpmark (fragment) parameter ...
935 static ::rtl::OUString SERVICENAME_URIREFERENCEFACTORY
= ::rtl::OUString::createFromAscii("com.sun.star.uri.UriReferenceFactory");
939 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= ::comphelper::getProcessServiceFactory();
940 css::uno::Reference
< css::uri::XUriReferenceFactory
> xUriFactory(xSMGR
->createInstance(SERVICENAME_URIREFERENCEFACTORY
), css::uno::UNO_QUERY_THROW
);
941 css::uno::Reference
< css::uri::XUriReference
> xUriRef
= xUriFactory
->parse(sURL
);
944 xUriRef
->clearFragment();
945 return xUriRef
->getUriReference();
948 catch(const css::uno::RuntimeException
& exRun
)
950 catch(const css::uno::Exception
&)
953 // If an error ocurred ... return the original URL.
958 } // namespace comphelper