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