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