1*b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b5088357SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b5088357SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b5088357SAndrew Rist  * distributed with this work for additional information
6*b5088357SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b5088357SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b5088357SAndrew Rist  * "License"); you may not use this file except in compliance
9*b5088357SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b5088357SAndrew Rist  *
11*b5088357SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b5088357SAndrew Rist  *
13*b5088357SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b5088357SAndrew Rist  * software distributed under the License is distributed on an
15*b5088357SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b5088357SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b5088357SAndrew Rist  * specific language governing permissions and limitations
18*b5088357SAndrew Rist  * under the License.
19*b5088357SAndrew Rist  *
20*b5088357SAndrew Rist  *************************************************************/
21*b5088357SAndrew Rist 
22*b5088357SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <unotools/ucblockbytes.hxx>
28cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
29cdf0e10cSrcweir #include <salhelper/condition.hxx>
30cdf0e10cSrcweir #ifndef _OSL_THREAD_HXX_
31cdf0e10cSrcweir #include <osl/thread.hxx>
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #include <tools/urlobj.hxx>
34cdf0e10cSrcweir #include <ucbhelper/interactionrequest.hxx>
35cdf0e10cSrcweir #include <com/sun/star/task/XInteractionAbort.hpp>
36cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveNetworkConnectException.hpp>
37cdf0e10cSrcweir #include <com/sun/star/ucb/CommandFailedException.hpp>
38cdf0e10cSrcweir #include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
39cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UCB_INTERACTIVEIODEXCEPTION_HPP_
40cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveIOException.hpp>
41cdf0e10cSrcweir #endif
42cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataStreamer.hpp>
43cdf0e10cSrcweir #include <com/sun/star/ucb/DocumentHeaderField.hpp>
44cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandInfo.hpp>
45cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandProcessor.hpp>
46cdf0e10cSrcweir #include <com/sun/star/task/XInteractionHandler.hpp>
47cdf0e10cSrcweir #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
48cdf0e10cSrcweir #include <com/sun/star/ucb/PostCommandArgument2.hpp>
49cdf0e10cSrcweir #include <com/sun/star/ucb/OpenMode.hpp>
50cdf0e10cSrcweir #include <com/sun/star/beans/Property.hpp>
51cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
52cdf0e10cSrcweir #include <com/sun/star/beans/XPropertiesChangeNotifier.hpp>
53cdf0e10cSrcweir #include <com/sun/star/beans/XPropertiesChangeListener.hpp>
54cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
55cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSink.hpp>
56cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataControl.hpp>
57cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
58cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
59cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
60cdf0e10cSrcweir #include <tools/inetmsg.hxx>
61cdf0e10cSrcweir #include <com/sun/star/io/XTruncate.hpp>
62cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
63cdf0e10cSrcweir 
64cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
65cdf0e10cSrcweir 
66cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
67cdf0e10cSrcweir #include <ucbhelper/content.hxx>
68cdf0e10cSrcweir 
69cdf0e10cSrcweir using namespace ::com::sun::star::uno;
70cdf0e10cSrcweir using namespace ::com::sun::star::io;
71cdf0e10cSrcweir using namespace ::com::sun::star::uno;
72cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
73cdf0e10cSrcweir using namespace ::com::sun::star::task;
74cdf0e10cSrcweir using namespace ::com::sun::star::lang;
75cdf0e10cSrcweir using namespace ::com::sun::star::beans;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 
78cdf0e10cSrcweir namespace utl
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 
81cdf0e10cSrcweir /**
82cdf0e10cSrcweir     Helper class for getting a XInputStream when opening a content
83cdf0e10cSrcweir  */
84cdf0e10cSrcweir class UcbDataSink_Impl : public ::cppu::WeakImplHelper2< XActiveDataControl, XActiveDataSink >
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	UcbLockBytesRef         m_xLockBytes;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir public:
UcbDataSink_Impl(UcbLockBytes * pLockBytes)89cdf0e10cSrcweir 							UcbDataSink_Impl( UcbLockBytes* pLockBytes )
90cdf0e10cSrcweir 								: m_xLockBytes( pLockBytes )
91cdf0e10cSrcweir 							{}
92cdf0e10cSrcweir 
getLockBytes(void)93cdf0e10cSrcweir 	SvLockBytes*            getLockBytes (void)
94cdf0e10cSrcweir 							{ return m_xLockBytes; }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	// XActiveDataControl.
addListener(const Reference<XStreamListener> &)97cdf0e10cSrcweir     virtual void SAL_CALL   addListener ( const Reference<XStreamListener> &/*rxListener*/) throw(RuntimeException) {}
removeListener(const Reference<XStreamListener> &)98cdf0e10cSrcweir     virtual void SAL_CALL   removeListener ( const Reference<XStreamListener> &/*rxListener*/) throw(RuntimeException) {}
start(void)99cdf0e10cSrcweir     virtual void SAL_CALL   start (void) throw(RuntimeException) {}
terminate(void)100cdf0e10cSrcweir     virtual void SAL_CALL   terminate (void) throw(RuntimeException)
101cdf0e10cSrcweir                             { m_xLockBytes->terminate_Impl(); }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	// XActiveDataSink.
setInputStream(const Reference<XInputStream> & rxInputStream)104cdf0e10cSrcweir     virtual void SAL_CALL   setInputStream ( const Reference<XInputStream> &rxInputStream) throw(RuntimeException)
105cdf0e10cSrcweir                             { m_xLockBytes->setInputStream_Impl (rxInputStream); }
getInputStream(void)106cdf0e10cSrcweir     virtual Reference<XInputStream> SAL_CALL getInputStream (void) throw(RuntimeException)
107cdf0e10cSrcweir                             { return m_xLockBytes->getInputStream_Impl(); }
108cdf0e10cSrcweir };
109cdf0e10cSrcweir 
110cdf0e10cSrcweir /**
111cdf0e10cSrcweir     Helper class for getting a XStream when opening a content
112cdf0e10cSrcweir  */
113cdf0e10cSrcweir class UcbStreamer_Impl : public ::cppu::WeakImplHelper2< XActiveDataStreamer, XActiveDataControl >
114cdf0e10cSrcweir {
115cdf0e10cSrcweir     Reference < XStream >   m_xStream;
116cdf0e10cSrcweir     UcbLockBytesRef         m_xLockBytes;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir public:
119cdf0e10cSrcweir 
UcbStreamer_Impl(UcbLockBytes * pLockBytes)120cdf0e10cSrcweir                             UcbStreamer_Impl( UcbLockBytes* pLockBytes )
121cdf0e10cSrcweir                                 : m_xLockBytes( pLockBytes )
122cdf0e10cSrcweir                             {}
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	// XActiveDataControl.
addListener(const Reference<XStreamListener> &)125cdf0e10cSrcweir     virtual void SAL_CALL   addListener ( const Reference<XStreamListener> &/*rxListener*/) throw(RuntimeException) {}
removeListener(const Reference<XStreamListener> &)126cdf0e10cSrcweir     virtual void SAL_CALL   removeListener ( const Reference<XStreamListener> &/*rxListener*/) throw(RuntimeException) {}
start(void)127cdf0e10cSrcweir     virtual void SAL_CALL   start (void) throw(RuntimeException) {}
terminate(void)128cdf0e10cSrcweir     virtual void SAL_CALL   terminate (void) throw(RuntimeException)
129cdf0e10cSrcweir                             { m_xLockBytes->terminate_Impl(); }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     // XActiveDataStreamer
setStream(const Reference<XStream> & aStream)132cdf0e10cSrcweir     virtual void SAL_CALL   setStream( const Reference< XStream >& aStream ) throw(RuntimeException)
133cdf0e10cSrcweir                             { m_xStream = aStream; m_xLockBytes->setStream_Impl( aStream ); }
getStream()134cdf0e10cSrcweir     virtual Reference< XStream > SAL_CALL getStream() throw(RuntimeException)
135cdf0e10cSrcweir                             { return m_xStream; }
136cdf0e10cSrcweir };
137cdf0e10cSrcweir 
138cdf0e10cSrcweir /**
139cdf0e10cSrcweir     Helper class for progress handling while executing UCB commands
140cdf0e10cSrcweir  */
141cdf0e10cSrcweir class ProgressHandler_Impl: public ::cppu::WeakImplHelper1< XProgressHandler >
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     Link                    m_aProgress;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir public:
ProgressHandler_Impl(const Link & rLink)146cdf0e10cSrcweir                             ProgressHandler_Impl( const Link& rLink )
147cdf0e10cSrcweir                                 : m_aProgress( rLink )
148cdf0e10cSrcweir                             {}
149cdf0e10cSrcweir     // XProgressHandler
push(const Any &)150cdf0e10cSrcweir     virtual void SAL_CALL   push(const Any & /*rStatus*/) throw (RuntimeException) {}
pop()151cdf0e10cSrcweir     virtual void SAL_CALL   pop() throw (RuntimeException) {}
update(const Any &)152cdf0e10cSrcweir     virtual void SAL_CALL   update(const Any & /*rStatus*/) throw (RuntimeException)
153cdf0e10cSrcweir                             { if ( m_aProgress.IsSet() ) m_aProgress.Call( 0 ); }
154cdf0e10cSrcweir };
155cdf0e10cSrcweir 
156cdf0e10cSrcweir /**
157cdf0e10cSrcweir     Helper class for managing interactions and progress when executing UCB commands
158cdf0e10cSrcweir  */
159cdf0e10cSrcweir class UcbTaskEnvironment : public ::cppu::WeakImplHelper1< XCommandEnvironment >
160cdf0e10cSrcweir {
161cdf0e10cSrcweir     Reference< XInteractionHandler >                m_xInteractionHandler;
162cdf0e10cSrcweir     Reference< XProgressHandler >                   m_xProgressHandler;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir public:
UcbTaskEnvironment(const Reference<XInteractionHandler> & rxInteractionHandler,const Reference<XProgressHandler> & rxProgressHandler)165cdf0e10cSrcweir                             UcbTaskEnvironment( const Reference< XInteractionHandler>& rxInteractionHandler,
166cdf0e10cSrcweir                                                 const Reference< XProgressHandler>& rxProgressHandler )
167cdf0e10cSrcweir                                 : m_xInteractionHandler( rxInteractionHandler )
168cdf0e10cSrcweir                                 , m_xProgressHandler( rxProgressHandler )
169cdf0e10cSrcweir                             {}
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 
getInteractionHandler()172cdf0e10cSrcweir     virtual Reference<XInteractionHandler> SAL_CALL getInteractionHandler() throw (RuntimeException)
173cdf0e10cSrcweir 	{ return m_xInteractionHandler; }
174cdf0e10cSrcweir 
getProgressHandler()175cdf0e10cSrcweir     virtual Reference<XProgressHandler> SAL_CALL    getProgressHandler() throw (RuntimeException)
176cdf0e10cSrcweir 	{ return m_xProgressHandler; }
177cdf0e10cSrcweir };
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 
180cdf0e10cSrcweir /**
181cdf0e10cSrcweir     Helper class for property change notifies when executing UCB commands
182cdf0e10cSrcweir */
183cdf0e10cSrcweir class UcbPropertiesChangeListener_Impl : public ::cppu::WeakImplHelper1< XPropertiesChangeListener >
184cdf0e10cSrcweir {
185cdf0e10cSrcweir public:
186cdf0e10cSrcweir     UcbLockBytesRef         m_xLockBytes;
187cdf0e10cSrcweir 
UcbPropertiesChangeListener_Impl(UcbLockBytesRef rRef)188cdf0e10cSrcweir                             UcbPropertiesChangeListener_Impl( UcbLockBytesRef rRef )
189cdf0e10cSrcweir                                 : m_xLockBytes( rRef )
190cdf0e10cSrcweir                             {}
191cdf0e10cSrcweir 
disposing(const EventObject &)192cdf0e10cSrcweir     virtual void SAL_CALL   disposing ( const EventObject &/*rEvent*/) throw(RuntimeException) {}
193cdf0e10cSrcweir     virtual void SAL_CALL   propertiesChange ( const Sequence<PropertyChangeEvent> &rEvent) throw(RuntimeException);
194cdf0e10cSrcweir };
195cdf0e10cSrcweir 
propertiesChange(const Sequence<PropertyChangeEvent> & rEvent)196cdf0e10cSrcweir void SAL_CALL UcbPropertiesChangeListener_Impl::propertiesChange ( const Sequence<PropertyChangeEvent> &rEvent) throw(RuntimeException)
197cdf0e10cSrcweir {
198cdf0e10cSrcweir 	sal_Int32 i, n = rEvent.getLength();
199cdf0e10cSrcweir 	for (i = 0; i < n; i++)
200cdf0e10cSrcweir 	{
201cdf0e10cSrcweir 		PropertyChangeEvent evt (rEvent[i]);
202cdf0e10cSrcweir         if (evt.PropertyName == ::rtl::OUString::createFromAscii ("DocumentHeader"))
203cdf0e10cSrcweir 		{
204cdf0e10cSrcweir 			Sequence<DocumentHeaderField> aHead;
205cdf0e10cSrcweir 			if (evt.NewValue >>= aHead)
206cdf0e10cSrcweir 			{
207cdf0e10cSrcweir                 sal_Int32 k, m = aHead.getLength();
208cdf0e10cSrcweir                 for (k = 0; k < m; k++)
209cdf0e10cSrcweir                 {
210cdf0e10cSrcweir                     String aName( aHead[k].Name );
211cdf0e10cSrcweir                     String aValue( aHead[k].Value );
212cdf0e10cSrcweir 
213cdf0e10cSrcweir                     if (aName.CompareIgnoreCaseToAscii("Expires") == COMPARE_EQUAL)
214cdf0e10cSrcweir                     {
215cdf0e10cSrcweir                         DateTime aExpires (0, 0);
216cdf0e10cSrcweir                         if (INetRFC822Message::ParseDateField (aValue, aExpires))
217cdf0e10cSrcweir                         {
218cdf0e10cSrcweir                             aExpires.ConvertToLocalTime();
219cdf0e10cSrcweir                             m_xLockBytes->SetExpireDate_Impl( aExpires );
220cdf0e10cSrcweir                         }
221cdf0e10cSrcweir                     }
222cdf0e10cSrcweir                 }
223cdf0e10cSrcweir 			}
224cdf0e10cSrcweir 
225cdf0e10cSrcweir             m_xLockBytes->SetStreamValid_Impl();
226cdf0e10cSrcweir 		}
227cdf0e10cSrcweir         else if (evt.PropertyName == rtl::OUString::createFromAscii ("PresentationURL"))
228cdf0e10cSrcweir 		{
229cdf0e10cSrcweir             ::rtl::OUString aUrl;
230cdf0e10cSrcweir 			if (evt.NewValue >>= aUrl)
231cdf0e10cSrcweir 			{
232cdf0e10cSrcweir                 ::rtl::OUString aBad (::rtl::OUString::createFromAscii ("private:"));
233cdf0e10cSrcweir 				if (!(aUrl.compareTo (aBad, aBad.getLength()) == 0))
234cdf0e10cSrcweir 				{
235cdf0e10cSrcweir 					// URL changed (Redirection).
236cdf0e10cSrcweir                     m_xLockBytes->SetRealURL_Impl( aUrl );
237cdf0e10cSrcweir 				}
238cdf0e10cSrcweir 			}
239cdf0e10cSrcweir 		}
240cdf0e10cSrcweir         else if (evt.PropertyName == ::rtl::OUString::createFromAscii ("MediaType"))
241cdf0e10cSrcweir         {
242cdf0e10cSrcweir             ::rtl::OUString aContentType;
243cdf0e10cSrcweir             if (evt.NewValue >>= aContentType)
244cdf0e10cSrcweir                 m_xLockBytes->SetContentType_Impl( aContentType );
245cdf0e10cSrcweir         }
246cdf0e10cSrcweir 	}
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 
251cdf0e10cSrcweir class Moderator
252cdf0e10cSrcweir 	: public osl::Thread
253cdf0e10cSrcweir {
254cdf0e10cSrcweir 	// usage restriction:
255cdf0e10cSrcweir 	// It might be possible, that the call to the interactionhandler and/or
256cdf0e10cSrcweir 	// progresshandler is done asynchrounsly, while the 'execute' simply
257cdf0e10cSrcweir 	// returns. This would imply that these class must be refcounted !!!
258cdf0e10cSrcweir 
259cdf0e10cSrcweir public:
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 	Moderator(
262cdf0e10cSrcweir 		Reference < XContent >& xContent,
263cdf0e10cSrcweir 		Reference < XInteractionHandler >& xInteract,
264cdf0e10cSrcweir 		Reference < XProgressHandler >& xProgress,
265cdf0e10cSrcweir 		const Command& rArg
266cdf0e10cSrcweir     )
267cdf0e10cSrcweir         throw(
268cdf0e10cSrcweir             ContentCreationException,
269cdf0e10cSrcweir             RuntimeException
270cdf0e10cSrcweir         );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	~Moderator();
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 	enum ResultType {
276cdf0e10cSrcweir 		NORESULT,
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 		INTERACTIONREQUEST,    // reply expected
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 		PROGRESSPUSH,
281cdf0e10cSrcweir 		PROGRESSUPDATE,
282cdf0e10cSrcweir 		PROGRESSPOP,
283cdf0e10cSrcweir 
284cdf0e10cSrcweir         INPUTSTREAM,
285cdf0e10cSrcweir         STREAM,
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 		RESULT,
288cdf0e10cSrcweir 		TIMEDOUT,
289cdf0e10cSrcweir 		COMMANDABORTED,
290cdf0e10cSrcweir 		COMMANDFAILED,
291cdf0e10cSrcweir 		INTERACTIVEIO,
292cdf0e10cSrcweir 		UNSUPPORTED,
293cdf0e10cSrcweir 		GENERAL
294cdf0e10cSrcweir 	};
295cdf0e10cSrcweir 
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 	class ConditionRes
298cdf0e10cSrcweir 		: public salhelper::Condition
299cdf0e10cSrcweir 	{
300cdf0e10cSrcweir 	public:
301cdf0e10cSrcweir 
ConditionRes(osl::Mutex & aMutex,Moderator & aModerator)302cdf0e10cSrcweir 		ConditionRes(osl::Mutex& aMutex,Moderator& aModerator)
303cdf0e10cSrcweir 			: salhelper::Condition(aMutex),
304cdf0e10cSrcweir               m_aModerator(aModerator)
305cdf0e10cSrcweir 		{
306cdf0e10cSrcweir 		}
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 	protected:
309cdf0e10cSrcweir 
applies() const310cdf0e10cSrcweir 		bool applies() const {
311cdf0e10cSrcweir 			return m_aModerator.m_aResultType != NORESULT;
312cdf0e10cSrcweir 		}
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 	private:
315cdf0e10cSrcweir 
316cdf0e10cSrcweir 		Moderator& m_aModerator;
317cdf0e10cSrcweir 	};
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 	struct Result {
321cdf0e10cSrcweir 		ResultType        type;
322cdf0e10cSrcweir 		Any               result;
323cdf0e10cSrcweir 		sal_Int32         ioErrorCode;
324cdf0e10cSrcweir 	};
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 
327cdf0e10cSrcweir 	Result getResult(const sal_uInt32 milliSec);
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 
330cdf0e10cSrcweir 	enum ReplyType {
331cdf0e10cSrcweir 		NOREPLY,
332cdf0e10cSrcweir 		EXIT,
333cdf0e10cSrcweir 		RETRY,
334cdf0e10cSrcweir 		REQUESTHANDLED
335cdf0e10cSrcweir 	};
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 	class ConditionRep
339cdf0e10cSrcweir 		: public salhelper::Condition
340cdf0e10cSrcweir 	{
341cdf0e10cSrcweir 	public:
342cdf0e10cSrcweir 
ConditionRep(osl::Mutex & aMutex,Moderator & aModerator)343cdf0e10cSrcweir 		ConditionRep(osl::Mutex& aMutex,Moderator& aModerator)
344cdf0e10cSrcweir 			: salhelper::Condition(aMutex),
345cdf0e10cSrcweir               m_aModerator(aModerator)
346cdf0e10cSrcweir 		{
347cdf0e10cSrcweir 		}
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 	protected:
350cdf0e10cSrcweir 
applies() const351cdf0e10cSrcweir 		bool applies() const {
352cdf0e10cSrcweir 			return m_aModerator.m_aReplyType != NOREPLY;
353cdf0e10cSrcweir 		}
354cdf0e10cSrcweir 
355cdf0e10cSrcweir 	private:
356cdf0e10cSrcweir 
357cdf0e10cSrcweir 		Moderator& m_aModerator;
358cdf0e10cSrcweir 	};
359cdf0e10cSrcweir 
360cdf0e10cSrcweir 	void setReply(ReplyType);
361cdf0e10cSrcweir 
362cdf0e10cSrcweir 
363cdf0e10cSrcweir 	void handle( const Reference<XInteractionRequest >& Request );
364cdf0e10cSrcweir 
365cdf0e10cSrcweir 	void push( const Any& Status );
366cdf0e10cSrcweir 
367cdf0e10cSrcweir 	void update( const Any& Status );
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 	void pop(  );
370cdf0e10cSrcweir 
371cdf0e10cSrcweir     void setStream(const Reference< XStream >& aStream);
372cdf0e10cSrcweir 
373cdf0e10cSrcweir     void setInputStream(const Reference<XInputStream> &rxInputStream);
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 
376cdf0e10cSrcweir protected:
377cdf0e10cSrcweir 
378cdf0e10cSrcweir 	virtual void SAL_CALL run();
379cdf0e10cSrcweir 
380cdf0e10cSrcweir 	virtual void SAL_CALL onTerminated();
381cdf0e10cSrcweir 
382cdf0e10cSrcweir private:
383cdf0e10cSrcweir 
384cdf0e10cSrcweir     osl::Mutex        m_aMutex;
385cdf0e10cSrcweir 
386cdf0e10cSrcweir     friend class ConditionRes;
387cdf0e10cSrcweir 
388cdf0e10cSrcweir 	ConditionRes      m_aRes;
389cdf0e10cSrcweir 	ResultType        m_aResultType;
390cdf0e10cSrcweir 	sal_Int32         m_nIOErrorCode;
391cdf0e10cSrcweir 	Any               m_aResult;
392cdf0e10cSrcweir 
393cdf0e10cSrcweir     friend class ConditionRep;
394cdf0e10cSrcweir 
395cdf0e10cSrcweir 	ConditionRep      m_aRep;
396cdf0e10cSrcweir 	ReplyType         m_aReplyType;
397cdf0e10cSrcweir 
398cdf0e10cSrcweir     Command                           m_aArg;
399cdf0e10cSrcweir 	::ucbhelper::Content              m_aContent;
400cdf0e10cSrcweir };
401cdf0e10cSrcweir 
402cdf0e10cSrcweir 
403cdf0e10cSrcweir class ModeratorsActiveDataStreamer
404cdf0e10cSrcweir 	: public ::cppu::WeakImplHelper1<XActiveDataStreamer>
405cdf0e10cSrcweir {
406cdf0e10cSrcweir public:
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 	ModeratorsActiveDataStreamer(Moderator &theModerator);
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 	~ModeratorsActiveDataStreamer();
411cdf0e10cSrcweir 
412cdf0e10cSrcweir     // XActiveDataStreamer
413cdf0e10cSrcweir     virtual void SAL_CALL
414cdf0e10cSrcweir     setStream(
415cdf0e10cSrcweir         const Reference< XStream >& aStream
416cdf0e10cSrcweir     )
417cdf0e10cSrcweir         throw(
418cdf0e10cSrcweir             RuntimeException
419cdf0e10cSrcweir         );
420cdf0e10cSrcweir 
421cdf0e10cSrcweir     virtual Reference<XStream> SAL_CALL
getStream(void)422cdf0e10cSrcweir     getStream (
423cdf0e10cSrcweir         void
424cdf0e10cSrcweir     ) throw(
425cdf0e10cSrcweir         RuntimeException
426cdf0e10cSrcweir     )
427cdf0e10cSrcweir     {
428cdf0e10cSrcweir         osl::MutexGuard aGuard(m_aMutex);
429cdf0e10cSrcweir         return m_xStream;
430cdf0e10cSrcweir     }
431cdf0e10cSrcweir 
432cdf0e10cSrcweir 
433cdf0e10cSrcweir private:
434cdf0e10cSrcweir 
435cdf0e10cSrcweir     Moderator& m_aModerator;
436cdf0e10cSrcweir 
437cdf0e10cSrcweir     osl::Mutex m_aMutex;
438cdf0e10cSrcweir     Reference<XStream> m_xStream;
439cdf0e10cSrcweir };
440cdf0e10cSrcweir 
441cdf0e10cSrcweir 
442cdf0e10cSrcweir 
443cdf0e10cSrcweir class ModeratorsActiveDataSink
444cdf0e10cSrcweir 	: public ::cppu::WeakImplHelper1<XActiveDataSink>
445cdf0e10cSrcweir {
446cdf0e10cSrcweir public:
447cdf0e10cSrcweir 
448cdf0e10cSrcweir 	ModeratorsActiveDataSink(Moderator &theModerator);
449cdf0e10cSrcweir 
450cdf0e10cSrcweir 	~ModeratorsActiveDataSink();
451cdf0e10cSrcweir 
452cdf0e10cSrcweir 	// XActiveDataSink.
453cdf0e10cSrcweir     virtual void SAL_CALL
454cdf0e10cSrcweir     setInputStream (
455cdf0e10cSrcweir         const Reference<XInputStream> &rxInputStream
456cdf0e10cSrcweir     )
457cdf0e10cSrcweir         throw(
458cdf0e10cSrcweir             RuntimeException
459cdf0e10cSrcweir         );
460cdf0e10cSrcweir 
461cdf0e10cSrcweir     virtual Reference<XInputStream> SAL_CALL
getInputStream(void)462cdf0e10cSrcweir     getInputStream (
463cdf0e10cSrcweir         void
464cdf0e10cSrcweir     ) throw(
465cdf0e10cSrcweir         RuntimeException
466cdf0e10cSrcweir     )
467cdf0e10cSrcweir     {
468cdf0e10cSrcweir         osl::MutexGuard aGuard(m_aMutex);
469cdf0e10cSrcweir         return m_xStream;
470cdf0e10cSrcweir     }
471cdf0e10cSrcweir 
472cdf0e10cSrcweir 
473cdf0e10cSrcweir private:
474cdf0e10cSrcweir 
475cdf0e10cSrcweir 	Moderator& m_aModerator;
476cdf0e10cSrcweir     osl::Mutex m_aMutex;
477cdf0e10cSrcweir     Reference<XInputStream> m_xStream;
478cdf0e10cSrcweir };
479cdf0e10cSrcweir 
480cdf0e10cSrcweir 
481cdf0e10cSrcweir 
ModeratorsActiveDataSink(Moderator & theModerator)482cdf0e10cSrcweir ModeratorsActiveDataSink::ModeratorsActiveDataSink(Moderator &theModerator)
483cdf0e10cSrcweir     : m_aModerator(theModerator)
484cdf0e10cSrcweir {
485cdf0e10cSrcweir }
486cdf0e10cSrcweir 
487cdf0e10cSrcweir 
~ModeratorsActiveDataSink()488cdf0e10cSrcweir ModeratorsActiveDataSink::~ModeratorsActiveDataSink()
489cdf0e10cSrcweir {
490cdf0e10cSrcweir }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir // XActiveDataSink.
493cdf0e10cSrcweir void SAL_CALL
setInputStream(const Reference<XInputStream> & rxInputStream)494cdf0e10cSrcweir ModeratorsActiveDataSink::setInputStream (
495cdf0e10cSrcweir     const Reference<XInputStream> &rxInputStream
496cdf0e10cSrcweir )
497cdf0e10cSrcweir     throw(
498cdf0e10cSrcweir         RuntimeException
499cdf0e10cSrcweir     )
500cdf0e10cSrcweir {
501cdf0e10cSrcweir     m_aModerator.setInputStream(rxInputStream);
502cdf0e10cSrcweir     osl::MutexGuard aGuard(m_aMutex);
503cdf0e10cSrcweir     m_xStream = rxInputStream;
504cdf0e10cSrcweir }
505cdf0e10cSrcweir 
506cdf0e10cSrcweir 
ModeratorsActiveDataStreamer(Moderator & theModerator)507cdf0e10cSrcweir ModeratorsActiveDataStreamer::ModeratorsActiveDataStreamer(
508cdf0e10cSrcweir     Moderator &theModerator
509cdf0e10cSrcweir )
510cdf0e10cSrcweir     : m_aModerator(theModerator)
511cdf0e10cSrcweir {
512cdf0e10cSrcweir }
513cdf0e10cSrcweir 
514cdf0e10cSrcweir 
~ModeratorsActiveDataStreamer()515cdf0e10cSrcweir ModeratorsActiveDataStreamer::~ModeratorsActiveDataStreamer()
516cdf0e10cSrcweir {
517cdf0e10cSrcweir }
518cdf0e10cSrcweir 
519cdf0e10cSrcweir // XActiveDataStreamer.
520cdf0e10cSrcweir void SAL_CALL
setStream(const Reference<XStream> & rxStream)521cdf0e10cSrcweir ModeratorsActiveDataStreamer::setStream (
522cdf0e10cSrcweir     const Reference<XStream> &rxStream
523cdf0e10cSrcweir )
524cdf0e10cSrcweir     throw(
525cdf0e10cSrcweir         RuntimeException
526cdf0e10cSrcweir     )
527cdf0e10cSrcweir {
528cdf0e10cSrcweir     m_aModerator.setStream(rxStream);
529cdf0e10cSrcweir     osl::MutexGuard aGuard(m_aMutex);
530cdf0e10cSrcweir     m_xStream = rxStream;
531cdf0e10cSrcweir }
532cdf0e10cSrcweir 
533cdf0e10cSrcweir 
534cdf0e10cSrcweir 
535cdf0e10cSrcweir class ModeratorsInteractionHandler
536cdf0e10cSrcweir 	: public ::cppu::WeakImplHelper1<XInteractionHandler>
537cdf0e10cSrcweir {
538cdf0e10cSrcweir public:
539cdf0e10cSrcweir 
540cdf0e10cSrcweir 	ModeratorsInteractionHandler(Moderator &theModerator);
541cdf0e10cSrcweir 
542cdf0e10cSrcweir 	~ModeratorsInteractionHandler();
543cdf0e10cSrcweir 
544cdf0e10cSrcweir 	virtual void SAL_CALL
545cdf0e10cSrcweir 	handle( const Reference<XInteractionRequest >& Request )
546cdf0e10cSrcweir 		throw (RuntimeException);
547cdf0e10cSrcweir 
548cdf0e10cSrcweir private:
549cdf0e10cSrcweir 
550cdf0e10cSrcweir 	Moderator& m_aModerator;
551cdf0e10cSrcweir };
552cdf0e10cSrcweir 
553cdf0e10cSrcweir 
554cdf0e10cSrcweir class ModeratorsProgressHandler
555cdf0e10cSrcweir 	: public ::cppu::WeakImplHelper1<XProgressHandler>
556cdf0e10cSrcweir {
557cdf0e10cSrcweir public:
558cdf0e10cSrcweir 
559cdf0e10cSrcweir 	ModeratorsProgressHandler(Moderator &theModerator);
560cdf0e10cSrcweir 
561cdf0e10cSrcweir 	~ModeratorsProgressHandler();
562cdf0e10cSrcweir 
563cdf0e10cSrcweir 	virtual void SAL_CALL push( const Any& Status )
564cdf0e10cSrcweir 		throw (
565cdf0e10cSrcweir 			RuntimeException);
566cdf0e10cSrcweir 
567cdf0e10cSrcweir     virtual void SAL_CALL update( const Any& Status )
568cdf0e10cSrcweir 		throw (RuntimeException);
569cdf0e10cSrcweir 
570cdf0e10cSrcweir     virtual void SAL_CALL pop(  )
571cdf0e10cSrcweir 		throw (RuntimeException);
572cdf0e10cSrcweir 
573cdf0e10cSrcweir 
574cdf0e10cSrcweir private:
575cdf0e10cSrcweir 
576cdf0e10cSrcweir 	Moderator& m_aModerator;
577cdf0e10cSrcweir };
578cdf0e10cSrcweir 
579cdf0e10cSrcweir 
ModeratorsProgressHandler(Moderator & theModerator)580cdf0e10cSrcweir ModeratorsProgressHandler::ModeratorsProgressHandler(Moderator &theModerator)
581cdf0e10cSrcweir 	: m_aModerator(theModerator)
582cdf0e10cSrcweir {
583cdf0e10cSrcweir }
584cdf0e10cSrcweir 
~ModeratorsProgressHandler()585cdf0e10cSrcweir ModeratorsProgressHandler::~ModeratorsProgressHandler()
586cdf0e10cSrcweir {
587cdf0e10cSrcweir }
588cdf0e10cSrcweir 
589cdf0e10cSrcweir 
push(const Any & Status)590cdf0e10cSrcweir void SAL_CALL ModeratorsProgressHandler::push( const Any& Status )
591cdf0e10cSrcweir 	throw (
592cdf0e10cSrcweir 		RuntimeException)
593cdf0e10cSrcweir {
594cdf0e10cSrcweir 	m_aModerator.push(Status);
595cdf0e10cSrcweir }
596cdf0e10cSrcweir 
597cdf0e10cSrcweir 
update(const Any & Status)598cdf0e10cSrcweir void SAL_CALL ModeratorsProgressHandler::update( const Any& Status )
599cdf0e10cSrcweir 	throw (RuntimeException)
600cdf0e10cSrcweir {
601cdf0e10cSrcweir 	m_aModerator.update(Status);
602cdf0e10cSrcweir }
603cdf0e10cSrcweir 
604cdf0e10cSrcweir 
pop()605cdf0e10cSrcweir void SAL_CALL ModeratorsProgressHandler::pop(  )
606cdf0e10cSrcweir 	throw (RuntimeException)
607cdf0e10cSrcweir {
608cdf0e10cSrcweir 	m_aModerator.pop();
609cdf0e10cSrcweir }
610cdf0e10cSrcweir 
611cdf0e10cSrcweir 
612cdf0e10cSrcweir 
613cdf0e10cSrcweir 
ModeratorsInteractionHandler(Moderator & aModerator)614cdf0e10cSrcweir ModeratorsInteractionHandler::ModeratorsInteractionHandler(
615cdf0e10cSrcweir 	Moderator &aModerator)
616cdf0e10cSrcweir 	: m_aModerator(aModerator)
617cdf0e10cSrcweir {
618cdf0e10cSrcweir }
619cdf0e10cSrcweir 
620cdf0e10cSrcweir 
~ModeratorsInteractionHandler()621cdf0e10cSrcweir ModeratorsInteractionHandler::~ModeratorsInteractionHandler()
622cdf0e10cSrcweir {
623cdf0e10cSrcweir }
624cdf0e10cSrcweir 
625cdf0e10cSrcweir 
626cdf0e10cSrcweir void SAL_CALL
handle(const Reference<XInteractionRequest> & Request)627cdf0e10cSrcweir ModeratorsInteractionHandler::handle(
628cdf0e10cSrcweir 	const Reference<XInteractionRequest >& Request
629cdf0e10cSrcweir )
630cdf0e10cSrcweir 	throw (
631cdf0e10cSrcweir 		RuntimeException
632cdf0e10cSrcweir 	)
633cdf0e10cSrcweir {
634cdf0e10cSrcweir 	// wakes up the mainthread
635cdf0e10cSrcweir 	m_aModerator.handle(Request);
636cdf0e10cSrcweir }
637cdf0e10cSrcweir 
638cdf0e10cSrcweir 
639cdf0e10cSrcweir 
640cdf0e10cSrcweir 
Moderator(Reference<XContent> & xContent,Reference<XInteractionHandler> & xInteract,Reference<XProgressHandler> & xProgress,const Command & rArg)641cdf0e10cSrcweir Moderator::Moderator(
642cdf0e10cSrcweir 	Reference < XContent >& xContent,
643cdf0e10cSrcweir 	Reference < XInteractionHandler >& xInteract,
644cdf0e10cSrcweir 	Reference < XProgressHandler >& xProgress,
645cdf0e10cSrcweir 	const Command& rArg
646cdf0e10cSrcweir )
647cdf0e10cSrcweir     throw(
648cdf0e10cSrcweir         ::com::sun::star::ucb::ContentCreationException,
649cdf0e10cSrcweir         ::com::sun::star::uno::RuntimeException
650cdf0e10cSrcweir     )
651cdf0e10cSrcweir 	: m_aMutex(),
652cdf0e10cSrcweir 
653cdf0e10cSrcweir       m_aRes(m_aMutex,*this),
654cdf0e10cSrcweir 	  m_aResultType(NORESULT),
655cdf0e10cSrcweir 	  m_nIOErrorCode(0),
656cdf0e10cSrcweir 	  m_aResult(),
657cdf0e10cSrcweir 
658cdf0e10cSrcweir 	  m_aRep(m_aMutex,*this),
659cdf0e10cSrcweir 	  m_aReplyType(NOREPLY),
660cdf0e10cSrcweir 
661cdf0e10cSrcweir 	  m_aArg(rArg),
662cdf0e10cSrcweir       m_aContent(
663cdf0e10cSrcweir           xContent,
664cdf0e10cSrcweir           new UcbTaskEnvironment(
665cdf0e10cSrcweir               xInteract.is() ? new ModeratorsInteractionHandler(*this) : 0,
666cdf0e10cSrcweir               xProgress.is() ? new ModeratorsProgressHandler(*this) : 0
667cdf0e10cSrcweir           ))
668cdf0e10cSrcweir {
669cdf0e10cSrcweir     // now exchange the whole data sink stuff
670cdf0e10cSrcweir     // with a thread safe version
671cdf0e10cSrcweir 
672cdf0e10cSrcweir     Reference<XInterface> *pxSink = NULL;
673cdf0e10cSrcweir 
674cdf0e10cSrcweir     PostCommandArgument2 aPostArg;
675cdf0e10cSrcweir     OpenCommandArgument2 aOpenArg;
676cdf0e10cSrcweir 
677cdf0e10cSrcweir     int dec(2);
678cdf0e10cSrcweir     if(m_aArg.Argument >>= aPostArg) {
679cdf0e10cSrcweir         pxSink = &aPostArg.Sink;
680cdf0e10cSrcweir         dec = 0;
681cdf0e10cSrcweir     }
682cdf0e10cSrcweir     else if(m_aArg.Argument >>= aOpenArg) {
683cdf0e10cSrcweir         pxSink = &aOpenArg.Sink;
684cdf0e10cSrcweir         dec = 1;
685cdf0e10cSrcweir     }
686cdf0e10cSrcweir 
687cdf0e10cSrcweir     if(dec ==2)
688cdf0e10cSrcweir         throw ContentCreationException();
689cdf0e10cSrcweir 
690cdf0e10cSrcweir     Reference < XActiveDataSink > xActiveSink(*pxSink,UNO_QUERY);
691cdf0e10cSrcweir     if(xActiveSink.is())
692cdf0e10cSrcweir         *pxSink = Reference<XInterface>(
693cdf0e10cSrcweir             (cppu::OWeakObject*)new ModeratorsActiveDataSink(*this));
694cdf0e10cSrcweir 
695cdf0e10cSrcweir     Reference<XActiveDataStreamer> xStreamer( *pxSink, UNO_QUERY );
696cdf0e10cSrcweir     if ( xStreamer.is() )
697cdf0e10cSrcweir         *pxSink = Reference<XInterface>(
698cdf0e10cSrcweir             (cppu::OWeakObject*)new ModeratorsActiveDataStreamer(*this));
699cdf0e10cSrcweir 
700cdf0e10cSrcweir     if(dec == 0)
701cdf0e10cSrcweir         m_aArg.Argument <<= aPostArg;
702cdf0e10cSrcweir     else if(dec == 1)
703cdf0e10cSrcweir         m_aArg.Argument <<= aOpenArg;
704cdf0e10cSrcweir }
705cdf0e10cSrcweir 
706cdf0e10cSrcweir 
~Moderator()707cdf0e10cSrcweir Moderator::~Moderator()
708cdf0e10cSrcweir {
709cdf0e10cSrcweir }
710cdf0e10cSrcweir 
711cdf0e10cSrcweir 
getResult(const sal_uInt32 milliSec)712cdf0e10cSrcweir Moderator::Result Moderator::getResult(const sal_uInt32 milliSec)
713cdf0e10cSrcweir {
714cdf0e10cSrcweir 	Result ret;
715cdf0e10cSrcweir 	try {
716cdf0e10cSrcweir         salhelper::ConditionWaiter aWaiter(m_aRes,milliSec);
717cdf0e10cSrcweir         ret.type = m_aResultType;
718cdf0e10cSrcweir         ret.result = m_aResult;
719cdf0e10cSrcweir         ret.ioErrorCode = m_nIOErrorCode;
720cdf0e10cSrcweir 
721cdf0e10cSrcweir         // reset
722cdf0e10cSrcweir         m_aResultType = NORESULT;
723cdf0e10cSrcweir 	}
724cdf0e10cSrcweir 	catch(const salhelper::ConditionWaiter::timedout&)
725cdf0e10cSrcweir 	{
726cdf0e10cSrcweir 		ret.type = TIMEDOUT;
727cdf0e10cSrcweir 	}
728cdf0e10cSrcweir 
729cdf0e10cSrcweir 	return ret;
730cdf0e10cSrcweir }
731cdf0e10cSrcweir 
732cdf0e10cSrcweir 
setReply(ReplyType aReplyType)733cdf0e10cSrcweir void Moderator::setReply(ReplyType aReplyType )
734cdf0e10cSrcweir {
735cdf0e10cSrcweir 	salhelper::ConditionModifier aMod(m_aRep);
736cdf0e10cSrcweir 	m_aReplyType = aReplyType;
737cdf0e10cSrcweir }
738cdf0e10cSrcweir 
739cdf0e10cSrcweir 
handle(const Reference<XInteractionRequest> & Request)740cdf0e10cSrcweir void Moderator::handle( const Reference<XInteractionRequest >& Request )
741cdf0e10cSrcweir {
742cdf0e10cSrcweir 	ReplyType aReplyType;
743cdf0e10cSrcweir 
744cdf0e10cSrcweir 	do {
745cdf0e10cSrcweir 		{
746cdf0e10cSrcweir 			salhelper::ConditionModifier aMod(m_aRes);
747cdf0e10cSrcweir 			m_aResultType = INTERACTIONREQUEST;
748cdf0e10cSrcweir 			m_aResult <<= Request;
749cdf0e10cSrcweir 		}
750cdf0e10cSrcweir 
751cdf0e10cSrcweir 		{
752cdf0e10cSrcweir 			salhelper::ConditionWaiter aWait(m_aRep);
753cdf0e10cSrcweir 			aReplyType = m_aReplyType;
754cdf0e10cSrcweir 
755cdf0e10cSrcweir 			// reset
756cdf0e10cSrcweir 			m_aReplyType = NOREPLY;
757cdf0e10cSrcweir 		}
758cdf0e10cSrcweir 
759cdf0e10cSrcweir 		if(aReplyType == EXIT) {
760cdf0e10cSrcweir 			Sequence<Reference<XInteractionContinuation> > aSeq(
761cdf0e10cSrcweir 				Request->getContinuations());
762cdf0e10cSrcweir 			for(sal_Int32 i = 0; i < aSeq.getLength(); ++i) {
763cdf0e10cSrcweir 				Reference<XInteractionAbort> aRef(aSeq[i],UNO_QUERY);
764cdf0e10cSrcweir 				if(aRef.is()) {
765cdf0e10cSrcweir 					aRef->select();
766cdf0e10cSrcweir 				}
767cdf0e10cSrcweir 			}
768cdf0e10cSrcweir 
769cdf0e10cSrcweir 			// resignal the exitcondition
770cdf0e10cSrcweir 			setReply(EXIT);
771cdf0e10cSrcweir 			break;
772cdf0e10cSrcweir 		}
773cdf0e10cSrcweir 	} while(aReplyType != REQUESTHANDLED);
774cdf0e10cSrcweir }
775cdf0e10cSrcweir 
776cdf0e10cSrcweir 
777cdf0e10cSrcweir 
push(const Any & Status)778cdf0e10cSrcweir void Moderator::push( const Any& Status )
779cdf0e10cSrcweir {
780cdf0e10cSrcweir     {
781cdf0e10cSrcweir         salhelper::ConditionModifier aMod(m_aRes);
782cdf0e10cSrcweir         m_aResultType = PROGRESSPUSH;
783cdf0e10cSrcweir         m_aResult = Status;
784cdf0e10cSrcweir     }
785cdf0e10cSrcweir 	ReplyType aReplyType;
786cdf0e10cSrcweir     {
787cdf0e10cSrcweir         salhelper::ConditionWaiter aWait(m_aRep);
788cdf0e10cSrcweir 		aReplyType = m_aReplyType;
789cdf0e10cSrcweir 		m_aReplyType = NOREPLY;
790cdf0e10cSrcweir     }
791cdf0e10cSrcweir 	if(aReplyType == EXIT)
792cdf0e10cSrcweir 		setReply(EXIT);
793cdf0e10cSrcweir }
794cdf0e10cSrcweir 
795cdf0e10cSrcweir 
update(const Any & Status)796cdf0e10cSrcweir void Moderator::update( const Any& Status )
797cdf0e10cSrcweir {
798cdf0e10cSrcweir 	{
799cdf0e10cSrcweir         salhelper::ConditionModifier aMod(m_aRes);
800cdf0e10cSrcweir         m_aResultType = PROGRESSUPDATE;
801cdf0e10cSrcweir         m_aResult = Status;
802cdf0e10cSrcweir     }
803cdf0e10cSrcweir 	ReplyType aReplyType;
804cdf0e10cSrcweir     {
805cdf0e10cSrcweir         salhelper::ConditionWaiter aWait(m_aRep);
806cdf0e10cSrcweir 		aReplyType = m_aReplyType;
807cdf0e10cSrcweir 		m_aReplyType = NOREPLY;
808cdf0e10cSrcweir     }
809cdf0e10cSrcweir 	if(aReplyType == EXIT)
810cdf0e10cSrcweir 		setReply(EXIT);
811cdf0e10cSrcweir }
812cdf0e10cSrcweir 
813cdf0e10cSrcweir 
pop()814cdf0e10cSrcweir void Moderator::pop(  )
815cdf0e10cSrcweir {
816cdf0e10cSrcweir     {
817cdf0e10cSrcweir         salhelper::ConditionModifier aMod(m_aRes);
818cdf0e10cSrcweir         m_aResultType = PROGRESSPOP;
819cdf0e10cSrcweir     }
820cdf0e10cSrcweir 	ReplyType aReplyType;
821cdf0e10cSrcweir     {
822cdf0e10cSrcweir         salhelper::ConditionWaiter aWait(m_aRep);
823cdf0e10cSrcweir 		aReplyType = m_aReplyType;
824cdf0e10cSrcweir 		m_aReplyType = NOREPLY;
825cdf0e10cSrcweir 	}
826cdf0e10cSrcweir 	if(aReplyType == EXIT)
827cdf0e10cSrcweir 		setReply(EXIT);
828cdf0e10cSrcweir }
829cdf0e10cSrcweir 
830cdf0e10cSrcweir 
setStream(const Reference<XStream> & aStream)831cdf0e10cSrcweir void Moderator::setStream(const Reference< XStream >& aStream)
832cdf0e10cSrcweir {
833cdf0e10cSrcweir     {
834cdf0e10cSrcweir         salhelper::ConditionModifier aMod(m_aRes);
835cdf0e10cSrcweir         m_aResultType = STREAM;
836cdf0e10cSrcweir         m_aResult <<= aStream;
837cdf0e10cSrcweir     }
838cdf0e10cSrcweir 	ReplyType aReplyType;
839cdf0e10cSrcweir     {
840cdf0e10cSrcweir         salhelper::ConditionWaiter aWait(m_aRep);
841cdf0e10cSrcweir 		aReplyType = m_aReplyType;
842cdf0e10cSrcweir         m_aReplyType = NOREPLY;
843cdf0e10cSrcweir     }
844cdf0e10cSrcweir 	if(aReplyType == EXIT)
845cdf0e10cSrcweir 		setReply(EXIT);
846cdf0e10cSrcweir }
847cdf0e10cSrcweir 
848cdf0e10cSrcweir 
setInputStream(const Reference<XInputStream> & rxInputStream)849cdf0e10cSrcweir void Moderator::setInputStream(const Reference<XInputStream> &rxInputStream)
850cdf0e10cSrcweir {
851cdf0e10cSrcweir     {
852cdf0e10cSrcweir         salhelper::ConditionModifier aMod(m_aRes);
853cdf0e10cSrcweir         m_aResultType = INPUTSTREAM;
854cdf0e10cSrcweir         m_aResult <<= rxInputStream;
855cdf0e10cSrcweir     }
856cdf0e10cSrcweir 	ReplyType aReplyType;
857cdf0e10cSrcweir     {
858cdf0e10cSrcweir         salhelper::ConditionWaiter aWait(m_aRep);
859cdf0e10cSrcweir 		aReplyType = m_aReplyType;
860cdf0e10cSrcweir         m_aReplyType = NOREPLY;
861cdf0e10cSrcweir     }
862cdf0e10cSrcweir 	if(aReplyType == EXIT)
863cdf0e10cSrcweir 		setReply(EXIT);
864cdf0e10cSrcweir }
865cdf0e10cSrcweir 
866cdf0e10cSrcweir 
867cdf0e10cSrcweir 
run()868cdf0e10cSrcweir void SAL_CALL Moderator::run()
869cdf0e10cSrcweir {
870cdf0e10cSrcweir 	ResultType aResultType;
871cdf0e10cSrcweir 	Any        aResult;
872cdf0e10cSrcweir 	sal_Int32  nIOErrorCode = 0;
873cdf0e10cSrcweir 
874cdf0e10cSrcweir     try
875cdf0e10cSrcweir     {
876cdf0e10cSrcweir 		aResult = m_aContent.executeCommand(m_aArg.Name,m_aArg.Argument);
877cdf0e10cSrcweir 		aResultType = RESULT;
878cdf0e10cSrcweir     }
879cdf0e10cSrcweir     catch ( CommandAbortedException )
880cdf0e10cSrcweir     {
881cdf0e10cSrcweir 		aResultType = COMMANDABORTED;
882cdf0e10cSrcweir     }
883cdf0e10cSrcweir     catch ( CommandFailedException )
884cdf0e10cSrcweir     {
885cdf0e10cSrcweir 		aResultType = COMMANDFAILED;
886cdf0e10cSrcweir     }
887cdf0e10cSrcweir     catch ( InteractiveIOException& r )
888cdf0e10cSrcweir     {
889cdf0e10cSrcweir 		nIOErrorCode = r.Code;
890cdf0e10cSrcweir 		aResultType = INTERACTIVEIO;
891cdf0e10cSrcweir     }
892cdf0e10cSrcweir     catch ( UnsupportedDataSinkException& )
893cdf0e10cSrcweir     {
894cdf0e10cSrcweir 		aResultType = UNSUPPORTED;
895cdf0e10cSrcweir     }
896cdf0e10cSrcweir     catch ( Exception )
897cdf0e10cSrcweir     {
898cdf0e10cSrcweir 		aResultType = GENERAL;
899cdf0e10cSrcweir     }
900cdf0e10cSrcweir 
901cdf0e10cSrcweir 	{
902cdf0e10cSrcweir 		salhelper::ConditionModifier aMod(m_aRes);
903cdf0e10cSrcweir 		m_aResultType = aResultType;
904cdf0e10cSrcweir 		m_aResult = aResult;
905cdf0e10cSrcweir 		m_nIOErrorCode = nIOErrorCode;
906cdf0e10cSrcweir 	}
907cdf0e10cSrcweir }
908cdf0e10cSrcweir 
909cdf0e10cSrcweir 
910cdf0e10cSrcweir 
onTerminated()911cdf0e10cSrcweir void SAL_CALL Moderator::onTerminated()
912cdf0e10cSrcweir {
913cdf0e10cSrcweir     {
914cdf0e10cSrcweir         salhelper::ConditionWaiter aWaiter(m_aRep);
915cdf0e10cSrcweir     }
916cdf0e10cSrcweir  	delete this;
917cdf0e10cSrcweir }
918cdf0e10cSrcweir 
919cdf0e10cSrcweir 
920cdf0e10cSrcweir /**
921cdf0e10cSrcweir    Function for opening UCB contents synchronously,
922cdf0e10cSrcweir    but with handled timeout;
923cdf0e10cSrcweir */
924cdf0e10cSrcweir 
925cdf0e10cSrcweir static sal_Bool _UCBOpenContentSync(
926cdf0e10cSrcweir 	UcbLockBytesRef xLockBytes,
927cdf0e10cSrcweir 	Reference < XContent > xContent,
928cdf0e10cSrcweir 	const Command& rArg,
929cdf0e10cSrcweir 	Reference < XInterface > xSink,
930cdf0e10cSrcweir 	Reference < XInteractionHandler > xInteract,
931cdf0e10cSrcweir 	Reference < XProgressHandler > xProgress,
932cdf0e10cSrcweir 	UcbLockBytesHandlerRef xHandler );
933cdf0e10cSrcweir 
934cdf0e10cSrcweir 
UCBOpenContentSync(UcbLockBytesRef xLockBytes,Reference<XContent> xContent,const Command & rArg,Reference<XInterface> xSink,Reference<XInteractionHandler> xInteract,Reference<XProgressHandler> xProgress,UcbLockBytesHandlerRef xHandler)935cdf0e10cSrcweir static sal_Bool UCBOpenContentSync(
936cdf0e10cSrcweir 	UcbLockBytesRef xLockBytes,
937cdf0e10cSrcweir 	Reference < XContent > xContent,
938cdf0e10cSrcweir 	const Command& rArg,
939cdf0e10cSrcweir 	Reference < XInterface > xSink,
940cdf0e10cSrcweir 	Reference < XInteractionHandler > xInteract,
941cdf0e10cSrcweir 	Reference < XProgressHandler > xProgress,
942cdf0e10cSrcweir 	UcbLockBytesHandlerRef xHandler )
943cdf0e10cSrcweir {
944cdf0e10cSrcweir     // http protocol must be handled in a special way:
945cdf0e10cSrcweir 	//        during the opening process the input stream may change
946cdf0e10cSrcweir     //        only the last inputstream after notifying the document
947cdf0e10cSrcweir 	//        headers is valid
948cdf0e10cSrcweir 
949cdf0e10cSrcweir 	Reference<XContentIdentifier> xContId(
950cdf0e10cSrcweir 		xContent.is() ? xContent->getIdentifier() : 0 );
951cdf0e10cSrcweir 
952cdf0e10cSrcweir 	rtl::OUString aScheme;
953cdf0e10cSrcweir 	if(xContId.is())
954cdf0e10cSrcweir 		aScheme = xContId->getContentProviderScheme();
955cdf0e10cSrcweir 
956cdf0e10cSrcweir     // now determine wether we use a timeout or not;
957cdf0e10cSrcweir     if( ! aScheme.equalsIgnoreAsciiCaseAscii("http")                &&
958cdf0e10cSrcweir 		! aScheme.equalsIgnoreAsciiCaseAscii("https")                &&
959cdf0e10cSrcweir         ! aScheme.equalsIgnoreAsciiCaseAscii("vnd.sun.star.webdav") &&
960cdf0e10cSrcweir         ! aScheme.equalsIgnoreAsciiCaseAscii("ftp"))
961cdf0e10cSrcweir 		return _UCBOpenContentSync(
962cdf0e10cSrcweir 			xLockBytes,xContent,rArg,xSink,xInteract,xProgress,xHandler);
963cdf0e10cSrcweir 
964cdf0e10cSrcweir     if ( (aScheme.compareToAscii( "http" ) != COMPARE_EQUAL) ||
965cdf0e10cSrcweir 		 (aScheme.compareToAscii( "https" ) != COMPARE_EQUAL) )
966cdf0e10cSrcweir         xLockBytes->SetStreamValid_Impl();
967cdf0e10cSrcweir 
968cdf0e10cSrcweir     Reference< XPropertiesChangeListener > xListener;
969cdf0e10cSrcweir 	Reference< XPropertiesChangeNotifier > xProps(xContent,UNO_QUERY);
970cdf0e10cSrcweir     if(xProps.is()) {
971cdf0e10cSrcweir 		xListener =
972cdf0e10cSrcweir 			new UcbPropertiesChangeListener_Impl(xLockBytes);
973cdf0e10cSrcweir         xProps->addPropertiesChangeListener(
974cdf0e10cSrcweir 			Sequence< ::rtl::OUString >(),
975cdf0e10cSrcweir 			xListener);
976cdf0e10cSrcweir 	}
977cdf0e10cSrcweir 
978cdf0e10cSrcweir     Any aResult;
979cdf0e10cSrcweir     bool bException(false);
980cdf0e10cSrcweir     bool bAborted(false);
981cdf0e10cSrcweir     bool bResultAchieved(false);
982cdf0e10cSrcweir 
983cdf0e10cSrcweir     Moderator* pMod = 0;
984cdf0e10cSrcweir     try {
985cdf0e10cSrcweir         pMod = new Moderator(xContent,xInteract,xProgress,rArg);
986cdf0e10cSrcweir         pMod->create();
987cdf0e10cSrcweir     } catch(const ContentCreationException&) {
988cdf0e10cSrcweir         bResultAchieved = bException = true;
989cdf0e10cSrcweir         xLockBytes->SetError( ERRCODE_IO_GENERAL );
990cdf0e10cSrcweir     }
991cdf0e10cSrcweir 
992cdf0e10cSrcweir     sal_uInt32 nTimeout(5000); // initially 5000 milliSec
993cdf0e10cSrcweir 	while(!bResultAchieved) {
994cdf0e10cSrcweir 
995cdf0e10cSrcweir         Moderator::Result res;
996cdf0e10cSrcweir 		// try to get the result for with timeout
997cdf0e10cSrcweir 		res = pMod->getResult(nTimeout);
998cdf0e10cSrcweir 
999cdf0e10cSrcweir 		switch(res.type) {
1000cdf0e10cSrcweir         case Moderator::PROGRESSPUSH:
1001cdf0e10cSrcweir 			{
1002cdf0e10cSrcweir 				if(xProgress.is())
1003cdf0e10cSrcweir 					xProgress->push(res.result);
1004cdf0e10cSrcweir 				pMod->setReply(Moderator::REQUESTHANDLED);
1005cdf0e10cSrcweir 				break;
1006cdf0e10cSrcweir 			}
1007cdf0e10cSrcweir         case Moderator::PROGRESSUPDATE:
1008cdf0e10cSrcweir 			{
1009cdf0e10cSrcweir 				if(xProgress.is())
1010cdf0e10cSrcweir 					xProgress->update(res.result);
1011cdf0e10cSrcweir 				pMod->setReply(Moderator::REQUESTHANDLED);
1012cdf0e10cSrcweir 				break;
1013cdf0e10cSrcweir 			}
1014cdf0e10cSrcweir         case Moderator::PROGRESSPOP:
1015cdf0e10cSrcweir 			{
1016cdf0e10cSrcweir 				if(xProgress.is())
1017cdf0e10cSrcweir 					xProgress->pop();
1018cdf0e10cSrcweir 				pMod->setReply(Moderator::REQUESTHANDLED);
1019cdf0e10cSrcweir 				break;
1020cdf0e10cSrcweir 			}
1021cdf0e10cSrcweir         case Moderator::STREAM:
1022cdf0e10cSrcweir             {
1023cdf0e10cSrcweir                 Reference<XStream> result;
1024cdf0e10cSrcweir                 if(res.result >>= result) {
1025cdf0e10cSrcweir                     Reference < XActiveDataStreamer > xStreamer(
1026cdf0e10cSrcweir                         xSink, UNO_QUERY
1027cdf0e10cSrcweir                     );
1028cdf0e10cSrcweir 
1029cdf0e10cSrcweir                     if(xStreamer.is())
1030cdf0e10cSrcweir                         xStreamer->setStream(result);
1031cdf0e10cSrcweir                 }
1032cdf0e10cSrcweir 				pMod->setReply(Moderator::REQUESTHANDLED);
1033cdf0e10cSrcweir                 break;
1034cdf0e10cSrcweir             }
1035cdf0e10cSrcweir         case Moderator::INPUTSTREAM:
1036cdf0e10cSrcweir             {
1037cdf0e10cSrcweir 				Reference<XInputStream> result;
1038cdf0e10cSrcweir 				res.result >>= result;
1039cdf0e10cSrcweir                 Reference < XActiveDataSink > xActiveSink(
1040cdf0e10cSrcweir                     xSink, UNO_QUERY
1041cdf0e10cSrcweir                 );
1042cdf0e10cSrcweir 
1043cdf0e10cSrcweir                 if(xActiveSink.is())
1044cdf0e10cSrcweir                     xActiveSink->setInputStream(result);
1045cdf0e10cSrcweir 				pMod->setReply(Moderator::REQUESTHANDLED);
1046cdf0e10cSrcweir                 break;
1047cdf0e10cSrcweir             }
1048cdf0e10cSrcweir         case Moderator::TIMEDOUT:
1049cdf0e10cSrcweir 			{
1050cdf0e10cSrcweir 				Reference<XInteractionRetry> xRet;
1051cdf0e10cSrcweir 				if(xInteract.is()) {
1052cdf0e10cSrcweir 					InteractiveNetworkConnectException aExcep;
1053cdf0e10cSrcweir 					INetURLObject aURL(
1054cdf0e10cSrcweir 						xContId.is() ?
1055cdf0e10cSrcweir 						xContId->getContentIdentifier() :
1056cdf0e10cSrcweir 						rtl::OUString() );
1057cdf0e10cSrcweir 					aExcep.Server = aURL.GetHost();
1058cdf0e10cSrcweir 					aExcep.Classification = InteractionClassification_ERROR;
1059cdf0e10cSrcweir 					aExcep.Message =
1060cdf0e10cSrcweir 						rtl::OUString(
1061cdf0e10cSrcweir 							RTL_CONSTASCII_USTRINGPARAM(
1062cdf0e10cSrcweir 								"server not responding after five seconds"));
1063cdf0e10cSrcweir 					Any request;
1064cdf0e10cSrcweir 					request <<= aExcep;
1065cdf0e10cSrcweir 					ucbhelper::InteractionRequest *ir =
1066cdf0e10cSrcweir 						new ucbhelper::InteractionRequest(request);
1067cdf0e10cSrcweir 					Reference<XInteractionRequest> xIR(ir);
1068cdf0e10cSrcweir 					Sequence<Reference<XInteractionContinuation> > aSeq(2);
1069cdf0e10cSrcweir 					ucbhelper::InteractionRetry *retryP =
1070cdf0e10cSrcweir 						new ucbhelper::InteractionRetry(ir);
1071cdf0e10cSrcweir 					aSeq[0] = retryP;
1072cdf0e10cSrcweir 					ucbhelper::InteractionAbort *abortP =
1073cdf0e10cSrcweir 						new ucbhelper::InteractionAbort(ir);
1074cdf0e10cSrcweir 					aSeq[1] = abortP;
1075cdf0e10cSrcweir 
1076cdf0e10cSrcweir 					ir->setContinuations(aSeq);
1077cdf0e10cSrcweir 					xInteract->handle(xIR);
1078cdf0e10cSrcweir 					rtl::Reference< ucbhelper::InteractionContinuation > ref
1079cdf0e10cSrcweir 						= ir->getSelection();
1080cdf0e10cSrcweir 					if(ref.is()) {
1081cdf0e10cSrcweir 						Reference<XInterface> xInt(ref.get());
1082cdf0e10cSrcweir 						xRet = Reference<XInteractionRetry>(xInt,UNO_QUERY);
1083cdf0e10cSrcweir 					}
1084cdf0e10cSrcweir 				}
1085cdf0e10cSrcweir 
1086cdf0e10cSrcweir 				if(!xRet.is()) {
1087cdf0e10cSrcweir 					bAborted = true;
1088cdf0e10cSrcweir 					xLockBytes->SetError(ERRCODE_ABORT);
1089cdf0e10cSrcweir 				}
1090cdf0e10cSrcweir 
1091cdf0e10cSrcweir 				break;
1092cdf0e10cSrcweir 			}
1093cdf0e10cSrcweir         case Moderator::INTERACTIONREQUEST:
1094cdf0e10cSrcweir 			{
1095cdf0e10cSrcweir 				Reference<XInteractionRequest> Request;
1096cdf0e10cSrcweir 				res.result >>= Request;
1097cdf0e10cSrcweir 				xInteract->handle(Request);
1098cdf0e10cSrcweir 				pMod->setReply(Moderator::REQUESTHANDLED);
1099cdf0e10cSrcweir 				break;
1100cdf0e10cSrcweir 			}
1101cdf0e10cSrcweir         case Moderator::RESULT:
1102cdf0e10cSrcweir 			{
1103cdf0e10cSrcweir 				bResultAchieved = true;
1104cdf0e10cSrcweir 				aResult = res.result;
1105cdf0e10cSrcweir 				break;
1106cdf0e10cSrcweir 			}
1107cdf0e10cSrcweir         case Moderator::COMMANDABORTED:
1108cdf0e10cSrcweir 			{
1109cdf0e10cSrcweir 				bAborted = true;
1110cdf0e10cSrcweir 				xLockBytes->SetError( ERRCODE_ABORT );
1111cdf0e10cSrcweir 				break;
1112cdf0e10cSrcweir 			}
1113cdf0e10cSrcweir         case Moderator::COMMANDFAILED:
1114cdf0e10cSrcweir 			{
1115cdf0e10cSrcweir 				bAborted = true;
1116cdf0e10cSrcweir 				xLockBytes->SetError( ERRCODE_ABORT );
1117cdf0e10cSrcweir 				break;
1118cdf0e10cSrcweir 			}
1119cdf0e10cSrcweir         case Moderator::INTERACTIVEIO:
1120cdf0e10cSrcweir 			{
1121cdf0e10cSrcweir 				bException = true;
1122cdf0e10cSrcweir 				if ( res.ioErrorCode == IOErrorCode_ACCESS_DENIED ||
1123cdf0e10cSrcweir 					 res.ioErrorCode == IOErrorCode_LOCKING_VIOLATION )
1124cdf0e10cSrcweir 					xLockBytes->SetError( ERRCODE_IO_ACCESSDENIED );
1125cdf0e10cSrcweir 				else if ( res.ioErrorCode == IOErrorCode_NOT_EXISTING )
1126cdf0e10cSrcweir 					xLockBytes->SetError( ERRCODE_IO_NOTEXISTS );
1127cdf0e10cSrcweir 				else if ( res.ioErrorCode == IOErrorCode_CANT_READ )
1128cdf0e10cSrcweir 					xLockBytes->SetError( ERRCODE_IO_CANTREAD );
1129cdf0e10cSrcweir 				else
1130cdf0e10cSrcweir 					xLockBytes->SetError( ERRCODE_IO_GENERAL );
1131cdf0e10cSrcweir 				break;
1132cdf0e10cSrcweir 			}
1133cdf0e10cSrcweir         case Moderator::UNSUPPORTED:
1134cdf0e10cSrcweir 			{
1135cdf0e10cSrcweir 				bException = true;
1136cdf0e10cSrcweir 				xLockBytes->SetError( ERRCODE_IO_NOTSUPPORTED );
1137cdf0e10cSrcweir 				break;
1138cdf0e10cSrcweir 			}
1139cdf0e10cSrcweir         default:
1140cdf0e10cSrcweir 			{
1141cdf0e10cSrcweir 				bException = true;
1142cdf0e10cSrcweir 				xLockBytes->SetError( ERRCODE_IO_GENERAL );
1143cdf0e10cSrcweir 				break;
1144cdf0e10cSrcweir 			}
1145cdf0e10cSrcweir 		}
1146cdf0e10cSrcweir 
1147cdf0e10cSrcweir 		bResultAchieved |= bException;
1148cdf0e10cSrcweir 		bResultAchieved |= bAborted;
1149cdf0e10cSrcweir         if(nTimeout == 5000) nTimeout *= 2;
1150cdf0e10cSrcweir     }
1151cdf0e10cSrcweir 
1152cdf0e10cSrcweir     if(pMod) pMod->setReply(Moderator::EXIT);
1153cdf0e10cSrcweir 
1154cdf0e10cSrcweir     if ( bAborted || bException )
1155cdf0e10cSrcweir     {
1156cdf0e10cSrcweir         if( xHandler.Is() )
1157cdf0e10cSrcweir             xHandler->Handle( UcbLockBytesHandler::CANCEL, xLockBytes );
1158cdf0e10cSrcweir 
1159cdf0e10cSrcweir         Reference < XActiveDataSink > xActiveSink( xSink, UNO_QUERY );
1160cdf0e10cSrcweir         if ( xActiveSink.is() )
1161cdf0e10cSrcweir             xActiveSink->setInputStream( Reference < XInputStream >() );
1162cdf0e10cSrcweir 
1163cdf0e10cSrcweir         Reference < XActiveDataStreamer > xStreamer( xSink, UNO_QUERY );
1164cdf0e10cSrcweir         if ( xStreamer.is() )
1165cdf0e10cSrcweir             xStreamer->setStream( Reference < XStream >() );
1166cdf0e10cSrcweir     }
1167cdf0e10cSrcweir 
1168cdf0e10cSrcweir     Reference < XActiveDataControl > xControl( xSink, UNO_QUERY );
1169cdf0e10cSrcweir     if ( xControl.is() )
1170cdf0e10cSrcweir         xControl->terminate();
1171cdf0e10cSrcweir 
1172cdf0e10cSrcweir     if ( xProps.is() )
1173cdf0e10cSrcweir         xProps->removePropertiesChangeListener(
1174cdf0e10cSrcweir 			Sequence< ::rtl::OUString >(),
1175cdf0e10cSrcweir 			xListener );
1176cdf0e10cSrcweir 
1177cdf0e10cSrcweir     return ( bAborted || bException );
1178cdf0e10cSrcweir }
1179cdf0e10cSrcweir 
1180cdf0e10cSrcweir /**
1181cdf0e10cSrcweir     Function for opening UCB contents synchronously
1182cdf0e10cSrcweir  */
_UCBOpenContentSync(UcbLockBytesRef xLockBytes,Reference<XContent> xContent,const Command & rArg,Reference<XInterface> xSink,Reference<XInteractionHandler> xInteract,Reference<XProgressHandler> xProgress,UcbLockBytesHandlerRef xHandler)1183cdf0e10cSrcweir static sal_Bool _UCBOpenContentSync(
1184cdf0e10cSrcweir 	UcbLockBytesRef xLockBytes,
1185cdf0e10cSrcweir 	Reference < XContent > xContent,
1186cdf0e10cSrcweir 	const Command& rArg,
1187cdf0e10cSrcweir 	Reference < XInterface > xSink,
1188cdf0e10cSrcweir 	Reference < XInteractionHandler > xInteract,
1189cdf0e10cSrcweir 	Reference < XProgressHandler > xProgress,
1190cdf0e10cSrcweir 	UcbLockBytesHandlerRef xHandler )
1191cdf0e10cSrcweir {
1192cdf0e10cSrcweir     ::ucbhelper::Content aContent( xContent, new UcbTaskEnvironment( xInteract, xProgress ) );
1193cdf0e10cSrcweir     Reference < XContentIdentifier > xIdent = xContent->getIdentifier();
1194cdf0e10cSrcweir     ::rtl::OUString aScheme = xIdent->getContentProviderScheme();
1195cdf0e10cSrcweir 
1196cdf0e10cSrcweir     // http protocol must be handled in a special way: during the opening process the input stream may change
1197cdf0e10cSrcweir     // only the last inputstream after notifying the document headers is valid
1198cdf0e10cSrcweir     if ( aScheme.compareToAscii("http") != COMPARE_EQUAL )
1199cdf0e10cSrcweir         xLockBytes->SetStreamValid_Impl();
1200cdf0e10cSrcweir 
1201cdf0e10cSrcweir     Reference< XPropertiesChangeListener > xListener = new UcbPropertiesChangeListener_Impl( xLockBytes );
1202cdf0e10cSrcweir     Reference< XPropertiesChangeNotifier > xProps ( xContent, UNO_QUERY );
1203cdf0e10cSrcweir     if ( xProps.is() )
1204cdf0e10cSrcweir         xProps->addPropertiesChangeListener( Sequence< ::rtl::OUString >(), xListener );
1205cdf0e10cSrcweir 
1206cdf0e10cSrcweir     Any aResult;
1207cdf0e10cSrcweir     bool bException = false;
1208cdf0e10cSrcweir     bool bAborted = false;
1209cdf0e10cSrcweir 
1210cdf0e10cSrcweir     try
1211cdf0e10cSrcweir     {
1212cdf0e10cSrcweir         aResult = aContent.executeCommand( rArg.Name, rArg.Argument );
1213cdf0e10cSrcweir     }
1214cdf0e10cSrcweir     catch ( CommandAbortedException )
1215cdf0e10cSrcweir     {
1216cdf0e10cSrcweir         bAborted = true;
1217cdf0e10cSrcweir 		xLockBytes->SetError( ERRCODE_ABORT );
1218cdf0e10cSrcweir     }
1219cdf0e10cSrcweir     catch ( CommandFailedException )
1220cdf0e10cSrcweir     {
1221cdf0e10cSrcweir         bAborted = true;
1222cdf0e10cSrcweir 		xLockBytes->SetError( ERRCODE_ABORT );
1223cdf0e10cSrcweir     }
1224cdf0e10cSrcweir     catch ( InteractiveIOException& r )
1225cdf0e10cSrcweir     {
1226cdf0e10cSrcweir 		bException = true;
1227cdf0e10cSrcweir         if ( r.Code == IOErrorCode_ACCESS_DENIED || r.Code == IOErrorCode_LOCKING_VIOLATION )
1228cdf0e10cSrcweir 			xLockBytes->SetError( ERRCODE_IO_ACCESSDENIED );
1229cdf0e10cSrcweir 		else if ( r.Code == IOErrorCode_NOT_EXISTING )
1230cdf0e10cSrcweir 			xLockBytes->SetError( ERRCODE_IO_NOTEXISTS );
1231cdf0e10cSrcweir 		else if ( r.Code == IOErrorCode_CANT_READ )
1232cdf0e10cSrcweir 			xLockBytes->SetError( ERRCODE_IO_CANTREAD );
1233cdf0e10cSrcweir 		else
1234cdf0e10cSrcweir 			xLockBytes->SetError( ERRCODE_IO_GENERAL );
1235cdf0e10cSrcweir     }
1236cdf0e10cSrcweir     catch ( UnsupportedDataSinkException& )
1237cdf0e10cSrcweir     {
1238cdf0e10cSrcweir 		bException = true;
1239cdf0e10cSrcweir         xLockBytes->SetError( ERRCODE_IO_NOTSUPPORTED );
1240cdf0e10cSrcweir     }
1241cdf0e10cSrcweir     catch ( Exception )
1242cdf0e10cSrcweir     {
1243cdf0e10cSrcweir         bException = true;
1244cdf0e10cSrcweir 		xLockBytes->SetError( ERRCODE_IO_GENERAL );
1245cdf0e10cSrcweir     }
1246cdf0e10cSrcweir 
1247cdf0e10cSrcweir     if ( bAborted || bException )
1248cdf0e10cSrcweir     {
1249cdf0e10cSrcweir         if( xHandler.Is() )
1250cdf0e10cSrcweir             xHandler->Handle( UcbLockBytesHandler::CANCEL, xLockBytes );
1251cdf0e10cSrcweir 
1252cdf0e10cSrcweir         Reference < XActiveDataSink > xActiveSink( xSink, UNO_QUERY );
1253cdf0e10cSrcweir         if ( xActiveSink.is() )
1254cdf0e10cSrcweir             xActiveSink->setInputStream( Reference < XInputStream >() );
1255cdf0e10cSrcweir 
1256cdf0e10cSrcweir         Reference < XActiveDataStreamer > xStreamer( xSink, UNO_QUERY );
1257cdf0e10cSrcweir         if ( xStreamer.is() )
1258cdf0e10cSrcweir             xStreamer->setStream( Reference < XStream >() );
1259cdf0e10cSrcweir     }
1260cdf0e10cSrcweir 
1261cdf0e10cSrcweir     Reference < XActiveDataControl > xControl( xSink, UNO_QUERY );
1262cdf0e10cSrcweir     if ( xControl.is() )
1263cdf0e10cSrcweir         xControl->terminate();
1264cdf0e10cSrcweir 
1265cdf0e10cSrcweir 
1266cdf0e10cSrcweir     if ( xProps.is() )
1267cdf0e10cSrcweir         xProps->removePropertiesChangeListener( Sequence< ::rtl::OUString >(), xListener );
1268cdf0e10cSrcweir 
1269cdf0e10cSrcweir     return ( bAborted || bException );
1270cdf0e10cSrcweir }
1271cdf0e10cSrcweir 
1272cdf0e10cSrcweir 
1273cdf0e10cSrcweir //----------------------------------------------------------------------------
UcbLockBytes(UcbLockBytesHandler * pHandler)1274cdf0e10cSrcweir UcbLockBytes::UcbLockBytes( UcbLockBytesHandler* pHandler )
1275cdf0e10cSrcweir     : m_xInputStream (NULL)
1276cdf0e10cSrcweir     , m_pCommandThread( NULL )
1277cdf0e10cSrcweir     , m_xHandler( pHandler )
1278cdf0e10cSrcweir     , m_nError( ERRCODE_NONE )
1279cdf0e10cSrcweir     , m_bTerminated  (sal_False)
1280cdf0e10cSrcweir     , m_bDontClose( sal_False )
1281cdf0e10cSrcweir     , m_bStreamValid  (sal_False)
1282cdf0e10cSrcweir {
1283cdf0e10cSrcweir     SetSynchronMode( sal_True );
1284cdf0e10cSrcweir }
1285cdf0e10cSrcweir 
1286cdf0e10cSrcweir //----------------------------------------------------------------------------
~UcbLockBytes()1287cdf0e10cSrcweir UcbLockBytes::~UcbLockBytes()
1288cdf0e10cSrcweir {
1289cdf0e10cSrcweir     if ( !m_bDontClose )
1290cdf0e10cSrcweir     {
1291cdf0e10cSrcweir         if ( m_xInputStream.is() )
1292cdf0e10cSrcweir         {
1293cdf0e10cSrcweir 			try
1294cdf0e10cSrcweir 			{
1295cdf0e10cSrcweir 				m_xInputStream->closeInput();
1296cdf0e10cSrcweir 			}
1297cdf0e10cSrcweir 			catch ( RuntimeException const & )
1298cdf0e10cSrcweir 			{}
1299cdf0e10cSrcweir 			catch ( IOException const & )
1300cdf0e10cSrcweir 			{}
1301cdf0e10cSrcweir         }
1302cdf0e10cSrcweir     }
1303cdf0e10cSrcweir 
1304cdf0e10cSrcweir     if ( !m_xInputStream.is() && m_xOutputStream.is() )
1305cdf0e10cSrcweir     {
1306cdf0e10cSrcweir         try
1307cdf0e10cSrcweir         {
1308cdf0e10cSrcweir             m_xOutputStream->closeOutput();
1309cdf0e10cSrcweir         }
1310cdf0e10cSrcweir         catch ( RuntimeException const & )
1311cdf0e10cSrcweir         {}
1312cdf0e10cSrcweir         catch ( IOException const & )
1313cdf0e10cSrcweir         {}
1314cdf0e10cSrcweir     }
1315cdf0e10cSrcweir }
1316cdf0e10cSrcweir 
getInputStream()1317cdf0e10cSrcweir Reference < XInputStream > UcbLockBytes::getInputStream()
1318cdf0e10cSrcweir {
1319cdf0e10cSrcweir 	vos::OClearableGuard aGuard( m_aMutex );
1320cdf0e10cSrcweir     m_bDontClose = sal_True;
1321cdf0e10cSrcweir     return m_xInputStream;
1322cdf0e10cSrcweir }
1323cdf0e10cSrcweir 
getStream()1324cdf0e10cSrcweir Reference < XStream > UcbLockBytes::getStream()
1325cdf0e10cSrcweir {
1326cdf0e10cSrcweir 	vos::OClearableGuard aGuard( m_aMutex );
1327cdf0e10cSrcweir 	Reference < XStream > xStream( m_xSeekable, UNO_QUERY );
1328cdf0e10cSrcweir 	if ( xStream.is() )
1329cdf0e10cSrcweir     	m_bDontClose = sal_True;
1330cdf0e10cSrcweir     return xStream;
1331cdf0e10cSrcweir }
1332cdf0e10cSrcweir 
1333cdf0e10cSrcweir //----------------------------------------------------------------------------
1334cdf0e10cSrcweir 
setStream_Impl(const Reference<XStream> & aStream)1335cdf0e10cSrcweir sal_Bool UcbLockBytes::setStream_Impl( const Reference<XStream>& aStream )
1336cdf0e10cSrcweir {
1337cdf0e10cSrcweir 	vos::OClearableGuard aGuard( m_aMutex );
1338cdf0e10cSrcweir     if ( aStream.is() )
1339cdf0e10cSrcweir     {
1340cdf0e10cSrcweir         m_xOutputStream = aStream->getOutputStream();
1341cdf0e10cSrcweir         setInputStream_Impl( aStream->getInputStream(), sal_False );
1342cdf0e10cSrcweir         m_xSeekable = Reference < XSeekable > ( aStream, UNO_QUERY );
1343cdf0e10cSrcweir     }
1344cdf0e10cSrcweir     else
1345cdf0e10cSrcweir     {
1346cdf0e10cSrcweir         m_xOutputStream = Reference < XOutputStream >();
1347cdf0e10cSrcweir         setInputStream_Impl( Reference < XInputStream >() );
1348cdf0e10cSrcweir     }
1349cdf0e10cSrcweir 
1350cdf0e10cSrcweir     return m_xInputStream.is();
1351cdf0e10cSrcweir }
1352cdf0e10cSrcweir 
setInputStream_Impl(const Reference<XInputStream> & rxInputStream,sal_Bool bSetXSeekable)1353cdf0e10cSrcweir sal_Bool UcbLockBytes::setInputStream_Impl( const Reference<XInputStream> &rxInputStream, sal_Bool bSetXSeekable )
1354cdf0e10cSrcweir {
1355cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
1356cdf0e10cSrcweir 
1357cdf0e10cSrcweir 	try
1358cdf0e10cSrcweir 	{
1359cdf0e10cSrcweir 		vos::OClearableGuard aGuard( m_aMutex );
1360cdf0e10cSrcweir 
1361cdf0e10cSrcweir 		if ( !m_bDontClose && m_xInputStream.is() )
1362cdf0e10cSrcweir 			m_xInputStream->closeInput();
1363cdf0e10cSrcweir 
1364cdf0e10cSrcweir 		m_xInputStream = rxInputStream;
1365cdf0e10cSrcweir 
1366cdf0e10cSrcweir 		if( bSetXSeekable )
1367cdf0e10cSrcweir 		{
1368cdf0e10cSrcweir     		m_xSeekable = Reference < XSeekable > ( rxInputStream, UNO_QUERY );
1369cdf0e10cSrcweir 			if( !m_xSeekable.is() && rxInputStream.is() )
1370cdf0e10cSrcweir 			{
1371cdf0e10cSrcweir             	Reference < XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
1372cdf0e10cSrcweir 				Reference< XOutputStream > rxTempOut = Reference < XOutputStream > (
1373cdf0e10cSrcweir 									xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
1374cdf0e10cSrcweir 									UNO_QUERY );
1375cdf0e10cSrcweir 
1376cdf0e10cSrcweir 				if( rxTempOut.is() )
1377cdf0e10cSrcweir 				{
1378cdf0e10cSrcweir 					::comphelper::OStorageHelper::CopyInputToOutput( rxInputStream, rxTempOut );
1379cdf0e10cSrcweir 					m_xInputStream = Reference< XInputStream >( rxTempOut, UNO_QUERY );
1380cdf0e10cSrcweir         			m_xSeekable = Reference < XSeekable > ( rxTempOut, UNO_QUERY );
1381cdf0e10cSrcweir 				}
1382cdf0e10cSrcweir 			}
1383cdf0e10cSrcweir 		}
1384cdf0e10cSrcweir 
1385cdf0e10cSrcweir 		bRet = m_xInputStream.is();
1386cdf0e10cSrcweir 		// aGuard.clear();
1387cdf0e10cSrcweir 	}
1388cdf0e10cSrcweir 	catch( Exception& )
1389cdf0e10cSrcweir 	{}
1390cdf0e10cSrcweir 
1391cdf0e10cSrcweir     if ( m_bStreamValid && m_xInputStream.is() )
1392cdf0e10cSrcweir         m_aInitialized.set();
1393cdf0e10cSrcweir 
1394cdf0e10cSrcweir 	return bRet;
1395cdf0e10cSrcweir }
1396cdf0e10cSrcweir 
SetStreamValid_Impl()1397cdf0e10cSrcweir void UcbLockBytes::SetStreamValid_Impl()
1398cdf0e10cSrcweir {
1399cdf0e10cSrcweir     m_bStreamValid = sal_True;
1400cdf0e10cSrcweir     if ( m_xInputStream.is() )
1401cdf0e10cSrcweir         m_aInitialized.set();
1402cdf0e10cSrcweir }
1403cdf0e10cSrcweir 
1404cdf0e10cSrcweir //----------------------------------------------------------------------------
terminate_Impl()1405cdf0e10cSrcweir void UcbLockBytes::terminate_Impl()
1406cdf0e10cSrcweir {
1407cdf0e10cSrcweir 	m_bTerminated = sal_True;
1408cdf0e10cSrcweir 	m_aInitialized.set();
1409cdf0e10cSrcweir 	m_aTerminated.set();
1410cdf0e10cSrcweir 
1411cdf0e10cSrcweir     if ( GetError() == ERRCODE_NONE && !m_xInputStream.is() )
1412cdf0e10cSrcweir 	{
1413cdf0e10cSrcweir 		DBG_ERROR("No InputStream, but no error set!" );
1414cdf0e10cSrcweir         SetError( ERRCODE_IO_NOTEXISTS );
1415cdf0e10cSrcweir 	}
1416cdf0e10cSrcweir 
1417cdf0e10cSrcweir     if ( m_xHandler.Is() )
1418cdf0e10cSrcweir         m_xHandler->Handle( UcbLockBytesHandler::DONE, this );
1419cdf0e10cSrcweir }
1420cdf0e10cSrcweir 
1421cdf0e10cSrcweir //----------------------------------------------------------------------------
SetSynchronMode(sal_Bool bSynchron)1422cdf0e10cSrcweir void UcbLockBytes::SetSynchronMode (sal_Bool bSynchron)
1423cdf0e10cSrcweir {
1424cdf0e10cSrcweir 	SvLockBytes::SetSynchronMode (bSynchron);
1425cdf0e10cSrcweir }
1426cdf0e10cSrcweir 
1427cdf0e10cSrcweir //----------------------------------------------------------------------------
ReadAt(sal_uLong nPos,void * pBuffer,sal_uLong nCount,sal_uLong * pRead) const1428cdf0e10cSrcweir ErrCode UcbLockBytes::ReadAt ( sal_uLong nPos, void *pBuffer, sal_uLong nCount, sal_uLong *pRead) const
1429cdf0e10cSrcweir {
1430cdf0e10cSrcweir 	if ( IsSynchronMode() )
1431cdf0e10cSrcweir     {
1432cdf0e10cSrcweir         UcbLockBytes* pThis = const_cast < UcbLockBytes* >( this );
1433cdf0e10cSrcweir         pThis->m_aInitialized.wait();
1434cdf0e10cSrcweir     }
1435cdf0e10cSrcweir 
1436cdf0e10cSrcweir     Reference <XInputStream> xStream = getInputStream_Impl();
1437cdf0e10cSrcweir 	if ( !xStream.is() )
1438cdf0e10cSrcweir 	{
1439cdf0e10cSrcweir 		if ( m_bTerminated )
1440cdf0e10cSrcweir 			return ERRCODE_IO_CANTREAD;
1441cdf0e10cSrcweir 		else
1442cdf0e10cSrcweir 			return ERRCODE_IO_PENDING;
1443cdf0e10cSrcweir 	}
1444cdf0e10cSrcweir 
1445cdf0e10cSrcweir 	if ( pRead )
1446cdf0e10cSrcweir 		*pRead = 0;
1447cdf0e10cSrcweir 
1448cdf0e10cSrcweir     Reference <XSeekable> xSeekable = getSeekable_Impl();
1449cdf0e10cSrcweir     if ( !xSeekable.is() )
1450cdf0e10cSrcweir 		return ERRCODE_IO_CANTREAD;
1451cdf0e10cSrcweir 
1452cdf0e10cSrcweir 	try
1453cdf0e10cSrcweir 	{
1454cdf0e10cSrcweir         xSeekable->seek( nPos );
1455cdf0e10cSrcweir 	}
1456cdf0e10cSrcweir 	catch ( IOException )
1457cdf0e10cSrcweir 	{
1458cdf0e10cSrcweir 		return ERRCODE_IO_CANTSEEK;
1459cdf0e10cSrcweir 	}
1460cdf0e10cSrcweir 	catch (com::sun::star::lang::IllegalArgumentException)
1461cdf0e10cSrcweir 	{
1462cdf0e10cSrcweir 		return ERRCODE_IO_CANTSEEK;
1463cdf0e10cSrcweir 	}
1464cdf0e10cSrcweir 
1465cdf0e10cSrcweir 	Sequence<sal_Int8> aData;
1466cdf0e10cSrcweir 	sal_Int32          nSize;
1467cdf0e10cSrcweir 
1468cdf0e10cSrcweir 	nCount = VOS_MIN(nCount, 0x7FFFFFFF);
1469cdf0e10cSrcweir 	try
1470cdf0e10cSrcweir 	{
1471cdf0e10cSrcweir 		if ( !m_bTerminated && !IsSynchronMode() )
1472cdf0e10cSrcweir 		{
1473cdf0e10cSrcweir             sal_uInt64 nLen = xSeekable->getLength();
1474cdf0e10cSrcweir 			if ( nPos + nCount > nLen )
1475cdf0e10cSrcweir 				return ERRCODE_IO_PENDING;
1476cdf0e10cSrcweir 		}
1477cdf0e10cSrcweir 
1478cdf0e10cSrcweir 		nSize = xStream->readBytes( aData, sal_Int32(nCount) );
1479cdf0e10cSrcweir 	}
1480cdf0e10cSrcweir 	catch (IOException)
1481cdf0e10cSrcweir 	{
1482cdf0e10cSrcweir 		return ERRCODE_IO_CANTREAD;
1483cdf0e10cSrcweir 	}
1484cdf0e10cSrcweir 
1485cdf0e10cSrcweir 	rtl_copyMemory (pBuffer, aData.getConstArray(), nSize);
1486cdf0e10cSrcweir 	if (pRead)
1487cdf0e10cSrcweir 		*pRead = sal_uLong(nSize);
1488cdf0e10cSrcweir 
1489cdf0e10cSrcweir 	return ERRCODE_NONE;
1490cdf0e10cSrcweir }
1491cdf0e10cSrcweir 
1492cdf0e10cSrcweir //----------------------------------------------------------------------------
WriteAt(sal_uLong nPos,const void * pBuffer,sal_uLong nCount,sal_uLong * pWritten)1493cdf0e10cSrcweir ErrCode UcbLockBytes::WriteAt ( sal_uLong nPos, const void *pBuffer, sal_uLong nCount, sal_uLong *pWritten)
1494cdf0e10cSrcweir {
1495cdf0e10cSrcweir 	if ( pWritten )
1496cdf0e10cSrcweir 		*pWritten = 0;
1497cdf0e10cSrcweir 
1498cdf0e10cSrcweir     DBG_ASSERT( IsSynchronMode(), "Writing is only possible in SynchronMode!" );
1499cdf0e10cSrcweir     DBG_ASSERT( m_aInitialized.check(), "Writing bevor stream is ready!" );
1500cdf0e10cSrcweir 
1501cdf0e10cSrcweir     Reference <XSeekable> xSeekable = getSeekable_Impl();
1502cdf0e10cSrcweir     Reference <XOutputStream> xOutputStream = getOutputStream_Impl();
1503cdf0e10cSrcweir     if ( !xOutputStream.is() || !xSeekable.is() )
1504cdf0e10cSrcweir         return ERRCODE_IO_CANTWRITE;
1505cdf0e10cSrcweir 
1506cdf0e10cSrcweir 	try
1507cdf0e10cSrcweir 	{
1508cdf0e10cSrcweir         xSeekable->seek( nPos );
1509cdf0e10cSrcweir 	}
1510cdf0e10cSrcweir 	catch ( IOException )
1511cdf0e10cSrcweir 	{
1512cdf0e10cSrcweir 		return ERRCODE_IO_CANTSEEK;
1513cdf0e10cSrcweir 	}
1514cdf0e10cSrcweir 
1515cdf0e10cSrcweir     sal_Int8* pData = (sal_Int8*) pBuffer;
1516cdf0e10cSrcweir     Sequence<sal_Int8> aData( pData, nCount );
1517cdf0e10cSrcweir 	try
1518cdf0e10cSrcweir 	{
1519cdf0e10cSrcweir         xOutputStream->writeBytes( aData );
1520cdf0e10cSrcweir         if ( pWritten )
1521cdf0e10cSrcweir             *pWritten = nCount;
1522cdf0e10cSrcweir 	}
1523cdf0e10cSrcweir     catch ( Exception )
1524cdf0e10cSrcweir 	{
1525cdf0e10cSrcweir         return ERRCODE_IO_CANTWRITE;
1526cdf0e10cSrcweir 	}
1527cdf0e10cSrcweir 
1528cdf0e10cSrcweir 	return ERRCODE_NONE;
1529cdf0e10cSrcweir }
1530cdf0e10cSrcweir 
1531cdf0e10cSrcweir //----------------------------------------------------------------------------
Flush() const1532cdf0e10cSrcweir ErrCode UcbLockBytes::Flush() const
1533cdf0e10cSrcweir {
1534cdf0e10cSrcweir     Reference <XOutputStream > xOutputStream = getOutputStream_Impl();
1535cdf0e10cSrcweir     if ( !xOutputStream.is() )
1536cdf0e10cSrcweir         return ERRCODE_IO_CANTWRITE;
1537cdf0e10cSrcweir 
1538cdf0e10cSrcweir     try
1539cdf0e10cSrcweir     {
1540cdf0e10cSrcweir         xOutputStream->flush();
1541cdf0e10cSrcweir     }
1542cdf0e10cSrcweir     catch( Exception )
1543cdf0e10cSrcweir     {
1544cdf0e10cSrcweir         return ERRCODE_IO_CANTWRITE;
1545cdf0e10cSrcweir     }
1546cdf0e10cSrcweir 
1547cdf0e10cSrcweir     return ERRCODE_NONE;
1548cdf0e10cSrcweir }
1549cdf0e10cSrcweir 
1550cdf0e10cSrcweir //----------------------------------------------------------------------------
SetSize(sal_uLong nNewSize)1551cdf0e10cSrcweir ErrCode UcbLockBytes::SetSize (sal_uLong nNewSize)
1552cdf0e10cSrcweir {
1553cdf0e10cSrcweir     SvLockBytesStat aStat;
1554cdf0e10cSrcweir     Stat( &aStat, (SvLockBytesStatFlag) 0 );
1555cdf0e10cSrcweir     sal_uLong nSize = aStat.nSize;
1556cdf0e10cSrcweir 
1557cdf0e10cSrcweir     if ( nSize > nNewSize )
1558cdf0e10cSrcweir     {
1559cdf0e10cSrcweir         Reference < XTruncate > xTrunc( getOutputStream_Impl(), UNO_QUERY );
1560cdf0e10cSrcweir         if ( xTrunc.is() )
1561cdf0e10cSrcweir         {
1562cdf0e10cSrcweir             xTrunc->truncate();
1563cdf0e10cSrcweir             nSize = 0;
1564cdf0e10cSrcweir         }
1565cdf0e10cSrcweir         else {
1566cdf0e10cSrcweir             DBG_WARNING("Not truncatable!");
1567cdf0e10cSrcweir         }
1568cdf0e10cSrcweir     }
1569cdf0e10cSrcweir 
1570cdf0e10cSrcweir     if ( nSize < nNewSize )
1571cdf0e10cSrcweir     {
1572cdf0e10cSrcweir         sal_uLong nDiff = nNewSize-nSize, nCount=0;
1573cdf0e10cSrcweir         sal_uInt8* pBuffer = new sal_uInt8[ nDiff ];
1574cdf0e10cSrcweir         memset(pBuffer, 0, nDiff); // initialize for enhanced security
1575cdf0e10cSrcweir         WriteAt( nSize, pBuffer, nDiff, &nCount );
1576cdf0e10cSrcweir         delete[] pBuffer;
1577cdf0e10cSrcweir         if ( nCount != nDiff )
1578cdf0e10cSrcweir             return ERRCODE_IO_CANTWRITE;
1579cdf0e10cSrcweir     }
1580cdf0e10cSrcweir 
1581cdf0e10cSrcweir     return ERRCODE_NONE;
1582cdf0e10cSrcweir }
1583cdf0e10cSrcweir 
1584cdf0e10cSrcweir //----------------------------------------------------------------------------
Stat(SvLockBytesStat * pStat,SvLockBytesStatFlag) const1585cdf0e10cSrcweir ErrCode UcbLockBytes::Stat( SvLockBytesStat *pStat, SvLockBytesStatFlag) const
1586cdf0e10cSrcweir {
1587cdf0e10cSrcweir 	if ( IsSynchronMode() )
1588cdf0e10cSrcweir     {
1589cdf0e10cSrcweir         UcbLockBytes* pThis = const_cast < UcbLockBytes* >( this );
1590cdf0e10cSrcweir         pThis->m_aInitialized.wait();
1591cdf0e10cSrcweir     }
1592cdf0e10cSrcweir 
1593cdf0e10cSrcweir 	if (!pStat)
1594cdf0e10cSrcweir 		return ERRCODE_IO_INVALIDPARAMETER;
1595cdf0e10cSrcweir 
1596cdf0e10cSrcweir     Reference <XInputStream> xStream = getInputStream_Impl();
1597cdf0e10cSrcweir     Reference <XSeekable> xSeekable = getSeekable_Impl();
1598cdf0e10cSrcweir 
1599cdf0e10cSrcweir     if ( !xStream.is() )
1600cdf0e10cSrcweir     {
1601cdf0e10cSrcweir 		if ( m_bTerminated )
1602cdf0e10cSrcweir             return ERRCODE_IO_INVALIDACCESS;
1603cdf0e10cSrcweir 		else
1604cdf0e10cSrcweir 			return ERRCODE_IO_PENDING;
1605cdf0e10cSrcweir     }
1606cdf0e10cSrcweir     else if( !xSeekable.is() )
1607cdf0e10cSrcweir 		return ERRCODE_IO_CANTTELL;
1608cdf0e10cSrcweir 
1609cdf0e10cSrcweir 	try
1610cdf0e10cSrcweir 	{
1611cdf0e10cSrcweir         pStat->nSize = sal_uLong(xSeekable->getLength());
1612cdf0e10cSrcweir 	}
1613cdf0e10cSrcweir 	catch (IOException)
1614cdf0e10cSrcweir 	{
1615cdf0e10cSrcweir 		return ERRCODE_IO_CANTTELL;
1616cdf0e10cSrcweir 	}
1617cdf0e10cSrcweir 
1618cdf0e10cSrcweir     return ERRCODE_NONE;
1619cdf0e10cSrcweir }
1620cdf0e10cSrcweir 
1621cdf0e10cSrcweir //----------------------------------------------------------------------------
Cancel()1622cdf0e10cSrcweir void UcbLockBytes::Cancel()
1623cdf0e10cSrcweir {
1624cdf0e10cSrcweir 	// is alive only for compatibility reasons
1625cdf0e10cSrcweir 	OSL_ENSURE( m_bTerminated, "UcbLockBytes is not thread safe so it can be used only syncronously!\n" );
1626cdf0e10cSrcweir }
1627cdf0e10cSrcweir 
1628cdf0e10cSrcweir //----------------------------------------------------------------------------
IMPL_LINK(UcbLockBytes,DataAvailHdl,void *,EMPTYARG)1629cdf0e10cSrcweir IMPL_LINK( UcbLockBytes, DataAvailHdl, void*, EMPTYARG )
1630cdf0e10cSrcweir {
1631cdf0e10cSrcweir     if ( hasInputStream_Impl() && m_xHandler.Is() )
1632cdf0e10cSrcweir         m_xHandler->Handle( UcbLockBytesHandler::DATA_AVAILABLE, this );
1633cdf0e10cSrcweir 
1634cdf0e10cSrcweir 	return 0;
1635cdf0e10cSrcweir }
1636cdf0e10cSrcweir 
CreateInputLockBytes(const Reference<XInputStream> & xInputStream)1637cdf0e10cSrcweir UcbLockBytesRef UcbLockBytes::CreateInputLockBytes( const Reference< XInputStream >& xInputStream )
1638cdf0e10cSrcweir {
1639cdf0e10cSrcweir     if( !xInputStream.is() )
1640cdf0e10cSrcweir 		return NULL;;
1641cdf0e10cSrcweir 
1642cdf0e10cSrcweir     UcbLockBytesRef xLockBytes = new UcbLockBytes();
1643cdf0e10cSrcweir     xLockBytes->setDontClose_Impl();
1644cdf0e10cSrcweir     xLockBytes->setInputStream_Impl( xInputStream );
1645cdf0e10cSrcweir     xLockBytes->terminate_Impl();
1646cdf0e10cSrcweir     return xLockBytes;
1647cdf0e10cSrcweir }
1648cdf0e10cSrcweir 
CreateLockBytes(const Reference<XStream> & xStream)1649cdf0e10cSrcweir UcbLockBytesRef UcbLockBytes::CreateLockBytes( const Reference< XStream >& xStream )
1650cdf0e10cSrcweir {
1651cdf0e10cSrcweir     if( !xStream.is() )
1652cdf0e10cSrcweir 		return NULL;;
1653cdf0e10cSrcweir 
1654cdf0e10cSrcweir     UcbLockBytesRef xLockBytes = new UcbLockBytes();
1655cdf0e10cSrcweir     xLockBytes->setDontClose_Impl();
1656cdf0e10cSrcweir     xLockBytes->setStream_Impl( xStream );
1657cdf0e10cSrcweir     xLockBytes->terminate_Impl();
1658cdf0e10cSrcweir     return xLockBytes;
1659cdf0e10cSrcweir }
1660cdf0e10cSrcweir 
CreateLockBytes(const Reference<XContent> & xContent,const::rtl::OUString & rReferer,const::rtl::OUString & rMediaType,const Reference<XInputStream> & xPostData,const Reference<XInteractionHandler> & xInteractionHandler,UcbLockBytesHandler * pHandler)1661cdf0e10cSrcweir UcbLockBytesRef UcbLockBytes::CreateLockBytes( const Reference < XContent >& xContent, const ::rtl::OUString& rReferer, const ::rtl::OUString& rMediaType,
1662cdf0e10cSrcweir         const Reference < XInputStream >& xPostData, const Reference < XInteractionHandler >& xInteractionHandler, UcbLockBytesHandler* pHandler )
1663cdf0e10cSrcweir {
1664cdf0e10cSrcweir 	if( !xContent.is() )
1665cdf0e10cSrcweir 		return NULL;;
1666cdf0e10cSrcweir 
1667cdf0e10cSrcweir     UcbLockBytesRef xLockBytes = new UcbLockBytes( pHandler );
1668cdf0e10cSrcweir     xLockBytes->SetSynchronMode( !pHandler );
1669cdf0e10cSrcweir     Reference< XActiveDataControl > xSink = (XActiveDataControl*) new UcbDataSink_Impl( xLockBytes );
1670cdf0e10cSrcweir 
1671cdf0e10cSrcweir     PostCommandArgument2 aArgument;
1672cdf0e10cSrcweir 	aArgument.Source = xPostData;
1673cdf0e10cSrcweir     aArgument.Sink = xSink;
1674cdf0e10cSrcweir     aArgument.MediaType = rMediaType;
1675cdf0e10cSrcweir     aArgument.Referer = rReferer;
1676cdf0e10cSrcweir 
1677cdf0e10cSrcweir     Command aCommand;
1678cdf0e10cSrcweir     aCommand.Name = ::rtl::OUString::createFromAscii ("post");
1679cdf0e10cSrcweir     aCommand.Argument <<= aArgument;
1680cdf0e10cSrcweir 
1681cdf0e10cSrcweir     Reference< XProgressHandler > xProgressHdl = new ProgressHandler_Impl( LINK( &xLockBytes, UcbLockBytes, DataAvailHdl ) );
1682cdf0e10cSrcweir 
1683cdf0e10cSrcweir 	sal_Bool bError = UCBOpenContentSync( xLockBytes,
1684cdf0e10cSrcweir 										  xContent,
1685cdf0e10cSrcweir 										  aCommand,
1686cdf0e10cSrcweir 										  xSink,
1687cdf0e10cSrcweir 										  xInteractionHandler,
1688cdf0e10cSrcweir 										  xProgressHdl,
1689cdf0e10cSrcweir 										  pHandler );
1690cdf0e10cSrcweir 
1691cdf0e10cSrcweir    	if ( xLockBytes->GetError() == ERRCODE_NONE && ( bError || !xLockBytes->getInputStream().is() ) )
1692cdf0e10cSrcweir 	{
1693cdf0e10cSrcweir 		DBG_ERROR("No InputStream, but no error set!" );
1694cdf0e10cSrcweir        	xLockBytes->SetError( ERRCODE_IO_GENERAL );
1695cdf0e10cSrcweir 	}
1696cdf0e10cSrcweir 
1697cdf0e10cSrcweir     return xLockBytes;
1698cdf0e10cSrcweir }
1699cdf0e10cSrcweir 
CreateLockBytes(const Reference<XContent> & xContent,const Sequence<PropertyValue> & rProps,StreamMode eOpenMode,const Reference<XInteractionHandler> & xInteractionHandler,UcbLockBytesHandler * pHandler)1700cdf0e10cSrcweir UcbLockBytesRef UcbLockBytes::CreateLockBytes( const Reference < XContent >& xContent, const Sequence < PropertyValue >& rProps,
1701cdf0e10cSrcweir         StreamMode eOpenMode, const Reference < XInteractionHandler >& xInteractionHandler, UcbLockBytesHandler* pHandler )
1702cdf0e10cSrcweir {
1703cdf0e10cSrcweir 	if( !xContent.is() )
1704cdf0e10cSrcweir 		return NULL;;
1705cdf0e10cSrcweir 
1706cdf0e10cSrcweir     UcbLockBytesRef xLockBytes = new UcbLockBytes( pHandler );
1707cdf0e10cSrcweir     xLockBytes->SetSynchronMode( !pHandler );
1708cdf0e10cSrcweir     Reference< XActiveDataControl > xSink;
1709cdf0e10cSrcweir     if ( eOpenMode & STREAM_WRITE )
1710cdf0e10cSrcweir         xSink = (XActiveDataControl*) new UcbStreamer_Impl( xLockBytes );
1711cdf0e10cSrcweir     else
1712cdf0e10cSrcweir         xSink = (XActiveDataControl*) new UcbDataSink_Impl( xLockBytes );
1713cdf0e10cSrcweir 
1714cdf0e10cSrcweir     if ( rProps.getLength() )
1715cdf0e10cSrcweir     {
1716cdf0e10cSrcweir         Reference < XCommandProcessor > xProcessor( xContent, UNO_QUERY );
1717cdf0e10cSrcweir         Command aCommand;
1718cdf0e10cSrcweir         aCommand.Name     = ::rtl::OUString::createFromAscii("setPropertyValues");
1719cdf0e10cSrcweir         aCommand.Handle   = -1; /* unknown */
1720cdf0e10cSrcweir         aCommand.Argument <<= rProps;
1721cdf0e10cSrcweir         xProcessor->execute( aCommand, 0, Reference < XCommandEnvironment >() );
1722cdf0e10cSrcweir     }
1723cdf0e10cSrcweir 
1724cdf0e10cSrcweir 	OpenCommandArgument2 aArgument;
1725cdf0e10cSrcweir     aArgument.Sink = xSink;
1726cdf0e10cSrcweir 	aArgument.Mode = OpenMode::DOCUMENT;
1727cdf0e10cSrcweir 
1728cdf0e10cSrcweir     Command aCommand;
1729cdf0e10cSrcweir 	aCommand.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("open") );
1730cdf0e10cSrcweir 	aCommand.Argument <<= aArgument;
1731cdf0e10cSrcweir 
1732cdf0e10cSrcweir     Reference< XProgressHandler > xProgressHdl = new ProgressHandler_Impl( LINK( &xLockBytes, UcbLockBytes, DataAvailHdl ) );
1733cdf0e10cSrcweir 
1734cdf0e10cSrcweir     sal_Bool bError = UCBOpenContentSync( xLockBytes,
1735cdf0e10cSrcweir 										  xContent,
1736cdf0e10cSrcweir 										  aCommand,
1737cdf0e10cSrcweir 										  xSink,
1738cdf0e10cSrcweir 										  xInteractionHandler,
1739cdf0e10cSrcweir 										  xProgressHdl,
1740cdf0e10cSrcweir 										  pHandler );
1741cdf0e10cSrcweir 
1742cdf0e10cSrcweir     if ( xLockBytes->GetError() == ERRCODE_NONE && ( bError || !xLockBytes->getInputStream().is() ) )
1743cdf0e10cSrcweir 	{
1744cdf0e10cSrcweir 		DBG_ERROR("No InputStream, but no error set!" );
1745cdf0e10cSrcweir        	xLockBytes->SetError( ERRCODE_IO_GENERAL );
1746cdf0e10cSrcweir 	}
1747cdf0e10cSrcweir 
1748cdf0e10cSrcweir     return xLockBytes;
1749cdf0e10cSrcweir }
1750cdf0e10cSrcweir 
1751cdf0e10cSrcweir }
1752