source: trunk/macosx/NSStringAdditions.m @ 10988

Last change on this file since 10988 was 10988, checked in by livings124, 12 years ago

when localizing units of speed, localize the full speed string; localize the speed & size strings passed to libtransmission

  • Property svn:keywords set to Date Rev Author Id
File size: 7.5 KB
Line 
1/******************************************************************************
2 * $Id: NSStringAdditions.m 10988 2010-07-10 02:31:05Z livings124 $
3 *
4 * Copyright (c) 2005-2010 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "NSStringAdditions.h"
26#import "NSApplicationAdditions.h"
27
28#import <transmission.h>
29#import "utils.h"
30
31@interface NSString (Private)
32
33+ (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb;
34
35@end
36
37@implementation NSString (NSStringAdditions)
38
39+ (NSString *) ellipsis
40{
41        return [NSString stringWithUTF8String: "\xE2\x80\xA6"];
42}
43
44- (NSString *) stringByAppendingEllipsis
45{
46        return [self stringByAppendingString: [NSString ellipsis]];
47}
48
49+ (NSString *) stringForFileSize: (uint64_t) size
50{
51    if (size < 1024)
52    {
53        if (size != 1)
54            return [NSString stringWithFormat: @"%lld %@", size, NSLocalizedString(@"bytes", "File size - bytes")];
55        else
56            return NSLocalizedString(@"1 byte", "File size - bytes");
57    }
58
59    CGFloat convertedSize;
60    NSString * unit;
61    if (size < pow(1024, 2))
62    {
63        convertedSize = size / 1024.0;
64        unit = NSLocalizedString(@"KB", "File size - kilobytes");
65    }
66    else if (size < pow(1024, 3))
67    {
68        convertedSize = size / (CGFloat)pow(1024, 2);
69        unit = NSLocalizedString(@"MB", "File size - megabytes");
70    }
71    else if (size < pow(1024, 4))
72    {
73        convertedSize = size / (CGFloat)pow(1024, 3);
74        unit = NSLocalizedString(@"GB", "File size - gigabytes");
75    }
76    else
77    {
78        convertedSize = size / (CGFloat)pow(1024, 4);
79        unit = NSLocalizedString(@"TB", "File size - terabytes");
80    }
81   
82    //attempt to have minimum of 3 digits with at least 1 decimal
83    return convertedSize <= 9.995 ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit]
84                                : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit];
85}
86
87+ (NSString *) stringForSpeed: (CGFloat) speed
88{
89    return [self stringForSpeed: speed
90                kb: NSLocalizedString(@"KB/s", "Transfer speed (kilobytes per second)")
91                mb: NSLocalizedString(@"MB/s", "Transfer speed (megabytes per second)")
92                gb: NSLocalizedString(@"GB/s", "Transfer speed (gigabytes per second)")];
93}
94
95+ (NSString *) stringForSpeedAbbrev: (CGFloat) speed
96{
97    return [self stringForSpeed: speed kb: @"K" mb: @"M" gb: @"G"];
98}
99
100+ (NSString *) stringForRatio: (CGFloat) ratio
101{
102    //N/A is different than libtransmission's
103    if ((int)ratio == TR_RATIO_NA)
104        return NSLocalizedString(@"N/A", "No Ratio");
105    else if ((int)ratio == TR_RATIO_INF)
106        return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
107    else
108    {
109        if (ratio < 10.0)
110            return [NSString localizedStringWithFormat: @"%.2f", tr_truncd(ratio, 2)];
111        else if (ratio < 100.0)
112            return [NSString localizedStringWithFormat: @"%.1f", tr_truncd(ratio, 1)];
113        else
114            return [NSString localizedStringWithFormat: @"%.0f", tr_truncd(ratio, 0)];
115    }
116}
117
118+ (NSString *) percentString: (CGFloat) progress longDecimals: (BOOL) longDecimals
119{
120    if (progress >= 1.0)
121        return @"100%";
122    else if (longDecimals)
123        return [NSString localizedStringWithFormat: @"%.2f%%", tr_truncd(progress * 100.0, 2)];
124    else
125        return [NSString localizedStringWithFormat: @"%.1f%%", tr_truncd(progress * 100.0, 1)];
126}
127
128+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds
129{
130    return [NSString timeString: seconds showSeconds: showSeconds maxFields: NSUIntegerMax];
131}
132
133+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max
134{
135    NSAssert(max > 0, @"Cannot generate a time string with no fields");
136   
137    NSMutableArray * timeArray = [NSMutableArray arrayWithCapacity: MIN(max, 4)];
138    NSUInteger remaining = seconds; //causes problems for some users when it's a uint64_t
139   
140    if (seconds >= (24 * 60 * 60))
141    {
142        const NSUInteger days = remaining / (24 * 60 * 60);
143        if (days == 1)
144            [timeArray addObject: NSLocalizedString(@"1 day", "time string")];
145        else
146            [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u days", "time string"), days]];
147        remaining %= (24 * 60 * 60);
148        max--;
149    }
150    if (max > 0 && seconds >= (60 * 60))
151    {
152        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u hr", "time string"), remaining / (60 * 60)]];
153        remaining %= (60 * 60);
154        max--;
155    }
156    if (max > 0 && (!showSeconds || seconds >= 60))
157    {
158        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u min", "time string"), remaining / 60]];
159        remaining %= 60;
160        max--;
161    }
162    if (max > 0 && showSeconds)
163        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u sec", "time string"), remaining]];
164   
165    return [timeArray componentsJoinedByString: @" "];
166}
167
168//also used in InfoWindow.xib and MessageWindow.xib
169- (NSComparisonResult) compareFinder: (NSString *) string
170{
171    if ([NSApp isOnSnowLeopardOrBetter])
172        return [self localizedStandardCompare: string];
173    else
174    {
175        const NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch
176                                                            | NSForcedOrderingSearch;
177        return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
178    }
179}
180
181- (NSComparisonResult) compareNumeric: (NSString *) string
182{
183    const NSStringCompareOptions comparisonOptions = NSNumericSearch | NSForcedOrderingSearch;
184    return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
185}
186
187@end
188
189@implementation NSString (Private)
190
191+ (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb
192{
193    if (speed <= 999.95) //0.0 KB/s to 999.9 KB/s
194        return [NSString localizedStringWithFormat: @"%.1f %@", speed, kb];
195   
196    speed /= 1024.0;
197   
198    if (speed <= 99.995) //0.98 MB/s to 99.99 MB/s
199        return [NSString localizedStringWithFormat: @"%.2f %@", speed, mb];
200    else if (speed <= 999.95) //100.0 MB/s to 999.9 MB/s
201        return [NSString localizedStringWithFormat: @"%.1f %@", speed, mb];
202    else //insane speeds
203        return [NSString localizedStringWithFormat: @"%.2f %@", (speed / 1024.0), gb];
204}
205
206@end
Note: See TracBrowser for help on using the repository browser.