source: trunk/macosx/StringAdditions.m @ 272

Last change on this file since 272 was 272, checked in by livings124, 16 years ago

First commit on my own 8-)

Sorting by progress, sorting no longer causes crashes.
New look for the table.
Info is now inspector: can set individual and multiple ratio limits, shows listing of files, more info in general, resizes when changing tabs, can change tabs with cmd-left and cmd-right.
Menu items moved to "Transfers" menu.
Sliding status bar in its own view.
Prefs moved into their own nib.
Toolbar items for pause and resume selected (many wanted this, but it needs better icons)
New icons for Transfers and General.
A lot of tweaking of main window to fix alignment etc.
Sparkle used for updated (still needs to be added to website to work).

And a lot more tweaking and changes that I'm too lazy to list.

...now let's hope I commit this right

  • Property svn:keywords set to Date Rev Author Id
File size: 5.2 KB
Line 
1/******************************************************************************
2 * $Id: StringAdditions.m 272 2006-06-06 18:05:57Z livings124 $
3 *
4 * Copyright (c) 2005-2006 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 "StringAdditions.h"
26#import "Utils.h"
27
28@implementation NSString (StringAdditions)
29
30+ (NSString *) stringWithInt: (int) value
31{
32    return [NSString stringWithFormat: @"%d", value];
33}
34
35+ (NSString *) stringForFileSize: (uint64_t) size
36{
37    if (size < 1024)
38    {
39        /* 0 to 1023 bytes */
40        return [NSString stringWithFormat: @"%lld bytes", size];
41    }
42
43    NSString * unit;
44    if (size < 1048576)
45    {
46        unit = @" KB";
47    }
48    else if (size < 1073741824)
49    {
50        unit = @" MB";
51        size /= 1024;
52    }
53    else
54    {
55        unit = @" GB";
56        size /= 1048576;
57    }
58
59    NSString * value;
60    if (size < 10240)
61        /* 1.00 to 9.99 XB */
62        value = [NSString stringWithFormat: @"%lld.%02lld",
63                    size / 1024, ( size % 1024 ) * 100 / 1024];
64    else if (size < 102400)
65        /* 10.0 to 99.9 XB */
66        value = [NSString stringWithFormat: @"%lld.%lld",
67                    size / 1024, ( size % 1024 ) * 10 / 1024];
68    else
69        /* 100+ XB */
70        value = [NSString stringWithFormat: @"%lld", size / 1024];
71
72    return [value stringByAppendingString: unit];
73}
74
75+ (NSString *) stringForSpeed: (float) speed
76{
77    return [[self stringForSpeedAbbrev: speed]
78        stringByAppendingString: @"B/s"];
79}
80
81+ (NSString *) stringForSpeedAbbrev: (float) speed
82{
83    if (speed < 1000)         /* 0.0 K to 999.9 K */
84        return [NSString stringWithFormat: @"%.1f K", speed];
85    else if (speed < 102400)  /* 0.98 M to 99.99 M */
86        return [NSString stringWithFormat: @"%.2f M", speed / 1024];
87    else if (speed < 1024000) /* 100.0 M to 999.9 M */
88        return [NSString stringWithFormat: @"%.1f M", speed / 1024];
89    else                      /* Insane speeds */
90        return [NSString stringWithFormat: @"%.2f G", speed / 1048576];
91}
92
93+ (NSString *) stringForRatioWithDownload: (uint64_t) down upload: (uint64_t) up
94{
95    if (down == 0)
96        return up == 0 ? @"N/A" : [NSString stringWithUTF8String: "\xE2\x88\x9E"];
97
98    float ratio = (float) up / (float) down;
99    if( ratio < 10.0 )
100        return [NSString stringWithFormat: @"%.2f", ratio];
101    else if( ratio < 100.0 )
102        return [NSString stringWithFormat: @"%.1f", ratio];
103    else
104        return [NSString stringWithFormat: @"%.0f", ratio];
105}
106
107- (NSAttributedString *) attributedStringFittingInWidth: (float) width
108                                attributes: (NSDictionary *) attributes
109{
110    int i;
111    float realWidth = [self sizeWithAttributes: attributes].width;
112   
113    /* The whole string fits */
114    if( realWidth <= width )
115        return [[[NSAttributedString alloc] initWithString: self attributes: attributes] autorelease];
116   
117    float ellipsisWidth = [NS_ELLIPSIS sizeWithAttributes: attributes].width;
118   
119    /* Width is too small */
120    if ( ellipsisWidth > width )
121        return [[[NSAttributedString alloc] initWithString: @"" attributes: attributes] autorelease];
122
123    /* Don't worry about ellipsis until the end */
124    width -= ellipsisWidth;
125
126    /* Approximate how many characters we'll need to drop... */
127    i = [self length] * (width / realWidth);
128
129    /* ... then refine it */
130    NSString * newString = [self substringToIndex: i];
131    realWidth = [newString sizeWithAttributes: attributes].width;
132
133    if( realWidth < width )
134    {
135        NSString * smallerString;
136        do
137        {
138            smallerString = newString;
139            newString = [self substringToIndex: ++i];
140        } while ([newString sizeWithAttributes: attributes].width <= width);
141       
142        newString = smallerString;
143    }
144    else if( realWidth > width )
145    {
146        do
147        {
148            newString = [self substringToIndex: --i];
149        } while ([newString sizeWithAttributes: attributes].width > width);
150    }
151    else;
152
153    return [[[NSAttributedString alloc] initWithString: [newString stringByAppendingString: NS_ELLIPSIS]
154                                        attributes: attributes] autorelease];
155}
156
157@end
Note: See TracBrowser for help on using the repository browser.