4
using System.Collections.Generic;
5
using System.Text.RegularExpressions;
12
namespace Transmission {
14
public abstract class TorrentAbstractLimitSpeedAction: Act {
16
public override IEnumerable<Type> SupportedItemTypes {
17
get { yield return typeof (TorrentItem); }
20
public override IEnumerable<Type> SupportedModifierItemTypes {
22
yield return typeof (ITextItem);
23
yield return typeof (PredefinedSpeed);
27
public override bool ModifierItemsOptional {
31
protected abstract PredefinedSpeed GetCurrentSpeedItem(TorrentItem torrent);
33
public override IEnumerable<Item> DynamicModifierItemsForItem(Item item) {
34
TorrentItem torrent = (TorrentItem)item;
36
yield return new PredefinedSpeed(0, "Unlimited", "Turn download speed limit off");
37
yield return GetCurrentSpeedItem(torrent);
38
foreach (PredefinedSpeed speed in Utils.PredefinedSpeedItems)
42
protected abstract void SetSpeedLimit(TransmissionAPI api, IEnumerable<TorrentItem> torrents, int speed);
44
public override IEnumerable<Item> Perform(IEnumerable<Item> items, IEnumerable<Item> modItems) {
47
// Get speed item, it can be either ITextItem or PredefinedSpeed.
48
Item modItem = modItems.First();
49
if (modItem is PredefinedSpeed) {
50
speed = ((PredefinedSpeed)modItem).Value;
52
string speed_str = ((ITextItem)modItem).Text;
55
// Try to parse entered speed value.
56
speed = Utils.ParseSpeed(speed_str);
58
} catch (ArgumentException) {
59
Log<TransmissionPlugin>.Debug("Invalid speed string: {0}", speed_str);
61
// Show notification about invalid speed value with some hints on
63
string message = AddinManager.CurrentLocalizer.GetString(
64
"Can't recognize \"{0}\" as speed\nUse values like: 100k, 50 kb, 20m, 10 mib"
66
Services.Notifications.Notify("Transmission", string.Format(message, speed_str), "transmission");
70
// If speed is recognized successfully, set speed limit and update item.
72
TransmissionAPI api = TransmissionPlugin.getTransmission();
73
IEnumerable<TorrentItem> torrents = items.Cast<TorrentItem>();
74
SetSpeedLimit(api, torrents, speed.Value);