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 <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>
38 using namespace ::cppu
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::frame
;
41 using namespace ::com::sun::star::frame::status
;
42 using namespace ::com::sun::star::lang
;
43 using namespace ::com::sun::star::util
;
45 class SfxQueryStatus_Impl
: public ::com::sun::star::frame::XStatusListener
,
46 public ::com::sun::star::lang::XTypeProvider
,
47 public ::cppu::OWeakObject
50 SFX_DECL_XINTERFACE_XTYPEPROVIDER
52 SfxQueryStatus_Impl( const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XDispatchProvider
>& rDispatchProvider
, sal_uInt16 nSlotId
, const OUString
& aCommand
);
53 virtual ~SfxQueryStatus_Impl();
56 SfxItemState
QueryState( SfxPoolItem
*& pPoolItem
);
59 virtual void SAL_CALL
disposing(const ::com::sun::star::lang::EventObject
& Source
) throw( ::com::sun::star::uno::RuntimeException
);
62 virtual void SAL_CALL
statusChanged(const ::com::sun::star::frame::FeatureStateEvent
& Event
) throw( ::com::sun::star::uno::RuntimeException
);
65 SfxQueryStatus_Impl( const SfxQueryStatus
& );
66 SfxQueryStatus_Impl();
67 SfxQueryStatus_Impl
& operator=( const SfxQueryStatus
& );
69 sal_Bool m_bQueryInProgress
;
70 SfxItemState m_eState
;
73 osl::Condition m_aCondition
;
74 ::com::sun::star::util::URL m_aCommand
;
75 com::sun::star::uno::Reference
< com::sun::star::frame::XDispatch
> m_xDispatch
;
78 SFX_IMPL_XINTERFACE_2( SfxQueryStatus_Impl
, OWeakObject
, ::com::sun::star::frame::XStatusListener
, ::com::sun::star::lang::XEventListener
)
79 SFX_IMPL_XTYPEPROVIDER_2( SfxQueryStatus_Impl
, ::com::sun::star::frame::XStatusListener
, ::com::sun::star::lang::XEventListener
)
81 SfxQueryStatus_Impl::SfxQueryStatus_Impl( const Reference
< XDispatchProvider
>& rDispatchProvider
, sal_uInt16 nSlotId
, const OUString
& rCommand
) :
83 m_bQueryInProgress( sal_False
),
84 m_eState( SFX_ITEM_DISABLED
),
88 m_aCommand
.Complete
= rCommand
;
89 Reference
< XURLTransformer
> xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
90 xTrans
->parseStrict( m_aCommand
);
91 if ( rDispatchProvider
.is() )
92 m_xDispatch
= rDispatchProvider
->queryDispatch( m_aCommand
, OUString(), 0 );
96 SfxQueryStatus_Impl::~SfxQueryStatus_Impl()
100 void SAL_CALL
SfxQueryStatus_Impl::disposing( const EventObject
& )
101 throw( RuntimeException
)
103 SolarMutexGuard aGuard
;
107 void SAL_CALL
SfxQueryStatus_Impl::statusChanged( const FeatureStateEvent
& rEvent
)
108 throw( RuntimeException
)
110 SolarMutexGuard aGuard
;
113 m_eState
= SFX_ITEM_DISABLED
;
115 if ( rEvent
.IsEnabled
)
117 m_eState
= SFX_ITEM_AVAILABLE
;
118 ::com::sun::star::uno::Type pType
= rEvent
.State
.getValueType();
120 if ( pType
== ::getBooleanCppuType() )
122 sal_Bool bTemp
= false;
123 rEvent
.State
>>= bTemp
;
124 m_pItem
= new SfxBoolItem( m_nSlotID
, bTemp
);
126 else if ( pType
== ::getCppuType((const sal_uInt16
*)0) )
128 sal_uInt16 nTemp
= 0;
129 rEvent
.State
>>= nTemp
;
130 m_pItem
= new SfxUInt16Item( m_nSlotID
, nTemp
);
132 else if ( pType
== ::getCppuType((const sal_uInt32
*)0) )
134 sal_uInt32 nTemp
= 0;
135 rEvent
.State
>>= nTemp
;
136 m_pItem
= new SfxUInt32Item( m_nSlotID
, nTemp
);
138 else if ( pType
== ::getCppuType((const OUString
*)0) )
141 rEvent
.State
>>= sTemp
;
142 m_pItem
= new SfxStringItem( m_nSlotID
, sTemp
);
144 else if ( pType
== ::getCppuType((const ::com::sun::star::frame::status::ItemStatus
*)0) )
146 ItemStatus aItemStatus
;
147 rEvent
.State
>>= aItemStatus
;
148 m_eState
= aItemStatus
.State
;
149 m_pItem
= new SfxVoidItem( m_nSlotID
);
151 else if ( pType
== ::getCppuType((const ::com::sun::star::frame::status::Visibility
*)0) )
153 Visibility aVisibilityStatus
;
154 rEvent
.State
>>= aVisibilityStatus
;
155 m_pItem
= new SfxVisibilityItem( m_nSlotID
, aVisibilityStatus
.bVisible
);
159 m_eState
= SFX_ITEM_UNKNOWN
;
160 m_pItem
= new SfxVoidItem( m_nSlotID
);
165 DeleteItemOnIdle( m_pItem
);
170 m_xDispatch
->removeStatusListener( Reference
< XStatusListener
>( static_cast< cppu::OWeakObject
* >( this ), UNO_QUERY
),
179 SfxItemState
SfxQueryStatus_Impl::QueryState( SfxPoolItem
*& rpPoolItem
)
181 SolarMutexGuard aGuard
;
182 if ( !m_bQueryInProgress
)
185 m_eState
= SFX_ITEM_DISABLED
;
187 if ( m_xDispatch
.is() )
191 m_aCondition
.reset();
192 m_bQueryInProgress
= sal_True
;
193 m_xDispatch
->addStatusListener( Reference
< XStatusListener
>( static_cast< cppu::OWeakObject
* >( this ), UNO_QUERY
),
207 m_bQueryInProgress
= sal_False
;
208 rpPoolItem
= m_pItem
;
212 //*************************************************************************
214 SfxQueryStatus::SfxQueryStatus( const Reference
< XDispatchProvider
>& rDispatchProvider
, sal_uInt16 nSlotId
, const OUString
& rCommand
)
216 m_pSfxQueryStatusImpl
= new SfxQueryStatus_Impl( rDispatchProvider
, nSlotId
, rCommand
);
217 m_xStatusListener
= Reference
< XStatusListener
>(
218 static_cast< cppu::OWeakObject
* >( m_pSfxQueryStatusImpl
),
222 SfxQueryStatus::~SfxQueryStatus()
226 SfxItemState
SfxQueryStatus::QueryState( SfxPoolItem
*& rpPoolItem
)
228 SolarMutexGuard aGuard
;
229 return m_pSfxQueryStatusImpl
->QueryState( rpPoolItem
);
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */