Changeset 11647


Ignore:
Timestamp:
Jan 8, 2011, 5:11:28 AM (11 years ago)
Author:
livings124
Message:

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

Location:
trunk/macosx
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/BlocklistDownloaderViewController.m

    r11617 r11647  
    7777        [fProgressBar setIndeterminate: NO];
    7878       
    79         NSString * substring = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Blocklist -> message"),
    80                                 [NSString stringForFileSize: currentSize], [NSString stringForFileSize: expectedSize]];
     79        NSString * substring = [NSString stringForFilePartialSize: currentSize fullSize: expectedSize];
    8180        string = [string stringByAppendingFormat: @" (%@)",  substring];
    8281        [fProgressBar setDoubleValue: (double)currentSize / expectedSize];
  • trunk/macosx/NSStringAdditions.h

    r11617 r11647  
    3333
    3434+ (NSString *) stringForFileSize: (uint64_t) size;
     35+ (NSString *) stringForFilePartialSize: (uint64_t) partialSize fullSize: (uint64_t) fullSize;
    3536
    3637+ (NSString *) stringForSpeed: (CGFloat) speed;
  • trunk/macosx/NSStringAdditions.m

    r11646 r11647  
    3131@interface NSString (Private)
    3232
     33+ (NSString *) stringForFileSize: (uint64_t) size showUnitUnless: (NSString *) notAllowedUnit
     34    unitsUsed: (NSString **) unitUsed;
     35
    3336+ (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb;
    3437
     
    5861+ (NSString *) stringForFileSize: (uint64_t) size
    5962{
    60     const double baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0;
    61     const NSUInteger baseInt = [NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024;
    62    
    63     double convertedSize;
    64     NSString * unit;
    65     NSUInteger decimals;
    66     if (size < pow(baseInt, 2))
    67     {
    68         convertedSize = size / baseFloat;
    69         unit = NSLocalizedString(@"KB", "File size - kilobytes");
    70         decimals = 0;
    71     }
    72     else if (size < pow(baseInt, 3))
    73     {
    74         convertedSize = size / powf(baseFloat, 2);
    75         unit = NSLocalizedString(@"MB", "File size - megabytes");
    76         decimals = 1;
    77     }
    78     else if (size < pow(baseInt, 4))
    79     {
    80         convertedSize = size / powf(baseFloat, 3);
    81         unit = NSLocalizedString(@"GB", "File size - gigabytes");
    82         decimals = 2;
    83     }
    84     else
    85     {
    86         convertedSize = size / powf(baseFloat, 4);
    87         unit = NSLocalizedString(@"TB", "File size - terabytes");
    88         decimals = 3; //guessing on this one
    89     }
    90    
    91     //match Finder's behavior
    92     NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    93     [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
    94     [numberFormatter setMinimumFractionDigits: 0];
    95     [numberFormatter setMaximumFractionDigits: decimals];
    96    
    97     return [[numberFormatter stringFromNumber: [NSNumber numberWithDouble: convertedSize]] stringByAppendingFormat: @" %@", unit];
     63    return [self stringForFileSize: size showUnitUnless: nil unitsUsed: nil];
     64}
     65
     66+ (NSString *) stringForFilePartialSize: (uint64_t) partialSize fullSize: (uint64_t) fullSize
     67{
     68    NSString * units;
     69    NSString * fullString = [self stringForFileSize: fullSize showUnitUnless: nil unitsUsed: &units];
     70    NSString * partialString = [self stringForFileSize: partialSize showUnitUnless: units unitsUsed: nil];
     71   
     72    return [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "file size string"), partialString, fullString];
    9873}
    9974
     
    202177@implementation NSString (Private)
    203178
     179+ (NSString *) stringForFileSize: (uint64_t) size showUnitUnless: (NSString *) notAllowedUnit
     180    unitsUsed: (NSString **) unitUsed
     181{
     182    const double baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0;
     183    const NSUInteger baseInt = [NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024;
     184   
     185    double convertedSize;
     186    NSString * unit;
     187    NSUInteger decimals;
     188    if (size < pow(baseInt, 2))
     189    {
     190        convertedSize = size / baseFloat;
     191        unit = NSLocalizedString(@"KB", "File size - kilobytes");
     192        decimals = 0;
     193    }
     194    else if (size < pow(baseInt, 3))
     195    {
     196        convertedSize = size / powf(baseFloat, 2);
     197        unit = NSLocalizedString(@"MB", "File size - megabytes");
     198        decimals = 1;
     199    }
     200    else if (size < pow(baseInt, 4))
     201    {
     202        convertedSize = size / powf(baseFloat, 3);
     203        unit = NSLocalizedString(@"GB", "File size - gigabytes");
     204        decimals = 2;
     205    }
     206    else
     207    {
     208        convertedSize = size / powf(baseFloat, 4);
     209        unit = NSLocalizedString(@"TB", "File size - terabytes");
     210        decimals = 3; //guessing on this one
     211    }
     212   
     213    //match Finder's behavior
     214    NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
     215    [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
     216    [numberFormatter setMinimumFractionDigits: 0];
     217    [numberFormatter setMaximumFractionDigits: decimals];
     218   
     219    NSString * fileSizeString = [numberFormatter stringFromNumber: [NSNumber numberWithDouble: convertedSize]];
     220   
     221    if (!notAllowedUnit || ![unit isEqualToString: notAllowedUnit])
     222        fileSizeString = [fileSizeString stringByAppendingFormat: @" %@", unit];
     223   
     224    if (unitUsed)
     225        *unitUsed = unit;
     226   
     227    return fileSizeString;
     228}
     229
    204230+ (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb
    205231{
  • trunk/macosx/Torrent.m

    r11617 r11647  
    978978        if ([self isFolder] && [fDefaults boolForKey: @"DisplayStatusProgressSelected"])
    979979        {
    980             string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@ selected", "Torrent -> progress string"),
    981                         [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self totalSizeSelected]]];
     980            string = [NSString stringForFilePartialSize: [self haveTotal] fullSize: [self totalSizeSelected]];
    982981            progress = [self progressDone];
    983982        }
    984983        else
    985984        {
    986             string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Torrent -> progress string"),
    987                         [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self size]]];
     985            string = [NSString stringForFilePartialSize: [self haveTotal] fullSize: [self size]];
    988986            progress = [self progress];
    989987        }
     
    1001999            else
    10021000            {
    1003                 downloadString = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Torrent -> progress string"),
    1004                                     [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self size]]];
     1001                downloadString = [NSString stringForFilePartialSize: [self haveTotal] fullSize: [self size]];
    10051002                downloadString = [downloadString stringByAppendingFormat: @" (%@)",
    10061003                                    [NSString percentString: [self progress] longDecimals: YES]];
Note: See TracChangeset for help on using the changeset viewer.