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 program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (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 General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. import pkg_resources
  22. from gi.repository import Gtk, Gdk, GLib
  23. import os
  24. from qubespolicy.gtkhelpers import VMListModeler, FocusStealingHelper
  25. from qubespolicy.utils import sanitize_domain_name, \
  26. sanitize_service_name
  27. class RPCConfirmationWindow:
  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[1] == 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. def _escape_and_format_rpc_text(self, rpc_operation):
  93. escaped = GLib.markup_escape_text(rpc_operation)
  94. partitioned = escaped.partition('.')
  95. formatted = partitioned[0] + partitioned[1]
  96. if len(partitioned[2]) > 0:
  97. formatted += "<b>" + partitioned[2] + "</b>"
  98. else:
  99. formatted = "<b>" + formatted + "</b>"
  100. return formatted
  101. def _connect_events(self):
  102. self._rpc_window.connect("key-press-event", self._key_pressed)
  103. self._rpc_ok_button.connect("clicked", self._clicked_ok)
  104. self._rpc_cancel_button.connect("clicked", self._clicked_cancel)
  105. self._error_bar.connect("response", self._close_error)
  106. def __init__(self, entries_info, source, rpc_operation, targets_list,
  107. target=None):
  108. sanitize_domain_name(source, assert_sanitized=True)
  109. sanitize_service_name(source, assert_sanitized=True)
  110. self._gtk_builder = Gtk.Builder()
  111. self._gtk_builder.add_from_file(self._source_file)
  112. self._rpc_window = self._gtk_builder.get_object(
  113. self._source_id['window'])
  114. self._rpc_ok_button = self._gtk_builder.get_object(
  115. self._source_id['ok'])
  116. self._rpc_cancel_button = self._gtk_builder.get_object(
  117. self._source_id['cancel'])
  118. self._rpc_label = self._gtk_builder.get_object(
  119. self._source_id['rpc_label'])
  120. self._source_entry = self._gtk_builder.get_object(
  121. self._source_id['source'])
  122. self._rpc_combo_box = self._gtk_builder.get_object(
  123. self._source_id['target'])
  124. self._error_bar = self._gtk_builder.get_object(
  125. self._source_id['error_bar'])
  126. self._error_message = self._gtk_builder.get_object(
  127. self._source_id['error_message'])
  128. self._target_name = None
  129. self._focus_helper = self._new_focus_stealing_helper()
  130. self._rpc_label.set_markup(
  131. self._escape_and_format_rpc_text(rpc_operation))
  132. self._entries_info = entries_info
  133. list_modeler = self._new_VM_list_modeler()
  134. list_modeler.apply_model(self._rpc_combo_box, targets_list,
  135. selection_trigger=self._update_ok_button_sensitivity,
  136. activation_trigger=self._clicked_ok)
  137. self._source_entry.set_text(source)
  138. list_modeler.apply_icon(self._source_entry, source)
  139. self._confirmed = None
  140. self._set_initial_target(source, target)
  141. self._connect_events()
  142. def _close(self):
  143. self._rpc_window.close()
  144. def _show(self):
  145. self._rpc_window.set_keep_above(True)
  146. self._rpc_window.connect("delete-event", Gtk.main_quit)
  147. self._rpc_window.show_all()
  148. Gtk.main()
  149. def _new_VM_list_modeler(self):
  150. return VMListModeler(self._entries_info)
  151. def _new_focus_stealing_helper(self):
  152. return FocusStealingHelper(
  153. self._rpc_window,
  154. self._rpc_ok_button,
  155. 1)
  156. def confirm_rpc(self):
  157. self._show()
  158. if self._confirmed:
  159. return self._target_name
  160. else:
  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()