1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <boost/scoped_ptr.hpp>
23 #include <vcl/msgbox.hxx>
24 #include <svl/lstner.hxx>
25 #include <basic/basmgr.hxx>
26 #include <basic/sbmod.hxx>
27 #include <basic/sbx.hxx>
28 #include <sot/storage.hxx>
29 #include <unotools/securityoptions.hxx>
31 #include <rtl/ustring.h>
32 #include <com/sun/star/uno/Any.hxx>
33 #include <framework/eventsconfiguration.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <sfx2/evntconf.hxx>
37 #include <sfx2/docfile.hxx>
38 #include <sfx2/app.hxx>
39 #include <sfx2/objsh.hxx>
40 #include <sfx2/dispatch.hxx>
41 #include "sfx2/sfxresid.hxx"
42 #include "eventsupplier.hxx"
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/container/XNameReplace.hpp>
46 #include <com/sun/star/document/XEventsSupplier.hpp>
47 #include <com/sun/star/frame/GlobalEventBroadcaster.hpp>
48 #include <com/sun/star/uno/Sequence.hxx>
49 #include <com/sun/star/uno/Reference.hxx>
51 // -----------------------------------------------------------------------
52 TYPEINIT1(SfxEventHint
, SfxHint
);
53 TYPEINIT1(SfxEventNamesItem
, SfxPoolItem
);
54 TYPEINIT1(SfxViewEventHint
, SfxEventHint
);
56 using namespace com::sun::star
;
58 SfxEventNamesList
& SfxEventNamesList::operator=( const SfxEventNamesList
& rTbl
)
61 for ( size_t i
= 0, n
= rTbl
.size(); i
< n
; ++i
)
63 SfxEventName
* pTmp
= rTbl
.at( i
);
64 SfxEventName
* pNew
= new SfxEventName( *pTmp
);
65 aEventNamesList
.push_back( pNew
);
70 void SfxEventNamesList::DelDtor()
72 for ( size_t i
= 0, n
= aEventNamesList
.size(); i
< n
; ++i
)
73 delete aEventNamesList
[ i
];
74 aEventNamesList
.clear();
77 int SfxEventNamesItem::operator==( const SfxPoolItem
& rAttr
) const
79 DBG_ASSERT( SfxPoolItem::operator==(rAttr
), "unequal types" );
81 const SfxEventNamesList
& rOwn
= aEventsList
;
82 const SfxEventNamesList
& rOther
= ( (SfxEventNamesItem
&) rAttr
).aEventsList
;
84 if ( rOwn
.size() != rOther
.size() )
87 for ( size_t nNo
= 0, nCnt
= rOwn
.size(); nNo
< nCnt
; ++nNo
)
89 const SfxEventName
*pOwn
= rOwn
.at( nNo
);
90 const SfxEventName
*pOther
= rOther
.at( nNo
);
91 if ( pOwn
->mnId
!= pOther
->mnId
||
92 pOwn
->maEventName
!= pOther
->maEventName
||
93 pOwn
->maUIName
!= pOther
->maUIName
)
101 SfxItemPresentation
SfxEventNamesItem::GetPresentation( SfxItemPresentation
,
105 const IntlWrapper
* ) const
108 return SFX_ITEM_PRESENTATION_NONE
;
111 SfxPoolItem
* SfxEventNamesItem::Clone( SfxItemPool
*) const
113 return new SfxEventNamesItem(*this);
116 SfxPoolItem
* SfxEventNamesItem::Create(SvStream
&, sal_uInt16
) const
118 OSL_FAIL("not streamable!");
119 return new SfxEventNamesItem(Which());
122 SvStream
& SfxEventNamesItem::Store(SvStream
&rStream
, sal_uInt16
) const
124 OSL_FAIL("not streamable!");
128 sal_uInt16
SfxEventNamesItem::GetVersion( sal_uInt16
) const
130 OSL_FAIL("not streamable!");
134 void SfxEventNamesItem::AddEvent( const String
& rName
, const String
& rUIName
, sal_uInt16 nID
)
136 aEventsList
.push_back( new SfxEventName( nID
, rName
, rUIName
.Len() ? rUIName
: rName
) );
140 //==========================================================================
142 //--------------------------------------------------------------------------
143 uno::Any
CreateEventData_Impl( const SvxMacro
*pMacro
)
146 This function converts a SvxMacro into an Any containing three
147 properties. These properties are EventType and Script. Possible
148 values for EventType ar StarBasic, JavaScript, ...
149 The Script property should contain the URL to the macro and looks
150 like "macro://./standard.module1.main()"
152 If pMacro is NULL, we return an empty property sequence, so PropagateEvent_Impl
153 can delete an event binding.
159 if ( pMacro
->GetScriptType() == STARBASIC
)
161 uno::Sequence
< beans::PropertyValue
> aProperties(3);
162 beans::PropertyValue
*pValues
= aProperties
.getArray();
164 OUString
aType(STAR_BASIC
);
165 OUString aLib
= pMacro
->GetLibName();
166 OUString aMacro
= pMacro
->GetMacName();
168 pValues
[ 0 ].Name
= OUString(PROP_EVENT_TYPE
);
169 pValues
[ 0 ].Value
<<= aType
;
171 pValues
[ 1 ].Name
= OUString(PROP_LIBRARY
);
172 pValues
[ 1 ].Value
<<= aLib
;
174 pValues
[ 2 ].Name
= OUString(PROP_MACRO_NAME
);
175 pValues
[ 2 ].Value
<<= aMacro
;
177 aEventData
<<= aProperties
;
179 else if ( pMacro
->GetScriptType() == EXTENDED_STYPE
)
181 uno::Sequence
< beans::PropertyValue
> aProperties(2);
182 beans::PropertyValue
*pValues
= aProperties
.getArray();
184 OUString aLib
= pMacro
->GetLibName();
185 OUString aMacro
= pMacro
->GetMacName();
187 pValues
[ 0 ].Name
= OUString(PROP_EVENT_TYPE
);
188 pValues
[ 0 ].Value
<<= aLib
;
190 pValues
[ 1 ].Name
= OUString(PROP_SCRIPT
);
191 pValues
[ 1 ].Value
<<= aMacro
;
193 aEventData
<<= aProperties
;
195 else if ( pMacro
->GetScriptType() == JAVASCRIPT
)
197 uno::Sequence
< beans::PropertyValue
> aProperties(2);
198 beans::PropertyValue
*pValues
= aProperties
.getArray();
200 OUString aMacro
= pMacro
->GetMacName();
202 pValues
[ 0 ].Name
= OUString(PROP_EVENT_TYPE
);
203 pValues
[ 0 ].Value
<<= OUString(SVX_MACRO_LANGUAGE_JAVASCRIPT
);
205 pValues
[ 1 ].Name
= OUString(PROP_MACRO_NAME
);
206 pValues
[ 1 ].Value
<<= aMacro
;
208 aEventData
<<= aProperties
;
212 SAL_WARN( "sfx2.config", "CreateEventData_Impl(): ScriptType not supported!");
217 uno::Sequence
< beans::PropertyValue
> aProperties
;
218 aEventData
<<= aProperties
;
224 //--------------------------------------------------------------------------
225 void PropagateEvent_Impl( SfxObjectShell
*pDoc
, OUString aEventName
, const SvxMacro
* pMacro
)
227 uno::Reference
< document::XEventsSupplier
> xSupplier
;
230 xSupplier
= uno::Reference
< document::XEventsSupplier
>( pDoc
->GetModel(), uno::UNO_QUERY
);
234 xSupplier
= uno::Reference
< document::XEventsSupplier
>
235 ( frame::GlobalEventBroadcaster::create(::comphelper::getProcessComponentContext()),
239 if ( xSupplier
.is() )
241 uno::Reference
< container::XNameReplace
> xEvents
= xSupplier
->getEvents();
242 if ( !aEventName
.isEmpty() )
244 uno::Any aEventData
= CreateEventData_Impl( pMacro
);
248 xEvents
->replaceByName( aEventName
, aEventData
);
250 catch( const ::com::sun::star::lang::IllegalArgumentException
& )
252 SAL_WARN( "sfx2.config", "PropagateEvents_Impl: caught IllegalArgumentException" );
254 catch( const ::com::sun::star::container::NoSuchElementException
& )
256 SAL_WARN( "sfx2.config", "PropagateEvents_Impl: caught NoSuchElementException" );
260 DBG_WARNING( "PropagateEvents_Impl: Got unknown event" );
265 //--------------------------------------------------------------------------------------------------------
266 void SfxEventConfiguration::ConfigureEvent( OUString aName
, const SvxMacro
& rMacro
, SfxObjectShell
*pDoc
)
268 boost::scoped_ptr
<SvxMacro
> pMacro
;
269 if ( rMacro
.HasMacro() )
270 pMacro
.reset( new SvxMacro( rMacro
.GetMacName(), rMacro
.GetLibName(), rMacro
.GetScriptType() ) );
271 PropagateEvent_Impl( pDoc
? pDoc
: 0, aName
, pMacro
.get() );
274 // -------------------------------------------------------------------------------------------------------
275 SvxMacro
* SfxEventConfiguration::ConvertToMacro( const com::sun::star::uno::Any
& rElement
, SfxObjectShell
* pDoc
, sal_Bool bBlowUp
)
277 return SfxEvents_Impl::ConvertToMacro( rElement
, pDoc
, bBlowUp
);
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */