transmission/macosx/TorrentTableView.m

859 lines
28 KiB
Mathematica
Raw Normal View History

2007-09-16 01:02:06 +00:00
/******************************************************************************
* Copyright (c) 2005-2019 Transmission authors and contributors
2007-09-16 01:02:06 +00:00
*
* 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.
*****************************************************************************/
#import "TorrentTableView.h"
2009-10-11 02:22:38 +00:00
#import "Controller.h"
#import "FileListNode.h"
#import "InfoOptionsViewController.h"
2009-08-30 18:38:56 +00:00
#import "NSApplicationAdditions.h"
2010-11-21 14:45:06 +00:00
#import "NSStringAdditions.h"
2009-10-11 02:22:38 +00:00
#import "Torrent.h"
#import "TorrentCell.h"
#import "TorrentGroup.h"
2007-09-16 01:02:06 +00:00
#define MAX_GROUP 999999
//eliminate when Lion-only
2007-09-16 01:02:06 +00:00
#define ACTION_MENU_GLOBAL_TAG 101
#define ACTION_MENU_UNLIMITED_TAG 102
#define ACTION_MENU_LIMIT_TAG 103
#define ACTION_MENU_PRIORITY_HIGH_TAG 101
#define ACTION_MENU_PRIORITY_NORMAL_TAG 102
#define ACTION_MENU_PRIORITY_LOW_TAG 103
#define TOGGLE_PROGRESS_SECONDS 0.175
2007-09-16 01:02:06 +00:00
@interface TorrentTableView (Private)
- (BOOL) pointInGroupStatusRect: (NSPoint) point;
- (void) setGroupStatusColumns;
2007-09-16 01:02:06 +00:00
@end
@implementation TorrentTableView
- (id) initWithCoder: (NSCoder *) decoder
{
if ((self = [super initWithCoder: decoder]))
{
fDefaults = [NSUserDefaults standardUserDefaults];
fTorrentCell = [[TorrentCell alloc] init];
2008-02-07 21:26:04 +00:00
NSData * groupData = [fDefaults dataForKey: @"CollapsedGroups"];
if (groupData)
2008-02-10 16:56:13 +00:00
fCollapsedGroups = [[NSUnarchiver unarchiveObjectWithData: groupData] mutableCopy];
2008-02-07 21:26:04 +00:00
else
fCollapsedGroups = [[NSMutableIndexSet alloc] init];
fMouseRow = -1;
2008-01-15 04:45:57 +00:00
fMouseControlRow = -1;
fMouseRevealRow = -1;
fMouseActionRow = -1;
fActionPopoverShown = NO;
2007-09-16 01:02:06 +00:00
[self setDelegate: self];
2010-02-15 14:56:14 +00:00
fPiecesBarPercent = [fDefaults boolForKey: @"PiecesBar"] ? 1.0 : 0.0;
2007-09-16 01:02:06 +00:00
}
2007-09-16 01:02:06 +00:00
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
2007-09-16 01:02:06 +00:00
}
- (void) awakeFromNib
{
//set group columns to show ratio, needs to be in awakeFromNib to size columns correctly
[self setGroupStatusColumns];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(setNeedsDisplay) name: @"RefreshTorrentTable" object: nil];
}
2008-10-29 01:18:03 +00:00
- (BOOL) isGroupCollapsed: (NSInteger) value
{
2008-06-11 03:28:14 +00:00
if (value == -1)
value = MAX_GROUP;
return [fCollapsedGroups containsIndex: value];
}
2008-10-29 01:18:03 +00:00
- (void) removeCollapsedGroup: (NSInteger) value
{
2008-06-11 03:28:14 +00:00
if (value == -1)
value = MAX_GROUP;
[fCollapsedGroups removeIndex: value];
}
- (void) removeAllCollapsedGroups
{
[fCollapsedGroups removeAllIndexes];
}
2008-02-07 21:26:04 +00:00
- (void) saveCollapsedGroups
{
[fDefaults setObject: [NSArchiver archivedDataWithRootObject: fCollapsedGroups] forKey: @"CollapsedGroups"];
}
- (BOOL) outlineView: (NSOutlineView *) outlineView isGroupItem: (id) item
{
return ![item isKindOfClass: [Torrent class]];
}
- (CGFloat) outlineView: (NSOutlineView *) outlineView heightOfRowByItem: (id) item
{
return [item isKindOfClass: [Torrent class]] ? [self rowHeight] : GROUP_SEPARATOR_HEIGHT;
}
- (NSCell *) outlineView: (NSOutlineView *) outlineView dataCellForTableColumn: (NSTableColumn *) tableColumn item: (id) item
{
const BOOL group = ![item isKindOfClass: [Torrent class]];
2008-02-18 20:53:10 +00:00
if (!tableColumn)
return !group ? fTorrentCell : nil;
else
return group ? [tableColumn dataCellForRow: [self rowForItem: item]] : nil;
}
- (void) outlineView: (NSOutlineView *) outlineView willDisplayCell: (id) cell forTableColumn: (NSTableColumn *) tableColumn
item: (id) item
2007-09-16 01:02:06 +00:00
{
2008-02-18 20:53:10 +00:00
if ([item isKindOfClass: [Torrent class]])
{
if (!tableColumn)
{
[cell setRepresentedObject: item];
const NSInteger row = [self rowForItem: item];
[cell setHover: row == fMouseRow];
[cell setControlHover: row == fMouseControlRow];
[cell setRevealHover: row == fMouseRevealRow];
[cell setActionHover: row == fMouseActionRow];
}
2008-02-18 20:53:10 +00:00
}
2007-09-16 01:02:06 +00:00
}
- (NSRect) frameOfCellAtColumn: (NSInteger) column row: (NSInteger) row
{
2008-12-26 05:57:51 +00:00
if (column == -1)
return [self rectOfRow: row];
else
2008-02-19 02:10:20 +00:00
{
NSRect rect = [super frameOfCellAtColumn: column row: row];
2008-02-19 02:10:20 +00:00
//adjust placement for proper vertical alignment
if (column == [self columnWithIdentifier: @"Group"])
2008-10-29 01:18:03 +00:00
rect.size.height -= 1.0f;
2008-02-19 02:10:20 +00:00
return rect;
}
}
- (NSString *) outlineView: (NSOutlineView *) outlineView typeSelectStringForTableColumn: (NSTableColumn *) tableColumn item: (id) item
2008-01-23 21:33:54 +00:00
{
if ([item isKindOfClass: [Torrent class]])
return [(Torrent *)item name];
else
return [[self dataSource] outlineView:outlineView objectValueForTableColumn:[self tableColumnWithIdentifier:@"Group"] byItem:item];
2008-01-23 21:33:54 +00:00
}
- (NSString *) outlineView: (NSOutlineView *) outlineView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect tableColumn: (NSTableColumn *) column item: (id) item mouseLocation: (NSPoint) mouseLocation
2008-02-28 21:28:48 +00:00
{
NSString * ident = [column identifier];
if ([ident isEqualToString: @"DL"] || [ident isEqualToString: @"DL Image"])
return NSLocalizedString(@"Download speed", "Torrent table -> group row -> tooltip");
else if ([ident isEqualToString: @"UL"] || [ident isEqualToString: @"UL Image"])
return [fDefaults boolForKey: @"DisplayGroupRowRatio"] ? NSLocalizedString(@"Ratio", "Torrent table -> group row -> tooltip")
: NSLocalizedString(@"Upload speed", "Torrent table -> group row -> tooltip");
else if (ident)
{
2010-11-20 16:09:48 +00:00
NSUInteger count = [[item torrents] count];
if (count == 1)
return NSLocalizedString(@"1 transfer", "Torrent table -> group row -> tooltip");
else
2010-11-20 16:09:48 +00:00
return [NSString stringWithFormat: NSLocalizedString(@"%@ transfers", "Torrent table -> group row -> tooltip"),
[NSString formattedUInteger: count]];
}
2008-02-28 21:28:48 +00:00
else
return nil;
}
- (void) updateTrackingAreas
{
[super updateTrackingAreas];
[self removeTrackingAreas];
const NSRange rows = [self rowsInRect: [self visibleRect]];
if (rows.length == 0)
return;
NSPoint mouseLocation = [self convertPoint: [[self window] mouseLocationOutsideOfEventStream] fromView: nil];
2008-10-12 22:17:27 +00:00
for (NSUInteger row = rows.location; row < NSMaxRange(rows); row++)
{
if (![[self itemAtRow: row] isKindOfClass: [Torrent class]])
continue;
2017-07-08 15:02:00 +00:00
NSDictionary * userInfo = @{@"Row": @(row)};
TorrentCell * cell = (TorrentCell *)[self preparedCellAtColumn: -1 row: row];
[cell addTrackingAreasForView: self inRect: [self rectOfRow: row] withUserInfo: userInfo mouseLocation: mouseLocation];
}
}
- (void) removeTrackingAreas
{
fMouseRow = -1;
fMouseControlRow = -1;
fMouseRevealRow = -1;
fMouseActionRow = -1;
2008-12-26 15:11:37 +00:00
for (NSTrackingArea * area in [self trackingAreas])
{
2017-07-08 14:38:47 +00:00
if ([area owner] == self && [area userInfo][@"Row"])
[self removeTrackingArea: area];
}
}
- (void) setRowHover: (NSInteger) row
{
NSAssert([fDefaults boolForKey: @"SmallView"], @"cannot set a hover row when not in compact view");
fMouseRow = row;
if (row >= 0)
[self setNeedsDisplayInRect: [self rectOfRow: row]];
}
2008-10-29 01:18:03 +00:00
- (void) setControlButtonHover: (NSInteger) row
{
fMouseControlRow = row;
if (row >= 0)
[self setNeedsDisplayInRect: [self rectOfRow: row]];
}
2008-10-29 01:18:03 +00:00
- (void) setRevealButtonHover: (NSInteger) row
{
fMouseRevealRow = row;
if (row >= 0)
[self setNeedsDisplayInRect: [self rectOfRow: row]];
}
2008-10-29 01:18:03 +00:00
- (void) setActionButtonHover: (NSInteger) row
{
fMouseActionRow = row;
if (row >= 0)
[self setNeedsDisplayInRect: [self rectOfRow: row]];
}
- (void) mouseEntered: (NSEvent *) event
{
2008-01-15 04:45:57 +00:00
NSDictionary * dict = (NSDictionary *)[event userData];
NSNumber * row;
2017-07-08 14:38:47 +00:00
if ((row = dict[@"Row"]))
{
2009-12-23 02:02:11 +00:00
NSInteger rowVal = [row integerValue];
2017-07-08 14:38:47 +00:00
NSString * type = dict[@"Type"];
if ([type isEqualToString: @"Action"])
fMouseActionRow = rowVal;
else if ([type isEqualToString: @"Control"])
2008-01-15 04:45:57 +00:00
fMouseControlRow = rowVal;
else if ([type isEqualToString: @"Reveal"])
fMouseRevealRow = rowVal;
else
{
fMouseRow = rowVal;
if (![fDefaults boolForKey: @"SmallView"])
return;
}
2008-01-15 04:45:57 +00:00
[self setNeedsDisplayInRect: [self rectOfRow: rowVal]];
}
}
- (void) mouseExited: (NSEvent *) event
{
NSDictionary * dict = (NSDictionary *)[event userData];
NSNumber * row;
2017-07-08 14:38:47 +00:00
if ((row = dict[@"Row"]))
{
2017-07-08 14:38:47 +00:00
NSString * type = dict[@"Type"];
if ([type isEqualToString: @"Action"])
fMouseActionRow = -1;
else if ([type isEqualToString: @"Control"])
fMouseControlRow = -1;
else if ([type isEqualToString: @"Reveal"])
fMouseRevealRow = -1;
else
{
fMouseRow = -1;
if (![fDefaults boolForKey: @"SmallView"])
return;
}
2009-12-23 02:02:11 +00:00
[self setNeedsDisplayInRect: [self rectOfRow: [row integerValue]]];
}
}
- (void) outlineViewSelectionIsChanging: (NSNotification *) notification
{
#warning elliminate when view-based?
//if pushing a button, don't change the selected rows
if (fSelectedValues)
[self selectValues: fSelectedValues];
}
- (void) outlineViewItemDidExpand: (NSNotification *) notification
{
2017-07-08 14:38:47 +00:00
NSInteger value = [[notification userInfo][@"NSObject"] groupIndex];
if (value < 0)
value = MAX_GROUP;
if ([fCollapsedGroups containsIndex: value])
{
[fCollapsedGroups removeIndex: value];
[[NSNotificationCenter defaultCenter] postNotificationName: @"OutlineExpandCollapse" object: self];
}
}
- (void) outlineViewItemDidCollapse: (NSNotification *) notification
{
2017-07-08 14:38:47 +00:00
NSInteger value = [[notification userInfo][@"NSObject"] groupIndex];
if (value < 0)
value = MAX_GROUP;
[fCollapsedGroups addIndex: value];
[[NSNotificationCenter defaultCenter] postNotificationName: @"OutlineExpandCollapse" object: self];
}
2007-09-16 01:02:06 +00:00
- (void) mouseDown: (NSEvent *) event
{
NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
2008-11-17 03:20:15 +00:00
const NSInteger row = [self rowAtPoint: point];
//check to toggle group status before anything else
if ([self pointInGroupStatusRect: point])
{
[fDefaults setBool: ![fDefaults boolForKey: @"DisplayGroupRowRatio"] forKey: @"DisplayGroupRowRatio"];
[self setGroupStatusColumns];
return;
}
2008-12-26 05:57:51 +00:00
const BOOL pushed = row != -1 && (fMouseActionRow == row || fMouseRevealRow == row || fMouseControlRow == row);
//if pushing a button, don't change the selected rows
if (pushed)
2017-07-29 16:14:22 +00:00
fSelectedValues = [self selectedValues];
[super mouseDown: event];
fSelectedValues = nil;
//avoid weird behavior when showing menu by doing this after mouse down
2008-12-26 05:57:51 +00:00
if (row != -1 && fMouseActionRow == row)
2007-09-16 01:02:06 +00:00
{
#warning maybe make appear on mouse down
[self displayTorrentActionPopoverForEvent: event];
2007-09-16 01:02:06 +00:00
}
else if (!pushed && [event clickCount] == 2) //double click
{
id item = nil;
if (row != -1)
item = [self itemAtRow: row];
if (!item || [item isKindOfClass: [Torrent class]])
[fController showInfo: nil];
else
{
if ([self isItemExpanded: item])
[self collapseItem: item];
else
[self expandItem: item];
}
}
else;
2007-09-16 01:02:06 +00:00
}
- (void) selectValues: (NSArray *) values
{
NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet];
2008-12-26 15:11:37 +00:00
for (id item in values)
{
if ([item isKindOfClass: [Torrent class]])
{
2009-12-23 02:02:11 +00:00
const NSInteger index = [self rowForItem: item];
if (index != -1)
[indexSet addIndex: index];
}
else
{
2009-12-23 02:02:11 +00:00
const NSInteger group = [item groupIndex];
2008-10-12 22:17:27 +00:00
for (NSInteger i = 0; i < [self numberOfRows]; i++)
{
id tableItem = [self itemAtRow: i];
2009-12-23 02:02:11 +00:00
if ([tableItem isKindOfClass: [TorrentGroup class]] && group == [tableItem groupIndex])
{
[indexSet addIndex: i];
break;
}
}
}
}
[self selectRowIndexes: indexSet byExtendingSelection: NO];
}
- (NSArray *) selectedValues
{
NSIndexSet * selectedIndexes = [self selectedRowIndexes];
NSMutableArray * values = [NSMutableArray arrayWithCapacity: [selectedIndexes count]];
2008-10-12 22:17:27 +00:00
for (NSUInteger i = [selectedIndexes firstIndex]; i != NSNotFound; i = [selectedIndexes indexGreaterThanIndex: i])
[values addObject: [self itemAtRow: i]];
return values;
}
- (NSArray *) selectedTorrents
{
NSIndexSet * selectedIndexes = [self selectedRowIndexes];
2008-06-05 02:16:33 +00:00
NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [selectedIndexes count]]; //take a shot at guessing capacity
2008-10-12 22:17:27 +00:00
for (NSUInteger i = [selectedIndexes firstIndex]; i != NSNotFound; i = [selectedIndexes indexGreaterThanIndex: i])
{
id item = [self itemAtRow: i];
if ([item isKindOfClass: [Torrent class]])
2008-06-05 02:16:33 +00:00
[torrents addObject: item];
else
{
NSArray * groupTorrents = [item torrents];
2008-06-05 02:16:33 +00:00
[torrents addObjectsFromArray: groupTorrents];
if ([self isItemExpanded: item])
i +=[groupTorrents count];
}
}
return torrents;
}
2007-09-16 01:02:06 +00:00
- (NSMenu *) menuForEvent: (NSEvent *) event
{
2008-10-29 01:18:03 +00:00
NSInteger row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
2007-09-16 01:02:06 +00:00
if (row >= 0)
{
if (![self isRowSelected: row])
[self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
return fContextRow;
}
else
{
[self deselectAll: self];
return fContextNoRow;
}
}
//make sure that the pause buttons become orange when holding down the option key
- (void) flagsChanged: (NSEvent *) event
{
[self display];
[super flagsChanged: event];
}
//option-command-f will focus the filter bar's search field
- (void) keyDown: (NSEvent *) event
{
2008-11-04 00:34:13 +00:00
const unichar firstChar = [[event charactersIgnoringModifiers] characterAtIndex: 0];
2008-06-17 17:17:15 +00:00
if (firstChar == 'f' && [event modifierFlags] & NSAlternateKeyMask && [event modifierFlags] & NSCommandKeyMask)
[fController focusFilterField];
else if (firstChar == ' ')
[fController toggleQuickLook: nil];
else if ([event keyCode] == 53) //esc key
[self deselectAll: nil];
else
[super keyDown: event];
}
2008-10-29 01:18:03 +00:00
- (NSRect) iconRectForRow: (NSInteger) row
{
return [fTorrentCell iconRectForBounds: [self rectOfRow: row]];
}
- (void) paste: (id) sender
{
NSURL * url;
if ((url = [NSURL URLFromPasteboard: [NSPasteboard generalPasteboard]]))
[fController openURL: [url absoluteString]];
else
{
2017-07-08 08:23:05 +00:00
NSArray * items = [[NSPasteboard generalPasteboard] readObjectsForClasses: @[[NSString class]] options: nil];
if (items)
{
NSDataDetector * detector = [NSDataDetector dataDetectorWithTypes: NSTextCheckingTypeLink error: nil];
2017-07-29 16:14:22 +00:00
for (__strong NSString * pbItem in items)
{
pbItem = [pbItem stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([pbItem rangeOfString: @"magnet:" options: (NSAnchoredSearch | NSCaseInsensitiveSearch)].location != NSNotFound)
[fController openURL: pbItem];
else
{
#warning only accept full text?
for (NSTextCheckingResult * result in [detector matchesInString: pbItem options: 0 range: NSMakeRange(0, [pbItem length])])
[fController openURL: [[result URL] absoluteString]];
}
}
}
}
}
- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
{
SEL action = [menuItem action];
if (action == @selector(paste:))
{
if ([[[NSPasteboard generalPasteboard] types] containsObject: NSURLPboardType])
return YES;
2017-07-08 08:23:05 +00:00
NSArray * items = [[NSPasteboard generalPasteboard] readObjectsForClasses: @[[NSString class]] options: nil];
if (items)
{
NSDataDetector * detector = [NSDataDetector dataDetectorWithTypes: NSTextCheckingTypeLink error: nil];
2017-07-29 16:14:22 +00:00
for (__strong NSString * pbItem in items)
{
pbItem = [pbItem stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (([pbItem rangeOfString: @"magnet:" options: (NSAnchoredSearch | NSCaseInsensitiveSearch)].location != NSNotFound)
|| [detector firstMatchInString: pbItem options: 0 range: NSMakeRange(0, [pbItem length])])
return YES;
}
}
return NO;
}
return YES;
}
- (void) toggleControlForTorrent: (Torrent *) torrent
{
if ([torrent isActive])
2017-07-08 08:23:05 +00:00
[fController stopTorrents: @[torrent]];
else
{
if ([NSEvent modifierFlags] & NSAlternateKeyMask)
2017-07-08 08:23:05 +00:00
[fController resumeTorrentsNoWait: @[torrent]];
else if ([torrent waitingToStart])
2017-07-08 08:23:05 +00:00
[fController stopTorrents: @[torrent]];
else
2017-07-08 08:23:05 +00:00
[fController resumeTorrents: @[torrent]];
}
2007-12-17 16:06:20 +00:00
}
- (void) displayTorrentActionPopoverForEvent: (NSEvent *) event
2007-09-16 01:02:06 +00:00
{
const NSInteger row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
2007-09-16 01:02:06 +00:00
if (row < 0)
return;
const NSRect rect = [fTorrentCell iconRectForBounds: [self rectOfRow: row]];
if (fActionPopoverShown)
return;
Torrent * torrent = [self itemAtRow: row];
NSPopover * popover = [[NSPopover alloc] init];
[popover setBehavior: NSPopoverBehaviorTransient];
InfoOptionsViewController * infoViewController = [[InfoOptionsViewController alloc] init];
[popover setContentViewController: infoViewController];
[popover setDelegate: self];
[popover showRelativeToRect: rect ofView: self preferredEdge: NSMaxYEdge];
2017-07-08 08:23:05 +00:00
[infoViewController setInfoForTorrents: @[torrent]];
[infoViewController updateInfo];
2007-09-16 01:02:06 +00:00
}
//don't show multiple popovers when clicking the gear button repeatedly
- (void) popoverWillShow: (NSNotification *) notification
{
fActionPopoverShown = YES;
}
- (void) popoverWillClose: (NSNotification *) notification
{
fActionPopoverShown = NO;
}
//eliminate when Lion-only, along with all the menu item instance variables
2007-09-16 01:02:06 +00:00
- (void) menuNeedsUpdate: (NSMenu *) menu
{
//this method seems to be called when it shouldn't be
if (!fMenuTorrent || ![menu supermenu])
return;
2007-09-16 01:02:06 +00:00
if (menu == fUploadMenu || menu == fDownloadMenu)
{
NSMenuItem * item;
if ([menu numberOfItems] == 3)
{
const NSInteger speedLimitActionValue[] = { 0, 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500,
750, 1000, 1500, 2000, -1 };
2008-10-12 22:17:27 +00:00
for (NSInteger i = 0; speedLimitActionValue[i] != -1; i++)
{
item = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: NSLocalizedString(@"%d KB/s",
"Action menu -> upload/download limit"), speedLimitActionValue[i]] action: @selector(setQuickLimit:)
keyEquivalent: @""];
[item setTarget: self];
2017-07-07 10:34:27 +00:00
[item setRepresentedObject: @(speedLimitActionValue[i])];
[menu addItem: item];
}
}
const BOOL upload = menu == fUploadMenu;
const BOOL limit = [fMenuTorrent usesSpeedLimit: upload];
item = [menu itemWithTag: ACTION_MENU_LIMIT_TAG];
[item setState: limit ? NSOnState : NSOffState];
2007-09-16 01:02:06 +00:00
[item setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)",
"torrent action menu -> upload/download limit"), [fMenuTorrent speedLimit: upload]]];
2007-09-16 01:02:06 +00:00
item = [menu itemWithTag: ACTION_MENU_UNLIMITED_TAG];
[item setState: !limit ? NSOnState : NSOffState];
2007-09-16 01:02:06 +00:00
}
else if (menu == fRatioMenu)
{
NSMenuItem * item;
if ([menu numberOfItems] == 4)
{
2010-11-14 15:16:39 +00:00
const float ratioLimitActionValue[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, -1.0 };
2010-02-15 14:56:14 +00:00
for (NSInteger i = 0; ratioLimitActionValue[i] != -1.0; i++)
{
2008-03-24 00:06:54 +00:00
item = [[NSMenuItem alloc] initWithTitle: [NSString localizedStringWithFormat: @"%.2f", ratioLimitActionValue[i]]
2008-01-10 15:53:02 +00:00
action: @selector(setQuickRatio:) keyEquivalent: @""];
[item setTarget: self];
2017-07-07 10:34:27 +00:00
[item setRepresentedObject: @(ratioLimitActionValue[i])];
[menu addItem: item];
}
}
const tr_ratiolimit mode = [fMenuTorrent ratioSetting];
item = [menu itemWithTag: ACTION_MENU_LIMIT_TAG];
[item setState: mode == TR_RATIOLIMIT_SINGLE ? NSOnState : NSOffState];
2008-03-24 00:06:54 +00:00
[item setTitle: [NSString localizedStringWithFormat: NSLocalizedString(@"Stop at Ratio (%.2f)",
"torrent action menu -> ratio stop"), [fMenuTorrent ratioLimit]]];
2007-09-16 01:02:06 +00:00
item = [menu itemWithTag: ACTION_MENU_UNLIMITED_TAG];
[item setState: mode == TR_RATIOLIMIT_UNLIMITED ? NSOnState : NSOffState];
2007-09-16 01:02:06 +00:00
item = [menu itemWithTag: ACTION_MENU_GLOBAL_TAG];
[item setState: mode == TR_RATIOLIMIT_GLOBAL ? NSOnState : NSOffState];
2007-09-16 01:02:06 +00:00
}
else if (menu == fPriorityMenu)
{
const tr_priority_t priority = [fMenuTorrent priority];
NSMenuItem * item = [menu itemWithTag: ACTION_MENU_PRIORITY_HIGH_TAG];
[item setState: priority == TR_PRI_HIGH ? NSOnState : NSOffState];
item = [menu itemWithTag: ACTION_MENU_PRIORITY_NORMAL_TAG];
[item setState: priority == TR_PRI_NORMAL ? NSOnState : NSOffState];
item = [menu itemWithTag: ACTION_MENU_PRIORITY_LOW_TAG];
[item setState: priority == TR_PRI_LOW ? NSOnState : NSOffState];
}
2007-09-16 01:02:06 +00:00
}
//the following methods might not be needed when Lion-only
2007-09-16 01:02:06 +00:00
- (void) setQuickLimitMode: (id) sender
{
const BOOL limit = [sender tag] == ACTION_MENU_LIMIT_TAG;
[fMenuTorrent setUseSpeedLimit: limit upload: [sender menu] == fUploadMenu];
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
2007-09-16 01:02:06 +00:00
}
- (void) setQuickLimit: (id) sender
{
2009-02-17 04:00:53 +00:00
const BOOL upload = [sender menu] == fUploadMenu;
[fMenuTorrent setUseSpeedLimit: YES upload: upload];
[fMenuTorrent setSpeedLimit: [[sender representedObject] intValue] upload: upload];
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
2007-09-16 01:02:06 +00:00
}
- (void) setGlobalLimit: (id) sender
{
[fMenuTorrent setUseGlobalSpeedLimit: [(NSButton *)sender state] != NSOnState];
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
}
2007-09-16 01:02:06 +00:00
- (void) setQuickRatioMode: (id) sender
{
tr_ratiolimit mode;
2007-10-11 12:06:46 +00:00
switch ([sender tag])
{
case ACTION_MENU_UNLIMITED_TAG:
mode = TR_RATIOLIMIT_UNLIMITED;
2007-10-11 12:06:46 +00:00
break;
case ACTION_MENU_LIMIT_TAG:
mode = TR_RATIOLIMIT_SINGLE;
2007-10-11 12:06:46 +00:00
break;
case ACTION_MENU_GLOBAL_TAG:
mode = TR_RATIOLIMIT_GLOBAL;
2007-10-11 12:06:46 +00:00
break;
default:
return;
}
2007-09-16 01:02:06 +00:00
[fMenuTorrent setRatioSetting: mode];
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
2007-09-16 01:02:06 +00:00
}
- (void) setQuickRatio: (id) sender
{
[fMenuTorrent setRatioSetting: TR_RATIOLIMIT_SINGLE];
[fMenuTorrent setRatioLimit: [[sender representedObject] floatValue]];
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
2007-09-16 01:02:06 +00:00
}
- (void) setPriority: (id) sender
{
tr_priority_t priority;
switch ([sender tag])
{
case ACTION_MENU_PRIORITY_HIGH_TAG:
priority = TR_PRI_HIGH;
break;
case ACTION_MENU_PRIORITY_NORMAL_TAG:
priority = TR_PRI_NORMAL;
break;
case ACTION_MENU_PRIORITY_LOW_TAG:
priority = TR_PRI_LOW;
break;
default:
NSAssert1(NO, @"Unknown priority: %ld", [sender tag]);
priority = TR_PRI_NORMAL;
}
[fMenuTorrent setPriority: priority];
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
}
- (void) togglePiecesBar
{
2008-06-10 18:08:57 +00:00
NSMutableArray * progressMarks = [NSMutableArray arrayWithCapacity: 16];
for (NSAnimationProgress i = 0.0625; i <= 1.0; i += 0.0625)
2017-07-07 10:34:27 +00:00
[progressMarks addObject: @(i)];
2012-03-13 03:49:30 +00:00
//this stops a previous animation
fPiecesBarAnimation = [[NSAnimation alloc] initWithDuration: TOGGLE_PROGRESS_SECONDS animationCurve: NSAnimationEaseIn];
[fPiecesBarAnimation setAnimationBlockingMode: NSAnimationNonblocking];
[fPiecesBarAnimation setProgressMarks: progressMarks];
[fPiecesBarAnimation setDelegate: self];
[fPiecesBarAnimation startAnimation];
}
- (void) animationDidEnd: (NSAnimation *) animation
{
if (animation == fPiecesBarAnimation)
{
fPiecesBarAnimation = nil;
}
}
- (void) animation: (NSAnimation *) animation didReachProgressMark: (NSAnimationProgress) progress
{
if (animation == fPiecesBarAnimation)
{
if ([fDefaults boolForKey: @"PiecesBar"])
fPiecesBarPercent = progress;
else
2010-02-15 14:56:14 +00:00
fPiecesBarPercent = 1.0 - progress;
[self setNeedsDisplay: YES];
}
}
2008-10-29 01:18:03 +00:00
- (CGFloat) piecesBarPercent
{
return fPiecesBarPercent;
}
- (void) selectAndScrollToRow: (NSInteger) row
{
NSParameterAssert(row >= 0);
NSParameterAssert(row < [self numberOfRows]);
[self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
const NSRect rowRect = [self rectOfRow: row];
const NSRect viewRect = [[self superview] frame];
NSPoint scrollOrigin = rowRect.origin;
scrollOrigin.y += (rowRect.size.height - viewRect.size.height) / 2;
if (scrollOrigin.y < 0)
scrollOrigin.y = 0;
[[[self superview] animator] setBoundsOrigin: scrollOrigin];
}
2007-09-16 01:02:06 +00:00
@end
@implementation TorrentTableView (Private)
- (BOOL) pointInGroupStatusRect: (NSPoint) point
{
2008-10-29 01:18:03 +00:00
NSInteger row = [self rowAtPoint: point];
if (row < 0 || [[self itemAtRow: row] isKindOfClass: [Torrent class]])
return NO;
2017-07-08 09:16:01 +00:00
NSString * ident = [[self tableColumns][[self columnAtPoint: point]] identifier];
return [ident isEqualToString: @"UL"] || [ident isEqualToString: @"UL Image"]
|| [ident isEqualToString: @"DL"] || [ident isEqualToString: @"DL Image"];
}
- (void) setGroupStatusColumns
{
const BOOL ratio = [fDefaults boolForKey: @"DisplayGroupRowRatio"];
[[self tableColumnWithIdentifier: @"DL"] setHidden: ratio];
[[self tableColumnWithIdentifier: @"DL Image"] setHidden: ratio];
}
2007-09-16 01:02:06 +00:00
@end