1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-02-25 07:22:38 +00:00
transmission/macosx/NameCell.m

169 lines
5.4 KiB
Mathematica
Raw Normal View History

2006-01-12 17:43:21 +00:00
/******************************************************************************
* Copyright (c) 2005 Eric Petit
*
* 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 "NameCell.h"
#import "StringAdditions.h"
#import "Utils.h"
2006-01-12 17:43:21 +00:00
@implementation NameCell
2006-02-09 12:58:10 +00:00
- (id) init
{
2006-02-10 06:14:36 +00:00
if ((self = [super init]))
fIcons = [[NSMutableDictionary alloc] initWithCapacity: 10];
2006-02-09 12:58:10 +00:00
return self;
}
2006-02-10 06:14:36 +00:00
- (void) dealloc
2006-02-09 12:58:10 +00:00
{
2006-02-10 06:14:36 +00:00
[fIcons release];
[super dealloc];
}
2006-02-09 12:58:10 +00:00
2006-02-10 06:14:36 +00:00
- (NSImage *) iconForFileType: (NSString *) type
{
NSImage * icon;
if (!(icon = [fIcons objectForKey: type]))
2006-02-09 12:58:10 +00:00
{
/* Unknown file type, get its icon and cache it */
icon = [[NSWorkspace sharedWorkspace] iconForFileType: type];
[icon setFlipped: YES];
2006-02-10 06:14:36 +00:00
[fIcons setObject: icon forKey: type];
2006-02-09 12:58:10 +00:00
}
2006-02-10 06:14:36 +00:00
return icon;
2006-02-09 12:58:10 +00:00
}
2006-01-12 18:53:05 +00:00
- (void) setStat: (tr_stat_t *) stat whiteText: (BOOL) w
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:53:05 +00:00
fWhiteText = w;
2006-01-12 18:29:20 +00:00
fNameString = [NSString stringWithUTF8String: stat->info.name];
fSizeString = [NSString stringWithFormat: @" (%@)",
[NSString stringForFileSize: stat->info.totalSize]];
2006-02-09 12:58:10 +00:00
fCurrentIcon = [self iconForFileType: stat->info.fileCount > 1 ?
NSFileTypeForHFSTypeCode('fldr') : [fNameString pathExtension]];
2006-01-12 18:29:20 +00:00
fTimeString = @"";
fPeersString = @"";
2006-01-12 17:43:21 +00:00
2006-01-12 18:29:20 +00:00
if( stat->status & TR_STATUS_PAUSE )
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:29:20 +00:00
fTimeString = [NSString stringWithFormat:
@"Paused (%.2f %%)", 100 * stat->progress];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:29:20 +00:00
else if( stat->status & TR_STATUS_CHECK )
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:29:20 +00:00
fTimeString = [NSString stringWithFormat:
@"Checking existing files (%.2f %%)", 100 * stat->progress];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:29:20 +00:00
else if( stat->status & TR_STATUS_DOWNLOAD )
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:29:20 +00:00
if( stat->eta < 0 )
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:29:20 +00:00
fTimeString = [NSString stringWithFormat:
@"Finishing in --:--:-- (%.2f %%)", 100 * stat->progress];
2006-01-12 17:43:21 +00:00
}
else
{
2006-01-12 18:29:20 +00:00
fTimeString = [NSString stringWithFormat:
2006-01-12 17:43:21 +00:00
@"Finishing in %02d:%02d:%02d (%.2f %%)",
2006-01-12 18:29:20 +00:00
stat->eta / 3600, ( stat->eta / 60 ) % 60,
stat->eta % 60, 100 * stat->progress];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:29:20 +00:00
fPeersString = [NSString stringWithFormat:
2006-01-12 17:43:21 +00:00
@"Downloading from %d of %d peer%s",
2006-01-12 18:29:20 +00:00
stat->peersUploading, stat->peersTotal,
( stat->peersTotal == 1 ) ? "" : "s"];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:29:20 +00:00
else if( stat->status & TR_STATUS_SEED )
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:29:20 +00:00
fTimeString = [NSString stringWithFormat:
2006-01-12 17:43:21 +00:00
@"Seeding, uploading to %d of %d peer%s",
2006-01-12 18:29:20 +00:00
stat->peersDownloading, stat->peersTotal,
( stat->peersTotal == 1 ) ? "" : "s"];
}
else if( stat->status & TR_STATUS_STOPPING )
{
fTimeString = @"Stopping...";
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:29:20 +00:00
if( ( stat->status & ( TR_STATUS_DOWNLOAD | TR_STATUS_SEED ) ) &&
( stat->status & TR_TRACKER_ERROR ) )
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:29:20 +00:00
fPeersString = [NSString stringWithFormat: @"%@%@",
@"Error: ", [NSString stringWithUTF8String: stat->error]];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:29:20 +00:00
}
- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view
{
NSString * string;
NSPoint pen;
NSMutableDictionary * attributes;
2006-01-12 17:43:21 +00:00
2006-01-12 18:29:20 +00:00
if( ![view lockFocusIfCanDraw] )
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:29:20 +00:00
return;
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:29:20 +00:00
pen = cellFrame.origin;
float cellWidth = cellFrame.size.width;
pen.x += 5;
2006-02-10 06:14:36 +00:00
pen.y += 5;
2006-02-09 12:58:10 +00:00
[fCurrentIcon drawAtPoint: pen fromRect:
NSMakeRect(0,0,[fCurrentIcon size].width,[fCurrentIcon size].height)
operation: NSCompositeSourceOver fraction: 1.0];
2006-01-12 18:29:20 +00:00
2006-01-12 18:53:05 +00:00
attributes = [NSMutableDictionary dictionaryWithCapacity: 2];
[attributes setObject: fWhiteText ? [NSColor whiteColor] :
[NSColor blackColor] forKey: NSForegroundColorAttributeName];
[attributes setObject: [NSFont messageFontOfSize: 12.0]
2006-01-12 17:43:21 +00:00
forKey: NSFontAttributeName];
pen.x += 37;
string = [[fNameString stringFittingInWidth: cellWidth -
72 - [fSizeString sizeWithAttributes: attributes].width
withAttributes: attributes] stringByAppendingString: fSizeString];
2006-01-12 18:29:20 +00:00
[string drawAtPoint: pen withAttributes: attributes];
2006-01-12 17:43:21 +00:00
2006-01-12 18:53:05 +00:00
[attributes setObject: [NSFont messageFontOfSize: 10.0]
2006-01-12 17:43:21 +00:00
forKey: NSFontAttributeName];
pen.x += 5; pen.y += 20;
2006-01-12 18:29:20 +00:00
[fTimeString drawAtPoint: pen withAttributes: attributes];
2006-01-12 17:43:21 +00:00
pen.x += 0; pen.y += 15;
string = [fPeersString stringFittingInWidth: cellFrame.size.width -
77 withAttributes: attributes];
2006-01-12 18:29:20 +00:00
[string drawAtPoint: pen withAttributes: attributes];
2006-01-12 17:43:21 +00:00
[view unlockFocus];
}
@end