RemovableDrivePlugin.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Uranium is released under the terms of the AGPLv3 or higher.
  3. import threading
  4. import time
  5. from UM.Message import Message
  6. from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
  7. from UM.Logger import Logger
  8. from . import RemovableDriveOutputDevice
  9. from UM.Logger import Logger
  10. from UM.i18n import i18nCatalog
  11. catalog = i18nCatalog("cura")
  12. class RemovableDrivePlugin(OutputDevicePlugin):
  13. def __init__(self):
  14. super().__init__()
  15. self._update_thread = threading.Thread(target = self._updateThread)
  16. self._update_thread.setDaemon(True)
  17. self._check_updates = True
  18. self._drives = {}
  19. def start(self):
  20. self._update_thread.start()
  21. def stop(self):
  22. self._check_updates = False
  23. self._update_thread.join()
  24. self._addRemoveDrives({})
  25. def checkRemovableDrives(self):
  26. raise NotImplementedError()
  27. def ejectDevice(self, device):
  28. try:
  29. Logger.log("i", "Attempting to eject the device")
  30. result = self.performEjectDevice(device)
  31. except Exception as e:
  32. Logger.log("e", "Ejection failed due to: %s" % str(e))
  33. result = False
  34. if result:
  35. Logger.log("i", "Succesfully ejected the device")
  36. message = Message(catalog.i18nc("@info:status", "Ejected {0}. You can now safely remove the drive.").format(device.getName()))
  37. message.show()
  38. else:
  39. message = Message(catalog.i18nc("@info:status", "Failed to eject {0}. Another program may be using the drive.").format(device.getName()))
  40. message.show()
  41. return result
  42. def performEjectDevice(self, device):
  43. raise NotImplementedError()
  44. def _updateThread(self):
  45. while self._check_updates:
  46. result = self.checkRemovableDrives()
  47. self._addRemoveDrives(result)
  48. time.sleep(5)
  49. def _addRemoveDrives(self, drives):
  50. # First, find and add all new or changed keys
  51. for key, value in drives.items():
  52. if key not in self._drives:
  53. self.getOutputDeviceManager().addOutputDevice(RemovableDriveOutputDevice.RemovableDriveOutputDevice(key, value))
  54. continue
  55. if self._drives[key] != value:
  56. self.getOutputDeviceManager().removeOutputDevice(key)
  57. self.getOutputDeviceManager().addOutputDevice(RemovableDriveOutputDevice.RemovableDriveOutputDevice(key, value))
  58. # Then check for keys that have been removed
  59. for key in self._drives.keys():
  60. if key not in drives:
  61. self.getOutputDeviceManager().removeOutputDevice(key)
  62. self._drives = drives