rpcconfirmation.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/python
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2017 boring-stuff <boring-stuff@users.noreply.github.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library 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 GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  19. #
  20. import os
  21. from gi.repository import Gtk, Gdk, GLib # pylint: disable=import-error
  22. import pkg_resources
  23. from qubespolicy.gtkhelpers import VMListModeler, FocusStealingHelper
  24. from qubespolicy.utils import sanitize_domain_name, \
  25. sanitize_service_name
  26. class RPCConfirmationWindow:
  27. # pylint: disable=too-few-public-methods
  28. _source_file = pkg_resources.resource_filename('qubespolicy',
  29. os.path.join('glade', "RPCConfirmationWindow.glade"))
  30. _source_id = {'window': "RPCConfirmationWindow",
  31. 'ok': "okButton",
  32. 'cancel': "cancelButton",
  33. 'source': "sourceEntry",
  34. 'rpc_label': "rpcLabel",
  35. 'target': "TargetCombo",
  36. 'error_bar': "ErrorBar",
  37. 'error_message': "ErrorMessage",
  38. }
  39. def _clicked_ok(self, source):
  40. assert source is not None, \
  41. 'Called the clicked ok callback from no source object'
  42. if self._can_perform_action():
  43. self._confirmed = True
  44. self._close()
  45. def _clicked_cancel(self, button):
  46. assert button == self._rpc_cancel_button, \
  47. 'Called the clicked cancel callback through the wrong button'
  48. if self._can_perform_action():
  49. self._confirmed = False
  50. self._close()
  51. def _key_pressed(self, window, key):
  52. assert window == self._rpc_window, \
  53. 'Key pressed callback called with wrong window'
  54. if self._can_perform_action():
  55. if key.keyval == Gdk.KEY_Escape:
  56. self._confirmed = False
  57. self._close()
  58. def _update_ok_button_sensitivity(self, data):
  59. valid = (data is not None)
  60. if valid:
  61. self._target_name = data
  62. else:
  63. self._target_name = None
  64. self._focus_helper.request_sensitivity(valid)
  65. def _show_error(self, error_message):
  66. self._error_message.set_text(error_message)
  67. self._error_bar.set_visible(True)
  68. def _close_error(self, error_bar, response):
  69. assert error_bar == self._error_bar, \
  70. 'Closed the error bar with the wrong error bar as parameter'
  71. assert response is not None, \
  72. 'Closed the error bar with None as a response'
  73. self._error_bar.set_visible(False)
  74. def _set_initial_target(self, source, target):
  75. if target is not None:
  76. if target == source:
  77. self._show_error(
  78. "Source and target domains must not be the same.")
  79. else:
  80. model = self._rpc_combo_box.get_model()
  81. found = False
  82. for item in model:
  83. if item[3] == target:
  84. found = True
  85. self._rpc_combo_box.set_active_iter(
  86. model.get_iter(item.path))
  87. break
  88. if not found:
  89. self._show_error("Domain '%s' doesn't exist." % target)
  90. def _can_perform_action(self):
  91. return self._focus_helper.can_perform_action()
  92. @staticmethod
  93. def _escape_and_format_rpc_text(rpc_operation):
  94. escaped = GLib.markup_escape_text(rpc_operation)
  95. partitioned = escaped.partition('.')
  96. formatted = partitioned[0] + partitioned[1]
  97. if partitioned[2]:
  98. formatted += "<b>" + partitioned[2] + "</b>"
  99. else:
  100. formatted = "<b>" + formatted + "</b>"
  101. return formatted
  102. def _connect_events(self):
  103. self._rpc_window.connect("key-press-event", self._key_pressed)
  104. self._rpc_ok_button.connect("clicked", self._clicked_ok)
  105. self._rpc_cancel_button.connect("clicked", self._clicked_cancel)
  106. self._error_bar.connect("response", self._close_error)
  107. def __init__(self, entries_info, source, rpc_operation, targets_list,
  108. target=None):
  109. sanitize_domain_name(source, assert_sanitized=True)
  110. sanitize_service_name(source, assert_sanitized=True)
  111. self._gtk_builder = Gtk.Builder()
  112. self._gtk_builder.add_from_file(self._source_file)
  113. self._rpc_window = self._gtk_builder.get_object(
  114. self._source_id['window'])
  115. self._rpc_ok_button = self._gtk_builder.get_object(
  116. self._source_id['ok'])
  117. self._rpc_cancel_button = self._gtk_builder.get_object(
  118. self._source_id['cancel'])
  119. self._rpc_label = self._gtk_builder.get_object(
  120. self._source_id['rpc_label'])
  121. self._source_entry = self._gtk_builder.get_object(
  122. self._source_id['source'])
  123. self._rpc_combo_box = self._gtk_builder.get_object(
  124. self._source_id['target'])
  125. self._error_bar = self._gtk_builder.get_object(
  126. self._source_id['error_bar'])
  127. self._error_message = self._gtk_builder.get_object(
  128. self._source_id['error_message'])
  129. self._target_name = None
  130. self._focus_helper = self._new_focus_stealing_helper()
  131. self._rpc_label.set_markup(
  132. self._escape_and_format_rpc_text(rpc_operation))
  133. self._entries_info = entries_info
  134. list_modeler = self._new_vm_list_modeler()
  135. list_modeler.apply_model(self._rpc_combo_box, targets_list,
  136. selection_trigger=self._update_ok_button_sensitivity,
  137. activation_trigger=self._clicked_ok)
  138. self._source_entry.set_text(source)
  139. list_modeler.apply_icon(self._source_entry, source)
  140. self._confirmed = None
  141. self._set_initial_target(source, target)
  142. self._connect_events()
  143. def _close(self):
  144. self._rpc_window.close()
  145. def _show(self):
  146. self._rpc_window.set_keep_above(True)
  147. self._rpc_window.connect("delete-event", Gtk.main_quit)
  148. self._rpc_window.show_all()
  149. Gtk.main()
  150. def _new_vm_list_modeler(self):
  151. return VMListModeler(self._entries_info)
  152. def _new_focus_stealing_helper(self):
  153. return FocusStealingHelper(
  154. self._rpc_window,
  155. self._rpc_ok_button,
  156. 1)
  157. def confirm_rpc(self):
  158. self._show()
  159. if self._confirmed:
  160. return self._target_name
  161. return False
  162. def confirm_rpc(entries_info, source, rpc_operation, targets_list, target=None):
  163. window = RPCConfirmationWindow(entries_info, source, rpc_operation,
  164. targets_list, target)
  165. return window.confirm_rpc()