LibreOffice Module sfx2 (master) 1
appdispatchprovider.cxx
Go to the documentation of this file.
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#include <sal/config.h>
21
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>
30
34#include <sfx2/app.hxx>
35#include <sfx2/dispatch.hxx>
36#include <sfx2/msg.hxx>
37#include <sfx2/msgpool.hxx>
39#include <unoctitm.hxx>
40#include <vcl/svapp.hxx>
41
42using namespace ::com::sun::star;
43using namespace ::com::sun::star::frame;
44using namespace ::com::sun::star::uno;
45
46namespace {
47
48class SfxAppDispatchProvider : public ::cppu::WeakImplHelper< css::frame::XAppDispatchProvider,
49 css::lang::XServiceInfo,
50 css::lang::XInitialization >
51{
52 css::uno::WeakReference < css::frame::XFrame > m_xFrame;
53public:
54 SfxAppDispatchProvider() {}
55
56 virtual void SAL_CALL initialize(
57 css::uno::Sequence<css::uno::Any> const & aArguments) override;
58
59 virtual OUString SAL_CALL getImplementationName() override;
60
61 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override;
62
63 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
64
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;
68
69 virtual css::uno::Sequence< css::uno::Reference < css::frame::XDispatch > > SAL_CALL queryDispatches(
70 const css::uno::Sequence < css::frame::DispatchDescriptor >& seqDescriptor ) override;
71
72 virtual css::uno::Sequence< sal_Int16 > SAL_CALL getSupportedCommandGroups() override;
73
74 virtual css::uno::Sequence< css::frame::DispatchInformation > SAL_CALL getConfigurableDispatchInformation( sal_Int16 ) override;
75};
76
77void SfxAppDispatchProvider::initialize(
78 css::uno::Sequence<css::uno::Any> const & aArguments)
79{
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 getXWeak(), 0);
85 }
86 m_xFrame = f;
87}
88
89OUString SAL_CALL SfxAppDispatchProvider::getImplementationName()
90{
91 return "com.sun.star.comp.sfx2.AppDispatchProvider";
92}
93
94sal_Bool SAL_CALL SfxAppDispatchProvider::supportsService( const OUString& sServiceName )
95{
96 return cppu::supportsService(this, sServiceName);
97}
98
99css::uno::Sequence< OUString > SAL_CALL SfxAppDispatchProvider::getSupportedServiceNames()
100{
101 return { "com.sun.star.frame.ProtocolHandler", "com.sun.star.frame.AppDispatchProvider" };
102}
103
104Reference < XDispatch > SAL_CALL SfxAppDispatchProvider::queryDispatch(
105 const util::URL& aURL,
106 const OUString& /*sTargetFrameName*/,
107 sal_Int32 /*eSearchFlags*/ )
108{
109 SolarMutexGuard guard;
110
111 bool bMasterCommand( false );
112 Reference < XDispatch > xDisp;
113 const SfxSlot* pSlot = nullptr;
114 SfxApplication* pApp = SfxGetpApp();
115 if ( !pApp )
116 return xDisp;
117 SfxDispatcher* pAppDisp = pApp->GetAppDispatcher_Impl();
118 if ( aURL.Protocol == "slot:" || aURL.Protocol == "commandId:" )
119 {
120 sal_uInt16 nId = static_cast<sal_uInt16>(aURL.Path.toInt32());
121 SfxShell* pShell;
122 pAppDisp->GetShellAndSlot_Impl( nId, &pShell, &pSlot, true, true );
123 }
124 else if ( aURL.Protocol == ".uno:" )
125 {
126 // Support ".uno" commands. Map commands to slotid
127 bMasterCommand = SfxOfficeDispatch::IsMasterUnoCommand( aURL );
128 if ( bMasterCommand )
129 pSlot = pAppDisp->GetSlot( SfxOfficeDispatch::GetMasterUnoCommand( aURL ) );
130 else
131 pSlot = pAppDisp->GetSlot( aURL.Main );
132 }
133
134 if ( pSlot )
135 {
136 rtl::Reference<SfxOfficeDispatch> pDispatch = new SfxOfficeDispatch( pAppDisp, pSlot, aURL ) ;
137 pDispatch->SetFrame(m_xFrame);
138 pDispatch->SetMasterUnoCommand( bMasterCommand );
139 xDisp = pDispatch;
140 }
141
142 return xDisp;
143}
144
145Sequence< Reference < XDispatch > > SAL_CALL SfxAppDispatchProvider::queryDispatches( const Sequence < DispatchDescriptor >& seqDescriptor )
146{
147 sal_Int32 nCount = seqDescriptor.getLength();
148 uno::Sequence< uno::Reference < frame::XDispatch > > lDispatcher(nCount);
149 std::transform(seqDescriptor.begin(), seqDescriptor.end(), lDispatcher.getArray(),
150 [this](const DispatchDescriptor& rDescr) -> uno::Reference<frame::XDispatch> {
151 return queryDispatch(rDescr.FeatureURL, rDescr.FrameName, rDescr.SearchFlags); });
152 return lDispatcher;
153}
154
155Sequence< sal_Int16 > SAL_CALL SfxAppDispatchProvider::getSupportedCommandGroups()
156{
157 SolarMutexGuard aGuard;
158
159 std::vector< sal_Int16 > aGroupList;
160 SfxSlotPool& rAppSlotPool = SfxGetpApp()->GetAppSlotPool_Impl();
161
163
164 // Select group ( group 0 is internal )
165 for (sal_uInt16 i=0; i< rAppSlotPool.GetGroupCount(); ++i)
166 {
167 rAppSlotPool.SeekGroup(i);
168 const SfxSlot* pSfxSlot = rAppSlotPool.FirstSlot();
169 while ( pSfxSlot )
170 {
171 if ( pSfxSlot->GetMode() & nMode )
172 {
173 sal_Int16 nCommandGroup = MapGroupIDToCommandGroup( pSfxSlot->GetGroupId() );
174 aGroupList.push_back( nCommandGroup );
175 break;
176 }
177 pSfxSlot = rAppSlotPool.NextSlot();
178 }
179 }
180
181 return comphelper::containerToSequence( aGroupList );
182}
183
184Sequence< frame::DispatchInformation > SAL_CALL SfxAppDispatchProvider::getConfigurableDispatchInformation( sal_Int16 nCmdGroup )
185{
186 std::vector< frame::DispatchInformation > aCmdVector;
187
188 SolarMutexGuard aGuard;
189 SfxSlotPool& rAppSlotPool = SfxGetpApp()->GetAppSlotPool_Impl();
190
192
193 // Select group ( group 0 is internal )
194 for (sal_uInt16 i=0; i< rAppSlotPool.GetGroupCount(); ++i)
195 {
196 rAppSlotPool.SeekGroup(i);
197 const SfxSlot* pSfxSlot = rAppSlotPool.FirstSlot();
198 if ( pSfxSlot )
199 {
200 sal_Int16 nCommandGroup = MapGroupIDToCommandGroup( pSfxSlot->GetGroupId() );
201 if ( nCommandGroup == nCmdGroup )
202 {
203 while ( pSfxSlot )
204 {
205 if ( pSfxSlot->GetMode() & nMode )
206 {
207 frame::DispatchInformation aCmdInfo;
208 aCmdInfo.Command = pSfxSlot->GetCommand();
209 aCmdInfo.GroupId = nCommandGroup;
210 aCmdVector.push_back( aCmdInfo );
211 }
212 pSfxSlot = rAppSlotPool.NextSlot();
213 }
214 }
215 }
216 }
217
218 return comphelper::containerToSequence( aCmdVector );
219}
220
221}
222
223extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
225 css::uno::XComponentContext *,
226 css::uno::Sequence<css::uno::Any> const &)
227{
228 return cppu::acquire(new SfxAppDispatchProvider);
229}
230
231/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::lang::XComponent > m_xFrame
SfxApplication * SfxGetpApp()
Definition: app.hxx:231
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_sfx2_AppDispatchProvider_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
SAL_DLLPRIVATE SfxSlotPool & GetAppSlotPool_Impl() const
Definition: appmisc.cxx:104
SAL_DLLPRIVATE SfxDispatcher * GetAppDispatcher_Impl()
Definition: appmisc.cxx:103
const SfxSlot * GetSlot(const OUString &rCommand)
Definition: dispatch.cxx:769
SAL_DLLPRIVATE bool GetShellAndSlot_Impl(sal_uInt16 nSlot, SfxShell **ppShell, const SfxSlot **ppSlot, bool bOwnShellsOnly, bool bRealSlot)
This method searches in SfxDispatcher after <SfxShell> , from the Slot Id nSlot currently being handl...
Definition: dispatch.cxx:696
static OUString GetMasterUnoCommand(const css::util::URL &aURL)
Definition: unoctitm.cxx:311
static bool IsMasterUnoCommand(const css::util::URL &aURL)
Definition: unoctitm.cxx:306
The class SfxShell is the base class for all classes, which provide the functionality of the form <Sl...
Definition: shell.hxx:128
OUString SeekGroup(sal_uInt16 nNo)
Definition: msgpool.cxx:172
const SfxSlot * FirstSlot()
Definition: msgpool.hxx:65
const SfxSlot * NextSlot()
Definition: msgpool.cxx:259
sal_uInt16 GetGroupCount() const
Definition: msgpool.cxx:210
Definition: msg.hxx:184
SfxGroupId GetGroupId() const
Definition: msg.hxx:276
SFX2_DLLPUBLIC OUString GetCommand() const
Definition: msg.cxx:46
SfxSlotMode GetMode() const
Definition: msg.hxx:260
int nCount
URL aURL
Sequence< PropertyValue > aArguments
SfxSlotMode
Definition: msg.hxx:35
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
sal_Int16 nId
sal_Int16 MapGroupIDToCommandGroup(SfxGroupId nGroupID)
unsigned char sal_Bool