summaryrefslogtreecommitdiff
path: root/test/map-client
blob: be3c30724e837572832d4e1c0f577bacd03e173a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python

import gobject

import dbus
import dbus.mainloop.glib
from optparse import OptionParser

from pprint import pformat

def unwrap(x):
    """Hack to unwrap D-Bus values, so that they're easier to read when
    printed. Taken from d-feet """

    if isinstance(x, list):
        return map(unwrap, x)

    if isinstance(x, tuple):
        return tuple(map(unwrap, x))

    if isinstance(x, dict):
        return dict([(unwrap(k), unwrap(v)) for k, v in x.iteritems()])

    for t in [unicode, str, long, int, float, bool]:
        if isinstance(x, t):
            return t(x)

    return x

def parse_options():
	parser.add_option("-d", "--device", dest="device",
			help="Device to connect", metavar="DEVICE")
	parser.add_option("-c", "--chdir", dest="new_dir",
			help="Change current directory to DIR", metavar="DIR")
	parser.add_option("-l", "--lsdir", action="store_true", dest="ls_dir",
			help="List folders in current directory")
	parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
	parser.add_option("-L", "--lsmsg", action="store", dest="ls_msg",
			help="List messages in supplied CWD subdir")

	return parser.parse_args()

def set_folder(session, new_dir):
	session.SetFolder(new_dir)

if  __name__ == '__main__':

	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

	parser = OptionParser()

	(options, args) = parse_options()

	if not options.device:
		parser.print_help()
		exit(0)

	bus = dbus.SessionBus()
	mainloop = gobject.MainLoop()

	client = dbus.Interface(bus.get_object("org.bluez.obex.client", "/"),
				"org.bluez.obex.Client")

	path = client.CreateSession(options.device, { "Target": "map" })

	obj = bus.get_object("org.bluez.obex.client", path)
	session = dbus.Interface(obj, "org.bluez.obex.Session")
	map = dbus.Interface(obj, "org.bluez.obex.MessageAccess")

	if options.new_dir:
		set_folder(map, options.new_dir)

	if options.ls_dir:
		for i in map.GetFolderListing(dict()):
			print "%s/" % (i["Name"])

	if options.ls_msg is not None:
		ret = map.GetMessageListing(options.ls_msg, dict())
		print pformat(unwrap(ret))

	mainloop.run()