Update ooo320-m1
[ooovba.git] / sfx2 / source / control / sfxstatuslistener.cxx
blob4fe13466f44271f609106aead1c9843db5325d9a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sfxstatuslistener.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
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.
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).
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.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
33 #include <sfx2/sfxstatuslistener.hxx>
34 #include <svtools/poolitem.hxx>
35 #include <svtools/eitem.hxx>
36 #include <svtools/stritem.hxx>
37 #include <svtools/intitem.hxx>
38 #include <svtools/itemset.hxx>
39 #include <svtools/itemdel.hxx>
40 #include <svtools/visitem.hxx>
41 #include <cppuhelper/weak.hxx>
42 #include <comphelper/processfactory.hxx>
43 #include <vos/mutex.hxx>
44 #include <vcl/svapp.hxx>
45 #include <com/sun/star/util/XURLTransformer.hpp>
46 #include <com/sun/star/lang/XUnoTunnel.hpp>
47 #include <com/sun/star/frame/status/ItemStatus.hpp>
48 #include <com/sun/star/frame/status/ItemState.hpp>
49 #include <com/sun/star/frame/status/Visibility.hpp>
51 #include <sfx2/viewfrm.hxx>
52 #include <sfx2/dispatch.hxx>
53 #include <sfx2/unoctitm.hxx>
54 #include <sfx2/msgpool.hxx>
56 using namespace ::rtl;
57 using namespace ::cppu;
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::frame;
60 using namespace ::com::sun::star::frame::status;
61 using namespace ::com::sun::star::lang;
62 using namespace ::com::sun::star::util;
64 SFX_IMPL_XINTERFACE_3( SfxStatusListener, OWeakObject, ::com::sun::star::lang::XComponent, ::com::sun::star::frame::XStatusListener, ::com::sun::star::lang::XEventListener )
65 SFX_IMPL_XTYPEPROVIDER_3( SfxStatusListener, ::com::sun::star::lang::XComponent, ::com::sun::star::frame::XStatusListener, ::com::sun::star::lang::XEventListener )
67 SfxStatusListener::SfxStatusListener( const Reference< XDispatchProvider >& rDispatchProvider, USHORT nSlotId, const OUString& rCommand ) :
68 cppu::OWeakObject(),
69 m_nSlotID( nSlotId ),
70 m_xDispatchProvider( rDispatchProvider )
72 m_aCommand.Complete = rCommand;
73 Reference < XURLTransformer > xTrans( ::comphelper::getProcessServiceFactory()->createInstance(
74 rtl::OUString::createFromAscii("com.sun.star.util.URLTransformer" )), UNO_QUERY );
75 xTrans->parseStrict( m_aCommand );
76 if ( rDispatchProvider.is() )
77 m_xDispatch = rDispatchProvider->queryDispatch( m_aCommand, rtl::OUString(), 0 );
80 SfxStatusListener::~SfxStatusListener()
84 // old sfx controller item C++ API
85 void SfxStatusListener::StateChanged( USHORT, SfxItemState, const SfxPoolItem* )
87 // must be implemented by sub class
90 void SfxStatusListener::Bind()
92 if ( !m_xDispatch.is() && m_xDispatchProvider.is() )
94 m_xDispatch = m_xDispatchProvider->queryDispatch( m_aCommand, rtl::OUString(), 0 );
95 try
97 Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
98 m_xDispatch->addStatusListener( aStatusListener, m_aCommand );
100 catch( Exception& )
106 void SfxStatusListener::Bind( USHORT nSlotId, const rtl::OUString& rNewCommand )
108 // first remove old listener, if we have a dispatch object
109 Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
110 if ( m_xDispatch.is() )
111 m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
112 if ( m_xDispatchProvider.is() )
114 // Store new command data and query for new dispatch
115 m_nSlotID = nSlotId;
116 m_aCommand.Complete = rNewCommand;
117 Reference < XURLTransformer > xTrans( ::comphelper::getProcessServiceFactory()->createInstance(
118 rtl::OUString::createFromAscii("com.sun.star.util.URLTransformer" )), UNO_QUERY );
119 xTrans->parseStrict( m_aCommand );
121 m_xDispatch = m_xDispatchProvider->queryDispatch( m_aCommand, rtl::OUString(), 0 );
125 m_xDispatch->addStatusListener( aStatusListener, m_aCommand );
127 catch( Exception& )
133 void SfxStatusListener::UnBind()
135 if ( m_xDispatch.is() )
137 Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
138 m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
139 m_xDispatch.clear();
143 void SfxStatusListener::ReBind()
145 Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
146 if ( m_xDispatch.is() )
147 m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
148 if ( m_xDispatchProvider.is() )
152 m_xDispatch = m_xDispatchProvider->queryDispatch( m_aCommand, rtl::OUString(), 0 );
153 if ( m_xDispatch.is() )
154 m_xDispatch->addStatusListener( aStatusListener, m_aCommand );
156 catch( Exception& )
162 // new UNO API
163 void SAL_CALL SfxStatusListener::dispose() throw( ::com::sun::star::uno::RuntimeException )
165 if ( m_xDispatch.is() && m_aCommand.Complete.getLength() > 0 )
169 Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
170 m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
172 catch ( Exception& )
177 m_xDispatch.clear();
178 m_xDispatchProvider.clear();
181 void SAL_CALL SfxStatusListener::addEventListener( const Reference< XEventListener >& )
182 throw ( RuntimeException )
184 // do nothing - this is a wrapper class which does not support listeners
187 void SAL_CALL SfxStatusListener::removeEventListener( const Reference< XEventListener >& )
188 throw ( RuntimeException )
190 // do nothing - this is a wrapper class which does not support listeners
193 void SAL_CALL SfxStatusListener::disposing( const EventObject& Source )
194 throw( RuntimeException )
196 ::vos::OGuard aGuard( Application::GetSolarMutex() );
198 if ( Source.Source == Reference< XInterface >( m_xDispatch, UNO_QUERY ))
199 m_xDispatch.clear();
200 else if ( Source.Source == Reference< XInterface >( m_xDispatchProvider, UNO_QUERY ))
201 m_xDispatchProvider.clear();
204 void SAL_CALL SfxStatusListener::statusChanged( const FeatureStateEvent& rEvent)
205 throw( RuntimeException )
207 ::vos::OGuard aGuard( Application::GetSolarMutex() );
209 SfxViewFrame* pViewFrame = NULL;
210 if ( m_xDispatch.is() )
212 Reference< XUnoTunnel > xTunnel( m_xDispatch, UNO_QUERY );
213 SfxOfficeDispatch* pDisp = NULL;
214 if ( xTunnel.is() )
216 sal_Int64 nImplementation = xTunnel->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier());
217 pDisp = reinterpret_cast< SfxOfficeDispatch* >(sal::static_int_cast< sal_IntPtr >( nImplementation ));
220 if ( pDisp )
221 pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame();
224 SfxSlotPool& rPool = SfxSlotPool::GetSlotPool( pViewFrame );
225 const SfxSlot* pSlot = rPool.GetSlot( m_nSlotID );
227 SfxItemState eState = SFX_ITEM_DISABLED;
228 SfxPoolItem* pItem = NULL;
229 if ( rEvent.IsEnabled )
231 eState = SFX_ITEM_AVAILABLE;
232 ::com::sun::star::uno::Type pType = rEvent.State.getValueType();
234 if ( pType == ::getVoidCppuType() )
236 pItem = new SfxVoidItem( m_nSlotID );
237 eState = SFX_ITEM_UNKNOWN;
239 else if ( pType == ::getBooleanCppuType() )
241 sal_Bool bTemp = false;
242 rEvent.State >>= bTemp ;
243 pItem = new SfxBoolItem( m_nSlotID, bTemp );
245 else if ( pType == ::getCppuType((const sal_uInt16*)0) )
247 sal_uInt16 nTemp = 0;
248 rEvent.State >>= nTemp ;
249 pItem = new SfxUInt16Item( m_nSlotID, nTemp );
251 else if ( pType == ::getCppuType((const sal_uInt32*)0) )
253 sal_uInt32 nTemp = 0;
254 rEvent.State >>= nTemp ;
255 pItem = new SfxUInt32Item( m_nSlotID, nTemp );
257 else if ( pType == ::getCppuType((const ::rtl::OUString*)0) )
259 ::rtl::OUString sTemp ;
260 rEvent.State >>= sTemp ;
261 pItem = new SfxStringItem( m_nSlotID, sTemp );
263 else if ( pType == ::getCppuType((const ::com::sun::star::frame::status::ItemStatus*)0) )
265 ItemStatus aItemStatus;
266 rEvent.State >>= aItemStatus;
267 eState = aItemStatus.State;
268 pItem = new SfxVoidItem( m_nSlotID );
270 else if ( pType == ::getCppuType((const ::com::sun::star::frame::status::Visibility*)0) )
272 Visibility aVisibilityStatus;
273 rEvent.State >>= aVisibilityStatus;
274 pItem = new SfxVisibilityItem( m_nSlotID, aVisibilityStatus.bVisible );
276 else
278 if ( pSlot )
279 pItem = pSlot->GetType()->CreateItem();
280 if ( pItem )
282 pItem->SetWhich( m_nSlotID );
283 pItem->PutValue( rEvent.State );
285 else
286 pItem = new SfxVoidItem( m_nSlotID );
290 StateChanged( m_nSlotID, eState, pItem );
291 delete pItem;