source: trunk/macosx/InfoWindowController.m @ 451

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

Checkbox for paused torrent to be in queue.

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