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>
15#include <uno/current_context.hxx>
16#include <utility>
17
18namespace comphelper
19{
20// Used to flag some named value to be true for all code running in this context
21class SetFlagContext final : public cppu::WeakImplHelper<css::uno::XCurrentContext>
22{
23public:
24 explicit SetFlagContext(OUString sName, css::uno::Reference<css::uno::XCurrentContext> xContext)
25 : m_sName(std::move(sName))
26 , mxNextContext(std::move(xContext))
27 {
28 }
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
42private:
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
48inline 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
54inline css::uno::Reference<css::uno::XCurrentContext> NoEnableJavaInteractionContext()
55{
56 return NewFlagContext("DontEnableJava");
57}
58
59inline 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: */
virtual css::uno::Any SAL_CALL getValueByName(OUString const &Name) override
SetFlagContext(const SetFlagContext &)=delete
SetFlagContext(OUString sName, css::uno::Reference< css::uno::XCurrentContext > xContext)
css::uno::Reference< css::uno::XCurrentContext > mxNextContext
SetFlagContext & operator=(const SetFlagContext &)=delete
OUString sName
bool IsContextFlagActive(const OUString &sName)
css::uno::Reference< css::uno::XCurrentContext > NoEnableJavaInteractionContext()
css::uno::Reference< css::uno::XCurrentContext > NewFlagContext(const OUString &sName)
OUString Name