Line data Source code
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/systemexec.hxx>
21 : #include <threadhelp/readguard.hxx>
22 : #include <general.h>
23 : #include <services.h>
24 :
25 : #include <com/sun/star/system/SystemShellExecute.hpp>
26 : #include <com/sun/star/util/PathSubstitution.hpp>
27 : #include <com/sun/star/util/XStringSubstitution.hpp>
28 : #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
29 : #include <com/sun/star/frame/DispatchResultState.hpp>
30 :
31 : #include <vcl/svapp.hxx>
32 : #include <comphelper/processfactory.hxx>
33 :
34 : namespace framework{
35 :
36 : #define PROTOCOL_VALUE "systemexecute:"
37 : #define PROTOCOL_LENGTH 14
38 :
39 : //_________________________________________________________________________________________________________________
40 : // XInterface, XTypeProvider, XServiceInfo
41 :
42 0 : DEFINE_XINTERFACE_5(SystemExec ,
43 : OWeakObject ,
44 : DIRECT_INTERFACE(css::lang::XTypeProvider ),
45 : DIRECT_INTERFACE(css::lang::XServiceInfo ),
46 : DIRECT_INTERFACE(css::frame::XDispatchProvider ),
47 : DIRECT_INTERFACE(css::frame::XNotifyingDispatch),
48 : DIRECT_INTERFACE(css::frame::XDispatch ))
49 :
50 0 : DEFINE_XTYPEPROVIDER_5(SystemExec ,
51 : css::lang::XTypeProvider ,
52 : css::lang::XServiceInfo ,
53 : css::frame::XDispatchProvider ,
54 : css::frame::XNotifyingDispatch,
55 : css::frame::XDispatch )
56 :
57 0 : DEFINE_XSERVICEINFO_MULTISERVICE_2(SystemExec ,
58 : ::cppu::OWeakObject ,
59 : SERVICENAME_PROTOCOLHANDLER ,
60 : IMPLEMENTATIONNAME_SYSTEMEXEC)
61 :
62 0 : DEFINE_INIT_SERVICE(SystemExec,
63 : {
64 : /*Attention
65 : I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
66 : to create a new instance of this class by our own supported service factory.
67 : see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
68 : */
69 : }
70 : )
71 :
72 : //_________________________________________________________________________________________________________________
73 :
74 0 : SystemExec::SystemExec( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
75 : // Init baseclasses first
76 0 : : ThreadHelpBase( &Application::GetSolarMutex() )
77 : , OWeakObject ( )
78 : // Init member
79 0 : , m_xContext ( rxContext )
80 : {
81 0 : }
82 :
83 : //_________________________________________________________________________________________________________________
84 :
85 0 : SystemExec::~SystemExec()
86 : {
87 0 : m_xContext = NULL;
88 0 : }
89 :
90 : //_________________________________________________________________________________________________________________
91 :
92 0 : css::uno::Reference< css::frame::XDispatch > SAL_CALL SystemExec::queryDispatch( const css::util::URL& aURL ,
93 : const ::rtl::OUString&,
94 : sal_Int32 ) throw( css::uno::RuntimeException )
95 : {
96 0 : css::uno::Reference< css::frame::XDispatch > xDispatcher;
97 0 : if (aURL.Complete.compareToAscii(PROTOCOL_VALUE,PROTOCOL_LENGTH)==0)
98 0 : xDispatcher = this;
99 0 : return xDispatcher;
100 : }
101 :
102 : //_________________________________________________________________________________________________________________
103 :
104 0 : 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 )
105 : {
106 0 : sal_Int32 nCount = lDescriptor.getLength();
107 0 : css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatcher( nCount );
108 0 : for( sal_Int32 i=0; i<nCount; ++i )
109 : {
110 0 : lDispatcher[i] = this->queryDispatch(
111 0 : lDescriptor[i].FeatureURL,
112 0 : lDescriptor[i].FrameName,
113 0 : lDescriptor[i].SearchFlags);
114 : }
115 0 : return lDispatcher;
116 : }
117 :
118 : //_________________________________________________________________________________________________________________
119 :
120 0 : void SAL_CALL SystemExec::dispatch( const css::util::URL& aURL ,
121 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) throw( css::uno::RuntimeException )
122 : {
123 0 : dispatchWithNotification(aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >());
124 0 : }
125 :
126 : //_________________________________________________________________________________________________________________
127 :
128 0 : void SAL_CALL SystemExec::dispatchWithNotification( const css::util::URL& aURL ,
129 : const css::uno::Sequence< css::beans::PropertyValue >&,
130 : const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ) throw( css::uno::RuntimeException )
131 : {
132 : // convert "systemexec:file:///c:/temp/test.html" => "file:///c:/temp/test.html"
133 0 : sal_Int32 c = aURL.Complete.getLength()-PROTOCOL_LENGTH;
134 0 : if (c<1) // we dont check for valid URLs here! The system will show an error message ...
135 : {
136 0 : impl_notifyResultListener(xListener, css::frame::DispatchResultState::FAILURE);
137 0 : return;
138 : }
139 0 : ::rtl::OUString sSystemURLWithVariables = aURL.Complete.copy(PROTOCOL_LENGTH, c);
140 :
141 : // SAFE ->
142 0 : ReadGuard aReadLock(m_aLock);
143 0 : css::uno::Reference< css::uno::XComponentContext > xContext = m_xContext;
144 0 : aReadLock.unlock();
145 : // <- SAFE
146 :
147 : // TODO check security settings ...
148 :
149 : try
150 : {
151 0 : css::uno::Reference< css::util::XStringSubstitution > xPathSubst( css::util::PathSubstitution::create(xContext) );
152 :
153 0 : ::rtl::OUString sSystemURL = xPathSubst->substituteVariables(sSystemURLWithVariables, sal_True); // sal_True force an exception if unknown variables exists !
154 :
155 0 : css::uno::Reference< css::system::XSystemShellExecute > xShell = css::system::SystemShellExecute::create( xContext );
156 :
157 0 : xShell->execute(sSystemURL, ::rtl::OUString(), css::system::SystemShellExecuteFlags::URIS_ONLY);
158 0 : impl_notifyResultListener(xListener, css::frame::DispatchResultState::SUCCESS);
159 : }
160 0 : catch(const css::uno::Exception&)
161 : {
162 0 : impl_notifyResultListener(xListener, css::frame::DispatchResultState::FAILURE);
163 0 : }
164 : }
165 :
166 : //_________________________________________________________________________________________________________________
167 :
168 0 : void SAL_CALL SystemExec::addStatusListener( const css::uno::Reference< css::frame::XStatusListener >&,
169 : const css::util::URL& ) throw( css::uno::RuntimeException )
170 : {
171 : // not suported yet
172 0 : }
173 :
174 : //_________________________________________________________________________________________________________________
175 :
176 0 : void SAL_CALL SystemExec::removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >&,
177 : const css::util::URL& ) throw( css::uno::RuntimeException )
178 : {
179 : // not suported yet
180 0 : }
181 :
182 : //_________________________________________________________________________________________________________________
183 :
184 0 : void SystemExec::impl_notifyResultListener(const css::uno::Reference< css::frame::XDispatchResultListener >& xListener,
185 : const sal_Int16 nState )
186 : {
187 0 : if (xListener.is())
188 : {
189 0 : css::frame::DispatchResultEvent aEvent;
190 0 : aEvent.State = nState;
191 0 : xListener->dispatchFinished(aEvent);
192 : }
193 0 : }
194 :
195 : } // namespace framework
196 :
197 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|