LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/extensions/source/plugin/base - context.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 117 0.0 %
Date: 2013-07-09 Functions: 0 20 0.0 %
Legend: Lines: hit not hit

          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             : #ifdef AIX
      30             : #define _LINUX_SOURCE_COMPAT
      31             : #include <sys/timer.h>
      32             : #undef _LINUX_SOURCE_COMPAT
      33             : #endif
      34             : 
      35             : #ifdef WNT
      36             : #include <prewin.h>
      37             : #include <postwin.h>
      38             : #undef OPTIONAL
      39             : #endif
      40             : 
      41             : #include <cstdarg>
      42             : #include <cstdio>
      43             : 
      44             : #include <plugin/impl.hxx>
      45             : 
      46             : #include <sal/log.hxx>
      47             : #include <com/sun/star/frame/FrameSearchFlag.hpp>
      48             : #include <com/sun/star/frame/XComponentLoader.hpp>
      49             : #include <com/sun/star/frame/Desktop.hpp>
      50             : #include <tools/urlobj.hxx>
      51             : #include <osl/file.hxx>
      52             : 
      53             : #include <cppuhelper/implbase1.hxx>
      54             : 
      55             : using namespace com::sun::star::io;
      56             : using namespace com::sun::star::frame;
      57             : 
      58             : namespace ext_plug {
      59             : 
      60             : class FileSink : public ::cppu::WeakAggImplHelper1< ::com::sun::star::io::XOutputStream >
      61             : {
      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_aMIMEType;
      67             :     OUString                 m_aTarget;
      68             :     OUString                 m_aFileName;
      69             : 
      70             : public:
      71             :     FileSink( const Reference< ::com::sun::star::uno::XComponentContext > &,
      72             :               const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
      73             :               const OUString& mimetype,
      74             :               const OUString& target,
      75             :               const Reference< ::com::sun::star::io::XActiveDataSource > & source );
      76             :     virtual ~FileSink();
      77             : 
      78             :     // ::com::sun::star::io::XOutputStream
      79             :     virtual void SAL_CALL writeBytes( const Sequence<sal_Int8>& ) throw();
      80             :     virtual void SAL_CALL flush() throw();
      81             :     virtual void SAL_CALL closeOutput() throw();
      82             : };
      83             : 
      84             : }
      85             : using namespace ext_plug;
      86             : 
      87             : class XPluginContext_Impl : public ::cppu::WeakAggImplHelper1< ::com::sun::star::plugin::XPluginContext >
      88             : {
      89             :     Reference< ::com::sun::star::uno::XComponentContext >   m_xContext;
      90             :     rtl_TextEncoding                                        m_aEncoding;
      91             : public:
      92             : 
      93             :     XPluginContext_Impl( const Reference< ::com::sun::star::uno::XComponentContext >  & );
      94             :     virtual ~XPluginContext_Impl();
      95             : 
      96             : 
      97             :     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 );
      98             :     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 );
      99             :     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 );
     100             :     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 );
     101             :     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 );
     102             :     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 );
     103             :     virtual void SAL_CALL displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& message) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
     104             :     virtual OUString SAL_CALL getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & plugin) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
     105             : };
     106             : 
     107           0 : Reference< ::com::sun::star::plugin::XPluginContext >  XPluginManager_Impl::createPluginContext() throw()
     108             : {
     109           0 :     return new XPluginContext_Impl( m_xContext );
     110             : }
     111             : 
     112           0 : XPluginContext_Impl::XPluginContext_Impl( const Reference< ::com::sun::star::uno::XComponentContext >  & rxContext )
     113             :     : m_xContext( rxContext ),
     114           0 :       m_aEncoding( osl_getThreadTextEncoding() )
     115             : {
     116           0 : }
     117             : 
     118           0 : XPluginContext_Impl::~XPluginContext_Impl()
     119             : {
     120           0 : }
     121             : 
     122           0 : OUString XPluginContext_Impl::getValue( const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, ::com::sun::star::plugin::PluginVariable /*variable*/ )
     123             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     124             : {
     125           0 :     return OUString();
     126             : }
     127             : 
     128             : 
     129           0 : 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 )
     130             : {
     131           0 :     Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
     132             : 
     133           0 :     if(  target.isEmpty() )
     134             :     {
     135           0 :         INetURLObject aURL;
     136           0 :         aURL.SetSmartProtocol( INET_PROT_FILE );
     137           0 :         aURL.SetSmartURL( OUStringToOString( url, m_aEncoding ) );
     138             : 
     139           0 :         OUString aUrl = aURL.GetMainURL(INetURLObject::DECODE_TO_IURI);
     140             :         // the mimetype cannot be specified
     141           0 :         plugin->provideNewStream( OUString(),
     142             :                                   Reference< XActiveDataSource >(),
     143             :                                   aUrl,
     144           0 :                                   0, 0, aUrl.startsWith("file:") );
     145           0 :         return;
     146             :     }
     147             : 
     148           0 :     XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
     149             : 
     150           0 :     if( pPlugin )
     151             :     {
     152             :         try
     153             :         {
     154           0 :             ::com::sun::star::beans::PropertyValue aValue;
     155           0 :             aValue.Name     = OUString("Referer");
     156           0 :             aValue.Value <<= pPlugin->getRefererURL();
     157             : 
     158           0 :             Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
     159             :             Reference< ::com::sun::star::lang::XComponent >  xComp =
     160           0 :                 xDesktop->loadComponentFromURL(
     161             :                                               url,
     162             :                                               target,
     163             :                                               ::com::sun::star::frame::FrameSearchFlag::PARENT          |
     164             :                                               ::com::sun::star::frame::FrameSearchFlag::SELF            |
     165             :                                               ::com::sun::star::frame::FrameSearchFlag::CHILDREN        |
     166             :                                               ::com::sun::star::frame::FrameSearchFlag::SIBLINGS        |
     167             :                                               ::com::sun::star::frame::FrameSearchFlag::TASKS           |
     168             :                                               ::com::sun::star::frame::FrameSearchFlag::CREATE,
     169             :                                               aArgs
     170           0 :                                               );
     171             :         }
     172           0 :         catch(...)
     173             :         {
     174           0 :             throw ::com::sun::star::plugin::PluginException();
     175             :         }
     176           0 :     }
     177             : }
     178             : 
     179           0 : 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 )
     180             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     181             : {
     182           0 :     getURL( plugin, url, target );
     183           0 :     if( listener.is() )
     184           0 :         listener->disposing( ::com::sun::star::lang::EventObject() );
     185           0 : }
     186             : 
     187           0 : OUString XPluginContext_Impl::getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/)
     188             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     189             : {
     190           0 :     return OUString("Mozilla 3.0");
     191             : }
     192             : 
     193           0 : void XPluginContext_Impl::displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, const OUString& /*message*/)
     194             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     195             : {
     196           0 : }
     197             : 
     198           0 : 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)
     199             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     200             : {
     201           0 :     Sequence< sal_Int8 > aBuf;
     202             : 
     203           0 :     if( file )
     204             :     {
     205           0 :         OUString aFileName( (char*)buf.getConstArray(), strlen((char*)buf.getConstArray()), m_aEncoding );
     206           0 :         INetURLObject aFilePath( aFileName );
     207           0 :         aFileName = aFilePath.PathToFileName();
     208           0 :         SvFileStream aStream( aFileName, STREAM_READ );
     209           0 :         if( aStream.IsOpen() )
     210             :         {
     211           0 :             int nBytes = 0;
     212           0 :             aStream.Seek( STREAM_SEEK_TO_END );
     213           0 :             aBuf = Sequence<sal_Int8>( nBytes = aStream.Tell() );
     214           0 :             aStream.Seek( STREAM_SEEK_TO_BEGIN );
     215           0 :             aStream.Read( aBuf.getArray(), nBytes );
     216           0 :             aStream.Close();
     217           0 :             osl::FileBase::getFileURLFromSystemPath( aFileName, aFileName );
     218           0 :             osl::File::remove( aFileName );
     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 = OUString("Referer");
     231           0 :             aValues[0].Value <<= pPlugin->getRefererURL();
     232             : 
     233           0 :             aValues[1].Name = OUString("PostString");
     234           0 :             aValues[1].Value <<= 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 OUString& url, const 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 OUString& mimetype, const 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 OUString& mimetype,
     275             :                     const 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 :     osl::FileBase::createTempFile( 0, 0, &m_aFileName );
     282           0 :     OString aFile = OUStringToOString( m_aFileName, osl_getThreadTextEncoding() );
     283           0 :     fp = fopen( aFile.getStr() , "wb" );
     284             : 
     285           0 :     Reference< ::com::sun::star::io::XActiveDataControl >  xControl( source, UNO_QUERY );
     286             : 
     287           0 :     source->setOutputStream( Reference< ::com::sun::star::io::XOutputStream > ( this ) );
     288           0 :     if( xControl.is() )
     289           0 :         xControl->start();
     290           0 : }
     291             : 
     292           0 : FileSink::~FileSink()
     293             : {
     294           0 :     osl::File::remove( m_aFileName );
     295           0 : }
     296             : 
     297           0 : void FileSink::closeOutput() throw()
     298             : {
     299           0 :     if( fp )
     300           0 :         fclose( fp );
     301             : 
     302           0 :     Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
     303           0 :     XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( m_xPlugin );
     304             : 
     305           0 :     if( pPlugin )
     306             :     {
     307             :         try
     308             :         {
     309           0 :             ::com::sun::star::beans::PropertyValue aValue;
     310           0 :             aValue.Name = OUString("Referer");
     311           0 :             aValue.Value <<= pPlugin->getRefererURL();
     312             : 
     313           0 :             Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
     314             :             Reference< ::com::sun::star::lang::XComponent >  xComp =
     315           0 :                 xDesktop->loadComponentFromURL(
     316             :                                               m_aFileName,
     317             :                                               m_aTarget,
     318             :                                               ::com::sun::star::frame::FrameSearchFlag::PARENT          |
     319             :                                               ::com::sun::star::frame::FrameSearchFlag::SELF            |
     320             :                                               ::com::sun::star::frame::FrameSearchFlag::CHILDREN        |
     321             :                                               ::com::sun::star::frame::FrameSearchFlag::SIBLINGS        |
     322             :                                               ::com::sun::star::frame::FrameSearchFlag::TASKS           |
     323             :                                               ::com::sun::star::frame::FrameSearchFlag::CREATE,
     324             :                                               aArgs
     325           0 :                                               );
     326             :         }
     327           0 :         catch( ... )
     328             :         {
     329             :         }
     330             :     }
     331           0 :     release();
     332           0 : }
     333             : 
     334           0 : void FileSink::writeBytes( const Sequence<sal_Int8>& Buffer ) throw()
     335             : {
     336           0 :     if( fp )
     337             :     {
     338           0 :         size_t nItems = Buffer.getLength();
     339           0 :         bool bSuccess = (fwrite(Buffer.getConstArray(), 1, nItems, fp) == nItems);
     340             :         SAL_WARN_IF( !bSuccess, "extensions.plugin", "short write");
     341             :     }
     342           0 : }
     343             : 
     344           0 : void FileSink::flush() throw()
     345             : {
     346           0 :     if( fp )
     347           0 :         fflush( fp );
     348           0 : }
     349             : 
     350             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10