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