Index: TrackInfo.cs
===================================================================
RCS file: /cvs/gnome/banshee/src/Banshee.Base/TrackInfo.cs,v
retrieving revision 1.18
diff -u -r1.18 TrackInfo.cs
--- TrackInfo.cs	5 Dec 2006 19:06:20 -0000	1.18
+++ TrackInfo.cs	21 Dec 2006 04:55:55 -0000
@@ -29,6 +29,7 @@
 using System;
 using System.IO;
 using Mono.Unix;
+using TagLib;
 using Gtk;
  
 namespace Banshee.Base
@@ -158,7 +159,7 @@
                 }
                 
                 string path = Paths.GetCoverArtPath(asin);
-                if(File.Exists(path)) {
+                if(System.IO.File.Exists(path)) {
                     cover_art_file = path;
                     return path;
                 }
@@ -166,7 +167,7 @@
                 string basepath = Path.GetDirectoryName(Uri.AbsolutePath) + Path.DirectorySeparatorChar;
                
                 path = basepath + Asin + ".01._SCLZZZZZZZ_.jpg";
-                if(File.Exists(path)) { 
+                if(System.IO.File.Exists(path)) { 
                     cover_art_file = path;
                     return path;
                 }
@@ -177,15 +178,115 @@
                 foreach(string cover in cover_names) {
                     foreach(string ext in cover_extensions) {
                         string img = basepath + cover + "." + ext;
-                        if(File.Exists(img)) {
+                        if(System.IO.File.Exists(img)) {
                             cover_art_file = img;
                             return img;
                         }
                     }
                 }
                 
+                Console.WriteLine("Couldn't find image for song: " + this.Artist + " - " + this.Title);
+                
+                // tale doit:
+                // try and grab the embedded image, save it, and return that path
+                IPicture[] pictures = GetEmbeddedPictures();
+                
+                if (pictures != null && pictures.Length > 0) {
+                    Console.WriteLine("File has " + pictures.Length + " pictures");
+                    int preferred_index = GetPictureIndexToUse(pictures);
+                    
+                    Console.WriteLine("Preferred index is: " + preferred_index);
+                    IPicture picture = pictures[preferred_index];
+                    
+                    string ext = GetFileExtension(picture);
+                    Console.WriteLine("Extension is: " + ext);
+                    
+                    string cover_art_directory = Paths.CoverArtDirectory;
+                    string image_path = cover_art_directory + TrackId.ToString() + "embedded-image" + ext;
+                    Console.WriteLine("image_path is: " + image_path);
+                    
+                    if (SavePicture(picture, image_path)) {
+                        Console.WriteLine("Saved " + image_path);
+                        return image_path;
+                    } else {
+                        Console.WriteLine("Save failed");
+                    }
+                }
+                
                 return null;
             }
+        }
+        
+        private IPicture[] GetEmbeddedPictures()
+        {
+            IPicture[] pictures = null;
+            TagLib.File file = StreamTagger.ProcessUri(this.Uri);            
+            return file.Tag.Pictures;            
+        }
+        
+        private 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 string GetFileExtension(IPicture picture)
+        {
+            string ext = "";
+            
+            string[] tokens = picture.MimeType.Split(new char[] {'/'});
+            if (tokens.Length == 2) {
+                ext = "." + tokens[1];
+            }
+            
+            return ext;
+        }
+        
+        private 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 {
+                // TODO: change to use Proxy file class
+                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;
         }
        
         public string MimeType { 
