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 <memory>
21 #include <sal/config.h>
22 #include <osl/diagnose.h>
23
24 #include <com/sun/star/awt/XWindow.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
27 #include <com/sun/star/lang/XInitialization.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/task/XInteractionHandler2.hpp>
30 #include <com/sun/star/uno/RuntimeException.hpp>
31
32 #include "iahndl.hxx"
33 #include <comphelper/namedvaluecollection.hxx>
34 #include <cppuhelper/exc_hlp.hxx>
35 #include <cppuhelper/implbase.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37
38 using namespace com::sun::star;
39
40 namespace {
41
42 class UUIInteractionHandler:
43 public cppu::WeakImplHelper<css::lang::XServiceInfo,
44 css::lang::XInitialization,
45 css::task::XInteractionHandler2,
46 css::beans::XPropertySet>
47 {
48 private:
49 UUIInteractionHelper m_pImpl;
50
51 public:
52 explicit UUIInteractionHandler(css::uno::Reference< css::uno::XComponentContext > const & rxContext);
53
54 UUIInteractionHandler(const UUIInteractionHandler&) = delete;
55 UUIInteractionHandler& operator=(const UUIInteractionHandler&) = delete;
56
57 virtual OUString SAL_CALL getImplementationName() override;
58
59 virtual sal_Bool SAL_CALL supportsService(OUString const & rServiceName) override;
60
61 virtual css::uno::Sequence< OUString > SAL_CALL
62 getSupportedServiceNames() override;
63
64 virtual void SAL_CALL
65 initialize(
66 css::uno::Sequence< css::uno::Any > const & rArguments) override;
67
68 virtual void SAL_CALL
69 handle(css::uno::Reference< css::task::XInteractionRequest > const & rRequest) override;
70
71 virtual sal_Bool SAL_CALL
72 handleInteractionRequest(
73 const css::uno::Reference< css::task::XInteractionRequest >& Request
74 ) override;
75
76 virtual void SAL_CALL
addPropertyChangeListener(const OUString &,const css::uno::Reference<css::beans::XPropertyChangeListener> &)77 addPropertyChangeListener( const OUString& /*aPropertyName*/, const css::uno::Reference< css::beans::XPropertyChangeListener >& /*xListener*/ ) override
78 {
79 throw css::uno::RuntimeException(
80 u"UUIInteractionHandler addPropertyChangeListener is not supported"_ustr);
81 }
82
83 virtual void SAL_CALL
removePropertyChangeListener(const OUString &,const css::uno::Reference<css::beans::XPropertyChangeListener> &)84 removePropertyChangeListener( const OUString& /*aPropertyName*/, const css::uno::Reference< css::beans::XPropertyChangeListener >& /*xListener*/ ) override
85 {
86 throw css::uno::RuntimeException(
87 u"UUIInteractionHandler removePropertyChangeListener is not supported"_ustr);
88 }
89
90 virtual void SAL_CALL
addVetoableChangeListener(const OUString &,const css::uno::Reference<css::beans::XVetoableChangeListener> &)91 addVetoableChangeListener( const OUString& /*aPropertyName*/, const css::uno::Reference< css::beans::XVetoableChangeListener >& /*xListener*/ ) override
92 {
93 throw css::uno::RuntimeException(
94 u"UUIInteractionHandler addVetoableChangeListener is not supported"_ustr);
95 }
96
97 virtual void SAL_CALL
removeVetoableChangeListener(const OUString &,const css::uno::Reference<css::beans::XVetoableChangeListener> &)98 removeVetoableChangeListener( const OUString& /*aPropertyName*/, const css::uno::Reference< css::beans::XVetoableChangeListener >& /*xListener*/ ) override
99 {
100 throw css::uno::RuntimeException(
101 u"UUIInteractionHandler removeVetoableChangeListener is not supported"_ustr);
102 }
103
104 virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL
getPropertySetInfo()105 getPropertySetInfo() override
106 {
107 return nullptr;
108 }
109
setPropertyValue(const OUString & rPropertyName,const css::uno::Any & rValue)110 virtual void SAL_CALL setPropertyValue(const OUString& rPropertyName, const css::uno::Any& rValue) override
111 {
112 if (rPropertyName == "ParentWindow")
113 {
114 css::uno::Reference<css::awt::XWindow> xWindow;
115 rValue >>= xWindow;
116 m_pImpl.SetParentWindow(xWindow);
117 return;
118 }
119 throw css::beans::UnknownPropertyException(rPropertyName);
120 }
121
getPropertyValue(const OUString & rPropertyName)122 virtual css::uno::Any SAL_CALL getPropertyValue(const OUString& rPropertyName) override
123 {
124 if (rPropertyName == "ParentWindow")
125 {
126 return uno::Any(m_pImpl.GetParentWindow());
127 }
128 throw css::beans::UnknownPropertyException(rPropertyName);
129 }
130 };
131
UUIInteractionHandler(uno::Reference<uno::XComponentContext> const & rxContext)132 UUIInteractionHandler::UUIInteractionHandler(
133 uno::Reference< uno::XComponentContext > const & rxContext)
134 : m_pImpl(rxContext)
135 {
136 }
137
getImplementationName()138 OUString SAL_CALL UUIInteractionHandler::getImplementationName()
139 {
140 return u"com.sun.star.comp.uui.UUIInteractionHandler"_ustr;
141 }
142
143 sal_Bool SAL_CALL
supportsService(OUString const & rServiceName)144 UUIInteractionHandler::supportsService(OUString const & rServiceName)
145 {
146 return cppu::supportsService(this, rServiceName);
147 }
148
149 uno::Sequence< OUString > SAL_CALL
getSupportedServiceNames()150 UUIInteractionHandler::getSupportedServiceNames()
151 {
152 return { u"com.sun.star.task.InteractionHandler"_ustr,
153 // added to indicate support for configuration.backend.MergeRecoveryRequest
154 u"com.sun.star.configuration.backend.InteractionHandler"_ustr,
155 // for backwards compatibility
156 u"com.sun.star.uui.InteractionHandler"_ustr };
157 }
158
159 void SAL_CALL
initialize(uno::Sequence<uno::Any> const & rArguments)160 UUIInteractionHandler::initialize(
161 uno::Sequence< uno::Any > const & rArguments)
162 {
163 // The old-style InteractionHandler service supported a sequence of
164 // PropertyValue, while the new-style service now uses constructors to pass
165 // in Parent and Context values; for backwards compatibility, keep support
166 // for a PropertyValue sequence, too:
167 uno::Reference< awt::XWindow > xWindow;
168 OUString aContext;
169 if (!((rArguments.getLength() == 1 && (rArguments[0] >>= xWindow)) ||
170 (rArguments.getLength() == 2 && (rArguments[0] >>= xWindow) &&
171 (rArguments[1] >>= aContext))))
172 {
173 ::comphelper::NamedValueCollection aProperties( rArguments );
174 if ( aProperties.has( u"Parent"_ustr ) )
175 {
176 OSL_VERIFY( aProperties.get( u"Parent"_ustr ) >>= xWindow );
177 }
178 if ( aProperties.has( u"Context"_ustr ) )
179 {
180 OSL_VERIFY( aProperties.get( u"Context"_ustr ) >>= aContext );
181 }
182 }
183
184 m_pImpl.SetParentWindow(xWindow);
185 m_pImpl.setContext(aContext);
186 }
187
188 void SAL_CALL
handle(uno::Reference<task::XInteractionRequest> const & rRequest)189 UUIInteractionHandler::handle(
190 uno::Reference< task::XInteractionRequest > const & rRequest)
191 {
192 try
193 {
194 m_pImpl.handleRequest(rRequest);
195 }
196 catch (uno::RuntimeException const & ex)
197 {
198 css::uno::Any anyEx = cppu::getCaughtException();
199 throw css::lang::WrappedTargetRuntimeException( ex.Message,
200 *this, anyEx );
201 }
202 }
203
handleInteractionRequest(const uno::Reference<task::XInteractionRequest> & Request)204 sal_Bool SAL_CALL UUIInteractionHandler::handleInteractionRequest(
205 const uno::Reference< task::XInteractionRequest >& Request )
206 {
207 try
208 {
209 return m_pImpl.handleRequest( Request );
210 }
211 catch (uno::RuntimeException const & ex)
212 {
213 css::uno::Any anyEx = cppu::getCaughtException();
214 throw css::lang::WrappedTargetRuntimeException( ex.Message,
215 *this, anyEx );
216 }
217 }
218
219 }
220
221 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_uui_UUIInteractionHandler_get_implementation(css::uno::XComponentContext * context,css::uno::Sequence<css::uno::Any> const &)222 com_sun_star_comp_uui_UUIInteractionHandler_get_implementation(
223 css::uno::XComponentContext *context,
224 css::uno::Sequence<css::uno::Any> const &)
225 {
226 return cppu::acquire(new UUIInteractionHandler(context));
227 }
228
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
230