]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-discover.in
* add an handler in glade to handle the destroy event
[catta] / avahi-utils / 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, gtk, gobject, dbus
26     from avahi.SimpleGladeApp import SimpleGladeApp
27 except ImportError:
28     print "Sorry, to use this tool you need to install Avahi, pygtk and python-dbus."
29     sys.exit(1)
30
31 try:
32     import dbus.glib
33 except ImportError, e:
34     pass
35
36 service_type_browsers = {}
37 service_browsers = {}
38
39 glade_dir = "@interfacesdir@"
40
41 class Main_window(SimpleGladeApp):
42     def __init__(self, path="avahi-discover.glade", root="main_window", domain=None, **kwargs):
43         path = os.path.join(glade_dir, path)
44         SimpleGladeApp.__init__(self, path, root, domain, **kwargs)
45
46     def on_tree_view_cursor_changed(self, widget, *args):
47         (model, iter) = widget.get_selection().get_selected()
48         (name,interface,protocol,type,domain) = self.treemodel.get(iter,1,2,3,4,5)
49         if type == None:
50             self.info_label.set_markup("<i>No service currently selected.</i>")
51             return
52         #Asynchronous resolving
53         self.server.ResolveService( int(interface), int(protocol), name, type, domain, avahi.PROTO_UNSPEC, reply_handler=self.service_resolved, error_handler=self.print_error)
54
55
56     def protoname(self,protocol):
57         if protocol == avahi.PROTO_INET:
58             return "IPv4"
59         if protocol == avahi.PROTO_INET6:
60             return "IPv6"
61             
62             
63     def siocgifname(self, interface):
64         if interface <= 0:
65             return "any"
66         else:
67             return self.server.GetNetworkInterfaceNameByIndex(interface)
68                         
69     def service_resolved(self, interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
70         print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, type, domain, interface, protocol)
71         print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt)))
72         self.update_label(interface, protocol, name, type, domain, host, aprotocol, address, port, str(avahi.txt_array_to_string_array(txt)))
73         
74     def print_error(self, err):
75         error_label = "<b>Error:</b> %s" % (err)
76         self.info_label.set_markup(error_label)
77         print "Error:", str(err)
78             
79     def new_service(self, interface, protocol, name, type, domain):
80         print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, type, domain, interface, protocol)
81         if self.zc_ifaces.has_key((interface,protocol)) == False:
82             self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, str(self.siocgifname(interface))+" "+str(self.protoname(protocol)),None,interface,protocol,None,domain)
83         if self.zc_domains.has_key((interface,protocol,domain)) == False:
84             self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
85         if self.zc_types.has_key((interface,protocol,type,domain)) == False:
86             thisDomain = self.zc_domains[(interface,protocol,domain)]
87             self.zc_types[(interface,protocol,type,domain)] = self.insert_row(self.treemodel, thisDomain, type, name, interface,None,None,None)
88         treeiter = self.insert_row(self.treemodel,self.zc_types[(interface,protocol,type,domain)], name, name, interface,protocol,type,domain)
89         self.services_browsed[(interface, protocol, name, type, domain)] = treeiter
90         # expand the tree of this path
91         self.tree_view.expand_to_path(self.treemodel.get_path(treeiter))
92
93
94     def remove_service(self, interface, protocol, name, type, domain):
95         print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, type, domain, interface, protocol)
96         self.info_label.set_markup("")
97         treeiter=self.services_browsed[(interface, protocol, name, type, domain)]
98         parent = self.treemodel.iter_parent(treeiter)
99         self.treemodel.remove(treeiter)
100         del self.services_browsed[(interface, protocol, name, type, domain)]
101         if self.treemodel.iter_has_child(parent) == False:
102             treeiter=self.zc_types[(interface,protocol,type,domain)]
103             parent = self.treemodel.iter_parent(treeiter)
104             self.treemodel.remove(treeiter)
105             del self.zc_types[(interface,protocol,type,domain)]
106             if self.treemodel.iter_has_child(parent) == False:
107                 treeiter=self.zc_domains[(interface,protocol,domain)]
108                 parent = self.treemodel.iter_parent(treeiter)
109                 self.treemodel.remove(treeiter)
110                 del self.zc_domains[(interface,protocol,domain)]
111                 if self.treemodel.iter_has_child(parent) == False:
112                     treeiter=self.zc_ifaces[(interface,protocol)]
113                     parent = self.treemodel.iter_parent(treeiter)
114                     self.treemodel.remove(treeiter)
115                     del self.zc_ifaces[(interface,protocol)]
116
117  
118     def new_service_type(self, interface, protocol, type, domain):
119         global service_browsers
120
121         # Are we already browsing this domain for this type? 
122         if service_browsers.has_key((interface, protocol, type, domain)):
123             return
124         
125         print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (type, domain, interface, protocol)
126         
127         b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(interface, protocol, type, domain)),  avahi.DBUS_INTERFACE_SERVICE_BROWSER)
128         b.connect_to_signal('ItemNew', self.new_service)
129         b.connect_to_signal('ItemRemove', self.remove_service)
130
131         service_browsers[(interface, protocol, type, domain)] = b
132
133     def browse_domain(self, interface, protocol, domain):
134         global service_type_browsers
135
136         # Are we already browsing this domain?
137         if service_type_browsers.has_key((interface, protocol, domain)):
138             return
139
140         if self.stype is None:
141             print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol)
142         
143             b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceTypeBrowserNew(interface, protocol, domain)),  avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
144             b.connect_to_signal('ItemNew', self.new_service_type)
145
146             service_type_browsers[(interface, protocol, domain)] = b
147         else:
148             new_service_type(interface, protocol, stype, domain)
149
150     def new_domain(self,interface, protocol, domain):
151         if self.zc_ifaces.has_key((interface,protocol)) == False:
152             self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, str(self.siocgifname(interface))+" "+str(self.protoname(protocol)),None,interface,protocol,None,domain)
153         if self.zc_domains.has_key((interface,protocol,domain)) == False:
154             self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
155         if domain != "local":
156             self.browse_domain(interface, protocol, domain)
157
158     def update_label(self,interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
159         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<b>TXT Data:</b> %s" % (type, name, domain, self.siocgifname(interface), self.protoname(protocol), host, address, port, str(txt))
160         self.info_label.set_markup(infos)
161
162     def insert_row(self, model,parent,
163                    content, name, interface,protocol,type,domain):
164         myiter=model.insert_after(parent,None)
165         model.set(myiter,0,content,1,name,2,interface,3,protocol,4,type,5,domain)
166         return myiter
167
168     def new(self):
169         print "A new main_window has been created"
170         self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
171         self.tree_view.set_model(self.treemodel)
172
173         #creating the columns headers
174         self.tree_view.set_headers_visible(False)
175         renderer=gtk.CellRendererText()
176         column=gtk.TreeViewColumn("",renderer, text=0)
177         column.set_resizable(True)
178         column.set_sizing("GTK_TREE_VIEW_COLUMN_GROW_ONLY");
179         column.set_expand(True);
180         self.tree_view.append_column(column)
181
182         self.domain = None
183         self.stype = None
184         self.zc_ifaces = {}
185         self.zc_domains = {}
186         self.zc_types = {}
187         self.services_browsed = {}
188         
189         self.bus = dbus.SystemBus()
190         self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
191
192
193         if self.domain is None:
194             # Explicitly browse .local
195             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
196                         
197             # Browse for other browsable domains
198             db = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE)), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
199             db.connect_to_signal('ItemNew', self.new_domain)
200         else:
201             # Just browse the domain the user wants us to browse
202             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
203
204
205         
206 def main():
207     main_window = Main_window()
208
209     main_window.run()
210     
211 if __name__ == "__main__":
212     main()
213
214