source: trunk/macosx/NSStringAdditions.m @ 9125

Last change on this file since 9125 was 9125, checked in by livings124, 13 years ago

remove distinction between built-in trackers and custom-added trackers; add trackers to the end of the list instead of the beginning

  • Property svn:keywords set to Date Rev Author Id
File size: 5.9 KB
Line 
1/******************************************************************************
2 * $Id: NSStringAdditions.m 9125 2009-09-17 00:33:39Z livings124 $
3 *
4 * Copyright (c) 2005-2009 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#import "utils.h"
28#import <transmission.h>
29
30@implementation NSString (NSStringAdditions)
31
32+ (NSString *) ellipsis
33{
34        return [NSString stringWithUTF8String: "\xE2\x80\xA6"];
35}
36
37- (NSString *) stringByAppendingEllipsis
38{
39        return [self stringByAppendingString: [NSString ellipsis]];
40}
41
42+ (NSString *) stringForFileSize: (uint64_t) size
43{
44    if (size < 1024)
45        return [NSString stringWithFormat: @"%lld %@", size, NSLocalizedString(@"bytes", "File size - bytes")];
46
47    CGFloat convertedSize;
48    NSString * unit;
49    if (size < pow(1024, 2))
50    {
51        convertedSize = size / 1024.0f;
52        unit = NSLocalizedString(@"KB", "File size - kilobytes");
53    }
54    else if (size < pow(1024, 3))
55    {
56        convertedSize = size / powf(1024.0f, 2.0f);
57        unit = NSLocalizedString(@"MB", "File size - megabytes");
58    }
59    else if (size < pow(1024, 4))
60    {
61        convertedSize = size / powf(1024.0f, 3.0f);
62        unit = NSLocalizedString(@"GB", "File size - gigabytes");
63    }
64    else
65    {
66        convertedSize = size / powf(1024.0f, 4.0f);
67        unit = NSLocalizedString(@"TB", "File size - terabytes");
68    }
69   
70    //attempt to have minimum of 3 digits with at least 1 decimal
71    return convertedSize <= 9.995f ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit]
72                                : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit];
73}
74
75+ (NSString *) stringForSpeed: (CGFloat) speed
76{
77    return [[self stringForSpeedAbbrev: speed] stringByAppendingString: NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")];
78}
79
80+ (NSString *) stringForSpeedAbbrev: (CGFloat) speed
81{
82    if (speed <= 999.95f) //0.0 K to 999.9 K
83        return [NSString localizedStringWithFormat: @"%.1f K", speed];
84   
85    speed /= 1024.0f;
86   
87    if (speed <= 99.995f) //0.98 M to 99.99 M
88        return [NSString localizedStringWithFormat: @"%.2f M", speed];
89    else if (speed <= 999.95f) //100.0 M to 999.9 M
90        return [NSString localizedStringWithFormat: @"%.1f M", speed];
91    else //insane speeds
92        return [NSString localizedStringWithFormat: @"%.2f G", (speed / 1024.0f)];
93}
94
95+ (NSString *) stringForRatio: (CGFloat) ratio
96{
97    //N/A is different than libtransmission's
98    if (ratio == TR_RATIO_NA)
99        return NSLocalizedString(@"N/A", "No Ratio");
100   
101    char buf[50];
102    return [NSString stringWithUTF8String: tr_strratio(buf, sizeof(buf), ratio, "\xE2\x88\x9E")];
103}
104
105+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds
106{
107    return [NSString timeString: seconds showSeconds: showSeconds maxFields: NSUIntegerMax];
108}
109
110+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max
111{
112    NSMutableArray * timeArray = [NSMutableArray arrayWithCapacity: MIN(max, 4)];
113    NSUInteger remaining = seconds;
114   
115    if (max > 0 && seconds >= (24 * 60 * 60))
116    {
117        NSUInteger days = remaining / (24 * 60 * 60);
118        if (days == 1)
119            [timeArray addObject: NSLocalizedString(@"1 day", "time string")];
120        else
121            [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u days", "time string"), days]];
122        remaining %= (24 * 60 * 60);
123        max--;
124    }
125    if (max > 0 && seconds >= (60 * 60))
126    {
127        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u hr", "time string"), remaining / (60 * 60)]];
128        remaining %= (60 * 60);
129        max--;
130    }
131    if (max > 0 && (!showSeconds || seconds >= 60))
132    {
133        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u min", "time string"), remaining / 60]];
134        remaining %= 60;
135        max--;
136    }
137    if (max > 0 && showSeconds)
138        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u sec", "time string"), remaining]];
139   
140    return [timeArray componentsJoinedByString: @" "];
141}
142
143//also used in InfoWindow.xib
144- (NSComparisonResult) compareFinder: (NSString *) string
145{
146    if ([NSApp isOnSnowLeopardOrBetter])
147        return [self localizedStandardCompare: string];
148    else
149    {
150        const NSInteger comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch;
151        return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
152    }
153}
154
155- (NSComparisonResult) compareNumeric: (NSString *) string
156{
157    const NSInteger comparisonOptions = NSNumericSearch | NSForcedOrderingSearch;
158    return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
159}
160
161@end
Note: See TracBrowser for help on using the repository browser.