1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski 
23*b1cdbd2cSJim Jagielski 
24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove
25*b1cdbd2cSJim Jagielski #include "precompiled_comphelper.hxx"
26*b1cdbd2cSJim Jagielski #include <comphelper/stillreadwriteinteraction.hxx>
27*b1cdbd2cSJim Jagielski 
28*b1cdbd2cSJim Jagielski #ifndef __COM_SUN_STAR_UCB_INTERACTIVEIOEXCEPTION_HPP__
29*b1cdbd2cSJim Jagielski #include <com/sun/star/ucb/InteractiveIOException.hpp>
30*b1cdbd2cSJim Jagielski #endif
31*b1cdbd2cSJim Jagielski 
32*b1cdbd2cSJim Jagielski #ifndef __COM_SUN_STAR_TASK_XINTERACTIONABORT_HPP__
33*b1cdbd2cSJim Jagielski #include <com/sun/star/task/XInteractionAbort.hpp>
34*b1cdbd2cSJim Jagielski #endif
35*b1cdbd2cSJim Jagielski 
36*b1cdbd2cSJim Jagielski #ifndef __COM_SUN_STAR_UCB_UNSUPPORTEDDATASINKEXCEPTION_HPP__
37*b1cdbd2cSJim Jagielski #include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
38*b1cdbd2cSJim Jagielski #endif
39*b1cdbd2cSJim Jagielski 
40*b1cdbd2cSJim Jagielski #include <com/sun/star/ucb/AuthenticationRequest.hpp>
41*b1cdbd2cSJim Jagielski 
42*b1cdbd2cSJim Jagielski namespace comphelper{
43*b1cdbd2cSJim Jagielski 
44*b1cdbd2cSJim Jagielski 	namespace css = ::com::sun::star;
45*b1cdbd2cSJim Jagielski 
StillReadWriteInteraction(const css::uno::Reference<css::task::XInteractionHandler> & xHandler,const css::uno::Reference<css::task::XInteractionHandler> & xAuthenticationHandler)46*b1cdbd2cSJim Jagielski     StillReadWriteInteraction::StillReadWriteInteraction(const css::uno::Reference< css::task::XInteractionHandler >& xHandler,
47*b1cdbd2cSJim Jagielski                                                          const css::uno::Reference< css::task::XInteractionHandler >& xAuthenticationHandler)
48*b1cdbd2cSJim Jagielski  			: m_bUsed                    (sal_False)
49*b1cdbd2cSJim Jagielski  			, m_bHandledByMySelf         (sal_False)
50*b1cdbd2cSJim Jagielski  			, m_bHandledByInternalHandler(sal_False)
51*b1cdbd2cSJim Jagielski  			, m_xAuthenticationHandler(xAuthenticationHandler)
52*b1cdbd2cSJim Jagielski {
53*b1cdbd2cSJim Jagielski 	::std::vector< ::ucbhelper::InterceptedInteraction::InterceptedRequest > lInterceptions;
54*b1cdbd2cSJim Jagielski 	::ucbhelper::InterceptedInteraction::InterceptedRequest                  aInterceptedRequest;
55*b1cdbd2cSJim Jagielski 
56*b1cdbd2cSJim Jagielski 	aInterceptedRequest.Handle = HANDLE_INTERACTIVEIOEXCEPTION;
57*b1cdbd2cSJim Jagielski 	aInterceptedRequest.Request <<= css::ucb::InteractiveIOException();
58*b1cdbd2cSJim Jagielski 	aInterceptedRequest.Continuation = ::getCppuType(static_cast< css::uno::Reference< css::task::XInteractionAbort >* >(0));
59*b1cdbd2cSJim Jagielski 	aInterceptedRequest.MatchExact = sal_False;
60*b1cdbd2cSJim Jagielski 	lInterceptions.push_back(aInterceptedRequest);
61*b1cdbd2cSJim Jagielski 
62*b1cdbd2cSJim Jagielski 	aInterceptedRequest.Handle = HANDLE_UNSUPPORTEDDATASINKEXCEPTION;
63*b1cdbd2cSJim Jagielski 	aInterceptedRequest.Request <<= css::ucb::UnsupportedDataSinkException();
64*b1cdbd2cSJim Jagielski 	aInterceptedRequest.Continuation = ::getCppuType(static_cast< css::uno::Reference< css::task::XInteractionAbort >* >(0));
65*b1cdbd2cSJim Jagielski 	aInterceptedRequest.MatchExact = sal_False;
66*b1cdbd2cSJim Jagielski 	lInterceptions.push_back(aInterceptedRequest);
67*b1cdbd2cSJim Jagielski 
68*b1cdbd2cSJim Jagielski 	aInterceptedRequest.Handle = HANDLE_AUTHENTICATIONREQUESTEXCEPTION;
69*b1cdbd2cSJim Jagielski 	aInterceptedRequest.Request <<= css::ucb::AuthenticationRequest();
70*b1cdbd2cSJim Jagielski 	aInterceptedRequest.Continuation = ::getCppuType(static_cast< css::uno::Reference< css::task::XInteractionAbort >* >(0));
71*b1cdbd2cSJim Jagielski 	aInterceptedRequest.MatchExact = sal_False;
72*b1cdbd2cSJim Jagielski 	lInterceptions.push_back(aInterceptedRequest);
73*b1cdbd2cSJim Jagielski 
74*b1cdbd2cSJim Jagielski 	setInterceptedHandler(xHandler);
75*b1cdbd2cSJim Jagielski 	setInterceptions(lInterceptions);
76*b1cdbd2cSJim Jagielski }
77*b1cdbd2cSJim Jagielski 
resetInterceptions()78*b1cdbd2cSJim Jagielski void StillReadWriteInteraction::resetInterceptions()
79*b1cdbd2cSJim Jagielski {
80*b1cdbd2cSJim Jagielski 	setInterceptions(::std::vector< ::ucbhelper::InterceptedInteraction::InterceptedRequest >());
81*b1cdbd2cSJim Jagielski }
82*b1cdbd2cSJim Jagielski 
resetErrorStates()83*b1cdbd2cSJim Jagielski void StillReadWriteInteraction::resetErrorStates()
84*b1cdbd2cSJim Jagielski {
85*b1cdbd2cSJim Jagielski 	m_bUsed                     = sal_False;
86*b1cdbd2cSJim Jagielski 	m_bHandledByMySelf          = sal_False;
87*b1cdbd2cSJim Jagielski 	m_bHandledByInternalHandler = sal_False;
88*b1cdbd2cSJim Jagielski }
89*b1cdbd2cSJim Jagielski 
wasWriteError()90*b1cdbd2cSJim Jagielski sal_Bool StillReadWriteInteraction::wasWriteError()
91*b1cdbd2cSJim Jagielski {
92*b1cdbd2cSJim Jagielski 	return (m_bUsed && m_bHandledByMySelf);
93*b1cdbd2cSJim Jagielski }
94*b1cdbd2cSJim Jagielski 
intercepted(const::ucbhelper::InterceptedInteraction::InterceptedRequest & aRequest,const::com::sun::star::uno::Reference<::com::sun::star::task::XInteractionRequest> & xRequest)95*b1cdbd2cSJim Jagielski ucbhelper::InterceptedInteraction::EInterceptionState StillReadWriteInteraction::intercepted(const ::ucbhelper::InterceptedInteraction::InterceptedRequest&                         aRequest,
96*b1cdbd2cSJim Jagielski 																  const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >& xRequest)
97*b1cdbd2cSJim Jagielski {
98*b1cdbd2cSJim Jagielski 	// we are used!
99*b1cdbd2cSJim Jagielski 	m_bUsed = sal_True;
100*b1cdbd2cSJim Jagielski 
101*b1cdbd2cSJim Jagielski 	// check if its a real interception - might some parameters are not the right ones ...
102*b1cdbd2cSJim Jagielski 	sal_Bool bAbort = sal_False;
103*b1cdbd2cSJim Jagielski 	switch(aRequest.Handle)
104*b1cdbd2cSJim Jagielski 	{
105*b1cdbd2cSJim Jagielski 	case HANDLE_INTERACTIVEIOEXCEPTION:
106*b1cdbd2cSJim Jagielski 		{
107*b1cdbd2cSJim Jagielski 			css::ucb::InteractiveIOException exIO;
108*b1cdbd2cSJim Jagielski 			xRequest->getRequest() >>= exIO;
109*b1cdbd2cSJim Jagielski 			bAbort = (
110*b1cdbd2cSJim Jagielski 				(exIO.Code == css::ucb::IOErrorCode_ACCESS_DENIED     )
111*b1cdbd2cSJim Jagielski 				|| (exIO.Code == css::ucb::IOErrorCode_LOCKING_VIOLATION )
112*b1cdbd2cSJim Jagielski 				|| (exIO.Code == css::ucb::IOErrorCode_NOT_EXISTING )
113*b1cdbd2cSJim Jagielski #ifdef MACOSX
114*b1cdbd2cSJim Jagielski 				// this is a workaround for MAC, on this platform if the file is locked
115*b1cdbd2cSJim Jagielski 				// the returned error code looks to be wrong
116*b1cdbd2cSJim Jagielski 				|| (exIO.Code == css::ucb::IOErrorCode_GENERAL )
117*b1cdbd2cSJim Jagielski #endif
118*b1cdbd2cSJim Jagielski 				);
119*b1cdbd2cSJim Jagielski 		}
120*b1cdbd2cSJim Jagielski 		break;
121*b1cdbd2cSJim Jagielski 
122*b1cdbd2cSJim Jagielski 	case HANDLE_UNSUPPORTEDDATASINKEXCEPTION:
123*b1cdbd2cSJim Jagielski 		{
124*b1cdbd2cSJim Jagielski 			bAbort = sal_True;
125*b1cdbd2cSJim Jagielski 		}
126*b1cdbd2cSJim Jagielski 		break;
127*b1cdbd2cSJim Jagielski     case HANDLE_AUTHENTICATIONREQUESTEXCEPTION:
128*b1cdbd2cSJim Jagielski 		{
129*b1cdbd2cSJim Jagielski //use internal authentication dedicated handler and return
130*b1cdbd2cSJim Jagielski             if (m_xAuthenticationHandler.is())
131*b1cdbd2cSJim Jagielski             {
132*b1cdbd2cSJim Jagielski                 m_xAuthenticationHandler->handle(xRequest);
133*b1cdbd2cSJim Jagielski                 return ::ucbhelper::InterceptedInteraction::E_INTERCEPTED;
134*b1cdbd2cSJim Jagielski             }
135*b1cdbd2cSJim Jagielski             else //simply abort
136*b1cdbd2cSJim Jagielski                 bAbort = sal_True;;
137*b1cdbd2cSJim Jagielski 		}
138*b1cdbd2cSJim Jagielski 		break;
139*b1cdbd2cSJim Jagielski 	}
140*b1cdbd2cSJim Jagielski 
141*b1cdbd2cSJim Jagielski 	// handle interaction by ourself
142*b1cdbd2cSJim Jagielski 	if (bAbort)
143*b1cdbd2cSJim Jagielski 	{
144*b1cdbd2cSJim Jagielski 		m_bHandledByMySelf = sal_True;
145*b1cdbd2cSJim Jagielski 		css::uno::Reference< css::task::XInteractionContinuation > xAbort = ::ucbhelper::InterceptedInteraction::extractContinuation(
146*b1cdbd2cSJim Jagielski 			xRequest->getContinuations(),
147*b1cdbd2cSJim Jagielski 			::getCppuType(static_cast< css::uno::Reference< css::task::XInteractionAbort >* >(0)));
148*b1cdbd2cSJim Jagielski 		if (!xAbort.is())
149*b1cdbd2cSJim Jagielski 			return ::ucbhelper::InterceptedInteraction::E_NO_CONTINUATION_FOUND;
150*b1cdbd2cSJim Jagielski 		xAbort->select();
151*b1cdbd2cSJim Jagielski 		return ::ucbhelper::InterceptedInteraction::E_INTERCEPTED;
152*b1cdbd2cSJim Jagielski 	}
153*b1cdbd2cSJim Jagielski 
154*b1cdbd2cSJim Jagielski 	// Otherwhise use internal handler.
155*b1cdbd2cSJim Jagielski 	if (m_xInterceptedHandler.is())
156*b1cdbd2cSJim Jagielski 	{
157*b1cdbd2cSJim Jagielski 		m_bHandledByInternalHandler = sal_True;
158*b1cdbd2cSJim Jagielski 		m_xInterceptedHandler->handle(xRequest);
159*b1cdbd2cSJim Jagielski 	}
160*b1cdbd2cSJim Jagielski 	return ::ucbhelper::InterceptedInteraction::E_INTERCEPTED;
161*b1cdbd2cSJim Jagielski }
162*b1cdbd2cSJim Jagielski }
163