Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <svtools/generictoolboxcontroller.hxx>
21 :
22 : #include <com/sun/star/util/URLTransformer.hpp>
23 : #include <com/sun/star/util/XURLTransformer.hpp>
24 : #include <com/sun/star/frame/XDispatchProvider.hpp>
25 : #include <com/sun/star/beans/PropertyValue.hpp>
26 : #include <com/sun/star/lang/DisposedException.hpp>
27 : #include <com/sun/star/frame/status/ItemStatus.hpp>
28 : #include <com/sun/star/frame/status/ItemState.hpp>
29 :
30 : #include <comphelper/processfactory.hxx>
31 : #include <osl/mutex.hxx>
32 : #include <vcl/svapp.hxx>
33 :
34 : using namespace ::com::sun::star::awt;
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::com::sun::star::beans;
37 : using namespace ::com::sun::star::lang;
38 : using namespace ::com::sun::star::frame;
39 : using namespace ::com::sun::star::frame::status;
40 : using namespace ::com::sun::star::util;
41 :
42 : namespace svt
43 : {
44 :
45 0 : struct ExecuteInfo
46 : {
47 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > xDispatch;
48 : ::com::sun::star::util::URL aTargetURL;
49 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aArgs;
50 : };
51 :
52 14764 : GenericToolboxController::GenericToolboxController( const Reference< XComponentContext >& rxContext,
53 : const Reference< XFrame >& rFrame,
54 : ToolBox* pToolbox,
55 : sal_uInt16 nID,
56 : const OUString& aCommand ) :
57 : svt::ToolboxController( rxContext, rFrame, aCommand )
58 : , m_pToolbox( pToolbox )
59 14764 : , m_nID( nID )
60 : {
61 : // Initialization is done through ctor
62 14764 : m_bInitialized = true;
63 :
64 : // insert main command to our listener map
65 14764 : if ( !m_aCommandURL.isEmpty() )
66 14764 : m_aListenerMap.insert( URLToDispatchMap::value_type( aCommand, Reference< XDispatch >() ));
67 14764 : }
68 :
69 29528 : GenericToolboxController::~GenericToolboxController()
70 : {
71 29528 : }
72 :
73 14764 : void SAL_CALL GenericToolboxController::dispose()
74 : throw ( RuntimeException, std::exception )
75 : {
76 14764 : SolarMutexGuard aSolarMutexGuard;
77 :
78 14764 : svt::ToolboxController::dispose();
79 :
80 14764 : m_pToolbox = 0;
81 14764 : m_nID = 0;
82 14764 : }
83 :
84 0 : void SAL_CALL GenericToolboxController::execute( sal_Int16 /*KeyModifier*/ )
85 : throw ( RuntimeException, std::exception )
86 : {
87 0 : Reference< XDispatch > xDispatch;
88 0 : Reference< XURLTransformer > xURLTransformer;
89 0 : OUString aCommandURL;
90 :
91 : {
92 0 : SolarMutexGuard aSolarMutexGuard;
93 :
94 0 : if ( m_bDisposed )
95 0 : throw DisposedException();
96 :
97 0 : if ( m_bInitialized &&
98 0 : m_xFrame.is() &&
99 0 : m_xContext.is() &&
100 0 : !m_aCommandURL.isEmpty() )
101 : {
102 0 : xURLTransformer = URLTransformer::create( m_xContext );
103 :
104 0 : aCommandURL = m_aCommandURL;
105 0 : URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
106 0 : if ( pIter != m_aListenerMap.end() )
107 0 : xDispatch = pIter->second;
108 0 : }
109 : }
110 :
111 0 : if ( xDispatch.is() && xURLTransformer.is() )
112 : {
113 0 : com::sun::star::util::URL aTargetURL;
114 0 : Sequence<PropertyValue> aArgs;
115 :
116 0 : aTargetURL.Complete = aCommandURL;
117 0 : xURLTransformer->parseStrict( aTargetURL );
118 :
119 : // Execute dispatch asynchronously
120 0 : ExecuteInfo* pExecuteInfo = new ExecuteInfo;
121 0 : pExecuteInfo->xDispatch = xDispatch;
122 0 : pExecuteInfo->aTargetURL = aTargetURL;
123 0 : pExecuteInfo->aArgs = aArgs;
124 0 : Application::PostUserEvent( STATIC_LINK(0, GenericToolboxController , ExecuteHdl_Impl), pExecuteInfo );
125 0 : }
126 0 : }
127 :
128 18344 : void GenericToolboxController::statusChanged( const FeatureStateEvent& Event )
129 : throw ( RuntimeException, std::exception )
130 : {
131 18344 : SolarMutexGuard aSolarMutexGuard;
132 :
133 18344 : if ( m_bDisposed )
134 18344 : return;
135 :
136 18344 : if ( m_pToolbox )
137 : {
138 18344 : m_pToolbox->EnableItem( m_nID, Event.IsEnabled );
139 :
140 18344 : ToolBoxItemBits nItemBits = m_pToolbox->GetItemBits( m_nID );
141 18344 : nItemBits &= ~ToolBoxItemBits::CHECKABLE;
142 18344 : TriState eTri = TRISTATE_FALSE;
143 :
144 : bool bValue;
145 18344 : OUString aStrValue;
146 36688 : ItemStatus aItemState;
147 :
148 18344 : if ( Event.State >>= bValue )
149 : {
150 : // Boolean, treat it as checked/unchecked
151 10112 : m_pToolbox->SetItemBits( m_nID, nItemBits );
152 10112 : m_pToolbox->CheckItem( m_nID, bValue );
153 10112 : if ( bValue )
154 926 : eTri = TRISTATE_TRUE;
155 10112 : nItemBits |= ToolBoxItemBits::CHECKABLE;
156 : }
157 8232 : else if ( Event.State >>= aStrValue )
158 : {
159 0 : m_pToolbox->SetItemText( m_nID, aStrValue );
160 : }
161 8232 : else if ( Event.State >>= aItemState )
162 : {
163 10 : eTri = TRISTATE_INDET;
164 10 : nItemBits |= ToolBoxItemBits::CHECKABLE;
165 : }
166 :
167 18344 : m_pToolbox->SetItemState( m_nID, eTri );
168 36688 : m_pToolbox->SetItemBits( m_nID, nItemBits );
169 18344 : }
170 : }
171 :
172 0 : IMPL_STATIC_LINK_NOINSTANCE( GenericToolboxController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
173 : {
174 : try
175 : {
176 : // Asynchronous execution as this can lead to our own destruction!
177 : // Framework can recycle our current frame and the layout manager disposes all user interface
178 : // elements if a component gets detached from its frame!
179 0 : pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
180 : }
181 0 : catch ( Exception& )
182 : {
183 : }
184 0 : delete pExecuteInfo;
185 0 : return 0;
186 : }
187 :
188 1227 : } // namespace
189 :
190 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|