1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2011-2012 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 "GlobalOptionsPopoverViewController.h"
26
27@implementation GlobalOptionsPopoverViewController
28
29- (id) initWithHandle: (tr_session *) handle
30{
31    if ((self = [super initWithNibName: @"GlobalOptionsPopover" bundle: nil]))
32    {
33        fHandle = handle;
34        
35        fDefaults = [NSUserDefaults standardUserDefaults];
36    }
37    
38    return self;
39}
40
41- (void) awakeFromNib
42{
43    [fUploadLimitField setIntValue: [fDefaults integerForKey: @"UploadLimit"]];
44    [fDownloadLimitField setIntValue: [fDefaults integerForKey: @"DownloadLimit"]];
45    
46    [fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
47    [fIdleStopField setIntegerValue: [fDefaults integerForKey: @"IdleLimitMinutes"]];
48}
49
50- (IBAction) updatedDisplayString: (id) sender
51{
52    [[NSNotificationCenter defaultCenter] postNotificationName: @"RefreshTorrentTable" object: nil];
53}
54
55- (IBAction) setDownSpeedSetting: (id) sender
56{
57    tr_sessionLimitSpeed(fHandle, TR_DOWN, [fDefaults boolForKey: @"CheckDownload"]);
58    
59    [[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
60}
61
62- (IBAction) setDownSpeedLimit: (id) sender
63{
64    const NSInteger limit = [sender integerValue];
65    [fDefaults setInteger: limit forKey: @"DownloadLimit"];
66    tr_sessionSetSpeedLimit_KBps(fHandle, TR_DOWN, limit);
67    
68    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateSpeedLimitValuesOutsidePrefs" object: nil];
69    [[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
70}
71
72- (IBAction) setUpSpeedSetting: (id) sender
73{
74    tr_sessionLimitSpeed(fHandle, TR_UP, [fDefaults boolForKey: @"CheckUpload"]);
75    
76    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateSpeedLimitValuesOutsidePrefs" object: nil];
77    [[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
78}
79
80- (IBAction) setUpSpeedLimit: (id) sender
81{
82    const NSInteger limit = [sender integerValue];
83    [fDefaults setInteger: limit forKey: @"UploadLimit"];
84    tr_sessionSetSpeedLimit_KBps(fHandle, TR_UP, limit);
85    
86    [[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
87}
88
89- (IBAction) setRatioStopSetting: (id) sender
90{
91    tr_sessionSetRatioLimited(fHandle, [fDefaults boolForKey: @"RatioCheck"]);
92    
93    //reload main table for seeding progress
94    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
95    
96    //reload global settings in inspector
97    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGlobalOptions" object: nil];
98}
99
100- (IBAction) setRatioStopLimit: (id) sender
101{
102    const CGFloat value = [sender floatValue];
103    [fDefaults setFloat: value forKey: @"RatioLimit"];
104    tr_sessionSetRatioLimit(fHandle, value);
105    
106    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateRatioStopValueOutsidePrefs" object: nil];
107    
108    //reload main table for seeding progress
109    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
110    
111    //reload global settings in inspector
112    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGlobalOptions" object: nil];
113}
114
115- (IBAction) setIdleStopSetting: (id) sender
116{
117    tr_sessionSetIdleLimited(fHandle, [fDefaults boolForKey: @"IdleLimitCheck"]);
118    
119    //reload main table for remaining seeding time
120    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
121    
122    //reload global settings in inspector
123    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGlobalOptions" object: nil];
124}
125
126- (IBAction) setIdleStopLimit: (id) sender
127{
128    const NSInteger value = [sender integerValue];
129    [fDefaults setInteger: value forKey: @"IdleLimitMinutes"];
130    tr_sessionSetIdleLimit(fHandle, value);
131    
132    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateIdleStopValueOutsidePrefs" object: nil];
133    
134    //reload main table for remaining seeding time
135    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
136    
137    //reload global settings in inspector
138    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGlobalOptions" object: nil];
139}
140
141- (BOOL) control: (NSControl *) control textShouldBeginEditing: (NSText *) fieldEditor
142{
143    [fInitialString release];
144    fInitialString = [[control stringValue] retain];
145    
146    return YES;
147}
148
149- (BOOL) control: (NSControl *) control didFailToFormatString: (NSString *) string errorDescription: (NSString *) error
150{
151    NSBeep();
152    if (fInitialString)
153    {
154        [control setStringValue: fInitialString];
155        [fInitialString release];
156        fInitialString = nil;
157    }
158    return NO;
159}
160
161@end
162