source: trunk/macosx/InfoWindowController.m @ 676

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

Updated help with better icon.

  • Property svn:keywords set to Date Rev Author Id
File size: 16.4 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 676 2006-07-23 18:57:50Z 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:
221                                            @"%.2f%%", 100.0 * [torrent progress]]];
222*/
223            int seeders = [torrent seeders], leechers = [torrent leechers];
224            [fSeedersField setStringValue: seeders < 0 ?
225                @"N/A" : [NSString stringWithInt: seeders]];
226            [fLeechersField setStringValue: leechers < 0 ?
227                @"N/A" : [NSString stringWithInt: leechers]];
228           
229            BOOL active = [torrent isActive];
230           
231            [fConnectedPeersField setStringValue: active ? [NSString
232                    stringWithInt: [torrent totalPeers]] : @"N/A"];
233            [fDownloadingFromField setStringValue: active ? [NSString
234                    stringWithInt: [torrent peersUploading]] : @"N/A"];
235            [fUploadingToField setStringValue: active ? [NSString
236                    stringWithInt: [torrent peersDownloading]] : @"N/A"];
237           
238            [fRatioField setStringValue: [NSString stringForRatioWithDownload:
239                                        downloadedTotal upload: uploadedTotal]];
240        }
241    }
242}
243
244- (void) updateInfoStatsAndSettings
245{
246    int numberSelected = [fTorrents count];
247
248    //set wait to start
249    BOOL waiting = NO, notWaiting = NO, canEnableWaiting = numberSelected > 0,
250        waitingSettingOn = [[[NSUserDefaults standardUserDefaults] stringForKey: @"StartSetting"]
251                                isEqualToString: @"Wait"];
252   
253    NSEnumerator * enumerator = [fTorrents objectEnumerator];
254    Torrent * torrent;
255    while ((torrent = [enumerator nextObject]))
256    {
257        if ([torrent waitingToStart])
258            waiting = YES;
259        else
260            notWaiting = YES;
261       
262        if (canEnableWaiting && !(![torrent isActive] && [torrent progress] < 1.0 && waitingSettingOn))
263            canEnableWaiting = NO;
264    }
265   
266    [fWaitToStartButton setState: waiting && notWaiting ? NSMixedState : (waiting ? NSOnState : NSOffState)];
267    [fWaitToStartButton setEnabled: canEnableWaiting];
268
269    //set ratio settings
270    if (numberSelected > 0)
271    {
272        enumerator = [fTorrents objectEnumerator];
273        torrent = [enumerator nextObject]; //first torrent
274        const int INVALID = -99;
275        int ratioSetting = [torrent stopRatioSetting];
276        float ratioLimit = [torrent ratioLimit];
277       
278        while ((ratioSetting != INVALID || ratioLimit != INVALID)
279                && (torrent = [enumerator nextObject]))
280        {
281            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
282                ratioSetting = INVALID;
283           
284            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
285                ratioLimit = INVALID;
286        }
287       
288        [fRatioMatrix setEnabled: YES];
289       
290        if (ratioSetting == RATIO_CHECK)
291        {
292            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
293            [fRatioLimitField setEnabled: YES];
294        }
295        else
296        {
297            if (ratioSetting == RATIO_NO_CHECK)
298                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
299            else if (ratioSetting == RATIO_GLOBAL)
300                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
301            else
302                [fRatioMatrix deselectAllCells];
303           
304            [fRatioLimitField setEnabled: NO];
305        }
306       
307        if (ratioLimit != INVALID)
308            [fRatioLimitField setFloatValue: ratioLimit];
309        else
310            [fRatioLimitField setStringValue: @""];
311    }
312    else
313    {
314        [fRatioMatrix deselectAllCells];
315        [fRatioMatrix setEnabled: NO];
316       
317        [fRatioLimitField setEnabled: NO];
318        [fRatioLimitField setStringValue: @""];
319    }
320   
321    [self updateInfoStats];
322}
323
324- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
325{
326    SEL action = [menuItem action];
327   
328    if (action == @selector(revealFile:))
329        return [fFileTable numberOfSelectedRows] > 0 &&
330            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
331       
332    return YES;
333}
334
335- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
336{
337    NSRect windowRect = [window frame];
338    windowRect.size.width = [window minSize].width;   
339    return windowRect;
340}
341
342- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
343{
344    NSString * identifier = [tabViewItem identifier];
345    [self setWindowForTab: identifier animate: YES];
346    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InfoTab"];
347}
348
349- (void) setWindowForTab: (NSString *) identifier animate: (BOOL) animate
350{
351    float height;
352    if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
353        height = TAB_ACTIVITY_HEIGHT;
354    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
355        height = TAB_OPTIONS_HEIGHT;
356    else if ([identifier isEqualToString: TAB_FILES_IDENT])
357        height = TAB_FILES_HEIGHT;
358    else
359        height = TAB_INFO_HEIGHT;
360   
361    NSWindow * window = [self window];
362    NSRect frame = [window frame];
363    NSView * view = [[fTabView selectedTabViewItem] view];
364   
365    float difference = height - [view frame].size.height;
366    frame.origin.y -= difference;
367    frame.size.height += difference;
368   
369    if (animate)
370    {
371        [view setHidden: YES];
372        [window setFrame: frame display: YES animate: YES];
373        [view setHidden: NO];
374    }
375    else
376        [window setFrame: frame display: YES];
377   
378    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
379    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
380}
381
382- (void) setNextTab
383{
384    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == [fTabView numberOfTabViewItems] - 1)
385        [fTabView selectFirstTabViewItem: nil];
386    else
387        [fTabView selectNextTabViewItem: nil];
388}
389
390- (void) setPreviousTab
391{
392    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
393        [fTabView selectLastTabViewItem: nil];
394    else
395        [fTabView selectPreviousTabViewItem: nil];
396}
397
398- (int) numberOfRowsInTableView: (NSTableView *) tableView
399{
400    return [fFiles count];
401}
402
403- (id) tableView: (NSTableView *) tableView objectValueForTableColumn:
404                    (NSTableColumn *) column row: (int) row
405{
406    NSString * file = [fFiles objectAtIndex: row];
407    if ([[column identifier] isEqualToString: @"Icon"])
408        return [[NSWorkspace sharedWorkspace] iconForFileType: [file pathExtension]];
409    else
410        return [file lastPathComponent];
411}
412
413//only called on >= 10.4
414- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell
415        rect: (NSRectPointer) rect tableColumn: (NSTableColumn *) column
416        row: (int) row mouseLocation: (NSPoint) mouseLocation
417{
418    return [fFiles objectAtIndex: row];
419}
420
421- (void) revealFile: (id) sender
422{
423    NSIndexSet * indexSet = [fFileTable selectedRowIndexes];
424    unsigned int i;
425    for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i])
426        [[NSWorkspace sharedWorkspace] selectFile: [fFiles objectAtIndex: i]
427            inFileViewerRootedAtPath: nil];
428}
429
430- (void) setRatioCheck: (id) sender
431{
432    int ratioSetting, tag = [[fRatioMatrix selectedCell] tag];
433    if (tag == RATIO_CHECK_TAG)
434        ratioSetting = RATIO_CHECK;
435    else if (tag == RATIO_NO_CHECK_TAG)
436        ratioSetting = RATIO_NO_CHECK;
437    else
438        ratioSetting = RATIO_GLOBAL;
439
440    Torrent * torrent;
441    NSEnumerator * enumerator = [fTorrents objectEnumerator];
442    while ((torrent = [enumerator nextObject]))
443        [torrent setStopRatioSetting: ratioSetting];
444   
445    [self setRatioLimit: fRatioLimitField];
446    [fRatioLimitField setEnabled: tag == RATIO_CHECK_TAG];
447}
448
449- (void) setRatioLimit: (id) sender
450{
451    Torrent * torrent;
452    NSEnumerator * enumerator = [fTorrents objectEnumerator];
453
454    float ratioLimit = [sender floatValue];
455    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
456            || ratioLimit < 0)
457    {
458        NSBeep();
459        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
460        while ((torrent = [enumerator nextObject]))
461            if (ratioLimit != [torrent ratioLimit])
462            {
463                [sender setStringValue: @""];
464                return;
465            }
466       
467        [sender setFloatValue: ratioLimit];
468    }
469    else
470    {
471        while ((torrent = [enumerator nextObject]))
472            [torrent setRatioLimit: ratioLimit];
473    }
474}
475
476- (void) setWaitToStart: (id) sender
477{
478    int state = [sender state];
479
480    NSEnumerator * enumerator = [fTorrents objectEnumerator];
481    Torrent * torrent;
482    while ((torrent = [enumerator nextObject]))
483        [torrent setWaitToStart: state];
484   
485    [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentStartSettingChange" object: fTorrents];
486}
487
488@end
Note: See TracBrowser for help on using the repository browser.