source: branches/nat-traversal/macosx/InfoWindowController.m @ 880

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

Simplified queue and autostart prefs

  • Property svn:keywords set to Date Rev Author Id
File size: 19.9 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 880 2006-09-22 02:08:57Z 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 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        [fPeerTableStatusField setStringValue: @"info not available"];
173    }
174    else
175    {   
176        Torrent * torrent = [fTorrents objectAtIndex: 0];
177       
178        [fImageView setImage: [torrent icon]];
179       
180        NSString * name = [torrent name];
181        [fNameField setStringValue: name];
182        [fNameField setToolTip: name];
183        [fSizeField setStringValue: [NSString stringForFileSize: [torrent size]]];
184       
185        NSString * tracker = [torrent tracker],
186                * announce = [torrent announce],
187                * hashString = [torrent hashString];
188        [fTrackerField setStringValue: tracker];
189        [fTrackerField setToolTip: tracker];
190        [fAnnounceField setStringValue: announce];
191        [fAnnounceField setToolTip: announce];
192        [fPieceSizeField setStringValue: [NSString stringForFileSize: [torrent pieceSize]]];
193        [fPiecesField setIntValue: [torrent pieceCount]];
194        [fHashField setStringValue: hashString];
195        [fHashField setToolTip: hashString];
196       
197        [fTorrentLocationField setStringValue: [torrent torrentLocationString]];
198        [fTorrentLocationField setToolTip: [torrent torrentLocation]];
199        [fDataLocationField setStringValue: [[torrent dataLocation] stringByAbbreviatingWithTildeInPath]];
200        [fDataLocationField setToolTip: [torrent dataLocation]];
201        [fDateStartedField setObjectValue: [torrent date]];
202       
203        //allow these fields to be selected
204        [fTrackerField setSelectable: YES];
205        [fAnnounceField setSelectable: YES];
206        [fHashField setSelectable: YES];
207        [fTorrentLocationField setSelectable: YES];
208        [fDataLocationField setSelectable: YES];
209    }
210   
211    //update stats and settings
212    [self updateInfoStatsAndSettings];
213
214    //set file table
215    [fFiles removeAllObjects];
216   
217    if (numberSelected > 0)
218    {
219        Torrent * torrent;
220        NSEnumerator * enumerator = [fTorrents objectEnumerator];
221        while ((torrent = [enumerator nextObject]))
222            [fFiles addObjectsFromArray: [torrent fileList]];
223   
224        [fFileTableStatusField setStringValue: [NSString stringWithFormat: @"%d file%s", [fFiles count],
225                                                [fFiles count] == 1 ? "" : "s"]];
226    }
227    else
228        [fFileTableStatusField setStringValue: @"info not available"];
229   
230    [fFileTable deselectAll: nil];
231    [fFileTable reloadData];
232}
233
234- (void) updateInfoStats
235{
236    int numberSelected = [fTorrents count];
237    if (numberSelected == 0)
238        return;
239   
240    float downloadedValid = 0;
241    uint64_t downloadedTotal = 0, uploadedTotal = 0;
242    Torrent * torrent;
243    NSEnumerator * enumerator = [fTorrents objectEnumerator];
244    while ((torrent = [enumerator nextObject]))
245    {
246        downloadedValid += [torrent downloadedValid];
247        downloadedTotal += [torrent downloadedTotal];
248        uploadedTotal += [torrent uploadedTotal];
249    }
250
251    [fDownloadedValidField setStringValue: [NSString stringForFileSize: downloadedValid]];
252    [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
253    [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
254
255    if (numberSelected == 1)
256    {
257        torrent = [fTorrents objectAtIndex: 0];
258       
259        //append percentage to amount downloaded if 1 torrent
260        [fDownloadedValidField setStringValue: [[fDownloadedValidField stringValue]
261                                        stringByAppendingFormat: @" (%.2f%%)", 100.0 * [torrent progress]]];
262       
263        [fStateField setStringValue: [torrent stateString]];
264       
265        int seeders = [torrent seeders], leechers = [torrent leechers];
266        [fSeedersField setStringValue: seeders < 0 ? @"" : [NSString stringWithInt: seeders]];
267        [fLeechersField setStringValue: leechers < 0 ? @"" : [NSString stringWithInt: leechers]];
268       
269        BOOL active = [torrent isActive];
270       
271        [fConnectedPeersField setStringValue: active ? [NSString stringWithInt: [torrent totalPeers]] : @""];
272        [fDownloadingFromField setStringValue: active ? [NSString stringWithInt: [torrent peersUploading]] : @""];
273        [fUploadingToField setStringValue: active ? [NSString stringWithInt: [torrent peersDownloading]] : @""];
274       
275        [fRatioField setStringValue: [NSString stringForRatioWithDownload: downloadedTotal upload: uploadedTotal]];
276       
277        [fSwarmSpeedField setStringValue: [torrent isActive] ? [NSString stringForSpeed: [torrent swarmSpeed]] : @""];
278       
279        //set peers table if visible
280        if ([[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_PEERS_IDENT])
281            [self reloadPeerTable];
282    }
283}
284
285- (void) updateInfoStatsAndSettings
286{
287    int numberSelected = [fTorrents count];
288
289    //set ratio settings
290    if (numberSelected > 0)
291    {
292        NSEnumerator * enumerator = [fTorrents objectEnumerator];
293        Torrent * torrent = [enumerator nextObject]; //first torrent
294        const int INVALID = -99;
295        int ratioSetting = [torrent stopRatioSetting];
296        float ratioLimit = [torrent ratioLimit];
297       
298        while ((ratioSetting != INVALID || ratioLimit != INVALID)
299                && (torrent = [enumerator nextObject]))
300        {
301            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
302                ratioSetting = INVALID;
303           
304            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
305                ratioLimit = INVALID;
306        }
307       
308        [fRatioMatrix setEnabled: YES];
309       
310        if (ratioSetting == RATIO_CHECK)
311        {
312            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
313            [fRatioLimitField setEnabled: YES];
314        }
315        else
316        {
317            if (ratioSetting == RATIO_NO_CHECK)
318                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
319            else if (ratioSetting == RATIO_GLOBAL)
320                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
321            else
322                [fRatioMatrix deselectAllCells];
323           
324            [fRatioLimitField setEnabled: NO];
325        }
326       
327        if (ratioLimit != INVALID)
328            [fRatioLimitField setFloatValue: ratioLimit];
329        else
330            [fRatioLimitField setStringValue: @""];
331    }
332    else
333    {
334        [fRatioMatrix deselectAllCells];
335        [fRatioMatrix setEnabled: NO];
336       
337        [fRatioLimitField setEnabled: NO];
338        [fRatioLimitField setStringValue: @""];
339    }
340   
341    [self updateInfoStats];
342}
343
344//requires a non-empty torrent array
345- (void) reloadPeerTable
346{
347    Torrent * torrent = [fTorrents objectAtIndex: 0];
348   
349    [fPeers setArray: [torrent peers]];
350    [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
351   
352    [fPeerTable reloadData];
353    [fPeerTableStatusField setStringValue: [NSString stringWithFormat: @"%d of %d connected",
354                                            [torrent totalPeers], [fPeers count]]];
355}
356
357- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
358{
359    SEL action = [menuItem action];
360   
361    if (action == @selector(revealFile:))
362        return [fFileTable numberOfSelectedRows] > 0 &&
363            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
364       
365    return YES;
366}
367
368- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
369{
370    NSRect windowRect = [window frame];
371    windowRect.size.width = [window minSize].width;   
372    return windowRect;
373}
374
375- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
376{
377    NSString * identifier = [tabViewItem identifier];
378    [self setWindowForTab: identifier animate: YES];
379    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InfoTab"];
380}
381
382- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate
383{
384    float height;
385    if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
386        height = TAB_ACTIVITY_HEIGHT;
387    else if ([identifier isEqualToString: TAB_PEERS_IDENT])
388    {
389        height = TAB_PEERS_HEIGHT;
390       
391        if ([fTorrents count] == 1)
392            [self reloadPeerTable]; //initial update of peer table
393    }
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
443        return [fFiles count];
444}
445
446- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
447{
448    NSString * ident = [column identifier];
449    if (tableView == fPeerTable)
450    {
451        NSDictionary * peer = [fPeers objectAtIndex: row];
452       
453        if ([ident isEqualToString: @"Connected"])
454            return [[peer objectForKey: @"Connected"] boolValue] ? fDotGreen : fDotRed;
455        else if ([ident isEqualToString: @"UL To"])
456            return [[peer objectForKey: @"UL To"] boolValue] ? fCheckImage : nil;
457        else if ([ident isEqualToString: @"DL From"])
458            return [[peer objectForKey: @"DL From"] boolValue] ? fCheckImage : nil;
459        else if ([ident isEqualToString: @"Client"])
460            return [peer objectForKey: @"Client"];
461        else
462            return [peer objectForKey: @"IP"];
463    }
464    else
465    {
466        NSDictionary * file = [fFiles objectAtIndex: row];
467        if ([ident isEqualToString: @"Icon"])
468            return [[NSWorkspace sharedWorkspace] iconForFileType: [[file objectForKey: @"Name"] pathExtension]];
469        else if ([ident isEqualToString: @"Size"])
470            return [NSString stringForFileSize: [[file objectForKey: @"Size"] unsignedLongLongValue]];
471        else
472            return [[file objectForKey: @"Name"] 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    if (tableView == fFileTable)
496    {
497        NSDictionary * file = [fFiles objectAtIndex: row];
498        if ([[column identifier] isEqualToString: @"Size"])
499            return [[[file objectForKey: @"Size"] stringValue] stringByAppendingString: @" bytes"];
500        else
501            return [file objectForKey: @"Name"];
502    }
503    else
504        return nil;
505}
506
507- (NSArray *) peerSortDescriptors
508{
509    NSMutableArray * descriptors = [NSMutableArray array];
510   
511    NSArray * oldDescriptors = [fPeerTable sortDescriptors];
512    if ([oldDescriptors count] > 0)
513        [descriptors addObject: [oldDescriptors objectAtIndex: 0]];
514   
515    [descriptors addObject: [[fPeerTable tableColumnWithIdentifier: @"IP"] sortDescriptorPrototype]];
516   
517    return descriptors;
518}
519
520- (void) revealFile: (id) sender
521{
522    NSIndexSet * indexSet = [fFileTable selectedRowIndexes];
523    unsigned int i;
524    for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i])
525        [[NSWorkspace sharedWorkspace] selectFile: [[fFiles objectAtIndex: i] objectForKey: @"Name"]
526                                        inFileViewerRootedAtPath: nil];
527}
528
529- (void) setRatioCheck: (id) sender
530{
531    int ratioSetting, tag = [[fRatioMatrix selectedCell] tag];
532    if (tag == RATIO_CHECK_TAG)
533        ratioSetting = RATIO_CHECK;
534    else if (tag == RATIO_NO_CHECK_TAG)
535        ratioSetting = RATIO_NO_CHECK;
536    else
537        ratioSetting = RATIO_GLOBAL;
538
539    Torrent * torrent;
540    NSEnumerator * enumerator = [fTorrents objectEnumerator];
541    while ((torrent = [enumerator nextObject]))
542        [torrent setStopRatioSetting: ratioSetting];
543   
544    [self setRatioLimit: fRatioLimitField];
545    [fRatioLimitField setEnabled: tag == RATIO_CHECK_TAG];
546}
547
548- (void) setRatioLimit: (id) sender
549{
550    Torrent * torrent;
551    NSEnumerator * enumerator = [fTorrents objectEnumerator];
552
553    float ratioLimit = [sender floatValue];
554    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]] || ratioLimit < 0)
555    {
556        NSBeep();
557        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
558        while ((torrent = [enumerator nextObject]))
559            if (ratioLimit != [torrent ratioLimit])
560            {
561                [sender setStringValue: @""];
562                return;
563            }
564       
565        [sender setFloatValue: ratioLimit];
566    }
567    else
568    {
569        while ((torrent = [enumerator nextObject]))
570            [torrent setRatioLimit: ratioLimit];
571    }
572}
573
574@end
Note: See TracBrowser for help on using the repository browser.