application.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "application.h"
  2. #include <QProcess>
  3. #include <QDebug>
  4. #include <QFile>
  5. #include <QTextStream>
  6. ApplicationLaunch::ApplicationLaunch(QObject *parent) :
  7. QObject(parent),
  8. m_process(new QProcess(this))
  9. {
  10. connect(m_process,
  11. SIGNAL(finished(int, QProcess::ExitStatus)),
  12. SLOT(finished(int, QProcess::ExitStatus)));
  13. connect(m_process,
  14. SIGNAL(started()),
  15. SLOT(process_started()));
  16. connect(m_process,
  17. SIGNAL(errorOccurred(QProcess::ProcessError)),
  18. SLOT(process_error(QProcess::ProcessError)));
  19. }
  20. QString ApplicationLaunch::appName() const
  21. {
  22. return m_AppName;
  23. }
  24. void ApplicationLaunch::setAppName(const QString &appName)
  25. {
  26. m_AppName = appName;
  27. }
  28. QString ApplicationLaunch::outFName() const
  29. {
  30. return m_outFName;
  31. }
  32. void ApplicationLaunch::setoutFName(const QString &outFName)
  33. {
  34. m_outFName = outFName;
  35. }
  36. QString ApplicationLaunch::stdERR() const
  37. {
  38. return m_stdERR;
  39. }
  40. void ApplicationLaunch::setstdERR(const QString &stdERR)
  41. {
  42. m_stdERR = stdERR;
  43. }
  44. QString ApplicationLaunch::stdOUT() const
  45. {
  46. return m_stdOUT;
  47. }
  48. void ApplicationLaunch::setstdOUT(const QString &stdOUT)
  49. {
  50. m_stdOUT = stdOUT;
  51. }
  52. int ApplicationLaunch::exitCode()
  53. {
  54. return m_exitCode;
  55. }
  56. int ApplicationLaunch::exitStatus()
  57. {
  58. return m_exitStatus;
  59. }
  60. int ApplicationLaunch::exitError()
  61. {
  62. return m_error;
  63. }
  64. QString ApplicationLaunch::arguments() const
  65. {
  66. return m_Arguments;
  67. }
  68. void ApplicationLaunch::setArguments(const QString &arguments)
  69. {
  70. m_Arguments = arguments;
  71. }
  72. void ApplicationLaunch::launchScript()
  73. {
  74. m_stdERR.clear();
  75. m_stdOUT.clear();
  76. m_exitCode = -1;
  77. m_exitStatus = -1;
  78. m_error = -1;
  79. m_process->start(m_AppName + " " + m_Arguments);
  80. qDebug() << "launching application ::" << m_AppName ;
  81. qDebug() << "with the Argument of";
  82. qDebug() << m_Arguments ;
  83. }
  84. void ApplicationLaunch::finished(int exitCode, QProcess::ExitStatus status)
  85. {
  86. m_stdOUT = QString(m_process->readAllStandardOutput());
  87. m_stdERR = QString(m_process->readAllStandardError());
  88. m_exitCode = exitCode;
  89. m_exitStatus = status;
  90. if(m_outFName.length() > 0) {
  91. QFile outfile(m_outFName);
  92. if(outfile.open(QIODevice::ReadWrite|QFile::Truncate)){
  93. QTextStream stream (&outfile);
  94. stream << m_stdOUT;
  95. outfile.close();
  96. }
  97. }
  98. emit this->appFinished();
  99. }
  100. void ApplicationLaunch::process_started(void)
  101. {
  102. emit this->appStarted();
  103. }
  104. void ApplicationLaunch::process_error(QProcess::ProcessError app_error)
  105. {
  106. m_error = app_error;
  107. emit this->appError();
  108. }