RunScript.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * Copyright (c) 2014 by Robert S. All rights reserved.
  5. *
  6. * This file is part of the QCAD project.
  7. *
  8. * QCAD is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * QCAD is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with QCAD.
  20. */
  21. include("scripts/EAction.js");
  22. include("scripts/simple.js");
  23. /**
  24. * \class RunScript
  25. * \brief Handles all user interaction to load and run scripts.
  26. * \ingroup ecma_file
  27. */
  28. function RunScript(guiAction) {
  29. EAction.call(this, guiAction);
  30. }
  31. RunScript.prototype = new EAction();
  32. RunScript.includeBasePath = includeBasePath;
  33. RunScript.prototype.beginEvent = function() {
  34. EAction.prototype.beginEvent.call(this);
  35. var fileNames = [];
  36. var i;
  37. // get file names from command line:
  38. if (!isNull(this.guiAction)) {
  39. fileNames = this.guiAction.getArguments();
  40. for (i=0; i<fileNames.length; i++) {
  41. if (!new QFileInfo(fileNames[i]).isAbsolute()) {
  42. fileNames[i] = RSettings.getLaunchPath() + QDir.separator + fileNames[i];
  43. }
  44. }
  45. // make sure next run asks user for file names:
  46. this.guiAction.clearArguments();
  47. }
  48. // show file dialog:
  49. if (fileNames.length===0) {
  50. var lastOpenScriptDir = RSettings.getStringValue("RunScript/Path", RSettings.getDocumentsLocation());
  51. var appWin = EAction.getMainWindow();
  52. var fileDialog = new QFileDialog(appWin, qsTr("Open Script File"), lastOpenScriptDir, "");
  53. var filters = new Array(qsTr("All Files") + " (*)", qsTr("ECMAScript Files") + " (*.js)");
  54. fileDialog.setNameFilters(filters);
  55. fileDialog.selectNameFilter(filters[0]);
  56. fileDialog.setOption(QFileDialog.DontUseNativeDialog, false);
  57. fileDialog.fileMode = QFileDialog.ExistingFiles;
  58. //fileDialog.setLabelText(QFileDialog.FileType, qsTr("Format:"));
  59. if (!fileDialog.exec()) {
  60. destr(fileDialog);
  61. EAction.activateMainWindow();
  62. return;
  63. }
  64. RSettings.setValue("RunScript/Path", fileDialog.directory().absolutePath());
  65. fileNames = fileDialog.selectedFiles();
  66. destr(fileDialog);
  67. EAction.activateMainWindow();
  68. // show warning:
  69. if (RSettings.getBoolValue("RunScript/DontShowDialog", false)!==true) {
  70. var dialog = WidgetFactory.createDialog(RunScript.includeBasePath, "RunScriptDialog.ui", appWin);
  71. var bb = dialog.findChild("ButtonBox");
  72. var b = bb.addButton(qsTr("Run Script"), QDialogButtonBox.AcceptRole);
  73. b["default"] = false;
  74. bb.button(QDialogButtonBox.Cancel)["default"] = true;
  75. var l = dialog.findChild("Message");
  76. l.text = l.text.arg(fileNames.join("<br>"));
  77. var ret = dialog.exec();
  78. if (ret!==QDialog.Accepted.valueOf()) {
  79. destr(dialog);
  80. EAction.activateMainWindow();
  81. return;
  82. }
  83. WidgetFactory.saveState(dialog);
  84. destr(dialog);
  85. EAction.activateMainWindow();
  86. }
  87. }
  88. for (i=0; i<fileNames.length; i++) {
  89. // user chose an action (derived from EAction and already initialized):
  90. var action = RGuiAction.getByScriptFile(fileNames[i]);
  91. if (!isNull(action)) {
  92. action.slotTrigger();
  93. continue;
  94. }
  95. // user chose any js file:
  96. var file = new QFile(fileNames[i]);
  97. var flags = makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text);
  98. if (!file.open(flags)) {
  99. continue;
  100. }
  101. else {
  102. var textStream = new QTextStream(file);
  103. setUtf8Codec(textStream);
  104. var contents = textStream.readAll();
  105. file.close();
  106. try {
  107. var func = new Function(contents);
  108. func();
  109. }
  110. catch (e) {
  111. EAction.handleUserWarning("Script exception in script '" + fileNames[i] + "': " + e);
  112. }
  113. }
  114. }
  115. };