/Users/ericb/Desktop/NATIVEPRINTDLG01/extensions/source/scanner/scanunx.cxx

Go to the documentation of this file.
00001 /*************************************************************************
00002  *
00003  *  OpenOffice.org - a multi-platform office productivity suite
00004  *
00005  *  $RCSfile: scanunx.cxx,v $
00006  *
00007  *  $Revision: 1.9 $
00008  *
00009  *  last change: $Author: ihi $ $Date: 2008/01/14 15:03:52 $
00010  *
00011  *  The Contents of this file are made available subject to
00012  *  the terms of GNU Lesser General Public License Version 2.1.
00013  *
00014  *
00015  *    GNU Lesser General Public License Version 2.1
00016  *    =============================================
00017  *    Copyright 2005 by Sun Microsystems, Inc.
00018  *    901 San Antonio Road, Palo Alto, CA 94303, USA
00019  *
00020  *    This library is free software; you can redistribute it and/or
00021  *    modify it under the terms of the GNU Lesser General Public
00022  *    License version 2.1, as published by the Free Software Foundation.
00023  *
00024  *    This library is distributed in the hope that it will be useful,
00025  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00026  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027  *    Lesser General Public License for more details.
00028  *
00029  *    You should have received a copy of the GNU Lesser General Public
00030  *    License along with this library; if not, write to the Free Software
00031  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00032  *    MA  02111-1307  USA
00033  *
00034  ************************************************************************/
00035 
00036 // MARKER(update_precomp.py): autogen include statement, do not remove
00037 #include "precompiled_extensions.hxx"
00038 #include <scanner.hxx>
00039 #include <sanedlg.hxx>
00040 #include <vos/thread.hxx>
00041 #include <tools/list.hxx>
00042 
00043 #if OSL_DEBUG_LEVEL > 1
00044 #include <stdio.h>
00045 #endif
00046 
00047 BitmapTransporter::BitmapTransporter()
00048 {
00049 #if OSL_DEBUG_LEVEL > 1
00050     fprintf( stderr, "BitmapTransporter\n" );
00051 #endif
00052 }
00053 
00054 BitmapTransporter::~BitmapTransporter()
00055 {
00056 #if OSL_DEBUG_LEVEL > 1
00057     fprintf( stderr, "~BitmapTransporter\n" );
00058 #endif
00059 }
00060 
00061 // -----------------------------------------------------------------------------
00062 
00063 ANY SAL_CALL BitmapTransporter::queryInterface( const Type& rType ) throw( RuntimeException )
00064 {
00065         const ANY aRet( cppu::queryInterface( rType, static_cast< AWT::XBitmap* >( this ) ) );
00066 
00067         return( aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ) );
00068 }
00069 
00070 // -----------------------------------------------------------------------------
00071 
00072 AWT::Size BitmapTransporter::getSize() throw()
00073 {
00074         vos::OGuard     aGuard( m_aProtector );
00075         int                     nPreviousPos = m_aStream.Tell();
00076         AWT::Size       aRet;
00077 
00078         // ensure that there is at least a header
00079         m_aStream.Seek( STREAM_SEEK_TO_END );
00080         int nLen = m_aStream.Tell();
00081         if( nLen > 15 )
00082         {
00083                 m_aStream.Seek( 4 );
00084                 m_aStream >> aRet.Width >> aRet.Height;
00085         }
00086         else
00087                 aRet.Width = aRet.Height = 0;
00088 
00089         m_aStream.Seek( nPreviousPos );
00090 
00091         return aRet;
00092 }
00093 
00094 // -----------------------------------------------------------------------------
00095 
00096 SEQ( sal_Int8 ) BitmapTransporter::getDIB() throw()
00097 {
00098         vos::OGuard aGuard( m_aProtector );
00099         int                     nPreviousPos = m_aStream.Tell();
00100 
00101         // create return value
00102         m_aStream.Seek( STREAM_SEEK_TO_END );
00103         int nBytes = m_aStream.Tell();
00104         m_aStream.Seek( 0 );
00105 
00106         SEQ( sal_Int8 ) aValue( nBytes );
00107         m_aStream.Read( aValue.getArray(), nBytes );
00108         m_aStream.Seek( nPreviousPos );
00109 
00110         return aValue;
00111 }
00112 
00113 // --------------
00114 // - SaneHolder -
00115 // --------------
00116 
00117 struct SaneHolder
00118 {
00119         Sane                            m_aSane;
00120         REF( AWT::XBitmap )     m_xBitmap;
00121         vos::OMutex                     m_aProtector;
00122         ScanError                       m_nError;
00123     bool                                m_bBusy;
00124 };
00125 
00126 DECLARE_LIST( SaneHolderList, SaneHolder* )
00127 
00128 static SaneHolderList   allSanes;
00129 static vos::OMutex              aSaneProtector;
00130 
00131 // -----------------
00132 // - ScannerThread -
00133 // -----------------
00134 
00135 class ScannerThread : public vos::OThread
00136 {
00137         SaneHolder*                                                                     m_pHolder;
00138         REF( com::sun::star::lang::XEventListener )     m_xListener;
00139         ScannerManager*                                                         m_pManager; // just for the disposing call
00140 
00141 public:
00142         virtual void run();
00143         virtual void onTerminated() { delete this; }
00144 public:
00145         ScannerThread( SaneHolder* pHolder,
00146                                    const REF( com::sun::star::lang::XEventListener )& listener,
00147                                    ScannerManager* pManager );
00148         virtual ~ScannerThread();
00149 };
00150 
00151 // -----------------------------------------------------------------------------
00152 
00153 ScannerThread::ScannerThread(
00154                              SaneHolder* pHolder,
00155                              const REF( com::sun::star::lang::XEventListener )& listener,
00156                              ScannerManager* pManager )
00157         : m_pHolder( pHolder ), m_xListener( listener ), m_pManager( pManager )
00158 {
00159 #if OSL_DEBUG_LEVEL > 1
00160     fprintf( stderr, "ScannerThread\n" );
00161 #endif
00162 }
00163 
00164 ScannerThread::~ScannerThread()
00165 {
00166 #if OSL_DEBUG_LEVEL > 1
00167     fprintf( stderr, "~ScannerThread\n" );
00168 #endif
00169 }
00170 
00171 void ScannerThread::run()
00172 {
00173         vos::OGuard                     aGuard( m_pHolder->m_aProtector );
00174     BitmapTransporter*  pTransporter = new BitmapTransporter;
00175         REF( XInterface )       aIf( static_cast< OWeakObject* >( pTransporter ) );
00176 
00177         m_pHolder->m_xBitmap = REF( AWT::XBitmap )( aIf, UNO_QUERY );
00178 
00179     m_pHolder->m_bBusy = true;
00180         if( m_pHolder->m_aSane.IsOpen() )
00181         {
00182                 int nOption = m_pHolder->m_aSane.GetOptionByName( "preview" );
00183                 if( nOption != -1 )
00184                         m_pHolder->m_aSane.SetOptionValue( nOption, (BOOL)FALSE );
00185 
00186                 m_pHolder->m_nError =
00187                         m_pHolder->m_aSane.Start( *pTransporter ) ?
00188                         ScanError_ScanErrorNone : ScanError_ScanCanceled;
00189         }
00190         else
00191                 m_pHolder->m_nError = ScanError_ScannerNotAvailable;
00192 
00193 
00194     REF( XInterface ) xXInterface( static_cast< OWeakObject* >( m_pManager ) );
00195         m_xListener->disposing( com::sun::star::lang::EventObject(xXInterface) );
00196     m_pHolder->m_bBusy = false;
00197 }
00198 
00199 // ------------------
00200 // - ScannerManager -
00201 // ------------------
00202 
00203 void ScannerManager::DestroyData()
00204 {
00205         // unused
00206 }
00207 
00208 // -----------------------------------------------------------------------------
00209 
00210 AWT::Size ScannerManager::getSize() throw()
00211 {
00212         AWT::Size aRet;
00213         aRet.Width = aRet.Height = 0;
00214         return aRet;
00215 }
00216 
00217 // -----------------------------------------------------------------------------
00218 
00219 SEQ( sal_Int8 ) ScannerManager::getDIB() throw()
00220 {
00221         return SEQ( sal_Int8 )();
00222 }
00223 
00224 // -----------------------------------------------------------------------------
00225 
00226 SEQ( ScannerContext ) ScannerManager::getAvailableScanners() throw()
00227 {
00228         vos::OGuard aGuard( aSaneProtector );
00229 
00230         if( ! allSanes.Count() )
00231         {
00232                 SaneHolder* pSaneHolder = new SaneHolder;
00233                 pSaneHolder->m_nError = ScanError_ScanErrorNone;
00234         pSaneHolder->m_bBusy = false;
00235                 if( Sane::IsSane() )
00236                         allSanes.Insert( pSaneHolder );
00237                 else
00238                         delete pSaneHolder;
00239         }
00240 
00241         if( Sane::IsSane() )
00242         {
00243                 SEQ( ScannerContext ) aRet(1);
00244                 aRet.getArray()[0].ScannerName          = ::rtl::OUString::createFromAscii( "SANE" );
00245                 aRet.getArray()[0].InternalData         = 0;
00246                 return aRet;
00247         }
00248 
00249         return SEQ( ScannerContext )();
00250 }
00251 
00252 // -----------------------------------------------------------------------------
00253 
00254 BOOL ScannerManager::configureScanner( ScannerContext& scanner_context ) throw( ScannerException )
00255 {
00256         vos::OGuard aGuard( aSaneProtector );
00257 
00258 #if OSL_DEBUG_LEVEL > 1
00259     fprintf( stderr, "ScannerManager::configureScanner\n" );
00260 #endif
00261 
00262         if( scanner_context.InternalData < 0 || (ULONG)scanner_context.InternalData >= allSanes.Count() )
00263                 throw ScannerException(
00264                         ::rtl::OUString::createFromAscii( "Scanner does not exist" ),
00265                         REF( XScannerManager )( this ),
00266                         ScanError_InvalidContext
00267                         );
00268 
00269     SaneHolder* pHolder = allSanes.GetObject( scanner_context.InternalData );
00270     if( pHolder->m_bBusy )
00271                 throw ScannerException(
00272                         ::rtl::OUString::createFromAscii( "Scanner is busy" ),
00273                         REF( XScannerManager )( this ),
00274                         ScanError_ScanInProgress
00275                         );
00276 
00277     pHolder->m_bBusy = true;
00278         SaneDlg aDlg( NULL, pHolder->m_aSane );
00279     BOOL bRet = (BOOL)aDlg.Execute();
00280     pHolder->m_bBusy = false;
00281         
00282         return bRet;
00283 }
00284 
00285 // -----------------------------------------------------------------------------
00286 
00287 void ScannerManager::startScan( const ScannerContext& scanner_context,
00288                                                                 const REF( com::sun::star::lang::XEventListener )& listener ) throw( ScannerException )
00289 {
00290         vos::OGuard aGuard( aSaneProtector );
00291 
00292 #if OSL_DEBUG_LEVEL > 1
00293     fprintf( stderr, "ScannerManager::startScan\n" );
00294 #endif
00295 
00296         if( scanner_context.InternalData < 0 || (ULONG)scanner_context.InternalData >= allSanes.Count() )
00297                 throw ScannerException(
00298                         ::rtl::OUString::createFromAscii( "Scanner does not exist" ),
00299                         REF( XScannerManager )( this ),
00300                         ScanError_InvalidContext
00301                         );
00302         SaneHolder* pHolder = allSanes.GetObject( scanner_context.InternalData );
00303     if( pHolder->m_bBusy )
00304                 throw ScannerException(
00305                         ::rtl::OUString::createFromAscii( "Scanner is busy" ),
00306                         REF( XScannerManager )( this ),
00307                         ScanError_ScanInProgress
00308                         );
00309     pHolder->m_bBusy = true;
00310 
00311         ScannerThread* pThread = new ScannerThread( pHolder, listener, this );
00312         pThread->create();
00313 }
00314 
00315 // -----------------------------------------------------------------------------
00316 
00317 ScanError ScannerManager::getError( const ScannerContext& scanner_context ) throw( ScannerException )
00318 {
00319         vos::OGuard aGuard( aSaneProtector );
00320 
00321         if( scanner_context.InternalData < 0 || (ULONG)scanner_context.InternalData >= allSanes.Count() )
00322                 throw ScannerException(
00323                         ::rtl::OUString::createFromAscii( "Scanner does not exist" ),
00324                         REF( XScannerManager )( this ),
00325                         ScanError_InvalidContext
00326                         );
00327 
00328         SaneHolder* pHolder = allSanes.GetObject( scanner_context.InternalData );
00329 
00330         return pHolder->m_nError;
00331 }
00332 
00333 // -----------------------------------------------------------------------------
00334 
00335 REF( AWT::XBitmap ) ScannerManager::getBitmap( const ScannerContext& scanner_context ) throw( ScannerException )
00336 {
00337         vos::OGuard aGuard( aSaneProtector );
00338 
00339         if( scanner_context.InternalData < 0 || (ULONG)scanner_context.InternalData >= allSanes.Count() )
00340                 throw ScannerException(
00341                         ::rtl::OUString::createFromAscii( "Scanner does not exist" ),
00342                         REF( XScannerManager )( this ),
00343                         ScanError_InvalidContext
00344                         );
00345         SaneHolder* pHolder = allSanes.GetObject( scanner_context.InternalData );
00346 
00347         vos::OGuard aProtGuard( pHolder->m_aProtector );
00348 
00349         REF( AWT::XBitmap ) xRet( pHolder->m_xBitmap );
00350         pHolder->m_xBitmap = REF( AWT::XBitmap )();
00351 
00352         return xRet;
00353 }

Generated on Thu Feb 28 17:53:26 2008 for AquaScanner by  doxygen 1.5.1