source: trunk/macosx/InfoWindowController.m @ 1618

Last change on this file since 1618 was 1618, checked in by livings124, 15 years ago

Add preference option to disable PEX in Mac frontend.

  • Property svn:keywords set to Date Rev Author Id
File size: 34.6 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 1618 2007-03-31 20:08:10Z 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 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 156.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 dateAdded]];
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                //set pex check
513                enumerator = [fTorrents objectEnumerator];
514        torrent = [enumerator nextObject]; //first torrent
515               
516                BOOL pexEnabled = ![torrent privateTorrent];
517                int pexState = [torrent pex] ? NSOnState : NSOffState;
518               
519                while ((pexEnabled || pexState != NSMixedState)
520                && (torrent = [enumerator nextObject]))
521        {
522            if (pexEnabled)
523                pexEnabled = ![torrent privateTorrent];
524           
525            if (pexState != NSMixedState && pexState != ([torrent pex] ? NSOnState : NSOffState))
526                pexState = NSMixedState;
527        }
528               
529                [fPexCheck setEnabled: pexEnabled];
530                [fPexCheck setState: pexState];
531    }
532    else
533    {
534        [fUploadLimitPopUp setEnabled: NO];
535        [fUploadLimitPopUp selectItemAtIndex: -1];
536        [fUploadLimitField setHidden: YES];
537        [fUploadLimitLabel setHidden: YES];
538        [fUploadLimitField setStringValue: @""];
539       
540        [fDownloadLimitPopUp setEnabled: NO];
541        [fDownloadLimitPopUp selectItemAtIndex: -1];
542        [fDownloadLimitField setHidden: YES];
543        [fDownloadLimitLabel setHidden: YES];
544        [fDownloadLimitField setStringValue: @""];
545       
546        [fRatioPopUp setEnabled: NO];
547        [fRatioPopUp selectItemAtIndex: -1];
548        [fRatioLimitField setHidden: YES];
549        [fRatioLimitField setStringValue: @""];
550               
551                [fPexCheck setEnabled: NO];
552    }
553   
554    [self updateInfoStats];
555}
556
557- (int) stateSettingToPopUpIndex: (int) index
558{
559    if (index == NSOnState)
560        return OPTION_POPUP_LIMIT;
561    else if (index == NSOffState)
562        return OPTION_POPUP_NO_LIMIT;
563    else if (index == NSMixedState)
564        return OPTION_POPUP_GLOBAL;
565    else
566        return -1;
567}
568
569- (int) popUpIndexToStateSetting: (int) index
570{
571    if (index == OPTION_POPUP_LIMIT)
572        return NSOnState;
573    else if (index == OPTION_POPUP_NO_LIMIT)
574        return NSOffState;
575    else if (index == OPTION_POPUP_GLOBAL)
576        return NSMixedState;
577    else
578        return INVALID;
579}
580
581- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
582{
583    SEL action = [menuItem action];
584   
585    if (action == @selector(revealFile:))
586        return [fFileOutline numberOfSelectedRows] > 0 &&
587            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
588       
589    return YES;
590}
591
592- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
593{
594    NSRect windowRect = [window frame];
595    windowRect.size.width = [window minSize].width;   
596    return windowRect;
597}
598
599- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
600{
601    NSString * identifier = [tabViewItem identifier];
602    [self setWindowForTab: identifier animate: YES];
603    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InfoTab"];
604}
605
606- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate
607{
608    [self updateInfoStats];
609   
610    float height;
611    if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
612    {
613        height = TAB_ACTIVITY_HEIGHT;
614        [fPiecesView updateView: YES];
615    }
616    else if ([identifier isEqualToString: TAB_PEERS_IDENT])
617        height = TAB_PEERS_HEIGHT;
618    else if ([identifier isEqualToString: TAB_FILES_IDENT])
619        height = TAB_FILES_HEIGHT;
620    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
621        height = TAB_OPTIONS_HEIGHT;
622    else
623        height = TAB_INFO_HEIGHT;
624   
625    NSWindow * window = [self window];
626    NSRect frame = [window frame];
627    NSView * view = [[fTabView selectedTabViewItem] view];
628   
629    float difference = height - [view frame].size.height;
630    frame.origin.y -= difference;
631    frame.size.height += difference;
632   
633    if (animate)
634    {
635        [view setHidden: YES];
636        [window setFrame: frame display: YES animate: YES];
637        [view setHidden: NO];
638    }
639    else
640        [window setFrame: frame display: YES];
641   
642    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
643    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
644}
645
646- (void) setNextTab
647{
648    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == [fTabView numberOfTabViewItems] - 1)
649        [fTabView selectFirstTabViewItem: nil];
650    else
651        [fTabView selectNextTabViewItem: nil];
652}
653
654- (void) setPreviousTab
655{
656    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
657        [fTabView selectLastTabViewItem: nil];
658    else
659        [fTabView selectPreviousTabViewItem: nil];
660}
661
662- (int) numberOfRowsInTableView: (NSTableView *) tableView
663{
664    if (tableView == fPeerTable)
665        return fPeers ? [fPeers count] : 0;
666    return 0;
667}
668
669- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
670{
671    if (tableView == fPeerTable)
672    {
673        NSString * ident = [column identifier];
674        NSDictionary * peer = [fPeers objectAtIndex: row];
675       
676        if ([ident isEqualToString: @"Connected"])
677            return [[peer objectForKey: @"Connected"] boolValue] ? fDotGreen : fDotRed;
678        else if ([ident isEqualToString: @"Client"])
679            return [peer objectForKey: @"Client"];
680        else if  ([ident isEqualToString: @"Progress"])
681            return [peer objectForKey: @"Progress"];
682        else if ([ident isEqualToString: @"UL To"])
683            return [[peer objectForKey: @"UL To"] boolValue]
684                    ? [NSString stringForSpeedAbbrev: [[peer objectForKey: @"UL To Rate"] floatValue]] : @"";
685        else if ([ident isEqualToString: @"DL From"])
686            return [[peer objectForKey: @"DL From"] boolValue]
687                    ? [NSString stringForSpeedAbbrev: [[peer objectForKey: @"DL From Rate"] floatValue]] : @"";
688        else
689            return [peer objectForKey: @"IP"];
690    }
691    return nil;
692}
693
694- (void) tableView: (NSTableView *) tableView didClickTableColumn: (NSTableColumn *) tableColumn
695{
696    if (tableView == fPeerTable)
697    {
698        if (fPeers)
699        {
700            NSArray * oldPeers = fPeers;
701            fPeers = [[fPeers sortedArrayUsingDescriptors: [self peerSortDescriptors]] retain];
702            [oldPeers release];
703            [tableView reloadData];
704        }
705    }
706}
707
708- (BOOL) tableView: (NSTableView *) tableView shouldSelectRow:(int) row
709{
710    return tableView != fPeerTable;
711}
712
713- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
714                tableColumn: (NSTableColumn *) column row: (int) row mouseLocation: (NSPoint) mouseLocation
715{
716    if (tableView == fPeerTable)
717    {
718        NSDictionary * peerDic = [fPeers objectAtIndex: row];
719       
720        NSString * fromString;
721        int from = [[peerDic objectForKey: @"From"] intValue];
722        if (from == TR_PEER_FROM_INCOMING)
723            fromString = NSLocalizedString(@"incoming connection", "Inspector -> Peers tab -> table row tooltip");
724        else if (from == TR_PEER_FROM_CACHE)
725            fromString = NSLocalizedString(@"cache", "Inspector -> Peers tab -> table row tooltip");
726        else if (from == TR_PEER_FROM_PEX)
727            fromString = NSLocalizedString(@"peer exchange", "Inspector -> Peers tab -> table row tooltip");
728        else
729            fromString = NSLocalizedString(@"tracker", "Inspector -> Peers tab -> table row tooltip");
730       
731        return [NSString stringWithFormat: NSLocalizedString(@"Progress: %.1f%%"
732                    "\nPort: %@"
733                    "\nFrom: %@", "Inspector -> Peers tab -> table row tooltip"),
734                    [[peerDic objectForKey: @"Progress"] floatValue] * 100.0,
735                    [peerDic objectForKey: @"Port"], fromString];
736    }
737    return nil;
738}
739
740- (int) outlineView: (NSOutlineView *) outlineView numberOfChildrenOfItem: (id) item
741{
742    if (!item)
743        return fFiles ? [fFiles count] : 1;
744    return [[item objectForKey: @"IsFolder"] boolValue] ? [[item objectForKey: @"Children"] count] : 0;
745}
746
747- (BOOL) outlineView: (NSOutlineView *) outlineView isItemExpandable: (id) item
748{
749    return item && [[item objectForKey: @"IsFolder"] boolValue];
750}
751
752- (id) outlineView: (NSOutlineView *) outlineView child: (int) index ofItem: (id) item
753{
754    if (!fFiles)
755        return nil;
756   
757    return [(item ? [item objectForKey: @"Children"] : fFiles) objectAtIndex: index];
758}
759
760- (id) outlineView: (NSOutlineView *) outlineView objectValueForTableColumn: (NSTableColumn *) tableColumn
761            byItem: (id) item
762{
763    if (!item)
764        return nil;
765   
766    if ([[tableColumn identifier] isEqualToString: @"Check"])
767        return [item objectForKey: @"Check"];
768    else
769        return item;
770}
771
772- (void) outlineView: (NSOutlineView *) outlineView willDisplayCell: (id) cell
773            forTableColumn: (NSTableColumn *) tableColumn item: (id) item
774{
775    if ([[tableColumn identifier] isEqualToString: @"Name"])
776    {
777        if (!item)
778            return;
779       
780        [cell setImage: [[item objectForKey: @"IsFolder"] boolValue] ? fFolderIcon : [item objectForKey: @"Icon"]];
781    }
782    else if ([[tableColumn identifier] isEqualToString: @"Check"])
783    {
784        /*if (!item)
785        {
786            [(NSButtonCell *)cell setImagePosition: NSNoImage];
787            [cell setEnabled: NO];
788            return;
789        }
790       
791        [(NSButtonCell *)cell setImagePosition: NSImageOnly];
792        [cell setEnabled: [[item objectForKey: @"IsFolder"] boolValue] ? [[item objectForKey: @"Remaining"] intValue] > 0
793                                                                    : [[item objectForKey: @"Progress"] floatValue] < 1.0];*/
794        [(NSButtonCell *)cell setImagePosition: NSNoImage];
795    }
796    else;
797}
798
799- (void) outlineView: (NSOutlineView *) outlineView setObjectValue: (id) object
800        forTableColumn: (NSTableColumn *) tableColumn byItem: (id) item
801{
802    Torrent * torrent = [fTorrents objectAtIndex: 0];
803    int state = [object intValue] != NSOffState ? NSOnState : NSOffState;
804   
805    [torrent setFileCheckState: state forFileItem: item];
806    NSMutableDictionary * topItem = [torrent resetFileCheckStateForItemParent: item];
807   
808    [fFileOutline reloadItem: topItem reloadChildren: YES];
809}
810
811- (NSString *) outlineView: (NSOutlineView *) outlineView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
812                tableColumn: (NSTableColumn *) tableColumn item: (id) item mouseLocation: (NSPoint) mouseLocation
813{
814    if (!item)
815        return nil;
816   
817    NSString * ident = [tableColumn identifier];
818    if ([ident isEqualToString: @"Name"])
819        return [[[fTorrents objectAtIndex: 0] downloadFolder] stringByAppendingPathComponent: [item objectForKey: @"Path"]];
820    else
821        return nil;
822}
823
824- (float) outlineView: (NSOutlineView *) outlineView heightOfRowByItem: (id) item
825{
826    if ([[item objectForKey: @"IsFolder"] boolValue])
827        return FILE_ROW_SMALL_HEIGHT;
828    else
829        return [outlineView rowHeight];
830}
831
832- (BOOL) outlineView: (NSOutlineView *) outlineView shouldSelectItem: (id) item
833{
834    return item != nil;
835}
836
837- (NSArray *) peerSortDescriptors
838{
839    NSMutableArray * descriptors = [NSMutableArray array];
840   
841    NSArray * oldDescriptors = [fPeerTable sortDescriptors];
842    if ([oldDescriptors count] > 0)
843        [descriptors addObject: [oldDescriptors objectAtIndex: 0]];
844   
845    [descriptors addObject: [[fPeerTable tableColumnWithIdentifier: @"IP"] sortDescriptorPrototype]];
846   
847    return descriptors;
848}
849
850- (void) revealTorrentFile: (id) sender
851{
852    if ([fTorrents count] > 0)
853        [[fTorrents objectAtIndex: 0] revealPublicTorrent];
854}
855
856- (void) revealDataFile: (id) sender
857{
858    if ([fTorrents count] > 0)
859        [[fTorrents objectAtIndex: 0] revealData];
860}
861
862- (void) revealFile: (id) sender
863{
864    if (!fFiles)
865        return;
866   
867    NSString * folder = [[fTorrents objectAtIndex: 0] downloadFolder];
868    NSIndexSet * indexes = [fFileOutline selectedRowIndexes];
869    int i;
870    for (i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
871        [[NSWorkspace sharedWorkspace] selectFile: [folder stringByAppendingPathComponent:
872                [[fFileOutline itemAtRow: i] objectForKey: @"Path"]] inFileViewerRootedAtPath: nil];
873}
874
875- (void) setLimitSetting: (id) sender
876{
877    BOOL upload = sender == fUploadLimitPopUp;
878    int setting;
879    if ((setting = [self popUpIndexToStateSetting: [sender indexOfSelectedItem]]) == INVALID)
880        return;
881   
882    Torrent * torrent;
883    NSEnumerator * enumerator = [fTorrents objectEnumerator];
884    while ((torrent = [enumerator nextObject]))
885        upload ? [torrent setCheckUpload: setting] : [torrent setCheckDownload: setting];
886   
887    NSTextField * field = upload ? fUploadLimitField : fDownloadLimitField;
888    [field setHidden: setting != NSOnState];
889    NSTextField * label = upload ? fUploadLimitLabel : fDownloadLimitLabel;
890    [label setHidden: setting != NSOnState];
891}
892
893- (void) setSpeedLimit: (id) sender
894{
895    BOOL upload = sender == fUploadLimitField;
896   
897    Torrent * torrent;
898    NSEnumerator * enumerator = [fTorrents objectEnumerator];
899
900    int limit = [sender intValue];
901    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%i", limit]] || limit < 0)
902    {
903        NSBeep();
904       
905        torrent = [enumerator nextObject]; //use first torrent
906        limit = upload ? [torrent uploadLimit] : [torrent downloadLimit];
907        while ((torrent = [enumerator nextObject]))
908            if (limit != (upload ? [torrent uploadLimit] : [torrent downloadLimit]))
909            {
910                [sender setStringValue: @""];
911                return;
912            }
913       
914        [sender setIntValue: limit];
915    }
916    else
917    {
918        while ((torrent = [enumerator nextObject]))
919            upload ? [torrent setUploadLimit: limit] : [torrent setDownloadLimit: limit];
920    }
921}
922
923- (void) setRatioSetting: (id) sender
924{
925    int setting;
926    if ((setting = [self popUpIndexToStateSetting: [sender indexOfSelectedItem]]) == INVALID)
927        return;
928   
929    Torrent * torrent;
930    NSEnumerator * enumerator = [fTorrents objectEnumerator];
931    while ((torrent = [enumerator nextObject]))
932        [torrent setRatioSetting: setting];
933   
934    [fRatioLimitField setHidden: setting != NSOnState];
935   
936    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
937}
938
939- (void) setRatioLimit: (id) sender
940{
941    Torrent * torrent;
942    NSEnumerator * enumerator = [fTorrents objectEnumerator];
943
944    float ratioLimit = [sender floatValue];
945    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]] || ratioLimit < 0)
946    {
947        NSBeep();
948        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
949        while ((torrent = [enumerator nextObject]))
950            if (ratioLimit != [torrent ratioLimit])
951            {
952                [sender setStringValue: @""];
953                return;
954            }
955       
956        [sender setFloatValue: ratioLimit];
957    }
958    else
959    {
960        while ((torrent = [enumerator nextObject]))
961            [torrent setRatioLimit: ratioLimit];
962    }
963   
964    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
965}
966
967- (void) setPex: (id) sender
968{
969        int state = [sender state];
970        if (state == NSMixedState)
971        {
972                state = NSOnState;
973                [sender setState: state];
974        }
975       
976        Torrent * torrent;
977    NSEnumerator * enumerator = [fTorrents objectEnumerator];
978       
979        while ((torrent = [enumerator nextObject]))
980                [torrent setPex: state == NSOnState];
981}
982
983@end
Note: See TracBrowser for help on using the repository browser.