xref: /AOO41X/main/framework/source/services/dispatchhelper.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_framework.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //_______________________________________________
32*cdf0e10cSrcweir // my own includes
33*cdf0e10cSrcweir #include <services/dispatchhelper.hxx>
34*cdf0e10cSrcweir #include <threadhelp/readguard.hxx>
35*cdf0e10cSrcweir #include <threadhelp/writeguard.hxx>
36*cdf0e10cSrcweir #include <services.h>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir //_______________________________________________
39*cdf0e10cSrcweir // interface includes
40*cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/frame/XNotifyingDispatch.hpp>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir //_______________________________________________
44*cdf0e10cSrcweir // includes of other projects
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir //_______________________________________________
47*cdf0e10cSrcweir // namespace
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace framework{
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //_______________________________________________
52*cdf0e10cSrcweir // non exported const
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir //_______________________________________________
55*cdf0e10cSrcweir // non exported definitions
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir //_______________________________________________
58*cdf0e10cSrcweir // declarations
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir //_______________________________________________
61*cdf0e10cSrcweir // XInterface, XTypeProvider, XServiceInfo
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE(DispatchHelper                   ,
64*cdf0e10cSrcweir                                  ::cppu::OWeakObject              ,
65*cdf0e10cSrcweir                                  SERVICENAME_DISPATCHHELPER       ,
66*cdf0e10cSrcweir                                  IMPLEMENTATIONNAME_DISPATCHHELPER)
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir DEFINE_INIT_SERVICE( DispatchHelper, {} )
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir //_______________________________________________
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir /** ctor.
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     @param xSMGR    the global uno service manager, which can be used to create own needed services.
75*cdf0e10cSrcweir */
76*cdf0e10cSrcweir DispatchHelper::DispatchHelper( const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR )
77*cdf0e10cSrcweir         :   ThreadHelpBase(     )
78*cdf0e10cSrcweir 		// Init member
79*cdf0e10cSrcweir         ,   m_xSMGR       (xSMGR)
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir //_______________________________________________
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir /** dtor.
86*cdf0e10cSrcweir */
87*cdf0e10cSrcweir DispatchHelper::~DispatchHelper()
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir //_______________________________________________
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir /** capsulate all steps of a dispatch request and provide so an easy way for dispatches.
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     @param xDispatchProvider
96*cdf0e10cSrcweir                 identifies the object, which provides may be valid dispatch objects for this execute.
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     @param sURL
99*cdf0e10cSrcweir                 describes the requested feature.
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     @param sTargetFrameName
102*cdf0e10cSrcweir                 points to the frame, which must be used (or may be created) for this dispatch.
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     @param nSearchFlags
105*cdf0e10cSrcweir                 in case the <var>sTargetFrameName</var> isn't unique, these flags regulate further searches.
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     @param lArguments
108*cdf0e10cSrcweir                 optional arguments for this request.
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     @return An Any which capsulate a possible result of the internal wrapped dispatch.
111*cdf0e10cSrcweir  */
112*cdf0e10cSrcweir css::uno::Any SAL_CALL DispatchHelper::executeDispatch(
113*cdf0e10cSrcweir                                 const css::uno::Reference< css::frame::XDispatchProvider >& xDispatchProvider ,
114*cdf0e10cSrcweir                                 const ::rtl::OUString&                                      sURL              ,
115*cdf0e10cSrcweir                                 const ::rtl::OUString&                                      sTargetFrameName  ,
116*cdf0e10cSrcweir                                       sal_Int32                                             nSearchFlags      ,
117*cdf0e10cSrcweir                                 const css::uno::Sequence< css::beans::PropertyValue >&      lArguments        )
118*cdf0e10cSrcweir     throw(css::uno::RuntimeException)
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > xTHIS(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     // check for valid parameters
123*cdf0e10cSrcweir     if (
124*cdf0e10cSrcweir         (!xDispatchProvider.is()) ||
125*cdf0e10cSrcweir         (sURL.getLength()<1     )
126*cdf0e10cSrcweir        )
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir 		return css::uno::Any();
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     // parse given URL
132*cdf0e10cSrcweir     /* SAFE { */
133*cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
134*cdf0e10cSrcweir     css::uno::Reference< css::util::XURLTransformer > xParser(m_xSMGR->createInstance(SERVICENAME_URLTRANSFORMER), css::uno::UNO_QUERY);
135*cdf0e10cSrcweir     aReadLock.unlock();
136*cdf0e10cSrcweir     /* } SAFE */
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     css::util::URL aURL;
139*cdf0e10cSrcweir     aURL.Complete = sURL;
140*cdf0e10cSrcweir     xParser->parseStrict(aURL);
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     // search dispatcher
143*cdf0e10cSrcweir     css::uno::Reference< css::frame::XDispatch >          xDispatch       = xDispatchProvider->queryDispatch(aURL, sTargetFrameName, nSearchFlags);
144*cdf0e10cSrcweir     css::uno::Reference< css::frame::XNotifyingDispatch > xNotifyDispatch (xDispatch, css::uno::UNO_QUERY);
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     // make sure that synchronous execution is used (if possible)
147*cdf0e10cSrcweir     css::uno::Sequence< css::beans::PropertyValue > aArguments( lArguments );
148*cdf0e10cSrcweir     sal_Int32 nLength = lArguments.getLength();
149*cdf0e10cSrcweir     aArguments.realloc( nLength + 1 );
150*cdf0e10cSrcweir     aArguments[ nLength ].Name = ::rtl::OUString::createFromAscii("SynchronMode");
151*cdf0e10cSrcweir     aArguments[ nLength ].Value <<= (sal_Bool) sal_True;
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     css::uno::Any aResult;
154*cdf0e10cSrcweir     if (xNotifyDispatch.is())
155*cdf0e10cSrcweir     {
156*cdf0e10cSrcweir         // dispatch it with guaranteed notification
157*cdf0e10cSrcweir         // Here we can hope for a result ... instead of the normal dispatch.
158*cdf0e10cSrcweir         css::uno::Reference< css::frame::XDispatchResultListener > xListener(xTHIS, css::uno::UNO_QUERY);
159*cdf0e10cSrcweir         /* SAFE { */
160*cdf0e10cSrcweir         WriteGuard aWriteLock(m_aLock);
161*cdf0e10cSrcweir         m_xBroadcaster = css::uno::Reference< css::uno::XInterface >(xNotifyDispatch, css::uno::UNO_QUERY);
162*cdf0e10cSrcweir         m_aResult      = css::uno::Any();
163*cdf0e10cSrcweir         m_aBlock.reset();
164*cdf0e10cSrcweir         aWriteLock.unlock();
165*cdf0e10cSrcweir         /* } SAFE */
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir         // dispatch it and wait for a notification
168*cdf0e10cSrcweir         // TODO/MBA: waiting in main thread?!
169*cdf0e10cSrcweir         xNotifyDispatch->dispatchWithNotification(aURL, aArguments, xListener);
170*cdf0e10cSrcweir         //m_aBlock.wait();
171*cdf0e10cSrcweir         aResult = m_aResult;
172*cdf0e10cSrcweir     }
173*cdf0e10cSrcweir     else
174*cdf0e10cSrcweir     if (xDispatch.is())
175*cdf0e10cSrcweir     {
176*cdf0e10cSrcweir         // dispatch it without any chance to get a result
177*cdf0e10cSrcweir         xDispatch->dispatch( aURL, aArguments );
178*cdf0e10cSrcweir     }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     return aResult;
181*cdf0e10cSrcweir }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir //_______________________________________________
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir /** callback for started dispatch with guaranteed notifications.
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     We must save the result, so the method executeDispatch() can return it.
188*cdf0e10cSrcweir     Further we must release the broadcaster (otherwhise it can't die)
189*cdf0e10cSrcweir     and unblock the waiting executeDispatch() request.
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     @param  aResult
192*cdf0e10cSrcweir                 describes the result of the dispatch operation
193*cdf0e10cSrcweir  */
194*cdf0e10cSrcweir void SAL_CALL DispatchHelper::dispatchFinished( const css::frame::DispatchResultEvent& aResult )
195*cdf0e10cSrcweir     throw(css::uno::RuntimeException)
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir     /* SAFE { */
198*cdf0e10cSrcweir     WriteGuard aWriteLock(m_aLock);
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     m_aResult <<= aResult;
201*cdf0e10cSrcweir     m_aBlock.set();
202*cdf0e10cSrcweir     m_xBroadcaster.clear();
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     /* } SAFE */
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir //_______________________________________________
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir /** we has to realease our broadcaster reference.
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir     @param aEvent
212*cdf0e10cSrcweir                 describe the source of this event and MUST be our save broadcaster!
213*cdf0e10cSrcweir  */
214*cdf0e10cSrcweir void SAL_CALL DispatchHelper::disposing( const css::lang::EventObject& )
215*cdf0e10cSrcweir     throw(css::uno::RuntimeException)
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir     /* SAFE { */
218*cdf0e10cSrcweir     WriteGuard aWriteLock(m_aLock);
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     m_aResult.clear();
221*cdf0e10cSrcweir 	m_aBlock.set();
222*cdf0e10cSrcweir     m_xBroadcaster.clear();
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir     /* } SAFE */
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir }
228