1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <unotools/extendedsecurityoptions.hxx>
21 #include <unotools/configmgr.hxx>
22 #include <unotools/configitem.hxx>
23 #include <tools/debug.hxx>
24 #include <com/sun/star/uno/Any.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <rtl/ustrbuf.hxx>
28 #include <unotools/pathoptions.hxx>
30 #include <boost/unordered_map.hpp>
32 #include "itemholder1.hxx"
34 using namespace ::utl
;
35 using namespace ::rtl
;
36 using namespace ::osl
;
37 using namespace ::com::sun::star::uno
;
39 #define ROOTNODE_SECURITY OUString("Office.Security")
41 #define SECURE_EXTENSIONS_SET OUString("SecureExtensions")
42 #define EXTENSION_PROPNAME OUString("/Extension")
44 #define PROPERTYNAME_HYPERLINKS_OPEN OUString("Hyperlinks/Open")
46 #define PROPERTYHANDLE_HYPERLINKS_OPEN 0
48 #define PROPERTYCOUNT 1
50 typedef boost::unordered_map
<OUString
, sal_Int32
, OUStringHash
>
53 class SvtExtendedSecurityOptions_Impl
: public ConfigItem
56 SvtExtendedSecurityOptions_Impl();
57 virtual ~SvtExtendedSecurityOptions_Impl();
59 /*-****************************************************************************************************
60 @short called for notify of configmanager
61 @descr These method is called from the ConfigManager before application ends or from the
62 PropertyChangeListener if the sub tree broadcasts changes. You must update your
65 @seealso baseclass ConfigItem
67 @param "seqPropertyNames" is the list of properties which should be updated.
68 *//*-*****************************************************************************************************/
70 virtual void Notify( const Sequence
< OUString
>& seqPropertyNames
) SAL_OVERRIDE
;
72 /*-****************************************************************************************************
73 @short write changes to configuration
74 @descr These method writes the changed values into the sub tree
75 and should always called in our destructor to guarantee consistency of config data.
77 @seealso baseclass ConfigItem
78 *//*-*****************************************************************************************************/
80 virtual void Commit() SAL_OVERRIDE
;
82 SvtExtendedSecurityOptions::OpenHyperlinkMode
GetOpenHyperlinkMode();
85 /*-****************************************************************************************************
86 @short return list of key names of our configuration management which represent oue module tree
87 @descr These methods return a static const list of key names. We need it to get needed values from our
88 configuration management.
89 @return A list of needed configuration keys is returned.
90 *//*-*****************************************************************************************************/
92 static Sequence
< OUString
> GetPropertyNames();
94 /*-****************************************************************************************************
95 @short Fills the hash map with all extensions known to be secure
96 @descr These methods fills the given hash map object with all extensions known to be secure.
98 A hash map to be filled with secure extension strings.
99 *//*-*****************************************************************************************************/
100 void FillExtensionHashMap( ExtensionHashMap
& aHashMap
);
102 OUString m_aSecureExtensionsSetName
;
103 OUString m_aExtensionPropName
;
105 SvtExtendedSecurityOptions::OpenHyperlinkMode m_eOpenHyperlinkMode
;
106 bool m_bROOpenHyperlinkMode
;
107 ExtensionHashMap m_aExtensionHashMap
;
112 SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()
113 // Init baseclasses first
114 : ConfigItem ( ROOTNODE_SECURITY
)
115 , m_aSecureExtensionsSetName( SECURE_EXTENSIONS_SET
)
116 , m_aExtensionPropName( EXTENSION_PROPNAME
)
117 , m_eOpenHyperlinkMode(SvtExtendedSecurityOptions::OPEN_NEVER
)
118 , m_bROOpenHyperlinkMode(false)
121 // Fill the extension hash map with all secure extension strings
122 FillExtensionHashMap( m_aExtensionHashMap
);
124 Sequence
< OUString
> seqNames
= GetPropertyNames();
125 Sequence
< Any
> seqValues
= GetProperties( seqNames
);
126 Sequence
< sal_Bool
> seqRO
= GetReadOnlyStates ( seqNames
);
128 sal_Int32 nPropertyCount
= seqValues
.getLength();
129 for( sal_Int32 nProperty
=0; nProperty
<nPropertyCount
; ++nProperty
)
131 // Safe impossible cases.
132 // Check any for valid value.
133 DBG_ASSERT( seqValues
[nProperty
].hasValue(), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nInvalid property value detected!\n" );
136 case PROPERTYHANDLE_HYPERLINKS_OPEN
:
138 DBG_ASSERT( ( seqValues
[nProperty
].getValueTypeClass() == TypeClass_LONG
), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nWho has changed the value type of 'Hyperlink/Open'?" );
140 sal_Int32 nMode
= SvtExtendedSecurityOptions::OPEN_WITHSECURITYCHECK
;
141 if ( seqValues
[nProperty
] >>= nMode
)
142 m_eOpenHyperlinkMode
= (SvtExtendedSecurityOptions::OpenHyperlinkMode
)nMode
;
144 OSL_FAIL("Wrong type for Open mode!");
146 m_bROOpenHyperlinkMode
= seqRO
[nProperty
];
152 // Enable notification mechanism of our baseclass.
153 // We need it to get information about changes outside these class on our used configuration keys!
154 Sequence
< OUString
> seqNotifyNames( 1 );
155 seqNotifyNames
[0] = m_aSecureExtensionsSetName
;
156 EnableNotification( seqNotifyNames
);
161 SvtExtendedSecurityOptions_Impl::~SvtExtendedSecurityOptions_Impl()
163 // We must save our current values .. if user forget it!
172 void SvtExtendedSecurityOptions_Impl::Notify( const Sequence
< OUString
>& )
179 void SvtExtendedSecurityOptions_Impl::Commit()
181 // Get names of supported properties, create a list for values and copy current values to it.
182 Sequence
< OUString
> seqNames
= GetPropertyNames ();
183 sal_Int32 nCount
= seqNames
.getLength();
184 Sequence
< Any
> seqValues ( nCount
);
185 for( sal_Int32 nProperty
=0; nProperty
<nCount
; ++nProperty
)
189 case PROPERTYHANDLE_HYPERLINKS_OPEN
: {
190 seqValues
[nProperty
] <<= (sal_Int32
)m_eOpenHyperlinkMode
;
196 // Set properties in configuration.
197 PutProperties( seqNames
, seqValues
);
202 SvtExtendedSecurityOptions::OpenHyperlinkMode
SvtExtendedSecurityOptions_Impl::GetOpenHyperlinkMode()
204 return m_eOpenHyperlinkMode
;
209 void SvtExtendedSecurityOptions_Impl::FillExtensionHashMap( ExtensionHashMap
& aHashMap
)
211 // Get sequence with secure extensions from configuration
212 Sequence
< OUString
> seqNodes
= GetNodeNames( m_aSecureExtensionsSetName
);
215 Sequence
< Any
> aValues
;
216 Sequence
< OUString
> aPropSeq( 1 );
217 for ( int i
= 0; i
< seqNodes
.getLength(); i
++ )
219 // Create access name for property
220 OUStringBuffer
aExtEntryProp( m_aSecureExtensionsSetName
);
221 aExtEntryProp
.appendAscii( "/" );
222 aExtEntryProp
.append( seqNodes
[i
] );
223 aExtEntryProp
.append( m_aExtensionPropName
);
225 aPropSeq
[0] = aExtEntryProp
.makeStringAndClear();
226 aValues
= GetProperties( aPropSeq
);
227 if ( aValues
.getLength() == 1 )
229 // Don't use value if sequence has not the correct length
230 if ( aValues
[0] >>= aValue
)
231 // Add extension into secure extensions hash map
232 aHashMap
.insert( ExtensionHashMap::value_type( aValue
.toAsciiLowerCase(), 1 ) );
235 SAL_WARN( "unotools.config", "SvtExtendedSecurityOptions_Impl::FillExtensionHashMap(): not string value?" );
241 // private method (currently not used)
243 Sequence
< OUString
> SvtExtendedSecurityOptions_Impl::GetPropertyNames()
245 // Build list of configuration key names.
246 const OUString pProperties
[] =
248 PROPERTYNAME_HYPERLINKS_OPEN
250 // Initialize return sequence with these list ...
251 const Sequence
< OUString
> seqPropertyNames( pProperties
, PROPERTYCOUNT
);
252 // ... and return it.
253 return seqPropertyNames
;
256 // initialize static member
257 // DON'T DO IT IN YOUR HEADER!
258 // see definition for further information
260 SvtExtendedSecurityOptions_Impl
* SvtExtendedSecurityOptions::m_pDataContainer
= NULL
;
261 sal_Int32
SvtExtendedSecurityOptions::m_nRefCount
= 0;
265 SvtExtendedSecurityOptions::SvtExtendedSecurityOptions()
267 // Global access, must be guarded (multithreading!).
268 MutexGuard
aGuard( GetInitMutex() );
269 // Increase our refcount ...
271 // ... and initialize our data container only if it not already exist!
272 if( m_pDataContainer
== NULL
)
274 m_pDataContainer
= new SvtExtendedSecurityOptions_Impl
;
276 ItemHolder1::holdConfigItem(E_EXTENDEDSECURITYOPTIONS
);
282 SvtExtendedSecurityOptions::~SvtExtendedSecurityOptions()
284 // Global access, must be guarded (multithreading!)
285 MutexGuard
aGuard( GetInitMutex() );
286 // Decrease our refcount.
288 // If last instance was deleted ...
289 // we must destroy our static data container!
290 if( m_nRefCount
<= 0 )
292 delete m_pDataContainer
;
293 m_pDataContainer
= NULL
;
299 SvtExtendedSecurityOptions::OpenHyperlinkMode
SvtExtendedSecurityOptions::GetOpenHyperlinkMode()
301 MutexGuard
aGuard( GetInitMutex() );
302 return m_pDataContainer
->GetOpenHyperlinkMode();
307 class theExtendedSecurityOptionsMutex
: public rtl::Static
<osl::Mutex
, theExtendedSecurityOptionsMutex
>{};
312 Mutex
& SvtExtendedSecurityOptions::GetInitMutex()
314 return theExtendedSecurityOptionsMutex::get();
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */