Revert "transmission: update from 2.13 to 2.22"
[tomato.git] / release / src / router / transmission / macosx / InfoActivityViewController.m
blob47f6e061ed632793aa4dfa44031e7ef9c826ecf1
1 /******************************************************************************
2  * $Id: InfoActivityViewController.m 10832 2010-06-24 00:00:43Z 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  *****************************************************************************/
25 #import "InfoActivityViewController.h"
26 #import "NSStringAdditions.h"
27 #import "PiecesView.h"
28 #import "Torrent.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)
38 - (void) setupInfo;
40 - (void) updatePiecesView;
42 @end
44 @implementation InfoActivityViewController
46 - (id) init
48     if ((self = [super initWithNibName: @"InfoActivityView" bundle: nil]))
49     {
50         [self setTitle: NSLocalizedString(@"Activity", "Inspector view -> title")];
51     }
52     
53     return self;
56 - (void) awakeFromNib
58     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updatePiecesView) name: @"UpdatePiecesView" object: nil];
61 - (void) dealloc
63     [[NSNotificationCenter defaultCenter] removeObserver: self];
64     
65     [fTorrents release];
66     
67     [super dealloc];
70 - (void) setInfoForTorrents: (NSArray *) torrents
72     //don't check if it's the same in case the metadata changed
73     [fTorrents release];
74     fTorrents = [torrents retain];
75     
76     fSet = NO;
79 - (void) updateInfo
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         NSString * progressString = [NSString percentString: [torrent progress] longDecimals: YES];
128         if ([torrent isFolder])
129         {
130             NSString * progressSelectedString = [NSString stringWithFormat:
131                                                     NSLocalizedString(@"%@ selected", "Inspector -> Activity tab -> progress"),
132                                                     [NSString percentString: [torrent progressDone] longDecimals: YES]];
133             progressString = [progressString stringByAppendingFormat: @" (%@)", progressSelectedString];
134         }
135         [fProgressField setStringValue: progressString];
136             
137         [fRatioField setStringValue: [NSString stringForRatio: [torrent ratio]]];
138         
139         NSString * errorMessage = [torrent errorMessage];
140         if (![errorMessage isEqualToString: [fErrorMessageView string]])
141             [fErrorMessageView setString: errorMessage];
142         
143         [fDateCompletedField setObjectValue: [torrent dateCompleted]];
144         
145         [fPiecesView updateView];
146     }
147     else if (numberSelected > 1)
148     {
149         [fRatioField setStringValue: [NSString stringForRatio: tr_getRatio(uploadedTotal, downloadedTotal)]];
150     }
151     else;
154 - (void) setPiecesView: (id) sender
156     const BOOL availability = [sender selectedSegment] == PIECES_CONTROL_AVAILABLE;
157     [[NSUserDefaults standardUserDefaults] setBool: availability forKey: @"PiecesViewShowAvailability"];
158     [self updatePiecesView];
161 - (void) clearView
163     [fPiecesView clearView];
166 @end
168 @implementation InfoActivityViewController (Private)
170 - (void) setupInfo
172     const NSUInteger count = [fTorrents count];
173     if (count != 1)
174     {
175         if (count == 0)
176         {
177             [fHaveField setStringValue: @""];
178             [fDownloadedTotalField setStringValue: @""];
179             [fUploadedTotalField setStringValue: @""];
180             [fFailedHashField setStringValue: @""];
181             [fDateActivityField setStringValue: @""];
182             [fRatioField setStringValue: @""];
183         }
184     
185         [fStateField setStringValue: @""];
186         [fProgressField setStringValue: @""];
187         
188         [fErrorMessageView setString: @""];
189         
190         [fDateAddedField setStringValue: @""];
191         [fDateCompletedField setStringValue: @""];
192         
193         [fPiecesControl setSelected: NO forSegment: PIECES_CONTROL_AVAILABLE];
194         [fPiecesControl setSelected: NO forSegment: PIECES_CONTROL_PROGRESS];
195         [fPiecesControl setEnabled: NO];
196         [fPiecesView setTorrent: nil];
197     }
198     else
199     {
200         Torrent * torrent = [fTorrents objectAtIndex: 0];
201         
202         [fDateAddedField setObjectValue: [torrent dateAdded]];
203         
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];
208         
209         [fPiecesView setTorrent: torrent];
210     }
211     
212     fSet = YES;
215 - (void) updatePiecesView
217     const BOOL piecesAvailableSegment = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
218     
219     [fPiecesControl setSelected: piecesAvailableSegment forSegment: PIECES_CONTROL_AVAILABLE];
220     [fPiecesControl setSelected: !piecesAvailableSegment forSegment: PIECES_CONTROL_PROGRESS];
221     
222     [fPiecesView updateView];
225 @end