source: trunk/macosx/InfoWindowController.m @ 780

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

Check for connected peers, as well as better peer table resizing behavior.

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