source: trunk/macosx/InfoWindowController.m @ 1008

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

rearrange and rename individual seeding settings

  • Property svn:keywords set to Date Rev Author Id
File size: 20.3 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 1008 2006-10-15 19:08:05Z 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        //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 stringWithFormat: @"%d (%d incoming)",
276                                                        [torrent totalPeers], [torrent totalPeersIncoming]]: @""];
277        [fDownloadingFromField setStringValue: active ? [NSString stringWithInt: [torrent peersUploading]] : @""];
278        [fUploadingToField setStringValue: active ? [NSString stringWithInt: [torrent peersDownloading]] : @""];
279       
280        [fRatioField setStringValue: [NSString stringForRatioWithDownload: downloadedTotal upload: uploadedTotal]];
281       
282        [fSwarmSpeedField setStringValue: [torrent isActive] ? [NSString stringForSpeed: [torrent swarmSpeed]] : @""];
283       
284        //set peers table if visible
285        if ([[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_PEERS_IDENT])
286            [self reloadPeerTable];
287       
288        //update pieces view if visible
289        if ([[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_ACTIVITY_IDENT])
290            [fPiecesView updateView: NO];
291    }
292}
293
294- (void) updateInfoSettings
295{
296    int numberSelected = [fTorrents count];
297
298    //set ratio settings
299    if (numberSelected > 0)
300    {
301        NSEnumerator * enumerator = [fTorrents objectEnumerator];
302        Torrent * torrent = [enumerator nextObject]; //first torrent
303        const int INVALID = -99;
304        int ratioSetting = [torrent stopRatioSetting];
305        float ratioLimit = [torrent ratioLimit];
306       
307        while ((ratioSetting != INVALID || ratioLimit != INVALID)
308                && (torrent = [enumerator nextObject]))
309        {
310            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
311                ratioSetting = INVALID;
312           
313            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
314                ratioLimit = INVALID;
315        }
316       
317        [fRatioMatrix setEnabled: YES];
318       
319        if (ratioSetting == RATIO_CHECK)
320        {
321            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
322            [fRatioLimitField setEnabled: YES];
323        }
324        else
325        {
326            if (ratioSetting == RATIO_NO_CHECK)
327                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
328            else if (ratioSetting == RATIO_GLOBAL)
329                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
330            else
331                [fRatioMatrix deselectAllCells];
332           
333            [fRatioLimitField setEnabled: NO];
334        }
335       
336        if (ratioLimit != INVALID)
337            [fRatioLimitField setFloatValue: ratioLimit];
338        else
339            [fRatioLimitField setStringValue: @""];
340    }
341    else
342    {
343        [fRatioMatrix deselectAllCells];
344        [fRatioMatrix setEnabled: NO];
345       
346        [fRatioLimitField setEnabled: NO];
347        [fRatioLimitField setStringValue: @""];
348    }
349   
350    [self updateInfoStats];
351}
352
353//requires a non-empty torrent array
354- (void) reloadPeerTable
355{
356    Torrent * torrent = [fTorrents objectAtIndex: 0];
357   
358    [fPeers setArray: [torrent peers]];
359    [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
360   
361    [fPeerTable reloadData];
362}
363
364- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
365{
366    SEL action = [menuItem action];
367   
368    if (action == @selector(revealFile:))
369        return [fFileTable numberOfSelectedRows] > 0 &&
370            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
371       
372    return YES;
373}
374
375- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
376{
377    NSRect windowRect = [window frame];
378    windowRect.size.width = [window minSize].width;   
379    return windowRect;
380}
381
382- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
383{
384    NSString * identifier = [tabViewItem identifier];
385    [self setWindowForTab: identifier animate: YES];
386    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InfoTab"];
387}
388
389- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate
390{
391    float height;
392    if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
393    {
394        height = TAB_ACTIVITY_HEIGHT;
395       
396        [fPiecesView updateView: YES];
397    }
398    else if ([identifier isEqualToString: TAB_PEERS_IDENT])
399    {
400        height = TAB_PEERS_HEIGHT;
401       
402        if ([fTorrents count] == 1)
403            [self reloadPeerTable]; //initial update of peer table
404    }
405    else if ([identifier isEqualToString: TAB_FILES_IDENT])
406        height = TAB_FILES_HEIGHT;
407    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
408        height = TAB_OPTIONS_HEIGHT;
409    else
410        height = TAB_INFO_HEIGHT;
411   
412    NSWindow * window = [self window];
413    NSRect frame = [window frame];
414    NSView * view = [[fTabView selectedTabViewItem] view];
415   
416    float difference = height - [view frame].size.height;
417    frame.origin.y -= difference;
418    frame.size.height += difference;
419   
420    if (animate)
421    {
422        [view setHidden: YES];
423        [window setFrame: frame display: YES animate: YES];
424        [view setHidden: NO];
425    }
426    else
427        [window setFrame: frame display: YES];
428   
429    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
430    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
431}
432
433- (void) setNextTab
434{
435    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == [fTabView numberOfTabViewItems] - 1)
436        [fTabView selectFirstTabViewItem: nil];
437    else
438        [fTabView selectNextTabViewItem: nil];
439}
440
441- (void) setPreviousTab
442{
443    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
444        [fTabView selectLastTabViewItem: nil];
445    else
446        [fTabView selectPreviousTabViewItem: nil];
447}
448
449- (int) numberOfRowsInTableView: (NSTableView *) tableView
450{
451    if (tableView == fPeerTable)
452        return [fPeers count];
453    else
454        return [fFiles count];
455}
456
457- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
458{
459    NSString * ident = [column identifier];
460    if (tableView == fPeerTable)
461    {
462        NSDictionary * peer = [fPeers objectAtIndex: row];
463       
464        if ([ident isEqualToString: @"Connected"])
465            return [[peer objectForKey: @"Connected"] boolValue] ? fDotGreen : fDotRed;
466        else if ([ident isEqualToString: @"UL To"])
467            return [[peer objectForKey: @"UL To"] boolValue] ? fCheckImage : nil;
468        else if ([ident isEqualToString: @"DL From"])
469            return [[peer objectForKey: @"DL From"] boolValue] ? fCheckImage : nil;
470        else if ([ident isEqualToString: @"Client"])
471            return [peer objectForKey: @"Client"];
472        else
473            return [peer objectForKey: @"IP"];
474    }
475    else
476    {
477        NSDictionary * file = [fFiles objectAtIndex: row];
478        if ([ident isEqualToString: @"Icon"])
479            return [[NSWorkspace sharedWorkspace] iconForFileType: [[file objectForKey: @"Name"] pathExtension]];
480        else if ([ident isEqualToString: @"Size"])
481            return [NSString stringForFileSize: [[file objectForKey: @"Size"] unsignedLongLongValue]];
482        else
483            return [[file objectForKey: @"Name"] lastPathComponent];
484    }
485}
486
487- (void) tableView: (NSTableView *) tableView didClickTableColumn: (NSTableColumn *) tableColumn
488{
489    if (tableView == fPeerTable)
490    {
491        [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
492        [tableView reloadData];
493    }
494}
495
496- (BOOL) tableView: (NSTableView *) tableView shouldSelectRow:(int) row
497{
498    return tableView != fPeerTable;
499}
500
501//only called on >= 10.4
502- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell
503        rect: (NSRectPointer) rect tableColumn: (NSTableColumn *) column
504        row: (int) row mouseLocation: (NSPoint) mouseLocation
505{
506    if (tableView == fFileTable)
507    {
508        NSDictionary * file = [fFiles objectAtIndex: row];
509        if ([[column identifier] isEqualToString: @"Size"])
510            return [[[file objectForKey: @"Size"] stringValue] stringByAppendingString: @" bytes"];
511        else
512            return [file objectForKey: @"Name"];
513    }
514    else if (tableView == fPeerTable)
515        return [NSString stringWithFormat: @"From %@ connection",
516                [[[fPeers objectAtIndex: row] objectForKey: @"Incoming"] boolValue] ? @"incoming" : @"outgoing"];
517    else
518        return nil;
519}
520
521- (NSArray *) peerSortDescriptors
522{
523    NSMutableArray * descriptors = [NSMutableArray array];
524   
525    NSArray * oldDescriptors = [fPeerTable sortDescriptors];
526    if ([oldDescriptors count] > 0)
527        [descriptors addObject: [oldDescriptors objectAtIndex: 0]];
528   
529    [descriptors addObject: [[fPeerTable tableColumnWithIdentifier: @"IP"] sortDescriptorPrototype]];
530   
531    return descriptors;
532}
533
534- (void) revealFile: (id) sender
535{
536    NSIndexSet * indexSet = [fFileTable selectedRowIndexes];
537    unsigned int i;
538    for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i])
539        [[NSWorkspace sharedWorkspace] selectFile: [[fFiles objectAtIndex: i] objectForKey: @"Name"]
540                                        inFileViewerRootedAtPath: nil];
541}
542
543- (void) setRatioCheck: (id) sender
544{
545    int ratioSetting, tag = [[fRatioMatrix selectedCell] tag];
546    if (tag == RATIO_CHECK_TAG)
547        ratioSetting = RATIO_CHECK;
548    else if (tag == RATIO_NO_CHECK_TAG)
549        ratioSetting = RATIO_NO_CHECK;
550    else
551        ratioSetting = RATIO_GLOBAL;
552
553    Torrent * torrent;
554    NSEnumerator * enumerator = [fTorrents objectEnumerator];
555    while ((torrent = [enumerator nextObject]))
556        [torrent setStopRatioSetting: ratioSetting];
557   
558    [self setRatioLimit: fRatioLimitField];
559    [fRatioLimitField setEnabled: tag == RATIO_CHECK_TAG];
560}
561
562- (void) setRatioLimit: (id) sender
563{
564    Torrent * torrent;
565    NSEnumerator * enumerator = [fTorrents objectEnumerator];
566
567    float ratioLimit = [sender floatValue];
568    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]] || ratioLimit < 0)
569    {
570        NSBeep();
571        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
572        while ((torrent = [enumerator nextObject]))
573            if (ratioLimit != [torrent ratioLimit])
574            {
575                [sender setStringValue: @""];
576                return;
577            }
578       
579        [sender setFloatValue: ratioLimit];
580    }
581    else
582    {
583        while ((torrent = [enumerator nextObject]))
584            [torrent setRatioLimit: ratioLimit];
585    }
586}
587
588@end
Note: See TracBrowser for help on using the repository browser.