1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3
4__author__    = 'Fabian Affolter <fabian()affolter-engineering.ch>'
5__copyright__ = 'Copyright 2014 Fabian Affolter'
6__license__   = 'Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)'
7
8import dbus
9
10
11def plugin(srv, item):
12    """Send a message through dbus to the user's desktop."""
13
14    srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target)
15
16    text = item.message
17    summary = item.addrs[0]
18    app_name = item.get('title', srv.SCRIPTNAME)
19    replaces_id = 0
20    service = 'org.freedesktop.Notifications'
21    path = '/' + service.replace('.', '/')
22    interface = service
23    app_icon = '/usr/share/icons/gnome/32x32/places/network-server.png'
24    expire_timeout = 1000
25    actions = []
26    hints = []
27
28    try:
29        srv.logging.debug("Sending message to %s..." % (item.target))
30        session_bus = dbus.SessionBus()
31        obj = session_bus.get_object(service, path)
32        interface = dbus.Interface(obj, interface)
33        interface.Notify(app_name, replaces_id, app_icon, summary, text,
34                    actions, hints, expire_timeout)
35        srv.logging.debug("Successfully sent message")
36    except Exception as e:
37        srv.logging.error("Error sending message to %s: %s" % (item.target, e))
38        return False
39
40    return True
41