guihelpers.py 2.6 KB

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