1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <dispatch/mailtodispatcher.hxx>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <com/sun/star/system/SystemShellExecute.hpp>
25 #include <com/sun/star/system/SystemShellExecuteException.hpp>
26 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
27 #include <com/sun/star/frame/DispatchResultState.hpp>
28 #include <cppuhelper/supportsservice.hxx>
33 // XInterface, XTypeProvider, XServiceInfo
35 OUString SAL_CALL
MailToDispatcher::getImplementationName()
37 return "com.sun.star.comp.framework.MailToDispatcher";
40 sal_Bool SAL_CALL
MailToDispatcher::supportsService( const OUString
& sServiceName
)
42 return cppu::supportsService(this, sServiceName
);
45 css::uno::Sequence
< OUString
> SAL_CALL
MailToDispatcher::getSupportedServiceNames()
47 return { SERVICENAME_PROTOCOLHANDLER
};
53 @descr This initializes a new instance of this class with needed information for work.
56 reference to uno servicemanager for creation of new services
58 MailToDispatcher::MailToDispatcher( css::uno::Reference
< css::uno::XComponentContext
> xContext
)
59 : m_xContext (std::move( xContext
))
66 MailToDispatcher::~MailToDispatcher()
71 @short decide if this dispatch implementation can be used for requested URL or not
72 @descr A protocol handler is registered for a URL pattern inside configuration and will
73 be asked by the generic dispatch mechanism inside framework, if he can handle this
74 special URL which match his registration. He can agree by returning of a valid dispatch
75 instance or disagree by returning <NULL/>.
76 We don't create new dispatch instances here really - we return THIS as result to handle it
77 at the same implementation.
79 css::uno::Reference
< css::frame::XDispatch
> SAL_CALL
MailToDispatcher::queryDispatch( const css::util::URL
& aURL
,
80 const OUString
& /*sTarget*/ ,
81 sal_Int32
/*nFlags*/ )
83 css::uno::Reference
< css::frame::XDispatch
> xDispatcher
;
84 if (aURL
.Complete
.startsWith("mailto:"))
90 @short do the same like dispatch() but for multiple requests at the same time
92 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > SAL_CALL
MailToDispatcher::queryDispatches( const css::uno::Sequence
< css::frame::DispatchDescriptor
>& lDescriptor
)
94 sal_Int32 nCount
= lDescriptor
.getLength();
95 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > lDispatcher( nCount
);
96 auto lDispatcherRange
= asNonConstRange(lDispatcher
);
97 for( sal_Int32 i
=0; i
<nCount
; ++i
)
99 lDispatcherRange
[i
] = queryDispatch(
100 lDescriptor
[i
].FeatureURL
,
101 lDescriptor
[i
].FrameName
,
102 lDescriptor
[i
].SearchFlags
);
108 @short dispatch URL with arguments
109 @descr We use threadsafe internal method to do so. It returns a state value - but we ignore it.
110 Because we don't support status listener notifications here. Status events are not guaranteed -
111 and we call another service internally which doesn't return any notifications too.
114 mail URL which should be executed
116 list of optional arguments for this mail request
118 void SAL_CALL
MailToDispatcher::dispatch( const css::util::URL
& aURL
,
119 const css::uno::Sequence
< css::beans::PropertyValue
>& /*lArguments*/ )
121 // dispatch() is an [oneway] call ... and may our user release his reference to us immediately.
122 // So we should hold us self alive till this call ends.
123 css::uno::Reference
< css::frame::XNotifyingDispatch
> xSelfHold(this);
124 implts_dispatch(aURL
);
125 // No notification for status listener!
129 @short dispatch with guaranteed notifications about success
130 @descr We use threadsafe internal method to do so. Return state of this function will be used
131 for notification if an optional listener is given.
134 mail URL which should be executed
136 list of optional arguments for this mail request
138 reference to a valid listener for state events
140 void SAL_CALL
MailToDispatcher::dispatchWithNotification( const css::util::URL
& aURL
,
141 const css::uno::Sequence
< css::beans::PropertyValue
>& /*lArguments*/,
142 const css::uno::Reference
< css::frame::XDispatchResultListener
>& xListener
)
144 // This class was designed to die by reference. And if user release his reference to us immediately after calling this method
145 // we can run into some problems. So we hold us self alive till this method ends.
146 // Another reason: We can use this reference as source of sending event at the end too.
147 css::uno::Reference
< css::frame::XNotifyingDispatch
> xThis(this);
149 bool bState
= implts_dispatch(aURL
);
152 css::frame::DispatchResultEvent aEvent
;
154 aEvent
.State
= css::frame::DispatchResultState::SUCCESS
;
156 aEvent
.State
= css::frame::DispatchResultState::FAILURE
;
157 aEvent
.Source
= xThis
;
159 xListener
->dispatchFinished( aEvent
);
164 @short threadsafe helper for dispatch calls
165 @descr We support two interfaces for the same process - dispatch URLs. That the reason for this internal
166 function. It implements the real dispatch operation and returns a state value which inform caller
167 about success. He can notify listener then by using this return value.
170 mail URL which should be executed
172 @return <TRUE/> if dispatch could be started successfully
173 Note: Our internal used shell executor doesn't return any state value - so we must
174 believe that call was successful.
175 <FALSE/> if necessary resource couldn't be created or an exception was thrown.
177 bool MailToDispatcher::implts_dispatch( const css::util::URL
& aURL
)
179 bool bSuccess
= false;
181 css::uno::Reference
< css::system::XSystemShellExecute
> xSystemShellExecute
= css::system::SystemShellExecute::create( m_xContext
);
186 // Because there is no notification about success - we use case of
187 // no detected exception as SUCCESS - FAILED otherwise.
188 xSystemShellExecute
->execute( aURL
.Complete
, OUString(), css::system::SystemShellExecuteFlags::URIS_ONLY
);
191 catch (const css::lang::IllegalArgumentException
&)
194 catch (const css::system::SystemShellExecuteException
&)
202 @short add/remove listener for state events
203 @descr Because we use an external process to forward such mail URLs, and this process doesn't
204 return any notifications about success or failed state - we don't support such status
205 listener. We have no status to send.
208 reference to a valid listener for state events
210 URL about listener will be informed, if something occurred
212 void SAL_CALL
MailToDispatcher::addStatusListener( const css::uno::Reference
< css::frame::XStatusListener
>& /*xListener*/ ,
213 const css::util::URL
& /*aURL*/ )
218 void SAL_CALL
MailToDispatcher::removeStatusListener( const css::uno::Reference
< css::frame::XStatusListener
>& /*xListener*/ ,
219 const css::util::URL
& /*aURL*/ )
224 } // namespace framework
226 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
227 framework_MailToDispatcher_get_implementation(
228 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& )
230 return cppu::acquire(new framework::MailToDispatcher(context
));
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */