1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25 ************************************************************************/
26 
27 // MARKER(update_precomp.py): autogen include statement, do not remove
28 #include "precompiled_forms.hxx"
29 
30 #include "commanddescriptionprovider.hxx"
31 
32 /** === begin UNO includes === **/
33 #include <com/sun/star/container/XNameAccess.hpp>
34 #include <com/sun/star/frame/XModuleManager.hpp>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 /** === end UNO includes === **/
37 
38 #include <comphelper/namedvaluecollection.hxx>
39 #include <tools/diagnose_ex.h>
40 
41 //........................................................................
42 namespace frm
43 {
44 //........................................................................
45 
46 	/** === begin UNO using === **/
47 	using ::com::sun::star::uno::Reference;
48 	using ::com::sun::star::uno::XInterface;
49 	using ::com::sun::star::uno::UNO_QUERY;
50 	using ::com::sun::star::uno::UNO_QUERY_THROW;
51 	using ::com::sun::star::uno::UNO_SET_THROW;
52 	using ::com::sun::star::uno::Exception;
53 	using ::com::sun::star::uno::RuntimeException;
54 	using ::com::sun::star::uno::Any;
55 	using ::com::sun::star::uno::makeAny;
56 	using ::com::sun::star::uno::Sequence;
57 	using ::com::sun::star::uno::Type;
58     using ::com::sun::star::frame::XModel;
59     using ::com::sun::star::container::XNameAccess;
60     using ::com::sun::star::frame::XModuleManager;
61     using ::com::sun::star::beans::PropertyValue;
62 	/** === end UNO using === **/
63 
64 	//====================================================================
65 	//= DefaultCommandDescriptionProvider
66 	//====================================================================
67     class DefaultCommandDescriptionProvider : public ICommandDescriptionProvider
68 	{
69     public:
70         DefaultCommandDescriptionProvider( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
71         {
72             impl_init_nothrow( _rContext, _rxDocument );
73         }
74 
75         ~DefaultCommandDescriptionProvider()
76         {
77         }
78 
79         // ICommandDescriptionProvider
80         virtual ::rtl::OUString getCommandDescription( const ::rtl::OUString& _rCommandURL ) const;
81 
82     private:
83         void    impl_init_nothrow( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument );
84 
85     private:
86         Reference< XNameAccess >    m_xCommandAccess;
87 	};
88 
89 
90 	//--------------------------------------------------------------------
91     void DefaultCommandDescriptionProvider::impl_init_nothrow( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
92     {
93         OSL_ENSURE( _rxDocument.is(), "DefaultCommandDescriptionProvider::impl_init_nothrow: no document => no command descriptions!" );
94         if ( !_rxDocument.is() )
95             return;
96 
97         try
98         {
99             Reference< XModuleManager > xModuleManager( _rContext.createComponent( "com.sun.star.frame.ModuleManager" ), UNO_QUERY_THROW );
100             ::rtl::OUString sModuleID = xModuleManager->identify( _rxDocument );
101 
102             Reference< XNameAccess > xUICommandDescriptions( _rContext.createComponent( "com.sun.star.frame.UICommandDescription" ), UNO_QUERY_THROW );
103             m_xCommandAccess.set( xUICommandDescriptions->getByName( sModuleID ), UNO_QUERY_THROW );
104         }
105         catch( const Exception& )
106         {
107         	DBG_UNHANDLED_EXCEPTION();
108         }
109     }
110 
111 	//--------------------------------------------------------------------
112     ::rtl::OUString DefaultCommandDescriptionProvider::getCommandDescription( const ::rtl::OUString& _rCommandURL ) const
113     {
114         if ( !m_xCommandAccess.is() )
115             return ::rtl::OUString();
116 
117         try
118         {
119             ::comphelper::NamedValueCollection aCommandProperties( m_xCommandAccess->getByName( _rCommandURL ) );
120             return aCommandProperties.getOrDefault( "Name", ::rtl::OUString() );
121         }
122         catch( const Exception& )
123         {
124         	DBG_UNHANDLED_EXCEPTION();
125         }
126 
127         return ::rtl::OUString();
128     }
129 
130 	//--------------------------------------------------------------------
131     PCommandDescriptionProvider createDocumentCommandDescriptionProvider(
132         const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
133     {
134         PCommandDescriptionProvider pDescriptionProvider( new DefaultCommandDescriptionProvider( _rContext, _rxDocument ) );
135         return pDescriptionProvider;
136     }
137 
138 //........................................................................
139 } // namespace frm
140 //........................................................................
141