xref: /trunk/main/comphelper/source/misc/stillreadwriteinteraction.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2 *
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
6 *
7 * OpenOffice.org - a multi-platform office productivity suite
8 *
9 * This file is part of OpenOffice.org.
10 *
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org.  If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
25 *
26 ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/stillreadwriteinteraction.hxx>
31 
32 #ifndef __COM_SUN_STAR_UCB_INTERACTIVEIOEXCEPTION_HPP__
33 #include <com/sun/star/ucb/InteractiveIOException.hpp>
34 #endif
35 
36 #ifndef __COM_SUN_STAR_TASK_XINTERACTIONABORT_HPP__
37 #include <com/sun/star/task/XInteractionAbort.hpp>
38 #endif
39 
40 #ifndef __COM_SUN_STAR_UCB_UNSUPPORTEDDATASINKEXCEPTION_HPP__
41 #include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
42 #endif
43 
44 namespace comphelper{
45 
46     namespace css = ::com::sun::star;
47 
48 StillReadWriteInteraction::StillReadWriteInteraction(const css::uno::Reference< css::task::XInteractionHandler >& xHandler)
49             : m_bUsed                    (sal_False)
50             , m_bHandledByMySelf         (sal_False)
51             , m_bHandledByInternalHandler(sal_False)
52 {
53     ::std::vector< ::ucbhelper::InterceptedInteraction::InterceptedRequest > lInterceptions;
54     ::ucbhelper::InterceptedInteraction::InterceptedRequest                  aInterceptedRequest;
55 
56     aInterceptedRequest.Handle = HANDLE_INTERACTIVEIOEXCEPTION;
57     aInterceptedRequest.Request <<= css::ucb::InteractiveIOException();
58     aInterceptedRequest.Continuation = ::getCppuType(static_cast< css::uno::Reference< css::task::XInteractionAbort >* >(0));
59     aInterceptedRequest.MatchExact = sal_False;
60     lInterceptions.push_back(aInterceptedRequest);
61 
62     aInterceptedRequest.Handle = HANDLE_UNSUPPORTEDDATASINKEXCEPTION;
63     aInterceptedRequest.Request <<= css::ucb::UnsupportedDataSinkException();
64     aInterceptedRequest.Continuation = ::getCppuType(static_cast< css::uno::Reference< css::task::XInteractionAbort >* >(0));
65     aInterceptedRequest.MatchExact = sal_False;
66     lInterceptions.push_back(aInterceptedRequest);
67 
68     setInterceptedHandler(xHandler);
69     setInterceptions(lInterceptions);
70 }
71 
72 void StillReadWriteInteraction::resetInterceptions()
73 {
74     setInterceptions(::std::vector< ::ucbhelper::InterceptedInteraction::InterceptedRequest >());
75 }
76 
77 void StillReadWriteInteraction::resetErrorStates()
78 {
79     m_bUsed                     = sal_False;
80     m_bHandledByMySelf          = sal_False;
81     m_bHandledByInternalHandler = sal_False;
82 }
83 
84 sal_Bool StillReadWriteInteraction::wasWriteError()
85 {
86     return (m_bUsed && m_bHandledByMySelf);
87 }
88 
89 ucbhelper::InterceptedInteraction::EInterceptionState StillReadWriteInteraction::intercepted(const ::ucbhelper::InterceptedInteraction::InterceptedRequest&                         aRequest,
90                                                                   const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >& xRequest)
91 {
92     // we are used!
93     m_bUsed = sal_True;
94 
95     // check if its a real interception - might some parameters are not the right ones ...
96     sal_Bool bAbort = sal_False;
97     switch(aRequest.Handle)
98     {
99     case HANDLE_INTERACTIVEIOEXCEPTION:
100         {
101             css::ucb::InteractiveIOException exIO;
102             xRequest->getRequest() >>= exIO;
103             bAbort = (
104                 (exIO.Code == css::ucb::IOErrorCode_ACCESS_DENIED     )
105                 || (exIO.Code == css::ucb::IOErrorCode_LOCKING_VIOLATION )
106                 || (exIO.Code == css::ucb::IOErrorCode_NOT_EXISTING )
107 #ifdef MACOSX
108                 // this is a workaround for MAC, on this platform if the file is locked
109                 // the returned error code looks to be wrong
110                 || (exIO.Code == css::ucb::IOErrorCode_GENERAL )
111 #endif
112                 );
113         }
114         break;
115 
116     case HANDLE_UNSUPPORTEDDATASINKEXCEPTION:
117         {
118             bAbort = sal_True;
119         }
120         break;
121     }
122 
123     // handle interaction by ourself
124     if (bAbort)
125     {
126         m_bHandledByMySelf = sal_True;
127         css::uno::Reference< css::task::XInteractionContinuation > xAbort = ::ucbhelper::InterceptedInteraction::extractContinuation(
128             xRequest->getContinuations(),
129             ::getCppuType(static_cast< css::uno::Reference< css::task::XInteractionAbort >* >(0)));
130         if (!xAbort.is())
131             return ::ucbhelper::InterceptedInteraction::E_NO_CONTINUATION_FOUND;
132         xAbort->select();
133         return ::ucbhelper::InterceptedInteraction::E_INTERCEPTED;
134     }
135 
136     // Otherwhise use internal handler.
137     if (m_xInterceptedHandler.is())
138     {
139         m_bHandledByInternalHandler = sal_True;
140         m_xInterceptedHandler->handle(xRequest);
141     }
142     return ::ucbhelper::InterceptedInteraction::E_INTERCEPTED;
143 }
144 }
145