~do-plugins/do-plugins/trunk

« back to all changes in this revision

Viewing changes to Transmission/src/TorrentAbstractLimitSpeedAction.cs

  • Committer: Christopher James Halse Rogers
  • Date: 2013-05-02 07:23:58 UTC
  • mfrom: (684.1.9 do-plugins)
  • Revision ID: chris@ed-20130502072358-ddn5oxyjngeearme
Merge long-awaited Transmission control plugin

Ported to Json.NET, as this has packages in the Debian and Ubuntu archives.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
using System;
 
3
using System.Linq;
 
4
using System.Collections.Generic;
 
5
using System.Text.RegularExpressions;
 
6
 
 
7
using Mono.Addins;
 
8
 
 
9
using Do.Platform;
 
10
using Do.Universe;
 
11
 
 
12
namespace Transmission {
 
13
 
 
14
        public abstract class TorrentAbstractLimitSpeedAction: Act {
 
15
 
 
16
                public override IEnumerable<Type> SupportedItemTypes {
 
17
                        get { yield return typeof (TorrentItem); }
 
18
                }
 
19
 
 
20
                public override IEnumerable<Type> SupportedModifierItemTypes {
 
21
                        get {
 
22
                                yield return typeof (ITextItem);
 
23
                                yield return typeof (PredefinedSpeed);
 
24
                        }
 
25
                }
 
26
 
 
27
                public override bool ModifierItemsOptional {
 
28
                        get { return false; }
 
29
                }
 
30
 
 
31
                protected abstract PredefinedSpeed GetCurrentSpeedItem(TorrentItem torrent);
 
32
 
 
33
                public override IEnumerable<Item> DynamicModifierItemsForItem(Item item) {
 
34
                        TorrentItem torrent = (TorrentItem)item;
 
35
 
 
36
                        yield return new PredefinedSpeed(0, "Unlimited", "Turn download speed limit off");
 
37
                        yield return GetCurrentSpeedItem(torrent);
 
38
                        foreach (PredefinedSpeed speed in Utils.PredefinedSpeedItems)
 
39
                                yield return speed;
 
40
                }
 
41
 
 
42
                protected abstract void SetSpeedLimit(TransmissionAPI api, IEnumerable<TorrentItem> torrents, int speed);
 
43
 
 
44
                public override IEnumerable<Item> Perform(IEnumerable<Item> items, IEnumerable<Item> modItems) {
 
45
                        int? speed = null;
 
46
 
 
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;
 
51
                        } else {
 
52
                                string speed_str = ((ITextItem)modItem).Text;
 
53
 
 
54
                                try {
 
55
                                        // Try to parse entered speed value.
 
56
                                        speed = Utils.ParseSpeed(speed_str);
 
57
 
 
58
                                } catch (ArgumentException) {
 
59
                                        Log<TransmissionPlugin>.Debug("Invalid speed string: {0}", speed_str);
 
60
 
 
61
                                        // Show notification about invalid speed value with some hints on
 
62
                                        // accepted formats.
 
63
                                        string message = AddinManager.CurrentLocalizer.GetString(
 
64
                                                "Can't recognize \"{0}\" as speed\nUse values like: 100k, 50 kb, 20m, 10 mib"
 
65
                                        );
 
66
                                        Services.Notifications.Notify("Transmission", string.Format(message, speed_str), "transmission");
 
67
                                }
 
68
                        }
 
69
 
 
70
                        // If speed is recognized successfully, set speed limit and update item.
 
71
                        if (speed.HasValue) {
 
72
                                TransmissionAPI api = TransmissionPlugin.getTransmission();
 
73
                                IEnumerable<TorrentItem> torrents = items.Cast<TorrentItem>();
 
74
                                SetSpeedLimit(api, torrents, speed.Value);
 
75
                        }
 
76
 
 
77
                        yield break;
 
78
                }
 
79
        }
 
80
}