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/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/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/frame/status/ItemStatus.hpp>
35 : #include <com/sun/star/frame/status/ItemState.hpp>
36 : #include <com/sun/star/frame/status/Visibility.hpp>
37 :
38 : using ::rtl::OUString;
39 : using namespace ::cppu;
40 : using namespace ::com::sun::star::uno;
41 : using namespace ::com::sun::star::frame;
42 : using namespace ::com::sun::star::frame::status;
43 : using namespace ::com::sun::star::lang;
44 : using namespace ::com::sun::star::util;
45 :
46 : class SfxQueryStatus_Impl : public ::com::sun::star::frame::XStatusListener ,
47 : public ::com::sun::star::lang::XTypeProvider ,
48 : public ::cppu::OWeakObject
49 : {
50 : public:
51 : SFX_DECL_XINTERFACE_XTYPEPROVIDER
52 :
53 : SfxQueryStatus_Impl( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const rtl::OUString& aCommand );
54 : virtual ~SfxQueryStatus_Impl();
55 :
56 : // Query method
57 : SfxItemState QueryState( SfxPoolItem*& pPoolItem );
58 :
59 : // XEventListener
60 : virtual void SAL_CALL disposing(const ::com::sun::star::lang::EventObject& Source) throw( ::com::sun::star::uno::RuntimeException );
61 :
62 : // XStatusListener
63 : virtual void SAL_CALL statusChanged(const ::com::sun::star::frame::FeatureStateEvent& Event) throw( ::com::sun::star::uno::RuntimeException );
64 :
65 : private:
66 : SfxQueryStatus_Impl( const SfxQueryStatus& );
67 : SfxQueryStatus_Impl();
68 : SfxQueryStatus_Impl& operator=( const SfxQueryStatus& );
69 :
70 : sal_Bool m_bQueryInProgress;
71 : SfxItemState m_eState;
72 : SfxPoolItem* m_pItem;
73 : sal_uInt16 m_nSlotID;
74 : osl::Condition m_aCondition;
75 : ::com::sun::star::util::URL m_aCommand;
76 : com::sun::star::uno::Reference< com::sun::star::frame::XDispatch > m_xDispatch;
77 : };
78 :
79 0 : SFX_IMPL_XINTERFACE_2( SfxQueryStatus_Impl, OWeakObject, ::com::sun::star::frame::XStatusListener, ::com::sun::star::lang::XEventListener )
80 0 : SFX_IMPL_XTYPEPROVIDER_2( SfxQueryStatus_Impl, ::com::sun::star::frame::XStatusListener, ::com::sun::star::lang::XEventListener )
81 :
82 0 : SfxQueryStatus_Impl::SfxQueryStatus_Impl( const Reference< XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& rCommand ) :
83 : cppu::OWeakObject(),
84 : m_bQueryInProgress( sal_False ),
85 : m_eState( SFX_ITEM_DISABLED ),
86 : m_pItem( 0 ),
87 0 : m_nSlotID( nSlotId )
88 : {
89 0 : m_aCommand.Complete = rCommand;
90 0 : Reference< XURLTransformer > xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
91 0 : xTrans->parseStrict( m_aCommand );
92 0 : if ( rDispatchProvider.is() )
93 0 : m_xDispatch = rDispatchProvider->queryDispatch( m_aCommand, rtl::OUString(), 0 );
94 0 : m_aCondition.reset();
95 0 : }
96 :
97 0 : SfxQueryStatus_Impl::~SfxQueryStatus_Impl()
98 : {
99 0 : }
100 :
101 0 : void SAL_CALL SfxQueryStatus_Impl::disposing( const EventObject& )
102 : throw( RuntimeException )
103 : {
104 0 : SolarMutexGuard aGuard;
105 0 : m_xDispatch.clear();
106 0 : }
107 :
108 0 : void SAL_CALL SfxQueryStatus_Impl::statusChanged( const FeatureStateEvent& rEvent)
109 : throw( RuntimeException )
110 : {
111 0 : SolarMutexGuard aGuard;
112 :
113 0 : m_pItem = NULL;
114 0 : m_eState = SFX_ITEM_DISABLED;
115 :
116 0 : if ( rEvent.IsEnabled )
117 : {
118 0 : m_eState = SFX_ITEM_AVAILABLE;
119 0 : ::com::sun::star::uno::Type pType = rEvent.State.getValueType();
120 :
121 0 : if ( pType == ::getBooleanCppuType() )
122 : {
123 0 : sal_Bool bTemp = false;
124 0 : rEvent.State >>= bTemp ;
125 0 : m_pItem = new SfxBoolItem( m_nSlotID, bTemp );
126 : }
127 0 : else if ( pType == ::getCppuType((const sal_uInt16*)0) )
128 : {
129 0 : sal_uInt16 nTemp = 0;
130 0 : rEvent.State >>= nTemp ;
131 0 : m_pItem = new SfxUInt16Item( m_nSlotID, nTemp );
132 : }
133 0 : else if ( pType == ::getCppuType((const sal_uInt32*)0) )
134 : {
135 0 : sal_uInt32 nTemp = 0;
136 0 : rEvent.State >>= nTemp ;
137 0 : m_pItem = new SfxUInt32Item( m_nSlotID, nTemp );
138 : }
139 0 : else if ( pType == ::getCppuType((const ::rtl::OUString*)0) )
140 : {
141 0 : ::rtl::OUString sTemp ;
142 0 : rEvent.State >>= sTemp ;
143 0 : m_pItem = new SfxStringItem( m_nSlotID, sTemp );
144 : }
145 0 : else if ( pType == ::getCppuType((const ::com::sun::star::frame::status::ItemStatus*)0) )
146 : {
147 0 : ItemStatus aItemStatus;
148 0 : rEvent.State >>= aItemStatus;
149 0 : m_eState = aItemStatus.State;
150 0 : m_pItem = new SfxVoidItem( m_nSlotID );
151 : }
152 0 : else if ( pType == ::getCppuType((const ::com::sun::star::frame::status::Visibility*)0) )
153 : {
154 0 : Visibility aVisibilityStatus;
155 0 : rEvent.State >>= aVisibilityStatus;
156 0 : m_pItem = new SfxVisibilityItem( m_nSlotID, aVisibilityStatus.bVisible );
157 : }
158 : else
159 : {
160 0 : m_eState = SFX_ITEM_UNKNOWN;
161 0 : m_pItem = new SfxVoidItem( m_nSlotID );
162 0 : }
163 : }
164 :
165 0 : if ( m_pItem )
166 0 : DeleteItemOnIdle( m_pItem );
167 :
168 : try
169 : {
170 0 : m_aCondition.set();
171 0 : m_xDispatch->removeStatusListener( Reference< XStatusListener >( static_cast< cppu::OWeakObject* >( this ), UNO_QUERY ),
172 0 : m_aCommand );
173 : }
174 0 : catch ( Exception& )
175 : {
176 0 : }
177 0 : }
178 :
179 : // Query method
180 0 : SfxItemState SfxQueryStatus_Impl::QueryState( SfxPoolItem*& rpPoolItem )
181 : {
182 0 : SolarMutexGuard aGuard;
183 0 : if ( !m_bQueryInProgress )
184 : {
185 0 : m_pItem = NULL;
186 0 : m_eState = SFX_ITEM_DISABLED;
187 :
188 0 : if ( m_xDispatch.is() )
189 : {
190 : try
191 : {
192 0 : m_aCondition.reset();
193 0 : m_bQueryInProgress = sal_True;
194 0 : m_xDispatch->addStatusListener( Reference< XStatusListener >( static_cast< cppu::OWeakObject* >( this ), UNO_QUERY ),
195 0 : m_aCommand );
196 : }
197 0 : catch ( Exception& )
198 : {
199 0 : m_aCondition.set();
200 : }
201 : }
202 : else
203 0 : m_aCondition.set();
204 : }
205 :
206 0 : m_aCondition.wait();
207 :
208 0 : m_bQueryInProgress = sal_False;
209 0 : rpPoolItem = m_pItem;
210 0 : return m_eState;
211 : }
212 :
213 : //*************************************************************************
214 :
215 0 : SfxQueryStatus::SfxQueryStatus( const Reference< XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& rCommand )
216 : {
217 0 : m_pSfxQueryStatusImpl = new SfxQueryStatus_Impl( rDispatchProvider, nSlotId, rCommand );
218 : m_xStatusListener = Reference< XStatusListener >(
219 0 : static_cast< cppu::OWeakObject* >( m_pSfxQueryStatusImpl ),
220 0 : UNO_QUERY );
221 0 : }
222 :
223 0 : SfxQueryStatus::~SfxQueryStatus()
224 : {
225 0 : }
226 :
227 0 : SfxItemState SfxQueryStatus::QueryState( SfxPoolItem*& rpPoolItem )
228 : {
229 0 : SolarMutexGuard aGuard;
230 0 : return m_pSfxQueryStatusImpl->QueryState( rpPoolItem );
231 : }
232 :
233 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|