LCOV - code coverage report
Current view: top level - libreoffice/extensions/source/plugin/base - context.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 123 0.0 %
Date: 2012-12-17 Functions: 0 18 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             : 
      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 <tools/fsys.hxx>
      51             : #include <tools/urlobj.hxx>
      52             : 
      53             : #include <cppuhelper/implbase1.hxx>
      54             : 
      55             : using namespace com::sun::star::io;
      56             : 
      57             : namespace ext_plug {
      58             : 
      59             : class FileSink : public ::cppu::WeakAggImplHelper1< ::com::sun::star::io::XOutputStream >
      60             : {
      61             : private:
      62             :     Reference< ::com::sun::star::lang::XMultiServiceFactory >   m_xSMgr;
      63             :     FILE*                   fp;
      64             :     Reference< ::com::sun::star::plugin::XPlugin >              m_xPlugin;
      65             :     ::rtl::OUString                 m_aMIMEType;
      66             :     ::rtl::OUString                 m_aTarget;
      67             :     ::rtl::OUString                 m_aFileName;
      68             : 
      69             : public:
      70             :     FileSink( const Reference< ::com::sun::star::lang::XMultiServiceFactory >   &,
      71             :               const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
      72             :               const ::rtl::OUString& mimetype,
      73             :               const ::rtl::OUString& target,
      74             :               const Reference< ::com::sun::star::io::XActiveDataSource > & source );
      75             :     virtual ~FileSink();
      76             : 
      77             :     // ::com::sun::star::io::XOutputStream
      78             :     virtual void SAL_CALL writeBytes( const Sequence<sal_Int8>& ) throw();
      79             :     virtual void SAL_CALL flush() throw();
      80             :     virtual void SAL_CALL closeOutput() throw();
      81             : };
      82             : 
      83             : }
      84             : using namespace ext_plug;
      85             : 
      86             : class XPluginContext_Impl : public ::cppu::WeakAggImplHelper1< ::com::sun::star::plugin::XPluginContext >
      87             : {
      88             :     Reference< ::com::sun::star::lang::XMultiServiceFactory >   m_xSMgr;
      89             :     rtl_TextEncoding                                        m_aEncoding;
      90             : public:
      91             : 
      92             :     XPluginContext_Impl( const Reference< ::com::sun::star::lang::XMultiServiceFactory >  & );
      93             :     virtual ~XPluginContext_Impl();
      94             : 
      95             : 
      96             :     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 );
      97             :     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 );
      98             :     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 );
      99             :     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 );
     100             :     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 );
     101             :     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 );
     102             :     virtual void SAL_CALL displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const ::rtl::OUString& message) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
     103             :     virtual ::rtl::OUString SAL_CALL getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & plugin) throw( ::com::sun::star::plugin::PluginException, RuntimeException );
     104             : };
     105             : 
     106           0 : Reference< ::com::sun::star::plugin::XPluginContext >  XPluginManager_Impl::createPluginContext() throw()
     107             : {
     108           0 :     return new XPluginContext_Impl( m_xSMgr );
     109             : }
     110             : 
     111           0 : XPluginContext_Impl::XPluginContext_Impl( const Reference< ::com::sun::star::lang::XMultiServiceFactory >  & rSMgr )
     112             :     : m_xSMgr( rSMgr ),
     113           0 :       m_aEncoding( osl_getThreadTextEncoding() )
     114             : {
     115           0 : }
     116             : 
     117           0 : XPluginContext_Impl::~XPluginContext_Impl()
     118             : {
     119           0 : }
     120             : 
     121           0 : ::rtl::OUString XPluginContext_Impl::getValue( const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, ::com::sun::star::plugin::PluginVariable /*variable*/ )
     122             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     123             : {
     124           0 :     return ::rtl::OUString();
     125             : }
     126             : 
     127             : 
     128           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 )
     129             : {
     130           0 :     Reference< XInterface >  xInst = m_xSMgr->createInstance( ::rtl::OUString("com.sun.star.frame.Desktop") );
     131           0 :     if( ! xInst.is() )
     132             :         return;
     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 :     Reference< ::com::sun::star::frame::XComponentLoader >  xLoader( xInst, UNO_QUERY );
     150           0 :     XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
     151             : 
     152           0 :     if( xLoader.is() && pPlugin )
     153             :     {
     154             :         try
     155             :         {
     156           0 :             ::com::sun::star::beans::PropertyValue aValue;
     157           0 :             aValue.Name     = ::rtl::OUString("Referer");
     158           0 :             aValue.Value <<= pPlugin->getRefererURL();
     159             : 
     160           0 :             Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
     161             :             Reference< ::com::sun::star::lang::XComponent >  xComp =
     162           0 :                 xLoader->loadComponentFromURL(
     163             :                                               url,
     164             :                                               target,
     165             :                                               ::com::sun::star::frame::FrameSearchFlag::PARENT          |
     166             :                                               ::com::sun::star::frame::FrameSearchFlag::SELF            |
     167             :                                               ::com::sun::star::frame::FrameSearchFlag::CHILDREN        |
     168             :                                               ::com::sun::star::frame::FrameSearchFlag::SIBLINGS        |
     169             :                                               ::com::sun::star::frame::FrameSearchFlag::TASKS           |
     170             :                                               ::com::sun::star::frame::FrameSearchFlag::CREATE,
     171             :                                               aArgs
     172           0 :                                               );
     173             :         }
     174           0 :         catch(...)
     175             :         {
     176           0 :             throw ::com::sun::star::plugin::PluginException();
     177             :         }
     178           0 :     }
     179             : }
     180             : 
     181           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 )
     182             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     183             : {
     184           0 :     getURL( plugin, url, target );
     185           0 :     if( listener.is() )
     186           0 :         listener->disposing( ::com::sun::star::lang::EventObject() );
     187           0 : }
     188             : 
     189           0 : ::rtl::OUString XPluginContext_Impl::getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/)
     190             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     191             : {
     192           0 :     return ::rtl::OUString("Mozilla 3.0");
     193             : }
     194             : 
     195           0 : void XPluginContext_Impl::displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, const ::rtl::OUString& /*message*/)
     196             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     197             : {
     198           0 : }
     199             : 
     200           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)
     201             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     202             : {
     203           0 :     Sequence< sal_Int8 > aBuf;
     204             : 
     205           0 :     if( file )
     206             :     {
     207           0 :         String aFileName( (char*)buf.getConstArray(), m_aEncoding );
     208           0 :         INetURLObject aFilePath( aFileName );
     209           0 :         aFileName = aFilePath.PathToFileName();
     210           0 :         SvFileStream aStream( aFileName, STREAM_READ );
     211           0 :         if( aStream.IsOpen() )
     212             :         {
     213           0 :             int nBytes = 0;
     214           0 :             aStream.Seek( STREAM_SEEK_TO_END );
     215           0 :             aBuf = Sequence<sal_Int8>( nBytes = aStream.Tell() );
     216           0 :             aStream.Seek( STREAM_SEEK_TO_BEGIN );
     217           0 :             aStream.Read( aBuf.getArray(), nBytes );
     218           0 :             aStream.Close();
     219           0 :             DirEntry( aFileName ).Kill();
     220           0 :         }
     221             :     }
     222             : 
     223           0 :     Reference< XInterface > xInst = m_xSMgr->createInstance( ::rtl::OUString("com.sun.star.frame.Desktop") );
     224           0 :     if( ! xInst.is() )
     225           0 :         return ;
     226             : 
     227           0 :     Reference< ::com::sun::star::frame::XComponentLoader >  xLoader( xInst, UNO_QUERY );
     228           0 :     XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
     229           0 :     if( xLoader.is() && pPlugin )
     230             :     {
     231             :         try
     232             :         {
     233           0 :             ::com::sun::star::beans::PropertyValue aValues[2];
     234           0 :             aValues[0].Name = ::rtl::OUString("Referer");
     235           0 :             aValues[0].Value <<= pPlugin->getRefererURL();
     236             : 
     237           0 :             aValues[1].Name = ::rtl::OUString("PostString");
     238           0 :             aValues[1].Value <<= ::rtl::OStringToOUString( (char*)( file ? aBuf : buf ).getConstArray(), m_aEncoding );
     239           0 :             Sequence< ::com::sun::star::beans::PropertyValue > aArgs( aValues, 2 );
     240             :             Reference< ::com::sun::star::lang::XComponent >  xComp =
     241           0 :                 xLoader->loadComponentFromURL(
     242             :                                               url,
     243             :                                               target,
     244             :                                               ::com::sun::star::frame::FrameSearchFlag::PARENT          |
     245             :                                               ::com::sun::star::frame::FrameSearchFlag::SELF            |
     246             :                                               ::com::sun::star::frame::FrameSearchFlag::CHILDREN        |
     247             :                                               ::com::sun::star::frame::FrameSearchFlag::SIBLINGS        |
     248             :                                               ::com::sun::star::frame::FrameSearchFlag::TASKS           |
     249             :                                               ::com::sun::star::frame::FrameSearchFlag::CREATE,
     250             :                                               aArgs
     251           0 :                                               );
     252             :         }
     253           0 :         catch( ... )
     254             :         {
     255           0 :             throw ::com::sun::star::plugin::PluginException();
     256             :         }
     257           0 :     }
     258             : }
     259             : 
     260           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 )
     261             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     262             : {
     263           0 :     postURL( plugin, url, target, buf, file );
     264           0 :     if( listener.is() )
     265           0 :         listener->disposing( ::com::sun::star::lang::EventObject() );
     266           0 : }
     267             : 
     268           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 )
     269             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException )
     270             : {
     271           0 :     FileSink*  pNewSink = new FileSink( m_xSMgr, plugin, mimetype, target, source );
     272           0 :     pNewSink->acquire();
     273           0 : }
     274             : 
     275             : 
     276             : 
     277           0 : FileSink::FileSink( const Reference< ::com::sun::star::lang::XMultiServiceFactory >  & rSMgr, const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
     278             :                     const ::rtl::OUString& mimetype,
     279             :                     const ::rtl::OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source ) :
     280             :         m_xSMgr( rSMgr ),
     281             :         m_xPlugin( plugin ),
     282             :         m_aMIMEType( mimetype ),
     283           0 :         m_aTarget( target )
     284             : {
     285           0 :     DirEntry aEntry;
     286           0 :     m_aFileName = aEntry.TempName().GetFull();
     287           0 :     ::rtl::OString aFile = ::rtl::OUStringToOString( m_aFileName, osl_getThreadTextEncoding() );
     288           0 :     fp = fopen( aFile.getStr() , "wb" );
     289             : 
     290           0 :     Reference< ::com::sun::star::io::XActiveDataControl >  xControl( source, UNO_QUERY );
     291             : 
     292           0 :     source->setOutputStream( Reference< ::com::sun::star::io::XOutputStream > ( this ) );
     293           0 :     if( xControl.is() )
     294           0 :         xControl->start();
     295           0 : }
     296             : 
     297           0 : FileSink::~FileSink()
     298             : {
     299           0 :     DirEntry aEntry( m_aFileName );
     300           0 :     aEntry.Kill();
     301           0 : }
     302             : 
     303           0 : void FileSink::closeOutput() throw()
     304             : {
     305           0 :     if( fp )
     306           0 :         fclose( fp );
     307             : 
     308           0 :     Reference< XInterface >  xInst = m_xSMgr->createInstance( ::rtl::OUString("com.sun.star.frame.Desktop") );
     309           0 :     Reference< ::com::sun::star::frame::XComponentLoader >  xLoader( xInst, UNO_QUERY );
     310           0 :     XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( m_xPlugin );
     311             : 
     312           0 :     if( xLoader.is() && pPlugin )
     313             :     {
     314             :         try
     315             :         {
     316           0 :             ::com::sun::star::beans::PropertyValue aValue;
     317           0 :             aValue.Name = ::rtl::OUString("Referer");
     318           0 :             aValue.Value <<= pPlugin->getRefererURL();
     319             : 
     320           0 :             Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
     321             :             Reference< ::com::sun::star::lang::XComponent >  xComp =
     322           0 :                 xLoader->loadComponentFromURL(
     323             :                                               m_aFileName,
     324             :                                               m_aTarget,
     325             :                                               ::com::sun::star::frame::FrameSearchFlag::PARENT          |
     326             :                                               ::com::sun::star::frame::FrameSearchFlag::SELF            |
     327             :                                               ::com::sun::star::frame::FrameSearchFlag::CHILDREN        |
     328             :                                               ::com::sun::star::frame::FrameSearchFlag::SIBLINGS        |
     329             :                                               ::com::sun::star::frame::FrameSearchFlag::TASKS           |
     330             :                                               ::com::sun::star::frame::FrameSearchFlag::CREATE,
     331             :                                               aArgs
     332           0 :                                               );
     333             :         }
     334           0 :         catch( ... )
     335             :         {
     336             :         }
     337             :     }
     338           0 :     release();
     339           0 : }
     340             : 
     341           0 : void FileSink::writeBytes( const Sequence<sal_Int8>& Buffer ) throw()
     342             : {
     343           0 :     if( fp )
     344             :     {
     345           0 :         size_t nItems = Buffer.getLength();
     346           0 :         bool bSuccess = (fwrite(Buffer.getConstArray(), 1, nItems, fp) == nItems);
     347             :         SAL_WARN_IF( !bSuccess, "extensions", "short write");
     348             :     }
     349           0 : }
     350             : 
     351           0 : void FileSink::flush() throw()
     352             : {
     353           0 :     if( fp )
     354           0 :         fflush( fp );
     355           0 : }
     356             : 
     357             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10