[banshee] [MusicBrainz] Update to latest from remote svn



commit 1170206d9ddf8903253f410115863654d98edb04
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Fri Jun 11 10:37:57 2010 -0700

    [MusicBrainz] Update to latest from remote svn

 src/Libraries/MusicBrainz/MusicBrainz/Disc.cs      |    1 -
 .../MusicBrainz/MusicBrainz/DiscFreeBsd.cs         |  145 ++++++++++++++++++++
 src/Libraries/MusicBrainz/MusicBrainz/LocalDisc.cs |   10 +-
 .../MusicBrainz/MusicBrainz/MusicBrainzObject.cs   |   36 ++++--
 src/Libraries/MusicBrainz/MusicBrainz/Release.cs   |    2 +-
 src/Libraries/MusicBrainz/MusicBrainz/Track.cs     |   16 ++
 src/Libraries/MusicBrainz/MusicBrainz/Utils.cs     |    3 +-
 7 files changed, 196 insertions(+), 17 deletions(-)
---
diff --git a/src/Libraries/MusicBrainz/MusicBrainz/Disc.cs b/src/Libraries/MusicBrainz/MusicBrainz/Disc.cs
index 91f9f6f..37bec07 100644
--- a/src/Libraries/MusicBrainz/MusicBrainz/Disc.cs
+++ b/src/Libraries/MusicBrainz/MusicBrainz/Disc.cs
@@ -20,7 +20,6 @@
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 
-using System;
 using System.Xml;
 
 namespace MusicBrainz
diff --git a/src/Libraries/MusicBrainz/MusicBrainz/DiscFreeBsd.cs b/src/Libraries/MusicBrainz/MusicBrainz/DiscFreeBsd.cs
new file mode 100644
index 0000000..a89e387
--- /dev/null
+++ b/src/Libraries/MusicBrainz/MusicBrainz/DiscFreeBsd.cs
@@ -0,0 +1,145 @@
+// DiskFreeBSD.cs
+// 
+// Copyright (c) 2009 Romain Tartière <romain blogreen org>
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace MusicBrainz
+{
+    internal sealed class DiscFreeBSD : LocalDisc
+    {
+
+        #region <fcntl.h>
+        const int O_RDONLY = 0x0;
+        // open for reading only
+        const int O_NONBLOCK = 0x4;
+        // no delay
+        [DllImport("libc.so.7", CharSet = CharSet.Auto, SetLastError = true)]
+        static extern int open (string path, int flags);
+        #endregion
+
+        #region <unistd.h>
+        [DllImport("libc.so.7", SetLastError = true)]
+        static extern int close (int d);
+        #endregion
+
+        #region <sys/cdio.h>
+        struct msf_lba
+        {
+            public int lba;
+            // network byte order
+        }
+
+        [StructLayout(LayoutKind.Explicit)]
+        struct cd_toc_entry
+        {
+            [FieldOffset(2)]
+            public byte track;
+            [FieldOffset(4)]
+            public msf_lba addr;
+        }
+
+        // Ioctls for the CD drive
+
+        const byte CD_LBA_FORMAT = 1;
+
+        struct ioc_toc_header
+        {
+            public short len;
+            public byte starting_track;
+            public byte ending_track;
+        }
+        const ulong CDIOREADTOCHEADER = 1074029316;
+        [DllImport("libc.so.7", EntryPoint = "ioctl")]
+        static extern int cd_read_toc_header (int fd, ulong request, ref ioc_toc_header data);
+        static int cd_read_toc_header (int fd, ref ioc_toc_header data)
+        {
+            return cd_read_toc_header (fd, CDIOREADTOCHEADER, ref data);
+        }
+
+        struct ioc_read_toc_entry
+        {
+            public byte address_format;
+            public byte starting_track;
+            public ushort data_len;
+            public IntPtr data;
+            // cd_toc_entry*
+        }
+        const ulong CDIOREADTOCENTRYS = 3222299397u;
+        [DllImport("libc.so.7", EntryPoint = "ioctl")]
+        static extern int cd_read_toc_entrys (int fd, ulong request, ref ioc_read_toc_entry data);
+        static int cd_read_toc_entrys (int fd, ref ioc_read_toc_entry data)
+        {
+            return cd_read_toc_entrys (fd, CDIOREADTOCENTRYS, ref data);
+        }
+
+        #endregion
+
+        internal DiscFreeBSD (string device)
+        {
+            int fd = open (device, O_RDONLY | O_NONBLOCK);
+            
+            if (fd < 0)
+                throw new LocalDiscException (String.Format ("Cannot open device `{0}'", device));
+            
+            try {
+                ioc_toc_header h = new ioc_toc_header ();
+                if (cd_read_toc_header (fd, ref h) < 0)
+                    throw new LocalDiscException ("Cannot read table of contents header");
+                if (h.ending_track == 0)
+                    throw new LocalDiscException ("This disc has no tracks");
+                
+                first_track = h.starting_track;
+                last_track = h.ending_track;
+                
+                int n = h.ending_track - h.starting_track + 1;
+                int len = (n + 1) * Marshal.SizeOf (typeof(cd_toc_entry));
+                
+                ioc_read_toc_entry t = new ioc_read_toc_entry ();
+                t.address_format = CD_LBA_FORMAT;
+                t.starting_track = 0;
+                t.data_len = (ushort)len;
+                t.data = Marshal.AllocHGlobal (len);
+                try {
+                    
+                    if (cd_read_toc_entrys (fd, ref t) < 0)
+                        throw new LocalDiscException ("Cannot read table of contents entries");
+                    
+                    for (int i = 0; i <= n; i++) {
+                        ulong offset = (ulong)(i * Marshal.SizeOf (typeof(cd_toc_entry)));
+                        cd_toc_entry e = (cd_toc_entry)Marshal.PtrToStructure ((IntPtr)((ulong)t.data + offset), typeof(cd_toc_entry));
+                        track_offsets[first_track + i] = System.Net.IPAddress.NetworkToHostOrder (e.addr.lba) + 150;
+                    }
+                    // Move Leadout to the beginning.
+                    track_offsets[0] = track_offsets[last_track + 1];
+                    track_offsets[last_track + 1] = 0;
+                } finally {
+                    Marshal.FreeHGlobal (t.data);
+                }
+            } finally {
+                close (fd);
+            }
+            
+            Init ();
+        }
+    }
+}
diff --git a/src/Libraries/MusicBrainz/MusicBrainz/LocalDisc.cs b/src/Libraries/MusicBrainz/MusicBrainz/LocalDisc.cs
index dc4da1b..e129126 100644
--- a/src/Libraries/MusicBrainz/MusicBrainz/LocalDisc.cs
+++ b/src/Libraries/MusicBrainz/MusicBrainz/LocalDisc.cs
@@ -101,7 +101,8 @@ namespace MusicBrainz
                 if (submission_url == null) {
                     submission_url = BuildSubmissionUrl ();
                 }
-                return submission_url; }
+                return submission_url;
+            }
         }
 
         Uri BuildSubmissionUrl ()
@@ -131,7 +132,12 @@ namespace MusicBrainz
             try {
                 switch (Environment.OSVersion.Platform){
                 case PlatformID.Unix:
-                    return new DiscLinux (device);
+                    // TODO can we actually detect the environment?
+                    //try {
+                        return new DiscLinux (device);
+                    //} catch {
+                    //    return new DiscFreeBSD (device);
+                    //}
                 //case PlatformID.Win32NT:
                     //return new DiscWin32NT (device);
                 default:
diff --git a/src/Libraries/MusicBrainz/MusicBrainz/MusicBrainzObject.cs b/src/Libraries/MusicBrainz/MusicBrainz/MusicBrainzObject.cs
index 4fea83f..1a2732a 100644
--- a/src/Libraries/MusicBrainz/MusicBrainz/MusicBrainzObject.cs
+++ b/src/Libraries/MusicBrainz/MusicBrainz/MusicBrainzObject.cs
@@ -38,7 +38,7 @@ namespace MusicBrainz
         #region Private Fields
 
         static DateTime last_accessed;
-        static readonly TimeSpan min_interval = new TimeSpan (0, 0, 1); // 1 second
+        static readonly TimeSpan min_interval = TimeSpan.FromSeconds (1);
         static readonly object server_mutex = new object ();
         static readonly string [] rels_params = new string [] {
             "artist-rels",
@@ -377,6 +377,8 @@ namespace MusicBrainz
             builder.Append (parameters);
             return builder.ToString ();
         }
+        
+        static bool? cache_implemented;
 
         static void XmlProcessingClosure (string url, XmlProcessingDelegate code)
         {
@@ -388,12 +390,15 @@ namespace MusicBrainz
                 Thread.Sleep ((min_interval - time).Milliseconds);
 
             WebRequest request = WebRequest.Create (url);
-            bool cache_implemented = false;
-
-            try {
+            if (cache_implemented == null) {
+                try {
+                    request.CachePolicy = MusicBrainzService.CachePolicy;
+                } catch (NotImplementedException) {
+                    cache_implemented = false;
+                }
+            } else if (cache_implemented.Value == true) {
                 request.CachePolicy = MusicBrainzService.CachePolicy;
-                cache_implemented = true;
-            } catch (NotImplementedException) {}
+            }
 
             HttpWebResponse response = null;
 
@@ -417,16 +422,25 @@ namespace MusicBrainz
                 throw new MusicBrainzNotFoundException ();
             }
 
-            bool from_cache = false;
-            try {
-                from_cache = cache_implemented && response.IsFromCache;
-            } catch (NotImplementedException) {}
+            bool from_cache;
+            if (cache_implemented == null) {
+                try {
+                    from_cache = response.IsFromCache;
+                    cache_implemented = true;
+                } catch (NotImplementedException) {
+                    from_cache = false;
+                    cache_implemented = false;
+                }
+            } else if (cache_implemented.Value) {
+                from_cache = response.IsFromCache;
+            } else {
+                from_cache = false;
+            }
 
             if (from_cache) Monitor.Exit (server_mutex);
 
             MusicBrainzService.OnXmlRequest (url, from_cache);
 
-            // Should we read the stream into a memory stream and run the XmlReader off of that?
             code (new XmlTextReader (response.GetResponseStream ()));
             response.Close ();
 
diff --git a/src/Libraries/MusicBrainz/MusicBrainz/Release.cs b/src/Libraries/MusicBrainz/MusicBrainz/Release.cs
index 64e8c97..9f2f49e 100644
--- a/src/Libraries/MusicBrainz/MusicBrainz/Release.cs
+++ b/src/Libraries/MusicBrainz/MusicBrainz/Release.cs
@@ -64,7 +64,7 @@ namespace MusicBrainz
             get { return EXTENSION; }
         }
 
-        static readonly string [] track_params = new string [] { "tracks", "track-level-rels", "artist" };
+        static readonly string [] track_params = new string [] { "tracks", "track-level-rels", "artist", "isrcs" };
 
         internal override void CreateIncCore (StringBuilder builder)
         {
diff --git a/src/Libraries/MusicBrainz/MusicBrainz/Track.cs b/src/Libraries/MusicBrainz/MusicBrainz/Track.cs
index b23ffdd..6de8923 100644
--- a/src/Libraries/MusicBrainz/MusicBrainz/Track.cs
+++ b/src/Libraries/MusicBrainz/MusicBrainz/Track.cs
@@ -37,6 +37,7 @@ namespace MusicBrainz
         TimeSpan? duration;
         ReadOnlyCollection<Release> releases;
         ReadOnlyCollection<string> puids;
+        ReadOnlyCollection<string> isrcs;
 
         #endregion
 
@@ -66,6 +67,7 @@ namespace MusicBrainz
         {
             if (releases == null) AppendIncParameters (builder, "releases");
             if (puids == null) AppendIncParameters (builder, "puids");
+            if (isrcs == null) AppendIncParameters (builder, "isrcs");
             base.CreateIncCore (builder);
         }
 
@@ -75,6 +77,7 @@ namespace MusicBrainz
             duration = track.GetDuration ();
             if (releases == null) releases = track.GetReleases ();
             if (puids == null) puids = track.GetPuids ();
+            if (isrcs == null) isrcs = track.GetIsrcs ();
             base.LoadMissingDataCore (track);
         }
 
@@ -100,6 +103,14 @@ namespace MusicBrainz
                     this.puids = puids.AsReadOnly ();
                 }
                 break;
+            case "isrc-list":
+                if (reader.ReadToDescendant ("isrc")) {
+                    List<string> isrcs = new List<string> ();
+                    do isrcs.Add (reader["id"]);
+                    while (reader.ReadToNextSibling ("isrc"));
+                    this.isrcs = isrcs.AsReadOnly ();
+                }
+                break;
             default:
                 base.ProcessXmlCore (reader);
                 break;
@@ -137,6 +148,11 @@ namespace MusicBrainz
         {
             return GetPropertyOrNew (ref puids);
         }
+        
+        public ReadOnlyCollection<string> GetIsrcs ()
+        {
+            return GetPropertyOrNew (ref isrcs, !AllRelsLoaded);
+        }
 
         public int GetTrackNumber (Release release)
         {
diff --git a/src/Libraries/MusicBrainz/MusicBrainz/Utils.cs b/src/Libraries/MusicBrainz/MusicBrainz/Utils.cs
index 3c1fcb9..d85e720 100644
--- a/src/Libraries/MusicBrainz/MusicBrainz/Utils.cs
+++ b/src/Libraries/MusicBrainz/MusicBrainz/Utils.cs
@@ -66,9 +66,8 @@ namespace MusicBrainz
                     c == '-' || c == '_' || c == '.' || c == '~')
                     builder.Append (c);
                 else {
-                    builder.Append ('%');
                     foreach (byte b in Encoding.UTF8.GetBytes (new char [] { c }))
-                        builder.AppendFormat ("{0:X}", b);
+                        builder.AppendFormat ("%{0:X}", b);
                 }
             }
         }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]