source: trunk/macosx/InfoWindowController.m @ 802

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

Make sure the scheduler 24-hour format is double digits.

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