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 .
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 "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 SfxStatusListener::SfxStatusListener( const Reference
< XDispatchProvider
>& rDispatchProvider
, sal_uInt16 nSlotId
, const OUString
& rCommand
) :
53 m_xDispatchProvider( rDispatchProvider
)
55 m_aCommand
.Complete
= rCommand
;
56 Reference
< XURLTransformer
> xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
57 xTrans
->parseStrict( m_aCommand
);
58 if ( rDispatchProvider
.is() )
59 m_xDispatch
= rDispatchProvider
->queryDispatch( m_aCommand
, OUString(), 0 );
62 SfxStatusListener::~SfxStatusListener()
66 // old sfx controller item C++ API
67 void SfxStatusListener::StateChanged( sal_uInt16
, SfxItemState
, const SfxPoolItem
* )
69 // must be implemented by sub class
72 void SfxStatusListener::UnBind()
74 if ( m_xDispatch
.is() )
76 Reference
< XStatusListener
> aStatusListener( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
77 m_xDispatch
->removeStatusListener( aStatusListener
, m_aCommand
);
82 void SfxStatusListener::ReBind()
84 Reference
< XStatusListener
> aStatusListener( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
85 if ( m_xDispatch
.is() )
86 m_xDispatch
->removeStatusListener( aStatusListener
, m_aCommand
);
87 if ( m_xDispatchProvider
.is() )
91 m_xDispatch
= m_xDispatchProvider
->queryDispatch( m_aCommand
, OUString(), 0 );
92 if ( m_xDispatch
.is() )
93 m_xDispatch
->addStatusListener( aStatusListener
, m_aCommand
);
102 void SAL_CALL
SfxStatusListener::dispose() throw( ::com::sun::star::uno::RuntimeException
, std::exception
)
104 if ( m_xDispatch
.is() && !m_aCommand
.Complete
.isEmpty() )
108 Reference
< XStatusListener
> aStatusListener( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
109 m_xDispatch
->removeStatusListener( aStatusListener
, m_aCommand
);
117 m_xDispatchProvider
.clear();
120 void SAL_CALL
SfxStatusListener::addEventListener( const Reference
< XEventListener
>& )
121 throw ( RuntimeException
, std::exception
)
123 // do nothing - this is a wrapper class which does not support listeners
126 void SAL_CALL
SfxStatusListener::removeEventListener( const Reference
< XEventListener
>& )
127 throw ( RuntimeException
, std::exception
)
129 // do nothing - this is a wrapper class which does not support listeners
132 void SAL_CALL
SfxStatusListener::disposing( const EventObject
& Source
)
133 throw( RuntimeException
, std::exception
)
135 SolarMutexGuard aGuard
;
137 if ( Source
.Source
== Reference
< XInterface
>( m_xDispatch
, UNO_QUERY
))
139 else if ( Source
.Source
== Reference
< XInterface
>( m_xDispatchProvider
, UNO_QUERY
))
140 m_xDispatchProvider
.clear();
143 void SAL_CALL
SfxStatusListener::statusChanged( const FeatureStateEvent
& rEvent
)
144 throw( RuntimeException
, std::exception
)
146 SolarMutexGuard aGuard
;
148 SfxViewFrame
* pViewFrame
= NULL
;
149 if ( m_xDispatch
.is() )
151 Reference
< XUnoTunnel
> xTunnel( m_xDispatch
, UNO_QUERY
);
152 SfxOfficeDispatch
* pDisp
= NULL
;
155 sal_Int64 nImplementation
= xTunnel
->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier());
156 pDisp
= reinterpret_cast< SfxOfficeDispatch
* >(sal::static_int_cast
< sal_IntPtr
>( nImplementation
));
160 pViewFrame
= pDisp
->GetDispatcher_Impl()->GetFrame();
163 SfxSlotPool
& rPool
= SfxSlotPool::GetSlotPool( pViewFrame
);
164 const SfxSlot
* pSlot
= rPool
.GetSlot( m_nSlotID
);
166 SfxItemState eState
= SfxItemState::DISABLED
;
167 SfxPoolItem
* pItem
= NULL
;
168 if ( rEvent
.IsEnabled
)
170 eState
= SfxItemState::DEFAULT
;
171 ::com::sun::star::uno::Type pType
= rEvent
.State
.getValueType();
173 if ( pType
== ::cppu::UnoType
<void>::get() )
175 pItem
= new SfxVoidItem( m_nSlotID
);
176 eState
= SfxItemState::UNKNOWN
;
178 else if ( pType
== cppu::UnoType
< bool >::get() )
181 rEvent
.State
>>= bTemp
;
182 pItem
= new SfxBoolItem( m_nSlotID
, bTemp
);
184 else if ( pType
== cppu::UnoType
< ::cppu::UnoUnsignedShortType
>::get() )
186 sal_uInt16 nTemp
= 0;
187 rEvent
.State
>>= nTemp
;
188 pItem
= new SfxUInt16Item( m_nSlotID
, nTemp
);
190 else if ( pType
== cppu::UnoType
<sal_uInt32
>::get() )
192 sal_uInt32 nTemp
= 0;
193 rEvent
.State
>>= nTemp
;
194 pItem
= new SfxUInt32Item( m_nSlotID
, nTemp
);
196 else if ( pType
== cppu::UnoType
<OUString
>::get() )
199 rEvent
.State
>>= sTemp
;
200 pItem
= new SfxStringItem( m_nSlotID
, sTemp
);
202 else if ( pType
== cppu::UnoType
< ::com::sun::star::frame::status::ItemStatus
>::get() )
204 ItemStatus aItemStatus
;
205 rEvent
.State
>>= aItemStatus
;
206 eState
= (SfxItemState
) aItemStatus
.State
;
207 pItem
= new SfxVoidItem( m_nSlotID
);
209 else if ( pType
== cppu::UnoType
< ::com::sun::star::frame::status::Visibility
>::get() )
211 Visibility aVisibilityStatus
;
212 rEvent
.State
>>= aVisibilityStatus
;
213 pItem
= new SfxVisibilityItem( m_nSlotID
, aVisibilityStatus
.bVisible
);
218 pItem
= pSlot
->GetType()->CreateItem();
221 pItem
->SetWhich( m_nSlotID
);
222 pItem
->PutValue( rEvent
.State
);
225 pItem
= new SfxVoidItem( m_nSlotID
);
229 StateChanged( m_nSlotID
, eState
, pItem
);
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */