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