Author: jwilcox
Date: 2006-06-21 20:03:12 -0400 (Wed, 21 Jun 2006)
New Revision: 61924

Added:
   trunk/ipod-sharp/src/Photo.cs
Removed:
   trunk/ipod-sharp/src/Image.cs
Modified:
   trunk/ipod-sharp/ChangeLog
   trunk/ipod-sharp/src/Album.cs
   trunk/ipod-sharp/src/Device.cs
   trunk/ipod-sharp/src/Makefile.am
   trunk/ipod-sharp/src/PhotoDatabase.cs
   trunk/ipod-sharp/src/Thumbnail.cs
   trunk/ipod-sharp/src/Track.cs
   trunk/ipod-sharp/src/TrackDatabase.cs
   trunk/ipod-sharp/ui/ProgressDialog.cs
Log:
2006-06-21  James Willcox  <[EMAIL PROTECTED]>

        * Fix a ton of photo/artwork bugs, flesh out the public API some more



Modified: trunk/ipod-sharp/ChangeLog
===================================================================
--- trunk/ipod-sharp/ChangeLog  2006-06-21 23:31:49 UTC (rev 61923)
+++ trunk/ipod-sharp/ChangeLog  2006-06-22 00:03:12 UTC (rev 61924)
@@ -1,3 +1,7 @@
+2006-06-21  James Willcox  <[EMAIL PROTECTED]>
+
+       * Fix a ton of photo/artwork bugs, flesh out the public API some more
+
 2006-06-16  James Willcox  <[EMAIL PROTECTED]>
 
        * src/PhotoDatabase.cs: more write fixes.  It can now read/write the

Modified: trunk/ipod-sharp/src/Album.cs
===================================================================
--- trunk/ipod-sharp/src/Album.cs       2006-06-21 23:31:49 UTC (rev 61923)
+++ trunk/ipod-sharp/src/Album.cs       2006-06-22 00:03:12 UTC (rev 61924)
@@ -7,13 +7,12 @@
     public class Album {
 
         private AlbumRecord record;
-        private PhotoDatabase db;
 
-        private List<Image> images;
+        private List<Photo> photos;
 
-        public ReadOnlyCollection<Image> Images {
+        public ReadOnlyCollection<Photo> Photos {
             get {
-                return new ReadOnlyCollection<Image> (images);
+                return new ReadOnlyCollection<Photo> (photos);
             }
         }
 
@@ -32,14 +31,23 @@
         
         internal Album (AlbumRecord record, PhotoDatabase db) {
             this.record = record;
-            this.db = db;
 
-            images = new List<Image> ();
+            photos = new List<Photo> ();
 
             foreach (AlbumItemRecord item in record.Items) {
-                Image img = db.LookupImageById (item.ImageId);
-                images.Add (img);
+                Photo photo = db.LookupPhotoById (item.Id);
+                photos.Add (photo);
             }
         }
+
+        public void Add (Photo photo) {
+            record.AddItem (new AlbumItemRecord (record.IsBE, photo.Id));
+            photos.Add (photo);
+        }
+
+        public void Remove (Photo photo) {
+            record.RemoveItem (photo.Id);
+            photos.Remove (photo);
+        }
     }
 }

Modified: trunk/ipod-sharp/src/Device.cs
===================================================================
--- trunk/ipod-sharp/src/Device.cs      2006-06-21 23:31:49 UTC (rev 61923)
+++ trunk/ipod-sharp/src/Device.cs      2006-06-22 00:03:12 UTC (rev 61924)
@@ -2,6 +2,8 @@
 using System;
 using System.IO;
 using System.Collections;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.Runtime.InteropServices;
 
 namespace IPod {
@@ -79,6 +81,7 @@
         private EqualizerContainerRecord eqsrec;
         private TrackDatabase tracks;
         private PhotoDatabase photos;
+        private Dictionary<int, ArtworkFormat> formats = new Dictionary<int, 
ArtworkFormat> ();
 
         public event EventHandler Changed;
 
@@ -229,8 +232,13 @@
             }
         }
 
-        public ArtworkFormat[] ArtworkFormats {
+        public IList<ArtworkFormat> ArtworkFormats {
             get {
+                return new ReadOnlyCollection<ArtworkFormat> (new 
List<ArtworkFormat> (formats.Values));
+            }
+        }
+
+                /*
                 IntPtr array = (IntPtr) GetProperty ("artwork-formats").Val;
 
                 if (array == IntPtr.Zero) {
@@ -262,8 +270,7 @@
                 }
 
                 return (ArtworkFormat[]) list.ToArray (typeof (ArtworkFormat));
-            }
-        }
+                */
 
         public Equalizer[] Equalizers {
             get {
@@ -315,11 +322,45 @@
             if (Raw == IntPtr.Zero) {
                 throw new DeviceException (this, "Failed to create device");
             }
+
+            // load the artwork formats
+            IntPtr array = (IntPtr) GetProperty ("artwork-formats").Val;
+
+            if (array == IntPtr.Zero) {
+                return;
+            }
+                
+            int offset = 0;
+            
+            while (true) {
+                int type = Marshal.ReadInt32 (array, offset);
+                offset += 4;
+                
+                if (type == -1)
+                    break;
+                
+                short width = Marshal.ReadInt16 (array, offset);
+                offset += 2;
+                
+                short height = Marshal.ReadInt16 (array, offset);
+                offset += 2;
+                
+                short correlationId = Marshal.ReadInt16 (array, offset);
+                offset += 2;
+                
+                offset += 2; // two bytes of padding after the struct; if you 
read it, it's always 0xffff
+                
+                formats[correlationId] = new ArtworkFormat ((ArtworkType) 
type, width, height, correlationId);
+            }
         }
 
         public Device (string mountOrDevice) : this (ipod_device_new 
(mountOrDevice)) {
         }
 
+        internal ArtworkFormat LookupFormat (int correlationId) {
+            return formats[correlationId];
+        }
+
         public void LoadPhotoDatabase () {
             LoadPhotoDatabase (false);
         }

Deleted: trunk/ipod-sharp/src/Image.cs
===================================================================
--- trunk/ipod-sharp/src/Image.cs       2006-06-21 23:31:49 UTC (rev 61923)
+++ trunk/ipod-sharp/src/Image.cs       2006-06-22 00:03:12 UTC (rev 61924)
@@ -1,51 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-
-namespace IPod {
-
-    public class Image {
-
-        private ImageItemRecord item;
-        private Device device;
-        private List<Thumbnail> thumbnails = new List<Thumbnail> ();
-
-        internal ImageItemRecord Record {
-            get { return item; }
-        }
-
-        internal Device Device {
-            get { return device; }
-        }
-
-        public DateTime OriginalDate {
-            get { return item.OriginalDate; }
-            set { item.OriginalDate = value; }
-        }
-
-        public DateTime DigitizedDate {
-            get { return item.DigitizedDate; }
-            set { item.DigitizedDate = value; }
-        }
-
-        // FIXME: use an enum or whatever
-        public int Rating {
-            get { return item.Rating; }
-            set { item.Rating = value; }
-        }
-
-        public ReadOnlyCollection<Thumbnail> Thumbnails {
-            get { return new ReadOnlyCollection<Thumbnail> (thumbnails); }
-        }
-        
-        internal Image (ImageItemRecord item, Device device) {
-            this.item = item;
-            this.device = device;
-
-            foreach (ImageNameRecord name in item.Names) {
-                thumbnails.Add (new Thumbnail (this, name));
-            }
-        }
-    }
-
-}

Modified: trunk/ipod-sharp/src/Makefile.am
===================================================================
--- trunk/ipod-sharp/src/Makefile.am    2006-06-21 23:31:49 UTC (rev 61923)
+++ trunk/ipod-sharp/src/Makefile.am    2006-06-22 00:03:12 UTC (rev 61924)
@@ -25,8 +25,8 @@
        $(srcdir)/DeviceBusyException.cs        \
        $(srcdir)/EndianBinaryWriter.cs         \
        $(srcdir)/Equalizer.cs          \
-       $(srcdir)/Image.cs      \
        $(srcdir)/Initializer.cs        \
+       $(srcdir)/Photo.cs      \
        $(srcdir)/PhotoDatabase.cs              \
        $(srcdir)/Playlist.cs           \
        $(srcdir)/ObjectDumper.cs               \

Copied: trunk/ipod-sharp/src/Photo.cs (from rev 61723, 
trunk/ipod-sharp/src/Image.cs)
===================================================================
--- trunk/ipod-sharp/src/Image.cs       2006-06-14 23:41:25 UTC (rev 61723)
+++ trunk/ipod-sharp/src/Photo.cs       2006-06-22 00:03:12 UTC (rev 61924)
@@ -0,0 +1,108 @@
+using System;
+using System.IO;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+
+namespace IPod {
+
+    public class Photo {
+
+        private ImageItemRecord item;
+        private PhotoDatabase db;
+        private List<Thumbnail> thumbnails = new List<Thumbnail> ();
+        private string fullSizeFile;
+        private bool needsCopy = false;
+
+        internal int Id {
+            get { return item.Id; }
+        }
+        
+        internal ImageItemRecord Record {
+            get { return item; }
+        }
+
+        internal PhotoDatabase PhotoDatabase {
+            get { return db; }
+        }
+
+        internal bool NeedsCopy {
+            get { return needsCopy; }
+        }
+
+        public DateTime OriginalDate {
+            get { return item.OriginalDate; }
+            set { item.OriginalDate = value; }
+        }
+
+        public DateTime DigitizedDate {
+            get { return item.DigitizedDate; }
+            set { item.DigitizedDate = value; }
+        }
+
+        // FIXME: use an enum or whatever
+        public int Rating {
+            get { return item.Rating; }
+            set { item.Rating = value; }
+        }
+
+        public IList<Thumbnail> Thumbnails {
+            get { return new ReadOnlyCollection<Thumbnail> (thumbnails); }
+        }
+
+        public string FullSizeFileName {
+            get {
+                if (fullSizeFile != null) {
+                    return fullSizeFile;
+                } else if (item.FullName == null) {
+                    return null;
+                } else {
+                    return db.GetFilesystemPath (item.FullName.FileName);
+                }
+            } set {
+                fullSizeFile = value;
+                needsCopy = true;
+            }
+        }
+        
+        internal Photo (ImageItemRecord item, PhotoDatabase db) {
+            this.item = item;
+            this.db = db;
+
+            foreach (ImageNameRecord name in item.Names) {
+                thumbnails.Add (new Thumbnail (this, name));
+            }
+        }
+
+        internal void SetPodFileName () {
+            item.FullName.FileName = String.Format (":Full 
Resolution:{0}:{1}:{2}:{3}", OriginalDate.Year,
+                                                    OriginalDate.Month, 
OriginalDate.Day,
+                                                    Path.GetFileName 
(fullSizeFile));
+        }
+
+        public Thumbnail CreateThumbnail () {
+            ImageNameRecord name = new ImageNameRecord (item.IsBE);
+            item.AddName (name);
+            
+            Thumbnail thumbnail = new Thumbnail (this, name);
+            thumbnails.Add (thumbnail);
+
+            return thumbnail;
+        }
+
+        public void RemoveThumbnail (Thumbnail thumbnail) {
+            item.RemoveName (thumbnail.Record);
+            thumbnails.Remove (thumbnail);
+        }
+
+        public Thumbnail LookupThumbnail (ArtworkFormat format) {
+            foreach (Thumbnail thumbnail in thumbnails) {
+                if (thumbnail.Format == format) {
+                    return thumbnail;
+                }
+            }
+
+            return null;
+        }
+    }
+
+}

Modified: trunk/ipod-sharp/src/PhotoDatabase.cs
===================================================================
--- trunk/ipod-sharp/src/PhotoDatabase.cs       2006-06-21 23:31:49 UTC (rev 
61923)
+++ trunk/ipod-sharp/src/PhotoDatabase.cs       2006-06-22 00:03:12 UTC (rev 
61924)
@@ -3,7 +3,10 @@
 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
+using System.Runtime.InteropServices;
 using System.IO;
+using Mono.Unix;
+using Mono.Unix.Native;
 
 namespace IPod {
 
@@ -77,6 +80,11 @@
 
         public DataFileRecord (bool isbe) : base (isbe) {
             this.Name = "mhfd";
+            recordPadding = 64;
+
+            children.Add (new PhotoDataSetRecord (isbe, new ImageListRecord 
(isbe), PhotoDataSetIndex.ImageList));
+            children.Add (new PhotoDataSetRecord (isbe, new AlbumListRecord 
(isbe), PhotoDataSetIndex.AlbumList));
+            children.Add (new PhotoDataSetRecord (isbe, new FileListRecord 
(isbe), PhotoDataSetIndex.FileList));
         }
         
         public override void Read (BinaryReader reader) {
@@ -144,8 +152,15 @@
 
         public PhotoDbRecord Data;
 
-        public PhotoDataSetRecord (bool isbe) : base (isbe) {
+        public PhotoDataSetRecord (bool isbe) : this (isbe, null, 0) {
+        }
+        
+        public PhotoDataSetRecord (bool isbe, PhotoDbRecord data, 
PhotoDataSetIndex index) : base (isbe) {
             this.Name = "mhsd";
+            recordPadding = 80;
+
+            this.Data = data;
+            this.Index = index;
         }
         
         public override void Read (BinaryReader reader) {
@@ -208,6 +223,7 @@
         
         public AlbumListRecord (bool isbe) : base (isbe) {
             this.Name = "mhla";
+            recordPadding = 80;
         }
         
         public override void Read (BinaryReader reader) {
@@ -250,7 +266,7 @@
 
         private PhotoDetailRecord nameRecord;
 
-        private List<AlbumItemRecord> items = new List<AlbumItemRecord> ();
+        private Dictionary<int, AlbumItemRecord> items = new Dictionary<int, 
AlbumItemRecord> ();
         
         public int PlaylistId;
         public bool IsMaster;
@@ -266,7 +282,7 @@
 
         public IList<AlbumItemRecord> Items {
             get {
-                return new ReadOnlyCollection<AlbumItemRecord> (items);
+                return new ReadOnlyCollection<AlbumItemRecord> (new 
List<AlbumItemRecord> (items.Values));
             }
         }
 
@@ -278,14 +294,22 @@
         public AlbumRecord (bool isbe) : base (isbe) {
             this.Name = "mhba";
             nameRecord = new PhotoDetailRecord (IsBE);
+            nameRecord.Type = PhotoDetailType.String;
+            nameRecord.BrokenChildPadding = true;
+
+            recordPadding = 84;
         }
 
         public void AddItem (AlbumItemRecord item) {
-            items.Add (item);
+            items[item.Id] = item;
         }
 
+        public void RemoveItem (int id) {
+            items.Remove (id);
+        }
+
         public void RemoveItem (AlbumItemRecord item) {
-            items.Remove (item);
+            items.Remove (item.Id);
         }
         
         public override void Read (BinaryReader reader) {
@@ -324,10 +348,13 @@
                 }
             }
 
+            items.Clear ();
+
             for (int i = 0; i < numAlbums; i++) {
                 AlbumItemRecord item = new AlbumItemRecord (IsBE);
                 item.Read (reader);
-                items.Add (item);
+
+                AddItem (item);
             }
         }
 
@@ -340,7 +367,7 @@
             byte[] childBytes;
             int childLen;
 
-            SaveChildren (items, out childBytes, out childLen);
+            SaveChildren (items.Values, out childBytes, out childLen);
 
             WriteName (writer);
             writer.Write (64 + recordPadding);
@@ -373,10 +400,16 @@
 
         private int unknownOne;
 
-        public int ImageId;
+        public int Id;
 
-        public AlbumItemRecord (bool isbe) : base (isbe) {
+        public AlbumItemRecord (bool isbe) : this (isbe, 0) {
+        }
+
+        public AlbumItemRecord (bool isbe, int id) : base (isbe) {
             this.Name = "mhia";
+            this.Id = id;
+
+            recordPadding = 20;
         }
         
         public override void Read (BinaryReader reader) {
@@ -385,7 +418,7 @@
             byte[] body = reader.ReadBytes (HeaderOne - 12);
 
             unknownOne = ToInt32 (body, 0);
-            ImageId = ToInt32 (body, 4);
+            Id = ToInt32 (body, 4);
 
             recordPadding = body.Length - 8;
         }
@@ -395,7 +428,7 @@
             writer.Write (20 + recordPadding);
             writer.Write (20 + recordPadding);
             writer.Write (unknownOne);
-            writer.Write (ImageId);
+            writer.Write (Id);
             WritePadding (writer);
         }
     }
@@ -404,6 +437,7 @@
     internal class ImageListRecord : PhotoDbRecord {
 
         private List<ImageItemRecord> items = new List<ImageItemRecord> ();
+        private List<ImageItemRecord> removedItems = new List<ImageItemRecord> 
();
 
         public IList<ImageItemRecord> Items {
             get {
@@ -411,16 +445,25 @@
             }
         }
 
+        public IList<ImageItemRecord> RemovedItems {
+            get {
+                return new ReadOnlyCollection<ImageItemRecord> (removedItems);
+            }
+        }
+
         public ImageListRecord (bool isbe) : base (isbe) {
             this.Name = "mhli";
+            recordPadding = 80;
         }
 
         public void AddItem (ImageItemRecord item) {
-            items[item.Id] = item;
+            items.Add (item);
+            removedItems.Remove (item);
         }
 
         public void RemoveItem (ImageItemRecord item) {
             items.Remove (item);
+            removedItems.Add (item);
         }
 
         public override void Read (BinaryReader reader) {
@@ -433,7 +476,7 @@
             for (int i = 0; i < this.HeaderTwo; i++) {
                 ImageItemRecord item = new ImageItemRecord (IsBE);
                 item.Read (reader);
-
+                
                 items.Add (item);
             }
         }
@@ -486,7 +529,6 @@
 
             byte[] body = reader.ReadBytes (this.HeaderOne - 12);
             Type = (PhotoDetailType) ToInt16 (body, 0);
-            short childPadding = ToInt16 (body, 2);
 
             switch (Type) {
             case PhotoDetailType.ThumbnailContainer:
@@ -592,18 +634,27 @@
         }
     }
 
+    internal class ImageNameRecordSorter : IComparer<ImageNameRecord> {
+
+        public int Compare (ImageNameRecord a, ImageNameRecord b) {
+            return a.ThumbnailOffset.CompareTo (b.ThumbnailOffset);
+        }
+    }
+
     internal class ImageNameRecord : PhotoDbRecord {
 
         private PhotoDetailRecord fileDetail;
         
-        public int CorrelationID;
-        public int ThumbnailOffset;
+        public int CorrelationId;
+        public int ThumbnailOffset = -1;
         public int ImageSize;
         public short VerticalPadding;
         public short HorizontalPadding;
         public short ImageHeight;
         public short ImageWidth;
 
+        public bool Dirty = false;
+        
         public string FileName {
             get { return fileDetail.Value; }
             set { fileDetail.Value = value; }
@@ -611,8 +662,10 @@
 
         public ImageNameRecord (bool isbe) : base (isbe) {
             this.Name = "mhni";
+            recordPadding = 40;
 
             fileDetail = new PhotoDetailRecord (IsBE);
+            fileDetail.Type = PhotoDetailType.FileName;
         }
 
         public override void Read (BinaryReader reader) {
@@ -621,7 +674,7 @@
             byte[] body = reader.ReadBytes (this.HeaderOne - 12);
 
             int numChildren = ToInt32 (body, 0);
-            CorrelationID = ToInt32 (body, 4);
+            CorrelationId = ToInt32 (body, 4);
             ThumbnailOffset = ToInt32 (body, 8);
             ImageSize = ToInt32 (body, 12);
             VerticalPadding = ToInt16 (body, 16);
@@ -641,6 +694,17 @@
             }
         }
 
+        public void SetThumbFileName (bool isPhoto) {
+            if (CorrelationId <= 0)
+                return;
+
+            if (isPhoto) {
+                fileDetail.Value = String.Format (":Thumbs:F{0}_1.ithmb", 
CorrelationId);
+            } else {
+                fileDetail.Value = String.Format (":F{0}_1.ithmb", 
CorrelationId);
+            }
+        }
+
         public override void Save (BinaryWriter writer) {
             byte[] childBytes;
             int childLen;
@@ -652,7 +716,7 @@
             writer.Write (36 + recordPadding);
             writer.Write (36 + recordPadding + childLen);
             writer.Write (1);
-            writer.Write (CorrelationID);
+            writer.Write (CorrelationId);
             writer.Write (ThumbnailOffset);
             writer.Write (ImageSize);
             writer.Write (VerticalPadding);
@@ -662,39 +726,81 @@
             WritePadding (writer);
             writer.Write (childBytes, 0, childLen);
         }
+
+        public byte[] GetData (Stream stream) {
+            stream.Seek ((long) ThumbnailOffset, SeekOrigin.Begin);
+
+            byte[] buf = new byte[ImageSize];
+            stream.Read (buf, 0, ImageSize);
+            return buf;
+        }
+
+        public void SetData (Stream stream, byte[] data) {
+            SetData (stream, data, ThumbnailOffset);
+        }
+
+        public void SetData (Stream stream, byte[] data, int offset) {
+            if (offset < 0) {
+                stream.Seek (0, SeekOrigin.End);
+            } else {
+                stream.Seek (offset, SeekOrigin.Begin);
+            }
+
+            stream.Write (data, 0, data.Length);
+        }
     }
 
     internal class ImageItemRecord : PhotoDbRecord {
 
         private int unknownOne;
         private int unknownTwo;
-        private int sourceImageSize;
-        private ImageNameRecord fullName;
 
         private List<ImageNameRecord> names = new List<ImageNameRecord> ();
+        private List<ImageNameRecord> newNames = new List<ImageNameRecord> ();
+        private List<ImageNameRecord> removedNames = new List<ImageNameRecord> 
();
         
         public int Id;
         public long TrackId;
         public int Rating;
-        public DateTime OriginalDate;
-        public DateTime DigitizedDate;
+        public DateTime OriginalDate = DateTime.Now;
+        public DateTime DigitizedDate = DateTime.Now;
+        public int SourceImageSize;
 
         public IList<ImageNameRecord> Names {
             get {
                 return new ReadOnlyCollection<ImageNameRecord> (names);
             }
         }
+
+        public IList<ImageNameRecord> NewNames {
+            get {
+                return new ReadOnlyCollection<ImageNameRecord> (newNames);
+            }
+        }
+
+        public IList<ImageNameRecord> RemovedNames {
+            get {
+                return new ReadOnlyCollection<ImageNameRecord> (removedNames);
+            }
+        }
+
+        public ImageNameRecord FullName;
         
         public ImageItemRecord (bool isbe) : base (isbe) {
             this.Name = "mhii";
+            recordPadding = 100;
+
+            FullName = new ImageNameRecord (isbe);
         }
 
         public void AddName (ImageNameRecord name) {
             names.Add (name);
+            newNames.Add (name);
         }
 
         public void RemoveName (ImageNameRecord name) {
             names.Remove (name);
+            removedNames.Add (name);
         }
 
         public override void Read (BinaryReader reader) {
@@ -710,7 +816,7 @@
             unknownTwo = ToInt32 (body, 24);
             OriginalDate = Utility.MacTimeToDate (ToUInt32 (body, 28));
             DigitizedDate = Utility.MacTimeToDate (ToUInt32 (body, 32));
-            sourceImageSize = ToInt32 (body, 36);
+            SourceImageSize = ToInt32 (body, 36);
 
             recordPadding = body.Length - 40;
 
@@ -721,7 +827,7 @@
                 if (detail.Type == PhotoDetailType.ThumbnailContainer) {
                     names.Add (detail.ImageName);
                 } else {
-                    fullName = detail.ImageName;
+                    FullName = detail.ImageName;
                 }
             }
         }
@@ -736,8 +842,8 @@
                 details.Add (new PhotoDetailRecord (IsBE, name, 
PhotoDetailType.ThumbnailContainer));
             }
 
-            if (fullName != null) {
-                details.Insert (0, new PhotoDetailRecord (IsBE, fullName, 
PhotoDetailType.ImageContainer));
+            if (FullName != null && FullName.FileName != null) {
+                details.Insert (0, new PhotoDetailRecord (IsBE, FullName, 
PhotoDetailType.ImageContainer));
             }
             
             SaveChildren (details, out childBytes, out childLen);
@@ -753,7 +859,7 @@
             writer.Write (unknownTwo);
             writer.Write (Utility.DateToMacTime (OriginalDate));
             writer.Write (Utility.DateToMacTime (DigitizedDate));
-            writer.Write (sourceImageSize);
+            writer.Write (SourceImageSize);
             WritePadding (writer);
             writer.Write (childBytes, 0, childLen);
         }
@@ -761,15 +867,36 @@
 
     internal class FileListRecord : PhotoDbRecord {
 
-        private List<FileRecord> files = new List<FileRecord> ();
+        private Dictionary<int, FileRecord> files = new Dictionary<int, 
FileRecord> ();
 
         public IList<FileRecord> Files {
-            get { return new ReadOnlyCollection<FileRecord> (files); }
+            get { return new ReadOnlyCollection<FileRecord> (new 
List<FileRecord> (files.Values)); }
         }
         
         public FileListRecord (bool isbe) : base (isbe) {
             this.Name = "mhlf";
+            recordPadding = 80;
         }
+
+        public void AddFile (FileRecord file) {
+            files[file.CorrelationId] = file;
+        }
+
+        public void RemoveFile (int correlationId) {
+            files.Remove (correlationId);
+        }
+
+        public void RemoveFile (FileRecord file) {
+            RemoveFile (file.CorrelationId);
+        }
+
+        public FileRecord LookupFile (int correlationId) {
+            try {
+                return files[correlationId];
+            } catch (KeyNotFoundException e) {
+                return null;
+            }
+        }
         
         public override void Read (BinaryReader reader) {
             base.Read (reader);
@@ -783,7 +910,7 @@
             for (int i = 0; i < numChildren; i++) {
                 FileRecord record = new FileRecord (IsBE);
                 record.Read (reader);
-                files.Add (record);
+                files[record.CorrelationId] = record;
             }
         }
 
@@ -791,11 +918,11 @@
             byte[] childBytes;
             int childLen;
 
-            SaveChildren (files, out childBytes, out childLen);
+            SaveChildren (files.Values, out childBytes, out childLen);
 
             WriteName (writer);
             writer.Write (12 + recordPadding);
-            writer.Write (files.Count);
+            writer.Write (files.Values.Count);
             WritePadding (writer);
             writer.Write (childBytes, 0, childLen);
         }
@@ -810,6 +937,7 @@
 
         public FileRecord (bool isbe) : base (isbe) {
             this.Name = "mhif";
+            recordPadding = 100;
         }
         
         public override void Read (BinaryReader reader) {
@@ -835,16 +963,42 @@
         }
     }
 
+    public class PhotoSaveProgressArgs : EventArgs {
+
+        private double percent;
+
+        public double Percent {
+            get { return percent; }
+        }
+        
+        public PhotoSaveProgressArgs (double percent) {
+            this.percent = percent;
+        }
+    }
+
+    public delegate void PhotoSaveProgressHandler (object o, 
PhotoSaveProgressArgs args);
+
     public class PhotoDatabase {
 
+        private const ulong MapSize = 32 * 1024 * 1024;
+
         private Device device;
-        private Dictionary<int, Image> images = new Dictionary<int, Image> ();
+        private Dictionary<int, Photo> photos = new Dictionary<int, Photo> ();
+        private Dictionary<long, Photo> trackPhotos = new Dictionary<long, 
Photo> ();
         private List<Album> albums = new List<Album> ();
         private DataFileRecord dfr;
-        private bool isPhoto;
+        private bool isPhoto; // is this the photo database or the artwork 
database
 
+        private Stream tempStream; // place where we can put to-be-saved 
thumbnails
         private Album masterAlbum;
 
+        private List<Photo> addedPhotos = new List<Photo> ();
+        private List<Photo> removedPhotos = new List<Photo> ();
+
+        public event EventHandler SaveStarted;
+        public event PhotoSaveProgressHandler SaveProgressChanged;
+        public event EventHandler SaveEnded;
+
         private string PhotoDbPath {
             get {
                 if (isPhoto) {
@@ -861,13 +1015,21 @@
             }
         }
 
+        internal Device Device {
+            get { return device; }
+        }
+
+        internal bool IsPhotoDatabase {
+            get { return isPhoto; }
+        }
+
         public IList<Album> Albums {
             get { return new ReadOnlyCollection<Album> (albums); }
         }
 
-        public IList<Image> Images {
+        public IList<Photo> Photos {
             get {
-                return new ReadOnlyCollection<Image> (new List<Image> 
(images.Values));
+                return new ReadOnlyCollection<Photo> (new List<Photo> 
(photos.Values));
             }
         }
         
@@ -883,12 +1045,21 @@
         }
 
         public void Reload (bool createFresh) {
-            images.Clear ();
+            photos.Clear ();
             albums.Clear ();
+
+            CloseTempFile ();
             
             dfr = new DataFileRecord (device.IsBE);
 
-            if (createFresh)
+            if (isPhoto) {
+                // create a default master album if this is a photo database
+                masterAlbum = CreateAlbum ();
+                masterAlbum.Name = "My Pictures";
+                masterAlbum.Record.IsMaster = true;
+            }
+            
+            if (createFresh || !File.Exists (PhotoDbPath))
                 return;
 
             using (BinaryReader reader = new BinaryReader (File.Open 
(PhotoDbPath, FileMode.Open))) {
@@ -897,13 +1068,18 @@
                 PhotoDataSetRecord albumSet = dfr[PhotoDataSetIndex.AlbumList];
                 ImageListRecord imageList = 
dfr[PhotoDataSetIndex.ImageList].Data as ImageListRecord;
 
-                foreach (ImageItemRecord image in imageList.Items) {
-                    images[image.Id] = new Image (image, device);
+                foreach (ImageItemRecord item in imageList.Items) {
+                    Photo photo = new Photo (item, this);
+                    photos[item.Id] = photo;
+
+                    if (item.TrackId != 0) {
+                        trackPhotos[item.TrackId] = photo;
+                    }
                 }
 
                 foreach (AlbumRecord albumRecord in (albumSet.Data as 
AlbumListRecord).Albums) {
                     Album album = new Album (albumRecord, this);
-
+                    
                     if (album.IsMaster) {
                         masterAlbum = album;
                     } else {
@@ -914,14 +1090,311 @@
         }
 
         public void Save () {
-            using (BinaryWriter writer = new BinaryWriter (File.Open 
(PhotoDbPath, FileMode.Create))) {
-                dfr.Save (writer);
+            if (SaveStarted != null)
+                SaveStarted (this, new EventArgs ());
+
+            try {
+                SaveThumbnails ();
+                CloseTempFile ();
+                
+                List<Photo> dirtyPhotos = new List<Photo> ();
+                foreach (Photo photo in photos.Values) {
+                    if (photo.NeedsCopy) {
+                        photo.SetPodFileName ();
+                        dirtyPhotos.Add (photo);
+                    }
+                }
+                
+                using (BinaryWriter writer = new BinaryWriter (File.Open 
(PhotoDbPath, FileMode.Create))) {
+                    dfr.Save (writer);
+                }
+
+                for (int i = 0; i < dirtyPhotos.Count; i++) {
+                    if (SaveProgressChanged != null)
+                        SaveProgressChanged (this, new PhotoSaveProgressArgs 
((double) i / (double) dirtyPhotos.Count));
+                    CopyPhoto (dirtyPhotos[i]);
+                }
+
+                if (SaveProgressChanged != null)
+                    SaveProgressChanged (this, new PhotoSaveProgressArgs 
((double) 1.0));
+            } catch (Exception e) {
+                throw new DatabaseWriteException (e, "Failed to save 
database");
+            } finally {
+                if (SaveEnded != null)
+                    SaveEnded (this, new EventArgs ());
             }
         }
 
-        internal Image LookupImageById (int id) {
-            return images[id];
+        private void CopyPhoto (Photo photo) {
+            string src = photo.FullSizeFileName;
+            string dest = GetFilesystemPath (photo.Record.FullName.FileName);
+
+            string destdir = Path.GetDirectoryName (dest);
+            if (!Directory.Exists (destdir)) {
+                Directory.CreateDirectory (destdir);
+            }
+            
+            File.Copy (src, dest, true);
         }
+
+        private Album CreateAlbum () {
+            AlbumRecord record = new AlbumRecord (device.IsBE);
+
+            AlbumListRecord albumList = dfr[PhotoDataSetIndex.AlbumList].Data 
as AlbumListRecord;
+            albumList.AddAlbum (record);
+
+            return new Album (record, this);
+        }
+
+        public Album CreateAlbum (string name) {
+            Album album = CreateAlbum ();
+            album.Name = name;
+            albums.Add (album);
+            return album;
+        }
+
+        public void RemoveAlbum (Album album) {
+            AlbumListRecord albumList = dfr[PhotoDataSetIndex.AlbumList].Data 
as AlbumListRecord;
+            albumList.RemoveAlbum (album.Record);
+
+            albums.Remove (album);
+        }
+
+        public Photo CreatePhoto () {
+            ImageListRecord imageList = dfr[PhotoDataSetIndex.ImageList].Data 
as ImageListRecord;
+            ImageItemRecord item = new ImageItemRecord (device.IsBE);
+            item.Id = dfr.NextId++;
+            
+            imageList.AddItem (item);
+            
+            Photo photo = new Photo (item, this);
+            photos[photo.Id] = photo;
+
+            if (masterAlbum != null) {
+                masterAlbum.Add (photo);
+            }
+            
+            addedPhotos.Add (photo);
+
+            return photo;
+        }
+
+        public void RemovePhoto (Photo photo) {
+            photos.Remove (photo.Id);
+            trackPhotos.Remove (photo.Record.TrackId);
+            addedPhotos.Remove (photo);
+            removedPhotos.Add (photo);
+
+            foreach (Album album in albums) {
+                album.Remove (photo);
+            }
+
+            if (masterAlbum != null) {
+                masterAlbum.Remove (photo);
+            }
+            
+            ImageListRecord imageList = dfr[PhotoDataSetIndex.ImageList].Data 
as ImageListRecord;
+            imageList.RemoveItem (photo.Record);
+        }
+
+        internal int GetThumbSize (int correlationId) {
+            FileListRecord fileList = dfr[PhotoDataSetIndex.FileList].Data as 
FileListRecord;
+            FileRecord file = fileList.LookupFile (correlationId);
+            if (file == null)
+                return -1;
+
+            return file.ImageSize;
+        }
+
+        internal Photo LookupPhotoById (int id) {
+            try {
+                return photos[id];
+            } catch (KeyNotFoundException e) {
+                return null;
+            }
+        }
+
+        internal Photo LookupPhotoByTrackId (long id) {
+            try {
+                return trackPhotos[id];
+            } catch (KeyNotFoundException e) {
+                return null;
+            }
+        }
+
+        internal string GetFilesystemPath (string podpath) {
+            return String.Format ("{0}/Photos{1}", device.MountPoint, 
podpath.Replace(':', '/'));
+        }
+
+        internal string GetThumbPath (ArtworkFormat format) {
+            if (isPhoto) {
+                return String.Format ("{0}/Photos/Thumbs/F{1}_1.ithmb", 
device.MountPoint, format.CorrelationId);
+            } else {
+                return String.Format ("{0}/Artwork/F{1}_1.ithmb", 
device.ControlPath, format.CorrelationId);
+            }
+
+        }
+
+        private void FindThumbnails (IList<ImageItemRecord> items, 
List<ImageNameRecord> existingNames,
+                                     List<ImageNameRecord> newNames, 
List<ImageNameRecord> removedNames,
+                                     ArtworkFormat format) {
+            foreach (ImageItemRecord item in items) {
+
+                if (existingNames != null) {
+                    foreach (ImageNameRecord name in item.Names) {
+                        if (name.CorrelationId == format.CorrelationId) {
+                            existingNames.Add (name);
+                        }
+                    }
+                }
+
+                if (newNames != null) {
+                    foreach (ImageNameRecord name in item.NewNames) {
+                        if (name.CorrelationId == format.CorrelationId) {
+                            newNames.Add (name);
+                        }
+                    }
+                }
+
+                if (removedNames != null) {
+                    foreach (ImageNameRecord name in item.RemovedNames) {
+                        if (name.CorrelationId == format.CorrelationId) {
+                            removedNames.Add (name);
+                        }
+                    }
+                }
+            }
+        }
+
+        private void SaveThumbnails () {
+            foreach (ArtworkFormat format in device.ArtworkFormats) {
+                List<ImageNameRecord> existingNames = new 
List<ImageNameRecord> ();
+                List<ImageNameRecord> removedNames = new List<ImageNameRecord> 
();
+                List<ImageNameRecord> newNames = new List<ImageNameRecord> ();
+
+                ImageListRecord imageList = 
dfr[PhotoDataSetIndex.ImageList].Data as ImageListRecord;
+
+                FindThumbnails (imageList.Items, existingNames, newNames, 
removedNames, format);
+                existingNames.Sort (new ImageNameRecordSorter ());
+                
+                FindThumbnails (imageList.RemovedItems, removedNames, null, 
removedNames, format);
+
+                if (existingNames.Count == 0 && newNames.Count == 0 && 
removedNames.Count == 0) {
+                    continue;
+                }
+
+                SaveThumbnails (existingNames, newNames, removedNames, format);
+            }
+        }
+
+        private ImageNameRecord Pop (List<ImageNameRecord> list) {
+            ImageNameRecord record = Peek (list);
+            if (record != null)
+                list.Remove (record);
+
+            return record;
+        }
+
+        private ImageNameRecord Peek (List<ImageNameRecord> list) {
+            if (list == null || list.Count == 0)
+                return null;
+
+            return list[list.Count - 1];
+        }
+
+        private byte[] GetNameData (ImageNameRecord record, Stream stream) {
+            if (record.Dirty) {
+                return record.GetData (GetTempFile ());
+            } else {
+                return record.GetData (stream);
+            }
+        }
+
+        private void SaveThumbnails (List<ImageNameRecord> existingNames, 
List<ImageNameRecord> newNames,
+                                     List<ImageNameRecord> removedNames, 
ArtworkFormat format) {
+            string thumbPath = GetThumbPath (format);
+
+            int fileLength = 0;
+            if (existingNames.Count > 0) {
+                ImageNameRecord last = Peek (existingNames);
+                fileLength = last.ThumbnailOffset + last.ImageSize;
+            }
+
+            string thumbDir = Path.GetDirectoryName (thumbPath);
+            if (!Directory.Exists (thumbDir)) {
+                Directory.CreateDirectory (thumbDir);
+            }
+
+            using (FileStream stream = File.Open (thumbPath, 
FileMode.OpenOrCreate)) {
+                // process the removals, filling the gaps with new or existing 
records when possible
+                foreach (ImageNameRecord removal in removedNames) {
+
+                    // try to replace it with a new one
+                    ImageNameRecord replacement = Pop (newNames);
+                    
+                    if (replacement == null) {
+
+                        // no new ones, try to replace it with an existing one 
from the end of the file
+                        ImageNameRecord record = Peek (existingNames);
+                        if (record != null && record.ThumbnailOffset > 
removal.ThumbnailOffset) {
+                            replacement = record;
+
+                            // it was the last one in the file, so we can 
reduce the file length
+                            fileLength -= replacement.ImageSize;
+                        }
+                    }
+
+                    if (replacement != null) {
+                        // write the replacement chunk into the old one's spot
+                        byte[] data = GetNameData (replacement, stream);
+                        replacement.ThumbnailOffset = removal.ThumbnailOffset;
+                        replacement.SetData (stream, data);
+                        replacement.Dirty = false;
+
+                        existingNames.Sort (new ImageNameRecordSorter ());
+                    } else if (fileLength > removal.ThumbnailOffset) {
+                        // we can't fill the gap, but it's ok to reduce the 
file length
+                        fileLength = removal.ThumbnailOffset;
+                    }
+                }
+
+                // process the additions by appending them to the file
+                foreach (ImageNameRecord addition in newNames) {
+                    byte[] data = GetNameData (addition, stream);
+                    addition.ThumbnailOffset = fileLength;
+                    addition.SetData (stream, data);
+                    addition.Dirty = false;
+                    
+                    fileLength += addition.ImageSize;
+                }
+
+                // lastly, flush any dirty existing records
+                foreach (ImageNameRecord existing in existingNames) {
+                    if (existing.Dirty) {
+                        byte[] data = GetNameData (existing, stream);
+                        existing.SetData (stream, data);
+                    }
+                }
+            }
+
+            Syscall.truncate (thumbPath, (long) fileLength);
+        }
+
+        internal Stream GetTempFile () {
+            if (tempStream == null) {
+                string path = Path.GetTempFileName ();
+                tempStream = File.Open (path, FileMode.Create);
+                File.Delete (path);
+            }
+
+            return tempStream;
+        }
+
+        private void CloseTempFile () {
+            if (tempStream != null) {
+                tempStream.Close ();
+                tempStream = null;
+            }
+        }
     }
-
 }

Modified: trunk/ipod-sharp/src/Thumbnail.cs
===================================================================
--- trunk/ipod-sharp/src/Thumbnail.cs   2006-06-21 23:31:49 UTC (rev 61923)
+++ trunk/ipod-sharp/src/Thumbnail.cs   2006-06-22 00:03:12 UTC (rev 61924)
@@ -3,22 +3,24 @@
 
 namespace IPod {
 
+    /*
     public enum ThumbnailFormat {
         Rgb565,
         IYUV,
         Rgb565BE,
         Rgb565BE90
     }
+    */
 
     public class Thumbnail {
 
-        private Image image;
+        private Photo photo;
         private ImageNameRecord record;
 
         private ArtworkFormat format = null;
 
-        public Image Image {
-            get { return image; }
+        public Photo Photo {
+            get { return photo; }
         }
 
         public int Size {
@@ -48,39 +50,57 @@
 
         public ArtworkFormat Format {
             get {
-                if (format == null) {
-                    foreach (ArtworkFormat f in image.Device.ArtworkFormats) {
-                        if (f.CorrelationId == record.CorrelationID) {
-                            format = f;
-                            break;
-                        }
-                    }
-                }
-
                 return format;
+            } set {
+                if (value == null)
+                    throw new ArgumentNullException ("Format cannot be null");
+                
+                format = value;
+                record.CorrelationId = format.CorrelationId;
+                record.SetThumbFileName (photo.PhotoDatabase.IsPhotoDatabase);
             }
         }
+
+        internal ImageNameRecord Record {
+            get { return record; }
+        }
         
-        internal Thumbnail (Image image, ImageNameRecord record) {
-            this.image = image;
+        internal Thumbnail (Photo photo, ImageNameRecord record) {
+            this.photo = photo;
             this.record = record;
-        }
 
-        private string GetThumbPath (string file) {
-            return String.Format ("{0}/Photos{1}", image.Device.MountPoint,
-                                  file.Replace (":", "/"));
+            if (record.CorrelationId > 0) {
+                Format = photo.PhotoDatabase.Device.LookupFormat 
(record.CorrelationId);
+            }
         }
 
         public byte[] GetData () {
-            string file = GetThumbPath (record.FileName);
-
-            using (FileStream stream = File.Open (file, FileMode.Open, 
FileAccess.Read, FileShare.Read)) {
-                stream.Seek ((long) record.ThumbnailOffset, SeekOrigin.Begin);
-
-                byte[] buf = new byte[record.ImageSize];
-                stream.Read (buf, 0, record.ImageSize);
-                return buf;
+            if (record.Dirty) {
+                return record.GetData (photo.PhotoDatabase.GetTempFile ());
+            } else {
+                string file = photo.PhotoDatabase.GetThumbPath (Format);
+                
+                using (FileStream stream = File.Open (file, FileMode.Open, 
FileAccess.Read, FileShare.Read)) {
+                    return record.GetData (stream);
+                }
             }
         }
+
+        public void SetData (byte[] data) {
+            int expectedLength = photo.PhotoDatabase.GetThumbSize 
(format.CorrelationId);
+            if (expectedLength > 0 && data.Length != expectedLength)
+                throw new ArgumentException (String.Format ("Expected data 
length of {0}, but got {1}",
+                                                            expectedLength, 
data.Length));
+            
+            Stream stream = photo.PhotoDatabase.GetTempFile ();
+            stream.Seek (0, SeekOrigin.End);
+            
+            record.ThumbnailOffset = (int) stream.Position;
+            record.ImageSize = data.Length;
+            
+            stream.Write (data, 0, data.Length);
+            
+            record.Dirty = true;
+        }
     }
 }

Modified: trunk/ipod-sharp/src/Track.cs
===================================================================
--- trunk/ipod-sharp/src/Track.cs       2006-06-21 23:31:49 UTC (rev 61923)
+++ trunk/ipod-sharp/src/Track.cs       2006-06-22 00:03:12 UTC (rev 61924)
@@ -33,12 +33,16 @@
         private TrackRecord record;
         private TrackDatabase db;
         private int latestPlayCount;
+        private Uri uri;
+        private Photo coverPhoto;
 
         internal int playCount;
         internal DateTime lastPlayed;
 
-        private Uri uri;
-        
+        internal TrackRecord Record {
+            get { return record; }
+        }
+
         public int Id {
             get { return record.Id; }
         }
@@ -268,7 +272,60 @@
             return Id.GetHashCode ();
         }
 
+        private void FindCoverPhoto () {
+            if (coverPhoto == null) {
+                coverPhoto = db.ArtworkDatabase.LookupPhotoByTrackId 
(record.DatabaseId);
+            }
+        }
 
+        public byte[] GetCoverArt (ArtworkFormat format) {
+            FindCoverPhoto ();
+
+            if (coverPhoto == null)
+                return null;
+            
+            foreach (Thumbnail thumbnail in coverPhoto.Thumbnails) {
+                if (thumbnail.Format == format) {
+                    return thumbnail.GetData ();
+                }
+            }
+            
+            return null;
+        }
+
+        public void SetCoverArt (ArtworkFormat format, byte[] data) {
+            FindCoverPhoto ();
+            
+            if (coverPhoto == null) {
+                coverPhoto = db.ArtworkDatabase.CreatePhoto ();
+                coverPhoto.Record.TrackId = record.DatabaseId;
+            }
+
+            Thumbnail thumbnail = coverPhoto.LookupThumbnail (format);
+            if (thumbnail == null) {
+                thumbnail = coverPhoto.CreateThumbnail ();
+                thumbnail.Format = format;
+                thumbnail.Width = format.Width;
+                thumbnail.Height = format.Height;
+            }
+
+            thumbnail.SetData (data);
+            record.HasArtwork = true;
+            record.ArtworkCount = 1; // this is actually for artwork in mp3 
tags, but it seems to be needed anyway
+
+            //record.ArtworkSize = 21741;
+            //record.ArtworkSize = 0;
+            //coverPhoto.Record.SourceImageSize = record.ArtworkSize;
+            /*
+            record.ArtworkSize = 0;
+            foreach (Thumbnail t in coverPhoto.Thumbnails) {
+                record.ArtworkSize += (t.Size + 1);
+            }
+
+            coverPhoto.Record.SourceImageSize = record.ArtworkSize;
+            */
+        }
+
         private static Uri PathToFileUri (string path) {
             if (path == null)
                 return null;

Modified: trunk/ipod-sharp/src/TrackDatabase.cs
===================================================================
--- trunk/ipod-sharp/src/TrackDatabase.cs       2006-06-21 23:31:49 UTC (rev 
61923)
+++ trunk/ipod-sharp/src/TrackDatabase.cs       2006-06-22 00:03:12 UTC (rev 
61924)
@@ -815,7 +815,7 @@
     internal class TrackRecord : TrackDbRecord {
 
         private short unknownThree = 0;
-        private short unknownFour;
+        private short unknownFour = 1;
         private int unknownFive;
         private int unknownSix = 0x472c4400;
         private short unknownSeven;
@@ -1458,7 +1458,7 @@
         }
     }
 
-    public class SaveProgressArgs : EventArgs {
+    public class TrackSaveProgressArgs : EventArgs {
 
         private Track track;
         private double trackPercent;
@@ -1493,7 +1493,7 @@
             get { return total; }
         }
 
-        public SaveProgressArgs (Track track, double trackPercent, int 
completed, int total) {
+        public TrackSaveProgressArgs (Track track, double trackPercent, int 
completed, int total) {
             this.track = track;
             this.trackPercent = trackPercent;
             this.completed = completed;
@@ -1502,7 +1502,7 @@
     }
 
     public delegate void PlaylistHandler (object o, Playlist playlist);
-    public delegate void SaveProgressHandler (object o, SaveProgressArgs args);
+    public delegate void TrackSaveProgressHandler (object o, 
TrackSaveProgressArgs args);
 
     public class TrackDatabase {
 
@@ -1524,7 +1524,7 @@
         private PhotoDatabase artdb;
 
         public event EventHandler SaveStarted;
-        public event SaveProgressHandler SaveProgressChanged;
+        public event TrackSaveProgressHandler SaveProgressChanged;
         public event EventHandler SaveEnded;
 
         public event PlaylistHandler PlaylistAdded;
@@ -1584,6 +1584,10 @@
             get { return dbrec.Version; }
         }
 
+        internal PhotoDatabase ArtworkDatabase {
+            get { return artdb; }
+        }
+
         internal TrackDatabase (Device device) : this (device, false) {
         }
         
@@ -1711,7 +1715,6 @@
 
             using (BinaryReader reader = new BinaryReader (File.Open 
(TrackDbPath, FileMode.Open, FileAccess.Read))) {
 
-                // FIXME
                 dbrec = new DatabaseRecord (useBE);
                 dbrec.Read (null, reader);
 
@@ -1816,8 +1819,8 @@
 
                     double percent = (double) count / (double) length;
                     if (percent >= lastPercent + PercentThreshold && 
SaveProgressChanged != null) {
-                        SaveProgressArgs args = new SaveProgressArgs (track, 
(double) count / (double) length,
-                                                                      
completed, total);
+                        TrackSaveProgressArgs args = new TrackSaveProgressArgs 
(track, (double) count / (double) length,
+                                                                               
 completed, total);
 
                         try {
                             SaveProgressChanged (this, args);
@@ -1899,7 +1902,7 @@
                 // Force progress to 100% so the app can now we're in the 
"sync()" phase
                 if (SaveProgressChanged != null) {
                     try {
-                        SaveProgressChanged (this, new SaveProgressArgs (null, 
1.0, 1, 1));
+                        SaveProgressChanged (this, new TrackSaveProgressArgs 
(null, 1.0, 1, 1));
                     } catch (Exception e) {
                         Console.Error.WriteLine ("Exception in progress 
handler: " + e);
                     }
@@ -1956,9 +1959,8 @@
         internal string GetUniquePodPath (string path) {
             if (path == null)
                 return null;
-            
+
             return String.Format (":{0}:Music:{1}", ControlDirectoryName, 
MakeUniquePodTrackPath(path));
-
         }
 
         private int GetNextTrackId () {
@@ -2000,6 +2002,12 @@
                 // remove from the track db
                 dbrec[DataSetIndex.Library].TrackList.Remove (track.Id);
                 dbrec[DataSetIndex.Playlist].Library.RemoveTrack 
(track.TrackRecord.Id);
+
+                // remove from cover art db
+                Photo artPhoto = artdb.LookupPhotoByTrackId 
(track.Record.DatabaseId);
+                if (artPhoto != null) {
+                    artdb.RemovePhoto (artPhoto);
+                }
                     
                 // remove from the "normal" playlists
                 foreach (Playlist list in playlists) {

Modified: trunk/ipod-sharp/ui/ProgressDialog.cs
===================================================================
--- trunk/ipod-sharp/ui/ProgressDialog.cs       2006-06-21 23:31:49 UTC (rev 
61923)
+++ trunk/ipod-sharp/ui/ProgressDialog.cs       2006-06-22 00:03:12 UTC (rev 
61924)
@@ -82,7 +82,7 @@
             notify.WakeupMain ();
         }
 
-        private void OnSaveProgressChanged (object o, SaveProgressArgs args) {
+        private void OnSaveProgressChanged (object o, TrackSaveProgressArgs 
args) {
             lock (this) {
                 if (args.CurrentTrack != null) {
                     string padstr = String.Format ("Adding {0} of {0}", 
args.TracksTotal);

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to