1 /******************************************************************************
2 * $Id: InfoActivityViewController.m 10832 2010-06-24 00:00:43Z livings124 $
4 * Copyright (c) 2010 Transmission authors and contributors
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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 *****************************************************************************/
25 #import "InfoActivityViewController.h"
26 #import "NSStringAdditions.h"
27 #import "PiecesView.h"
30 #include "transmission.h" // required by utils.h
31 #include "utils.h" //tr_getRatio()
33 #define PIECES_CONTROL_PROGRESS 0
34 #define PIECES_CONTROL_AVAILABLE 1
36 @interface InfoActivityViewController (Private)
40 - (void) updatePiecesView;
44 @implementation InfoActivityViewController
48 if ((self = [super initWithNibName: @"InfoActivityView" bundle: nil]))
50 [self setTitle: NSLocalizedString(@"Activity", "Inspector view -> title")];
58 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updatePiecesView) name: @"UpdatePiecesView" object: nil];
63 [[NSNotificationCenter defaultCenter] removeObserver: self];
70 - (void) setInfoForTorrents: (NSArray *) torrents
72 //don't check if it's the same in case the metadata changed
74 fTorrents = [torrents retain];
84 const NSInteger numberSelected = [fTorrents count];
85 if (numberSelected == 0)
88 uint64_t have = 0, haveVerified = 0, downloadedTotal = 0, uploadedTotal = 0, failedHash = 0;
89 NSDate * lastActivity = nil;
90 for (Torrent * torrent in fTorrents)
92 have += [torrent haveTotal];
93 haveVerified += [torrent haveVerified];
94 downloadedTotal += [torrent downloadedTotal];
95 uploadedTotal += [torrent uploadedTotal];
96 failedHash += [torrent failedHash];
98 NSDate * nextLastActivity;
99 if ((nextLastActivity = [torrent dateActivity]))
100 lastActivity = lastActivity ? [lastActivity laterDate: nextLastActivity] : nextLastActivity;
104 [fHaveField setStringValue: [NSString stringForFileSize: 0]];
107 NSString * verifiedString = [NSString stringWithFormat: NSLocalizedString(@"%@ verified", "Inspector -> Activity tab -> have"),
108 [NSString stringForFileSize: haveVerified]];
109 if (have == haveVerified)
110 [fHaveField setStringValue: verifiedString];
112 [fHaveField setStringValue: [NSString stringWithFormat: @"%@ (%@)", [NSString stringForFileSize: have], verifiedString]];
115 [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
116 [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
117 [fFailedHashField setStringValue: [NSString stringForFileSize: failedHash]];
119 [fDateActivityField setObjectValue: lastActivity];
121 if (numberSelected == 1)
123 Torrent * torrent = [fTorrents objectAtIndex: 0];
125 [fStateField setStringValue: [torrent stateString]];
127 NSString * progressString = [NSString percentString: [torrent progress] longDecimals: YES];
128 if ([torrent isFolder])
130 NSString * progressSelectedString = [NSString stringWithFormat:
131 NSLocalizedString(@"%@ selected", "Inspector -> Activity tab -> progress"),
132 [NSString percentString: [torrent progressDone] longDecimals: YES]];
133 progressString = [progressString stringByAppendingFormat: @" (%@)", progressSelectedString];
135 [fProgressField setStringValue: progressString];
137 [fRatioField setStringValue: [NSString stringForRatio: [torrent ratio]]];
139 NSString * errorMessage = [torrent errorMessage];
140 if (![errorMessage isEqualToString: [fErrorMessageView string]])
141 [fErrorMessageView setString: errorMessage];
143 [fDateCompletedField setObjectValue: [torrent dateCompleted]];
145 [fPiecesView updateView];
147 else if (numberSelected > 1)
149 [fRatioField setStringValue: [NSString stringForRatio: tr_getRatio(uploadedTotal, downloadedTotal)]];
154 - (void) setPiecesView: (id) sender
156 const BOOL availability = [sender selectedSegment] == PIECES_CONTROL_AVAILABLE;
157 [[NSUserDefaults standardUserDefaults] setBool: availability forKey: @"PiecesViewShowAvailability"];
158 [self updatePiecesView];
163 [fPiecesView clearView];
168 @implementation InfoActivityViewController (Private)
172 const NSUInteger count = [fTorrents count];
177 [fHaveField setStringValue: @""];
178 [fDownloadedTotalField setStringValue: @""];
179 [fUploadedTotalField setStringValue: @""];
180 [fFailedHashField setStringValue: @""];
181 [fDateActivityField setStringValue: @""];
182 [fRatioField setStringValue: @""];
185 [fStateField setStringValue: @""];
186 [fProgressField setStringValue: @""];
188 [fErrorMessageView setString: @""];
190 [fDateAddedField setStringValue: @""];
191 [fDateCompletedField setStringValue: @""];
193 [fPiecesControl setSelected: NO forSegment: PIECES_CONTROL_AVAILABLE];
194 [fPiecesControl setSelected: NO forSegment: PIECES_CONTROL_PROGRESS];
195 [fPiecesControl setEnabled: NO];
196 [fPiecesView setTorrent: nil];
200 Torrent * torrent = [fTorrents objectAtIndex: 0];
202 [fDateAddedField setObjectValue: [torrent dateAdded]];
204 const BOOL piecesAvailableSegment = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
205 [fPiecesControl setSelected: piecesAvailableSegment forSegment: PIECES_CONTROL_AVAILABLE];
206 [fPiecesControl setSelected: !piecesAvailableSegment forSegment: PIECES_CONTROL_PROGRESS];
207 [fPiecesControl setEnabled: YES];
209 [fPiecesView setTorrent: torrent];
215 - (void) updatePiecesView
217 const BOOL piecesAvailableSegment = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
219 [fPiecesControl setSelected: piecesAvailableSegment forSegment: PIECES_CONTROL_AVAILABLE];
220 [fPiecesControl setSelected: !piecesAvailableSegment forSegment: PIECES_CONTROL_PROGRESS];
222 [fPiecesView updateView];