LibreOffice Module comphelper (master)  1
SetFlagContextHelper.hxx
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 
10 #ifndef INCLUDED_COMPHELPER_SETFLAGCONTEXTHELPER_HXX
11 #define INCLUDED_COMPHELPER_SETFLAGCONTEXTHELPER_HXX
12 
13 #include <com/sun/star/uno/XCurrentContext.hpp>
14 #include <cppuhelper/implbase.hxx>
15 #include <uno/current_context.hxx>
16 
17 namespace comphelper
18 {
19 // Used to flag some named value to be true for all code running in this context
20 class SetFlagContext final : public cppu::WeakImplHelper<css::uno::XCurrentContext>
21 {
22 public:
23  explicit SetFlagContext(const OUString& sName,
24  css::uno::Reference<css::uno::XCurrentContext> const& xContext)
25  : m_sName(sName)
26  , mxNextContext(xContext)
27  {
28  }
29  SetFlagContext(const SetFlagContext&) = delete;
30  SetFlagContext& operator=(const SetFlagContext&) = delete;
31 
32  virtual css::uno::Any SAL_CALL getValueByName(OUString const& Name) override
33  {
34  if (Name == m_sName)
35  return css::uno::Any(true);
36  else if (mxNextContext.is())
37  return mxNextContext->getValueByName(Name);
38  else
39  return css::uno::Any();
40  }
41 
42 private:
43  OUString m_sName;
44  css::uno::Reference<css::uno::XCurrentContext> mxNextContext;
45 };
46 
47 // Returns a new context that reports the named value to be true
48 inline css::uno::Reference<css::uno::XCurrentContext> NewFlagContext(const OUString& sName)
49 {
50  return new SetFlagContext(sName, css::uno::getCurrentContext());
51 }
52 
53 // A specialization for preventing "Java must be enabled" interaction
54 inline css::uno::Reference<css::uno::XCurrentContext> NoEnableJavaInteractionContext()
55 {
56  return NewFlagContext("DontEnableJava");
57 }
58 
59 inline bool IsContextFlagActive(const OUString& sName)
60 {
61  bool bFlag = false;
62  if (const auto xContext = css::uno::getCurrentContext())
63  xContext->getValueByName(sName) >>= bFlag;
64  return bFlag;
65 }
66 
67 } // namespace comphelper
68 
69 #endif // INCLUDED_COMPHELPER_SETFLAGCONTEXTHELPER_HXX
70 
71 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XCurrentContext > mxNextContext
css::uno::Reference< css::uno::XCurrentContext > NoEnableJavaInteractionContext()
virtual css::uno::Any SAL_CALL getValueByName(OUString const &Name) override
bool IsContextFlagActive(const OUString &sName)
SetFlagContext(const OUString &sName, css::uno::Reference< css::uno::XCurrentContext > const &xContext)
SetFlagContext & operator=(const SetFlagContext &)=delete
css::uno::Reference< css::uno::XCurrentContext > NewFlagContext(const OUString &sName)