]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-discover.in
* update svn:ignore and svn:keywords for many files and directories
[catta] / avahi-utils / avahi-discover.in
1 #!/usr/bin/env @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
23 import gtk
24 import gobject
25
26 import avahi, dbus, gobject, sys
27 from avahi.SimpleGladeApp import SimpleGladeApp
28
29 try:
30     import dbus.glib
31 except ImportError, e:
32     pass
33
34 service_type_browsers = {}
35 service_browsers = {}
36
37
38 glade_dir = "@prefix@/share/@PACKAGE@/interfaces"
39
40 class Main_window(SimpleGladeApp):
41     def __init__(self, path="avahi-discover.glade", root="main_window", domain=None, **kwargs):
42         path = os.path.join(glade_dir, path)
43         SimpleGladeApp.__init__(self, path, root, domain, **kwargs)
44
45     def on_tree_view_cursor_changed(self, widget, *args):
46         (model, iter) = widget.get_selection().get_selected()
47         (name,interface,protocol,type,domain) = self.treemodel.get(iter,0,1,2,3,4)
48         if type == None:
49             self.info_label.set_markup("<i>No service currently selected.</i>")
50             return
51         #Asynchronous resolving
52         self.server.ResolveService( int(interface), int(protocol), name, type, domain, avahi.PROTO_UNSPEC, reply_handler=self.service_resolved, error_handler=self.print_error)
53
54
55     def service_resolved(self, interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
56         print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, type, domain, interface, protocol)
57         print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(txt))
58         self.update_label(interface, protocol, name, type, domain, host, aprotocol, address, port, str(txt))
59         
60     def print_error(err):
61         print "Error:", str(err)
62             
63     def new_service(self, interface, protocol, name, type, domain):
64         print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, type, domain, interface, protocol)
65         if self.service_type.has_key(type) == False:
66             self.service_type[type] = self.insert_row(self.treemodel,None,type, interface,None,None,None)
67         treeiter = self.insert_row(self.treemodel,self.service_type[type],name, interface,protocol,type,domain)
68         self.services_browsed[(interface, protocol, name, type, domain)] = treeiter
69
70
71     def remove_service(self, interface, protocol, name, type, domain):
72         print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, type, domain, interface, protocol)
73         treeiter=self.services_browsed[(interface, protocol, name, type, domain)]
74         parent = self.treemodel.iter_parent(treeiter)
75         self.treemodel.remove(treeiter)
76         del self.services_browsed[(interface, protocol, name, type, domain)]
77         if self.treemodel.iter_has_child(parent) == False:
78             self.treemodel.remove(parent)
79             del self.service_type[type]
80  
81     def new_service_type(self, interface, protocol, type, domain):
82         global service_browsers
83
84         # Are we already browsing this domain for this type? 
85         if service_browsers.has_key((interface, protocol, type, domain)):
86             return
87         
88         print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (type, domain, interface, protocol)
89         
90         b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(interface, protocol, type, domain)),  avahi.DBUS_INTERFACE_SERVICE_BROWSER)
91         b.connect_to_signal('ItemNew', self.new_service)
92         b.connect_to_signal('ItemRemove', self.remove_service)
93
94         service_browsers[(interface, protocol, type, domain)] = b
95
96     def browse_domain(self, interface, protocol, domain):
97         global service_type_browsers
98
99         # Are we already browsing this domain?
100         if service_type_browsers.has_key((interface, protocol, domain)):
101             return
102             
103         print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol)
104         
105         b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceTypeBrowserNew(interface, protocol, domain)),  avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
106         b.connect_to_signal('ItemNew', self.new_service_type)
107
108         service_type_browsers[(interface, protocol, domain)] = b
109
110     def new_domain(self,interface, protocol, domain):
111
112         # We browse for .local anyway...
113         if domain != "local":
114             browse_domain(interface, protocol, domain)
115
116     def update_label(self,interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
117         if protocol == avahi.PROTO_INET:
118             protocol = "IPv4"
119         if protocol == avahi.PROTO_INET6:
120             protocol = "IPv6"
121         infos = "<b>Service Type:</b> %s\n<b>Service Name:</b> %s\n<b>Domain Name:</b> %s\n<b>Interface:</b>#%i %s\n<b>Address:</b> %s/%s:%i\n<b>TXT Data:</b> %s" % (type, name, domain, interface, protocol, host, address, port, str(txt))
122         self.info_label.set_markup(infos)
123
124     def insert_row(self, model,parent,
125                    name, interface,protocol,type,domain):
126         myiter=model.insert_after(parent,None)
127         model.set(myiter,0,name,1,interface,2,protocol,3,type,4,domain)
128         return myiter
129     
130     def new(self):
131         print "A new main_window has been created"
132         self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
133         self.tree_view.set_model(self.treemodel)
134
135         #creating the columns headers
136         self.tree_view.set_headers_visible(True)
137         renderer=gtk.CellRendererText()
138         column=gtk.TreeViewColumn("Name",renderer, text=0)
139         column.set_resizable(True)
140         column.set_sizing("GTK_TREE_VIEW_COLUMN_GROW_ONLY");
141         column.set_expand(True);
142         self.tree_view.append_column(column)
143         renderer=gtk.CellRendererText()
144         column=gtk.TreeViewColumn("Interface",renderer,
145                                   text=1)
146         column.set_resizable(True)
147         self.tree_view.append_column(column)
148         
149         # testing with fake data
150         self.service_type = {}
151         self.services_browsed = {}
152         
153         model=self.treemodel
154
155         domain = None
156
157         self.bus = dbus.SystemBus()
158         self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
159
160         if domain is None:
161             # Explicitly browse .local
162             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
163
164             # Browse for other browsable domains
165             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)
166             db.connect_to_signal('ItemNew', self.new_domain)
167
168         else:
169             # Just browse the domain the user wants us to browse
170             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
171
172
173         
174 def main():
175     main_window = Main_window()
176
177     main_window.run()
178     
179 if __name__ == "__main__":
180     main()
181
182