[kupfer] plugin.favorites: Fix Registering and Finding the FavoritesSource



commit 65054d3c3cef2c922485d53a9b20f954624cbcd0
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Tue Sep 8 17:16:08 2009 +0200

    plugin.favorites: Fix Registering and Finding the FavoritesSource
    
    We keep a list of weak references to all FavoritesSource ever created;
    the only referenced one of these is the FavoritesSource we want to
    work with.
    
    At any time, we should have at least one source, and we only have more
    just before replacing created with unpickled, or just when creating
    an instance to get its icon.

 kupfer/plugin/favorites.py |   34 +++++++++++++++++++++++-----------
 1 files changed, 23 insertions(+), 11 deletions(-)
---
diff --git a/kupfer/plugin/favorites.py b/kupfer/plugin/favorites.py
index d8e279a..1465599 100644
--- a/kupfer/plugin/favorites.py
+++ b/kupfer/plugin/favorites.py
@@ -1,9 +1,10 @@
 from collections import deque
+import weakref
 
 import gtk
 
 from kupfer.objects import Source, Action, PicklingHelperMixin
-from kupfer import utils, objects
+from kupfer import utils, objects, pretty
 
 __kupfer_name__ = _("Favorites")
 __kupfer_sources__ = ("FavoritesSource", )
@@ -12,8 +13,6 @@ __description__ = _("(Simple) favorites plugin")
 __version__ = ""
 __author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
 
-_fav_control = None
-
 def _FavoritesLeafTypes():
 	"""reasonable pickleable types"""
 	yield objects.FileLeaf
@@ -21,16 +20,14 @@ def _FavoritesLeafTypes():
 	yield objects.UrlLeaf
 
 class FavoritesSource (Source, PicklingHelperMixin):
-	"""
-	"""
+	"""Keep a list of Leaves that the User may add and remove from"""
 	def __init__(self):
 		Source.__init__(self, _("Favorites"))
 		self.favorites = deque()
 		self.unpickle_finish()
 
 	def unpickle_finish(self):
-		global _fav_control
-		_fav_control = self
+		RegisterFavoritesSource(self)
 		# check items for validity
 		for itm in list(self.favorites):
 			if hasattr(itm, "is_valid") and not itm.is_valid():
@@ -40,8 +37,10 @@ class FavoritesSource (Source, PicklingHelperMixin):
 	def add(self, itm):
 		self.favorites.append(itm)
 		self.mark_for_update()
+
 	def has_item(self, itm):
 		return itm in set(self.favorites)
+
 	def remove(self, itm):
 		self.favorites.remove(itm)
 		self.mark_for_update()
@@ -55,19 +54,32 @@ class FavoritesSource (Source, PicklingHelperMixin):
 
 	def get_icon_name(self):
 		return "emblem-favorite"
+
 	def provides(self):
 		return list(_FavoritesLeafTypes())
 
+_favorites_source = []
+
+def RegisterFavoritesSource(fav):
+	_favorites_source.append(weakref.ref(fav))
+
 def GetFavoritesSource():
-	return _fav_control
+	for favref in list(_favorites_source):
+		if favref() is None:
+			_favorites_source.remove(favref)
+	if len(_favorites_source) > 1:
+		pretty.print_error("have > 1 favorites source")
+
+	try:
+		return _favorites_source[0]()
+	except IndexError:
+		raise LookupError("Favorites Source not found")
 
 class AddFavorite (Action):
 	def __init__(self):
 		Action.__init__(self, _("Add to Favorites"))
 	def activate(self, leaf):
-		fav = GetFavoritesSource()
-		if fav:
-			fav.add(leaf)
+		GetFavoritesSource().add(leaf)
 	def item_types(self):
 		return list(_FavoritesLeafTypes())
 	def valid_for_item(self, item):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]