From 3abf813bbfa41c5b42236d9a792ee749c64e490f Mon Sep 17 00:00:00 2001 From: Mitchell Livingston Date: Sat, 8 Jan 2011 05:11:28 +0000 Subject: [PATCH] #3878 don't show the units of a partial file size when it's the same as the full file size's units --- macosx/BlocklistDownloaderViewController.m | 3 +- macosx/NSStringAdditions.h | 1 + macosx/NSStringAdditions.m | 100 +++++++++++++-------- macosx/Torrent.m | 9 +- 4 files changed, 68 insertions(+), 45 deletions(-) diff --git a/macosx/BlocklistDownloaderViewController.m b/macosx/BlocklistDownloaderViewController.m index be2c85c6e..571c0c4a5 100644 --- a/macosx/BlocklistDownloaderViewController.m +++ b/macosx/BlocklistDownloaderViewController.m @@ -76,8 +76,7 @@ { [fProgressBar setIndeterminate: NO]; - NSString * substring = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Blocklist -> message"), - [NSString stringForFileSize: currentSize], [NSString stringForFileSize: expectedSize]]; + NSString * substring = [NSString stringForFilePartialSize: currentSize fullSize: expectedSize]; string = [string stringByAppendingFormat: @" (%@)", substring]; [fProgressBar setDoubleValue: (double)currentSize / expectedSize]; } diff --git a/macosx/NSStringAdditions.h b/macosx/NSStringAdditions.h index 81d82970c..bd6c0a878 100644 --- a/macosx/NSStringAdditions.h +++ b/macosx/NSStringAdditions.h @@ -32,6 +32,7 @@ + (NSString *) formattedUInteger: (NSUInteger) value; + (NSString *) stringForFileSize: (uint64_t) size; ++ (NSString *) stringForFilePartialSize: (uint64_t) partialSize fullSize: (uint64_t) fullSize; + (NSString *) stringForSpeed: (CGFloat) speed; + (NSString *) stringForSpeedAbbrev: (CGFloat) speed; diff --git a/macosx/NSStringAdditions.m b/macosx/NSStringAdditions.m index d1667bed2..f3641183d 100644 --- a/macosx/NSStringAdditions.m +++ b/macosx/NSStringAdditions.m @@ -30,6 +30,9 @@ @interface NSString (Private) ++ (NSString *) stringForFileSize: (uint64_t) size showUnitUnless: (NSString *) notAllowedUnit + unitsUsed: (NSString **) unitUsed; + + (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb; @end @@ -57,44 +60,16 @@ + (NSString *) stringForFileSize: (uint64_t) size { - const double baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0; - const NSUInteger baseInt = [NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024; + return [self stringForFileSize: size showUnitUnless: nil unitsUsed: nil]; +} + ++ (NSString *) stringForFilePartialSize: (uint64_t) partialSize fullSize: (uint64_t) fullSize +{ + NSString * units; + NSString * fullString = [self stringForFileSize: fullSize showUnitUnless: nil unitsUsed: &units]; + NSString * partialString = [self stringForFileSize: partialSize showUnitUnless: units unitsUsed: nil]; - double convertedSize; - NSString * unit; - NSUInteger decimals; - if (size < pow(baseInt, 2)) - { - convertedSize = size / baseFloat; - unit = NSLocalizedString(@"KB", "File size - kilobytes"); - decimals = 0; - } - else if (size < pow(baseInt, 3)) - { - convertedSize = size / powf(baseFloat, 2); - unit = NSLocalizedString(@"MB", "File size - megabytes"); - decimals = 1; - } - else if (size < pow(baseInt, 4)) - { - convertedSize = size / powf(baseFloat, 3); - unit = NSLocalizedString(@"GB", "File size - gigabytes"); - decimals = 2; - } - else - { - convertedSize = size / powf(baseFloat, 4); - unit = NSLocalizedString(@"TB", "File size - terabytes"); - decimals = 3; //guessing on this one - } - - //match Finder's behavior - NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease]; - [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; - [numberFormatter setMinimumFractionDigits: 0]; - [numberFormatter setMaximumFractionDigits: decimals]; - - return [[numberFormatter stringFromNumber: [NSNumber numberWithDouble: convertedSize]] stringByAppendingFormat: @" %@", unit]; + return [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "file size string"), partialString, fullString]; } + (NSString *) stringForSpeed: (CGFloat) speed @@ -201,6 +176,57 @@ @implementation NSString (Private) ++ (NSString *) stringForFileSize: (uint64_t) size showUnitUnless: (NSString *) notAllowedUnit + unitsUsed: (NSString **) unitUsed +{ + const double baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0; + const NSUInteger baseInt = [NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024; + + double convertedSize; + NSString * unit; + NSUInteger decimals; + if (size < pow(baseInt, 2)) + { + convertedSize = size / baseFloat; + unit = NSLocalizedString(@"KB", "File size - kilobytes"); + decimals = 0; + } + else if (size < pow(baseInt, 3)) + { + convertedSize = size / powf(baseFloat, 2); + unit = NSLocalizedString(@"MB", "File size - megabytes"); + decimals = 1; + } + else if (size < pow(baseInt, 4)) + { + convertedSize = size / powf(baseFloat, 3); + unit = NSLocalizedString(@"GB", "File size - gigabytes"); + decimals = 2; + } + else + { + convertedSize = size / powf(baseFloat, 4); + unit = NSLocalizedString(@"TB", "File size - terabytes"); + decimals = 3; //guessing on this one + } + + //match Finder's behavior + NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease]; + [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; + [numberFormatter setMinimumFractionDigits: 0]; + [numberFormatter setMaximumFractionDigits: decimals]; + + NSString * fileSizeString = [numberFormatter stringFromNumber: [NSNumber numberWithDouble: convertedSize]]; + + if (!notAllowedUnit || ![unit isEqualToString: notAllowedUnit]) + fileSizeString = [fileSizeString stringByAppendingFormat: @" %@", unit]; + + if (unitUsed) + *unitUsed = unit; + + return fileSizeString; +} + + (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb { const CGFloat baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0; diff --git a/macosx/Torrent.m b/macosx/Torrent.m index d0a0d26a3..d8ccfe3af 100644 --- a/macosx/Torrent.m +++ b/macosx/Torrent.m @@ -977,14 +977,12 @@ int trashDataFile(const char * filename) CGFloat progress; if ([self isFolder] && [fDefaults boolForKey: @"DisplayStatusProgressSelected"]) { - string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@ selected", "Torrent -> progress string"), - [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self totalSizeSelected]]]; + string = [NSString stringForFilePartialSize: [self haveTotal] fullSize: [self totalSizeSelected]]; progress = [self progressDone]; } else { - string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Torrent -> progress string"), - [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self size]]]; + string = [NSString stringForFilePartialSize: [self haveTotal] fullSize: [self size]]; progress = [self progress]; } @@ -1000,8 +998,7 @@ int trashDataFile(const char * filename) [NSString stringForFileSize: [self haveTotal]]]; else { - downloadString = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Torrent -> progress string"), - [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self size]]]; + downloadString = [NSString stringForFilePartialSize: [self haveTotal] fullSize: [self size]]; downloadString = [downloadString stringByAppendingFormat: @" (%@)", [NSString percentString: [self progress] longDecimals: YES]]; }