cid#1636561 Dereference after null check
[LibreOffice.git] / unotools / source / config / eventcfg.cxx
blob3649ae69a6cd2e19610e3e3cc56f9c4cfaf23a3d
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 <comphelper/configuration.hxx>
23 #include <comphelper/propertyvalue.hxx>
24 #include <unotools/eventcfg.hxx>
25 #include <unotools/configmgr.hxx>
26 #include <unotools/configitem.hxx>
27 #include <com/sun/star/uno/Any.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <com/sun/star/beans/PropertyValue.hpp>
30 #include <o3tl/enumarray.hxx>
31 #include <o3tl/enumrange.hxx>
32 #include <rtl/ref.hxx>
33 #include <sal/log.hxx>
35 #include "itemholder1.hxx"
37 #include <algorithm>
38 #include <unordered_map>
40 using namespace ::utl;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star;
44 #define PATHDELIMITER "/"
45 #define SETNODE_BINDINGS "Bindings"
46 #define PROPERTYNAME_BINDINGURL "BindingURL"
48 constexpr o3tl::enumarray<GlobalEventId, OUString> pEventAsciiNames =
50 u"OnStartApp"_ustr,
51 u"OnCloseApp"_ustr,
52 u"OnCreate"_ustr,
53 u"OnNew"_ustr,
54 u"OnLoadFinished"_ustr,
55 u"OnLoad"_ustr,
56 u"OnPrepareUnload"_ustr,
57 u"OnUnload"_ustr,
58 u"OnSave"_ustr,
59 u"OnSaveDone"_ustr,
60 u"OnSaveFailed"_ustr,
61 u"OnSaveAs"_ustr,
62 u"OnSaveAsDone"_ustr,
63 u"OnSaveAsFailed"_ustr,
64 u"OnCopyTo"_ustr,
65 u"OnCopyToDone"_ustr,
66 u"OnCopyToFailed"_ustr,
67 u"OnFocus"_ustr,
68 u"OnUnfocus"_ustr,
69 u"OnPrint"_ustr,
70 u"OnViewCreated"_ustr,
71 u"OnPrepareViewClosing"_ustr,
72 u"OnViewClosed"_ustr,
73 u"OnModifyChanged"_ustr,
74 u"OnTitleChanged"_ustr,
75 u"OnVisAreaChanged"_ustr,
76 u"OnModeChanged"_ustr,
77 u"OnStorageChanged"_ustr
80 typedef std::unordered_map< OUString, OUString > EventBindingHash;
81 typedef o3tl::enumarray< GlobalEventId, OUString > SupportedEventsVector;
83 static std::mutex& GetOwnStaticMutex()
85 static std::mutex INSTANCE;
86 return INSTANCE;
89 class GlobalEventConfig_Impl : public utl::ConfigItem
91 private:
92 EventBindingHash m_eventBindingHash;
93 SupportedEventsVector m_supportedEvents;
95 void initBindingInfo();
97 virtual void ImplCommit() override;
99 public:
100 GlobalEventConfig_Impl( );
101 virtual ~GlobalEventConfig_Impl( ) override;
103 void Notify( const css::uno::Sequence<OUString>& aPropertyNames) override;
105 /// @throws css::lang::IllegalArgumentException
106 /// @throws css::container::NoSuchElementException
107 /// @throws css::lang::WrappedTargetException
108 /// @throws css::uno::RuntimeException
109 void replaceByName( const OUString& aName, const css::uno::Any& aElement );
110 /// @throws css::container::NoSuchElementException
111 /// @throws css::lang::WrappedTargetException
112 /// @throws css::uno::RuntimeException
113 css::uno::Sequence < css::beans::PropertyValue > getByName( const OUString& aName );
114 /// @throws css::uno::RuntimeException
115 css::uno::Sequence< OUString > getElementNames( );
116 /// @throws css::uno::RuntimeException
117 bool hasByName( const OUString& aName );
118 /// @throws css::uno::RuntimeException
119 static css::uno::Type const & getElementType( );
120 /// @throws css::uno::RuntimeException
121 bool hasElements() const;
122 OUString const & GetEventName( GlobalEventId nID ) const;
126 GlobalEventConfig_Impl::GlobalEventConfig_Impl()
127 : ConfigItem( u"Office.Events/ApplicationEvents"_ustr, ConfigItemMode::NONE )
129 // the supported event names
130 for (const GlobalEventId id : o3tl::enumrange<GlobalEventId>())
131 m_supportedEvents[id] = pEventAsciiNames[id];
133 initBindingInfo();
135 /*TODO: Not used in the moment! see Notify() ...
136 // Enable notification mechanism of our baseclass.
137 // We need it to get information about changes outside these class on our used configuration keys! */
138 Sequence<OUString> aNotifySeq { u"Events"_ustr };
139 EnableNotification( aNotifySeq, true );
142 // destructor
144 GlobalEventConfig_Impl::~GlobalEventConfig_Impl()
146 assert(!IsModified()); // should have been committed
149 OUString const & GlobalEventConfig_Impl::GetEventName( GlobalEventId nIndex ) const
151 return m_supportedEvents[nIndex];
154 // public method
156 void GlobalEventConfig_Impl::Notify( const Sequence< OUString >& )
158 std::unique_lock aGuard( GetOwnStaticMutex() );
160 initBindingInfo();
163 // public method
165 void GlobalEventConfig_Impl::ImplCommit()
167 //DF need to check it this is correct??
168 SAL_INFO("unotools", "In GlobalEventConfig_Impl::ImplCommit");
169 // clear the existing nodes
170 ClearNodeSet( u"" SETNODE_BINDINGS ""_ustr );
171 OUString sNode;
172 //step through the list of events
173 for(const auto& rEntry : m_eventBindingHash)
175 //no point in writing out empty bindings!
176 if(rEntry.second.isEmpty() )
177 continue;
178 sNode = SETNODE_BINDINGS PATHDELIMITER "BindingType['" +
179 rEntry.first +
180 "']" PATHDELIMITER PROPERTYNAME_BINDINGURL;
181 SAL_INFO("unotools", "writing binding for: " << sNode);
182 //write the data to the registry
183 SetSetProperties(u"" SETNODE_BINDINGS ""_ustr,{ comphelper::makePropertyValue(sNode, rEntry.second) });
187 // private method
189 void GlobalEventConfig_Impl::initBindingInfo()
191 // Get ALL names of current existing list items in configuration!
192 const Sequence< OUString > lEventNames = GetNodeNames( u"" SETNODE_BINDINGS ""_ustr, utl::ConfigNameFormat::LocalPath );
194 OUString aSetNode = u"" SETNODE_BINDINGS PATHDELIMITER ""_ustr;
195 OUString aCommandKey = u"" PATHDELIMITER PROPERTYNAME_BINDINGURL ""_ustr;
197 // Expand all keys
198 Sequence< OUString > lMacros(1);
199 auto plMacros = lMacros.getArray();
200 for (const auto& rEventName : lEventNames )
202 plMacros[0] = aSetNode + rEventName + aCommandKey;
203 SAL_INFO("unotools", "reading binding for: " << lMacros[0]);
204 Sequence< Any > lValues = GetProperties( lMacros );
205 if( lValues.hasElements() )
207 OUString sMacroURL;
208 lValues[0] >>= sMacroURL;
209 sal_Int32 startIndex = rEventName.indexOf('\'');
210 sal_Int32 endIndex = rEventName.lastIndexOf('\'');
211 if( startIndex >=0 && endIndex > 0 )
213 startIndex++;
214 OUString eventName = rEventName.copy(startIndex,endIndex-startIndex);
215 m_eventBindingHash[ eventName ] = sMacroURL;
221 void GlobalEventConfig_Impl::replaceByName( const OUString& aName, const Any& aElement )
223 Sequence< beans::PropertyValue > props;
224 //DF should we prepopulate the hash with a list of valid event Names?
225 if( !( aElement >>= props ) )
227 throw lang::IllegalArgumentException( OUString(),
228 Reference< XInterface > (), 2);
230 OUString macroURL;
231 for (const auto& rProp : props)
233 if ( rProp.Name == "Script" )
234 rProp.Value >>= macroURL;
236 m_eventBindingHash[ aName ] = macroURL;
237 SetModified();
240 css::uno::Sequence < css::beans::PropertyValue > GlobalEventConfig_Impl::getByName( const OUString& aName )
242 static constexpr OUStringLiteral sEventType = u"EventType";
243 static constexpr OUString sScript = u"Script"_ustr;
244 Sequence< beans::PropertyValue > props(2);
245 auto pProps = props.getArray();
246 pProps[0].Name = sEventType;
247 pProps[0].Value <<= sScript;
248 pProps[1].Name = sScript;
249 EventBindingHash::const_iterator it = m_eventBindingHash.find( aName );
250 if( it != m_eventBindingHash.end() )
252 pProps[1].Value <<= it->second;
254 else
256 // not yet accessed - is it a supported name?
257 SupportedEventsVector::iterator pos = ::std::find(
258 m_supportedEvents.begin(), m_supportedEvents.end(), aName );
259 if ( pos == m_supportedEvents.end() )
260 throw container::NoSuchElementException( aName );
262 pProps[1].Value <<= OUString();
264 return props;
267 Sequence< OUString > GlobalEventConfig_Impl::getElementNames( )
269 return uno::Sequence< OUString >(m_supportedEvents.data(), SupportedEventsVector::size());
272 bool GlobalEventConfig_Impl::hasByName( const OUString& aName )
274 if ( m_eventBindingHash.find( aName ) != m_eventBindingHash.end() )
275 return true;
277 // never accessed before - is it supported in general?
278 SupportedEventsVector::iterator pos = ::std::find(
279 m_supportedEvents.begin(), m_supportedEvents.end(), aName );
280 return pos != m_supportedEvents.end();
283 Type const & GlobalEventConfig_Impl::getElementType( )
285 //DF definitely not sure about this??
286 return cppu::UnoType<Sequence<beans::PropertyValue>>::get();
289 bool GlobalEventConfig_Impl::hasElements() const
291 return !m_eventBindingHash.empty();
294 // and now the wrapper
296 //initialize static member
297 GlobalEventConfig_Impl* GlobalEventConfig::m_pImpl = nullptr;
298 sal_Int32 GlobalEventConfig::m_nRefCount = 0;
300 GlobalEventConfig::GlobalEventConfig()
302 // Global access, must be guarded (multithreading!).
303 std::unique_lock aGuard( GetOwnStaticMutex() );
304 // Increase our refcount ...
305 ++m_nRefCount;
306 // ... and initialize our data container only if it not already exist!
307 if( m_pImpl == nullptr )
309 m_pImpl = new GlobalEventConfig_Impl;
310 aGuard.unlock();
311 ItemHolder1::holdConfigItem(EItem::EventConfig);
315 GlobalEventConfig::~GlobalEventConfig()
317 // Global access, must be guarded (multithreading!)
318 std::unique_lock aGuard( GetOwnStaticMutex() );
319 // Decrease our refcount.
320 --m_nRefCount;
321 // If last instance was deleted ...
322 // we must destroy our static data container!
323 if( m_nRefCount <= 0 )
325 delete m_pImpl;
326 m_pImpl = nullptr;
330 Reference< container::XNameReplace > SAL_CALL GlobalEventConfig::getEvents()
332 std::unique_lock aGuard( GetOwnStaticMutex() );
333 Reference< container::XNameReplace > ret(this);
334 return ret;
337 void SAL_CALL GlobalEventConfig::replaceByName( const OUString& aName, const Any& aElement )
339 std::unique_lock aGuard( GetOwnStaticMutex() );
340 m_pImpl->replaceByName( aName, aElement );
342 Any SAL_CALL GlobalEventConfig::getByName( const OUString& aName )
344 return Any(getByName2(aName));
346 css::uno::Sequence < css::beans::PropertyValue > GlobalEventConfig::getByName2( const OUString& aName )
348 std::unique_lock aGuard( GetOwnStaticMutex() );
349 return m_pImpl->getByName( aName );
351 Sequence< OUString > SAL_CALL GlobalEventConfig::getElementNames( )
353 std::unique_lock aGuard( GetOwnStaticMutex() );
354 return m_pImpl->getElementNames( );
356 sal_Bool SAL_CALL GlobalEventConfig::hasByName( const OUString& aName )
358 std::unique_lock aGuard( GetOwnStaticMutex() );
359 return m_pImpl->hasByName( aName );
361 Type SAL_CALL GlobalEventConfig::getElementType( )
363 std::unique_lock aGuard( GetOwnStaticMutex() );
364 return GlobalEventConfig_Impl::getElementType( );
366 sal_Bool SAL_CALL GlobalEventConfig::hasElements( )
368 std::unique_lock aGuard( GetOwnStaticMutex() );
369 return m_pImpl->hasElements( );
372 const OUString & GlobalEventConfig::GetEventName( GlobalEventId nIndex )
374 if (comphelper::IsFuzzing())
375 return EMPTY_OUSTRING;
376 static rtl::Reference<GlobalEventConfig> createImpl(new GlobalEventConfig);
377 return GlobalEventConfig::m_pImpl->GetEventName( nIndex );
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */