PageRenderTime 26ms CodeModel.GetById 24ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/dbus.sh

http://github.com/sukria/Backup-Manager
Shell | 98 lines | 49 code | 14 blank | 35 comment | 9 complexity | 597c46d476616564b98f1d465702da7e MD5 | raw file
Possible License(s): GPL-2.0
 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
27# Message contents:
28# * int32 percentage
29# * string label (human-readable string describing what is
30#                 currently being done)
31PROGRESS_INTERFACE="org.backupmanager.BackupManager.ProgressNotify"
32
33# Message contents:
34# * string event_name (one of "startup", "shutdown")
35# * string argument (details of the event)
36EVENT_INTERFACE="org.backupmanager.BackupManager.EventNotify"
37
38# Message contents:
39# * string level (one of debug, info, warning, error)
40# * string message (possibly truncated to fit in a dbus message)
41LOG_MESSAGE_INTERFACE="org.backupmanager.BackupManager.LogMessageNotify"
42
43SYSTEM_BUS_OBJECT="/org/backupmanager/instance/SystemInstance"
44USER_BUS_OBJECT="/org/backupmanager/instance/UserInstance/${USERNAME}"
45
46function bm_dbus_init()
47{
48    debug "bm_dbus_init()"
49    dbus_send=$(which dbus-send 2>/dev/null) || true
50
51    if [ -x "${dbus_send}" ]; then
52        if [ "${UID}" = "0" ]; then
53            bus_type="system"
54            bus_object=${SYSTEM_BUS_OBJECT}
55        else
56            bus_type="session"
57            bus_object=${USER_BUS_OBJECT}
58        fi
59
60        dbus_send_cmd="${dbus_send} --${bus_type} --type=signal ${bus_object}"
61    else
62        dbus_send_cmd=""
63    fi
64}
65
66function bm_dbus_send_progress()
67{
68    local percentage label
69    percentage=${1}
70    label=${2}
71
72    if [ -n "${dbus_send_cmd}" ]; then
73        ${dbus_send_cmd} ${PROGRESS_INTERFACE} int32:"${percentage}" string:"${label}" || true
74    fi
75}
76
77function bm_dbus_send_log_message()
78{
79    local level message
80    level=${1}
81    message=${2}
82
83    if [ -n "${dbus_send_cmd}" ]; then
84        ${dbus_send_cmd} ${LOG_MESSAGE_INTERFACE} string:"${level}" string:"${message}" || true
85    fi
86}
87
88function bm_dbus_send_event()
89{
90    local event_name details
91    event_name=${1}
92    details=${2}
93
94    if [ -n "${dbus_send_cmd}" ]; then
95        ${dbus_send_cmd} ${EVENT_INTERFACE} string:"${event_name}" string:"${details}" || true
96    fi
97}
98