1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 
31 #ifndef __FRAMEWORK_UIELEMENT_BUTTONTOOLBARCONTROLLER_HXX
32 #include "uielement/buttontoolbarcontroller.hxx"
33 #endif
34 
35 //_________________________________________________________________________________________________________________
36 //  my own includes
37 //_________________________________________________________________________________________________________________
38 
39 #ifndef __FRAMEWORK_TOOLBAR_HXX_
40 #include "uielement/toolbar.hxx"
41 #endif
42 
43 //_________________________________________________________________________________________________________________
44 //  interface includes
45 //_________________________________________________________________________________________________________________
46 #include <com/sun/star/util/XURLTransformer.hpp>
47 #include <com/sun/star/frame/XDispatchProvider.hpp>
48 #include <com/sun/star/beans/PropertyValue.hpp>
49 #include <com/sun/star/lang/DisposedException.hpp>
50 #include "com/sun/star/util/XMacroExpander.hpp"
51 #include "com/sun/star/uno/XComponentContext.hpp"
52 #include "com/sun/star/beans/XPropertySet.hpp"
53 
54 //_________________________________________________________________________________________________________________
55 //  other includes
56 //_________________________________________________________________________________________________________________
57 
58 #include <rtl/uri.hxx>
59 #include <vos/mutex.hxx>
60 #include <comphelper/processfactory.hxx>
61 #include <unotools/ucbstreamhelper.hxx>
62 #include <tools/urlobj.hxx>
63 #include <vcl/svapp.hxx>
64 #include <vcl/mnemonic.hxx>
65 #include <vcl/window.hxx>
66 #include <vcl/graph.hxx>
67 #include <vcl/bitmap.hxx>
68 #include <svtools/filter.hxx>
69 #include <svtools/miscopt.hxx>
70 #include <dispatch/uieventloghelper.hxx>
71 
72 using namespace ::com::sun::star;
73 using namespace ::com::sun::star::awt;
74 using namespace ::com::sun::star::uno;
75 using namespace ::com::sun::star::beans;
76 using namespace ::com::sun::star::lang;
77 using namespace ::com::sun::star::frame;
78 using namespace ::com::sun::star::util;
79 
80 namespace framework
81 {
82 
83 ButtonToolbarController::ButtonToolbarController(
84     const uno::Reference< lang::XMultiServiceFactory >& rServiceManager,
85     ToolBox*                                            pToolBar,
86     const rtl::OUString&                                aCommand ) :
87     cppu::OWeakObject(),
88     m_bInitialized( sal_False ),
89     m_bDisposed( sal_False ),
90     m_aCommandURL( aCommand ),
91     m_xServiceManager( rServiceManager ),
92     m_pToolbar( pToolBar )
93 {
94 }
95 
96 ButtonToolbarController::~ButtonToolbarController()
97 {
98 }
99 
100         // XInterface
101 uno::Any SAL_CALL ButtonToolbarController::queryInterface( const uno::Type& rType )
102 throw (::com::sun::star::uno::RuntimeException)
103 {
104     Any a = ::cppu::queryInterface(
105                 rType ,
106                 static_cast< frame::XStatusListener* >( this ),
107                 static_cast< frame::XToolbarController* >( this ),
108                 static_cast< lang::XInitialization* >( this ),
109                 static_cast< lang::XComponent* >( this ),
110                 static_cast< util::XUpdatable* >( this ));
111 
112     if ( a.hasValue() )
113         return a;
114 
115     return cppu::OWeakObject::queryInterface( rType );
116 }
117 
118 void SAL_CALL ButtonToolbarController::acquire() throw ()
119 {
120     cppu::OWeakObject::acquire();
121 }
122 
123 void SAL_CALL ButtonToolbarController::release() throw ()
124 {
125     cppu::OWeakObject::release();
126 }
127 
128 // XInitialization
129 void SAL_CALL ButtonToolbarController::initialize(
130     const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments )
131 throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
132 {
133     const rtl::OUString aFrameName( RTL_CONSTASCII_USTRINGPARAM( "Frame" ));
134     const rtl::OUString aCommandURLName( RTL_CONSTASCII_USTRINGPARAM( "CommandURL" ));
135     const rtl::OUString aServiceManagerName( RTL_CONSTASCII_USTRINGPARAM( "ServiceManager" ));
136 
137     bool bInitialized( true );
138 
139     {
140         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
141 
142         if ( m_bDisposed )
143             throw DisposedException();
144 
145         bInitialized = m_bInitialized;
146     }
147 
148     if ( !bInitialized )
149     {
150         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
151         m_bInitialized = sal_True;
152 
153         PropertyValue aPropValue;
154         for ( int i = 0; i < aArguments.getLength(); i++ )
155         {
156             if ( aArguments[i] >>= aPropValue )
157             {
158                 if ( aPropValue.Name.equalsAscii( "Frame" ))
159                     m_xFrame.set(aPropValue.Value,UNO_QUERY);
160                 else if ( aPropValue.Name.equalsAscii( "CommandURL" ))
161                     aPropValue.Value >>= m_aCommandURL;
162                 else if ( aPropValue.Name.equalsAscii( "ServiceManager" ))
163                     m_xServiceManager.set(aPropValue.Value,UNO_QUERY);
164             }
165         }
166     }
167 }
168 
169 // XComponent
170 void SAL_CALL ButtonToolbarController::dispose() throw (::com::sun::star::uno::RuntimeException)
171 {
172     Reference< XComponent > xThis( static_cast< OWeakObject* >(this), UNO_QUERY );
173 
174     {
175         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
176         if ( m_bDisposed )
177             throw DisposedException();
178 
179         m_xServiceManager.clear();
180         m_xURLTransformer.clear();
181         m_xFrame.clear();
182         m_pToolbar = 0;
183         m_bDisposed = sal_True;
184     }
185 }
186 
187 void SAL_CALL ButtonToolbarController::addEventListener(
188     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& )
189 throw (::com::sun::star::uno::RuntimeException)
190 {
191     // do nothing
192 }
193 
194 void SAL_CALL ButtonToolbarController::removeEventListener(
195     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& )
196 throw (::com::sun::star::uno::RuntimeException)
197 {
198     // do nothing
199 }
200 
201 // XUpdatable
202 void SAL_CALL ButtonToolbarController::update()
203 throw (::com::sun::star::uno::RuntimeException)
204 {
205     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
206     if ( m_bDisposed )
207         throw DisposedException();
208 }
209 
210 // XEventListener
211 void SAL_CALL ButtonToolbarController::disposing(
212     const com::sun::star::lang::EventObject& Source )
213 throw ( ::com::sun::star::uno::RuntimeException )
214 {
215     uno::Reference< uno::XInterface > xSource( Source.Source );
216 
217     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
218 
219     if ( m_bDisposed )
220         return;
221 
222     uno::Reference< uno::XInterface > xIfac( m_xFrame, uno::UNO_QUERY );
223     if ( xIfac == xSource )
224         m_xFrame.clear();
225 }
226 
227 void SAL_CALL ButtonToolbarController::statusChanged( const ::com::sun::star::frame::FeatureStateEvent& )
228 throw ( ::com::sun::star::uno::RuntimeException )
229 {
230     // do nothing
231     if ( m_bDisposed )
232         throw DisposedException();
233 }
234 
235 // XToolbarController
236 void SAL_CALL ButtonToolbarController::execute( sal_Int16 KeyModifier )
237 throw (::com::sun::star::uno::RuntimeException)
238 {
239     uno::Reference< frame::XDispatch >      xDispatch;
240     uno::Reference< frame::XFrame >         xFrame;
241     uno::Reference< util::XURLTransformer > xURLTransformer;
242     rtl::OUString                           aCommandURL;
243     ::com::sun::star::util::URL             aTargetURL;
244 
245     {
246         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
247 
248         if ( m_bDisposed )
249             throw DisposedException();
250 
251         if ( m_bInitialized &&
252              m_xFrame.is() &&
253              m_xServiceManager.is() &&
254              m_aCommandURL.getLength() )
255         {
256             if ( !m_xURLTransformer.is() )
257             {
258                 m_xURLTransformer = uno::Reference< util::XURLTransformer >(
259                     m_xServiceManager->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ))),
260                     uno::UNO_QUERY_THROW );
261             }
262 
263             xFrame          = m_xFrame;
264             aCommandURL     = m_aCommandURL;
265             xURLTransformer = m_xURLTransformer;
266         }
267     }
268 
269     uno::Reference< frame::XDispatchProvider > xDispatchProvider( xFrame, uno::UNO_QUERY );
270     if ( xDispatchProvider.is() )
271     {
272         aTargetURL.Complete = aCommandURL;
273         xURLTransformer->parseStrict( aTargetURL );
274         xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
275     }
276 
277     if ( xDispatch.is() )
278     {
279         try
280         {
281             Sequence<PropertyValue>   aArgs( 1 );
282 
283             // Provide key modifier information to dispatch function
284             aArgs[0].Name   = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
285             aArgs[0].Value  <<= KeyModifier;
286 
287             if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
288                 UiEventLogHelper(::rtl::OUString::createFromAscii("ButtonToolbarController")).log(m_xServiceManager, m_xFrame, aTargetURL, aArgs);
289             xDispatch->dispatch( aTargetURL, aArgs );
290         }
291         catch ( DisposedException& )
292         {
293         }
294     }
295 }
296 
297 void SAL_CALL ButtonToolbarController::click()
298 throw (::com::sun::star::uno::RuntimeException)
299 {
300     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
301 
302     if ( m_bDisposed )
303         throw DisposedException();
304 
305     sal_Int16   nKeyModifier( (sal_Int16)m_pToolbar->GetModifier() );
306     execute( nKeyModifier );
307 }
308 
309 void SAL_CALL ButtonToolbarController::doubleClick()
310 throw (::com::sun::star::uno::RuntimeException)
311 {
312     // do nothing
313     if ( m_bDisposed )
314         throw DisposedException();
315 }
316 
317 uno::Reference< awt::XWindow > SAL_CALL ButtonToolbarController::createPopupWindow()
318 throw (::com::sun::star::uno::RuntimeException)
319 {
320     if ( m_bDisposed )
321         throw DisposedException();
322 
323     return uno::Reference< awt::XWindow >();
324 }
325 
326 uno::Reference< awt::XWindow > SAL_CALL ButtonToolbarController::createItemWindow(
327     const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow >& )
328 throw (::com::sun::star::uno::RuntimeException)
329 {
330     if ( m_bDisposed )
331         throw DisposedException();
332 
333     return uno::Reference< awt::XWindow >();
334 }
335 
336 } // namespace
337 
338