source: trunk/macosx/InfoWindowController.m @ 1579

Last change on this file since 1579 was 1579, checked in by joshe, 15 years ago

Merge PEX branch, I hope this works.

  • Property svn:keywords set to Date Rev Author Id
File size: 33.6 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 1579 2007-03-23 08:28:01Z joshe $
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 MIN_WINDOW_WIDTH 300
29#define MAX_WINDOW_WIDTH 5000
30
31#define FILE_ROW_SMALL_HEIGHT 18.0
32
33#define TAB_INFO_IDENT @"Info"
34#define TAB_ACTIVITY_IDENT @"Activity"
35#define TAB_PEERS_IDENT @"Peers"
36#define TAB_FILES_IDENT @"Files"
37#define TAB_OPTIONS_IDENT @"Options"
38
39//15 spacing at the bottom of each tab
40#define TAB_INFO_HEIGHT 284.0
41#define TAB_ACTIVITY_HEIGHT 170.0
42#define TAB_PEERS_HEIGHT 279.0
43#define TAB_FILES_HEIGHT 279.0
44#define TAB_OPTIONS_HEIGHT 117.0
45
46#define OPTION_POPUP_GLOBAL 0
47#define OPTION_POPUP_NO_LIMIT 1
48#define OPTION_POPUP_LIMIT 2
49
50#define INVALID -99
51
52@interface InfoWindowController (Private)
53
54- (void) updateInfoGeneral;
55- (void) updateInfoActivity;
56- (void) updateInfoPeers;
57- (void) updateInfoFiles;
58- (void) updateInfoSettings;
59
60- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate;
61- (NSArray *) peerSortDescriptors;
62
63- (int) stateSettingToPopUpIndex: (int) index;
64- (int) popUpIndexToStateSetting: (int) index;
65
66@end
67
68@implementation InfoWindowController
69
70- (id) initWithWindowNibName: (NSString *) name
71{
72    if ((self = [super initWithWindowNibName: name]))
73    {
74        fAppIcon = [NSImage imageNamed: @"NSApplicationIcon"];
75        fDotGreen = [NSImage imageNamed: @"GreenDot.tiff"];
76        fDotRed = [NSImage imageNamed: @"RedDot.tiff"];
77       
78        fFolderIcon = [[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')] copy];
79    }
80    return self;
81}
82
83- (void) awakeFromNib
84{
85    //window location and size
86    NSPanel * window = (NSPanel *)[self window];
87   
88    [window setBecomesKeyOnlyIfNeeded: YES];
89   
90    [window setFrameAutosaveName: @"InspectorWindowFrame"];
91    [window setFrameUsingName: @"InspectorWindowFrame"];
92   
93    //select tab
94    NSString * identifier = [[NSUserDefaults standardUserDefaults] stringForKey: @"InfoTab"];
95    if ([fTabView indexOfTabViewItemWithIdentifier: identifier] == NSNotFound)
96        identifier = TAB_INFO_IDENT;
97   
98    [fTabView selectTabViewItemWithIdentifier: identifier];
99    [self setWindowForTab: identifier animate: NO];
100   
101    //initially sort peer table by IP
102    if ([[fPeerTable sortDescriptors] count] == 0)
103        [fPeerTable setSortDescriptors: [NSArray arrayWithObject: [[fPeerTable tableColumnWithIdentifier: @"IP"]
104                                            sortDescriptorPrototype]]];
105   
106    //set file table
107    [fFileOutline setDoubleAction: @selector(revealFile:)];
108   
109    //set blank inspector
110    [self updateInfoForTorrents: [NSArray array]];
111}
112
113- (void) dealloc
114{
115    if (fTorrents)
116        [fTorrents release];
117    if (fPeers)
118        [fPeers release];
119    if (fFiles)
120        [fFiles release];
121   
122    [fFolderIcon release];
123    [super dealloc];
124}
125
126- (void) updateInfoForTorrents: (NSArray *) torrents
127{
128    if (fTorrents)
129        [fTorrents release];
130    fTorrents = [torrents retain];
131
132    int numberSelected = [fTorrents count];
133    if (numberSelected != 1)
134    {
135        if (numberSelected > 0)
136        {
137            [fNameField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%d Torrents Selected",
138                                            "Inspector -> above tabs -> selected torrents"), numberSelected]];
139       
140            uint64_t size = 0;
141            NSEnumerator * enumerator = [torrents objectEnumerator];
142            Torrent * torrent;
143            while ((torrent = [enumerator nextObject]))
144                size += [torrent size];
145           
146            [fSizeField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ Total",
147                "Inspector -> above tabs -> total size (several torrents selected)"), [NSString stringForFileSize: size]]];
148        }
149        else
150        {
151            [fNameField setStringValue: NSLocalizedString(@"No Torrents Selected",
152                                                            "Inspector -> above tabs -> selected torrents")];
153            [fSizeField setStringValue: @""];
154   
155            [fDownloadedValidField setStringValue: @""];
156            [fDownloadedTotalField setStringValue: @""];
157            [fUploadedTotalField setStringValue: @""];
158        }
159       
160        [fImageView setImage: fAppIcon];
161       
162        [fNameField setToolTip: nil];
163
164        [fTrackerField setStringValue: @""];
165        [fTrackerField setToolTip: nil];
166        [fPiecesField setStringValue: @""];
167        [fHashField setStringValue: @""];
168        [fHashField setToolTip: nil];
169        [fSecureField setStringValue: @""];
170        [fCommentView setString: @""];
171       
172        [fCreatorField setStringValue: @""];
173        [fDateCreatedField setStringValue: @""];
174       
175        [fTorrentLocationField setStringValue: @""];
176        [fTorrentLocationField setToolTip: nil];
177        [fDataLocationField setStringValue: @""];
178        [fDataLocationField setToolTip: nil];
179        [fDateStartedField setStringValue: @""];
180        [fCommentView setSelectable: NO];
181       
182        [fRevealDataButton setHidden: YES];
183        [fRevealTorrentButton setHidden: YES];
184       
185        //don't allow empty fields to be selected
186        [fTrackerField setSelectable: NO];
187        [fHashField setSelectable: NO];
188        [fCreatorField setSelectable: NO];
189        [fTorrentLocationField setSelectable: NO];
190        [fDataLocationField setSelectable: NO];
191       
192        [fStateField setStringValue: @""];
193        [fRatioField setStringValue: @""];
194       
195        [fSeedersField setStringValue: @""];
196        [fLeechersField setStringValue: @""];
197        [fCompletedFromTrackerField setStringValue: @""];
198        [fConnectedPeersField setStringValue: NSLocalizedString(@"info not available", "Inspector -> Peers tab -> peers")];
199        [fDownloadingFromField setStringValue: @""];
200        [fUploadingToField setStringValue: @""];
201        [fSwarmSpeedField setStringValue: @""];
202        [fErrorMessageView setString: @""];
203        [fErrorMessageView setSelectable: NO];
204       
205        [fPiecesView setTorrent: nil];
206       
207        if (fPeers)
208        {
209            [fPeers release];
210            fPeers = nil;
211        }
212       
213        if (fFiles)
214        {
215            [fFiles release];
216            fFiles = nil;
217        }
218        [fFileTableStatusField setStringValue: NSLocalizedString(@"info not available",
219                                        "Inspector -> Files tab -> bottom text (number of files)")];
220    }
221    else
222    {   
223        Torrent * torrent = [fTorrents objectAtIndex: 0];
224       
225        [fImageView setImage: [torrent icon]];
226       
227        NSString * name = [torrent name];
228        [fNameField setStringValue: name];
229        [fNameField setToolTip: name];
230        [fSizeField setStringValue: [NSString stringForFileSize: [torrent size]]];
231       
232        NSString * hashString = [torrent hashString],
233                * commentString = [torrent comment];
234        [fPiecesField setStringValue: [NSString stringWithFormat: @"%d, %@", [torrent pieceCount],
235                                        [NSString stringForFileSize: [torrent pieceSize]]]];
236        [fHashField setStringValue: hashString];
237        [fHashField setToolTip: hashString];
238        [fSecureField setStringValue: [torrent privateTorrent]
239                        ? NSLocalizedString(@"Private Torrent, PEX disabled", "Inspector -> is private torrent")
240                        : NSLocalizedString(@"Public Torrent", "Inspector -> is not private torrent")];
241        [fCommentView setString: commentString];
242       
243        [fCreatorField setStringValue: [torrent creator]];
244        [fDateCreatedField setObjectValue: [torrent dateCreated]];
245       
246        BOOL publicTorrent = [torrent publicTorrent];
247        [fTorrentLocationField setStringValue: publicTorrent
248                    ? [[torrent publicTorrentLocation] stringByAbbreviatingWithTildeInPath]
249                    : NSLocalizedString(@"Transmission Support Folder", "Torrent -> location when deleting original")];
250        if (publicTorrent)
251            [fTorrentLocationField setToolTip: [NSString stringWithFormat: @"%@\n\n%@",
252                        [torrent publicTorrentLocation], [torrent torrentLocation]]];
253        else
254            [fTorrentLocationField setToolTip: [torrent torrentLocation]];
255       
256        [fDateStartedField setObjectValue: [torrent date]];
257       
258        [fRevealDataButton setHidden: NO];
259        [fRevealTorrentButton setHidden: ![torrent publicTorrent]];
260       
261        //allow these fields to be selected
262        [fTrackerField setSelectable: YES];
263        [fHashField setSelectable: YES];
264        [fCommentView setSelectable: YES];
265        [fCreatorField setSelectable: YES];
266        [fTorrentLocationField setSelectable: YES];
267        [fDataLocationField setSelectable: YES];
268       
269        [fPiecesView setTorrent: torrent];
270       
271        //set file table
272        [fFileOutline deselectAll: nil];
273        if (fFiles)
274            [fFiles release];
275        fFiles = [[torrent fileList] retain];
276       
277        [self updateInfoFiles];
278       
279        int fileCount = [torrent fileCount];
280        if (fileCount != 1)
281            [fFileTableStatusField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%d files total",
282                                "Inspector -> Files tab -> bottom text (number of files)"), fileCount]];
283        else
284            [fFileTableStatusField setStringValue: NSLocalizedString(@"1 file total",
285                                "Inspector -> Files tab -> bottom text (number of files)")];
286    }
287   
288    //update stats and settings
289    [self updateInfoStats];
290    [self updateInfoSettings];
291   
292    [fPeerTable reloadData];
293    [fFileOutline deselectAll: nil];
294    [fFileOutline reloadData];
295}
296
297- (void) updateInfoStats
298{
299    NSString * ident = [[fTabView selectedTabViewItem] identifier];
300    if ([ident isEqualToString: TAB_ACTIVITY_IDENT])
301        [self updateInfoActivity];
302    else if ([ident isEqualToString: TAB_PEERS_IDENT])
303        [self updateInfoPeers];
304    else if ([ident isEqualToString: TAB_INFO_IDENT])
305        [self updateInfoGeneral];
306    else if ([ident isEqualToString: TAB_FILES_IDENT])
307        [self updateInfoFiles];
308    else;
309}
310
311- (void) updateInfoGeneral
312{   
313    int numberSelected = [fTorrents count];
314    if (numberSelected != 1)
315        return;
316   
317    Torrent * torrent = [fTorrents objectAtIndex: 0];
318   
319    NSString * tracker = [[torrent trackerAddress] stringByAppendingString: [torrent trackerAddressAnnounce]];
320    [fTrackerField setStringValue: tracker];
321    [fTrackerField setToolTip: tracker];
322   
323    NSString * location = [torrent dataLocation];
324    [fDataLocationField setStringValue: [location stringByAbbreviatingWithTildeInPath]];
325    [fDataLocationField setToolTip: location];
326}
327
328- (void) updateInfoActivity
329{
330    int numberSelected = [fTorrents count];
331    if (numberSelected == 0)
332        return;
333   
334    float downloadedValid = 0;
335    uint64_t downloadedTotal = 0, uploadedTotal = 0;
336    Torrent * torrent;
337    NSEnumerator * enumerator = [fTorrents objectEnumerator];
338    while ((torrent = [enumerator nextObject]))
339    {
340        downloadedValid += [torrent downloadedValid];
341        downloadedTotal += [torrent downloadedTotal];
342        uploadedTotal += [torrent uploadedTotal];
343    }
344
345    [fDownloadedValidField setStringValue: [NSString stringForFileSize: downloadedValid]];
346    [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
347    [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
348   
349    if (numberSelected == 1)
350    {
351        torrent = [fTorrents objectAtIndex: 0];
352       
353        //append percentage to amount downloaded if 1 torrent
354        [fDownloadedValidField setStringValue: [[fDownloadedValidField stringValue]
355                                        stringByAppendingFormat: @" (%.2f%%)", 100.0 * [torrent progress]]];
356       
357        [fStateField setStringValue: [torrent stateString]];
358        [fRatioField setStringValue: [NSString stringForRatio: [torrent ratio]]];
359        [fSwarmSpeedField setStringValue: [torrent isActive] ? [NSString stringForSpeed: [torrent swarmSpeed]] : @""];
360       
361        NSString * errorMessage = [torrent errorMessage];
362        if (![errorMessage isEqualToString: [fErrorMessageView string]])
363        {
364            [fErrorMessageView setString: errorMessage];
365            [fErrorMessageView setSelectable: ![errorMessage isEqualToString: @""]];
366        }
367       
368        [fPiecesView updateView: NO];
369    }
370}
371
372- (void) updateInfoPeers
373{
374    if ([fTorrents count] != 1)
375        return;
376    Torrent * torrent = [fTorrents objectAtIndex: 0];
377   
378    int seeders = [torrent seeders], leechers = [torrent leechers], downloaded = [torrent completedFromTracker];
379    [fSeedersField setStringValue: seeders < 0 ? @"" : [NSString stringWithInt: seeders]];
380    [fLeechersField setStringValue: leechers < 0 ? @"" : [NSString stringWithInt: leechers]];
381    [fCompletedFromTrackerField setStringValue: downloaded < 0 ? @"" : [NSString stringWithInt: downloaded]];
382   
383    BOOL active = [torrent isActive];
384   
385    if (active)
386    {
387        int total = [torrent totalPeers];
388        NSString * connected = [NSString stringWithFormat:
389                                NSLocalizedString(@"%d Connected", "Inspector -> Peers tab -> peers"), total];
390       
391        if (total > 0)
392        {
393            NSMutableArray * components = [NSMutableArray arrayWithCapacity: 4];
394            int count;
395            if ((count = [torrent totalPeersTracker]) > 0)
396                [components addObject: [NSString stringWithFormat:
397                                        NSLocalizedString(@"%d tracker", "Inspector -> Peers tab -> peers"), count]];
398            if ((count = [torrent totalPeersIncoming]) > 0)
399                [components addObject: [NSString stringWithFormat:
400                                        NSLocalizedString(@"%d incoming", "Inspector -> Peers tab -> peers"), count]];
401            if ((count = [torrent totalPeersPex]) > 0)
402                [components addObject: [NSString stringWithFormat:
403                                        NSLocalizedString(@"%d PEX", "Inspector -> Peers tab -> peers"), count]];
404            if ((count = [torrent totalPeersCache]) > 0)
405                [components addObject: [NSString stringWithFormat:
406                                        NSLocalizedString(@"%d cache", "Inspector -> Peers tab -> peers"), count]];
407           
408            connected = [NSString stringWithFormat: @"%@: %@", connected, [components componentsJoinedByString: @", "]];
409        }
410       
411        [fConnectedPeersField setStringValue: connected];
412    }
413    else
414        [fConnectedPeersField setStringValue: NSLocalizedString(@"info not available", "Inspector -> Peers tab -> peers")];
415   
416    [fDownloadingFromField setStringValue: active ? [NSString stringWithInt: [torrent peersUploading]] : @""];
417    [fUploadingToField setStringValue: active ? [NSString stringWithInt: [torrent peersDownloading]] : @""];
418   
419    if (fPeers)
420        [fPeers release];
421    fPeers = [[[torrent peers] sortedArrayUsingDescriptors: [self peerSortDescriptors]] retain];
422   
423    [fPeerTable reloadData];
424}
425
426- (void) updateInfoFiles
427{
428    if ([fTorrents count] != 1)
429        return;
430   
431    if ([[fTorrents objectAtIndex: 0] updateFileProgress])
432        [fFileOutline reloadData];
433}
434
435- (void) updateInfoSettings
436{
437    int numberSelected = [fTorrents count];
438
439    if (numberSelected > 0)
440    {
441        Torrent * torrent;
442       
443        //set bandwidth limits
444        NSEnumerator * enumerator = [fTorrents objectEnumerator];
445        torrent = [enumerator nextObject]; //first torrent
446       
447        int checkUpload = [torrent checkUpload],
448            checkDownload = [torrent checkDownload],
449            uploadLimit = [torrent uploadLimit],
450            downloadLimit = [torrent downloadLimit];
451       
452        while ((checkUpload != INVALID || uploadLimit != INVALID
453                || checkDownload != INVALID || downloadLimit != INVALID)
454                && (torrent = [enumerator nextObject]))
455        {
456            if (checkUpload != INVALID && checkUpload != [torrent checkUpload])
457                checkUpload = INVALID;
458           
459            if (uploadLimit != INVALID && uploadLimit != [torrent uploadLimit])
460                uploadLimit = INVALID;
461           
462            if (checkDownload != INVALID && checkDownload != [torrent checkDownload])
463                checkDownload = INVALID;
464           
465            if (downloadLimit != INVALID && downloadLimit != [torrent downloadLimit])
466                downloadLimit = INVALID;
467        }
468       
469        [fUploadLimitPopUp setEnabled: YES];
470        [fUploadLimitPopUp selectItemAtIndex: [self stateSettingToPopUpIndex: checkUpload]];
471        [fUploadLimitLabel setHidden: checkUpload != NSOnState];
472        [fUploadLimitField setHidden: checkUpload != NSOnState];
473        if (uploadLimit != INVALID)
474            [fUploadLimitField setIntValue: uploadLimit];
475        else
476            [fUploadLimitField setStringValue: @""];
477       
478        [fDownloadLimitPopUp setEnabled: YES];
479        [fDownloadLimitPopUp selectItemAtIndex: [self stateSettingToPopUpIndex: checkDownload]];
480        [fDownloadLimitLabel setHidden: checkDownload != NSOnState];
481        [fDownloadLimitField setHidden: checkDownload != NSOnState];
482        if (downloadLimit != INVALID)
483            [fDownloadLimitField setIntValue: downloadLimit];
484        else
485            [fDownloadLimitField setStringValue: @""];
486       
487        //set ratio settings
488        enumerator = [fTorrents objectEnumerator];
489        torrent = [enumerator nextObject]; //first torrent
490       
491        int checkRatio = [torrent ratioSetting];
492        float ratioLimit = [torrent ratioLimit];
493       
494        while ((checkRatio != INVALID || checkRatio != INVALID)
495                && (torrent = [enumerator nextObject]))
496        {
497            if (checkRatio != INVALID && checkRatio != [torrent ratioSetting])
498                checkRatio = INVALID;
499           
500            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
501                ratioLimit = INVALID;
502        }
503       
504        [fRatioPopUp setEnabled: YES];
505        [fRatioPopUp selectItemAtIndex: [self stateSettingToPopUpIndex: checkRatio]];
506        [fRatioLimitField setHidden: checkRatio != NSOnState];
507        if (ratioLimit != INVALID)
508            [fRatioLimitField setFloatValue: ratioLimit];
509        else
510            [fRatioLimitField setStringValue: @""];
511    }
512    else
513    {
514        [fUploadLimitPopUp setEnabled: NO];
515        [fUploadLimitPopUp selectItemAtIndex: -1];
516        [fUploadLimitField setHidden: YES];
517        [fUploadLimitLabel setHidden: YES];
518        [fUploadLimitField setStringValue: @""];
519       
520        [fDownloadLimitPopUp setEnabled: NO];
521        [fDownloadLimitPopUp selectItemAtIndex: -1];
522        [fDownloadLimitField setHidden: YES];
523        [fDownloadLimitLabel setHidden: YES];
524        [fDownloadLimitField setStringValue: @""];
525       
526        [fRatioPopUp setEnabled: NO];
527        [fRatioPopUp selectItemAtIndex: -1];
528        [fRatioLimitField setHidden: YES];
529        [fRatioLimitField setStringValue: @""];
530    }
531   
532    [self updateInfoStats];
533}
534
535- (int) stateSettingToPopUpIndex: (int) index
536{
537    if (index == NSOnState)
538        return OPTION_POPUP_LIMIT;
539    else if (index == NSOffState)
540        return OPTION_POPUP_NO_LIMIT;
541    else if (index == NSMixedState)
542        return OPTION_POPUP_GLOBAL;
543    else
544        return -1;
545}
546
547- (int) popUpIndexToStateSetting: (int) index
548{
549    if (index == OPTION_POPUP_LIMIT)
550        return NSOnState;
551    else if (index == OPTION_POPUP_NO_LIMIT)
552        return NSOffState;
553    else if (index == OPTION_POPUP_GLOBAL)
554        return NSMixedState;
555    else
556        return INVALID;
557}
558
559- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
560{
561    SEL action = [menuItem action];
562   
563    if (action == @selector(revealFile:))
564        return [fFileOutline numberOfSelectedRows] > 0 &&
565            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
566       
567    return YES;
568}
569
570- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
571{
572    NSRect windowRect = [window frame];
573    windowRect.size.width = [window minSize].width;   
574    return windowRect;
575}
576
577- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
578{
579    NSString * identifier = [tabViewItem identifier];
580    [self setWindowForTab: identifier animate: YES];
581    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InfoTab"];
582}
583
584- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate
585{
586    [self updateInfoStats];
587   
588    float height;
589    if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
590    {
591        height = TAB_ACTIVITY_HEIGHT;
592        [fPiecesView updateView: YES];
593    }
594    else if ([identifier isEqualToString: TAB_PEERS_IDENT])
595        height = TAB_PEERS_HEIGHT;
596    else if ([identifier isEqualToString: TAB_FILES_IDENT])
597        height = TAB_FILES_HEIGHT;
598    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
599        height = TAB_OPTIONS_HEIGHT;
600    else
601        height = TAB_INFO_HEIGHT;
602   
603    NSWindow * window = [self window];
604    NSRect frame = [window frame];
605    NSView * view = [[fTabView selectedTabViewItem] view];
606   
607    float difference = height - [view frame].size.height;
608    frame.origin.y -= difference;
609    frame.size.height += difference;
610   
611    if (animate)
612    {
613        [view setHidden: YES];
614        [window setFrame: frame display: YES animate: YES];
615        [view setHidden: NO];
616    }
617    else
618        [window setFrame: frame display: YES];
619   
620    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
621    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
622}
623
624- (void) setNextTab
625{
626    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == [fTabView numberOfTabViewItems] - 1)
627        [fTabView selectFirstTabViewItem: nil];
628    else
629        [fTabView selectNextTabViewItem: nil];
630}
631
632- (void) setPreviousTab
633{
634    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
635        [fTabView selectLastTabViewItem: nil];
636    else
637        [fTabView selectPreviousTabViewItem: nil];
638}
639
640- (int) numberOfRowsInTableView: (NSTableView *) tableView
641{
642    if (tableView == fPeerTable)
643        return fPeers ? [fPeers count] : 0;
644    return 0;
645}
646
647- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
648{
649    if (tableView == fPeerTable)
650    {
651        NSString * ident = [column identifier];
652        NSDictionary * peer = [fPeers objectAtIndex: row];
653       
654        if ([ident isEqualToString: @"Connected"])
655            return [[peer objectForKey: @"Connected"] boolValue] ? fDotGreen : fDotRed;
656        else if ([ident isEqualToString: @"Client"])
657            return [peer objectForKey: @"Client"];
658        else if  ([ident isEqualToString: @"Progress"])
659            return [peer objectForKey: @"Progress"];
660        else if ([ident isEqualToString: @"UL To"])
661            return [[peer objectForKey: @"UL To"] boolValue]
662                    ? [NSString stringForSpeedAbbrev: [[peer objectForKey: @"UL To Rate"] floatValue]] : @"";
663        else if ([ident isEqualToString: @"DL From"])
664            return [[peer objectForKey: @"DL From"] boolValue]
665                    ? [NSString stringForSpeedAbbrev: [[peer objectForKey: @"DL From Rate"] floatValue]] : @"";
666        else
667            return [peer objectForKey: @"IP"];
668    }
669    return nil;
670}
671
672- (void) tableView: (NSTableView *) tableView didClickTableColumn: (NSTableColumn *) tableColumn
673{
674    if (tableView == fPeerTable)
675    {
676        if (fPeers)
677        {
678            NSArray * oldPeers = fPeers;
679            fPeers = [[fPeers sortedArrayUsingDescriptors: [self peerSortDescriptors]] retain];
680            [oldPeers release];
681            [tableView reloadData];
682        }
683    }
684}
685
686- (BOOL) tableView: (NSTableView *) tableView shouldSelectRow:(int) row
687{
688    return tableView != fPeerTable;
689}
690
691- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
692                tableColumn: (NSTableColumn *) column row: (int) row mouseLocation: (NSPoint) mouseLocation
693{
694    if (tableView == fPeerTable)
695    {
696        NSDictionary * peerDic = [fPeers objectAtIndex: row];
697       
698        NSString * fromString;
699        int from = [[peerDic objectForKey: @"From"] intValue];
700        if (from == TR_PEER_FROM_INCOMING)
701            fromString = NSLocalizedString(@"incoming connection", "Inspector -> Peers tab -> table row tooltip");
702        else if (from == TR_PEER_FROM_CACHE)
703            fromString = NSLocalizedString(@"cache", "Inspector -> Peers tab -> table row tooltip");
704        else if (from == TR_PEER_FROM_PEX)
705            fromString = NSLocalizedString(@"peer exchange", "Inspector -> Peers tab -> table row tooltip");
706        else
707            fromString = NSLocalizedString(@"tracker", "Inspector -> Peers tab -> table row tooltip");
708       
709        return [NSString stringWithFormat: NSLocalizedString(@"Progress: %.1f%%"
710                    "\nPort: %@"
711                    "\nFrom: %@", "Inspector -> Peers tab -> table row tooltip"),
712                    [[peerDic objectForKey: @"Progress"] floatValue] * 100.0,
713                    [peerDic objectForKey: @"Port"], fromString];
714    }
715    return nil;
716}
717
718- (int) outlineView: (NSOutlineView *) outlineView numberOfChildrenOfItem: (id) item
719{
720    if (!item)
721        return fFiles ? [fFiles count] : 1;
722    return [[item objectForKey: @"IsFolder"] boolValue] ? [[item objectForKey: @"Children"] count] : 0;
723}
724
725- (BOOL) outlineView: (NSOutlineView *) outlineView isItemExpandable: (id) item
726{
727    return item && [[item objectForKey: @"IsFolder"] boolValue];
728}
729
730- (id) outlineView: (NSOutlineView *) outlineView child: (int) index ofItem: (id) item
731{
732    if (!fFiles)
733        return nil;
734   
735    return [(item ? [item objectForKey: @"Children"] : fFiles) objectAtIndex: index];
736}
737
738- (id) outlineView: (NSOutlineView *) outlineView objectValueForTableColumn: (NSTableColumn *) tableColumn
739            byItem: (id) item
740{
741    if (!item)
742        return nil;
743   
744    if ([[tableColumn identifier] isEqualToString: @"Check"])
745        return [item objectForKey: @"Check"];
746    else
747        return item;
748}
749
750- (void) outlineView: (NSOutlineView *) outlineView willDisplayCell: (id) cell
751            forTableColumn: (NSTableColumn *) tableColumn item: (id) item
752{
753    if ([[tableColumn identifier] isEqualToString: @"Name"])
754    {
755        if (!item)
756            return;
757       
758        [cell setImage: [[item objectForKey: @"IsFolder"] boolValue] ? fFolderIcon : [item objectForKey: @"Icon"]];
759    }
760    else if ([[tableColumn identifier] isEqualToString: @"Check"])
761    {
762        /*if (!item)
763        {
764            [(NSButtonCell *)cell setImagePosition: NSNoImage];
765            [cell setEnabled: NO];
766            return;
767        }
768       
769        [(NSButtonCell *)cell setImagePosition: NSImageOnly];
770        [cell setEnabled: [[item objectForKey: @"IsFolder"] boolValue] ? [[item objectForKey: @"Remaining"] intValue] > 0
771                                                                    : [[item objectForKey: @"Progress"] floatValue] < 1.0];*/
772        [(NSButtonCell *)cell setImagePosition: NSNoImage];
773    }
774    else;
775}
776
777- (void) outlineView: (NSOutlineView *) outlineView setObjectValue: (id) object
778        forTableColumn: (NSTableColumn *) tableColumn byItem: (id) item
779{
780    Torrent * torrent = [fTorrents objectAtIndex: 0];
781    int state = [object intValue] != NSOffState ? NSOnState : NSOffState;
782   
783    [torrent setFileCheckState: state forFileItem: item];
784    NSMutableDictionary * topItem = [torrent resetFileCheckStateForItemParent: item];
785   
786    [fFileOutline reloadItem: topItem reloadChildren: YES];
787}
788
789- (NSString *) outlineView: (NSOutlineView *) outlineView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
790                tableColumn: (NSTableColumn *) tableColumn item: (id) item mouseLocation: (NSPoint) mouseLocation
791{
792    if (!item)
793        return nil;
794   
795    NSString * ident = [tableColumn identifier];
796    if ([ident isEqualToString: @"Name"])
797        return [[[fTorrents objectAtIndex: 0] downloadFolder] stringByAppendingPathComponent: [item objectForKey: @"Path"]];
798    else
799        return nil;
800}
801
802- (float) outlineView: (NSOutlineView *) outlineView heightOfRowByItem: (id) item
803{
804    if ([[item objectForKey: @"IsFolder"] boolValue])
805        return FILE_ROW_SMALL_HEIGHT;
806    else
807        return [outlineView rowHeight];
808}
809
810- (BOOL) outlineView: (NSOutlineView *) outlineView shouldSelectItem: (id) item
811{
812    return item != nil;
813}
814
815- (NSArray *) peerSortDescriptors
816{
817    NSMutableArray * descriptors = [NSMutableArray array];
818   
819    NSArray * oldDescriptors = [fPeerTable sortDescriptors];
820    if ([oldDescriptors count] > 0)
821        [descriptors addObject: [oldDescriptors objectAtIndex: 0]];
822   
823    [descriptors addObject: [[fPeerTable tableColumnWithIdentifier: @"IP"] sortDescriptorPrototype]];
824   
825    return descriptors;
826}
827
828- (void) revealTorrentFile: (id) sender
829{
830    if ([fTorrents count] > 0)
831        [[fTorrents objectAtIndex: 0] revealPublicTorrent];
832}
833
834- (void) revealDataFile: (id) sender
835{
836    if ([fTorrents count] > 0)
837        [[fTorrents objectAtIndex: 0] revealData];
838}
839
840- (void) revealFile: (id) sender
841{
842    if (!fFiles)
843        return;
844   
845    NSString * folder = [[fTorrents objectAtIndex: 0] downloadFolder];
846    NSIndexSet * indexes = [fFileOutline selectedRowIndexes];
847    int i;
848    for (i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
849        [[NSWorkspace sharedWorkspace] selectFile: [folder stringByAppendingPathComponent:
850                [[fFileOutline itemAtRow: i] objectForKey: @"Path"]] inFileViewerRootedAtPath: nil];
851}
852
853- (void) setLimitSetting: (id) sender
854{
855    BOOL upload = sender == fUploadLimitPopUp;
856    int setting;
857    if ((setting = [self popUpIndexToStateSetting: [sender indexOfSelectedItem]]) == INVALID)
858        return;
859   
860    Torrent * torrent;
861    NSEnumerator * enumerator = [fTorrents objectEnumerator];
862    while ((torrent = [enumerator nextObject]))
863        upload ? [torrent setCheckUpload: setting] : [torrent setCheckDownload: setting];
864   
865    NSTextField * field = upload ? fUploadLimitField : fDownloadLimitField;
866    [field setHidden: setting != NSOnState];
867    NSTextField * label = upload ? fUploadLimitLabel : fDownloadLimitLabel;
868    [label setHidden: setting != NSOnState];
869}
870
871- (void) setSpeedLimit: (id) sender
872{
873    BOOL upload = sender == fUploadLimitField;
874   
875    Torrent * torrent;
876    NSEnumerator * enumerator = [fTorrents objectEnumerator];
877
878    int limit = [sender intValue];
879    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%i", limit]] || limit < 0)
880    {
881        NSBeep();
882       
883        torrent = [enumerator nextObject]; //use first torrent
884        limit = upload ? [torrent uploadLimit] : [torrent downloadLimit];
885        while ((torrent = [enumerator nextObject]))
886            if (limit != (upload ? [torrent uploadLimit] : [torrent downloadLimit]))
887            {
888                [sender setStringValue: @""];
889                return;
890            }
891       
892        [sender setIntValue: limit];
893    }
894    else
895    {
896        while ((torrent = [enumerator nextObject]))
897            upload ? [torrent setUploadLimit: limit] : [torrent setDownloadLimit: limit];
898    }
899}
900
901- (void) setRatioSetting: (id) sender
902{
903    int setting;
904    if ((setting = [self popUpIndexToStateSetting: [sender indexOfSelectedItem]]) == INVALID)
905        return;
906   
907    Torrent * torrent;
908    NSEnumerator * enumerator = [fTorrents objectEnumerator];
909    while ((torrent = [enumerator nextObject]))
910        [torrent setRatioSetting: setting];
911   
912    [fRatioLimitField setHidden: setting != NSOnState];
913   
914    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
915}
916
917- (void) setRatioLimit: (id) sender
918{
919    Torrent * torrent;
920    NSEnumerator * enumerator = [fTorrents objectEnumerator];
921
922    float ratioLimit = [sender floatValue];
923    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]] || ratioLimit < 0)
924    {
925        NSBeep();
926        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
927        while ((torrent = [enumerator nextObject]))
928            if (ratioLimit != [torrent ratioLimit])
929            {
930                [sender setStringValue: @""];
931                return;
932            }
933       
934        [sender setFloatValue: ratioLimit];
935    }
936    else
937    {
938        while ((torrent = [enumerator nextObject]))
939            [torrent setRatioLimit: ratioLimit];
940    }
941   
942    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
943}
944
945@end
Note: See TracBrowser for help on using the repository browser.