1 | /****************************************************************************** |
---|
2 | * $Id: TorrentTableView.m 12991 2011-10-18 02:30:22Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2011 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "TorrentTableView.h" |
---|
26 | #import "Controller.h" |
---|
27 | #import "FileListNode.h" |
---|
28 | #import "InfoOptionsViewController.h" |
---|
29 | #import "NSApplicationAdditions.h" |
---|
30 | #import "NSStringAdditions.h" |
---|
31 | #import "Torrent.h" |
---|
32 | #import "TorrentCell.h" |
---|
33 | #import "TorrentGroup.h" |
---|
34 | |
---|
35 | #define MAX_GROUP 999999 |
---|
36 | |
---|
37 | //eliminate when Lion-only |
---|
38 | #define ACTION_MENU_GLOBAL_TAG 101 |
---|
39 | #define ACTION_MENU_UNLIMITED_TAG 102 |
---|
40 | #define ACTION_MENU_LIMIT_TAG 103 |
---|
41 | |
---|
42 | #define ACTION_MENU_PRIORITY_HIGH_TAG 101 |
---|
43 | #define ACTION_MENU_PRIORITY_NORMAL_TAG 102 |
---|
44 | #define ACTION_MENU_PRIORITY_LOW_TAG 103 |
---|
45 | |
---|
46 | #define TOGGLE_PROGRESS_SECONDS 0.175 |
---|
47 | |
---|
48 | @interface TorrentTableView (Private) |
---|
49 | |
---|
50 | - (BOOL) pointInGroupStatusRect: (NSPoint) point; |
---|
51 | |
---|
52 | - (void) setGroupStatusColumns; |
---|
53 | |
---|
54 | @end |
---|
55 | |
---|
56 | @implementation TorrentTableView |
---|
57 | |
---|
58 | - (id) initWithCoder: (NSCoder *) decoder |
---|
59 | { |
---|
60 | if ((self = [super initWithCoder: decoder])) |
---|
61 | { |
---|
62 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
63 | |
---|
64 | fTorrentCell = [[TorrentCell alloc] init]; |
---|
65 | |
---|
66 | NSData * groupData = [fDefaults dataForKey: @"CollapsedGroups"]; |
---|
67 | if (groupData) |
---|
68 | fCollapsedGroups = [[NSUnarchiver unarchiveObjectWithData: groupData] mutableCopy]; |
---|
69 | else |
---|
70 | fCollapsedGroups = [[NSMutableIndexSet alloc] init]; |
---|
71 | |
---|
72 | fMouseRow = -1; |
---|
73 | fMouseControlRow = -1; |
---|
74 | fMouseRevealRow = -1; |
---|
75 | fMouseActionRow = -1; |
---|
76 | #warning we can get rid of the on 10.7 |
---|
77 | fActionPushedRow = -1; |
---|
78 | |
---|
79 | fActionPopoverShown = NO; |
---|
80 | |
---|
81 | [self setDelegate: self]; |
---|
82 | |
---|
83 | fPiecesBarPercent = [fDefaults boolForKey: @"PiecesBar"] ? 1.0 : 0.0; |
---|
84 | } |
---|
85 | |
---|
86 | return self; |
---|
87 | } |
---|
88 | |
---|
89 | - (void) dealloc |
---|
90 | { |
---|
91 | [[NSNotificationCenter defaultCenter] removeObserver: self]; |
---|
92 | |
---|
93 | [fCollapsedGroups release]; |
---|
94 | |
---|
95 | [fPiecesBarAnimation release]; |
---|
96 | [fMenuTorrent release]; |
---|
97 | |
---|
98 | [fSelectedValues release]; |
---|
99 | |
---|
100 | [fTorrentCell release]; |
---|
101 | |
---|
102 | [super dealloc]; |
---|
103 | } |
---|
104 | |
---|
105 | - (void) awakeFromNib |
---|
106 | { |
---|
107 | //set group columns to show ratio, needs to be in awakeFromNib to size columns correctly |
---|
108 | [self setGroupStatusColumns]; |
---|
109 | |
---|
110 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reloadData) |
---|
111 | name: @"ReloadTorrentTable" object: nil]; |
---|
112 | } |
---|
113 | |
---|
114 | - (BOOL) isGroupCollapsed: (NSInteger) value |
---|
115 | { |
---|
116 | if (value == -1) |
---|
117 | value = MAX_GROUP; |
---|
118 | |
---|
119 | return [fCollapsedGroups containsIndex: value]; |
---|
120 | } |
---|
121 | |
---|
122 | - (void) removeCollapsedGroup: (NSInteger) value |
---|
123 | { |
---|
124 | if (value == -1) |
---|
125 | value = MAX_GROUP; |
---|
126 | |
---|
127 | [fCollapsedGroups removeIndex: value]; |
---|
128 | } |
---|
129 | |
---|
130 | - (void) removeAllCollapsedGroups |
---|
131 | { |
---|
132 | [fCollapsedGroups removeAllIndexes]; |
---|
133 | } |
---|
134 | |
---|
135 | - (void) saveCollapsedGroups |
---|
136 | { |
---|
137 | [fDefaults setObject: [NSArchiver archivedDataWithRootObject: fCollapsedGroups] forKey: @"CollapsedGroups"]; |
---|
138 | } |
---|
139 | |
---|
140 | - (BOOL) outlineView: (NSOutlineView *) outlineView isGroupItem: (id) item |
---|
141 | { |
---|
142 | return ![item isKindOfClass: [Torrent class]]; |
---|
143 | } |
---|
144 | |
---|
145 | - (CGFloat) outlineView: (NSOutlineView *) outlineView heightOfRowByItem: (id) item |
---|
146 | { |
---|
147 | return [item isKindOfClass: [Torrent class]] ? [self rowHeight] : GROUP_SEPARATOR_HEIGHT; |
---|
148 | } |
---|
149 | |
---|
150 | - (NSCell *) outlineView: (NSOutlineView *) outlineView dataCellForTableColumn: (NSTableColumn *) tableColumn item: (id) item |
---|
151 | { |
---|
152 | const BOOL group = ![item isKindOfClass: [Torrent class]]; |
---|
153 | if (!tableColumn) |
---|
154 | return !group ? fTorrentCell : nil; |
---|
155 | else |
---|
156 | return group ? [tableColumn dataCellForRow: [self rowForItem: item]] : nil; |
---|
157 | } |
---|
158 | |
---|
159 | - (void) outlineView: (NSOutlineView *) outlineView willDisplayCell: (id) cell forTableColumn: (NSTableColumn *) tableColumn |
---|
160 | item: (id) item |
---|
161 | { |
---|
162 | if ([item isKindOfClass: [Torrent class]]) |
---|
163 | { |
---|
164 | if (!tableColumn) |
---|
165 | { |
---|
166 | [cell setRepresentedObject: item]; |
---|
167 | |
---|
168 | const NSInteger row = [self rowForItem: item]; |
---|
169 | [cell setHover: row == fMouseRow]; |
---|
170 | [cell setControlHover: row == fMouseControlRow]; |
---|
171 | [cell setRevealHover: row == fMouseRevealRow]; |
---|
172 | [cell setActionHover: row == fMouseActionRow]; |
---|
173 | [cell setActionPushed: row == fActionPushedRow]; |
---|
174 | } |
---|
175 | } |
---|
176 | else |
---|
177 | { |
---|
178 | NSString * ident = [tableColumn identifier]; |
---|
179 | if ([ident isEqualToString: @"UL Image"] || [ident isEqualToString: @"DL Image"]) |
---|
180 | { |
---|
181 | //ensure arrows are white only when selected |
---|
182 | [[cell image] setTemplate: [cell backgroundStyle] == NSBackgroundStyleLowered]; |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | - (NSRect) frameOfCellAtColumn: (NSInteger) column row: (NSInteger) row |
---|
188 | { |
---|
189 | if (column == -1) |
---|
190 | return [self rectOfRow: row]; |
---|
191 | else |
---|
192 | { |
---|
193 | NSRect rect = [super frameOfCellAtColumn: column row: row]; |
---|
194 | |
---|
195 | //adjust placement for proper vertical alignment |
---|
196 | if (column == [self columnWithIdentifier: @"Group"]) |
---|
197 | rect.size.height -= 1.0f; |
---|
198 | |
---|
199 | return rect; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | - (NSString *) outlineView: (NSOutlineView *) outlineView typeSelectStringForTableColumn: (NSTableColumn *) tableColumn item: (id) item |
---|
204 | { |
---|
205 | return [item isKindOfClass: [Torrent class]] ? [(Torrent *)item name] |
---|
206 | : [[self preparedCellAtColumn: [self columnWithIdentifier: @"Group"] row: [self rowForItem: item]] stringValue]; |
---|
207 | } |
---|
208 | |
---|
209 | - (NSString *) outlineView: (NSOutlineView *) outlineView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect |
---|
210 | tableColumn: (NSTableColumn *) column item: (id) item mouseLocation: (NSPoint) mouseLocation |
---|
211 | { |
---|
212 | NSString * ident = [column identifier]; |
---|
213 | if ([ident isEqualToString: @"DL"] || [ident isEqualToString: @"DL Image"]) |
---|
214 | return NSLocalizedString(@"Download speed", "Torrent table -> group row -> tooltip"); |
---|
215 | else if ([ident isEqualToString: @"UL"] || [ident isEqualToString: @"UL Image"]) |
---|
216 | return [fDefaults boolForKey: @"DisplayGroupRowRatio"] ? NSLocalizedString(@"Ratio", "Torrent table -> group row -> tooltip") |
---|
217 | : NSLocalizedString(@"Upload speed", "Torrent table -> group row -> tooltip"); |
---|
218 | else if (ident) |
---|
219 | { |
---|
220 | NSUInteger count = [[item torrents] count]; |
---|
221 | if (count == 1) |
---|
222 | return NSLocalizedString(@"1 transfer", "Torrent table -> group row -> tooltip"); |
---|
223 | else |
---|
224 | return [NSString stringWithFormat: NSLocalizedString(@"%@ transfers", "Torrent table -> group row -> tooltip"), |
---|
225 | [NSString formattedUInteger: count]]; |
---|
226 | } |
---|
227 | else |
---|
228 | return nil; |
---|
229 | } |
---|
230 | |
---|
231 | - (void) updateTrackingAreas |
---|
232 | { |
---|
233 | [super updateTrackingAreas]; |
---|
234 | [self removeTrackingAreas]; |
---|
235 | |
---|
236 | const NSRange rows = [self rowsInRect: [self visibleRect]]; |
---|
237 | if (rows.length == 0) |
---|
238 | return; |
---|
239 | |
---|
240 | NSPoint mouseLocation = [self convertPoint: [[self window] convertScreenToBase: [NSEvent mouseLocation]] fromView: nil]; |
---|
241 | for (NSUInteger row = rows.location; row < NSMaxRange(rows); row++) |
---|
242 | { |
---|
243 | if (![[self itemAtRow: row] isKindOfClass: [Torrent class]]) |
---|
244 | continue; |
---|
245 | |
---|
246 | NSDictionary * userInfo = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger: row] forKey: @"Row"]; |
---|
247 | TorrentCell * cell = (TorrentCell *)[self preparedCellAtColumn: -1 row: row]; |
---|
248 | [cell addTrackingAreasForView: self inRect: [self rectOfRow: row] withUserInfo: userInfo mouseLocation: mouseLocation]; |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | - (void) removeTrackingAreas |
---|
253 | { |
---|
254 | fMouseRow = -1; |
---|
255 | fMouseControlRow = -1; |
---|
256 | fMouseRevealRow = -1; |
---|
257 | fMouseActionRow = -1; |
---|
258 | |
---|
259 | for (NSTrackingArea * area in [self trackingAreas]) |
---|
260 | { |
---|
261 | if ([area owner] == self && [[area userInfo] objectForKey: @"Row"]) |
---|
262 | [self removeTrackingArea: area]; |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | - (void) setRowHover: (NSInteger) row |
---|
267 | { |
---|
268 | NSAssert([fDefaults boolForKey: @"SmallView"], @"cannot set a hover row when not in compact view"); |
---|
269 | |
---|
270 | fMouseRow = row; |
---|
271 | if (row >= 0) |
---|
272 | [self setNeedsDisplayInRect: [self rectOfRow: row]]; |
---|
273 | } |
---|
274 | |
---|
275 | - (void) setControlButtonHover: (NSInteger) row |
---|
276 | { |
---|
277 | fMouseControlRow = row; |
---|
278 | if (row >= 0) |
---|
279 | [self setNeedsDisplayInRect: [self rectOfRow: row]]; |
---|
280 | } |
---|
281 | |
---|
282 | - (void) setRevealButtonHover: (NSInteger) row |
---|
283 | { |
---|
284 | fMouseRevealRow = row; |
---|
285 | if (row >= 0) |
---|
286 | [self setNeedsDisplayInRect: [self rectOfRow: row]]; |
---|
287 | } |
---|
288 | |
---|
289 | - (void) setActionButtonHover: (NSInteger) row |
---|
290 | { |
---|
291 | fMouseActionRow = row; |
---|
292 | if (row >= 0) |
---|
293 | [self setNeedsDisplayInRect: [self rectOfRow: row]]; |
---|
294 | } |
---|
295 | |
---|
296 | - (void) mouseEntered: (NSEvent *) event |
---|
297 | { |
---|
298 | NSDictionary * dict = (NSDictionary *)[event userData]; |
---|
299 | |
---|
300 | NSNumber * row; |
---|
301 | if ((row = [dict objectForKey: @"Row"])) |
---|
302 | { |
---|
303 | NSInteger rowVal = [row integerValue]; |
---|
304 | NSString * type = [dict objectForKey: @"Type"]; |
---|
305 | if ([type isEqualToString: @"Action"]) |
---|
306 | fMouseActionRow = rowVal; |
---|
307 | else if ([type isEqualToString: @"Control"]) |
---|
308 | fMouseControlRow = rowVal; |
---|
309 | else if ([type isEqualToString: @"Reveal"]) |
---|
310 | fMouseRevealRow = rowVal; |
---|
311 | else |
---|
312 | { |
---|
313 | fMouseRow = rowVal; |
---|
314 | if (![fDefaults boolForKey: @"SmallView"]) |
---|
315 | return; |
---|
316 | } |
---|
317 | |
---|
318 | [self setNeedsDisplayInRect: [self rectOfRow: rowVal]]; |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | - (void) mouseExited: (NSEvent *) event |
---|
323 | { |
---|
324 | NSDictionary * dict = (NSDictionary *)[event userData]; |
---|
325 | |
---|
326 | NSNumber * row; |
---|
327 | if ((row = [dict objectForKey: @"Row"])) |
---|
328 | { |
---|
329 | NSString * type = [dict objectForKey: @"Type"]; |
---|
330 | if ([type isEqualToString: @"Action"]) |
---|
331 | fMouseActionRow = -1; |
---|
332 | else if ([type isEqualToString: @"Control"]) |
---|
333 | fMouseControlRow = -1; |
---|
334 | else if ([type isEqualToString: @"Reveal"]) |
---|
335 | fMouseRevealRow = -1; |
---|
336 | else |
---|
337 | { |
---|
338 | fMouseRow = -1; |
---|
339 | if (![fDefaults boolForKey: @"SmallView"]) |
---|
340 | return; |
---|
341 | } |
---|
342 | |
---|
343 | [self setNeedsDisplayInRect: [self rectOfRow: [row integerValue]]]; |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | - (void) outlineViewSelectionIsChanging: (NSNotification *) notification |
---|
348 | { |
---|
349 | if (fSelectedValues) |
---|
350 | [self selectValues: fSelectedValues]; |
---|
351 | } |
---|
352 | |
---|
353 | - (void) outlineViewItemDidExpand: (NSNotification *) notification |
---|
354 | { |
---|
355 | NSInteger value = [[[notification userInfo] objectForKey: @"NSObject"] groupIndex]; |
---|
356 | if (value < 0) |
---|
357 | value = MAX_GROUP; |
---|
358 | |
---|
359 | if ([fCollapsedGroups containsIndex: value]) |
---|
360 | { |
---|
361 | [fCollapsedGroups removeIndex: value]; |
---|
362 | [[NSNotificationCenter defaultCenter] postNotificationName: @"OutlineExpandCollapse" object: self]; |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | - (void) outlineViewItemDidCollapse: (NSNotification *) notification |
---|
367 | { |
---|
368 | NSInteger value = [[[notification userInfo] objectForKey: @"NSObject"] groupIndex]; |
---|
369 | if (value < 0) |
---|
370 | value = MAX_GROUP; |
---|
371 | |
---|
372 | [fCollapsedGroups addIndex: value]; |
---|
373 | [[NSNotificationCenter defaultCenter] postNotificationName: @"OutlineExpandCollapse" object: self]; |
---|
374 | } |
---|
375 | |
---|
376 | - (void) mouseDown: (NSEvent *) event |
---|
377 | { |
---|
378 | NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil]; |
---|
379 | const NSInteger row = [self rowAtPoint: point]; |
---|
380 | |
---|
381 | //check to toggle group status before anything else |
---|
382 | if ([self pointInGroupStatusRect: point]) |
---|
383 | { |
---|
384 | [fDefaults setBool: ![fDefaults boolForKey: @"DisplayGroupRowRatio"] forKey: @"DisplayGroupRowRatio"]; |
---|
385 | [self setGroupStatusColumns]; |
---|
386 | |
---|
387 | return; |
---|
388 | } |
---|
389 | |
---|
390 | const BOOL pushed = row != -1 && (fMouseActionRow == row || fMouseRevealRow == row || fMouseControlRow == row); |
---|
391 | |
---|
392 | //if pushing a button, don't change the selected rows |
---|
393 | if (pushed) |
---|
394 | fSelectedValues = [[self selectedValues] retain]; |
---|
395 | |
---|
396 | [super mouseDown: event]; |
---|
397 | |
---|
398 | [fSelectedValues release]; |
---|
399 | fSelectedValues = nil; |
---|
400 | |
---|
401 | //avoid weird behavior when showing menu by doing this after mouse down |
---|
402 | if (row != -1 && fMouseActionRow == row) |
---|
403 | { |
---|
404 | if (![NSApp isOnLionOrBetter]) |
---|
405 | { |
---|
406 | fActionPushedRow = row; |
---|
407 | [self setNeedsDisplayInRect: [self rectOfRow: row]]; //ensure button is pushed down |
---|
408 | } |
---|
409 | |
---|
410 | #warning maybe make appear on mouse down |
---|
411 | [self displayTorrentActionPopoverForEvent: event]; |
---|
412 | |
---|
413 | if (![NSApp isOnLionOrBetter]) |
---|
414 | { |
---|
415 | fActionPushedRow = -1; |
---|
416 | [self setNeedsDisplayInRect: [self rectOfRow: row]]; |
---|
417 | } |
---|
418 | } |
---|
419 | else if (!pushed && [event clickCount] == 2) //double click |
---|
420 | { |
---|
421 | id item = nil; |
---|
422 | if (row != -1) |
---|
423 | item = [self itemAtRow: row]; |
---|
424 | |
---|
425 | if (!item || [item isKindOfClass: [Torrent class]]) |
---|
426 | [fController showInfo: nil]; |
---|
427 | else |
---|
428 | { |
---|
429 | if ([self isItemExpanded: item]) |
---|
430 | [self collapseItem: item]; |
---|
431 | else |
---|
432 | [self expandItem: item]; |
---|
433 | } |
---|
434 | } |
---|
435 | else; |
---|
436 | } |
---|
437 | |
---|
438 | - (void) selectValues: (NSArray *) values |
---|
439 | { |
---|
440 | NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet]; |
---|
441 | |
---|
442 | for (id item in values) |
---|
443 | { |
---|
444 | if ([item isKindOfClass: [Torrent class]]) |
---|
445 | { |
---|
446 | const NSInteger index = [self rowForItem: item]; |
---|
447 | if (index != -1) |
---|
448 | [indexSet addIndex: index]; |
---|
449 | } |
---|
450 | else |
---|
451 | { |
---|
452 | const NSInteger group = [item groupIndex]; |
---|
453 | for (NSInteger i = 0; i < [self numberOfRows]; i++) |
---|
454 | { |
---|
455 | id tableItem = [self itemAtRow: i]; |
---|
456 | if ([tableItem isKindOfClass: [TorrentGroup class]] && group == [tableItem groupIndex]) |
---|
457 | { |
---|
458 | [indexSet addIndex: i]; |
---|
459 | break; |
---|
460 | } |
---|
461 | } |
---|
462 | } |
---|
463 | } |
---|
464 | |
---|
465 | [self selectRowIndexes: indexSet byExtendingSelection: NO]; |
---|
466 | } |
---|
467 | |
---|
468 | - (NSArray *) selectedValues |
---|
469 | { |
---|
470 | NSIndexSet * selectedIndexes = [self selectedRowIndexes]; |
---|
471 | NSMutableArray * values = [NSMutableArray arrayWithCapacity: [selectedIndexes count]]; |
---|
472 | |
---|
473 | for (NSUInteger i = [selectedIndexes firstIndex]; i != NSNotFound; i = [selectedIndexes indexGreaterThanIndex: i]) |
---|
474 | [values addObject: [self itemAtRow: i]]; |
---|
475 | |
---|
476 | return values; |
---|
477 | } |
---|
478 | |
---|
479 | - (NSArray *) selectedTorrents |
---|
480 | { |
---|
481 | NSIndexSet * selectedIndexes = [self selectedRowIndexes]; |
---|
482 | NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [selectedIndexes count]]; //take a shot at guessing capacity |
---|
483 | |
---|
484 | for (NSUInteger i = [selectedIndexes firstIndex]; i != NSNotFound; i = [selectedIndexes indexGreaterThanIndex: i]) |
---|
485 | { |
---|
486 | id item = [self itemAtRow: i]; |
---|
487 | if ([item isKindOfClass: [Torrent class]]) |
---|
488 | [torrents addObject: item]; |
---|
489 | else |
---|
490 | { |
---|
491 | NSArray * groupTorrents = [item torrents]; |
---|
492 | [torrents addObjectsFromArray: groupTorrents]; |
---|
493 | if ([self isItemExpanded: item]) |
---|
494 | i +=[groupTorrents count]; |
---|
495 | } |
---|
496 | } |
---|
497 | |
---|
498 | return torrents; |
---|
499 | } |
---|
500 | |
---|
501 | - (NSMenu *) menuForEvent: (NSEvent *) event |
---|
502 | { |
---|
503 | NSInteger row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]]; |
---|
504 | if (row >= 0) |
---|
505 | { |
---|
506 | if (![self isRowSelected: row]) |
---|
507 | [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO]; |
---|
508 | return fContextRow; |
---|
509 | } |
---|
510 | else |
---|
511 | { |
---|
512 | [self deselectAll: self]; |
---|
513 | return fContextNoRow; |
---|
514 | } |
---|
515 | } |
---|
516 | |
---|
517 | //make sure that the pause buttons become orange when holding down the option key |
---|
518 | - (void) flagsChanged: (NSEvent *) event |
---|
519 | { |
---|
520 | [self display]; |
---|
521 | [super flagsChanged: event]; |
---|
522 | } |
---|
523 | |
---|
524 | //option-command-f will focus the filter bar's search field |
---|
525 | - (void) keyDown: (NSEvent *) event |
---|
526 | { |
---|
527 | const unichar firstChar = [[event charactersIgnoringModifiers] characterAtIndex: 0]; |
---|
528 | |
---|
529 | if (firstChar == 'f' && [event modifierFlags] & NSAlternateKeyMask && [event modifierFlags] & NSCommandKeyMask) |
---|
530 | [fController focusFilterField]; |
---|
531 | else if (firstChar == ' ') |
---|
532 | [fController toggleQuickLook: nil]; |
---|
533 | else if ([event keyCode] == 53) //esc key |
---|
534 | [self deselectAll: nil]; |
---|
535 | else |
---|
536 | [super keyDown: event]; |
---|
537 | } |
---|
538 | |
---|
539 | - (NSRect) iconRectForRow: (NSInteger) row |
---|
540 | { |
---|
541 | return [fTorrentCell iconRectForBounds: [self rectOfRow: row]]; |
---|
542 | } |
---|
543 | |
---|
544 | #warning catch string urls? |
---|
545 | - (void) paste: (id) sender |
---|
546 | { |
---|
547 | NSURL * url; |
---|
548 | if ((url = [NSURL URLFromPasteboard: [NSPasteboard generalPasteboard]])) |
---|
549 | [fController openURL: [url absoluteString]]; |
---|
550 | } |
---|
551 | |
---|
552 | - (BOOL) validateMenuItem: (NSMenuItem *) menuItem |
---|
553 | { |
---|
554 | SEL action = [menuItem action]; |
---|
555 | |
---|
556 | if (action == @selector(paste:)) |
---|
557 | return [[[NSPasteboard generalPasteboard] types] containsObject: NSURLPboardType]; |
---|
558 | |
---|
559 | return YES; |
---|
560 | } |
---|
561 | |
---|
562 | - (void) toggleControlForTorrent: (Torrent *) torrent |
---|
563 | { |
---|
564 | if ([torrent isActive]) |
---|
565 | [fController stopTorrents: [NSArray arrayWithObject: torrent]]; |
---|
566 | else |
---|
567 | { |
---|
568 | if ([NSEvent modifierFlags] & NSAlternateKeyMask) |
---|
569 | [fController resumeTorrentsNoWait: [NSArray arrayWithObject: torrent]]; |
---|
570 | else if ([torrent waitingToStart]) |
---|
571 | [fController stopTorrents: [NSArray arrayWithObject: torrent]]; |
---|
572 | else |
---|
573 | [fController resumeTorrents: [NSArray arrayWithObject: torrent]]; |
---|
574 | } |
---|
575 | } |
---|
576 | |
---|
577 | - (void) displayTorrentActionPopoverForEvent: (NSEvent *) event |
---|
578 | { |
---|
579 | const NSInteger row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]]; |
---|
580 | if (row < 0) |
---|
581 | return; |
---|
582 | |
---|
583 | const NSRect rect = [fTorrentCell iconRectForBounds: [self rectOfRow: row]]; |
---|
584 | |
---|
585 | if ([NSApp isOnLionOrBetter]) |
---|
586 | { |
---|
587 | if (fActionPopoverShown) |
---|
588 | return; |
---|
589 | |
---|
590 | Torrent * torrent = [self itemAtRow: row]; |
---|
591 | |
---|
592 | NSPopover * popover = [[NSPopoverLion alloc] init]; |
---|
593 | [popover setBehavior: NSPopoverBehaviorTransient]; |
---|
594 | InfoOptionsViewController * infoViewController = [[InfoOptionsViewController alloc] init]; |
---|
595 | [popover setContentViewController: infoViewController]; |
---|
596 | [popover setDelegate: self]; |
---|
597 | |
---|
598 | [popover showRelativeToRect: rect ofView: self preferredEdge: NSMaxYEdge]; |
---|
599 | [infoViewController setInfoForTorrents: [NSArray arrayWithObject: torrent]]; |
---|
600 | [infoViewController updateInfo]; |
---|
601 | |
---|
602 | [infoViewController release]; |
---|
603 | [popover release]; |
---|
604 | } |
---|
605 | else |
---|
606 | { |
---|
607 | //update file action menu |
---|
608 | fMenuTorrent = [[self itemAtRow: row] retain]; |
---|
609 | |
---|
610 | //update global limit check |
---|
611 | [fGlobalLimitItem setState: [fMenuTorrent usesGlobalSpeedLimit] ? NSOnState : NSOffState]; |
---|
612 | |
---|
613 | //place menu below button |
---|
614 | NSPoint location = rect.origin; |
---|
615 | location.y += NSHeight(rect) + 5.0; |
---|
616 | |
---|
617 | location = [self convertPoint: location toView: self]; |
---|
618 | [fActionMenu popUpMenuPositioningItem: nil atLocation: location inView: self]; |
---|
619 | |
---|
620 | [fMenuTorrent release]; |
---|
621 | fMenuTorrent = nil; |
---|
622 | } |
---|
623 | } |
---|
624 | |
---|
625 | //don't show multiple popovers when clicking the gear button repeatedly |
---|
626 | - (void) popoverWillShow: (NSNotification *) notification |
---|
627 | { |
---|
628 | fActionPopoverShown = YES; |
---|
629 | } |
---|
630 | |
---|
631 | - (void) popoverWillClose: (NSNotification *) notification |
---|
632 | { |
---|
633 | fActionPopoverShown = NO; |
---|
634 | } |
---|
635 | |
---|
636 | //eliminate when Lion-only, along with all the menu item instance variables |
---|
637 | - (void) menuNeedsUpdate: (NSMenu *) menu |
---|
638 | { |
---|
639 | //this method seems to be called when it shouldn't be |
---|
640 | if (!fMenuTorrent || ![menu supermenu]) |
---|
641 | return; |
---|
642 | |
---|
643 | if (menu == fUploadMenu || menu == fDownloadMenu) |
---|
644 | { |
---|
645 | NSMenuItem * item; |
---|
646 | if ([menu numberOfItems] == 3) |
---|
647 | { |
---|
648 | const NSInteger speedLimitActionValue[] = { 0, 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500, |
---|
649 | 750, 1000, 1500, 2000, -1 }; |
---|
650 | |
---|
651 | for (NSInteger i = 0; speedLimitActionValue[i] != -1; i++) |
---|
652 | { |
---|
653 | item = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", |
---|
654 | "Action menu -> upload/download limit"), speedLimitActionValue[i]] action: @selector(setQuickLimit:) |
---|
655 | keyEquivalent: @""]; |
---|
656 | [item setTarget: self]; |
---|
657 | [item setRepresentedObject: [NSNumber numberWithInt: speedLimitActionValue[i]]]; |
---|
658 | [menu addItem: item]; |
---|
659 | [item release]; |
---|
660 | } |
---|
661 | } |
---|
662 | |
---|
663 | const BOOL upload = menu == fUploadMenu; |
---|
664 | const BOOL limit = [fMenuTorrent usesSpeedLimit: upload]; |
---|
665 | |
---|
666 | item = [menu itemWithTag: ACTION_MENU_LIMIT_TAG]; |
---|
667 | [item setState: limit ? NSOnState : NSOffState]; |
---|
668 | [item setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)", |
---|
669 | "torrent action menu -> upload/download limit"), [fMenuTorrent speedLimit: upload]]]; |
---|
670 | |
---|
671 | item = [menu itemWithTag: ACTION_MENU_UNLIMITED_TAG]; |
---|
672 | [item setState: !limit ? NSOnState : NSOffState]; |
---|
673 | } |
---|
674 | else if (menu == fRatioMenu) |
---|
675 | { |
---|
676 | NSMenuItem * item; |
---|
677 | if ([menu numberOfItems] == 4) |
---|
678 | { |
---|
679 | const float ratioLimitActionValue[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, -1.0 }; |
---|
680 | |
---|
681 | for (NSInteger i = 0; ratioLimitActionValue[i] != -1.0; i++) |
---|
682 | { |
---|
683 | item = [[NSMenuItem alloc] initWithTitle: [NSString localizedStringWithFormat: @"%.2f", ratioLimitActionValue[i]] |
---|
684 | action: @selector(setQuickRatio:) keyEquivalent: @""]; |
---|
685 | [item setTarget: self]; |
---|
686 | [item setRepresentedObject: [NSNumber numberWithFloat: ratioLimitActionValue[i]]]; |
---|
687 | [menu addItem: item]; |
---|
688 | [item release]; |
---|
689 | } |
---|
690 | } |
---|
691 | |
---|
692 | const tr_ratiolimit mode = [fMenuTorrent ratioSetting]; |
---|
693 | |
---|
694 | item = [menu itemWithTag: ACTION_MENU_LIMIT_TAG]; |
---|
695 | [item setState: mode == TR_RATIOLIMIT_SINGLE ? NSOnState : NSOffState]; |
---|
696 | [item setTitle: [NSString localizedStringWithFormat: NSLocalizedString(@"Stop at Ratio (%.2f)", |
---|
697 | "torrent action menu -> ratio stop"), [fMenuTorrent ratioLimit]]]; |
---|
698 | |
---|
699 | item = [menu itemWithTag: ACTION_MENU_UNLIMITED_TAG]; |
---|
700 | [item setState: mode == TR_RATIOLIMIT_UNLIMITED ? NSOnState : NSOffState]; |
---|
701 | |
---|
702 | item = [menu itemWithTag: ACTION_MENU_GLOBAL_TAG]; |
---|
703 | [item setState: mode == TR_RATIOLIMIT_GLOBAL ? NSOnState : NSOffState]; |
---|
704 | } |
---|
705 | else if (menu == fPriorityMenu) |
---|
706 | { |
---|
707 | const tr_priority_t priority = [fMenuTorrent priority]; |
---|
708 | |
---|
709 | NSMenuItem * item = [menu itemWithTag: ACTION_MENU_PRIORITY_HIGH_TAG]; |
---|
710 | [item setState: priority == TR_PRI_HIGH ? NSOnState : NSOffState]; |
---|
711 | |
---|
712 | item = [menu itemWithTag: ACTION_MENU_PRIORITY_NORMAL_TAG]; |
---|
713 | [item setState: priority == TR_PRI_NORMAL ? NSOnState : NSOffState]; |
---|
714 | |
---|
715 | item = [menu itemWithTag: ACTION_MENU_PRIORITY_LOW_TAG]; |
---|
716 | [item setState: priority == TR_PRI_LOW ? NSOnState : NSOffState]; |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | //the following methods might not be needed when Lion-only |
---|
721 | - (void) setQuickLimitMode: (id) sender |
---|
722 | { |
---|
723 | const BOOL limit = [sender tag] == ACTION_MENU_LIMIT_TAG; |
---|
724 | [fMenuTorrent setUseSpeedLimit: limit upload: [sender menu] == fUploadMenu]; |
---|
725 | |
---|
726 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil]; |
---|
727 | } |
---|
728 | |
---|
729 | - (void) setQuickLimit: (id) sender |
---|
730 | { |
---|
731 | const BOOL upload = [sender menu] == fUploadMenu; |
---|
732 | [fMenuTorrent setUseSpeedLimit: YES upload: upload]; |
---|
733 | [fMenuTorrent setSpeedLimit: [[sender representedObject] intValue] upload: upload]; |
---|
734 | |
---|
735 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil]; |
---|
736 | } |
---|
737 | |
---|
738 | - (void) setGlobalLimit: (id) sender |
---|
739 | { |
---|
740 | [fMenuTorrent setUseGlobalSpeedLimit: [(NSButton *)sender state] != NSOnState]; |
---|
741 | |
---|
742 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil]; |
---|
743 | } |
---|
744 | |
---|
745 | - (void) setQuickRatioMode: (id) sender |
---|
746 | { |
---|
747 | tr_ratiolimit mode; |
---|
748 | switch ([sender tag]) |
---|
749 | { |
---|
750 | case ACTION_MENU_UNLIMITED_TAG: |
---|
751 | mode = TR_RATIOLIMIT_UNLIMITED; |
---|
752 | break; |
---|
753 | case ACTION_MENU_LIMIT_TAG: |
---|
754 | mode = TR_RATIOLIMIT_SINGLE; |
---|
755 | break; |
---|
756 | case ACTION_MENU_GLOBAL_TAG: |
---|
757 | mode = TR_RATIOLIMIT_GLOBAL; |
---|
758 | break; |
---|
759 | default: |
---|
760 | return; |
---|
761 | } |
---|
762 | |
---|
763 | [fMenuTorrent setRatioSetting: mode]; |
---|
764 | |
---|
765 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil]; |
---|
766 | } |
---|
767 | |
---|
768 | - (void) setQuickRatio: (id) sender |
---|
769 | { |
---|
770 | [fMenuTorrent setRatioSetting: TR_RATIOLIMIT_SINGLE]; |
---|
771 | [fMenuTorrent setRatioLimit: [[sender representedObject] floatValue]]; |
---|
772 | |
---|
773 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil]; |
---|
774 | } |
---|
775 | |
---|
776 | - (void) setPriority: (id) sender |
---|
777 | { |
---|
778 | tr_priority_t priority; |
---|
779 | switch ([sender tag]) |
---|
780 | { |
---|
781 | case ACTION_MENU_PRIORITY_HIGH_TAG: |
---|
782 | priority = TR_PRI_HIGH; |
---|
783 | break; |
---|
784 | case ACTION_MENU_PRIORITY_NORMAL_TAG: |
---|
785 | priority = TR_PRI_NORMAL; |
---|
786 | break; |
---|
787 | case ACTION_MENU_PRIORITY_LOW_TAG: |
---|
788 | priority = TR_PRI_LOW; |
---|
789 | break; |
---|
790 | default: |
---|
791 | NSAssert1(NO, @"Unknown priority: %d", [sender tag]); |
---|
792 | } |
---|
793 | |
---|
794 | [fMenuTorrent setPriority: priority]; |
---|
795 | |
---|
796 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil]; |
---|
797 | } |
---|
798 | |
---|
799 | - (void) togglePiecesBar |
---|
800 | { |
---|
801 | //stop previous animation |
---|
802 | if (fPiecesBarAnimation) |
---|
803 | [fPiecesBarAnimation release]; |
---|
804 | |
---|
805 | NSMutableArray * progressMarks = [NSMutableArray arrayWithCapacity: 16]; |
---|
806 | for (NSAnimationProgress i = 0.0625; i <= 1.0; i += 0.0625) |
---|
807 | [progressMarks addObject: [NSNumber numberWithDouble: i]]; |
---|
808 | |
---|
809 | fPiecesBarAnimation = [[NSAnimation alloc] initWithDuration: TOGGLE_PROGRESS_SECONDS animationCurve: NSAnimationEaseIn]; |
---|
810 | [fPiecesBarAnimation setAnimationBlockingMode: NSAnimationNonblocking]; |
---|
811 | [fPiecesBarAnimation setProgressMarks: progressMarks]; |
---|
812 | [fPiecesBarAnimation setDelegate: self]; |
---|
813 | |
---|
814 | [fPiecesBarAnimation startAnimation]; |
---|
815 | } |
---|
816 | |
---|
817 | - (void) animationDidEnd: (NSAnimation *) animation |
---|
818 | { |
---|
819 | if (animation == fPiecesBarAnimation) |
---|
820 | { |
---|
821 | [fPiecesBarAnimation release]; |
---|
822 | fPiecesBarAnimation = nil; |
---|
823 | } |
---|
824 | } |
---|
825 | |
---|
826 | - (void) animation: (NSAnimation *) animation didReachProgressMark: (NSAnimationProgress) progress |
---|
827 | { |
---|
828 | if (animation == fPiecesBarAnimation) |
---|
829 | { |
---|
830 | if ([fDefaults boolForKey: @"PiecesBar"]) |
---|
831 | fPiecesBarPercent = progress; |
---|
832 | else |
---|
833 | fPiecesBarPercent = 1.0 - progress; |
---|
834 | |
---|
835 | [self reloadData]; |
---|
836 | } |
---|
837 | } |
---|
838 | |
---|
839 | - (CGFloat) piecesBarPercent |
---|
840 | { |
---|
841 | return fPiecesBarPercent; |
---|
842 | } |
---|
843 | |
---|
844 | @end |
---|
845 | |
---|
846 | @implementation TorrentTableView (Private) |
---|
847 | |
---|
848 | - (BOOL) pointInGroupStatusRect: (NSPoint) point |
---|
849 | { |
---|
850 | NSInteger row = [self rowAtPoint: point]; |
---|
851 | if (row < 0 || [[self itemAtRow: row] isKindOfClass: [Torrent class]]) |
---|
852 | return NO; |
---|
853 | |
---|
854 | NSString * ident = [[[self tableColumns] objectAtIndex: [self columnAtPoint: point]] identifier]; |
---|
855 | return [ident isEqualToString: @"UL"] || [ident isEqualToString: @"UL Image"] |
---|
856 | || [ident isEqualToString: @"DL"] || [ident isEqualToString: @"DL Image"]; |
---|
857 | } |
---|
858 | |
---|
859 | - (void) setGroupStatusColumns |
---|
860 | { |
---|
861 | const BOOL ratio = [fDefaults boolForKey: @"DisplayGroupRowRatio"]; |
---|
862 | |
---|
863 | [[self tableColumnWithIdentifier: @"DL"] setHidden: ratio]; |
---|
864 | [[self tableColumnWithIdentifier: @"DL Image"] setHidden: ratio]; |
---|
865 | } |
---|
866 | |
---|
867 | @end |
---|