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 <sfx2/sidebar/CommandInfoProvider.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <svtools/acceleratorexecute.hxx>
24 #include <cppuhelper/compbase1.hxx>
25 #include <cppuhelper/basemutex.hxx>
27 #include <com/sun/star/frame/ModuleManager.hpp>
28 #include <com/sun/star/frame/theUICommandDescription.hpp>
29 #include <com/sun/star/ui/GlobalAcceleratorConfiguration.hpp>
30 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
31 #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
34 using namespace css::uno
;
35 using ::rtl::OUString
;
40 typedef ::cppu::WeakComponentImplHelper1
<
41 css::lang::XEventListener
42 > FrameListenerInterfaceBase
;
44 : public ::cppu::BaseMutex
,
45 public FrameListenerInterfaceBase
48 FrameListener (sfx2::sidebar::CommandInfoProvider
& rInfoProvider
, const Reference
<frame::XFrame
>& rxFrame
)
49 : FrameListenerInterfaceBase(m_aMutex
),
50 mrInfoProvider(rInfoProvider
),
54 mxFrame
->addEventListener(this);
56 virtual ~FrameListener()
59 virtual void SAL_CALL
disposing() SAL_OVERRIDE
62 mxFrame
->removeEventListener(this);
64 virtual void SAL_CALL
disposing (const css::lang::EventObject
& rEvent
)
65 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
68 mrInfoProvider
.SetFrame(NULL
);
73 sfx2::sidebar::CommandInfoProvider
& mrInfoProvider
;
74 Reference
<frame::XFrame
> mxFrame
;
78 namespace sfx2
{ namespace sidebar
{
80 CommandInfoProvider
& CommandInfoProvider::Instance()
82 static CommandInfoProvider aProvider
;
86 CommandInfoProvider::CommandInfoProvider()
87 : mxContext(comphelper::getProcessComponentContext()),
89 mxCachedDocumentAcceleratorConfiguration(),
90 mxCachedModuleAcceleratorConfiguration(),
91 mxCachedGlobalAcceleratorConfiguration(),
92 msCachedModuleIdentifier(),
97 CommandInfoProvider::~CommandInfoProvider()
99 if (mxFrameListener
.is())
101 mxFrameListener
->dispose();
102 mxFrameListener
= NULL
;
107 OUString
CommandInfoProvider::GetLabelForCommand (
108 const OUString
& rsCommandName
,
109 const Reference
<frame::XFrame
>& rxFrame
)
113 const OUString
sLabel (GetCommandLabel(rsCommandName
));
114 const OUString
sShortCut (GetCommandShortcut(rsCommandName
));
115 if (sShortCut
.getLength() > 0)
116 return sLabel
+ " (" + sShortCut
+ ")";
121 void CommandInfoProvider::SetFrame (const Reference
<frame::XFrame
>& rxFrame
)
123 if (rxFrame
!= mxCachedDataFrame
)
125 // Detach from the old frame.
126 if (mxFrameListener
.is())
128 mxFrameListener
->dispose();
129 mxFrameListener
= NULL
;
132 // Release objects that are tied to the old frame.
133 mxCachedDocumentAcceleratorConfiguration
= NULL
;
134 mxCachedModuleAcceleratorConfiguration
= NULL
;
135 msCachedModuleIdentifier
.clear();
136 mxCachedDataFrame
= rxFrame
;
138 // Connect to the new frame.
140 mxFrameListener
= new FrameListener(*this, rxFrame
);
144 Reference
<ui::XAcceleratorConfiguration
> CommandInfoProvider::GetDocumentAcceleratorConfiguration()
146 if ( ! mxCachedDocumentAcceleratorConfiguration
.is())
148 // Get the accelerator configuration for the document.
149 if (mxCachedDataFrame
.is())
151 Reference
<frame::XController
> xController
= mxCachedDataFrame
->getController();
152 if (xController
.is())
154 Reference
<frame::XModel
> xModel (xController
->getModel());
157 Reference
<ui::XUIConfigurationManagerSupplier
> xSupplier (xModel
, UNO_QUERY
);
160 Reference
<ui::XUIConfigurationManager
> xConfigurationManager(
161 xSupplier
->getUIConfigurationManager(),
163 if (xConfigurationManager
.is())
165 mxCachedDocumentAcceleratorConfiguration
= xConfigurationManager
->getShortCutManager();
172 return mxCachedDocumentAcceleratorConfiguration
;
175 Reference
<ui::XAcceleratorConfiguration
> CommandInfoProvider::GetModuleAcceleratorConfiguration()
177 if ( ! mxCachedModuleAcceleratorConfiguration
.is())
181 Reference
<ui::XModuleUIConfigurationManagerSupplier
> xSupplier
= ui::theModuleUIConfigurationManagerSupplier::get(mxContext
);
182 Reference
<ui::XUIConfigurationManager
> xManager (
183 xSupplier
->getUIConfigurationManager(GetModuleIdentifier()));
186 mxCachedModuleAcceleratorConfiguration
= xManager
->getShortCutManager();
193 return mxCachedModuleAcceleratorConfiguration
;
196 Reference
<ui::XAcceleratorConfiguration
> CommandInfoProvider::GetGlobalAcceleratorConfiguration()
198 // Get the global accelerator configuration.
199 if ( ! mxCachedGlobalAcceleratorConfiguration
.is())
201 mxCachedGlobalAcceleratorConfiguration
= ui::GlobalAcceleratorConfiguration::create(mxContext
);
204 return mxCachedGlobalAcceleratorConfiguration
;
207 OUString
CommandInfoProvider::GetModuleIdentifier()
209 if (msCachedModuleIdentifier
.getLength() == 0)
211 Reference
<frame::XModuleManager2
> xModuleManager
= frame::ModuleManager::create(mxContext
);
212 msCachedModuleIdentifier
= xModuleManager
->identify(mxCachedDataFrame
);
214 return msCachedModuleIdentifier
;
217 OUString
CommandInfoProvider::GetCommandShortcut (const OUString
& rsCommandName
)
221 sShortcut
= RetrieveShortcutsFromConfiguration(GetDocumentAcceleratorConfiguration(), rsCommandName
);
222 if (sShortcut
.getLength() > 0)
225 sShortcut
= RetrieveShortcutsFromConfiguration(GetModuleAcceleratorConfiguration(), rsCommandName
);
226 if (sShortcut
.getLength() > 0)
229 sShortcut
= RetrieveShortcutsFromConfiguration(GetGlobalAcceleratorConfiguration(), rsCommandName
);
230 if (sShortcut
.getLength() > 0)
236 OUString
CommandInfoProvider::RetrieveShortcutsFromConfiguration(
237 const Reference
<ui::XAcceleratorConfiguration
>& rxConfiguration
,
238 const OUString
& rsCommandName
)
240 if (rxConfiguration
.is())
244 Sequence
<OUString
> aCommands(1);
245 aCommands
[0] = rsCommandName
;
247 Sequence
<Any
> aKeyCodes (rxConfiguration
->getPreferredKeyEventsForCommandList(aCommands
));
248 if (aCommands
.getLength() == 1)
250 css::awt::KeyEvent aKeyEvent
;
251 if (aKeyCodes
[0] >>= aKeyEvent
)
253 return svt::AcceleratorExecute::st_AWTKey2VCLKey(aKeyEvent
).GetName();
257 catch (lang::IllegalArgumentException
&)
264 Sequence
<beans::PropertyValue
> CommandInfoProvider::GetCommandProperties (const OUString
& rsCommandName
)
266 Sequence
<beans::PropertyValue
> aProperties
;
270 const OUString
sModuleIdentifier (GetModuleIdentifier());
271 if (sModuleIdentifier
.getLength() > 0)
273 Reference
<container::XNameAccess
> xNameAccess
= frame::theUICommandDescription::get(mxContext
);
274 Reference
<container::XNameAccess
> xUICommandLabels
;
275 if (xNameAccess
->getByName(sModuleIdentifier
) >>= xUICommandLabels
)
276 xUICommandLabels
->getByName(rsCommandName
) >>= aProperties
;
286 OUString
CommandInfoProvider::GetCommandLabel (const OUString
& rsCommandName
)
288 const Sequence
<beans::PropertyValue
> aProperties (GetCommandProperties(rsCommandName
));
289 for (sal_Int32 nIndex
=0; nIndex
<aProperties
.getLength(); ++nIndex
)
291 if (aProperties
[nIndex
].Name
== "Name")
294 aProperties
[nIndex
].Value
>>= sLabel
;
301 } } // end of namespace sfx2/framework
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */