From 87a2bd647993e498dd5e0ddcbf02c0c8a8bf53cb Mon Sep 17 00:00:00 2001 From: Mitchell Livingston Date: Fri, 10 Aug 2007 03:20:01 +0000 Subject: [PATCH] use the actual Torrent class to access values for the torrent cell class, rather than creating a dictionary and using that on each reload --- Transmission.xcodeproj/project.pbxproj | 2 +- macosx/Controller.m | 4 +- macosx/Torrent.h | 4 +- macosx/Torrent.m | 298 +++++++++++-------------- macosx/TorrentCell.m | 45 ++-- macosx/TorrentTableView.m | 8 + 6 files changed, 170 insertions(+), 191 deletions(-) diff --git a/Transmission.xcodeproj/project.pbxproj b/Transmission.xcodeproj/project.pbxproj index 448e6cf8d..a4a43891a 100644 --- a/Transmission.xcodeproj/project.pbxproj +++ b/Transmission.xcodeproj/project.pbxproj @@ -313,7 +313,7 @@ 4DFBC2DD09C0970D00D5C571 /* Torrent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Torrent.h; path = macosx/Torrent.h; sourceTree = ""; }; 4DFBC2DE09C0970D00D5C571 /* Torrent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Torrent.m; path = macosx/Torrent.m; sourceTree = ""; }; 8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; name = Info.plist; path = macosx/Info.plist; sourceTree = ""; }; - 8D1107320486CEB800E47090 /* Transmission.app */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.application; path = Transmission.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 8D1107320486CEB800E47090 /* Transmission.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Transmission.app; sourceTree = BUILT_PRODUCTS_DIR; }; A200B8390A2263BA007BBB1E /* InfoWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = InfoWindowController.h; path = macosx/InfoWindowController.h; sourceTree = ""; }; A200B83A0A2263BA007BBB1E /* InfoWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = InfoWindowController.m; path = macosx/InfoWindowController.m; sourceTree = ""; }; A200B9630A227FD0007BBB1E /* English */ = {isa = PBXFileReference; lastKnownFileType = wrapper.nib; name = English; path = macosx/English.lproj/InfoWindow.nib; sourceTree = ""; }; diff --git a/macosx/Controller.m b/macosx/Controller.m index e37def329..ed71bfd17 100644 --- a/macosx/Controller.m +++ b/macosx/Controller.m @@ -2116,10 +2116,10 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy return [fDisplayedTorrents count]; } -- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (int) row +/*- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (int) row { return [[fDisplayedTorrents objectAtIndex: row] infoForCurrentView]; -} +}*/ - (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) indexes toPasteboard: (NSPasteboard *) pasteboard diff --git a/macosx/Torrent.h b/macosx/Torrent.h index a317e3656..b705b702c 100644 --- a/macosx/Torrent.h +++ b/macosx/Torrent.h @@ -85,7 +85,6 @@ - (void) getAmountFinished: (float *) tab size: (int) size; - (void) update; -- (NSDictionary *) infoForCurrentView; - (void) startTransfer; - (void) stopTransfer; @@ -152,6 +151,7 @@ - (float) progress; - (float) progressDone; +- (float) progressLeft; - (int) eta; - (BOOL) isActive; @@ -223,6 +223,8 @@ - (NSNumber *) progressSortKey; - (NSNumber *) ratioSortKey; +- (NSImage *) advancedBar; + - (int) torrentID; - (const tr_info_t *) torrentInfo; - (const tr_stat_t *) torrentStat; diff --git a/macosx/Torrent.m b/macosx/Torrent.m index 4bbf6b869..fd2e40478 100644 --- a/macosx/Torrent.m +++ b/macosx/Torrent.m @@ -51,7 +51,6 @@ static int static_lastid = 0; - (void) insertPath: (NSMutableArray *) components forSiblings: (NSMutableArray *) siblings withParent: (NSMutableDictionary *) parent previousPath: (NSString *) previousPath flatList: (NSMutableArray *) flatList fileSize: (uint64_t) size index: (int) index; -- (NSImage *) advancedBar; - (void) quickPause; - (void) endQuickPause; @@ -485,40 +484,6 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 [fRemainingTimeString setString: remainingTimeString]; } -- (NSDictionary *) infoForCurrentView -{ - NSMutableDictionary * info = [NSMutableDictionary dictionaryWithObjectsAndKeys: - [self name], @"Name", - [NSNumber numberWithFloat: [self progress]], @"Progress", - [NSNumber numberWithFloat: (float)fStat->left/[self size]], @"Left", - [NSNumber numberWithBool: [self isActive]], @"Active", - [NSNumber numberWithBool: [self isSeeding]], @"Seeding", - [NSNumber numberWithBool: [self isChecking]], @"Checking", - [NSNumber numberWithBool: fWaitToStart], @"Waiting", - [NSNumber numberWithBool: [self isError]], @"Error", nil]; - - if ([self isSeeding]) - [info setObject: [NSNumber numberWithFloat: [self progressStopRatio]] forKey: @"ProgressStopRatio"]; - - if (![fDefaults boolForKey: @"SmallView"]) - { - [info setObject: [self iconFlipped] forKey: @"Icon"]; - [info setObject: [self progressString] forKey: @"ProgressString"]; - [info setObject: [self statusString] forKey: @"StatusString"]; - } - else - { - [info setObject: [self iconSmall] forKey: @"Icon"]; - [info setObject: [self remainingTimeString] forKey: @"RemainingTimeString"]; - [info setObject: [self shortStatusString] forKey: @"ShortStatusString"]; - } - - if ([fDefaults boolForKey: @"UseAdvancedBar"]) - [info setObject: [self advancedBar] forKey: @"AdvancedBar"]; - - return info; -} - - (void) startTransfer { fWaitToStart = NO; @@ -1020,6 +985,11 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 return fStat->percentDone; } +- (float) progressLeft +{ + return fStat->left/[self size]; +} + - (int) eta { return fStat->eta; @@ -1450,6 +1420,135 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 return [NSNumber numberWithFloat: [self ratio]]; } +#warning move? +- (NSImage *) advancedBar +{ + uint32_t * p; + uint8_t * bitmapData = [fBitmap bitmapData]; + int bytesPerRow = [fBitmap bytesPerRow]; + + int pieceCount = [self pieceCount]; + int8_t * piecesAvailablity = malloc(pieceCount); + [self getAvailability: piecesAvailablity size: pieceCount]; + + //lines 2 to 14: blue, green, or gray depending on piece availability + int i, h, index = 0; + float increment = (float)pieceCount / MAX_PIECES, indexValue = 0; + uint32_t color; + BOOL change; + for (i = 0; i < MAX_PIECES; i++) + { + change = NO; + if (piecesAvailablity[index] < 0) + { + if (fPieces[i] != -1) + { + color = kBlue; + fPieces[i] = -1; + change = YES; + } + } + else if (piecesAvailablity[index] == 0) + { + if (fPieces[i] != 0) + { + color = kGray; + fPieces[i] = 0; + change = YES; + } + } + else if (piecesAvailablity[index] <= 4) + { + if (fPieces[i] != 1) + { + color = kGreen1; + fPieces[i] = 1; + change = YES; + } + } + else if (piecesAvailablity[index] <= 8) + { + if (fPieces[i] != 2) + { + color = kGreen2; + fPieces[i] = 2; + change = YES; + } + } + else + { + if (fPieces[i] != 3) + { + color = kGreen3; + fPieces[i] = 3; + change = YES; + } + } + + if (change) + { + //point to pixel (i, 2) and draw "vertically" + p = (uint32_t *)(bitmapData + 2 * bytesPerRow) + i; + for (h = 2; h < BAR_HEIGHT; h++) + { + p[0] = color; + p = (uint32_t *)((uint8_t *)p + bytesPerRow); + } + } + + indexValue += increment; + index = (int)indexValue; + } + + //determine percentage finished and available + int have = rintf((float)MAX_PIECES * [self progress]), avail; + if ([self progress] >= 1.0 || ![self isActive] || [self totalPeersConnected] <= 0) + avail = 0; + else + { + float * piecesFinished = malloc(pieceCount * sizeof(float)); + [self getAmountFinished: piecesFinished size: pieceCount]; + + float available = 0; + for (i = 0; i < pieceCount; i++) + if (piecesAvailablity[i] > 0) + available += 1.0 - piecesFinished[i]; + + avail = rintf(MAX_PIECES * available / (float)pieceCount); + if (have + avail > MAX_PIECES) //case if both end in .5 and all pieces are available + avail--; + + free(piecesFinished); + } + + free(piecesAvailablity); + + //first two lines: dark blue to show progression, green to show available + p = (uint32_t *)bitmapData; + for (i = 0; i < have; i++) + { + p[i] = kBlue2; + p[i + bytesPerRow / 4] = kBlue2; + } + for (; i < avail + have; i++) + { + p[i] = kGreen3; + p[i + bytesPerRow / 4] = kGreen3; + } + for (; i < MAX_PIECES; i++) + { + p[i] = kWhite; + p[i + bytesPerRow / 4] = kWhite; + } + + //actually draw image + NSImage * bar = [[NSImage alloc] initWithSize: [fBitmap size]]; + [bar addRepresentation: fBitmap]; + [bar setScalesWhenResized: YES]; + + return [bar autorelease]; +} + - (int) torrentID { return fID; @@ -1680,135 +1779,6 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 tr_torrentSetFolder(fHandle, [folder UTF8String]); } -#warning move? -- (NSImage *) advancedBar -{ - uint32_t * p; - uint8_t * bitmapData = [fBitmap bitmapData]; - int bytesPerRow = [fBitmap bytesPerRow]; - - int pieceCount = [self pieceCount]; - int8_t * piecesAvailablity = malloc(pieceCount); - [self getAvailability: piecesAvailablity size: pieceCount]; - - //lines 2 to 14: blue, green, or gray depending on piece availability - int i, h, index = 0; - float increment = (float)pieceCount / (float)MAX_PIECES, indexValue = 0; - uint32_t color; - BOOL change; - for (i = 0; i < MAX_PIECES; i++) - { - change = NO; - if (piecesAvailablity[index] < 0) - { - if (fPieces[i] != -1) - { - color = kBlue; - fPieces[i] = -1; - change = YES; - } - } - else if (piecesAvailablity[index] == 0) - { - if (fPieces[i] != 0) - { - color = kGray; - fPieces[i] = 0; - change = YES; - } - } - else if (piecesAvailablity[index] <= 4) - { - if (fPieces[i] != 1) - { - color = kGreen1; - fPieces[i] = 1; - change = YES; - } - } - else if (piecesAvailablity[index] <= 8) - { - if (fPieces[i] != 2) - { - color = kGreen2; - fPieces[i] = 2; - change = YES; - } - } - else - { - if (fPieces[i] != 3) - { - color = kGreen3; - fPieces[i] = 3; - change = YES; - } - } - - if (change) - { - //point to pixel (i, 2) and draw "vertically" - p = (uint32_t *)(bitmapData + 2 * bytesPerRow) + i; - for (h = 2; h < BAR_HEIGHT; h++) - { - p[0] = color; - p = (uint32_t *)((uint8_t *)p + bytesPerRow); - } - } - - indexValue += increment; - index = (int)indexValue; - } - - //determine percentage finished and available - int have = rintf((float)MAX_PIECES * [self progress]), avail; - if ([self progress] >= 1.0 || ![self isActive] || [self totalPeersConnected] <= 0) - avail = 0; - else - { - float * piecesFinished = malloc(pieceCount * sizeof(float)); - [self getAmountFinished: piecesFinished size: pieceCount]; - - float available = 0; - for (i = 0; i < pieceCount; i++) - if (piecesAvailablity[i] > 0) - available += 1.0 - piecesFinished[i]; - - avail = rintf((float)MAX_PIECES * available / (float)pieceCount); - if (have + avail > MAX_PIECES) //case if both end in .5 and all pieces are available - avail--; - - free(piecesFinished); - } - - free(piecesAvailablity); - - //first two lines: dark blue to show progression, green to show available - p = (uint32_t *)bitmapData; - for (i = 0; i < have; i++) - { - p[i] = kBlue2; - p[i + bytesPerRow / 4] = kBlue2; - } - for (; i < avail + have; i++) - { - p[i] = kGreen3; - p[i + bytesPerRow / 4] = kGreen3; - } - for (; i < MAX_PIECES; i++) - { - p[i] = kWhite; - p[i + bytesPerRow / 4] = kWhite; - } - - //actually draw image - NSImage * bar = [[NSImage alloc] initWithSize: [fBitmap size]]; - [bar addRepresentation: fBitmap]; - [bar setScalesWhenResized: YES]; - - return [bar autorelease]; -} - - (void) quickPause { if (fQuickPauseDict) diff --git a/macosx/TorrentCell.m b/macosx/TorrentCell.m index ff34c7c95..134fa3a16 100644 --- a/macosx/TorrentCell.m +++ b/macosx/TorrentCell.m @@ -75,7 +75,7 @@ - (void) buildSimpleBar: (float) width point: (NSPoint) point { - NSDictionary * info = [self objectValue]; + Torrent * torrent = [self representedObject]; NSRect barBounds, completeBounds; if([[self controlView] isFlipped]) @@ -84,7 +84,7 @@ barBounds = NSMakeRect(point.x, point.y - BAR_HEIGHT, width, BAR_HEIGHT); completeBounds = barBounds; - float progress = [[info objectForKey: @"Progress"] floatValue]; + float progress = [torrent progress]; completeBounds.size.width = progress * width; float left = INVALID; @@ -94,7 +94,7 @@ fWhiteGradient = [[CTGradient progressWhiteGradient] retain]; [fWhiteGradient fillRect: barBounds angle: -90]; - left = [[info objectForKey: @"Left"] floatValue]; + left = [torrent progressLeft]; if ((progress + left) < 1.0) { NSRect blankBounds = barBounds; @@ -107,18 +107,18 @@ } } - if ([[info objectForKey: @"Active"] boolValue]) + if ([torrent isActive]) { - if ([[info objectForKey: @"Checking"] boolValue]) + if ([torrent isChecking]) { if (!fYellowGradient) fYellowGradient = [[CTGradient progressYellowGradient] retain]; [fYellowGradient fillRect: completeBounds angle: -90]; } - else if ([[info objectForKey: @"Seeding"] boolValue]) + else if ([torrent isSeeding]) { NSRect ratioBounds = completeBounds; - ratioBounds.size.width *= [[info objectForKey: @"ProgressStopRatio"] floatValue]; + ratioBounds.size.width *= [torrent progressStopRatio]; if (ratioBounds.size.width < completeBounds.size.width) { @@ -140,10 +140,10 @@ } else { - if ([[info objectForKey: @"Waiting"] boolValue]) + if ([torrent waitingToStart]) { if (left == INVALID) - left = [[info objectForKey: @"Left"] floatValue]; + left = [torrent progressLeft]; if (left <= 0.0) { @@ -172,10 +172,10 @@ - (void) buildAdvancedBar: (float) width point: (NSPoint) point { - NSDictionary * info = [self objectValue]; + Torrent * torrent = [self representedObject]; //place actual advanced bar - NSImage * image = [info objectForKey: @"AdvancedBar"]; + NSImage * image = [torrent advancedBar]; [image setSize: NSMakeSize(width, BAR_HEIGHT)]; [image compositeToPoint: point operation: NSCompositeSourceOver]; @@ -211,12 +211,12 @@ NSPoint pen = cellFrame.origin; const float LINE_PADDING = 2.0, EXTRA_NAME_SHIFT = 1.0; //standard padding is defined in TorrentCell.h - NSDictionary * info = [self objectValue]; + Torrent * torrent = [self representedObject]; if (![fDefaults boolForKey: @"SmallView"]) //regular size { //icon - NSImage * icon = [info objectForKey: @"Icon"]; + NSImage * icon = [torrent iconFlipped]; NSSize iconSize = [icon size]; pen.x += PADDING; @@ -226,7 +226,7 @@ operation: NSCompositeSourceOver fraction: 1.0]; //error badge - if ([[info objectForKey: @"Error"] boolValue]) + if ([torrent isError]) { if (!fErrorImage) { @@ -247,14 +247,14 @@ pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT; pen.y = cellFrame.origin.y + PADDING; - NSString * nameString = [info objectForKey: @"Name"]; + NSString * nameString = [torrent name]; NSSize nameSize = [nameString sizeWithAttributes: nameAttributes]; [nameString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, nameSize.height) withAttributes: nameAttributes]; //progress string pen.y += nameSize.height + LINE_PADDING - 1.0; - NSString * progressString = [info objectForKey: @"ProgressString"]; + NSString * progressString = [torrent progressString]; NSSize progressSize = [progressString sizeWithAttributes: statusAttributes]; [progressString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, progressSize.height) withAttributes: statusAttributes]; @@ -273,7 +273,7 @@ pen.x += EXTRA_NAME_SHIFT; pen.y += LINE_PADDING; - NSString * statusString = [info objectForKey: @"StatusString"]; + NSString * statusString = [torrent statusString]; NSSize statusSize = [statusString sizeWithAttributes: statusAttributes]; [statusString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, statusSize.height) withAttributes: statusAttributes]; } @@ -281,7 +281,7 @@ { //icon NSImage * icon; - if ([[info objectForKey: @"Error"] boolValue]) + if ([torrent isError]) { if (!fErrorImage) { @@ -291,7 +291,7 @@ icon = fErrorImage; } else - icon = [info objectForKey: @"Icon"]; + icon = [torrent iconSmall]; NSSize iconSize = [icon size]; pen.x += PADDING; @@ -307,12 +307,11 @@ pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT; pen.y = cellFrame.origin.y + LINE_PADDING; - NSString * nameString = [info objectForKey: @"Name"]; + NSString * nameString = [torrent name]; NSSize nameSize = [nameString sizeWithAttributes: nameAttributes]; - NSString * statusString = ![fDefaults boolForKey: @"SmallStatusRegular"] && [[info objectForKey: @"Active"] boolValue] - ? [info objectForKey: @"RemainingTimeString"] - : [info objectForKey: @"ShortStatusString"]; + NSString * statusString = ![fDefaults boolForKey: @"SmallStatusRegular"] && [torrent isActive] + ? [torrent remainingTimeString] : [torrent shortStatusString]; NSSize statusSize = [statusString sizeWithAttributes: statusAttributes]; [nameString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth - statusSize.width - 2.0 * LINE_PADDING, nameSize.height) diff --git a/macosx/TorrentTableView.m b/macosx/TorrentTableView.m index 952fc835f..0e3c6e278 100644 --- a/macosx/TorrentTableView.m +++ b/macosx/TorrentTableView.m @@ -76,6 +76,8 @@ [NSFont messageFontOfSize: 9.0], NSFontAttributeName, nil]; fDefaults = [NSUserDefaults standardUserDefaults]; + + [self setDelegate: self]; } return self; @@ -102,6 +104,12 @@ fTorrents = torrents; } +- (void) tableView: (NSTableView *) tableView willDisplayCell: (id) cell + forTableColumn: (NSTableColumn *) tableColumn row: (int) row +{ + [cell setRepresentedObject: [fTorrents objectAtIndex: row]]; +} + - (void) mouseDown: (NSEvent *) event { fClickPoint = [self convertPoint: [event locationInWindow] fromView: nil];