Revert "transmission: update from 2.13 to 2.22"
[tomato.git] / release / src / router / transmission / macosx / NSStringAdditions.m
blobe4a97c27b2067ed05723258a3a826dec63c7bc13
1 /******************************************************************************
2  * $Id: NSStringAdditions.m 11417 2010-11-14 20:26:58Z 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  *****************************************************************************/
25 #import "NSStringAdditions.h"
26 #import "NSApplicationAdditions.h"
28 #import <transmission.h>
29 #import "utils.h"
31 @interface NSString (Private)
33 + (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb;
35 @end
37 @implementation NSString (NSStringAdditions)
39 + (NSString *) ellipsis
41         return [NSString stringWithUTF8String: "\xE2\x80\xA6"];
44 - (NSString *) stringByAppendingEllipsis
46         return [self stringByAppendingString: [NSString ellipsis]];
49 + (NSString *) formattedUInteger: (NSUInteger) value
51     NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
52     [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
53     [numberFormatter setMaximumFractionDigits: 0];
54     
55     return [numberFormatter stringFromNumber: [NSNumber numberWithUnsignedInteger: value]];
58 + (NSString *) stringForFileSize: (uint64_t) size
60     const CGFloat baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0;
61     const NSUInteger baseInt = [NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024;
62     
63     if (size < baseInt)
64     {
65         if (size != 1)
66             return [NSString stringWithFormat: @"%lld %@", size, NSLocalizedString(@"bytes", "File size - bytes")];
67         else
68             return NSLocalizedString(@"1 byte", "File size - bytes");
69     }
71     CGFloat convertedSize;
72     NSString * unit;
73     if (size < pow(baseInt, 2))
74     {
75         convertedSize = size / baseFloat;
76         unit = NSLocalizedString(@"KB", "File size - kilobytes");
77     }
78     else if (size < pow(baseInt, 3))
79     {
80         convertedSize = size / powf(baseFloat, 2);
81         unit = NSLocalizedString(@"MB", "File size - megabytes");
82     }
83     else if (size < pow(baseInt, 4))
84     {
85         convertedSize = size / powf(baseFloat, 3);
86         unit = NSLocalizedString(@"GB", "File size - gigabytes");
87     }
88     else
89     {
90         convertedSize = size / powf(baseFloat, 4);
91         unit = NSLocalizedString(@"TB", "File size - terabytes");
92     }
93     
94     //attempt to have minimum of 3 digits with at least 1 decimal
95     return convertedSize <= 9.995 ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit]
96                                 : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit];
99 + (NSString *) stringForSpeed: (CGFloat) speed
101     return [self stringForSpeed: speed
102                 kb: NSLocalizedString(@"KB/s", "Transfer speed (kilobytes per second)")
103                 mb: NSLocalizedString(@"MB/s", "Transfer speed (megabytes per second)")
104                 gb: NSLocalizedString(@"GB/s", "Transfer speed (gigabytes per second)")];
107 + (NSString *) stringForSpeedAbbrev: (CGFloat) speed
109     return [self stringForSpeed: speed kb: @"K" mb: @"M" gb: @"G"];
112 + (NSString *) stringForRatio: (CGFloat) ratio
114     //N/A is different than libtransmission's
115     if ((int)ratio == TR_RATIO_NA)
116         return NSLocalizedString(@"N/A", "No Ratio");
117     else if ((int)ratio == TR_RATIO_INF)
118         return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
119     else
120     {
121         if (ratio < 10.0)
122             return [NSString localizedStringWithFormat: @"%.2f", tr_truncd(ratio, 2)];
123         else if (ratio < 100.0)
124             return [NSString localizedStringWithFormat: @"%.1f", tr_truncd(ratio, 1)];
125         else
126             return [NSString localizedStringWithFormat: @"%.0f", tr_truncd(ratio, 0)];
127     }
130 + (NSString *) percentString: (CGFloat) progress longDecimals: (BOOL) longDecimals
132     if (progress >= 1.0)
133         return @"100%";
134     else if (longDecimals)
135         return [NSString localizedStringWithFormat: @"%.2f%%", tr_truncd(progress * 100.0, 2)];
136     else
137         return [NSString localizedStringWithFormat: @"%.1f%%", tr_truncd(progress * 100.0, 1)];
140 + (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds
142     return [NSString timeString: seconds showSeconds: showSeconds maxFields: NSUIntegerMax];
145 + (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max
147     NSAssert(max > 0, @"Cannot generate a time string with no fields");
148     
149     NSMutableArray * timeArray = [NSMutableArray arrayWithCapacity: MIN(max, 4)];
150     NSUInteger remaining = seconds; //causes problems for some users when it's a uint64_t
151     
152     if (seconds >= (24 * 60 * 60))
153     {
154         const NSUInteger days = remaining / (24 * 60 * 60);
155         if (days == 1)
156             [timeArray addObject: NSLocalizedString(@"1 day", "time string")];
157         else
158             [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u days", "time string"), days]];
159         remaining %= (24 * 60 * 60);
160         max--;
161     }
162     if (max > 0 && seconds >= (60 * 60))
163     {
164         [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u hr", "time string"), remaining / (60 * 60)]];
165         remaining %= (60 * 60);
166         max--;
167     }
168     if (max > 0 && (!showSeconds || seconds >= 60))
169     {
170         [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u min", "time string"), remaining / 60]];
171         remaining %= 60;
172         max--;
173     }
174     if (max > 0 && showSeconds)
175         [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u sec", "time string"), remaining]];
176     
177     return [timeArray componentsJoinedByString: @" "];
180 //also used in InfoWindow.xib and MessageWindow.xib
181 - (NSComparisonResult) compareFinder: (NSString *) string
183     if ([NSApp isOnSnowLeopardOrBetter])
184         return [self localizedStandardCompare: string];
185     else
186     {
187         const NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch
188                                                             | NSForcedOrderingSearch;
189         return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
190     }
193 - (NSComparisonResult) compareNumeric: (NSString *) string
195     const NSStringCompareOptions comparisonOptions = NSNumericSearch | NSForcedOrderingSearch;
196     return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
199 @end
201 @implementation NSString (Private)
203 + (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb
205     const CGFloat baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0;
206     
207     if (speed <= 999.95) //0.0 KB/s to 999.9 KB/s
208         return [NSString localizedStringWithFormat: @"%.1f %@", speed, kb];
209     
210     speed /= baseFloat;
211     
212     if (speed <= 99.995) //1.00 MB/s to 99.99 MB/s
213         return [NSString localizedStringWithFormat: @"%.2f %@", speed, mb];
214     else if (speed <= 999.95) //100.0 MB/s to 999.9 MB/s
215         return [NSString localizedStringWithFormat: @"%.1f %@", speed, mb];
216     else //insane speeds
217         return [NSString localizedStringWithFormat: @"%.2f %@", (speed / baseFloat), gb];
220 @end