diff options
Diffstat (limited to 'src/service-discovery-applet.in')
-rwxr-xr-x | src/service-discovery-applet.in | 186 |
1 files changed, 133 insertions, 53 deletions
diff --git a/src/service-discovery-applet.in b/src/service-discovery-applet.in index d773bfe..7a9845c 100755 --- a/src/service-discovery-applet.in +++ b/src/service-discovery-applet.in @@ -1,5 +1,5 @@ #!@PYTHON@ - +# -*-python-*- # This file is part of service-discovery-applet (sd-applet). # # sd-applet is free software; you can redistribute it and/or modify it @@ -20,7 +20,6 @@ # $id$ # # todo -# * memleaks ? # * dict([el.split('=',1) for el in l ]) import os @@ -43,12 +42,20 @@ import gtk import gnomeapplet import gnome import gnome.ui +import gconf + +# Gconf Paths +gc_options = "/apps/service-discovery-applet/options" +gc_services = "/apps/service-discovery-applet/services" + #from gettext import gettext as _ -domain = None +#type_browsed = ('_ssh._tcp', '_http._tcp','_ftp._tcp') -type_browsed = ('_ssh._tcp', '_http._tcp','_ftp._tcp') +show_local_services = False +show_notifications = False +show_applet_name = False service_browsers = {} service_menu = gtk.Menu() @@ -90,7 +97,7 @@ def menuitem_response(widget, interface, protocol, name, type, domain): server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC, reply_handler=service_resolved, error_handler=print_error) def new_service(interface, protocol, name, type, domain): - global server, service_menu, zc_types, zc_pretty_name, zc_services, notif + global server, service_menu, zc_types, zc_pretty_name, zc_services, notif, show_notifications print "Found service '%s' of type '%s' in domain '%s' on %s.%i." % (name, type, domain, siocgifname(interface), protocol) @@ -119,17 +126,18 @@ def new_service(interface, protocol, name, type, domain): iconfile = "@iconsdir@/48x48/%s.png" % (type) message = "<b>Name :</b> %s\n<b>Type : </b> %s <i>(%s)</i>" % (name,zc_pretty_name[type], type) try: - notif.Notify("Service Discovery Applet", - iconfile, dbus.UInt32(0),"",dbus.Byte(0), - "New Service found",message, - [iconfile],[""],[""],True,dbus.UInt32(3)) + if show_notifications == True: + notif.Notify("Service Discovery Applet", + iconfile, dbus.UInt32(0),"",dbus.Byte(0), + "New Service found",message, + [iconfile],[""],[""],True,dbus.UInt32(3)) except: print "can't use notification daemon" pass def remove_service(interface, protocol, name, type, domain): - global zc_services,notif + global zc_services, notif, show_notifications print "Service '%s' of type '%s' in domain '%s' on %s.%i disappeared." % (name, type, domain, siocgifname(interface), protocol) @@ -139,18 +147,20 @@ def remove_service(interface, protocol, name, type, domain): service_menu.remove(zc_types[type].get_attach_widget()) del zc_types[type] + # check that iconfil exist iconfile = "@iconsdir@/48x48/%s.png" % (type) message = "<b>Name :</b> %s\n<b>Type : </b> %s <i>(%s)</i>" % (name,zc_pretty_name[type], type) try: - notif.Notify("Service Discovery Applet", - iconfile, dbus.UInt32(0),"",dbus.Byte(0), - "Service disappeared",message, - [iconfile],[""],[""],True,dbus.UInt32(3)) + if show_notifications == True: + notif.Notify("Service Discovery Applet", + iconfile, dbus.UInt32(0),"",dbus.Byte(0), + "Service disappeared",message, + [iconfile],[""],[""],True,dbus.UInt32(3)) except: print "can't use notification daemon" pass -def new_service_type(interface, protocol, type, domain): +def add_service_type(interface, protocol, type, domain): global server, service_browsers, system_bus # Are we already browsing this domain for this type? @@ -159,23 +169,26 @@ def new_service_type(interface, protocol, type, domain): print "Browsing for services of type '%s' in domain '%s' on %s.%i ..." % (type, domain, siocgifname(interface), protocol) - b = dbus.Interface(system_bus.get_object(avahi.DBUS_NAME, server.ServiceBrowserNew(interface, protocol, type, domain)), avahi.DBUS_INTERFACE_SERVICE_BROWSER) + b = dbus.Interface(system_bus.get_object(avahi.DBUS_NAME, + server.ServiceBrowserNew(interface, protocol, type, domain)) + , avahi.DBUS_INTERFACE_SERVICE_BROWSER) b.connect_to_signal('ItemNew', new_service) b.connect_to_signal('ItemRemove', remove_service) service_browsers[(interface, protocol, type, domain)] = b -def browse_domain(interface, protocol, domain): - global server +def del_service_type(interface, protocol, type, domain): - # Are we already browsing this domain? - for type in type_browsed: - new_service_type(interface, protocol, type, domain) - -def new_domain(interface, protocol, domain): - # We browse for .local anyway... - if domain != "local": - browse_domain(interface, protocol, domain) + service = (interface, protocol, type, domain) + if not service_browsers.has_key(service): + return + sb = service_browsers[service] + sb.Free() + del service_browsers[service] + # delete the sub menu of service_type + if zc_types.has_key(type): + service_menu.remove(zc_types[type].get_attach_widget()) + del zc_types[type] def on_menubar_click(widget, event): @@ -188,7 +201,7 @@ def on_menubar_click(widget, event): def on_about(component, verb, applet): icon = gtk.Image() - icon.set_from_file("@iconsdir@/service-discovery-applet.png") + icon.set_from_file("@iconsdir@/48x48/service-discovery-applet.png") fullname = "Service Discovery Applet" copyright = "Copyright (C) 2005 Sebastien Estienne" @@ -199,8 +212,85 @@ def on_about(component, verb, applet): about.set_icon(icon.get_pixbuf()) about.show() +def on_config(component, verb, applet): + os.system("service-discovery-config") + +#FIXME replace with gconf key +def start_service_discovery(component, verb, applet): + global system_bus, server, db , gc_client, domain, interface, protocol + + if len(domain) != 0: + return + + try: + domain = server.GetDomainName() + except: + print "Check that Avahi daemon is running!" + return + + interface = avahi.IF_UNSPEC + protocol = avahi.PROTO_UNSPEC + + gc_entries = gc_client.all_entries(gc_services) + for gc_entry in gc_entries: + if gc_client.get_bool(gc_entry.key) == True: + service_type = os.path.basename(gc_entry.key) + add_service_type(interface, protocol, service_type, domain) + print "start" + +def stop_service_discovery(component, verb, applet): + global domain + + if len(domain) == 0: + return + + for service in service_browsers.copy(): + del_service_type(service[0],service[1],service[2],service[3]) + domain = "" + print "stop" + +# Callback called when a service is added/removed/enabled/disabled in gconf +def gc_services_cb (client, cnxn_id, gc_entry, data): + global interface, domain, protocol + + service_type = os.path.basename(gc_entry.key) + # FIXME unset key + if client.get_bool(gc_entry.key) == True: + # Browse for a new service + print "browse %s" % (service_type) + add_service_type(interface, protocol, service_type, domain) + else: + # Stop browsing for a service + print "remove %s" % (service_type) + del_service_type(interface, protocol, service_type, domain) + +def gc_options_cb (client, cnxn_id, gc_entry, data): + global show_notifications, show_applet_name, show_local_services + + key = os.path.basename(gc_entry.key) + if key == "show_applet_name": + show_applet_name = client.get_bool(gc_entry.key) + if key == "show_notifications": + show_notifications = client.get_bool(gc_entry.key) + if key == "show_local_services": + show_local_services = client.get_bool(gc_entry.key) + def ServiceDiscoveryApplet_factory(applet, iid): - global menuZC, system_bus, server, session_bus, domain, notif + global menuZC, session_bus, notif, show_notifications, gc_client, server, system_bus, domain + + # Gconf + gc_client = gconf.client_get_default () + + gc_client.add_dir (gc_options, gconf.CLIENT_PRELOAD_NONE) + + show_local_services = gc_client.get_bool ("%s/%s" % (gc_options,"show_local_services")) + show_notifications = gc_client.get_bool ("%s/%s" % (gc_options,"show_notifications")) + show_applet_name = gc_client.get_bool ("%s/%s" % (gc_options,"show_applet_name")) + + gc_client.notify_add (gc_options, gc_options_cb, None) + + gc_client.add_dir (gc_services, gconf.CLIENT_PRELOAD_NONE) + gc_client.notify_add (gc_services, gc_services_cb,None) print "Creating new applet instance" menubar = gtk.MenuBar() @@ -223,39 +313,29 @@ def ServiceDiscoveryApplet_factory(applet, iid): # funky right-click menu menuXml = """ <popup name="button3"> - <menuitem name="Service Discovery Applet About Item" verb="SDA About" _label="_About" pixtype="stock" pixname="gnome-stock-about"/> + <menuitem name="Service Discovery Applet About Item" verb="SDA About" _label="_About" pixtype="stock" pixname="gtk-about"/> <menuitem name="Service Discovery Applet Configuration Item" verb="SDA Config" _label="_Config" pixtype="stock" pixname="gtk-preferences"/> + <menuitem name="Service Discovery Applet Start Item" verb="SDA Start" _label="_Start" pixtype="stock" pixname="gtk-media-play"/> + <menuitem name="Service Discovery Applet Stop Item" verb="SDA Stop" _label="_Stop" pixtype="stock" pixname="gtk-media-stop"/> </popup> """ - applet.setup_menu(menuXml, [("SDA About", on_about)], applet) - applet.setup_menu(menuXml, [("SDA Config", on_about)], applet) - + applet.setup_menu(menuXml, [ + ("SDA About", on_about), + ("SDA Config", on_config), + ("SDA Start", start_service_discovery), + ("SDA Stop", stop_service_discovery) + ], applet) + popup = applet.get_popup_component() +# popup.set_prop ("/button3/Service Discovery Applet Start Item", "_label", "prout") + + #Start Service Discovery + domain = "" system_bus = dbus.SystemBus() server = dbus.Interface(system_bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) - if domain is None: - # Explicitly browse .local - try: - browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local") - except: - print "Can't connect to Avahi." - sys.exit(0) - - # Browse for other browsable domains - sdb = server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE) - obj = system_bus.get_object(avahi.DBUS_NAME, sdb) - db = dbus.Interface(obj, avahi.DBUS_INTERFACE_DOMAIN_BROWSER) - db.connect_to_signal('ItemNew', new_domain) - - else: - # Just browse the domain the user wants us to browse - try: - browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain) - except: - print "Can't connect to Avahi." - sys.exit(0) + start_service_discovery(None, None, None) session_bus = dbus.SessionBus() obj = session_bus.get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications") |