]> git.meshlink.io Git - catta/blob - avahi-python/avahi-discover/avahi-discover.in
6c22f45568598a337df556dc51ca68e43b21f3f0
[catta] / avahi-python / avahi-discover / avahi-discover.in
1 #!@PYTHON@
2 # -*-python-*-
3 # $Id$
4
5 # This file is part of avahi.
6 #
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.
11 #
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.
16 #
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
20 # USA.
21
22 import os, sys
23
24 try:
25     import avahi, gettext, gtk, gobject, dbus, avahi.ServiceTypeDatabase
26     _ = gettext.gettext
27 except ImportError, e:
28     print "Sorry, to use this tool you need to install Avahi, pygtk and python-dbus.\n Error: %s" % e
29     sys.exit(1)
30
31
32 ## !!NOTE!! ##
33 # It's really important to do this, else you won't see any events
34 ##
35 try:
36     from dbus import DBusException
37     import dbus.glib
38 except ImportError, e:
39     pass
40
41 service_type_browsers = {}
42 service_browsers = {}
43
44 def error_msg(msg):
45     d = gtk.MessageDialog(parent=None, flags=gtk.DIALOG_MODAL,
46                           type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
47     d.set_markup(msg)
48     d.show_all()
49     d.run()
50     d.destroy()
51
52 ui_dir = "@interfacesdir@"
53
54 service_type_db = avahi.ServiceTypeDatabase.ServiceTypeDatabase()
55
56 class Main_window:
57     def __init__(self, path="avahi-discover.ui", root="main_window", domain=None, **kwargs):
58         path = os.path.join(ui_dir, path)
59         gtk.window_set_default_icon_name("network-wired")
60         self.ui = gtk.Builder()
61         self.ui.add_from_file(path)
62         self.ui.connect_signals(self)
63         self.tree_view = self.ui.get_object("tree_view")
64         self.info_label = self.ui.get_object("info_label")
65         self.new()
66
67     def on_tree_view_cursor_changed(self, widget, *args):
68         (model, iter) = widget.get_selection().get_selected()
69         stype = None
70         if iter is not None:
71             (name,interface,protocol,stype,domain) = self.treemodel.get(iter,1,2,3,4,5)
72         if stype == None:
73             self.info_label.set_markup("<i>No service currently selected.</i>")
74             return
75         #Asynchronous resolving
76         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)
77
78     def protoname(self,protocol):
79         if protocol == avahi.PROTO_INET:
80             return "IPv4"
81         if protocol == avahi.PROTO_INET6:
82             return "IPv6"
83         return "n/a"
84             
85     def siocgifname(self, interface):
86         if interface <= 0:
87             return "n/a"
88         else:
89             return self.server.GetNetworkInterfaceNameByIndex(interface)
90
91     def get_interface_name(self, interface, protocol):
92         if interface == avahi.IF_UNSPEC and protocol == avahi.PROTO_UNSPEC:
93             return "Wide Area"
94         else:
95             return str(self.siocgifname(interface)) + " " + str(self.protoname(protocol))
96                         
97     def service_resolved(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
98         print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, stype, domain, interface, protocol)
99
100         print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt)))
101
102         self.update_label(interface, protocol, name, stype, domain, host, aprotocol, address, port, avahi.txt_array_to_string_array(txt))
103         
104     def print_error(self, err):
105         error_label = "<b>Error:</b> %s" % (err)
106         self.info_label.set_markup(error_label)
107         print "Error:", str(err)
108
109     def lookup_type(self, stype):
110         global service_type_db
111
112         try:
113             return service_type_db[stype]
114         except KeyError:
115             return stype
116             
117     def new_service(self, interface, protocol, name, stype, domain, flags):
118         print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, stype, domain, interface, protocol)
119         if self.zc_ifaces.has_key((interface,protocol)) == False:
120
121             ifn = self.get_interface_name(interface, protocol)
122             
123             self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, ifn, None,interface,protocol,None,domain)
124         if self.zc_domains.has_key((interface,protocol,domain)) == False:
125             self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
126         if self.zc_types.has_key((interface,protocol,stype,domain)) == False:
127             thisDomain = self.zc_domains[(interface,protocol,domain)]
128             self.zc_types[(interface,protocol,stype,domain)] = self.insert_row(self.treemodel, thisDomain, self.lookup_type(stype), name, interface,None,None,None)
129         treeiter = self.insert_row(self.treemodel,self.zc_types[(interface,protocol,stype,domain)], name, name, interface,protocol,stype,domain)
130         self.services_browsed[(interface, protocol, name, stype, domain)] = treeiter
131         # expand the tree of this path
132         self.tree_view.expand_to_path(self.treemodel.get_path(treeiter))
133
134     def remove_service(self, interface, protocol, name, stype, domain, flags):
135         print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, stype, domain, interface, protocol)
136         self.info_label.set_markup("")
137         treeiter=self.services_browsed[(interface, protocol, name, stype, domain)]
138         parent = self.treemodel.iter_parent(treeiter)
139         self.treemodel.remove(treeiter)
140         del self.services_browsed[(interface, protocol, name, stype, domain)]
141         if self.treemodel.iter_has_child(parent) == False:
142             treeiter=self.zc_types[(interface,protocol,stype,domain)]
143             parent = self.treemodel.iter_parent(treeiter)
144             self.treemodel.remove(treeiter)
145             del self.zc_types[(interface,protocol,stype,domain)]
146             if self.treemodel.iter_has_child(parent) == False:
147                 treeiter=self.zc_domains[(interface,protocol,domain)]
148                 parent = self.treemodel.iter_parent(treeiter)
149                 self.treemodel.remove(treeiter)
150                 del self.zc_domains[(interface,protocol,domain)]
151                 if self.treemodel.iter_has_child(parent) == False:
152                     treeiter=self.zc_ifaces[(interface,protocol)]
153                     parent = self.treemodel.iter_parent(treeiter)
154                     self.treemodel.remove(treeiter)
155                     del self.zc_ifaces[(interface,protocol)]
156  
157     def new_service_type(self, interface, protocol, stype, domain, flags):
158         global service_browsers
159
160         # Are we already browsing this domain for this type? 
161         if service_browsers.has_key((interface, protocol, stype, domain)):
162             return
163         
164         print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (stype, domain, interface, protocol)
165         
166         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)
167         b.connect_to_signal('ItemNew', self.new_service)
168         b.connect_to_signal('ItemRemove', self.remove_service)
169
170         service_browsers[(interface, protocol, stype, domain)] = b
171
172     def browse_domain(self, interface, protocol, domain):
173         global service_type_browsers
174
175         # Are we already browsing this domain?
176         if service_type_browsers.has_key((interface, protocol, domain)):
177             return
178
179         if self.stype is None:
180             print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol)
181
182             try:
183                 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)
184             except DBusException, e:
185                 print e
186                 error_msg("You should check that the avahi daemon is running.\n\nError : %s" % e)
187                 sys.exit(0)
188                 
189             b.connect_to_signal('ItemNew', self.new_service_type)
190
191             service_type_browsers[(interface, protocol, domain)] = b
192         else:
193             new_service_type(interface, protocol, stype, domain)
194
195     def new_domain(self,interface, protocol, domain, flags):
196         if self.zc_ifaces.has_key((interface,protocol)) == False:
197             ifn = self.get_interface_name(interface, protocol)
198             self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, ifn,None,interface,protocol,None,domain)
199         if self.zc_domains.has_key((interface,protocol,domain)) == False:
200             self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
201         if domain != "local":
202             self.browse_domain(interface, protocol, domain)
203
204     def pair_to_dict(self, l):
205         res = dict()
206         for el in l:
207             if "=" not in el:
208                 res[el]=''
209             else:
210                 tmp = el.split('=',1)
211                 if len(tmp[0]) > 0:
212                     res[tmp[0]] = tmp[1]
213         return res
214                                                                             
215
216     def update_label(self,interface, protocol, name, stype, domain, host, aprotocol, address, port, txt):
217         if len(txt) != 0:
218             txts = ""
219             txtd = self.pair_to_dict(txt)
220             for k,v in txtd.items():
221                 txts+="<b>TXT <i>%s</i></b> = %s\n" % (k,v)
222         else:
223             txts = "<b>TXT Data:</b> <i>empty</i>"
224
225         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())
226         self.info_label.set_markup(infos)
227
228     def insert_row(self, model,parent,
229                    content, name, interface,protocol,stype,domain):
230         myiter=model.insert_after(parent,None)
231         model.set(myiter,0,content,1,name,2,interface,3,protocol,4,stype,5,domain)
232         return myiter
233
234     def new(self):
235         print "A new main_window has been created"
236         self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
237         self.tree_view.set_model(self.treemodel)
238
239         #creating the columns headers
240         self.tree_view.set_headers_visible(False)
241         renderer=gtk.CellRendererText()
242         column=gtk.TreeViewColumn("",renderer, text=0)
243         column.set_resizable(True)
244         column.set_sizing("GTK_TREE_VIEW_COLUMN_GROW_ONLY");
245         column.set_expand(True);
246         self.tree_view.append_column(column)
247
248         self.domain = None
249         self.stype = None
250         self.zc_ifaces = {}
251         self.zc_domains = {}
252         self.zc_types = {}
253         self.services_browsed = {}
254         
255         self.bus = dbus.SystemBus()
256         self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
257
258         if self.domain is None:
259             # Explicitly browse .local
260             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
261                         
262             # Browse for other browsable domains
263             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)
264             db.connect_to_signal('ItemNew', self.new_domain)
265         else:
266             # Just browse the domain the user wants us to browse
267             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
268
269     def gtk_main_quit(self, *args):
270         gtk.main_quit()
271
272 def main():
273     main_window = Main_window()
274     gtk.main()
275     
276 if __name__ == "__main__":
277     main()
278
279