source: trunk/macosx/InfoWindowController.m @ 922

Last change on this file since 922 was 922, checked in by livings124, 15 years ago

Better sizing of table in inspector, and slightly larger min width.

  • Property svn:keywords set to Date Rev Author Id
File size: 19.9 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 922 2006-09-25 20:21:19Z 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 300
33#define MAX_WINDOW_WIDTH 5000
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 109.0
44#define TAB_PEERS_HEIGHT 260.0
45#define TAB_FILES_HEIGHT 260.0
46#define TAB_OPTIONS_HEIGHT 83.0
47
48@interface InfoWindowController (Private)
49
50- (void) reloadPeerTable;
51- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate;
52- (NSArray *) peerSortDescriptors;
53
54@end
55
56@implementation InfoWindowController
57
58- (void) awakeFromNib
59{
60    fAppIcon = [[NSApp applicationIconImage] copy];
61    fDotGreen = [NSImage imageNamed: @"GreenDot.tiff"];
62    fDotRed = [NSImage imageNamed: @"RedDot.tiff"];
63    fCheckImage = [NSImage imageNamed: @"NSMenuCheckmark"];
64   
65    fTorrents = [[NSArray alloc] init];
66    fPeers = [[NSMutableArray alloc] initWithCapacity: 30];
67    fFiles = [[NSMutableArray alloc] initWithCapacity: 6];
68    [fFileTable setDoubleAction: @selector(revealFile:)];
69   
70    //window location and size
71    NSPanel * window = (NSPanel *)[self window];
72   
73    [window setBecomesKeyOnlyIfNeeded: YES];
74   
75    [window setFrameAutosaveName: @"InspectorWindowFrame"];
76    [window setFrameUsingName: @"InspectorWindowFrame"];
77   
78    //select tab
79    NSString * identifier = [[NSUserDefaults standardUserDefaults] stringForKey: @"InfoTab"];
80    if ([fTabView indexOfTabViewItemWithIdentifier: identifier] == NSNotFound)
81        identifier = TAB_INFO_IDENT;
82   
83    [fTabView selectTabViewItemWithIdentifier: identifier];
84    [self setWindowForTab: identifier animate: NO];
85   
86    //initially sort peer table by IP
87    if ([[fPeerTable sortDescriptors] count] == 0)
88        [fPeerTable setSortDescriptors: [NSArray arrayWithObject: [[fPeerTable tableColumnWithIdentifier: @"IP"]
89                                            sortDescriptorPrototype]]];
90   
91    [self updateInfoForTorrents: [NSArray array]];
92}
93
94- (void) dealloc
95{
96    [fTorrents release];
97    [fPeers release];
98    [fFiles release];
99
100    [fAppIcon release];
101    [super dealloc];
102}
103
104- (void) updateInfoForTorrents: (NSArray *) torrents
105{
106    [fTorrents release];
107    fTorrents = [torrents retain];
108
109    int numberSelected = [fTorrents count];
110    if (numberSelected != 1)
111    {
112        if (numberSelected > 0)
113        {
114            [fNameField setStringValue: [NSString stringWithFormat: @"%d Torrents Selected", numberSelected]];
115       
116            uint64_t size = 0;
117            NSEnumerator * enumerator = [torrents objectEnumerator];
118            Torrent * torrent;
119            while ((torrent = [enumerator nextObject]))
120                size += [torrent size];
121           
122            [fSizeField setStringValue: [[NSString stringForFileSize: size] stringByAppendingString: @" Total"]];
123        }
124        else
125        {
126            [fNameField setStringValue: @"No Torrents Selected"];
127            [fSizeField setStringValue: @""];
128
129            [fDownloadedValidField setStringValue: @""];
130            [fDownloadedTotalField setStringValue: @""];
131            [fUploadedTotalField setStringValue: @""];
132        }
133       
134        [fImageView setImage: fAppIcon];
135       
136        [fNameField setToolTip: nil];
137
138        [fTrackerField setStringValue: @""];
139        [fTrackerField setToolTip: nil];
140        [fAnnounceField setStringValue: @""];
141        [fAnnounceField setToolTip: nil];
142        [fPieceSizeField setStringValue: @""];
143        [fPiecesField setStringValue: @""];
144        [fHashField setStringValue: @""];
145        [fHashField setToolTip: nil];
146       
147        [fTorrentLocationField setStringValue: @""];
148        [fTorrentLocationField setToolTip: nil];
149        [fDataLocationField setStringValue: @""];
150        [fDataLocationField setToolTip: nil];
151        [fDateStartedField setStringValue: @""];
152       
153        //don't allow empty fields to be selected
154        [fTrackerField setSelectable: NO];
155        [fAnnounceField setSelectable: NO];
156        [fHashField setSelectable: NO];
157        [fTorrentLocationField setSelectable: NO];
158        [fDataLocationField setSelectable: NO];
159       
160        [fStateField setStringValue: @""];
161        [fRatioField setStringValue: @""];
162       
163        [fSeedersField setStringValue: @""];
164        [fLeechersField setStringValue: @""];
165        [fConnectedPeersField setStringValue: @""];
166        [fDownloadingFromField setStringValue: @""];
167        [fUploadingToField setStringValue: @""];
168        [fSwarmSpeedField setStringValue: @""];
169       
170        [fPeers removeAllObjects];
171        [fPeerTable reloadData];
172    }
173    else
174    {   
175        Torrent * torrent = [fTorrents objectAtIndex: 0];
176       
177        [fImageView setImage: [torrent icon]];
178       
179        NSString * name = [torrent name];
180        [fNameField setStringValue: name];
181        [fNameField setToolTip: name];
182        [fSizeField setStringValue: [NSString stringForFileSize: [torrent size]]];
183       
184        NSString * tracker = [torrent tracker],
185                * announce = [torrent announce],
186                * hashString = [torrent hashString];
187        [fTrackerField setStringValue: tracker];
188        [fTrackerField setToolTip: tracker];
189        [fAnnounceField setStringValue: announce];
190        [fAnnounceField setToolTip: announce];
191        [fPieceSizeField setStringValue: [NSString stringForFileSize: [torrent pieceSize]]];
192        [fPiecesField setIntValue: [torrent pieceCount]];
193        [fHashField setStringValue: hashString];
194        [fHashField setToolTip: hashString];
195       
196        [fTorrentLocationField setStringValue: [torrent torrentLocationString]];
197        [fTorrentLocationField setToolTip: [torrent torrentLocation]];
198        [fDataLocationField setStringValue: [[torrent dataLocation] stringByAbbreviatingWithTildeInPath]];
199        [fDataLocationField setToolTip: [torrent dataLocation]];
200        [fDateStartedField setObjectValue: [torrent date]];
201       
202        //allow these fields to be selected
203        [fTrackerField setSelectable: YES];
204        [fAnnounceField setSelectable: YES];
205        [fHashField setSelectable: YES];
206        [fTorrentLocationField setSelectable: YES];
207        [fDataLocationField setSelectable: YES];
208    }
209   
210    //update stats and settings
211    [self updateInfoStats];
212    [self updateInfoSettings];
213
214    //set file table
215    [fFiles removeAllObjects];
216   
217    if (numberSelected > 0)
218    {
219        Torrent * torrent;
220        NSEnumerator * enumerator = [fTorrents objectEnumerator];
221        while ((torrent = [enumerator nextObject]))
222            [fFiles addObjectsFromArray: [torrent fileList]];
223   
224        [fFileTableStatusField setStringValue: [NSString stringWithFormat: @"%d file%s", [fFiles count],
225                                                [fFiles count] == 1 ? "" : "s"]];
226    }
227    else
228        [fFileTableStatusField setStringValue: @"info not available"];
229   
230    [fFileTable deselectAll: nil];
231    [fFileTable reloadData];
232}
233
234- (void) updateInfoStats
235{
236    int numberSelected = [fTorrents count];
237    if (numberSelected == 0)
238        return;
239   
240    float downloadedValid = 0;
241    uint64_t downloadedTotal = 0, uploadedTotal = 0;
242    Torrent * torrent;
243    NSEnumerator * enumerator = [fTorrents objectEnumerator];
244    while ((torrent = [enumerator nextObject]))
245    {
246        downloadedValid += [torrent downloadedValid];
247        downloadedTotal += [torrent downloadedTotal];
248        uploadedTotal += [torrent uploadedTotal];
249    }
250
251    [fDownloadedValidField setStringValue: [NSString stringForFileSize: downloadedValid]];
252    [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
253    [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
254
255    if (numberSelected == 1)
256    {
257        torrent = [fTorrents objectAtIndex: 0];
258       
259        //append percentage to amount downloaded if 1 torrent
260        [fDownloadedValidField setStringValue: [[fDownloadedValidField stringValue]
261                                        stringByAppendingFormat: @" (%.2f%%)", 100.0 * [torrent progress]]];
262       
263        [fStateField setStringValue: [torrent stateString]];
264       
265        int seeders = [torrent seeders], leechers = [torrent leechers];
266        [fSeedersField setStringValue: seeders < 0 ? @"" : [NSString stringWithInt: seeders]];
267        [fLeechersField setStringValue: leechers < 0 ? @"" : [NSString stringWithInt: leechers]];
268       
269        BOOL active = [torrent isActive];
270       
271        [fConnectedPeersField setStringValue: active ? [NSString stringWithInt: [torrent totalPeers]] : @""];
272        [fDownloadingFromField setStringValue: active ? [NSString stringWithInt: [torrent peersUploading]] : @""];
273        [fUploadingToField setStringValue: active ? [NSString stringWithInt: [torrent peersDownloading]] : @""];
274       
275        [fRatioField setStringValue: [NSString stringForRatioWithDownload: downloadedTotal upload: uploadedTotal]];
276       
277        [fSwarmSpeedField setStringValue: [torrent isActive] ? [NSString stringForSpeed: [torrent swarmSpeed]] : @""];
278       
279        //set peers table if visible
280        if ([[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_PEERS_IDENT])
281            [self reloadPeerTable];
282    }
283}
284
285- (void) updateInfoSettings
286{
287    int numberSelected = [fTorrents count];
288
289    //set ratio settings
290    if (numberSelected > 0)
291    {
292        NSEnumerator * enumerator = [fTorrents objectEnumerator];
293        Torrent * torrent = [enumerator nextObject]; //first torrent
294        const int INVALID = -99;
295        int ratioSetting = [torrent stopRatioSetting];
296        float ratioLimit = [torrent ratioLimit];
297       
298        while ((ratioSetting != INVALID || ratioLimit != INVALID)
299                && (torrent = [enumerator nextObject]))
300        {
301            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
302                ratioSetting = INVALID;
303           
304            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
305                ratioLimit = INVALID;
306        }
307       
308        [fRatioMatrix setEnabled: YES];
309       
310        if (ratioSetting == RATIO_CHECK)
311        {
312            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
313            [fRatioLimitField setEnabled: YES];
314        }
315        else
316        {
317            if (ratioSetting == RATIO_NO_CHECK)
318                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
319            else if (ratioSetting == RATIO_GLOBAL)
320                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
321            else
322                [fRatioMatrix deselectAllCells];
323           
324            [fRatioLimitField setEnabled: NO];
325        }
326       
327        if (ratioLimit != INVALID)
328            [fRatioLimitField setFloatValue: ratioLimit];
329        else
330            [fRatioLimitField setStringValue: @""];
331    }
332    else
333    {
334        [fRatioMatrix deselectAllCells];
335        [fRatioMatrix setEnabled: NO];
336       
337        [fRatioLimitField setEnabled: NO];
338        [fRatioLimitField setStringValue: @""];
339    }
340   
341    [self updateInfoStats];
342}
343
344//requires a non-empty torrent array
345- (void) reloadPeerTable
346{
347    Torrent * torrent = [fTorrents objectAtIndex: 0];
348   
349    [fPeers setArray: [torrent peers]];
350    [fPeers sortUsingDescriptors: [self peerSortDescriptors]];
351   
352    [fPeerTable reloadData];
353    #warning use [fpeers count]
354    //[fPeerTableStatusField setStringValue: [NSString stringWithFormat: @"%d of %d connected",
355    //                                        [torrent totalPeers], [fPeers count]]];
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    {
390        height = TAB_PEERS_HEIGHT;
391       
392        if ([fTorrents count] == 1)
393            [self reloadPeerTable]; //initial update of peer table
394    }
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
444        return [fFiles count];
445}
446
447- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
448{
449    NSString * ident = [column identifier];
450    if (tableView == fPeerTable)
451    {
452        NSDictionary * peer = [fPeers objectAtIndex: row];
453       
454        if ([ident isEqualToString: @"Connected"])
455            return [[peer objectForKey: @"Connected"] boolValue] ? fDotGreen : fDotRed;
456        else if ([ident isEqualToString: @"UL To"])
457            return [[peer objectForKey: @"UL To"] boolValue] ? fCheckImage : nil;
458        else if ([ident isEqualToString: @"DL From"])
459            return [[peer objectForKey: @"DL From"] boolValue] ? fCheckImage : nil;
460        else if ([ident isEqualToString: @"Client"])
461            return [peer objectForKey: @"Client"];
462        else
463            return [peer objectForKey: @"IP"];
464    }
465    else
466    {
467        NSDictionary * file = [fFiles objectAtIndex: row];
468        if ([ident isEqualToString: @"Icon"])
469            return [[NSWorkspace sharedWorkspace] iconForFileType: [[file objectForKey: @"Name"] pathExtension]];
470        else if ([ident isEqualToString: @"Size"])
471            return [NSString stringForFileSize: [[file objectForKey: @"Size"] unsignedLongLongValue]];
472        else
473            return [[file objectForKey: @"Name"] 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    if (tableView == fFileTable)
497    {
498        NSDictionary * file = [fFiles objectAtIndex: row];
499        if ([[column identifier] isEqualToString: @"Size"])
500            return [[[file objectForKey: @"Size"] stringValue] stringByAppendingString: @" bytes"];
501        else
502            return [file objectForKey: @"Name"];
503    }
504    else
505        return nil;
506}
507
508- (NSArray *) peerSortDescriptors
509{
510    NSMutableArray * descriptors = [NSMutableArray array];
511   
512    NSArray * oldDescriptors = [fPeerTable sortDescriptors];
513    if ([oldDescriptors count] > 0)
514        [descriptors addObject: [oldDescriptors objectAtIndex: 0]];
515   
516    [descriptors addObject: [[fPeerTable tableColumnWithIdentifier: @"IP"] sortDescriptorPrototype]];
517   
518    return descriptors;
519}
520
521- (void) revealFile: (id) sender
522{
523    NSIndexSet * indexSet = [fFileTable selectedRowIndexes];
524    unsigned int i;
525    for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i])
526        [[NSWorkspace sharedWorkspace] selectFile: [[fFiles objectAtIndex: i] objectForKey: @"Name"]
527                                        inFileViewerRootedAtPath: nil];
528}
529
530- (void) setRatioCheck: (id) sender
531{
532    int ratioSetting, tag = [[fRatioMatrix selectedCell] tag];
533    if (tag == RATIO_CHECK_TAG)
534        ratioSetting = RATIO_CHECK;
535    else if (tag == RATIO_NO_CHECK_TAG)
536        ratioSetting = RATIO_NO_CHECK;
537    else
538        ratioSetting = RATIO_GLOBAL;
539
540    Torrent * torrent;
541    NSEnumerator * enumerator = [fTorrents objectEnumerator];
542    while ((torrent = [enumerator nextObject]))
543        [torrent setStopRatioSetting: ratioSetting];
544   
545    [self setRatioLimit: fRatioLimitField];
546    [fRatioLimitField setEnabled: tag == RATIO_CHECK_TAG];
547}
548
549- (void) setRatioLimit: (id) sender
550{
551    Torrent * torrent;
552    NSEnumerator * enumerator = [fTorrents objectEnumerator];
553
554    float ratioLimit = [sender floatValue];
555    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]] || ratioLimit < 0)
556    {
557        NSBeep();
558        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
559        while ((torrent = [enumerator nextObject]))
560            if (ratioLimit != [torrent ratioLimit])
561            {
562                [sender setStringValue: @""];
563                return;
564            }
565       
566        [sender setFloatValue: ratioLimit];
567    }
568    else
569    {
570        while ((torrent = [enumerator nextObject]))
571            [torrent setRatioLimit: ratioLimit];
572    }
573}
574
575@end
Note: See TracBrowser for help on using the repository browser.