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/uno/Exception.hpp>
30 #include <com/sun/star/util/URL.hpp>
32 #include <basic/basmgr.hxx>
33 #include <basic/sbuno.hxx>
34 #include <comphelper/sequence.hxx>
35 #include <cppuhelper/implbase3.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <rtl/ref.hxx>
38 #include <sfx2/app.hxx>
39 #include <sfx2/dispatch.hxx>
40 #include <sfx2/docfile.hxx>
41 #include <sfx2/fcontnr.hxx>
42 #include <sfx2/frame.hxx>
43 #include <sfx2/module.hxx>
44 #include <sfx2/msg.hxx>
45 #include <sfx2/msgpool.hxx>
46 #include <sfx2/objsh.hxx>
47 #include <sfx2/request.hxx>
48 #include <sfx2/sfxbasecontroller.hxx>
49 #include <sfx2/sfxsids.hrc>
50 #include <sfx2/sfxuno.hxx>
51 #include <sfx2/unoctitm.hxx>
52 #include <svl/intitem.hxx>
53 #include <tools/urlobj.hxx>
54 #include <vcl/svapp.hxx>
56 using namespace ::com::sun::star
;
57 using namespace ::com::sun::star::frame
;
58 using namespace ::com::sun::star::uno
;
62 class SfxAppDispatchProvider
: public ::cppu::WeakImplHelper3
< css::frame::XAppDispatchProvider
,
63 css::lang::XServiceInfo
,
64 css::lang::XInitialization
>
66 css::uno::WeakReference
< css::frame::XFrame
> m_xFrame
;
68 SfxAppDispatchProvider() {}
70 virtual void SAL_CALL
initialize(
71 css::uno::Sequence
<css::uno::Any
> const & aArguments
)
72 throw (css::uno::Exception
, css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
74 virtual OUString SAL_CALL
getImplementationName()
75 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
77 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
)
78 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
80 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames()
81 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
83 virtual css::uno::Reference
< css::frame::XDispatch
> SAL_CALL
queryDispatch(
84 const css::util::URL
& aURL
, const OUString
& sTargetFrameName
,
85 FrameSearchFlags eSearchFlags
)
86 throw( css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
88 virtual css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > SAL_CALL
queryDispatches(
89 const css::uno::Sequence
< css::frame::DispatchDescriptor
>& seqDescriptor
)
90 throw( css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
92 virtual css::uno::Sequence
< sal_Int16
> SAL_CALL
getSupportedCommandGroups()
93 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
95 virtual css::uno::Sequence
< css::frame::DispatchInformation
> SAL_CALL
getConfigurableDispatchInformation( sal_Int16
)
96 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
99 void SfxAppDispatchProvider::initialize(
100 css::uno::Sequence
<css::uno::Any
> const & aArguments
)
101 throw (css::uno::Exception
, css::uno::RuntimeException
, std::exception
)
103 css::uno::Reference
<css::frame::XFrame
> f
;
104 if (aArguments
.getLength() != 1 || !(aArguments
[0] >>= f
)) {
105 throw css::lang::IllegalArgumentException(
106 "SfxAppDispatchProvider::initialize expects one XFrame argument",
107 static_cast<OWeakObject
*>(this), 0);
112 OUString SAL_CALL
SfxAppDispatchProvider::getImplementationName() throw( css::uno::RuntimeException
, std::exception
)
114 return OUString( "com.sun.star.comp.sfx2.AppDispatchProvider" );
117 sal_Bool SAL_CALL
SfxAppDispatchProvider::supportsService( const OUString
& sServiceName
) throw( css::uno::RuntimeException
, std::exception
)
119 return cppu::supportsService(this, sServiceName
);
122 css::uno::Sequence
< OUString
> SAL_CALL
SfxAppDispatchProvider::getSupportedServiceNames() throw( css::uno::RuntimeException
, std::exception
)
124 css::uno::Sequence
< OUString
> seqServiceNames( 2 );
125 seqServiceNames
.getArray()[0] = "com.sun.star.frame.ProtocolHandler";
126 seqServiceNames
.getArray()[1] = "com.sun.star.frame.AppDispatchProvider";
127 return seqServiceNames
;
130 Reference
< XDispatch
> SAL_CALL
SfxAppDispatchProvider::queryDispatch(
131 const util::URL
& aURL
,
132 const OUString
& /*sTargetFrameName*/,
133 FrameSearchFlags
/*eSearchFlags*/ ) throw( RuntimeException
, std::exception
)
136 bool bMasterCommand( false );
137 Reference
< XDispatch
> xDisp
;
138 const SfxSlot
* pSlot
= 0;
139 SfxDispatcher
* pAppDisp
= SfxGetpApp()->GetAppDispatcher_Impl();
140 if ( aURL
.Protocol
== "slot:" || aURL
.Protocol
== "commandId:" )
142 nId
= (sal_uInt16
) aURL
.Path
.toInt32();
144 pAppDisp
->GetShellAndSlot_Impl( nId
, &pShell
, &pSlot
, true, true );
146 else if ( aURL
.Protocol
== ".uno:" )
148 // Support ".uno" commands. Map commands to slotid
149 bMasterCommand
= SfxOfficeDispatch::IsMasterUnoCommand( aURL
);
150 if ( bMasterCommand
)
151 pSlot
= pAppDisp
->GetSlot( SfxOfficeDispatch::GetMasterUnoCommand( aURL
) );
153 pSlot
= pAppDisp
->GetSlot( aURL
.Main
);
158 SfxOfficeDispatch
* pDispatch
= new SfxOfficeDispatch( pAppDisp
, pSlot
, aURL
) ;
159 pDispatch
->SetFrame(m_xFrame
);
160 pDispatch
->SetMasterUnoCommand( bMasterCommand
);
167 Sequence
< Reference
< XDispatch
> > SAL_CALL
SfxAppDispatchProvider::queryDispatches( const Sequence
< DispatchDescriptor
>& seqDescriptor
)
168 throw( RuntimeException
, std::exception
)
170 sal_Int32 nCount
= seqDescriptor
.getLength();
171 uno::Sequence
< uno::Reference
< frame::XDispatch
> > lDispatcher(nCount
);
172 for( sal_Int32 i
=0; i
<nCount
; ++i
)
173 lDispatcher
[i
] = this->queryDispatch( seqDescriptor
[i
].FeatureURL
,
174 seqDescriptor
[i
].FrameName
,
175 seqDescriptor
[i
].SearchFlags
);
179 Sequence
< sal_Int16
> SAL_CALL
SfxAppDispatchProvider::getSupportedCommandGroups()
180 throw (uno::RuntimeException
, std::exception
)
182 SolarMutexGuard aGuard
;
184 std::list
< sal_Int16
> aGroupList
;
185 SfxSlotPool
* pAppSlotPool
= &SfxGetpApp()->GetAppSlotPool_Impl();
187 const SfxSlotMode
nMode( SfxSlotMode::TOOLBOXCONFIG
|SfxSlotMode::ACCELCONFIG
|SfxSlotMode::MENUCONFIG
);
189 // Gruppe anw"ahlen ( Gruppe 0 ist intern )
190 for ( sal_uInt16 i
=0; i
<pAppSlotPool
->GetGroupCount(); i
++ )
192 pAppSlotPool
->SeekGroup( i
);
193 const SfxSlot
* pSfxSlot
= pAppSlotPool
->FirstSlot();
196 if ( pSfxSlot
->GetMode() & nMode
)
198 sal_Int16 nCommandGroup
= MapGroupIDToCommandGroup( pSfxSlot
->GetGroupId() );
199 aGroupList
.push_back( nCommandGroup
);
202 pSfxSlot
= pAppSlotPool
->NextSlot();
206 uno::Sequence
< sal_Int16
> aSeq
=
207 comphelper::containerToSequence
< sal_Int16
, std::list
< sal_Int16
> >( aGroupList
);
212 Sequence
< frame::DispatchInformation
> SAL_CALL
SfxAppDispatchProvider::getConfigurableDispatchInformation( sal_Int16 nCmdGroup
)
213 throw (uno::RuntimeException
, std::exception
)
215 std::list
< frame::DispatchInformation
> aCmdList
;
217 SolarMutexGuard aGuard
;
218 SfxSlotPool
* pAppSlotPool
= &SfxGetpApp()->GetAppSlotPool_Impl();
222 const SfxSlotMode
nMode( SfxSlotMode::TOOLBOXCONFIG
|SfxSlotMode::ACCELCONFIG
|SfxSlotMode::MENUCONFIG
);
223 OUString
aCmdPrefix( ".uno:" );
225 // Gruppe anw"ahlen ( Gruppe 0 ist intern )
226 for ( sal_uInt16 i
=0; i
<pAppSlotPool
->GetGroupCount(); i
++ )
228 pAppSlotPool
->SeekGroup( i
);
229 const SfxSlot
* pSfxSlot
= pAppSlotPool
->FirstSlot();
232 sal_Int16 nCommandGroup
= MapGroupIDToCommandGroup( pSfxSlot
->GetGroupId() );
233 if ( nCommandGroup
== nCmdGroup
)
237 if ( pSfxSlot
->GetMode() & nMode
)
239 frame::DispatchInformation aCmdInfo
;
240 OUStringBuffer
aBuf( aCmdPrefix
);
241 aBuf
.appendAscii( pSfxSlot
->GetUnoName() );
242 aCmdInfo
.Command
= aBuf
.makeStringAndClear();
243 aCmdInfo
.GroupId
= nCommandGroup
;
244 aCmdList
.push_back( aCmdInfo
);
246 pSfxSlot
= pAppSlotPool
->NextSlot();
253 uno::Sequence
< frame::DispatchInformation
> aSeq
=
254 comphelper::containerToSequence
< frame::DispatchInformation
, std::list
< frame::DispatchInformation
> >( aCmdList
);
261 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
262 com_sun_star_comp_sfx2_AppDispatchProvider_get_implementation(
263 css::uno::XComponentContext
*,
264 css::uno::Sequence
<css::uno::Any
> const &)
266 return cppu::acquire(new SfxAppDispatchProvider
);
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */