source: trunk/macosx/InfoWindowController.m @ 395

Last change on this file since 395 was 395, checked in by titer, 16 years ago

Removed some fields from the info window that are duplicate with the
main window

  • Property svn:keywords set to Date Rev Author Id
File size: 14.9 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 395 2006-06-19 23:16:07Z titer $
3 *
4 * Copyright (c) 2006 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 "InfoWindowController.h"
26#import "StringAdditions.h"
27
28#define RATIO_NO_CHECK_TAG 0
29#define RATIO_GLOBAL_TAG 1
30#define RATIO_CHECK_TAG 2
31
32#define TAB_INFO_IDENT @"Info"
33#define TAB_STATUS_IDENT @"Status"
34#define TAB_OPTIONS_IDENT @"Options"
35#define TAB_FILES_IDENT @"Files"
36
37//15 spacing at the bottom of each tab
38#define TAB_INFO_HEIGHT 196.0
39#define TAB_STATUS_HEIGHT 199.0
40#define TAB_OPTIONS_HEIGHT 82.0
41#define TAB_FILES_HEIGHT 250.0
42
43@implementation InfoWindowController
44
45- (void) awakeFromNib
46{
47    fAppIcon = [[NSApp applicationIconImage] copy];
48   
49    fFiles = [[NSMutableArray alloc] initWithCapacity: 6];
50    [fFileTable setDoubleAction: @selector(revealFile:)];
51   
52    //window location and size
53    NSPanel * window = (NSPanel *)[self window];
54   
55    [window setBecomesKeyOnlyIfNeeded: YES];
56   
57    [window setFrameAutosaveName: @"InspectorWindowFrame"];
58    [window setFrameUsingName: @"InspectorWindowFrame" force: YES];
59   
60    NSRect frame = [window frame];
61    float difference = TAB_INFO_HEIGHT - [[[fTabView selectedTabViewItem]
62                                                    view] frame].size.height;
63    frame.origin.y -= difference;
64    frame.size.height += difference;
65    [window setFrame: frame display: YES];
66}
67
68- (void) dealloc
69{
70    if (fTorrents)
71        [fTorrents release];
72    [fFiles release];
73
74    [fAppIcon release];
75    [super dealloc];
76}
77
78- (void) updateInfoForTorrents: (NSArray *) torrents
79{
80    if (fTorrents)
81        [fTorrents release];
82    fTorrents = [torrents retain];
83
84    int numberSelected = [fTorrents count];
85    if (numberSelected != 1)
86    {
87        if (numberSelected > 0)
88        {
89            [fNameField setStringValue: [NSString stringWithFormat:
90                            @"%d Torrents Selected", numberSelected]];
91       
92            uint64_t size = 0;
93            NSEnumerator * enumerator = [torrents objectEnumerator];
94            Torrent * torrent;
95            while ((torrent = [enumerator nextObject]))
96                size += [torrent size];
97           
98            [fSizeField setStringValue: [[NSString stringForFileSize: size]
99                                stringByAppendingString: @" Total"]];
100        }
101        else
102        {
103            [fNameField setStringValue: @"No Torrents Selected"];
104            [fSizeField setStringValue: @""];
105           
106#if 0
107            [fDownloadRateField setStringValue: @""];
108            [fUploadRateField setStringValue: @""];
109#endif
110           
111            [fDownloadedValidField setStringValue: @""];
112            [fDownloadedTotalField setStringValue: @""];
113            [fUploadedTotalField setStringValue: @""];
114        }
115       
116        [fImageView setImage: fAppIcon];
117       
118        [fNameField setToolTip: nil];
119
120        [fTrackerField setStringValue: @""];
121        [fTrackerField setToolTip: nil];
122        [fAnnounceField setStringValue: @""];
123        [fAnnounceField setToolTip: nil];
124        [fPieceSizeField setStringValue: @""];
125        [fPiecesField setStringValue: @""];
126        [fHashField setStringValue: @""];
127       
128        [fTorrentLocationField setStringValue: @""];
129        [fTorrentLocationField setToolTip: nil];
130        [fDataLocationField setStringValue: @""];
131        [fDataLocationField setToolTip: nil];
132        [fDateStartedField setStringValue: @""];
133       
134#if 0
135        [fStateField setStringValue: @""];
136        [fPercentField setStringValue: @""];
137#endif
138        [fRatioField setStringValue: @""];
139       
140        [fSeedersField setStringValue: @""];
141        [fLeechersField setStringValue: @""];
142        [fConnectedPeersField setStringValue: @""];
143        [fDownloadingFromField setStringValue: @""];
144        [fUploadingToField setStringValue: @""];
145    }
146    else
147    {   
148        Torrent * torrent = [fTorrents objectAtIndex: 0];
149       
150        [fImageView setImage: [torrent icon]];
151       
152        NSString * name = [torrent name];
153        [fNameField setStringValue: name];
154        [fNameField setToolTip: name];
155        [fSizeField setStringValue: [NSString stringForFileSize: [torrent size]]];
156       
157        NSString * tracker = [torrent tracker], * announce = [torrent announce];
158        [fTrackerField setStringValue: tracker];
159        [fTrackerField setToolTip: tracker];
160        [fAnnounceField setStringValue: announce];
161        [fAnnounceField setToolTip: announce];
162        [fPieceSizeField setStringValue: [NSString stringForFileSize: [torrent pieceSize]]];
163        [fPiecesField setIntValue: [torrent pieceCount]];
164        [fHashField setStringValue: [torrent hashString]];
165       
166        [fTorrentLocationField setStringValue: [[torrent torrentLocation]
167                                        stringByAbbreviatingWithTildeInPath]];
168        [fTorrentLocationField setToolTip: [torrent torrentLocation]];
169        [fDataLocationField setStringValue: [[torrent dataLocation]
170                                        stringByAbbreviatingWithTildeInPath]];
171        [fDataLocationField setToolTip: [torrent dataLocation]];
172        [fDateStartedField setObjectValue: [torrent date]];
173    }
174    [self updateInfoStats];
175
176    //set file table
177    [fFiles removeAllObjects];
178   
179    Torrent * torrent;
180    NSEnumerator * enumerator = [fTorrents objectEnumerator];
181    while ((torrent = [enumerator nextObject]))
182        [fFiles addObjectsFromArray: [torrent fileList]];
183   
184    [fFileTable deselectAll: nil];
185    [fFileTable reloadData];
186   
187    //set ratio settings
188    if (numberSelected > 0)
189    {
190        NSEnumerator * enumerator = [fTorrents objectEnumerator];
191        Torrent * torrent = [enumerator nextObject]; //first torrent
192        const int INVALID = -99;
193        int ratioSetting = [torrent stopRatioSetting];
194        float ratioLimit = [torrent ratioLimit];
195       
196        while ((ratioSetting != INVALID || ratioLimit != INVALID)
197                && (torrent = [enumerator nextObject]))
198        {
199            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
200                ratioSetting = INVALID;
201           
202            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
203                ratioLimit = INVALID;
204        }
205       
206        [fRatioMatrix setEnabled: YES];
207       
208        if (ratioSetting == RATIO_CHECK)
209        {
210            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
211            [fRatioLimitField setEnabled: YES];
212        }
213        else
214        {
215            if (ratioSetting == RATIO_NO_CHECK)
216                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
217            else if (ratioSetting == RATIO_GLOBAL)
218                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
219            else
220                [fRatioMatrix deselectAllCells];
221           
222            [fRatioLimitField setEnabled: NO];
223        }
224       
225        if (ratioLimit != INVALID)
226            [fRatioLimitField setFloatValue: ratioLimit];
227        else
228            [fRatioLimitField setStringValue: @""];
229    }
230    else
231    {
232        [fRatioMatrix deselectAllCells];
233        [fRatioMatrix setEnabled: NO];
234       
235        [fRatioLimitField setEnabled: NO];
236        [fRatioLimitField setStringValue: @""];
237    }
238}
239
240- (void) updateInfoStats
241{
242    int numberSelected = [fTorrents count];
243    if (numberSelected > 0)
244    {
245        float downloadRate = 0, uploadRate = 0, downloadedValid;
246        uint64_t downloadedTotal = 0, uploadedTotal = 0;
247        Torrent * torrent;
248        NSEnumerator * enumerator = [fTorrents objectEnumerator];
249        while ((torrent = [enumerator nextObject]))
250        {
251            downloadRate += [torrent downloadRate];
252            uploadRate += [torrent uploadRate];
253           
254            downloadedValid += [torrent downloadedValid];
255            downloadedTotal += [torrent downloadedTotal];
256            uploadedTotal += [torrent uploadedTotal];
257        }
258       
259#if 0
260        [fDownloadRateField setStringValue: [NSString stringForSpeed: downloadRate]];
261        [fUploadRateField setStringValue: [NSString stringForSpeed: uploadRate]];
262#endif
263       
264        [fDownloadedValidField setStringValue: [NSString stringForFileSize: downloadedValid]];
265        [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
266        [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
267   
268        if (numberSelected == 1)
269        {
270            torrent = [fTorrents objectAtIndex: 0];
271           
272#if 0
273            [fStateField setStringValue: [torrent state]];
274            [fPercentField setStringValue: [NSString stringWithFormat:
275                                            @"%.2f%%", 100.0 * [torrent progress]]];
276#endif
277
278            int seeders = [torrent seeders], leechers = [torrent leechers];
279            [fSeedersField setStringValue: seeders < 0 ?
280                @"N/A" : [NSString stringWithInt: seeders]];
281            [fLeechersField setStringValue: leechers < 0 ?
282                @"N/A" : [NSString stringWithInt: leechers]];
283           
284            BOOL active = [torrent isActive];
285           
286            [fConnectedPeersField setStringValue: active ? [NSString
287                    stringWithInt: [torrent totalPeers]] : @"N/A"];
288            [fDownloadingFromField setStringValue: active ? [NSString
289                    stringWithInt: [torrent peersUploading]] : @"N/A"];
290            [fUploadingToField setStringValue: active ? [NSString
291                    stringWithInt: [torrent peersDownloading]] : @"N/A"];
292           
293            [fRatioField setStringValue: [NSString stringForRatioWithDownload:
294                                        downloadedTotal upload: uploadedTotal]];
295        }
296    }
297}
298
299- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
300{
301    SEL action = [menuItem action];
302   
303    if (action == @selector(revealFile:))
304        return [fFileTable numberOfSelectedRows] > 0 &&
305            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
306       
307    return YES;
308}
309
310- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
311{
312    NSWindow * window = [self window];
313    NSRect frame = [window frame];
314
315    float height;
316    NSString * identifier = [tabViewItem identifier];
317    if ([identifier isEqualToString: TAB_INFO_IDENT])
318        height = TAB_INFO_HEIGHT;
319    else if ([identifier isEqualToString: TAB_STATUS_IDENT])
320        height = TAB_STATUS_HEIGHT;
321    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
322        height = TAB_OPTIONS_HEIGHT;
323    else
324        height = TAB_FILES_HEIGHT;
325   
326    NSView * view = [tabViewItem view];
327    float difference = height - [view frame].size.height;
328    frame.origin.y -= difference;
329    frame.size.height += difference;
330   
331    [view setHidden: YES];
332    [window setFrame: frame display: YES animate: YES];
333    [view setHidden: NO];
334}
335
336- (void) setNextTab
337{
338    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]]
339                                    == [fTabView numberOfTabViewItems] - 1)
340        [fTabView selectFirstTabViewItem: nil];
341    else
342        [fTabView selectNextTabViewItem: nil];
343}
344
345- (void) setPreviousTab
346{
347    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
348        [fTabView selectLastTabViewItem: nil];
349    else
350        [fTabView selectPreviousTabViewItem: nil];
351}
352
353- (int) numberOfRowsInTableView: (NSTableView *) tableView
354{
355    return [fFiles count];
356}
357
358- (id) tableView: (NSTableView *) tableView objectValueForTableColumn:
359                    (NSTableColumn *) column row: (int) row
360{
361    NSString * file = [fFiles objectAtIndex: row];
362    if ([[column identifier] isEqualToString: @"Icon"])
363        return [[NSWorkspace sharedWorkspace] iconForFileType: [file pathExtension]];
364    else
365        return [file lastPathComponent];
366}
367
368//only called on >= 10.4
369- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell
370        rect: (NSRectPointer) rect tableColumn: (NSTableColumn *) column
371        row: (int) row mouseLocation: (NSPoint) mouseLocation
372{
373    return [fFiles objectAtIndex: row];
374}
375
376- (void) revealFile: (id) sender
377{
378    NSIndexSet * indexSet = [fFileTable selectedRowIndexes];
379    unsigned int i;
380    for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i])
381        [[NSWorkspace sharedWorkspace] selectFile: [fFiles objectAtIndex: i]
382            inFileViewerRootedAtPath: nil];
383}
384
385- (void) setRatioCheck: (id) sender
386{
387    NSButtonCell * selected = [fRatioMatrix selectedCell];
388    int ratioSetting;
389    if (selected == [fRatioMatrix cellWithTag: RATIO_CHECK_TAG])
390        ratioSetting = RATIO_CHECK;
391    else if (selected == [fRatioMatrix cellWithTag: RATIO_NO_CHECK_TAG])
392        ratioSetting = RATIO_NO_CHECK;
393    else
394        ratioSetting = RATIO_GLOBAL;
395
396    Torrent * torrent;
397    NSEnumerator * enumerator = [fTorrents objectEnumerator];
398    while ((torrent = [enumerator nextObject]))
399        [torrent setStopRatioSetting: ratioSetting];
400   
401    [self setRatioLimit: fRatioLimitField];
402    [fRatioLimitField setEnabled: selected == [fRatioMatrix cellWithTag: RATIO_CHECK_TAG]];
403}
404
405- (void) setRatioLimit: (id) sender
406{
407    Torrent * torrent;
408    NSEnumerator * enumerator = [fTorrents objectEnumerator];
409
410    float ratioLimit = [sender floatValue];
411    if (![[sender stringValue] isEqualToString:
412            [NSString stringWithFormat: @"%.2f", ratioLimit]]
413            || ratioLimit < 0)
414    {
415        NSBeep();
416        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
417        while ((torrent = [enumerator nextObject]))
418            if (ratioLimit != [torrent ratioLimit])
419            {
420                [sender setStringValue: @""];
421                return;
422            }
423       
424        [sender setFloatValue: ratioLimit];
425    }
426    else
427    {
428        while ((torrent = [enumerator nextObject]))
429            [torrent setRatioLimit: ratioLimit];
430    }
431}
432
433@end
Note: See TracBrowser for help on using the repository browser.