~xubuntu-dev/ubiquity/lp1437180_feh

« back to all changes in this revision

Viewing changes to bin/ubiquity-bluetooth-agent

  • Committer: Colin Watson
  • Date: 2012-04-30 23:38:41 UTC
  • mfrom: (5402 trunk)
  • mto: This revision was merged to the branch mainline in revision 5403.
  • Revision ID: cjwatson@canonical.com-20120430233841-xb0qsk46lnhski7m
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
import dbus, dbus.service, dbus.mainloop.glib, gobject, os, sys
 
3
 
 
4
class Rejected(dbus.DBusException):
 
5
    _dbus_error_name = "org.bluez.Error.Rejected"
 
6
 
 
7
class Agent(dbus.service.Object):
 
8
    exit_on_release = True
 
9
 
 
10
    def set_exit_on_release(self, exit_on_release):
 
11
        self.exit_on_release = exit_on_release
 
12
 
 
13
    @dbus.service.method("org.bluez.Agent", in_signature="", out_signature="")
 
14
    def Release(self):
 
15
        if self.exit_on_release:
 
16
            mainloop.quit()
 
17
 
 
18
    @dbus.service.method("org.bluez.Agent", in_signature="os", out_signature="")
 
19
    def Authorize(self, device, uuid):
 
20
        bus = dbus.SystemBus()
 
21
        attr = dbus.Interface(bus.get_object("org.bluez", device), "org.bluez.Device").GetProperties()
 
22
 
 
23
        # Only accept input (HID) devices
 
24
        if 'Class' in attr:
 
25
            if (attr['Class'] & 0x500) == 0x500:
 
26
                dbus.Interface(bus.get_object("org.bluez", device), "org.bluez.Device").SetProperty("Trusted", True)
 
27
                return
 
28
 
 
29
        raise Rejected("Connection rejected by user")
 
30
 
 
31
    @dbus.service.method("org.bluez.Agent", in_signature="o", out_signature="s")
 
32
    def RequestPinCode(self, device):
 
33
        return "0000"
 
34
 
 
35
    @dbus.service.method("org.bluez.Agent", in_signature="o", out_signature="u")
 
36
    def RequestPasskey(self, device):
 
37
        return dbus.UInt32(0000)
 
38
 
 
39
    @dbus.service.method("org.bluez.Agent", in_signature="ou", out_signature="")
 
40
    def DisplayPasskey(self, device, passkey):
 
41
        return
 
42
 
 
43
    @dbus.service.method("org.bluez.Agent", in_signature="ou", out_signature="")
 
44
    def RequestConfirmation(self, device, passkey):
 
45
        return
 
46
 
 
47
    @dbus.service.method("org.bluez.Agent", in_signature="s", out_signature="")
 
48
    def ConfirmModeChange(self, mode):
 
49
        return
 
50
 
 
51
    @dbus.service.method("org.bluez.Agent", in_signature="", out_signature="")
 
52
    def Cancel(self):
 
53
        return
 
54
 
 
55
def exit_handler():
 
56
    import os
 
57
    if not os.path.exists("/tmp/ubiquity-bluetooth-done"):
 
58
        touch = open("/tmp/ubiquity-bluetooth-done", "w+")
 
59
        touch.close()
 
60
 
 
61
    if os.path.exists("/usr/bin/bluetooth-applet.orig"):
 
62
        os.execl("/usr/bin/bluetooth-applet.orig", "bluetooth-applet")
 
63
    elif os.path.exists("/usr/bin/bluetooth-applet"):
 
64
        os.execl("/usr/bin/bluetooth-applet", "bluetooth-applet")
 
65
    else:
 
66
        sys.exit(0)
 
67
    return False
 
68
 
 
69
if os.path.exists("/tmp/ubiquity-bluetooth-done"):
 
70
    exit_handler()
 
71
 
 
72
try:
 
73
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
 
74
    bus = dbus.SystemBus()
 
75
    manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")
 
76
    adapter = dbus.Interface(bus.get_object("org.bluez", manager.DefaultAdapter()), "org.bluez.Adapter")
 
77
    agent = Agent(bus, "/ubiquity/bluetooth/agent")
 
78
    mainloop = gobject.MainLoop()
 
79
    adapter.RegisterAgent("/ubiquity/bluetooth/agent", "DisplayYesNo")
 
80
except dbus.exceptions.DBusException:
 
81
    exit_handler()
 
82
 
 
83
gobject.timeout_add(300000, exit_handler)
 
84
mainloop.run()