1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
30*cdf0e10cSrcweir #include <sfx2/querystatus.hxx>
31*cdf0e10cSrcweir #include <svl/poolitem.hxx>
32*cdf0e10cSrcweir #include <svl/eitem.hxx>
33*cdf0e10cSrcweir #include <svl/stritem.hxx>
34*cdf0e10cSrcweir #include <svl/intitem.hxx>
35*cdf0e10cSrcweir #include <svl/itemset.hxx>
36*cdf0e10cSrcweir #include <svtools/itemdel.hxx>
37*cdf0e10cSrcweir #include <svl/visitem.hxx>
38*cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
39*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
40*cdf0e10cSrcweir #include <vos/mutex.hxx>
41*cdf0e10cSrcweir #include <vcl/svapp.hxx>
42*cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/frame/status/ItemStatus.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/frame/status/ItemState.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/frame/status/Visibility.hpp>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using ::rtl::OUString;
48*cdf0e10cSrcweir using namespace ::cppu;
49*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
50*cdf0e10cSrcweir using namespace ::com::sun::star::frame;
51*cdf0e10cSrcweir using namespace ::com::sun::star::frame::status;
52*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
53*cdf0e10cSrcweir using namespace ::com::sun::star::util;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir class SfxQueryStatus_Impl : public ::com::sun::star::frame::XStatusListener	,
56*cdf0e10cSrcweir 					        public ::com::sun::star::lang::XTypeProvider	,
57*cdf0e10cSrcweir 					        public ::cppu::OWeakObject
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     public:
60*cdf0e10cSrcweir 	    SFX_DECL_XINTERFACE_XTYPEPROVIDER
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir         SfxQueryStatus_Impl( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const rtl::OUString& aCommand );
63*cdf0e10cSrcweir         virtual ~SfxQueryStatus_Impl();
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir         // Query method
66*cdf0e10cSrcweir         SfxItemState QueryState( SfxPoolItem*& pPoolItem );
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir         // XEventListener
69*cdf0e10cSrcweir 	    virtual void SAL_CALL disposing(const ::com::sun::star::lang::EventObject& Source) throw( ::com::sun::star::uno::RuntimeException );
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir         // XStatusListener
72*cdf0e10cSrcweir 	    virtual void SAL_CALL statusChanged(const ::com::sun::star::frame::FeatureStateEvent& Event) throw( ::com::sun::star::uno::RuntimeException );
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     private:
75*cdf0e10cSrcweir         SfxQueryStatus_Impl( const SfxQueryStatus& );
76*cdf0e10cSrcweir         SfxQueryStatus_Impl();
77*cdf0e10cSrcweir         SfxQueryStatus_Impl& operator=( const SfxQueryStatus& );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         sal_Bool                                                                   m_bQueryInProgress;
80*cdf0e10cSrcweir         SfxItemState                                                               m_eState;
81*cdf0e10cSrcweir         SfxPoolItem*                                                               m_pItem;
82*cdf0e10cSrcweir         sal_uInt16                                                                     m_nSlotID;
83*cdf0e10cSrcweir         osl::Condition                                                             m_aCondition;
84*cdf0e10cSrcweir         ::com::sun::star::util::URL                                                m_aCommand;
85*cdf0e10cSrcweir         com::sun::star::uno::Reference< com::sun::star::frame::XDispatch >         m_xDispatch;
86*cdf0e10cSrcweir };
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir SFX_IMPL_XINTERFACE_2( SfxQueryStatus_Impl, OWeakObject, ::com::sun::star::frame::XStatusListener, ::com::sun::star::lang::XEventListener )
89*cdf0e10cSrcweir SFX_IMPL_XTYPEPROVIDER_2( SfxQueryStatus_Impl, ::com::sun::star::frame::XStatusListener, ::com::sun::star::lang::XEventListener )
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir SfxQueryStatus_Impl::SfxQueryStatus_Impl( const Reference< XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& rCommand ) :
92*cdf0e10cSrcweir     cppu::OWeakObject(),
93*cdf0e10cSrcweir     m_bQueryInProgress( sal_False ),
94*cdf0e10cSrcweir     m_eState( SFX_ITEM_DISABLED ),
95*cdf0e10cSrcweir     m_pItem( 0 ),
96*cdf0e10cSrcweir     m_nSlotID( nSlotId )
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     m_aCommand.Complete = rCommand;
99*cdf0e10cSrcweir     Reference < XURLTransformer > xTrans( ::comphelper::getProcessServiceFactory()->createInstance(
100*cdf0e10cSrcweir                                             rtl::OUString::createFromAscii("com.sun.star.util.URLTransformer" )), UNO_QUERY );
101*cdf0e10cSrcweir     xTrans->parseStrict( m_aCommand );
102*cdf0e10cSrcweir     if ( rDispatchProvider.is() )
103*cdf0e10cSrcweir         m_xDispatch = rDispatchProvider->queryDispatch( m_aCommand, rtl::OUString(), 0 );
104*cdf0e10cSrcweir     m_aCondition.reset();
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir SfxQueryStatus_Impl::~SfxQueryStatus_Impl()
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir void SAL_CALL SfxQueryStatus_Impl::disposing( const EventObject& )
112*cdf0e10cSrcweir throw( RuntimeException )
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
115*cdf0e10cSrcweir     m_xDispatch.clear();
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir void SAL_CALL SfxQueryStatus_Impl::statusChanged( const FeatureStateEvent& rEvent)
119*cdf0e10cSrcweir throw( RuntimeException )
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     m_pItem  = NULL;
124*cdf0e10cSrcweir     m_eState = SFX_ITEM_DISABLED;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     if ( rEvent.IsEnabled )
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		m_eState = SFX_ITEM_AVAILABLE;
129*cdf0e10cSrcweir 		::com::sun::star::uno::Type pType =	rEvent.State.getValueType();
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		if ( pType == ::getBooleanCppuType() )
132*cdf0e10cSrcweir 		{
133*cdf0e10cSrcweir 			sal_Bool bTemp = false;
134*cdf0e10cSrcweir 			rEvent.State >>= bTemp ;
135*cdf0e10cSrcweir 			m_pItem = new SfxBoolItem( m_nSlotID, bTemp );
136*cdf0e10cSrcweir 		}
137*cdf0e10cSrcweir 		else if ( pType == ::getCppuType((const sal_uInt16*)0) )
138*cdf0e10cSrcweir 		{
139*cdf0e10cSrcweir 			sal_uInt16 nTemp = 0;
140*cdf0e10cSrcweir 			rEvent.State >>= nTemp ;
141*cdf0e10cSrcweir 			m_pItem = new SfxUInt16Item( m_nSlotID, nTemp );
142*cdf0e10cSrcweir 		}
143*cdf0e10cSrcweir 		else if ( pType == ::getCppuType((const sal_uInt32*)0) )
144*cdf0e10cSrcweir 		{
145*cdf0e10cSrcweir 			sal_uInt32 nTemp = 0;
146*cdf0e10cSrcweir 			rEvent.State >>= nTemp ;
147*cdf0e10cSrcweir 			m_pItem = new SfxUInt32Item( m_nSlotID, nTemp );
148*cdf0e10cSrcweir 		}
149*cdf0e10cSrcweir 		else if ( pType == ::getCppuType((const ::rtl::OUString*)0) )
150*cdf0e10cSrcweir 		{
151*cdf0e10cSrcweir 			::rtl::OUString sTemp ;
152*cdf0e10cSrcweir 			rEvent.State >>= sTemp ;
153*cdf0e10cSrcweir 			m_pItem = new SfxStringItem( m_nSlotID, sTemp );
154*cdf0e10cSrcweir 		}
155*cdf0e10cSrcweir         else if ( pType == ::getCppuType((const ::com::sun::star::frame::status::ItemStatus*)0) )
156*cdf0e10cSrcweir         {
157*cdf0e10cSrcweir             ItemStatus aItemStatus;
158*cdf0e10cSrcweir             rEvent.State >>= aItemStatus;
159*cdf0e10cSrcweir             m_eState = aItemStatus.State;
160*cdf0e10cSrcweir             m_pItem = new SfxVoidItem( m_nSlotID );
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir         else if ( pType == ::getCppuType((const ::com::sun::star::frame::status::Visibility*)0) )
163*cdf0e10cSrcweir         {
164*cdf0e10cSrcweir             Visibility aVisibilityStatus;
165*cdf0e10cSrcweir             rEvent.State >>= aVisibilityStatus;
166*cdf0e10cSrcweir             m_pItem = new SfxVisibilityItem( m_nSlotID, aVisibilityStatus.bVisible );
167*cdf0e10cSrcweir         }
168*cdf0e10cSrcweir 		else
169*cdf0e10cSrcweir         {
170*cdf0e10cSrcweir             m_eState = SFX_ITEM_UNKNOWN;
171*cdf0e10cSrcweir 			m_pItem  = new SfxVoidItem( m_nSlotID );
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir 	}
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 	if ( m_pItem )
176*cdf0e10cSrcweir         DeleteItemOnIdle( m_pItem );
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir     try
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir         m_aCondition.set();
181*cdf0e10cSrcweir         m_xDispatch->removeStatusListener( Reference< XStatusListener >( static_cast< cppu::OWeakObject* >( this ), UNO_QUERY ),
182*cdf0e10cSrcweir                                            m_aCommand );
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir     catch ( Exception& )
185*cdf0e10cSrcweir     {
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir // Query method
190*cdf0e10cSrcweir SfxItemState SfxQueryStatus_Impl::QueryState( SfxPoolItem*& rpPoolItem )
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
193*cdf0e10cSrcweir     if ( !m_bQueryInProgress )
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir         m_pItem  = NULL;
196*cdf0e10cSrcweir         m_eState = SFX_ITEM_DISABLED;
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir         if ( m_xDispatch.is() )
199*cdf0e10cSrcweir         {
200*cdf0e10cSrcweir             try
201*cdf0e10cSrcweir             {
202*cdf0e10cSrcweir                 m_aCondition.reset();
203*cdf0e10cSrcweir                 m_bQueryInProgress = sal_True;
204*cdf0e10cSrcweir                 m_xDispatch->addStatusListener( Reference< XStatusListener >( static_cast< cppu::OWeakObject* >( this ), UNO_QUERY ),
205*cdf0e10cSrcweir                                                 m_aCommand );
206*cdf0e10cSrcweir             }
207*cdf0e10cSrcweir             catch ( Exception& )
208*cdf0e10cSrcweir             {
209*cdf0e10cSrcweir                 m_aCondition.set();
210*cdf0e10cSrcweir             }
211*cdf0e10cSrcweir         }
212*cdf0e10cSrcweir         else
213*cdf0e10cSrcweir             m_aCondition.set();
214*cdf0e10cSrcweir     }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     m_aCondition.wait();
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     m_bQueryInProgress = sal_False;
219*cdf0e10cSrcweir     rpPoolItem = m_pItem;
220*cdf0e10cSrcweir     return m_eState;
221*cdf0e10cSrcweir }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir //*************************************************************************
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir SfxQueryStatus::SfxQueryStatus( const Reference< XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& rCommand )
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir     m_pSfxQueryStatusImpl = new SfxQueryStatus_Impl( rDispatchProvider, nSlotId, rCommand );
228*cdf0e10cSrcweir     m_xStatusListener     = Reference< XStatusListener >(
229*cdf0e10cSrcweir                                 static_cast< cppu::OWeakObject* >( m_pSfxQueryStatusImpl ),
230*cdf0e10cSrcweir                                 UNO_QUERY );
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir SfxQueryStatus::~SfxQueryStatus()
234*cdf0e10cSrcweir {
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir SfxItemState SfxQueryStatus::QueryState( SfxPoolItem*& rpPoolItem )
238*cdf0e10cSrcweir {
239*cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
240*cdf0e10cSrcweir     return m_pSfxQueryStatusImpl->QueryState( rpPoolItem );
241*cdf0e10cSrcweir }
242