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 <com/sun/star/frame/DispatchStatement.hpp>
21 #include <com/sun/star/container/XIndexReplace.hpp>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/util/URLTransformer.hpp>
26 #include <com/sun/star/util/XURLTransformer.hpp>
27 #include <com/sun/star/frame/XDispatchRecorderSupplier.hpp>
28 #include <svl/itemiter.hxx>
30 #include <svl/itempool.hxx>
31 #include "itemdel.hxx"
33 #include <comphelper/processfactory.hxx>
35 #include <svl/smplhint.hxx>
37 #include <sfx2/request.hxx>
38 #include <sfx2/dispatch.hxx>
39 #include <sfx2/msg.hxx>
40 #include <sfx2/viewfrm.hxx>
41 #include <sfx2/objface.hxx>
42 #include <rtl/strbuf.hxx>
46 using namespace ::com::sun::star
;
48 struct SfxRequest_Impl
: public SfxListener
52 Implementation structure of the <SfxRequest> class.
56 SfxRequest
* pAnti
; // Owner because of dying pool
57 OUString aTarget
; // if possible from target object set by App
58 SfxItemPool
* pPool
; // ItemSet build with this pool
59 SfxPoolItem
* pRetVal
; // Return value belongs to itself
60 SfxShell
* pShell
; // run from this shell
61 const SfxSlot
* pSlot
; // executed Slot
62 sal_uInt16 nModifier
; // which Modifier was pressed?
63 bool bDone
; // at all executed
64 bool bIgnored
; // Cancelled by the User
65 bool bCancelled
; // no longer notify
66 SfxCallMode nCallMode
; // Synch/Asynch/API/Record
68 SfxAllItemSet
* pInternalArgs
;
69 SfxViewFrame
* pViewFrame
;
71 com::sun::star::uno::Reference
< com::sun::star::frame::XDispatchRecorder
> xRecorder
;
73 SfxRequest_Impl( SfxRequest
*pOwner
)
83 , nCallMode( SfxCallMode::SYNCHRON
)
84 , bAllowRecording( false )
88 virtual ~SfxRequest_Impl() { delete pInternalArgs
; }
91 void SetPool( SfxItemPool
*pNewPool
);
92 virtual void Notify( SfxBroadcaster
&rBC
, const SfxHint
&rHint
) SAL_OVERRIDE
;
93 void Record( const uno::Sequence
< beans::PropertyValue
>& rArgs
);
99 void SfxRequest_Impl::Notify( SfxBroadcaster
&, const SfxHint
&rHint
)
101 const SfxSimpleHint
* pSimpleHint
= dynamic_cast<const SfxSimpleHint
*>(&rHint
);
102 if ( pSimpleHint
&& pSimpleHint
->GetId() == SFX_HINT_DYING
)
108 void SfxRequest_Impl::SetPool( SfxItemPool
*pNewPool
)
110 if ( pNewPool
!= pPool
)
113 EndListening( pPool
->BC() );
116 StartListening( pNewPool
->BC() );
123 SfxRequest::~SfxRequest()
125 // Leave out Done() marked requests with 'rem'
126 if ( pImp
->xRecorder
.is() && !pImp
->bDone
&& !pImp
->bIgnored
)
127 pImp
->Record( uno::Sequence
< beans::PropertyValue
>() );
132 DeleteItemOnIdle(pImp
->pRetVal
);
138 SfxRequest::SfxRequest
140 const SfxRequest
& rOrig
144 pArgs(rOrig
.pArgs
? new SfxAllItemSet(*rOrig
.pArgs
): 0),
145 pImp( new SfxRequest_Impl(this) )
147 pImp
->bAllowRecording
= rOrig
.pImp
->bAllowRecording
;
149 pImp
->bIgnored
= false;
153 pImp
->nCallMode
= rOrig
.pImp
->nCallMode
;
154 pImp
->aTarget
= rOrig
.pImp
->aTarget
;
155 pImp
->nModifier
= rOrig
.pImp
->nModifier
;
157 // deep copy needed !
158 pImp
->pInternalArgs
= (rOrig
.pImp
->pInternalArgs
? new SfxAllItemSet(*rOrig
.pImp
->pInternalArgs
) : 0);
161 pImp
->SetPool( pArgs
->GetPool() );
163 pImp
->SetPool( rOrig
.pImp
->pPool
);
168 SfxRequest::SfxRequest
170 SfxViewFrame
* pViewFrame
,
177 With this constructor events can subsequently be recorded that are not run
178 across SfxDispatcher (eg from KeyInput() or mouse events). For this, a
179 SfxRequest instance is created by this constructor and then proceed
180 exactly as with a SfxRequest that in a <Slot-Execute-Method> is given as a
186 pImp( new SfxRequest_Impl(this) )
189 pImp
->bIgnored
= false;
190 pImp
->SetPool( &pViewFrame
->GetPool() );
194 pImp
->nCallMode
= SfxCallMode::SYNCHRON
;
195 pImp
->pViewFrame
= pViewFrame
;
196 if( pImp
->pViewFrame
->GetDispatcher()->GetShellAndSlot_Impl( nSlotId
, &pImp
->pShell
, &pImp
->pSlot
, true, true ) )
198 pImp
->SetPool( &pImp
->pShell
->GetPool() );
199 pImp
->xRecorder
= SfxRequest::GetMacroRecorder( pViewFrame
);
200 pImp
->aTarget
= pImp
->pShell
->GetName();
205 OStringBuffer
aStr("Recording unsupported slot: ");
206 aStr
.append(static_cast<sal_Int32
>(pImp
->pPool
->GetSlotId(nSlotId
)));
207 OSL_FAIL(aStr
.getStr());
215 SfxRequest::SfxRequest
217 sal_uInt16 nSlotId
, // executed <Slot-Id>
218 SfxCallMode nMode
, // Synch/API/...
219 SfxItemPool
& rPool
// necessary for the SfxItemSet for parameters
222 // creates a SfxRequest without arguments
226 pImp( new SfxRequest_Impl(this) )
229 pImp
->bIgnored
= false;
230 pImp
->SetPool( &rPool
);
234 pImp
->nCallMode
= nMode
;
237 SfxRequest::SfxRequest
239 const SfxSlot
* pSlot
, // executed <Slot-Id>
240 const com::sun::star::uno::Sequence
< com::sun::star::beans::PropertyValue
>& rArgs
,
241 SfxCallMode nMode
, // Synch/API/...
242 SfxItemPool
& rPool
// necessary for the SfxItemSet for parameters
244 : nSlot(pSlot
->GetSlotId()),
245 pArgs(new SfxAllItemSet(rPool
)),
246 pImp( new SfxRequest_Impl(this) )
249 pImp
->bIgnored
= false;
250 pImp
->SetPool( &rPool
);
254 pImp
->nCallMode
= nMode
;
255 TransformParameters( nSlot
, rArgs
, *pArgs
, pSlot
);
260 SfxRequest::SfxRequest
264 const SfxAllItemSet
& rSfxArgs
267 // creates a SfxRequest with arguments
270 pArgs(new SfxAllItemSet(rSfxArgs
)),
271 pImp( new SfxRequest_Impl(this) )
274 pImp
->bIgnored
= false;
275 pImp
->SetPool( rSfxArgs
.GetPool() );
279 pImp
->nCallMode
= nMode
;
283 SfxCallMode
SfxRequest::GetCallMode() const
285 return pImp
->nCallMode
;
290 bool SfxRequest::IsSynchronCall() const
292 return SfxCallMode::SYNCHRON
== ( SfxCallMode::SYNCHRON
& pImp
->nCallMode
);
297 void SfxRequest::SetSynchronCall( bool bSynchron
)
300 pImp
->nCallMode
|= SfxCallMode::SYNCHRON
;
302 pImp
->nCallMode
&= ~SfxCallMode::SYNCHRON
;
305 void SfxRequest::SetInternalArgs_Impl( const SfxAllItemSet
& rArgs
)
307 delete pImp
->pInternalArgs
;
308 pImp
->pInternalArgs
= new SfxAllItemSet( rArgs
);
311 const SfxItemSet
* SfxRequest::GetInternalArgs_Impl() const
313 return pImp
->pInternalArgs
;
319 void SfxRequest_Impl::Record
321 const uno::Sequence
< beans::PropertyValue
>& rArgs
// current Parameter
326 Internal helper method to create a repeatable description of the just
331 OUString
aCommand(".uno:");
332 aCommand
+= OUString( pSlot
->GetUnoName(), strlen( pSlot
->GetUnoName() ), RTL_TEXTENCODING_UTF8
);
333 OUString
aCmd( aCommand
);
336 uno::Reference
< container::XIndexReplace
> xReplace( xRecorder
, uno::UNO_QUERY
);
337 if ( xReplace
.is() && aCmd
== ".uno:InsertText" )
339 sal_Int32 nCount
= xReplace
->getCount();
342 frame::DispatchStatement aStatement
;
343 uno::Any aElement
= xReplace
->getByIndex(nCount
-1);
344 if ( (aElement
>>= aStatement
) && aStatement
.aCommand
== aCmd
)
348 aStatement
.aArgs
[0].Value
>>= aStr
;
349 rArgs
[0].Value
>>= aNew
;
351 aStatement
.aArgs
[0].Value
<<= aStr
;
352 aElement
<<= aStatement
;
353 xReplace
->replaceByIndex( nCount
-1, aElement
);
359 uno::Reference
< uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
361 uno::Reference
< util::XURLTransformer
> xTransform
= util::URLTransformer::create( xContext
);
363 com::sun::star::util::URL aURL
;
364 aURL
.Complete
= aCmd
;
365 xTransform
->parseStrict(aURL
);
368 xRecorder
->recordDispatch(aURL
,rArgs
);
370 xRecorder
->recordDispatchAsComment(aURL
,rArgs
);
376 void SfxRequest::Record_Impl
378 SfxShell
& rSh
, // the <SfxShell>, which has executed the Request
379 const SfxSlot
& rSlot
, // the <SfxSlot>, which has executed the Request
380 com::sun::star::uno::Reference
< com::sun::star::frame::XDispatchRecorder
> xRecorder
,
381 SfxViewFrame
* pViewFrame
386 This internal method marks the specified SfxMakro SfxRequest as recorded in
387 SfxMakro. Pointer to the parameters in Done() is used again, thus has to
393 pImp
->pSlot
= &rSlot
;
394 pImp
->xRecorder
= xRecorder
;
395 pImp
->aTarget
= rSh
.GetName();
396 pImp
->pViewFrame
= pViewFrame
;
401 void SfxRequest::SetArgs( const SfxAllItemSet
& rArgs
)
404 pArgs
= new SfxAllItemSet(rArgs
);
405 pImp
->SetPool( pArgs
->GetPool() );
410 void SfxRequest::AppendItem(const SfxPoolItem
&rItem
)
413 pArgs
= new SfxAllItemSet(*pImp
->pPool
);
414 pArgs
->Put(rItem
, rItem
.Which());
419 void SfxRequest::RemoveItem( sal_uInt16 nID
)
423 pArgs
->ClearItem(nID
);
424 if ( !pArgs
->Count() )
431 const SfxPoolItem
* SfxRequest::GetArg
433 sal_uInt16 nSlotId
, // Slot-Id or Which-Id of the parameters
434 bool bDeep
, // sal_False: do not search in the Parent-ItemSets
435 TypeId aType
// != 0: RTTI check with Assertion
438 return GetItem( pArgs
, nSlotId
, bDeep
, aType
);
443 const SfxPoolItem
* SfxRequest::GetItem
445 const SfxItemSet
* pArgs
,
446 sal_uInt16 nSlotId
, // Slot-Id or Which-Id of the parameters
447 bool bDeep
, // sal_False: do not search in the Parent-ItemSets
448 TypeId aType
// != 0: RTTI check with Assertion
453 With this method the access to individual parameters in the SfxRequest is
454 simplified. In particular the type-examination (by Assertion) is performed,
455 whereby the application source code will be much clearer. In the product-
456 version is a 0 returned, if the found item is not of the specified class.
460 void MyShell::Execute( SfxRequest &rReq )
462 switch ( rReq.GetSlot() )
467 // An Example on not using the macros
468 const SfxInt32Item *pPosItem = (const SfxUInt32Item*)
469 rReq.GetArg( SID_POS, sal_False, TYPE(SfxInt32Item) );
470 sal_uInt16 nPos = pPosItem ? pPosItem->GetValue() : 0;
472 // An Example on using the macros
473 SFX_REQUEST_ARG(rReq, pSizeItem, SfxInt32Item, SID_SIZE, sal_False);
474 sal_uInt16 nSize = pSizeItem ? pPosItem->GetValue() : 0;
487 // Which may be converted to ID
488 sal_uInt16 nWhich
= pArgs
->GetPool()->GetWhich(nSlotId
);
490 // Is the item set or available at bDeep == sal_True?
491 const SfxPoolItem
*pItem
= 0;
492 if ( ( bDeep
? SfxItemState::DEFAULT
: SfxItemState::SET
)
493 <= pArgs
->GetItemState( nWhich
, bDeep
, &pItem
) )
496 if ( !pItem
|| pItem
->IsA(aType
) )
499 // Item of wrong type => Programming error
500 OSL_FAIL( "invalid argument type" );
504 // No Parameter, not found or wrong type
510 void SfxRequest::SetReturnValue(const SfxPoolItem
&rItem
)
512 DBG_ASSERT(!pImp
->pRetVal
, "Set Return value multiple times?");
514 delete pImp
->pRetVal
;
515 pImp
->pRetVal
= rItem
.Clone();
520 const SfxPoolItem
* SfxRequest::GetReturnValue() const
522 return pImp
->pRetVal
;
527 void SfxRequest::Done
529 const SfxItemSet
& rSet
, /* parameters passed on by the application,
530 that for example were asked for by the user
531 in a dialogue, 0 if no parameters have been
534 bool bKeep
/* true (default)
535 'rSet' is saved and GetArgs() queriable.
538 'rSet' is not copied (faster) */
543 This method must be called in the <Execute-Method> of the <SfxSlot>s, which
544 has performed the SfxRequest when the execution actually took place. If
545 'Done()' is not called, then the SfxRequest is considered canceled.
547 Any return values are passed only when 'Done()' was called. Similar, when
548 recording a macro only true statements are generated if 'Done()' was
549 called; for SfxRequests that were not identified as such will instead
550 be commented out by inserting ('rem').
554 'Done ()' is not called, for example when a dialoge started by the function
555 was canceled by the user or if the execution could not be performed due to
556 a wrong context (without use of separate <SfxShell>s). 'Done ()' will be
557 launched, when executing the function led to a regular error
558 (for example, file could not be opened).
564 // Keep items if possible, so they can be queried by StarDraw.
569 pArgs
= new SfxAllItemSet( rSet
);
570 pImp
->SetPool( pArgs
->GetPool() );
574 SfxItemIter
aIter(rSet
);
575 const SfxPoolItem
* pItem
= aIter
.FirstItem();
578 if(!IsInvalidItem(pItem
))
579 pArgs
->Put(*pItem
,pItem
->Which());
580 pItem
= aIter
.NextItem();
589 void SfxRequest::Done( bool bRelease
)
590 // [<SfxRequest::Done(SfxItemSet&)>]
599 void SfxRequest::ForgetAllArgs()
602 DELETEZ( pImp
->pInternalArgs
);
607 bool SfxRequest::IsCancelled() const
609 return pImp
->bCancelled
;
614 void SfxRequest::Cancel()
618 Marks this request as no longer executable. For example, if called when
619 the target (more precisely, its pool) dies.
623 pImp
->bCancelled
= true;
631 void SfxRequest::Ignore()
635 If this method is called instead of <SfxRequest::Done()>, then this
636 request is not recorded.
640 The selecting of tools in StarDraw should not be recorded, but the same
641 slots are to be used from the generation of the tools to the generated
642 objects. Thus can NoRecords not be specified, i.e. should not be recorded.
646 // Mark as actually executed
647 pImp
->bIgnored
= true;
652 void SfxRequest::Done_Impl
654 const SfxItemSet
* pSet
/* parameters passed on by the application,
655 that for example were asked for by the user
656 in a dialogue, 0 if no parameters have been
663 Internal method to mark SfxRequest with 'done' and to evaluate the
664 parameters in 'pSet' in case it is recorded.
668 // Mark as actually executed
672 if ( !pImp
->xRecorder
.is() )
675 // was running a different slot than requested (Delegation)
676 if ( nSlot
!= pImp
->pSlot
->GetSlotId() )
679 pImp
->pSlot
= pImp
->pShell
->GetInterface()->GetSlot(nSlot
);
680 DBG_ASSERT( pImp
->pSlot
, "delegated SlotId not found" );
681 if ( !pImp
->pSlot
) // playing it safe
686 // new Recording uses UnoName!
687 if ( !pImp
->pSlot
->pUnoName
)
689 OStringBuffer
aStr("Recording not exported slot: ");
690 aStr
.append(static_cast<sal_Int32
>(pImp
->pSlot
->GetSlotId()));
691 OSL_FAIL(aStr
.getStr());
694 if ( !pImp
->pSlot
->pUnoName
) // playing it safe
697 // often required values
698 SfxItemPool
&rPool
= pImp
->pShell
->GetPool();
701 if ( !pImp
->pSlot
->IsMode(SfxSlotMode::METHOD
) )
703 // get the property as SfxPoolItem
704 const SfxPoolItem
*pItem
;
705 sal_uInt16 nWhich
= rPool
.GetWhich(pImp
->pSlot
->GetSlotId());
706 SfxItemState eState
= pSet
? pSet
->GetItemState( nWhich
, false, &pItem
) : SfxItemState::UNKNOWN
;
708 if ( SfxItemState::SET
!= eState
)
710 OStringBuffer
aStr("Recording property not available: ");
711 aStr
.append(static_cast<sal_Int32
>(pImp
->pSlot
->GetSlotId()));
712 OSL_FAIL(aStr
.getStr());
715 uno::Sequence
< beans::PropertyValue
> aSeq
;
716 if ( eState
== SfxItemState::SET
)
717 TransformItems( pImp
->pSlot
->GetSlotId(), *pSet
, aSeq
, pImp
->pSlot
);
718 pImp
->Record( aSeq
);
721 // record everything in a single statement?
722 else if ( pImp
->pSlot
->IsMode(SfxSlotMode::RECORDPERSET
) )
724 uno::Sequence
< beans::PropertyValue
> aSeq
;
726 TransformItems( pImp
->pSlot
->GetSlotId(), *pSet
, aSeq
, pImp
->pSlot
);
727 pImp
->Record( aSeq
);
730 // record each item as a single statement
731 else if ( pImp
->pSlot
->IsMode(SfxSlotMode::RECORDPERITEM
) )
735 // iterate over Items
736 SfxItemIter
aIter(*pSet
);
737 for ( const SfxPoolItem
* pItem
= aIter
.FirstItem(); pItem
; pItem
= aIter
.NextItem() )
739 // to determine the slot ID for the individual item
740 sal_uInt16 nSlotId
= rPool
.GetSlotId( pItem
->Which() );
741 if ( nSlotId
== nSlot
)
743 // play it safe; repair the wrong flags
744 OSL_FAIL( "recursion RecordPerItem - use RecordPerSet!" );
745 SfxSlot
*pSlot
= const_cast<SfxSlot
*>(pImp
->pSlot
);
746 pSlot
->nFlags
&= ~(SfxSlotMode::RECORDPERITEM
);
747 pSlot
->nFlags
&= SfxSlotMode::RECORDPERSET
;
750 // Record a Sub-Request
751 SfxRequest
aReq( pImp
->pViewFrame
, nSlotId
);
752 if ( aReq
.pImp
->pSlot
)
753 aReq
.AppendItem( *pItem
);
759 //HACK(think about this again)
760 pImp
->Record( uno::Sequence
< beans::PropertyValue
>() );
767 bool SfxRequest::IsDone() const
771 With this method it can be queried whether the SfxRequest was actually
772 executed or not. If a SfxRequest was not executed, then this is for example
773 because it was canceled by the user or the context for this request was
774 wrong, this was not implemented on a separate <SfxShell>.
776 SfxRequest instances that return false will not be recorded.
780 <SfxRequest::Done(const SfxItemSet&)>
790 com::sun::star::uno::Reference
< com::sun::star::frame::XDispatchRecorder
> SfxRequest::GetMacroRecorder( SfxViewFrame
* pView
)
794 This recorder is an attempt for dispatch () to get calls from the Frame.
795 This is then available through a property by a supplier but only when
796 recording was turned on.
798 (See also SfxViewFrame::MiscExec_Impl() and SID_RECORDING)
802 com::sun::star::uno::Reference
< com::sun::star::frame::XDispatchRecorder
> xRecorder
;
804 com::sun::star::uno::Reference
< com::sun::star::beans::XPropertySet
> xSet(
805 (pView
? pView
: SfxViewFrame::Current())->GetFrame().GetFrameInterface(),
806 com::sun::star::uno::UNO_QUERY
);
810 com::sun::star::uno::Any aProp
= xSet
->getPropertyValue("DispatchRecorderSupplier");
811 com::sun::star::uno::Reference
< com::sun::star::frame::XDispatchRecorderSupplier
> xSupplier
;
814 xRecorder
= xSupplier
->getDispatchRecorder();
820 bool SfxRequest::HasMacroRecorder( SfxViewFrame
* pView
)
822 return GetMacroRecorder( pView
).is();
828 bool SfxRequest::IsAPI() const
832 Returns true if this SfxRequest was generated by an API (for example BASIC),
837 return SfxCallMode::API
== ( SfxCallMode::API
& pImp
->nCallMode
);
841 void SfxRequest::SetModifier( sal_uInt16 nModi
)
843 pImp
->nModifier
= nModi
;
847 sal_uInt16
SfxRequest::GetModifier() const
849 return pImp
->nModifier
;
854 void SfxRequest::AllowRecording( bool bSet
)
856 pImp
->bAllowRecording
= bSet
;
859 bool SfxRequest::AllowsRecording() const
861 bool bAllow
= pImp
->bAllowRecording
;
863 bAllow
= ( SfxCallMode::API
!= ( SfxCallMode::API
& pImp
->nCallMode
) ) &&
864 ( SfxCallMode::RECORD
== ( SfxCallMode::RECORD
& pImp
->nCallMode
) );
868 void SfxRequest::ReleaseArgs()
871 DELETEZ( pImp
->pInternalArgs
);
874 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */