notify.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 sys
  23. system_bus = None
  24. session_bus = None
  25. notify_object = None
  26. def tray_notify_init():
  27. import dbus
  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. import dbus
  46. global system_bus
  47. if system_bus is None:
  48. system_bus = dbus.SystemBus()
  49. try:
  50. qubes_manager = system_bus.get_object('org.qubesos.QubesManager',
  51. '/org/qubesos/QubesManager')
  52. qubes_manager.notify_error(name, message, dbus_interface='org.qubesos.QubesManager')
  53. except dbus.DBusException:
  54. # ignore the case when no qubes-manager is running
  55. pass
  56. def clear_error_qubes_manager(name, message):
  57. import dbus
  58. global system_bus
  59. if system_bus is None:
  60. system_bus = dbus.SystemBus()
  61. try:
  62. qubes_manager = system_bus.get_object('org.qubesos.QubesManager',
  63. '/org/qubesos/QubesManager')
  64. qubes_manager.clear_error_exact(name, message, dbus_interface='org.qubesos.QubesManager')
  65. except dbus.DBusException:
  66. # ignore the case when no qubes-manager is running
  67. pass