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 <eventuno.hxx>
21 #include <miscuno.hxx>
23 #include <sheetevents.hxx>
24 #include <unonames.hxx>
25 #include <comphelper/propertysequence.hxx>
26 #include <vcl/svapp.hxx>
28 using namespace ::com::sun::star
;
30 SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj
, u
"ScSheetEventsObj"_ustr
, u
"com.sun.star.document.Events"_ustr
)
32 ScSheetEventsObj::ScSheetEventsObj(ScDocShell
* pDocSh
, SCTAB nT
) :
36 mpDocShell
->GetDocument().AddUnoObject(*this);
39 ScSheetEventsObj::~ScSheetEventsObj()
44 mpDocShell
->GetDocument().RemoveUnoObject(*this);
47 void ScSheetEventsObj::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
50 if ( rHint
.GetId() == SfxHintId::Dying
)
56 static ScSheetEventId
lcl_GetEventFromName( std::u16string_view aName
)
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
);
62 return ScSheetEventId::NOTFOUND
; // not found
67 void SAL_CALL
ScSheetEventsObj::replaceByName( const OUString
& aName
, const uno::Any
& aElement
)
69 SolarMutexGuard aGuard
;
71 throw uno::RuntimeException();
73 ScSheetEventId nEvent
= lcl_GetEventFromName(aName
);
74 if (nEvent
== ScSheetEventId::NOTFOUND
)
75 throw container::NoSuchElementException();
77 std::unique_ptr
<ScSheetEvents
> pNewEvents(new ScSheetEvents
);
78 const ScSheetEvents
* pOldEvents
= mpDocShell
->GetDocument().GetSheetEvents(mnTab
);
80 *pNewEvents
= *pOldEvents
;
83 if ( aElement
.hasValue() ) // empty Any -> reset event
85 uno::Sequence
<beans::PropertyValue
> aPropSeq
;
86 if ( aElement
>>= aPropSeq
)
88 for (const beans::PropertyValue
& rProp
: aPropSeq
)
90 if ( rProp
.Name
== SC_UNO_EVENTTYPE
)
93 if ( rProp
.Value
>>= aEventType
)
95 // only "Script" is supported
96 if ( aEventType
!= SC_UNO_SCRIPT
)
97 throw lang::IllegalArgumentException();
100 else if ( rProp
.Name
== SC_UNO_SCRIPT
)
101 rProp
.Value
>>= aScript
;
105 if (!aScript
.isEmpty())
106 pNewEvents
->SetScript( nEvent
, &aScript
);
108 pNewEvents
->SetScript( nEvent
, nullptr ); // reset
110 mpDocShell
->GetDocument().SetSheetEvents( mnTab
, std::move(pNewEvents
) );
111 mpDocShell
->SetDocumentModified();
116 uno::Any SAL_CALL
ScSheetEventsObj::getByName( const OUString
& aName
)
118 SolarMutexGuard aGuard
;
119 ScSheetEventId nEvent
= lcl_GetEventFromName(aName
);
120 if (nEvent
== ScSheetEventId::NOTFOUND
)
121 throw container::NoSuchElementException();
123 const OUString
* pScript
= nullptr;
126 const ScSheetEvents
* pEvents
= mpDocShell
->GetDocument().GetSheetEvents(mnTab
);
128 pScript
= pEvents
->GetScript(nEvent
);
134 uno::Sequence
<beans::PropertyValue
> aPropSeq( comphelper::InitPropertySequence({
135 { "EventType", uno::Any( u
"Script"_ustr
) },
136 { "Script", uno::Any( *pScript
) }
140 // empty Any if nothing was set
144 uno::Sequence
<OUString
> SAL_CALL
ScSheetEventsObj::getElementNames()
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
));
153 sal_Bool SAL_CALL
ScSheetEventsObj::hasByName( const OUString
& aName
)
155 ScSheetEventId nEvent
= lcl_GetEventFromName(aName
);
156 return (nEvent
!= ScSheetEventId::NOTFOUND
);
161 uno::Type SAL_CALL
ScSheetEventsObj::getElementType()
163 return cppu::UnoType
<uno::Sequence
<beans::PropertyValue
>>::get();
166 sal_Bool SAL_CALL
ScSheetEventsObj::hasElements()
168 SolarMutexGuard aGuard
;
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */