Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : *
6 : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : *
8 : * OpenOffice.org - a multi-platform office productivity suite
9 : *
10 : * This file is part of OpenOffice.org.
11 : *
12 : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : * it under the terms of the GNU Lesser General Public License version 3
14 : * only, as published by the Free Software Foundation.
15 : *
16 : * OpenOffice.org is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : * GNU Lesser General Public License version 3 for more details
20 : * (a copy is included in the LICENSE file that accompanied this code).
21 : *
22 : * You should have received a copy of the GNU Lesser General Public License
23 : * version 3 along with OpenOffice.org. If not, see
24 : * <http://www.openoffice.org/license.html>
25 : * for a copy of the LGPLv3 License.
26 : *
27 : ************************************************************************/
28 :
29 :
30 : #ifdef AIX
31 : #define _LINUX_SOURCE_COMPAT
32 : #include <sys/timer.h>
33 : #undef _LINUX_SOURCE_COMPAT
34 : #endif
35 :
36 : #ifdef WNT
37 : #include <prewin.h>
38 : #include <postwin.h>
39 : #undef OPTIONAL
40 : #endif
41 :
42 : #include <cstdarg>
43 : #include <cstdio>
44 :
45 : #include <plugin/impl.hxx>
46 :
47 : #include <sal/log.hxx>
48 : #include <com/sun/star/frame/FrameSearchFlag.hpp>
49 : #include <com/sun/star/frame/XComponentLoader.hpp>
50 : #include <com/sun/star/frame/Desktop.hpp>
51 : #include <tools/fsys.hxx>
52 : #include <tools/urlobj.hxx>
53 :
54 : #include <cppuhelper/implbase1.hxx>
55 :
56 : using namespace com::sun::star::io;
57 : using namespace com::sun::star::frame;
58 :
59 : namespace ext_plug {
60 :
61 : class FileSink : public ::cppu::WeakAggImplHelper1< ::com::sun::star::io::XOutputStream >
62 : {
63 : private:
64 : Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
65 : FILE* fp;
66 : Reference< ::com::sun::star::plugin::XPlugin > m_xPlugin;
67 : ::rtl::OUString m_aMIMEType;
68 : ::rtl::OUString m_aTarget;
69 : ::rtl::OUString m_aFileName;
70 :
71 : public:
72 : FileSink( const Reference< ::com::sun::star::uno::XComponentContext > &,
73 : const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
74 : const ::rtl::OUString& mimetype,
75 : const ::rtl::OUString& target,
76 : const Reference< ::com::sun::star::io::XActiveDataSource > & source );
77 : virtual ~FileSink();
78 :
79 : // ::com::sun::star::io::XOutputStream
80 : virtual void SAL_CALL writeBytes( const Sequence<sal_Int8>& ) throw();
81 : virtual void SAL_CALL flush() throw();
82 : virtual void SAL_CALL closeOutput() throw();
83 : };
84 :
85 : }
86 : using namespace ext_plug;
87 :
88 : class XPluginContext_Impl : public ::cppu::WeakAggImplHelper1< ::com::sun::star::plugin::XPluginContext >
89 : {
90 : Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
91 : rtl_TextEncoding m_aEncoding;
92 : public:
93 :
94 : XPluginContext_Impl( const Reference< ::com::sun::star::uno::XComponentContext > & );
95 : virtual ~XPluginContext_Impl();
96 :
97 :
98 : virtual ::rtl::OUString SAL_CALL getValue(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, ::com::sun::star::plugin::PluginVariable variable) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
99 : virtual void SAL_CALL getURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& url, const ::rtl::OUString& target, const Reference< ::com::sun::star::lang::XEventListener > & listener) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
100 : virtual void SAL_CALL getURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& url, const ::rtl::OUString& target) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
101 : virtual void SAL_CALL postURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& url, const ::rtl::OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file, const Reference< ::com::sun::star::lang::XEventListener > & listener) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
102 : virtual void SAL_CALL postURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& url, const ::rtl::OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
103 : virtual void SAL_CALL newStream(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& mimetype, const ::rtl::OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
104 : virtual void SAL_CALL displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& message) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
105 : virtual ::rtl::OUString SAL_CALL getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & plugin) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
106 : };
107 :
108 0 : Reference< ::com::sun::star::plugin::XPluginContext > XPluginManager_Impl::createPluginContext() throw()
109 : {
110 0 : return new XPluginContext_Impl( m_xContext );
111 : }
112 :
113 0 : XPluginContext_Impl::XPluginContext_Impl( const Reference< ::com::sun::star::uno::XComponentContext > & rxContext )
114 : : m_xContext( rxContext ),
115 0 : m_aEncoding( osl_getThreadTextEncoding() )
116 : {
117 0 : }
118 :
119 0 : XPluginContext_Impl::~XPluginContext_Impl()
120 : {
121 0 : }
122 :
123 0 : ::rtl::OUString XPluginContext_Impl::getValue( const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, ::com::sun::star::plugin::PluginVariable /*variable*/ )
124 : throw( ::com::sun::star::plugin::PluginException, RuntimeException )
125 : {
126 0 : return ::rtl::OUString();
127 : }
128 :
129 :
130 0 : void XPluginContext_Impl::getURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& url, const ::rtl::OUString& target) throw( ::com::sun::star::plugin::PluginException, RuntimeException )
131 : {
132 0 : Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
133 :
134 0 : if( target.isEmpty() )
135 : {
136 0 : INetURLObject aURL;
137 0 : aURL.SetSmartProtocol( INET_PROT_FILE );
138 0 : aURL.SetSmartURL( ::rtl::OUStringToOString( url, m_aEncoding ) );
139 :
140 0 : rtl::OUString aUrl = aURL.GetMainURL(INetURLObject::DECODE_TO_IURI);
141 : // the mimetype cannot be specified
142 0 : plugin->provideNewStream( ::rtl::OUString(),
143 : Reference< XActiveDataSource >(),
144 : aUrl,
145 0 : 0, 0, (sal_Bool)(aUrl.compareToAscii( "file:", 5 ) == 0) );
146 0 : return;
147 : }
148 :
149 0 : XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
150 :
151 0 : if( pPlugin )
152 : {
153 : try
154 : {
155 0 : ::com::sun::star::beans::PropertyValue aValue;
156 0 : aValue.Name = ::rtl::OUString("Referer");
157 0 : aValue.Value <<= pPlugin->getRefererURL();
158 :
159 0 : Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
160 : Reference< ::com::sun::star::lang::XComponent > xComp =
161 0 : xDesktop->loadComponentFromURL(
162 : url,
163 : target,
164 : ::com::sun::star::frame::FrameSearchFlag::PARENT |
165 : ::com::sun::star::frame::FrameSearchFlag::SELF |
166 : ::com::sun::star::frame::FrameSearchFlag::CHILDREN |
167 : ::com::sun::star::frame::FrameSearchFlag::SIBLINGS |
168 : ::com::sun::star::frame::FrameSearchFlag::TASKS |
169 : ::com::sun::star::frame::FrameSearchFlag::CREATE,
170 : aArgs
171 0 : );
172 : }
173 0 : catch(...)
174 : {
175 0 : throw ::com::sun::star::plugin::PluginException();
176 : }
177 0 : }
178 : }
179 :
180 0 : void XPluginContext_Impl::getURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& url, const ::rtl::OUString& target, const Reference< ::com::sun::star::lang::XEventListener > & listener )
181 : throw( ::com::sun::star::plugin::PluginException, RuntimeException )
182 : {
183 0 : getURL( plugin, url, target );
184 0 : if( listener.is() )
185 0 : listener->disposing( ::com::sun::star::lang::EventObject() );
186 0 : }
187 :
188 0 : ::rtl::OUString XPluginContext_Impl::getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/)
189 : throw( ::com::sun::star::plugin::PluginException, RuntimeException )
190 : {
191 0 : return ::rtl::OUString("Mozilla 3.0");
192 : }
193 :
194 0 : void XPluginContext_Impl::displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, const ::rtl::OUString& /*message*/)
195 : throw( ::com::sun::star::plugin::PluginException, RuntimeException )
196 : {
197 0 : }
198 :
199 0 : void XPluginContext_Impl::postURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& url, const ::rtl::OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file)
200 : throw( ::com::sun::star::plugin::PluginException, RuntimeException )
201 : {
202 0 : Sequence< sal_Int8 > aBuf;
203 :
204 0 : if( file )
205 : {
206 0 : String aFileName( (char*)buf.getConstArray(), m_aEncoding );
207 0 : INetURLObject aFilePath( aFileName );
208 0 : aFileName = aFilePath.PathToFileName();
209 0 : SvFileStream aStream( aFileName, STREAM_READ );
210 0 : if( aStream.IsOpen() )
211 : {
212 0 : int nBytes = 0;
213 0 : aStream.Seek( STREAM_SEEK_TO_END );
214 0 : aBuf = Sequence<sal_Int8>( nBytes = aStream.Tell() );
215 0 : aStream.Seek( STREAM_SEEK_TO_BEGIN );
216 0 : aStream.Read( aBuf.getArray(), nBytes );
217 0 : aStream.Close();
218 0 : DirEntry( aFileName ).Kill();
219 0 : }
220 : }
221 :
222 0 : Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
223 :
224 0 : XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
225 0 : if( pPlugin )
226 : {
227 : try
228 : {
229 0 : ::com::sun::star::beans::PropertyValue aValues[2];
230 0 : aValues[0].Name = ::rtl::OUString("Referer");
231 0 : aValues[0].Value <<= pPlugin->getRefererURL();
232 :
233 0 : aValues[1].Name = ::rtl::OUString("PostString");
234 0 : aValues[1].Value <<= ::rtl::OStringToOUString( (char*)( file ? aBuf : buf ).getConstArray(), m_aEncoding );
235 0 : Sequence< ::com::sun::star::beans::PropertyValue > aArgs( aValues, 2 );
236 : Reference< ::com::sun::star::lang::XComponent > xComp =
237 0 : xDesktop->loadComponentFromURL(
238 : url,
239 : target,
240 : ::com::sun::star::frame::FrameSearchFlag::PARENT |
241 : ::com::sun::star::frame::FrameSearchFlag::SELF |
242 : ::com::sun::star::frame::FrameSearchFlag::CHILDREN |
243 : ::com::sun::star::frame::FrameSearchFlag::SIBLINGS |
244 : ::com::sun::star::frame::FrameSearchFlag::TASKS |
245 : ::com::sun::star::frame::FrameSearchFlag::CREATE,
246 : aArgs
247 0 : );
248 : }
249 0 : catch( ... )
250 : {
251 0 : throw ::com::sun::star::plugin::PluginException();
252 : }
253 0 : }
254 0 : }
255 :
256 0 : void XPluginContext_Impl::postURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& url, const ::rtl::OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file, const Reference< ::com::sun::star::lang::XEventListener > & listener )
257 : throw( ::com::sun::star::plugin::PluginException, RuntimeException )
258 : {
259 0 : postURL( plugin, url, target, buf, file );
260 0 : if( listener.is() )
261 0 : listener->disposing( ::com::sun::star::lang::EventObject() );
262 0 : }
263 :
264 0 : void XPluginContext_Impl::newStream( const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& mimetype, const ::rtl::OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source )
265 : throw( ::com::sun::star::plugin::PluginException, RuntimeException )
266 : {
267 0 : FileSink* pNewSink = new FileSink( m_xContext, plugin, mimetype, target, source );
268 0 : pNewSink->acquire();
269 0 : }
270 :
271 :
272 :
273 0 : FileSink::FileSink( const Reference< ::com::sun::star::uno::XComponentContext > & rxContext, const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
274 : const ::rtl::OUString& mimetype,
275 : const ::rtl::OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source ) :
276 : m_xContext( rxContext ),
277 : m_xPlugin( plugin ),
278 : m_aMIMEType( mimetype ),
279 0 : m_aTarget( target )
280 : {
281 0 : DirEntry aEntry;
282 0 : m_aFileName = aEntry.TempName().GetFull();
283 0 : ::rtl::OString aFile = ::rtl::OUStringToOString( m_aFileName, osl_getThreadTextEncoding() );
284 0 : fp = fopen( aFile.getStr() , "wb" );
285 :
286 0 : Reference< ::com::sun::star::io::XActiveDataControl > xControl( source, UNO_QUERY );
287 :
288 0 : source->setOutputStream( Reference< ::com::sun::star::io::XOutputStream > ( this ) );
289 0 : if( xControl.is() )
290 0 : xControl->start();
291 0 : }
292 :
293 0 : FileSink::~FileSink()
294 : {
295 0 : DirEntry aEntry( m_aFileName );
296 0 : aEntry.Kill();
297 0 : }
298 :
299 0 : void FileSink::closeOutput() throw()
300 : {
301 0 : if( fp )
302 0 : fclose( fp );
303 :
304 0 : Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
305 0 : XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( m_xPlugin );
306 :
307 0 : if( pPlugin )
308 : {
309 : try
310 : {
311 0 : ::com::sun::star::beans::PropertyValue aValue;
312 0 : aValue.Name = ::rtl::OUString("Referer");
313 0 : aValue.Value <<= pPlugin->getRefererURL();
314 :
315 0 : Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
316 : Reference< ::com::sun::star::lang::XComponent > xComp =
317 0 : xDesktop->loadComponentFromURL(
318 : m_aFileName,
319 : m_aTarget,
320 : ::com::sun::star::frame::FrameSearchFlag::PARENT |
321 : ::com::sun::star::frame::FrameSearchFlag::SELF |
322 : ::com::sun::star::frame::FrameSearchFlag::CHILDREN |
323 : ::com::sun::star::frame::FrameSearchFlag::SIBLINGS |
324 : ::com::sun::star::frame::FrameSearchFlag::TASKS |
325 : ::com::sun::star::frame::FrameSearchFlag::CREATE,
326 : aArgs
327 0 : );
328 : }
329 0 : catch( ... )
330 : {
331 : }
332 : }
333 0 : release();
334 0 : }
335 :
336 0 : void FileSink::writeBytes( const Sequence<sal_Int8>& Buffer ) throw()
337 : {
338 0 : if( fp )
339 : {
340 0 : size_t nItems = Buffer.getLength();
341 0 : bool bSuccess = (fwrite(Buffer.getConstArray(), 1, nItems, fp) == nItems);
342 : SAL_WARN_IF( !bSuccess, "extensions", "short write");
343 : }
344 0 : }
345 :
346 0 : void FileSink::flush() throw()
347 : {
348 0 : if( fp )
349 0 : fflush( fp );
350 0 : }
351 :
352 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|