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_svtools.hxx"
26 #include <svtools/generictoolboxcontroller.hxx>
27 
28 //_________________________________________________________________________________________________________________
29 //	my own includes
30 //_________________________________________________________________________________________________________________
31 
32 //_________________________________________________________________________________________________________________
33 //	interface includes
34 //_________________________________________________________________________________________________________________
35 #include <com/sun/star/util/XURLTransformer.hpp>
36 #include <com/sun/star/frame/XDispatchProvider.hpp>
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/lang/DisposedException.hpp>
39 #include <com/sun/star/frame/status/ItemStatus.hpp>
40 #include <com/sun/star/frame/status/ItemState.hpp>
41 
42 //_________________________________________________________________________________________________________________
43 //	other includes
44 //_________________________________________________________________________________________________________________
45 #include <vos/mutex.hxx>
46 #include <vcl/svapp.hxx>
47 
48 using namespace ::com::sun::star::awt;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::beans;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::frame;
53 using namespace ::com::sun::star::frame::status;
54 using namespace ::com::sun::star::util;
55 
56 namespace svt
57 {
58 
59 struct ExecuteInfo
60 {
61     ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >     xDispatch;
62     ::com::sun::star::util::URL                                                aTargetURL;
63     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >  aArgs;
64 };
65 
GenericToolboxController(const Reference<XMultiServiceFactory> & rServiceManager,const Reference<XFrame> & rFrame,ToolBox * pToolbox,sal_uInt16 nID,const::rtl::OUString & aCommand)66 GenericToolboxController::GenericToolboxController( const Reference< XMultiServiceFactory >& rServiceManager,
67                                                     const Reference< XFrame >&               rFrame,
68                                                     ToolBox*                                 pToolbox,
69                                                     sal_uInt16                                   nID,
70                                                     const ::rtl::OUString&                          aCommand ) :
71     svt::ToolboxController( rServiceManager, rFrame, aCommand )
72     ,   m_pToolbox( pToolbox )
73     ,   m_nID( nID )
74 {
75     // Initialization is done through ctor
76     m_bInitialized = sal_True;
77 
78     // insert main command to our listener map
79     if ( m_aCommandURL.getLength() )
80         m_aListenerMap.insert( URLToDispatchMap::value_type( aCommand, Reference< XDispatch >() ));
81 }
82 
~GenericToolboxController()83 GenericToolboxController::~GenericToolboxController()
84 {
85 }
86 
dispose()87 void SAL_CALL GenericToolboxController::dispose()
88 throw ( RuntimeException )
89 {
90     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
91 
92     svt::ToolboxController::dispose();
93 
94     m_pToolbox = 0;
95     m_nID = 0;
96 }
97 
execute(sal_Int16)98 void SAL_CALL GenericToolboxController::execute( sal_Int16 /*KeyModifier*/ )
99 throw ( RuntimeException )
100 {
101     Reference< XDispatch >       xDispatch;
102     Reference< XURLTransformer > xURLTransformer;
103     ::rtl::OUString                     aCommandURL;
104 
105     {
106         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
107 
108         if ( m_bDisposed )
109             throw DisposedException();
110 
111         if ( m_bInitialized &&
112              m_xFrame.is() &&
113              m_xServiceManager.is() &&
114              m_aCommandURL.getLength() )
115         {
116             xURLTransformer = Reference< XURLTransformer >( m_xServiceManager->createInstance(
117                                                                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ))),
118                                                             UNO_QUERY );
119 
120             aCommandURL = m_aCommandURL;
121             URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
122             if ( pIter != m_aListenerMap.end() )
123                 xDispatch = pIter->second;
124         }
125     }
126 
127     if ( xDispatch.is() && xURLTransformer.is() )
128     {
129         com::sun::star::util::URL aTargetURL;
130         Sequence<PropertyValue>   aArgs;
131 
132         aTargetURL.Complete = aCommandURL;
133         xURLTransformer->parseStrict( aTargetURL );
134 
135         // Execute dispatch asynchronously
136         ExecuteInfo* pExecuteInfo = new ExecuteInfo;
137         pExecuteInfo->xDispatch     = xDispatch;
138         pExecuteInfo->aTargetURL    = aTargetURL;
139         pExecuteInfo->aArgs         = aArgs;
140         Application::PostUserEvent( STATIC_LINK(0, GenericToolboxController , ExecuteHdl_Impl), pExecuteInfo );
141     }
142 }
143 
statusChanged(const FeatureStateEvent & Event)144 void GenericToolboxController::statusChanged( const FeatureStateEvent& Event )
145 throw ( RuntimeException )
146 {
147     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
148 
149     if ( m_bDisposed )
150         return;
151 
152     if ( m_pToolbox )
153     {
154         m_pToolbox->EnableItem( m_nID, Event.IsEnabled );
155 
156         sal_uInt16 nItemBits = m_pToolbox->GetItemBits( m_nID );
157         nItemBits &= ~TIB_CHECKABLE;
158 	    TriState eTri = STATE_NOCHECK;
159 
160         sal_Bool        bValue = sal_Bool();
161         rtl::OUString   aStrValue;
162         ItemStatus      aItemState;
163 
164         if ( Event.State >>= bValue )
165         {
166             // Boolean, treat it as checked/unchecked
167             m_pToolbox->SetItemBits( m_nID, nItemBits );
168             m_pToolbox->CheckItem( m_nID, bValue );
169             if ( bValue )
170                 eTri = STATE_CHECK;
171 		    nItemBits |= TIB_CHECKABLE;
172         }
173         else if ( Event.State >>= aStrValue )
174         {
175             m_pToolbox->SetItemText( m_nID, aStrValue );
176         }
177         else if ( Event.State >>= aItemState )
178         {
179 			eTri = STATE_DONTKNOW;
180             nItemBits |= TIB_CHECKABLE;
181         }
182 
183         m_pToolbox->SetItemState( m_nID, eTri );
184 	    m_pToolbox->SetItemBits( m_nID, nItemBits );
185     }
186 }
187 
IMPL_STATIC_LINK_NOINSTANCE(GenericToolboxController,ExecuteHdl_Impl,ExecuteInfo *,pExecuteInfo)188 IMPL_STATIC_LINK_NOINSTANCE( GenericToolboxController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
189 {
190    try
191    {
192        // Asynchronous execution as this can lead to our own destruction!
193        // Framework can recycle our current frame and the layout manager disposes all user interface
194        // elements if a component gets detached from its frame!
195        pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
196    }
197    catch ( Exception& )
198    {
199    }
200    delete pExecuteInfo;
201    return 0;
202 }
203 
204 } // namespace
205