transmission/macosx/TorrentTableView.m

540 lines
18 KiB
Mathematica
Raw Normal View History

2006-07-16 19:39:23 +00:00
/******************************************************************************
* $Id$
*
2007-04-03 02:22:25 +00:00
* Copyright (c) 2005-2007 Transmission authors and contributors
2006-07-16 19:39:23 +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"
#import "TorrentCell.h"
2006-07-16 19:39:23 +00:00
#import "Controller.h"
#import "Torrent.h"
#define BUTTON_TO_TOP_REGULAR 33.0
2006-07-16 19:39:23 +00:00
#define BUTTON_TO_TOP_SMALL 20.0
2007-08-08 01:03:34 +00:00
#define ACTION_BUTTON_TO_TOP 47.0
2006-07-16 19:39:23 +00:00
@interface TorrentTableView (Private)
- (NSRect) pauseRectForRow: (int) row;
- (NSRect) revealRectForRow: (int) row;
2007-08-08 01:03:34 +00:00
- (NSRect) actionRectForRow: (int) row;
2006-07-16 19:39:23 +00:00
- (BOOL) pointInIconRect: (NSPoint) point;
- (BOOL) pointInMinimalStatusRect: (NSPoint) point;
2006-07-16 19:39:23 +00:00
2007-08-08 01:03:34 +00:00
- (BOOL) pointInPauseRect: (NSPoint) point;
- (BOOL) pointInRevealRect: (NSPoint) point;
- (BOOL) pointInActionRect: (NSPoint) point;
2006-07-16 19:39:23 +00:00
@end
@implementation TorrentTableView
- (id) initWithCoder: (NSCoder *) decoder
{
if ((self = [super initWithCoder: decoder]))
{
fResumeOnIcon = [NSImage imageNamed: @"ResumeOn.png"];
fResumeOffIcon = [NSImage imageNamed: @"ResumeOff.png"];
fPauseOnIcon = [NSImage imageNamed: @"PauseOn.png"];
2007-08-08 01:03:34 +00:00
fPauseOffIcon = [NSImage imageNamed: @"PauseOff.png"];
2006-09-25 18:37:45 +00:00
fResumeNoWaitOnIcon = [NSImage imageNamed: @"ResumeNoWaitOn.png"];
fResumeNoWaitOffIcon = [NSImage imageNamed: @"ResumeNoWaitOff.png"];
2007-08-08 01:03:34 +00:00
2006-07-16 19:39:23 +00:00
fRevealOnIcon = [NSImage imageNamed: @"RevealOn.png"];
fRevealOffIcon = [NSImage imageNamed: @"RevealOff.png"];
2007-08-08 01:03:34 +00:00
fActionOnIcon = [NSImage imageNamed: @"ActionOn.png"];
fActionOffIcon = [NSImage imageNamed: @"ActionOff.png"];
2006-07-16 19:39:23 +00:00
fClickPoint = NSZeroPoint;
fClickIn = NO;
2006-07-16 19:39:23 +00:00
fKeyStrokes = [[NSMutableArray alloc] init];
2006-07-26 02:54:36 +00:00
fSmallStatusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSFont messageFontOfSize: 9.0], NSFontAttributeName, nil];
2006-07-16 19:39:23 +00:00
fDefaults = [NSUserDefaults standardUserDefaults];
[self setDelegate: self];
2006-07-16 19:39:23 +00:00
}
return self;
}
- (void) awakeFromNib
{
[fContextRow setTitle: @"Context"];
[fContextNoRow setTitle: @"Context"];
}
- (void) dealloc
{
[fKeyStrokes release];
2006-07-26 02:54:36 +00:00
[fSmallStatusAttributes release];
2007-08-08 13:36:27 +00:00
[fMenuTorrent release];
[super dealloc];
}
2006-07-16 19:39:23 +00:00
- (void) setTorrents: (NSArray *) torrents
{
fTorrents = torrents;
}
- (void) tableView: (NSTableView *) tableView willDisplayCell: (id) cell
forTableColumn: (NSTableColumn *) tableColumn row: (int) row
{
[cell setRepresentedObject: [fTorrents objectAtIndex: row]];
}
2006-07-16 19:39:23 +00:00
- (void) mouseDown: (NSEvent *) event
{
fClickPoint = [self convertPoint: [event locationInWindow] fromView: nil];
2007-08-08 01:03:34 +00:00
if ([self pointInActionRect: fClickPoint])
{
int row = [self rowAtPoint: fClickPoint];
[self setNeedsDisplayInRect: [self rectOfRow: row]]; //ensure button is pushed down
2007-08-08 01:03:34 +00:00
[self displayTorrentMenuForEvent: event];
fClickPoint = NSZeroPoint;
[self setNeedsDisplayInRect: [self rectOfRow: row]];
2007-08-08 01:03:34 +00:00
}
else if ([self pointInPauseRect: fClickPoint] || [self pointInRevealRect: fClickPoint])
{
fClickIn = YES;
[self setNeedsDisplayInRect: [self rectOfRow: [self rowAtPoint: fClickPoint]]];
}
else
2006-07-16 19:39:23 +00:00
{
2006-09-25 18:37:45 +00:00
if ([event modifierFlags] & NSAlternateKeyMask)
{
[fDefaults setBool: ![fDefaults boolForKey: @"UseAdvancedBar"] forKey: @"UseAdvancedBar"];
2006-09-25 18:37:45 +00:00
fClickPoint = NSZeroPoint;
[self reloadData];
2006-09-25 18:37:45 +00:00
}
else
{
if ([self pointInMinimalStatusRect: fClickPoint])
{
2007-08-11 01:48:06 +00:00
[fDefaults setBool: ![fDefaults boolForKey: @"SmallStatusRegular"] forKey: @"SmallStatusRegular"];
fClickPoint = NSZeroPoint;
[self reloadData];
}
2006-09-25 18:37:45 +00:00
[super mouseDown: event];
}
}
2006-07-16 19:39:23 +00:00
}
- (void) mouseUp: (NSEvent *) event
{
NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
int row = [self rowAtPoint: point], oldRow = [self rowAtPoint: fClickPoint];
BOOL sameRow = row == oldRow;
2006-07-16 19:39:23 +00:00
if (sameRow && [self pointInPauseRect: point] && [self pointInPauseRect: fClickPoint])
{
Torrent * torrent = [fTorrents objectAtIndex: row];
2006-09-25 18:37:45 +00:00
if ([torrent isActive])
2006-07-16 19:39:23 +00:00
[fController stopTorrents: [NSArray arrayWithObject: torrent]];
2006-09-25 18:37:45 +00:00
else if ([torrent isPaused])
{
if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask)
[fController resumeTorrentsNoWait: [NSArray arrayWithObject: torrent]];
else if ([torrent waitingToStart])
[fController stopTorrents: [NSArray arrayWithObject: torrent]];
else
[fController resumeTorrents: [NSArray arrayWithObject: torrent]];
}
else;
2006-07-16 19:39:23 +00:00
}
else if (sameRow && [self pointInRevealRect: point] && [self pointInRevealRect: fClickPoint])
[[fTorrents objectAtIndex: row] revealData];
else if ([event clickCount] == 2 && !NSEqualPoints(fClickPoint, NSZeroPoint))
2006-07-16 19:39:23 +00:00
{
if ([self pointInIconRect: point])
[[fTorrents objectAtIndex: row] revealData];
else if (![self pointInActionRect: point])
2006-07-16 19:39:23 +00:00
[fController showInfo: nil];
else;
2006-07-16 19:39:23 +00:00
}
else;
2006-09-25 18:37:45 +00:00
[super mouseUp: event];
2006-07-16 19:39:23 +00:00
fClickPoint = NSZeroPoint;
fClickIn = NO;
[self setNeedsDisplayInRect: [self rectOfRow: oldRow]];
2006-07-16 19:39:23 +00:00
}
- (void) mouseDragged: (NSEvent *) event
{
if (NSEqualPoints(fClickPoint, NSZeroPoint))
{
[super mouseDragged: event];
return;
}
NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
int oldRow = [self rowAtPoint: fClickPoint];
BOOL inRect;
if ([self pointInRevealRect: fClickPoint])
inRect = oldRow == [self rowAtPoint: point] && [self pointInRevealRect: point];
else if ([self pointInPauseRect: fClickPoint])
inRect = oldRow == [self rowAtPoint: point] && [self pointInPauseRect: point];
else
{
[super mouseDragged: event];
return;
}
if (inRect != fClickIn)
{
fClickIn = inRect;
[self setNeedsDisplayInRect: [self rectOfRow: oldRow]];
}
}
2006-07-16 19:39:23 +00:00
- (NSMenu *) menuForEvent: (NSEvent *) event
{
int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
if (row >= 0)
{
if (![self isRowSelected: row])
[self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
return fContextRow;
}
else
{
[self deselectAll: self];
return fContextNoRow;
}
}
- (void) keyDown: (NSEvent *) event
{
unichar newChar = [[event characters] characterAtIndex: 0];
2006-07-23 21:29:24 +00:00
if (newChar == ' ' || [[NSCharacterSet alphanumericCharacterSet] characterIsMember: newChar]
|| [[NSCharacterSet symbolCharacterSet] characterIsMember: newChar]
|| [[NSCharacterSet punctuationCharacterSet] characterIsMember: newChar])
{
if ([fKeyStrokes count] > 0 && [event timestamp] - [[fKeyStrokes lastObject] timestamp] > 1.0)
[fKeyStrokes removeAllObjects];
[fKeyStrokes addObject: event];
[self interpretKeyEvents: fKeyStrokes];
}
else
{
if ([fKeyStrokes count] > 0)
[fKeyStrokes removeAllObjects];
[super keyDown: event];
}
}
2006-09-25 18:37:45 +00:00
- (void) flagsChanged: (NSEvent *) event
{
[self display];
[super flagsChanged: event];
}
- (void) insertText: (NSString *) text
{
//sort torrents by name before finding closest match
2006-07-23 21:29:24 +00:00
NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES
selector: @selector(caseInsensitiveCompare:)] autorelease];
NSArray * descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, nil];
NSArray * tempTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors];
[descriptors release];
//select torrent closest to text that isn't before text alphabetically
int row;
NSEnumerator * enumerator = [tempTorrents objectEnumerator];
Torrent * torrent;
while ((torrent = [enumerator nextObject]))
if ([[torrent name] caseInsensitiveCompare: text] != NSOrderedAscending)
{
row = [fTorrents indexOfObject: torrent];
break;
}
//select last torrent alphabetically if no match found
if (!torrent)
row = [fTorrents indexOfObject: [tempTorrents lastObject]];
[self selectRow: row byExtendingSelection: NO];
[self scrollRowToVisible: row];
}
2007-08-08 01:03:34 +00:00
- (void) displayTorrentMenuForEvent: (NSEvent *) event
{
int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
if (row < 0)
return;
2007-08-08 13:36:27 +00:00
fMenuTorrent = [[fTorrents objectAtIndex: row] retain];
NSMenu * torrentMenu = [fMenuTorrent torrentMenu];
2007-08-08 01:03:34 +00:00
[torrentMenu setDelegate: self];
NSRect rect = [self actionRectForRow: row];
NSPoint location = rect.origin;
location.y += rect.size.height + 5.0;
location = [self convertPoint: location toView: nil];
NSEvent * newEvent = [NSEvent mouseEventWithType: [event type] location: location
modifierFlags: [event modifierFlags] timestamp: [event timestamp] windowNumber: [event windowNumber]
context: [event context] eventNumber: [event eventNumber] clickCount: [event clickCount] pressure: [event pressure]];
[NSMenu popUpContextMenu: torrentMenu withEvent: newEvent forView: self];
2007-08-08 13:36:27 +00:00
[fMenuTorrent release];
fMenuTorrent = nil;
2007-08-08 01:03:34 +00:00
}
- (void) menuNeedsUpdate: (NSMenu *) menu
{
2007-08-08 13:36:27 +00:00
//this method seems to be called when it shouldn't be
if (!fMenuTorrent)
return;
2007-08-08 01:03:34 +00:00
2007-08-08 13:36:27 +00:00
BOOL create = [menu numberOfItems] <= 0, folder;
2007-08-08 01:03:34 +00:00
NSMenu * supermenu = [menu supermenu];
NSArray * items;
if (supermenu)
items = [[[supermenu itemAtIndex: [supermenu indexOfItemWithSubmenu: menu]] representedObject] objectForKey: @"Children"];
else
2007-08-08 13:36:27 +00:00
items = [fMenuTorrent fileList];
2007-08-08 01:03:34 +00:00
NSEnumerator * enumerator = [items objectEnumerator];
NSDictionary * dict;
2007-08-08 13:36:27 +00:00
NSMenuItem * item;
2007-08-08 01:03:34 +00:00
while ((dict = [enumerator nextObject]))
{
NSString * name = [dict objectForKey: @"Name"];
folder = [[dict objectForKey: @"IsFolder"] boolValue];
if (create)
{
item = [[NSMenuItem alloc] initWithTitle: name action: NULL keyEquivalent: @""];
NSImage * icon;
if (!folder)
{
[item setAction: @selector(checkFile:)];
icon = [[dict objectForKey: @"Icon"] copy];
[icon setFlipped: NO];
}
else
{
NSMenu * itemMenu = [[NSMenu alloc] initWithTitle: name];
[itemMenu setAutoenablesItems: NO];
[item setSubmenu: itemMenu];
[itemMenu setDelegate: self];
[itemMenu release];
2007-08-08 01:03:34 +00:00
icon = [[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')] copy];
}
[item setRepresentedObject: dict];
[icon setScalesWhenResized: YES];
[icon setSize: NSMakeSize(16.0, 16.0)];
[item setImage: icon];
[icon release];
2007-08-08 13:36:27 +00:00
[menu addItem: item];
[item release];
2007-08-08 01:03:34 +00:00
}
else
item = [menu itemWithTitle: name];
if (!folder)
{
NSIndexSet * indexSet = [dict objectForKey: @"Indexes"];
2007-08-08 13:36:27 +00:00
[item setState: [fMenuTorrent checkForFiles: indexSet]];
[item setEnabled: [fMenuTorrent canChangeDownloadCheckForFiles: indexSet]];
2007-08-08 01:03:34 +00:00
}
}
}
- (void) checkFile: (id) sender
{
NSIndexSet * indexes = [[sender representedObject] objectForKey: @"Indexes"];
2007-08-08 13:36:27 +00:00
[fMenuTorrent setFileCheckState: [sender state] != NSOnState ? NSOnState : NSOffState forIndexes: indexes];
2007-08-08 01:03:34 +00:00
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil];
2007-08-08 01:03:34 +00:00
}
2007-08-08 17:57:50 +00:00
- (void) drawRow: (int) row clipRect: (NSRect) rect
2006-07-16 19:39:23 +00:00
{
2007-08-08 17:57:50 +00:00
[super drawRow: row clipRect: rect];
2007-08-08 14:35:40 +00:00
2007-08-08 17:57:50 +00:00
Torrent * torrent = [fTorrents objectAtIndex: row];
2007-08-08 14:35:40 +00:00
2007-08-08 17:57:50 +00:00
//pause/resume icon
NSImage * pauseImage = nil;
NSRect pauseRect = [self pauseRectForRow: row];
if ([torrent isActive])
2006-07-16 19:39:23 +00:00
{
2007-08-08 17:57:50 +00:00
if (![torrent isChecking])
pauseImage = fClickIn && NSPointInRect(fClickPoint, pauseRect) ? fPauseOnIcon : fPauseOffIcon;
2007-08-08 17:57:50 +00:00
}
else if ([torrent isPaused])
{
BOOL inPauseRect = fClickIn && NSPointInRect(fClickPoint, pauseRect);
2007-08-08 17:57:50 +00:00
if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask && [fDefaults boolForKey: @"Queue"])
pauseImage = inPauseRect ? fResumeNoWaitOnIcon : fResumeNoWaitOffIcon;
2007-08-08 17:57:50 +00:00
else if ([torrent waitingToStart])
pauseImage = inPauseRect ? fPauseOnIcon : fPauseOffIcon;
2007-08-08 17:57:50 +00:00
else
pauseImage = inPauseRect ? fResumeOnIcon : fResumeOffIcon;
2007-08-08 17:57:50 +00:00
}
else;
if (pauseImage)
[pauseImage compositeToPoint: NSMakePoint(pauseRect.origin.x, NSMaxY(pauseRect)) operation: NSCompositeSourceOver];
//reveal icon
NSRect revealRect = [self revealRectForRow: row];
NSImage * revealImage = fClickIn && NSPointInRect(fClickPoint, revealRect) ? fRevealOnIcon : fRevealOffIcon;
2007-08-08 17:57:50 +00:00
[revealImage compositeToPoint: NSMakePoint(revealRect.origin.x, NSMaxY(revealRect)) operation: NSCompositeSourceOver];
//action icon
if (![fDefaults boolForKey: @"SmallView"])
{
NSRect actionRect = [self actionRectForRow: row];
NSImage * actionImage = NSPointInRect(fClickPoint, actionRect) ? fActionOnIcon : fActionOffIcon;
[actionImage compositeToPoint: NSMakePoint(actionRect.origin.x, NSMaxY(actionRect)) operation: NSCompositeSourceOver];
2006-07-16 19:39:23 +00:00
}
}
@end
@implementation TorrentTableView (Private)
- (NSRect) pauseRectForRow: (int) row
{
if (row < 0)
return NSZeroRect;
2006-07-16 19:39:23 +00:00
NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
return NSMakeRect(NSMaxX(cellRect) - PADDING - BUTTON_WIDTH - PADDING - BUTTON_WIDTH,
2006-07-16 19:39:23 +00:00
cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
}
- (NSRect) revealRectForRow: (int) row
{
if (row < 0)
return NSZeroRect;
2006-07-16 19:39:23 +00:00
NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
return NSMakeRect(NSMaxX(cellRect) - PADDING - BUTTON_WIDTH,
2006-07-16 19:39:23 +00:00
cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
}
2007-08-08 01:03:34 +00:00
- (NSRect) actionRectForRow: (int) row
{
if (row < 0)
2007-08-08 01:03:34 +00:00
return NSZeroRect;
else if ([fDefaults boolForKey: @"SmallView"])
{
TorrentCell * cell = [[self tableColumnWithIdentifier: @"Torrent"] dataCell];
return [cell iconRectForBounds: [self frameOfCellAtColumn: 0 row: row]];
}
else;
2007-08-08 01:03:34 +00:00
NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
return NSMakeRect(cellRect.origin.x + PADDING +
([[[fTorrents objectAtIndex: row] iconFlipped] size].width - ACTION_BUTTON_WIDTH) * 0.5,
cellRect.origin.y + ACTION_BUTTON_TO_TOP, ACTION_BUTTON_WIDTH, ACTION_BUTTON_HEIGHT);
}
2006-07-16 19:39:23 +00:00
- (BOOL) pointInIconRect: (NSPoint) point
{
int row = [self rowAtPoint: point];
if (row < 0)
return NO;
2007-08-08 01:03:34 +00:00
TorrentCell * cell = [[self tableColumnWithIdentifier: @"Torrent"] dataCell];
return NSPointInRect(point, [cell iconRectForBounds: [self frameOfCellAtColumn: 0 row: row]]);
2006-07-16 19:39:23 +00:00
}
- (BOOL) pointInMinimalStatusRect: (NSPoint) point
{
int row = [self rowAtPoint: point];
if (row < 0 || ![fDefaults boolForKey: @"SmallView"])
return NO;
TorrentCell * cell = [[self tableColumnWithIdentifier: @"Torrent"] dataCell];
[cell setRepresentedObject: [fTorrents objectAtIndex: row]];
return NSPointInRect(point, [cell minimalStatusRectForBounds: [self frameOfCellAtColumn: 0 row: row]]);
}
2006-07-16 19:39:23 +00:00
- (BOOL) pointInPauseRect: (NSPoint) point
{
2007-08-08 01:03:34 +00:00
int row = [self rowAtPoint: point];
if (row < 0)
return NO;
return NSPointInRect(point, [self pauseRectForRow: row]);
2006-07-16 19:39:23 +00:00
}
- (BOOL) pointInRevealRect: (NSPoint) point
{
2007-08-08 01:03:34 +00:00
int row = [self rowAtPoint: point];
if (row < 0)
return NO;
return NSPointInRect(point, [self revealRectForRow: row]);
}
- (BOOL) pointInActionRect: (NSPoint) point
{
int row = [self rowAtPoint: point];
if (row < 0)
return NO;
return NSPointInRect(point, [self actionRectForRow: row]);
2006-07-16 19:39:23 +00:00
}
@end