source: trunk/macosx/InfoWindowController.m @ 800

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

Reveal in file table works again.

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