1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: querystatus.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
33 #include <sfx2/querystatus.hxx>
34 #include <svtools/poolitem.hxx>
35 #include <svtools/eitem.hxx>
36 #include <svtools/stritem.hxx>
37 #include <svtools/intitem.hxx>
38 #include <svtools/itemset.hxx>
39 #include <svtools/itemdel.hxx>
40 #include <svtools/visitem.hxx>
41 #include <cppuhelper/weak.hxx>
42 #include <comphelper/processfactory.hxx>
43 #include <vos/mutex.hxx>
44 #include <vcl/svapp.hxx>
45 #include <com/sun/star/util/XURLTransformer.hpp>
46 #include <com/sun/star/frame/status/ItemStatus.hpp>
47 #include <com/sun/star/frame/status/ItemState.hpp>
48 #include <com/sun/star/frame/status/Visibility.hpp>
50 using namespace ::rtl
;
51 using namespace ::cppu
;
52 using namespace ::com::sun::star::uno
;
53 using namespace ::com::sun::star::frame
;
54 using namespace ::com::sun::star::frame::status
;
55 using namespace ::com::sun::star::lang
;
56 using namespace ::com::sun::star::util
;
58 class SfxQueryStatus_Impl
: public ::com::sun::star::frame::XStatusListener
,
59 public ::com::sun::star::lang::XTypeProvider
,
60 public ::cppu::OWeakObject
63 SFX_DECL_XINTERFACE_XTYPEPROVIDER
65 SfxQueryStatus_Impl( const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XDispatchProvider
>& rDispatchProvider
, USHORT nSlotId
, const rtl::OUString
& aCommand
);
66 virtual ~SfxQueryStatus_Impl();
69 SfxItemState
QueryState( SfxPoolItem
*& pPoolItem
);
72 virtual void SAL_CALL
disposing(const ::com::sun::star::lang::EventObject
& Source
) throw( ::com::sun::star::uno::RuntimeException
);
75 virtual void SAL_CALL
statusChanged(const ::com::sun::star::frame::FeatureStateEvent
& Event
) throw( ::com::sun::star::uno::RuntimeException
);
78 SfxQueryStatus_Impl( const SfxQueryStatus
& );
79 SfxQueryStatus_Impl();
80 SfxQueryStatus_Impl
& operator=( const SfxQueryStatus
& );
82 sal_Bool m_bQueryInProgress
;
83 SfxItemState m_eState
;
86 osl::Condition m_aCondition
;
87 ::com::sun::star::util::URL m_aCommand
;
88 com::sun::star::uno::Reference
< com::sun::star::frame::XDispatch
> m_xDispatch
;
91 SFX_IMPL_XINTERFACE_2( SfxQueryStatus_Impl
, OWeakObject
, ::com::sun::star::frame::XStatusListener
, ::com::sun::star::lang::XEventListener
)
92 SFX_IMPL_XTYPEPROVIDER_2( SfxQueryStatus_Impl
, ::com::sun::star::frame::XStatusListener
, ::com::sun::star::lang::XEventListener
)
94 SfxQueryStatus_Impl::SfxQueryStatus_Impl( const Reference
< XDispatchProvider
>& rDispatchProvider
, USHORT nSlotId
, const OUString
& rCommand
) :
96 m_bQueryInProgress( sal_False
),
97 m_eState( SFX_ITEM_DISABLED
),
101 m_aCommand
.Complete
= rCommand
;
102 Reference
< XURLTransformer
> xTrans( ::comphelper::getProcessServiceFactory()->createInstance(
103 rtl::OUString::createFromAscii("com.sun.star.util.URLTransformer" )), UNO_QUERY
);
104 xTrans
->parseStrict( m_aCommand
);
105 if ( rDispatchProvider
.is() )
106 m_xDispatch
= rDispatchProvider
->queryDispatch( m_aCommand
, rtl::OUString(), 0 );
107 m_aCondition
.reset();
110 SfxQueryStatus_Impl::~SfxQueryStatus_Impl()
114 void SAL_CALL
SfxQueryStatus_Impl::disposing( const EventObject
& )
115 throw( RuntimeException
)
117 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
121 void SAL_CALL
SfxQueryStatus_Impl::statusChanged( const FeatureStateEvent
& rEvent
)
122 throw( RuntimeException
)
124 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
127 m_eState
= SFX_ITEM_DISABLED
;
129 if ( rEvent
.IsEnabled
)
131 m_eState
= SFX_ITEM_AVAILABLE
;
132 ::com::sun::star::uno::Type pType
= rEvent
.State
.getValueType();
134 if ( pType
== ::getBooleanCppuType() )
136 sal_Bool bTemp
= false;
137 rEvent
.State
>>= bTemp
;
138 m_pItem
= new SfxBoolItem( m_nSlotID
, bTemp
);
140 else if ( pType
== ::getCppuType((const sal_uInt16
*)0) )
142 sal_uInt16 nTemp
= 0;
143 rEvent
.State
>>= nTemp
;
144 m_pItem
= new SfxUInt16Item( m_nSlotID
, nTemp
);
146 else if ( pType
== ::getCppuType((const sal_uInt32
*)0) )
148 sal_uInt32 nTemp
= 0;
149 rEvent
.State
>>= nTemp
;
150 m_pItem
= new SfxUInt32Item( m_nSlotID
, nTemp
);
152 else if ( pType
== ::getCppuType((const ::rtl::OUString
*)0) )
154 ::rtl::OUString sTemp
;
155 rEvent
.State
>>= sTemp
;
156 m_pItem
= new SfxStringItem( m_nSlotID
, sTemp
);
158 else if ( pType
== ::getCppuType((const ::com::sun::star::frame::status::ItemStatus
*)0) )
160 ItemStatus aItemStatus
;
161 rEvent
.State
>>= aItemStatus
;
162 m_eState
= aItemStatus
.State
;
163 m_pItem
= new SfxVoidItem( m_nSlotID
);
165 else if ( pType
== ::getCppuType((const ::com::sun::star::frame::status::Visibility
*)0) )
167 Visibility aVisibilityStatus
;
168 rEvent
.State
>>= aVisibilityStatus
;
169 m_pItem
= new SfxVisibilityItem( m_nSlotID
, aVisibilityStatus
.bVisible
);
173 m_eState
= SFX_ITEM_UNKNOWN
;
174 m_pItem
= new SfxVoidItem( m_nSlotID
);
179 DeleteItemOnIdle( m_pItem
);
184 m_xDispatch
->removeStatusListener( Reference
< XStatusListener
>( static_cast< cppu::OWeakObject
* >( this ), UNO_QUERY
),
193 SfxItemState
SfxQueryStatus_Impl::QueryState( SfxPoolItem
*& rpPoolItem
)
195 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
196 if ( !m_bQueryInProgress
)
199 m_eState
= SFX_ITEM_DISABLED
;
201 if ( m_xDispatch
.is() )
205 m_aCondition
.reset();
206 m_bQueryInProgress
= sal_True
;
207 m_xDispatch
->addStatusListener( Reference
< XStatusListener
>( static_cast< cppu::OWeakObject
* >( this ), UNO_QUERY
),
221 m_bQueryInProgress
= sal_False
;
222 rpPoolItem
= m_pItem
;
226 //*************************************************************************
228 SfxQueryStatus::SfxQueryStatus( const Reference
< XDispatchProvider
>& rDispatchProvider
, USHORT nSlotId
, const OUString
& rCommand
)
230 m_pSfxQueryStatusImpl
= new SfxQueryStatus_Impl( rDispatchProvider
, nSlotId
, rCommand
);
231 m_xStatusListener
= Reference
< XStatusListener
>(
232 static_cast< cppu::OWeakObject
* >( m_pSfxQueryStatusImpl
),
236 SfxQueryStatus::~SfxQueryStatus()
240 SfxItemState
SfxQueryStatus::QueryState( SfxPoolItem
*& rpPoolItem
)
242 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
243 return m_pSfxQueryStatusImpl
->QueryState( rpPoolItem
);