Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <ucbhelper/interceptedinteraction.hxx>
21 :
22 :
23 : namespace ucbhelper{
24 :
25 0 : InterceptedInteraction::InterceptedInteraction()
26 : {
27 0 : }
28 :
29 0 : void InterceptedInteraction::setInterceptedHandler(const css::uno::Reference< css::task::XInteractionHandler >& xInterceptedHandler)
30 : {
31 0 : m_xInterceptedHandler = xInterceptedHandler;
32 0 : }
33 :
34 0 : void InterceptedInteraction::setInterceptions(const ::std::vector< InterceptedRequest >& lInterceptions)
35 : {
36 0 : m_lInterceptions = lInterceptions;
37 0 : }
38 :
39 0 : InterceptedInteraction::EInterceptionState InterceptedInteraction::intercepted(
40 : const InterceptedRequest&,
41 : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >&)
42 : {
43 : // default behaviour! see impl_interceptRequest() for further information ...
44 0 : return E_NOT_INTERCEPTED;
45 : }
46 :
47 0 : css::uno::Reference< css::task::XInteractionContinuation > InterceptedInteraction::extractContinuation(const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > >& lContinuations,
48 : const css::uno::Type& aType )
49 : {
50 0 : const css::uno::Reference< css::task::XInteractionContinuation >* pContinuations = lContinuations.getConstArray();
51 :
52 0 : sal_Int32 c = lContinuations.getLength();
53 0 : sal_Int32 i = 0;
54 :
55 0 : for (i=0; i<c; ++i)
56 : {
57 0 : css::uno::Reference< css::uno::XInterface > xCheck(pContinuations[i], css::uno::UNO_QUERY);
58 0 : if (xCheck->queryInterface(aType).hasValue())
59 0 : return pContinuations[i];
60 0 : }
61 :
62 0 : return css::uno::Reference< css::task::XInteractionContinuation >();
63 : }
64 :
65 0 : void SAL_CALL InterceptedInteraction::handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
66 : throw(css::uno::RuntimeException, std::exception)
67 : {
68 0 : impl_handleDefault(xRequest);
69 0 : }
70 :
71 0 : void InterceptedInteraction::impl_handleDefault(const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >& xRequest)
72 : {
73 0 : EInterceptionState eState = impl_interceptRequest(xRequest);
74 :
75 0 : switch(eState)
76 : {
77 : case E_NOT_INTERCEPTED:
78 : {
79 : // Non of the intercepted requests match to the given one.
80 : // => forward request to the internal wrapped handler - if there is one.
81 0 : if (m_xInterceptedHandler.is())
82 0 : m_xInterceptedHandler->handle(xRequest);
83 : }
84 0 : break;
85 :
86 : case E_NO_CONTINUATION_FOUND:
87 : {
88 : // Runtime error! The defined continuation could not be located
89 : // inside the set of available containuations of the incoming request.
90 : // Whats wrong - the interception list or the request?
91 : OSL_FAIL("InterceptedInteraction::handle()\nCould intercept this interaction request - but cant locate the right continuation!");
92 : }
93 0 : break;
94 :
95 : case E_INTERCEPTED:
96 0 : break;
97 : }
98 0 : }
99 :
100 0 : InterceptedInteraction::EInterceptionState InterceptedInteraction::impl_interceptRequest(const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >& xRequest)
101 : {
102 0 : css::uno::Any aRequest = xRequest->getRequest();
103 0 : css::uno::Type aRequestType = aRequest.getValueType();
104 0 : css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
105 :
106 : // check against the list of static requests
107 0 : sal_Int32 nHandle = 0;
108 0 : ::std::vector< InterceptedRequest >::const_iterator pIt;
109 0 : for ( pIt = m_lInterceptions.begin();
110 0 : pIt != m_lInterceptions.end() ;
111 : ++pIt )
112 : {
113 0 : const InterceptedRequest& rInterception = *pIt;
114 0 : css::uno::Type aInterceptedType = rInterception.Request.getValueType();
115 :
116 : // check the request
117 0 : bool bMatch = false;
118 0 : if (rInterception.MatchExact)
119 0 : bMatch = aInterceptedType.equals(aRequestType);
120 : else
121 0 : bMatch = aInterceptedType.isAssignableFrom(aRequestType); // dont change intercepted and request type here -> it will check the wrong direction!
122 :
123 : // intercepted ...
124 : // Call they might existing derived class, so they can handle that by its own.
125 : // If its not interested on that (may be its not overwritten and the default implementation
126 : // returns E_NOT_INTERCEPTED as default) -> break this loop and search for the right continuation.
127 0 : if (bMatch)
128 : {
129 0 : EInterceptionState eState = intercepted(rInterception, xRequest);
130 0 : if (eState == E_NOT_INTERCEPTED)
131 0 : break;
132 0 : return eState;
133 : }
134 :
135 0 : ++nHandle;
136 0 : }
137 :
138 0 : if (pIt != m_lInterceptions.end()) // => can be true only if bMatch=TRUE!
139 : {
140 : // match -> search required continuation
141 0 : const InterceptedRequest& rInterception = *pIt;
142 0 : css::uno::Reference< css::task::XInteractionContinuation > xContinuation = InterceptedInteraction::extractContinuation(lContinuations, rInterception.Continuation);
143 0 : if (xContinuation.is())
144 : {
145 0 : xContinuation->select();
146 0 : return E_INTERCEPTED;
147 : }
148 :
149 : // Can be reached only, if the request does not support the given continuation!
150 : // => RuntimeError!?
151 0 : return E_NO_CONTINUATION_FOUND;
152 : }
153 :
154 0 : return E_NOT_INTERCEPTED;
155 : }
156 :
157 : } // namespace ucbhelper
158 :
159 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|