source: trunk/macosx/InfoWindowController.m @ 781

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

Initially select IP column for sorting.

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