Author: jwilcox
Date: 2006-06-14 19:41:25 -0400 (Wed, 14 Jun 2006)
New Revision: 61723

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

        * Add initial photo support.  Currently read-only, and still a
        * bit raw.
        * switch to gmcs (C# 2.0), and start using generic collections
        * configure.ac: bump to 0.6.0



Modified: trunk/ipod-sharp/ChangeLog
===================================================================
--- trunk/ipod-sharp/ChangeLog  2006-06-14 22:24:01 UTC (rev 61722)
+++ trunk/ipod-sharp/ChangeLog  2006-06-14 23:41:25 UTC (rev 61723)
@@ -1,3 +1,9 @@
+2006-06-14  James Willcox  <[EMAIL PROTECTED]>
+
+       * Add initial photo support.  Currently read-only, and still a bit raw.
+       * switch to gmcs (C# 2.0), and start using generic collections
+       * configure.ac: bump to 0.6.0
+
 2006-06-10  Aaron Bockover  <[EMAIL PROTECTED]>
 
        * ipod-sharp.mdp: Updated MonoDevelop project

Modified: trunk/ipod-sharp/configure.ac
===================================================================
--- trunk/ipod-sharp/configure.ac       2006-06-14 22:24:01 UTC (rev 61722)
+++ trunk/ipod-sharp/configure.ac       2006-06-14 23:41:25 UTC (rev 61723)
@@ -2,8 +2,8 @@
 AC_CANONICAL_SYSTEM
 
 MAJOR_VERSION=0
-MINOR_VERSION=5
-MICRO_VERSION=16
+MINOR_VERSION=6
+MICRO_VERSION=0
 VERSION=$MAJOR_VERSION.$MINOR_VERSION.$MICRO_VERSION
                                                                                
 
 AM_INIT_AUTOMAKE(ipod-sharp, $VERSION)
@@ -26,10 +26,10 @@
    AC_MSG_ERROR([Can not find "mono" in your PATH])
 fi
 
-dnl mcs           
-AC_PATH_PROG(MCS, mcs)
+dnl gmcs          
+AC_PATH_PROG(MCS, gmcs)
 if test "x$MCS" = "x" ; then
-   AC_MSG_ERROR([Can not find "mcs" in your PATH])
+   AC_MSG_ERROR([Can not find "gmcs" in your PATH])
 fi
 
 AC_ARG_ENABLE(docs, [  --disable-docs    Do not build documentation],

Added: trunk/ipod-sharp/src/Album.cs
===================================================================
--- trunk/ipod-sharp/src/Album.cs       2006-06-14 22:24:01 UTC (rev 61722)
+++ trunk/ipod-sharp/src/Album.cs       2006-06-14 23:41:25 UTC (rev 61723)
@@ -0,0 +1,45 @@
+using System;
+using System.Collections.ObjectModel;
+using System.Collections.Generic;
+
+namespace IPod {
+
+    public class Album {
+
+        private AlbumRecord record;
+        private PhotoDatabase db;
+
+        private List<Image> images;
+
+        public ReadOnlyCollection<Image> Images {
+            get {
+                return new ReadOnlyCollection<Image> (images);
+            }
+        }
+
+        public string Name {
+            get { return record.AlbumName; }
+            set { record.AlbumName = value; }
+        }
+
+        internal bool IsMaster {
+            get { return record.IsMaster; }
+        }
+
+        internal AlbumRecord Record {
+            get { return record; }
+        }
+        
+        internal Album (AlbumRecord record, PhotoDatabase db) {
+            this.record = record;
+            this.db = db;
+
+            images = new List<Image> ();
+
+            foreach (AlbumItemRecord item in record.Items) {
+                Image img = db.LookupImageById (item.ImageId);
+                images.Add (img);
+            }
+        }
+    }
+}

Modified: trunk/ipod-sharp/src/Device.cs
===================================================================
--- trunk/ipod-sharp/src/Device.cs      2006-06-14 22:24:01 UTC (rev 61722)
+++ trunk/ipod-sharp/src/Device.cs      2006-06-14 23:41:25 UTC (rev 61723)
@@ -40,7 +40,7 @@
             get { return height; }
         }
 
-        public short CorrelationId {
+        internal short CorrelationId {
             get { return correlationId; }
         }
         
@@ -80,8 +80,8 @@
         
         private ArrayList equalizers;
         private EqualizerContainerRecord eqsrec;
-        private TrackDatabase songs;
-        private string controlPath;
+        private TrackDatabase tracks;
+        private PhotoDatabase photos;
 
         public event EventHandler Changed;
 
@@ -279,14 +279,24 @@
 
         public TrackDatabase TrackDatabase {
             get {
-                if(songs == null) {
+                if (tracks == null) {
                     LoadTrackDatabase ();
                 }
 
-                return songs;
+                return tracks;
             }
         }
 
+        public PhotoDatabase PhotoDatabase {
+            get {
+                if (photos == null) {
+                    LoadPhotoDatabase ();
+                }
+
+                return photos;
+            }
+        }
+
         public static new GLib.GType GType { 
             get {
                 IntPtr raw_ret = ipod_device_get_type();
@@ -307,6 +317,17 @@
 
         public Device (string mountOrDevice) : this (ipod_device_new 
(mountOrDevice)) {
         }
+
+        public void LoadPhotoDatabase () {
+            LoadPhotoDatabase (false);
+        }
+
+        public void LoadPhotoDatabase (bool createFresh) {
+            //FIXME: refuse if the device lacks photo capability
+
+            if (photos == null)
+                photos = new PhotoDatabase (this, createFresh);
+        }
         
         public void LoadTrackDatabase () {
             LoadTrackDatabase (false);
@@ -317,13 +338,13 @@
                 throw new DeviceException (this, "Cannot get song database, as 
this device is not an iPod");
             }
 
-            if (songs == null)
-                songs = new TrackDatabase (this, createFresh);
+            if (tracks == null)
+                tracks = new TrackDatabase (this, createFresh);
         }
         
         public void CreateEmptyTrackDatabase () {
-            songs = null;
-            LoadTrackDatabase(true);
+            tracks = null;
+            LoadTrackDatabase (true);
         }
 
         private void EmitChanged () {

Added: trunk/ipod-sharp/src/Image.cs
===================================================================
--- trunk/ipod-sharp/src/Image.cs       2006-06-14 22:24:01 UTC (rev 61722)
+++ trunk/ipod-sharp/src/Image.cs       2006-06-14 23:41:25 UTC (rev 61723)
@@ -0,0 +1,51 @@
+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-14 22:24:01 UTC (rev 61722)
+++ trunk/ipod-sharp/src/Makefile.am    2006-06-14 23:41:25 UTC (rev 61723)
@@ -11,6 +11,7 @@
 ipod_DATA = $(ASSEMBLY)
 
 ASSEMBLY_SOURCES =                     \
+       $(srcdir)/Album.cs      \
        $(srcdir)/AssemblyInfo.cs       \
        $(srcdir)/DatabaseReadException.cs      \
        $(srcdir)/DatabaseWriteException.cs     \
@@ -24,8 +25,11 @@
        $(srcdir)/DeviceBusyException.cs        \
        $(srcdir)/EndianBinaryWriter.cs         \
        $(srcdir)/Equalizer.cs          \
+       $(srcdir)/Image.cs      \
        $(srcdir)/Initializer.cs        \
+       $(srcdir)/PhotoDatabase.cs              \
        $(srcdir)/Playlist.cs           \
+       $(srcdir)/Thumbnail.cs          \
        $(srcdir)/Track.cs              \
        $(srcdir)/TrackDatabase.cs \
        $(srcdir)/Utility.cs \

Added: trunk/ipod-sharp/src/PhotoDatabase.cs
===================================================================
--- trunk/ipod-sharp/src/PhotoDatabase.cs       2006-06-14 22:24:01 UTC (rev 
61722)
+++ trunk/ipod-sharp/src/PhotoDatabase.cs       2006-06-14 23:41:25 UTC (rev 
61723)
@@ -0,0 +1,670 @@
+using System;
+using System.Text;
+using System.Collections;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.IO;
+
+namespace IPod {
+
+    internal abstract class PhotoDbRecord : Record {
+
+        public PhotoDbRecord (bool isbe) : base (isbe) {
+        }
+        
+        public virtual void Read (BinaryReader reader) {
+            ReadHeader (reader);
+        }
+
+        public abstract void Save (BinaryWriter writer);
+
+        protected void SaveChild (PhotoDbRecord record, out byte[] data, out 
int length) {
+            MemoryStream stream = new MemoryStream ();
+            BinaryWriter writer = new EndianBinaryWriter (stream, IsBE);
+            record.Save (writer);
+            writer.Flush ();
+            length = (int) stream.Length;
+            data = stream.GetBuffer ();
+            writer.Close ();
+        }
+                    
+    }
+
+    internal class DataFileRecord : PhotoDbRecord {
+
+        private int unknownOne;
+        private int unknownTwo;
+        private int unknownThree;
+        private int unknownFour;
+        private long unknownFive;
+        private long unknownSix;
+        private int unknownSeven = 2;
+        private int unknownEight;
+        private int unknownNine;
+        private int unknownTen;
+        private int unknownEleven;
+
+        private List<PhotoDataSetRecord> children = new 
List<PhotoDataSetRecord> ();
+
+        public int NextId;
+
+        public PhotoDataSetRecord this [PhotoDataSetIndex index] {
+            get {
+                foreach (PhotoDataSetRecord ds in children) {
+                    if (ds.Index == index) {
+                        return ds;
+                    }
+                }
+
+                return null;
+            }
+        }
+
+        public DataFileRecord (bool isbe) : base (isbe) {
+            this.Name = "mhfd";
+        }
+        
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            byte[] body = reader.ReadBytes (this.HeaderOne - 12);
+
+            unknownOne = ToInt32 (body, 0);
+            unknownTwo = ToInt32 (body, 4);
+            int numChildren = ToInt32 (body, 8);
+            unknownThree = ToInt32 (body, 12);
+            NextId = ToInt32 (body, 16);
+            unknownFive = ToInt64 (body, 20);
+            unknownSix = ToInt64 (body, 28);
+            unknownSeven = ToInt32 (body, 36);
+            unknownEight = ToInt32 (body, 40);
+            unknownNine = ToInt32 (body, 44);
+            unknownTen = ToInt32 (body, 48);
+            unknownEleven = ToInt32 (body, 52);
+
+            for (int i = 0; i < numChildren; i++) {
+                PhotoDataSetRecord ds = new PhotoDataSetRecord (IsBE);
+                ds.Read (reader);
+                children.Add (ds);
+            }
+        }
+
+        public override void Save (BinaryWriter writer) {
+            /*
+            WriteName (writer);
+            writer.Write (this.HeaderOne);
+            writer.Write (this.HeaderTwo);
+            writer.Write (unknownOne);
+            writer.Write (unknownTwo);
+            writer.Write (numChildren);
+            writer.Write (
+            */
+        }
+    }
+
+    internal enum PhotoDataSetIndex {
+        ImageList = 1,
+        AlbumList = 2,
+        FileList = 3
+    }
+
+    internal class PhotoDataSetRecord : PhotoDbRecord {
+
+        public PhotoDataSetIndex Index;
+
+        public PhotoDbRecord Data;
+
+        public PhotoDataSetRecord (bool isbe) : base (isbe) {
+            this.Name = "mhsd";
+        }
+        
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            byte[] body = reader.ReadBytes (this.HeaderOne - 12);
+            Index = (PhotoDataSetIndex) ToInt32 (body, 0);
+
+            switch (Index) {
+            case PhotoDataSetIndex.ImageList:
+                Data = new ImageListRecord (IsBE);
+                Data.Read (reader);
+                break;
+            case PhotoDataSetIndex.AlbumList:
+                Data = new AlbumListRecord (IsBE);
+                Data.Read (reader);
+                break;
+            case PhotoDataSetIndex.FileList:
+                Data = new FileListRecord (IsBE);
+                Data.Read (reader);
+                break;
+            default:
+                break;
+            }
+        }
+
+        public override void Save (BinaryWriter writer) {
+
+        }
+    }
+
+    internal class AlbumListRecord : PhotoDbRecord {
+
+        private List<AlbumRecord> albums = new List<AlbumRecord> ();
+
+        public ReadOnlyCollection<AlbumRecord> Albums {
+            get {
+                return new ReadOnlyCollection<AlbumRecord> (albums);
+            }
+        }
+
+        public void AddAlbum (AlbumRecord album) {
+            albums.Add (album);
+        }
+
+        public void RemoveAlbum (AlbumRecord album) {
+            albums.Remove (album);
+        }
+        
+        public AlbumListRecord (bool isbe) : base (isbe) {
+            this.Name = "mhla";
+        }
+        
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            if (HeaderOne - 12 > 0) {
+                reader.ReadBytes (HeaderOne - 12);
+            }
+
+            albums.Clear ();
+            
+            int numChildren = HeaderTwo;
+            for (int i = 0; i < numChildren; i++) {
+                AlbumRecord record = new AlbumRecord (IsBE);
+                record.Read (reader);
+                albums.Add (record);
+            }
+        }
+
+        public override void Save (BinaryWriter writer) {
+
+        }
+    }
+
+    internal class AlbumRecord : PhotoDbRecord {
+
+        private int unknownOne;
+        private short unknownTwo;
+        private int unknownThree;
+        private int unknownFour;
+
+        private PhotoDetailRecord nameRecord;
+
+        private List<AlbumItemRecord> items = new List<AlbumItemRecord> ();
+        
+        public int PlaylistId;
+        public bool IsMaster;
+        public bool PlayMusic;
+        public bool Repeat;
+        public bool Random;
+        public bool ShowTitles;
+        public byte TransitionDirection;
+        public int SlideDuration;
+        public int TransitionDuration;
+        public long TrackId;
+        public int PreviousPlaylistId;
+
+        public ReadOnlyCollection<AlbumItemRecord> Items {
+            get {
+                return new ReadOnlyCollection<AlbumItemRecord> (items);
+            }
+        }
+
+        public string AlbumName {
+            get { return nameRecord.Value; }
+            set { nameRecord.Value = value; }
+        }
+
+        public AlbumRecord (bool isbe) : base (isbe) {
+            this.Name = "mhba";
+            nameRecord = new PhotoDetailRecord (IsBE);
+        }
+
+        public void AddItem (AlbumItemRecord item) {
+            items.Add (item);
+        }
+
+        public void RemoveItem (AlbumItemRecord item) {
+            items.Remove (item);
+        }
+        
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            byte[] body = reader.ReadBytes (HeaderOne - 12);
+
+            int numDataObjects = ToInt32 (body, 0);
+            int numAlbums = ToInt32 (body, 4);
+            PlaylistId = ToInt32 (body, 8);
+            unknownOne = ToInt32 (body, 12);
+            unknownTwo = ToInt16 (body, 16);
+            IsMaster = body[18] == (byte) 1;
+            PlayMusic = body[19] == (byte) 1;
+            Repeat = body[20] == (byte) 1;
+            Random = body[21] == (byte) 1;
+            ShowTitles = body[22] == (byte) 1;
+            TransitionDirection = body[23];
+            SlideDuration = ToInt32 (body, 24);
+            TransitionDuration = ToInt32 (body, 28);
+            unknownThree = ToInt32 (body, 32);
+            unknownFour = ToInt32 (body, 36);
+            TrackId = ToInt64 (body, 40);
+            PreviousPlaylistId = ToInt32 (body, 48);
+
+            for (int i = 0; i < numDataObjects; i++) {
+                PhotoDetailRecord detail = new PhotoDetailRecord (IsBE);
+                detail.Read (reader);
+
+                if (i == 0) {
+                    nameRecord = detail;
+                }
+            }
+
+            for (int i = 0; i < numAlbums; i++) {
+                AlbumItemRecord item = new AlbumItemRecord (IsBE);
+                item.Read (reader);
+                items.Add (item);
+            }
+        }
+
+        public override void Save (BinaryWriter writer) {
+
+        }
+    }
+
+    internal class AlbumItemRecord : PhotoDbRecord {
+
+        private int unknownOne;
+
+        public int ImageId;
+
+        public AlbumItemRecord (bool isbe) : base (isbe) {
+            this.Name = "mhia";
+        }
+        
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            byte[] body = reader.ReadBytes (HeaderOne - 12);
+
+            unknownOne = ToInt32 (body, 0);
+            ImageId = ToInt32 (body, 4);
+        }
+
+        public override void Save (BinaryWriter writer) {
+            
+        }
+    }
+
+
+    internal class ImageListRecord : PhotoDbRecord {
+
+        private List<ImageItemRecord> items = new List<ImageItemRecord> ();
+
+        public ReadOnlyCollection<ImageItemRecord> Items {
+            get {
+                return new ReadOnlyCollection<ImageItemRecord> (items);
+            }
+        }
+
+        public ImageListRecord (bool isbe) : base (isbe) {
+            this.Name = "mhli";
+        }
+
+        public void AddItem (ImageItemRecord item) {
+            items[item.Id] = item;
+        }
+
+        public void RemoveItem (ImageItemRecord item) {
+            items.Remove (item);
+        }
+
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            byte[] body = reader.ReadBytes (this.HeaderOne - 12);
+
+            for (int i = 0; i < this.HeaderTwo; i++) {
+                ImageItemRecord item = new ImageItemRecord (IsBE);
+                item.Read (reader);
+
+                items.Add (item);
+            }
+        }
+
+        public override void Save (BinaryWriter writer) {
+
+        }
+    }
+
+    internal enum PhotoDetailType {
+        String = 1,
+        ThumbnailContainer = 2,
+        FileName = 3,
+        ImageContainer = 5
+    }
+
+    internal class PhotoDetailRecord : PhotoDbRecord {
+
+        public PhotoDetailType Type;
+
+        public ImageNameRecord ImageName;
+        public string Value;
+
+        public PhotoDetailRecord (bool isbe) : base (isbe) {
+            this.Name = "mhod";
+        }
+
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            byte[] body = reader.ReadBytes (this.HeaderOne - 12);
+            Type = (PhotoDetailType) ToInt16 (body, 0);
+
+            switch (Type) {
+            case PhotoDetailType.ThumbnailContainer:
+                ImageName = new ImageNameRecord (IsBE);
+                ImageName.Read (reader);
+                break;
+            default:
+                ReadString (reader, Type == PhotoDetailType.FileName);
+                break;
+            }
+        }
+
+        private void ReadString (BinaryReader reader, bool utf16) {
+            byte[] stringData = reader.ReadBytes (12);
+            int len = ToInt32 (stringData, 0);
+
+            if (utf16) {
+                Value = Encoding.Unicode.GetString (reader.ReadBytes (len));
+            } else {
+                Value = Encoding.UTF8.GetString (reader.ReadBytes (len));
+            }
+            
+            int padding = HeaderTwo - len - HeaderOne - 12;
+            
+            if (padding > 0) {
+                reader.ReadBytes (padding);
+            }
+        }
+
+        public override void Save (BinaryWriter writer) {
+
+        }
+    }
+
+    internal class ImageNameRecord : PhotoDbRecord {
+
+        private PhotoDetailRecord fileDetail;
+        
+        public int CorrelationID;
+        public int ThumbnailOffset;
+        public int ImageSize;
+        public short VerticalPadding;
+        public short HorizontalPadding;
+        public short ImageHeight;
+        public short ImageWidth;
+
+        public ThumbnailFormat Format;
+
+        public string FileName {
+            get { return fileDetail.Value; }
+            set { fileDetail.Value = value; }
+        }
+
+        public ImageNameRecord (bool isbe) : base (isbe) {
+            this.Name = "mhni";
+
+            fileDetail = new PhotoDetailRecord (IsBE);
+        }
+
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            byte[] body = reader.ReadBytes (this.HeaderOne - 12);
+
+            int numChildren = ToInt32 (body, 0);
+            CorrelationID = ToInt32 (body, 4);
+            ThumbnailOffset = ToInt32 (body, 8);
+            ImageSize = ToInt32 (body, 12);
+            VerticalPadding = ToInt16 (body, 16);
+            HorizontalPadding = ToInt16 (body, 18);
+            ImageHeight = ToInt16 (body, 20);
+            ImageWidth = ToInt16 (body, 22);
+
+            for (int i = 0; i < numChildren; i++) {
+                PhotoDetailRecord detail = new PhotoDetailRecord (IsBE);
+                detail.Read (reader);
+
+                if (i == 0) {
+                    fileDetail = detail;
+                }
+            }
+
+            SetFormat ();
+        }
+
+        private void SetFormat () {
+            switch (CorrelationID) {
+            case 1009:
+            case 1015:
+            case 1013:
+            case 1036:
+                Format = ThumbnailFormat.Rgb565;
+                break;
+            case 1019:
+                Format = ThumbnailFormat.IYUV;
+                break;
+            case 1020:
+                Format = ThumbnailFormat.Rgb565BE;
+                break;
+            case 1024:
+                Format = ThumbnailFormat.Rgb565BE90;
+                break;
+            default:
+                throw new ApplicationException ("Unknown correltion id: " + 
CorrelationID);
+            }
+        }
+
+        public override void Save (BinaryWriter writer) {
+
+        }
+    }
+
+    internal class ImageItemRecord : PhotoDbRecord {
+
+        private int unknownOne;
+        private int unknownTwo;
+        private int sourceImageSize;
+
+        private List<ImageNameRecord> names = new List<ImageNameRecord> ();
+        
+        public int Id;
+        public long TrackId;
+        public int Rating;
+        public DateTime OriginalDate;
+        public DateTime DigitizedDate;
+
+        public ReadOnlyCollection<ImageNameRecord> Names {
+            get {
+                return new ReadOnlyCollection<ImageNameRecord> (names);
+            }
+        }
+        
+        public ImageItemRecord (bool isbe) : base (isbe) {
+            this.Name = "mhii";
+        }
+
+        public void AddName (ImageNameRecord name) {
+            names.Add (name);
+        }
+
+        public void RemoveName (ImageNameRecord name) {
+            names.Remove (name);
+        }
+
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            byte[] body = reader.ReadBytes (this.HeaderOne - 12);
+
+            int numChildren = ToInt32 (body, 0);
+            Id = ToInt32 (body, 4);
+            TrackId = ToInt64 (body, 8);
+            unknownOne = ToInt32 (body, 16);
+            Rating = ToInt32 (body, 20);
+            unknownTwo = ToInt32 (body, 24);
+            OriginalDate = Utility.MacTimeToDate (ToUInt32 (body, 28));
+            DigitizedDate = Utility.MacTimeToDate (ToUInt32 (body, 32));
+            sourceImageSize = ToInt32 (body, 36);
+
+            for (int i = 0; i < numChildren; i++) {
+                PhotoDetailRecord detail = new PhotoDetailRecord (IsBE);
+                detail.Read (reader);
+                names.Add (detail.ImageName);
+            }
+        }
+
+        public override void Save (BinaryWriter writer) {
+
+        }
+    }
+
+    internal class FileListRecord : PhotoDbRecord {
+
+        public FileListRecord (bool isbe) : base (isbe) {
+            this.Name = "mhlf";
+        }
+        
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            if (HeaderOne - 12 > 0) {
+                reader.ReadBytes (HeaderOne - 12);
+            }
+            
+            int numChildren = HeaderTwo;
+            for (int i = 0; i < numChildren; i++) {
+                FileRecord record = new FileRecord (IsBE);
+                record.Read (reader);
+            }
+        }
+
+        public override void Save (BinaryWriter writer) {
+
+        }
+    }
+
+    internal class FileRecord : PhotoDbRecord {
+
+        private int unknownOne;
+
+        public int CorrelationId;
+        public int ImageSize;
+
+        public FileRecord (bool isbe) : base (isbe) {
+            this.Name = "mhif";
+        }
+        
+        public override void Read (BinaryReader reader) {
+            base.Read (reader);
+
+            byte[] body = reader.ReadBytes (HeaderOne - 12);
+            
+            unknownOne = ToInt32 (body, 0);
+            CorrelationId = ToInt32 (body, 4);
+            ImageSize = ToInt32 (body, 8);
+        }
+
+        public override void Save (BinaryWriter writer) {
+
+        }
+    }
+
+    public class PhotoDatabase {
+
+        private Device device;
+        private Dictionary<int, Image> images = new Dictionary<int, Image> ();
+        private List<Album> albums = new List<Album> ();
+        private DataFileRecord dfr;
+
+        private Album masterAlbum;
+
+        private string PhotoDbPath {
+            get {
+                return device.MountPoint + "/Photos/Photo Database";
+            }
+        }
+
+        private string PhotoDbBackupPath {
+            get {
+                return PhotoDbPath + ".bak";
+            }
+        }
+
+        public ReadOnlyCollection<Album> Albums {
+            get { return new ReadOnlyCollection<Album> (albums); }
+        }
+
+        public ReadOnlyCollection<Image> Images {
+            get {
+                return new ReadOnlyCollection<Image> (new List<Image> 
(images.Values));
+            }
+        }
+        
+        internal PhotoDatabase (Device device) : this (device, false) {
+        }
+
+        internal PhotoDatabase (Device device, bool createFresh) {
+            this.device = device;
+
+            // FIXME: do something with createFresh
+
+            Reload ();
+        }
+
+        public void Reload () {
+            images.Clear ();
+            albums.Clear ();
+            
+            dfr = new DataFileRecord (false);
+
+            using (BinaryReader reader = new BinaryReader (File.Open 
(PhotoDbPath, FileMode.Open))) {
+                dfr.Read (reader);
+
+                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 (AlbumRecord albumRecord in (albumSet.Data as 
AlbumListRecord).Albums) {
+                    Album album = new Album (albumRecord, this);
+
+                    if (album.IsMaster) {
+                        masterAlbum = album;
+                    } else {
+                        albums.Add (album);
+                    }
+                }
+            }
+        }
+
+        internal Image LookupImageById (int id) {
+            return images[id];
+        }
+    }
+
+}

Added: trunk/ipod-sharp/src/Thumbnail.cs
===================================================================
--- trunk/ipod-sharp/src/Thumbnail.cs   2006-06-14 22:24:01 UTC (rev 61722)
+++ trunk/ipod-sharp/src/Thumbnail.cs   2006-06-14 23:41:25 UTC (rev 61723)
@@ -0,0 +1,86 @@
+using System;
+using System.IO;
+
+namespace IPod {
+
+    public enum ThumbnailFormat {
+        Rgb565,
+        IYUV,
+        Rgb565BE,
+        Rgb565BE90
+    }
+
+    public class Thumbnail {
+
+        private Image image;
+        private ImageNameRecord record;
+
+        private ArtworkFormat format = null;
+
+        public Image Image {
+            get { return image; }
+        }
+
+        public int Size {
+            get { return record.ImageSize; }
+            set { record.ImageSize = value; }
+        }
+
+        public short VerticalPadding {
+            get { return record.VerticalPadding; }
+            set { record.VerticalPadding = value; }
+        }
+
+        public short HorizontalPadding {
+            get { return record.HorizontalPadding; }
+            set { record.HorizontalPadding = value; }
+        }
+
+        public short Height {
+            get { return record.ImageHeight; }
+            set { record.ImageHeight = value; }
+        }
+
+        public short Width {
+            get { return record.ImageWidth; }
+            set { record.ImageWidth = value; }
+        }
+
+        public ArtworkFormat Format {
+            get {
+                if (format == null) {
+                    foreach (ArtworkFormat f in image.Device.ArtworkFormats) {
+                        if (f.CorrelationId == record.CorrelationID) {
+                            format = f;
+                            break;
+                        }
+                    }
+                }
+
+                return format;
+            }
+        }
+        
+        internal Thumbnail (Image image, ImageNameRecord record) {
+            this.image = image;
+            this.record = record;
+        }
+
+        private string GetThumbPath (string file) {
+            return String.Format ("{0}/Photos{1}", image.Device.MountPoint,
+                                  file.Replace (":", "/"));
+        }
+
+        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;
+            }
+        }
+    }
+}

Modified: trunk/ipod-sharp/src/TrackDatabase.cs
===================================================================
--- trunk/ipod-sharp/src/TrackDatabase.cs       2006-06-14 22:24:01 UTC (rev 
61722)
+++ trunk/ipod-sharp/src/TrackDatabase.cs       2006-06-14 23:41:25 UTC (rev 
61723)
@@ -19,27 +19,6 @@
             this.IsBE = isbe;
         }
 
-        public virtual void Read (DatabaseRecord db, BinaryReader reader) {
-            byte[] nameBytes = reader.ReadBytes (4);
-            if (IsBE)
-                nameBytes = Utility.Swap (nameBytes);
-            
-            string n = Encoding.ASCII.GetString (nameBytes);
-
-            if (this.Name != null && this.Name != n) {
-                throw new DatabaseReadException ("Expected record name of 
'{0}', got '{1}'", this.Name, n);
-            }
-
-            this.Name = n;
-            this.HeaderOne = reader.ReadInt32 ();
-            this.HeaderTwo = reader.ReadInt32 ();
-
-            if (IsBE) {
-                this.HeaderOne = Utility.Swap (this.HeaderOne);
-                this.HeaderTwo = Utility.Swap (this.HeaderTwo);
-            }
-        }
-
         protected void WriteName (BinaryWriter writer) {
             byte[] nameBytes = Encoding.ASCII.GetBytes (this.Name);
             if (IsBE)
@@ -84,9 +63,40 @@
             return Utility.MaybeSwap (val, IsBE);
         }
 
+        protected void ReadHeader (BinaryReader reader) {
+            byte[] nameBytes = reader.ReadBytes (4);
+            if (IsBE)
+                nameBytes = Utility.Swap (nameBytes);
+            
+            string n = Encoding.ASCII.GetString (nameBytes);
+
+            if (this.Name != null && this.Name != n) {
+                throw new DatabaseReadException ("Expected record name of 
'{0}', got '{1}'", this.Name, n);
+            }
+
+            this.Name = n;
+            this.HeaderOne = reader.ReadInt32 ();
+            this.HeaderTwo = reader.ReadInt32 ();
+
+            if (IsBE) {
+                this.HeaderOne = Utility.Swap (this.HeaderOne);
+                this.HeaderTwo = Utility.Swap (this.HeaderTwo);
+            }
+        }
+    }
+
+    internal abstract class TrackDbRecord : Record {
+
+        public TrackDbRecord (bool isbe) : base (isbe) {
+        }
+
+        public virtual void Read (DatabaseRecord db, BinaryReader reader) {
+            ReadHeader (reader);
+        }
+        
         public abstract void Save (DatabaseRecord db, BinaryWriter writer);
 
-        protected void SaveChild (DatabaseRecord db, Record record, out byte[] 
data, out int length) {
+        protected void SaveChild (DatabaseRecord db, TrackDbRecord record, out 
byte[] data, out int length) {
             MemoryStream stream = new MemoryStream ();
             BinaryWriter writer = new EndianBinaryWriter (stream, IsBE);
             record.Save (db, writer);
@@ -95,9 +105,10 @@
             data = stream.GetBuffer ();
             writer.Close ();
         }
+        
     }
 
-    internal class GenericRecord : Record {
+    internal class GenericRecord : TrackDbRecord {
 
         private byte[] data;
 
@@ -118,7 +129,7 @@
         }
     }
 
-    internal class PlaylistItemRecord : Record {
+    internal class PlaylistItemRecord : TrackDbRecord {
 
         private int unknownOne = 0;
         private int unknownTwo = 0;
@@ -235,7 +246,7 @@
         Description
     }
 
-    internal class PlaylistRecord : Record {
+    internal class PlaylistRecord : TrackDbRecord {
 
         private int unknownOne;
         //private int unknownTwo;
@@ -431,11 +442,11 @@
             MemoryStream stream = new MemoryStream ();
             BinaryWriter childWriter = new EndianBinaryWriter (stream, IsBE);
 
-            foreach (Record rec in stringDetails) {
+            foreach (TrackDbRecord rec in stringDetails) {
                 rec.Save (db, childWriter);
             }
 
-            foreach (Record rec in otherDetails) {
+            foreach (TrackDbRecord rec in otherDetails) {
                 rec.Save (db, childWriter);
             }
 
@@ -521,7 +532,7 @@
     }
         
 
-    internal class PlaylistListRecord : Record, IEnumerable {
+    internal class PlaylistListRecord : TrackDbRecord, IEnumerable {
 
         private ArrayList playlists = new ArrayList ();
 
@@ -631,7 +642,7 @@
         Composer = 18
     }
     
-    internal class DetailRecord : Record {
+    internal class DetailRecord : TrackDbRecord {
 
         private static UnicodeEncoding encoding = new UnicodeEncoding (false, 
false);
 
@@ -795,7 +806,7 @@
         AAC = 0x0
     }
 
-    internal class TrackRecord : Record {
+    internal class TrackRecord : TrackDbRecord {
 
         private short unknownThree = 0;
         private short unknownFour;
@@ -1142,7 +1153,7 @@
         }
     }
 
-    internal class TrackListRecord : Record, IEnumerable {
+    internal class TrackListRecord : TrackDbRecord, IEnumerable {
 
         private ArrayList tracks = new ArrayList ();
 
@@ -1234,7 +1245,7 @@
         PlaylistDuplicate = 3
     }
 
-    internal class DataSetRecord : Record {
+    internal class DataSetRecord : TrackDbRecord {
 
         public DataSetIndex Index;
 
@@ -1321,7 +1332,7 @@
         }
     }
 
-    internal class DatabaseRecord : Record {
+    internal class DatabaseRecord : TrackDbRecord {
 
         private const int MaxSupportedVersion = 17;
         private const int TrackIdStart = 1000;

Modified: trunk/ipod-sharp/ui/ProgressDialog.cs
===================================================================
--- trunk/ipod-sharp/ui/ProgressDialog.cs       2006-06-14 22:24:01 UTC (rev 
61722)
+++ trunk/ipod-sharp/ui/ProgressDialog.cs       2006-06-14 23:41:25 UTC (rev 
61723)
@@ -50,7 +50,7 @@
 
             Gdk.PixbufAnimation animation = new Gdk.PixbufAnimation 
(Assembly.GetExecutingAssembly (),
                                                                      
"ipod.gif");
-            hbox.PackStart (new Image (animation), false, false, 0);
+            hbox.PackStart (new Gtk.Image (animation), false, false, 0);
 
             label = new Label ("");
             label.Xalign = 0.0f;

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

Reply via email to