bump product version to 4.2.0.1
[LibreOffice.git] / sfx2 / source / control / querystatus.cxx
blob47a456f71673f0c6c8d934a022b1c0c484f43dce
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/querystatus.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/implbase1.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/frame/status/ItemStatus.hpp>
35 #include <com/sun/star/frame/status/ItemState.hpp>
36 #include <com/sun/star/frame/status/Visibility.hpp>
38 using namespace ::cppu;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::frame;
41 using namespace ::com::sun::star::frame::status;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star::util;
45 class SfxQueryStatus_Impl : public ::cppu::WeakImplHelper1< css::frame::XStatusListener >
47 public:
49 SfxQueryStatus_Impl( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& aCommand );
50 virtual ~SfxQueryStatus_Impl();
52 // Query method
53 SfxItemState QueryState( SfxPoolItem*& pPoolItem );
55 // XEventListener
56 virtual void SAL_CALL disposing(const ::com::sun::star::lang::EventObject& Source) throw( ::com::sun::star::uno::RuntimeException );
58 // XStatusListener
59 virtual void SAL_CALL statusChanged(const ::com::sun::star::frame::FeatureStateEvent& Event) throw( ::com::sun::star::uno::RuntimeException );
61 private:
62 SfxQueryStatus_Impl( const SfxQueryStatus& );
63 SfxQueryStatus_Impl();
64 SfxQueryStatus_Impl& operator=( const SfxQueryStatus& );
66 sal_Bool m_bQueryInProgress;
67 SfxItemState m_eState;
68 SfxPoolItem* m_pItem;
69 sal_uInt16 m_nSlotID;
70 osl::Condition m_aCondition;
71 ::com::sun::star::util::URL m_aCommand;
72 com::sun::star::uno::Reference< com::sun::star::frame::XDispatch > m_xDispatch;
75 SfxQueryStatus_Impl::SfxQueryStatus_Impl( const Reference< XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& rCommand ) :
76 m_bQueryInProgress( sal_False ),
77 m_eState( SFX_ITEM_DISABLED ),
78 m_pItem( 0 ),
79 m_nSlotID( nSlotId )
81 m_aCommand.Complete = rCommand;
82 Reference< XURLTransformer > xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
83 xTrans->parseStrict( m_aCommand );
84 if ( rDispatchProvider.is() )
85 m_xDispatch = rDispatchProvider->queryDispatch( m_aCommand, OUString(), 0 );
86 m_aCondition.reset();
89 SfxQueryStatus_Impl::~SfxQueryStatus_Impl()
93 void SAL_CALL SfxQueryStatus_Impl::disposing( const EventObject& )
94 throw( RuntimeException )
96 SolarMutexGuard aGuard;
97 m_xDispatch.clear();
100 void SAL_CALL SfxQueryStatus_Impl::statusChanged( const FeatureStateEvent& rEvent)
101 throw( RuntimeException )
103 SolarMutexGuard aGuard;
105 m_pItem = NULL;
106 m_eState = SFX_ITEM_DISABLED;
108 if ( rEvent.IsEnabled )
110 m_eState = SFX_ITEM_AVAILABLE;
111 ::com::sun::star::uno::Type pType = rEvent.State.getValueType();
113 if ( pType == ::getBooleanCppuType() )
115 sal_Bool bTemp = false;
116 rEvent.State >>= bTemp ;
117 m_pItem = new SfxBoolItem( m_nSlotID, bTemp );
119 else if ( pType == ::getCppuType((const sal_uInt16*)0) )
121 sal_uInt16 nTemp = 0;
122 rEvent.State >>= nTemp ;
123 m_pItem = new SfxUInt16Item( m_nSlotID, nTemp );
125 else if ( pType == ::getCppuType((const sal_uInt32*)0) )
127 sal_uInt32 nTemp = 0;
128 rEvent.State >>= nTemp ;
129 m_pItem = new SfxUInt32Item( m_nSlotID, nTemp );
131 else if ( pType == ::getCppuType((const OUString*)0) )
133 OUString sTemp ;
134 rEvent.State >>= sTemp ;
135 m_pItem = new SfxStringItem( m_nSlotID, sTemp );
137 else if ( pType == ::getCppuType((const ::com::sun::star::frame::status::ItemStatus*)0) )
139 ItemStatus aItemStatus;
140 rEvent.State >>= aItemStatus;
141 m_eState = aItemStatus.State;
142 m_pItem = new SfxVoidItem( m_nSlotID );
144 else if ( pType == ::getCppuType((const ::com::sun::star::frame::status::Visibility*)0) )
146 Visibility aVisibilityStatus;
147 rEvent.State >>= aVisibilityStatus;
148 m_pItem = new SfxVisibilityItem( m_nSlotID, aVisibilityStatus.bVisible );
150 else
152 m_eState = SFX_ITEM_UNKNOWN;
153 m_pItem = new SfxVoidItem( m_nSlotID );
157 if ( m_pItem )
158 DeleteItemOnIdle( m_pItem );
162 m_aCondition.set();
163 m_xDispatch->removeStatusListener( Reference< XStatusListener >( static_cast< cppu::OWeakObject* >( this ), UNO_QUERY ),
164 m_aCommand );
166 catch ( Exception& )
171 // Query method
172 SfxItemState SfxQueryStatus_Impl::QueryState( SfxPoolItem*& rpPoolItem )
174 SolarMutexGuard aGuard;
175 if ( !m_bQueryInProgress )
177 m_pItem = NULL;
178 m_eState = SFX_ITEM_DISABLED;
180 if ( m_xDispatch.is() )
184 m_aCondition.reset();
185 m_bQueryInProgress = sal_True;
186 m_xDispatch->addStatusListener( Reference< XStatusListener >( static_cast< cppu::OWeakObject* >( this ), UNO_QUERY ),
187 m_aCommand );
189 catch ( Exception& )
191 m_aCondition.set();
194 else
195 m_aCondition.set();
198 m_aCondition.wait();
200 m_bQueryInProgress = sal_False;
201 rpPoolItem = m_pItem;
202 return m_eState;
205 //*************************************************************************
207 SfxQueryStatus::SfxQueryStatus( const Reference< XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& rCommand )
209 m_pSfxQueryStatusImpl = new SfxQueryStatus_Impl( rDispatchProvider, nSlotId, rCommand );
210 m_xStatusListener = Reference< XStatusListener >(
211 static_cast< cppu::OWeakObject* >( m_pSfxQueryStatusImpl ),
212 UNO_QUERY );
215 SfxQueryStatus::~SfxQueryStatus()
219 SfxItemState SfxQueryStatus::QueryState( SfxPoolItem*& rpPoolItem )
221 SolarMutexGuard aGuard;
222 return m_pSfxQueryStatusImpl->QueryState( rpPoolItem );
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */