1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_forms.hxx"
26 
27 #include "commanddescriptionprovider.hxx"
28 
29 /** === begin UNO includes === **/
30 #include <com/sun/star/container/XNameAccess.hpp>
31 #include <com/sun/star/frame/XModuleManager.hpp>
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 /** === end UNO includes === **/
34 
35 #include <comphelper/namedvaluecollection.hxx>
36 #include <tools/diagnose_ex.h>
37 
38 //........................................................................
39 namespace frm
40 {
41 //........................................................................
42 
43 	/** === begin UNO using === **/
44 	using ::com::sun::star::uno::Reference;
45 	using ::com::sun::star::uno::XInterface;
46 	using ::com::sun::star::uno::UNO_QUERY;
47 	using ::com::sun::star::uno::UNO_QUERY_THROW;
48 	using ::com::sun::star::uno::UNO_SET_THROW;
49 	using ::com::sun::star::uno::Exception;
50 	using ::com::sun::star::uno::RuntimeException;
51 	using ::com::sun::star::uno::Any;
52 	using ::com::sun::star::uno::makeAny;
53 	using ::com::sun::star::uno::Sequence;
54 	using ::com::sun::star::uno::Type;
55     using ::com::sun::star::frame::XModel;
56     using ::com::sun::star::container::XNameAccess;
57     using ::com::sun::star::frame::XModuleManager;
58     using ::com::sun::star::beans::PropertyValue;
59 	/** === end UNO using === **/
60 
61 	//====================================================================
62 	//= DefaultCommandDescriptionProvider
63 	//====================================================================
64     class DefaultCommandDescriptionProvider : public ICommandDescriptionProvider
65 	{
66     public:
DefaultCommandDescriptionProvider(const::comphelper::ComponentContext & _rContext,const Reference<XModel> & _rxDocument)67         DefaultCommandDescriptionProvider( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
68         {
69             impl_init_nothrow( _rContext, _rxDocument );
70         }
71 
~DefaultCommandDescriptionProvider()72         ~DefaultCommandDescriptionProvider()
73         {
74         }
75 
76         // ICommandDescriptionProvider
77         virtual ::rtl::OUString getCommandDescription( const ::rtl::OUString& _rCommandURL ) const;
78 
79     private:
80         void    impl_init_nothrow( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument );
81 
82     private:
83         Reference< XNameAccess >    m_xCommandAccess;
84 	};
85 
86 
87 	//--------------------------------------------------------------------
impl_init_nothrow(const::comphelper::ComponentContext & _rContext,const Reference<XModel> & _rxDocument)88     void DefaultCommandDescriptionProvider::impl_init_nothrow( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
89     {
90         OSL_ENSURE( _rxDocument.is(), "DefaultCommandDescriptionProvider::impl_init_nothrow: no document => no command descriptions!" );
91         if ( !_rxDocument.is() )
92             return;
93 
94         try
95         {
96             Reference< XModuleManager > xModuleManager( _rContext.createComponent( "com.sun.star.frame.ModuleManager" ), UNO_QUERY_THROW );
97             ::rtl::OUString sModuleID = xModuleManager->identify( _rxDocument );
98 
99             Reference< XNameAccess > xUICommandDescriptions( _rContext.createComponent( "com.sun.star.frame.UICommandDescription" ), UNO_QUERY_THROW );
100             m_xCommandAccess.set( xUICommandDescriptions->getByName( sModuleID ), UNO_QUERY_THROW );
101         }
102         catch( const Exception& )
103         {
104         	DBG_UNHANDLED_EXCEPTION();
105         }
106     }
107 
108 	//--------------------------------------------------------------------
getCommandDescription(const::rtl::OUString & _rCommandURL) const109     ::rtl::OUString DefaultCommandDescriptionProvider::getCommandDescription( const ::rtl::OUString& _rCommandURL ) const
110     {
111         if ( !m_xCommandAccess.is() )
112             return ::rtl::OUString();
113 
114         try
115         {
116             ::comphelper::NamedValueCollection aCommandProperties( m_xCommandAccess->getByName( _rCommandURL ) );
117             return aCommandProperties.getOrDefault( "Name", ::rtl::OUString() );
118         }
119         catch( const Exception& )
120         {
121         	DBG_UNHANDLED_EXCEPTION();
122         }
123 
124         return ::rtl::OUString();
125     }
126 
127 	//--------------------------------------------------------------------
createDocumentCommandDescriptionProvider(const::comphelper::ComponentContext & _rContext,const Reference<XModel> & _rxDocument)128     PCommandDescriptionProvider createDocumentCommandDescriptionProvider(
129         const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
130     {
131         PCommandDescriptionProvider pDescriptionProvider( new DefaultCommandDescriptionProvider( _rContext, _rxDocument ) );
132         return pDescriptionProvider;
133     }
134 
135 //........................................................................
136 } // namespace frm
137 //........................................................................
138