xref: /trunk/main/sc/source/ui/unoobj/eventuno.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2008 by Sun Microsystems, Inc.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * $RCSfile: tokenuno.cxx,v $
10  * $Revision: 1.6.108.8 $
11  *
12  * This file is part of OpenOffice.org.
13  *
14  * OpenOffice.org is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License version 3
16  * only, as published by the Free Software Foundation.
17  *
18  * OpenOffice.org is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU Lesser General Public License version 3 for more details
22  * (a copy is included in the LICENSE file that accompanied this code).
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * version 3 along with OpenOffice.org.  If not, see
26  * <http://www.openoffice.org/license.html>
27  * for a copy of the LGPLv3 License.
28  *
29  ************************************************************************/
30 
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
33 
34 #include "eventuno.hxx"
35 #include "miscuno.hxx"
36 #include "unoguard.hxx"
37 #include "docsh.hxx"
38 #include "sheetevents.hxx"
39 #include "unonames.hxx"
40 
41 using namespace ::com::sun::star;
42 
43 //------------------------------------------------------------------------
44 
45 SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj, "ScSheetEventsObj", "com.sun.star.document.Events" )
46 
47 //------------------------------------------------------------------------
48 
49 ScSheetEventsObj::ScSheetEventsObj(ScDocShell* pDocSh, SCTAB nT) :
50     mpDocShell( pDocSh ),
51     mnTab( nT )
52 {
53     mpDocShell->GetDocument()->AddUnoObject(*this);
54 }
55 
56 ScSheetEventsObj::~ScSheetEventsObj()
57 {
58     if (mpDocShell)
59         mpDocShell->GetDocument()->RemoveUnoObject(*this);
60 }
61 
62 void ScSheetEventsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
63 {
64     //! reference update
65     if ( rHint.ISA( SfxSimpleHint ) &&
66             ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
67     {
68         mpDocShell = NULL;
69     }
70 }
71 
72 sal_Int32 lcl_GetEventFromName( const rtl::OUString& aName )
73 {
74     for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
75         if ( aName == ScSheetEvents::GetEventName(nEvent) )
76             return nEvent;
77 
78     return -1;      // not found
79 }
80 
81 // XNameReplace
82 
83 void SAL_CALL ScSheetEventsObj::replaceByName( const rtl::OUString& aName, const uno::Any& aElement )
84     throw(lang::IllegalArgumentException, container::NoSuchElementException,
85           lang::WrappedTargetException, uno::RuntimeException)
86 {
87     ScUnoGuard aGuard;
88     if (!mpDocShell)
89         throw uno::RuntimeException();
90 
91     sal_Int32 nEvent = lcl_GetEventFromName(aName);
92     if (nEvent < 0)
93         throw container::NoSuchElementException();
94 
95     ScSheetEvents aNewEvents;
96     const ScSheetEvents* pOldEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab);
97     if (pOldEvents)
98         aNewEvents = *pOldEvents;
99 
100     rtl::OUString aScript;
101     if ( aElement.hasValue() )      // empty Any -> reset event
102     {
103         uno::Sequence<beans::PropertyValue> aPropSeq;
104         if ( aElement >>= aPropSeq )
105         {
106             sal_Int32 nPropCount = aPropSeq.getLength();
107             for (sal_Int32 nPos=0; nPos<nPropCount; ++nPos)
108             {
109         		const beans::PropertyValue& rProp = aPropSeq[nPos];
110                 if ( rProp.Name.compareToAscii( SC_UNO_EVENTTYPE ) == 0 )
111                 {
112                     rtl::OUString aEventType;
113                     if ( rProp.Value >>= aEventType )
114                     {
115                         // only "Script" is supported
116                         if ( aEventType.compareToAscii( SC_UNO_SCRIPT ) != 0 )
117                             throw lang::IllegalArgumentException();
118                     }
119                 }
120                 else if ( rProp.Name.compareToAscii( SC_UNO_SCRIPT ) == 0 )
121                     rProp.Value >>= aScript;
122             }
123         }
124     }
125     if (aScript.getLength())
126         aNewEvents.SetScript( nEvent, &aScript );
127     else
128         aNewEvents.SetScript( nEvent, NULL );       // reset
129 
130     mpDocShell->GetDocument()->SetSheetEvents( mnTab, &aNewEvents );
131     mpDocShell->SetDocumentModified();
132 }
133 
134 // XNameAccess
135 
136 uno::Any SAL_CALL ScSheetEventsObj::getByName( const rtl::OUString& aName )
137     throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
138 {
139     ScUnoGuard aGuard;
140     sal_Int32 nEvent = lcl_GetEventFromName(aName);
141     if (nEvent < 0)
142         throw container::NoSuchElementException();
143 
144     const rtl::OUString* pScript = NULL;
145     if (mpDocShell)
146     {
147         const ScSheetEvents* pEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab);
148         if (pEvents)
149             pScript = pEvents->GetScript(nEvent);
150     }
151 
152     uno::Any aRet;
153     if (pScript)
154     {
155         uno::Sequence<beans::PropertyValue> aPropSeq( 2 );
156         aPropSeq[0] = beans::PropertyValue(
157                         rtl::OUString::createFromAscii("EventType"), -1,
158                         uno::makeAny( rtl::OUString::createFromAscii("Script") ), beans::PropertyState_DIRECT_VALUE );
159         aPropSeq[1] = beans::PropertyValue(
160                         rtl::OUString::createFromAscii("Script"), -1,
161                         uno::makeAny( *pScript ), beans::PropertyState_DIRECT_VALUE );
162         aRet <<= aPropSeq;
163     }
164     // empty Any if nothing was set
165     return aRet;
166 }
167 
168 uno::Sequence<rtl::OUString> SAL_CALL ScSheetEventsObj::getElementNames() throw(uno::RuntimeException)
169 {
170     ScUnoGuard aGuard;
171     uno::Sequence<rtl::OUString> aNames(SC_SHEETEVENT_COUNT);
172     for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
173         aNames[nEvent] = ScSheetEvents::GetEventName(nEvent);
174     return aNames;
175 }
176 
177 sal_Bool SAL_CALL ScSheetEventsObj::hasByName( const ::rtl::OUString& aName ) throw(uno::RuntimeException)
178 {
179     ScUnoGuard aGuard;
180     sal_Int32 nEvent = lcl_GetEventFromName(aName);
181     return (nEvent >= 0);
182 }
183 
184 // XElementAccess
185 
186 uno::Type SAL_CALL ScSheetEventsObj::getElementType() throw(uno::RuntimeException)
187 {
188     ScUnoGuard aGuard;
189     return getCppuType((uno::Sequence<beans::PropertyValue>*)0);
190 }
191 
192 sal_Bool SAL_CALL ScSheetEventsObj::hasElements() throw(uno::RuntimeException)
193 {
194     ScUnoGuard aGuard;
195     if (mpDocShell)
196         return sal_True;
197     return sal_False;
198 }
199 
200 
201 
202