Hi,

Attached is the patch which allows subscribing to events in
common/events.py (using publisher/subscriber pattern). As a
side-effect, this patch decouples systray.py and common/events.py
(common/events.py no longer even imports common.gajim).

The patch allows additional modules to be written, which do the
different things when events arrive (e.g. blinking LED, executing
external program). Existing things, such as emiting DBus messages on
new events also may be restructured to use this mechanism.

-- 
JID: [EMAIL PROTECTED]
Index: src/systray.py
===================================================================
--- src/systray.py	(revision 8005)
+++ src/systray.py	(working copy)
@@ -379,3 +379,18 @@
 		if self.t:
 			self.t.destroy()
 			self.t = None
+
+class SystrayEventsListener(object):
+	def __init__(self, events):
+		events.event_added_subscribe(self.on_event_added)
+		events.event_removed_subscribe(self.on_event_removed)
+		
+	def on_event_added(self, event):
+		if event.show_in_systray and gajim.interface.systray_capabilities:
+			gajim.interface.systray.set_img()
+
+	def on_event_removed(self, event):
+		if event and event.show_in_systray and gajim.interface.systray_capabilities:
+			gajim.interface.systray.set_img()
+
+__systray_events_listener = SystrayEventsListener(gajim.events)
Index: src/common/events.py
===================================================================
--- src/common/events.py	(revision 8005)
+++ src/common/events.py	(working copy)
@@ -21,7 +21,6 @@
 ##
 
 import time
-import gajim
 
 class Event:
 	'''Information concerning each event'''
@@ -48,7 +47,20 @@
 	'''Information concerning all events'''
 	def __init__(self):
 		self._events = {} # list of events {acct: {jid1: [E1, E2]}, }
+		self._event_added_listeners = []
+		self._event_removed_listeners = []
 
+	def event_added_subscribe(self, listener):
+		self._event_added_listeners.append(listener)
+	def event_removed_subscribe(self, listener):
+		self._event_removed_listeners.append(listener)
+	def fire_event_added(self, event):
+		for listener in self._event_added_listeners:
+			listener(event)
+	def fire_event_removed(self, event = None):
+		for listener in self._event_removed_listeners:
+			listener()
+
 	def change_account_name(self, old_name, new_name):
 		if self._events.has_key(old_name):
 			self._events[new_name] = self._events[old_name]
@@ -77,8 +89,7 @@
 			self._events[account][jid] = [event]
 		else:
 			self._events[account][jid].append(event)
-		if event.show_in_systray and gajim.interface.systray_capabilities:
-			gajim.interface.systray.set_img()
+		self.fire_event_added(event)
 
 	def remove_events(self, account, jid, event = None, types = []):
 		'''if event is not specified, remove all events from this jid,
@@ -94,8 +105,7 @@
 					del self._events[account][jid]
 				else:
 					self._events[account][jid].remove(event)
-				if event.show_in_systray and gajim.interface.systray_capabilities:
-					gajim.interface.systray.set_img()
+				self.fire_event_deleted(event)
 				return
 			else:
 				return True
@@ -110,13 +120,11 @@
 				self._events[account][jid] = new_list
 			else:
 				del self._events[account][jid]
-			if gajim.interface.systray_capabilities:
-				gajim.interface.systray.set_img()
+			self.fire_event_removed()
 			return
 		# no event nor type given, remove them all
 		del self._events[account][jid]
-		if gajim.interface.systray_capabilities:
-			gajim.interface.systray.set_img()
+		self.fire_event_removed()
 
 	def change_jid(self, account, old_jid, new_jid):
 		if not self._events[account].has_key(old_jid):
@@ -240,3 +248,4 @@
 		'''return all events that must be displayed in roster:
 		{account1: {jid1: [ev1, ev2], },. }'''
 		return self._get_some_events('roster')
+
_______________________________________________
Gajim-devel mailing list
Gajim-devel@gajim.org
https://lists.gajim.org/cgi-bin/listinfo/gajim-devel

Reply via email to