source: trunk/macosx/InfoOptionsViewController.m @ 11874

Last change on this file since 11874 was 11874, checked in by livings124, 11 years ago

#4017 display the global seeding settings in the inspector

  • Property svn:keywords set to Date Rev Author Id
File size: 16.0 KB
Line 
1/******************************************************************************
2 * $Id: InfoOptionsViewController.m 11874 2011-02-12 03:13:14Z livings124 $
3 *
4 * Copyright (c) 2010-2011 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 "InfoOptionsViewController.h"
26#import "NSStringAdditions.h"
27#import "Torrent.h"
28
29#define OPTION_POPUP_GLOBAL 0
30#define OPTION_POPUP_NO_LIMIT 1
31#define OPTION_POPUP_LIMIT 2
32
33#define OPTION_POPUP_PRIORITY_HIGH 0
34#define OPTION_POPUP_PRIORITY_NORMAL 1
35#define OPTION_POPUP_PRIORITY_LOW 2
36
37#define INVALID -99
38
39@interface InfoOptionsViewController (Private)
40
41- (void) setupInfo;
42
43@end
44
45@implementation InfoOptionsViewController
46
47- (id) init
48{
49    if ((self = [super initWithNibName: @"InfoOptionsView" bundle: nil]))
50    {
51        [self setTitle: NSLocalizedString(@"Options", "Inspector view -> title")];
52    }
53   
54    return self;
55}
56
57- (void) dealloc
58{
59    [fTorrents release];
60   
61    [super dealloc];
62}
63
64- (void) setInfoForTorrents: (NSArray *) torrents
65{
66    //don't check if it's the same in case the metadata changed
67    [fTorrents release];
68    fTorrents = [torrents retain];
69   
70    fSet = NO;
71}
72
73- (void) updateInfo
74{
75    if (!fSet)
76        [self setupInfo];
77   
78    fSet = YES;
79}
80
81- (void) updateOptions
82{
83    if ([fTorrents count] == 0)
84        return;
85   
86    //get bandwidth info
87    NSEnumerator * enumerator = [fTorrents objectEnumerator];
88    Torrent * torrent = [enumerator nextObject]; //first torrent
89   
90    NSInteger uploadUseSpeedLimit = [torrent usesSpeedLimit: YES] ? NSOnState : NSOffState,
91                uploadSpeedLimit = [torrent speedLimit: YES],
92                downloadUseSpeedLimit = [torrent usesSpeedLimit: NO] ? NSOnState : NSOffState,
93                downloadSpeedLimit = [torrent speedLimit: NO],
94                globalUseSpeedLimit = [torrent usesGlobalSpeedLimit] ? NSOnState : NSOffState;
95   
96    while ((torrent = [enumerator nextObject])
97            && (uploadUseSpeedLimit != NSMixedState || uploadSpeedLimit != INVALID
98                || downloadUseSpeedLimit != NSMixedState || downloadSpeedLimit != INVALID
99                || globalUseSpeedLimit != NSMixedState))
100    {
101        if (uploadUseSpeedLimit != NSMixedState && uploadUseSpeedLimit != ([torrent usesSpeedLimit: YES] ? NSOnState : NSOffState))
102            uploadUseSpeedLimit = NSMixedState;
103       
104        if (uploadSpeedLimit != INVALID && uploadSpeedLimit != [torrent speedLimit: YES])
105            uploadSpeedLimit = INVALID;
106       
107        if (downloadUseSpeedLimit != NSMixedState && downloadUseSpeedLimit != ([torrent usesSpeedLimit: NO] ? NSOnState : NSOffState))
108            downloadUseSpeedLimit = NSMixedState;
109       
110        if (downloadSpeedLimit != INVALID && downloadSpeedLimit != [torrent speedLimit: NO])
111            downloadSpeedLimit = INVALID;
112       
113        if (globalUseSpeedLimit != NSMixedState && globalUseSpeedLimit != ([torrent usesGlobalSpeedLimit] ? NSOnState : NSOffState))
114            globalUseSpeedLimit = NSMixedState;
115    }
116   
117    //set upload view
118    [fUploadLimitCheck setState: uploadUseSpeedLimit];
119    [fUploadLimitCheck setEnabled: YES];
120   
121    [fUploadLimitLabel setEnabled: uploadUseSpeedLimit == NSOnState];
122    [fUploadLimitField setEnabled: uploadUseSpeedLimit == NSOnState];
123    if (uploadSpeedLimit != INVALID)
124        [fUploadLimitField setIntValue: uploadSpeedLimit];
125    else
126        [fUploadLimitField setStringValue: @""];
127   
128    //set download view
129    [fDownloadLimitCheck setState: downloadUseSpeedLimit];
130    [fDownloadLimitCheck setEnabled: YES];
131   
132    [fDownloadLimitLabel setEnabled: downloadUseSpeedLimit == NSOnState];
133    [fDownloadLimitField setEnabled: downloadUseSpeedLimit == NSOnState];
134    if (downloadSpeedLimit != INVALID)
135        [fDownloadLimitField setIntValue: downloadSpeedLimit];
136    else
137        [fDownloadLimitField setStringValue: @""];
138   
139    //set global check
140    [fGlobalLimitCheck setState: globalUseSpeedLimit];
141    [fGlobalLimitCheck setEnabled: YES];
142   
143    //get ratio and idle info
144    enumerator = [fTorrents objectEnumerator];
145    torrent = [enumerator nextObject]; //first torrent
146   
147    NSInteger checkRatio = [torrent ratioSetting], checkIdle = [torrent idleSetting];
148    CGFloat ratioLimit = [torrent ratioLimit];
149    NSUInteger idleLimit = [torrent idleLimitMinutes];
150   
151    while ((torrent = [enumerator nextObject])
152            && (checkRatio != INVALID || ratioLimit != INVALID || checkIdle != INVALID || idleLimit != INVALID))
153    {
154        if (checkRatio != INVALID && checkRatio != [torrent ratioSetting])
155            checkRatio = INVALID;
156       
157        if (ratioLimit != INVALID && ratioLimit != [torrent ratioLimit])
158            ratioLimit = INVALID;
159       
160        if (checkIdle != INVALID && checkIdle != [torrent idleSetting])
161            checkIdle = INVALID;
162       
163        if (idleLimit != INVALID && idleLimit != [torrent idleLimitMinutes])
164            idleLimit = INVALID;
165    }
166   
167    //set ratio view
168    NSInteger index;
169    if (checkRatio == TR_RATIOLIMIT_SINGLE)
170        index = OPTION_POPUP_LIMIT;
171    else if (checkRatio == TR_RATIOLIMIT_UNLIMITED)
172        index = OPTION_POPUP_NO_LIMIT;
173    else if (checkRatio == TR_RATIOLIMIT_GLOBAL)
174        index = OPTION_POPUP_GLOBAL;
175    else
176        index = -1;
177    [fRatioPopUp selectItemAtIndex: index];
178    [fRatioPopUp setEnabled: YES];
179   
180    [fRatioLimitField setHidden: checkRatio != TR_RATIOLIMIT_SINGLE];
181    if (ratioLimit != INVALID)
182        [fRatioLimitField setFloatValue: ratioLimit];
183    else
184        [fRatioLimitField setStringValue: @""];
185   
186    [fRatioLimitGlobalLabel setHidden: checkRatio != TR_RATIOLIMIT_GLOBAL];
187    if (checkRatio == TR_RATIOLIMIT_GLOBAL)
188    {
189        NSString * global = [[NSUserDefaults standardUserDefaults] boolForKey: @"RatioCheck"]
190            ? [NSString stringForRatio: [[NSUserDefaults standardUserDefaults] floatForKey: @"RatioLimit"]]
191            : NSLocalizedString(@"disabled", "Info options -> global setting");
192        [fRatioLimitGlobalLabel setStringValue: global];
193    }
194   
195    //set idle view
196    if (checkIdle == TR_IDLELIMIT_SINGLE)
197        index = OPTION_POPUP_LIMIT;
198    else if (checkIdle == TR_IDLELIMIT_UNLIMITED)
199        index = OPTION_POPUP_NO_LIMIT;
200    else if (checkIdle == TR_IDLELIMIT_GLOBAL)
201        index = OPTION_POPUP_GLOBAL;
202    else
203        index = -1;
204    [fIdlePopUp selectItemAtIndex: index];
205    [fIdlePopUp setEnabled: YES];
206   
207    [fIdleLimitField setHidden: checkIdle != TR_IDLELIMIT_SINGLE];
208    if (idleLimit != INVALID)
209        [fIdleLimitField setIntegerValue: idleLimit];
210    else
211        [fIdleLimitField setStringValue: @""];
212    [fIdleLimitLabel setHidden: checkIdle != TR_IDLELIMIT_SINGLE];
213   
214    [fIdleLimitGlobalLabel setHidden: checkIdle != TR_IDLELIMIT_GLOBAL];
215    if (checkIdle == TR_IDLELIMIT_GLOBAL)
216    {
217        NSString * global;
218        if ([[NSUserDefaults standardUserDefaults] boolForKey: @"IdleLimitCheck"])
219        {
220            const NSInteger globalMin = [[NSUserDefaults standardUserDefaults] integerForKey: @"IdleLimitMinutes"];
221            global = globalMin == 1 ? NSLocalizedString(@"1 minute", "Info options -> global setting")
222                : [NSString stringWithFormat: NSLocalizedString(@"%d minutes", "Info options -> global setting"), globalMin];
223        }
224        else
225            global = NSLocalizedString(@"disabled", "Info options -> global setting");
226        [fIdleLimitGlobalLabel setStringValue: global];
227    }
228   
229    //get priority info
230    enumerator = [fTorrents objectEnumerator];
231    torrent = [enumerator nextObject]; //first torrent
232   
233    NSInteger priority = [torrent priority];
234   
235    while ((torrent = [enumerator nextObject]) && priority != INVALID)
236    {
237        if (priority != INVALID && priority != [torrent priority])
238            priority = INVALID;
239    }
240   
241    //set priority view
242    if (priority == TR_PRI_HIGH)
243        index = OPTION_POPUP_PRIORITY_HIGH;
244    else if (priority == TR_PRI_NORMAL)
245        index = OPTION_POPUP_PRIORITY_NORMAL;
246    else if (priority == TR_PRI_LOW)
247        index = OPTION_POPUP_PRIORITY_LOW;
248    else
249        index = -1;
250    [fPriorityPopUp selectItemAtIndex: index];
251    [fPriorityPopUp setEnabled: YES];
252   
253    //get peer info
254    enumerator = [fTorrents objectEnumerator];
255    torrent = [enumerator nextObject]; //first torrent
256   
257    NSInteger maxPeers = [torrent maxPeerConnect];
258   
259    while ((torrent = [enumerator nextObject]))
260    {
261        if (maxPeers != [torrent maxPeerConnect])
262        {
263            maxPeers = INVALID;
264            break;
265        }
266    }
267   
268    //set peer view
269    [fPeersConnectField setEnabled: YES];
270    [fPeersConnectLabel setEnabled: YES];
271    if (maxPeers != INVALID)
272        [fPeersConnectField setIntValue: maxPeers];
273    else
274        [fPeersConnectField setStringValue: @""];
275}
276
277- (void) setUseSpeedLimit: (id) sender
278{
279    const BOOL upload = sender == fUploadLimitCheck;
280   
281    if ([sender state] == NSMixedState)
282        [sender setState: NSOnState];
283    const BOOL limit = [sender state] == NSOnState;
284   
285    for (Torrent * torrent in fTorrents)
286        [torrent setUseSpeedLimit: limit upload: upload];
287   
288    NSTextField * field = upload ? fUploadLimitField : fDownloadLimitField;
289    [field setEnabled: limit];
290    if (limit)
291    {
292        [field selectText: self];
293        [[[self view] window] makeKeyAndOrderFront: self];
294    }
295   
296    NSTextField * label = upload ? fUploadLimitLabel : fDownloadLimitLabel;
297    [label setEnabled: limit];
298}
299
300- (void) setUseGlobalSpeedLimit: (id) sender
301{
302    if ([sender state] == NSMixedState)
303        [sender setState: NSOnState];
304    const BOOL limit = [sender state] == NSOnState;
305   
306    for (Torrent * torrent in fTorrents)
307        [torrent setUseGlobalSpeedLimit: limit];
308}
309
310- (void) setSpeedLimit: (id) sender
311{
312    const BOOL upload = sender == fUploadLimitField;
313    const NSInteger limit = [sender intValue];
314   
315    for (Torrent * torrent in fTorrents)
316        [torrent setSpeedLimit: limit upload: upload];
317}
318
319- (void) setRatioSetting: (id) sender
320{
321    NSInteger setting;
322    BOOL single = NO;
323    switch ([sender indexOfSelectedItem])
324    {
325        case OPTION_POPUP_LIMIT:
326            setting = TR_RATIOLIMIT_SINGLE;
327            single = YES;
328            break;
329        case OPTION_POPUP_NO_LIMIT:
330            setting = TR_RATIOLIMIT_UNLIMITED;
331            break;
332        case OPTION_POPUP_GLOBAL:
333            setting = TR_RATIOLIMIT_GLOBAL;
334            break;
335        default:
336            NSAssert1(NO, @"Unknown option selected in ratio popup: %d", [sender indexOfSelectedItem]);
337            return;
338    }
339   
340    for (Torrent * torrent in fTorrents)
341        [torrent setRatioSetting: setting];
342   
343    [fRatioLimitField setHidden: !single];
344    if (single)
345    {
346        [fRatioLimitField selectText: self];
347        [[[self view] window] makeKeyAndOrderFront: self];
348    }
349   
350    [self updateOptions]; //heavy-handed reload of global settings
351}
352
353- (void) setRatioLimit: (id) sender
354{
355    const CGFloat limit = [sender floatValue];
356   
357    for (Torrent * torrent in fTorrents)
358        [torrent setRatioLimit: limit];
359}
360
361- (void) setIdleSetting: (id) sender
362{
363    NSInteger setting;
364    BOOL single = NO;
365    switch ([sender indexOfSelectedItem])
366    {
367        case OPTION_POPUP_LIMIT:
368            setting = TR_IDLELIMIT_SINGLE;
369            single = YES;
370            break;
371        case OPTION_POPUP_NO_LIMIT:
372            setting = TR_IDLELIMIT_UNLIMITED;
373            break;
374        case OPTION_POPUP_GLOBAL:
375            setting = TR_IDLELIMIT_GLOBAL;
376            break;
377        default:
378            NSAssert1(NO, @"Unknown option selected in idle popup: %d", [sender indexOfSelectedItem]);
379            return;
380    }
381   
382    for (Torrent * torrent in fTorrents)
383        [torrent setIdleSetting: setting];
384   
385    [fIdleLimitField setHidden: !single];
386    [fIdleLimitLabel setHidden: !single];
387    if (single)
388    {
389        [fIdleLimitField selectText: self];
390        [[[self view] window] makeKeyAndOrderFront: self];
391    }
392   
393    [self updateOptions]; //heavy-handed reload of global settings
394}
395
396- (void) setIdleLimit: (id) sender
397{
398    const NSUInteger limit = [sender integerValue];
399   
400    for (Torrent * torrent in fTorrents)
401        [torrent setIdleLimitMinutes: limit];
402}
403
404- (void) setPriority: (id) sender
405{
406    tr_priority_t priority;
407    switch ([sender indexOfSelectedItem])
408    {
409        case OPTION_POPUP_PRIORITY_HIGH:
410            priority = TR_PRI_HIGH;
411            break;
412        case OPTION_POPUP_PRIORITY_NORMAL:
413            priority = TR_PRI_NORMAL;
414            break;
415        case OPTION_POPUP_PRIORITY_LOW:
416            priority = TR_PRI_LOW;
417            break;
418        default:
419            NSAssert1(NO, @"Unknown option selected in priority popup: %d", [sender indexOfSelectedItem]);
420            return;
421    }
422   
423    for (Torrent * torrent in fTorrents)
424        [torrent setPriority: priority];
425   
426    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
427}
428
429- (void) setPeersConnectLimit: (id) sender
430{
431    NSInteger limit = [sender intValue];
432   
433    for (Torrent * torrent in fTorrents)
434        [torrent setMaxPeerConnect: limit];
435}
436
437- (BOOL) control: (NSControl *) control textShouldBeginEditing: (NSText *) fieldEditor
438{
439    [fInitialString release];
440    fInitialString = [[control stringValue] retain];
441   
442    return YES;
443}
444
445- (BOOL) control: (NSControl *) control didFailToFormatString: (NSString *) string errorDescription: (NSString *) error
446{
447    NSBeep();
448    if (fInitialString)
449    {
450        [control setStringValue: fInitialString];
451        [fInitialString release];
452        fInitialString = nil;
453    }
454    return NO;
455}
456
457@end
458
459@implementation InfoOptionsViewController (Private)
460
461- (void) setupInfo
462{
463    if ([fTorrents count] == 0)
464    {
465        [fUploadLimitCheck setEnabled: NO];
466        [fUploadLimitCheck setState: NSOffState];
467        [fUploadLimitField setEnabled: NO];
468        [fUploadLimitLabel setEnabled: NO];
469        [fUploadLimitField setStringValue: @""];
470       
471        [fDownloadLimitCheck setEnabled: NO];
472        [fDownloadLimitCheck setState: NSOffState];
473        [fDownloadLimitField setEnabled: NO];
474        [fDownloadLimitLabel setEnabled: NO];
475        [fDownloadLimitField setStringValue: @""];
476       
477        [fGlobalLimitCheck setEnabled: NO];
478        [fGlobalLimitCheck setState: NSOffState];
479       
480        [fPriorityPopUp setEnabled: NO];
481        [fPriorityPopUp selectItemAtIndex: -1];
482       
483        [fRatioPopUp setEnabled: NO];
484        [fRatioPopUp selectItemAtIndex: -1];
485        [fRatioLimitField setHidden: YES];
486        [fRatioLimitField setStringValue: @""];
487        [fRatioLimitGlobalLabel setHidden: YES];
488       
489        [fIdlePopUp setEnabled: NO];
490        [fIdlePopUp selectItemAtIndex: -1];
491        [fIdleLimitField setHidden: YES];
492        [fIdleLimitField setStringValue: @""];
493        [fIdleLimitLabel setHidden: YES];
494        [fIdleLimitGlobalLabel setHidden: YES];
495       
496        [fPeersConnectField setEnabled: NO];
497        [fPeersConnectField setStringValue: @""];
498        [fPeersConnectLabel setEnabled: NO];
499    }
500    else
501        [self updateOptions];
502}
503
504@end
Note: See TracBrowser for help on using the repository browser.