LibreOffice Module sfx2 (master) 1
sfxstatuslistener.cxx
Go to the documentation of this file.
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
21#include <svl/poolitem.hxx>
22#include <svl/eitem.hxx>
23#include <svl/stritem.hxx>
24#include <svl/intitem.hxx>
25#include <svl/visitem.hxx>
28#include <vcl/svapp.hxx>
29#include <com/sun/star/util/URLTransformer.hpp>
30#include <com/sun/star/util/XURLTransformer.hpp>
31#include <com/sun/star/lang/XUnoTunnel.hpp>
32#include <com/sun/star/frame/status/ItemStatus.hpp>
33#include <com/sun/star/frame/status/Visibility.hpp>
34#include <com/sun/star/frame/XDispatchProvider.hpp>
35
36#include <sfx2/viewfrm.hxx>
37#include <sfx2/dispatch.hxx>
38#include <unoctitm.hxx>
39#include <sfx2/msgpool.hxx>
40#include <sfx2/msg.hxx>
41
42using namespace ::cppu;
43using namespace ::com::sun::star::uno;
44using namespace ::com::sun::star::frame;
45using namespace ::com::sun::star::frame::status;
46using namespace ::com::sun::star::lang;
47using namespace ::com::sun::star::util;
48
49SfxStatusListener::SfxStatusListener( const Reference< XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& rCommand ) :
50 m_nSlotID( nSlotId ),
51 m_xDispatchProvider( rDispatchProvider )
52{
53 m_aCommand.Complete = rCommand;
54 Reference< XURLTransformer > xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
55 xTrans->parseStrict( m_aCommand );
56 if ( rDispatchProvider.is() )
57 m_xDispatch = rDispatchProvider->queryDispatch( m_aCommand, OUString(), 0 );
58}
59
61{
62}
63
64// old sfx controller item C++ API
66{
67 // must be implemented by sub class
68}
69
71{
72 if ( m_xDispatch.is() )
73 {
74 Reference< XStatusListener > aStatusListener(this);
75 m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
76 m_xDispatch.clear();
77 }
78}
79
81{
82 Reference< XStatusListener > aStatusListener(this);
83 if ( m_xDispatch.is() )
84 m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
85 if ( m_xDispatchProvider.is() )
86 {
87 try
88 {
89 m_xDispatch = m_xDispatchProvider->queryDispatch( m_aCommand, OUString(), 0 );
90 if ( m_xDispatch.is() )
91 m_xDispatch->addStatusListener( aStatusListener, m_aCommand );
92 }
93 catch( Exception& )
94 {
95 }
96 }
97}
98
99// new UNO API
101{
102 if ( m_xDispatch.is() && !m_aCommand.Complete.isEmpty() )
103 {
104 try
105 {
106 Reference< XStatusListener > aStatusListener(this);
107 m_xDispatch->removeStatusListener( aStatusListener, m_aCommand );
108 }
109 catch ( Exception& )
110 {
111 }
112 }
113
114 m_xDispatch.clear();
115 m_xDispatchProvider.clear();
116}
117
118void SAL_CALL SfxStatusListener::addEventListener( const Reference< XEventListener >& )
119{
120 // do nothing - this is a wrapper class which does not support listeners
121}
122
123void SAL_CALL SfxStatusListener::removeEventListener( const Reference< XEventListener >& )
124{
125 // do nothing - this is a wrapper class which does not support listeners
126}
127
128void SAL_CALL SfxStatusListener::disposing( const EventObject& Source )
129{
130 SolarMutexGuard aGuard;
131
132 if ( Source.Source == Reference< XInterface >( m_xDispatch, UNO_QUERY ))
133 m_xDispatch.clear();
134 else if ( Source.Source == Reference< XInterface >( m_xDispatchProvider, UNO_QUERY ))
135 m_xDispatchProvider.clear();
136}
137
138void SAL_CALL SfxStatusListener::statusChanged( const FeatureStateEvent& rEvent)
139{
140 SolarMutexGuard aGuard;
141
142 SfxViewFrame* pViewFrame = nullptr;
143 if ( m_xDispatch.is() )
144 {
145 if (auto pDisp = dynamic_cast<SfxOfficeDispatch*>(m_xDispatch.get()))
146 pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame();
147 }
148
149 SfxSlotPool& rPool = SfxSlotPool::GetSlotPool( pViewFrame );
150 const SfxSlot* pSlot = rPool.GetSlot( m_nSlotID );
151
152 SfxItemState eState = SfxItemState::DISABLED;
153 std::unique_ptr<SfxPoolItem> pItem;
154 if ( rEvent.IsEnabled )
155 {
156 eState = SfxItemState::DEFAULT;
157 css::uno::Type aType = rEvent.State.getValueType();
158
159 if ( aType == ::cppu::UnoType<void>::get() )
160 {
161 pItem.reset(new SfxVoidItem( m_nSlotID ));
162 eState = SfxItemState::UNKNOWN;
163 }
164 else if ( aType == cppu::UnoType< bool >::get() )
165 {
166 bool bTemp = false;
167 rEvent.State >>= bTemp ;
168 pItem.reset(new SfxBoolItem( m_nSlotID, bTemp ));
169 }
171 {
172 sal_uInt16 nTemp = 0;
173 rEvent.State >>= nTemp ;
174 pItem.reset(new SfxUInt16Item( m_nSlotID, nTemp ));
175 }
176 else if ( aType == cppu::UnoType<sal_uInt32>::get() )
177 {
178 sal_uInt32 nTemp = 0;
179 rEvent.State >>= nTemp ;
180 pItem.reset(new SfxUInt32Item( m_nSlotID, nTemp ));
181 }
182 else if ( aType == cppu::UnoType<OUString>::get() )
183 {
184 OUString sTemp ;
185 rEvent.State >>= sTemp ;
186 pItem.reset(new SfxStringItem( m_nSlotID, sTemp ));
187 }
189 {
190 ItemStatus aItemStatus;
191 rEvent.State >>= aItemStatus;
192 eState = static_cast<SfxItemState>(aItemStatus.State);
193 pItem.reset(new SfxVoidItem( m_nSlotID ));
194 }
196 {
197 Visibility aVisibilityStatus;
198 rEvent.State >>= aVisibilityStatus;
199 pItem.reset(new SfxVisibilityItem( m_nSlotID, aVisibilityStatus.bVisible ));
200 }
201 else
202 {
203 if ( pSlot )
204 pItem = pSlot->GetType()->CreateItem();
205 if ( pItem )
206 {
207 pItem->SetWhich( m_nSlotID );
208 pItem->PutValue( rEvent.State, 0 );
209 }
210 else
211 pItem.reset(new SfxVoidItem( m_nSlotID ));
212 }
213 }
214
215 StateChangedAtStatusListener( eState, pItem.get() );
216}
217
218/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::frame::XDispatch > m_xDispatch
static SfxSlotPool & GetSlotPool(SfxViewFrame *pFrame=nullptr)
Definition: msgpool.cxx:316
const SfxSlot * GetSlot(sal_uInt16 nId) const
Definition: msgpool.cxx:155
Definition: msg.hxx:184
const SfxType * GetType() const
Definition: msg.hxx:236
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual ~SfxStatusListener() override
css::util::URL m_aCommand
SfxStatusListener(const css::uno::Reference< css::frame::XDispatchProvider > &rDispatchProvider, sal_uInt16 nSlotId, const OUString &aCommand)
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &xListener) override
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
css::uno::Reference< css::frame::XDispatch > m_xDispatch
css::uno::Reference< css::frame::XDispatchProvider > m_xDispatchProvider
virtual void SAL_CALL dispose() override
virtual void StateChangedAtStatusListener(SfxItemState eState, const SfxPoolItem *pState)
SfxFrame & GetFrame() const
Definition: viewfrm.cxx:2782
@ Exception
Visibility
SfxItemState
std::unique_ptr< SfxPoolItem > CreateItem() const
Definition: msg.hxx:111