[banshee] PlaylistElement: make this class strongly-typed



commit 38e90dd7358db06fe2fd0350e93210bbc2fe61c4
Author: Andres G. Aragoneses <knocte gmail com>
Date:   Sat Dec 1 17:26:22 2012 +0000

    PlaylistElement: make this class strongly-typed
    
    This class will no longer be Dictionary-based. Before this, it was a
    Dictionary<string,object> in order to have essentially a list of elements
    with different types, accessible through strings. Now these elements are
    just the 3 that were being used (Uri, Duration, Title) but strongly-typed
    (to Uri, TimeSpan and string respectively).
    
    This avoids the use of "object" as effectively a dynamic type, and thus
    allows us to remove some nasty casts that were in place, increasing type
    safety, performance (micro?)optimization and readability.
    
    No change of behaviour in this commit.

 .../Banshee.Playlist/PlaylistFileUtil.cs           |    4 ++--
 .../AsfReferencePlaylistFormat.cs                  |    2 +-
 .../Banshee.Playlists.Formats/AsxPlaylistFormat.cs |    6 +++---
 .../Banshee.Playlists.Formats/M3uPlaylistFormat.cs |    8 ++++----
 .../Banshee.Playlists.Formats/PlaylistElement.cs   |    5 ++++-
 .../Banshee.Playlists.Formats/PlsPlaylistFormat.cs |    6 +++---
 .../XspfPlaylistFormat.cs                          |    2 +-
 .../Banshee.Streaming/RadioTrackInfo.cs            |    6 +++---
 .../Banshee.Dap.MassStorage/MassStorageSource.cs   |    2 +-
 9 files changed, 22 insertions(+), 19 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Playlist/PlaylistFileUtil.cs b/src/Core/Banshee.Services/Banshee.Playlist/PlaylistFileUtil.cs
index 0eb2d97..5754d97 100644
--- a/src/Core/Banshee.Services/Banshee.Playlist/PlaylistFileUtil.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlist/PlaylistFileUtil.cs
@@ -119,7 +119,7 @@ namespace Banshee.Playlist
             List<string> uris = new List<string> ();
             if (playlist != null) {
                 foreach (PlaylistElement element in playlist.Elements) {
-                    uris.Add (((Uri)element["uri"]).AbsoluteUri);
+                    uris.Add (element.Uri.AbsoluteUri);
                 }
             }
             return uris.ToArray ();
@@ -196,7 +196,7 @@ namespace Banshee.Playlist
                 if (parser.Parse (uri)) {
                     List<string> uris = new List<string> ();
                     foreach (PlaylistElement element in parser.Elements) {
-                        uris.Add (((Uri)element["uri"]).LocalPath);
+                        uris.Add (element.Uri.LocalPath);
                     }
 
                     if (source == null) {
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsfReferencePlaylistFormat.cs b/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsfReferencePlaylistFormat.cs
index 384503c..c5396ba 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsfReferencePlaylistFormat.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsfReferencePlaylistFormat.cs
@@ -69,7 +69,7 @@ namespace Banshee.Playlists.Formats
                     if (uri_aux.StartsWith ("http", StringComparison.CurrentCultureIgnoreCase))
                         uri_aux = "mmsh" + uri_aux.Substring (4);
 
-                    element["uri"] = ResolveUri (uri_aux);
+                    element.Uri = ResolveUri (uri_aux);
                     break;
                 }
             }
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsxPlaylistFormat.cs b/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsxPlaylistFormat.cs
index 918e832..a5a7c66 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsxPlaylistFormat.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsxPlaylistFormat.cs
@@ -137,7 +137,7 @@ namespace Banshee.Playlists.Formats
                 switch (xml_reader.LocalName.ToLower ()) {
                     case "title":
                         xml_reader.Read ();
-                        element["title"] = xml_reader.Value;
+                        element.Title = xml_reader.Value;
                         break;
 
                     case "ref":
@@ -147,13 +147,13 @@ namespace Banshee.Playlists.Formats
                             uri_aux = "mmsh" + uri_aux.Substring (4);
                     }
 
-                        element["uri"] = ResolveUri (uri_aux);
+                        element.Uri = ResolveUri (uri_aux);
                         break;
 
                     case "duration":
                         try {
                             xml_reader.Read ();
-                            element["duration"] = TimeSpan.Parse (xml_reader.Value);
+                            element.Duration = TimeSpan.Parse (xml_reader.Value);
                         } catch {
                         }
                         break;
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/M3uPlaylistFormat.cs b/src/Core/Banshee.Services/Banshee.Playlists.Formats/M3uPlaylistFormat.cs
index 77885ec..a8a6460 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/M3uPlaylistFormat.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/M3uPlaylistFormat.cs
@@ -88,7 +88,7 @@ namespace Banshee.Playlists.Formats
                 }
 
                 try {
-                    element["uri"] = ResolveUri(line);
+                    element.Uri = ResolveUri (line);
                 } catch {
                     Elements.Remove(element);
                 }
@@ -103,10 +103,10 @@ namespace Banshee.Playlists.Formats
             string [] parts = split.Split(new char [] { ',' }, 2);
 
             if(parts.Length == 2) {
-                element["duration"] = SecondsStringToTimeSpan(parts[0]);
-                element["title"] = parts[1].Trim();
+                element.Duration = SecondsStringToTimeSpan (parts[0]);
+                element.Title = parts[1].Trim ();
             } else {
-                element["title"] = split.Trim();
+                element.Title = split.Trim ();
             }
         }
 
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistElement.cs b/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistElement.cs
index 7e5690d..269f898 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistElement.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistElement.cs
@@ -29,8 +29,11 @@ using System.Collections.Generic;
 
 namespace Banshee.Playlists.Formats
 {
-    public class PlaylistElement : Dictionary<string, object>
+    public class PlaylistElement
     {
+        public string Title { get; set; }
+        public Uri Uri { get; set; }
+        public TimeSpan Duration { get; set; }
     }
 }
 
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlsPlaylistFormat.cs b/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlsPlaylistFormat.cs
index 84f8aa0..b605e96 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlsPlaylistFormat.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlsPlaylistFormat.cs
@@ -114,13 +114,13 @@ namespace Banshee.Playlists.Formats
 
                     switch(element_type) {
                         case PlsType.File:
-                            element["uri"] = ResolveUri(value_string);
+                            element.Uri = ResolveUri (value_string);
                             break;
                         case PlsType.Title:
-                            element["title"] = value_string;
+                            element.Title = value_string;
                             break;
                         case PlsType.Length:
-                            element["duration"] = SecondsStringToTimeSpan(value_string);
+                            element.Duration = SecondsStringToTimeSpan (value_string);
                             break;
                     }
                 } catch {
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/XspfPlaylistFormat.cs b/src/Core/Banshee.Services/Banshee.Playlists.Formats/XspfPlaylistFormat.cs
index 58d90b7..aabb2e5 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/XspfPlaylistFormat.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/XspfPlaylistFormat.cs
@@ -67,7 +67,7 @@ namespace Banshee.Playlists.Formats
             Title = playlist.Title;
             foreach (Xspf.Track track in playlist.Tracks) {
                 PlaylistElement element = AddElement ();
-                element["uri"] = track.GetLocationAt (0);
+                element.Uri = track.GetLocationAt (0);
             }
         }
 
diff --git a/src/Core/Banshee.Services/Banshee.Streaming/RadioTrackInfo.cs b/src/Core/Banshee.Services/Banshee.Streaming/RadioTrackInfo.cs
index e83cbaa..cb7324d 100644
--- a/src/Core/Banshee.Services/Banshee.Streaming/RadioTrackInfo.cs
+++ b/src/Core/Banshee.Services/Banshee.Streaming/RadioTrackInfo.cs
@@ -293,13 +293,13 @@ namespace Banshee.Streaming
                 PlaylistParser parser = new PlaylistParser();
                 if (parser.Parse(new SafeUri(uri))) {
                     foreach (PlaylistElement element in parser.Elements) {
-                        if(element.ContainsKey("uri")) {
+                        if (element.Uri != null) {
                             // mms can be a nested link
-                            string element_uri = element["uri"].ToString();
+                            string element_uri = element.Uri.ToString ();
                             if(element_uri.StartsWith("mms:", StringComparison.CurrentCultureIgnoreCase)){
                                 LoadStreamUri("http" + element_uri.Substring(element_uri.IndexOf(":")));
                             }
-                            stream_uris.Add(new SafeUri(((Uri)element["uri"]).AbsoluteUri));
+                            stream_uris.Add (new SafeUri (element.Uri.AbsoluteUri));
                         }
                     }
                 } else {
diff --git a/src/Dap/Banshee.Dap.MassStorage/Banshee.Dap.MassStorage/MassStorageSource.cs b/src/Dap/Banshee.Dap.MassStorage/Banshee.Dap.MassStorage/MassStorageSource.cs
index f142f6d..660e176 100644
--- a/src/Dap/Banshee.Dap.MassStorage/Banshee.Dap.MassStorage/MassStorageSource.cs
+++ b/src/Dap/Banshee.Dap.MassStorage/Banshee.Dap.MassStorage/MassStorageSource.cs
@@ -178,7 +178,7 @@ namespace Banshee.Dap.MassStorage
                     playlist.Save ();
                     //Hyena.Data.Sqlite.HyenaSqliteCommand.LogAll = true;
                     foreach (PlaylistElement element in loaded_playlist.Elements) {
-                        string track_path = (element["uri"] as Uri).LocalPath;
+                        string track_path = element.Uri.LocalPath;
                         int track_id = DatabaseTrackInfo.GetTrackIdForUri (new SafeUri (track_path), DbId);
                         if (track_id == 0) {
                             Log.DebugFormat ("Failed to find track {0} in DAP library to load it into playlist {1}", track_path, playlist_path);



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