merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / plugin / base / context.cxx
blobb4243a58907497e66555a91f187764f8bb87e95c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: context.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
34 #if STLPORT_VERSION>=321
35 #include <cstdarg>
36 #include <cstdio>
37 #endif
39 #include <plugin/impl.hxx>
41 #ifndef _COM_SUN_STAR_FRAME_FRAMESEARCHFLAGS_HPP_
42 #include <com/sun/star/frame/FrameSearchFlag.hpp>
43 #endif
44 #include <com/sun/star/frame/XComponentLoader.hpp>
45 #include <tools/fsys.hxx>
46 #include <tools/urlobj.hxx>
48 #include <cppuhelper/implbase1.hxx>
50 using namespace com::sun::star::io;
52 namespace ext_plug {
54 class FileSink : public ::cppu::WeakAggImplHelper1< ::com::sun::star::io::XOutputStream >
56 private:
57 Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xSMgr;
58 FILE* fp;
59 Reference< ::com::sun::star::plugin::XPlugin > m_xPlugin;
60 ::rtl::OUString m_aMIMEType;
61 ::rtl::OUString m_aTarget;
62 ::rtl::OUString m_aFileName;
64 public:
65 FileSink( const Reference< ::com::sun::star::lang::XMultiServiceFactory > &,
66 const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
67 const ::rtl::OUString& mimetype,
68 const ::rtl::OUString& target,
69 const Reference< ::com::sun::star::io::XActiveDataSource > & source );
70 virtual ~FileSink();
72 // ::com::sun::star::io::XOutputStream
73 virtual void SAL_CALL writeBytes( const Sequence<sal_Int8>& ) throw();
74 virtual void SAL_CALL flush() throw();
75 virtual void SAL_CALL closeOutput() throw();
79 using namespace ext_plug;
81 class XPluginContext_Impl : public ::cppu::WeakAggImplHelper1< ::com::sun::star::plugin::XPluginContext >
83 Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xSMgr;
84 rtl_TextEncoding m_aEncoding;
85 public:
87 XPluginContext_Impl( const Reference< ::com::sun::star::lang::XMultiServiceFactory > & );
88 virtual ~XPluginContext_Impl();
91 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 );
92 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 );
93 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 );
94 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 );
95 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 );
96 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 );
97 virtual void SAL_CALL displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& message) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
98 virtual ::rtl::OUString SAL_CALL getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & plugin) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
101 Reference< ::com::sun::star::plugin::XPluginContext > XPluginManager_Impl::createPluginContext() throw()
103 return new XPluginContext_Impl( m_xSMgr );
106 XPluginContext_Impl::XPluginContext_Impl( const Reference< ::com::sun::star::lang::XMultiServiceFactory > & rSMgr )
107 : m_xSMgr( rSMgr ),
108 m_aEncoding( gsl_getSystemTextEncoding() )
112 XPluginContext_Impl::~XPluginContext_Impl()
116 ::rtl::OUString XPluginContext_Impl::getValue( const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, ::com::sun::star::plugin::PluginVariable /*variable*/ )
117 throw( ::com::sun::star::plugin::PluginException, RuntimeException )
119 return ::rtl::OUString();
123 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 )
125 Reference< XInterface > xInst = m_xSMgr->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.frame.Desktop" ) );
126 if( ! xInst.is() )
127 return;
129 if( ! target.getLength() )
131 INetURLObject aURL;
132 aURL.SetSmartProtocol( INET_PROT_FILE );
133 aURL.SetSmartURL( ::rtl::OUStringToOString( url, m_aEncoding ) );
135 rtl::OUString aUrl = aURL.GetMainURL(INetURLObject::DECODE_TO_IURI);
136 // the mimetype cannot be specified
137 plugin->provideNewStream( ::rtl::OUString(),
138 Reference< XActiveDataSource >(),
139 aUrl,
140 0, 0, (sal_Bool)(aUrl.compareToAscii( "file:", 5 ) == 0) );
141 return;
144 Reference< ::com::sun::star::frame::XComponentLoader > xLoader( xInst, UNO_QUERY );
145 XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
147 if( xLoader.is() && pPlugin )
151 ::com::sun::star::beans::PropertyValue aValue;
152 aValue.Name = ::rtl::OUString::createFromAscii( "Referer" );
153 aValue.Value <<= pPlugin->getRefererURL();
155 Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
156 Reference< ::com::sun::star::lang::XComponent > xComp =
157 xLoader->loadComponentFromURL(
158 url,
159 target,
160 ::com::sun::star::frame::FrameSearchFlag::PARENT |
161 ::com::sun::star::frame::FrameSearchFlag::SELF |
162 ::com::sun::star::frame::FrameSearchFlag::CHILDREN |
163 ::com::sun::star::frame::FrameSearchFlag::SIBLINGS |
164 ::com::sun::star::frame::FrameSearchFlag::TASKS |
165 ::com::sun::star::frame::FrameSearchFlag::CREATE,
166 aArgs
169 catch(...)
171 throw ::com::sun::star::plugin::PluginException();
176 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 )
177 throw( ::com::sun::star::plugin::PluginException, RuntimeException )
179 getURL( plugin, url, target );
180 if( listener.is() )
181 listener->disposing( ::com::sun::star::lang::EventObject() );
184 ::rtl::OUString XPluginContext_Impl::getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/)
185 throw( ::com::sun::star::plugin::PluginException, RuntimeException )
187 return ::rtl::OUString::createFromAscii( "Mozilla 3.0" );
190 void XPluginContext_Impl::displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, const ::rtl::OUString& /*message*/)
191 throw( ::com::sun::star::plugin::PluginException, RuntimeException )
195 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)
196 throw( ::com::sun::star::plugin::PluginException, RuntimeException )
198 Sequence< sal_Int8 > aBuf;
200 if( file )
202 String aFileName( (char*)buf.getConstArray(), m_aEncoding );
203 INetURLObject aFilePath( aFileName );
204 aFileName = aFilePath.PathToFileName();
205 SvFileStream aStream( aFileName, STREAM_READ );
206 if( aStream.IsOpen() )
208 int nBytes = 0;
209 aStream.Seek( STREAM_SEEK_TO_END );
210 aBuf = Sequence<sal_Int8>( nBytes = aStream.Tell() );
211 aStream.Seek( STREAM_SEEK_TO_BEGIN );
212 aStream.Read( aBuf.getArray(), nBytes );
213 aStream.Close();
214 DirEntry( aFileName ).Kill();
218 Reference< XInterface > xInst = m_xSMgr->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.frame.Desktop" ) );
219 if( ! xInst.is() )
220 return ;
222 Reference< ::com::sun::star::frame::XComponentLoader > xLoader( xInst, UNO_QUERY );
223 XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
224 if( xLoader.is() && pPlugin )
228 ::com::sun::star::beans::PropertyValue aValues[2];
229 aValues[0].Name = ::rtl::OUString::createFromAscii( "Referer" );
230 aValues[0].Value <<= pPlugin->getRefererURL();
232 aValues[1].Name = ::rtl::OUString::createFromAscii( "PostString" );
233 aValues[1].Value <<= ::rtl::OStringToOUString( (char*)( file ? aBuf : buf ).getConstArray(), m_aEncoding );
234 Sequence< ::com::sun::star::beans::PropertyValue > aArgs( aValues, 2 );
235 Reference< ::com::sun::star::lang::XComponent > xComp =
236 xLoader->loadComponentFromURL(
237 url,
238 target,
239 ::com::sun::star::frame::FrameSearchFlag::PARENT |
240 ::com::sun::star::frame::FrameSearchFlag::SELF |
241 ::com::sun::star::frame::FrameSearchFlag::CHILDREN |
242 ::com::sun::star::frame::FrameSearchFlag::SIBLINGS |
243 ::com::sun::star::frame::FrameSearchFlag::TASKS |
244 ::com::sun::star::frame::FrameSearchFlag::CREATE,
245 aArgs
248 catch( ... )
250 throw ::com::sun::star::plugin::PluginException();
255 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 )
256 throw( ::com::sun::star::plugin::PluginException, RuntimeException )
258 postURL( plugin, url, target, buf, file );
259 if( listener.is() )
260 listener->disposing( ::com::sun::star::lang::EventObject() );
263 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 )
264 throw( ::com::sun::star::plugin::PluginException, RuntimeException )
266 FileSink* pNewSink = new FileSink( m_xSMgr, plugin, mimetype, target, source );
267 pNewSink->acquire();
272 FileSink::FileSink( const Reference< ::com::sun::star::lang::XMultiServiceFactory > & rSMgr, const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
273 const ::rtl::OUString& mimetype,
274 const ::rtl::OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source ) :
275 m_xSMgr( rSMgr ),
276 m_xPlugin( plugin ),
277 m_aMIMEType( mimetype ),
278 m_aTarget( target )
280 DirEntry aEntry;
281 m_aFileName = aEntry.TempName().GetFull();
282 ::rtl::OString aFile = ::rtl::OUStringToOString( m_aFileName, gsl_getSystemTextEncoding() );
283 fp = fopen( aFile.getStr() , "wb" );
285 Reference< ::com::sun::star::io::XActiveDataControl > xControl( source, UNO_QUERY );
287 source->setOutputStream( Reference< ::com::sun::star::io::XOutputStream > ( this ) );
288 if( xControl.is() )
289 xControl->start();
292 FileSink::~FileSink()
294 DirEntry aEntry( m_aFileName );
295 aEntry.Kill();
298 void FileSink::closeOutput() throw()
300 if( fp )
301 fclose( fp );
303 Reference< XInterface > xInst = m_xSMgr->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.frame.Desktop" ) );
304 Reference< ::com::sun::star::frame::XComponentLoader > xLoader( xInst, UNO_QUERY );
305 XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( m_xPlugin );
307 if( xLoader.is() && pPlugin )
311 ::com::sun::star::beans::PropertyValue aValue;
312 aValue.Name = ::rtl::OUString::createFromAscii( "Referer" );
313 aValue.Value <<= pPlugin->getRefererURL();
315 Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
316 Reference< ::com::sun::star::lang::XComponent > xComp =
317 xLoader->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
329 catch( ... )
333 release();
336 void FileSink::writeBytes( const Sequence<sal_Int8>& Buffer ) throw()
338 if( fp )
339 fwrite( Buffer.getConstArray(), 1, Buffer.getLength(), fp );
342 void FileSink::flush() throw()
344 if( fp )
345 fflush( fp );