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 <dispatch/mailtodispatcher.hxx>
21 #include <general.h>
22 #include <services.h>
23 
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <com/sun/star/system/SystemShellExecute.hpp>
26 #include <com/sun/star/system/SystemShellExecuteException.hpp>
27 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
28 #include <com/sun/star/frame/DispatchResultState.hpp>
29 
30 #include <vcl/svapp.hxx>
31 
32 namespace framework{
33 
34 // XInterface, XTypeProvider, XServiceInfo
35 
DEFINE_XSERVICEINFO_MULTISERVICE_2(MailToDispatcher,::cppu::OWeakObject,SERVICENAME_PROTOCOLHANDLER,IMPLEMENTATIONNAME_MAILTODISPATCHER)36 DEFINE_XSERVICEINFO_MULTISERVICE_2(MailToDispatcher                   ,
37                                  ::cppu::OWeakObject                ,
38                                  SERVICENAME_PROTOCOLHANDLER        ,
39                                  IMPLEMENTATIONNAME_MAILTODISPATCHER)
40 
41 DEFINE_INIT_SERVICE(MailToDispatcher,
42                     {
43                         /*Attention
44                             I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
45                             to create a new instance of this class by our own supported service factory.
46                             see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further information!
47                         */
48                     }
49                    )
50 
51 /**
52     @short      standard ctor
53     @descr      This initializes a new instance of this class with needed information for work.
54 
55     @param      rxContext
56                     reference to uno servicemanager for creation of new services
57 */
58 MailToDispatcher::MailToDispatcher( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
59         : m_xContext    ( rxContext                     )
60 {
61 }
62 
63 /**
64     @short      standard dtor
65 */
~MailToDispatcher()66 MailToDispatcher::~MailToDispatcher()
67 {
68 }
69 
70 /**
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.
78 */
queryDispatch(const css::util::URL & aURL,const OUString &,sal_Int32)79 css::uno::Reference< css::frame::XDispatch > SAL_CALL MailToDispatcher::queryDispatch( const css::util::URL&  aURL    ,
80                                                                                        const OUString& /*sTarget*/ ,
81                                                                                              sal_Int32        /*nFlags*/  )
82 {
83     css::uno::Reference< css::frame::XDispatch > xDispatcher;
84     if (aURL.Complete.startsWith("mailto:"))
85         xDispatcher = this;
86     return xDispatcher;
87 }
88 
89 /**
90     @short      do the same like dispatch() but for multiple requests at the same time
91 */
queryDispatches(const css::uno::Sequence<css::frame::DispatchDescriptor> & lDescriptor)92 css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL MailToDispatcher::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor )
93 {
94     sal_Int32 nCount = lDescriptor.getLength();
95     css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatcher( nCount );
96     for( sal_Int32 i=0; i<nCount; ++i )
97     {
98         lDispatcher[i] = queryDispatch(
99                             lDescriptor[i].FeatureURL,
100                             lDescriptor[i].FrameName,
101                             lDescriptor[i].SearchFlags);
102     }
103     return lDispatcher;
104 }
105 
106 /**
107     @short      dispatch URL with arguments
108     @descr      We use threadsafe internal method to do so. It returns a state value - but we ignore it.
109                 Because we don't support status listener notifications here. Status events are not guaranteed -
110                 and we call another service internally which doesn't return any notifications too.
111 
112     @param      aURL
113                     mail URL which should be executed
114     @param      lArguments
115                     list of optional arguments for this mail request
116 */
dispatch(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> &)117 void SAL_CALL MailToDispatcher::dispatch( const css::util::URL&                                  aURL       ,
118                                           const css::uno::Sequence< css::beans::PropertyValue >& /*lArguments*/ )
119 {
120     // dispatch() is an [oneway] call ... and may our user release his reference to us immediately.
121     // So we should hold us self alive till this call ends.
122     css::uno::Reference< css::frame::XNotifyingDispatch > xSelfHold(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
123     implts_dispatch(aURL);
124     // No notification for status listener!
125 }
126 
127 /**
128     @short      dispatch with guaranteed notifications about success
129     @descr      We use threadsafe internal method to do so. Return state of this function will be used
130                 for notification if an optional listener is given.
131 
132     @param      aURL
133                     mail URL which should be executed
134     @param      lArguments
135                     list of optional arguments for this mail request
136     @param      xListener
137                     reference to a valid listener for state events
138 */
dispatchWithNotification(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> &,const css::uno::Reference<css::frame::XDispatchResultListener> & xListener)139 void SAL_CALL MailToDispatcher::dispatchWithNotification( const css::util::URL&                                             aURL      ,
140                                                           const css::uno::Sequence< css::beans::PropertyValue >&            /*lArguments*/,
141                                                           const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
142 {
143     // This class was designed to die by reference. And if user release his reference to us immediately after calling this method
144     // we can run into some problems. So we hold us self alive till this method ends.
145     // Another reason: We can use this reference as source of sending event at the end too.
146     css::uno::Reference< css::frame::XNotifyingDispatch > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
147 
148     bool bState = implts_dispatch(aURL);
149     if (xListener.is())
150     {
151         css::frame::DispatchResultEvent aEvent;
152         if (bState)
153             aEvent.State = css::frame::DispatchResultState::SUCCESS;
154         else
155             aEvent.State = css::frame::DispatchResultState::FAILURE;
156         aEvent.Source = xThis;
157 
158         xListener->dispatchFinished( aEvent );
159     }
160 }
161 
162 /**
163     @short      threadsafe helper for dispatch calls
164     @descr      We support two interfaces for the same process - dispatch URLs. That the reason for this internal
165                 function. It implements the real dispatch operation and returns a state value which inform caller
166                 about success. He can notify listener then by using this return value.
167 
168     @param      aURL
169                     mail URL which should be executed
170 
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 successful.
174                 <FALSE/> if necessary resource couldn't be created or an exception was thrown.
175 */
implts_dispatch(const css::util::URL & aURL)176 bool MailToDispatcher::implts_dispatch( const css::util::URL& aURL )
177 {
178     bool bSuccess = false;
179 
180     css::uno::Reference< css::system::XSystemShellExecute > xSystemShellExecute = css::system::SystemShellExecute::create( m_xContext );
181 
182     try
183     {
184         // start mail client
185         // Because there is no notification about success - we use case of
186         // no detected exception as SUCCESS - FAILED otherwise.
187         xSystemShellExecute->execute( aURL.Complete, OUString(), css::system::SystemShellExecuteFlags::URIS_ONLY );
188         bSuccess = true;
189     }
190     catch (const css::lang::IllegalArgumentException&)
191     {
192     }
193     catch (const css::system::SystemShellExecuteException&)
194     {
195     }
196 
197     return bSuccess;
198 }
199 
200 /**
201     @short      add/remove listener for state events
202     @descr      Because we use an external process to forward such mail URLs, and this process doesn't
203                 return any notifications about success or failed state - we don't support such status
204                 listener. We have no status to send.
205 
206     @param      xListener
207                     reference to a valid listener for state events
208     @param      aURL
209                     URL about listener will be informed, if something occurred
210 */
addStatusListener(const css::uno::Reference<css::frame::XStatusListener> &,const css::util::URL &)211 void SAL_CALL MailToDispatcher::addStatusListener( const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/ ,
212                                                    const css::util::URL&                                     /*aURL*/      )
213 {
214     // not supported yet
215 }
216 
removeStatusListener(const css::uno::Reference<css::frame::XStatusListener> &,const css::util::URL &)217 void SAL_CALL MailToDispatcher::removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/ ,
218                                                       const css::util::URL&                                     /*aURL*/      )
219 {
220     // not supported yet
221 }
222 
223 } //  namespace framework
224 
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
226