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/UICommandDescription.hpp>
27 :
28 : #include <comphelper/namedvaluecollection.hxx>
29 : #include <tools/diagnose_ex.h>
30 :
31 : //........................................................................
32 : namespace frm
33 : {
34 : //........................................................................
35 :
36 : /** === begin UNO using === **/
37 : using ::com::sun::star::uno::Reference;
38 : using ::com::sun::star::uno::XInterface;
39 : using ::com::sun::star::uno::UNO_QUERY;
40 : using ::com::sun::star::uno::UNO_QUERY_THROW;
41 : using ::com::sun::star::uno::UNO_SET_THROW;
42 : using ::com::sun::star::uno::Exception;
43 : using ::com::sun::star::uno::RuntimeException;
44 : using ::com::sun::star::uno::Any;
45 : using ::com::sun::star::uno::makeAny;
46 : using ::com::sun::star::uno::Sequence;
47 : using ::com::sun::star::uno::Type;
48 : using ::com::sun::star::uno::XComponentContext;
49 : using ::com::sun::star::frame::XModel;
50 : using ::com::sun::star::container::XNameAccess;
51 : using ::com::sun::star::frame::ModuleManager;
52 : using ::com::sun::star::frame::XModuleManager2;
53 : using ::com::sun::star::beans::PropertyValue;
54 : using ::com::sun::star::frame::UICommandDescription;
55 : /** === end UNO using === **/
56 :
57 : //====================================================================
58 : //= DefaultCommandDescriptionProvider
59 : //====================================================================
60 : class DefaultCommandDescriptionProvider : public ICommandDescriptionProvider
61 : {
62 : public:
63 0 : DefaultCommandDescriptionProvider( const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument )
64 0 : {
65 0 : impl_init_nothrow( _rxContext, _rxDocument );
66 0 : }
67 :
68 0 : ~DefaultCommandDescriptionProvider()
69 0 : {
70 0 : }
71 :
72 : // ICommandDescriptionProvider
73 : virtual ::rtl::OUString getCommandDescription( const ::rtl::OUString& _rCommandURL ) const;
74 :
75 : private:
76 : void impl_init_nothrow( const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument );
77 :
78 : private:
79 : Reference< XNameAccess > m_xCommandAccess;
80 : };
81 :
82 :
83 : //--------------------------------------------------------------------
84 0 : void DefaultCommandDescriptionProvider::impl_init_nothrow( const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument )
85 : {
86 : OSL_ENSURE( _rxDocument.is(), "DefaultCommandDescriptionProvider::impl_init_nothrow: no document => no command descriptions!" );
87 0 : if ( !_rxDocument.is() )
88 0 : return;
89 :
90 : try
91 : {
92 0 : Reference< XModuleManager2 > xModuleManager( ModuleManager::create(_rxContext) );
93 0 : ::rtl::OUString sModuleID = xModuleManager->identify( _rxDocument );
94 :
95 0 : Reference< XNameAccess > xUICommandDescriptions( UICommandDescription::create(_rxContext) );
96 0 : m_xCommandAccess.set( xUICommandDescriptions->getByName( sModuleID ), UNO_QUERY_THROW );
97 : }
98 0 : catch( const Exception& )
99 : {
100 : DBG_UNHANDLED_EXCEPTION();
101 : }
102 : }
103 :
104 : //--------------------------------------------------------------------
105 0 : ::rtl::OUString DefaultCommandDescriptionProvider::getCommandDescription( const ::rtl::OUString& _rCommandURL ) const
106 : {
107 0 : if ( !m_xCommandAccess.is() )
108 0 : return ::rtl::OUString();
109 :
110 : try
111 : {
112 0 : ::comphelper::NamedValueCollection aCommandProperties( m_xCommandAccess->getByName( _rCommandURL ) );
113 0 : return aCommandProperties.getOrDefault( "Name", ::rtl::OUString() );
114 : }
115 0 : catch( const Exception& )
116 : {
117 : DBG_UNHANDLED_EXCEPTION();
118 : }
119 :
120 0 : return ::rtl::OUString();
121 : }
122 :
123 : //--------------------------------------------------------------------
124 0 : PCommandDescriptionProvider createDocumentCommandDescriptionProvider(
125 : const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument )
126 : {
127 0 : PCommandDescriptionProvider pDescriptionProvider( new DefaultCommandDescriptionProvider( _rxContext, _rxDocument ) );
128 0 : return pDescriptionProvider;
129 : }
130 :
131 : //........................................................................
132 : } // namespace frm
133 : //........................................................................
134 :
135 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|