From 0a0184ef6366934edb36664444089e04622d6d32 Mon Sep 17 00:00:00 2001 From: Sebastien Estienne Date: Sat, 12 Nov 2005 22:09:12 +0000 Subject: * start splitting sda in more objects git-svn-id: file:///home/lennart/svn/public/service-discovery-applet/trunk@75 3be567f1-68ff-0310-b24a-ad7cc433fd2f --- src/service-discovery-applet.in | 237 +++++++++++++++++++++++----------------- 1 file changed, 136 insertions(+), 101 deletions(-) diff --git a/src/service-discovery-applet.in b/src/service-discovery-applet.in index 5a13bc8..ce7f083 100755 --- a/src/service-discovery-applet.in +++ b/src/service-discovery-applet.in @@ -69,23 +69,28 @@ first_run_services = { } + ############################################################################### # -# SERVIDE DISCOVERY APPLET MAIN CLASS +# ServiceTypeDatabase # -class ServiceDiscoveryApplet(gnomeapplet.Applet): - def __init__(self, applet, iid): - self.__gobject_init__() - self.applet = applet +class ServiceTypeDatabase: + def __init__(self): + self.pretty_name = avahi.ServiceTypeDatabase.ServiceTypeDatabase() - self.service_browsers = {} - self.service_menu = gtk.Menu() - self.service_menu.connect("hide", self.on_hide_service_menu) - self.add_no_services_menuitem() - - self.zc_types = {} - self.zc_services = {} - self.zc_pretty_name = avahi.ServiceTypeDatabase.ServiceTypeDatabase() + def get_human_type(self, type): + if self.pretty_name.has_key(type): + return self.pretty_name[type] + else: + return type + +############################################################################### +# +# GCONF +# +class SDAGconf: + def __init__(self, applet): + self.applet = applet # Gconf Paths self.gc_options = "/apps/service-discovery-applet/options" @@ -96,8 +101,6 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): self.gc_client.add_dir (self.gc_options, gconf.CLIENT_PRELOAD_NONE) - self.show_local_services = self.gc_client.get_bool ("%s/%s" % (self.gc_options,"show_local_services")) - self.show_notifications = self.gc_client.get_bool ("%s/%s" % (self.gc_options,"show_notifications")) self.gc_client.add_dir (self.gc_services, gconf.CLIENT_PRELOAD_NONE) self.gc_client.notify_add (self.gc_services, self.gc_services_cb) @@ -110,6 +113,102 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): for service_type, enabled in first_run_services.iteritems(): self.gc_client.set_bool("%s/%s" % (self.gc_services, service_type), enabled) + def get_services(self): + services = [] + gc_entries = self.gc_client.all_entries(self.gc_services) + for gc_entry in gc_entries: + if self.gc_client.get_bool(gc_entry.key) == True: + service_type = os.path.basename(gc_entry.key) + services.append(service_type) + return services + + def get_option(self, key): + return self.gc_client.get_bool ("%s/%s" % (self.gc_options, key)) + + # Callback called when a service is added/removed/enabled/disabled in gconf + def gc_services_cb (self, client, cnxn_id, gc_entry, data): + service_type = os.path.basename(gc_entry.key) + if client.get_bool(gc_entry.key) == True: + # Browse for a new service + self.applet.add_service_type(self.applet.interface, self.applet.protocol, service_type, self.applet.domain) + else: + # Stop browsing for a service + self.applet.del_service_type(self.applet.interface, self.applet.protocol, service_type, self.applet.domain) + + def gc_options_cb (self, client, cnxn_id, gc_entry, data): + key = os.path.basename(gc_entry.key) + if key == "show_notifications": + self.applet.show_notifications = client.get_bool(gc_entry.key) + if key == "show_local_services": + self.applet.show_local_services = client.get_bool(gc_entry.key) + self.applet.show_notifications = False + status = self.applet.domain + self.applet.stop_service_discovery(None,None,None) + # only start if it was running before + if len(status) != 0: + self.applet.start_service_discovery(None,None,None) + +############################################################################### +# +# NOTIFICATIONS +# +class Notifications: + def __init__(self, applet): + self.session_bus = dbus.SessionBus() + obj = self.session_bus.get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications") + self.notif = dbus.Interface(obj, "org.freedesktop.Notifications") + self.applet = applet + + + def display_service_notification(self, new, name, type): + iconfile = "@iconsdir@/48x48/%s.png" % (type) + if not os.path.exists(iconfile): + iconfile = "@iconsdir@/48x48/service-discovery-applet.png" + + stdb = ServiceTypeDatabase() + h_type = stdb.get_human_type(type) + message = _("Name : %s\nType : %s (%s)") % (name, h_type, type) + + if new == True: + title = _("New service found") + else: + title = _("Service disappeared") + + self.display_notification(title, message, iconfile) + + def display_notification(self, title, message, iconfile = "@iconsdir@/48x48/service-discovery-applet.png"): + try: + if self.applet.show_notifications == True: + self.notif.Notify(_("Zeroconf Service Discovery"), + iconfile, dbus.UInt32(0),"",dbus.Byte(0), + title,message, + [iconfile],[""],[""],True,dbus.UInt32(3)) + except: + print "can't use notification daemon" + pass + + +############################################################################### +# +# SERVIDE DISCOVERY APPLET MAIN CLASS +# +class ServiceDiscoveryApplet(gnomeapplet.Applet): + def __init__(self, applet, iid): + self.__gobject_init__() + self.applet = applet + + self.service_browsers = {} + self.service_menu = gtk.Menu() + self.service_menu.connect("hide", self.on_hide_service_menu) + self.add_no_services_menuitem() + + self.zc_types = {} + self.zc_services = {} + + self.sdaGconf = SDAGconf(self) + self.show_local_services = self.sdaGconf.get_option("show_local_services") + self.show_notifications = self.sdaGconf.get_option("show_notifications") + applet.connect("button-press-event", self.on_button_press) applet.connect("size-allocate", self.on_applet_size_allocate) @@ -150,15 +249,19 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): self.server = dbus.Interface(self.system_bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) - self.session_bus = dbus.SessionBus() - obj = self.session_bus.get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications") - self.notif = dbus.Interface(obj, "org.freedesktop.Notifications") + + self.sdaNotifications = Notifications(self) self.start_service_discovery(None, None, None) # applet.connect("destroy",self.cleanup) # applet.show_all() + def start_notifying_cb(self): + print "start notifying" + self.show_notifications = self.sdaGconf.get_option("show_notifications") + + ############################################################################### # # AVAHI @@ -217,16 +320,15 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): self.service_menu.remove(menuitem) if self.zc_types.has_key(type) == False: - if self.zc_pretty_name.has_key(type): - pretty_name = self.zc_pretty_name[type] - else: - pretty_name = type img = gtk.Image() iconfile = "@iconsdir@/24x24/%s.png" % (type) if not os.path.exists(iconfile): iconfile = "@iconsdir@/24x24/service-discovery-applet.png" img.set_from_file(iconfile) - menuitem = gtk.ImageMenuItem(pretty_name) + + stdb = ServiceTypeDatabase() + h_type = stdb.get_human_type(type) + menuitem = gtk.ImageMenuItem(h_type) menuitem.set_image(img) menuitem.get_child().set_use_underline(False) @@ -241,7 +343,7 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): menuitem.connect("activate", self.menuitem_response,interface, protocol, name, type, domain) menuitem.show_all() - self.display_service_notification(True, name, type) + self.sdaNotifications.display_service_notification(True, name, type) def remove_service_old(self, interface, protocol, name, type, domain): self.remove_service(interface, protocol, name, type, domain, None) @@ -251,7 +353,7 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): if self.zc_services.has_key((interface, protocol, name, type, domain)): self.zc_types[type].remove(self.zc_services[(interface, protocol, name, type, domain)]) - self.display_service_notification(False, name, type) + self.sdaNotifications.display_service_notification(False, name, type) if self.zc_types[type].get_children() == []: self.service_menu.remove(self.zc_types[type].get_attach_widget()) @@ -331,79 +433,39 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): def start_service_discovery(self, component, verb, applet): if len(self.domain) != 0: print "domain not null %s" % (self.domain) - self.display_notification(_("Already Discovering"),"") + self.sdaNotifications.display_notification(_("Already Discovering"),"") return try: self.avahi_version = self.server.GetVersionString() self.domain = self.server.GetDomainName() except: - self.display_notification(_("Error Detected!"),_("Check that the Avahi daemon is running!")) + self.sdaNotifications.display_notification(_("Error Detected!"),_("Check that the Avahi daemon is running!")) return - self.display_notification(_("Starting discovery"),"") + self.sdaNotifications.display_notification(_("Starting discovery"),"") self.interface = avahi.IF_UNSPEC self.protocol = avahi.PROTO_INET - gc_entries = self.gc_client.all_entries(self.gc_services) - for gc_entry in gc_entries: - if self.gc_client.get_bool(gc_entry.key) == True: - service_type = os.path.basename(gc_entry.key) - self.add_service_type(self.interface, self.protocol, service_type, self.domain) + services = self.sdaGconf.get_services() + for service_type in services: + self.add_service_type(self.interface, self.protocol, service_type, self.domain) + # Wait one second before displaying notifications self.show_notifications = False gobject.timeout_add(1000, self.start_notifying_cb) def stop_service_discovery(self, component, verb, applet): if len(self.domain) == 0: - self.display_notification(_("Discovery already stopped"),"") + self.sdaNotifications.display_notification(_("Discovery already stopped"),"") return for service in self.service_browsers.copy(): self.del_service_type(service[0],service[1],service[2],service[3]) self.domain = "" - self.display_notification(_("Discovery stopped"),"") + self.sdaNotifications.display_notification(_("Discovery stopped"),"") -############################################################################### -# -# NOTIFICATIONS -# - def start_notifying_cb(self): - print "start notifying" - self.show_notifications = self.gc_client.get_bool ("%s/%s" % (self.gc_options,"show_notifications")) - - def display_service_notification(self, new, name, type): - # FIXME handle this in avahi.ServiceTypeDatabase - if self.zc_pretty_name.has_key(type): - pretty_name = self.zc_pretty_name[type] - else: - pretty_name = type - - iconfile = "@iconsdir@/48x48/%s.png" % (type) - if not os.path.exists(iconfile): - iconfile = "@iconsdir@/48x48/service-discovery-applet.png" - - message = _("Name : %s\nType : %s (%s)") % (name, pretty_name, type) - - if new == True: - title = _("New service found") - else: - title = _("Service disappeared") - - self.display_notification(title, message, iconfile) - - def display_notification(self, title, message, iconfile = "@iconsdir@/48x48/service-discovery-applet.png"): - try: - if self.show_notifications == True: - self.notif.Notify(_("Zeroconf Service Discovery"), - iconfile, dbus.UInt32(0),"",dbus.Byte(0), - title,message, - [iconfile],[""],[""],True,dbus.UInt32(3)) - except: - print "can't use notification daemon" - pass - ############################################################################### # # APPLET CALLBACKS @@ -453,33 +515,6 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): s = panelapplet.get_style() s.bg_pixmap[gtk.STATE_NORMAL] = pixmap -############################################################################### -# -# GCONF -# - # Callback called when a service is added/removed/enabled/disabled in gconf - def gc_services_cb (self, client, cnxn_id, gc_entry, data): - service_type = os.path.basename(gc_entry.key) - if client.get_bool(gc_entry.key) == True: - # Browse for a new service - self.add_service_type(self.interface, self.protocol, service_type, self.domain) - else: - # Stop browsing for a service - self.del_service_type(self.interface, self.protocol, service_type, self.domain) - - def gc_options_cb (self, client, cnxn_id, gc_entry, data): - key = os.path.basename(gc_entry.key) - if key == "show_notifications": - self.show_notifications = client.get_bool(gc_entry.key) - if key == "show_local_services": - self.show_local_services = client.get_bool(gc_entry.key) - self.show_notifications = False - status = self.domain - self.stop_service_discovery(None,None,None) - # only start if it was running before - if len(status) != 0: - self.start_service_discovery(None,None,None) - ############################################################################### # # STARTING POINT OF THE APPLET -- cgit