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 <dispatch/popupmenudispatcher.hxx>
21 : #include <general.h>
22 : #include <framework/menuconfiguration.hxx>
23 : #include <framework/addonmenu.hxx>
24 : #include <services.h>
25 : #include <properties.h>
26 :
27 : #include <com/sun/star/frame/FrameSearchFlag.hpp>
28 : #include <com/sun/star/frame/XLayoutManager2.hpp>
29 : #include <com/sun/star/awt/WindowAttribute.hpp>
30 : #include <com/sun/star/awt/WindowDescriptor.hpp>
31 : #include <com/sun/star/awt/PosSize.hpp>
32 : #include <com/sun/star/awt/XWindowPeer.hpp>
33 : #include <com/sun/star/beans/UnknownPropertyException.hpp>
34 : #include <com/sun/star/lang/WrappedTargetException.hpp>
35 : #include <com/sun/star/beans/XPropertySet.hpp>
36 : #include <com/sun/star/container/XEnumeration.hpp>
37 : #include <com/sun/star/uri/UriReferenceFactory.hpp>
38 : #include <com/sun/star/ui/XUIElement.hpp>
39 :
40 : #include <cppuhelper/supportsservice.hxx>
41 : #include <osl/mutex.hxx>
42 : #include <rtl/ustrbuf.hxx>
43 : #include <ucbhelper/content.hxx>
44 : #include <vcl/svapp.hxx>
45 :
46 : namespace framework{
47 :
48 : using namespace ::com::sun::star;
49 : using namespace ::com::sun::star::awt;
50 : using namespace ::com::sun::star::beans;
51 : using namespace ::com::sun::star::container;
52 : using namespace ::com::sun::star::frame;
53 : using namespace ::com::sun::star::lang;
54 : using namespace ::com::sun::star::uno;
55 : using namespace ::com::sun::star::util;
56 : using namespace ::cppu;
57 : using namespace ::osl;
58 :
59 0 : PopupMenuDispatcher::PopupMenuDispatcher(
60 : const uno::Reference< XComponentContext >& xContext )
61 : : m_xContext ( xContext )
62 : , m_aListenerContainer ( m_mutex )
63 : , m_bAlreadyDisposed ( false )
64 0 : , m_bActivateListener ( false )
65 : {
66 0 : }
67 :
68 0 : PopupMenuDispatcher::~PopupMenuDispatcher()
69 : {
70 : // Warn programmer if he forgot to dispose this instance.
71 : // We must release all our references ...
72 : // and a dtor isn't the best place to do that!
73 0 : }
74 :
75 0 : OUString SAL_CALL PopupMenuDispatcher::getImplementationName() throw( css::uno::RuntimeException, std::exception )
76 : {
77 0 : return impl_getStaticImplementationName();
78 : }
79 :
80 0 : sal_Bool SAL_CALL PopupMenuDispatcher::supportsService( const OUString& sServiceName )
81 : throw( css::uno::RuntimeException, std::exception )
82 : {
83 0 : return cppu::supportsService(this, sServiceName);
84 : }
85 :
86 0 : css::uno::Sequence< OUString > SAL_CALL PopupMenuDispatcher::getSupportedServiceNames()
87 : throw( css::uno::RuntimeException, std::exception )
88 : {
89 0 : return impl_getStaticSupportedServiceNames();
90 : }
91 :
92 0 : css::uno::Sequence< OUString > PopupMenuDispatcher::impl_getStaticSupportedServiceNames()
93 : {
94 0 : css::uno::Sequence< OUString > seqServiceNames( 1 );
95 0 : seqServiceNames.getArray() [0] = SERVICENAME_PROTOCOLHANDLER;
96 0 : return seqServiceNames;
97 : }
98 :
99 98 : OUString PopupMenuDispatcher::impl_getStaticImplementationName()
100 : {
101 98 : return IMPLEMENTATIONNAME_POPUPMENUDISPATCHER;
102 : }
103 :
104 : css::uno::Reference< css::uno::XInterface >
105 0 : SAL_CALL PopupMenuDispatcher::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager )
106 : throw( css::uno::Exception )
107 : {
108 : /* create new instance of service */
109 0 : PopupMenuDispatcher* pClass = new PopupMenuDispatcher( comphelper::getComponentContext(xServiceManager) );
110 : /* hold it alive by increasing his ref count!!! */
111 0 : css::uno::Reference< css::uno::XInterface > xService( static_cast< ::cppu::OWeakObject* >(pClass), css::uno::UNO_QUERY );
112 : /* initialize new service instance ... he can use his own refcount ... we hold it! */
113 0 : pClass->impl_initService();
114 : /* return new created service as reference */
115 0 : return xService;
116 : }
117 :
118 : css::uno::Reference< css::lang::XSingleServiceFactory >
119 0 : PopupMenuDispatcher::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager )
120 : {
121 : css::uno::Reference< css::lang::XSingleServiceFactory > xReturn (
122 : cppu::createSingleFactory ( xServiceManager,
123 : PopupMenuDispatcher::impl_getStaticImplementationName() ,
124 : PopupMenuDispatcher::impl_createInstance ,
125 : PopupMenuDispatcher::impl_getStaticSupportedServiceNames() )
126 0 : );
127 0 : return xReturn;
128 : }
129 :
130 0 : DEFINE_INIT_SERVICE(PopupMenuDispatcher,
131 : {
132 : /*Attention
133 : I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
134 : to create a new instance of this class by our own supported service factory.
135 : see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further information!
136 : */
137 : }
138 : )
139 :
140 0 : void SAL_CALL PopupMenuDispatcher::initialize( const css::uno::Sequence< css::uno::Any >& lArguments )
141 : throw( css::uno::Exception, css::uno::RuntimeException, std::exception)
142 : {
143 0 : css::uno::Reference< css::frame::XFrame > xFrame;
144 :
145 0 : SolarMutexGuard g;
146 0 : for (int a=0; a<lArguments.getLength(); ++a)
147 : {
148 0 : if (a==0)
149 : {
150 0 : lArguments[a] >>= xFrame;
151 0 : m_xWeakFrame = xFrame;
152 :
153 0 : m_bActivateListener = true;
154 : uno::Reference< css::frame::XFrameActionListener > xFrameActionListener(
155 0 : (OWeakObject *)this, css::uno::UNO_QUERY );
156 0 : xFrame->addFrameActionListener( xFrameActionListener );
157 : }
158 0 : }
159 0 : }
160 :
161 : css::uno::Reference< css::frame::XDispatch >
162 0 : SAL_CALL PopupMenuDispatcher::queryDispatch(
163 : const css::util::URL& rURL ,
164 : const OUString& sTarget ,
165 : sal_Int32 nFlags )
166 : throw( css::uno::RuntimeException, std::exception )
167 : {
168 0 : css::uno::Reference< css::frame::XDispatch > xDispatch;
169 :
170 0 : if ( rURL.Complete.startsWith( "vnd.sun.star.popup:" ) )
171 : {
172 : // --- SAFE ---
173 0 : SolarMutexClearableGuard aGuard;
174 0 : impl_RetrievePopupControllerQuery();
175 0 : impl_CreateUriRefFactory();
176 :
177 0 : css::uno::Reference< css::container::XNameAccess > xPopupCtrlQuery( m_xPopupCtrlQuery );
178 0 : css::uno::Reference< css::uri::XUriReferenceFactory > xUriRefFactory( m_xUriRefFactory );
179 0 : aGuard.clear();
180 : // --- SAFE ---
181 :
182 0 : if ( xPopupCtrlQuery.is() )
183 : {
184 : try
185 : {
186 : // Just use the main part of the URL for popup menu controllers
187 0 : sal_Int32 nQueryPart( 0 );
188 0 : sal_Int32 nSchemePart( 0 );
189 0 : OUString aBaseURL( "vnd.sun.star.popup:" );
190 0 : OUString aURL( rURL.Complete );
191 :
192 0 : nSchemePart = aURL.indexOf( ':' );
193 0 : if (( nSchemePart > 0 ) &&
194 0 : ( aURL.getLength() > ( nSchemePart+1 )))
195 : {
196 0 : nQueryPart = aURL.indexOf( '?', nSchemePart );
197 0 : if ( nQueryPart > 0 )
198 0 : aBaseURL += aURL.copy( nSchemePart+1, nQueryPart-(nSchemePart+1) );
199 0 : else if ( nQueryPart == -1 )
200 0 : aBaseURL += aURL.copy( nSchemePart+1 );
201 : }
202 :
203 0 : css::uno::Reference< css::frame::XDispatchProvider > xDispatchProvider;
204 :
205 : // Find popup menu controller using the base URL
206 0 : xPopupCtrlQuery->getByName( aBaseURL ) >>= xDispatchProvider;
207 0 : aGuard.clear();
208 :
209 : // Ask popup menu dispatch provider for dispatch object
210 0 : if ( xDispatchProvider.is() )
211 0 : xDispatch = xDispatchProvider->queryDispatch( rURL, sTarget, nFlags );
212 : }
213 0 : catch ( const RuntimeException& )
214 : {
215 0 : throw;
216 : }
217 0 : catch ( const Exception& )
218 : {
219 : }
220 0 : }
221 : }
222 0 : return xDispatch;
223 : }
224 :
225 : css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL
226 0 : PopupMenuDispatcher::queryDispatches(
227 : const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor )
228 : throw( css::uno::RuntimeException, std::exception )
229 : {
230 0 : sal_Int32 nCount = lDescriptor.getLength();
231 0 : css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatcher( nCount );
232 0 : for( sal_Int32 i=0; i<nCount; ++i )
233 : {
234 0 : lDispatcher[i] = this->queryDispatch(
235 0 : lDescriptor[i].FeatureURL,
236 0 : lDescriptor[i].FrameName,
237 0 : lDescriptor[i].SearchFlags);
238 : }
239 0 : return lDispatcher;
240 : }
241 :
242 0 : void SAL_CALL PopupMenuDispatcher::dispatch( const URL& /*aURL*/, const Sequence< PropertyValue >& /*seqProperties*/ )
243 : throw( RuntimeException, std::exception )
244 : {
245 0 : }
246 :
247 0 : void SAL_CALL PopupMenuDispatcher::addStatusListener( const uno::Reference< XStatusListener >& xControl,
248 : const URL& aURL )
249 : throw( RuntimeException, std::exception )
250 : {
251 0 : SolarMutexGuard g;
252 : // Safe impossible cases
253 : // Add listener to container.
254 0 : m_aListenerContainer.addInterface( aURL.Complete, xControl );
255 0 : }
256 :
257 0 : void SAL_CALL PopupMenuDispatcher::removeStatusListener( const uno::Reference< XStatusListener >& xControl,
258 : const URL& aURL )
259 : throw( RuntimeException, std::exception )
260 : {
261 0 : SolarMutexGuard g;
262 : // Safe impossible cases
263 : // Add listener to container.
264 0 : m_aListenerContainer.removeInterface( aURL.Complete, xControl );
265 0 : }
266 :
267 0 : void SAL_CALL PopupMenuDispatcher::frameAction( const FrameActionEvent& aEvent )
268 : throw ( RuntimeException, std::exception )
269 : {
270 0 : SolarMutexGuard g;
271 0 : if (( aEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING ) ||
272 0 : ( aEvent.Action == css::frame::FrameAction_COMPONENT_ATTACHED ))
273 : {
274 : // Reset query reference to requery it again next time
275 0 : m_xPopupCtrlQuery.clear();
276 0 : }
277 0 : }
278 :
279 0 : void SAL_CALL PopupMenuDispatcher::disposing( const EventObject& ) throw( RuntimeException, std::exception )
280 : {
281 0 : SolarMutexGuard g;
282 : // Safe impossible cases
283 : SAL_WARN_IF( m_bAlreadyDisposed, "fwk", "MenuDispatcher::disposing(): Object already disposed .. don't call it again!" );
284 :
285 0 : if( m_bAlreadyDisposed == false )
286 : {
287 0 : m_bAlreadyDisposed = true;
288 :
289 0 : if ( m_bActivateListener )
290 : {
291 0 : uno::Reference< XFrame > xFrame( m_xWeakFrame.get(), UNO_QUERY );
292 0 : if ( xFrame.is() )
293 : {
294 0 : xFrame->removeFrameActionListener( uno::Reference< XFrameActionListener >( (OWeakObject *)this, UNO_QUERY ));
295 0 : m_bActivateListener = false;
296 0 : }
297 : }
298 :
299 : // Forget our factory.
300 0 : m_xContext = uno::Reference< XComponentContext >();
301 0 : }
302 0 : }
303 :
304 0 : void PopupMenuDispatcher::impl_RetrievePopupControllerQuery()
305 : {
306 0 : if ( !m_xPopupCtrlQuery.is() )
307 : {
308 0 : css::uno::Reference< css::frame::XLayoutManager2 > xLayoutManager;
309 0 : css::uno::Reference< css::frame::XFrame > xFrame( m_xWeakFrame );
310 :
311 0 : if ( xFrame.is() )
312 : {
313 0 : css::uno::Reference< css::beans::XPropertySet > xPropSet( xFrame, css::uno::UNO_QUERY );
314 0 : if ( xPropSet.is() )
315 : {
316 : try
317 : {
318 0 : xPropSet->getPropertyValue( FRAME_PROPNAME_LAYOUTMANAGER ) >>= xLayoutManager;
319 :
320 0 : if ( xLayoutManager.is() )
321 : {
322 0 : css::uno::Reference< css::ui::XUIElement > xMenuBar;
323 0 : OUString aMenuBar( "private:resource/menubar/menubar" );
324 0 : xMenuBar = xLayoutManager->getElement( aMenuBar );
325 :
326 0 : m_xPopupCtrlQuery = css::uno::Reference< css::container::XNameAccess >(
327 0 : xMenuBar, css::uno::UNO_QUERY );
328 : }
329 : }
330 0 : catch ( const css::uno::RuntimeException& )
331 : {
332 0 : throw;
333 : }
334 0 : catch ( const css::uno::Exception& )
335 : {
336 : }
337 0 : }
338 0 : }
339 : }
340 0 : }
341 :
342 0 : void PopupMenuDispatcher::impl_CreateUriRefFactory()
343 : {
344 0 : if ( !m_xUriRefFactory.is() )
345 : {
346 0 : m_xUriRefFactory = css::uri::UriReferenceFactory::create( m_xContext );
347 : }
348 0 : }
349 :
350 198 : } // namespace framework
351 :
352 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|