--- plugins/artdisplay/artdisplay/CoverArtDatabase.py	2007/01/18 18:32:49	1.2
+++ plugins/artdisplay/artdisplay/CoverArtDatabase.py	2007/01/18 18:48:24
@@ -91,6 +91,24 @@ class CoverArtDatabase (object):
 			if Engine.__name__ not in blist:
 				yield Engine (self.loader), Engine.__name__, True
 	
+	def set_pixbuf_from_uri (self, db, entry, uri, callback):
+		def loader_cb (data):
+			self.set_pixbuf (db, entry, self.image_data_load (data), callback)
+		self.loader.get_url (str (uri), loader_cb)
+
+	def set_pixbuf (self, db, entry, pixbuf, callback):
+		if entry is None or pixbuf is None:
+			return
+		art_location = self.build_art_cache_filename (db, entry, ART_CACHE_EXTENSION)
+		self.ticket.purge (entry)
+		pixbuf.save (art_location, ART_CACHE_FORMAT, ART_CACHE_SETTINGS)
+		callback (entry, pixbuf, art_location)
+		for Engine in ART_SEARCHES_LOCAL:
+			try:
+				Engine (self.loader).save_pixbuf (db, entry, pixbuf)
+			except AttributeError:
+				pass
+
 	def cancel_get_pixbuf (self, entry):
 		self.ticket.purge (entry)
   
--- plugins/artdisplay/artdisplay/LocalCoverArtSearch.py	2007/01/14 22:42:46	1.1
+++ plugins/artdisplay/artdisplay/LocalCoverArtSearch.py	2007/01/18 19:38:23
@@ -23,6 +23,9 @@ import gnomevfs
 IMAGE_NAMES = ["cover", "album", "albumart", ".folder", "folder"]
 LOAD_DIRECTORY_FLAGS = gnomevfs.FILE_INFO_GET_MIME_TYPE | gnomevfs.FILE_INFO_FORCE_FAST_MIME_TYPE
 ITEMS_PER_NOTIFICATION = 10
+ART_SAVE_NAME = 'Cover.jpg'
+ART_SAVE_FORMAT = 'jpeg'
+ART_SAVE_SETTINGS = {"quality": "100"}
 
 def file_root (f_name):
 	return os.path.splitext (f_name)[0].lower ()
@@ -65,3 +68,43 @@ class LocalCoverArtSearch:
 				if file_root (f_name) == name:
 					yield self.uri.parent.append_file_name (f_name).path
 
+	def _save_dir_cb (self, handle, files, exception, (db, entry, pixbuf)):
+		artist, album = [db.entry_get (entry, x) for x in [rhythmdb.PROP_ARTIST, rhythmdb.PROP_ALBUM]]
+		for f in files:
+			if f.mime_type.startswith ("image/"):
+				return
+			uri = str (self.uri.parent.append_file_name (f.name))
+			if self.uri.scheme == 'file':
+				# see rb_canonicalise_uri ()
+				uri = "file://" + gnomevfs.escape_set(gnomevfs.escape_path_string(uri[7:]), "&=")
+			u_entry = db.entry_lookup_by_location (uri)
+			if u_entry:
+				u_artist, u_album = [db.entry_get (u_entry, x) for x in [rhythmdb.PROP_ARTIST, rhythmdb.PROP_ALBUM]]
+				if artist, album != u_artist, u_album:
+					print "Not saving local art; encountered media with different artist/album (%s, %s, %s)" % (uri, u_artist, u_album)
+					handle.cancel ()
+				return
+			print "Not saving local art; encountered unknown file (%s)" % uri
+			handle.cancel ()
+			return
+		if exception:
+			if issubclass (exception, gnomevfs.EOFError):
+				art_uri = str (self.uri.parent.append_file_name (ART_SAVE_NAME))
+				print "Saving local art to \"%s\"" % art_uri
+				pixbuf.save (art_uri, ART_SAVE_FORMAT, ART_SAVE_SETTINGS)
+			else:
+				print "Error reading \"%s\": %s" % (self.uri.parent, exception)
+
+	def save_pixbuf (self, db, entry, pixbuf):
+		self.uri = None
+		try:
+			self.uri = gnomevfs.URI (entry.get_playback_uri())
+		except TypeError:
+			pass
+
+		if self.uri is None or self.uri.scheme == 'http':
+			print 'not saving local art to', self.uri
+			return
+
+		print 'checking whether to save local art to', self.uri
+		gnomevfs.async.load_directory (self.uri.parent, self._save_dir_cb, LOAD_DIRECTORY_FLAGS, ITEMS_PER_NOTIFICATION, data=(db, entry, pixbuf))
