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 <services/dispatchhelper.hxx>
21 : #include <threadhelp/readguard.hxx>
22 : #include <threadhelp/writeguard.hxx>
23 : #include <services.h>
24 :
25 : #include <com/sun/star/util/URLTransformer.hpp>
26 : #include <com/sun/star/util/XURLTransformer.hpp>
27 : #include <com/sun/star/frame/XNotifyingDispatch.hpp>
28 :
29 : #include <comphelper/processfactory.hxx>
30 :
31 : namespace framework{
32 :
33 : //_______________________________________________
34 : // XInterface, XTypeProvider, XServiceInfo
35 :
36 51 : DEFINE_XSERVICEINFO_MULTISERVICE_2(DispatchHelper ,
37 : ::cppu::OWeakObject ,
38 : "com.sun.star.frame.DispatchHelper",
39 : IMPLEMENTATIONNAME_DISPATCHHELPER)
40 :
41 3 : DEFINE_INIT_SERVICE( DispatchHelper, {} )
42 :
43 : //_______________________________________________
44 :
45 : /** ctor.
46 :
47 : @param xSMGR the global uno service manager, which can be used to create own needed services.
48 : */
49 3 : DispatchHelper::DispatchHelper( const css::uno::Reference< css::uno::XComponentContext >& xContext )
50 : : ThreadHelpBase( )
51 : // Init member
52 3 : , m_xContext (xContext)
53 : {
54 3 : }
55 :
56 : //_______________________________________________
57 :
58 : /** dtor.
59 : */
60 6 : DispatchHelper::~DispatchHelper()
61 : {
62 6 : }
63 :
64 : //_______________________________________________
65 :
66 : /** capsulate all steps of a dispatch request and provide so an easy way for dispatches.
67 :
68 : @param xDispatchProvider
69 : identifies the object, which provides may be valid dispatch objects for this execute.
70 :
71 : @param sURL
72 : describes the requested feature.
73 :
74 : @param sTargetFrameName
75 : points to the frame, which must be used (or may be created) for this dispatch.
76 :
77 : @param nSearchFlags
78 : in case the <var>sTargetFrameName</var> isn't unique, these flags regulate further searches.
79 :
80 : @param lArguments
81 : optional arguments for this request.
82 :
83 : @return An Any which capsulate a possible result of the internal wrapped dispatch.
84 : */
85 5 : css::uno::Any SAL_CALL DispatchHelper::executeDispatch(
86 : const css::uno::Reference< css::frame::XDispatchProvider >& xDispatchProvider ,
87 : const OUString& sURL ,
88 : const OUString& sTargetFrameName ,
89 : sal_Int32 nSearchFlags ,
90 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments )
91 : throw(css::uno::RuntimeException)
92 : {
93 5 : css::uno::Reference< css::uno::XInterface > xTHIS(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
94 :
95 : // check for valid parameters
96 5 : if (
97 10 : (!xDispatchProvider.is()) ||
98 5 : (sURL.isEmpty() )
99 : )
100 : {
101 0 : return css::uno::Any();
102 : }
103 :
104 : // parse given URL
105 : /* SAFE { */
106 10 : ReadGuard aReadLock(m_aLock);
107 10 : css::uno::Reference< css::util::XURLTransformer > xParser = css::util::URLTransformer::create(m_xContext);
108 5 : aReadLock.unlock();
109 : /* } SAFE */
110 :
111 10 : css::util::URL aURL;
112 5 : aURL.Complete = sURL;
113 5 : xParser->parseStrict(aURL);
114 :
115 : // search dispatcher
116 10 : css::uno::Reference< css::frame::XDispatch > xDispatch = xDispatchProvider->queryDispatch(aURL, sTargetFrameName, nSearchFlags);
117 10 : css::uno::Reference< css::frame::XNotifyingDispatch > xNotifyDispatch (xDispatch, css::uno::UNO_QUERY);
118 :
119 : // make sure that synchronous execution is used (if possible)
120 10 : css::uno::Sequence< css::beans::PropertyValue > aArguments( lArguments );
121 5 : sal_Int32 nLength = lArguments.getLength();
122 5 : aArguments.realloc( nLength + 1 );
123 5 : aArguments[ nLength ].Name = OUString("SynchronMode");
124 5 : aArguments[ nLength ].Value <<= (sal_Bool) sal_True;
125 :
126 10 : css::uno::Any aResult;
127 5 : if (xNotifyDispatch.is())
128 : {
129 : // dispatch it with guaranteed notification
130 : // Here we can hope for a result ... instead of the normal dispatch.
131 5 : css::uno::Reference< css::frame::XDispatchResultListener > xListener(xTHIS, css::uno::UNO_QUERY);
132 : /* SAFE { */
133 10 : WriteGuard aWriteLock(m_aLock);
134 5 : m_xBroadcaster = css::uno::Reference< css::uno::XInterface >(xNotifyDispatch, css::uno::UNO_QUERY);
135 5 : m_aResult = css::uno::Any();
136 5 : m_aBlock.reset();
137 5 : aWriteLock.unlock();
138 : /* } SAFE */
139 :
140 : // dispatch it and wait for a notification
141 : // TODO/MBA: waiting in main thread?!
142 5 : xNotifyDispatch->dispatchWithNotification(aURL, aArguments, xListener);
143 10 : aResult = m_aResult;
144 : }
145 0 : else if (xDispatch.is())
146 : {
147 : // dispatch it without any chance to get a result
148 0 : xDispatch->dispatch( aURL, aArguments );
149 : }
150 :
151 10 : return aResult;
152 : }
153 :
154 : //_______________________________________________
155 :
156 : /** callback for started dispatch with guaranteed notifications.
157 :
158 : We must save the result, so the method executeDispatch() can return it.
159 : Further we must release the broadcaster (otherwise it can't die)
160 : and unblock the waiting executeDispatch() request.
161 :
162 : @param aResult
163 : describes the result of the dispatch operation
164 : */
165 5 : void SAL_CALL DispatchHelper::dispatchFinished( const css::frame::DispatchResultEvent& aResult )
166 : throw(css::uno::RuntimeException)
167 : {
168 : /* SAFE { */
169 5 : WriteGuard aWriteLock(m_aLock);
170 :
171 5 : m_aResult <<= aResult;
172 5 : m_aBlock.set();
173 5 : m_xBroadcaster.clear();
174 :
175 : /* } SAFE */
176 5 : }
177 :
178 : //_______________________________________________
179 :
180 : /** we has to realease our broadcaster reference.
181 :
182 : @param aEvent
183 : describe the source of this event and MUST be our save broadcaster!
184 : */
185 0 : void SAL_CALL DispatchHelper::disposing( const css::lang::EventObject& )
186 : throw(css::uno::RuntimeException)
187 : {
188 : /* SAFE { */
189 0 : WriteGuard aWriteLock(m_aLock);
190 :
191 0 : m_aResult.clear();
192 0 : m_aBlock.set();
193 0 : m_xBroadcaster.clear();
194 :
195 : /* } SAFE */
196 0 : }
197 :
198 : }
199 :
200 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|