2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2006 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* 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"
|
2006-07-25 00:59:14 +00:00
|
|
|
#import "TorrentCell.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
#import "Controller.h"
|
|
|
|
#import "Torrent.h"
|
|
|
|
|
2006-10-02 04:20:05 +00:00
|
|
|
#define BUTTON_TO_TOP_REGULAR 33.0
|
2006-07-16 19:39:23 +00:00
|
|
|
#define BUTTON_TO_TOP_SMALL 20.0
|
|
|
|
|
2006-10-02 04:20:05 +00:00
|
|
|
//button layout (from end of bar) is: padding, button, padding, button, padding
|
|
|
|
//change BUTTONS_TOTAL_WIDTH in .h when changing these values, add 2.0 to that value
|
2006-07-16 19:39:23 +00:00
|
|
|
#define BUTTON_WIDTH 14.0
|
2006-10-02 04:20:05 +00:00
|
|
|
#define PADDING 3.0
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
@interface TorrentTableView (Private)
|
|
|
|
|
|
|
|
- (NSRect) pauseRectForRow: (int) row;
|
|
|
|
- (NSRect) revealRectForRow: (int) row;
|
|
|
|
- (BOOL) pointInPauseRect: (NSPoint) point;
|
|
|
|
- (BOOL) pointInRevealRect: (NSPoint) point;
|
|
|
|
- (BOOL) pointInIconRect: (NSPoint) point;
|
2006-07-25 00:59:14 +00:00
|
|
|
- (BOOL) pointInMinimalStatusRect: (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"];
|
2006-09-25 18:37:45 +00:00
|
|
|
fResumeNoWaitOnIcon = [NSImage imageNamed: @"ResumeNoWaitOn.png"];
|
|
|
|
fResumeNoWaitOffIcon = [NSImage imageNamed: @"ResumeNoWaitOff.png"];
|
2006-07-16 19:39:23 +00:00
|
|
|
fPauseOffIcon = [NSImage imageNamed: @"PauseOff.png"];
|
|
|
|
fRevealOnIcon = [NSImage imageNamed: @"RevealOn.png"];
|
|
|
|
fRevealOffIcon = [NSImage imageNamed: @"RevealOff.png"];
|
|
|
|
|
|
|
|
fClickPoint = NSZeroPoint;
|
|
|
|
|
2006-07-23 20:53:31 +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];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) awakeFromNib
|
|
|
|
{
|
|
|
|
[fContextRow setTitle: @"Context"];
|
|
|
|
[fContextNoRow setTitle: @"Context"];
|
|
|
|
}
|
|
|
|
|
2006-07-23 20:53:31 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[fKeyStrokes release];
|
2006-07-26 02:54:36 +00:00
|
|
|
[fSmallStatusAttributes release];
|
2006-07-23 20:53:31 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
- (void) setTorrents: (NSArray *) torrents
|
|
|
|
{
|
|
|
|
fTorrents = torrents;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseDown: (NSEvent *) event
|
|
|
|
{
|
|
|
|
fClickPoint = [self convertPoint: [event locationInWindow] fromView: nil];
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
if (![self pointInPauseRect: fClickPoint] && ![self pointInRevealRect: fClickPoint])
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-09-25 18:37:45 +00:00
|
|
|
if ([event modifierFlags] & NSAlternateKeyMask)
|
|
|
|
{
|
2006-10-31 19:16:15 +00:00
|
|
|
[fDefaults setBool: ![fDefaults boolForKey: @"UseAdvancedBar"] forKey: @"UseAdvancedBar"];
|
2006-09-25 18:37:45 +00:00
|
|
|
fClickPoint = NSZeroPoint;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ([self pointInMinimalStatusRect: fClickPoint])
|
2006-12-15 16:56:55 +00:00
|
|
|
{
|
2006-09-25 18:37:45 +00:00
|
|
|
[(TorrentCell *)[[self tableColumnWithIdentifier: @"Torrent"] dataCell] toggleMinimalStatus];
|
2006-12-15 16:56:55 +00:00
|
|
|
fClickPoint = NSZeroPoint;
|
|
|
|
}
|
2006-07-25 00:59:14 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
[super mouseDown: event];
|
|
|
|
}
|
2006-07-25 00:59:14 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
else;
|
|
|
|
|
|
|
|
[self display];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseUp: (NSEvent *) event
|
|
|
|
{
|
|
|
|
NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
|
|
|
|
int row = [self rowAtPoint: point];
|
|
|
|
BOOL sameRow = row == [self rowAtPoint: fClickPoint];
|
|
|
|
|
|
|
|
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];
|
2006-12-15 16:56:55 +00:00
|
|
|
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
|
|
|
|
[fController showInfo: nil];
|
|
|
|
}
|
|
|
|
else;
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
[super mouseUp: event];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
fClickPoint = NSZeroPoint;
|
|
|
|
[self display];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-23 20:53:31 +00:00
|
|
|
- (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])
|
2006-07-23 20:53:31 +00:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2006-07-23 20:53:31 +00:00
|
|
|
- (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];
|
2006-07-23 20:53:31 +00:00
|
|
|
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
|
|
|
|
NSEnumerator * enumerator = [tempTorrents objectEnumerator];
|
|
|
|
Torrent * torrent;
|
|
|
|
while ((torrent = [enumerator nextObject]))
|
|
|
|
if ([[torrent name] caseInsensitiveCompare: text] != NSOrderedAscending)
|
|
|
|
{
|
|
|
|
[self selectRow: [fTorrents indexOfObject: torrent] byExtendingSelection: NO];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//select last torrent alphabetically if no match found
|
|
|
|
if (!torrent)
|
|
|
|
[self selectRow: [fTorrents indexOfObject: [tempTorrents lastObject]] byExtendingSelection: NO];
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
- (void) drawRect: (NSRect) r
|
|
|
|
{
|
|
|
|
NSRect rect;
|
|
|
|
Torrent * torrent;
|
|
|
|
NSImage * image;
|
2006-10-02 04:20:05 +00:00
|
|
|
NSRect buttonRect = NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
[super drawRect: r];
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < [fTorrents count]; i++)
|
|
|
|
{
|
|
|
|
torrent = [fTorrents objectAtIndex: i];
|
|
|
|
rect = [self pauseRectForRow: i];
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
if ([torrent isActive])
|
2006-07-16 19:39:23 +00:00
|
|
|
image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
|
2006-09-25 18:37:45 +00:00
|
|
|
else if ([torrent isPaused])
|
|
|
|
{
|
|
|
|
if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask && [fDefaults boolForKey: @"Queue"])
|
|
|
|
image = NSPointInRect(fClickPoint, rect) ? fResumeNoWaitOnIcon : fResumeNoWaitOffIcon;
|
|
|
|
else if ([torrent waitingToStart])
|
|
|
|
image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
|
|
|
|
else
|
|
|
|
image = NSPointInRect(fClickPoint, rect) ? fResumeOnIcon : fResumeOffIcon;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
else
|
|
|
|
image = nil;
|
|
|
|
|
|
|
|
if (image)
|
2006-10-02 04:20:05 +00:00
|
|
|
[image compositeToPoint: NSMakePoint(rect.origin.x, NSMaxY(rect)) fromRect: buttonRect
|
|
|
|
operation: NSCompositeSourceOver];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
rect = [self revealRectForRow: i];
|
|
|
|
image = NSPointInRect(fClickPoint, rect) ? fRevealOnIcon : fRevealOffIcon;
|
2006-10-02 04:20:05 +00:00
|
|
|
[image compositeToPoint: NSMakePoint(rect.origin.x, NSMaxY(rect)) fromRect: buttonRect
|
|
|
|
operation: NSCompositeSourceOver];
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation TorrentTableView (Private)
|
|
|
|
|
|
|
|
- (NSRect) pauseRectForRow: (int) row
|
|
|
|
{
|
|
|
|
NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
|
|
|
|
|
|
|
|
float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
|
|
|
|
|
2006-10-02 04:20:05 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
|
|
|
|
|
|
|
|
float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
|
|
|
|
|
2006-10-02 04:20:05 +00:00
|
|
|
return NSMakeRect(NSMaxX(cellRect) - PADDING - BUTTON_WIDTH,
|
2006-07-16 19:39:23 +00:00
|
|
|
cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) pointInIconRect: (NSPoint) point
|
|
|
|
{
|
|
|
|
int row = [self rowAtPoint: point];
|
|
|
|
if (row < 0)
|
|
|
|
return NO;
|
2006-07-25 00:59:14 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
|
|
|
|
NSSize iconSize = [fDefaults boolForKey: @"SmallView"] ? [[[fTorrents objectAtIndex: row] iconSmall] size]
|
|
|
|
: [[[fTorrents objectAtIndex: row] iconFlipped] size];
|
|
|
|
|
|
|
|
NSRect iconRect = NSMakeRect(cellRect.origin.x + 3.0, cellRect.origin.y
|
|
|
|
+ (cellRect.size.height - iconSize.height) * 0.5, iconSize.width, iconSize.height);
|
|
|
|
|
|
|
|
return NSPointInRect(point, iconRect);
|
|
|
|
}
|
|
|
|
|
2006-07-25 00:59:14 +00:00
|
|
|
- (BOOL) pointInMinimalStatusRect: (NSPoint) point
|
|
|
|
{
|
|
|
|
int row = [self rowAtPoint: point];
|
2006-07-26 02:42:07 +00:00
|
|
|
if (row < 0 || ![fDefaults boolForKey: @"SmallView"])
|
2006-07-25 00:59:14 +00:00
|
|
|
return NO;
|
|
|
|
|
2006-07-26 02:42:07 +00:00
|
|
|
Torrent * torrent = [fTorrents objectAtIndex: row];
|
|
|
|
NSString * statusString = ![fDefaults boolForKey: @"SmallStatusRegular"] && [torrent isActive]
|
2006-08-06 20:40:12 +00:00
|
|
|
? [torrent remainingTimeString] : [torrent shortStatusString];
|
2006-07-26 02:42:07 +00:00
|
|
|
|
2006-07-26 02:54:36 +00:00
|
|
|
float statusWidth = [statusString sizeWithAttributes: fSmallStatusAttributes].width + 3.0;
|
2006-07-26 02:42:07 +00:00
|
|
|
|
2006-07-25 00:59:14 +00:00
|
|
|
NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
|
2006-07-26 02:42:07 +00:00
|
|
|
NSRect statusRect = NSMakeRect(NSMaxX(cellRect) - statusWidth, cellRect.origin.y,
|
|
|
|
statusWidth, cellRect.size.height - BUTTON_WIDTH);
|
2006-07-25 00:59:14 +00:00
|
|
|
|
|
|
|
return NSPointInRect(point, statusRect);
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
- (BOOL) pointInPauseRect: (NSPoint) point
|
|
|
|
{
|
|
|
|
return NSPointInRect(point, [self pauseRectForRow: [self rowAtPoint: point]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) pointInRevealRect: (NSPoint) point
|
|
|
|
{
|
|
|
|
return NSPointInRect(point, [self revealRectForRow: [self rowAtPoint: point]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|