Blame test/opp-client

Packit 34410b
#!/usr/bin/python
Packit 34410b
Packit 34410b
from __future__ import absolute_import, print_function, unicode_literals
Packit 34410b
Packit 34410b
from optparse import OptionParser
Packit 34410b
import os.path
Packit 34410b
import sys
Packit 34410b
import dbus
Packit 34410b
import dbus.mainloop.glib
Packit 34410b
try:
Packit 34410b
  from gi.repository import GObject
Packit 34410b
except ImportError:
Packit 34410b
  import gobject as GObject
Packit 34410b
Packit 34410b
BUS_NAME='org.bluez.obex'
Packit 34410b
PATH = '/org/bluez/obex'
Packit 34410b
CLIENT_INTERFACE='org.bluez.obex.Client1'
Packit 34410b
SESSION_INTERFACE='org.bluez.obex.Session1'
Packit 34410b
OBJECT_PUSH_INTERFACE='org.bluez.obex.ObjectPush1'
Packit 34410b
TRANSFER_INTERFACE='org.bluez.obex.Transfer1'
Packit 34410b
Packit 34410b
def parse_options():
Packit 34410b
	parser.add_option("-d", "--device", dest="device",
Packit 34410b
			help="Device to connect", metavar="DEVICE")
Packit 34410b
	parser.add_option("-p", "--pull", dest="pull_to_file",
Packit 34410b
			help="Pull vcard and store in FILE", metavar="FILE")
Packit 34410b
	parser.add_option("-s", "--send", dest="send_file",
Packit 34410b
			help="Send FILE", metavar="FILE")
Packit 34410b
	parser.add_option("-v", "--verbose", action="store_true",
Packit 34410b
			dest="verbose")
Packit 34410b
Packit 34410b
	return parser.parse_args()
Packit 34410b
Packit 34410b
class OppClient:
Packit 34410b
	def __init__(self, session_path, verbose=False):
Packit 34410b
		self.transferred = 0
Packit 34410b
		self.transfer_path = None
Packit 34410b
		self.verbose = verbose
Packit 34410b
		bus = dbus.SessionBus()
Packit 34410b
		obj = bus.get_object(BUS_NAME, session_path)
Packit 34410b
		self.session = dbus.Interface(obj, SESSION_INTERFACE)
Packit 34410b
		self.opp = dbus.Interface(obj, OBJECT_PUSH_INTERFACE)
Packit 34410b
		bus.add_signal_receiver(self.properties_changed,
Packit 34410b
			dbus_interface="org.freedesktop.DBus.Properties",
Packit 34410b
			signal_name="PropertiesChanged",
Packit 34410b
			path_keyword="path")
Packit 34410b
Packit 34410b
	def create_transfer_reply(self, path, properties):
Packit 34410b
		self.transfer_path = path
Packit 34410b
		self.transfer_size = properties["Size"]
Packit 34410b
		if self.verbose:
Packit 34410b
			print("Transfer created: %s" % path)
Packit 34410b
Packit 34410b
	def error(self, err):
Packit 34410b
		print(err)
Packit 34410b
		mainloop.quit()
Packit 34410b
Packit 34410b
	def properties_changed(self, interface, properties, invalidated, path):
Packit 34410b
		if path != self.transfer_path:
Packit 34410b
			return
Packit 34410b
Packit 34410b
		if "Status" in properties and \
Packit 34410b
				(properties["Status"] == "complete" or \
Packit 34410b
				properties["Status"] == "error"):
Packit 34410b
			if self.verbose:
Packit 34410b
				print("Transfer %s" % properties["Status"])
Packit 34410b
			mainloop.quit()
Packit 34410b
			return
Packit 34410b
Packit 34410b
		if "Transferred" not in properties:
Packit 34410b
			return
Packit 34410b
Packit 34410b
		value = properties["Transferred"]
Packit 34410b
		speed = (value - self.transferred) / 1000
Packit 34410b
		print("Transfer progress %d/%d at %d kBps" % (value,
Packit 34410b
							self.transfer_size,
Packit 34410b
							speed))
Packit 34410b
		self.transferred = value
Packit 34410b
Packit 34410b
	def pull_business_card(self, filename):
Packit 34410b
		self.opp.PullBusinessCard(os.path.abspath(filename),
Packit 34410b
				reply_handler=self.create_transfer_reply,
Packit 34410b
				error_handler=self.error)
Packit 34410b
Packit 34410b
	def send_file(self, filename):
Packit 34410b
		self.opp.SendFile(os.path.abspath(filename),
Packit 34410b
				reply_handler=self.create_transfer_reply,
Packit 34410b
				error_handler=self.error)
Packit 34410b
Packit 34410b
if  __name__ == '__main__':
Packit 34410b
Packit 34410b
	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Packit 34410b
Packit 34410b
	parser = OptionParser()
Packit 34410b
Packit 34410b
	(options, args) = parse_options()
Packit 34410b
Packit 34410b
	if not options.device:
Packit 34410b
		parser.print_help()
Packit 34410b
		sys.exit(0)
Packit 34410b
Packit 34410b
	bus = dbus.SessionBus()
Packit 34410b
	mainloop = GObject.MainLoop()
Packit 34410b
Packit 34410b
	client = dbus.Interface(bus.get_object(BUS_NAME, PATH),
Packit 34410b
							CLIENT_INTERFACE)
Packit 34410b
Packit 34410b
	print("Creating Session")
Packit 34410b
	path = client.CreateSession(options.device, { "Target": "OPP" })
Packit 34410b
Packit 34410b
	opp_client = OppClient(path, options.verbose)
Packit 34410b
Packit 34410b
	if options.pull_to_file:
Packit 34410b
		opp_client.pull_business_card(options.pull_to_file)
Packit 34410b
Packit 34410b
	if options.send_file:
Packit 34410b
		opp_client.send_file(options.send_file)
Packit 34410b
Packit 34410b
	mainloop.run()