source: trunk/macosx/NameCell.m @ 34

Last change on this file since 34 was 34, checked in by titer, 17 years ago

Imports instead of include
Category for NSString instead of using Utils
Speed string in NSString additions

File size: 4.5 KB
Line 
1/******************************************************************************
2 * Copyright (c) 2005 Eric Petit
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
22
23#import "NameCell.h"
24#import "StringAdditions.h"
25#import "Utils.h"
26
27@implementation NameCell
28
29- (void) setStat: (tr_stat_t *) stat whiteText: (BOOL) w
30{
31    fWhiteText = w;
32
33    fNameString  = [NSString stringWithUTF8String: stat->info.name];
34    fSizeString  = [NSString stringWithFormat: @" (%@)",
35                    [NSString stringForFileSize: stat->info.totalSize]];
36    fTimeString  = @"";
37    fPeersString = @"";
38
39    if( stat->status & TR_STATUS_PAUSE )
40    {
41        fTimeString = [NSString stringWithFormat:
42            @"Paused (%.2f %%)", 100 * stat->progress];
43    }
44    else if( stat->status & TR_STATUS_CHECK )
45    {
46        fTimeString = [NSString stringWithFormat:
47            @"Checking existing files (%.2f %%)", 100 * stat->progress];
48    }
49    else if( stat->status & TR_STATUS_DOWNLOAD )
50    {
51        if( stat->eta < 0 )
52        {
53            fTimeString = [NSString stringWithFormat:
54                @"Finishing in --:--:-- (%.2f %%)", 100 * stat->progress];
55        }
56        else
57        {
58            fTimeString = [NSString stringWithFormat:
59                @"Finishing in %02d:%02d:%02d (%.2f %%)",
60                stat->eta / 3600, ( stat->eta / 60 ) % 60,
61                stat->eta % 60, 100 * stat->progress];
62        }
63        fPeersString = [NSString stringWithFormat:
64            @"Downloading from %d of %d peer%s",
65            stat->peersUploading, stat->peersTotal,
66            ( stat->peersTotal == 1 ) ? "" : "s"];
67    }
68    else if( stat->status & TR_STATUS_SEED )
69    {
70        fTimeString  = [NSString stringWithFormat:
71            @"Seeding, uploading to %d of %d peer%s",
72            stat->peersDownloading, stat->peersTotal,
73            ( stat->peersTotal == 1 ) ? "" : "s"];
74    }
75    else if( stat->status & TR_STATUS_STOPPING )
76    {
77        fTimeString  = @"Stopping...";
78    }
79
80    if( ( stat->status & ( TR_STATUS_DOWNLOAD | TR_STATUS_SEED ) ) &&
81        ( stat->status & TR_TRACKER_ERROR ) )
82    {
83        fPeersString = [NSString stringWithFormat: @"%@%@",
84            @"Error: ", [NSString stringWithUTF8String: stat->error]];
85    }
86}
87
88- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view
89{
90    NSString * string;
91    NSPoint pen;
92    NSMutableDictionary * attributes;
93
94    if( ![view lockFocusIfCanDraw] )
95    {
96        return;
97    }
98
99    pen = cellFrame.origin;
100
101    attributes = [NSMutableDictionary dictionaryWithCapacity: 2];
102    [attributes setObject: fWhiteText ? [NSColor whiteColor] :
103        [NSColor blackColor] forKey: NSForegroundColorAttributeName];
104
105    [attributes setObject: [NSFont messageFontOfSize: 12.0]
106        forKey: NSFontAttributeName];
107
108    pen.x += 5; pen.y += 5;
109    string = [[fNameString stringFittingInWidth: cellFrame.size.width -
110        35 - [fSizeString sizeWithAttributes: attributes].width
111        withAttributes: attributes] stringByAppendingString: fSizeString];
112    [string drawAtPoint: pen withAttributes: attributes];
113
114    [attributes setObject: [NSFont messageFontOfSize: 10.0]
115        forKey: NSFontAttributeName];
116
117    pen.x += 5; pen.y += 20;
118    [fTimeString drawAtPoint: pen withAttributes: attributes];
119
120    pen.x += 0; pen.y += 15;
121    string = [fPeersString stringFittingInWidth: cellFrame.size.width -
122        40 withAttributes: attributes];
123    [string drawAtPoint: pen withAttributes: attributes];
124
125    [view unlockFocus];
126}
127
128@end
Note: See TracBrowser for help on using the repository browser.