source: trunk/macosx/InfoActivityViewController.m @ 10448

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

restore ability to toggle pieces view by clicking on it

  • Property svn:keywords set to Date Rev Author Id
File size: 7.4 KB
Line 
1/******************************************************************************
2 * $Id: InfoActivityViewController.m 10448 2010-04-06 01:41:16Z livings124 $
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- (void) updatePiecesView;
39
40@end
41
42@implementation InfoActivityViewController
43
44- (id) init
45{
46    if ((self = [super initWithNibName: @"InfoActivityView" bundle: nil]))
47    {
48        [self setTitle: NSLocalizedString(@"Activity", "Inspector view -> title")];
49    }
50   
51    return self;
52}
53
54- (void) awakeFromNib
55{
56    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updatePiecesView) name: @"UpdatePiecesView" object: nil];
57}
58
59- (void) dealloc
60{
61    [[NSNotificationCenter defaultCenter] removeObserver: self];
62   
63    [fTorrents release];
64   
65    [super dealloc];
66}
67
68- (void) setInfoForTorrents: (NSArray *) torrents
69{
70    if (fTorrents && [fTorrents isEqualToArray: torrents])
71        return;
72   
73    [fTorrents release];
74    fTorrents = [torrents retain];
75   
76    fSet = NO;
77}
78
79- (void) updateInfo
80{
81    if (!fSet)
82        [self setupInfo];
83   
84    const NSInteger numberSelected = [fTorrents count];
85    if (numberSelected == 0)
86        return;
87   
88    uint64_t have = 0, haveVerified = 0, downloadedTotal = 0, uploadedTotal = 0, failedHash = 0;
89    NSDate * lastActivity = nil;
90    for (Torrent * torrent in fTorrents)
91    {
92        have += [torrent haveTotal];
93        haveVerified += [torrent haveVerified];
94        downloadedTotal += [torrent downloadedTotal];
95        uploadedTotal += [torrent uploadedTotal];
96        failedHash += [torrent failedHash];
97       
98        NSDate * nextLastActivity;
99        if ((nextLastActivity = [torrent dateActivity]))
100            lastActivity = lastActivity ? [lastActivity laterDate: nextLastActivity] : nextLastActivity;
101    }
102   
103    if (have == 0)
104        [fHaveField setStringValue: [NSString stringForFileSize: 0]];
105    else
106    {
107        NSString * verifiedString = [NSString stringWithFormat: NSLocalizedString(@"%@ verified", "Inspector -> Activity tab -> have"),
108                                        [NSString stringForFileSize: haveVerified]];
109        if (have == haveVerified)
110            [fHaveField setStringValue: verifiedString];
111        else
112            [fHaveField setStringValue: [NSString stringWithFormat: @"%@ (%@)", [NSString stringForFileSize: have], verifiedString]];
113    }
114   
115    [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
116    [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
117    [fFailedHashField setStringValue: [NSString stringForFileSize: failedHash]];
118   
119    [fDateActivityField setObjectValue: lastActivity];
120   
121    if (numberSelected == 1)
122    {
123        Torrent * torrent = [fTorrents objectAtIndex: 0];
124       
125        [fStateField setStringValue: [torrent stateString]];
126       
127        if ([torrent isFolder])
128            [fProgressField setStringValue: [NSString localizedStringWithFormat: NSLocalizedString(@"%.2f%% (%.2f%% selected)",
129                "Inspector -> Activity tab -> progress"), 100.0 * [torrent progress], 100.0 * [torrent progressDone]]];
130        else
131            [fProgressField setStringValue: [NSString localizedStringWithFormat: @"%.2f%%", 100.0 * [torrent progress]]];
132           
133        [fRatioField setStringValue: [NSString stringForRatio: [torrent ratio]]];
134       
135        NSString * errorMessage = [torrent errorMessage];
136        if (![errorMessage isEqualToString: [fErrorMessageView string]])
137            [fErrorMessageView setString: errorMessage];
138       
139        [fDateCompletedField setObjectValue: [torrent dateCompleted]];
140       
141        [fPiecesView updateView];
142    }
143    else if (numberSelected > 1)
144    {
145        [fRatioField setStringValue: [NSString stringForRatio: tr_getRatio(uploadedTotal, downloadedTotal)]];
146    }
147    else;
148}
149
150- (void) setPiecesView: (id) sender
151{
152    const BOOL availability = [sender selectedSegment] == PIECES_CONTROL_AVAILABLE;
153    [[NSUserDefaults standardUserDefaults] setBool: availability forKey: @"PiecesViewShowAvailability"];
154    [self updatePiecesView];
155}
156
157- (void) clearView
158{
159    [fPiecesView clearView];
160}
161
162@end
163
164@implementation InfoActivityViewController (Private)
165
166- (void) setupInfo
167{
168    const NSUInteger count = [fTorrents count];
169    if (count != 1)
170    {
171        if (count == 0)
172        {
173            [fHaveField setStringValue: @""];
174            [fDownloadedTotalField setStringValue: @""];
175            [fUploadedTotalField setStringValue: @""];
176            [fFailedHashField setStringValue: @""];
177            [fDateActivityField setStringValue: @""];
178            [fRatioField setStringValue: @""];
179        }
180   
181        [fStateField setStringValue: @""];
182        [fProgressField setStringValue: @""];
183       
184        [fErrorMessageView setString: @""];
185       
186        [fDateAddedField setStringValue: @""];
187        [fDateCompletedField setStringValue: @""];
188       
189        [fPiecesControl setSelected: NO forSegment: PIECES_CONTROL_AVAILABLE];
190        [fPiecesControl setSelected: NO forSegment: PIECES_CONTROL_PROGRESS];
191        [fPiecesControl setEnabled: NO];
192        [fPiecesView setTorrent: nil];
193    }
194    else
195    {
196        Torrent * torrent = [fTorrents objectAtIndex: 0];
197       
198        [fDateAddedField setObjectValue: [torrent dateAdded]];
199       
200        const BOOL piecesAvailableSegment = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
201        [fPiecesControl setSelected: piecesAvailableSegment forSegment: PIECES_CONTROL_AVAILABLE];
202        [fPiecesControl setSelected: !piecesAvailableSegment forSegment: PIECES_CONTROL_PROGRESS];
203        [fPiecesControl setEnabled: YES];
204       
205        [fPiecesView setTorrent: torrent];
206    }
207   
208    fSet = YES;
209}
210
211- (void) updatePiecesView
212{
213    const BOOL piecesAvailableSegment = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
214   
215    [fPiecesControl setSelected: piecesAvailableSegment forSegment: PIECES_CONTROL_AVAILABLE];
216    [fPiecesControl setSelected: !piecesAvailableSegment forSegment: PIECES_CONTROL_PROGRESS];
217   
218    [fPiecesView updateView];
219}
220
221@end
Note: See TracBrowser for help on using the repository browser.