ExProcess.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("../QtExamples.js");
  20. /**
  21. * \ingroup ecma_misc_examples_qtexamples
  22. * \class ExProcess
  23. * This action runs an external process
  24. */
  25. function ExProcess(guiAction) {
  26. QtExamples.call(this, guiAction);
  27. }
  28. ExProcess.prototype = new QtExamples();
  29. ExProcess.includeBasePath = includeBasePath;
  30. ExProcess.prototype.beginEvent = function() {
  31. QtExamples.prototype.beginEvent.call(this);
  32. var i;
  33. // application binary:
  34. var program = "ls";
  35. // arguments:
  36. var args = [ "-la" ];
  37. // additional environment variables:
  38. var environment = [ ["MYENVVAR", "1"] ];
  39. var process = new QProcess();
  40. // error handling:
  41. if (RSettings.getQtVersion() >= 0x050000) {
  42. process.errorOccurred.connect(function(e) { qDebug("Error: ", e); });
  43. }
  44. else {
  45. process.error.connect(function(e) { qDebug("Error: ", e); });
  46. }
  47. // buffer stdout, stderr for later output:
  48. var stdout = "";
  49. var stderr = "";
  50. process.readyReadStandardOutput.connect(function() { stdout += process.readAllStandardOutput(); });
  51. process.readyReadStandardError.connect(function() { stderr += process.readAllStandardError(); });
  52. // set enviromnet to current environment, add custom environment:
  53. if (environment!==undefined) {
  54. var env = QProcessEnvironment.systemEnvironment();
  55. for (i=0; i<environment.length; i++) {
  56. env.insert(environment[i][0], environment[i][1]);
  57. }
  58. process.setProcessEnvironment(env);
  59. }
  60. // start process:
  61. process.start(program, args);
  62. // wait for process to finish:
  63. process.waitForFinished(-1); // no timeout
  64. var stdoutLines = stdout.split("\n");
  65. var stderrLines = stderr.split("\n");
  66. // output buffered stdout, stderr:
  67. for (i=0; i<stdoutLines.length; i++) {
  68. EAction.handleUserMessage(stdoutLines[i]);
  69. }
  70. for (i=0; i<stderrLines.length; i++) {
  71. EAction.handleUserMessage(stderrLines[i]);
  72. }
  73. this.terminate();
  74. };
  75. /**
  76. * Adds a menu for this action to Examples/Math Examples/ExProcess.
  77. */
  78. ExProcess.init = function(basePath) {
  79. var action = new RGuiAction(qsTr("&Run Process"), RMainWindowQt.getMainWindow());
  80. action.setRequiresDocument(false);
  81. action.setScriptFile(basePath + "/ExProcess.js");
  82. action.setGroupSortOrder(79600);
  83. action.setSortOrder(300);
  84. action.setWidgetNames(["QtExamplesMenu"]);
  85. };