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>
24 #include <com/sun/star/system/SystemShellExecute.hpp>
25 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
26 #include <com/sun/star/frame/DispatchResultState.hpp>
28 #include <vcl/svapp.hxx>
32 // XInterface, XTypeProvider, XServiceInfo
34 DEFINE_XSERVICEINFO_MULTISERVICE_2(MailToDispatcher
,
36 SERVICENAME_PROTOCOLHANDLER
,
37 IMPLEMENTATIONNAME_MAILTODISPATCHER
)
39 DEFINE_INIT_SERVICE(MailToDispatcher
,
42 I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
43 to create a new instance of this class by our own supported service factory.
44 see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further information!
51 @descr This initializes a new instance of ths class with needed information for work.
54 reference to uno servicemanager for creation of new services
56 MailToDispatcher::MailToDispatcher( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
)
57 : m_xContext ( rxContext
)
64 MailToDispatcher::~MailToDispatcher()
69 @short decide if this dispatch implementation can be used for requested URL or not
70 @descr A protocol handler is registered for an URL pattern inside configuration and will
71 be asked by the generic dispatch mechanism inside framework, if he can handle this
72 special URL which match his registration. He can agree by returning of a valid dispatch
73 instance or disagree by returning <NULL/>.
74 We don't create new dispatch instances here really - we return THIS as result to handle it
75 at the same implementation.
77 css::uno::Reference
< css::frame::XDispatch
> SAL_CALL
MailToDispatcher::queryDispatch( const css::util::URL
& aURL
,
78 const OUString
& /*sTarget*/ ,
79 sal_Int32
/*nFlags*/ ) throw( css::uno::RuntimeException
, std::exception
)
81 css::uno::Reference
< css::frame::XDispatch
> xDispatcher
;
82 if (aURL
.Complete
.startsWith("mailto:"))
88 @short do the same like dispatch() but for multiple requests at the same time
90 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > SAL_CALL
MailToDispatcher::queryDispatches( const css::uno::Sequence
< css::frame::DispatchDescriptor
>& lDescriptor
) throw( css::uno::RuntimeException
, std::exception
)
92 sal_Int32 nCount
= lDescriptor
.getLength();
93 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > lDispatcher( nCount
);
94 for( sal_Int32 i
=0; i
<nCount
; ++i
)
96 lDispatcher
[i
] = this->queryDispatch(
97 lDescriptor
[i
].FeatureURL
,
98 lDescriptor
[i
].FrameName
,
99 lDescriptor
[i
].SearchFlags
);
105 @short dispatch URL with arguments
106 @descr We use threadsafe internal method to do so. It returns a state value - but we ignore it.
107 Because we don't support status listener notifications here. Status events are not guaranteed -
108 and we call another service internally which doesn't return any notifications too.
111 mail URL which should be executed
113 list of optional arguments for this mail request
115 void SAL_CALL
MailToDispatcher::dispatch( const css::util::URL
& aURL
,
116 const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
) throw( css::uno::RuntimeException
, std::exception
)
118 // dispatch() is an [oneway] call ... and may our user release his reference to us immediately.
119 // So we should hold us self alive till this call ends.
120 css::uno::Reference
< css::frame::XNotifyingDispatch
> xSelfHold(static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY
);
121 implts_dispatch(aURL
,lArguments
);
122 // No notification for status listener!
126 @short dispatch with guaranteed notifications about success
127 @descr We use threadsafe internal method to do so. Return state of this function will be used
128 for notification if an optional listener is given.
131 mail URL which should be executed
133 list of optional arguments for this mail request
135 reference to a valid listener for state events
137 void SAL_CALL
MailToDispatcher::dispatchWithNotification( const css::util::URL
& aURL
,
138 const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
,
139 const css::uno::Reference
< css::frame::XDispatchResultListener
>& xListener
) throw( css::uno::RuntimeException
, std::exception
)
141 // This class was designed to die by reference. And if user release his reference to us immediately after calling this method
142 // we can run into some problems. So we hold us self alive till this method ends.
143 // Another reason: We can use this reference as source of sending event at the end too.
144 css::uno::Reference
< css::frame::XNotifyingDispatch
> xThis(static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY
);
146 bool bState
= implts_dispatch(aURL
,lArguments
);
149 css::frame::DispatchResultEvent aEvent
;
151 aEvent
.State
= css::frame::DispatchResultState::SUCCESS
;
153 aEvent
.State
= css::frame::DispatchResultState::FAILURE
;
154 aEvent
.Source
= xThis
;
156 xListener
->dispatchFinished( aEvent
);
161 @short threadsafe helper for dispatch calls
162 @descr We support two interfaces for the same process - dispatch URLs. That the reason for this internal
163 function. It implements the real dispatch operation and returns a state value which inform caller
164 about success. He can notify listener then by using this return value.
167 mail URL which should be executed
169 list of optional arguments for this mail request
171 @return <TRUE/> if dispatch could be started successfully
172 Note: Our internal used shell executor doesn't return any state value - so we must
173 believe that call was successfully.
174 <FALSE/> if necessary resource couldn't be created or an exception was thrown.
176 bool MailToDispatcher::implts_dispatch( const css::util::URL
& aURL
,
177 const css::uno::Sequence
< css::beans::PropertyValue
>& /*lArguments*/ ) throw( css::uno::RuntimeException
)
179 bool bSuccess
= false;
181 css::uno::Reference
< css::system::XSystemShellExecute
> xSystemShellExecute
= css::system::SystemShellExecute::create( m_xContext
);
186 // Because there is no notofocation 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*/ ) throw( css::uno::RuntimeException
, std::exception
)
218 void SAL_CALL
MailToDispatcher::removeStatusListener( const css::uno::Reference
< css::frame::XStatusListener
>& /*xListener*/ ,
219 const css::util::URL
& /*aURL*/ ) throw( css::uno::RuntimeException
, std::exception
)
224 } // namespace framework
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */