source: trunk/macosx/InfoWindowController.m @ 497

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

Auto-start check in the inspector now can be applied to multiple torrents.

  • Property svn:keywords set to Date Rev Author Id
File size: 16.6 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 497 2006-07-01 21:34:53Z 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 198.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/*
112            [fDownloadRateField setStringValue: @""];
113            [fUploadRateField setStringValue: @""];
114*/
115            [fDownloadedValidField setStringValue: @""];
116            [fDownloadedTotalField setStringValue: @""];
117            [fUploadedTotalField setStringValue: @""];
118        }
119       
120        [fImageView setImage: fAppIcon];
121       
122        [fNameField setToolTip: nil];
123
124        [fTrackerField setStringValue: @""];
125        [fTrackerField setToolTip: nil];
126        [fAnnounceField setStringValue: @""];
127        [fAnnounceField setToolTip: nil];
128        [fPieceSizeField setStringValue: @""];
129        [fPiecesField setStringValue: @""];
130        [fHashField setStringValue: @""];
131        [fHashField setToolTip: nil];
132       
133        [fTorrentLocationField setStringValue: @""];
134        [fTorrentLocationField setToolTip: nil];
135        [fDataLocationField setStringValue: @""];
136        [fDataLocationField setToolTip: nil];
137        [fDateStartedField setStringValue: @""];
138       
139/*
140        [fStateField setStringValue: @""];
141        [fPercentField setStringValue: @""];
142*/
143        [fRatioField setStringValue: @""];
144       
145        [fSeedersField setStringValue: @""];
146        [fLeechersField setStringValue: @""];
147        [fConnectedPeersField setStringValue: @""];
148        [fDownloadingFromField setStringValue: @""];
149        [fUploadingToField setStringValue: @""];
150    }
151    else
152    {   
153        Torrent * torrent = [fTorrents objectAtIndex: 0];
154       
155        [fImageView setImage: [torrent icon]];
156       
157        NSString * name = [torrent name];
158        [fNameField setStringValue: name];
159        [fNameField setToolTip: name];
160        [fSizeField setStringValue: [NSString stringForFileSize: [torrent size]]];
161       
162        NSString * tracker = [torrent tracker],
163                * announce = [torrent announce],
164                * hashString = [torrent hashString];
165        [fTrackerField setStringValue: tracker];
166        [fTrackerField setToolTip: tracker];
167        [fAnnounceField setStringValue: announce];
168        [fAnnounceField setToolTip: announce];
169        [fPieceSizeField setStringValue: [NSString stringForFileSize: [torrent pieceSize]]];
170        [fPiecesField setIntValue: [torrent pieceCount]];
171        [fHashField setStringValue: hashString];
172        [fHashField setToolTip: hashString];
173       
174        [fTorrentLocationField setStringValue: [torrent torrentLocationString]];
175        [fTorrentLocationField setToolTip: [torrent torrentLocation]];
176        [fDataLocationField setStringValue: [[torrent dataLocation] stringByAbbreviatingWithTildeInPath]];
177        [fDataLocationField setToolTip: [torrent dataLocation]];
178        [fDateStartedField setObjectValue: [torrent date]];
179    }
180    [self updateInfoStats];
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    //set wait to start
194    BOOL waiting = NO, notWaiting = NO, canEnableWaiting = numberSelected > 0,
195        waitingSettingOn = [[[NSUserDefaults standardUserDefaults] stringForKey: @"StartSetting"]
196                                isEqualToString: @"Wait"];
197   
198    enumerator = [fTorrents objectEnumerator];
199    while ((torrent = [enumerator nextObject]))
200    {
201        if ([torrent waitingToStart])
202            waiting = YES;
203        else
204            notWaiting = YES;
205       
206        if (canEnableWaiting && !(![torrent isActive] && [torrent progress] < 1.0 && waitingSettingOn))
207            canEnableWaiting = NO;
208    }
209   
210    [fWaitToStartButton setState: waiting && notWaiting ? NSMixedState : (waiting ? NSOnState : NSOffState)];
211    [fWaitToStartButton setEnabled: canEnableWaiting];
212
213    //set ratio settings
214    if (numberSelected > 0)
215    {
216        enumerator = [fTorrents objectEnumerator];
217        torrent = [enumerator nextObject]; //first torrent
218        const int INVALID = -99;
219        int ratioSetting = [torrent stopRatioSetting];
220        float ratioLimit = [torrent ratioLimit];
221       
222        while ((ratioSetting != INVALID || ratioLimit != INVALID)
223                && (torrent = [enumerator nextObject]))
224        {
225            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
226                ratioSetting = INVALID;
227           
228            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
229                ratioLimit = INVALID;
230        }
231       
232        [fRatioMatrix setEnabled: YES];
233       
234        if (ratioSetting == RATIO_CHECK)
235        {
236            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
237            [fRatioLimitField setEnabled: YES];
238        }
239        else
240        {
241            if (ratioSetting == RATIO_NO_CHECK)
242                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
243            else if (ratioSetting == RATIO_GLOBAL)
244                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
245            else
246                [fRatioMatrix deselectAllCells];
247           
248            [fRatioLimitField setEnabled: NO];
249        }
250       
251        if (ratioLimit != INVALID)
252            [fRatioLimitField setFloatValue: ratioLimit];
253        else
254            [fRatioLimitField setStringValue: @""];
255    }
256    else
257    {
258        [fRatioMatrix deselectAllCells];
259        [fRatioMatrix setEnabled: NO];
260       
261        [fRatioLimitField setEnabled: NO];
262        [fRatioLimitField setStringValue: @""];
263    }
264}
265
266- (void) updateInfoStats
267{
268    int numberSelected = [fTorrents count];
269    if (numberSelected > 0)
270    {
271        //float downloadRate = 0, uploadRate = 0;
272        float downloadedValid = 0;
273        uint64_t downloadedTotal = 0, uploadedTotal = 0;
274        Torrent * torrent;
275        NSEnumerator * enumerator = [fTorrents objectEnumerator];
276        while ((torrent = [enumerator nextObject]))
277        {
278            /*downloadRate += [torrent downloadRate];
279            uploadRate += [torrent uploadRate];
280            */
281            downloadedValid += [torrent downloadedValid];
282            downloadedTotal += [torrent downloadedTotal];
283            uploadedTotal += [torrent uploadedTotal];
284        }
285/*
286        [fDownloadRateField setStringValue: [NSString stringForSpeed: downloadRate]];
287        [fUploadRateField setStringValue: [NSString stringForSpeed: uploadRate]];
288*/
289        [fDownloadedValidField setStringValue: [NSString stringForFileSize: downloadedValid]];
290        [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
291        [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
292   
293        if (numberSelected == 1)
294        {
295            torrent = [fTorrents objectAtIndex: 0];
296/*
297            [fStateField setStringValue: [torrent state]];
298            [fPercentField setStringValue: [NSString stringWithFormat:
299                                            @"%.2f%%", 100.0 * [torrent progress]]];
300*/
301            int seeders = [torrent seeders], leechers = [torrent leechers];
302            [fSeedersField setStringValue: seeders < 0 ?
303                @"N/A" : [NSString stringWithInt: seeders]];
304            [fLeechersField setStringValue: leechers < 0 ?
305                @"N/A" : [NSString stringWithInt: leechers]];
306           
307            BOOL active = [torrent isActive];
308           
309            [fConnectedPeersField setStringValue: active ? [NSString
310                    stringWithInt: [torrent totalPeers]] : @"N/A"];
311            [fDownloadingFromField setStringValue: active ? [NSString
312                    stringWithInt: [torrent peersUploading]] : @"N/A"];
313            [fUploadingToField setStringValue: active ? [NSString
314                    stringWithInt: [torrent peersDownloading]] : @"N/A"];
315           
316            [fRatioField setStringValue: [NSString stringForRatioWithDownload:
317                                        downloadedTotal upload: uploadedTotal]];
318        }
319    }
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]]
383                                    == [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]
426            inFileViewerRootedAtPath: nil];
427}
428
429- (void) setRatioCheck: (id) sender
430{
431    int ratioSetting, tag = [[fRatioMatrix selectedCell] tag];
432    if (tag == RATIO_CHECK_TAG)
433        ratioSetting = RATIO_CHECK;
434    else if (tag == RATIO_NO_CHECK_TAG)
435        ratioSetting = RATIO_NO_CHECK;
436    else
437        ratioSetting = RATIO_GLOBAL;
438
439    Torrent * torrent;
440    NSEnumerator * enumerator = [fTorrents objectEnumerator];
441    while ((torrent = [enumerator nextObject]))
442        [torrent setStopRatioSetting: ratioSetting];
443   
444    [self setRatioLimit: fRatioLimitField];
445    [fRatioLimitField setEnabled: tag == RATIO_CHECK_TAG];
446}
447
448- (void) setRatioLimit: (id) sender
449{
450    Torrent * torrent;
451    NSEnumerator * enumerator = [fTorrents objectEnumerator];
452
453    float ratioLimit = [sender floatValue];
454    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
455            || ratioLimit < 0)
456    {
457        NSBeep();
458        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
459        while ((torrent = [enumerator nextObject]))
460            if (ratioLimit != [torrent ratioLimit])
461            {
462                [sender setStringValue: @""];
463                return;
464            }
465       
466        [sender setFloatValue: ratioLimit];
467    }
468    else
469    {
470        while ((torrent = [enumerator nextObject]))
471            [torrent setRatioLimit: ratioLimit];
472    }
473}
474
475- (void) setWaitToStart: (id) sender
476{
477    int state = [sender state];
478
479    NSEnumerator * enumerator = [fTorrents objectEnumerator];
480    Torrent * torrent;
481    while ((torrent = [enumerator nextObject]))
482        [torrent setWaitToStart: state];
483   
484    [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentStartSettingChange" object: fTorrents];
485}
486
487@end
Note: See TracBrowser for help on using the repository browser.