LibreOffice Module ucbhelper (master)  1
interceptedinteraction.cxx
Go to the documentation of this file.
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 
21 
22 #include <osl/diagnose.h>
23 
24 namespace ucbhelper{
25 
27 {
28 }
29 
30 void InterceptedInteraction::setInterceptedHandler(const css::uno::Reference< css::task::XInteractionHandler >& xInterceptedHandler)
31 {
32  m_xInterceptedHandler = xInterceptedHandler;
33 }
34 
35 void InterceptedInteraction::setInterceptions(const ::std::vector< InterceptedRequest >& lInterceptions)
36 {
37  m_lInterceptions = lInterceptions;
38 }
39 
41  const InterceptedRequest&,
42  const css::uno::Reference< css::task::XInteractionRequest >&)
43 {
44  // default behaviour! see impl_interceptRequest() for further information ...
45  return E_NOT_INTERCEPTED;
46 }
47 
48 css::uno::Reference< css::task::XInteractionContinuation > InterceptedInteraction::extractContinuation(const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > >& lContinuations,
49  const css::uno::Type& aType )
50 {
51  const css::uno::Reference< css::task::XInteractionContinuation >* pContinuations = std::find_if(lContinuations.begin(), lContinuations.end(),
52  [&aType](const css::uno::Reference< css::task::XInteractionContinuation >& rContinuation) {
53  css::uno::Reference< css::uno::XInterface > xCheck(rContinuation, css::uno::UNO_QUERY);
54  return xCheck->queryInterface(aType).hasValue();
55  });
56  if (pContinuations != lContinuations.end())
57  return *pContinuations;
58 
59  return css::uno::Reference< css::task::XInteractionContinuation >();
60 }
61 
62 void SAL_CALL InterceptedInteraction::handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
63 {
64  impl_handleDefault(xRequest);
65 }
66 
67 void InterceptedInteraction::impl_handleDefault(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
68 {
69  EInterceptionState eState = impl_interceptRequest(xRequest);
70 
71  switch(eState)
72  {
73  case E_NOT_INTERCEPTED:
74  {
75  // Non of the intercepted requests match to the given one.
76  // => forward request to the internal wrapped handler - if there is one.
77  if (m_xInterceptedHandler.is())
78  m_xInterceptedHandler->handle(xRequest);
79  }
80  break;
81 
83  {
84  // Runtime error! The defined continuation could not be located
85  // inside the set of available continuations of the incoming request.
86  // What's wrong - the interception list or the request?
87  OSL_FAIL("InterceptedInteraction::handle()\nCould intercept this interaction request - but can't locate the right continuation!");
88  }
89  break;
90 
91  case E_INTERCEPTED:
92  break;
93  }
94 }
95 
96 InterceptedInteraction::EInterceptionState InterceptedInteraction::impl_interceptRequest(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
97 {
98  css::uno::Any aRequest = xRequest->getRequest();
99  const css::uno::Type& aRequestType = aRequest.getValueType();
100  css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
101 
102  // check against the list of static requests
103  auto pIt = std::find_if(m_lInterceptions.begin(), m_lInterceptions.end(),
104  [&aRequestType](const InterceptedRequest& rInterception) {
105  // check the request
106  // don't change intercepted and request type here -> it will check the wrong direction!
107  return rInterception.Request.getValueType().isAssignableFrom(aRequestType);
108  });
109 
110  if (pIt != m_lInterceptions.end()) // intercepted ...
111  {
112  const InterceptedRequest& rInterception = *pIt;
113 
114  // Call they might existing derived class, so they can handle that by its own.
115  // If it's not interested on that (maybe it's not overwritten and the default implementation
116  // returns E_NOT_INTERCEPTED as default) -> search required continuation
117  EInterceptionState eState = intercepted(rInterception, xRequest);
118  if (eState != E_NOT_INTERCEPTED)
119  return eState;
120 
121  css::uno::Reference< css::task::XInteractionContinuation > xContinuation = InterceptedInteraction::extractContinuation(lContinuations, rInterception.Continuation);
122  if (xContinuation.is())
123  {
124  xContinuation->select();
125  return E_INTERCEPTED;
126  }
127 
128  // Can be reached only, if the request does not support the given continuation!
129  // => RuntimeError!?
131  }
132 
133  return E_NOT_INTERCEPTED;
134 }
135 
136 } // namespace ucbhelper
137 
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
the request could be intercepted and the specified continuation could be selected successfully...
InterceptedInteraction()
initialize a new instance with default values.
UCBHELPER_DLLPRIVATE EInterceptionState impl_interceptRequest(const css::uno::Reference< css::task::XInteractionRequest > &xRequest)
implements the interception of requests.
static css::uno::Reference< css::task::XInteractionContinuation > extractContinuation(const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > &lContinuations, const css::uno::Type &aType)
extract a requested continuation from the list of available ones.
virtual EInterceptionState intercepted(const InterceptedRequest &rRequest, const css::uno::Reference< css::task::XInteractionRequest > &xOrgRequest)
can be overwritten by a derived class to handle interceptions outside.
virtual void SAL_CALL handle(const css::uno::Reference< css::task::XInteractionRequest > &xRequest) override
implements the default handling of this class...
void setInterceptions(const ::std::vector< InterceptedRequest > &lInterceptions)
set a new list of intercepted interactions.
void setInterceptedHandler(const css::uno::Reference< css::task::XInteractionHandler > &xInterceptedHandler)
initialize a new instance with the interaction handler, which should be intercepted.
css::uno::Reference< css::task::XInteractionHandler > m_xInterceptedHandler
reference to the intercepted interaction handler.
::std::vector< InterceptedRequest > m_lInterceptions
these list contains the requests, which should be intercepted.
none of the specified interceptions match the incoming request
the request could be intercepted - but the specified continuation could not be located.
EInterceptionState
represent the different states, which can occur as result of an interception.
css::uno::Type Continuation
specify the fix continuation, which must be selected, if the interaction could be intercepted success...
UCBHELPER_DLLPRIVATE void impl_handleDefault(const css::uno::Reference< css::task::XInteractionRequest > &xRequest)
implements the default handling: