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 0 : DEFINE_XSERVICEINFO_MULTISERVICE(DispatchHelper ,
37 : ::cppu::OWeakObject ,
38 : "com.sun.star.frame.DispatchHelper",
39 : IMPLEMENTATIONNAME_DISPATCHHELPER)
40 :
41 0 : 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 0 : DispatchHelper::DispatchHelper( const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR )
50 : : ThreadHelpBase( )
51 : // Init member
52 0 : , m_xSMGR (xSMGR)
53 : {
54 0 : }
55 :
56 : //_______________________________________________
57 :
58 : /** dtor.
59 : */
60 0 : DispatchHelper::~DispatchHelper()
61 : {
62 0 : }
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 0 : css::uno::Any SAL_CALL DispatchHelper::executeDispatch(
86 : const css::uno::Reference< css::frame::XDispatchProvider >& xDispatchProvider ,
87 : const ::rtl::OUString& sURL ,
88 : const ::rtl::OUString& sTargetFrameName ,
89 : sal_Int32 nSearchFlags ,
90 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments )
91 : throw(css::uno::RuntimeException)
92 : {
93 0 : css::uno::Reference< css::uno::XInterface > xTHIS(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
94 :
95 : // check for valid parameters
96 0 : if (
97 0 : (!xDispatchProvider.is()) ||
98 0 : (sURL.isEmpty() )
99 : )
100 : {
101 0 : return css::uno::Any();
102 : }
103 :
104 : // parse given URL
105 : /* SAFE { */
106 0 : ReadGuard aReadLock(m_aLock);
107 0 : css::uno::Reference< css::util::XURLTransformer > xParser(css::util::URLTransformer::create(::comphelper::getComponentContext(m_xSMGR)) );
108 0 : aReadLock.unlock();
109 : /* } SAFE */
110 :
111 0 : css::util::URL aURL;
112 0 : aURL.Complete = sURL;
113 0 : xParser->parseStrict(aURL);
114 :
115 : // search dispatcher
116 0 : css::uno::Reference< css::frame::XDispatch > xDispatch = xDispatchProvider->queryDispatch(aURL, sTargetFrameName, nSearchFlags);
117 0 : css::uno::Reference< css::frame::XNotifyingDispatch > xNotifyDispatch (xDispatch, css::uno::UNO_QUERY);
118 :
119 : // make sure that synchronous execution is used (if possible)
120 0 : css::uno::Sequence< css::beans::PropertyValue > aArguments( lArguments );
121 0 : sal_Int32 nLength = lArguments.getLength();
122 0 : aArguments.realloc( nLength + 1 );
123 0 : aArguments[ nLength ].Name = ::rtl::OUString("SynchronMode");
124 0 : aArguments[ nLength ].Value <<= (sal_Bool) sal_True;
125 :
126 0 : css::uno::Any aResult;
127 0 : if (xNotifyDispatch.is())
128 : {
129 : // dispatch it with guaranteed notification
130 : // Here we can hope for a result ... instead of the normal dispatch.
131 0 : css::uno::Reference< css::frame::XDispatchResultListener > xListener(xTHIS, css::uno::UNO_QUERY);
132 : /* SAFE { */
133 0 : WriteGuard aWriteLock(m_aLock);
134 0 : m_xBroadcaster = css::uno::Reference< css::uno::XInterface >(xNotifyDispatch, css::uno::UNO_QUERY);
135 0 : m_aResult = css::uno::Any();
136 0 : m_aBlock.reset();
137 0 : aWriteLock.unlock();
138 : /* } SAFE */
139 :
140 : // dispatch it and wait for a notification
141 : // TODO/MBA: waiting in main thread?!
142 0 : xNotifyDispatch->dispatchWithNotification(aURL, aArguments, xListener);
143 0 : aResult = m_aResult;
144 : }
145 : else
146 0 : if (xDispatch.is())
147 : {
148 : // dispatch it without any chance to get a result
149 0 : xDispatch->dispatch( aURL, aArguments );
150 : }
151 :
152 0 : return aResult;
153 : }
154 :
155 : //_______________________________________________
156 :
157 : /** callback for started dispatch with guaranteed notifications.
158 :
159 : We must save the result, so the method executeDispatch() can return it.
160 : Further we must release the broadcaster (otherwhise it can't die)
161 : and unblock the waiting executeDispatch() request.
162 :
163 : @param aResult
164 : describes the result of the dispatch operation
165 : */
166 0 : void SAL_CALL DispatchHelper::dispatchFinished( const css::frame::DispatchResultEvent& aResult )
167 : throw(css::uno::RuntimeException)
168 : {
169 : /* SAFE { */
170 0 : WriteGuard aWriteLock(m_aLock);
171 :
172 0 : m_aResult <<= aResult;
173 0 : m_aBlock.set();
174 0 : m_xBroadcaster.clear();
175 :
176 : /* } SAFE */
177 0 : }
178 :
179 : //_______________________________________________
180 :
181 : /** we has to realease our broadcaster reference.
182 :
183 : @param aEvent
184 : describe the source of this event and MUST be our save broadcaster!
185 : */
186 0 : void SAL_CALL DispatchHelper::disposing( const css::lang::EventObject& )
187 : throw(css::uno::RuntimeException)
188 : {
189 : /* SAFE { */
190 0 : WriteGuard aWriteLock(m_aLock);
191 :
192 0 : m_aResult.clear();
193 0 : m_aBlock.set();
194 0 : m_xBroadcaster.clear();
195 :
196 : /* } SAFE */
197 0 : }
198 :
199 : }
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|