LibreOffice Module dbaccess (master) 1
documentevents.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 <documentevents.hxx>
21
22#include <com/sun/star/beans/PropertyValue.hpp>
23
24#include <o3tl/string_view.hxx>
25#include <osl/diagnose.h>
28
29namespace dbaccess
30{
31
32 using ::com::sun::star::uno::Any;
33 using ::com::sun::star::beans::PropertyValue;
34 using ::com::sun::star::container::NoSuchElementException;
35 using ::com::sun::star::lang::IllegalArgumentException;
36 using ::com::sun::star::uno::Sequence;
37 using ::com::sun::star::uno::Type;
38
39 namespace {
40
41 // helper
42 struct DocumentEventData
43 {
44 const char* pAsciiEventName;
46 };
47
48 const DocumentEventData* lcl_getDocumentEventData()
49 {
50 static const DocumentEventData s_aData[] = {
51 { "OnCreate", true },
52 { "OnLoadFinished", true },
53 { "OnNew", false }, // compatibility, see https://bz.apache.org/ooo/show_bug.cgi?id=46484
54 { "OnLoad", false }, // compatibility, see https://bz.apache.org/ooo/show_bug.cgi?id=46484
55 { "OnSaveAs", true },
56 { "OnSaveAsDone", false },
57 { "OnSaveAsFailed", false },
58 { "OnSave", true },
59 { "OnSaveDone", false },
60 { "OnSaveFailed", false },
61 { "OnSaveTo", true },
62 { "OnSaveToDone", false },
63 { "OnSaveToFailed", false },
64 { "OnPrepareUnload", true },
65 { "OnUnload", true },
66 { "OnFocus", false },
67 { "OnUnfocus", false },
68 { "OnModifyChanged", false },
69 { "OnViewCreated", false },
70 { "OnPrepareViewClosing", true },
71 { "OnViewClosed", false },
72 { "OnTitleChanged", false },
73 { "OnSubComponentOpened", false },
74 { "OnSubComponentClosed", false },
75 { nullptr, false }
76 };
77 return s_aData;
78 }
79 }
80
81 // DocumentEvents
82 DocumentEvents::DocumentEvents( ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex, DocumentEventsData& _rEventsData )
83 :mrParent(_rParent), mrMutex(_rMutex), mrEventsData(_rEventsData)
84 {
85 const DocumentEventData* pEventData = lcl_getDocumentEventData();
86 while ( pEventData->pAsciiEventName )
87 {
88 OUString sEventName = OUString::createFromAscii( pEventData->pAsciiEventName );
89 DocumentEventsData::const_iterator existingPos = mrEventsData.find( sEventName );
90 if ( existingPos == mrEventsData.end() )
91 mrEventsData[ sEventName ] = Sequence< PropertyValue >();
92 ++pEventData;
93 }
94 }
95
97 {
98 }
99
100 void SAL_CALL DocumentEvents::acquire() noexcept
101 {
103 }
104
105 void SAL_CALL DocumentEvents::release() noexcept
106 {
108 }
109
110 bool DocumentEvents::needsSynchronousNotification( std::u16string_view _rEventName )
111 {
112 const DocumentEventData* pEventData = lcl_getDocumentEventData();
113 while ( pEventData->pAsciiEventName )
114 {
115 if ( o3tl::equalsAscii( _rEventName, pEventData->pAsciiEventName ) )
116 return pEventData->bNeedsSyncNotify;
117 ++pEventData;
118 }
119
120 // this is an unknown event ... assume async notification
121 return false;
122 }
123
124 void SAL_CALL DocumentEvents::replaceByName( const OUString& Name, const Any& Element )
125 {
126 ::osl::MutexGuard aGuard( mrMutex );
127
128 DocumentEventsData::iterator elementPos = mrEventsData.find( Name );
129 if ( elementPos == mrEventsData.end() )
130 throw NoSuchElementException( Name, *this );
131
132 Sequence< PropertyValue > aEventDescriptor;
133 if ( Element.hasValue() && !( Element >>= aEventDescriptor ) )
134 throw IllegalArgumentException( Element.getValueTypeName(), *this, 2 );
135
136 // Weird enough, the event assignment UI has (well: had) the idea of using an empty "EventType"/"Script"
137 // to indicate the event descriptor should be reset, instead of just passing an empty event descriptor.
138 ::comphelper::NamedValueCollection aCheck( aEventDescriptor );
139 if ( aCheck.has( "EventType" ) )
140 {
141 OUString sEventType = aCheck.getOrDefault( "EventType", OUString() );
142 OSL_ENSURE( !sEventType.isEmpty(), "DocumentEvents::replaceByName: doing a reset via an empty EventType is weird!" );
143 if ( sEventType.isEmpty() )
144 aEventDescriptor.realloc( 0 );
145 }
146 if ( aCheck.has( "Script" ) )
147 {
148 OUString sScript = aCheck.getOrDefault( "Script", OUString() );
149 OSL_ENSURE( !sScript.isEmpty(), "DocumentEvents::replaceByName: doing a reset via an empty Script is weird!" );
150 if ( sScript.isEmpty() )
151 aEventDescriptor.realloc( 0 );
152 }
153
154 elementPos->second = aEventDescriptor;
155 }
156
157 Any SAL_CALL DocumentEvents::getByName( const OUString& Name )
158 {
159 ::osl::MutexGuard aGuard( mrMutex );
160
161 DocumentEventsData::const_iterator elementPos = mrEventsData.find( Name );
162 if ( elementPos == mrEventsData.end() )
163 throw NoSuchElementException( Name, *this );
164
165 Any aReturn;
166 const Sequence< PropertyValue >& rEventDesc( elementPos->second );
167 if ( rEventDesc.hasElements() )
168 aReturn <<= rEventDesc;
169 return aReturn;
170 }
171
172 Sequence< OUString > SAL_CALL DocumentEvents::getElementNames( )
173 {
174 ::osl::MutexGuard aGuard( mrMutex );
175
177 }
178
179 sal_Bool SAL_CALL DocumentEvents::hasByName( const OUString& Name )
180 {
181 ::osl::MutexGuard aGuard( mrMutex );
182
183 return mrEventsData.find( Name ) != mrEventsData.end();
184 }
185
187 {
188 return ::cppu::UnoType< Sequence< PropertyValue > >::get();
189 }
190
192 {
193 ::osl::MutexGuard aGuard( mrMutex );
194 return !mrEventsData.empty();
195 }
196
197} // namespace dbaccess
198
199/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
BaseContainerNodeSharedPtr & mrParent
bool has(const OUString &_rValueName) const
VALUE_TYPE getOrDefault(const OUString &_rValueName, const VALUE_TYPE &_rDefault) const
virtual void SAL_CALL acquire() SAL_NOEXCEPT SAL_OVERRIDE
virtual void SAL_CALL release() SAL_NOEXCEPT SAL_OVERRIDE
static bool needsSynchronousNotification(std::u16string_view _rEventName)
virtual sal_Bool SAL_CALL hasElements() override
virtual void SAL_CALL acquire() noexcept override
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
virtual void SAL_CALL release() noexcept override
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
DocumentEventsData & mrEventsData
::cppu::OWeakObject & mrParent
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
virtual ~DocumentEvents() override
virtual css::uno::Type SAL_CALL getElementType() override
virtual void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
DocumentEvents(::cppu::OWeakObject &_rParent, ::osl::Mutex &_rMutex, DocumentEventsData &_rEventsData)
const char * pAsciiEventName
bool bNeedsSyncNotify
css::uno::Sequence< typename M::key_type > mapKeysToSequence(M const &map)
Type
std::map< OUString, css::uno::Sequence< css::beans::PropertyValue > > DocumentEventsData
bool equalsAscii(std::u16string_view s1, std::string_view s2)
OUString Name
unsigned char sal_Bool
constexpr OUStringLiteral sScript
constexpr OUStringLiteral sEventType