source: trunk/macosx/InfoWindowController.m @ 782

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

Colored dots for connected/not connected

  • Property svn:keywords set to Date Rev Author Id
File size: 19.5 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 782 2006-08-15 01:51:45Z 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 250.0
45#define TAB_FILES_HEIGHT 250.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   
62    fTorrents = [[NSArray alloc] init];
63    fPeers = [[NSMutableArray alloc] initWithCapacity: 6];
64    fFiles = [[NSMutableArray alloc] initWithCapacity: 6];
65    [fFileTable setDoubleAction: @selector(revealFile:)];
66   
67    //window location and size
68    NSPanel * window = (NSPanel *)[self window];
69   
70    [window setBecomesKeyOnlyIfNeeded: YES];
71   
72    [window setFrameAutosaveName: @"InspectorWindowFrame"];
73    [window setFrameUsingName: @"InspectorWindowFrame"];
74   
75    //select tab
76    NSString * identifier = [[NSUserDefaults standardUserDefaults] stringForKey: @"InfoTab"];
77    if ([fTabView indexOfTabViewItemWithIdentifier: identifier] == NSNotFound)
78        identifier = TAB_INFO_IDENT;
79   
80    [fTabView selectTabViewItemWithIdentifier: identifier];
81    [self setWindowForTab: identifier animate: NO];
82   
83    if ([[fPeerTable sortDescriptors] count] == 0)
84        [fPeerTable setSortDescriptors: [NSArray arrayWithObject: [[fPeerTable tableColumnWithIdentifier: @"IP"]
85                                            sortDescriptorPrototype]]];
86   
87    [self updateInfoForTorrents: [NSArray array]];
88}
89
90- (void) dealloc
91{
92    [fTorrents release];
93    [fPeers release];
94    [fFiles release];
95
96    [fAppIcon release];
97    [super dealloc];
98}
99
100- (void) updateInfoForTorrents: (NSArray *) torrents
101{
102    [fTorrents release];
103    fTorrents = [torrents retain];
104
105    int numberSelected = [fTorrents count];
106    if (numberSelected != 1)
107    {
108        if (numberSelected > 0)
109        {
110            [fNameField setStringValue: [NSString stringWithFormat:
111                            @"%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    else
171    {   
172        Torrent * torrent = [fTorrents objectAtIndex: 0];
173       
174        [fImageView setImage: [torrent icon]];
175       
176        NSString * name = [torrent name];
177        [fNameField setStringValue: name];
178        [fNameField setToolTip: name];
179        [fSizeField setStringValue: [NSString stringForFileSize: [torrent size]]];
180       
181        NSString * tracker = [torrent tracker],
182                * announce = [torrent announce],
183                * hashString = [torrent hashString];
184        [fTrackerField setStringValue: tracker];
185        [fTrackerField setToolTip: tracker];
186        [fAnnounceField setStringValue: announce];
187        [fAnnounceField setToolTip: announce];
188        [fPieceSizeField setStringValue: [NSString stringForFileSize: [torrent pieceSize]]];
189        [fPiecesField setIntValue: [torrent pieceCount]];
190        [fHashField setStringValue: hashString];
191        [fHashField setToolTip: hashString];
192       
193        [fTorrentLocationField setStringValue: [torrent torrentLocationString]];
194        [fTorrentLocationField setToolTip: [torrent torrentLocation]];
195        [fDataLocationField setStringValue: [[torrent dataLocation] stringByAbbreviatingWithTildeInPath]];
196        [fDataLocationField setToolTip: [torrent dataLocation]];
197        [fDateStartedField setObjectValue: [torrent date]];
198       
199        //allow these fields to be selected
200        [fTrackerField setSelectable: YES];
201        [fAnnounceField setSelectable: YES];
202        [fHashField setSelectable: YES];
203        [fTorrentLocationField setSelectable: YES];
204        [fDataLocationField setSelectable: YES];
205    }
206   
207    //update stats and settings
208    [self updateInfoStatsAndSettings];
209
210    //set file table
211    [fFiles removeAllObjects];
212   
213    Torrent * torrent;
214    NSEnumerator * enumerator = [fTorrents objectEnumerator];
215    while ((torrent = [enumerator nextObject]))
216        [fFiles addObjectsFromArray: [torrent fileList]];
217   
218    [fFileTable deselectAll: nil];
219    [fFileTable reloadData];
220}
221
222- (void) updateInfoStats
223{
224    int numberSelected = [fTorrents count];
225    if (numberSelected > 0)
226    {
227        float downloadedValid = 0;
228        uint64_t downloadedTotal = 0, uploadedTotal = 0;
229        Torrent * torrent;
230        NSEnumerator * enumerator = [fTorrents objectEnumerator];
231        while ((torrent = [enumerator nextObject]))
232        {
233            downloadedValid += [torrent downloadedValid];
234            downloadedTotal += [torrent downloadedTotal];
235            uploadedTotal += [torrent uploadedTotal];
236        }
237
238        [fDownloadedValidField setStringValue: [NSString stringForFileSize: downloadedValid]];
239        [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
240        [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
241   
242        if (numberSelected == 1)
243        {
244            torrent = [fTorrents objectAtIndex: 0];
245
246            [fStateField setStringValue: [torrent stateString]];
247/*
248            [fPercentField setStringValue: [NSString stringWithFormat: @"%.2f%%", 100.0 * [torrent progress]]];
249*/
250            int seeders = [torrent seeders], leechers = [torrent leechers];
251            [fSeedersField setStringValue: seeders < 0 ?
252                @"" : [NSString stringWithInt: seeders]];
253            [fLeechersField setStringValue: leechers < 0 ?
254                @"" : [NSString stringWithInt: leechers]];
255           
256            BOOL active = [torrent isActive];
257           
258            [fConnectedPeersField setStringValue: active ? [NSString
259                    stringWithInt: [torrent totalPeers]] : @""];
260            [fDownloadingFromField setStringValue: active ? [NSString
261                    stringWithInt: [torrent peersUploading]] : @""];
262            [fUploadingToField setStringValue: active ? [NSString
263                    stringWithInt: [torrent peersDownloading]] : @""];
264           
265            [fRatioField setStringValue: [NSString stringForRatioWithDownload:
266                                        downloadedTotal upload: uploadedTotal]];
267        }
268    }
269   
270    //set peers table
271    if (numberSelected == 1)
272    {
273        [fPeers setArray: [[fTorrents objectAtIndex: 0] peers]];
274        [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
275    }
276    else
277        [fPeers removeAllObjects];
278   
279    [fPeerTable deselectAll: nil];
280    [fPeerTable reloadData];
281}
282
283- (void) updateInfoStatsAndSettings
284{
285    int numberSelected = [fTorrents count];
286
287    //set wait to start
288    BOOL waiting = NO, notWaiting = NO, canEnableWaiting = numberSelected > 0,
289        waitingSettingOn = [[[NSUserDefaults standardUserDefaults] stringForKey: @"StartSetting"]
290                                isEqualToString: @"Wait"];
291   
292    NSEnumerator * enumerator = [fTorrents objectEnumerator];
293    Torrent * torrent;
294    while ((torrent = [enumerator nextObject]))
295    {
296        if ([torrent waitingToStart])
297            waiting = YES;
298        else
299            notWaiting = YES;
300       
301        if (canEnableWaiting && !(![torrent isActive] && [torrent progress] < 1.0 && waitingSettingOn))
302            canEnableWaiting = NO;
303    }
304   
305    [fWaitToStartButton setState: waiting && notWaiting ? NSMixedState : (waiting ? NSOnState : NSOffState)];
306    [fWaitToStartButton setEnabled: canEnableWaiting];
307
308    //set ratio settings
309    if (numberSelected > 0)
310    {
311        enumerator = [fTorrents objectEnumerator];
312        torrent = [enumerator nextObject]; //first torrent
313        const int INVALID = -99;
314        int ratioSetting = [torrent stopRatioSetting];
315        float ratioLimit = [torrent ratioLimit];
316       
317        while ((ratioSetting != INVALID || ratioLimit != INVALID)
318                && (torrent = [enumerator nextObject]))
319        {
320            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
321                ratioSetting = INVALID;
322           
323            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
324                ratioLimit = INVALID;
325        }
326       
327        [fRatioMatrix setEnabled: YES];
328       
329        if (ratioSetting == RATIO_CHECK)
330        {
331            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
332            [fRatioLimitField setEnabled: YES];
333        }
334        else
335        {
336            if (ratioSetting == RATIO_NO_CHECK)
337                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
338            else if (ratioSetting == RATIO_GLOBAL)
339                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
340            else
341                [fRatioMatrix deselectAllCells];
342           
343            [fRatioLimitField setEnabled: NO];
344        }
345       
346        if (ratioLimit != INVALID)
347            [fRatioLimitField setFloatValue: ratioLimit];
348        else
349            [fRatioLimitField setStringValue: @""];
350    }
351    else
352    {
353        [fRatioMatrix deselectAllCells];
354        [fRatioMatrix setEnabled: NO];
355       
356        [fRatioLimitField setEnabled: NO];
357        [fRatioLimitField setStringValue: @""];
358    }
359   
360    [self updateInfoStats];
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        height = TAB_ACTIVITY_HEIGHT;
393    else if ([identifier isEqualToString: TAB_PEERS_IDENT])
394        height = TAB_PEERS_HEIGHT;
395    else if ([identifier isEqualToString: TAB_FILES_IDENT])
396        height = TAB_FILES_HEIGHT;
397    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
398        height = TAB_OPTIONS_HEIGHT;
399    else
400        height = TAB_INFO_HEIGHT;
401   
402    NSWindow * window = [self window];
403    NSRect frame = [window frame];
404    NSView * view = [[fTabView selectedTabViewItem] view];
405   
406    float difference = height - [view frame].size.height;
407    frame.origin.y -= difference;
408    frame.size.height += difference;
409   
410    if (animate)
411    {
412        [view setHidden: YES];
413        [window setFrame: frame display: YES animate: YES];
414        [view setHidden: NO];
415    }
416    else
417        [window setFrame: frame display: YES];
418   
419    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
420    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
421}
422
423- (void) setNextTab
424{
425    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == [fTabView numberOfTabViewItems] - 1)
426        [fTabView selectFirstTabViewItem: nil];
427    else
428        [fTabView selectNextTabViewItem: nil];
429}
430
431- (void) setPreviousTab
432{
433    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
434        [fTabView selectLastTabViewItem: nil];
435    else
436        [fTabView selectPreviousTabViewItem: nil];
437}
438
439- (int) numberOfRowsInTableView: (NSTableView *) tableView
440{
441    if (tableView == fPeerTable)
442        return [fPeers count];
443    else if (tableView == fFileTable)
444        return [fFiles count];
445    else
446        return 0;
447}
448
449- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
450{
451    NSString * ident = [column identifier];
452    if (tableView == fPeerTable)
453    {
454        NSDictionary * peer = [fPeers objectAtIndex: row];
455       
456        if ([ident isEqualToString: @"Connected"])
457            return [[peer objectForKey: @"Connected"] boolValue] ? fDotGreen : fDotRed;
458        else if ([ident isEqualToString: @"UL To"])
459            return [[peer objectForKey: @"UL To"] boolValue] ? @"Y" : @"";
460        else if ([ident isEqualToString: @"DL From"])
461            return [[peer objectForKey: @"DL From"] boolValue] ? @"Y" : @"";
462        else if ([ident isEqualToString: @"Client"])
463            return [peer objectForKey: @"Client"];
464        else
465            return [peer objectForKey: @"IP"];
466    }
467    else
468    {
469        NSString * file = [fFiles objectAtIndex: row];
470        if ([ident isEqualToString: @"Icon"])
471            return [[NSWorkspace sharedWorkspace] iconForFileType: [file pathExtension]];
472        else
473            return [file lastPathComponent];
474    }
475}
476
477- (void) tableView: (NSTableView *) tableView didClickTableColumn: (NSTableColumn *) tableColumn
478{
479    if (tableView == fPeerTable)
480    {
481        [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
482        [tableView reloadData];
483    }
484}
485
486- (BOOL) tableView: (NSTableView *) tableView shouldSelectRow:(int) row
487{
488    return tableView != fPeerTable;
489}
490
491//only called on >= 10.4
492- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell
493        rect: (NSRectPointer) rect tableColumn: (NSTableColumn *) column
494        row: (int) row mouseLocation: (NSPoint) mouseLocation
495{
496    return tableView == fFileTable ? [fFiles objectAtIndex: row] : nil;
497}
498
499- (NSArray *) peerSortDescriptors
500{
501    NSMutableArray * descriptors = [NSMutableArray array];
502   
503    NSArray * oldDescriptors = [fPeerTable sortDescriptors];
504    if ([oldDescriptors count] > 0)
505        [descriptors addObject: [oldDescriptors objectAtIndex: 0]];
506   
507    [descriptors addObject: [[fPeerTable tableColumnWithIdentifier: @"IP"] sortDescriptorPrototype]];
508   
509    return descriptors;
510}
511
512- (void) revealFile: (id) sender
513{
514    NSIndexSet * indexSet = [fFileTable selectedRowIndexes];
515    unsigned int i;
516    for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i])
517        [[NSWorkspace sharedWorkspace] selectFile: [fFiles objectAtIndex: i] inFileViewerRootedAtPath: nil];
518}
519
520- (void) setRatioCheck: (id) sender
521{
522    int ratioSetting, tag = [[fRatioMatrix selectedCell] tag];
523    if (tag == RATIO_CHECK_TAG)
524        ratioSetting = RATIO_CHECK;
525    else if (tag == RATIO_NO_CHECK_TAG)
526        ratioSetting = RATIO_NO_CHECK;
527    else
528        ratioSetting = RATIO_GLOBAL;
529
530    Torrent * torrent;
531    NSEnumerator * enumerator = [fTorrents objectEnumerator];
532    while ((torrent = [enumerator nextObject]))
533        [torrent setStopRatioSetting: ratioSetting];
534   
535    [self setRatioLimit: fRatioLimitField];
536    [fRatioLimitField setEnabled: tag == RATIO_CHECK_TAG];
537}
538
539- (void) setRatioLimit: (id) sender
540{
541    Torrent * torrent;
542    NSEnumerator * enumerator = [fTorrents objectEnumerator];
543
544    float ratioLimit = [sender floatValue];
545    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
546            || ratioLimit < 0)
547    {
548        NSBeep();
549        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
550        while ((torrent = [enumerator nextObject]))
551            if (ratioLimit != [torrent ratioLimit])
552            {
553                [sender setStringValue: @""];
554                return;
555            }
556       
557        [sender setFloatValue: ratioLimit];
558    }
559    else
560    {
561        while ((torrent = [enumerator nextObject]))
562            [torrent setRatioLimit: ratioLimit];
563    }
564}
565
566- (void) setWaitToStart: (id) sender
567{
568    int state = [sender state];
569
570    NSEnumerator * enumerator = [fTorrents objectEnumerator];
571    Torrent * torrent;
572    while ((torrent = [enumerator nextObject]))
573        [torrent setWaitToStart: state];
574   
575    [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentStartSettingChange" object: fTorrents];
576}
577
578@end
Note: See TracBrowser for help on using the repository browser.