1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 //_________________________________________________________________________________________________________________
28 //	my own includes
29 //_________________________________________________________________________________________________________________
30 #include <dispatch/systemexec.hxx>
31 #include <threadhelp/readguard.hxx>
32 #include <general.h>
33 #include <services.h>
34 
35 //_________________________________________________________________________________________________________________
36 //	interface includes
37 //_________________________________________________________________________________________________________________
38 #include <com/sun/star/system/SystemShellExecute.hpp>
39 #include <com/sun/star/util/XStringSubstitution.hpp>
40 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
41 #include <com/sun/star/frame/DispatchResultState.hpp>
42 
43 //_________________________________________________________________________________________________________________
44 //	includes of other projects
45 //_________________________________________________________________________________________________________________
46 
47 #include <vcl/svapp.hxx>
48 
49 //_________________________________________________________________________________________________________________
50 //	namespace
51 //_________________________________________________________________________________________________________________
52 
53 namespace framework{
54 
55 //_________________________________________________________________________________________________________________
56 //	non exported const
57 //_________________________________________________________________________________________________________________
58 
59 #define PROTOCOL_VALUE      "systemexecute:"
60 #define PROTOCOL_LENGTH     14
61 
62 //_________________________________________________________________________________________________________________
63 //	non exported definitions
64 //_________________________________________________________________________________________________________________
65 
66 //_________________________________________________________________________________________________________________
67 //	declarations
68 //_________________________________________________________________________________________________________________
69 
70 //_________________________________________________________________________________________________________________
71 // XInterface, XTypeProvider, XServiceInfo
72 
DEFINE_XINTERFACE_5(SystemExec,OWeakObject,DIRECT_INTERFACE (css::lang::XTypeProvider),DIRECT_INTERFACE (css::lang::XServiceInfo),DIRECT_INTERFACE (css::frame::XDispatchProvider),DIRECT_INTERFACE (css::frame::XNotifyingDispatch),DIRECT_INTERFACE (css::frame::XDispatch))73 DEFINE_XINTERFACE_5(SystemExec                                      ,
74                     OWeakObject                                     ,
75                     DIRECT_INTERFACE(css::lang::XTypeProvider      ),
76                     DIRECT_INTERFACE(css::lang::XServiceInfo       ),
77                     DIRECT_INTERFACE(css::frame::XDispatchProvider ),
78                     DIRECT_INTERFACE(css::frame::XNotifyingDispatch),
79                     DIRECT_INTERFACE(css::frame::XDispatch         ))
80 
81 DEFINE_XTYPEPROVIDER_5(SystemExec                    ,
82                        css::lang::XTypeProvider      ,
83                        css::lang::XServiceInfo       ,
84                        css::frame::XDispatchProvider ,
85                        css::frame::XNotifyingDispatch,
86                        css::frame::XDispatch         )
87 
88 DEFINE_XSERVICEINFO_MULTISERVICE_2(SystemExec                   ,
89                                    ::cppu::OWeakObject          ,
90                                    SERVICENAME_PROTOCOLHANDLER  ,
91                                    IMPLEMENTATIONNAME_SYSTEMEXEC)
92 
93 DEFINE_INIT_SERVICE(SystemExec,
94                     {
95                         /*Attention
96                             I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
97                             to create a new instance of this class by our own supported service factory.
98                             see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
99                         */
100                     }
101                    )
102 
103 //_________________________________________________________________________________________________________________
104 
105 SystemExec::SystemExec( const css::uno::Reference< css::uno::XComponentContext >& xContext )
106 		//	Init baseclasses first
107         : ThreadHelpBase( &Application::GetSolarMutex() )
108         , OWeakObject   (                               )
109         // Init member
110         , m_xContext    ( xContext                      )
111 {
112 }
113 
114 //_________________________________________________________________________________________________________________
115 
~SystemExec()116 SystemExec::~SystemExec()
117 {
118     m_xContext = NULL;
119 }
120 
121 //_________________________________________________________________________________________________________________
122 
queryDispatch(const css::util::URL & aURL,const::rtl::OUString &,sal_Int32)123 css::uno::Reference< css::frame::XDispatch > SAL_CALL SystemExec::queryDispatch( const css::util::URL&  aURL    ,
124                                                                                  const ::rtl::OUString&,
125                                                                                        sal_Int32 ) throw( css::uno::RuntimeException )
126 {
127     css::uno::Reference< css::frame::XDispatch > xDispatcher;
128     if (aURL.Complete.compareToAscii(PROTOCOL_VALUE,PROTOCOL_LENGTH)==0)
129         xDispatcher = this;
130     return xDispatcher;
131 }
132 
133 //_________________________________________________________________________________________________________________
134 
queryDispatches(const css::uno::Sequence<css::frame::DispatchDescriptor> & lDescriptor)135 css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL SystemExec::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor ) throw( css::uno::RuntimeException )
136 {
137     sal_Int32 nCount = lDescriptor.getLength();
138     css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatcher( nCount );
139     for( sal_Int32 i=0; i<nCount; ++i )
140     {
141         lDispatcher[i] = this->queryDispatch(
142                             lDescriptor[i].FeatureURL,
143                             lDescriptor[i].FrameName,
144                             lDescriptor[i].SearchFlags);
145     }
146     return lDispatcher;
147 }
148 
149 //_________________________________________________________________________________________________________________
150 
dispatch(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> & lArguments)151 void SAL_CALL SystemExec::dispatch( const css::util::URL&                                  aURL       ,
152                                     const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) throw( css::uno::RuntimeException )
153 {
154     dispatchWithNotification(aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >());
155 }
156 
157 //_________________________________________________________________________________________________________________
158 
dispatchWithNotification(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> &,const css::uno::Reference<css::frame::XDispatchResultListener> & xListener)159 void SAL_CALL SystemExec::dispatchWithNotification( const css::util::URL&                                             aURL      ,
160                                                     const css::uno::Sequence< css::beans::PropertyValue >&,
161                                                     const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ) throw( css::uno::RuntimeException )
162 {
163     // convert "systemexec:file:///c:/temp/test.html" => "file:///c:/temp/test.html"
164     sal_Int32 c = aURL.Complete.getLength()-PROTOCOL_LENGTH;
165     if (c<1) // we dont check for valid URLs here! The system will show an error message ...
166     {
167         impl_notifyResultListener(xListener, css::frame::DispatchResultState::FAILURE);
168         return;
169     }
170     ::rtl::OUString sSystemURLWithVariables = aURL.Complete.copy(PROTOCOL_LENGTH, c);
171 
172     // SAFE ->
173     ReadGuard aReadLock(m_aLock);
174     css::uno::Reference< css::uno::XComponentContext > xContext = m_xContext;
175     aReadLock.unlock();
176     // <- SAFE
177 
178     // TODO check security settings ...
179 
180     try
181     {
182         css::uno::Reference< css::util::XStringSubstitution > xPathSubst(
183             xContext->getServiceManager()->createInstanceWithContext(
184                 SERVICENAME_SUBSTITUTEPATHVARIABLES, xContext),
185             css::uno::UNO_QUERY_THROW);
186 
187         ::rtl::OUString sSystemURL = xPathSubst->substituteVariables(sSystemURLWithVariables, sal_True); // sal_True force an exception if unknown variables exists !
188 
189         css::uno::Reference< css::system::XSystemShellExecute > xShell(
190             css::system::SystemShellExecute::create(xContext));
191 
192         xShell->execute(sSystemURL, ::rtl::OUString(), css::system::SystemShellExecuteFlags::DEFAULTS);
193         impl_notifyResultListener(xListener, css::frame::DispatchResultState::SUCCESS);
194     }
195     catch(const css::uno::Exception&)
196         {
197             impl_notifyResultListener(xListener, css::frame::DispatchResultState::FAILURE);
198         }
199 }
200 
201 //_________________________________________________________________________________________________________________
202 
addStatusListener(const css::uno::Reference<css::frame::XStatusListener> &,const css::util::URL &)203 void SAL_CALL SystemExec::addStatusListener( const css::uno::Reference< css::frame::XStatusListener >&,
204                                              const css::util::URL& ) throw( css::uno::RuntimeException )
205 {
206     // not suported yet
207 }
208 
209 //_________________________________________________________________________________________________________________
210 
removeStatusListener(const css::uno::Reference<css::frame::XStatusListener> &,const css::util::URL &)211 void SAL_CALL SystemExec::removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >&,
212                                                 const css::util::URL& ) throw( css::uno::RuntimeException )
213 {
214     // not suported yet
215 }
216 
217 //_________________________________________________________________________________________________________________
218 
impl_notifyResultListener(const css::uno::Reference<css::frame::XDispatchResultListener> & xListener,const sal_Int16 nState)219 void SystemExec::impl_notifyResultListener(const css::uno::Reference< css::frame::XDispatchResultListener >& xListener,
220                                            const sal_Int16                                                   nState   )
221 {
222     if (xListener.is())
223     {
224         css::frame::DispatchResultEvent aEvent;
225         aEvent.State = nState;
226         xListener->dispatchFinished(aEvent);
227     }
228 }
229 
230 }       //  namespace framework
231