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