--- old-bb/plugins/artdisplay/artdisplay/LocalCoverArtSearch.py	2007-07-10 22:37:09.000000000 +1000
+++ new-bb/plugins/artdisplay/artdisplay/LocalCoverArtSearch.py	2007-07-10 22:37:09.000000000 +1000
@@ -32,6 +32,13 @@
 def file_root (f_name):
 	return os.path.splitext (f_name)[0].lower ()
 
+def shared_prefix_length (a, b):
+	l = 0
+	while a[l] == b[l]:
+		l = l+1
+	return l
+
+
 class LocalCoverArtSearch:
 	def __init__ (self, loader):
 		self.loader = loader
@@ -57,6 +64,9 @@
 			on_search_completed_cb (self, entry, [], *args)
 			return
 
+		self.artist = db.entry_get (entry, rhythmdb.PROP_ARTIST)
+		self.album = db.entry_get (entry, rhythmdb.PROP_ALBUM)
+
 		print 'searching for art local to', self.uri
 		gnomevfs.async.load_directory (self.uri.parent, self._load_dir_cb, LOAD_DIRECTORY_FLAGS, ITEMS_PER_NOTIFICATION, data=([], on_search_completed_cb, entry, args))
 
@@ -70,6 +80,26 @@
 				if file_root (f_name) == name:
 					yield self.uri.parent.append_file_name (f_name).path
 
+		# look for file names containing the artist and album (case-insensitive)
+		# (mostly for jamendo downloads)
+		artist = self.artist.lower()
+		album = self.album.lower()
+		for f_name in results:
+			f_root = file_root (f_name).lower()
+			if f_root.find (artist) != -1 and f_root.find (album) != -1:
+				yield self.uri.parent.append_file_name (f_name).path
+
+		# if that didn't work, look for the longest shared prefix
+		# only accept matches longer than 2 to avoid weird false positives
+		match = (2, None)
+		for f_name in results:
+			pl = shared_prefix_length(f_name, self.uri.short_name)
+			if pl > match[0]:
+				match = (pl, f_name)
+
+		if match[1] is not None:
+			yield self.uri.parent.append_file_name (match[1]).path
+
 	def pixbuf_save (self, plexer, pixbuf, uri):
 		gnomevfs.async.create (uri, plexer.send (), gnomevfs.OPEN_WRITE | gnomevfs.OPEN_TRUNCATE, False, 0644, gnomevfs.PRIORITY_DEFAULT)
 		yield None

