~do-plugins/do-plugins/trunk

« back to all changes in this revision

Viewing changes to Transmission/src/TorrentFileSetPriorityAction.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
 
 
6
using Mono.Addins;
 
7
 
 
8
using Do.Platform;
 
9
using Do.Universe;
 
10
 
 
11
namespace Transmission {
 
12
 
 
13
        public class PriorityItem: Item {
 
14
                private TransmissionAPI.FilePriority _value;
 
15
                private string _name;
 
16
                private string _icon;
 
17
 
 
18
                public PriorityItem(TransmissionAPI.FilePriority value, string name, string icon) {
 
19
                        _value = value;
 
20
                        _name = name;
 
21
                        _icon = icon;
 
22
                }
 
23
 
 
24
                public override string Name {
 
25
                        get { return _name; }
 
26
                }
 
27
 
 
28
                public override string Description {
 
29
                        get { return ""; }
 
30
                }
 
31
 
 
32
                public override string Icon {
 
33
                        get { return _icon; }
 
34
                }
 
35
 
 
36
                public TransmissionAPI.FilePriority Value {
 
37
                        get { return _value; }
 
38
                }
 
39
        }
 
40
 
 
41
        public class TorrentFileSetPriorityAction: Act {
 
42
 
 
43
                public TorrentFileSetPriorityAction() {
 
44
                }
 
45
 
 
46
            public override string Name {
 
47
                        get { return AddinManager.CurrentLocalizer.GetString ("Set priority"); }
 
48
            }
 
49
 
 
50
                public override string Description {
 
51
                        get { return AddinManager.CurrentLocalizer.GetString ("Set download priority"); }
 
52
                }
 
53
 
 
54
                public override string Icon {
 
55
                        get { return "object-flip-vertical"; }
 
56
                }
 
57
 
 
58
                public override IEnumerable<Type> SupportedItemTypes {
 
59
                        get {
 
60
                                yield return typeof (ITorrentEntry);
 
61
                        }
 
62
                }
 
63
 
 
64
                public override IEnumerable<Type> SupportedModifierItemTypes {
 
65
                        get {
 
66
                                yield return typeof (PriorityItem);
 
67
                        }
 
68
                }
 
69
 
 
70
                public override bool ModifierItemsOptional {
 
71
                        get { return false; }
 
72
                }
 
73
 
 
74
                public override IEnumerable<Item> DynamicModifierItemsForItem(Item item) {
 
75
                        yield return new PriorityItem(TransmissionAPI.FilePriority.Low, "Low", "down");
 
76
                        yield return new PriorityItem(TransmissionAPI.FilePriority.Normal, "Normal", "forward");
 
77
                        yield return new PriorityItem(TransmissionAPI.FilePriority.High, "High", "up");
 
78
                }
 
79
 
 
80
                public override IEnumerable<Item> Perform(IEnumerable<Item> items, IEnumerable<Item> modItems) {
 
81
                        TransmissionAPI.FilePriority priority = (modItems.First() as PriorityItem).Value;
 
82
 
 
83
                        TransmissionAPI.FileOperation operation = new TransmissionAPI.FileOperation(null, priority);
 
84
 
 
85
                        // Group torrent entries by torrent.
 
86
                        var files_by_torrent = items
 
87
                                .Cast<ITorrentEntry>()
 
88
                                .GroupBy(
 
89
                                        item => item.Torrent,
 
90
                                        (torrent, entries) => new {
 
91
                                                Torrent = torrent,
 
92
                                                Files = entries.SelectMany(entry => entry.GetFiles())
 
93
                                        }
 
94
                                );
 
95
 
 
96
                        TransmissionAPI api = TransmissionPlugin.getTransmission();
 
97
 
 
98
                        // Expand entries for each torrent into set of torrent file entries.
 
99
                        // Perform action for each torrent separately.
 
100
                        foreach (var group in files_by_torrent) {
 
101
                                var operations = group.Files.ToDictionary(f => f.Index, f => operation);
 
102
                                api.SetTorrent(group.Torrent.HashString, null, null, null, null, null, operations);
 
103
                        }
 
104
 
 
105
                        yield break;
 
106
                }
 
107
        }
 
108
}