1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
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 "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>
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
;
48 class SfxQueryStatus_Impl
:
49 public cppu::WeakImplHelper1
<css::frame::XStatusListener
>,
50 private boost::noncopyable
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();
58 SfxItemState
QueryState( SfxPoolItem
*& pPoolItem
);
61 virtual void SAL_CALL
disposing(const ::com::sun::star::lang::EventObject
& Source
) throw( ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
64 virtual void SAL_CALL
statusChanged(const ::com::sun::star::frame::FeatureStateEvent
& Event
) throw( ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
67 bool m_bQueryInProgress
;
68 SfxItemState m_eState
;
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
;
76 SfxQueryStatus_Impl::SfxQueryStatus_Impl( const Reference
< XDispatchProvider
>& rDispatchProvider
, sal_uInt16 nSlotId
, const OUString
& rCommand
) :
77 m_bQueryInProgress( false ),
78 m_eState( SfxItemState::DISABLED
),
82 m_aCommand
.Complete
= rCommand
;
83 Reference
< XURLTransformer
> xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
84 xTrans
->parseStrict( m_aCommand
);
85 if ( rDispatchProvider
.is() )
86 m_xDispatch
= rDispatchProvider
->queryDispatch( m_aCommand
, OUString(), 0 );
90 SfxQueryStatus_Impl::~SfxQueryStatus_Impl()
94 void SAL_CALL
SfxQueryStatus_Impl::disposing( const EventObject
& )
95 throw( RuntimeException
, std::exception
)
97 SolarMutexGuard aGuard
;
101 void SAL_CALL
SfxQueryStatus_Impl::statusChanged( const FeatureStateEvent
& rEvent
)
102 throw( RuntimeException
, std::exception
)
104 SolarMutexGuard aGuard
;
107 m_eState
= SfxItemState::DISABLED
;
109 if ( rEvent
.IsEnabled
)
111 m_eState
= SfxItemState::DEFAULT
;
112 ::com::sun::star::uno::Type pType
= rEvent
.State
.getValueType();
114 if ( pType
== cppu::UnoType
<bool>::get() )
117 rEvent
.State
>>= bTemp
;
118 m_pItem
= new SfxBoolItem( m_nSlotID
, bTemp
);
120 else if ( pType
== ::cppu::UnoType
< ::cppu::UnoUnsignedShortType
>::get() )
122 sal_uInt16 nTemp
= 0;
123 rEvent
.State
>>= nTemp
;
124 m_pItem
= new SfxUInt16Item( m_nSlotID
, nTemp
);
126 else if ( pType
== cppu::UnoType
<sal_uInt32
>::get() )
128 sal_uInt32 nTemp
= 0;
129 rEvent
.State
>>= nTemp
;
130 m_pItem
= new SfxUInt32Item( m_nSlotID
, nTemp
);
132 else if ( pType
== cppu::UnoType
<OUString
>::get() )
135 rEvent
.State
>>= sTemp
;
136 m_pItem
= new SfxStringItem( m_nSlotID
, sTemp
);
138 else if ( pType
== cppu::UnoType
< ::com::sun::star::frame::status::ItemStatus
>::get() )
140 ItemStatus aItemStatus
;
141 rEvent
.State
>>= aItemStatus
;
142 m_eState
= (SfxItemState
) aItemStatus
.State
;
143 m_pItem
= new SfxVoidItem( m_nSlotID
);
145 else if ( pType
== cppu::UnoType
< ::com::sun::star::frame::status::Visibility
>::get() )
147 Visibility aVisibilityStatus
;
148 rEvent
.State
>>= aVisibilityStatus
;
149 m_pItem
= new SfxVisibilityItem( m_nSlotID
, aVisibilityStatus
.bVisible
);
153 m_eState
= SfxItemState::UNKNOWN
;
154 m_pItem
= new SfxVoidItem( m_nSlotID
);
159 DeleteItemOnIdle( m_pItem
);
164 m_xDispatch
->removeStatusListener( Reference
< XStatusListener
>( static_cast< cppu::OWeakObject
* >( this ), UNO_QUERY
),
173 SfxItemState
SfxQueryStatus_Impl::QueryState( SfxPoolItem
*& rpPoolItem
)
175 SolarMutexGuard aGuard
;
176 if ( !m_bQueryInProgress
)
179 m_eState
= SfxItemState::DISABLED
;
181 if ( m_xDispatch
.is() )
185 m_aCondition
.reset();
186 m_bQueryInProgress
= true;
187 m_xDispatch
->addStatusListener( Reference
< XStatusListener
>( static_cast< cppu::OWeakObject
* >( this ), UNO_QUERY
),
201 m_bQueryInProgress
= false;
202 rpPoolItem
= m_pItem
;
208 SfxQueryStatus::SfxQueryStatus( const Reference
< XDispatchProvider
>& rDispatchProvider
, sal_uInt16 nSlotId
, const OUString
& rCommand
)
210 m_pSfxQueryStatusImpl
= new SfxQueryStatus_Impl( rDispatchProvider
, nSlotId
, rCommand
);
211 m_xStatusListener
= Reference
< XStatusListener
>(
212 static_cast< cppu::OWeakObject
* >( m_pSfxQueryStatusImpl
),
216 SfxQueryStatus::~SfxQueryStatus()
220 SfxItemState
SfxQueryStatus::QueryState( SfxPoolItem
*& rpPoolItem
)
222 SolarMutexGuard aGuard
;
223 return m_pSfxQueryStatusImpl
->QueryState( rpPoolItem
);
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */