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_ucbhelper.hxx"
26 #include <ucbhelper/interceptedinteraction.hxx>
27 
28 //_______________________________________________
29 // includes
30 
31 //_______________________________________________
32 // namespace
33 
34 namespace ucbhelper{
35 
36 namespace css = ::com::sun::star;
37 
38 //_______________________________________________
39 // definitions
40 
41 /*-----------------------------------------------
42     17.03.2004 11:00
43 -----------------------------------------------*/
InterceptedInteraction()44 InterceptedInteraction::InterceptedInteraction()
45 {
46 }
47 
48 /*-----------------------------------------------
49     17.03.2004 14:55
50 -----------------------------------------------*/
setInterceptedHandler(const css::uno::Reference<css::task::XInteractionHandler> & xInterceptedHandler)51 void InterceptedInteraction::setInterceptedHandler(const css::uno::Reference< css::task::XInteractionHandler >& xInterceptedHandler)
52 {
53     m_xInterceptedHandler = xInterceptedHandler;
54 }
55 
56 /*-----------------------------------------------
57     17.03.2004 14:55
58 -----------------------------------------------*/
setInterceptions(const::std::vector<InterceptedRequest> & lInterceptions)59 void InterceptedInteraction::setInterceptions(const ::std::vector< InterceptedRequest >& lInterceptions)
60 {
61     m_lInterceptions = lInterceptions;
62 }
63 
64 /*-----------------------------------------------
65     18.03.2004 10:10
66 -----------------------------------------------*/
intercepted(const InterceptedRequest &,const::com::sun::star::uno::Reference<::com::sun::star::task::XInteractionRequest> &)67 InterceptedInteraction::EInterceptionState InterceptedInteraction::intercepted(
68     const InterceptedRequest&,
69     const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >&)
70 {
71     // default behaviour! see impl_interceptRequest() for further informations ...
72     return E_NOT_INTERCEPTED;
73 }
74 
75 /*-----------------------------------------------
76     18.03.2004 09:46
77 -----------------------------------------------*/
extractContinuation(const css::uno::Sequence<css::uno::Reference<css::task::XInteractionContinuation>> & lContinuations,const css::uno::Type & aType)78 css::uno::Reference< css::task::XInteractionContinuation > InterceptedInteraction::extractContinuation(const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > >& lContinuations,
79                                                                                                        const css::uno::Type&                                                                   aType         )
80 {
81     const css::uno::Reference< css::task::XInteractionContinuation >* pContinuations = lContinuations.getConstArray();
82 
83     sal_Int32 c = lContinuations.getLength();
84     sal_Int32 i = 0;
85 
86     for (i=0; i<c; ++i)
87     {
88         css::uno::Reference< css::uno::XInterface > xCheck(pContinuations[i], css::uno::UNO_QUERY);
89         if (xCheck->queryInterface(aType).hasValue())
90             return pContinuations[i];
91     }
92 
93     return css::uno::Reference< css::task::XInteractionContinuation >();
94 }
95 
96 /*-----------------------------------------------
97     18.03.2004 10:03
98 -----------------------------------------------*/
handle(const css::uno::Reference<css::task::XInteractionRequest> & xRequest)99 void SAL_CALL InterceptedInteraction::handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
100     throw(css::uno::RuntimeException)
101 {
102     impl_handleDefault(xRequest);
103 }
104 
105 /*-----------------------------------------------
106     18.03.2004 10:02
107 -----------------------------------------------*/
impl_handleDefault(const::com::sun::star::uno::Reference<::com::sun::star::task::XInteractionRequest> & xRequest)108 void InterceptedInteraction::impl_handleDefault(const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >& xRequest)
109 {
110     EInterceptionState eState = impl_interceptRequest(xRequest);
111 
112     switch(eState)
113     {
114         case E_NOT_INTERCEPTED:
115         {
116             // Non of the intercepted requests match to the given one.
117             // => forward request to the internal wrapped handler - if there is one.
118             if (m_xInterceptedHandler.is())
119                 m_xInterceptedHandler->handle(xRequest);
120         }
121         break;
122 
123         case E_NO_CONTINUATION_FOUND:
124         {
125             // Runtime error! The defined continuation could not be located
126             // inside the set of available containuations of the incoming request.
127             // Whats wrong - the interception list or the request?
128             OSL_ENSURE(sal_False, "InterceptedInteraction::handle()\nCould intercept this interaction request - but cant locate the right continuation!");
129         }
130         break;
131 
132         case E_INTERCEPTED:
133         break;
134     }
135 }
136 
137 /*-----------------------------------------------
138     18.03.2004 09:48
139 -----------------------------------------------*/
impl_interceptRequest(const::com::sun::star::uno::Reference<::com::sun::star::task::XInteractionRequest> & xRequest)140 InterceptedInteraction::EInterceptionState InterceptedInteraction::impl_interceptRequest(const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >& xRequest)
141 {
142     css::uno::Any                                                                    aRequest       = xRequest->getRequest();
143     css::uno::Type                                                                   aRequestType   = aRequest.getValueType();
144     css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
145 
146     // check against the list of static requests
147     sal_Int32 nHandle = 0;
148     ::std::vector< InterceptedRequest >::const_iterator pIt;
149     for (  pIt  = m_lInterceptions.begin();
150            pIt != m_lInterceptions.end()  ;
151          ++pIt                            )
152     {
153         const InterceptedRequest& rInterception = *pIt;
154         css::uno::Type aInterceptedType = rInterception.Request.getValueType();
155 
156         // check the request
157         sal_Bool bMatch = sal_False;
158         if (rInterception.MatchExact)
159             bMatch = aInterceptedType.equals(aRequestType);
160         else
161             bMatch = aInterceptedType.isAssignableFrom(aRequestType); // dont change intercepted and request type here -> it will check the wrong direction!
162 
163         // intercepted ...
164         // Call they might existing derived class, so they can handle that by its own.
165         // If its not interested on that (may be its not overwritten and the default implementation
166         // returns E_NOT_INTERCEPTED as default) -> break this loop and search for the right continuation.
167         if (bMatch)
168         {
169             EInterceptionState eState = intercepted(rInterception, xRequest);
170             if (eState == E_NOT_INTERCEPTED)
171                 break;
172             return eState;
173         }
174 
175         ++nHandle;
176     }
177 
178     if (pIt != m_lInterceptions.end()) // => can be true only if bMatch=TRUE!
179     {
180         // match -> search required continuation
181         const InterceptedRequest& rInterception = *pIt;
182         css::uno::Reference< css::task::XInteractionContinuation > xContinuation = InterceptedInteraction::extractContinuation(lContinuations, rInterception.Continuation);
183         if (xContinuation.is())
184         {
185             xContinuation->select();
186             return E_INTERCEPTED;
187         }
188 
189         // Can be reached only, if the request does not support the given continuation!
190         // => RuntimeError!?
191         return E_NO_CONTINUATION_FOUND;
192     }
193 
194     return E_NOT_INTERCEPTED;
195 }
196 
197 } // namespace ucbhelper
198