Bump version to 4.1-6
[LibreOffice.git] / sfx2 / source / control / sfxstatuslistener.cxx
blobb44dd04ca48e39fb4b07b1132ec776f4639de14e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <sfx2/sfxstatuslistener.hxx>
21 #include <svl/poolitem.hxx>
22 #include <svl/eitem.hxx>
23 #include <svl/stritem.hxx>
24 #include <svl/intitem.hxx>
25 #include <svl/itemset.hxx>
26 #include <svtools/itemdel.hxx>
27 #include <svl/visitem.hxx>
28 #include <cppuhelper/weak.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <osl/mutex.hxx>
31 #include <vcl/svapp.hxx>
32 #include <com/sun/star/util/URLTransformer.hpp>
33 #include <com/sun/star/util/XURLTransformer.hpp>
34 #include <com/sun/star/lang/XUnoTunnel.hpp>
35 #include <com/sun/star/frame/status/ItemStatus.hpp>
36 #include <com/sun/star/frame/status/ItemState.hpp>
37 #include <com/sun/star/frame/status/Visibility.hpp>
39 #include <sfx2/viewfrm.hxx>
40 #include <sfx2/dispatch.hxx>
41 #include <sfx2/unoctitm.hxx>
42 #include <sfx2/msgpool.hxx>
44 using namespace ::cppu;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::frame;
47 using namespace ::com::sun::star::frame::status;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::util;
51 SFX_IMPL_XINTERFACE_3( SfxStatusListener, OWeakObject, ::com::sun::star::lang::XComponent, ::com::sun::star::frame::XStatusListener, ::com::sun::star::lang::XEventListener )
52 SFX_IMPL_XTYPEPROVIDER_3( SfxStatusListener, ::com::sun::star::lang::XComponent, ::com::sun::star::frame::XStatusListener, ::com::sun::star::lang::XEventListener )
54 SfxStatusListener::SfxStatusListener( const Reference< XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& rCommand ) :
55 cppu::OWeakObject(),
56 m_nSlotID( nSlotId ),
57 m_xDispatchProvider( rDispatchProvider )
59 m_aCommand.Complete = rCommand;
60 Reference< XURLTransformer > xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
61 xTrans->parseStrict( m_aCommand );
62 if ( rDispatchProvider.is() )
63 m_xDispatch = rDispatchProvider->queryDispatch( m_aCommand, OUString(), 0 );
66 SfxStatusListener::~SfxStatusListener()
70 // old sfx controller item C++ API
71 void SfxStatusListener::StateChanged( sal_uInt16, SfxItemState, const SfxPoolItem* )
73 // must be implemented by sub class
76 void SfxStatusListener::UnBind()
78 if ( m_xDispatch.is() )
80 Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
81 m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
82 m_xDispatch.clear();
86 void SfxStatusListener::ReBind()
88 Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
89 if ( m_xDispatch.is() )
90 m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
91 if ( m_xDispatchProvider.is() )
93 try
95 m_xDispatch = m_xDispatchProvider->queryDispatch( m_aCommand, OUString(), 0 );
96 if ( m_xDispatch.is() )
97 m_xDispatch->addStatusListener( aStatusListener, m_aCommand );
99 catch( Exception& )
105 // new UNO API
106 void SAL_CALL SfxStatusListener::dispose() throw( ::com::sun::star::uno::RuntimeException )
108 if ( m_xDispatch.is() && !m_aCommand.Complete.isEmpty() )
112 Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
113 m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
115 catch ( Exception& )
120 m_xDispatch.clear();
121 m_xDispatchProvider.clear();
124 void SAL_CALL SfxStatusListener::addEventListener( const Reference< XEventListener >& )
125 throw ( RuntimeException )
127 // do nothing - this is a wrapper class which does not support listeners
130 void SAL_CALL SfxStatusListener::removeEventListener( const Reference< XEventListener >& )
131 throw ( RuntimeException )
133 // do nothing - this is a wrapper class which does not support listeners
136 void SAL_CALL SfxStatusListener::disposing( const EventObject& Source )
137 throw( RuntimeException )
139 SolarMutexGuard aGuard;
141 if ( Source.Source == Reference< XInterface >( m_xDispatch, UNO_QUERY ))
142 m_xDispatch.clear();
143 else if ( Source.Source == Reference< XInterface >( m_xDispatchProvider, UNO_QUERY ))
144 m_xDispatchProvider.clear();
147 void SAL_CALL SfxStatusListener::statusChanged( const FeatureStateEvent& rEvent)
148 throw( RuntimeException )
150 SolarMutexGuard aGuard;
152 SfxViewFrame* pViewFrame = NULL;
153 if ( m_xDispatch.is() )
155 Reference< XUnoTunnel > xTunnel( m_xDispatch, UNO_QUERY );
156 SfxOfficeDispatch* pDisp = NULL;
157 if ( xTunnel.is() )
159 sal_Int64 nImplementation = xTunnel->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier());
160 pDisp = reinterpret_cast< SfxOfficeDispatch* >(sal::static_int_cast< sal_IntPtr >( nImplementation ));
163 if ( pDisp )
164 pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame();
167 SfxSlotPool& rPool = SfxSlotPool::GetSlotPool( pViewFrame );
168 const SfxSlot* pSlot = rPool.GetSlot( m_nSlotID );
170 SfxItemState eState = SFX_ITEM_DISABLED;
171 SfxPoolItem* pItem = NULL;
172 if ( rEvent.IsEnabled )
174 eState = SFX_ITEM_AVAILABLE;
175 ::com::sun::star::uno::Type pType = rEvent.State.getValueType();
177 if ( pType == ::getVoidCppuType() )
179 pItem = new SfxVoidItem( m_nSlotID );
180 eState = SFX_ITEM_UNKNOWN;
182 else if ( pType == ::getBooleanCppuType() )
184 sal_Bool bTemp = false;
185 rEvent.State >>= bTemp ;
186 pItem = new SfxBoolItem( m_nSlotID, bTemp );
188 else if ( pType == ::getCppuType((const sal_uInt16*)0) )
190 sal_uInt16 nTemp = 0;
191 rEvent.State >>= nTemp ;
192 pItem = new SfxUInt16Item( m_nSlotID, nTemp );
194 else if ( pType == ::getCppuType((const sal_uInt32*)0) )
196 sal_uInt32 nTemp = 0;
197 rEvent.State >>= nTemp ;
198 pItem = new SfxUInt32Item( m_nSlotID, nTemp );
200 else if ( pType == ::getCppuType((const OUString*)0) )
202 OUString sTemp ;
203 rEvent.State >>= sTemp ;
204 pItem = new SfxStringItem( m_nSlotID, sTemp );
206 else if ( pType == ::getCppuType((const ::com::sun::star::frame::status::ItemStatus*)0) )
208 ItemStatus aItemStatus;
209 rEvent.State >>= aItemStatus;
210 eState = aItemStatus.State;
211 pItem = new SfxVoidItem( m_nSlotID );
213 else if ( pType == ::getCppuType((const ::com::sun::star::frame::status::Visibility*)0) )
215 Visibility aVisibilityStatus;
216 rEvent.State >>= aVisibilityStatus;
217 pItem = new SfxVisibilityItem( m_nSlotID, aVisibilityStatus.bVisible );
219 else
221 if ( pSlot )
222 pItem = pSlot->GetType()->CreateItem();
223 if ( pItem )
225 pItem->SetWhich( m_nSlotID );
226 pItem->PutValue( rEvent.State );
228 else
229 pItem = new SfxVoidItem( m_nSlotID );
233 StateChanged( m_nSlotID, eState, pItem );
234 delete pItem;
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */