common_threads.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2018 Donoban <donoban@riseup.net>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License along
  18. # with this program; if not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #
  21. from PyQt5 import QtCore, QtWidgets # pylint: disable=import-error
  22. from contextlib import contextmanager
  23. from qubesadmin import exc
  24. @contextmanager
  25. def busy_cursor():
  26. try:
  27. QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.BusyCursor)
  28. yield
  29. finally:
  30. QtWidgets.QApplication.restoreOverrideCursor()
  31. # pylint: disable=too-few-public-methods
  32. class QubesThread(QtCore.QThread):
  33. def __init__(self, vm):
  34. QtCore.QThread.__init__(self)
  35. self.vm = vm
  36. self.msg = None
  37. self.msg_is_success = False
  38. # pylint: disable=too-few-public-methods
  39. class RemoveVMThread(QubesThread):
  40. def run(self):
  41. try:
  42. del self.vm.app.domains[self.vm.name]
  43. except (exc.QubesException, KeyError) as ex:
  44. self.msg = (self.tr("Error removing qube!"), str(ex))
  45. # pylint: disable=too-few-public-methods
  46. class CloneVMThread(QubesThread):
  47. def __init__(self, vm, dst_name, pool=None, label=None):
  48. super().__init__(vm)
  49. self.dst_name = dst_name
  50. self.pool = pool
  51. self.label = label
  52. def run(self):
  53. try:
  54. self.vm.app.clone_vm(self.vm, self.dst_name, pool=self.pool)
  55. if self.label:
  56. result_vm = self.vm.app.domains[self.dst_name]
  57. result_vm.label = self.label
  58. self.msg = (self.tr("Success"),
  59. self.tr("The qube was cloned successfully."))
  60. self.msg_is_success = True
  61. except exc.QubesException as ex:
  62. self.msg = (self.tr("Error while cloning qube!"), str(ex))