/lib/dbus.sh

http://github.com/sukria/Backup-Manager · Shell · 98 lines · 49 code · 14 blank · 35 comment · 9 complexity · 597c46d476616564b98f1d465702da7e MD5 · raw file

  1. # Copyright © 2007 Rached Ben Mustapha
  2. #
  3. # See the AUTHORS file for details.
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. #
  16. # The backup-manager's dbus.sh library.
  17. #
  18. # If the dbus utilities are installed, these functions send
  19. # signals on the bus, (system or session one, as appropriate)
  20. # so that other processes on the system can react to backup
  21. # events, and thus increase the hype-level of backup-manager.
  22. #
  23. # These interfaces are still experimental and are subject to
  24. # change.
  25. #
  26. # Message contents:
  27. # * int32 percentage
  28. # * string label (human-readable string describing what is
  29. # currently being done)
  30. PROGRESS_INTERFACE="org.backupmanager.BackupManager.ProgressNotify"
  31. # Message contents:
  32. # * string event_name (one of "startup", "shutdown")
  33. # * string argument (details of the event)
  34. EVENT_INTERFACE="org.backupmanager.BackupManager.EventNotify"
  35. # Message contents:
  36. # * string level (one of debug, info, warning, error)
  37. # * string message (possibly truncated to fit in a dbus message)
  38. LOG_MESSAGE_INTERFACE="org.backupmanager.BackupManager.LogMessageNotify"
  39. SYSTEM_BUS_OBJECT="/org/backupmanager/instance/SystemInstance"
  40. USER_BUS_OBJECT="/org/backupmanager/instance/UserInstance/${USERNAME}"
  41. function bm_dbus_init()
  42. {
  43. debug "bm_dbus_init()"
  44. dbus_send=$(which dbus-send 2>/dev/null) || true
  45. if [ -x "${dbus_send}" ]; then
  46. if [ "${UID}" = "0" ]; then
  47. bus_type="system"
  48. bus_object=${SYSTEM_BUS_OBJECT}
  49. else
  50. bus_type="session"
  51. bus_object=${USER_BUS_OBJECT}
  52. fi
  53. dbus_send_cmd="${dbus_send} --${bus_type} --type=signal ${bus_object}"
  54. else
  55. dbus_send_cmd=""
  56. fi
  57. }
  58. function bm_dbus_send_progress()
  59. {
  60. local percentage label
  61. percentage=${1}
  62. label=${2}
  63. if [ -n "${dbus_send_cmd}" ]; then
  64. ${dbus_send_cmd} ${PROGRESS_INTERFACE} int32:"${percentage}" string:"${label}" || true
  65. fi
  66. }
  67. function bm_dbus_send_log_message()
  68. {
  69. local level message
  70. level=${1}
  71. message=${2}
  72. if [ -n "${dbus_send_cmd}" ]; then
  73. ${dbus_send_cmd} ${LOG_MESSAGE_INTERFACE} string:"${level}" string:"${message}" || true
  74. fi
  75. }
  76. function bm_dbus_send_event()
  77. {
  78. local event_name details
  79. event_name=${1}
  80. details=${2}
  81. if [ -n "${dbus_send_cmd}" ]; then
  82. ${dbus_send_cmd} ${EVENT_INTERFACE} string:"${event_name}" string:"${details}" || true
  83. fi
  84. }