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