SingleInstance.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import json
  4. import os
  5. from typing import List, Optional
  6. from PyQt6.QtCore import QUrl
  7. from PyQt6.QtNetwork import QLocalServer, QLocalSocket
  8. from UM.Qt.QtApplication import QtApplication # For typing.
  9. from UM.Logger import Logger
  10. class SingleInstance:
  11. def __init__(self, application: QtApplication, files_to_open: Optional[List[str]], url_to_open: Optional[List[str]]) -> None:
  12. self._application = application
  13. self._files_to_open = files_to_open
  14. self._url_to_open = url_to_open
  15. self._single_instance_server = None
  16. self._application.getPreferences().addPreference("cura/single_instance_clear_before_load", True)
  17. # Starts a client that checks for a single instance server and sends the files that need to opened if the server
  18. # exists. Returns True if the single instance server is found, otherwise False.
  19. def startClient(self) -> bool:
  20. Logger.log("i", "Checking for the presence of an ready running Cura instance.")
  21. single_instance_socket = QLocalSocket(self._application)
  22. Logger.log("d", "Full single instance server name: %s", single_instance_socket.fullServerName())
  23. single_instance_socket.connectToServer("ultimaker-cura")
  24. single_instance_socket.waitForConnected(msecs = 3000) # wait for 3 seconds
  25. if single_instance_socket.state() != QLocalSocket.LocalSocketState.ConnectedState:
  26. return False
  27. # We only send the files that need to be opened.
  28. if not self._files_to_open and not self._url_to_open:
  29. Logger.log("i", "No file need to be opened, do nothing.")
  30. return True
  31. if single_instance_socket.state() == QLocalSocket.LocalSocketState.ConnectedState:
  32. Logger.log("i", "Connection has been made to the single-instance Cura socket.")
  33. # Protocol is one line of JSON terminated with a carriage return.
  34. # "command" field is required and holds the name of the command to execute.
  35. # Other fields depend on the command.
  36. if self._application.getPreferences().getValue("cura/single_instance_clear_before_load"):
  37. payload = {"command": "clear-all"}
  38. single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))
  39. payload = {"command": "focus"}
  40. single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))
  41. for filename in self._files_to_open:
  42. payload = {"command": "open", "filePath": os.path.abspath(filename)}
  43. single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))
  44. for url in self._url_to_open:
  45. payload = {"command": "open-url", "urlPath": url.toString()}
  46. single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding="ascii"))
  47. payload = {"command": "close-connection"}
  48. single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding="ascii"))
  49. single_instance_socket.flush()
  50. single_instance_socket.waitForDisconnected()
  51. return True
  52. def startServer(self) -> None:
  53. self._single_instance_server = QLocalServer()
  54. if self._single_instance_server:
  55. self._single_instance_server.newConnection.connect(self._onClientConnected)
  56. self._single_instance_server.listen("ultimaker-cura")
  57. else:
  58. Logger.log("e", "Single instance server was not created.")
  59. def _onClientConnected(self) -> None:
  60. Logger.log("i", "New connection received on our single-instance server")
  61. connection = None # type: Optional[QLocalSocket]
  62. if self._single_instance_server:
  63. connection = self._single_instance_server.nextPendingConnection()
  64. if connection is not None:
  65. connection.readyRead.connect(lambda c = connection: self.__readCommands(c))
  66. def __readCommands(self, connection: QLocalSocket) -> None:
  67. line = connection.readLine()
  68. while len(line) != 0: # There is also a .canReadLine()
  69. try:
  70. payload = json.loads(str(line, encoding = "ascii").strip())
  71. command = payload["command"]
  72. # Command: Remove all models from the build plate.
  73. if command == "clear-all":
  74. self._application.callLater(lambda: self._application.deleteAll())
  75. # Command: Load a model or project file
  76. elif command == "open":
  77. self._application.callLater(lambda f = payload["filePath"]: self._application._openFile(f))
  78. #command: Load a url link in Cura
  79. elif command == "open-url":
  80. url = QUrl(payload["urlPath"])
  81. self._application.callLater(lambda: self._application._openUrl(url))
  82. # Command: Activate the window and bring it to the top.
  83. elif command == "focus":
  84. # Operating systems these days prevent windows from moving around by themselves.
  85. # 'alert' or flashing the icon in the taskbar is the best thing we do now.
  86. main_window = self._application.getMainWindow()
  87. if main_window is not None:
  88. self._application.callLater(lambda: main_window.alert(0)) # type: ignore # I don't know why MyPy complains here
  89. # Command: Close the socket connection. We're done.
  90. elif command == "close-connection":
  91. connection.close()
  92. else:
  93. Logger.log("w", "Received an unrecognized command " + str(command))
  94. except json.decoder.JSONDecodeError as ex:
  95. Logger.log("w", "Unable to parse JSON command '%s': %s", line, repr(ex))
  96. line = connection.readLine()