]> git.meshlink.io Git - catta/blobdiff - avahi-python/avahi-discover/avahi-discover.in
avahi-browse: replace unprintable chars when printing services names
[catta] / avahi-python / avahi-discover / avahi-discover.in
index 6c22f45568598a337df556dc51ca68e43b21f3f0..ac6b65783332b3d372f79d3951450ef2b0a1246c 100755 (executable)
@@ -1,7 +1,5 @@
 #!@PYTHON@
 # -*-python-*-
-# $Id$
-
 # This file is part of avahi.
 #
 # avahi is free software; you can redistribute it and/or modify it
@@ -27,6 +25,9 @@ try:
 except ImportError, e:
     print "Sorry, to use this tool you need to install Avahi, pygtk and python-dbus.\n Error: %s" % e
     sys.exit(1)
+except Exception, e:
+    print "Failed to initialize: %s" % e
+    sys.exit(1)
 
 
 ## !!NOTE!! ##
@@ -81,7 +82,7 @@ class Main_window:
         if protocol == avahi.PROTO_INET6:
             return "IPv6"
         return "n/a"
-            
+
     def siocgifname(self, interface):
         if interface <= 0:
             return "n/a"
@@ -93,14 +94,14 @@ class Main_window:
             return "Wide Area"
         else:
             return str(self.siocgifname(interface)) + " " + str(self.protoname(protocol))
-                        
+
     def service_resolved(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
         print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, stype, domain, interface, protocol)
 
         print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt)))
 
         self.update_label(interface, protocol, name, stype, domain, host, aprotocol, address, port, avahi.txt_array_to_string_array(txt))
-        
+
     def print_error(self, err):
         error_label = "<b>Error:</b> %s" % (err)
         self.info_label.set_markup(error_label)
@@ -113,13 +114,13 @@ class Main_window:
             return service_type_db[stype]
         except KeyError:
             return stype
-            
+
     def new_service(self, interface, protocol, name, stype, domain, flags):
         print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, stype, domain, interface, protocol)
         if self.zc_ifaces.has_key((interface,protocol)) == False:
 
             ifn = self.get_interface_name(interface, protocol)
-            
+
             self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, ifn, None,interface,protocol,None,domain)
         if self.zc_domains.has_key((interface,protocol,domain)) == False:
             self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
@@ -153,16 +154,16 @@ class Main_window:
                     parent = self.treemodel.iter_parent(treeiter)
                     self.treemodel.remove(treeiter)
                     del self.zc_ifaces[(interface,protocol)]
+
     def new_service_type(self, interface, protocol, stype, domain, flags):
         global service_browsers
 
-        # Are we already browsing this domain for this type? 
+        # Are we already browsing this domain for this type?
         if service_browsers.has_key((interface, protocol, stype, domain)):
             return
-        
+
         print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (stype, domain, interface, protocol)
-        
+
         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)
         b.connect_to_signal('ItemNew', self.new_service)
         b.connect_to_signal('ItemRemove', self.remove_service)
@@ -185,7 +186,7 @@ class Main_window:
                 print e
                 error_msg("You should check that the avahi daemon is running.\n\nError : %s" % e)
                 sys.exit(0)
-                
+
             b.connect_to_signal('ItemNew', self.new_service_type)
 
             service_type_browsers[(interface, protocol, domain)] = b
@@ -211,7 +212,7 @@ class Main_window:
                 if len(tmp[0]) > 0:
                     res[tmp[0]] = tmp[1]
         return res
-                                                                            
+
 
     def update_label(self,interface, protocol, name, stype, domain, host, aprotocol, address, port, txt):
         if len(txt) != 0:
@@ -232,7 +233,6 @@ class Main_window:
         return myiter
 
     def new(self):
-        print "A new main_window has been created"
         self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
         self.tree_view.set_model(self.treemodel)
 
@@ -251,14 +251,18 @@ class Main_window:
         self.zc_domains = {}
         self.zc_types = {}
         self.services_browsed = {}
-        
-        self.bus = dbus.SystemBus()
-        self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
+
+        try:
+            self.bus = dbus.SystemBus()
+            self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
+        except Exception, e:
+            print "Failed to connect to Avahi Server (Is it running?): %s" % e
+            sys.exit(1)
 
         if self.domain is None:
             # Explicitly browse .local
             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
-                        
+
             # Browse for other browsable domains
             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)
             db.connect_to_signal('ItemNew', self.new_domain)
@@ -272,8 +276,6 @@ class Main_window:
 def main():
     main_window = Main_window()
     gtk.main()
-    
+
 if __name__ == "__main__":
     main()
-
-