source: trunk/macosx/InfoWindowController.m @ 569

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

I can't imagine putting the upload/download rates back into the inspector.

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