source: trunk/macosx/InfoWindowController.m @ 790

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

percentage next to valid downloaded in inspector

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