5 # This file is part of avahi.
7 # avahi is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
12 # avahi is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 # License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with avahi; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 import avahi, gtk, gobject, dbus, avahi.ServiceTypeDatabase
26 from avahi.SimpleGladeApp import SimpleGladeApp
27 except ImportError, e:
28 print "Sorry, to use this tool you need to install Avahi, pygtk and python-dbus.\n Error: %s" % e
33 # It's really important to do this, else you won't see any events
36 from dbus.dbus_bindings import DBusException
38 except ImportError, e:
41 service_type_browsers = {}
45 d = gtk.MessageDialog(parent=None, flags=gtk.DIALOG_MODAL,
46 type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
52 glade_dir = "@interfacesdir@"
54 service_type_db = avahi.ServiceTypeDatabase.ServiceTypeDatabase()
56 class Main_window(SimpleGladeApp):
57 def __init__(self, path="avahi-discover.glade", root="main_window", domain=None, **kwargs):
58 path = os.path.join(glade_dir, path)
59 gtk.window_set_default_icon_name("gnome-networktool")
60 SimpleGladeApp.__init__(self, path, root, domain, **kwargs)
62 def on_tree_view_cursor_changed(self, widget, *args):
63 (model, iter) = widget.get_selection().get_selected()
64 (name,interface,protocol,stype,domain) = self.treemodel.get(iter,1,2,3,4,5)
66 self.info_label.set_markup("<i>No service currently selected.</i>")
68 #Asynchronous resolving
69 self.server.ResolveService( int(interface), int(protocol), name, stype, domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), reply_handler=self.service_resolved, error_handler=self.print_error)
71 def protoname(self,protocol):
72 if protocol == avahi.PROTO_INET:
74 if protocol == avahi.PROTO_INET6:
78 def siocgifname(self, interface):
82 return self.server.GetNetworkInterfaceNameByIndex(interface)
84 def get_interface_name(self, interface, protocol):
85 if interface == avahi.IF_UNSPEC and protocol == avahi.PROTO_UNSPEC:
88 return str(self.siocgifname(interface)) + " " + str(self.protoname(protocol))
90 def service_resolved(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
91 print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, stype, domain, interface, protocol)
93 print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt)))
95 self.update_label(interface, protocol, name, stype, domain, host, aprotocol, address, port, avahi.txt_array_to_string_array(txt))
97 def print_error(self, err):
98 error_label = "<b>Error:</b> %s" % (err)
99 self.info_label.set_markup(error_label)
100 print "Error:", str(err)
102 def lookup_type(self, stype):
103 global service_type_db
106 return service_type_db[stype]
110 def new_service(self, interface, protocol, name, stype, domain, flags):
111 print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, stype, domain, interface, protocol)
112 if self.zc_ifaces.has_key((interface,protocol)) == False:
114 ifn = self.get_interface_name(interface, protocol)
116 self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, ifn, None,interface,protocol,None,domain)
117 if self.zc_domains.has_key((interface,protocol,domain)) == False:
118 self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
119 if self.zc_types.has_key((interface,protocol,stype,domain)) == False:
120 thisDomain = self.zc_domains[(interface,protocol,domain)]
121 self.zc_types[(interface,protocol,stype,domain)] = self.insert_row(self.treemodel, thisDomain, self.lookup_type(stype), name, interface,None,None,None)
122 treeiter = self.insert_row(self.treemodel,self.zc_types[(interface,protocol,stype,domain)], name, name, interface,protocol,stype,domain)
123 self.services_browsed[(interface, protocol, name, stype, domain)] = treeiter
124 # expand the tree of this path
125 self.tree_view.expand_to_path(self.treemodel.get_path(treeiter))
127 def remove_service(self, interface, protocol, name, stype, domain, flags):
128 print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, stype, domain, interface, protocol)
129 self.info_label.set_markup("")
130 treeiter=self.services_browsed[(interface, protocol, name, stype, domain)]
131 parent = self.treemodel.iter_parent(treeiter)
132 self.treemodel.remove(treeiter)
133 del self.services_browsed[(interface, protocol, name, stype, domain)]
134 if self.treemodel.iter_has_child(parent) == False:
135 treeiter=self.zc_types[(interface,protocol,stype,domain)]
136 parent = self.treemodel.iter_parent(treeiter)
137 self.treemodel.remove(treeiter)
138 del self.zc_types[(interface,protocol,stype,domain)]
139 if self.treemodel.iter_has_child(parent) == False:
140 treeiter=self.zc_domains[(interface,protocol,domain)]
141 parent = self.treemodel.iter_parent(treeiter)
142 self.treemodel.remove(treeiter)
143 del self.zc_domains[(interface,protocol,domain)]
144 if self.treemodel.iter_has_child(parent) == False:
145 treeiter=self.zc_ifaces[(interface,protocol)]
146 parent = self.treemodel.iter_parent(treeiter)
147 self.treemodel.remove(treeiter)
148 del self.zc_ifaces[(interface,protocol)]
150 def new_service_type(self, interface, protocol, stype, domain, flags):
151 global service_browsers
153 # Are we already browsing this domain for this type?
154 if service_browsers.has_key((interface, protocol, stype, domain)):
157 print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (stype, domain, interface, protocol)
159 b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(interface, protocol, stype, domain, dbus.UInt32(0))), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
160 b.connect_to_signal('ItemNew', self.new_service)
161 b.connect_to_signal('ItemRemove', self.remove_service)
163 service_browsers[(interface, protocol, stype, domain)] = b
165 def browse_domain(self, interface, protocol, domain):
166 global service_type_browsers
168 # Are we already browsing this domain?
169 if service_type_browsers.has_key((interface, protocol, domain)):
172 if self.stype is None:
173 print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol)
176 b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceTypeBrowserNew(interface, protocol, domain, dbus.UInt32(0))), avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
177 except DBusException, e:
179 error_msg("You should check that the avahi daemon is running.\n\nError : %s" % e)
182 b.connect_to_signal('ItemNew', self.new_service_type)
184 service_type_browsers[(interface, protocol, domain)] = b
186 new_service_type(interface, protocol, stype, domain)
188 def new_domain(self,interface, protocol, domain, flags):
189 if self.zc_ifaces.has_key((interface,protocol)) == False:
190 ifn = self.get_interface_name(interface, protocol)
191 self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, ifn,None,interface,protocol,None,domain)
192 if self.zc_domains.has_key((interface,protocol,domain)) == False:
193 self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
194 if domain != "local":
195 self.browse_domain(interface, protocol, domain)
197 def pair_to_dict(self, l):
203 tmp = el.split('=',1)
209 def update_label(self,interface, protocol, name, stype, domain, host, aprotocol, address, port, txt):
212 txtd = self.pair_to_dict(txt)
213 for k,v in txtd.items():
214 txts+="<b>TXT <i>%s</i></b> = %s\n" % (k,v)
216 txts = "<b>TXT Data:</b> <i>empty</i>"
218 infos = "<b>Service Type:</b> %s\n<b>Service Name:</b> %s\n<b>Domain Name:</b> %s\n<b>Interface:</b> %s %s\n<b>Address:</b> %s/%s:%i\n%s" % (stype, name, domain, self.siocgifname(interface), self.protoname(protocol), host, address, port, txts.strip())
219 self.info_label.set_markup(infos)
221 def insert_row(self, model,parent,
222 content, name, interface,protocol,stype,domain):
223 myiter=model.insert_after(parent,None)
224 model.set(myiter,0,content,1,name,2,interface,3,protocol,4,stype,5,domain)
228 print "A new main_window has been created"
229 self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
230 self.tree_view.set_model(self.treemodel)
232 #creating the columns headers
233 self.tree_view.set_headers_visible(False)
234 renderer=gtk.CellRendererText()
235 column=gtk.TreeViewColumn("",renderer, text=0)
236 column.set_resizable(True)
237 column.set_sizing("GTK_TREE_VIEW_COLUMN_GROW_ONLY");
238 column.set_expand(True);
239 self.tree_view.append_column(column)
246 self.services_browsed = {}
248 self.bus = dbus.SystemBus()
249 self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
251 if self.domain is None:
252 # Explicitly browse .local
253 self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
255 # Browse for other browsable domains
256 db = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE, dbus.UInt32(0))), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
257 db.connect_to_signal('ItemNew', self.new_domain)
259 # Just browse the domain the user wants us to browse
260 self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
264 main_window = Main_window()
268 if __name__ == "__main__":