Line data Source code
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 :
21 : #include "commanddescriptionprovider.hxx"
22 :
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>
27 :
28 : #include <comphelper/namedvaluecollection.hxx>
29 : #include <tools/diagnose_ex.h>
30 :
31 :
32 : namespace frm
33 : {
34 :
35 :
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;
54 :
55 : class DefaultCommandDescriptionProvider : public ICommandDescriptionProvider
56 : {
57 : public:
58 18 : DefaultCommandDescriptionProvider( const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument )
59 18 : {
60 18 : impl_init_nothrow( _rxContext, _rxDocument );
61 18 : }
62 :
63 36 : virtual ~DefaultCommandDescriptionProvider()
64 18 : {
65 36 : }
66 :
67 : // ICommandDescriptionProvider
68 : virtual OUString getCommandDescription( const OUString& _rCommandURL ) const SAL_OVERRIDE;
69 :
70 : private:
71 : void impl_init_nothrow( const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument );
72 :
73 : private:
74 : Reference< XNameAccess > m_xCommandAccess;
75 : };
76 :
77 :
78 :
79 18 : void DefaultCommandDescriptionProvider::impl_init_nothrow( const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument )
80 : {
81 : OSL_ENSURE( _rxDocument.is(), "DefaultCommandDescriptionProvider::impl_init_nothrow: no document => no command descriptions!" );
82 18 : if ( !_rxDocument.is() )
83 18 : return;
84 :
85 : try
86 : {
87 18 : Reference< XModuleManager2 > xModuleManager( ModuleManager::create(_rxContext) );
88 36 : OUString sModuleID = xModuleManager->identify( _rxDocument );
89 :
90 36 : Reference< XNameAccess > xUICommandDescriptions( theUICommandDescription::get(_rxContext) );
91 36 : m_xCommandAccess.set( xUICommandDescriptions->getByName( sModuleID ), UNO_QUERY_THROW );
92 : }
93 0 : catch( const Exception& )
94 : {
95 : DBG_UNHANDLED_EXCEPTION();
96 : }
97 : }
98 :
99 :
100 342 : OUString DefaultCommandDescriptionProvider::getCommandDescription( const OUString& _rCommandURL ) const
101 : {
102 342 : if ( !m_xCommandAccess.is() )
103 0 : return OUString();
104 :
105 : try
106 : {
107 342 : ::comphelper::NamedValueCollection aCommandProperties( m_xCommandAccess->getByName( _rCommandURL ) );
108 342 : return aCommandProperties.getOrDefault( "Name", OUString() );
109 : }
110 0 : catch( const Exception& )
111 : {
112 : DBG_UNHANDLED_EXCEPTION();
113 : }
114 :
115 0 : return OUString();
116 : }
117 :
118 :
119 18 : PCommandDescriptionProvider createDocumentCommandDescriptionProvider(
120 : const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument )
121 : {
122 18 : PCommandDescriptionProvider pDescriptionProvider( new DefaultCommandDescriptionProvider( _rxContext, _rxDocument ) );
123 18 : return pDescriptionProvider;
124 : }
125 :
126 :
127 : } // namespace frm
128 :
129 :
130 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|