Index: src/Banshee.Base/EmbeddedCoverFetcher.cs
===================================================================
RCS file: src/Banshee.Base/EmbeddedCoverFetcher.cs
diff -N src/Banshee.Base/EmbeddedCoverFetcher.cs
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/Banshee.Base/EmbeddedCoverFetcher.cs	28 Dec 2006 04:02:59 -0000
@@ -0,0 +1,131 @@
+/***************************************************************************
+ *  EmbeddedCoverFetcher.cs
+ *
+ *  Copyright (C) 2006 Novell
+ *  Written by Trey Ethridge (tale@juno.com)
+ ****************************************************************************/
+
+/*  THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
+ *
+ *  Permission is hereby granted, free of charge, to any person obtaining a
+ *  copy of this software and associated documentation files (the "Software"),  
+ *  to deal in the Software without restriction, including without limitation  
+ *  the rights to use, copy, modify, merge, publish, distribute, sublicense,  
+ *  and/or sell copies of the Software, and to permit persons to whom the  
+ *  Software is furnished to do so, subject to the following conditions:
+ *
+ *  The above copyright notice and this permission notice shall be included in 
+ *  all copies or substantial portions of the Software.
+ *
+ *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
+ *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
+ *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
+ *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
+ *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ *  DEALINGS IN THE SOFTWARE.
+ */
+
+using System;
+using System.IO;
+using TagLib;
+
+namespace Banshee.Base
+{
+    public class EmbeddedCoverFetcher
+    {
+        private static readonly string cover_art_suffix = "-embedded-image";
+            
+        public static string Fetch(TrackInfo ti, string save_directory)
+        {
+            string image_path = null;
+            
+            if (ti == null) {
+                return image_path;
+            }
+            
+            IPicture[] pictures = EmbeddedCoverFetcher.GetEmbeddedPictures(ti.Uri);
+                    
+            if (pictures != null && pictures.Length > 0) {
+                int preferred_index = EmbeddedCoverFetcher.GetPictureIndexToUse(pictures);
+                IPicture picture = pictures[preferred_index];
+                string ext = EmbeddedCoverFetcher.GetFileExtension(picture);
+                string path = save_directory + ti.TrackId.ToString() + cover_art_suffix + ext;
+                
+                if (EmbeddedCoverFetcher.SavePicture(picture, path)) {                
+                    image_path = path;                
+                } 
+            }
+            
+            return image_path;
+        }
+
+        private static IPicture[] GetEmbeddedPictures(SafeUri uri)
+        {
+            TagLib.File file = StreamTagger.ProcessUri(uri);            
+            return file.Tag.Pictures;            
+        }
+        
+        private static int GetPictureIndexToUse(IPicture[] pictures)
+        {
+            int preferred_index = -1;
+            
+            // Use the front cover.  If we don't find it, use the first image. 
+            for (int i = 0; i < pictures.Length; i++) {
+                if (preferred_index == -1) {
+                    preferred_index = i;
+                }
+                
+                if (pictures[i].Type == PictureType.FrontCover) {
+                    preferred_index = i;
+                    break;
+                }
+            }
+            
+            return preferred_index;
+        }
+        
+        private static string GetFileExtension(IPicture picture)
+        {
+            string ext = "";
+            
+            string[] tokens = picture.MimeType.Split(new char[] {'/'});
+            if (tokens.Length == 2) {
+                ext = "." + tokens[1];
+            }
+            
+            return ext;
+        }
+        
+        private static bool SavePicture(IPicture picture, string image_path)
+        {
+            // If data is empty, return.
+            if (picture.Data.Count == 0) {
+                return false;
+            }
+            
+            // Write the byte array to a file.
+            FileStream fs = null;
+            try {            
+                fs = System.IO.File.Create(image_path);
+            } catch {
+                // Unable to create input file.
+                return false;
+            }
+
+            // Write the byte array to it.
+            try {
+                byte[] data = picture.Data.Data;
+                fs.Write(data, 0, data.Length);
+            } catch {
+                // Unable to write to the file.
+                fs.Close();
+                return false;
+            }
+
+            // Close the file.
+            fs.Close();
+            return true;
+        }
+    }
+}
Index: src/Banshee.Base/Makefile.am
===================================================================
RCS file: /cvs/gnome/banshee/src/Banshee.Base/Makefile.am,v
retrieving revision 1.83
diff -u -r1.83 Makefile.am
--- src/Banshee.Base/Makefile.am	23 Dec 2006 07:04:05 -0000	1.83
+++ src/Banshee.Base/Makefile.am	28 Dec 2006 04:02:59 -0000
@@ -239,6 +239,7 @@
 	$(srcdir)/ArgumentQueue.cs \
 	$(srcdir)/LogCore.cs \
 	$(srcdir)/AmazonCoverFetcher.cs \
+	$(srcdir)/EmbeddedCoverFetcher.cs \
 	$(srcdir)/Paths.cs \
 	$(srcdir)/TrackInfo.cs \
 	$(srcdir)/FileNamePattern.cs \
Index: src/Banshee.Base/TrackInfo.cs
===================================================================
RCS file: /cvs/gnome/banshee/src/Banshee.Base/TrackInfo.cs,v
retrieving revision 1.18
diff -u -r1.18 TrackInfo.cs
--- src/Banshee.Base/TrackInfo.cs	5 Dec 2006 19:06:20 -0000	1.18
+++ src/Banshee.Base/TrackInfo.cs	28 Dec 2006 04:03:00 -0000
@@ -184,6 +184,17 @@
                     }
                 }
                 
+                // TODO: move this code before the directory image
+                // when we enable a way for the user to embed an image
+                // in a track using banshee.  For now, let's leave it here
+                // so that the user can override the image by putting a 
+                // file in the folder.
+                string embedded_cover_art_file = EmbeddedCoverFetcher.Fetch(this, Paths.CoverArtDirectory);
+                if (embedded_cover_art_file != null) {
+                    cover_art_file = embedded_cover_art_file;
+                    return embedded_cover_art_file;
+                }
+                
                 return null;
             }
         }
