Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
38 :
39 : #include <sfx2/viewfrm.hxx>
40 : #include <sfx2/dispatch.hxx>
41 : #include <sfx2/unoctitm.hxx>
42 : #include <sfx2/msgpool.hxx>
43 :
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;
50 :
51 2485 : SfxStatusListener::SfxStatusListener( const Reference< XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& rCommand ) :
52 : m_nSlotID( nSlotId ),
53 2485 : m_xDispatchProvider( rDispatchProvider )
54 : {
55 2485 : m_aCommand.Complete = rCommand;
56 2485 : Reference< XURLTransformer > xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
57 2485 : xTrans->parseStrict( m_aCommand );
58 2485 : if ( rDispatchProvider.is() )
59 2485 : m_xDispatch = rDispatchProvider->queryDispatch( m_aCommand, OUString(), 0 );
60 2485 : }
61 :
62 2485 : SfxStatusListener::~SfxStatusListener()
63 : {
64 2485 : }
65 :
66 : // old sfx controller item C++ API
67 0 : void SfxStatusListener::StateChanged( sal_uInt16, SfxItemState, const SfxPoolItem* )
68 : {
69 : // must be implemented by sub class
70 0 : }
71 :
72 0 : void SfxStatusListener::UnBind()
73 : {
74 0 : if ( m_xDispatch.is() )
75 : {
76 0 : Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
77 0 : m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
78 0 : m_xDispatch.clear();
79 : }
80 0 : }
81 :
82 3385 : void SfxStatusListener::ReBind()
83 : {
84 3385 : Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
85 3385 : if ( m_xDispatch.is() )
86 2708 : m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
87 3385 : if ( m_xDispatchProvider.is() )
88 : {
89 : try
90 : {
91 3385 : m_xDispatch = m_xDispatchProvider->queryDispatch( m_aCommand, OUString(), 0 );
92 3385 : if ( m_xDispatch.is() )
93 2708 : m_xDispatch->addStatusListener( aStatusListener, m_aCommand );
94 : }
95 0 : catch( Exception& )
96 : {
97 : }
98 3385 : }
99 3385 : }
100 :
101 : // new UNO API
102 2485 : void SAL_CALL SfxStatusListener::dispose() throw( ::com::sun::star::uno::RuntimeException, std::exception )
103 : {
104 2485 : if ( m_xDispatch.is() && !m_aCommand.Complete.isEmpty() )
105 : {
106 : try
107 : {
108 1680 : Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
109 1680 : m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
110 : }
111 0 : catch ( Exception& )
112 : {
113 : }
114 : }
115 :
116 2485 : m_xDispatch.clear();
117 2485 : m_xDispatchProvider.clear();
118 2485 : }
119 :
120 0 : void SAL_CALL SfxStatusListener::addEventListener( const Reference< XEventListener >& )
121 : throw ( RuntimeException, std::exception )
122 : {
123 : // do nothing - this is a wrapper class which does not support listeners
124 0 : }
125 :
126 0 : void SAL_CALL SfxStatusListener::removeEventListener( const Reference< XEventListener >& )
127 : throw ( RuntimeException, std::exception )
128 : {
129 : // do nothing - this is a wrapper class which does not support listeners
130 0 : }
131 :
132 0 : void SAL_CALL SfxStatusListener::disposing( const EventObject& Source )
133 : throw( RuntimeException, std::exception )
134 : {
135 0 : SolarMutexGuard aGuard;
136 :
137 0 : if ( Source.Source == Reference< XInterface >( m_xDispatch, UNO_QUERY ))
138 0 : m_xDispatch.clear();
139 0 : else if ( Source.Source == Reference< XInterface >( m_xDispatchProvider, UNO_QUERY ))
140 0 : m_xDispatchProvider.clear();
141 0 : }
142 :
143 3763 : void SAL_CALL SfxStatusListener::statusChanged( const FeatureStateEvent& rEvent)
144 : throw( RuntimeException, std::exception )
145 : {
146 3763 : SolarMutexGuard aGuard;
147 :
148 3763 : SfxViewFrame* pViewFrame = NULL;
149 3763 : if ( m_xDispatch.is() )
150 : {
151 3763 : Reference< XUnoTunnel > xTunnel( m_xDispatch, UNO_QUERY );
152 3763 : SfxOfficeDispatch* pDisp = NULL;
153 3763 : if ( xTunnel.is() )
154 : {
155 3763 : sal_Int64 nImplementation = xTunnel->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier());
156 3763 : pDisp = reinterpret_cast< SfxOfficeDispatch* >(sal::static_int_cast< sal_IntPtr >( nImplementation ));
157 : }
158 :
159 3763 : if ( pDisp )
160 3763 : pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame();
161 : }
162 :
163 3763 : SfxSlotPool& rPool = SfxSlotPool::GetSlotPool( pViewFrame );
164 3763 : const SfxSlot* pSlot = rPool.GetSlot( m_nSlotID );
165 :
166 3763 : SfxItemState eState = SfxItemState::DISABLED;
167 3763 : SfxPoolItem* pItem = NULL;
168 3763 : if ( rEvent.IsEnabled )
169 : {
170 3757 : eState = SfxItemState::DEFAULT;
171 3757 : ::com::sun::star::uno::Type pType = rEvent.State.getValueType();
172 :
173 3757 : if ( pType == ::cppu::UnoType<void>::get() )
174 : {
175 937 : pItem = new SfxVoidItem( m_nSlotID );
176 937 : eState = SfxItemState::UNKNOWN;
177 : }
178 2820 : else if ( pType == cppu::UnoType< bool >::get() )
179 : {
180 0 : bool bTemp = false;
181 0 : rEvent.State >>= bTemp ;
182 0 : pItem = new SfxBoolItem( m_nSlotID, bTemp );
183 : }
184 2820 : else if ( pType == cppu::UnoType< ::cppu::UnoUnsignedShortType >::get() )
185 : {
186 0 : sal_uInt16 nTemp = 0;
187 0 : rEvent.State >>= nTemp ;
188 0 : pItem = new SfxUInt16Item( m_nSlotID, nTemp );
189 : }
190 2820 : else if ( pType == cppu::UnoType<sal_uInt32>::get() )
191 : {
192 0 : sal_uInt32 nTemp = 0;
193 0 : rEvent.State >>= nTemp ;
194 0 : pItem = new SfxUInt32Item( m_nSlotID, nTemp );
195 : }
196 2820 : else if ( pType == cppu::UnoType<OUString>::get() )
197 : {
198 0 : OUString sTemp ;
199 0 : rEvent.State >>= sTemp ;
200 0 : pItem = new SfxStringItem( m_nSlotID, sTemp );
201 : }
202 2820 : else if ( pType == cppu::UnoType< ::com::sun::star::frame::status::ItemStatus >::get() )
203 : {
204 0 : ItemStatus aItemStatus;
205 0 : rEvent.State >>= aItemStatus;
206 0 : eState = (SfxItemState) aItemStatus.State;
207 0 : pItem = new SfxVoidItem( m_nSlotID );
208 : }
209 2820 : else if ( pType == cppu::UnoType< ::com::sun::star::frame::status::Visibility >::get() )
210 : {
211 0 : Visibility aVisibilityStatus;
212 0 : rEvent.State >>= aVisibilityStatus;
213 0 : pItem = new SfxVisibilityItem( m_nSlotID, aVisibilityStatus.bVisible );
214 : }
215 : else
216 : {
217 2820 : if ( pSlot )
218 2820 : pItem = pSlot->GetType()->CreateItem();
219 2820 : if ( pItem )
220 : {
221 2820 : pItem->SetWhich( m_nSlotID );
222 2820 : pItem->PutValue( rEvent.State );
223 : }
224 : else
225 0 : pItem = new SfxVoidItem( m_nSlotID );
226 3757 : }
227 : }
228 :
229 3763 : StateChanged( m_nSlotID, eState, pItem );
230 3763 : delete pItem;
231 3763 : }
232 :
233 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|