1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dispatchhelper.cxx,v $
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 //_______________________________________________
36 #include <services/dispatchhelper.hxx>
37 #include <threadhelp/readguard.hxx>
38 #include <threadhelp/writeguard.hxx>
41 //_______________________________________________
43 #include <com/sun/star/util/XURLTransformer.hpp>
44 #include <com/sun/star/frame/XNotifyingDispatch.hpp>
46 //_______________________________________________
47 // includes of other projects
49 //_______________________________________________
54 //_______________________________________________
57 //_______________________________________________
58 // non exported definitions
60 //_______________________________________________
63 //_______________________________________________
64 // XInterface, XTypeProvider, XServiceInfo
66 DEFINE_XSERVICEINFO_MULTISERVICE(DispatchHelper
,
68 SERVICENAME_DISPATCHHELPER
,
69 IMPLEMENTATIONNAME_DISPATCHHELPER
)
71 DEFINE_INIT_SERVICE( DispatchHelper
, {} )
73 //_______________________________________________
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
)
86 //_______________________________________________
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.
102 describes the requested feature.
104 @param sTargetFrameName
105 points to the frame, which must be used (or may be created) for this dispatch.
108 in case the <var>sTargetFrameName</var> isn't unique, these flags regulate further searches.
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
127 (!xDispatchProvider
.is()) ||
128 (sURL
.getLength()<1 )
131 return css::uno::Any();
136 ReadGuard
aReadLock(m_aLock
);
137 css::uno::Reference
< css::util::XURLTransformer
> xParser(m_xSMGR
->createInstance(SERVICENAME_URLTRANSFORMER
), css::uno::UNO_QUERY
);
142 aURL
.Complete
= sURL
;
143 xParser
->parseStrict(aURL
);
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
);
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();
170 // dispatch it and wait for a notification
171 // TODO/MBA: waiting in main thread?!
172 xNotifyDispatch
->dispatchWithNotification(aURL
, aArguments
, xListener
);
179 // dispatch it without any chance to get a result
180 xDispatch
->dispatch( aURL
, aArguments
);
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.
195 describes the result of the dispatch operation
197 void SAL_CALL
DispatchHelper::dispatchFinished( const css::frame::DispatchResultEvent
& aResult
)
198 throw(css::uno::RuntimeException
)
201 WriteGuard
aWriteLock(m_aLock
);
203 m_aResult
<<= aResult
;
205 m_xBroadcaster
.clear();
210 //_______________________________________________
212 /** we has to realease our broadcaster reference.
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
)
221 WriteGuard
aWriteLock(m_aLock
);
225 m_xBroadcaster
.clear();