xref: /trunk/main/svtools/source/uno/unoevent.cxx (revision 4c77d9855a97d670c1bf20bf0bbd0aa291258a3a)
15900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
35900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
45900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
55900e8ecSAndrew Rist  * distributed with this work for additional information
65900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
75900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
85900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
95900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
115900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
135900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
145900e8ecSAndrew Rist  * software distributed under the License is distributed on an
155900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
175900e8ecSAndrew Rist  * specific language governing permissions and limitations
185900e8ecSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
205900e8ecSAndrew Rist  *************************************************************/
215900e8ecSAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_svtools.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
26cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
27cdf0e10cSrcweir #include <tools/rtti.hxx>
28cdf0e10cSrcweir #include <tools/solar.h>
29cdf0e10cSrcweir #include <svtools/unoevent.hxx>
30cdf0e10cSrcweir #include <svl/macitem.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace ::com::sun::star;
33cdf0e10cSrcweir using namespace ::com::sun::star::uno;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using ::com::sun::star::container::NoSuchElementException;
36cdf0e10cSrcweir using ::com::sun::star::container::XNameReplace;
37cdf0e10cSrcweir using ::com::sun::star::lang::IllegalArgumentException;
38cdf0e10cSrcweir using ::com::sun::star::lang::WrappedTargetException;
39cdf0e10cSrcweir using ::com::sun::star::lang::XServiceInfo;
40cdf0e10cSrcweir using ::com::sun::star::beans::PropertyValue;
41cdf0e10cSrcweir using ::cppu::WeakImplHelper2;
42cdf0e10cSrcweir using ::rtl::OUString;
43cdf0e10cSrcweir using ::rtl::OUStringBuffer;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir const sal_Char sAPI_ServiceName[] = "com.sun.star.container.XNameReplace";
47cdf0e10cSrcweir const sal_Char sAPI_SvDetachedEventDescriptor[] = "SvDetachedEventDescriptor";
48cdf0e10cSrcweir 
49cdf0e10cSrcweir // SvBaseEventDescriptor
SvBaseEventDescriptor(const SvEventDescription * pSupportedMacroItems)50cdf0e10cSrcweir SvBaseEventDescriptor::SvBaseEventDescriptor( const SvEventDescription* pSupportedMacroItems ) :
51cdf0e10cSrcweir         sEventType(RTL_CONSTASCII_USTRINGPARAM("EventType")),
52cdf0e10cSrcweir         sMacroName(RTL_CONSTASCII_USTRINGPARAM("MacroName")),
53cdf0e10cSrcweir         sLibrary(RTL_CONSTASCII_USTRINGPARAM("Library")),
54cdf0e10cSrcweir         sStarBasic(RTL_CONSTASCII_USTRINGPARAM("StarBasic")),
55cdf0e10cSrcweir         sJavaScript(RTL_CONSTASCII_USTRINGPARAM("JavaScript")),
56cdf0e10cSrcweir         sScript(RTL_CONSTASCII_USTRINGPARAM("Script")),
57cdf0e10cSrcweir         sNone(RTL_CONSTASCII_USTRINGPARAM("None")),
58cdf0e10cSrcweir         sServiceName(RTL_CONSTASCII_USTRINGPARAM(sAPI_ServiceName)),
59cdf0e10cSrcweir         sEmpty(),
60cdf0e10cSrcweir         mpSupportedMacroItems(pSupportedMacroItems),
61cdf0e10cSrcweir         mnMacroItems(0)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     DBG_ASSERT(pSupportedMacroItems != NULL, "Need a list of supported events!");
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     for( ; mpSupportedMacroItems[mnMacroItems].mnEvent != 0; mnMacroItems++) ;
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 
~SvBaseEventDescriptor()69cdf0e10cSrcweir SvBaseEventDescriptor::~SvBaseEventDescriptor()
70cdf0e10cSrcweir {
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
replaceByName(const OUString & rName,const Any & rElement)73cdf0e10cSrcweir void SvBaseEventDescriptor::replaceByName(
74cdf0e10cSrcweir     const OUString& rName,
75cdf0e10cSrcweir     const Any& rElement )
76cdf0e10cSrcweir     throw(
77cdf0e10cSrcweir         IllegalArgumentException,
78cdf0e10cSrcweir         NoSuchElementException,
79cdf0e10cSrcweir         WrappedTargetException,
80cdf0e10cSrcweir         RuntimeException)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     sal_uInt16 nMacroID = getMacroID(rName);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     // error checking
85cdf0e10cSrcweir     if (0 == nMacroID)
86cdf0e10cSrcweir         throw NoSuchElementException();
87cdf0e10cSrcweir     if (rElement.getValueType() != getElementType())
88cdf0e10cSrcweir         throw IllegalArgumentException();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     // get sequence
91cdf0e10cSrcweir     Sequence<PropertyValue> aSequence;
92cdf0e10cSrcweir     rElement >>= aSequence;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     // perform replace (in subclass)
95cdf0e10cSrcweir     SvxMacro aMacro(sEmpty,sEmpty);
96cdf0e10cSrcweir     getMacroFromAny(aMacro, rElement);
97cdf0e10cSrcweir     replaceByName(nMacroID, aMacro);
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
getByName(const OUString & rName)100cdf0e10cSrcweir Any SvBaseEventDescriptor::getByName(
101cdf0e10cSrcweir     const OUString& rName )
102cdf0e10cSrcweir     throw(
103cdf0e10cSrcweir         NoSuchElementException,
104cdf0e10cSrcweir         WrappedTargetException,
105cdf0e10cSrcweir         RuntimeException)
106cdf0e10cSrcweir {
107cdf0e10cSrcweir     sal_uInt16 nMacroID = getMacroID(rName);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     // error checking
110cdf0e10cSrcweir     if (0 == nMacroID)
111cdf0e10cSrcweir         throw NoSuchElementException();
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     // perform get (in subclass)
114cdf0e10cSrcweir     Any aAny;
115cdf0e10cSrcweir     SvxMacro aMacro( sEmpty, sEmpty );
116cdf0e10cSrcweir     getByName(aMacro, nMacroID);
117cdf0e10cSrcweir     getAnyFromMacro(aAny, aMacro);
118cdf0e10cSrcweir     return aAny;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
getElementNames()121cdf0e10cSrcweir Sequence<OUString> SvBaseEventDescriptor::getElementNames()
122cdf0e10cSrcweir     throw(RuntimeException)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir     // create and fill sequence
125cdf0e10cSrcweir     Sequence<OUString> aSequence(mnMacroItems);
126cdf0e10cSrcweir     for( sal_Int16 i = 0; i < mnMacroItems; i++)
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         aSequence[i] = OUString::createFromAscii( mpSupportedMacroItems[i].mpEventName );
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     return aSequence;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
hasByName(const OUString & rName)134cdf0e10cSrcweir sal_Bool SvBaseEventDescriptor::hasByName(
135cdf0e10cSrcweir     const OUString& rName )
136cdf0e10cSrcweir     throw(RuntimeException)
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     sal_uInt16 nMacroID = getMacroID(rName);
139cdf0e10cSrcweir     return (nMacroID != 0);
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
getElementType()142cdf0e10cSrcweir Type SvBaseEventDescriptor::getElementType()
143cdf0e10cSrcweir     throw(RuntimeException)
144cdf0e10cSrcweir {
145cdf0e10cSrcweir     return ::getCppuType((Sequence<PropertyValue> *)0);
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
hasElements()148cdf0e10cSrcweir sal_Bool SvBaseEventDescriptor::hasElements()
149cdf0e10cSrcweir     throw(RuntimeException)
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     return mnMacroItems != 0;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
supportsService(const OUString & rServiceName)154cdf0e10cSrcweir sal_Bool SvBaseEventDescriptor::supportsService(const OUString& rServiceName)
155cdf0e10cSrcweir     throw(RuntimeException)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir     return sServiceName.equals(rServiceName);
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
getSupportedServiceNames(void)160cdf0e10cSrcweir Sequence<OUString> SvBaseEventDescriptor::getSupportedServiceNames(void)
161cdf0e10cSrcweir     throw(RuntimeException)
162cdf0e10cSrcweir {
163cdf0e10cSrcweir     Sequence<OUString> aSequence(1);
164cdf0e10cSrcweir     aSequence[0] = sServiceName;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     return aSequence;
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
mapNameToEventID(const OUString & rName) const169cdf0e10cSrcweir sal_uInt16 SvBaseEventDescriptor::mapNameToEventID(const OUString& rName) const
170cdf0e10cSrcweir {
171cdf0e10cSrcweir     // iterate over known event names
172cdf0e10cSrcweir     for(sal_Int16 i = 0; i < mnMacroItems; i++)
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         if (0 == rName.compareToAscii(mpSupportedMacroItems[i].mpEventName))
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             return mpSupportedMacroItems[i].mnEvent;
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     // not found -> return zero
181cdf0e10cSrcweir     return 0;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
mapEventIDToName(sal_uInt16 nPoolID) const184cdf0e10cSrcweir OUString SvBaseEventDescriptor::mapEventIDToName(sal_uInt16 nPoolID) const
185cdf0e10cSrcweir {
186cdf0e10cSrcweir     // iterate over known event IDs
187cdf0e10cSrcweir     for(sal_Int16 i = 0; i < mnMacroItems; i++)
188cdf0e10cSrcweir     {
189cdf0e10cSrcweir         if (nPoolID == mpSupportedMacroItems[i].mnEvent)
190cdf0e10cSrcweir         {
191cdf0e10cSrcweir             return OUString::createFromAscii(mpSupportedMacroItems[i].mpEventName);
192cdf0e10cSrcweir         }
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     // not found -> return empty string
196cdf0e10cSrcweir     return OUString();
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
getMacroID(const OUString & rName) const199cdf0e10cSrcweir sal_uInt16 SvBaseEventDescriptor::getMacroID(const OUString& rName) const
200cdf0e10cSrcweir {
201cdf0e10cSrcweir     return mapNameToEventID(rName);
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
getAnyFromMacro(Any & rAny,const SvxMacro & rMacro)204cdf0e10cSrcweir void SvBaseEventDescriptor::getAnyFromMacro(Any& rAny,
205cdf0e10cSrcweir                                        const SvxMacro& rMacro)
206cdf0e10cSrcweir {
207cdf0e10cSrcweir     sal_Bool bRetValueOK = sal_False;   // do we have a ret value?
208cdf0e10cSrcweir 
209cdf0e10cSrcweir     if (rMacro.HasMacro())
210cdf0e10cSrcweir     {
211cdf0e10cSrcweir         switch (rMacro.GetScriptType())
212cdf0e10cSrcweir         {
213cdf0e10cSrcweir             case STARBASIC:
214cdf0e10cSrcweir             {
215cdf0e10cSrcweir                 // create sequence
216cdf0e10cSrcweir                 Sequence<PropertyValue> aSequence(3);
217cdf0e10cSrcweir                 Any aTmp;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir                 // create type
220cdf0e10cSrcweir                 PropertyValue aTypeValue;
221cdf0e10cSrcweir                 aTypeValue.Name = sEventType;
222cdf0e10cSrcweir                 aTmp <<= sStarBasic;
223cdf0e10cSrcweir                 aTypeValue.Value = aTmp;
224cdf0e10cSrcweir                 aSequence[0] = aTypeValue;
225cdf0e10cSrcweir 
226cdf0e10cSrcweir                 // macro name
227cdf0e10cSrcweir                 PropertyValue aNameValue;
228cdf0e10cSrcweir                 aNameValue.Name = sMacroName;
229cdf0e10cSrcweir                 OUString sNameTmp(rMacro.GetMacName());
230cdf0e10cSrcweir                 aTmp <<= sNameTmp;
231cdf0e10cSrcweir                 aNameValue.Value = aTmp;
232cdf0e10cSrcweir                 aSequence[1] = aNameValue;
233cdf0e10cSrcweir 
234cdf0e10cSrcweir                 // library name
235cdf0e10cSrcweir                 PropertyValue aLibValue;
236cdf0e10cSrcweir                 aLibValue.Name = sLibrary;
237cdf0e10cSrcweir                 OUString sLibTmp(rMacro.GetLibName());
238cdf0e10cSrcweir                 aTmp <<= sLibTmp;
239cdf0e10cSrcweir                 aLibValue.Value = aTmp;
240cdf0e10cSrcweir                 aSequence[2] = aLibValue;
241cdf0e10cSrcweir 
242cdf0e10cSrcweir                 rAny <<= aSequence;
243cdf0e10cSrcweir                 bRetValueOK = sal_True;
244cdf0e10cSrcweir                 break;
245cdf0e10cSrcweir             }
246cdf0e10cSrcweir             case EXTENDED_STYPE:
247cdf0e10cSrcweir             {
248cdf0e10cSrcweir                 // create sequence
249cdf0e10cSrcweir                 Sequence<PropertyValue> aSequence(2);
250cdf0e10cSrcweir                 Any aTmp;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir                 // create type
253cdf0e10cSrcweir                 PropertyValue aTypeValue;
254cdf0e10cSrcweir                 aTypeValue.Name = sEventType;
255cdf0e10cSrcweir                 aTmp <<= sScript;
256cdf0e10cSrcweir                 aTypeValue.Value = aTmp;
257cdf0e10cSrcweir                 aSequence[0] = aTypeValue;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir                 // macro name
260cdf0e10cSrcweir                 PropertyValue aNameValue;
261cdf0e10cSrcweir                 aNameValue.Name = sScript;
262cdf0e10cSrcweir                 OUString sNameTmp(rMacro.GetMacName());
263cdf0e10cSrcweir                 aTmp <<= sNameTmp;
264cdf0e10cSrcweir                 aNameValue.Value = aTmp;
265cdf0e10cSrcweir                 aSequence[1] = aNameValue;
266cdf0e10cSrcweir 
267cdf0e10cSrcweir                 rAny <<= aSequence;
268cdf0e10cSrcweir                 bRetValueOK = sal_True;
269cdf0e10cSrcweir                 break;
270cdf0e10cSrcweir             }
271cdf0e10cSrcweir             case JAVASCRIPT:
272cdf0e10cSrcweir             default:
273cdf0e10cSrcweir                 DBG_ERROR("not implemented");
274cdf0e10cSrcweir         }
275cdf0e10cSrcweir     }
276cdf0e10cSrcweir     // else: bRetValueOK not set
277cdf0e10cSrcweir 
278cdf0e10cSrcweir     // if we don't have a return value, make an empty one
279cdf0e10cSrcweir     if (! bRetValueOK)
280cdf0e10cSrcweir     {
281cdf0e10cSrcweir         // create "None" macro
282cdf0e10cSrcweir         Sequence<PropertyValue> aSequence(1);
283cdf0e10cSrcweir 
284cdf0e10cSrcweir         PropertyValue aKindValue;
285cdf0e10cSrcweir         aKindValue.Name = sEventType;
286cdf0e10cSrcweir         Any aTmp;
287cdf0e10cSrcweir         aTmp <<= sNone;
288cdf0e10cSrcweir         aKindValue.Value = aTmp;
289cdf0e10cSrcweir         aSequence[0] = aKindValue;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir         rAny <<= aSequence;
292cdf0e10cSrcweir         bRetValueOK = sal_True;
293cdf0e10cSrcweir     }
294cdf0e10cSrcweir }
295cdf0e10cSrcweir 
296cdf0e10cSrcweir 
getMacroFromAny(SvxMacro & rMacro,const Any & rAny)297cdf0e10cSrcweir void SvBaseEventDescriptor::getMacroFromAny(
298cdf0e10cSrcweir     SvxMacro& rMacro,
299cdf0e10cSrcweir     const Any& rAny)
300cdf0e10cSrcweir         throw ( IllegalArgumentException )
301cdf0e10cSrcweir {
302cdf0e10cSrcweir     // get sequence
303cdf0e10cSrcweir     Sequence<PropertyValue> aSequence;
304cdf0e10cSrcweir     rAny >>= aSequence;
305cdf0e10cSrcweir 
306cdf0e10cSrcweir     // process ...
307cdf0e10cSrcweir     sal_Bool bTypeOK = sal_False;
308cdf0e10cSrcweir     sal_Bool bNone = sal_False; // true if EventType=="None"
309cdf0e10cSrcweir     enum ScriptType eType = EXTENDED_STYPE;
310cdf0e10cSrcweir     OUString sScriptVal;
311cdf0e10cSrcweir     OUString sMacroVal;
312cdf0e10cSrcweir     OUString sLibVal;
313cdf0e10cSrcweir     sal_Int32 nCount = aSequence.getLength();
314cdf0e10cSrcweir     for (sal_Int32 i = 0; i < nCount; i++)
315cdf0e10cSrcweir     {
316cdf0e10cSrcweir         PropertyValue& aValue = aSequence[i];
317cdf0e10cSrcweir         if (aValue.Name.equals(sEventType))
318cdf0e10cSrcweir         {
319cdf0e10cSrcweir             OUString sTmp;
320cdf0e10cSrcweir             aValue.Value >>= sTmp;
321cdf0e10cSrcweir             if (sTmp.equals(sStarBasic))
322cdf0e10cSrcweir             {
323cdf0e10cSrcweir                 eType = STARBASIC;
324cdf0e10cSrcweir                 bTypeOK = sal_True;
325cdf0e10cSrcweir             }
326cdf0e10cSrcweir             else if (sTmp.equals(sJavaScript))
327cdf0e10cSrcweir             {
328cdf0e10cSrcweir                 eType = JAVASCRIPT;
329cdf0e10cSrcweir                 bTypeOK = sal_True;
330cdf0e10cSrcweir             }
331cdf0e10cSrcweir             else if (sTmp.equals(sScript))
332cdf0e10cSrcweir             {
333cdf0e10cSrcweir                 eType = EXTENDED_STYPE;
334cdf0e10cSrcweir                 bTypeOK = sal_True;
335cdf0e10cSrcweir             }
336cdf0e10cSrcweir             else if (sTmp.equals(sNone))
337cdf0e10cSrcweir             {
338cdf0e10cSrcweir                 bNone = sal_True;
339cdf0e10cSrcweir                 bTypeOK = sal_True;
340cdf0e10cSrcweir             }
341cdf0e10cSrcweir             // else: unknown script type
342cdf0e10cSrcweir         }
343cdf0e10cSrcweir         else if (aValue.Name.equals(sMacroName))
344cdf0e10cSrcweir         {
345cdf0e10cSrcweir             aValue.Value >>= sMacroVal;
346cdf0e10cSrcweir         }
347cdf0e10cSrcweir         else if (aValue.Name.equals(sLibrary))
348cdf0e10cSrcweir         {
349cdf0e10cSrcweir             aValue.Value >>= sLibVal;
350cdf0e10cSrcweir         }
351cdf0e10cSrcweir         else if (aValue.Name.equals(sScript))
352cdf0e10cSrcweir         {
353cdf0e10cSrcweir             aValue.Value >>= sScriptVal;
354cdf0e10cSrcweir         }
355cdf0e10cSrcweir         // else: unknown PropertyValue -> ignore
356cdf0e10cSrcweir     }
357cdf0e10cSrcweir 
358cdf0e10cSrcweir     if (bTypeOK)
359cdf0e10cSrcweir     {
360cdf0e10cSrcweir         if (bNone)
361cdf0e10cSrcweir         {
362cdf0e10cSrcweir             // return empty macro
363cdf0e10cSrcweir             rMacro = SvxMacro( sEmpty, sEmpty );
364cdf0e10cSrcweir         }
365cdf0e10cSrcweir         else
366cdf0e10cSrcweir         {
367cdf0e10cSrcweir             if (eType == STARBASIC)
368cdf0e10cSrcweir             {
369cdf0e10cSrcweir                 // create macro and return
370cdf0e10cSrcweir                 SvxMacro aMacro(sMacroVal, sLibVal, eType);
371cdf0e10cSrcweir                 rMacro = aMacro;
372cdf0e10cSrcweir             }
373cdf0e10cSrcweir             else if (eType == EXTENDED_STYPE)
374cdf0e10cSrcweir             {
375cdf0e10cSrcweir                 SvxMacro aMacro(sScriptVal, sScript);
376cdf0e10cSrcweir                 rMacro = aMacro;
377cdf0e10cSrcweir             }
378cdf0e10cSrcweir             else
379cdf0e10cSrcweir             {
380cdf0e10cSrcweir                 // we can't process type: abort
381cdf0e10cSrcweir                 // TODO: JavaScript macros
382cdf0e10cSrcweir                 throw IllegalArgumentException();
383cdf0e10cSrcweir             }
384cdf0e10cSrcweir         }
385cdf0e10cSrcweir     }
386cdf0e10cSrcweir     else
387cdf0e10cSrcweir     {
388cdf0e10cSrcweir         // no valid type: abort
389cdf0e10cSrcweir         throw IllegalArgumentException();
390cdf0e10cSrcweir     }
391cdf0e10cSrcweir }
392cdf0e10cSrcweir 
393cdf0e10cSrcweir 
394cdf0e10cSrcweir // SvEventDescriptor
SvEventDescriptor(XInterface & rParent,const SvEventDescription * pSupportedMacroItems)395cdf0e10cSrcweir SvEventDescriptor::SvEventDescriptor(
396cdf0e10cSrcweir     XInterface& rParent,
397cdf0e10cSrcweir     const SvEventDescription* pSupportedMacroItems) :
398cdf0e10cSrcweir         SvBaseEventDescriptor(pSupportedMacroItems),
399cdf0e10cSrcweir         xParentRef(&rParent)
400cdf0e10cSrcweir {
401cdf0e10cSrcweir }
402cdf0e10cSrcweir 
403cdf0e10cSrcweir 
~SvEventDescriptor()404cdf0e10cSrcweir SvEventDescriptor::~SvEventDescriptor()
405cdf0e10cSrcweir {
406cdf0e10cSrcweir     // automatically release xParentRef !
407cdf0e10cSrcweir }
408cdf0e10cSrcweir 
replaceByName(const sal_uInt16 nEvent,const SvxMacro & rMacro)409cdf0e10cSrcweir void SvEventDescriptor::replaceByName(
410cdf0e10cSrcweir     const sal_uInt16 nEvent,
411cdf0e10cSrcweir     const SvxMacro& rMacro)
412cdf0e10cSrcweir         throw(
413cdf0e10cSrcweir             IllegalArgumentException,
414cdf0e10cSrcweir             NoSuchElementException,
415cdf0e10cSrcweir             WrappedTargetException,
416cdf0e10cSrcweir             RuntimeException)
417cdf0e10cSrcweir {
418cdf0e10cSrcweir     SvxMacroItem aItem(getMacroItemWhich());
419cdf0e10cSrcweir     aItem.SetMacroTable(getMacroItem().GetMacroTable());
420cdf0e10cSrcweir     aItem.SetMacro(nEvent, rMacro);
421cdf0e10cSrcweir     setMacroItem(aItem);
422cdf0e10cSrcweir }
423cdf0e10cSrcweir 
getByName(SvxMacro & rMacro,const sal_uInt16 nEvent)424cdf0e10cSrcweir void SvEventDescriptor::getByName(
425cdf0e10cSrcweir     SvxMacro& rMacro,
426cdf0e10cSrcweir     const sal_uInt16 nEvent )
427cdf0e10cSrcweir         throw(
428cdf0e10cSrcweir             NoSuchElementException,
429cdf0e10cSrcweir             WrappedTargetException,
430cdf0e10cSrcweir             RuntimeException)
431cdf0e10cSrcweir {
432cdf0e10cSrcweir     const SvxMacroItem& rItem = getMacroItem();
433cdf0e10cSrcweir     if( rItem.HasMacro( nEvent ) )
434cdf0e10cSrcweir         rMacro = rItem.GetMacro(nEvent);
435cdf0e10cSrcweir     else
436cdf0e10cSrcweir     {
437cdf0e10cSrcweir         SvxMacro aEmptyMacro(sEmpty, sEmpty);
438cdf0e10cSrcweir         rMacro = aEmptyMacro;
439cdf0e10cSrcweir     }
440cdf0e10cSrcweir }
441cdf0e10cSrcweir 
442cdf0e10cSrcweir 
443cdf0e10cSrcweir // SvDetachedEventDescriptor
SvDetachedEventDescriptor(const SvEventDescription * pSupportedMacroItems)444cdf0e10cSrcweir SvDetachedEventDescriptor::SvDetachedEventDescriptor(
445cdf0e10cSrcweir     const SvEventDescription* pSupportedMacroItems) :
446cdf0e10cSrcweir     SvBaseEventDescriptor(pSupportedMacroItems),
447cdf0e10cSrcweir     sImplName(RTL_CONSTASCII_USTRINGPARAM(sAPI_SvDetachedEventDescriptor))
448cdf0e10cSrcweir {
449cdf0e10cSrcweir     // allocate aMacros
450cdf0e10cSrcweir     aMacros = new SvxMacro*[mnMacroItems];
451cdf0e10cSrcweir 
452cdf0e10cSrcweir     // ... and initialize
453cdf0e10cSrcweir     for(sal_Int16 i = 0; i < mnMacroItems; i++)
454cdf0e10cSrcweir     {
455cdf0e10cSrcweir         aMacros[i] = NULL;
456cdf0e10cSrcweir     }
457cdf0e10cSrcweir }
458cdf0e10cSrcweir 
~SvDetachedEventDescriptor()459cdf0e10cSrcweir SvDetachedEventDescriptor::~SvDetachedEventDescriptor()
460cdf0e10cSrcweir {
461cdf0e10cSrcweir     // delete contents of aMacros
462cdf0e10cSrcweir     for(sal_Int16 i = 0; i < mnMacroItems; i++)
463cdf0e10cSrcweir     {
464cdf0e10cSrcweir         if (NULL != aMacros[i])
465cdf0e10cSrcweir             delete aMacros[i];
466cdf0e10cSrcweir     }
467cdf0e10cSrcweir 
468cdf0e10cSrcweir     delete [] aMacros;
469cdf0e10cSrcweir }
470cdf0e10cSrcweir 
getIndex(const sal_uInt16 nID) const471cdf0e10cSrcweir sal_Int16 SvDetachedEventDescriptor::getIndex(const sal_uInt16 nID) const
472cdf0e10cSrcweir {
473cdf0e10cSrcweir     // iterate over supported events
474cdf0e10cSrcweir     sal_Int16 nIndex = 0;
475cdf0e10cSrcweir     while ( (mpSupportedMacroItems[nIndex].mnEvent != nID) &&
476cdf0e10cSrcweir             (mpSupportedMacroItems[nIndex].mnEvent != 0)      )
477cdf0e10cSrcweir     {
478cdf0e10cSrcweir         nIndex++;
479cdf0e10cSrcweir     }
480cdf0e10cSrcweir     return (mpSupportedMacroItems[nIndex].mnEvent == nID) ? nIndex : -1;
481cdf0e10cSrcweir }
482cdf0e10cSrcweir 
getImplementationName()483cdf0e10cSrcweir OUString SvDetachedEventDescriptor::getImplementationName()
484cdf0e10cSrcweir     throw( ::com::sun::star::uno::RuntimeException )
485cdf0e10cSrcweir {
486cdf0e10cSrcweir     return sImplName;
487cdf0e10cSrcweir }
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 
replaceByName(const sal_uInt16 nEvent,const SvxMacro & rMacro)490cdf0e10cSrcweir void SvDetachedEventDescriptor::replaceByName(
491cdf0e10cSrcweir     const sal_uInt16 nEvent,
492cdf0e10cSrcweir     const SvxMacro& rMacro)
493cdf0e10cSrcweir     throw(
494cdf0e10cSrcweir         IllegalArgumentException,
495cdf0e10cSrcweir         NoSuchElementException,
496cdf0e10cSrcweir         WrappedTargetException,
497cdf0e10cSrcweir         RuntimeException)
498cdf0e10cSrcweir {
499cdf0e10cSrcweir     sal_Int16 nIndex = getIndex(nEvent);
500cdf0e10cSrcweir     if (-1 == nIndex)
501cdf0e10cSrcweir         throw IllegalArgumentException();
502cdf0e10cSrcweir 
503cdf0e10cSrcweir     aMacros[nIndex] = new SvxMacro(rMacro.GetMacName(), rMacro.GetLibName(),
504cdf0e10cSrcweir                                    rMacro.GetScriptType() );
505cdf0e10cSrcweir }
506cdf0e10cSrcweir 
507cdf0e10cSrcweir 
getByName(SvxMacro & rMacro,const sal_uInt16 nEvent)508cdf0e10cSrcweir void SvDetachedEventDescriptor::getByName(
509cdf0e10cSrcweir     SvxMacro& rMacro,
510cdf0e10cSrcweir     const sal_uInt16 nEvent )
511cdf0e10cSrcweir     throw(
512cdf0e10cSrcweir         NoSuchElementException,
513cdf0e10cSrcweir         WrappedTargetException,
514cdf0e10cSrcweir         RuntimeException)
515cdf0e10cSrcweir {
516cdf0e10cSrcweir     sal_Int16 nIndex = getIndex(nEvent);
517cdf0e10cSrcweir     if (-1 == nIndex )
518cdf0e10cSrcweir         throw NoSuchElementException();
519cdf0e10cSrcweir 
520cdf0e10cSrcweir     if( aMacros[nIndex] )
521cdf0e10cSrcweir         rMacro = (*aMacros[nIndex]);
522cdf0e10cSrcweir }
523cdf0e10cSrcweir 
hasByName(const sal_uInt16 nEvent) const524cdf0e10cSrcweir sal_Bool SvDetachedEventDescriptor::hasByName(
525*4c77d985Smseidel     const sal_uInt16 nEvent ) const     // item ID of event
526cdf0e10cSrcweir         throw(IllegalArgumentException)
527cdf0e10cSrcweir {
528cdf0e10cSrcweir     sal_Int16 nIndex = getIndex(nEvent);
529cdf0e10cSrcweir     if (-1 == nIndex)
530cdf0e10cSrcweir         throw IllegalArgumentException();
531cdf0e10cSrcweir 
532cdf0e10cSrcweir     return (NULL == aMacros[nIndex]) ? sal_False : aMacros[nIndex]->HasMacro();
533cdf0e10cSrcweir }
534cdf0e10cSrcweir 
535cdf0e10cSrcweir 
536cdf0e10cSrcweir // SvMacroTableEventDescriptor
SvMacroTableEventDescriptor(const SvEventDescription * pSupportedMacroItems)537cdf0e10cSrcweir SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(const SvEventDescription* pSupportedMacroItems) :
538cdf0e10cSrcweir     SvDetachedEventDescriptor(pSupportedMacroItems)
539cdf0e10cSrcweir {
540cdf0e10cSrcweir }
541cdf0e10cSrcweir 
SvMacroTableEventDescriptor(const SvxMacroTableDtor & rMacroTable,const SvEventDescription * pSupportedMacroItems)542cdf0e10cSrcweir SvMacroTableEventDescriptor::SvMacroTableEventDescriptor(
543cdf0e10cSrcweir     const SvxMacroTableDtor& rMacroTable,
544cdf0e10cSrcweir     const SvEventDescription* pSupportedMacroItems) :
545cdf0e10cSrcweir         SvDetachedEventDescriptor(pSupportedMacroItems)
546cdf0e10cSrcweir {
547cdf0e10cSrcweir     copyMacrosFromTable(rMacroTable);
548cdf0e10cSrcweir }
549cdf0e10cSrcweir 
~SvMacroTableEventDescriptor()550cdf0e10cSrcweir SvMacroTableEventDescriptor::~SvMacroTableEventDescriptor()
551cdf0e10cSrcweir {
552cdf0e10cSrcweir }
553cdf0e10cSrcweir 
copyMacrosFromTable(const SvxMacroTableDtor & rMacroTable)554cdf0e10cSrcweir void SvMacroTableEventDescriptor::copyMacrosFromTable(
555cdf0e10cSrcweir     const SvxMacroTableDtor& rMacroTable)
556cdf0e10cSrcweir {
557cdf0e10cSrcweir     for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
558cdf0e10cSrcweir     {
559cdf0e10cSrcweir         const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
560cdf0e10cSrcweir         const SvxMacro* pMacro = rMacroTable.Get(nEvent);
561cdf0e10cSrcweir         if (NULL != pMacro)
562cdf0e10cSrcweir             replaceByName(nEvent, *pMacro);
563cdf0e10cSrcweir     }
564cdf0e10cSrcweir 
565cdf0e10cSrcweir }
566cdf0e10cSrcweir 
copyMacrosIntoTable(SvxMacroTableDtor & rMacroTable)567cdf0e10cSrcweir void SvMacroTableEventDescriptor::copyMacrosIntoTable(
568cdf0e10cSrcweir     SvxMacroTableDtor& rMacroTable)
569cdf0e10cSrcweir {
570cdf0e10cSrcweir     for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
571cdf0e10cSrcweir     {
572cdf0e10cSrcweir         const sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
573cdf0e10cSrcweir         if (hasByName(nEvent))
574cdf0e10cSrcweir         {
575cdf0e10cSrcweir             SvxMacro* pMacro = new SvxMacro(sEmpty, sEmpty);
576cdf0e10cSrcweir             getByName(*pMacro, nEvent);
577cdf0e10cSrcweir             rMacroTable.Insert(nEvent, pMacro);
578cdf0e10cSrcweir         }
579cdf0e10cSrcweir     }
580cdf0e10cSrcweir }
581*4c77d985Smseidel 
582*4c77d985Smseidel /* vim: set noet sw=4 ts=4: */
583