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 
28 using namespace ::com::sun::star;
29 
30 SC_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 
56 static 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 
67 void SAL_CALL ScSheetEventsObj::replaceByName( const OUString& aName, const uno::Any& aElement )
68 {
69  SolarMutexGuard aGuard;
70  if (!mpDocShell)
71  throw uno::RuntimeException();
72 
73  ScSheetEventId nEvent = lcl_GetEventFromName(aName);
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 
116 uno::Any SAL_CALL ScSheetEventsObj::getByName( const OUString& aName )
117 {
118  SolarMutexGuard aGuard;
119  ScSheetEventId nEvent = lcl_GetEventFromName(aName);
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 
144 uno::Sequence<OUString> SAL_CALL ScSheetEventsObj::getElementNames()
145 {
146  SolarMutexGuard aGuard;
147  auto aNames = uno::Sequence<OUString>(int(ScSheetEventId::COUNT));
148  for (sal_Int32 nEvent=0; nEvent<int(ScSheetEventId::COUNT); ++nEvent)
149  aNames[nEvent] = ScSheetEvents::GetEventName(static_cast<ScSheetEventId>(nEvent));
150  return aNames;
151 }
152 
153 sal_Bool SAL_CALL ScSheetEventsObj::hasByName( const OUString& aName )
154 {
155  SolarMutexGuard aGuard;
156  ScSheetEventId nEvent = lcl_GetEventFromName(aName);
157  return (nEvent != ScSheetEventId::NOTFOUND);
158 }
159 
160 // XElementAccess
161 
163 {
164  SolarMutexGuard aGuard;
166 }
167 
169 {
170  SolarMutexGuard aGuard;
171  if (mpDocShell)
172  return true;
173  return false;
174 }
175 
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool hasValue()
virtual ~ScSheetEventsObj() override
Definition: eventuno.cxx:39
void SetSheetEvents(SCTAB nTab, std::unique_ptr< ScSheetEvents > pNew)
Definition: documen3.cxx:666
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
virtual sal_Bool SAL_CALL hasElements() override
Definition: eventuno.cxx:168
void SetDocumentModified()
Definition: docsh.cxx:2820
#define SC_UNO_SCRIPT
Definition: unonames.hxx:688
SfxHintId GetId() const
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: eventuno.cxx:153
virtual css::uno::Type SAL_CALL getElementType() override
Definition: eventuno.cxx:162
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: eventuno.cxx:116
css::uno::Sequence< css::beans::PropertyValue > InitPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
virtual void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
Definition: eventuno.cxx:67
static OUString GetEventName(ScSheetEventId nEvent)
Definition: sheetevents.cxx:24
unsigned char sal_Bool
#define SC_SIMPLE_SERVICE_INFO(ClassName, ClassNameAscii, ServiceAscii)
Definition: miscuno.hxx:63
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: eventuno.cxx:47
const OUString * GetScript(ScSheetEventId nEvent) const
Definition: sheetevents.cxx:99
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: eventuno.cxx:144
#define SC_UNO_EVENTTYPE
Definition: unonames.hxx:687
const ScSheetEvents * GetSheetEvents(SCTAB nTab) const
Definition: documen3.cxx:659
void RemoveUnoObject(SfxListener &rObject)
Definition: documen3.cxx:906
const ScDocument & GetDocument() const
Definition: docsh.hxx:216
ScSheetEventId
Definition: sheetevents.hxx:27
ScDocShell * mpDocShell
Definition: eventuno.hxx:37
static ScSheetEventId lcl_GetEventFromName(std::u16string_view aName)
Definition: eventuno.cxx:56
sal_Int16 SCTAB
Definition: types.hxx:22