Source: Engines/Wine/Shortcuts/Reader/script.js

  1. const Wine = include("engines.wine.engine.object");
  2. const { WINE_PREFIX_DIR } = include("engines.wine.engine.constants");
  3. const { remove } = include("utils.functions.filesystem.files");
  4. class WineShortcutReader {
  5. constructor(shortcut) {
  6. this.shortcut = shortcut;
  7. this.shortcutManager = Bean("shortcutManager");
  8. this.libraryManager = Bean("libraryManager");
  9. this.uiQuestionFactory = Bean("uiQuestionFactory");
  10. this.winePrefixesDirectory =
  11. Bean("propertyReader").getProperty("application.user.containers") + "/" + WINE_PREFIX_DIR + "/";
  12. this.shortcutContent = JSON.parse(this.shortcut.script);
  13. }
  14. get container() {
  15. return this.shortcutContent.winePrefix;
  16. }
  17. run(userArguments) {
  18. if (!userArguments) {
  19. userArguments = [];
  20. }
  21. let shortcutArguments = this.shortcutContent.arguments;
  22. if (!shortcutArguments) {
  23. shortcutArguments = [];
  24. }
  25. const args = shortcutArguments.concat(Java.from(userArguments));
  26. const userData = {
  27. environment: this.shortcutContent.environment,
  28. trustLevel: this.shortcutContent.trustLevel
  29. };
  30. new Wine()
  31. .prefix(this.container)
  32. .run(this.shortcutContent.executable, args, this.shortcutContent.workingDirectory, false, false, userData);
  33. }
  34. stop() {
  35. new Wine().prefix(this.shortcutContent.winePrefix).kill();
  36. }
  37. uninstall() {
  38. const found = Java.from(this.libraryManager.fetchShortcuts())
  39. .flatMap(shortcutCategory => shortcutCategory.getShortcuts())
  40. .some(shortcut => {
  41. const otherShortcutContent = JSON.parse(shortcut.script);
  42. return otherShortcutContent.winePrefix == this.container && shortcut.name != this.shortcut.name;
  43. });
  44. this.shortcutManager.deleteShortcut(this.shortcut);
  45. if (!found) {
  46. this.uiQuestionFactory.create(
  47. tr("The container {0} is no longer used.\nDo you want to delete it?", this.container),
  48. () => remove(this.winePrefixesDirectory + this.container)
  49. );
  50. }
  51. }
  52. }
  53. module.default = class ShortcutReader {
  54. constructor() {
  55. // do nothing
  56. }
  57. /**
  58. * Sets shortcut
  59. *
  60. * @param {string} shortcut shortcut
  61. * @returns {void}
  62. */
  63. of(shortcut) {
  64. const shortcutContentParsed = JSON.parse(shortcut.script);
  65. switch (shortcutContentParsed.type) {
  66. case "WINE":
  67. this.runner = new WineShortcutReader(shortcut);
  68. break;
  69. default:
  70. throw new Error(`Unknown container type ${shortcutContentParsed.type}`);
  71. }
  72. }
  73. /**
  74. * Returns the name of the container belonging to a shortcut
  75. *
  76. * @returns {string} The container name
  77. */
  78. getContainer() {
  79. return this.runner.container;
  80. }
  81. /**
  82. * Runs a shortcut with the given user arguments
  83. *
  84. * @param {array} userArguments The user arguments
  85. * @returns {void}
  86. */
  87. run(userArguments) {
  88. this.runner.run(userArguments);
  89. }
  90. /**
  91. * Stops the running shortcut
  92. *
  93. * @returns {void}
  94. */
  95. stop() {
  96. this.runner.stop();
  97. }
  98. /**
  99. * Uninstalls the shortcut
  100. *
  101. * @returns {void}
  102. */
  103. uninstall() {
  104. this.runner.uninstall();
  105. }
  106. }