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 <documentevents.hxx>
22 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <o3tl/string_view.hxx>
25 #include <osl/diagnose.h>
26 #include <comphelper/namedvaluecollection.hxx>
27 #include <comphelper/sequence.hxx>
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
;
42 struct DocumentEventData
44 OUString aAsciiEventName
;
45 bool bNeedsSyncNotify
;
48 constexpr DocumentEventData s_DocumentEventData
[] {
49 { u
"OnCreate"_ustr
, true },
50 { u
"OnLoadFinished"_ustr
, true },
51 { u
"OnNew"_ustr
, false }, // compatibility, see https://bz.apache.org/ooo/show_bug.cgi?id=46484
52 { u
"OnLoad"_ustr
, false }, // compatibility, see https://bz.apache.org/ooo/show_bug.cgi?id=46484
53 { u
"OnSaveAs"_ustr
, true },
54 { u
"OnSaveAsDone"_ustr
, false },
55 { u
"OnSaveAsFailed"_ustr
, false },
56 { u
"OnSave"_ustr
, true },
57 { u
"OnSaveDone"_ustr
, false },
58 { u
"OnSaveFailed"_ustr
, false },
59 { u
"OnSaveTo"_ustr
, true },
60 { u
"OnSaveToDone"_ustr
, false },
61 { u
"OnSaveToFailed"_ustr
, false },
62 { u
"OnPrepareUnload"_ustr
, true },
63 { u
"OnUnload"_ustr
, true },
64 { u
"OnFocus"_ustr
, false },
65 { u
"OnUnfocus"_ustr
, false },
66 { u
"OnModifyChanged"_ustr
, false },
67 { u
"OnViewCreated"_ustr
, false },
68 { u
"OnPrepareViewClosing"_ustr
, true },
69 { u
"OnViewClosed"_ustr
, false },
70 { u
"OnTitleChanged"_ustr
, false },
71 { u
"OnSubComponentOpened"_ustr
, false },
72 { u
"OnSubComponentClosed"_ustr
, false },
77 DocumentEvents::DocumentEvents( ::cppu::OWeakObject
& _rParent
, ::osl::Mutex
& _rMutex
, DocumentEventsData
& _rEventsData
)
78 :mrParent(_rParent
), mrMutex(_rMutex
), mrEventsData(_rEventsData
)
80 for (const auto & rEventData
: s_DocumentEventData
)
82 OUString sEventName
= rEventData
.aAsciiEventName
;
83 DocumentEventsData::const_iterator existingPos
= mrEventsData
.find( sEventName
);
84 if ( existingPos
== mrEventsData
.end() )
85 mrEventsData
[ sEventName
] = Sequence
< PropertyValue
>();
89 DocumentEvents::~DocumentEvents()
93 void SAL_CALL
DocumentEvents::acquire() noexcept
98 void SAL_CALL
DocumentEvents::release() noexcept
103 bool DocumentEvents::needsSynchronousNotification( std::u16string_view _rEventName
)
105 for (const auto & rEventData
: s_DocumentEventData
)
107 if ( _rEventName
== rEventData
.aAsciiEventName
)
108 return rEventData
.bNeedsSyncNotify
;
111 // this is an unknown event ... assume async notification
115 void SAL_CALL
DocumentEvents::replaceByName( const OUString
& Name
, const Any
& Element
)
117 ::osl::MutexGuard
aGuard( mrMutex
);
119 DocumentEventsData::iterator elementPos
= mrEventsData
.find( Name
);
120 if ( elementPos
== mrEventsData
.end() )
121 throw NoSuchElementException( Name
, *this );
123 Sequence
< PropertyValue
> aEventDescriptor
;
124 if ( Element
.hasValue() && !( Element
>>= aEventDescriptor
) )
125 throw IllegalArgumentException( Element
.getValueTypeName(), *this, 2 );
127 // Weird enough, the event assignment UI has (well: had) the idea of using an empty "EventType"/"Script"
128 // to indicate the event descriptor should be reset, instead of just passing an empty event descriptor.
129 ::comphelper::NamedValueCollection
aCheck( aEventDescriptor
);
130 if ( aCheck
.has( u
"EventType"_ustr
) )
132 OUString sEventType
= aCheck
.getOrDefault( u
"EventType"_ustr
, OUString() );
133 OSL_ENSURE( !sEventType
.isEmpty(), "DocumentEvents::replaceByName: doing a reset via an empty EventType is weird!" );
134 if ( sEventType
.isEmpty() )
135 aEventDescriptor
.realloc( 0 );
137 if ( aCheck
.has( u
"Script"_ustr
) )
139 OUString sScript
= aCheck
.getOrDefault( u
"Script"_ustr
, OUString() );
140 OSL_ENSURE( !sScript
.isEmpty(), "DocumentEvents::replaceByName: doing a reset via an empty Script is weird!" );
141 if ( sScript
.isEmpty() )
142 aEventDescriptor
.realloc( 0 );
145 elementPos
->second
= std::move(aEventDescriptor
);
148 Any SAL_CALL
DocumentEvents::getByName( const OUString
& Name
)
150 ::osl::MutexGuard
aGuard( mrMutex
);
152 DocumentEventsData::const_iterator elementPos
= mrEventsData
.find( Name
);
153 if ( elementPos
== mrEventsData
.end() )
154 throw NoSuchElementException( Name
, *this );
157 const Sequence
< PropertyValue
>& rEventDesc( elementPos
->second
);
158 if ( rEventDesc
.hasElements() )
159 aReturn
<<= rEventDesc
;
163 Sequence
< OUString
> SAL_CALL
DocumentEvents::getElementNames( )
165 ::osl::MutexGuard
aGuard( mrMutex
);
167 return comphelper::mapKeysToSequence( mrEventsData
);
170 sal_Bool SAL_CALL
DocumentEvents::hasByName( const OUString
& Name
)
172 ::osl::MutexGuard
aGuard( mrMutex
);
174 return mrEventsData
.find( Name
) != mrEventsData
.end();
177 Type SAL_CALL
DocumentEvents::getElementType( )
179 return ::cppu::UnoType
< Sequence
< PropertyValue
> >::get();
182 sal_Bool SAL_CALL
DocumentEvents::hasElements( )
184 ::osl::MutexGuard
aGuard( mrMutex
);
185 return !mrEventsData
.empty();
188 } // namespace dbaccess
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */