source: trunk/macosx/InfoWindowController.m @ 784

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

Status in peers tab.

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