xref: /trunk/main/sc/source/ui/unoobj/eventuno.cxx (revision b3f79822)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 #include "eventuno.hxx"
28 #include "miscuno.hxx"
29 #include "unoguard.hxx"
30 #include "docsh.hxx"
31 #include "sheetevents.hxx"
32 #include "unonames.hxx"
33 
34 using namespace ::com::sun::star;
35 
36 //------------------------------------------------------------------------
37 
38 SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj, "ScSheetEventsObj", "com.sun.star.document.Events" )
39 
40 //------------------------------------------------------------------------
41 
ScSheetEventsObj(ScDocShell * pDocSh,SCTAB nT)42 ScSheetEventsObj::ScSheetEventsObj(ScDocShell* pDocSh, SCTAB nT) :
43     mpDocShell( pDocSh ),
44     mnTab( nT )
45 {
46     mpDocShell->GetDocument()->AddUnoObject(*this);
47 }
48 
~ScSheetEventsObj()49 ScSheetEventsObj::~ScSheetEventsObj()
50 {
51     if (mpDocShell)
52         mpDocShell->GetDocument()->RemoveUnoObject(*this);
53 }
54 
Notify(SfxBroadcaster &,const SfxHint & rHint)55 void ScSheetEventsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
56 {
57     //! reference update
58     if ( rHint.ISA( SfxSimpleHint ) &&
59             ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
60     {
61         mpDocShell = NULL;
62     }
63 }
64 
lcl_GetEventFromName(const rtl::OUString & aName)65 sal_Int32 lcl_GetEventFromName( const rtl::OUString& aName )
66 {
67     for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
68         if ( aName == ScSheetEvents::GetEventName(nEvent) )
69             return nEvent;
70 
71     return -1;      // not found
72 }
73 
74 // XNameReplace
75 
replaceByName(const rtl::OUString & aName,const uno::Any & aElement)76 void SAL_CALL ScSheetEventsObj::replaceByName( const rtl::OUString& aName, const uno::Any& aElement )
77     throw(lang::IllegalArgumentException, container::NoSuchElementException,
78           lang::WrappedTargetException, uno::RuntimeException)
79 {
80     ScUnoGuard aGuard;
81     if (!mpDocShell)
82         throw uno::RuntimeException();
83 
84     sal_Int32 nEvent = lcl_GetEventFromName(aName);
85     if (nEvent < 0)
86         throw container::NoSuchElementException();
87 
88     ScSheetEvents aNewEvents;
89     const ScSheetEvents* pOldEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab);
90     if (pOldEvents)
91         aNewEvents = *pOldEvents;
92 
93     rtl::OUString aScript;
94     if ( aElement.hasValue() )      // empty Any -> reset event
95     {
96         uno::Sequence<beans::PropertyValue> aPropSeq;
97         if ( aElement >>= aPropSeq )
98         {
99             sal_Int32 nPropCount = aPropSeq.getLength();
100             for (sal_Int32 nPos=0; nPos<nPropCount; ++nPos)
101             {
102         		const beans::PropertyValue& rProp = aPropSeq[nPos];
103                 if ( rProp.Name.compareToAscii( SC_UNO_EVENTTYPE ) == 0 )
104                 {
105                     rtl::OUString aEventType;
106                     if ( rProp.Value >>= aEventType )
107                     {
108                         // only "Script" is supported
109                         if ( aEventType.compareToAscii( SC_UNO_SCRIPT ) != 0 )
110                             throw lang::IllegalArgumentException();
111                     }
112                 }
113                 else if ( rProp.Name.compareToAscii( SC_UNO_SCRIPT ) == 0 )
114                     rProp.Value >>= aScript;
115             }
116         }
117     }
118     if (aScript.getLength())
119         aNewEvents.SetScript( nEvent, &aScript );
120     else
121         aNewEvents.SetScript( nEvent, NULL );       // reset
122 
123     mpDocShell->GetDocument()->SetSheetEvents( mnTab, &aNewEvents );
124     mpDocShell->SetDocumentModified();
125 }
126 
127 // XNameAccess
128 
getByName(const rtl::OUString & aName)129 uno::Any SAL_CALL ScSheetEventsObj::getByName( const rtl::OUString& aName )
130     throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
131 {
132     ScUnoGuard aGuard;
133     sal_Int32 nEvent = lcl_GetEventFromName(aName);
134     if (nEvent < 0)
135         throw container::NoSuchElementException();
136 
137     const rtl::OUString* pScript = NULL;
138     if (mpDocShell)
139     {
140         const ScSheetEvents* pEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab);
141         if (pEvents)
142             pScript = pEvents->GetScript(nEvent);
143     }
144 
145     uno::Any aRet;
146     if (pScript)
147     {
148         uno::Sequence<beans::PropertyValue> aPropSeq( 2 );
149         aPropSeq[0] = beans::PropertyValue(
150                         rtl::OUString::createFromAscii("EventType"), -1,
151                         uno::makeAny( rtl::OUString::createFromAscii("Script") ), beans::PropertyState_DIRECT_VALUE );
152         aPropSeq[1] = beans::PropertyValue(
153                         rtl::OUString::createFromAscii("Script"), -1,
154                         uno::makeAny( *pScript ), beans::PropertyState_DIRECT_VALUE );
155         aRet <<= aPropSeq;
156     }
157     // empty Any if nothing was set
158     return aRet;
159 }
160 
getElementNames()161 uno::Sequence<rtl::OUString> SAL_CALL ScSheetEventsObj::getElementNames() throw(uno::RuntimeException)
162 {
163     ScUnoGuard aGuard;
164     uno::Sequence<rtl::OUString> aNames(SC_SHEETEVENT_COUNT);
165     for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
166         aNames[nEvent] = ScSheetEvents::GetEventName(nEvent);
167     return aNames;
168 }
169 
hasByName(const::rtl::OUString & aName)170 sal_Bool SAL_CALL ScSheetEventsObj::hasByName( const ::rtl::OUString& aName ) throw(uno::RuntimeException)
171 {
172     ScUnoGuard aGuard;
173     sal_Int32 nEvent = lcl_GetEventFromName(aName);
174     return (nEvent >= 0);
175 }
176 
177 // XElementAccess
178 
getElementType()179 uno::Type SAL_CALL ScSheetEventsObj::getElementType() throw(uno::RuntimeException)
180 {
181     ScUnoGuard aGuard;
182     return getCppuType((uno::Sequence<beans::PropertyValue>*)0);
183 }
184 
hasElements()185 sal_Bool SAL_CALL ScSheetEventsObj::hasElements() throw(uno::RuntimeException)
186 {
187     ScUnoGuard aGuard;
188     if (mpDocShell)
189         return sal_True;
190     return sal_False;
191 }
192 
193 
194 
195