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 42 ScSheetEventsObj::ScSheetEventsObj(ScDocShell* pDocSh, SCTAB nT) : 43 mpDocShell( pDocSh ), 44 mnTab( nT ) 45 { 46 mpDocShell->GetDocument()->AddUnoObject(*this); 47 } 48 49 ScSheetEventsObj::~ScSheetEventsObj() 50 { 51 if (mpDocShell) 52 mpDocShell->GetDocument()->RemoveUnoObject(*this); 53 } 54 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 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 76 void SAL_CALL ScSheetEventsObj::replaceByName( const rtl::OUString& aName, const uno::Any& aElement ) 77 { 78 ScUnoGuard aGuard; 79 if (!mpDocShell) 80 throw uno::RuntimeException(); 81 82 sal_Int32 nEvent = lcl_GetEventFromName(aName); 83 if (nEvent < 0) 84 throw container::NoSuchElementException(); 85 86 ScSheetEvents aNewEvents; 87 const ScSheetEvents* pOldEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab); 88 if (pOldEvents) 89 aNewEvents = *pOldEvents; 90 91 rtl::OUString aScript; 92 if ( aElement.hasValue() ) // empty Any -> reset event 93 { 94 uno::Sequence<beans::PropertyValue> aPropSeq; 95 if ( aElement >>= aPropSeq ) 96 { 97 sal_Int32 nPropCount = aPropSeq.getLength(); 98 for (sal_Int32 nPos=0; nPos<nPropCount; ++nPos) 99 { 100 const beans::PropertyValue& rProp = aPropSeq[nPos]; 101 if ( rProp.Name.compareToAscii( SC_UNO_EVENTTYPE ) == 0 ) 102 { 103 rtl::OUString aEventType; 104 if ( rProp.Value >>= aEventType ) 105 { 106 // only "Script" is supported 107 if ( aEventType.compareToAscii( SC_UNO_SCRIPT ) != 0 ) 108 throw lang::IllegalArgumentException(); 109 } 110 } 111 else if ( rProp.Name.compareToAscii( SC_UNO_SCRIPT ) == 0 ) 112 rProp.Value >>= aScript; 113 } 114 } 115 } 116 if (aScript.getLength()) 117 aNewEvents.SetScript( nEvent, &aScript ); 118 else 119 aNewEvents.SetScript( nEvent, NULL ); // reset 120 121 mpDocShell->GetDocument()->SetSheetEvents( mnTab, &aNewEvents ); 122 mpDocShell->SetDocumentModified(); 123 } 124 125 // XNameAccess 126 127 uno::Any SAL_CALL ScSheetEventsObj::getByName( const rtl::OUString& aName ) 128 { 129 ScUnoGuard aGuard; 130 sal_Int32 nEvent = lcl_GetEventFromName(aName); 131 if (nEvent < 0) 132 throw container::NoSuchElementException(); 133 134 const rtl::OUString* pScript = NULL; 135 if (mpDocShell) 136 { 137 const ScSheetEvents* pEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab); 138 if (pEvents) 139 pScript = pEvents->GetScript(nEvent); 140 } 141 142 uno::Any aRet; 143 if (pScript) 144 { 145 uno::Sequence<beans::PropertyValue> aPropSeq( 2 ); 146 aPropSeq[0] = beans::PropertyValue( 147 rtl::OUString::createFromAscii("EventType"), -1, 148 uno::makeAny( rtl::OUString::createFromAscii("Script") ), beans::PropertyState_DIRECT_VALUE ); 149 aPropSeq[1] = beans::PropertyValue( 150 rtl::OUString::createFromAscii("Script"), -1, 151 uno::makeAny( *pScript ), beans::PropertyState_DIRECT_VALUE ); 152 aRet <<= aPropSeq; 153 } 154 // empty Any if nothing was set 155 return aRet; 156 } 157 158 uno::Sequence<rtl::OUString> SAL_CALL ScSheetEventsObj::getElementNames() 159 { 160 ScUnoGuard aGuard; 161 uno::Sequence<rtl::OUString> aNames(SC_SHEETEVENT_COUNT); 162 for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent) 163 aNames[nEvent] = ScSheetEvents::GetEventName(nEvent); 164 return aNames; 165 } 166 167 sal_Bool SAL_CALL ScSheetEventsObj::hasByName( const ::rtl::OUString& aName ) 168 { 169 ScUnoGuard aGuard; 170 sal_Int32 nEvent = lcl_GetEventFromName(aName); 171 return (nEvent >= 0); 172 } 173 174 // XElementAccess 175 176 uno::Type SAL_CALL ScSheetEventsObj::getElementType() 177 { 178 ScUnoGuard aGuard; 179 return getCppuType((uno::Sequence<beans::PropertyValue>*)0); 180 } 181 182 sal_Bool SAL_CALL ScSheetEventsObj::hasElements() 183 { 184 ScUnoGuard aGuard; 185 if (mpDocShell) 186 return sal_True; 187 return sal_False; 188 } 189