[rhythmbox] artdisplay: allow searches to return pixbufs directly



commit bfdd5663d51955d55e46588f369a815038fba214
Author: Jonathan Matthew <jonathan d14n org>
Date:   Sat Apr 18 02:12:42 2009 +1000

    artdisplay: allow searches to return pixbufs directly
    
    This allows cover art searches to return pixbuf objects as well as URIs
    for cover art images.  Also passes through a flag indicating whether the
    entry being searched for is currently playing.
---
 .../artdisplay/artdisplay/AmazonCoverArtSearch.py  |    5 +-
 plugins/artdisplay/artdisplay/CoverArtDatabase.py  |   60 ++++++++++++--------
 .../artdisplay/artdisplay/LocalCoverArtSearch.py   |    5 +-
 .../artdisplay/LocalCoverArtSearchGIO.py           |    5 +-
 .../artdisplay/artdisplay/PodcastCoverArtSearch.py |   18 +++---
 plugins/artdisplay/artdisplay/__init__.py          |    7 +-
 6 files changed, 60 insertions(+), 40 deletions(-)

diff --git a/plugins/artdisplay/artdisplay/AmazonCoverArtSearch.py b/plugins/artdisplay/artdisplay/AmazonCoverArtSearch.py
index 792e4a2..b442797 100644
--- a/plugins/artdisplay/artdisplay/AmazonCoverArtSearch.py
+++ b/plugins/artdisplay/artdisplay/AmazonCoverArtSearch.py
@@ -73,7 +73,7 @@ class AmazonCoverArtSearch (object):
 
 		return supported_locales[lc_id]
 
-	def search (self, db, entry, on_search_completed_callback, *args):
+	def search (self, db, entry, is_playing, on_search_completed_callback, *args):
 		self.searching = True
 		self.cancel = False
 		self.db = db
@@ -235,6 +235,9 @@ class AmazonCoverArtSearch (object):
 		return (hasattr (item, "LargeImage") or hasattr (item, "MediumImage")) \
 		       and hasattr (item, "ItemAttributes")
 
+	def get_result_pixbuf (self, search_results):
+		return None
+
 	def get_best_match_urls (self, search_results):
 		# Default to "no match", our results must match our criteria
 		best_match = None
diff --git a/plugins/artdisplay/artdisplay/CoverArtDatabase.py b/plugins/artdisplay/artdisplay/CoverArtDatabase.py
index 7cdf1c0..3931698 100644
--- a/plugins/artdisplay/artdisplay/CoverArtDatabase.py
+++ b/plugins/artdisplay/artdisplay/CoverArtDatabase.py
@@ -171,7 +171,7 @@ class CoverArtDatabase (object):
 	def cancel_get_pixbuf (self, entry):
 		self.ticket.purge (entry)
   
-	def get_pixbuf (self, db, entry, callback):
+	def get_pixbuf (self, db, entry, is_playing, callback):
 		if entry is None:
 			callback (entry, None, None)
 			return
@@ -185,10 +185,9 @@ class CoverArtDatabase (object):
 			st_artist = st_artist.replace (char, '')
 			st_album = st_album.replace (char, '')
 
-		rb.Coroutine (self.image_search, db, st_album, st_artist, entry, callback).begin ()
+		rb.Coroutine (self.image_search, db, st_album, st_artist, entry, is_playing, callback).begin ()
 
-
-	def image_search (self, plexer, db, st_album, st_artist, entry, callback):
+	def image_search (self, plexer, db, st_album, st_artist, entry, is_playing, callback):
 		art_location_jpg = self.build_art_cache_filename (db, entry, ART_CACHE_EXTENSION_JPG)
 		art_location_png = self.build_art_cache_filename (db, entry, ART_CACHE_EXTENSION_PNG)
 		blist_location = self.build_art_cache_filename (db, entry, "rb-blist")
@@ -202,7 +201,7 @@ class CoverArtDatabase (object):
 		# Check local cache
 		if art_location:
 			self.ticket.purge (entry)
-			pixbuf = gtk.gdk.pixbuf_new_from_file (art_location)	
+			pixbuf = gtk.gdk.pixbuf_new_from_file (art_location)
 			callback (entry, pixbuf, art_location)
 			return
 
@@ -227,12 +226,42 @@ class CoverArtDatabase (object):
 		ticket = self.ticket.get (entry)
 		for engine, engine_name, engine_remote in self.engines (blist):
 			plexer.clear ()
-			engine.search (db, entry, plexer.send ())
+			engine.search (db, entry, is_playing, plexer.send ())
 			while True:
 				yield None
 				_, (engine, entry, results) = plexer.receive ()
 				if not results:
 					break
+
+				def handle_result_pixbuf (pixbuf, engine_uri, should_save):
+					if self.ticket.release (entry, ticket):
+						if should_save:
+							if pixbuf.get_has_alpha ():
+								pixbuf.save (art_location_png, ART_CACHE_FORMAT_PNG, ART_CACHE_SETTINGS_PNG)
+								uri = art_location_png
+							else:
+								pixbuf.save (art_location_jpg, ART_CACHE_FORMAT_JPG, ART_CACHE_SETTINGS_JPG)
+								uri = art_location_jpg
+						else:
+							uri = engine_uri
+
+						print "found image for %s" % (db.entry_get(entry, rhythmdb.PROP_LOCATION))
+						callback (entry, pixbuf, uri)
+						for m in self.same_search.pop(entry, []):
+							print "and for same search %s" % (db.entry_get(m, rhythmdb.PROP_LOCATION))
+							callback (m, pixbuf, uri)
+
+					self.write_blist (blist_location, blist)
+					self.same_search.pop (entry, None)
+
+
+				# first check if the engine gave us a pixbuf
+				pixbuf = engine.get_result_pixbuf (results)
+				if pixbuf:
+					handle_result_pixbuf (pixbuf, None, True)
+					return
+
+				# then check URIs
 				for url in engine.get_best_match_urls (results):
 					if str(url) == "":
 						print "got empty url from engine %s." % (engine)
@@ -243,24 +272,7 @@ class CoverArtDatabase (object):
 					_, (data, ) = plexer.receive ()
 					pixbuf = self.image_data_load (data)
 					if pixbuf:
-						if self.ticket.release (entry, ticket):
-							if engine_remote:
-								if pixbuf.get_has_alpha ():
-									pixbuf.save (art_location_png, ART_CACHE_FORMAT_PNG, ART_CACHE_SETTINGS_PNG)
-								else:
-									pixbuf.save (art_location_jpg, ART_CACHE_FORMAT_JPG, ART_CACHE_SETTINGS_JPG)
-								uri = art_location
-							else:
-								uri = unquote (str (url))
-
-							print "found image for %s" % (db.entry_get(entry, rhythmdb.PROP_LOCATION))
-							callback (entry, pixbuf, uri)
-							for m in self.same_search.pop(entry, []):
-								print "and for same search %s" % (db.entry_get(m, rhythmdb.PROP_LOCATION))
-								callback (m, pixbuf, uri)
-
-						self.write_blist (blist_location, blist)
-						self.same_search.pop (entry, None)
+						handle_result_pixbuf (pixbuf, url, engine_remote)
 						return
 
 				if not engine.search_next ():
diff --git a/plugins/artdisplay/artdisplay/LocalCoverArtSearch.py b/plugins/artdisplay/artdisplay/LocalCoverArtSearch.py
index cb39779..42e284d 100644
--- a/plugins/artdisplay/artdisplay/LocalCoverArtSearch.py
+++ b/plugins/artdisplay/artdisplay/LocalCoverArtSearch.py
@@ -62,7 +62,7 @@ class LocalCoverArtSearch:
 			if not issubclass (exception, gnomevfs.EOFError):
 				print "Error reading \"%s\": %s" % (self.uri.parent, exception)
 
-	def search (self, db, entry, on_search_completed_cb, *args):
+	def search (self, db, entry, is_playing, on_search_completed_cb, *args):
 		self.uri = None
 		try:
 			self.uri = gnomevfs.URI (entry.get_playback_uri())
@@ -83,6 +83,9 @@ class LocalCoverArtSearch:
 	def search_next (self):
 		return False
 
+	def get_result_pixbuf (self, results):
+		return None
+
 	def get_best_match_urls (self, results):
 		# Compare lower case, without file extension
 		for name in [file_root (self.uri.short_name)] + IMAGE_NAMES:
diff --git a/plugins/artdisplay/artdisplay/LocalCoverArtSearchGIO.py b/plugins/artdisplay/artdisplay/LocalCoverArtSearchGIO.py
index d6522df..b238c41 100644
--- a/plugins/artdisplay/artdisplay/LocalCoverArtSearchGIO.py
+++ b/plugins/artdisplay/artdisplay/LocalCoverArtSearchGIO.py
@@ -69,7 +69,7 @@ class LocalCoverArtSearch:
 			print "okay, probably done: %s" % e
 			on_search_completed_cb(self, entry, results, *args)
 
-	def search (self, db, entry, on_search_completed_cb, *args):
+	def search (self, db, entry, is_playing, on_search_completed_cb, *args):
 
 		self.file = gio.File(entry.get_playback_uri())
 		if self.file.get_uri_scheme() in ('http','cdda','daap'):
@@ -88,6 +88,9 @@ class LocalCoverArtSearch:
 	def search_next (self):
 		return False
 
+	def get_result_pixbuf (self, results):
+		return None
+
 	def get_best_match_urls (self, results):
 		parent = self.file.get_parent()
 
diff --git a/plugins/artdisplay/artdisplay/PodcastCoverArtSearch.py b/plugins/artdisplay/artdisplay/PodcastCoverArtSearch.py
index 263d7bc..5fbd522 100644
--- a/plugins/artdisplay/artdisplay/PodcastCoverArtSearch.py
+++ b/plugins/artdisplay/artdisplay/PodcastCoverArtSearch.py
@@ -28,19 +28,13 @@ import rhythmdb
 
 class PodcastCoverArtSearch (object):
 	def __init__ (self):
-		self.searching = False
-		self.cancel = False
-		self.entry = None
+		pass
 
-	def search (self, db, entry, on_search_completed_callback, *args):
-		self.searching = True
-		self.cancel = False
-		self.entry = entry
-		self.args = args
+	def search (self, db, entry, is_playing, on_search_completed_callback, *args):
 
 		# Check if entry is a podcast for performance
 		if entry.get_entry_type() != db.entry_type_get_by_name("podcast-post"):
-			on_search_completed_callback (self, self.entry, None, *self.args)
+			on_search_completed_callback (self, entry, None, *args)
 			return
 
 		# Retrieve corresponding feed for this entry
@@ -50,11 +44,15 @@ class PodcastCoverArtSearch (object):
 		# Check for PROP_IMAGE in feed
 		image_url = db.entry_get(podcast_feed_entry, rhythmdb.PROP_IMAGE)
 		
-		on_search_completed_callback (self, self.entry, image_url, *self.args)
+		on_search_completed_callback (self, entry, image_url, *args)
 
 	def search_next (self):
 		return False
 
+	def get_result_pixbuf (self, search_results):
+		return None
+
 	def get_best_match_urls (self, search_results):
 		# Return image URL
 		return [search_results]
+
diff --git a/plugins/artdisplay/artdisplay/__init__.py b/plugins/artdisplay/artdisplay/__init__.py
index 1ee337f..e9010a8 100644
--- a/plugins/artdisplay/artdisplay/__init__.py
+++ b/plugins/artdisplay/artdisplay/__init__.py
@@ -401,7 +401,7 @@ class ArtDisplayPlugin (rb.Plugin):
 		# Intitates search in the database (which checks art cache, internet etc.)
 		self.current_entry = entry
 		self.current_pixbuf = None
-		self.art_db.get_pixbuf(db, entry, self.on_get_pixbuf_completed)
+		self.art_db.get_pixbuf(db, entry, True, self.on_get_pixbuf_completed)
 
 	def on_get_pixbuf_completed(self, entry, pixbuf, uri):
 		# Set the pixbuf for the entry returned from the art db
@@ -422,8 +422,9 @@ class ArtDisplayPlugin (rb.Plugin):
 		def callback(entry, pixbuf, uri):
 			a[0] = pixbuf
 			self.on_get_pixbuf_completed(entry, pixbuf, uri)
-			
-		self.art_db.get_pixbuf(db, entry, callback)
+
+		playing = (entry == self.current_entry)
+		self.art_db.get_pixbuf(db, entry, playing, callback)
 
 		# If callback was called synchronously we can return a pixmap
 		return a[0]



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