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 13056 : 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 13056 : , m_nID( nID )
60 : {
61 : // Initialization is done through ctor
62 13056 : m_bInitialized = true;
63 :
64 : // insert main command to our listener map
65 13056 : if ( !m_aCommandURL.isEmpty() )
66 13056 : m_aListenerMap.insert( URLToDispatchMap::value_type( aCommand, Reference< XDispatch >() ));
67 13056 : }
68 :
69 26112 : GenericToolboxController::~GenericToolboxController()
70 : {
71 26112 : }
72 :
73 13056 : void SAL_CALL GenericToolboxController::dispose()
74 : throw ( RuntimeException, std::exception )
75 : {
76 13056 : SolarMutexGuard aSolarMutexGuard;
77 13056 : m_pToolbox.clear();
78 13056 : m_nID = 0;
79 13056 : svt::ToolboxController::dispose();
80 13056 : }
81 :
82 0 : void SAL_CALL GenericToolboxController::execute( sal_Int16 /*KeyModifier*/ )
83 : throw ( RuntimeException, std::exception )
84 : {
85 0 : Reference< XDispatch > xDispatch;
86 0 : Reference< XURLTransformer > xURLTransformer;
87 0 : OUString aCommandURL;
88 :
89 : {
90 0 : SolarMutexGuard aSolarMutexGuard;
91 :
92 0 : if ( m_bDisposed )
93 0 : throw DisposedException();
94 :
95 0 : if ( m_bInitialized &&
96 0 : m_xFrame.is() &&
97 0 : m_xContext.is() &&
98 0 : !m_aCommandURL.isEmpty() )
99 : {
100 0 : xURLTransformer = URLTransformer::create( m_xContext );
101 :
102 0 : aCommandURL = m_aCommandURL;
103 0 : URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
104 0 : if ( pIter != m_aListenerMap.end() )
105 0 : xDispatch = pIter->second;
106 0 : }
107 : }
108 :
109 0 : if ( xDispatch.is() && xURLTransformer.is() )
110 : {
111 0 : com::sun::star::util::URL aTargetURL;
112 0 : Sequence<PropertyValue> aArgs;
113 :
114 0 : aTargetURL.Complete = aCommandURL;
115 0 : xURLTransformer->parseStrict( aTargetURL );
116 :
117 : // Execute dispatch asynchronously
118 0 : ExecuteInfo* pExecuteInfo = new ExecuteInfo;
119 0 : pExecuteInfo->xDispatch = xDispatch;
120 0 : pExecuteInfo->aTargetURL = aTargetURL;
121 0 : pExecuteInfo->aArgs = aArgs;
122 0 : Application::PostUserEvent( LINK(0, GenericToolboxController , ExecuteHdl_Impl), pExecuteInfo );
123 0 : }
124 0 : }
125 :
126 20509 : void GenericToolboxController::statusChanged( const FeatureStateEvent& Event )
127 : throw ( RuntimeException, std::exception )
128 : {
129 20509 : SolarMutexGuard aSolarMutexGuard;
130 :
131 20509 : if ( m_bDisposed )
132 20509 : return;
133 :
134 20509 : if ( m_pToolbox )
135 : {
136 20509 : m_pToolbox->EnableItem( m_nID, Event.IsEnabled );
137 :
138 20509 : ToolBoxItemBits nItemBits = m_pToolbox->GetItemBits( m_nID );
139 20509 : nItemBits &= ~ToolBoxItemBits::CHECKABLE;
140 20509 : TriState eTri = TRISTATE_FALSE;
141 :
142 : bool bValue;
143 20509 : OUString aStrValue;
144 41018 : ItemStatus aItemState;
145 :
146 20509 : if ( Event.State >>= bValue )
147 : {
148 : // Boolean, treat it as checked/unchecked
149 9647 : m_pToolbox->SetItemBits( m_nID, nItemBits );
150 9647 : m_pToolbox->CheckItem( m_nID, bValue );
151 9647 : if ( bValue )
152 606 : eTri = TRISTATE_TRUE;
153 9647 : nItemBits |= ToolBoxItemBits::CHECKABLE;
154 : }
155 10862 : else if ( Event.State >>= aStrValue )
156 : {
157 0 : m_pToolbox->SetItemText( m_nID, aStrValue );
158 : }
159 10862 : else if ( Event.State >>= aItemState )
160 : {
161 10 : eTri = TRISTATE_INDET;
162 10 : nItemBits |= ToolBoxItemBits::CHECKABLE;
163 : }
164 :
165 20509 : m_pToolbox->SetItemState( m_nID, eTri );
166 41018 : m_pToolbox->SetItemBits( m_nID, nItemBits );
167 20509 : }
168 : }
169 :
170 0 : IMPL_STATIC_LINK( GenericToolboxController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
171 : {
172 : try
173 : {
174 : // Asynchronous execution as this can lead to our own destruction!
175 : // Framework can recycle our current frame and the layout manager disposes all user interface
176 : // elements if a component gets detached from its frame!
177 0 : pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
178 : }
179 0 : catch ( Exception& )
180 : {
181 : }
182 0 : delete pExecuteInfo;
183 0 : return 0;
184 : }
185 :
186 : } // namespace
187 :
188 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|