00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 import dbus, gobject, dbus.glib
00035
00036 from python_qt_binding.QtGui import QComboBox
00037
00038 import collections
00039
00040 NetworkRobot = collections.namedtuple('NetworkRobot', ['name', 'address', 'port'])
00041
00042
00043 class avahi:
00044 DBUS_NAME = "org.freedesktop.Avahi"
00045 DBUS_INTERFACE_SERVER = DBUS_NAME + ".Server"
00046 DBUS_PATH_SERVER = "/"
00047 DBUS_INTERFACE_ENTRY_GROUP = DBUS_NAME + ".EntryGroup"
00048 DBUS_INTERFACE_DOMAIN_BROWSER = DBUS_NAME + ".DomainBrowser"
00049 DBUS_INTERFACE_SERVICE_TYPE_BROWSER = DBUS_NAME + ".ServiceTypeBrowser"
00050 DBUS_INTERFACE_SERVICE_BROWSER = DBUS_NAME + ".ServiceBrowser"
00051 DBUS_INTERFACE_ADDRESS_RESOLVER = DBUS_NAME + ".AddressResolver"
00052 DBUS_INTERFACE_HOST_NAME_RESOLVER = DBUS_NAME + ".HostNameResolver"
00053 DBUS_INTERFACE_SERVICE_RESOLVER = DBUS_NAME + ".ServiceResolver"
00054 DBUS_INTERFACE_RECORD_BROWSER = DBUS_NAME + ".RecordBrowser"
00055 PROTO_UNSPEC, PROTO_INET, PROTO_INET6 = -1, 0, 1
00056 IF_UNSPEC = -1
00057 LOOKUP_RESULT_CACHED = 1
00058 LOOKUP_RESULT_WIDE_AREA = 2
00059 LOOKUP_RESULT_MULTICAST = 4
00060 LOOKUP_RESULT_LOCAL = 8
00061 LOOKUP_RESULT_OUR_OWN = 16
00062 LOOKUP_RESULT_STATIC = 32
00063
00064
00065 class AvahiWidget(QComboBox):
00066 def __init__(self):
00067 super(AvahiWidget, self).__init__()
00068 self.setSizeAdjustPolicy(QComboBox.AdjustToContents)
00069 self.setInsertPolicy(QComboBox.InsertAlphabetically)
00070 self.setEditable(True)
00071
00072 gobject.threads_init()
00073 dbus.glib.threads_init()
00074 self.robots = set()
00075 self.sys_bus = dbus.SystemBus()
00076 self.avahi_server = dbus.Interface(self.sys_bus.get_object(avahi.DBUS_NAME, '/'), avahi.DBUS_INTERFACE_SERVER)
00077
00078 self.sbrowser = dbus.Interface(self.sys_bus.get_object(avahi.DBUS_NAME, self.avahi_server.ServiceBrowserNew(avahi.IF_UNSPEC,
00079 avahi.PROTO_INET, '_naoqi._tcp', 'local', dbus.UInt32(0))), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
00080
00081 self.sbrowser.connect_to_signal("ItemNew", self.avahiNewItem)
00082 self.sbrowser.connect_to_signal("ItemRemove", self.avahiItemRemove)
00083
00084 def avahiNewItem(self, interface, protocol, name, stype, domain, flags):
00085 self.avahi_server.ResolveService(interface, protocol, name, stype,
00086 domain, avahi.PROTO_INET, dbus.UInt32(0),
00087 reply_handler=self.service_resolved, error_handler=self.print_error)
00088
00089 def avahiItemRemove(self, interface, protocol, name, stype, domain, flags):
00090 tup = NetworkRobot(name, address, port)
00091 if tup in self.robots:
00092 self.robots.remove(tup)
00093 updateRobotCombobox();
00094
00095 def service_resolved(self, interface, protocol, name, type, domain, host, aprotocol, address, port, txt, flags):
00096 self.robots.add(NetworkRobot(name, address, port))
00097 self.updateRobotCombobox()
00098
00099 def updateRobotCombobox(self):
00100 selected = self.currentText()
00101 for i in range(self.count()):
00102 self.removeItem(0)
00103 id = -1
00104 texts = []
00105 for robot in self.robots:
00106 text = '%s:%s' % (robot.address, robot.port)
00107 text_full = "%s (%s)" % (robot.name, text)
00108 texts.append((text_full, text))
00109 texts = sorted(texts, key=lambda s: s[0].lower())
00110 for text in texts:
00111 self.addItem(text[0], text[1])
00112 if(text[0] == selected):
00113 id = self.count()-1;
00114
00115 if(self.count() == 1):
00116 self.setCurrentIndex(0)
00117 elif(id > -1):
00118 self.setCurrentIndex(id)
00119
00120 def print_error(self, *args):
00121 print('error_handler')
00122 print(args)