tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / io / source / stm / opump.cxx
blobfebd86b263335f7e3ce8cc1268cb099b0ccdb50f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <sal/log.hxx>
23 #include <com/sun/star/io/IOException.hpp>
24 #include <com/sun/star/io/NotConnectedException.hpp>
25 #include <com/sun/star/io/XActiveDataSource.hpp>
26 #include <com/sun/star/io/XActiveDataSink.hpp>
27 #include <com/sun/star/io/XActiveDataControl.hpp>
28 #include <com/sun/star/io/XConnectable.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
32 #include <cppuhelper/implbase.hxx>
33 #include <comphelper/interfacecontainer4.hxx>
34 #include <cppuhelper/supportsservice.hxx>
35 #include <osl/thread.h>
36 #include <mutex>
38 using namespace cppu;
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::lang;
41 using namespace com::sun::star::io;
43 namespace io_stm {
45 namespace {
47 class Pump : public WeakImplHelper<
48 XActiveDataSource, XActiveDataSink, XActiveDataControl, XConnectable, XServiceInfo >
50 std::mutex m_aMutex;
51 oslThread m_aThread;
53 Reference< XConnectable > m_xPred;
54 Reference< XConnectable > m_xSucc;
55 Reference< XInputStream > m_xInput;
56 Reference< XOutputStream > m_xOutput;
57 comphelper::OInterfaceContainerHelper4<XStreamListener> m_cnt;
58 bool m_closeFired;
60 void run();
61 static void static_run( void* pObject );
63 void close();
64 void fireClose();
65 void fireStarted();
66 void fireTerminated();
67 void fireError( const Any &a );
69 public:
70 Pump();
71 virtual ~Pump() override;
73 // XActiveDataSource
74 virtual void SAL_CALL setOutputStream( const Reference< css::io::XOutputStream >& xOutput ) override;
75 virtual Reference< css::io::XOutputStream > SAL_CALL getOutputStream() override;
77 // XActiveDataSink
78 virtual void SAL_CALL setInputStream( const Reference< css::io::XInputStream >& xStream ) override;
79 virtual Reference< css::io::XInputStream > SAL_CALL getInputStream() override;
81 // XActiveDataControl
82 virtual void SAL_CALL addListener( const Reference< css::io::XStreamListener >& xListener ) override;
83 virtual void SAL_CALL removeListener( const Reference< css::io::XStreamListener >& xListener ) override;
84 virtual void SAL_CALL start() override;
85 virtual void SAL_CALL terminate() override;
87 // XConnectable
88 virtual void SAL_CALL setPredecessor( const Reference< css::io::XConnectable >& xPred ) override;
89 virtual Reference< css::io::XConnectable > SAL_CALL getPredecessor() override;
90 virtual void SAL_CALL setSuccessor( const Reference< css::io::XConnectable >& xSucc ) override;
91 virtual Reference< css::io::XConnectable > SAL_CALL getSuccessor() override;
93 public: // XServiceInfo
94 virtual OUString SAL_CALL getImplementationName() override;
95 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
96 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
101 Pump::Pump() : m_aThread( nullptr ),
102 m_closeFired( false )
106 Pump::~Pump()
108 // exit gracefully
109 if( m_aThread )
111 osl_joinWithThread( m_aThread );
112 osl_destroyThread( m_aThread );
116 void Pump::fireError( const Any & exception )
118 std::unique_lock guard( m_aMutex );
119 comphelper::OInterfaceIteratorHelper4<XStreamListener> iter( guard, m_cnt );
120 guard.unlock();
121 while( iter.hasMoreElements() )
125 iter.next()->error( exception );
127 catch ( const RuntimeException &e )
129 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e);
134 void Pump::fireClose()
136 bool bFire = false;
138 std::unique_lock guard( m_aMutex );
139 if( ! m_closeFired )
141 m_closeFired = true;
142 bFire = true;
146 if( !bFire )
147 return;
149 std::unique_lock guard( m_aMutex );
150 comphelper::OInterfaceIteratorHelper4<XStreamListener> iter( guard, m_cnt );
151 guard.unlock();
152 while( iter.hasMoreElements() )
156 iter.next()->closed( );
158 catch ( const RuntimeException &e )
160 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e);
165 void Pump::fireStarted()
167 std::unique_lock guard( m_aMutex );
168 comphelper::OInterfaceIteratorHelper4<XStreamListener> iter( guard, m_cnt );
169 guard.unlock();
170 while( iter.hasMoreElements() )
174 iter.next()->started( );
176 catch ( const RuntimeException &e )
178 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e);
183 void Pump::fireTerminated()
185 std::unique_lock guard( m_aMutex );
186 comphelper::OInterfaceIteratorHelper4<XStreamListener> iter( guard, m_cnt );
187 guard.unlock();
188 while( iter.hasMoreElements() )
192 iter.next()->terminated();
194 catch ( const RuntimeException &e )
196 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e);
202 void Pump::close()
204 // close streams and release references
205 Reference< XInputStream > rInput;
206 Reference< XOutputStream > rOutput;
208 std::unique_lock guard( m_aMutex );
209 rInput = m_xInput;
210 m_xInput.clear();
212 rOutput = m_xOutput;
213 m_xOutput.clear();
214 m_xSucc.clear();
215 m_xPred.clear();
217 if( rInput.is() )
221 rInput->closeInput();
223 catch( Exception & )
225 // go down calm
228 if( rOutput.is() )
232 rOutput->closeOutput();
234 catch( Exception & )
236 // go down calm
241 void Pump::static_run( void* pObject )
243 osl_setThreadName("io_stm::Pump::run()");
244 static_cast<Pump*>(pObject)->run();
245 static_cast<Pump*>(pObject)->release();
248 void Pump::run()
252 fireStarted();
255 Reference< XInputStream > rInput;
256 Reference< XOutputStream > rOutput;
258 std::unique_lock aGuard( m_aMutex );
259 rInput = m_xInput;
260 rOutput = m_xOutput;
263 if( ! rInput.is() )
265 throw NotConnectedException( u"no input stream set"_ustr, getXWeak() );
267 Sequence< sal_Int8 > aData;
268 while( rInput->readSomeBytes( aData, 65536 ) )
270 if( ! rOutput.is() )
272 throw NotConnectedException( u"no output stream set"_ustr, getXWeak() );
274 rOutput->writeBytes( aData );
275 osl_yieldThread();
278 catch ( const IOException & e )
280 fireError( Any( e ) );
282 catch ( const RuntimeException & e )
284 fireError( Any( e ) );
286 catch ( const Exception & e )
288 fireError( Any( e ) );
291 close();
292 fireClose();
294 catch ( const css::uno::Exception &e )
296 // we are the last on the stack.
297 // this is to avoid crashing the program, when e.g. a bridge crashes
298 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e);
304 * XConnectable
307 void Pump::setPredecessor( const Reference< XConnectable >& xPred )
309 std::unique_lock aGuard( m_aMutex );
310 m_xPred = xPred;
314 Reference< XConnectable > Pump::getPredecessor()
316 std::unique_lock aGuard( m_aMutex );
317 return m_xPred;
321 void Pump::setSuccessor( const Reference< XConnectable >& xSucc )
323 std::unique_lock aGuard( m_aMutex );
324 m_xSucc = xSucc;
328 Reference< XConnectable > Pump::getSuccessor()
330 std::unique_lock aGuard( m_aMutex );
331 return m_xSucc;
336 * XActiveDataControl
339 void Pump::addListener( const Reference< XStreamListener >& xListener )
341 std::unique_lock aGuard( m_aMutex );
342 m_cnt.addInterface( aGuard, xListener );
346 void Pump::removeListener( const Reference< XStreamListener >& xListener )
348 std::unique_lock aGuard( m_aMutex );
349 m_cnt.removeInterface( aGuard, xListener );
353 void Pump::start()
355 std::unique_lock aGuard( m_aMutex );
356 m_aThread = osl_createSuspendedThread(Pump::static_run,this);
357 if( !m_aThread )
359 throw RuntimeException(
360 u"Pump::start Couldn't create worker thread"_ustr,
361 *this);
364 // will be released by OPump::static_run
365 acquire();
366 osl_resumeThread( m_aThread );
371 void Pump::terminate()
373 close();
375 // wait for the worker to die
376 if( m_aThread )
377 osl_joinWithThread( m_aThread );
379 fireTerminated();
380 fireClose();
385 * XActiveDataSink
388 void Pump::setInputStream( const Reference< XInputStream >& xStream )
390 std::unique_lock aGuard( m_aMutex );
391 m_xInput = xStream;
392 Reference< XConnectable > xConnect( xStream, UNO_QUERY );
393 if( xConnect.is() )
394 xConnect->setSuccessor( this );
395 // data transfer starts in XActiveDataControl::start
399 Reference< XInputStream > Pump::getInputStream()
401 std::unique_lock aGuard( m_aMutex );
402 return m_xInput;
407 * XActiveDataSource
410 void Pump::setOutputStream( const Reference< XOutputStream >& xOut )
412 std::unique_lock aGuard( m_aMutex );
413 m_xOutput = xOut;
414 Reference< XConnectable > xConnect( xOut, UNO_QUERY );
415 if( xConnect.is() )
416 xConnect->setPredecessor( this );
417 // data transfer starts in XActiveDataControl::start
420 Reference< XOutputStream > Pump::getOutputStream()
422 std::unique_lock aGuard( m_aMutex );
423 return m_xOutput;
426 // XServiceInfo
427 OUString Pump::getImplementationName()
429 return u"com.sun.star.comp.io.Pump"_ustr;
432 // XServiceInfo
433 sal_Bool Pump::supportsService(const OUString& ServiceName)
435 return cppu::supportsService(this, ServiceName);
438 // XServiceInfo
439 Sequence< OUString > Pump::getSupportedServiceNames()
441 return { u"com.sun.star.io.Pump"_ustr };
446 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
447 io_Pump_get_implementation(
448 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
450 return cppu::acquire(new io_stm::Pump());
454 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */