guihelpers.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2011 Marek Marczykowski <marmarek@invisiblethingslab.com>
  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 General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. import sys
  23. from optparse import OptionParser
  24. from PyQt4.QtCore import *
  25. from PyQt4.QtGui import *
  26. import dbus
  27. from dbus import DBusException
  28. app = None
  29. system_bus = None
  30. def prepare_app():
  31. global app
  32. app = QApplication(sys.argv)
  33. app.setOrganizationName("The Qubes Project")
  34. app.setOrganizationDomain("http://qubes-os.org")
  35. app.setApplicationName("Qubes")
  36. def ask(text, title="Question", yestoall=False):
  37. global app
  38. if app is None:
  39. prepare_app()
  40. buttons = QMessageBox.Yes | QMessageBox.No
  41. if yestoall:
  42. buttons |= QMessageBox.YesToAll
  43. reply = QMessageBox.question(None, title, text, buttons, defaultButton=QMessageBox.Yes)
  44. if reply == QMessageBox.Yes:
  45. return 0
  46. elif reply == QMessageBox.No:
  47. return 1
  48. elif reply == QMessageBox.YesToAll:
  49. return 2
  50. else:
  51. #?!
  52. return 127
  53. def notify_error_qubes_manager(name, message):
  54. global system_bus
  55. if system_bus is None:
  56. system_bus = dbus.SystemBus()
  57. try:
  58. qubes_manager = system_bus.get_object('org.qubesos.QubesManager',
  59. '/org/qubesos/QubesManager')
  60. qubes_manager.notify_error(name, message, dbus_interface='org.qubesos.QubesManager')
  61. except DBusException:
  62. # ignore the case when no qubes-manager is running
  63. pass
  64. def clear_error_qubes_manager(name, message):
  65. global system_bus
  66. if system_bus is None:
  67. system_bus = dbus.SystemBus()
  68. try:
  69. qubes_manager = system_bus.get_object('org.qubesos.QubesManager',
  70. '/org/qubesos/QubesManager')
  71. qubes_manager.clear_error_exact(name, message, dbus_interface='org.qubesos.QubesManager')
  72. except DBusException:
  73. # ignore the case when no qubes-manager is running
  74. pass