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