1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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>
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
;
55 class SfxQueryStatus_Impl
: public ::com::sun::star::frame::XStatusListener
,
56 public ::com::sun::star::lang::XTypeProvider
,
57 public ::cppu::OWeakObject
60 SFX_DECL_XINTERFACE_XTYPEPROVIDER
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();
66 SfxItemState
QueryState( SfxPoolItem
*& pPoolItem
);
69 virtual void SAL_CALL
disposing(const ::com::sun::star::lang::EventObject
& Source
) throw( ::com::sun::star::uno::RuntimeException
);
72 virtual void SAL_CALL
statusChanged(const ::com::sun::star::frame::FeatureStateEvent
& Event
) throw( ::com::sun::star::uno::RuntimeException
);
75 SfxQueryStatus_Impl( const SfxQueryStatus
& );
76 SfxQueryStatus_Impl();
77 SfxQueryStatus_Impl
& operator=( const SfxQueryStatus
& );
79 sal_Bool m_bQueryInProgress
;
80 SfxItemState m_eState
;
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
;
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
)
91 SfxQueryStatus_Impl::SfxQueryStatus_Impl( const Reference
< XDispatchProvider
>& rDispatchProvider
, sal_uInt16 nSlotId
, const OUString
& rCommand
) :
93 m_bQueryInProgress( sal_False
),
94 m_eState( SFX_ITEM_DISABLED
),
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();
107 SfxQueryStatus_Impl::~SfxQueryStatus_Impl()
111 void SAL_CALL
SfxQueryStatus_Impl::disposing( const EventObject
& )
112 throw( RuntimeException
)
114 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
118 void SAL_CALL
SfxQueryStatus_Impl::statusChanged( const FeatureStateEvent
& rEvent
)
119 throw( RuntimeException
)
121 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
124 m_eState
= SFX_ITEM_DISABLED
;
126 if ( rEvent
.IsEnabled
)
128 m_eState
= SFX_ITEM_AVAILABLE
;
129 ::com::sun::star::uno::Type pType
= rEvent
.State
.getValueType();
131 if ( pType
== ::getBooleanCppuType() )
133 sal_Bool bTemp
= false;
134 rEvent
.State
>>= bTemp
;
135 m_pItem
= new SfxBoolItem( m_nSlotID
, bTemp
);
137 else if ( pType
== ::getCppuType((const sal_uInt16
*)0) )
139 sal_uInt16 nTemp
= 0;
140 rEvent
.State
>>= nTemp
;
141 m_pItem
= new SfxUInt16Item( m_nSlotID
, nTemp
);
143 else if ( pType
== ::getCppuType((const sal_uInt32
*)0) )
145 sal_uInt32 nTemp
= 0;
146 rEvent
.State
>>= nTemp
;
147 m_pItem
= new SfxUInt32Item( m_nSlotID
, nTemp
);
149 else if ( pType
== ::getCppuType((const ::rtl::OUString
*)0) )
151 ::rtl::OUString sTemp
;
152 rEvent
.State
>>= sTemp
;
153 m_pItem
= new SfxStringItem( m_nSlotID
, sTemp
);
155 else if ( pType
== ::getCppuType((const ::com::sun::star::frame::status::ItemStatus
*)0) )
157 ItemStatus aItemStatus
;
158 rEvent
.State
>>= aItemStatus
;
159 m_eState
= aItemStatus
.State
;
160 m_pItem
= new SfxVoidItem( m_nSlotID
);
162 else if ( pType
== ::getCppuType((const ::com::sun::star::frame::status::Visibility
*)0) )
164 Visibility aVisibilityStatus
;
165 rEvent
.State
>>= aVisibilityStatus
;
166 m_pItem
= new SfxVisibilityItem( m_nSlotID
, aVisibilityStatus
.bVisible
);
170 m_eState
= SFX_ITEM_UNKNOWN
;
171 m_pItem
= new SfxVoidItem( m_nSlotID
);
176 DeleteItemOnIdle( m_pItem
);
181 m_xDispatch
->removeStatusListener( Reference
< XStatusListener
>( static_cast< cppu::OWeakObject
* >( this ), UNO_QUERY
),
190 SfxItemState
SfxQueryStatus_Impl::QueryState( SfxPoolItem
*& rpPoolItem
)
192 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
193 if ( !m_bQueryInProgress
)
196 m_eState
= SFX_ITEM_DISABLED
;
198 if ( m_xDispatch
.is() )
202 m_aCondition
.reset();
203 m_bQueryInProgress
= sal_True
;
204 m_xDispatch
->addStatusListener( Reference
< XStatusListener
>( static_cast< cppu::OWeakObject
* >( this ), UNO_QUERY
),
218 m_bQueryInProgress
= sal_False
;
219 rpPoolItem
= m_pItem
;
223 //*************************************************************************
225 SfxQueryStatus::SfxQueryStatus( const Reference
< XDispatchProvider
>& rDispatchProvider
, sal_uInt16 nSlotId
, const OUString
& rCommand
)
227 m_pSfxQueryStatusImpl
= new SfxQueryStatus_Impl( rDispatchProvider
, nSlotId
, rCommand
);
228 m_xStatusListener
= Reference
< XStatusListener
>(
229 static_cast< cppu::OWeakObject
* >( m_pSfxQueryStatusImpl
),
233 SfxQueryStatus::~SfxQueryStatus()
237 SfxItemState
SfxQueryStatus::QueryState( SfxPoolItem
*& rpPoolItem
)
239 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
240 return m_pSfxQueryStatusImpl
->QueryState( rpPoolItem
);