source: trunk/macosx/InfoActivityViewController.m @ 10369

Last change on this file since 10369 was 10369, checked in by livings124, 12 years ago

add an optional clearView method to the InfoViewController? protocol

File size: 6.9 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 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 *****************************************************************************/
24
25#import "InfoActivityViewController.h"
26#import "NSStringAdditions.h"
27#import "PiecesView.h"
28#import "Torrent.h"
29#include "utils.h" //tr_getRatio()
30
31#define PIECES_CONTROL_PROGRESS 0
32#define PIECES_CONTROL_AVAILABLE 1
33
34@interface InfoActivityViewController (Private)
35
36- (void) setupInfo;
37
38@end
39
40@implementation InfoActivityViewController
41
42- (id) init
43{
44    self = [super initWithNibName: @"InfoActivityView" bundle: nil];
45    return self;
46}
47
48- (void) dealloc
49{
50    [fTorrents release];
51   
52    [super dealloc];
53}
54
55- (void) setInfoForTorrents: (NSArray *) torrents
56{
57    if (fTorrents && [fTorrents isEqualToArray: torrents])
58        return;
59   
60    [fTorrents release];
61    fTorrents = [torrents retain];
62   
63    fSet = NO;
64}
65
66- (void) updateInfo
67{
68    if (!fSet)
69        [self setupInfo];
70   
71    const NSInteger numberSelected = [fTorrents count];
72    if (numberSelected == 0)
73        return;
74   
75    uint64_t have = 0, haveVerified = 0, downloadedTotal = 0, uploadedTotal = 0, failedHash = 0;
76    NSDate * lastActivity = nil;
77    for (Torrent * torrent in fTorrents)
78    {
79        have += [torrent haveTotal];
80        haveVerified += [torrent haveVerified];
81        downloadedTotal += [torrent downloadedTotal];
82        uploadedTotal += [torrent uploadedTotal];
83        failedHash += [torrent failedHash];
84       
85        NSDate * nextLastActivity;
86        if ((nextLastActivity = [torrent dateActivity]))
87            lastActivity = lastActivity ? [lastActivity laterDate: nextLastActivity] : nextLastActivity;
88    }
89   
90    if (have == 0)
91        [fHaveField setStringValue: [NSString stringForFileSize: 0]];
92    else
93    {
94        NSString * verifiedString = [NSString stringWithFormat: NSLocalizedString(@"%@ verified", "Inspector -> Activity tab -> have"),
95                                        [NSString stringForFileSize: haveVerified]];
96        if (have == haveVerified)
97            [fHaveField setStringValue: verifiedString];
98        else
99            [fHaveField setStringValue: [NSString stringWithFormat: @"%@ (%@)", [NSString stringForFileSize: have], verifiedString]];
100    }
101   
102    [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
103    [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
104    [fFailedHashField setStringValue: [NSString stringForFileSize: failedHash]];
105   
106    [fDateActivityField setObjectValue: lastActivity];
107   
108    if (numberSelected == 1)
109    {
110        Torrent * torrent = [fTorrents objectAtIndex: 0];
111       
112        [fStateField setStringValue: [torrent stateString]];
113       
114        if ([torrent isFolder])
115            [fProgressField setStringValue: [NSString localizedStringWithFormat: NSLocalizedString(@"%.2f%% (%.2f%% selected)",
116                "Inspector -> Activity tab -> progress"), 100.0 * [torrent progress], 100.0 * [torrent progressDone]]];
117        else
118            [fProgressField setStringValue: [NSString localizedStringWithFormat: @"%.2f%%", 100.0 * [torrent progress]]];
119           
120        [fRatioField setStringValue: [NSString stringForRatio: [torrent ratio]]];
121       
122        NSString * errorMessage = [torrent errorMessage];
123        if (![errorMessage isEqualToString: [fErrorMessageView string]])
124            [fErrorMessageView setString: errorMessage];
125       
126        [fDateCompletedField setObjectValue: [torrent dateCompleted]];
127       
128        [fPiecesView updateView];
129    }
130    else if (numberSelected > 1)
131    {
132        [fRatioField setStringValue: [NSString stringForRatio: tr_getRatio(uploadedTotal, downloadedTotal)]];
133    }
134    else;
135}
136
137- (void) setPiecesView: (id) sender
138{
139    [self setPiecesViewForAvailable: [sender selectedSegment] == PIECES_CONTROL_AVAILABLE];
140}
141
142- (void) setPiecesViewForAvailable: (BOOL) available
143{
144    [fPiecesControl setSelected: available forSegment: PIECES_CONTROL_AVAILABLE];
145    [fPiecesControl setSelected: !available forSegment: PIECES_CONTROL_PROGRESS];
146   
147    [[NSUserDefaults standardUserDefaults] setBool: available forKey: @"PiecesViewShowAvailability"];
148    [fPiecesView updateView];
149}
150
151- (void) clearView
152{
153    [fPiecesView clearView];
154}
155
156@end
157
158@implementation InfoActivityViewController (Private)
159
160- (void) setupInfo
161{
162    const NSUInteger count = [fTorrents count];
163    if (count != 1)
164    {
165        if (count == 0)
166        {
167            [fHaveField setStringValue: @""];
168            [fDownloadedTotalField setStringValue: @""];
169            [fUploadedTotalField setStringValue: @""];
170            [fFailedHashField setStringValue: @""];
171            [fDateActivityField setStringValue: @""];
172            [fRatioField setStringValue: @""];
173        }
174   
175        [fStateField setStringValue: @""];
176        [fProgressField setStringValue: @""];
177       
178        [fErrorMessageView setString: @""];
179       
180        [fDateAddedField setStringValue: @""];
181        [fDateCompletedField setStringValue: @""];
182       
183        [fPiecesControl setSelected: NO forSegment: PIECES_CONTROL_AVAILABLE];
184        [fPiecesControl setSelected: NO forSegment: PIECES_CONTROL_PROGRESS];
185        [fPiecesControl setEnabled: NO];
186        [fPiecesView setTorrent: nil];
187    }
188    else
189    {
190        Torrent * torrent = [fTorrents objectAtIndex: 0];
191       
192        [fDateAddedField setObjectValue: [torrent dateAdded]];
193       
194        BOOL piecesAvailableSegment = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
195        [fPiecesControl setSelected: piecesAvailableSegment forSegment: PIECES_CONTROL_AVAILABLE];
196        [fPiecesControl setSelected: !piecesAvailableSegment forSegment: PIECES_CONTROL_PROGRESS];
197        [fPiecesControl setEnabled: YES];
198        [fPiecesView setTorrent: torrent];
199    }
200   
201    fSet = YES;
202}
203
204@end
Note: See TracBrowser for help on using the repository browser.