source: trunk/macosx/InfoOptionsViewController.m @ 11617

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

update the copyright years in the Mac code to 2011

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