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 <sal/config.h>
22 #include <com/sun/star/frame/XAppDispatchProvider.hpp>
23 #include <com/sun/star/frame/XDispatch.hpp>
24 #include <com/sun/star/frame/XFrame.hpp>
25 #include <com/sun/star/frame/DispatchDescriptor.hpp>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/lang/XInitialization.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/util/URL.hpp>
31 #include <comphelper/sequence.hxx>
32 #include <cppuhelper/implbase.hxx>
33 #include <cppuhelper/supportsservice.hxx>
34 #include <sfx2/app.hxx>
35 #include <sfx2/dispatch.hxx>
36 #include <sfx2/msg.hxx>
37 #include <sfx2/msgpool.hxx>
38 #include <sfx2/sfxbasecontroller.hxx>
39 #include <unoctitm.hxx>
40 #include <vcl/svapp.hxx>
42 using namespace ::com::sun::star
;
43 using namespace ::com::sun::star::frame
;
44 using namespace ::com::sun::star::uno
;
48 class SfxAppDispatchProvider
: public ::cppu::WeakImplHelper
< css::frame::XAppDispatchProvider
,
49 css::lang::XServiceInfo
,
50 css::lang::XInitialization
>
52 css::uno::WeakReference
< css::frame::XFrame
> m_xFrame
;
54 SfxAppDispatchProvider() {}
56 virtual void SAL_CALL
initialize(
57 css::uno::Sequence
<css::uno::Any
> const & aArguments
) override
;
59 virtual OUString SAL_CALL
getImplementationName() override
;
61 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
;
63 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
;
65 virtual css::uno::Reference
< css::frame::XDispatch
> SAL_CALL
queryDispatch(
66 const css::util::URL
& aURL
, const OUString
& sTargetFrameName
,
67 sal_Int32 eSearchFlags
) override
;
69 virtual css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > SAL_CALL
queryDispatches(
70 const css::uno::Sequence
< css::frame::DispatchDescriptor
>& seqDescriptor
) override
;
72 virtual css::uno::Sequence
< sal_Int16
> SAL_CALL
getSupportedCommandGroups() override
;
74 virtual css::uno::Sequence
< css::frame::DispatchInformation
> SAL_CALL
getConfigurableDispatchInformation( sal_Int16
) override
;
77 void SfxAppDispatchProvider::initialize(
78 css::uno::Sequence
<css::uno::Any
> const & aArguments
)
80 css::uno::Reference
<css::frame::XFrame
> f
;
81 if (aArguments
.getLength() != 1 || !(aArguments
[0] >>= f
)) {
82 throw css::lang::IllegalArgumentException(
83 "SfxAppDispatchProvider::initialize expects one XFrame argument",
84 static_cast<OWeakObject
*>(this), 0);
89 OUString SAL_CALL
SfxAppDispatchProvider::getImplementationName()
91 return "com.sun.star.comp.sfx2.AppDispatchProvider";
94 sal_Bool SAL_CALL
SfxAppDispatchProvider::supportsService( const OUString
& sServiceName
)
96 return cppu::supportsService(this, sServiceName
);
99 css::uno::Sequence
< OUString
> SAL_CALL
SfxAppDispatchProvider::getSupportedServiceNames()
101 return { "com.sun.star.frame.ProtocolHandler", "com.sun.star.frame.AppDispatchProvider" };
104 Reference
< XDispatch
> SAL_CALL
SfxAppDispatchProvider::queryDispatch(
105 const util::URL
& aURL
,
106 const OUString
& /*sTargetFrameName*/,
107 sal_Int32
/*eSearchFlags*/ )
109 SolarMutexGuard guard
;
111 bool bMasterCommand( false );
112 Reference
< XDispatch
> xDisp
;
113 const SfxSlot
* pSlot
= nullptr;
114 SfxApplication
* pApp
= SfxGetpApp();
117 SfxDispatcher
* pAppDisp
= pApp
->GetAppDispatcher_Impl();
118 if ( aURL
.Protocol
== "slot:" || aURL
.Protocol
== "commandId:" )
120 sal_uInt16 nId
= static_cast<sal_uInt16
>(aURL
.Path
.toInt32());
122 pAppDisp
->GetShellAndSlot_Impl( nId
, &pShell
, &pSlot
, true, true );
124 else if ( aURL
.Protocol
== ".uno:" )
126 // Support ".uno" commands. Map commands to slotid
127 bMasterCommand
= SfxOfficeDispatch::IsMasterUnoCommand( aURL
);
128 if ( bMasterCommand
)
129 pSlot
= pAppDisp
->GetSlot( SfxOfficeDispatch::GetMasterUnoCommand( aURL
) );
131 pSlot
= pAppDisp
->GetSlot( aURL
.Main
);
136 SfxOfficeDispatch
* pDispatch
= new SfxOfficeDispatch( pAppDisp
, pSlot
, aURL
) ;
137 pDispatch
->SetFrame(m_xFrame
);
138 pDispatch
->SetMasterUnoCommand( bMasterCommand
);
145 Sequence
< Reference
< XDispatch
> > SAL_CALL
SfxAppDispatchProvider::queryDispatches( const Sequence
< DispatchDescriptor
>& seqDescriptor
)
147 sal_Int32 nCount
= seqDescriptor
.getLength();
148 uno::Sequence
< uno::Reference
< frame::XDispatch
> > lDispatcher(nCount
);
149 std::transform(seqDescriptor
.begin(), seqDescriptor
.end(), lDispatcher
.begin(),
150 [this](const DispatchDescriptor
& rDescr
) -> uno::Reference
<frame::XDispatch
> {
151 return queryDispatch(rDescr
.FeatureURL
, rDescr
.FrameName
, rDescr
.SearchFlags
); });
155 Sequence
< sal_Int16
> SAL_CALL
SfxAppDispatchProvider::getSupportedCommandGroups()
157 SolarMutexGuard aGuard
;
159 std::vector
< sal_Int16
> aGroupList
;
160 SfxSlotPool
& rAppSlotPool
= SfxGetpApp()->GetAppSlotPool_Impl();
162 const SfxSlotMode
nMode( SfxSlotMode::TOOLBOXCONFIG
|SfxSlotMode::ACCELCONFIG
|SfxSlotMode::MENUCONFIG
);
164 // Select group ( group 0 is internal )
165 for (sal_uInt16 i
=0; i
< rAppSlotPool
.GetGroupCount(); ++i
)
167 rAppSlotPool
.SeekGroup(i
);
168 const SfxSlot
* pSfxSlot
= rAppSlotPool
.FirstSlot();
171 if ( pSfxSlot
->GetMode() & nMode
)
173 sal_Int16 nCommandGroup
= MapGroupIDToCommandGroup( pSfxSlot
->GetGroupId() );
174 aGroupList
.push_back( nCommandGroup
);
177 pSfxSlot
= rAppSlotPool
.NextSlot();
181 return comphelper::containerToSequence( aGroupList
);
184 Sequence
< frame::DispatchInformation
> SAL_CALL
SfxAppDispatchProvider::getConfigurableDispatchInformation( sal_Int16 nCmdGroup
)
186 std::vector
< frame::DispatchInformation
> aCmdVector
;
188 SolarMutexGuard aGuard
;
189 SfxSlotPool
& rAppSlotPool
= SfxGetpApp()->GetAppSlotPool_Impl();
191 const SfxSlotMode
nMode( SfxSlotMode::TOOLBOXCONFIG
|SfxSlotMode::ACCELCONFIG
|SfxSlotMode::MENUCONFIG
);
193 // Select group ( group 0 is internal )
194 for (sal_uInt16 i
=0; i
< rAppSlotPool
.GetGroupCount(); ++i
)
196 rAppSlotPool
.SeekGroup(i
);
197 const SfxSlot
* pSfxSlot
= rAppSlotPool
.FirstSlot();
200 sal_Int16 nCommandGroup
= MapGroupIDToCommandGroup( pSfxSlot
->GetGroupId() );
201 if ( nCommandGroup
== nCmdGroup
)
205 if ( pSfxSlot
->GetMode() & nMode
)
207 frame::DispatchInformation aCmdInfo
;
208 aCmdInfo
.Command
= ".uno:" + OUString::createFromAscii(pSfxSlot
->GetUnoName());
209 aCmdInfo
.GroupId
= nCommandGroup
;
210 aCmdVector
.push_back( aCmdInfo
);
212 pSfxSlot
= rAppSlotPool
.NextSlot();
218 return comphelper::containerToSequence( aCmdVector
);
223 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
224 com_sun_star_comp_sfx2_AppDispatchProvider_get_implementation(
225 css::uno::XComponentContext
*,
226 css::uno::Sequence
<css::uno::Any
> const &)
228 return cppu::acquire(new SfxAppDispatchProvider
);
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */