Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / unoobj / eventuno.cxx
blob5d2b140eb850729debc2893c8020ace337f89c3f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include "eventuno.hxx"
22 #include "miscuno.hxx"
23 #include "docsh.hxx"
24 #include "sheetevents.hxx"
25 #include "unonames.hxx"
26 #include <vcl/svapp.hxx>
28 using namespace ::com::sun::star;
30 //------------------------------------------------------------------------
32 SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj, "ScSheetEventsObj", "com.sun.star.document.Events" )
34 //------------------------------------------------------------------------
36 ScSheetEventsObj::ScSheetEventsObj(ScDocShell* pDocSh, SCTAB nT) :
37 mpDocShell( pDocSh ),
38 mnTab( nT )
40 mpDocShell->GetDocument()->AddUnoObject(*this);
43 ScSheetEventsObj::~ScSheetEventsObj()
45 if (mpDocShell)
46 mpDocShell->GetDocument()->RemoveUnoObject(*this);
49 void ScSheetEventsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
51 //! reference update
52 if ( rHint.ISA( SfxSimpleHint ) &&
53 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
55 mpDocShell = NULL;
59 static sal_Int32 lcl_GetEventFromName( const OUString& aName )
61 for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
62 if ( aName == ScSheetEvents::GetEventName(nEvent) )
63 return nEvent;
65 return -1; // not found
68 // XNameReplace
70 void SAL_CALL ScSheetEventsObj::replaceByName( const OUString& aName, const uno::Any& aElement )
71 throw(lang::IllegalArgumentException, container::NoSuchElementException,
72 lang::WrappedTargetException, uno::RuntimeException)
74 SolarMutexGuard aGuard;
75 if (!mpDocShell)
76 throw uno::RuntimeException();
78 sal_Int32 nEvent = lcl_GetEventFromName(aName);
79 if (nEvent < 0)
80 throw container::NoSuchElementException();
82 ScSheetEvents aNewEvents;
83 const ScSheetEvents* pOldEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab);
84 if (pOldEvents)
85 aNewEvents = *pOldEvents;
87 OUString aScript;
88 if ( aElement.hasValue() ) // empty Any -> reset event
90 uno::Sequence<beans::PropertyValue> aPropSeq;
91 if ( aElement >>= aPropSeq )
93 sal_Int32 nPropCount = aPropSeq.getLength();
94 for (sal_Int32 nPos=0; nPos<nPropCount; ++nPos)
96 const beans::PropertyValue& rProp = aPropSeq[nPos];
97 if ( rProp.Name.equalsAscii( SC_UNO_EVENTTYPE ) )
99 OUString aEventType;
100 if ( rProp.Value >>= aEventType )
102 // only "Script" is supported
103 if ( aEventType.compareToAscii( SC_UNO_SCRIPT ) != 0 )
104 throw lang::IllegalArgumentException();
107 else if ( rProp.Name.equalsAscii( SC_UNO_SCRIPT ) )
108 rProp.Value >>= aScript;
112 if (!aScript.isEmpty())
113 aNewEvents.SetScript( nEvent, &aScript );
114 else
115 aNewEvents.SetScript( nEvent, NULL ); // reset
117 mpDocShell->GetDocument()->SetSheetEvents( mnTab, &aNewEvents );
118 mpDocShell->SetDocumentModified();
121 // XNameAccess
123 uno::Any SAL_CALL ScSheetEventsObj::getByName( const OUString& aName )
124 throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
126 SolarMutexGuard aGuard;
127 sal_Int32 nEvent = lcl_GetEventFromName(aName);
128 if (nEvent < 0)
129 throw container::NoSuchElementException();
131 const OUString* pScript = NULL;
132 if (mpDocShell)
134 const ScSheetEvents* pEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab);
135 if (pEvents)
136 pScript = pEvents->GetScript(nEvent);
139 uno::Any aRet;
140 if (pScript)
142 uno::Sequence<beans::PropertyValue> aPropSeq( 2 );
143 aPropSeq[0] = beans::PropertyValue(
144 OUString("EventType"), -1,
145 uno::makeAny( OUString("Script") ), beans::PropertyState_DIRECT_VALUE );
146 aPropSeq[1] = beans::PropertyValue(
147 OUString("Script"), -1,
148 uno::makeAny( *pScript ), beans::PropertyState_DIRECT_VALUE );
149 aRet <<= aPropSeq;
151 // empty Any if nothing was set
152 return aRet;
155 uno::Sequence<OUString> SAL_CALL ScSheetEventsObj::getElementNames() throw(uno::RuntimeException)
157 SolarMutexGuard aGuard;
158 uno::Sequence<OUString> aNames(SC_SHEETEVENT_COUNT);
159 for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
160 aNames[nEvent] = ScSheetEvents::GetEventName(nEvent);
161 return aNames;
164 sal_Bool SAL_CALL ScSheetEventsObj::hasByName( const OUString& aName ) throw(uno::RuntimeException)
166 SolarMutexGuard aGuard;
167 sal_Int32 nEvent = lcl_GetEventFromName(aName);
168 return (nEvent >= 0);
171 // XElementAccess
173 uno::Type SAL_CALL ScSheetEventsObj::getElementType() throw(uno::RuntimeException)
175 SolarMutexGuard aGuard;
176 return getCppuType((uno::Sequence<beans::PropertyValue>*)0);
179 sal_Bool SAL_CALL ScSheetEventsObj::hasElements() throw(uno::RuntimeException)
181 SolarMutexGuard aGuard;
182 if (mpDocShell)
183 return sal_True;
184 return false;
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */