source: trunk/macosx/NameCell.m @ 56

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

Show the file icon in the "Name" cell
Changed 0.5-cvs to 0.5-svn

File size: 5.0 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
37    fIcon = [[NSWorkspace sharedWorkspace] iconForFile:
38                [[NSString stringWithUTF8String: stat->folder]
39                stringByAppendingPathComponent: fNameString]];                 
40    [fIcon setFlipped: YES];
41
42    fTimeString  = @"";
43    fPeersString = @"";
44
45    if( stat->status & TR_STATUS_PAUSE )
46    {
47        fTimeString = [NSString stringWithFormat:
48            @"Paused (%.2f %%)", 100 * stat->progress];
49    }
50    else if( stat->status & TR_STATUS_CHECK )
51    {
52        fTimeString = [NSString stringWithFormat:
53            @"Checking existing files (%.2f %%)", 100 * stat->progress];
54    }
55    else if( stat->status & TR_STATUS_DOWNLOAD )
56    {
57        if( stat->eta < 0 )
58        {
59            fTimeString = [NSString stringWithFormat:
60                @"Finishing in --:--:-- (%.2f %%)", 100 * stat->progress];
61        }
62        else
63        {
64            fTimeString = [NSString stringWithFormat:
65                @"Finishing in %02d:%02d:%02d (%.2f %%)",
66                stat->eta / 3600, ( stat->eta / 60 ) % 60,
67                stat->eta % 60, 100 * stat->progress];
68        }
69        fPeersString = [NSString stringWithFormat:
70            @"Downloading from %d of %d peer%s",
71            stat->peersUploading, stat->peersTotal,
72            ( stat->peersTotal == 1 ) ? "" : "s"];
73    }
74    else if( stat->status & TR_STATUS_SEED )
75    {
76        fTimeString  = [NSString stringWithFormat:
77            @"Seeding, uploading to %d of %d peer%s",
78            stat->peersDownloading, stat->peersTotal,
79            ( stat->peersTotal == 1 ) ? "" : "s"];
80    }
81    else if( stat->status & TR_STATUS_STOPPING )
82    {
83        fTimeString  = @"Stopping...";
84    }
85
86    if( ( stat->status & ( TR_STATUS_DOWNLOAD | TR_STATUS_SEED ) ) &&
87        ( stat->status & TR_TRACKER_ERROR ) )
88    {
89        fPeersString = [NSString stringWithFormat: @"%@%@",
90            @"Error: ", [NSString stringWithUTF8String: stat->error]];
91    }
92}
93
94- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view
95{
96    NSString * string;
97    NSPoint pen;
98    NSMutableDictionary * attributes;
99
100    if( ![view lockFocusIfCanDraw] )
101    {
102        return;
103    }
104
105    pen = cellFrame.origin;
106    float cellWidth = cellFrame.size.width;
107
108    pen.x += 5;
109    pen.y += 5;                                                                 
110    [fIcon drawAtPoint: pen fromRect:
111        NSMakeRect(0,0,[fIcon size].width,[fIcon size].height)
112        operation: NSCompositeSourceOver fraction: 1.0];
113
114    attributes = [NSMutableDictionary dictionaryWithCapacity: 2];
115    [attributes setObject: fWhiteText ? [NSColor whiteColor] :
116        [NSColor blackColor] forKey: NSForegroundColorAttributeName];
117
118    [attributes setObject: [NSFont messageFontOfSize: 12.0]
119        forKey: NSFontAttributeName];
120
121    pen.x += 37;
122    string = [[fNameString stringFittingInWidth: cellWidth -
123        72 - [fSizeString sizeWithAttributes: attributes].width
124        withAttributes: attributes] stringByAppendingString: fSizeString];
125    [string drawAtPoint: pen withAttributes: attributes];
126
127    [attributes setObject: [NSFont messageFontOfSize: 10.0]
128        forKey: NSFontAttributeName];
129
130    pen.x += 5; pen.y += 20;
131    [fTimeString drawAtPoint: pen withAttributes: attributes];
132
133    pen.x += 0; pen.y += 15;
134    string = [fPeersString stringFittingInWidth: cellFrame.size.width -
135        77 withAttributes: attributes];
136    [string drawAtPoint: pen withAttributes: attributes];
137
138    [view unlockFocus];
139}
140
141@end
Note: See TracBrowser for help on using the repository browser.