source: trunk/macosx/InfoWindowController.m @ 785

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

Show checks instead of y's for DL and UL columns.

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