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 = SFX_ITEM_DISABLED;
89 0 : if ( !aStatus.IsEnabled )
90 : {
91 : // default
92 : }
93 0 : else if (aStatus.State.hasValue())
94 : {
95 0 : eState = SFX_ITEM_AVAILABLE;
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 == ::getBooleanCppuType() )
100 : {
101 0 : bool bTemp = false;
102 0 : aAny >>= bTemp ;
103 0 : pItem = new SfxBoolItem( nId, bTemp );
104 : }
105 0 : else if ( pType == ::getCppuType((const sal_uInt16*)0) )
106 : {
107 0 : sal_uInt16 nTemp = 0;
108 0 : aAny >>= nTemp ;
109 0 : pItem = new SfxUInt16Item( nId, nTemp );
110 : }
111 0 : else if ( pType == ::getCppuType((const sal_uInt32*)0) )
112 : {
113 0 : sal_uInt32 nTemp = 0;
114 0 : aAny >>= nTemp ;
115 0 : pItem = new SfxUInt32Item( nId, nTemp );
116 : }
117 0 : else if ( pType == ::getCppuType((const OUString*)0) )
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 = SFX_ITEM_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 0 : const ::com::sun::star::frame::FeatureStateEvent& BindDispatch_Impl::GetStatus() const
165 : {
166 0 : return aStatus;
167 : }
168 :
169 0 : void BindDispatch_Impl::Dispatch( uno::Sequence < beans::PropertyValue > aProps, bool bForceSynchron )
170 : {
171 0 : if ( xDisp.is() && aStatus.IsEnabled )
172 : {
173 0 : sal_Int32 nLength = aProps.getLength();
174 0 : aProps.realloc(nLength+1);
175 0 : aProps[nLength].Name = "SynchronMode";
176 0 : aProps[nLength].Value <<= bForceSynchron ;
177 0 : xDisp->dispatch( aURL, aProps );
178 : }
179 0 : }
180 :
181 :
182 : // This constructor for an invalid cache that is updated in the first request.
183 :
184 0 : SfxStateCache::SfxStateCache( sal_uInt16 nFuncId ):
185 : pDispatch( 0 ),
186 : nId(nFuncId),
187 : pInternalController(0),
188 : pController(0),
189 : pLastItem( 0 ),
190 : eLastState( 0 ),
191 0 : bItemVisible( true )
192 : {
193 0 : bCtrlDirty = true;
194 0 : bSlotDirty = true;
195 0 : bItemDirty = true;
196 0 : }
197 :
198 :
199 : // The Destructor checks by assertion, even if controllers are registered.
200 :
201 0 : SfxStateCache::~SfxStateCache()
202 : {
203 : DBG_ASSERT( pController == 0 && pInternalController == 0, "there are still Controllers registered" );
204 0 : if ( !IsInvalidItem(pLastItem) )
205 0 : delete pLastItem;
206 0 : if ( pDispatch )
207 : {
208 0 : pDispatch->Release();
209 0 : pDispatch = NULL;
210 : }
211 0 : }
212 :
213 :
214 : // invalidates the cache (next request will force update)
215 0 : void SfxStateCache::Invalidate( bool bWithMsg )
216 : {
217 0 : bCtrlDirty = true;
218 0 : if ( bWithMsg )
219 : {
220 0 : bSlotDirty = true;
221 0 : aSlotServ.SetSlot( 0 );
222 0 : if ( pDispatch )
223 : {
224 0 : pDispatch->Release();
225 0 : pDispatch = NULL;
226 : }
227 : }
228 0 : }
229 :
230 :
231 : // gets the corresponding function from the dispatcher or the cache
232 :
233 0 : const SfxSlotServer* SfxStateCache::GetSlotServer( SfxDispatcher &rDispat , const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider > & xProv )
234 : {
235 :
236 0 : if ( bSlotDirty )
237 : {
238 : // get the SlotServer; we need it for internal controllers anyway, but also in most cases
239 0 : rDispat._FindServer( nId, aSlotServ, false );
240 :
241 : DBG_ASSERT( !pDispatch, "Old Dispatch not removed!" );
242 :
243 : // we don't need to check the dispatch provider if we only have an internal controller
244 0 : if ( xProv.is() )
245 : {
246 0 : const SfxSlot* pSlot = aSlotServ.GetSlot();
247 0 : if ( !pSlot )
248 : // get the slot - even if it is disabled on the dispatcher
249 0 : pSlot = SfxSlotPool::GetSlotPool( rDispat.GetFrame() ).GetSlot( nId );
250 :
251 0 : if ( !pSlot || !pSlot->pUnoName )
252 : {
253 0 : bSlotDirty = false;
254 0 : bCtrlDirty = true;
255 0 : return aSlotServ.GetSlot()? &aSlotServ: 0;
256 : }
257 :
258 : // create the dispatch URL from the slot data
259 0 : ::com::sun::star::util::URL aURL;
260 0 : OUString aCmd = ".uno:";
261 0 : aURL.Protocol = aCmd;
262 0 : aURL.Path = OUString::createFromAscii( pSlot->GetUnoName() );
263 0 : aCmd += aURL.Path;
264 0 : aURL.Complete = aCmd;
265 0 : aURL.Main = aCmd;
266 :
267 : // try to get a dispatch object for this command
268 0 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > xDisp = xProv->queryDispatch( aURL, OUString(), 0 );
269 0 : if ( xDisp.is() )
270 : {
271 : // test the dispatch object if it is just a wrapper for a SfxDispatcher
272 0 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xTunnel( xDisp, ::com::sun::star::uno::UNO_QUERY );
273 0 : SfxOfficeDispatch* pDisp = NULL;
274 0 : if ( xTunnel.is() )
275 : {
276 0 : sal_Int64 nImplementation = xTunnel->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier());
277 0 : pDisp = reinterpret_cast< SfxOfficeDispatch* >(sal::static_int_cast< sal_IntPtr >( nImplementation ));
278 : }
279 :
280 0 : if ( pDisp )
281 : {
282 : // The intercepting object is an SFX component
283 : // If this dispatch object does not use the wanted dispatcher or the AppDispatcher, it's treated like any other UNO component
284 : // (intercepting by internal dispatches)
285 0 : SfxDispatcher *pDispatcher = pDisp->GetDispatcher_Impl();
286 0 : if ( pDispatcher == &rDispat || pDispatcher == SFX_APP()->GetAppDispatcher_Impl() )
287 : {
288 : // so we can use it directly
289 0 : bSlotDirty = false;
290 0 : bCtrlDirty = true;
291 0 : return aSlotServ.GetSlot()? &aSlotServ: 0;
292 : }
293 : }
294 :
295 : // so the dispatch object isn't a SfxDispatcher wrapper or it is one, but it uses another dispatcher, but not rDispat
296 0 : pDispatch = new BindDispatch_Impl( xDisp, aURL, this, pSlot );
297 0 : pDispatch->acquire();
298 :
299 : // flags must be set before adding StatusListener because the dispatch object will set the state
300 0 : bSlotDirty = false;
301 0 : bCtrlDirty = true;
302 0 : xDisp->addStatusListener( pDispatch, aURL );
303 : }
304 0 : else if ( rDispat.GetFrame() )
305 : {
306 : ::com::sun::star::uno::Reference < ::com::sun::star::frame::XDispatchProvider > xFrameProv(
307 0 : rDispat.GetFrame()->GetFrame().GetFrameInterface(), ::com::sun::star::uno::UNO_QUERY );
308 0 : if ( xFrameProv != xProv )
309 0 : return GetSlotServer( rDispat, xFrameProv );
310 0 : }
311 : }
312 :
313 0 : bSlotDirty = false;
314 0 : bCtrlDirty = true;
315 : }
316 :
317 : // we *always* return a SlotServer (if there is one); but in case of an external dispatch we might not use it
318 : // for the "real" (non internal) controllers
319 0 : return aSlotServ.GetSlot()? &aSlotServ: 0;
320 : }
321 :
322 :
323 :
324 :
325 : // Set Status in all Controllers
326 :
327 0 : void SfxStateCache::SetState
328 : (
329 : SfxItemState eState, // <SfxItemState> from 'pState'
330 : const SfxPoolItem* pState, // Slot Status, 0 or -1
331 : bool bMaybeDirty
332 : )
333 :
334 : /* [Description]
335 :
336 : This method distributes the status of all of this SID bound
337 : <SfxControllerItem>s. If the value is the same as before, and if neither
338 : controller was registered nor invalidated inbetween, then no value is
339 : passed. This way the flickering is for example avoided in ListBoxes.
340 : */
341 : {
342 0 : SetState_Impl( eState, pState, bMaybeDirty );
343 0 : }
344 :
345 :
346 :
347 0 : void SfxStateCache::SetVisibleState( bool bShow )
348 : {
349 0 : SfxItemState eState( SFX_ITEM_AVAILABLE );
350 0 : const SfxPoolItem* pState( NULL );
351 0 : bool bDeleteItem( false );
352 :
353 0 : if ( bShow != bItemVisible )
354 : {
355 0 : bItemVisible = bShow;
356 0 : if ( bShow )
357 : {
358 0 : if ( IsInvalidItem(pLastItem) || ( pLastItem == NULL ))
359 : {
360 0 : pState = new SfxVoidItem( nId );
361 0 : bDeleteItem = true;
362 : }
363 : else
364 0 : pState = pLastItem;
365 :
366 0 : eState = eLastState;
367 : }
368 : else
369 : {
370 0 : pState = new SfxVisibilityItem( nId, false );
371 0 : bDeleteItem = true;
372 : }
373 :
374 : // Update Controller
375 0 : if ( !pDispatch && pController )
376 : {
377 0 : for ( SfxControllerItem *pCtrl = pController;
378 : pCtrl;
379 : pCtrl = pCtrl->GetItemLink() )
380 0 : pCtrl->StateChanged( nId, eState, pState );
381 : }
382 :
383 0 : if ( pInternalController )
384 0 : pInternalController->StateChanged( nId, eState, pState );
385 :
386 0 : if ( bDeleteItem )
387 0 : delete pState;
388 : }
389 0 : }
390 :
391 :
392 :
393 0 : void SfxStateCache::SetState_Impl
394 : (
395 : SfxItemState eState, // <SfxItemState> from 'pState'
396 : const SfxPoolItem* pState, // Slot Status, 0 or -1
397 : bool bMaybeDirty
398 : )
399 : {
400 : (void)bMaybeDirty; //unused
401 :
402 : // If a hard update occurs between enter- and leave-registrations is a
403 : // can also intermediate Cached exist without controller.
404 0 : if ( !pController && !pInternalController )
405 0 : return;
406 :
407 : DBG_ASSERT( bMaybeDirty || !bSlotDirty, "setting state of dirty message" );
408 : DBG_ASSERT( SfxControllerItem::GetItemState(pState) == eState, "invalid SfxItemState" );
409 :
410 : // does the controller have to be notified at all?
411 0 : bool bNotify = bItemDirty;
412 0 : if ( !bItemDirty )
413 : {
414 0 : bool bBothAvailable = pLastItem && pState &&
415 0 : !IsInvalidItem(pState) && !IsInvalidItem(pLastItem);
416 : DBG_ASSERT( !bBothAvailable || pState != pLastItem, "setting state with own item" );
417 0 : if ( bBothAvailable )
418 0 : bNotify = pState->Type() != pLastItem->Type() ||
419 0 : *pState != *pLastItem;
420 : else
421 0 : bNotify = ( pState != pLastItem ) || ( eState != eLastState );
422 : }
423 :
424 0 : if ( bNotify )
425 : {
426 : // Update Controller
427 0 : if ( !pDispatch && pController )
428 : {
429 0 : for ( SfxControllerItem *pCtrl = pController;
430 : pCtrl;
431 : pCtrl = pCtrl->GetItemLink() )
432 0 : pCtrl->StateChanged( nId, eState, pState );
433 : }
434 :
435 0 : if ( pInternalController )
436 0 : ((SfxDispatchController_Impl *)pInternalController)->StateChanged( nId, eState, pState, &aSlotServ );
437 :
438 : // Remember new value
439 0 : if ( !IsInvalidItem(pLastItem) )
440 0 : DELETEZ(pLastItem);
441 0 : if ( pState && !IsInvalidItem(pState) )
442 0 : pLastItem = pState->Clone();
443 : else
444 0 : pLastItem = 0;
445 0 : eLastState = eState;
446 0 : bItemDirty = false;
447 : }
448 :
449 0 : bCtrlDirty = false;
450 : }
451 :
452 :
453 :
454 : // Set old status again in all the controllers
455 :
456 0 : void SfxStateCache::SetCachedState( bool bAlways )
457 : {
458 : DBG_ASSERT(pController==NULL||pController->GetId()==nId, "Cache with wrong ControllerItem" );
459 :
460 : // Only update if cached item exists and also able to process.
461 : // (If the State is sent, it must be ensured that a SlotServer is present,
462 : // see SfxControllerItem:: GetCoreMetric())
463 0 : if ( bAlways || ( !bItemDirty && !bSlotDirty ) )
464 : {
465 : // Update Controller
466 0 : if ( !pDispatch && pController )
467 : {
468 0 : for ( SfxControllerItem *pCtrl = pController;
469 : pCtrl;
470 : pCtrl = pCtrl->GetItemLink() )
471 0 : pCtrl->StateChanged( nId, eLastState, pLastItem );
472 : }
473 :
474 0 : if ( pInternalController )
475 0 : ((SfxDispatchController_Impl *)pInternalController)->StateChanged( nId, eLastState, pLastItem, &aSlotServ );
476 :
477 : // Controller is now ok
478 0 : bCtrlDirty = true;
479 : }
480 0 : }
481 :
482 :
483 :
484 : // Destroy FloatingWindows in all Controls with this Id
485 :
486 0 : void SfxStateCache::DeleteFloatingWindows()
487 : {
488 :
489 0 : SfxControllerItem *pNextCtrl=0;
490 0 : for ( SfxControllerItem *pCtrl=pController; pCtrl; pCtrl=pNextCtrl )
491 : {
492 0 : pNextCtrl = pCtrl->GetItemLink();
493 0 : pCtrl->DeleteFloatingWindow();
494 : }
495 0 : }
496 :
497 0 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > SfxStateCache::GetDispatch() const
498 : {
499 0 : if ( pDispatch )
500 0 : return pDispatch->xDisp;
501 0 : return ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > ();
502 : }
503 :
504 0 : void SfxStateCache::Dispatch( const SfxItemSet* pSet, bool bForceSynchron )
505 : {
506 : // protect pDispatch against destruction in the call
507 0 : ::com::sun::star::uno::Reference < ::com::sun::star::frame::XStatusListener > xKeepAlive( pDispatch );
508 0 : if ( pDispatch )
509 : {
510 0 : uno::Sequence < beans::PropertyValue > aArgs;
511 0 : if (pSet)
512 0 : TransformItems( nId, *pSet, aArgs );
513 0 : pDispatch->Dispatch( aArgs, bForceSynchron );
514 0 : }
515 0 : }
516 :
517 :
518 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|