-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainController.m
339 lines (279 loc) · 9.4 KB
/
MainController.m
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//
// MainView.m
// jigsaw
//
// Created by Will Goring on 06/12/2009.
//
#import "MainController.h"
#import "TorrentModel.h"
#import "PreferenceController.h"
#import <dispatch/dispatch.h>
@implementation MainController
#pragma mark initialistion
- (void)awakeFromNib
{
[rateModel setDelegate:self];
// Set the target for the filter change buttons
for (NSInteger i = 0; i < [[filters cells] count]; i++) {
[[[filters cells] objectAtIndex:i] setTarget:self];
[[[filters cells] objectAtIndex:i] setAction:@selector(filterChanged:)];
}
// Set up the View NSPredicates
completeFilter = [NSPredicate predicateWithBlock:^(id evaluatedObject, NSDictionary *bindings)
{
TorrentModel *t = evaluatedObject;
if ([t size] == [t downloaded]) {
return YES;
} else {
return NO;
}
}];
incompleteFilter = [NSPredicate predicateWithBlock:^(id evaluatedObject, NSDictionary *bindings)
{
TorrentModel *t = evaluatedObject;
if ([t size] == [t downloaded]) {
return NO;
} else {
return YES;
}
}];
// Set up the contents of the throttle selection menus
defaultDownThrottles = [NSArray arrayWithObjects:[NSNumber numberWithInt:50],
[NSNumber numberWithInt:100],[NSNumber numberWithInt:500],
[NSNumber numberWithInt:1000],[NSNumber numberWithInt:2000],[NSNumber numberWithInt:0], nil];
defaultUpThrottles = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:50],
[NSNumber numberWithInt:100],[NSNumber numberWithInt:200],[NSNumber numberWithInt:500],
[NSNumber numberWithInt:0], nil];
// Load the column order/visibility/size settings
[torrentTable setAutosaveName:@"org.shadowrealm.jigsaw.All"];
// Create pop-up menu of available columns
NSArray *headers = [torrentTable tableColumns];
for (int i = 0; i < [headers count]; i++) {
NSTableColumn *column = [headers objectAtIndex:i];
if ([[[column headerCell] stringValue] length] > 0) {
NSMenuItem *item = [[NSMenuItem new] initWithTitle:[[column headerCell] stringValue] action:nil keyEquivalent:@"" ];
[item setTarget:self];
[item setAction:@selector(changeColumnState:)];
[column setIdentifier:[item title]];
[headerSelectionMenu addItem:item];
}
}
[self setColumnMenuStates];
// Get the current state and start the update timer
[self displayed];
}
#pragma mark UI control
- (void) updateThrottlePopup: (NSPopUpButton *) popup throttle: (NSInteger) throttle defaultValues: (NSArray *) defaultValues {
if ([popup indexOfItemWithTag:throttle] != -1) {
[popup selectItemWithTag:throttle];
} else {
for (NSInteger i = 0; i < [popup numberOfItems]; i++) {
NSNumber *tag = [NSNumber numberWithInt:[[popup itemAtIndex:i] tag]];
if (![defaultValues containsObject:tag]) {
[popup removeItemAtIndex:i];
break;
}
}
for (NSInteger i = 0; i < [popup numberOfItems]; i++) {
NSInteger tag = [[popup itemAtIndex:i] tag];
if (tag > throttle || tag == 0) {
NSString *title = [NSString stringWithFormat:@"%d KB/s", throttle];
[popup insertItemWithTitle:title atIndex:i];
[[popup itemAtIndex:i] setTag:throttle];
[popup selectItemWithTag:throttle];
break;
}
}
}
}
#pragma mark data control
-(void)displayed
{
[self updateRateModel];
[self updateTorrentListModel];
if ([timer isValid]) {
[timer invalidate];
}
timer = [NSTimer scheduledTimerWithTimeInterval:[[NSUserDefaults standardUserDefaults] integerForKey:SROUpdate] target:self selector:@selector(onTimer:) userInfo:nil repeats:TRUE];
}
-(void)hidden
{
if ([timer isValid]) {
[timer invalidate];
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:SROUpdateWhileHidden]) {
timer = [NSTimer scheduledTimerWithTimeInterval:[[NSUserDefaults standardUserDefaults] integerForKey:SROHiddenUpdate] target:self selector:@selector(onTimer:) userInfo:nil repeats:TRUE];
}
}
- (void)updateRateModel
{
[rateModel update];
}
- (void)updateTorrentListModel
{
[torrentListModel update];
}
- (void)addLocalTorrentFile:(NSString *)filename
{
NSLog(@"%@", filename);
NSData *torrentData = [[NSFileManager defaultManager] contentsAtPath:filename];
[torrentListModel addTorrentWithData:torrentData];
[self updateTorrentListModel];
// TODO: This should be an option
// Delete the .torrent file, now we've added it to rtorrent
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:filename error:nil];
}
- (IBAction)addTorrentFile:(id)sender
{
// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];
// Enable the selection of directories in the dialog.
[openDlg setCanChooseDirectories:NO];
// Disable Multiple Selection
[openDlg setAllowsMultipleSelection:NO];
// Types we allow
NSArray* fileType = [NSArray arrayWithObjects: @"torrent", nil];
// Display the dialog. If the OK button was pressed,
// process the files.
if ( [openDlg runModalForDirectory:nil file:nil types:fileType] == NSOKButton )
{
NSArray *files = [openDlg URLs];
NSURL *url = [files objectAtIndex:0];
NSString *fileName = [url path];
[self addLocalTorrentFile:fileName];
// (Optionally) delete the .torrent file, now we've added it to rtorrent
if ([[NSUserDefaults standardUserDefaults] boolForKey:SRODeleteOnAdd]) {
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtURL:url error:nil];
}
}
}
#pragma mark events
- (void)onTimer:(NSTimer *)timer
{
[self updateTorrentListModel];
[self updateRateModel];
}
- (IBAction)filterChanged:(id)sender
{
NSString *newView = [NSString stringWithString:[[filters selectedCell] title]];
if ([newView isEqualToString:@"All"]) {
[torrentListController setFilterPredicate:nil];
[torrentTable setAutosaveName:@"org.shadowrealm.jigsaw.All"];
}
if ([newView isEqualToString:@"Complete"]) {
[torrentListController setFilterPredicate:completeFilter];
[torrentTable setAutosaveName:@"org.shadowrealm.jigsaw.Complete"];
}
if ([newView isEqualToString:@"Incomplete"]) {
[torrentListController setFilterPredicate:incompleteFilter];
[torrentTable setAutosaveName:@"org.shadowrealm.jigsaw.Incomplete"];
}
[self setColumnMenuStates];
}
- (void)didUpdateRates
{
[self updateThrottlePopup:upThrottlePopup throttle:[[rateModel upLimit] intValue] defaultValues:defaultUpThrottles];
[self updateThrottlePopup:downThrottlePopup throttle:[[rateModel downLimit] intValue] defaultValues:defaultDownThrottles];
}
- (IBAction)upThrottleChanged:(id)sender
{
NSInteger bytesPerSecond = [[upThrottlePopup selectedItem] tag];
[rateModel setUpThrottle:bytesPerSecond];
}
- (IBAction)downThrottleChanged:(id)sender
{
NSInteger bytesPerSecond = [[downThrottlePopup selectedItem] tag];
[rateModel setDownThrottle:bytesPerSecond];
}
#pragma mark menu handling methods
- (IBAction)stopTorrent:(id)sender
{
TorrentModel *torrent = [[torrentListController selectedObjects] objectAtIndex:0];
if (torrent != nil) {
[torrent stop];
[torrentListModel update];
}
}
- (IBAction)startTorrent:(id)sender
{
TorrentModel *torrent = [[torrentListController selectedObjects] objectAtIndex:0];
if (torrent != nil) {
[torrent start];
[torrentListModel update];
}
}
- (IBAction)deleteTorrent:(id)sender
{
TorrentModel *torrent = [[torrentListController selectedObjects] objectAtIndex:0];
if (torrent != nil) {
[torrent remove];
[torrentListModel update];
}
}
- (IBAction)setTorrentPriority:(id)sender
{
TorrentModel *torrent = [[torrentListController selectedObjects] objectAtIndex:0];
int priority = [sender tag];
[torrent changePriority:priority];
}
- (void)setPriorityCheckMark:(NSMenu*)menu forTorrent:(TorrentModel*)torrent
{
NSArray *items = [menu itemArray];
for (NSInteger menuItem = 0; menuItem < [items count]; menuItem++) {
if ([[items objectAtIndex:menuItem] tag] == [torrent priority]) {
[[items objectAtIndex:menuItem] setState:NSOnState];
} else {
[[items objectAtIndex:menuItem] setState:NSOffState];
}
}
}
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
TorrentModel *torrent = nil;
if ( [[torrentListController selectedObjects] count] > 0 ) {
torrent = [[torrentListController selectedObjects] objectAtIndex:0];
}
if ([item action] == @selector(setTorrentPriority:)) {
[self setPriorityCheckMark:priorityMenu forTorrent:torrent];
[self setPriorityCheckMark:priorityContextMenu forTorrent:torrent];
return (torrent != nil);
}
if ([item action] == @selector(stopTorrent:)) {
return (torrent != nil && [torrent active]) ? YES : NO;
}
if ([item action] == @selector(startTorrent:)) {
return (torrent != nil && ![torrent active]) ? YES : NO;
}
if ([item action] == @selector(deleteTorrent:)) {
return (torrent != nil && ![torrent active]) ? YES : NO;
} else {
return YES;
}
}
- (IBAction)changeColumnState:(id)sender
{
if ([sender state]) {
[[torrentTable tableColumnWithIdentifier:[sender title]] setHidden:YES];
[sender setState:NSOffState];
} else {
[[torrentTable tableColumnWithIdentifier:[sender title]] setHidden:NO];
[sender setState:NSOnState];
}
}
- (void)setColumnMenuStates
{
NSArray *items = [headerSelectionMenu itemArray];
for (int item = 0; item < [items count]; item++) {
NSMenuItem *menuItem = [items objectAtIndex:item];
if ([[torrentTable tableColumnWithIdentifier:[menuItem title]] isHidden]) {
[menuItem setState:NSOffState];
} else {
[menuItem setState:NSOnState];
}
}
}
@end