source: trunk/macosx/InfoWindowController.m @ 1577

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

update progress bar when seeding immediately when the stop ratio setting is changed

  • Property svn:keywords set to Date Rev Author Id
File size: 31.8 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 1577 2007-03-22 19:16:08Z 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 268.0
43#define TAB_FILES_HEIGHT 268.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: @""];
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", "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    [fConnectedPeersField setStringValue: active ? [NSString stringWithFormat: NSLocalizedString(@"%d (%d incoming)",
385                                                                                "Inspector -> Peers tab -> connected"),
386                                                    [torrent totalPeers], [torrent totalPeersIncoming]]: @""];
387    [fDownloadingFromField setStringValue: active ? [NSString stringWithInt: [torrent peersUploading]] : @""];
388    [fUploadingToField setStringValue: active ? [NSString stringWithInt: [torrent peersDownloading]] : @""];
389   
390    if (fPeers)
391        [fPeers release];
392    fPeers = [[[torrent peers] sortedArrayUsingDescriptors: [self peerSortDescriptors]] retain];
393   
394    [fPeerTable reloadData];
395}
396
397- (void) updateInfoFiles
398{
399    if ([fTorrents count] != 1)
400        return;
401   
402    if ([[fTorrents objectAtIndex: 0] updateFileProgress])
403        [fFileOutline reloadData];
404}
405
406- (void) updateInfoSettings
407{
408    int numberSelected = [fTorrents count];
409
410    if (numberSelected > 0)
411    {
412        Torrent * torrent;
413       
414        //set bandwidth limits
415        NSEnumerator * enumerator = [fTorrents objectEnumerator];
416        torrent = [enumerator nextObject]; //first torrent
417       
418        int checkUpload = [torrent checkUpload],
419            checkDownload = [torrent checkDownload],
420            uploadLimit = [torrent uploadLimit],
421            downloadLimit = [torrent downloadLimit];
422       
423        while ((checkUpload != INVALID || uploadLimit != INVALID
424                || checkDownload != INVALID || downloadLimit != INVALID)
425                && (torrent = [enumerator nextObject]))
426        {
427            if (checkUpload != INVALID && checkUpload != [torrent checkUpload])
428                checkUpload = INVALID;
429           
430            if (uploadLimit != INVALID && uploadLimit != [torrent uploadLimit])
431                uploadLimit = INVALID;
432           
433            if (checkDownload != INVALID && checkDownload != [torrent checkDownload])
434                checkDownload = INVALID;
435           
436            if (downloadLimit != INVALID && downloadLimit != [torrent downloadLimit])
437                downloadLimit = INVALID;
438        }
439       
440        [fUploadLimitPopUp setEnabled: YES];
441        [fUploadLimitPopUp selectItemAtIndex: [self stateSettingToPopUpIndex: checkUpload]];
442        [fUploadLimitLabel setHidden: checkUpload != NSOnState];
443        [fUploadLimitField setHidden: checkUpload != NSOnState];
444        if (uploadLimit != INVALID)
445            [fUploadLimitField setIntValue: uploadLimit];
446        else
447            [fUploadLimitField setStringValue: @""];
448       
449        [fDownloadLimitPopUp setEnabled: YES];
450        [fDownloadLimitPopUp selectItemAtIndex: [self stateSettingToPopUpIndex: checkDownload]];
451        [fDownloadLimitLabel setHidden: checkDownload != NSOnState];
452        [fDownloadLimitField setHidden: checkDownload != NSOnState];
453        if (downloadLimit != INVALID)
454            [fDownloadLimitField setIntValue: downloadLimit];
455        else
456            [fDownloadLimitField setStringValue: @""];
457       
458        //set ratio settings
459        enumerator = [fTorrents objectEnumerator];
460        torrent = [enumerator nextObject]; //first torrent
461       
462        int checkRatio = [torrent ratioSetting];
463        float ratioLimit = [torrent ratioLimit];
464       
465        while ((checkRatio != INVALID || checkRatio != INVALID)
466                && (torrent = [enumerator nextObject]))
467        {
468            if (checkRatio != INVALID && checkRatio != [torrent ratioSetting])
469                checkRatio = INVALID;
470           
471            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
472                ratioLimit = INVALID;
473        }
474       
475        [fRatioPopUp setEnabled: YES];
476        [fRatioPopUp selectItemAtIndex: [self stateSettingToPopUpIndex: checkRatio]];
477        [fRatioLimitField setHidden: checkRatio != NSOnState];
478        if (ratioLimit != INVALID)
479            [fRatioLimitField setFloatValue: ratioLimit];
480        else
481            [fRatioLimitField setStringValue: @""];
482    }
483    else
484    {
485        [fUploadLimitPopUp setEnabled: NO];
486        [fUploadLimitPopUp selectItemAtIndex: -1];
487        [fUploadLimitField setHidden: YES];
488        [fUploadLimitLabel setHidden: YES];
489        [fUploadLimitField setStringValue: @""];
490       
491        [fDownloadLimitPopUp setEnabled: NO];
492        [fDownloadLimitPopUp selectItemAtIndex: -1];
493        [fDownloadLimitField setHidden: YES];
494        [fDownloadLimitLabel setHidden: YES];
495        [fDownloadLimitField setStringValue: @""];
496       
497        [fRatioPopUp setEnabled: NO];
498        [fRatioPopUp selectItemAtIndex: -1];
499        [fRatioLimitField setHidden: YES];
500        [fRatioLimitField setStringValue: @""];
501    }
502   
503    [self updateInfoStats];
504}
505
506- (int) stateSettingToPopUpIndex: (int) index
507{
508    if (index == NSOnState)
509        return OPTION_POPUP_LIMIT;
510    else if (index == NSOffState)
511        return OPTION_POPUP_NO_LIMIT;
512    else if (index == NSMixedState)
513        return OPTION_POPUP_GLOBAL;
514    else
515        return -1;
516}
517
518- (int) popUpIndexToStateSetting: (int) index
519{
520    if (index == OPTION_POPUP_LIMIT)
521        return NSOnState;
522    else if (index == OPTION_POPUP_NO_LIMIT)
523        return NSOffState;
524    else if (index == OPTION_POPUP_GLOBAL)
525        return NSMixedState;
526    else
527        return INVALID;
528}
529
530- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
531{
532    SEL action = [menuItem action];
533   
534    if (action == @selector(revealFile:))
535        return [fFileOutline numberOfSelectedRows] > 0 &&
536            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
537       
538    return YES;
539}
540
541- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
542{
543    NSRect windowRect = [window frame];
544    windowRect.size.width = [window minSize].width;   
545    return windowRect;
546}
547
548- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
549{
550    NSString * identifier = [tabViewItem identifier];
551    [self setWindowForTab: identifier animate: YES];
552    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InfoTab"];
553}
554
555- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate
556{
557    [self updateInfoStats];
558   
559    float height;
560    if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
561    {
562        height = TAB_ACTIVITY_HEIGHT;
563        [fPiecesView updateView: YES];
564    }
565    else if ([identifier isEqualToString: TAB_PEERS_IDENT])
566        height = TAB_PEERS_HEIGHT;
567    else if ([identifier isEqualToString: TAB_FILES_IDENT])
568        height = TAB_FILES_HEIGHT;
569    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
570        height = TAB_OPTIONS_HEIGHT;
571    else
572        height = TAB_INFO_HEIGHT;
573   
574    NSWindow * window = [self window];
575    NSRect frame = [window frame];
576    NSView * view = [[fTabView selectedTabViewItem] view];
577   
578    float difference = height - [view frame].size.height;
579    frame.origin.y -= difference;
580    frame.size.height += difference;
581   
582    if (animate)
583    {
584        [view setHidden: YES];
585        [window setFrame: frame display: YES animate: YES];
586        [view setHidden: NO];
587    }
588    else
589        [window setFrame: frame display: YES];
590   
591    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
592    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
593}
594
595- (void) setNextTab
596{
597    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == [fTabView numberOfTabViewItems] - 1)
598        [fTabView selectFirstTabViewItem: nil];
599    else
600        [fTabView selectNextTabViewItem: nil];
601}
602
603- (void) setPreviousTab
604{
605    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
606        [fTabView selectLastTabViewItem: nil];
607    else
608        [fTabView selectPreviousTabViewItem: nil];
609}
610
611- (int) numberOfRowsInTableView: (NSTableView *) tableView
612{
613    if (tableView == fPeerTable)
614        return fPeers ? [fPeers count] : 0;
615    return 0;
616}
617
618- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
619{
620    if (tableView == fPeerTable)
621    {
622        NSString * ident = [column identifier];
623        NSDictionary * peer = [fPeers objectAtIndex: row];
624       
625        if ([ident isEqualToString: @"Connected"])
626            return [[peer objectForKey: @"Connected"] boolValue] ? fDotGreen : fDotRed;
627        else if ([ident isEqualToString: @"Client"])
628            return [peer objectForKey: @"Client"];
629        else if  ([ident isEqualToString: @"Progress"])
630            return [peer objectForKey: @"Progress"];
631        else if ([ident isEqualToString: @"UL To"])
632            return [[peer objectForKey: @"UL To"] boolValue]
633                    ? [NSString stringForSpeedAbbrev: [[peer objectForKey: @"UL To Rate"] floatValue]] : @"";
634        else if ([ident isEqualToString: @"DL From"])
635            return [[peer objectForKey: @"DL From"] boolValue]
636                    ? [NSString stringForSpeedAbbrev: [[peer objectForKey: @"DL From Rate"] floatValue]] : @"";
637        else
638            return [peer objectForKey: @"IP"];
639    }
640    return nil;
641}
642
643- (void) tableView: (NSTableView *) tableView didClickTableColumn: (NSTableColumn *) tableColumn
644{
645    if (tableView == fPeerTable)
646    {
647        if (fPeers)
648        {
649            NSArray * oldPeers = fPeers;
650            fPeers = [[fPeers sortedArrayUsingDescriptors: [self peerSortDescriptors]] retain];
651            [oldPeers release];
652            [tableView reloadData];
653        }
654    }
655}
656
657- (BOOL) tableView: (NSTableView *) tableView shouldSelectRow:(int) row
658{
659    return tableView != fPeerTable;
660}
661
662- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
663                tableColumn: (NSTableColumn *) column row: (int) row mouseLocation: (NSPoint) mouseLocation
664{
665    if (tableView == fPeerTable)
666    {
667        NSDictionary * peerDic = [fPeers objectAtIndex: row];
668        return [NSString stringWithFormat: NSLocalizedString(@"Progress: %.1f%%"
669                    "\nPort: %@"
670                    "\nFrom %@ connection", "Inspector -> Peers tab -> table row tooltip"),
671                    [[peerDic objectForKey: @"Progress"] floatValue] * 100.0,
672                    [peerDic objectForKey: @"Port"],
673                    [[peerDic objectForKey: @"Incoming"] boolValue]
674                        ? NSLocalizedString(@"incoming", "Inspector -> Peers tab -> table row tooltip")
675                        : NSLocalizedString(@"outgoing", "Inspector -> Peers tab -> table row tooltip")];
676    }
677    return nil;
678}
679
680- (int) outlineView: (NSOutlineView *) outlineView numberOfChildrenOfItem: (id) item
681{
682    if (!item)
683        return fFiles ? [fFiles count] : 1;
684    return [[item objectForKey: @"IsFolder"] boolValue] ? [[item objectForKey: @"Children"] count] : 0;
685}
686
687- (BOOL) outlineView: (NSOutlineView *) outlineView isItemExpandable: (id) item
688{
689    return item && [[item objectForKey: @"IsFolder"] boolValue];
690}
691
692- (id) outlineView: (NSOutlineView *) outlineView child: (int) index ofItem: (id) item
693{
694    if (!fFiles)
695        return nil;
696   
697    return [(item ? [item objectForKey: @"Children"] : fFiles) objectAtIndex: index];
698}
699
700- (id) outlineView: (NSOutlineView *) outlineView objectValueForTableColumn: (NSTableColumn *) tableColumn
701            byItem: (id) item
702{
703    if (!item)
704        return nil;
705   
706    if ([[tableColumn identifier] isEqualToString: @"Check"])
707        return [item objectForKey: @"Check"];
708    else
709        return item;
710}
711
712- (void) outlineView: (NSOutlineView *) outlineView willDisplayCell: (id) cell
713            forTableColumn: (NSTableColumn *) tableColumn item: (id) item
714{
715    if ([[tableColumn identifier] isEqualToString: @"Name"])
716    {
717        if (!item)
718            return;
719       
720        [cell setImage: [[item objectForKey: @"IsFolder"] boolValue] ? fFolderIcon : [item objectForKey: @"Icon"]];
721    }
722    else if ([[tableColumn identifier] isEqualToString: @"Check"])
723    {
724        /*if (!item)
725        {
726            [(NSButtonCell *)cell setImagePosition: NSNoImage];
727            [cell setEnabled: NO];
728            return;
729        }
730       
731        [(NSButtonCell *)cell setImagePosition: NSImageOnly];
732        [cell setEnabled: [[item objectForKey: @"IsFolder"] boolValue] ? [[item objectForKey: @"Remaining"] intValue] > 0
733                                                                    : [[item objectForKey: @"Progress"] floatValue] < 1.0];*/
734        [(NSButtonCell *)cell setImagePosition: NSNoImage];
735    }
736    else;
737}
738
739- (void) outlineView: (NSOutlineView *) outlineView setObjectValue: (id) object
740        forTableColumn: (NSTableColumn *) tableColumn byItem: (id) item
741{
742    Torrent * torrent = [fTorrents objectAtIndex: 0];
743    int state = [object intValue] != NSOffState ? NSOnState : NSOffState;
744   
745    [torrent setFileCheckState: state forFileItem: item];
746    NSMutableDictionary * topItem = [torrent resetFileCheckStateForItemParent: item];
747   
748    [fFileOutline reloadItem: topItem reloadChildren: YES];
749}
750
751- (NSString *) outlineView: (NSOutlineView *) outlineView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
752                tableColumn: (NSTableColumn *) tableColumn item: (id) item mouseLocation: (NSPoint) mouseLocation
753{
754    if (!item)
755        return nil;
756   
757    NSString * ident = [tableColumn identifier];
758    if ([ident isEqualToString: @"Name"])
759        return [[[fTorrents objectAtIndex: 0] downloadFolder] stringByAppendingPathComponent: [item objectForKey: @"Path"]];
760    else
761        return nil;
762}
763
764- (float) outlineView: (NSOutlineView *) outlineView heightOfRowByItem: (id) item
765{
766    if ([[item objectForKey: @"IsFolder"] boolValue])
767        return FILE_ROW_SMALL_HEIGHT;
768    else
769        return [outlineView rowHeight];
770}
771
772- (BOOL) outlineView: (NSOutlineView *) outlineView shouldSelectItem: (id) item
773{
774    return item != nil;
775}
776
777- (NSArray *) peerSortDescriptors
778{
779    NSMutableArray * descriptors = [NSMutableArray array];
780   
781    NSArray * oldDescriptors = [fPeerTable sortDescriptors];
782    if ([oldDescriptors count] > 0)
783        [descriptors addObject: [oldDescriptors objectAtIndex: 0]];
784   
785    [descriptors addObject: [[fPeerTable tableColumnWithIdentifier: @"IP"] sortDescriptorPrototype]];
786   
787    return descriptors;
788}
789
790- (void) revealTorrentFile: (id) sender
791{
792    if ([fTorrents count] > 0)
793        [[fTorrents objectAtIndex: 0] revealPublicTorrent];
794}
795
796- (void) revealDataFile: (id) sender
797{
798    if ([fTorrents count] > 0)
799        [[fTorrents objectAtIndex: 0] revealData];
800}
801
802- (void) revealFile: (id) sender
803{
804    if (!fFiles)
805        return;
806   
807    NSString * folder = [[fTorrents objectAtIndex: 0] downloadFolder];
808    NSIndexSet * indexes = [fFileOutline selectedRowIndexes];
809    int i;
810    for (i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
811        [[NSWorkspace sharedWorkspace] selectFile: [folder stringByAppendingPathComponent:
812                [[fFileOutline itemAtRow: i] objectForKey: @"Path"]] inFileViewerRootedAtPath: nil];
813}
814
815- (void) setLimitSetting: (id) sender
816{
817    BOOL upload = sender == fUploadLimitPopUp;
818    int setting;
819    if ((setting = [self popUpIndexToStateSetting: [sender indexOfSelectedItem]]) == INVALID)
820        return;
821   
822    Torrent * torrent;
823    NSEnumerator * enumerator = [fTorrents objectEnumerator];
824    while ((torrent = [enumerator nextObject]))
825        upload ? [torrent setCheckUpload: setting] : [torrent setCheckDownload: setting];
826   
827    NSTextField * field = upload ? fUploadLimitField : fDownloadLimitField;
828    [field setHidden: setting != NSOnState];
829    NSTextField * label = upload ? fUploadLimitLabel : fDownloadLimitLabel;
830    [label setHidden: setting != NSOnState];
831}
832
833- (void) setSpeedLimit: (id) sender
834{
835    BOOL upload = sender == fUploadLimitField;
836   
837    Torrent * torrent;
838    NSEnumerator * enumerator = [fTorrents objectEnumerator];
839
840    int limit = [sender intValue];
841    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%i", limit]] || limit < 0)
842    {
843        NSBeep();
844       
845        torrent = [enumerator nextObject]; //use first torrent
846        limit = upload ? [torrent uploadLimit] : [torrent downloadLimit];
847        while ((torrent = [enumerator nextObject]))
848            if (limit != (upload ? [torrent uploadLimit] : [torrent downloadLimit]))
849            {
850                [sender setStringValue: @""];
851                return;
852            }
853       
854        [sender setIntValue: limit];
855    }
856    else
857    {
858        while ((torrent = [enumerator nextObject]))
859            upload ? [torrent setUploadLimit: limit] : [torrent setDownloadLimit: limit];
860    }
861}
862
863- (void) setRatioSetting: (id) sender
864{
865    int setting;
866    if ((setting = [self popUpIndexToStateSetting: [sender indexOfSelectedItem]]) == INVALID)
867        return;
868   
869    Torrent * torrent;
870    NSEnumerator * enumerator = [fTorrents objectEnumerator];
871    while ((torrent = [enumerator nextObject]))
872        [torrent setRatioSetting: setting];
873   
874    [fRatioLimitField setHidden: setting != NSOnState];
875   
876    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
877}
878
879- (void) setRatioLimit: (id) sender
880{
881    Torrent * torrent;
882    NSEnumerator * enumerator = [fTorrents objectEnumerator];
883
884    float ratioLimit = [sender floatValue];
885    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]] || ratioLimit < 0)
886    {
887        NSBeep();
888        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
889        while ((torrent = [enumerator nextObject]))
890            if (ratioLimit != [torrent ratioLimit])
891            {
892                [sender setStringValue: @""];
893                return;
894            }
895       
896        [sender setFloatValue: ratioLimit];
897    }
898    else
899    {
900        while ((torrent = [enumerator nextObject]))
901            [torrent setRatioLimit: ratioLimit];
902    }
903   
904    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
905}
906
907@end
Note: See TracBrowser for help on using the repository browser.