1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 01:03:01 +00:00

#3878 don't show the units of a partial file size when it's the same as the full file size's units

This commit is contained in:
Mitchell Livingston 2011-01-08 05:11:28 +00:00
parent 035b67677b
commit 3abf813bbf
4 changed files with 68 additions and 45 deletions

View file

@ -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];
}

View file

@ -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;

View file

@ -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;

View file

@ -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]];
}