PageRenderTime 19ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/safeFatPrinter/branch/src/proc_thread.cpp

http://cupsfilter.googlecode.com/
C++ | 67 lines | 49 code | 18 blank | 0 comment | 5 complexity | 235644b2239cd5bd1cb5d3c1406f5719 MD5 | raw file
 1#include "proc_thread.h"
 2
 3
 4ProcessT::ProcessT( QObject *parent ) :
 5	QThread( parent )
 6{
 7	m_ChanMode = QProcess::SeparateChannels;
 8}
 9
10ProcessT::~ProcessT() {
11
12    wait();
13    qDebug() << Q_FUNC_INFO << "Thread destroyed";
14}
15
16void ProcessT::run() {
17
18
19	if ( m_Command.isEmpty() ) {
20		qWarning() << Q_FUNC_INFO << "No command set, doing nothing";
21		return;
22	}
23
24	qDebug() << Q_FUNC_INFO << "Executing command" << m_Command << m_Args << "\n";
25
26	QProcess proc;
27
28	proc.setProcessChannelMode( m_ChanMode );
29	proc.start( m_Command, m_Args );
30	if (!proc.waitForStarted()) {
31	    qDebug()<< QString("Unable to launch application %1").arg(m_Command);
32	}else{
33	    proc.waitForFinished(-1);
34	    proc.closeWriteChannel();
35	    m_Output = proc.readAll().trimmed();
36	    qDebug() << QString("Exit code %1").arg(proc.exitCode());
37
38	    emit commandOutput(proc.exitCode(), m_Output );
39	    qDebug() << Q_FUNC_INFO << "Command execution finished.\n" <<m_Output;
40	}
41}
42
43void ProcessT::setCommand( const QString &name, const QStringList &args, const QProcess::ProcessChannelMode &mode ) {
44
45	if ( name.isEmpty() ) {
46		qWarning() << Q_FUNC_INFO << "Empty command given, doing nothing";
47		return;
48	}
49	m_Command = name;
50	m_Args = args;
51	m_ChanMode = mode;
52}
53
54void ProcessT::execute( const QString &name, const QStringList &args, const QProcess::ProcessChannelMode &mode ) {
55
56	setCommand( name, args, mode );
57	
58	if ( !isRunning() ) {
59		start();
60	}
61	else {
62		qWarning() << Q_FUNC_INFO << "Thread already running, doing nothing.";
63		return;
64	}
65
66}
67