Permalink
Browse files

[TorrentEditor] Implement two helper classes to help edit Announce urls

Created a wrapper around the announce-urls structure to make it easy to
directly update the bencoded list. Added the ability to edit announces
to TorrentEditor using these new classes.
  • Loading branch information...
1 parent 4caf6ad commit 62ba25fda1eac3fe2b7e9ea3e3ec4f05e4f9f8d8 @alanmcgovern alanmcgovern committed Oct 7, 2010
View
90 src/MonoTorrent/MonoTorrent.Common/RawTrackerTier.cs
@@ -0,0 +1,90 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+using MonoTorrent.BEncoding;
+
+namespace MonoTorrent
+{
+ public class RawTrackerTier : IList<string>
+ {
+ public string this[int index] {
+ get { return ((BEncodedString) Tier [index]).Text; }
+ set { Tier [index] = new BEncodedString (value );}
+ }
+
+ internal BEncodedList Tier {
+ get; set;
+ }
+
+ public RawTrackerTier ()
+ : this (new BEncodedList ())
+ {
+ }
+
+ public RawTrackerTier (BEncodedList tier)
+ {
+ Tier = tier;
+ }
+
+ public int IndexOf (string item)
+ {
+ return Tier.IndexOf ((BEncodedString) item);
+ }
+
+ public void Insert (int index, string item)
+ {
+ Tier.Insert (index, (BEncodedString) item);
+ }
+
+ public void RemoveAt (int index)
+ {
+ Tier.RemoveAt (index);
+ }
+
+ public void Add (string item)
+ {
+ Tier.Add ((BEncodedString) item);
+ }
+
+ public void Clear ()
+ {
+ Tier.Clear ();
+ }
+
+ public bool Contains (string item)
+ {
+ return Tier.Contains ((BEncodedString) item);
+ }
+
+ public void CopyTo (string[] array, int arrayIndex)
+ {
+ foreach (var s in this)
+ array [arrayIndex ++] = s;
+ }
+
+ public bool Remove (string item)
+ {
+ return Tier.Remove ((BEncodedString) item);
+ }
+
+ public int Count {
+ get { return Tier.Count; }
+ }
+
+ public bool IsReadOnly {
+ get { return Tier.IsReadOnly; }
+ }
+
+ public IEnumerator<string> GetEnumerator ()
+ {
+ foreach (BEncodedString v in Tier)
+ yield return v.Text;
+ }
+
+ IEnumerator IEnumerable.GetEnumerator ()
+ {
+ return GetEnumerator ();
+ }
+ }
+}
View
99 src/MonoTorrent/MonoTorrent.Common/RawTrackerTiers.cs
@@ -0,0 +1,99 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+using MonoTorrent.BEncoding;
+
+namespace MonoTorrent
+{
+ public class RawTrackerTiers : IList<RawTrackerTier>
+ {
+ BEncodedList Tiers {
+ get; set;
+ }
+
+ public RawTrackerTiers ()
+ : this (new BEncodedList ())
+ {
+ }
+
+ public RawTrackerTiers (BEncodedList tiers)
+ {
+ Tiers = tiers;
+ }
+
+ public int IndexOf (RawTrackerTier item)
+ {
+ if (item != null) {
+ for (int i = 0; i < Tiers.Count; i++)
+ if (item.Tier == Tiers [i])
+ return i;
+ }
+ return -1;
+ }
+
+ public void Insert (int index, RawTrackerTier item)
+ {
+ Tiers.Insert (index, item.Tier);
+ }
+
+ public void RemoveAt (int index)
+ {
+ Tiers.RemoveAt (index);
+ }
+
+ public RawTrackerTier this[int index] {
+ get { return new RawTrackerTier ((BEncodedList) Tiers [index]); }
+ set { Tiers [index] = value.Tier; }
+ }
+
+ public void Add (RawTrackerTier item)
+ {
+ Tiers.Add (item.Tier);
+ }
+
+ public void Clear ()
+ {
+ Tiers.Clear ();
+ }
+
+ public bool Contains (RawTrackerTier item)
+ {
+ return IndexOf (item) != -1;
+ }
+
+ public void CopyTo (RawTrackerTier[] array, int arrayIndex)
+ {
+ foreach (var v in this)
+ array [arrayIndex ++] = v;
+ }
+
+ public bool Remove (RawTrackerTier item)
+ {
+ int index = IndexOf (item);
+ if (index != -1)
+ RemoveAt (index);
+
+ return index != -1;
+ }
+
+ public int Count {
+ get { return Tiers.Count; }
+ }
+
+ public bool IsReadOnly {
+ get { return Tiers.IsReadOnly; }
+ }
+
+ public IEnumerator<RawTrackerTier> GetEnumerator ()
+ {
+ foreach (var v in Tiers)
+ yield return new RawTrackerTier ((BEncodedList) v);
+ }
+
+ IEnumerator IEnumerable.GetEnumerator ()
+ {
+ return GetEnumerator ();
+ }
+ }
+}
View
30 src/MonoTorrent/MonoTorrent.Common/TorrentEditor.cs
@@ -26,10 +26,24 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
+using System;
+
+using MonoTorrent.BEncoding;
+using MonoTorrent.Common;
+
namespace MonoTorrent {
public class TorrentEditor {
+ public string Announce {
+ get { return GetString (Metadata, "announce"); }
+ set { SetString (Metadata, "announce", value); }
+ }
+
+ public RawTrackerTiers Announces {
+ get; private set;
+ }
+
public bool CanEditSecureMetadata {
get; set;
}
@@ -80,13 +94,25 @@ public class TorrentEditor {
public TorrentEditor (Torrent torrent)
{
Check.Torrent (torrent);
- Metadata = torrent.ToDictionary ();
+ LoadFrom (torrent.ToDictionary ());
}
public TorrentEditor (BEncodedDictionary metadata)
{
Check.Metadata (metadata);
- Metadata = (BEncodedDictionary) BEncodedValue.Decode (metadata.Encode ());
+ LoadFrom (BEncodedValue.Clone (metadata));
+ }
+
+ void LoadFrom (BEncodedDictionary metadata)
+ {
+ Metadata = metadata;
+ BEncodedValue value;
+ if (!Metadata.TryGetValue ("announce-list", out value)) {
+ value = new BEncodedList ();
+ Metadata.Add ("announce-list", value);
+ }
+
+ Announces = new RawTrackerTiers ((BEncodedList) value);
}
void CheckCanEditSecure ()
View
2 src/MonoTorrent/MonoTorrent.csproj
@@ -328,6 +328,8 @@
<Compile Include="MonoTorrent.Common\MagnetLink.cs" />
<Compile Include="MonoTorrent.Common\UriHelper.cs" />
<Compile Include="MonoTorrent.Common\TorrentEditor.cs" />
+ <Compile Include="MonoTorrent.Common\RawTrackerTiers.cs" />
+ <Compile Include="MonoTorrent.Common\RawTrackerTier.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Framework.2.0">

0 comments on commit 62ba25f

Please sign in to comment.