bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / plugin / base / context.cxx
blob863e7a3dcf8a9090e3a837990f12bc0f78f0dc67
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
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.
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).
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.
27 ************************************************************************/
29 #ifdef AIX
30 #define _LINUX_SOURCE_COMPAT
31 #include <sys/timer.h>
32 #undef _LINUX_SOURCE_COMPAT
33 #endif
35 #ifdef WNT
36 #include <prewin.h>
37 #include <postwin.h>
38 #endif
40 #include <cstdarg>
41 #include <cstdio>
43 #include <plugin/impl.hxx>
45 #include <sal/log.hxx>
46 #include <com/sun/star/frame/FrameSearchFlag.hpp>
47 #include <com/sun/star/frame/XComponentLoader.hpp>
48 #include <com/sun/star/uno/XComponentContext.hpp>
49 #include <com/sun/star/frame/Desktop.hpp>
50 #include <tools/urlobj.hxx>
51 #include <osl/file.hxx>
53 #include <cppuhelper/implbase1.hxx>
55 using namespace com::sun::star::io;
56 using namespace com::sun::star::frame;
58 namespace ext_plug {
60 class FileSink : public ::cppu::WeakAggImplHelper1< ::com::sun::star::io::XOutputStream >
62 private:
63 Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
64 FILE* fp;
65 Reference< ::com::sun::star::plugin::XPlugin > m_xPlugin;
66 OUString m_aTarget;
67 OUString m_aFileName;
69 public:
70 FileSink( const Reference< ::com::sun::star::uno::XComponentContext > &,
71 const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
72 const OUString& target,
73 const Reference< ::com::sun::star::io::XActiveDataSource > & source );
74 virtual ~FileSink();
76 // ::com::sun::star::io::XOutputStream
77 virtual void SAL_CALL writeBytes( const Sequence<sal_Int8>& ) throw(std::exception) SAL_OVERRIDE;
78 virtual void SAL_CALL flush() throw(std::exception) SAL_OVERRIDE;
79 virtual void SAL_CALL closeOutput() throw (RuntimeException, std::exception) SAL_OVERRIDE;
83 using namespace ext_plug;
85 class XPluginContext_Impl : public ::cppu::WeakAggImplHelper1< ::com::sun::star::plugin::XPluginContext >
87 Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
88 rtl_TextEncoding m_aEncoding;
89 public:
91 XPluginContext_Impl( const Reference< ::com::sun::star::uno::XComponentContext > & );
92 virtual ~XPluginContext_Impl();
95 virtual 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, std::exception ) SAL_OVERRIDE;
96 virtual void SAL_CALL getURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Reference< ::com::sun::star::lang::XEventListener > & listener) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
97 virtual void SAL_CALL getURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
98 virtual void SAL_CALL postURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const 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, std::exception ) SAL_OVERRIDE;
99 virtual void SAL_CALL postURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
100 virtual void SAL_CALL newStream(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& mimetype, const OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
101 virtual void SAL_CALL displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& message) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
102 virtual OUString SAL_CALL getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & plugin) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
105 Reference< ::com::sun::star::plugin::XPluginContext > XPluginManager_Impl::createPluginContext() throw(std::exception)
107 return new XPluginContext_Impl( m_xContext );
110 XPluginContext_Impl::XPluginContext_Impl( const Reference< ::com::sun::star::uno::XComponentContext > & rxContext )
111 : m_xContext( rxContext ),
112 m_aEncoding( osl_getThreadTextEncoding() )
116 XPluginContext_Impl::~XPluginContext_Impl()
120 OUString XPluginContext_Impl::getValue( const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, ::com::sun::star::plugin::PluginVariable /*variable*/ )
121 throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
123 return OUString();
127 void XPluginContext_Impl::getURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
129 Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
131 if( target.isEmpty() )
133 INetURLObject aURL;
134 aURL.SetSmartProtocol( INetProtocol::File );
135 aURL.SetSmartURL( url );
137 OUString aUrl = aURL.GetMainURL(INetURLObject::DECODE_TO_IURI);
138 // the mimetype cannot be specified
139 plugin->provideNewStream( OUString(),
140 Reference< XActiveDataSource >(),
141 aUrl,
142 0, 0, aUrl.startsWith("file:") );
143 return;
146 XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
148 if( pPlugin )
152 ::com::sun::star::beans::PropertyValue aValue;
153 aValue.Name = "Referer";
154 aValue.Value <<= pPlugin->getRefererURL();
156 Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
157 Reference< ::com::sun::star::lang::XComponent > xComp =
158 xDesktop->loadComponentFromURL(
159 url,
160 target,
161 ::com::sun::star::frame::FrameSearchFlag::PARENT |
162 ::com::sun::star::frame::FrameSearchFlag::SELF |
163 ::com::sun::star::frame::FrameSearchFlag::CHILDREN |
164 ::com::sun::star::frame::FrameSearchFlag::SIBLINGS |
165 ::com::sun::star::frame::FrameSearchFlag::TASKS |
166 ::com::sun::star::frame::FrameSearchFlag::CREATE,
167 aArgs
170 catch(...)
172 throw ::com::sun::star::plugin::PluginException();
177 void XPluginContext_Impl::getURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Reference< ::com::sun::star::lang::XEventListener > & listener )
178 throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
180 getURL( plugin, url, target );
181 if( listener.is() )
182 listener->disposing( ::com::sun::star::lang::EventObject() );
185 OUString XPluginContext_Impl::getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/)
186 throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
188 return OUString("Mozilla 3.0");
191 void XPluginContext_Impl::displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, const OUString& /*message*/)
192 throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
196 void XPluginContext_Impl::postURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file)
197 throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
199 Sequence< sal_Int8 > aBuf;
201 if( file )
203 OUString aFileName( reinterpret_cast<char const *>(buf.getConstArray()), strlen(reinterpret_cast<char const *>(buf.getConstArray())), m_aEncoding );
204 INetURLObject aFilePath( aFileName );
205 aFileName = aFilePath.PathToFileName();
206 SvFileStream aStream( aFileName, StreamMode::READ );
207 if( aStream.IsOpen() )
209 sal_Int64 const nBytes = aStream.remainingSize();
210 aBuf = Sequence<sal_Int8>( nBytes );
211 aStream.Read( aBuf.getArray(), nBytes );
212 aStream.Close();
213 osl::FileBase::getFileURLFromSystemPath( aFileName, aFileName );
214 osl::File::remove( aFileName );
218 Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
220 XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
221 if( pPlugin )
225 ::com::sun::star::beans::PropertyValue aValues[2];
226 aValues[0].Name = "Referer";
227 aValues[0].Value <<= pPlugin->getRefererURL();
229 aValues[1].Name = "PostString";
230 aValues[1].Value <<= OStringToOUString( reinterpret_cast<char const *>(( file ? aBuf : buf ).getConstArray()), m_aEncoding );
231 Sequence< ::com::sun::star::beans::PropertyValue > aArgs( aValues, 2 );
232 Reference< ::com::sun::star::lang::XComponent > xComp =
233 xDesktop->loadComponentFromURL(
234 url,
235 target,
236 ::com::sun::star::frame::FrameSearchFlag::PARENT |
237 ::com::sun::star::frame::FrameSearchFlag::SELF |
238 ::com::sun::star::frame::FrameSearchFlag::CHILDREN |
239 ::com::sun::star::frame::FrameSearchFlag::SIBLINGS |
240 ::com::sun::star::frame::FrameSearchFlag::TASKS |
241 ::com::sun::star::frame::FrameSearchFlag::CREATE,
242 aArgs
245 catch( ... )
247 throw ::com::sun::star::plugin::PluginException();
252 void XPluginContext_Impl::postURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file, const Reference< ::com::sun::star::lang::XEventListener > & listener )
253 throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
255 postURL( plugin, url, target, buf, file );
256 if( listener.is() )
257 listener->disposing( ::com::sun::star::lang::EventObject() );
260 void XPluginContext_Impl::newStream( const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString&, const OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source )
261 throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
263 FileSink* pNewSink = new FileSink( m_xContext, plugin, target, source );
264 pNewSink->acquire();
269 FileSink::FileSink( const Reference< ::com::sun::star::uno::XComponentContext > & rxContext, const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
270 const OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source ) :
271 m_xContext( rxContext ),
272 m_xPlugin( plugin ),
273 m_aTarget( target )
275 osl::FileBase::createTempFile( 0, 0, &m_aFileName );
276 OString aFile = OUStringToOString( m_aFileName, osl_getThreadTextEncoding() );
277 fp = fopen( aFile.getStr() , "wb" );
279 Reference< ::com::sun::star::io::XActiveDataControl > xControl( source, UNO_QUERY );
281 source->setOutputStream( Reference< ::com::sun::star::io::XOutputStream > ( this ) );
282 if( xControl.is() )
283 xControl->start();
286 FileSink::~FileSink()
288 osl::File::remove( m_aFileName );
291 void FileSink::closeOutput() throw (RuntimeException, std::exception)
293 if( fp )
294 fclose( fp );
296 Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
297 XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( m_xPlugin );
299 if( pPlugin )
303 ::com::sun::star::beans::PropertyValue aValue;
304 aValue.Name = "Referer";
305 aValue.Value <<= pPlugin->getRefererURL();
307 Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
308 Reference< ::com::sun::star::lang::XComponent > xComp =
309 xDesktop->loadComponentFromURL(
310 m_aFileName,
311 m_aTarget,
312 ::com::sun::star::frame::FrameSearchFlag::PARENT |
313 ::com::sun::star::frame::FrameSearchFlag::SELF |
314 ::com::sun::star::frame::FrameSearchFlag::CHILDREN |
315 ::com::sun::star::frame::FrameSearchFlag::SIBLINGS |
316 ::com::sun::star::frame::FrameSearchFlag::TASKS |
317 ::com::sun::star::frame::FrameSearchFlag::CREATE,
318 aArgs
321 catch( ... )
325 release();
328 void FileSink::writeBytes( const Sequence<sal_Int8>& Buffer ) throw(std::exception)
330 if( fp )
332 size_t nItems = Buffer.getLength();
333 bool bSuccess = (fwrite(Buffer.getConstArray(), 1, nItems, fp) == nItems);
334 SAL_WARN_IF( !bSuccess, "extensions.plugin", "short write");
338 void FileSink::flush() throw(std::exception)
340 if( fp )
341 fflush( fp );
344 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */