connman-notify 3.09 KB
Newer Older
wavexx's avatar
wavexx committed
1 2 3 4
#!/usr/bin/env python
# connman-notify: desktop notification integration for connman
# Copyright(c) 2014 by wave++ "Yuri D'Elia" <wavexx@thregr.org>
# Distributed under GPL2 (see COPYING) WITHOUT ANY WARRANTY.
wavexx's avatar
wavexx committed
5 6 7 8 9 10 11 12 13 14 15
from __future__ import print_function

import argparse
import dbus
import dbus.mainloop.glib
import glib
import pynotify

APP_NAME  = 'connman'
SHORT_DEV = True
SHOW_ADDR = True
wavexx's avatar
wavexx committed
16 17 18

PROTO_LST = ['IPv4', 'IPv6']
STATE_IGN = ['ready', 'idle']
wavexx's avatar
wavexx committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
STATE_MAP = {'disconnect': 'disconnected',
             'association': 'associating ...',
             'configuration': 'configuring ...'}


def notify(subject, body):
    ntf = pynotify.Notification(subject, body)
    ntf.show()


def property_changed(bus, manager, name, value, path):
    # ignore global state changes
    if path == '/': return

    # shorten device names
    srv_name = path[path.rfind("/") + 1:]
    if SHORT_DEV:
        srv_name = srv_name[0:srv_name.find("_")]

    # interface state changes
wavexx's avatar
wavexx committed
39
    if name == 'State' and value not in STATE_IGN:
wavexx's avatar
wavexx committed
40 41 42 43 44 45 46 47 48
        state = STATE_MAP.get(value, value)
        srv_obj = dbus.Interface(bus.get_object('net.connman', path), 'net.connman.Service')
        srv_prop = None
        try:
            srv_prop = srv_obj.GetProperties()
            if 'Name' in srv_prop:
                srv_name += "/" + srv_prop['Name']
            if SHOW_ADDR and value == 'online':
                addrs = []
wavexx's avatar
wavexx committed
49
                for proto in PROTO_LST:
wavexx's avatar
wavexx committed
50
                    if proto in srv_prop and 'Address' in srv_prop[proto]:
51
                        addrs.append(str(srv_prop[proto]['Address']))
wavexx's avatar
wavexx committed
52 53 54 55 56 57 58
                if addrs:
                    state += " [" + ", ".join(addrs) + "]"
        except dbus.DBusException:
            pass
        notify(srv_name, state)

    # IPv4/6 changes
wavexx's avatar
wavexx committed
59
    if SHOW_ADDR and name in PROTO_LST and 'Address' in value:
wavexx's avatar
wavexx committed
60 61 62 63 64 65 66
        srv_obj = dbus.Interface(bus.get_object('net.connman', path), 'net.connman.Service')
        srv_prop = srv_obj.GetProperties()

        # only report new addresses
        if srv_prop['State'] == 'online':
            if 'Name' in srv_prop:
                srv_name += "/" + srv_prop['Name']
67
            notify(srv_name, str(value['Address']))
wavexx's avatar
wavexx committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92


if __name__ == '__main__':
    ap = argparse.ArgumentParser(description='connman notification daemon')
    ap.add_argument('-l', dest="long_dev", action="store_true",
                    help='Show long interface names')
    ap.add_argument('-n', dest="hide_addr", action="store_true",
                    help='Hide assigned IPv4/6 addresses')
    args = ap.parse_args()

    if args.long_dev: SHORT_DEV = False
    if args.hide_addr: SHOW_ADDR = False

    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    pynotify.init(APP_NAME)
    bus = dbus.SystemBus()
    manager = dbus.Interface(bus.get_object("net.connman", "/"), "net.connman.Manager")

    bus.add_signal_receiver(lambda name, value, path: property_changed(bus, manager, name, value, path),
                            bus_name="net.connman",
                            signal_name="PropertyChanged",
                            path_keyword="path")

    mainloop = glib.MainLoop()
    mainloop.run()