source: trunk/macosx/InfoWindowController.m @ 761

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

Basic peer info in info window...more to come.

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