This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
FileTreeView.cs
234 lines (202 loc) · 7.63 KB
/
FileTreeView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// FileTreeView.cs
//
// Author:
// Jared Hendry (buchan@gmail.com)
// Mirco Bauer (meebey@meebey.net)
//
// Copyright (C) 2007 Jared Hendry
// Copyright (C) 2008 Mirco Bauer
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using Gtk;
using MonoTorrent.Common;
using MonoTorrent.Client;
namespace Monsoon
{
public class FileTreeView : TreeView
{
private TreeViewColumn priorityColumn;
private TreeViewColumn filenameColumn;
private TreeViewColumn progressColumn;
//private TreeStore treeStore;
private TorrentController torrentController;
private MainWindow mainWindow;
private Gtk.Menu contextMenu;
private ImageMenuItem highItem;
private ImageMenuItem highestItem;
private ImageMenuItem immediateItem;
private ImageMenuItem lowItem;
private ImageMenuItem lowestItem;
private ImageMenuItem normalItem;
private ImageMenuItem nodownItem;
public FileTreeView(MainWindow mainWindow, TorrentController torrentController, TreeStore treeStore) : base()
{
this.mainWindow = mainWindow;
this.torrentController = torrentController;
this.Selection.Mode = SelectionMode.Multiple;
HeadersVisible = true;
BuildColumns ();
BuildContextMenu ();
}
public static string GetPriorityIconName(Priority priority)
{
switch (priority) {
case Priority.Immediate:
return "immediate.png";
case Priority.Highest:
return "highest.png";
case Priority.High:
return "high.png";
case Priority.Normal:
return null;
case Priority.Low:
return "low.png";
case Priority.Lowest:
return "lowest.png";
case Priority.DoNotDownload:
return "donotdownload.png";
}
return null;
}
private void BuildColumns ()
{
priorityColumn = new TreeViewColumn ();
filenameColumn = new TreeViewColumn ();
progressColumn = new TreeViewColumn ();
filenameColumn.Resizable = true;
priorityColumn.Title = "";
filenameColumn.Title = _("Filename");
progressColumn.Title = _("Progress");
Gtk.CellRendererPixbuf priorityCell = new CellRendererPixbuf ();
Gtk.CellRendererText filenameCell = new CellRendererText ();
Gtk.CellRendererProgress progressCell = new CellRendererProgress ();
priorityColumn.PackStart (priorityCell, true);
priorityColumn.SetAttributes (priorityCell, "pixbuf", 2);
filenameColumn.PackStart (filenameCell, true);
filenameColumn.SetAttributes (filenameCell, "text", 3);
progressColumn.PackStart(progressCell, true);
progressColumn.SetCellDataFunc (progressCell, new Gtk.TreeCellDataFunc (RenderProgress));
AppendColumn (priorityColumn);
AppendColumn (filenameColumn);
AppendColumn (progressColumn);
}
private void BuildContextMenu ()
{
contextMenu = new Menu ();
highItem = new ImageMenuItem ("High");
highestItem = new ImageMenuItem ("Highest");
immediateItem = new ImageMenuItem ("Immediate");
lowItem = new ImageMenuItem ("Low");
lowestItem = new ImageMenuItem ("Lowest");
normalItem = new ImageMenuItem ("Normal");
nodownItem = new ImageMenuItem ("Do Not Download");
highItem.Image = new Image(System.IO.Path.Combine(Defines.IconPath, "high.png"));
highestItem.Image = new Image(System.IO.Path.Combine(Defines.IconPath, "highest.png"));
immediateItem.Image = new Image(System.IO.Path.Combine(Defines.IconPath, "immediate.png"));
lowItem.Image = new Image(System.IO.Path.Combine(Defines.IconPath, "low.png"));
lowestItem.Image = new Image(System.IO.Path.Combine(Defines.IconPath, "lowest.png"));
nodownItem.Image = new Image(System.IO.Path.Combine(Defines.IconPath, "donotdownload.png"));
highItem.Activated += OnContextMenuItemClicked;
highestItem.Activated += OnContextMenuItemClicked;
immediateItem.Activated += OnContextMenuItemClicked;
lowItem.Activated += OnContextMenuItemClicked;
lowestItem.Activated += OnContextMenuItemClicked;
normalItem.Activated += OnContextMenuItemClicked;
nodownItem.Activated += OnContextMenuItemClicked;
contextMenu.Append (immediateItem);
contextMenu.Append (highestItem);
contextMenu.Append (highItem);
contextMenu.Append (normalItem);
contextMenu.Append (lowItem);
contextMenu.Append (lowestItem);
contextMenu.Append (nodownItem);
}
private void OnContextMenuItemClicked (object sender, EventArgs args)
{
TorrentFile file;
Priority priority;
Selection.SelectedForeach(delegate (TreeModel model, TreePath path, TreeIter iter) {
ImageMenuItem item = (ImageMenuItem) sender;
// determine priority
if (item == highItem)
priority = Priority.High;
else if (item == highestItem)
priority = Priority.Highest;
else if (item == immediateItem)
priority = Priority.Immediate;
else if (item == lowItem)
priority = Priority.Low;
else if (item == lowestItem)
priority = Priority.Lowest;
else if (item == normalItem)
priority = Priority.Normal;
else if (item == nodownItem)
priority = Priority.DoNotDownload;
else
priority = Priority.Normal;
file = (TorrentFile) Model.GetValue (iter, 1);
// update priority in MonoTorrent
torrentController.SetFilePriority(file, priority);
// update priority icon in view model
Model.SetValue(iter, 2, MainWindow.GetIconPixbuf(GetPriorityIconName(priority)));
// update priority in settings model
//TorrentManager manager = (TorrentManager) Model.GetValue (iter, 0);
//TorrentFileSettingsController fileSettingsController =new TorrentFileSettingsController(settingsStorage);
//TorrentFileSettingsModel fileSettings = fileSettingsController.GetFileSettings(manager.Torrent.InfoHash, file.Path);
//fileSettings.Priority = priority;
//fileSettingsController.SetFileSettings(fileSettings);
mainWindow.StoreTorrentSettings();
});
}
protected override bool OnButtonPressEvent (Gdk.EventButton e)
{
int count = Selection.CountSelectedRows ();
if (count > 1 && e.Button == 3)
{
contextMenu.ShowAll();
contextMenu.Popup();
return true;
}
else if (count <= 1 && e.Button == 3)
{
base.OnButtonPressEvent(e);
contextMenu.ShowAll();
contextMenu.Popup();
return true;
}
base.OnButtonPressEvent(e);
return false;
}
private void RenderProgress (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
{
TorrentFile torrentFile = (TorrentFile) model.GetValue ( iter, 1);
(cell as Gtk.CellRendererProgress).Value = 0;
(cell as Gtk.CellRendererProgress).Value = (int)torrentFile.BitField.PercentComplete;
}
private static string _(string s)
{
return Mono.Unix.Catalog.GetString(s);
}
}
}