]> git.meshlink.io Git - catta/commitdiff
* progress on python port of avahi-discover basic things are in place
authorSebastien Estienne <sebastien.estienne@gmail.com>
Sun, 31 Jul 2005 01:04:52 +0000 (01:04 +0000)
committerSebastien Estienne <sebastien.estienne@gmail.com>
Sun, 31 Jul 2005 01:04:52 +0000 (01:04 +0000)
* it mays need to be clean and polished as i'm new to python
* modified avahi-discover.glade to add a signal (cursor_changed) on the TreeView

git-svn-id: file:///home/lennart/svn/public/avahi/trunk@206 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-discover/avahi-discover.glade
avahi-utils/avahi-discover

index 852f2eecf84b7422d394057b300c76df3af2e478..ca6b51f3b8bdfad151639d2e8d624d4b59802359 100644 (file)
@@ -18,6 +18,7 @@
   <property name="skip_pager_hint">False</property>
   <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
   <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
+  <property name="focus_on_map">True</property>
 
   <child>
     <widget class="GtkVBox" id="vbox1">
              <property name="rules_hint">False</property>
              <property name="reorderable">False</property>
              <property name="enable_search">True</property>
+             <property name="fixed_height_mode">False</property>
+             <property name="hover_selection">False</property>
+             <property name="hover_expand">False</property>
+             <signal name="cursor_changed" handler="on_tree_view_cursor_changed" last_modification_time="Sat, 30 Jul 2005 21:39:13 GMT"/>
            </widget>
          </child>
        </widget>
          <property name="yalign">0.5</property>
          <property name="xpad">0</property>
          <property name="ypad">0</property>
+         <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
+         <property name="width_chars">-1</property>
+         <property name="single_line_mode">False</property>
+         <property name="angle">0</property>
        </widget>
        <packing>
          <property name="padding">0</property>
index 3e720e8e8462f8aa307d192ca5696adafcad73b2..78d440a1b7c82a8dc6904e567c999c6bb6506318 100755 (executable)
@@ -1,11 +1,23 @@
 #!/usr/bin/python2.4
 # -*-python-*-
+# $Id$
 
 import os
 import gtk
 import gobject
 from SimpleGladeApp import SimpleGladeApp
 
+import avahi, dbus, gobject, sys
+try:
+    import dbus.glib
+except ImportError, e:
+    pass
+
+service_type_browsers = {}
+service_browsers = {}
+
+
 glade_dir = "../avahi-discover/"
 
 class Main_window(SimpleGladeApp):
@@ -13,20 +25,93 @@ class Main_window(SimpleGladeApp):
         path = os.path.join(glade_dir, path)
         SimpleGladeApp.__init__(self, path, root, domain, **kwargs)
 
+    def on_tree_view_cursor_changed(self, widget, *args):
+        (model, iter) = widget.get_selection().get_selected()
+        (name,interface,protocol,type,domain) = self.treemodel.get(iter,0,1,2,3,4)
+        if type == None:
+            return
+        #Asynchronous resolving
+        self.server.ResolveService( int(interface), int(protocol), name, type, domain, avahi.PROTO_UNSPEC, reply_handler=self.service_resolved, error_handler=self.print_error)
+
+
+    def service_resolved(self, interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
+        print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, type, domain, interface, protocol)
+        print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(txt))
+        self.update_label(interface, protocol, name, type, domain, host, aprotocol, address, port, str(txt))
+        
+    def print_error(err):
+        print "Error:", str(err)
+            
+    def new_service(self, interface, protocol, name, type, domain):
+        print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, type, domain, interface, protocol)
+        if self.service_type.has_key(type) == False:
+            self.service_type[type] = self.insert_row(self.treemodel,None,type, interface,None,None,None)
+        treeiter = self.insert_row(self.treemodel,self.service_type[type],name, interface,protocol,type,domain)
+        self.services_browsed[(interface, protocol, name, type, domain)] = treeiter
+
+
+    def remove_service(self, interface, protocol, name, type, domain):
+        print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, type, domain, interface, protocol)
+        treeiter=self.services_browsed[(interface, protocol, name, type, domain)]
+        parent = self.treemodel.iter_parent(treeiter)
+        self.treemodel.remove(treeiter)
+        del self.services_browsed[(interface, protocol, name, type, domain)]
+        if self.treemodel.iter_has_child(parent) == False:
+            self.treemodel.remove(parent)
+            del self.service_type[type]
+    def new_service_type(self, interface, protocol, type, domain):
+        global service_browsers
+
+        # Are we already browsing this domain for this type? 
+        if service_browsers.has_key((interface, protocol, type, domain)):
+            return
+        
+        print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (type, domain, interface, protocol)
+        
+        b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(interface, protocol, type, domain)),  avahi.DBUS_INTERFACE_SERVICE_BROWSER)
+        b.connect_to_signal('ItemNew', self.new_service)
+        b.connect_to_signal('ItemRemove', self.remove_service)
+
+        service_browsers[(interface, protocol, type, domain)] = b
+
+    def browse_domain(self, interface, protocol, domain):
+        global service_type_browsers
+
+        # Are we already browsing this domain?
+        if service_type_browsers.has_key((interface, protocol, domain)):
+            return
+            
+        print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol)
+        
+        b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceTypeBrowserNew(interface, protocol, domain)),  avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
+        b.connect_to_signal('ItemNew', self.new_service_type)
+
+        service_type_browsers[(interface, protocol, domain)] = b
+
+    def new_domain(self,interface, protocol, domain):
+
+        # We browse for .local anyway...
+        if domain != "local":
+            browse_domain(interface, protocol, domain)
+
     def update_label(self,interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
         infos = "<b>Service Type:</b> %s\n<b>Service Name:</b> %s\n<b>Domain Name:</b> %s\n<b>Interface:</b> %i.%i\n<b>Address:</b> %s/%s:%i\n<b>TXT Data:</b> %s" % (type, name, domain, interface, protocol, host, address, port, str(txt))
         self.info_label.set_markup(infos)
 
     def insert_row(self, model,parent,
-                   firstcolumn,secondcolumn):
+                   name, interface,protocol,type,domain):
         myiter=model.insert_after(parent,None)
-        model.set_value(myiter,0,firstcolumn)
-        model.set_value(myiter,1,secondcolumn)
+        model.set_value(myiter,0,name)
+        model.set_value(myiter,1,interface)
+        model.set_value(myiter,2,protocol)
+        model.set_value(myiter,3,type)
+        model.set_value(myiter,4,domain)
         return myiter
     
     def new(self):
         print "A new main_window has been created"
-        self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
+        self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
         self.tree_view.set_model(self.treemodel)
 
         #creating the columns headers
@@ -42,9 +127,29 @@ class Main_window(SimpleGladeApp):
         self.tree_view.append_column(column)
         
         # testing with fake data
+        self.service_type = {}
+        self.services_browsed = {}
+        
         model=self.treemodel
-        self.insert_row(model,None,'Service Name', 'Interface')
-        self.update_label(1, 1, "My Web Server", "_http._tcp.", ".local", "xenon", "aprotocol", "172.16.100.2", 42, "txt")
+
+        domain = None
+
+        self.bus = dbus.SystemBus()
+        self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
+
+        if 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)), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
+            db.connect_to_signal('ItemNew', self.new_domain)
+
+        else:
+            # Just browse the domain the user wants us to browse
+            self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
+
+
         
 def main():
     main_window = Main_window()