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 .
21 #include "commanddescriptionprovider.hxx"
23 #include <com/sun/star/container/XNameAccess.hpp>
24 #include <com/sun/star/frame/ModuleManager.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/frame/theUICommandDescription.hpp>
28 #include <comphelper/namedvaluecollection.hxx>
29 #include <tools/diagnose_ex.h>
36 using ::com::sun::star::uno::Reference
;
37 using ::com::sun::star::uno::XInterface
;
38 using ::com::sun::star::uno::UNO_QUERY
;
39 using ::com::sun::star::uno::UNO_QUERY_THROW
;
40 using ::com::sun::star::uno::UNO_SET_THROW
;
41 using ::com::sun::star::uno::Exception
;
42 using ::com::sun::star::uno::RuntimeException
;
43 using ::com::sun::star::uno::Any
;
44 using ::com::sun::star::uno::makeAny
;
45 using ::com::sun::star::uno::Sequence
;
46 using ::com::sun::star::uno::Type
;
47 using ::com::sun::star::uno::XComponentContext
;
48 using ::com::sun::star::frame::XModel
;
49 using ::com::sun::star::container::XNameAccess
;
50 using ::com::sun::star::frame::ModuleManager
;
51 using ::com::sun::star::frame::XModuleManager2
;
52 using ::com::sun::star::beans::PropertyValue
;
53 using ::com::sun::star::frame::theUICommandDescription
;
55 class DefaultCommandDescriptionProvider
: public ICommandDescriptionProvider
58 DefaultCommandDescriptionProvider( const Reference
<XComponentContext
>& _rxContext
, const Reference
< XModel
>& _rxDocument
)
60 impl_init_nothrow( _rxContext
, _rxDocument
);
63 virtual ~DefaultCommandDescriptionProvider()
67 // ICommandDescriptionProvider
68 virtual OUString
getCommandDescription( const OUString
& _rCommandURL
) const SAL_OVERRIDE
;
71 void impl_init_nothrow( const Reference
<XComponentContext
>& _rxContext
, const Reference
< XModel
>& _rxDocument
);
74 Reference
< XNameAccess
> m_xCommandAccess
;
79 void DefaultCommandDescriptionProvider::impl_init_nothrow( const Reference
<XComponentContext
>& _rxContext
, const Reference
< XModel
>& _rxDocument
)
81 OSL_ENSURE( _rxDocument
.is(), "DefaultCommandDescriptionProvider::impl_init_nothrow: no document => no command descriptions!" );
82 if ( !_rxDocument
.is() )
87 Reference
< XModuleManager2
> xModuleManager( ModuleManager::create(_rxContext
) );
88 OUString sModuleID
= xModuleManager
->identify( _rxDocument
);
90 Reference
< XNameAccess
> xUICommandDescriptions( theUICommandDescription::get(_rxContext
) );
91 m_xCommandAccess
.set( xUICommandDescriptions
->getByName( sModuleID
), UNO_QUERY_THROW
);
93 catch( const Exception
& )
95 DBG_UNHANDLED_EXCEPTION();
100 OUString
DefaultCommandDescriptionProvider::getCommandDescription( const OUString
& _rCommandURL
) const
102 if ( !m_xCommandAccess
.is() )
107 ::comphelper::NamedValueCollection
aCommandProperties( m_xCommandAccess
->getByName( _rCommandURL
) );
108 return aCommandProperties
.getOrDefault( "Name", OUString() );
110 catch( const Exception
& )
112 DBG_UNHANDLED_EXCEPTION();
119 PCommandDescriptionProvider
createDocumentCommandDescriptionProvider(
120 const Reference
<XComponentContext
>& _rxContext
, const Reference
< XModel
>& _rxDocument
)
122 PCommandDescriptionProvider
pDescriptionProvider( new DefaultCommandDescriptionProvider( _rxContext
, _rxDocument
) );
123 return pDescriptionProvider
;
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */