source: trunk/macosx/InfoWindowController.m @ 789

Last change on this file since 789 was 789, checked in by livings124, 16 years ago

Show swarm speed in inspector.

  • Property svn:keywords set to Date Rev Author Id
File size: 19.7 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 789 2006-08-17 03:12:40Z livings124 $
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 MIN_WINDOW_WIDTH 270
33#define MAX_WINDOW_WIDTH 2000
34
35#define TAB_INFO_IDENT @"Info"
36#define TAB_ACTIVITY_IDENT @"Activity"
37#define TAB_PEERS_IDENT @"Peers"
38#define TAB_FILES_IDENT @"Files"
39#define TAB_OPTIONS_IDENT @"Options"
40
41//15 spacing at the bottom of each tab
42#define TAB_INFO_HEIGHT 182.0
43#define TAB_ACTIVITY_HEIGHT 230.0
44#define TAB_PEERS_HEIGHT 255.0
45#define TAB_FILES_HEIGHT 255.0
46#define TAB_OPTIONS_HEIGHT 116.0
47
48@interface InfoWindowController (Private)
49
50- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate;
51
52@end
53
54@implementation InfoWindowController
55
56- (void) awakeFromNib
57{
58    fAppIcon = [[NSApp applicationIconImage] copy];
59    fDotGreen = [NSImage imageNamed: @"GreenDot.tiff"];
60    fDotRed = [NSImage imageNamed: @"RedDot.tiff"];
61    fCheckImage = [NSImage imageNamed: @"NSMenuCheckmark"];
62   
63    fTorrents = [[NSArray alloc] init];
64    fPeers = [[NSMutableArray alloc] initWithCapacity: 6];
65    fFiles = [[NSMutableArray alloc] initWithCapacity: 6];
66    [fFileTable setDoubleAction: @selector(revealFile:)];
67   
68    //window location and size
69    NSPanel * window = (NSPanel *)[self window];
70   
71    [window setBecomesKeyOnlyIfNeeded: YES];
72   
73    [window setFrameAutosaveName: @"InspectorWindowFrame"];
74    [window setFrameUsingName: @"InspectorWindowFrame"];
75   
76    //select tab
77    NSString * identifier = [[NSUserDefaults standardUserDefaults] stringForKey: @"InfoTab"];
78    if ([fTabView indexOfTabViewItemWithIdentifier: identifier] == NSNotFound)
79        identifier = TAB_INFO_IDENT;
80   
81    [fTabView selectTabViewItemWithIdentifier: identifier];
82    [self setWindowForTab: identifier animate: NO];
83   
84    if ([[fPeerTable sortDescriptors] count] == 0)
85        [fPeerTable setSortDescriptors: [NSArray arrayWithObject: [[fPeerTable tableColumnWithIdentifier: @"IP"]
86                                            sortDescriptorPrototype]]];
87   
88    [self updateInfoForTorrents: [NSArray array]];
89}
90
91- (void) dealloc
92{
93    [fTorrents release];
94    [fPeers release];
95    [fFiles release];
96
97    [fAppIcon release];
98    [super dealloc];
99}
100
101- (void) updateInfoForTorrents: (NSArray *) torrents
102{
103    [fTorrents release];
104    fTorrents = [torrents retain];
105
106    int numberSelected = [fTorrents count];
107    if (numberSelected != 1)
108    {
109        if (numberSelected > 0)
110        {
111            [fNameField setStringValue: [NSString stringWithFormat: @"%d Torrents Selected", numberSelected]];
112       
113            uint64_t size = 0;
114            NSEnumerator * enumerator = [torrents objectEnumerator];
115            Torrent * torrent;
116            while ((torrent = [enumerator nextObject]))
117                size += [torrent size];
118           
119            [fSizeField setStringValue: [[NSString stringForFileSize: size]
120                                stringByAppendingString: @" Total"]];
121        }
122        else
123        {
124            [fNameField setStringValue: @"No Torrents Selected"];
125            [fSizeField setStringValue: @""];
126
127            [fDownloadedValidField setStringValue: @""];
128            [fDownloadedTotalField setStringValue: @""];
129            [fUploadedTotalField setStringValue: @""];
130        }
131       
132        [fImageView setImage: fAppIcon];
133       
134        [fNameField setToolTip: nil];
135
136        [fTrackerField setStringValue: @""];
137        [fTrackerField setToolTip: nil];
138        [fAnnounceField setStringValue: @""];
139        [fAnnounceField setToolTip: nil];
140        [fPieceSizeField setStringValue: @""];
141        [fPiecesField setStringValue: @""];
142        [fHashField setStringValue: @""];
143        [fHashField setToolTip: nil];
144       
145        [fTorrentLocationField setStringValue: @""];
146        [fTorrentLocationField setToolTip: nil];
147        [fDataLocationField setStringValue: @""];
148        [fDataLocationField setToolTip: nil];
149        [fDateStartedField setStringValue: @""];
150       
151        //don't allow empty fields to be selected
152        [fTrackerField setSelectable: NO];
153        [fAnnounceField setSelectable: NO];
154        [fHashField setSelectable: NO];
155        [fTorrentLocationField setSelectable: NO];
156        [fDataLocationField setSelectable: NO];
157       
158        [fStateField setStringValue: @""];
159/*
160        [fPercentField setStringValue: @""];
161*/
162        [fRatioField setStringValue: @""];
163       
164        [fSeedersField setStringValue: @""];
165        [fLeechersField setStringValue: @""];
166        [fConnectedPeersField setStringValue: @""];
167        [fDownloadingFromField setStringValue: @""];
168        [fUploadingToField setStringValue: @""];
169        [fSwarmSpeedField setStringValue: @""];
170       
171        [fPeers removeAllObjects];
172        [fPeerTable reloadData];
173        [fPeerTableStatusField setStringValue: @"info not available"];
174    }
175    else
176    {   
177        Torrent * torrent = [fTorrents objectAtIndex: 0];
178       
179        [fImageView setImage: [torrent icon]];
180       
181        NSString * name = [torrent name];
182        [fNameField setStringValue: name];
183        [fNameField setToolTip: name];
184        [fSizeField setStringValue: [NSString stringForFileSize: [torrent size]]];
185       
186        NSString * tracker = [torrent tracker],
187                * announce = [torrent announce],
188                * hashString = [torrent hashString];
189        [fTrackerField setStringValue: tracker];
190        [fTrackerField setToolTip: tracker];
191        [fAnnounceField setStringValue: announce];
192        [fAnnounceField setToolTip: announce];
193        [fPieceSizeField setStringValue: [NSString stringForFileSize: [torrent pieceSize]]];
194        [fPiecesField setIntValue: [torrent pieceCount]];
195        [fHashField setStringValue: hashString];
196        [fHashField setToolTip: hashString];
197       
198        [fTorrentLocationField setStringValue: [torrent torrentLocationString]];
199        [fTorrentLocationField setToolTip: [torrent torrentLocation]];
200        [fDataLocationField setStringValue: [[torrent dataLocation] stringByAbbreviatingWithTildeInPath]];
201        [fDataLocationField setToolTip: [torrent dataLocation]];
202        [fDateStartedField setObjectValue: [torrent date]];
203       
204        //allow these fields to be selected
205        [fTrackerField setSelectable: YES];
206        [fAnnounceField setSelectable: YES];
207        [fHashField setSelectable: YES];
208        [fTorrentLocationField setSelectable: YES];
209        [fDataLocationField setSelectable: YES];
210    }
211   
212    //update stats and settings
213    [self updateInfoStatsAndSettings];
214
215    //set file table
216    [fFiles removeAllObjects];
217   
218    Torrent * torrent;
219    NSEnumerator * enumerator = [fTorrents objectEnumerator];
220    while ((torrent = [enumerator nextObject]))
221        [fFiles addObjectsFromArray: [torrent fileList]];
222   
223    [fFileTable deselectAll: nil];
224    [fFileTable reloadData];
225}
226
227- (void) updateInfoStats
228{
229    int numberSelected = [fTorrents count];
230    if (numberSelected == 0)
231        return;
232   
233    float downloadedValid = 0;
234    uint64_t downloadedTotal = 0, uploadedTotal = 0;
235    Torrent * torrent;
236    NSEnumerator * enumerator = [fTorrents objectEnumerator];
237    while ((torrent = [enumerator nextObject]))
238    {
239        downloadedValid += [torrent downloadedValid];
240        downloadedTotal += [torrent downloadedTotal];
241        uploadedTotal += [torrent uploadedTotal];
242    }
243
244    [fDownloadedValidField setStringValue: [NSString stringForFileSize: downloadedValid]];
245    [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
246    [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
247
248    if (numberSelected == 1)
249    {
250        torrent = [fTorrents objectAtIndex: 0];
251
252        [fStateField setStringValue: [torrent stateString]];
253/*
254        [fPercentField setStringValue: [NSString stringWithFormat: @"%.2f%%", 100.0 * [torrent progress]]];
255*/
256        int seeders = [torrent seeders], leechers = [torrent leechers];
257        [fSeedersField setStringValue: seeders < 0 ? @"" : [NSString stringWithInt: seeders]];
258        [fLeechersField setStringValue: leechers < 0 ? @"" : [NSString stringWithInt: leechers]];
259       
260        BOOL active = [torrent isActive];
261       
262        [fConnectedPeersField setStringValue: active ? [NSString
263                stringWithInt: [torrent totalPeers]] : @""];
264        [fDownloadingFromField setStringValue: active ? [NSString
265                stringWithInt: [torrent peersUploading]] : @""];
266        [fUploadingToField setStringValue: active ? [NSString
267                stringWithInt: [torrent peersDownloading]] : @""];
268       
269        [fRatioField setStringValue: [NSString stringForRatioWithDownload: downloadedTotal upload: uploadedTotal]];
270       
271        [fSwarmSpeedField setStringValue: [torrent isActive] ? [NSString stringForSpeed: [torrent swarmSpeed]] : @""];
272       
273        //set peers table
274        [fPeers setArray: [torrent peers]];
275        [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
276   
277        [fPeerTable reloadData];
278        [fPeerTableStatusField setStringValue: [NSString stringWithFormat: @"%d of %d connected",
279                                                [torrent totalPeers], [fPeers count]]];
280    }
281}
282
283- (void) updateInfoStatsAndSettings
284{
285    int numberSelected = [fTorrents count];
286
287    //set wait to start
288    BOOL waiting = NO, notWaiting = NO, canEnableWaiting = numberSelected > 0,
289        waitingSettingOn = [[[NSUserDefaults standardUserDefaults] stringForKey: @"StartSetting"]
290                                isEqualToString: @"Wait"];
291   
292    NSEnumerator * enumerator = [fTorrents objectEnumerator];
293    Torrent * torrent;
294    while ((torrent = [enumerator nextObject]))
295    {
296        if ([torrent waitingToStart])
297            waiting = YES;
298        else
299            notWaiting = YES;
300       
301        if (canEnableWaiting && !(![torrent isActive] && [torrent progress] < 1.0 && waitingSettingOn))
302            canEnableWaiting = NO;
303    }
304   
305    [fWaitToStartButton setState: waiting && notWaiting ? NSMixedState : (waiting ? NSOnState : NSOffState)];
306    [fWaitToStartButton setEnabled: canEnableWaiting];
307
308    //set ratio settings
309    if (numberSelected > 0)
310    {
311        enumerator = [fTorrents objectEnumerator];
312        torrent = [enumerator nextObject]; //first torrent
313        const int INVALID = -99;
314        int ratioSetting = [torrent stopRatioSetting];
315        float ratioLimit = [torrent ratioLimit];
316       
317        while ((ratioSetting != INVALID || ratioLimit != INVALID)
318                && (torrent = [enumerator nextObject]))
319        {
320            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
321                ratioSetting = INVALID;
322           
323            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
324                ratioLimit = INVALID;
325        }
326       
327        [fRatioMatrix setEnabled: YES];
328       
329        if (ratioSetting == RATIO_CHECK)
330        {
331            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
332            [fRatioLimitField setEnabled: YES];
333        }
334        else
335        {
336            if (ratioSetting == RATIO_NO_CHECK)
337                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
338            else if (ratioSetting == RATIO_GLOBAL)
339                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
340            else
341                [fRatioMatrix deselectAllCells];
342           
343            [fRatioLimitField setEnabled: NO];
344        }
345       
346        if (ratioLimit != INVALID)
347            [fRatioLimitField setFloatValue: ratioLimit];
348        else
349            [fRatioLimitField setStringValue: @""];
350    }
351    else
352    {
353        [fRatioMatrix deselectAllCells];
354        [fRatioMatrix setEnabled: NO];
355       
356        [fRatioLimitField setEnabled: NO];
357        [fRatioLimitField setStringValue: @""];
358    }
359   
360    [self updateInfoStats];
361}
362
363- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
364{
365    SEL action = [menuItem action];
366   
367    if (action == @selector(revealFile:))
368        return [fFileTable numberOfSelectedRows] > 0 &&
369            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
370       
371    return YES;
372}
373
374- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
375{
376    NSRect windowRect = [window frame];
377    windowRect.size.width = [window minSize].width;   
378    return windowRect;
379}
380
381- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
382{
383    NSString * identifier = [tabViewItem identifier];
384    [self setWindowForTab: identifier animate: YES];
385    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InfoTab"];
386}
387
388- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate
389{
390    float height;
391    if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
392        height = TAB_ACTIVITY_HEIGHT;
393    else if ([identifier isEqualToString: TAB_PEERS_IDENT])
394        height = TAB_PEERS_HEIGHT;
395    else if ([identifier isEqualToString: TAB_FILES_IDENT])
396        height = TAB_FILES_HEIGHT;
397    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
398        height = TAB_OPTIONS_HEIGHT;
399    else
400        height = TAB_INFO_HEIGHT;
401   
402    NSWindow * window = [self window];
403    NSRect frame = [window frame];
404    NSView * view = [[fTabView selectedTabViewItem] view];
405   
406    float difference = height - [view frame].size.height;
407    frame.origin.y -= difference;
408    frame.size.height += difference;
409   
410    if (animate)
411    {
412        [view setHidden: YES];
413        [window setFrame: frame display: YES animate: YES];
414        [view setHidden: NO];
415    }
416    else
417        [window setFrame: frame display: YES];
418   
419    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
420    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
421}
422
423- (void) setNextTab
424{
425    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == [fTabView numberOfTabViewItems] - 1)
426        [fTabView selectFirstTabViewItem: nil];
427    else
428        [fTabView selectNextTabViewItem: nil];
429}
430
431- (void) setPreviousTab
432{
433    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
434        [fTabView selectLastTabViewItem: nil];
435    else
436        [fTabView selectPreviousTabViewItem: nil];
437}
438
439- (int) numberOfRowsInTableView: (NSTableView *) tableView
440{
441    if (tableView == fPeerTable)
442        return [fPeers count];
443    else if (tableView == fFileTable)
444        return [fFiles count];
445    else
446        return 0;
447}
448
449- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
450{
451    NSString * ident = [column identifier];
452    if (tableView == fPeerTable)
453    {
454        NSDictionary * peer = [fPeers objectAtIndex: row];
455       
456        if ([ident isEqualToString: @"Connected"])
457            return [[peer objectForKey: @"Connected"] boolValue] ? fDotGreen : fDotRed;
458        else if ([ident isEqualToString: @"UL To"])
459            return [[peer objectForKey: @"UL To"] boolValue] ? fCheckImage : nil;
460        else if ([ident isEqualToString: @"DL From"])
461            return [[peer objectForKey: @"DL From"] boolValue] ? fCheckImage : nil;
462        else if ([ident isEqualToString: @"Client"])
463            return [peer objectForKey: @"Client"];
464        else
465            return [peer objectForKey: @"IP"];
466    }
467    else
468    {
469        NSString * file = [fFiles objectAtIndex: row];
470        if ([ident isEqualToString: @"Icon"])
471            return [[NSWorkspace sharedWorkspace] iconForFileType: [file pathExtension]];
472        else
473            return [file lastPathComponent];
474    }
475}
476
477- (void) tableView: (NSTableView *) tableView didClickTableColumn: (NSTableColumn *) tableColumn
478{
479    if (tableView == fPeerTable)
480    {
481        [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
482        [tableView reloadData];
483    }
484}
485
486- (BOOL) tableView: (NSTableView *) tableView shouldSelectRow:(int) row
487{
488    return tableView != fPeerTable;
489}
490
491//only called on >= 10.4
492- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell
493        rect: (NSRectPointer) rect tableColumn: (NSTableColumn *) column
494        row: (int) row mouseLocation: (NSPoint) mouseLocation
495{
496    return tableView == fFileTable ? [fFiles objectAtIndex: row] : nil;
497}
498
499- (NSArray *) peerSortDescriptors
500{
501    NSMutableArray * descriptors = [NSMutableArray array];
502   
503    NSArray * oldDescriptors = [fPeerTable sortDescriptors];
504    if ([oldDescriptors count] > 0)
505        [descriptors addObject: [oldDescriptors objectAtIndex: 0]];
506   
507    [descriptors addObject: [[fPeerTable tableColumnWithIdentifier: @"IP"] sortDescriptorPrototype]];
508   
509    return descriptors;
510}
511
512- (void) revealFile: (id) sender
513{
514    NSIndexSet * indexSet = [fFileTable selectedRowIndexes];
515    unsigned int i;
516    for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i])
517        [[NSWorkspace sharedWorkspace] selectFile: [fFiles objectAtIndex: i] inFileViewerRootedAtPath: nil];
518}
519
520- (void) setRatioCheck: (id) sender
521{
522    int ratioSetting, tag = [[fRatioMatrix selectedCell] tag];
523    if (tag == RATIO_CHECK_TAG)
524        ratioSetting = RATIO_CHECK;
525    else if (tag == RATIO_NO_CHECK_TAG)
526        ratioSetting = RATIO_NO_CHECK;
527    else
528        ratioSetting = RATIO_GLOBAL;
529
530    Torrent * torrent;
531    NSEnumerator * enumerator = [fTorrents objectEnumerator];
532    while ((torrent = [enumerator nextObject]))
533        [torrent setStopRatioSetting: ratioSetting];
534   
535    [self setRatioLimit: fRatioLimitField];
536    [fRatioLimitField setEnabled: tag == RATIO_CHECK_TAG];
537}
538
539- (void) setRatioLimit: (id) sender
540{
541    Torrent * torrent;
542    NSEnumerator * enumerator = [fTorrents objectEnumerator];
543
544    float ratioLimit = [sender floatValue];
545    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
546            || ratioLimit < 0)
547    {
548        NSBeep();
549        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
550        while ((torrent = [enumerator nextObject]))
551            if (ratioLimit != [torrent ratioLimit])
552            {
553                [sender setStringValue: @""];
554                return;
555            }
556       
557        [sender setFloatValue: ratioLimit];
558    }
559    else
560    {
561        while ((torrent = [enumerator nextObject]))
562            [torrent setRatioLimit: ratioLimit];
563    }
564}
565
566- (void) setWaitToStart: (id) sender
567{
568    int state = [sender state];
569
570    NSEnumerator * enumerator = [fTorrents objectEnumerator];
571    Torrent * torrent;
572    while ((torrent = [enumerator nextObject]))
573        [torrent setWaitToStart: state];
574   
575    [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentStartSettingChange" object: fTorrents];
576}
577
578@end
Note: See TracBrowser for help on using the repository browser.