LibreOffice Module unotools (master) 1
eventcfg.cxx
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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
23#include <unotools/eventcfg.hxx>
26#include <com/sun/star/uno/Any.hxx>
27#include <com/sun/star/uno/Sequence.hxx>
28#include <com/sun/star/beans/PropertyValue.hpp>
29#include <o3tl/enumarray.hxx>
30#include <o3tl/enumrange.hxx>
31#include <rtl/ref.hxx>
32#include <sal/log.hxx>
33
34#include "itemholder1.hxx"
35
36#include <algorithm>
37#include <unordered_map>
38
39using namespace ::utl;
40using namespace ::osl;
41using namespace ::com::sun::star::uno;
42using namespace ::com::sun::star;
43
44#define PATHDELIMITER "/"
45#define SETNODE_BINDINGS "Bindings"
46#define PROPERTYNAME_BINDINGURL "BindingURL"
47
49{
50"OnStartApp",
51"OnCloseApp",
52"OnCreate",
53"OnNew",
54"OnLoadFinished",
55"OnLoad",
56"OnPrepareUnload",
57"OnUnload",
58"OnSave",
59"OnSaveDone",
60"OnSaveFailed",
61"OnSaveAs",
62"OnSaveAsDone",
63"OnSaveAsFailed",
64"OnCopyTo",
65"OnCopyToDone",
66"OnCopyToFailed",
67"OnFocus",
68"OnUnfocus",
69"OnPrint",
70"OnViewCreated",
71"OnPrepareViewClosing",
72"OnViewClosed",
73"OnModifyChanged",
74"OnTitleChanged",
75"OnVisAreaChanged",
76"OnModeChanged",
77"OnStorageChanged"
78};
79
80typedef std::unordered_map< OUString, OUString > EventBindingHash;
82
84{
85 static std::mutex INSTANCE;
86 return INSTANCE;
87}
88
90{
91private:
94
95 void initBindingInfo();
96
97 virtual void ImplCommit() override;
98
99public:
101 virtual ~GlobalEventConfig_Impl( ) override;
102
103 void Notify( const css::uno::Sequence<OUString>& aPropertyNames) override;
104
109 void replaceByName( const OUString& aName, const css::uno::Any& aElement );
113 css::uno::Sequence < css::beans::PropertyValue > getByName( const OUString& aName );
115 css::uno::Sequence< OUString > getElementNames( );
117 bool hasByName( const OUString& aName );
119 static css::uno::Type const & getElementType( );
121 bool hasElements() const;
122 OUString const & GetEventName( GlobalEventId nID ) const;
123};
124
125
127 : ConfigItem( "Office.Events/ApplicationEvents", ConfigItemMode::NONE )
128{
129 // the supported event names
131 m_supportedEvents[id] = OUString::createFromAscii( pEventAsciiNames[id] );
132
134
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 { "Events" };
139 EnableNotification( aNotifySeq, true );
140}
141
142// destructor
143
145{
146 assert(!IsModified()); // should have been committed
147}
148
150{
152}
153
154// public method
155
157{
158 std::unique_lock aGuard( GetOwnStaticMutex() );
159
161}
162
163// public method
164
166{
167 //DF need to check it this is correct??
168 SAL_INFO("unotools", "In GlobalEventConfig_Impl::ImplCommit");
169 // clear the existing nodes
171 OUString sNode;
172 //step through the list of events
173 for(const auto& rEntry : m_eventBindingHash)
174 {
175 //no point in writing out empty bindings!
176 if(rEntry.second.isEmpty() )
177 continue;
178 sNode = SETNODE_BINDINGS PATHDELIMITER "BindingType['" +
179 rEntry.first +
181 SAL_INFO("unotools", "writing binding for: " << sNode);
182 //write the data to the registry
184 }
185}
186
187// private method
188
190{
191 // Get ALL names of current existing list items in configuration!
193
194 OUString aSetNode = SETNODE_BINDINGS PATHDELIMITER;
195 OUString aCommandKey = PATHDELIMITER PROPERTYNAME_BINDINGURL;
196
197 // Expand all keys
198 Sequence< OUString > lMacros(1);
199 auto plMacros = lMacros.getArray();
200 for (const auto& rEventName : lEventNames )
201 {
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() )
206 {
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 )
212 {
213 startIndex++;
214 OUString eventName = rEventName.copy(startIndex,endIndex-startIndex);
215 m_eventBindingHash[ eventName ] = sMacroURL;
216 }
217 }
218 }
219}
220
221void GlobalEventConfig_Impl::replaceByName( const OUString& aName, const Any& aElement )
222{
223 Sequence< beans::PropertyValue > props;
224 //DF should we prepopulate the hash with a list of valid event Names?
225 if( !( aElement >>= props ) )
226 {
227 throw lang::IllegalArgumentException( OUString(),
228 Reference< XInterface > (), 2);
229 }
230 OUString macroURL;
231 for( const auto& rProp : std::as_const(props) )
232 {
233 if ( rProp.Name == "Script" )
234 rProp.Value >>= macroURL;
235 }
236 m_eventBindingHash[ aName ] = macroURL;
237 SetModified();
238}
239
240css::uno::Sequence < css::beans::PropertyValue > GlobalEventConfig_Impl::getByName( const OUString& aName )
241{
242 static constexpr OUStringLiteral sEventType = u"EventType";
243 static constexpr OUStringLiteral sScript = u"Script";
244 Sequence< beans::PropertyValue > props(2);
245 auto pProps = props.getArray();
246 pProps[0].Name = sEventType;
247 pProps[0].Value <<= OUString(sScript);
248 pProps[1].Name = sScript;
249 EventBindingHash::const_iterator it = m_eventBindingHash.find( aName );
250 if( it != m_eventBindingHash.end() )
251 {
252 pProps[1].Value <<= it->second;
253 }
254 else
255 {
256 // not yet accessed - is it a supported name?
259 if ( pos == m_supportedEvents.end() )
260 throw container::NoSuchElementException( aName );
261
262 pProps[1].Value <<= OUString();
263 }
264 return props;
265}
266
268{
269 return uno::Sequence< OUString >(m_supportedEvents.data(), SupportedEventsVector::size());
270}
271
272bool GlobalEventConfig_Impl::hasByName( const OUString& aName )
273{
274 if ( m_eventBindingHash.find( aName ) != m_eventBindingHash.end() )
275 return true;
276
277 // never accessed before - is it supported in general?
280 return pos != m_supportedEvents.end();
281}
282
284{
285 //DF definitely not sure about this??
287}
288
290{
291 return !m_eventBindingHash.empty();
292}
293
294// and now the wrapper
295
296//initialize static member
299
301{
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 )
308 {
310 aGuard.unlock();
312 }
313}
314
316{
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 )
324 {
325 delete m_pImpl;
326 m_pImpl = nullptr;
327 }
328}
329
330Reference< container::XNameReplace > SAL_CALL GlobalEventConfig::getEvents()
331{
332 std::unique_lock aGuard( GetOwnStaticMutex() );
333 Reference< container::XNameReplace > ret(this);
334 return ret;
335}
336
337void SAL_CALL GlobalEventConfig::replaceByName( const OUString& aName, const Any& aElement )
338{
339 std::unique_lock aGuard( GetOwnStaticMutex() );
340 m_pImpl->replaceByName( aName, aElement );
341}
342Any SAL_CALL GlobalEventConfig::getByName( const OUString& aName )
343{
344 return Any(getByName2(aName));
345}
346css::uno::Sequence < css::beans::PropertyValue > GlobalEventConfig::getByName2( const OUString& aName )
347{
348 std::unique_lock aGuard( GetOwnStaticMutex() );
349 return m_pImpl->getByName( aName );
350}
352{
353 std::unique_lock aGuard( GetOwnStaticMutex() );
354 return m_pImpl->getElementNames( );
355}
356sal_Bool SAL_CALL GlobalEventConfig::hasByName( const OUString& aName )
357{
358 std::unique_lock aGuard( GetOwnStaticMutex() );
359 return m_pImpl->hasByName( aName );
360}
362{
363 std::unique_lock aGuard( GetOwnStaticMutex() );
365}
367{
368 std::unique_lock aGuard( GetOwnStaticMutex() );
369 return m_pImpl->hasElements( );
370}
371
373{
375 return OUString();
378}
379
380/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
EventBindingHash m_eventBindingHash
Definition: eventcfg.cxx:92
virtual ~GlobalEventConfig_Impl() override
Definition: eventcfg.cxx:144
void replaceByName(const OUString &aName, const css::uno::Any &aElement)
Definition: eventcfg.cxx:221
static css::uno::Type const & getElementType()
Definition: eventcfg.cxx:283
void Notify(const css::uno::Sequence< OUString > &aPropertyNames) override
is called from the ConfigManager before application ends of from the PropertyChangeListener if the su...
Definition: eventcfg.cxx:156
bool hasElements() const
Definition: eventcfg.cxx:289
SupportedEventsVector m_supportedEvents
Definition: eventcfg.cxx:93
css::uno::Sequence< css::beans::PropertyValue > getByName(const OUString &aName)
Definition: eventcfg.cxx:240
bool hasByName(const OUString &aName)
Definition: eventcfg.cxx:272
virtual void ImplCommit() override
writes the changed values into the sub tree.
Definition: eventcfg.cxx:165
css::uno::Sequence< OUString > getElementNames()
Definition: eventcfg.cxx:267
OUString const & GetEventName(GlobalEventId nID) const
Definition: eventcfg.cxx:149
sal_Bool SAL_CALL hasElements() override
Definition: eventcfg.cxx:366
void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
Definition: eventcfg.cxx:337
css::uno::Reference< css::container::XNameReplace > SAL_CALL getEvents() override
Definition: eventcfg.cxx:330
css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: eventcfg.cxx:351
css::uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: eventcfg.cxx:342
css::uno::Sequence< css::beans::PropertyValue > getByName2(const OUString &aName)
Definition: eventcfg.cxx:346
static OUString GetEventName(GlobalEventId nID)
Definition: eventcfg.cxx:372
virtual ~GlobalEventConfig() override
Definition: eventcfg.cxx:315
sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: eventcfg.cxx:356
static GlobalEventConfig_Impl * m_pImpl
Definition: eventcfg.hxx:82
static sal_Int32 m_nRefCount
Definition: eventcfg.hxx:83
css::uno::Type SAL_CALL getElementType() override
Definition: eventcfg.cxx:361
static void holdConfigItem(EItem eItem)
Definition: itemholder1.cxx:68
iterator end()
iterator begin()
css::uno::Sequence< css::uno::Any > GetProperties(const css::uno::Sequence< OUString > &rNames)
bool SetSetProperties(const OUString &rNode, const css::uno::Sequence< css::beans::PropertyValue > &rValues)
css::uno::Sequence< OUString > GetNodeNames(const OUString &rNode)
Definition: configitem.cxx:685
bool EnableNotification(const css::uno::Sequence< OUString > &rNames, bool bEnableInternalNotification=false)
enables notifications about changes on selected sub nodes/values
Definition: configitem.cxx:604
bool IsModified() const
Definition: configitem.hxx:177
bool ClearNodeSet(const OUString &rNode)
Definition: configitem.cxx:730
static bool IsFuzzing()
Definition: configmgr.cxx:181
ConfigItemMode
Definition: configitem.hxx:47
float u
o3tl::enumarray< GlobalEventId, OUString > SupportedEventsVector
Definition: eventcfg.cxx:81
#define PROPERTYNAME_BINDINGURL
Definition: eventcfg.cxx:46
std::unordered_map< OUString, OUString > EventBindingHash
Definition: eventcfg.cxx:80
#define SETNODE_BINDINGS
Definition: eventcfg.cxx:45
static o3tl::enumarray< GlobalEventId, const char * > pEventAsciiNames
Definition: eventcfg.cxx:48
#define PATHDELIMITER
Definition: eventcfg.cxx:44
static std::mutex & GetOwnStaticMutex()
Definition: eventcfg.cxx:83
GlobalEventId
Definition: eventcfg.hxx:29
sal_Int32 nIndex
OUString aName
@ EventConfig
#define SAL_INFO(area, stream)
NONE
OUString get(TranslateId sContextAndId, const std::locale &loc)
Definition: resmgr.cxx:211
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
Type
def createImpl(model)
dictionary props
std::mutex mutex
Definition: textsearch.cxx:94
unsigned char sal_Bool
constexpr OUStringLiteral sScript
constexpr OUStringLiteral sEventType
size_t pos