source: trunk/macosx/InfoWindowController.m @ 741

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

placeholder of N/A in info window

  • Property svn:keywords set to Date Rev Author Id
File size: 16.3 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 741 2006-08-08 02:36: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_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    [self updateInfoForTorrents: [NSArray array]];
75}
76
77- (void) dealloc
78{
79    [fTorrents release];
80    [fFiles release];
81
82    [fAppIcon release];
83    [super dealloc];
84}
85
86- (void) updateInfoForTorrents: (NSArray *) torrents
87{
88    [fTorrents release];
89    fTorrents = [torrents retain];
90
91    int numberSelected = [fTorrents count];
92    if (numberSelected != 1)
93    {
94        if (numberSelected > 0)
95        {
96            [fNameField setStringValue: [NSString stringWithFormat:
97                            @"%d Torrents Selected", numberSelected]];
98       
99            uint64_t size = 0;
100            NSEnumerator * enumerator = [torrents objectEnumerator];
101            Torrent * torrent;
102            while ((torrent = [enumerator nextObject]))
103                size += [torrent size];
104           
105            [fSizeField setStringValue: [[NSString stringForFileSize: size]
106                                stringByAppendingString: @" Total"]];
107        }
108        else
109        {
110            [fNameField setStringValue: @"No Torrents Selected"];
111            [fSizeField setStringValue: @""];
112
113            [fDownloadedValidField setStringValue: @""];
114            [fDownloadedTotalField setStringValue: @""];
115            [fUploadedTotalField setStringValue: @""];
116        }
117       
118        [fImageView setImage: fAppIcon];
119       
120        [fNameField setToolTip: nil];
121
122        [fTrackerField setStringValue: @""];
123        [fTrackerField setToolTip: nil];
124        [fAnnounceField setStringValue: @""];
125        [fAnnounceField setToolTip: nil];
126        [fPieceSizeField setStringValue: @""];
127        [fPiecesField setStringValue: @""];
128        [fHashField setStringValue: @""];
129        [fHashField setToolTip: nil];
130       
131        [fTorrentLocationField setStringValue: @""];
132        [fTorrentLocationField setToolTip: nil];
133        [fDataLocationField setStringValue: @""];
134        [fDataLocationField setToolTip: nil];
135        [fDateStartedField setStringValue: @""];
136       
137        [fStateField setStringValue: @""];
138/*
139        [fPercentField setStringValue: @""];
140*/
141        [fRatioField setStringValue: @""];
142       
143        [fSeedersField setStringValue: @""];
144        [fLeechersField setStringValue: @""];
145        [fConnectedPeersField setStringValue: @""];
146        [fDownloadingFromField setStringValue: @""];
147        [fUploadingToField setStringValue: @""];
148    }
149    else
150    {   
151        Torrent * torrent = [fTorrents objectAtIndex: 0];
152       
153        [fImageView setImage: [torrent icon]];
154       
155        NSString * name = [torrent name];
156        [fNameField setStringValue: name];
157        [fNameField setToolTip: name];
158        [fSizeField setStringValue: [NSString stringForFileSize: [torrent size]]];
159       
160        NSString * tracker = [torrent tracker],
161                * announce = [torrent announce],
162                * hashString = [torrent hashString];
163        [fTrackerField setStringValue: tracker];
164        [fTrackerField setToolTip: tracker];
165        [fAnnounceField setStringValue: announce];
166        [fAnnounceField setToolTip: announce];
167        [fPieceSizeField setStringValue: [NSString stringForFileSize: [torrent pieceSize]]];
168        [fPiecesField setIntValue: [torrent pieceCount]];
169        [fHashField setStringValue: hashString];
170        [fHashField setToolTip: hashString];
171       
172        [fTorrentLocationField setStringValue: [torrent torrentLocationString]];
173        [fTorrentLocationField setToolTip: [torrent torrentLocation]];
174        [fDataLocationField setStringValue: [[torrent dataLocation] stringByAbbreviatingWithTildeInPath]];
175        [fDataLocationField setToolTip: [torrent dataLocation]];
176        [fDateStartedField setObjectValue: [torrent date]];
177    }
178   
179    //update stats and settings
180    [self updateInfoStatsAndSettings];
181
182    //set file table
183    [fFiles removeAllObjects];
184   
185    Torrent * torrent;
186    NSEnumerator * enumerator = [fTorrents objectEnumerator];
187    while ((torrent = [enumerator nextObject]))
188        [fFiles addObjectsFromArray: [torrent fileList]];
189   
190    [fFileTable deselectAll: nil];
191    [fFileTable reloadData];
192}
193
194- (void) updateInfoStats
195{
196    int numberSelected = [fTorrents count];
197    if (numberSelected > 0)
198    {
199        float downloadedValid = 0;
200        uint64_t downloadedTotal = 0, uploadedTotal = 0;
201        Torrent * torrent;
202        NSEnumerator * enumerator = [fTorrents objectEnumerator];
203        while ((torrent = [enumerator nextObject]))
204        {
205            downloadedValid += [torrent downloadedValid];
206            downloadedTotal += [torrent downloadedTotal];
207            uploadedTotal += [torrent uploadedTotal];
208        }
209
210        [fDownloadedValidField setStringValue: [NSString stringForFileSize: downloadedValid]];
211        [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
212        [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
213   
214        if (numberSelected == 1)
215        {
216            torrent = [fTorrents objectAtIndex: 0];
217
218            [fStateField setStringValue: [torrent stateString]];
219/*
220            [fPercentField setStringValue: [NSString stringWithFormat: @"%.2f%%", 100.0 * [torrent progress]]];
221*/
222            int seeders = [torrent seeders], leechers = [torrent leechers];
223            [fSeedersField setStringValue: seeders < 0 ?
224                @"" : [NSString stringWithInt: seeders]];
225            [fLeechersField setStringValue: leechers < 0 ?
226                @"" : [NSString stringWithInt: leechers]];
227           
228            BOOL active = [torrent isActive];
229           
230            [fConnectedPeersField setStringValue: active ? [NSString
231                    stringWithInt: [torrent totalPeers]] : @""];
232            [fDownloadingFromField setStringValue: active ? [NSString
233                    stringWithInt: [torrent peersUploading]] : @""];
234            [fUploadingToField setStringValue: active ? [NSString
235                    stringWithInt: [torrent peersDownloading]] : @""];
236           
237            [fRatioField setStringValue: [NSString stringForRatioWithDownload:
238                                        downloadedTotal upload: uploadedTotal]];
239        }
240    }
241}
242
243- (void) updateInfoStatsAndSettings
244{
245    int numberSelected = [fTorrents count];
246
247    //set wait to start
248    BOOL waiting = NO, notWaiting = NO, canEnableWaiting = numberSelected > 0,
249        waitingSettingOn = [[[NSUserDefaults standardUserDefaults] stringForKey: @"StartSetting"]
250                                isEqualToString: @"Wait"];
251   
252    NSEnumerator * enumerator = [fTorrents objectEnumerator];
253    Torrent * torrent;
254    while ((torrent = [enumerator nextObject]))
255    {
256        if ([torrent waitingToStart])
257            waiting = YES;
258        else
259            notWaiting = YES;
260       
261        if (canEnableWaiting && !(![torrent isActive] && [torrent progress] < 1.0 && waitingSettingOn))
262            canEnableWaiting = NO;
263    }
264   
265    [fWaitToStartButton setState: waiting && notWaiting ? NSMixedState : (waiting ? NSOnState : NSOffState)];
266    [fWaitToStartButton setEnabled: canEnableWaiting];
267
268    //set ratio settings
269    if (numberSelected > 0)
270    {
271        enumerator = [fTorrents objectEnumerator];
272        torrent = [enumerator nextObject]; //first torrent
273        const int INVALID = -99;
274        int ratioSetting = [torrent stopRatioSetting];
275        float ratioLimit = [torrent ratioLimit];
276       
277        while ((ratioSetting != INVALID || ratioLimit != INVALID)
278                && (torrent = [enumerator nextObject]))
279        {
280            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
281                ratioSetting = INVALID;
282           
283            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
284                ratioLimit = INVALID;
285        }
286       
287        [fRatioMatrix setEnabled: YES];
288       
289        if (ratioSetting == RATIO_CHECK)
290        {
291            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
292            [fRatioLimitField setEnabled: YES];
293        }
294        else
295        {
296            if (ratioSetting == RATIO_NO_CHECK)
297                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
298            else if (ratioSetting == RATIO_GLOBAL)
299                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
300            else
301                [fRatioMatrix deselectAllCells];
302           
303            [fRatioLimitField setEnabled: NO];
304        }
305       
306        if (ratioLimit != INVALID)
307            [fRatioLimitField setFloatValue: ratioLimit];
308        else
309            [fRatioLimitField setStringValue: @""];
310    }
311    else
312    {
313        [fRatioMatrix deselectAllCells];
314        [fRatioMatrix setEnabled: NO];
315       
316        [fRatioLimitField setEnabled: NO];
317        [fRatioLimitField setStringValue: @""];
318    }
319   
320    [self updateInfoStats];
321}
322
323- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
324{
325    SEL action = [menuItem action];
326   
327    if (action == @selector(revealFile:))
328        return [fFileTable numberOfSelectedRows] > 0 &&
329            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
330       
331    return YES;
332}
333
334- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
335{
336    NSRect windowRect = [window frame];
337    windowRect.size.width = [window minSize].width;   
338    return windowRect;
339}
340
341- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
342{
343    NSString * identifier = [tabViewItem identifier];
344    [self setWindowForTab: identifier animate: YES];
345    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InfoTab"];
346}
347
348- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate
349{
350    float height;
351    if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
352        height = TAB_ACTIVITY_HEIGHT;
353    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
354        height = TAB_OPTIONS_HEIGHT;
355    else if ([identifier isEqualToString: TAB_FILES_IDENT])
356        height = TAB_FILES_HEIGHT;
357    else
358        height = TAB_INFO_HEIGHT;
359   
360    NSWindow * window = [self window];
361    NSRect frame = [window frame];
362    NSView * view = [[fTabView selectedTabViewItem] view];
363   
364    float difference = height - [view frame].size.height;
365    frame.origin.y -= difference;
366    frame.size.height += difference;
367   
368    if (animate)
369    {
370        [view setHidden: YES];
371        [window setFrame: frame display: YES animate: YES];
372        [view setHidden: NO];
373    }
374    else
375        [window setFrame: frame display: YES];
376   
377    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
378    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
379}
380
381- (void) setNextTab
382{
383    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == [fTabView numberOfTabViewItems] - 1)
384        [fTabView selectFirstTabViewItem: nil];
385    else
386        [fTabView selectNextTabViewItem: nil];
387}
388
389- (void) setPreviousTab
390{
391    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
392        [fTabView selectLastTabViewItem: nil];
393    else
394        [fTabView selectPreviousTabViewItem: nil];
395}
396
397- (int) numberOfRowsInTableView: (NSTableView *) tableView
398{
399    return [fFiles count];
400}
401
402- (id) tableView: (NSTableView *) tableView objectValueForTableColumn:
403                    (NSTableColumn *) column row: (int) row
404{
405    NSString * file = [fFiles objectAtIndex: row];
406    if ([[column identifier] isEqualToString: @"Icon"])
407        return [[NSWorkspace sharedWorkspace] iconForFileType: [file pathExtension]];
408    else
409        return [file lastPathComponent];
410}
411
412//only called on >= 10.4
413- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell
414        rect: (NSRectPointer) rect tableColumn: (NSTableColumn *) column
415        row: (int) row mouseLocation: (NSPoint) mouseLocation
416{
417    return [fFiles objectAtIndex: row];
418}
419
420- (void) revealFile: (id) sender
421{
422    NSIndexSet * indexSet = [fFileTable selectedRowIndexes];
423    unsigned int i;
424    for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i])
425        [[NSWorkspace sharedWorkspace] selectFile: [fFiles objectAtIndex: i] 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.