From 597081ae5e8edc5fd6e6e4eff348e27328632be6 Mon Sep 17 00:00:00 2001 From: Sebastien Estienne Date: Sun, 9 Oct 2005 18:17:29 +0000 Subject: * organize functions * add some comments git-svn-id: file:///home/lennart/svn/public/service-discovery-applet/trunk@62 3be567f1-68ff-0310-b24a-ad7cc433fd2f --- src/service-discovery-applet.in | 202 +++++++++++++++++++++++----------------- 1 file changed, 114 insertions(+), 88 deletions(-) diff --git a/src/service-discovery-applet.in b/src/service-discovery-applet.in index 18a8797..7e22f40 100755 --- a/src/service-discovery-applet.in +++ b/src/service-discovery-applet.in @@ -55,8 +55,10 @@ try: except ImportError, e: pass -#from gettext import gettext as _ - +############################################################################### +# +# SERVICES THAT WILL BE ADDED TO GCONF ON THE FIRST RUN +# # default browsing value first_run_services = { "_http._tcp" : True, @@ -67,6 +69,10 @@ first_run_services = { } +############################################################################### +# +# SERVIDE DISCOVERY APPLET MAIN CLASS +# class ServiceDiscoveryApplet(gnomeapplet.Applet): def __init__(self, applet, iid): self.__gobject_init__() @@ -151,10 +157,10 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): # applet.connect("destroy",self.cleanup) # applet.show_all() - def start_notifying_cb(self): - print "start notifying" - self.show_notifications = self.gc_client.get_bool ("%s/%s" % (self.gc_options,"show_notifications")) - +############################################################################### +# +# AVAHI +# def siocgifname(self, interface): if interface <= 0: return "any" @@ -170,6 +176,7 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): services.handle(name, type, host, address, port, txts) def print_error(self, err): + # FIXME we should use notifications print "Error:", str(err) def menuitem_response(self, widget, interface, protocol, name, type, domain): @@ -223,38 +230,6 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): self.service_menu.remove(self.zc_types[type].get_attach_widget()) del self.zc_types[type] - - 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 - def add_service_type(self, interface, protocol, type, domain): # Are we already browsing this domain for this type? if self.service_browsers.has_key((interface, protocol, type, domain)): @@ -283,51 +258,10 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): self.service_menu.remove(self.zc_types[type].get_attach_widget()) del self.zc_types[type] - def position_popup_cb(self, widget): - x, y = self.applet.window.get_origin() - applet_width = self.applet.allocation.width - applet_height = self.applet.allocation.height - widget_width ,widget_height = widget.size_request() - orientation = self.applet.get_orient() - if orientation == gnomeapplet.ORIENT_UP: - y -= widget_height - elif orientation == gnomeapplet.ORIENT_DOWN: - y += applet_height - elif orientation == gnomeapplet.ORIENT_LEFT: - x -= widget_width - elif orientation == gnomeapplet.ORIENT_RIGHT: - x += applet_width - return (x, y, True) - - def on_button_press(self, widget, event): - if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1: - self.service_menu.show_all() - self.service_menu.popup(None, None, self.position_popup_cb, event.button, event.time) - widget.set_state(gtk.STATE_SELECTED) -# if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3: -# self.popup_control.do_popup(event.button,event.time) - return False - - def on_hide_service_menu(self,widget): - self.applet.set_state(gtk.STATE_NORMAL) - return False - - def on_applet_size_allocate(self, eventbox, rect): - if (rect.x <= 0) or (rect.y <= 0): - return False - rect.x -= 1 - rect.y -= 1 - rect.width += 2 - rect.height += 2 - gtk.Widget.size_allocate(eventbox, rect) - return False - - def on_change_background(self, panelapplet, backgroundtype, color, pixmap): - panelapplet.modify_bg(gtk.STATE_NORMAL, color) - if backgroundtype == gnomeapplet.PIXMAP_BACKGROUND: - s = panelapplet.get_style() - s.bg_pixmap[gtk.STATE_NORMAL] = pixmap - +############################################################################### +# +# APPLET CONTEXTUAL OPTIONS MENU +# def on_about(self, component, verb, applet): icon = gtk.Image() icon.set_from_file("@iconsdir@/48x48/service-discovery-applet.png") @@ -347,7 +281,6 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): def on_config(self, component, verb, applet): pid = subprocess.Popen(["service-discovery-config", ""]).pid -#FIXME replace with gconf key def start_service_discovery(self, component, verb, applet): if len(self.domain) != 0: print "domain not null %s" % (self.domain) @@ -383,17 +316,107 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): self.domain = "" self.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 +# + def position_popup_cb(self, widget): + x, y = self.applet.window.get_origin() + applet_width = self.applet.allocation.width + applet_height = self.applet.allocation.height + widget_width ,widget_height = widget.size_request() + orientation = self.applet.get_orient() + if orientation == gnomeapplet.ORIENT_UP: + y -= widget_height + elif orientation == gnomeapplet.ORIENT_DOWN: + y += applet_height + elif orientation == gnomeapplet.ORIENT_LEFT: + x -= widget_width + elif orientation == gnomeapplet.ORIENT_RIGHT: + x += applet_width + return (x, y, True) + + def on_button_press(self, widget, event): + if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1: + self.service_menu.show_all() + self.service_menu.popup(None, None, self.position_popup_cb, event.button, event.time) + widget.set_state(gtk.STATE_SELECTED) +# if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3: +# self.popup_control.do_popup(event.button,event.time) + return False + + def on_hide_service_menu(self,widget): + self.applet.set_state(gtk.STATE_NORMAL) + return False + + def on_applet_size_allocate(self, eventbox, rect): + if (rect.x <= 0) or (rect.y <= 0): + return False + rect.x -= 1 + rect.y -= 1 + rect.width += 2 + rect.height += 2 + gtk.Widget.size_allocate(eventbox, rect) + return False + + def on_change_background(self, panelapplet, backgroundtype, color, pixmap): + panelapplet.modify_bg(gtk.STATE_NORMAL, color) + if backgroundtype == gnomeapplet.PIXMAP_BACKGROUND: + 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) - # FIXME unset key if client.get_bool(gc_entry.key) == True: # Browse for a new service - print "browse %s" % (service_type) self.add_service_type(self.interface, self.protocol, service_type, self.domain) else: # Stop browsing for a service - print "remove %s" % (service_type) self.del_service_type(self.interface, self.protocol, service_type, self.domain) def gc_options_cb (self, client, cnxn_id, gc_entry, data): @@ -409,10 +432,13 @@ class ServiceDiscoveryApplet(gnomeapplet.Applet): if len(status) != 0: self.start_service_discovery(None,None,None) +############################################################################### +# +# STARTING POINT OF THE APPLET +# def applet_factory(applet, iid): sda = ServiceDiscoveryApplet(applet, iid) sda.applet.show_all() - print "applet_factory" return True def activate_factory(): -- cgit