application.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "application.h"
  2. #include <QProcess>
  3. #include <QDebug>
  4. #include <QFile>
  5. #include <QTextStream>
  6. Application::Application(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 Application::appName() const
  21. {
  22. return m_AppName;
  23. }
  24. void Application::setAppName(const QString &appName)
  25. {
  26. m_AppName = appName;
  27. }
  28. QString Application::outFName() const
  29. {
  30. return m_outFName;
  31. }
  32. void Application::setoutFName(const QString &outFName)
  33. {
  34. m_outFName = outFName;
  35. }
  36. QString Application::stdERR() const
  37. {
  38. return m_stdERR;
  39. }
  40. void Application::setstdERR(const QString &stdERR)
  41. {
  42. m_stdERR = stdERR;
  43. }
  44. QString Application::stdOUT() const
  45. {
  46. return m_stdOUT;
  47. }
  48. void Application::setstdOUT(const QString &stdOUT)
  49. {
  50. m_stdOUT = stdOUT;
  51. }
  52. int Application::exitCode()
  53. {
  54. return m_exitCode;
  55. }
  56. int Application::exitStatus()
  57. {
  58. return m_exitStatus;
  59. }
  60. int Application::exitError()
  61. {
  62. return m_error;
  63. }
  64. QString Application::arguments() const
  65. {
  66. return m_Arguments;
  67. }
  68. void Application::setArguments(const QString &arguments)
  69. {
  70. m_Arguments = arguments;
  71. }
  72. void Application::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 << "\n with the Argument of \n" + m_Arguments ;
  81. }
  82. void Application::finished(int exitCode, QProcess::ExitStatus status)
  83. {
  84. m_stdOUT = QString(m_process->readAllStandardOutput());
  85. m_stdERR = QString(m_process->readAllStandardError());
  86. m_exitCode = exitCode;
  87. m_exitStatus = status;
  88. if(m_outFName.length() > 0) {
  89. QFile outfile(m_outFName);
  90. if(outfile.open(QIODevice::ReadWrite|QFile::Truncate)){
  91. QTextStream stream (&outfile);
  92. stream << m_stdOUT;
  93. outfile.close();
  94. }
  95. }
  96. emit this->appFinished();
  97. }
  98. void Application::process_started(void)
  99. {
  100. emit this->appStarted();
  101. }
  102. void Application::process_error(QProcess::ProcessError app_error)
  103. {
  104. m_error = app_error;
  105. emit this->appError();
  106. }