1*ac9096f4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ac9096f4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ac9096f4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ac9096f4SAndrew Rist  * distributed with this work for additional information
6*ac9096f4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ac9096f4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ac9096f4SAndrew Rist  * "License"); you may not use this file except in compliance
9*ac9096f4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ac9096f4SAndrew Rist  *
11*ac9096f4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ac9096f4SAndrew Rist  *
13*ac9096f4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ac9096f4SAndrew Rist  * software distributed under the License is distributed on an
15*ac9096f4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ac9096f4SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ac9096f4SAndrew Rist  * specific language governing permissions and limitations
18*ac9096f4SAndrew Rist  * under the License.
19*ac9096f4SAndrew Rist  *
20*ac9096f4SAndrew Rist  *************************************************************/
21*ac9096f4SAndrew Rist 
22*ac9096f4SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucbhelper.hxx"
26cdf0e10cSrcweir #include <ucbhelper/interceptedinteraction.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir //_______________________________________________
29cdf0e10cSrcweir // includes
30cdf0e10cSrcweir 
31cdf0e10cSrcweir //_______________________________________________
32cdf0e10cSrcweir // namespace
33cdf0e10cSrcweir 
34cdf0e10cSrcweir namespace ucbhelper{
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace css = ::com::sun::star;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir //_______________________________________________
39cdf0e10cSrcweir // definitions
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /*-----------------------------------------------
42cdf0e10cSrcweir     17.03.2004 11:00
43cdf0e10cSrcweir -----------------------------------------------*/
44cdf0e10cSrcweir InterceptedInteraction::InterceptedInteraction()
45cdf0e10cSrcweir {
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir /*-----------------------------------------------
49cdf0e10cSrcweir     17.03.2004 14:55
50cdf0e10cSrcweir -----------------------------------------------*/
51cdf0e10cSrcweir void InterceptedInteraction::setInterceptedHandler(const css::uno::Reference< css::task::XInteractionHandler >& xInterceptedHandler)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     m_xInterceptedHandler = xInterceptedHandler;
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir /*-----------------------------------------------
57cdf0e10cSrcweir     17.03.2004 14:55
58cdf0e10cSrcweir -----------------------------------------------*/
59cdf0e10cSrcweir void InterceptedInteraction::setInterceptions(const ::std::vector< InterceptedRequest >& lInterceptions)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     m_lInterceptions = lInterceptions;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir /*-----------------------------------------------
65cdf0e10cSrcweir     18.03.2004 10:10
66cdf0e10cSrcweir -----------------------------------------------*/
67cdf0e10cSrcweir InterceptedInteraction::EInterceptionState InterceptedInteraction::intercepted(
68cdf0e10cSrcweir     const InterceptedRequest&,
69cdf0e10cSrcweir     const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >&)
70cdf0e10cSrcweir {
71cdf0e10cSrcweir     // default behaviour! see impl_interceptRequest() for further informations ...
72cdf0e10cSrcweir     return E_NOT_INTERCEPTED;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir /*-----------------------------------------------
76cdf0e10cSrcweir     18.03.2004 09:46
77cdf0e10cSrcweir -----------------------------------------------*/
78cdf0e10cSrcweir css::uno::Reference< css::task::XInteractionContinuation > InterceptedInteraction::extractContinuation(const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > >& lContinuations,
79cdf0e10cSrcweir                                                                                                        const css::uno::Type&                                                                   aType         )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     const css::uno::Reference< css::task::XInteractionContinuation >* pContinuations = lContinuations.getConstArray();
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     sal_Int32 c = lContinuations.getLength();
84cdf0e10cSrcweir     sal_Int32 i = 0;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     for (i=0; i<c; ++i)
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         css::uno::Reference< css::uno::XInterface > xCheck(pContinuations[i], css::uno::UNO_QUERY);
89cdf0e10cSrcweir         if (xCheck->queryInterface(aType).hasValue())
90cdf0e10cSrcweir             return pContinuations[i];
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     return css::uno::Reference< css::task::XInteractionContinuation >();
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir /*-----------------------------------------------
97cdf0e10cSrcweir     18.03.2004 10:03
98cdf0e10cSrcweir -----------------------------------------------*/
99cdf0e10cSrcweir void SAL_CALL InterceptedInteraction::handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
100cdf0e10cSrcweir     throw(css::uno::RuntimeException)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir     impl_handleDefault(xRequest);
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir /*-----------------------------------------------
106cdf0e10cSrcweir     18.03.2004 10:02
107cdf0e10cSrcweir -----------------------------------------------*/
108cdf0e10cSrcweir void InterceptedInteraction::impl_handleDefault(const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >& xRequest)
109cdf0e10cSrcweir {
110cdf0e10cSrcweir     EInterceptionState eState = impl_interceptRequest(xRequest);
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     switch(eState)
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         case E_NOT_INTERCEPTED:
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             // Non of the intercepted requests match to the given one.
117cdf0e10cSrcweir             // => forward request to the internal wrapped handler - if there is one.
118cdf0e10cSrcweir             if (m_xInterceptedHandler.is())
119cdf0e10cSrcweir                 m_xInterceptedHandler->handle(xRequest);
120cdf0e10cSrcweir         }
121cdf0e10cSrcweir         break;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         case E_NO_CONTINUATION_FOUND:
124cdf0e10cSrcweir         {
125cdf0e10cSrcweir             // Runtime error! The defined continuation could not be located
126cdf0e10cSrcweir             // inside the set of available containuations of the incoming request.
127cdf0e10cSrcweir             // Whats wrong - the interception list or the request?
128cdf0e10cSrcweir             OSL_ENSURE(sal_False, "InterceptedInteraction::handle()\nCould intercept this interaction request - but cant locate the right continuation!");
129cdf0e10cSrcweir         }
130cdf0e10cSrcweir         break;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         case E_INTERCEPTED:
133cdf0e10cSrcweir         break;
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir /*-----------------------------------------------
138cdf0e10cSrcweir     18.03.2004 09:48
139cdf0e10cSrcweir -----------------------------------------------*/
140cdf0e10cSrcweir InterceptedInteraction::EInterceptionState InterceptedInteraction::impl_interceptRequest(const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >& xRequest)
141cdf0e10cSrcweir {
142cdf0e10cSrcweir     css::uno::Any                                                                    aRequest       = xRequest->getRequest();
143cdf0e10cSrcweir     css::uno::Type                                                                   aRequestType   = aRequest.getValueType();
144cdf0e10cSrcweir     css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     // check against the list of static requests
147cdf0e10cSrcweir     sal_Int32 nHandle = 0;
148cdf0e10cSrcweir     ::std::vector< InterceptedRequest >::const_iterator pIt;
149cdf0e10cSrcweir     for (  pIt  = m_lInterceptions.begin();
150cdf0e10cSrcweir            pIt != m_lInterceptions.end()  ;
151cdf0e10cSrcweir          ++pIt                            )
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         const InterceptedRequest& rInterception = *pIt;
154cdf0e10cSrcweir         css::uno::Type aInterceptedType = rInterception.Request.getValueType();
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         // check the request
157cdf0e10cSrcweir         sal_Bool bMatch = sal_False;
158cdf0e10cSrcweir         if (rInterception.MatchExact)
159cdf0e10cSrcweir             bMatch = aInterceptedType.equals(aRequestType);
160cdf0e10cSrcweir         else
161cdf0e10cSrcweir             bMatch = aInterceptedType.isAssignableFrom(aRequestType); // dont change intercepted and request type here -> it will check the wrong direction!
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         // intercepted ...
164cdf0e10cSrcweir         // Call they might existing derived class, so they can handle that by its own.
165cdf0e10cSrcweir         // If its not interested on that (may be its not overwritten and the default implementation
166cdf0e10cSrcweir         // returns E_NOT_INTERCEPTED as default) -> break this loop and search for the right continuation.
167cdf0e10cSrcweir         if (bMatch)
168cdf0e10cSrcweir         {
169cdf0e10cSrcweir             EInterceptionState eState = intercepted(rInterception, xRequest);
170cdf0e10cSrcweir             if (eState == E_NOT_INTERCEPTED)
171cdf0e10cSrcweir                 break;
172cdf0e10cSrcweir             return eState;
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         ++nHandle;
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     if (pIt != m_lInterceptions.end()) // => can be true only if bMatch=TRUE!
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         // match -> search required continuation
181cdf0e10cSrcweir         const InterceptedRequest& rInterception = *pIt;
182cdf0e10cSrcweir         css::uno::Reference< css::task::XInteractionContinuation > xContinuation = InterceptedInteraction::extractContinuation(lContinuations, rInterception.Continuation);
183cdf0e10cSrcweir         if (xContinuation.is())
184cdf0e10cSrcweir         {
185cdf0e10cSrcweir             xContinuation->select();
186cdf0e10cSrcweir             return E_INTERCEPTED;
187cdf0e10cSrcweir         }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         // Can be reached only, if the request does not support the given continuation!
190cdf0e10cSrcweir         // => RuntimeError!?
191cdf0e10cSrcweir         return E_NO_CONTINUATION_FOUND;
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     return E_NOT_INTERCEPTED;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir } // namespace ucbhelper
198