Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / source / config / extendedsecurityoptions.cxx
blob2d57ab4230a20a7433f15fb43e62e78e560993cf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <unotools/extendedsecurityoptions.hxx>
23 #include <unotools/configitem.hxx>
24 #include <tools/debug.hxx>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <osl/diagnose.h>
29 #include "itemholder1.hxx"
31 #include <unordered_map>
33 using namespace ::utl;
34 using namespace ::osl;
35 using namespace ::com::sun::star::uno;
37 #define ROOTNODE_SECURITY "Office.Security"
39 #define SECURE_EXTENSIONS_SET OUString("SecureExtensions")
41 #define PROPERTYNAME_HYPERLINKS_OPEN OUString("Hyperlinks/Open")
43 #define PROPERTYHANDLE_HYPERLINKS_OPEN 0
45 #define PROPERTYCOUNT 1
47 typedef std::unordered_map<OUString, sal_Int32>
48 ExtensionHashMap;
50 class SvtExtendedSecurityOptions_Impl : public ConfigItem
52 public:
53 SvtExtendedSecurityOptions_Impl();
54 virtual ~SvtExtendedSecurityOptions_Impl() override;
56 /*-****************************************************************************************************
57 @short called for notify of configmanager
58 @descr This method is called from the ConfigManager before the application ends or from the
59 PropertyChangeListener if the sub tree broadcasts changes. You must update your
60 internal values.
62 @seealso baseclass ConfigItem
64 @param "seqPropertyNames" is the list of properties which should be updated.
65 *//*-*****************************************************************************************************/
67 virtual void Notify( const Sequence< OUString >& seqPropertyNames ) override;
69 SvtExtendedSecurityOptions::OpenHyperlinkMode GetOpenHyperlinkMode() const { return m_eOpenHyperlinkMode;}
71 private:
72 virtual void ImplCommit() override;
74 /*-****************************************************************************************************
75 @short return list of key names of our configuration management which represent our module tree
76 @descr This method returns a static const list of key names. We need it to get needed values from our
77 configuration management.
78 @return A list of needed configuration keys is returned.
79 *//*-*****************************************************************************************************/
81 static Sequence< OUString > GetPropertyNames();
83 SvtExtendedSecurityOptions::OpenHyperlinkMode m_eOpenHyperlinkMode;
86 // constructor
88 SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()
89 // Init baseclasses first
90 : ConfigItem ( ROOTNODE_SECURITY )
91 , m_eOpenHyperlinkMode(SvtExtendedSecurityOptions::OPEN_NEVER)
92 // Init member then.
94 Sequence< OUString > seqNames = GetPropertyNames();
95 Sequence< Any > seqValues = GetProperties( seqNames );
97 sal_Int32 nPropertyCount = seqValues.getLength();
98 for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
100 // Safe impossible cases.
101 // Check any for valid value.
102 DBG_ASSERT( seqValues[nProperty].hasValue(), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nInvalid property value detected!\n" );
103 switch( nProperty )
105 case PROPERTYHANDLE_HYPERLINKS_OPEN:
107 DBG_ASSERT( ( seqValues[nProperty].getValueTypeClass() == TypeClass_LONG ), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nWho has changed the value type of 'Hyperlink/Open'?" );
109 sal_Int32 nMode = SvtExtendedSecurityOptions::OPEN_WITHSECURITYCHECK;
110 if ( seqValues[nProperty] >>= nMode )
111 m_eOpenHyperlinkMode = static_cast<SvtExtendedSecurityOptions::OpenHyperlinkMode>(nMode);
112 else {
113 OSL_FAIL("Wrong type for Open mode!");
116 break;
120 // Enable notification mechanism of our baseclass.
121 // We need it to get information about changes outside these class on our used configuration keys!
122 Sequence<OUString> seqNotifyNames { SECURE_EXTENSIONS_SET };
123 EnableNotification( seqNotifyNames );
126 // destructor
128 SvtExtendedSecurityOptions_Impl::~SvtExtendedSecurityOptions_Impl()
130 assert(!IsModified()); // should have been committed
133 // public method
135 void SvtExtendedSecurityOptions_Impl::Notify( const Sequence< OUString >& )
137 // Not implemented
140 // public method
142 void SvtExtendedSecurityOptions_Impl::ImplCommit()
144 // Get names of supported properties, create a list for values and copy current values to it.
145 Sequence< OUString > seqNames = GetPropertyNames ();
146 sal_Int32 nCount = seqNames.getLength();
147 Sequence< Any > seqValues ( nCount );
148 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
150 switch( nProperty )
152 case PROPERTYHANDLE_HYPERLINKS_OPEN: {
153 seqValues[nProperty] <<= static_cast<sal_Int32>(m_eOpenHyperlinkMode);
155 break;
159 // Set properties in configuration.
160 PutProperties( seqNames, seqValues );
163 // private method (currently not used)
165 Sequence< OUString > SvtExtendedSecurityOptions_Impl::GetPropertyNames()
167 // Build list of configuration key names.
168 const OUString pProperties[] =
170 PROPERTYNAME_HYPERLINKS_OPEN
172 // Initialize return sequence with these list ...
173 const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
174 // ... and return it.
175 return seqPropertyNames;
178 namespace {
180 std::weak_ptr<SvtExtendedSecurityOptions_Impl> g_pExtendedSecurityOptions;
184 SvtExtendedSecurityOptions::SvtExtendedSecurityOptions()
186 // Global access, must be guarded (multithreading!).
187 MutexGuard aGuard( GetInitMutex() );
189 m_pImpl = g_pExtendedSecurityOptions.lock();
190 if( !m_pImpl )
192 m_pImpl = std::make_shared<SvtExtendedSecurityOptions_Impl>();
193 g_pExtendedSecurityOptions = m_pImpl;
194 ItemHolder1::holdConfigItem(EItem::ExtendedSecurityOptions);
198 SvtExtendedSecurityOptions::~SvtExtendedSecurityOptions()
200 // Global access, must be guarded (multithreading!)
201 MutexGuard aGuard( GetInitMutex() );
203 m_pImpl.reset();
206 // public method
208 SvtExtendedSecurityOptions::OpenHyperlinkMode SvtExtendedSecurityOptions::GetOpenHyperlinkMode() const
210 MutexGuard aGuard( GetInitMutex() );
211 return m_pImpl->GetOpenHyperlinkMode();
214 namespace
216 class theExtendedSecurityOptionsMutex : public rtl::Static<osl::Mutex, theExtendedSecurityOptionsMutex>{};
219 // private method
221 Mutex& SvtExtendedSecurityOptions::GetInitMutex()
223 return theExtendedSecurityOptionsMutex::get();
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */