source: trunk/macosx/InfoWindowController.m @ 987

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

Tooltips display if peer is from incoming or outgoing connection.

  • Property svn:keywords set to Date Rev Author Id
File size: 20.2 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 987 2006-10-06 03:38:31Z 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}
362
363- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
364{
365    SEL action = [menuItem action];
366   
367    if (action == @selector(revealFile:))
368        return [fFileTable numberOfSelectedRows] > 0 &&
369            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
370       
371    return YES;
372}
373
374- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
375{
376    NSRect windowRect = [window frame];
377    windowRect.size.width = [window minSize].width;   
378    return windowRect;
379}
380
381- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
382{
383    NSString * identifier = [tabViewItem identifier];
384    [self setWindowForTab: identifier animate: YES];
385    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InfoTab"];
386}
387
388- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate
389{
390    float height;
391    if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
392    {
393        height = TAB_ACTIVITY_HEIGHT;
394       
395        [fPiecesView updateView: NO];
396    }
397    else if ([identifier isEqualToString: TAB_PEERS_IDENT])
398    {
399        height = TAB_PEERS_HEIGHT;
400       
401        if ([fTorrents count] == 1)
402            [self reloadPeerTable]; //initial update of peer table
403    }
404    else if ([identifier isEqualToString: TAB_FILES_IDENT])
405        height = TAB_FILES_HEIGHT;
406    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
407        height = TAB_OPTIONS_HEIGHT;
408    else
409        height = TAB_INFO_HEIGHT;
410   
411    NSWindow * window = [self window];
412    NSRect frame = [window frame];
413    NSView * view = [[fTabView selectedTabViewItem] view];
414   
415    float difference = height - [view frame].size.height;
416    frame.origin.y -= difference;
417    frame.size.height += difference;
418   
419    if (animate)
420    {
421        [view setHidden: YES];
422        [window setFrame: frame display: YES animate: YES];
423        [view setHidden: NO];
424    }
425    else
426        [window setFrame: frame display: YES];
427   
428    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
429    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
430}
431
432- (void) setNextTab
433{
434    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == [fTabView numberOfTabViewItems] - 1)
435        [fTabView selectFirstTabViewItem: nil];
436    else
437        [fTabView selectNextTabViewItem: nil];
438}
439
440- (void) setPreviousTab
441{
442    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
443        [fTabView selectLastTabViewItem: nil];
444    else
445        [fTabView selectPreviousTabViewItem: nil];
446}
447
448- (int) numberOfRowsInTableView: (NSTableView *) tableView
449{
450    if (tableView == fPeerTable)
451        return [fPeers count];
452    else
453        return [fFiles count];
454}
455
456- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
457{
458    NSString * ident = [column identifier];
459    if (tableView == fPeerTable)
460    {
461        NSDictionary * peer = [fPeers objectAtIndex: row];
462       
463        if ([ident isEqualToString: @"Connected"])
464            return [[peer objectForKey: @"Connected"] boolValue] ? fDotGreen : fDotRed;
465        else if ([ident isEqualToString: @"UL To"])
466            return [[peer objectForKey: @"UL To"] boolValue] ? fCheckImage : nil;
467        else if ([ident isEqualToString: @"DL From"])
468            return [[peer objectForKey: @"DL From"] boolValue] ? fCheckImage : nil;
469        else if ([ident isEqualToString: @"Client"])
470            return [peer objectForKey: @"Client"];
471        else
472            return [peer objectForKey: @"IP"];
473    }
474    else
475    {
476        NSDictionary * file = [fFiles objectAtIndex: row];
477        if ([ident isEqualToString: @"Icon"])
478            return [[NSWorkspace sharedWorkspace] iconForFileType: [[file objectForKey: @"Name"] pathExtension]];
479        else if ([ident isEqualToString: @"Size"])
480            return [NSString stringForFileSize: [[file objectForKey: @"Size"] unsignedLongLongValue]];
481        else
482            return [[file objectForKey: @"Name"] lastPathComponent];
483    }
484}
485
486- (void) tableView: (NSTableView *) tableView didClickTableColumn: (NSTableColumn *) tableColumn
487{
488    if (tableView == fPeerTable)
489    {
490        [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
491        [tableView reloadData];
492    }
493}
494
495- (BOOL) tableView: (NSTableView *) tableView shouldSelectRow:(int) row
496{
497    return tableView != fPeerTable;
498}
499
500//only called on >= 10.4
501- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell
502        rect: (NSRectPointer) rect tableColumn: (NSTableColumn *) column
503        row: (int) row mouseLocation: (NSPoint) mouseLocation
504{
505    if (tableView == fFileTable)
506    {
507        NSDictionary * file = [fFiles objectAtIndex: row];
508        if ([[column identifier] isEqualToString: @"Size"])
509            return [[[file objectForKey: @"Size"] stringValue] stringByAppendingString: @" bytes"];
510        else
511            return [file objectForKey: @"Name"];
512    }
513    else if (tableView == fPeerTable)
514        return [NSString stringWithFormat: @"From %@ connection",
515                [[[fPeers objectAtIndex: row] objectForKey: @"Incoming"] boolValue] ? @"incoming" : @"outgoing"];
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.