source: trunk/macosx/InfoWindowController.m @ 923

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

Move the pieces viewer into the Activity pane of Inspector.

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