source: trunk/macosx/InfoWindowController.m @ 417

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

Comment out code so connections in Interface Builder don't need to be made and unused calculations aren't made.

  • Property svn:keywords set to Date Rev Author Id
File size: 15.4 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 417 2006-06-20 17:10:35Z 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_STATUS_IDENT @"Status"
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 185.0
42#define TAB_STATUS_HEIGHT 199.0
43#define TAB_OPTIONS_HEIGHT 82.0
44#define TAB_FILES_HEIGHT 250.0
45
46@implementation InfoWindowController
47
48- (void) awakeFromNib
49{
50    fAppIcon = [[NSApp applicationIconImage] copy];
51   
52    fFiles = [[NSMutableArray alloc] initWithCapacity: 6];
53    [fFileTable setDoubleAction: @selector(revealFile:)];
54   
55    //window location and size
56    NSPanel * window = (NSPanel *)[self window];
57   
58    [window setBecomesKeyOnlyIfNeeded: YES];
59   
60    [window setFrameAutosaveName: @"InspectorWindowFrame"];
61    [window setFrameUsingName: @"InspectorWindowFrame"];
62   
63    NSRect frame = [window frame];
64    float difference = TAB_INFO_HEIGHT - [[[fTabView selectedTabViewItem] view] frame].size.height;
65    frame.origin.y -= difference;
66    frame.size.height += difference;
67    [window setFrame: frame display: YES];
68   
69    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
70    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
71}
72
73- (void) dealloc
74{
75    if (fTorrents)
76        [fTorrents release];
77    [fFiles release];
78
79    [fAppIcon release];
80    [super dealloc];
81}
82
83- (void) updateInfoForTorrents: (NSArray *) torrents
84{
85    if (fTorrents)
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 ratio settings
194    if (numberSelected > 0)
195    {
196        NSEnumerator * enumerator = [fTorrents objectEnumerator];
197        Torrent * torrent = [enumerator nextObject]; //first torrent
198        const int INVALID = -99;
199        int ratioSetting = [torrent stopRatioSetting];
200        float ratioLimit = [torrent ratioLimit];
201       
202        while ((ratioSetting != INVALID || ratioLimit != INVALID)
203                && (torrent = [enumerator nextObject]))
204        {
205            if (ratioSetting != INVALID && ratioSetting != [torrent stopRatioSetting])
206                ratioSetting = INVALID;
207           
208            if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
209                ratioLimit = INVALID;
210        }
211       
212        [fRatioMatrix setEnabled: YES];
213       
214        if (ratioSetting == RATIO_CHECK)
215        {
216            [fRatioMatrix selectCellWithTag: RATIO_CHECK_TAG];
217            [fRatioLimitField setEnabled: YES];
218        }
219        else
220        {
221            if (ratioSetting == RATIO_NO_CHECK)
222                [fRatioMatrix selectCellWithTag: RATIO_NO_CHECK_TAG];
223            else if (ratioSetting == RATIO_GLOBAL)
224                [fRatioMatrix selectCellWithTag: RATIO_GLOBAL_TAG];
225            else
226                [fRatioMatrix deselectAllCells];
227           
228            [fRatioLimitField setEnabled: NO];
229        }
230       
231        if (ratioLimit != INVALID)
232            [fRatioLimitField setFloatValue: ratioLimit];
233        else
234            [fRatioLimitField setStringValue: @""];
235    }
236    else
237    {
238        [fRatioMatrix deselectAllCells];
239        [fRatioMatrix setEnabled: NO];
240       
241        [fRatioLimitField setEnabled: NO];
242        [fRatioLimitField setStringValue: @""];
243    }
244}
245
246- (void) updateInfoStats
247{
248    int numberSelected = [fTorrents count];
249    if (numberSelected > 0)
250    {
251        //float downloadRate = 0, uploadRate = 0;
252        float downloadedValid = 0;
253        uint64_t downloadedTotal = 0, uploadedTotal = 0;
254        Torrent * torrent;
255        NSEnumerator * enumerator = [fTorrents objectEnumerator];
256        while ((torrent = [enumerator nextObject]))
257        {
258            /*downloadRate += [torrent downloadRate];
259            uploadRate += [torrent uploadRate];
260            */
261            downloadedValid += [torrent downloadedValid];
262            downloadedTotal += [torrent downloadedTotal];
263            uploadedTotal += [torrent uploadedTotal];
264        }
265/*
266        [fDownloadRateField setStringValue: [NSString stringForSpeed: downloadRate]];
267        [fUploadRateField setStringValue: [NSString stringForSpeed: uploadRate]];
268*/
269        [fDownloadedValidField setStringValue: [NSString stringForFileSize: downloadedValid]];
270        [fDownloadedTotalField setStringValue: [NSString stringForFileSize: downloadedTotal]];
271        [fUploadedTotalField setStringValue: [NSString stringForFileSize: uploadedTotal]];
272   
273        if (numberSelected == 1)
274        {
275            torrent = [fTorrents objectAtIndex: 0];
276/*
277            [fStateField setStringValue: [torrent state]];
278            [fPercentField setStringValue: [NSString stringWithFormat:
279                                            @"%.2f%%", 100.0 * [torrent progress]]];
280*/
281            int seeders = [torrent seeders], leechers = [torrent leechers];
282            [fSeedersField setStringValue: seeders < 0 ?
283                @"N/A" : [NSString stringWithInt: seeders]];
284            [fLeechersField setStringValue: leechers < 0 ?
285                @"N/A" : [NSString stringWithInt: leechers]];
286           
287            BOOL active = [torrent isActive];
288           
289            [fConnectedPeersField setStringValue: active ? [NSString
290                    stringWithInt: [torrent totalPeers]] : @"N/A"];
291            [fDownloadingFromField setStringValue: active ? [NSString
292                    stringWithInt: [torrent peersUploading]] : @"N/A"];
293            [fUploadingToField setStringValue: active ? [NSString
294                    stringWithInt: [torrent peersDownloading]] : @"N/A"];
295           
296            [fRatioField setStringValue: [NSString stringForRatioWithDownload:
297                                        downloadedTotal upload: uploadedTotal]];
298        }
299    }
300}
301
302- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
303{
304    SEL action = [menuItem action];
305   
306    if (action == @selector(revealFile:))
307        return [fFileTable numberOfSelectedRows] > 0 &&
308            [[[fTabView selectedTabViewItem] identifier] isEqualToString: TAB_FILES_IDENT];
309       
310    return YES;
311}
312
313- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
314{
315    NSRect windowRect = [window frame];
316    windowRect.size.width = [window minSize].width;
317   
318    return windowRect;
319}
320
321- (void) tabView: (NSTabView *) tabView didSelectTabViewItem: (NSTabViewItem *) tabViewItem
322{
323    NSWindow * window = [self window];
324    NSRect frame = [window frame];
325
326    float height;
327    NSString * identifier = [tabViewItem identifier];
328    if ([identifier isEqualToString: TAB_INFO_IDENT])
329        height = TAB_INFO_HEIGHT;
330    else if ([identifier isEqualToString: TAB_STATUS_IDENT])
331        height = TAB_STATUS_HEIGHT;
332    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
333        height = TAB_OPTIONS_HEIGHT;
334    else
335        height = TAB_FILES_HEIGHT;
336   
337    NSView * view = [tabViewItem view];
338    float difference = height - [view frame].size.height;
339    frame.origin.y -= difference;
340    frame.size.height += difference;
341   
342    [view setHidden: YES];
343    [window setFrame: frame display: YES animate: YES];
344    [view setHidden: NO];
345   
346    [window setMinSize: NSMakeSize(MIN_WINDOW_WIDTH, frame.size.height)];
347    [window setMaxSize: NSMakeSize(MAX_WINDOW_WIDTH, frame.size.height)];
348}
349
350- (void) setNextTab
351{
352    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]]
353                                    == [fTabView numberOfTabViewItems] - 1)
354        [fTabView selectFirstTabViewItem: nil];
355    else
356        [fTabView selectNextTabViewItem: nil];
357}
358
359- (void) setPreviousTab
360{
361    if ([fTabView indexOfTabViewItem: [fTabView selectedTabViewItem]] == 0)
362        [fTabView selectLastTabViewItem: nil];
363    else
364        [fTabView selectPreviousTabViewItem: nil];
365}
366
367- (int) numberOfRowsInTableView: (NSTableView *) tableView
368{
369    return [fFiles count];
370}
371
372- (id) tableView: (NSTableView *) tableView objectValueForTableColumn:
373                    (NSTableColumn *) column row: (int) row
374{
375    NSString * file = [fFiles objectAtIndex: row];
376    if ([[column identifier] isEqualToString: @"Icon"])
377        return [[NSWorkspace sharedWorkspace] iconForFileType: [file pathExtension]];
378    else
379        return [file lastPathComponent];
380}
381
382//only called on >= 10.4
383- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell
384        rect: (NSRectPointer) rect tableColumn: (NSTableColumn *) column
385        row: (int) row mouseLocation: (NSPoint) mouseLocation
386{
387    return [fFiles objectAtIndex: row];
388}
389
390- (void) revealFile: (id) sender
391{
392    NSIndexSet * indexSet = [fFileTable selectedRowIndexes];
393    unsigned int i;
394    for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i])
395        [[NSWorkspace sharedWorkspace] selectFile: [fFiles objectAtIndex: i]
396            inFileViewerRootedAtPath: nil];
397}
398
399- (void) setRatioCheck: (id) sender
400{
401    NSButtonCell * selected = [fRatioMatrix selectedCell];
402    int ratioSetting;
403    if (selected == [fRatioMatrix cellWithTag: RATIO_CHECK_TAG])
404        ratioSetting = RATIO_CHECK;
405    else if (selected == [fRatioMatrix cellWithTag: RATIO_NO_CHECK_TAG])
406        ratioSetting = RATIO_NO_CHECK;
407    else
408        ratioSetting = RATIO_GLOBAL;
409
410    Torrent * torrent;
411    NSEnumerator * enumerator = [fTorrents objectEnumerator];
412    while ((torrent = [enumerator nextObject]))
413        [torrent setStopRatioSetting: ratioSetting];
414   
415    [self setRatioLimit: fRatioLimitField];
416    [fRatioLimitField setEnabled: selected == [fRatioMatrix cellWithTag: RATIO_CHECK_TAG]];
417}
418
419- (void) setRatioLimit: (id) sender
420{
421    Torrent * torrent;
422    NSEnumerator * enumerator = [fTorrents objectEnumerator];
423
424    float ratioLimit = [sender floatValue];
425    if (![[sender stringValue] isEqualToString:
426            [NSString stringWithFormat: @"%.2f", ratioLimit]]
427            || ratioLimit < 0)
428    {
429        NSBeep();
430        float ratioLimit = [[enumerator nextObject] ratioLimit]; //use first torrent
431        while ((torrent = [enumerator nextObject]))
432            if (ratioLimit != [torrent ratioLimit])
433            {
434                [sender setStringValue: @""];
435                return;
436            }
437       
438        [sender setFloatValue: ratioLimit];
439    }
440    else
441    {
442        while ((torrent = [enumerator nextObject]))
443            [torrent setRatioLimit: ratioLimit];
444    }
445}
446
447@end
Note: See TracBrowser for help on using the repository browser.