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 :
21 : #ifdef SOLARIS
22 : #include <ctime>
23 : #endif
24 :
25 : #include <string>
26 : #include <com/sun/star/util/XURLTransformer.hpp>
27 : #include <com/sun/star/frame/XController.hpp>
28 : #include <com/sun/star/frame/XFrameActionListener.hpp>
29 : #include <com/sun/star/frame/XComponentLoader.hpp>
30 : #include <com/sun/star/frame/XFrame.hpp>
31 : #include <com/sun/star/frame/FrameActionEvent.hpp>
32 : #include <com/sun/star/frame/FrameAction.hpp>
33 : #include <com/sun/star/beans/PropertyValue.hpp>
34 : #include <cppuhelper/weak.hxx>
35 : #include <svl/eitem.hxx>
36 : #include <svl/intitem.hxx>
37 : #include <svl/stritem.hxx>
38 : #include <svl/visitem.hxx>
39 : #include <comphelper/processfactory.hxx>
40 :
41 : #include <sfx2/app.hxx>
42 : #include "statcach.hxx"
43 : #include <sfx2/msg.hxx>
44 : #include <sfx2/ctrlitem.hxx>
45 : #include <sfx2/dispatch.hxx>
46 : #include "sfxtypes.hxx"
47 : #include <sfx2/sfxuno.hxx>
48 : #include <sfx2/unoctitm.hxx>
49 : #include <sfx2/msgpool.hxx>
50 : #include <sfx2/viewfrm.hxx>
51 :
52 : using namespace ::com::sun::star;
53 : using namespace ::com::sun::star::uno;
54 : using namespace ::com::sun::star::util;
55 :
56 0 : BindDispatch_Impl::BindDispatch_Impl( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > & rDisp, const ::com::sun::star::util::URL& rURL, SfxStateCache *pStateCache, const SfxSlot* pS )
57 : : xDisp( rDisp )
58 : , aURL( rURL )
59 : , pCache( pStateCache )
60 0 : , pSlot( pS )
61 : {
62 : DBG_ASSERT( pCache && pSlot, "Invalid BindDispatch!");
63 0 : aStatus.IsEnabled = sal_True;
64 0 : }
65 :
66 0 : void SAL_CALL BindDispatch_Impl::disposing( const ::com::sun::star::lang::EventObject& ) throw( ::com::sun::star::uno::RuntimeException, std::exception )
67 : {
68 0 : if ( xDisp.is() )
69 : {
70 0 : xDisp->removeStatusListener( (::com::sun::star::frame::XStatusListener*) this, aURL );
71 0 : xDisp = ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > ();
72 : }
73 0 : }
74 :
75 0 : void SAL_CALL BindDispatch_Impl::statusChanged( const ::com::sun::star::frame::FeatureStateEvent& rEvent ) throw( ::com::sun::star::uno::RuntimeException, std::exception )
76 : {
77 0 : aStatus = rEvent;
78 0 : if ( !pCache )
79 0 : return;
80 :
81 0 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener > xRef( (::cppu::OWeakObject*)this, ::com::sun::star::uno::UNO_QUERY );
82 0 : if ( aStatus.Requery )
83 0 : pCache->Invalidate( true );
84 : else
85 : {
86 0 : SfxPoolItem *pItem=NULL;
87 0 : sal_uInt16 nId = pCache->GetId();
88 0 : SfxItemState eState = SfxItemState::DISABLED;
89 0 : if ( !aStatus.IsEnabled )
90 : {
91 : // default
92 : }
93 0 : else if (aStatus.State.hasValue())
94 : {
95 0 : eState = SfxItemState::DEFAULT;
96 0 : ::com::sun::star::uno::Any aAny = aStatus.State;
97 :
98 0 : ::com::sun::star::uno::Type pType = aAny.getValueType();
99 0 : if ( pType == cppu::UnoType< bool >::get() )
100 : {
101 0 : bool bTemp = false;
102 0 : aAny >>= bTemp ;
103 0 : pItem = new SfxBoolItem( nId, bTemp );
104 : }
105 0 : else if ( pType == ::cppu::UnoType< ::cppu::UnoUnsignedShortType >::get() )
106 : {
107 0 : sal_uInt16 nTemp = 0;
108 0 : aAny >>= nTemp ;
109 0 : pItem = new SfxUInt16Item( nId, nTemp );
110 : }
111 0 : else if ( pType == cppu::UnoType<sal_uInt32>::get() )
112 : {
113 0 : sal_uInt32 nTemp = 0;
114 0 : aAny >>= nTemp ;
115 0 : pItem = new SfxUInt32Item( nId, nTemp );
116 : }
117 0 : else if ( pType == cppu::UnoType<OUString>::get() )
118 : {
119 0 : OUString sTemp ;
120 0 : aAny >>= sTemp ;
121 0 : pItem = new SfxStringItem( nId, sTemp );
122 : }
123 : else
124 : {
125 0 : if ( pSlot )
126 0 : pItem = pSlot->GetType()->CreateItem();
127 0 : if ( pItem )
128 : {
129 0 : pItem->SetWhich( nId );
130 0 : pItem->PutValue( aAny );
131 : }
132 : else
133 0 : pItem = new SfxVoidItem( nId );
134 0 : }
135 : }
136 : else
137 : {
138 : // DONTCARE status
139 0 : pItem = new SfxVoidItem(0);
140 0 : eState = SfxItemState::UNKNOWN;
141 : }
142 :
143 0 : for ( SfxControllerItem *pCtrl = pCache->GetItemLink();
144 : pCtrl;
145 : pCtrl = pCtrl->GetItemLink() )
146 0 : pCtrl->StateChanged( nId, eState, pItem );
147 :
148 0 : delete pItem;
149 0 : }
150 : }
151 :
152 0 : void BindDispatch_Impl::Release()
153 : {
154 0 : if ( xDisp.is() )
155 : {
156 0 : xDisp->removeStatusListener( (::com::sun::star::frame::XStatusListener*) this, aURL );
157 0 : xDisp = ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > ();
158 : }
159 :
160 0 : pCache = NULL;
161 0 : release();
162 0 : }
163 :
164 :
165 0 : void BindDispatch_Impl::Dispatch( const uno::Sequence < beans::PropertyValue >& aProps, bool bForceSynchron )
166 : {
167 0 : if ( xDisp.is() && aStatus.IsEnabled )
168 : {
169 0 : sal_Int32 nLength = aProps.getLength();
170 0 : uno::Sequence < beans::PropertyValue > aProps2 = aProps;
171 0 : aProps2.realloc(nLength+1);
172 0 : aProps2[nLength].Name = "SynchronMode";
173 0 : aProps2[nLength].Value <<= bForceSynchron ;
174 0 : xDisp->dispatch( aURL, aProps2 );
175 : }
176 0 : }
177 :
178 :
179 : // This constructor for an invalid cache that is updated in the first request.
180 :
181 260393 : SfxStateCache::SfxStateCache( sal_uInt16 nFuncId ):
182 : pDispatch( 0 ),
183 : nId(nFuncId),
184 : pInternalController(0),
185 : pController(0),
186 : pLastItem( 0 ),
187 : eLastState( SfxItemState::UNKNOWN ),
188 260393 : bItemVisible( true )
189 : {
190 260393 : bCtrlDirty = true;
191 260393 : bSlotDirty = true;
192 260393 : bItemDirty = true;
193 260393 : }
194 :
195 :
196 : // The Destructor checks by assertion, even if controllers are registered.
197 :
198 520786 : SfxStateCache::~SfxStateCache()
199 : {
200 : DBG_ASSERT( pController == 0 && pInternalController == 0, "there are still Controllers registered" );
201 260393 : if ( !IsInvalidItem(pLastItem) )
202 260393 : delete pLastItem;
203 260393 : if ( pDispatch )
204 : {
205 0 : pDispatch->Release();
206 0 : pDispatch = NULL;
207 : }
208 260393 : }
209 :
210 :
211 : // invalidates the cache (next request will force update)
212 280538 : void SfxStateCache::Invalidate( bool bWithMsg )
213 : {
214 280538 : bCtrlDirty = true;
215 280538 : if ( bWithMsg )
216 : {
217 122615 : bSlotDirty = true;
218 122615 : aSlotServ.SetSlot( 0 );
219 122615 : if ( pDispatch )
220 : {
221 0 : pDispatch->Release();
222 0 : pDispatch = NULL;
223 : }
224 : }
225 280538 : }
226 :
227 :
228 : // gets the corresponding function from the dispatcher or the cache
229 :
230 212458 : const SfxSlotServer* SfxStateCache::GetSlotServer( SfxDispatcher &rDispat , const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider > & xProv )
231 : {
232 :
233 212458 : if ( bSlotDirty )
234 : {
235 : // get the SlotServer; we need it for internal controllers anyway, but also in most cases
236 106880 : rDispat._FindServer( nId, aSlotServ, false );
237 :
238 : DBG_ASSERT( !pDispatch, "Old Dispatch not removed!" );
239 :
240 : // we don't need to check the dispatch provider if we only have an internal controller
241 106880 : if ( xProv.is() )
242 : {
243 103926 : const SfxSlot* pSlot = aSlotServ.GetSlot();
244 103926 : if ( !pSlot )
245 : // get the slot - even if it is disabled on the dispatcher
246 4526 : pSlot = SfxSlotPool::GetSlotPool( rDispat.GetFrame() ).GetSlot( nId );
247 :
248 103926 : if ( !pSlot || !pSlot->pUnoName )
249 : {
250 1528 : bSlotDirty = false;
251 1528 : bCtrlDirty = true;
252 105454 : return aSlotServ.GetSlot()? &aSlotServ: 0;
253 : }
254 :
255 : // create the dispatch URL from the slot data
256 102398 : ::com::sun::star::util::URL aURL;
257 102398 : OUString aCmd = ".uno:";
258 102398 : aURL.Protocol = aCmd;
259 102398 : aURL.Path = OUString::createFromAscii( pSlot->GetUnoName() );
260 102398 : aCmd += aURL.Path;
261 102398 : aURL.Complete = aCmd;
262 102398 : aURL.Main = aCmd;
263 :
264 : // try to get a dispatch object for this command
265 102398 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > xDisp = xProv->queryDispatch( aURL, OUString(), 0 );
266 102398 : if ( xDisp.is() )
267 : {
268 : // test the dispatch object if it is just a wrapper for a SfxDispatcher
269 102398 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xTunnel( xDisp, ::com::sun::star::uno::UNO_QUERY );
270 102398 : SfxOfficeDispatch* pDisp = NULL;
271 102398 : if ( xTunnel.is() )
272 : {
273 102398 : sal_Int64 nImplementation = xTunnel->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier());
274 102398 : pDisp = reinterpret_cast< SfxOfficeDispatch* >(sal::static_int_cast< sal_IntPtr >( nImplementation ));
275 : }
276 :
277 102398 : if ( pDisp )
278 : {
279 : // The intercepting object is an SFX component
280 : // If this dispatch object does not use the wanted dispatcher or the AppDispatcher, it's treated like any other UNO component
281 : // (intercepting by internal dispatches)
282 102398 : SfxDispatcher *pDispatcher = pDisp->GetDispatcher_Impl();
283 102398 : if ( pDispatcher == &rDispat || pDispatcher == SfxGetpApp()->GetAppDispatcher_Impl() )
284 : {
285 : // so we can use it directly
286 102398 : bSlotDirty = false;
287 102398 : bCtrlDirty = true;
288 102398 : return aSlotServ.GetSlot()? &aSlotServ: 0;
289 : }
290 : }
291 :
292 : // so the dispatch object isn't a SfxDispatcher wrapper or it is one, but it uses another dispatcher, but not rDispat
293 0 : pDispatch = new BindDispatch_Impl( xDisp, aURL, this, pSlot );
294 0 : pDispatch->acquire();
295 :
296 : // flags must be set before adding StatusListener because the dispatch object will set the state
297 0 : bSlotDirty = false;
298 0 : bCtrlDirty = true;
299 0 : xDisp->addStatusListener( pDispatch, aURL );
300 : }
301 0 : else if ( rDispat.GetFrame() )
302 : {
303 : ::com::sun::star::uno::Reference < ::com::sun::star::frame::XDispatchProvider > xFrameProv(
304 0 : rDispat.GetFrame()->GetFrame().GetFrameInterface(), ::com::sun::star::uno::UNO_QUERY );
305 0 : if ( xFrameProv != xProv )
306 0 : return GetSlotServer( rDispat, xFrameProv );
307 0 : }
308 : }
309 :
310 2954 : bSlotDirty = false;
311 2954 : bCtrlDirty = true;
312 : }
313 :
314 : // we *always* return a SlotServer (if there is one); but in case of an external dispatch we might not use it
315 : // for the "real" (non internal) controllers
316 108532 : return aSlotServ.GetSlot()? &aSlotServ: 0;
317 : }
318 :
319 :
320 :
321 :
322 : // Set Status in all Controllers
323 :
324 38569 : void SfxStateCache::SetState
325 : (
326 : SfxItemState eState, // <SfxItemState> from 'pState'
327 : const SfxPoolItem* pState, // Slot Status, 0 or -1
328 : bool bMaybeDirty
329 : )
330 :
331 : /* [Description]
332 :
333 : This method distributes the status of all of this SID bound
334 : <SfxControllerItem>s. If the value is the same as before, and if neither
335 : controller was registered nor invalidated inbetween, then no value is
336 : passed. This way the flickering is for example avoided in ListBoxes.
337 : */
338 : {
339 38569 : SetState_Impl( eState, pState, bMaybeDirty );
340 38569 : }
341 :
342 :
343 :
344 0 : void SfxStateCache::SetVisibleState( bool bShow )
345 : {
346 0 : SfxItemState eState( SfxItemState::DEFAULT );
347 0 : const SfxPoolItem* pState( NULL );
348 0 : bool bDeleteItem( false );
349 :
350 0 : if ( bShow != bItemVisible )
351 : {
352 0 : bItemVisible = bShow;
353 0 : if ( bShow )
354 : {
355 0 : if ( IsInvalidItem(pLastItem) || ( pLastItem == NULL ))
356 : {
357 0 : pState = new SfxVoidItem( nId );
358 0 : bDeleteItem = true;
359 : }
360 : else
361 0 : pState = pLastItem;
362 :
363 0 : eState = eLastState;
364 : }
365 : else
366 : {
367 0 : pState = new SfxVisibilityItem( nId, false );
368 0 : bDeleteItem = true;
369 : }
370 :
371 : // Update Controller
372 0 : if ( !pDispatch && pController )
373 : {
374 0 : for ( SfxControllerItem *pCtrl = pController;
375 : pCtrl;
376 : pCtrl = pCtrl->GetItemLink() )
377 0 : pCtrl->StateChanged( nId, eState, pState );
378 : }
379 :
380 0 : if ( pInternalController )
381 0 : pInternalController->StateChanged( nId, eState, pState );
382 :
383 0 : if ( bDeleteItem )
384 0 : delete pState;
385 : }
386 0 : }
387 :
388 :
389 :
390 38569 : void SfxStateCache::SetState_Impl
391 : (
392 : SfxItemState eState, // <SfxItemState> from 'pState'
393 : const SfxPoolItem* pState, // Slot Status, 0 or -1
394 : bool bMaybeDirty
395 : )
396 : {
397 : (void)bMaybeDirty; //unused
398 :
399 : // If a hard update occurs between enter- and leave-registrations is a
400 : // can also intermediate Cached exist without controller.
401 38569 : if ( !pController && !pInternalController )
402 38569 : return;
403 :
404 : DBG_ASSERT( bMaybeDirty || !bSlotDirty, "setting state of dirty message" );
405 : DBG_ASSERT( SfxControllerItem::GetItemState(pState) == eState, "invalid SfxItemState" );
406 :
407 : // does the controller have to be notified at all?
408 38569 : bool bNotify = bItemDirty;
409 38569 : if ( !bItemDirty )
410 : {
411 16398 : bool bBothAvailable = pLastItem && pState &&
412 25492 : !IsInvalidItem(pState) && !IsInvalidItem(pLastItem);
413 : DBG_ASSERT( !bBothAvailable || pState != pLastItem, "setting state with own item" );
414 9112 : if ( bBothAvailable )
415 16380 : bNotify = pState->Type() != pLastItem->Type() ||
416 16380 : *pState != *pLastItem;
417 : else
418 922 : bNotify = ( pState != pLastItem ) || ( eState != eLastState );
419 : }
420 :
421 38569 : if ( bNotify )
422 : {
423 : // Update Controller
424 29649 : if ( !pDispatch && pController )
425 : {
426 18407 : for ( SfxControllerItem *pCtrl = pController;
427 : pCtrl;
428 : pCtrl = pCtrl->GetItemLink() )
429 9858 : pCtrl->StateChanged( nId, eState, pState );
430 : }
431 :
432 29649 : if ( pInternalController )
433 29279 : static_cast<SfxDispatchController_Impl *>(pInternalController)->StateChanged( nId, eState, pState, &aSlotServ );
434 :
435 : // Remember new value
436 29649 : if ( !IsInvalidItem(pLastItem) )
437 29649 : DELETEZ(pLastItem);
438 29649 : if ( pState && !IsInvalidItem(pState) )
439 25646 : pLastItem = pState->Clone();
440 : else
441 4003 : pLastItem = 0;
442 29649 : eLastState = eState;
443 29649 : bItemDirty = false;
444 : }
445 :
446 38569 : bCtrlDirty = false;
447 : }
448 :
449 :
450 :
451 : // Set old status again in all the controllers
452 :
453 174 : void SfxStateCache::SetCachedState( bool bAlways )
454 : {
455 : DBG_ASSERT(pController==NULL||pController->GetId()==nId, "Cache with wrong ControllerItem" );
456 :
457 : // Only update if cached item exists and also able to process.
458 : // (If the State is sent, it must be ensured that a SlotServer is present,
459 : // see SfxControllerItem:: GetCoreMetric())
460 174 : if ( bAlways || ( !bItemDirty && !bSlotDirty ) )
461 : {
462 : // Update Controller
463 174 : if ( !pDispatch && pController )
464 : {
465 0 : for ( SfxControllerItem *pCtrl = pController;
466 : pCtrl;
467 : pCtrl = pCtrl->GetItemLink() )
468 0 : pCtrl->StateChanged( nId, eLastState, pLastItem );
469 : }
470 :
471 174 : if ( pInternalController )
472 174 : static_cast<SfxDispatchController_Impl *>(pInternalController)->StateChanged( nId, eLastState, pLastItem, &aSlotServ );
473 :
474 : // Controller is now ok
475 174 : bCtrlDirty = true;
476 : }
477 174 : }
478 :
479 :
480 :
481 : // Destroy FloatingWindows in all Controls with this Id
482 :
483 473662 : void SfxStateCache::DeleteFloatingWindows()
484 : {
485 :
486 473662 : SfxControllerItem *pNextCtrl=0;
487 570234 : for ( SfxControllerItem *pCtrl=pController; pCtrl; pCtrl=pNextCtrl )
488 : {
489 96572 : pNextCtrl = pCtrl->GetItemLink();
490 96572 : pCtrl->DeleteFloatingWindow();
491 : }
492 473662 : }
493 :
494 23638 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > SfxStateCache::GetDispatch() const
495 : {
496 23638 : if ( pDispatch )
497 0 : return pDispatch->xDisp;
498 23638 : return ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > ();
499 : }
500 :
501 0 : void SfxStateCache::Dispatch( const SfxItemSet* pSet, bool bForceSynchron )
502 : {
503 : // protect pDispatch against destruction in the call
504 0 : ::com::sun::star::uno::Reference < ::com::sun::star::frame::XStatusListener > xKeepAlive( pDispatch );
505 0 : if ( pDispatch )
506 : {
507 0 : uno::Sequence < beans::PropertyValue > aArgs;
508 0 : if (pSet)
509 0 : TransformItems( nId, *pSet, aArgs );
510 0 : pDispatch->Dispatch( aArgs, bForceSynchron );
511 0 : }
512 951 : }
513 :
514 :
515 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|