LCOV - code coverage report
Current view: top level - extensions/source/scanner - scanunx.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 19 139 13.7 %
Date: 2015-06-13 12:38:46 Functions: 8 28 28.6 %
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             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <scanner.hxx>
      21             : #include <sanedlg.hxx>
      22             : #include <osl/thread.hxx>
      23             : #include <cppuhelper/queryinterface.hxx>
      24             : #include <boost/shared_ptr.hpp>
      25             : 
      26             : #if OSL_DEBUG_LEVEL > 1
      27             : #include <stdio.h>
      28             : #endif
      29             : 
      30           0 : BitmapTransporter::BitmapTransporter()
      31             : {
      32             : #if OSL_DEBUG_LEVEL > 1
      33             :     fprintf( stderr, "BitmapTransporter\n" );
      34             : #endif
      35           0 : }
      36             : 
      37           0 : BitmapTransporter::~BitmapTransporter()
      38             : {
      39             : #if OSL_DEBUG_LEVEL > 1
      40             :     fprintf( stderr, "~BitmapTransporter\n" );
      41             : #endif
      42           0 : }
      43             : 
      44             : 
      45             : 
      46           0 : Any SAL_CALL BitmapTransporter::queryInterface( const Type& rType ) throw( RuntimeException, std::exception )
      47             : {
      48           0 :     const Any aRet( cppu::queryInterface( rType, static_cast< css::awt::XBitmap* >( this ) ) );
      49             : 
      50           0 :     return( aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ) );
      51             : }
      52             : 
      53             : 
      54             : 
      55           0 : css::awt::Size BitmapTransporter::getSize() throw(std::exception)
      56             : {
      57           0 :     osl::MutexGuard aGuard( m_aProtector );
      58           0 :     int         nPreviousPos = m_aStream.Tell();
      59           0 :     css::awt::Size   aRet;
      60             : 
      61             :     // ensure that there is at least a header
      62           0 :     m_aStream.Seek( STREAM_SEEK_TO_END );
      63           0 :     int nLen = m_aStream.Tell();
      64           0 :     if( nLen > 15 )
      65             :     {
      66           0 :         m_aStream.Seek( 4 );
      67           0 :         m_aStream.ReadInt32( aRet.Width ).ReadInt32( aRet.Height );
      68             :     }
      69             :     else
      70           0 :         aRet.Width = aRet.Height = 0;
      71             : 
      72           0 :     m_aStream.Seek( nPreviousPos );
      73             : 
      74           0 :     return aRet;
      75             : }
      76             : 
      77             : 
      78             : 
      79           0 : Sequence< sal_Int8 > BitmapTransporter::getDIB() throw(std::exception)
      80             : {
      81           0 :     osl::MutexGuard aGuard( m_aProtector );
      82           0 :     int         nPreviousPos = m_aStream.Tell();
      83             : 
      84             :     // create return value
      85           0 :     m_aStream.Seek( STREAM_SEEK_TO_END );
      86           0 :     int nBytes = m_aStream.Tell();
      87           0 :     m_aStream.Seek( 0 );
      88             : 
      89           0 :     Sequence< sal_Int8 > aValue( nBytes );
      90           0 :     m_aStream.Read( aValue.getArray(), nBytes );
      91           0 :     m_aStream.Seek( nPreviousPos );
      92             : 
      93           0 :     return aValue;
      94             : }
      95             : 
      96             : 
      97             : // - SaneHolder -
      98             : 
      99             : 
     100           0 : struct SaneHolder
     101             : {
     102             :     Sane                m_aSane;
     103             :     Reference< css::awt::XBitmap > m_xBitmap;
     104             :     osl::Mutex          m_aProtector;
     105             :     ScanError           m_nError;
     106             :     bool                m_bBusy;
     107             : 
     108           0 :     SaneHolder() : m_nError(ScanError_ScanErrorNone), m_bBusy(false) {}
     109             : };
     110             : 
     111             : namespace
     112             : {
     113             :     typedef std::vector< boost::shared_ptr<SaneHolder> > sanevec;
     114          14 :     class allSanes
     115             :     {
     116             :     private:
     117             :         int mnRefCount;
     118             :     public:
     119             :         sanevec m_aSanes;
     120          14 :         allSanes() : mnRefCount(0) {}
     121             :         void acquire();
     122             :         void release();
     123             :     };
     124             : 
     125         126 :     void allSanes::acquire()
     126             :     {
     127         126 :         ++mnRefCount;
     128         126 :     }
     129             : 
     130         126 :     void allSanes::release()
     131             :     {
     132             :         // was unused, now because of i99835: "Scanning interface not SANE API
     133             :         // compliant" destroy all SaneHolder to get Sane Dtor called
     134         126 :         --mnRefCount;
     135         126 :         if (!mnRefCount)
     136         113 :             m_aSanes.clear();
     137         126 :     }
     138             : 
     139             :     struct theSaneProtector : public rtl::Static<osl::Mutex, theSaneProtector> {};
     140             :     struct theSanes : public rtl::Static<allSanes, theSanes> {};
     141             : }
     142             : 
     143             : 
     144             : // - ScannerThread -
     145             : 
     146             : 
     147             : class ScannerThread : public osl::Thread
     148             : {
     149             :     boost::shared_ptr<SaneHolder>               m_pHolder;
     150             :     Reference< com::sun::star::lang::XEventListener > m_xListener;
     151             :     ScannerManager*                             m_pManager; // just for the disposing call
     152             : 
     153             : public:
     154             :     virtual void run() SAL_OVERRIDE;
     155           0 :     virtual void onTerminated() SAL_OVERRIDE { delete this; }
     156             : public:
     157             :     ScannerThread( boost::shared_ptr<SaneHolder> pHolder,
     158             :                    const Reference< com::sun::star::lang::XEventListener >& listener,
     159             :                    ScannerManager* pManager );
     160             :     virtual ~ScannerThread();
     161             : };
     162             : 
     163             : 
     164             : 
     165           0 : ScannerThread::ScannerThread(
     166             :                              boost::shared_ptr<SaneHolder> pHolder,
     167             :                              const Reference< com::sun::star::lang::XEventListener >& listener,
     168             :                              ScannerManager* pManager )
     169           0 :         : m_pHolder( pHolder ), m_xListener( listener ), m_pManager( pManager )
     170             : {
     171             : #if OSL_DEBUG_LEVEL > 1
     172             :     fprintf( stderr, "ScannerThread\n" );
     173             : #endif
     174           0 : }
     175             : 
     176           0 : ScannerThread::~ScannerThread()
     177             : {
     178             : #if OSL_DEBUG_LEVEL > 1
     179             :     fprintf( stderr, "~ScannerThread\n" );
     180             : #endif
     181           0 : }
     182             : 
     183           0 : void ScannerThread::run()
     184             : {
     185           0 :     osl_setThreadName("ScannerThread");
     186             : 
     187           0 :     osl::MutexGuard         aGuard( m_pHolder->m_aProtector );
     188           0 :     BitmapTransporter*  pTransporter = new BitmapTransporter;
     189           0 :     Reference< XInterface >   aIf( static_cast< OWeakObject* >( pTransporter ) );
     190             : 
     191           0 :     m_pHolder->m_xBitmap = Reference< css::awt::XBitmap >( aIf, UNO_QUERY );
     192             : 
     193           0 :     m_pHolder->m_bBusy = true;
     194           0 :     if( m_pHolder->m_aSane.IsOpen() )
     195             :     {
     196           0 :         int nOption = m_pHolder->m_aSane.GetOptionByName( "preview" );
     197           0 :         if( nOption != -1 )
     198           0 :             m_pHolder->m_aSane.SetOptionValue( nOption, false );
     199             : 
     200           0 :         m_pHolder->m_nError =
     201           0 :             m_pHolder->m_aSane.Start( *pTransporter ) ?
     202           0 :             ScanError_ScanErrorNone : ScanError_ScanCanceled;
     203             :     }
     204             :     else
     205           0 :         m_pHolder->m_nError = ScanError_ScannerNotAvailable;
     206             : 
     207             : 
     208           0 :     Reference< XInterface > xXInterface( static_cast< OWeakObject* >( m_pManager ) );
     209           0 :     m_xListener->disposing( com::sun::star::lang::EventObject(xXInterface) );
     210           0 :     m_pHolder->m_bBusy = false;
     211           0 : }
     212             : 
     213             : 
     214             : // - ScannerManager -
     215             : 
     216             : 
     217         126 : void ScannerManager::AcquireData()
     218             : {
     219         126 :     osl::MutexGuard aGuard( theSaneProtector::get() );
     220         126 :     theSanes::get().acquire();
     221         126 : }
     222             : 
     223         126 : void ScannerManager::ReleaseData()
     224             : {
     225         126 :     osl::MutexGuard aGuard( theSaneProtector::get() );
     226         126 :     theSanes::get().release();
     227         126 : }
     228             : 
     229             : 
     230             : 
     231           0 : css::awt::Size ScannerManager::getSize() throw(std::exception)
     232             : {
     233           0 :     css::awt::Size aRet;
     234           0 :     aRet.Width = aRet.Height = 0;
     235           0 :     return aRet;
     236             : }
     237             : 
     238             : 
     239             : 
     240           0 : Sequence< sal_Int8 > ScannerManager::getDIB() throw(std::exception)
     241             : {
     242           0 :     return Sequence< sal_Int8 >();
     243             : }
     244             : 
     245             : 
     246             : 
     247           0 : Sequence< ScannerContext > ScannerManager::getAvailableScanners() throw(std::exception)
     248             : {
     249           0 :     osl::MutexGuard aGuard( theSaneProtector::get() );
     250           0 :     sanevec &rSanes = theSanes::get().m_aSanes;
     251             : 
     252           0 :     if( rSanes.empty() )
     253             :     {
     254           0 :         boost::shared_ptr<SaneHolder> pSaneHolder(new SaneHolder);
     255           0 :         if( Sane::IsSane() )
     256           0 :             rSanes.push_back( pSaneHolder );
     257             :     }
     258             : 
     259           0 :     if( Sane::IsSane() )
     260             :     {
     261           0 :         Sequence< ScannerContext > aRet(1);
     262           0 :         aRet[0].ScannerName      = "SANE";
     263           0 :         aRet[0].InternalData     = 0;
     264           0 :         return aRet;
     265             :     }
     266             : 
     267           0 :     return Sequence< ScannerContext >();
     268             : }
     269             : 
     270             : 
     271             : 
     272           0 : sal_Bool ScannerManager::configureScannerAndScan( ScannerContext& scanner_context,
     273             :                                                   const Reference< com::sun::star::lang::XEventListener >& listener )
     274             :     throw (ScannerException, RuntimeException, std::exception)
     275             : {
     276             :     bool bRet;
     277             :     bool bScan;
     278             :     {
     279           0 :         osl::MutexGuard aGuard( theSaneProtector::get() );
     280           0 :         sanevec &rSanes = theSanes::get().m_aSanes;
     281             : 
     282             : #if OSL_DEBUG_LEVEL > 1
     283             :         fprintf( stderr, "ScannerManager::configureScanner\n" );
     284             : #endif
     285             : 
     286           0 :         if( scanner_context.InternalData < 0 || (sal_uLong)scanner_context.InternalData >= rSanes.size() )
     287             :             throw ScannerException(
     288             :                 "Scanner does not exist",
     289             :                 Reference< XScannerManager >( this ),
     290             :                 ScanError_InvalidContext
     291           0 :             );
     292             : 
     293           0 :         boost::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];
     294           0 :         if( pHolder->m_bBusy )
     295             :             throw ScannerException(
     296             :                 "Scanner is busy",
     297             :                 Reference< XScannerManager >( this ),
     298             :                 ScanError_ScanInProgress
     299           0 :             );
     300             : 
     301           0 :         pHolder->m_bBusy = true;
     302           0 :         ScopedVclPtrInstance< SaneDlg > aDlg(nullptr, pHolder->m_aSane, listener.is());
     303           0 :         bRet = aDlg->Execute();
     304           0 :         bScan = aDlg->getDoScan();
     305           0 :         pHolder->m_bBusy = false;
     306             :     }
     307           0 :     if ( bScan )
     308           0 :         startScan( scanner_context, listener );
     309             : 
     310           0 :     return bRet;
     311             : }
     312             : 
     313             : 
     314             : 
     315           0 : void ScannerManager::startScan( const ScannerContext& scanner_context,
     316             :                                 const Reference< com::sun::star::lang::XEventListener >& listener ) throw( ScannerException, std::exception )
     317             : {
     318           0 :     osl::MutexGuard aGuard( theSaneProtector::get() );
     319           0 :     sanevec &rSanes = theSanes::get().m_aSanes;
     320             : 
     321             : #if OSL_DEBUG_LEVEL > 1
     322             :     fprintf( stderr, "ScannerManager::startScan\n" );
     323             : #endif
     324             : 
     325           0 :     if( scanner_context.InternalData < 0 || (sal_uLong)scanner_context.InternalData >= rSanes.size() )
     326             :         throw ScannerException(
     327             :             "Scanner does not exist",
     328             :             Reference< XScannerManager >( this ),
     329             :             ScanError_InvalidContext
     330           0 :             );
     331           0 :     boost::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];
     332           0 :     if( pHolder->m_bBusy )
     333             :         throw ScannerException(
     334             :             "Scanner is busy",
     335             :             Reference< XScannerManager >( this ),
     336             :             ScanError_ScanInProgress
     337           0 :             );
     338           0 :     pHolder->m_bBusy = true;
     339             : 
     340           0 :     ScannerThread* pThread = new ScannerThread( pHolder, listener, this );
     341           0 :     pThread->create();
     342           0 : }
     343             : 
     344             : 
     345             : 
     346           0 : ScanError ScannerManager::getError( const ScannerContext& scanner_context ) throw( ScannerException, std::exception )
     347             : {
     348           0 :     osl::MutexGuard aGuard( theSaneProtector::get() );
     349           0 :     sanevec &rSanes = theSanes::get().m_aSanes;
     350             : 
     351           0 :     if( scanner_context.InternalData < 0 || (sal_uLong)scanner_context.InternalData >= rSanes.size() )
     352             :         throw ScannerException(
     353             :             "Scanner does not exist",
     354             :             Reference< XScannerManager >( this ),
     355             :             ScanError_InvalidContext
     356           0 :             );
     357             : 
     358           0 :     boost::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];
     359             : 
     360           0 :     return pHolder->m_nError;
     361             : }
     362             : 
     363             : 
     364             : 
     365           0 : Reference< css::awt::XBitmap > ScannerManager::getBitmap( const ScannerContext& scanner_context ) throw( ScannerException, std::exception )
     366             : {
     367           0 :     osl::MutexGuard aGuard( theSaneProtector::get() );
     368           0 :     sanevec &rSanes = theSanes::get().m_aSanes;
     369             : 
     370           0 :     if( scanner_context.InternalData < 0 || (sal_uLong)scanner_context.InternalData >= rSanes.size() )
     371             :         throw ScannerException(
     372             :             "Scanner does not exist",
     373             :             Reference< XScannerManager >( this ),
     374             :             ScanError_InvalidContext
     375           0 :             );
     376           0 :     boost::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];
     377             : 
     378           0 :     osl::MutexGuard aProtGuard( pHolder->m_aProtector );
     379             : 
     380           0 :     Reference< css::awt::XBitmap > xRet( pHolder->m_xBitmap );
     381           0 :     pHolder->m_xBitmap = Reference< css::awt::XBitmap >();
     382             : 
     383           0 :     return xRet;
     384          42 : }
     385             : 
     386             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11