/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. ProcessT::ProcessT( QObject *parent ) :
  3. QThread( parent )
  4. {
  5. m_ChanMode = QProcess::SeparateChannels;
  6. }
  7. ProcessT::~ProcessT() {
  8. wait();
  9. qDebug() << Q_FUNC_INFO << "Thread destroyed";
  10. }
  11. void ProcessT::run() {
  12. if ( m_Command.isEmpty() ) {
  13. qWarning() << Q_FUNC_INFO << "No command set, doing nothing";
  14. return;
  15. }
  16. qDebug() << Q_FUNC_INFO << "Executing command" << m_Command << m_Args << "\n";
  17. QProcess proc;
  18. proc.setProcessChannelMode( m_ChanMode );
  19. proc.start( m_Command, m_Args );
  20. if (!proc.waitForStarted()) {
  21. qDebug()<< QString("Unable to launch application %1").arg(m_Command);
  22. }else{
  23. proc.waitForFinished(-1);
  24. proc.closeWriteChannel();
  25. m_Output = proc.readAll().trimmed();
  26. qDebug() << QString("Exit code %1").arg(proc.exitCode());
  27. emit commandOutput(proc.exitCode(), m_Output );
  28. qDebug() << Q_FUNC_INFO << "Command execution finished.\n" <<m_Output;
  29. }
  30. }
  31. void ProcessT::setCommand( const QString &name, const QStringList &args, const QProcess::ProcessChannelMode &mode ) {
  32. if ( name.isEmpty() ) {
  33. qWarning() << Q_FUNC_INFO << "Empty command given, doing nothing";
  34. return;
  35. }
  36. m_Command = name;
  37. m_Args = args;
  38. m_ChanMode = mode;
  39. }
  40. void ProcessT::execute( const QString &name, const QStringList &args, const QProcess::ProcessChannelMode &mode ) {
  41. setCommand( name, args, mode );
  42. if ( !isRunning() ) {
  43. start();
  44. }
  45. else {
  46. qWarning() << Q_FUNC_INFO << "Thread already running, doing nothing.";
  47. return;
  48. }
  49. }