notify.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2014 Marek Marczykowski-Górecki <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 dbus
  23. import sys
  24. system_bus = None
  25. session_bus = None
  26. notify_object = None
  27. def tray_notify_init():
  28. global notify_object
  29. try:
  30. notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
  31. except dbus.DBusException as ex:
  32. print >>sys.stderr, "WARNING: failed connect to tray notification service: %s" % str(ex)
  33. def tray_notify(msg, label, timeout = 3000):
  34. if notify_object:
  35. if label:
  36. if not isinstance(label, str):
  37. label = label.icon
  38. notify_object.Notify("Qubes", 0, label, "Qubes", msg, [], [], timeout,
  39. dbus_interface="org.freedesktop.Notifications")
  40. def tray_notify_error(msg, timeout = 3000):
  41. if notify_object:
  42. notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", msg, [], [],
  43. timeout, dbus_interface="org.freedesktop.Notifications")
  44. def notify_error_qubes_manager(name, message):
  45. global system_bus
  46. if system_bus is None:
  47. system_bus = dbus.SystemBus()
  48. try:
  49. qubes_manager = system_bus.get_object('org.qubesos.QubesManager',
  50. '/org/qubesos/QubesManager')
  51. qubes_manager.notify_error(name, message, dbus_interface='org.qubesos.QubesManager')
  52. except dbus.DBusException:
  53. # ignore the case when no qubes-manager is running
  54. pass
  55. def clear_error_qubes_manager(name, message):
  56. global system_bus
  57. if system_bus is None:
  58. system_bus = dbus.SystemBus()
  59. try:
  60. qubes_manager = system_bus.get_object('org.qubesos.QubesManager',
  61. '/org/qubesos/QubesManager')
  62. qubes_manager.clear_error_exact(name, message, dbus_interface='org.qubesos.QubesManager')
  63. except dbus.DBusException:
  64. # ignore the case when no qubes-manager is running
  65. pass