source: trunk/macosx/InfoWindowController.m @ 1010

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

buttons in inspector to reveal data file and (public) torrent file

  • Property svn:keywords set to Date Rev Author Id
File size: 20.7 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 1010 2006-10-15 19:57: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_GLOBAL_TAG 0
29#define RATIO_NO_CHECK_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        [fRevealDataButton setHidden: YES];
154        [fRevealTorrentButton setHidden: YES];
155       
156        //don't allow empty fields to be selected
157        [fTrackerField setSelectable: NO];
158        [fAnnounceField setSelectable: NO];
159        [fHashField setSelectable: NO];
160        [fTorrentLocationField setSelectable: NO];
161        [fDataLocationField setSelectable: NO];
162       
163        [fStateField setStringValue: @""];
164        [fRatioField setStringValue: @""];
165       
166        [fSeedersField setStringValue: @""];
167        [fLeechersField setStringValue: @""];
168        [fConnectedPeersField setStringValue: @""];
169        [fDownloadingFromField setStringValue: @""];
170        [fUploadingToField setStringValue: @""];
171        [fSwarmSpeedField setStringValue: @""];
172       
173        [fPeers removeAllObjects];
174        [fPeerTable reloadData];
175       
176        [fPiecesView setTorrent: nil];
177    }
178    else
179    {   
180        Torrent * torrent = [fTorrents objectAtIndex: 0];
181       
182        [fImageView setImage: [torrent icon]];
183       
184        NSString * name = [torrent name];
185        [fNameField setStringValue: name];
186        [fNameField setToolTip: name];
187        [fSizeField setStringValue: [NSString stringForFileSize: [torrent size]]];
188       
189        NSString * tracker = [torrent tracker],
190                * announce = [torrent announce],
191                * hashString = [torrent hashString];
192        [fTrackerField setStringValue: tracker];
193        [fTrackerField setToolTip: tracker];
194        [fAnnounceField setStringValue: announce];
195        [fAnnounceField setToolTip: announce];
196        [fPieceSizeField setStringValue: [NSString stringForFileSize: [torrent pieceSize]]];
197        [fPiecesField setIntValue: [torrent pieceCount]];
198        [fHashField setStringValue: hashString];
199        [fHashField setToolTip: hashString];
200       
201        [fTorrentLocationField setStringValue: [torrent torrentLocationString]];
202        [fTorrentLocationField setToolTip: [torrent torrentLocation]];
203        [fDataLocationField setStringValue: [[torrent dataLocation] stringByAbbreviatingWithTildeInPath]];
204        [fDataLocationField setToolTip: [torrent dataLocation]];
205        [fDateStartedField setObjectValue: [torrent date]];
206       
207        [fRevealDataButton setHidden: NO];
208        [fRevealTorrentButton setHidden: ![torrent publicTorrent]];
209       
210        //allow these fields to be selected
211        [fTrackerField setSelectable: YES];
212        [fAnnounceField setSelectable: YES];
213        [fHashField setSelectable: YES];
214        [fTorrentLocationField setSelectable: YES];
215        [fDataLocationField setSelectable: YES];
216       
217        [fPiecesView setTorrent: torrent];
218    }
219   
220    //update stats and settings
221    [self updateInfoStats];
222    [self updateInfoSettings];
223
224    //set file table
225    [fFiles removeAllObjects];
226   
227    if (numberSelected > 0)
228    {
229        Torrent * torrent;
230        NSEnumerator * enumerator = [fTorrents objectEnumerator];
231        while ((torrent = [enumerator nextObject]))
232            [fFiles addObjectsFromArray: [torrent fileList]];
233   
234        [fFileTableStatusField setStringValue: [NSString stringWithFormat: @"%d file%s", [fFiles count],
235                                                [fFiles count] == 1 ? "" : "s"]];
236    }
237    else
238        [fFileTableStatusField setStringValue: @"info not available"];
239   
240    [fFileTable deselectAll: nil];
241    [fFileTable reloadData];
242}
243
244- (void) updateInfoStats
245{
246    int numberSelected = [fTorrents count];
247    if (numberSelected == 0)
248        return;
249   
250    float downloadedValid = 0;
251    uint64_t downloadedTotal = 0, uploadedTotal = 0;
252    Torrent * torrent;
253    NSEnumerator * enumerator = [fTorrents objectEnumerator];
254    while ((torrent = [enumerator nextObject]))
255    {
256        downloadedValid += [torrent downloadedValid];
257        downloadedTotal += [torrent downloadedTotal];
258        uploadedTotal += [torrent uploadedTotal];
259    }
260
261    [fDownloadedValidField setStringValue: [NSString stringForFileSize: downloadedValid]];
262    [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
263    [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
264
265    if (numberSelected == 1)
266    {
267        torrent = [fTorrents objectAtIndex: 0];
268       
269        //append percentage to amount downloaded if 1 torrent
270        [fDownloadedValidField setStringValue: [[fDownloadedValidField stringValue]
271                                        stringByAppendingFormat: @" (%.2f%%)", 100.0 * [torrent progress]]];
272       
273        [fStateField setStringValue: [torrent stateString]];
274       
275        int seeders = [torrent seeders], leechers = [torrent leechers];
276        [fSeedersField setStringValue: seeders < 0 ? @"" : [NSString stringWithInt: seeders]];
277        [fLeechersField setStringValue: leechers < 0 ? @"" : [NSString stringWithInt: leechers]];
278       
279        BOOL active = [torrent isActive];
280       
281        [fConnectedPeersField setStringValue: active ? [NSString stringWithFormat: @"%d (%d incoming)",
282                                                        [torrent totalPeers], [torrent totalPeersIncoming]]: @""];
283        [fDownloadingFromField setStringValue: active ? [NSString stringWithInt: [torrent peersUploading]] : @""];
284        [fUploadingToField setStringValue: active ? [NSString stringWithInt: [torrent peersDownloading]] : @""];
285       
286        [fRatioField setStringValue: [NSString stringForRatioWithDownload: downloadedTotal upload: uploadedTotal]];
287       
288        [fSwarmSpeedField setStringValue: [torrent isActive] ? [NSString stringForSpeed: [torrent swarmSpeed]] : @""];
289       
290        //set peers table if visible
291        if ([[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_PEERS_IDENT])
292            [self reloadPeerTable];
293       
294        //update pieces view if visible
295        if ([[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_ACTIVITY_IDENT])
296            [fPiecesView updateView: NO];
297    }
298}
299
300- (void) updateInfoSettings
301{
302    int numberSelected = [fTorrents count];
303
304    //set ratio settings
305    if (numberSelected > 0)
306    {
307        NSEnumerator * enumerator = [fTorrents objectEnumerator];
308        Torrent * torrent = [enumerator nextObject]; //first torrent
309        const int INVALID = -99;
310        int ratioSetting = [torrent stopRatioSetting];
311        float ratioLimit = [torrent ratioLimit];
312       
313        while ((ratioSetting != INVALID || ratioLimit != INVALID)
314                && (torrent = [enumerator nextObject]))
315        {
316            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
317                ratioSetting = INVALID;
318           
319            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
320                ratioLimit = INVALID;
321        }
322       
323        [fRatioMatrix setEnabled: YES];
324       
325        if (ratioSetting == RATIO_CHECK)
326        {
327            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
328            [fRatioLimitField setEnabled: YES];
329        }
330        else
331        {
332            if (ratioSetting == RATIO_NO_CHECK)
333                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
334            else if (ratioSetting == RATIO_GLOBAL)
335                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
336            else
337                [fRatioMatrix deselectAllCells];
338           
339            [fRatioLimitField setEnabled: NO];
340        }
341       
342        if (ratioLimit != INVALID)
343            [fRatioLimitField setFloatValue: ratioLimit];
344        else
345            [fRatioLimitField setStringValue: @""];
346    }
347    else
348    {
349        [fRatioMatrix deselectAllCells];
350        [fRatioMatrix setEnabled: NO];
351       
352        [fRatioLimitField setEnabled: NO];
353        [fRatioLimitField setStringValue: @""];
354    }
355   
356    [self updateInfoStats];
357}
358
359//requires a non-empty torrent array
360- (void) reloadPeerTable
361{
362    Torrent * torrent = [fTorrents objectAtIndex: 0];
363   
364    [fPeers setArray: [torrent peers]];
365    [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
366   
367    [fPeerTable reloadData];
368}
369
370- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
371{
372    SEL action = [menuItem action];
373   
374    if (action == @selector(revealFile:))
375        return [fFileTable numberOfSelectedRows] > 0 &&
376            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
377       
378    return YES;
379}
380
381- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
382{
383    NSRect windowRect = [window frame];
384    windowRect.size.width = [window minSize].width;   
385    return windowRect;
386}
387
388- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
389{
390    NSString * identifier = [tabViewItem identifier];
391    [self setWindowForTab: identifier animate: YES];
392    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InfoTab"];
393}
394
395- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate
396{
397    float height;
398    if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
399    {
400        height = TAB_ACTIVITY_HEIGHT;
401       
402        [fPiecesView updateView: YES];
403    }
404    else if ([identifier isEqualToString: TAB_PEERS_IDENT])
405    {
406        height = TAB_PEERS_HEIGHT;
407       
408        if ([fTorrents count] == 1)
409            [self reloadPeerTable]; //initial update of peer table
410    }
411    else if ([identifier isEqualToString: TAB_FILES_IDENT])
412        height = TAB_FILES_HEIGHT;
413    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
414        height = TAB_OPTIONS_HEIGHT;
415    else
416        height = TAB_INFO_HEIGHT;
417   
418    NSWindow * window = [self window];
419    NSRect frame = [window frame];
420    NSView * view = [[fTabView selectedTabViewItem] view];
421   
422    float difference = height - [view frame].size.height;
423    frame.origin.y -= difference;
424    frame.size.height += difference;
425   
426    if (animate)
427    {
428        [view setHidden: YES];
429        [window setFrame: frame display: YES animate: YES];
430        [view setHidden: NO];
431    }
432    else
433        [window setFrame: frame display: YES];
434   
435    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
436    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
437}
438
439- (void) setNextTab
440{
441    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == [fTabView numberOfTabViewItems] - 1)
442        [fTabView selectFirstTabViewItem: nil];
443    else
444        [fTabView selectNextTabViewItem: nil];
445}
446
447- (void) setPreviousTab
448{
449    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
450        [fTabView selectLastTabViewItem: nil];
451    else
452        [fTabView selectPreviousTabViewItem: nil];
453}
454
455- (int) numberOfRowsInTableView: (NSTableView *) tableView
456{
457    if (tableView == fPeerTable)
458        return [fPeers count];
459    else
460        return [fFiles count];
461}
462
463- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
464{
465    NSString * ident = [column identifier];
466    if (tableView == fPeerTable)
467    {
468        NSDictionary * peer = [fPeers objectAtIndex: row];
469       
470        if ([ident isEqualToString: @"Connected"])
471            return [[peer objectForKey: @"Connected"] boolValue] ? fDotGreen : fDotRed;
472        else if ([ident isEqualToString: @"UL To"])
473            return [[peer objectForKey: @"UL To"] boolValue] ? fCheckImage : nil;
474        else if ([ident isEqualToString: @"DL From"])
475            return [[peer objectForKey: @"DL From"] boolValue] ? fCheckImage : nil;
476        else if ([ident isEqualToString: @"Client"])
477            return [peer objectForKey: @"Client"];
478        else
479            return [peer objectForKey: @"IP"];
480    }
481    else
482    {
483        NSDictionary * file = [fFiles objectAtIndex: row];
484        if ([ident isEqualToString: @"Icon"])
485            return [[NSWorkspace sharedWorkspace] iconForFileType: [[file objectForKey: @"Name"] pathExtension]];
486        else if ([ident isEqualToString: @"Size"])
487            return [NSString stringForFileSize: [[file objectForKey: @"Size"] unsignedLongLongValue]];
488        else
489            return [[file objectForKey: @"Name"] lastPathComponent];
490    }
491}
492
493- (void) tableView: (NSTableView *) tableView didClickTableColumn: (NSTableColumn *) tableColumn
494{
495    if (tableView == fPeerTable)
496    {
497        [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
498        [tableView reloadData];
499    }
500}
501
502- (BOOL) tableView: (NSTableView *) tableView shouldSelectRow:(int) row
503{
504    return tableView != fPeerTable;
505}
506
507//only called on >= 10.4
508- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell
509        rect: (NSRectPointer) rect tableColumn: (NSTableColumn *) column
510        row: (int) row mouseLocation: (NSPoint) mouseLocation
511{
512    if (tableView == fFileTable)
513    {
514        NSDictionary * file = [fFiles objectAtIndex: row];
515        if ([[column identifier] isEqualToString: @"Size"])
516            return [[[file objectForKey: @"Size"] stringValue] stringByAppendingString: @" bytes"];
517        else
518            return [file objectForKey: @"Name"];
519    }
520    else if (tableView == fPeerTable)
521        return [NSString stringWithFormat: @"From %@ connection",
522                [[[fPeers objectAtIndex: row] objectForKey: @"Incoming"] boolValue] ? @"incoming" : @"outgoing"];
523    else
524        return nil;
525}
526
527- (NSArray *) peerSortDescriptors
528{
529    NSMutableArray * descriptors = [NSMutableArray array];
530   
531    NSArray * oldDescriptors = [fPeerTable sortDescriptors];
532    if ([oldDescriptors count] > 0)
533        [descriptors addObject: [oldDescriptors objectAtIndex: 0]];
534   
535    [descriptors addObject: [[fPeerTable tableColumnWithIdentifier: @"IP"] sortDescriptorPrototype]];
536   
537    return descriptors;
538}
539
540- (void) revealTorrentFile: (id) sender
541{
542    [[fTorrents objectAtIndex: 0] revealPublicTorrent];
543}
544
545- (void) revealDataFile: (id) sender
546{
547    [[fTorrents objectAtIndex: 0] revealData];
548}
549
550- (void) revealFile: (id) sender
551{
552    NSIndexSet * indexSet = [fFileTable selectedRowIndexes];
553    unsigned int i;
554    for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i])
555        [[NSWorkspace sharedWorkspace] selectFile: [[fFiles objectAtIndex: i] objectForKey: @"Name"]
556                                        inFileViewerRootedAtPath: nil];
557}
558
559- (void) setRatioCheck: (id) sender
560{
561    int ratioSetting, tag = [[fRatioMatrix selectedCell] tag];
562    if (tag == RATIO_CHECK_TAG)
563        ratioSetting = RATIO_CHECK;
564    else if (tag == RATIO_NO_CHECK_TAG)
565        ratioSetting = RATIO_NO_CHECK;
566    else
567        ratioSetting = RATIO_GLOBAL;
568
569    Torrent * torrent;
570    NSEnumerator * enumerator = [fTorrents objectEnumerator];
571    while ((torrent = [enumerator nextObject]))
572        [torrent setStopRatioSetting: ratioSetting];
573   
574    [self setRatioLimit: fRatioLimitField];
575    [fRatioLimitField setEnabled: tag == RATIO_CHECK_TAG];
576}
577
578- (void) setRatioLimit: (id) sender
579{
580    Torrent * torrent;
581    NSEnumerator * enumerator = [fTorrents objectEnumerator];
582
583    float ratioLimit = [sender floatValue];
584    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]] || ratioLimit < 0)
585    {
586        NSBeep();
587        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
588        while ((torrent = [enumerator nextObject]))
589            if (ratioLimit != [torrent ratioLimit])
590            {
591                [sender setStringValue: @""];
592                return;
593            }
594       
595        [sender setFloatValue: ratioLimit];
596    }
597    else
598    {
599        while ((torrent = [enumerator nextObject]))
600            [torrent setRatioLimit: ratioLimit];
601    }
602}
603
604@end
Note: See TracBrowser for help on using the repository browser.