policycreateconfirmation.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- encoding: utf-8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. import os
  21. import pkg_resources
  22. # pylint: disable=import-error,wrong-import-position
  23. import gi
  24. gi.require_version('Gtk', '3.0')
  25. from gi.repository import Gtk
  26. # pylint: enable=import-error
  27. class PolicyCreateConfirmationWindow(object):
  28. # pylint: disable=too-few-public-methods
  29. _source_file = pkg_resources.resource_filename('qubespolicy',
  30. os.path.join('glade', "PolicyCreateConfirmationWindow.glade"))
  31. _source_id = {'window': "PolicyCreateConfirmationWindow",
  32. 'ok': "okButton",
  33. 'cancel': "cancelButton",
  34. 'source': "sourceEntry",
  35. 'service': "serviceEntry",
  36. 'confirm': "confirmEntry",
  37. }
  38. def __init__(self, source, service):
  39. self._gtk_builder = Gtk.Builder()
  40. self._gtk_builder.add_from_file(self._source_file)
  41. self._window = self._gtk_builder.get_object(
  42. self._source_id['window'])
  43. self._rpc_ok_button = self._gtk_builder.get_object(
  44. self._source_id['ok'])
  45. self._rpc_cancel_button = self._gtk_builder.get_object(
  46. self._source_id['cancel'])
  47. self._service_entry = self._gtk_builder.get_object(
  48. self._source_id['service'])
  49. self._source_entry = self._gtk_builder.get_object(
  50. self._source_id['source'])
  51. self._confirm_entry = self._gtk_builder.get_object(
  52. self._source_id['confirm'])
  53. self._source_entry.set_text(source)
  54. self._service_entry.set_text(service)
  55. # make OK button the default
  56. ok_button = self._window.get_widget_for_response(Gtk.ResponseType.OK)
  57. ok_button.set_can_default(True)
  58. ok_button.grab_default()
  59. def run(self):
  60. self._window.set_keep_above(True)
  61. self._window.connect("delete-event", Gtk.main_quit)
  62. self._window.show_all()
  63. response = self._window.run()
  64. self._window.hide()
  65. if response == Gtk.ResponseType.OK:
  66. return self._confirm_entry.get_text() == 'YES'
  67. return False
  68. def confirm(source, service):
  69. window = PolicyCreateConfirmationWindow(source, service)
  70. return window.run()