LibreOffice Module sc (master) 1
eventuno.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 <eventuno.hxx>
21#include <miscuno.hxx>
22#include <docsh.hxx>
23#include <sheetevents.hxx>
24#include <unonames.hxx>
26#include <vcl/svapp.hxx>
27
28using namespace ::com::sun::star;
29
30SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj, "ScSheetEventsObj", "com.sun.star.document.Events" )
31
33 mpDocShell( pDocSh ),
34 mnTab( nT )
35{
36 mpDocShell->GetDocument().AddUnoObject(*this);
37}
38
40{
42
43 if (mpDocShell)
45}
46
48{
50 if ( rHint.GetId() == SfxHintId::Dying )
51 {
52 mpDocShell = nullptr;
53 }
54}
55
56static ScSheetEventId lcl_GetEventFromName( std::u16string_view aName )
57{
58 for (sal_Int32 nEvent=0; nEvent<static_cast<sal_Int32>(ScSheetEventId::COUNT); ++nEvent)
59 if ( aName == ScSheetEvents::GetEventName(static_cast<ScSheetEventId>(nEvent)) )
60 return static_cast<ScSheetEventId>(nEvent);
61
62 return ScSheetEventId::NOTFOUND; // not found
63}
64
65// XNameReplace
66
67void SAL_CALL ScSheetEventsObj::replaceByName( const OUString& aName, const uno::Any& aElement )
68{
69 SolarMutexGuard aGuard;
70 if (!mpDocShell)
71 throw uno::RuntimeException();
72
74 if (nEvent == ScSheetEventId::NOTFOUND)
75 throw container::NoSuchElementException();
76
77 std::unique_ptr<ScSheetEvents> pNewEvents(new ScSheetEvents);
79 if (pOldEvents)
80 *pNewEvents = *pOldEvents;
81
82 OUString aScript;
83 if ( aElement.hasValue() ) // empty Any -> reset event
84 {
85 uno::Sequence<beans::PropertyValue> aPropSeq;
86 if ( aElement >>= aPropSeq )
87 {
88 for (const beans::PropertyValue& rProp : std::as_const(aPropSeq))
89 {
90 if ( rProp.Name == SC_UNO_EVENTTYPE )
91 {
92 OUString aEventType;
93 if ( rProp.Value >>= aEventType )
94 {
95 // only "Script" is supported
96 if ( aEventType != SC_UNO_SCRIPT )
97 throw lang::IllegalArgumentException();
98 }
99 }
100 else if ( rProp.Name == SC_UNO_SCRIPT )
101 rProp.Value >>= aScript;
102 }
103 }
104 }
105 if (!aScript.isEmpty())
106 pNewEvents->SetScript( nEvent, &aScript );
107 else
108 pNewEvents->SetScript( nEvent, nullptr ); // reset
109
110 mpDocShell->GetDocument().SetSheetEvents( mnTab, std::move(pNewEvents) );
112}
113
114// XNameAccess
115
116uno::Any SAL_CALL ScSheetEventsObj::getByName( const OUString& aName )
117{
118 SolarMutexGuard aGuard;
120 if (nEvent == ScSheetEventId::NOTFOUND)
121 throw container::NoSuchElementException();
122
123 const OUString* pScript = nullptr;
124 if (mpDocShell)
125 {
127 if (pEvents)
128 pScript = pEvents->GetScript(nEvent);
129 }
130
131 uno::Any aRet;
132 if (pScript)
133 {
134 uno::Sequence<beans::PropertyValue> aPropSeq( comphelper::InitPropertySequence({
135 { "EventType", uno::Any( OUString("Script") ) },
136 { "Script", uno::Any( *pScript ) }
137 }));
138 aRet <<= aPropSeq;
139 }
140 // empty Any if nothing was set
141 return aRet;
142}
143
144uno::Sequence<OUString> SAL_CALL ScSheetEventsObj::getElementNames()
145{
146 auto aNames = uno::Sequence<OUString>(int(ScSheetEventId::COUNT));
147 auto pNames = aNames.getArray();
148 for (sal_Int32 nEvent=0; nEvent<int(ScSheetEventId::COUNT); ++nEvent)
149 pNames[nEvent] = ScSheetEvents::GetEventName(static_cast<ScSheetEventId>(nEvent));
150 return aNames;
151}
152
153sal_Bool SAL_CALL ScSheetEventsObj::hasByName( const OUString& aName )
154{
156 return (nEvent != ScSheetEventId::NOTFOUND);
157}
158
159// XElementAccess
160
162{
164}
165
167{
168 SolarMutexGuard aGuard;
169 if (mpDocShell)
170 return true;
171 return false;
172}
173
174/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetDocumentModified()
Definition: docsh.cxx:2998
const ScDocument & GetDocument() const
Definition: docsh.hxx:219
const ScSheetEvents * GetSheetEvents(SCTAB nTab) const
Definition: documen3.cxx:665
void SetSheetEvents(SCTAB nTab, std::unique_ptr< ScSheetEvents > pNew)
Definition: documen3.cxx:672
void RemoveUnoObject(SfxListener &rObject)
Definition: documen3.cxx:911
virtual css::uno::Type SAL_CALL getElementType() override
Definition: eventuno.cxx:161
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: eventuno.cxx:153
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: eventuno.cxx:144
virtual ~ScSheetEventsObj() override
Definition: eventuno.cxx:39
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: eventuno.cxx:47
virtual void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
Definition: eventuno.cxx:67
ScDocShell * mpDocShell
Definition: eventuno.hxx:37
virtual sal_Bool SAL_CALL hasElements() override
Definition: eventuno.cxx:166
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: eventuno.cxx:116
const OUString * GetScript(ScSheetEventId nEvent) const
Definition: sheetevents.cxx:99
static OUString GetEventName(ScSheetEventId nEvent)
Definition: sheetevents.cxx:24
SfxHintId GetId() const
static ScSheetEventId lcl_GetEventFromName(std::u16string_view aName)
Definition: eventuno.cxx:56
OUString aName
#define SC_SIMPLE_SERVICE_INFO(ClassName, ClassNameAscii, ServiceAscii)
Definition: miscuno.hxx:63
css::uno::Sequence< css::beans::PropertyValue > InitPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
ScSheetEventId
Definition: sheetevents.hxx:27
bool hasValue()
unsigned char sal_Bool
sal_Int16 SCTAB
Definition: types.hxx:22
constexpr OUStringLiteral SC_UNO_SCRIPT
Definition: unonames.hxx:700
constexpr OUStringLiteral SC_UNO_EVENTTYPE
Definition: unonames.hxx:699