common_threads.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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):
  48. super(CloneVMThread, self).__init__(vm)
  49. self.dst_name = dst_name
  50. def run(self):
  51. try:
  52. self.vm.app.clone_vm(self.vm, self.dst_name)
  53. self.msg = (self.tr("Sucess"),
  54. self.tr("The qube was cloned sucessfully."))
  55. self.msg_is_success = True
  56. except exc.QubesException as ex:
  57. self.msg = (self.tr("Error while cloning qube!"), str(ex))