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