source: trunk/macosx/InfoOptionsViewController.m @ 11875

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

inspector: update the displayed global seeding settings only when necessary; update only the displayed global seeding settings instead of all the options

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