[banshee] Playlists: refactor PlaylistParser to be less mutable



commit 47834bb071a1994d01ddfd7b82c6af7952749fec
Author: Andrés G. Aragoneses <knocte gmail com>
Date:   Thu Apr 3 15:14:07 2014 +0200

    Playlists: refactor PlaylistParser to be less mutable
    
    There's no need to make PlaylistParser.Parse() method depend
    on whether some previous code set the property BaseUri to
    some value beforehand.
    
    All current uses of the setter of the property can simply be
    replaced by a constructor that receives the value for BaseUri.
    By this we avoid any race condition that could be derived from
    reading the object after/before setting the property. Its
    getter access wasn't used at all so we can remove the property
    altogether, leaving the private field for its main usage in
    the Parse() method.

 .../Banshee.Playlist/PlaylistFileUtil.cs           |    4 ++--
 .../Banshee.Playlists.Formats/PlaylistParser.cs    |   12 ++++++------
 .../Tests/PlaylistFormatTests.cs                   |    6 ++----
 3 files changed, 10 insertions(+), 12 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Playlist/PlaylistFileUtil.cs 
b/src/Core/Banshee.Services/Banshee.Playlist/PlaylistFileUtil.cs
index e2df84e..47b5ae6 100644
--- a/src/Core/Banshee.Services/Banshee.Playlist/PlaylistFileUtil.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlist/PlaylistFileUtil.cs
@@ -172,12 +172,12 @@ namespace Banshee.Playlist
             try {
                 Log.InformationFormat ("Importing playlist {0} to library", path);
                 SafeUri uri = new SafeUri (path);
-                PlaylistParser parser = new PlaylistParser ();
                 string relative_dir = System.IO.Path.GetDirectoryName (uri.LocalPath);
                 if (relative_dir[relative_dir.Length - 1] != System.IO.Path.DirectorySeparatorChar) {
                     relative_dir = relative_dir + System.IO.Path.DirectorySeparatorChar;
                 }
-                parser.BaseUri = new Uri (relative_dir);
+
+                var parser = new PlaylistParser (new Uri (relative_dir));
                 if (parser.Parse (uri)) {
                     List<string> uris = new List<string> ();
                     foreach (PlaylistElement element in parser.Elements) {
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistParser.cs 
b/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistParser.cs
index 9e416dc..0f40e39 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistParser.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistParser.cs
@@ -54,6 +54,11 @@ namespace Banshee.Playlists.Formats
         private string title = null;
         private readonly int HTTP_REQUEST_RETRIES = 3;
 
+        public PlaylistParser (Uri baseUri)
+        {
+            base_uri = baseUri;
+        }
+
         public PlaylistParser ()
         {
             if (Environment.CurrentDirectory.Equals ("/")) {
@@ -138,7 +143,7 @@ namespace Banshee.Playlists.Formats
                 }
                 stream.Position = 0;
                 IPlaylistFormat playlist = (IPlaylistFormat)Activator.CreateInstance (matching_format.Type);
-                playlist.BaseUri = BaseUri;
+                playlist.BaseUri = base_uri;
                 playlist.Load (stream, false);
                 stream.Dispose ();
 
@@ -196,11 +201,6 @@ namespace Banshee.Playlists.Formats
             get { return elements; }
         }
 
-        public Uri BaseUri {
-            get { return base_uri; }
-            set { base_uri = value; }
-        }
-
         public string Title {
             get { return title; }
             set { title = value; }
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/Tests/PlaylistFormatTests.cs 
b/src/Core/Banshee.Services/Banshee.Playlists.Formats/Tests/PlaylistFormatTests.cs
index a34f485..8c297db 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/Tests/PlaylistFormatTests.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/Tests/PlaylistFormatTests.cs
@@ -96,8 +96,7 @@ namespace Banshee.Playlists.Formats.Tests
         [Test]
         public void ReadAsxEntryRef ()
         {
-            PlaylistParser parser = new PlaylistParser ();
-            parser.BaseUri = BaseUri;
+            var parser = new PlaylistParser (BaseUri);
 
             parser.Parse (new SafeUri ("http://download.banshee.fm/test/remote.asx";));
             IPlaylistFormat plref = LoadPlaylist (new AsxPlaylistFormat (), "entryref.asx");
@@ -160,8 +159,7 @@ namespace Banshee.Playlists.Formats.Tests
         [Test]
         public void ReadDetectMagic ()
         {
-            PlaylistParser parser = new PlaylistParser ();
-            parser.BaseUri = BaseUri;
+            var parser = new PlaylistParser (BaseUri);
 
             foreach (string path in Directory.GetFiles (playlists_dir)) {
                 parser.Parse (new SafeUri (Path.Combine (Environment.CurrentDirectory, path)));


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