source: trunk/macosx/PrefsController.m @ 8227

Last change on this file since 8227 was 8156, checked in by livings124, 13 years ago

save the value of the speed limit day without conversion

  • Property svn:keywords set to Date Rev Author Id
File size: 44.6 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 8156 2009-04-05 20:13:16Z livings124 $
3 *
4 * Copyright (c) 2005-2009 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 "PrefsController.h"
26#import "BlocklistDownloaderViewController.h"
27#import "BlocklistScheduler.h"
28#import "PortChecker.h"
29#import "BonjourController.h"
30#import "NSStringAdditions.h"
31#import "UKKQueue.h"
32#import "utils.h"
33
34#import <Sparkle/Sparkle.h>
35
36#define DOWNLOAD_FOLDER     0
37#define DOWNLOAD_TORRENT    2
38
39#define PROXY_HTTP      0
40#define PROXY_SOCKS4    1
41#define PROXY_SOCKS5    2
42
43#define RPC_IP_ADD_TAG      0
44#define RPC_IP_REMOVE_TAG   1
45
46#define TOOLBAR_GENERAL     @"TOOLBAR_GENERAL"
47#define TOOLBAR_TRANSFERS   @"TOOLBAR_TRANSFERS"
48#define TOOLBAR_GROUPS      @"TOOLBAR_GROUPS"
49#define TOOLBAR_BANDWIDTH   @"TOOLBAR_BANDWIDTH"
50#define TOOLBAR_PEERS       @"TOOLBAR_PEERS"
51#define TOOLBAR_NETWORK     @"TOOLBAR_NETWORK"
52#define TOOLBAR_REMOTE      @"TOOLBAR_REMOTE"
53
54#define PROXY_KEYCHAIN_SERVICE  "Transmission:Proxy"
55#define PROXY_KEYCHAIN_NAME     "Proxy"
56
57#define RPC_KEYCHAIN_SERVICE    "Transmission:Remote"
58#define RPC_KEYCHAIN_NAME       "Remote"
59
60#define WEBUI_URL   @"http://localhost:%d/transmission/web/"
61
62@interface PrefsController (Private)
63
64- (void) setPrefView: (id) sender;
65
66- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
67- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
68- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
69
70- (void) setKeychainPassword: (const char *) password forService: (const char *) service username: (const char *) username;
71
72@end
73
74@implementation PrefsController
75
76tr_session * fHandle;
77+ (void) setHandle: (tr_session *) handle
78{
79    fHandle = handle;
80}
81
82+ (tr_session *) handle
83{
84    return fHandle;
85}
86
87- (id) init
88{
89    if ((self = [super initWithWindowNibName: @"PrefsWindow"]))
90    {
91        fDefaults = [NSUserDefaults standardUserDefaults];
92       
93        //check for old version download location (before 1.1)
94        NSString * choice;
95        if ((choice = [fDefaults stringForKey: @"DownloadChoice"]))
96        {
97            [fDefaults setBool: [choice isEqualToString: @"Constant"] forKey: @"DownloadLocationConstant"];
98            [fDefaults setBool: YES forKey: @"DownloadAsk"];
99           
100            [fDefaults removeObjectForKey: @"DownloadChoice"];
101        }
102       
103        //save a new random port
104        if ([fDefaults boolForKey: @"RandomPort"])
105            [fDefaults setInteger: tr_sessionGetPeerPort(fHandle) forKey: @"BindPort"];
106       
107        //set auto import
108        NSString * autoPath;
109        if ([fDefaults boolForKey: @"AutoImport"] && (autoPath = [fDefaults stringForKey: @"AutoImportDirectory"]))
110            [[UKKQueue sharedFileWatcher] addPath: [autoPath stringByExpandingTildeInPath]];
111       
112        //set blocklist scheduler
113        [[BlocklistScheduler scheduler] updateSchedule];
114       
115        //set encryption
116        [self setEncryptionMode: nil];
117       
118        //set proxy type
119        [self updateProxyType];
120        [self updateProxyPassword];
121       
122        //update rpc whitelist
123        [self updateRPCPassword];
124       
125        fRPCWhitelistArray = [[fDefaults arrayForKey: @"RPCWhitelist"] mutableCopy];
126        if (!fRPCWhitelistArray)
127            fRPCWhitelistArray = [[NSMutableArray arrayWithObject: @"127.0.0.1"] retain];
128        [self updateRPCWhitelist];
129       
130        //reset old Sparkle settings from previous versions
131        [fDefaults removeObjectForKey: @"SUScheduledCheckInterval"];
132        if ([fDefaults objectForKey: @"CheckForUpdates"])
133        {
134            [[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates: [fDefaults boolForKey: @"CheckForUpdates"]];
135            [fDefaults removeObjectForKey: @"CheckForUpdates"];
136        }
137       
138        [self setAutoUpdateToBeta: nil];
139    }
140   
141    return self;
142}
143
144- (void) dealloc
145{
146    [[NSNotificationCenter defaultCenter] removeObserver: self];
147   
148    [fPortStatusTimer invalidate];
149    if (fPortChecker)
150    {
151        [fPortChecker cancelProbe];
152        [fPortChecker release];
153    }
154   
155    [fRPCWhitelistArray release];
156   
157    [fRPCPassword release];
158   
159    [super dealloc];
160}
161
162- (void) awakeFromNib
163{
164    fHasLoaded = YES;
165   
166    NSToolbar * toolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
167    [toolbar setDelegate: self];
168    [toolbar setAllowsUserCustomization: NO];
169    [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
170    [toolbar setSizeMode: NSToolbarSizeModeRegular];
171    [toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
172    [[self window] setToolbar: toolbar];
173    [toolbar release];
174   
175    [self setPrefView: nil];
176   
177    //set download folder
178    [fFolderPopUp selectItemAtIndex: [fDefaults boolForKey: @"DownloadLocationConstant"] ? DOWNLOAD_FOLDER : DOWNLOAD_TORRENT];
179   
180    //set stop ratio
181    [fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
182   
183    //set limits
184    [self updateLimitFields];
185   
186    //set speed limit
187    [fSpeedLimitUploadField setIntValue: [fDefaults integerForKey: @"SpeedLimitUploadLimit"]];
188    [fSpeedLimitDownloadField setIntValue: [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]];
189   
190    //set port
191    [fPortField setIntValue: [fDefaults integerForKey: @"BindPort"]];
192    fNatStatus = -1;
193   
194    [self updatePortStatus];
195    fPortStatusTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0 target: self
196                        selector: @selector(updatePortStatus) userInfo: nil repeats: YES];
197   
198    //set peer connections
199    [fPeersGlobalField setIntValue: [fDefaults integerForKey: @"PeersTotal"]];
200    [fPeersTorrentField setIntValue: [fDefaults integerForKey: @"PeersTorrent"]];
201   
202    //set queue values
203    [fQueueDownloadField setIntValue: [fDefaults integerForKey: @"QueueDownloadNumber"]];
204    [fQueueSeedField setIntValue: [fDefaults integerForKey: @"QueueSeedNumber"]];
205    [fStalledField setIntValue: [fDefaults integerForKey: @"StalledMinutes"]];
206   
207    //set proxy type
208    [fProxyAddressField setStringValue: [fDefaults stringForKey: @"ProxyAddress"]];
209    int proxyType;
210    switch (tr_sessionGetProxyType(fHandle))
211    {
212        case TR_PROXY_SOCKS4:
213            proxyType = PROXY_SOCKS4;
214            break;
215        case TR_PROXY_SOCKS5:
216            proxyType = PROXY_SOCKS5;
217            break;
218        case TR_PROXY_HTTP:
219            proxyType = PROXY_HTTP;
220    }
221    [fProxyTypePopUp selectItemAtIndex: proxyType];
222   
223    //set proxy password - does NOT need to be released
224    [fProxyPasswordField setStringValue: [NSString stringWithUTF8String: tr_sessionGetProxyPassword(fHandle)]];
225   
226    //set proxy port
227    [fProxyPortField setIntValue: [fDefaults integerForKey: @"ProxyPort"]];
228   
229    //set blocklist
230    [self updateBlocklistFields];
231    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateBlocklistFields)
232        name: @"BlocklistUpdated" object: nil];
233   
234    //set rpc port
235    [fRPCPortField setIntValue: [fDefaults integerForKey: @"RPCPort"]];
236   
237    //set rpc password
238    if (fRPCPassword)
239        [fRPCPasswordField setStringValue: fRPCPassword];
240}
241
242- (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
243{
244    NSToolbarItem * item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
245
246    if ([ident isEqualToString: TOOLBAR_GENERAL])
247    {
248        [item setLabel: NSLocalizedString(@"General", "Preferences -> toolbar item title")];
249        [item setImage: [NSImage imageNamed: NSImageNamePreferencesGeneral]];
250        [item setTarget: self];
251        [item setAction: @selector(setPrefView:)];
252        [item setAutovalidates: NO];
253    }
254    else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
255    {
256        [item setLabel: NSLocalizedString(@"Transfers", "Preferences -> toolbar item title")];
257        [item setImage: [NSImage imageNamed: @"Transfers.png"]];
258        [item setTarget: self];
259        [item setAction: @selector(setPrefView:)];
260        [item setAutovalidates: NO];
261    }
262    else if ([ident isEqualToString: TOOLBAR_GROUPS])
263    {
264        [item setLabel: NSLocalizedString(@"Groups", "Preferences -> toolbar item title")];
265        [item setImage: [NSImage imageNamed: @"Groups.png"]];
266        [item setTarget: self];
267        [item setAction: @selector(setPrefView:)];
268        [item setAutovalidates: NO];
269    }
270    else if ([ident isEqualToString: TOOLBAR_BANDWIDTH])
271    {
272        [item setLabel: NSLocalizedString(@"Bandwidth", "Preferences -> toolbar item title")];
273        [item setImage: [NSImage imageNamed: @"Bandwidth.png"]];
274        [item setTarget: self];
275        [item setAction: @selector(setPrefView:)];
276        [item setAutovalidates: NO];
277    }
278    else if ([ident isEqualToString: TOOLBAR_PEERS])
279    {
280        [item setLabel: NSLocalizedString(@"Peers", "Preferences -> toolbar item title")];
281        [item setImage: [NSImage imageNamed: NSImageNameUserGroup]];
282        [item setTarget: self];
283        [item setAction: @selector(setPrefView:)];
284        [item setAutovalidates: NO];
285    }
286    else if ([ident isEqualToString: TOOLBAR_NETWORK])
287    {
288        [item setLabel: NSLocalizedString(@"Network", "Preferences -> toolbar item title")];
289        [item setImage: [NSImage imageNamed: NSImageNameNetwork]];
290        [item setTarget: self];
291        [item setAction: @selector(setPrefView:)];
292        [item setAutovalidates: NO];
293    }
294    else if ([ident isEqualToString: TOOLBAR_REMOTE])
295    {
296        [item setLabel: NSLocalizedString(@"Remote", "Preferences -> toolbar item title")];
297        [item setImage: [NSImage imageNamed: @"Remote.png"]];
298        [item setTarget: self];
299        [item setAction: @selector(setPrefView:)];
300        [item setAutovalidates: NO];
301    }
302    else
303    {
304        [item release];
305        return nil;
306    }
307
308    return [item autorelease];
309}
310
311- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
312{
313    return [self toolbarDefaultItemIdentifiers: toolbar];
314}
315
316- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
317{
318    return [self toolbarAllowedItemIdentifiers: toolbar];
319}
320
321- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
322{
323    return [NSArray arrayWithObjects: TOOLBAR_GENERAL, TOOLBAR_TRANSFERS, TOOLBAR_GROUPS, TOOLBAR_BANDWIDTH,
324                                        TOOLBAR_PEERS, TOOLBAR_NETWORK, TOOLBAR_REMOTE, nil];
325}
326
327//for a beta release, always use the beta appcast
328#if defined(TR_BETA_RELEASE)
329#define SPARKLE_TAG YES
330#else
331#define SPARKLE_TAG [fDefaults boolForKey: @"AutoUpdateBeta"]
332#endif
333- (void) setAutoUpdateToBeta: (id) sender
334{
335    [[SUUpdater sharedUpdater] setAllowedTags: SPARKLE_TAG ? [NSSet setWithObject: @"beta"] : nil];
336}
337
338- (void) setPort: (id) sender
339{
340    const int port = [sender intValue];
341    [fDefaults setInteger: port forKey: @"BindPort"];
342    tr_sessionSetPeerPort(fHandle, port);
343   
344    fPeerPort = -1;
345    [self updatePortStatus];
346}
347
348- (void) randomPort: (id) sender
349{
350    const tr_port port = tr_sessionSetPeerPortRandom(fHandle);
351   
352    [fPortField setIntValue: port];
353    [self setPort: fPortField];
354}
355
356- (void) setRandomPortOnStart: (id) sender
357{
358    tr_sessionSetPeerPortRandomOnStart(fHandle, [sender state] == NSOnState);
359}
360
361- (void) setNat: (id) sender
362{
363    tr_sessionSetPortForwardingEnabled(fHandle, [fDefaults boolForKey: @"NatTraversal"]);
364   
365    fNatStatus = -1;
366    [self updatePortStatus];
367}
368
369- (void) updatePortStatus
370{
371    const tr_port_forwarding fwd = tr_sessionGetPortForwarding(fHandle);
372    const int port = tr_sessionGetPeerPort(fHandle);
373    BOOL natStatusChanged = (fNatStatus != fwd);
374    BOOL peerPortChanged = (fPeerPort != port);
375
376    if (natStatusChanged || peerPortChanged)
377    {
378        fNatStatus = fwd;
379        fPeerPort = port;
380       
381        [fPortStatusField setStringValue: @""];
382        [fPortStatusImage setImage: nil];
383        [fPortStatusProgress startAnimation: self];
384       
385        if (fPortChecker)
386        {
387            [fPortChecker cancelProbe];
388            [fPortChecker release];
389        }
390        BOOL delay = natStatusChanged || tr_sessionIsPortForwardingEnabled(fHandle);
391        fPortChecker = [[PortChecker alloc] initForPort: fPeerPort delay: delay withDelegate: self];
392    }
393}
394
395- (void) portCheckerDidFinishProbing: (PortChecker *) portChecker
396{
397    [fPortStatusProgress stopAnimation: self];
398    switch ([fPortChecker status])
399    {
400        case PORT_STATUS_OPEN:
401            [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Network -> port status")];
402            [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.png"]];
403            break;
404        case PORT_STATUS_CLOSED:
405            [fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Network -> port status")];
406            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]];
407            break;
408        case PORT_STATUS_ERROR:
409            [fPortStatusField setStringValue: NSLocalizedString(@"Port check site is down", "Preferences -> Network -> port status")];
410            [fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.png"]];
411            break;
412    }
413    [fPortChecker release];
414    fPortChecker = nil;
415}
416
417- (NSArray *) sounds
418{
419    NSMutableArray * sounds = [NSMutableArray array];
420   
421    NSArray * directories = [NSArray arrayWithObjects: @"/System/Library/Sounds", @"/Library/Sounds", @"Library/Sounds", nil];
422   
423    for (NSString * directory in directories)
424    {
425        BOOL isDirectory;
426        if ([[NSFileManager defaultManager] fileExistsAtPath: directory isDirectory: &isDirectory] && isDirectory)
427        {
428            NSArray * directoryContents = [[NSFileManager defaultManager] directoryContentsAtPath: directory];
429            for (NSString * sound in directoryContents)
430            {
431                sound = [sound stringByDeletingPathExtension];
432                if ([NSSound soundNamed: sound])
433                    [sounds addObject: sound];
434            }
435        }
436    }
437   
438    return sounds;
439}
440
441- (void) setSound: (id) sender
442{
443    //play sound when selecting
444    NSSound * sound;
445    if ((sound = [NSSound soundNamed: [sender titleOfSelectedItem]]))
446        [sound play];
447}
448
449- (void) setPeersGlobal: (id) sender
450{
451    const int count = [sender intValue];
452    [fDefaults setInteger: count forKey: @"PeersTotal"];
453    tr_sessionSetPeerLimit(fHandle, count);
454}
455
456- (void) setPeersTorrent: (id) sender
457{
458    const int count = [sender intValue];
459    [fDefaults setInteger: count forKey: @"PeersTorrent"];
460}
461
462- (void) setPEX: (id) sender
463{
464    tr_sessionSetPexEnabled(fHandle, [fDefaults boolForKey: @"PEXGlobal"]);
465}
466
467- (void) setEncryptionMode: (id) sender
468{
469    const tr_encryption_mode mode = [fDefaults boolForKey: @"EncryptionPrefer"] ?
470        ([fDefaults boolForKey: @"EncryptionRequire"] ? TR_ENCRYPTION_REQUIRED : TR_ENCRYPTION_PREFERRED) : TR_CLEAR_PREFERRED;
471    tr_sessionSetEncryption(fHandle, mode);
472}
473
474- (void) setBlocklistEnabled: (id) sender
475{
476    const BOOL enable = [sender state] == NSOnState;
477    [fDefaults setBool: enable forKey: @"Blocklist"];
478    tr_blocklistSetEnabled(fHandle, enable);
479   
480    [[BlocklistScheduler scheduler] updateSchedule];
481}
482
483- (void) updateBlocklist: (id) sender
484{
485    [BlocklistDownloaderViewController downloadWithPrefsController: self];
486}
487
488- (void) setBlocklistAutoUpdate: (id) sender
489{
490    [[BlocklistScheduler scheduler] updateSchedule];
491}
492
493- (void) updateBlocklistFields
494{
495    const BOOL exists = tr_blocklistExists(fHandle);
496   
497    if (exists)
498    {
499        NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
500        [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
501        [numberFormatter setMaximumFractionDigits: 0];
502        NSString * countString = [numberFormatter stringFromNumber: [NSNumber numberWithInt: tr_blocklistGetRuleCount(fHandle)]];
503        [numberFormatter release];
504       
505        [fBlocklistMessageField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ IP address rules in list",
506            "Prefs -> blocklist -> message"), countString]];
507    }
508    else
509        [fBlocklistMessageField setStringValue: NSLocalizedString(@"A blocklist must first be downloaded",
510            "Prefs -> blocklist -> message")];
511   
512    [fBlocklistEnableCheck setEnabled: exists];
513    [fBlocklistEnableCheck setState: exists && [fDefaults boolForKey: @"Blocklist"]];
514   
515    NSString * updatedDateString;
516    if (exists)
517    {
518        NSDate * updatedDate = [fDefaults objectForKey: @"BlocklistLastUpdate"];
519        if (updatedDate)
520        {
521            NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
522            [dateFormatter setDateStyle: NSDateFormatterFullStyle];
523            [dateFormatter setTimeStyle: NSDateFormatterShortStyle];
524           
525            updatedDateString = [dateFormatter stringFromDate: updatedDate];
526            [dateFormatter release];
527        }
528        else
529            updatedDateString = NSLocalizedString(@"N/A", "Prefs -> blocklist -> message");
530    }
531    else
532        updatedDateString = NSLocalizedString(@"Never", "Prefs -> blocklist -> message");
533   
534    [fBlocklistDateField setStringValue: [NSString stringWithFormat: @"%@: %@",
535        NSLocalizedString(@"Last updated", "Prefs -> blocklist -> message"), updatedDateString]];
536}
537
538- (void) applySpeedSettings: (id) sender
539{
540    tr_sessionLimitSpeed(fHandle, TR_UP, [fDefaults boolForKey: @"CheckUpload"]);
541    tr_sessionSetSpeedLimit(fHandle, TR_UP, [fDefaults integerForKey: @"UploadLimit"]);
542   
543    tr_sessionLimitSpeed(fHandle, TR_DOWN, [fDefaults boolForKey: @"CheckDownload"]);
544    tr_sessionSetSpeedLimit(fHandle, TR_DOWN, [fDefaults integerForKey: @"DownloadLimit"]);
545   
546    [[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
547}
548
549- (void) applyAltSpeedSettings
550{
551    tr_sessionSetAltSpeed(fHandle, TR_UP, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
552    tr_sessionSetAltSpeed(fHandle, TR_DOWN, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
553       
554    [[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
555}
556
557- (void) applyRatioSetting: (id) sender
558{
559    //[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
560    tr_sessionSetRatioLimited(fHandle, [fDefaults boolForKey: @"RatioCheck"]);
561    tr_sessionSetRatioLimit(fHandle, [fDefaults floatForKey: @"RatioLimit"]);
562}
563
564- (void) updateRatioStopField
565{
566    if (fHasLoaded)
567        [fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
568   
569    [self applyRatioSetting: nil];
570}
571
572- (void) setRatioStop: (id) sender
573{
574    [fDefaults setFloat: [sender floatValue] forKey: @"RatioLimit"];
575   
576    [self applyRatioSetting: nil];
577}
578
579- (void) updateLimitFields
580{
581    if (!fHasLoaded)
582        return;
583   
584    [fUploadField setIntValue: [fDefaults integerForKey: @"UploadLimit"]];
585    [fDownloadField setIntValue: [fDefaults integerForKey: @"DownloadLimit"]];
586}
587
588- (void) setGlobalLimit: (id) sender
589{
590    [fDefaults setInteger: [sender intValue] forKey: sender == fUploadField ? @"UploadLimit" : @"DownloadLimit"];
591    [self applySpeedSettings: self];
592}
593
594- (void) setSpeedLimit: (id) sender
595{
596    [fDefaults setInteger: [sender intValue] forKey: sender == fSpeedLimitUploadField
597                                                        ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit"];
598    [self applyAltSpeedSettings];
599}
600
601- (void) setAutoSpeedLimit: (id) sender
602{
603    tr_sessionUseAltSpeedTime(fHandle, [fDefaults boolForKey: @"SpeedLimitAuto"]);
604}
605
606- (void) setAutoSpeedLimitTime: (id) sender
607{
608    tr_sessionSetAltSpeedBegin(fHandle, [PrefsController dateToTimeSum: [fDefaults objectForKey: @"SpeedLimitAutoOnDate"]]);
609    tr_sessionSetAltSpeedEnd(fHandle, [PrefsController dateToTimeSum: [fDefaults objectForKey: @"SpeedLimitAutoOffDate"]]);
610}
611
612- (void) setAutoSpeedLimitDay: (id) sender
613{
614    tr_sessionSetAltSpeedDay(fHandle, [[sender selectedItem] tag]);
615}
616
617+ (NSInteger) dateToTimeSum: (NSDate *) date
618{
619    NSCalendar * calendar = [NSCalendar currentCalendar];
620    NSDateComponents * components = [calendar components: NSHourCalendarUnit | NSMinuteCalendarUnit fromDate: date];
621    return [components hour] * 60 + [components minute];
622}
623
624+ (NSDate *) timeSumToDate: (NSInteger) sum
625{
626    NSDateComponents * comps = [[[NSDateComponents alloc] init] autorelease];
627    [comps setHour: sum / 60];
628    [comps setMinute: sum % 60];
629   
630    return [[NSCalendar currentCalendar] dateFromComponents: comps];
631}
632
633- (BOOL) control: (NSControl *) control textShouldBeginEditing: (NSText *) fieldEditor
634{
635    [fInitialString release];
636    fInitialString = [[control stringValue] retain];
637   
638    return YES;
639}
640
641- (BOOL) control: (NSControl *) control didFailToFormatString: (NSString *) string errorDescription: (NSString *) error
642{
643    NSBeep();
644    if (fInitialString)
645    {
646        [control setStringValue: fInitialString];
647        [fInitialString release];
648        fInitialString = nil;
649    }
650    return NO;
651}
652
653- (void) setBadge: (id) sender
654{
655    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
656}
657
658- (void) resetWarnings: (id) sender
659{
660    [fDefaults removeObjectForKey: @"WarningDuplicate"];
661    [fDefaults removeObjectForKey: @"WarningRemainingSpace"];
662    [fDefaults removeObjectForKey: @"WarningFolderDataSameName"];
663    [fDefaults removeObjectForKey: @"WarningResetStats"];
664    [fDefaults removeObjectForKey: @"WarningCreatorBlankAddress"];
665    [fDefaults removeObjectForKey: @"WarningRemoveBuiltInTracker"];
666    [fDefaults removeObjectForKey: @"WarningInvalidOpen"];
667}
668
669- (void) setQueue: (id) sender
670{
671    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
672}
673
674- (void) setQueueNumber: (id) sender
675{
676    [fDefaults setInteger: [sender intValue] forKey: sender == fQueueDownloadField ? @"QueueDownloadNumber" : @"QueueSeedNumber"];
677    [self setQueue: nil];
678}
679
680- (void) setStalled: (id) sender
681{
682    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
683}
684
685- (void) setStalledMinutes: (id) sender
686{
687    [fDefaults setInteger: [sender intValue] forKey: @"StalledMinutes"];
688    [self setStalled: nil];
689}
690
691- (void) setDownloadLocation: (id) sender
692{
693    [fDefaults setBool: [fFolderPopUp indexOfSelectedItem] == DOWNLOAD_FOLDER forKey: @"DownloadLocationConstant"];
694}
695
696- (void) folderSheetShow: (id) sender
697{
698    NSOpenPanel * panel = [NSOpenPanel openPanel];
699
700    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
701    [panel setAllowsMultipleSelection: NO];
702    [panel setCanChooseFiles: NO];
703    [panel setCanChooseDirectories: YES];
704    [panel setCanCreateDirectories: YES];
705
706    [panel beginSheetForDirectory: nil file: nil types: nil
707        modalForWindow: [self window] modalDelegate: self didEndSelector:
708        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
709}
710
711- (void) incompleteFolderSheetShow: (id) sender
712{
713    NSOpenPanel * panel = [NSOpenPanel openPanel];
714
715    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
716    [panel setAllowsMultipleSelection: NO];
717    [panel setCanChooseFiles: NO];
718    [panel setCanChooseDirectories: YES];
719    [panel setCanCreateDirectories: YES];
720
721    [panel beginSheetForDirectory: nil file: nil types: nil
722        modalForWindow: [self window] modalDelegate: self didEndSelector:
723        @selector(incompleteFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
724}
725
726- (void) setAutoImport: (id) sender
727{
728    NSString * path;
729    if ((path = [fDefaults stringForKey: @"AutoImportDirectory"]))
730    {
731        path = [path stringByExpandingTildeInPath];
732        if ([fDefaults boolForKey: @"AutoImport"])
733            [[UKKQueue sharedFileWatcher] addPath: path];
734        else
735            [[UKKQueue sharedFileWatcher] removePathFromQueue: path];
736       
737        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
738    }
739    else
740        [self importFolderSheetShow: nil];
741}
742
743- (void) importFolderSheetShow: (id) sender
744{
745    NSOpenPanel * panel = [NSOpenPanel openPanel];
746
747    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
748    [panel setAllowsMultipleSelection: NO];
749    [panel setCanChooseFiles: NO];
750    [panel setCanChooseDirectories: YES];
751    [panel setCanCreateDirectories: YES];
752
753    [panel beginSheetForDirectory: nil file: nil types: nil
754        modalForWindow: [self window] modalDelegate: self didEndSelector:
755        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
756}
757
758- (void) setAutoSize: (id) sender
759{
760    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
761}
762
763- (void) setProxyEnabled: (id) sender
764{
765    tr_sessionSetProxyEnabled(fHandle, [fDefaults boolForKey: @"Proxy"]);
766}
767
768- (void) setProxyAddress: (id) sender
769{
770    NSString * address = [sender stringValue];
771    tr_sessionSetProxy(fHandle, [address UTF8String]);
772    [fDefaults setObject: address forKey: @"ProxyAddress"];
773}
774
775- (void) setProxyPort: (id) sender
776{
777    int port = [sender intValue];
778    [fDefaults setInteger: port forKey: @"ProxyPort"];
779    tr_sessionSetProxyPort(fHandle, port);
780}
781
782- (void) setProxyType: (id) sender
783{
784    NSString * type;
785    switch ([sender indexOfSelectedItem])
786    {
787        case PROXY_HTTP:
788            type = @"HTTP";
789            break;
790        case PROXY_SOCKS4:
791            type = @"SOCKS4";
792            break;
793        case PROXY_SOCKS5:
794            type = @"SOCKS5";
795    }
796   
797    [fDefaults setObject: type forKey: @"ProxyType"];
798    [self updateProxyType];
799}
800
801- (void) updateProxyType
802{
803    NSString * typeString = [fDefaults stringForKey: @"ProxyType"];
804    tr_proxy_type type;
805    if ([typeString isEqualToString: @"SOCKS4"])
806        type = TR_PROXY_SOCKS4;
807    else if ([typeString isEqualToString: @"SOCKS5"])
808        type = TR_PROXY_SOCKS5;
809    else
810    {
811        //safety
812        if (![typeString isEqualToString: @"HTTP"])
813        {
814            typeString = @"HTTP";
815            [fDefaults setObject: typeString forKey: @"ProxyType"];
816        }
817        type = TR_PROXY_HTTP;
818    }
819   
820    tr_sessionSetProxyType(fHandle, type);
821}
822
823- (void) setProxyAuthorize: (id) sender
824{
825    BOOL enable = [fDefaults boolForKey: @"ProxyAuthorize"];
826    tr_sessionSetProxyAuthEnabled(fHandle, enable);
827}
828
829- (void) setProxyUsername: (id) sender
830{
831    tr_sessionSetProxyUsername(fHandle, [[fDefaults stringForKey: @"ProxyUsername"] UTF8String]);
832}
833
834- (void) setProxyPassword: (id) sender
835{
836    const char * password = [[sender stringValue] UTF8String];
837    [self setKeychainPassword: password forService: PROXY_KEYCHAIN_SERVICE username: PROXY_KEYCHAIN_NAME];
838   
839    tr_sessionSetProxyPassword(fHandle, password);
840}
841
842- (void) updateProxyPassword
843{
844    UInt32 passwordLength;
845    const char * password = nil;
846    SecKeychainFindGenericPassword(NULL, strlen(PROXY_KEYCHAIN_SERVICE), PROXY_KEYCHAIN_SERVICE,
847        strlen(PROXY_KEYCHAIN_NAME), PROXY_KEYCHAIN_NAME, &passwordLength, (void **)&password, NULL);
848   
849    if (password != NULL)
850    {
851        char fullPassword[passwordLength+1];
852        strncpy(fullPassword, password, passwordLength);
853        fullPassword[passwordLength] = '\0';
854        SecKeychainItemFreeContent(NULL, (void *)password);
855       
856        tr_sessionSetProxyPassword(fHandle, fullPassword);
857        [fProxyPasswordField setStringValue: [NSString stringWithUTF8String: fullPassword]];
858    }
859}
860
861- (void) setRPCEnabled: (id) sender
862{
863    BOOL enable = [fDefaults boolForKey: @"RPC"];
864    tr_sessionSetRPCEnabled(fHandle, enable);
865   
866    [self setRPCWebUIDiscovery: nil];
867}
868
869- (void) linkWebUI: (id) sender
870{
871    NSString * urlString = [NSString stringWithFormat: WEBUI_URL, [fDefaults integerForKey: @"RPCPort"]];
872    [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: urlString]];
873}
874
875- (void) setRPCAuthorize: (id) sender
876{
877    tr_sessionSetRPCPasswordEnabled(fHandle, [fDefaults boolForKey: @"RPCAuthorize"]);
878}
879
880- (void) setRPCUsername: (id) sender
881{
882    tr_sessionSetRPCUsername(fHandle, [[fDefaults stringForKey: @"RPCUsername"] UTF8String]);
883}
884
885- (void) setRPCPassword: (id) sender
886{
887    [fRPCPassword release];
888    fRPCPassword = [[sender stringValue] copy];
889   
890    const char * password = [[sender stringValue] UTF8String];
891    [self setKeychainPassword: password forService: RPC_KEYCHAIN_SERVICE username: RPC_KEYCHAIN_NAME];
892   
893    tr_sessionSetRPCPassword(fHandle, password);
894}
895
896- (void) updateRPCPassword
897{
898    UInt32 passwordLength;
899    const char * password = nil;
900    SecKeychainFindGenericPassword(NULL, strlen(RPC_KEYCHAIN_SERVICE), RPC_KEYCHAIN_SERVICE,
901        strlen(RPC_KEYCHAIN_NAME), RPC_KEYCHAIN_NAME, &passwordLength, (void **)&password, NULL);
902   
903    [fRPCPassword release];
904    if (password != NULL)
905    {
906        char fullPassword[passwordLength+1];
907        strncpy(fullPassword, password, passwordLength);
908        fullPassword[passwordLength] = '\0';
909        SecKeychainItemFreeContent(NULL, (void *)password);
910       
911        tr_sessionSetRPCPassword(fHandle, fullPassword);
912       
913        fRPCPassword = [[NSString alloc] initWithUTF8String: fullPassword];
914        [fRPCPasswordField setStringValue: fRPCPassword];
915    }
916    else
917        fRPCPassword = nil;
918}
919
920- (void) setRPCPort: (id) sender
921{
922    int port = [sender intValue];
923    [fDefaults setInteger: port forKey: @"RPCPort"];
924    tr_sessionSetRPCPort(fHandle, port);
925   
926    [self setRPCWebUIDiscovery: nil];
927}
928
929- (void) setRPCUseWhitelist: (id) sender
930{
931    tr_sessionSetRPCWhitelistEnabled(fHandle, [fDefaults boolForKey: @"RPCUseWhitelist"]);
932}
933
934- (void) setRPCWebUIDiscovery: (id) sender
935{
936    if ([fDefaults boolForKey:@"RPC"] && [fDefaults boolForKey: @"RPCWebDiscovery"])
937        [[BonjourController defaultController] startWithPort: [fDefaults integerForKey: @"RPCPort"]];
938    else
939        [[BonjourController defaultController] stop];
940}
941
942- (void) updateRPCWhitelist
943{
944    NSString * string = [fRPCWhitelistArray componentsJoinedByString: @","];
945    tr_sessionSetRPCWhitelist(fHandle, [string UTF8String]);
946}
947
948- (void) addRemoveRPCIP: (id) sender
949{
950    //don't allow add/remove when currently adding - it leads to weird results
951    if ([fRPCWhitelistTable editedRow] != -1)
952        return;
953   
954    if ([[sender cell] tagForSegment: [sender selectedSegment]] == RPC_IP_REMOVE_TAG)
955    {
956        [fRPCWhitelistArray removeObjectsAtIndexes: [fRPCWhitelistTable selectedRowIndexes]];
957        [fRPCWhitelistTable deselectAll: self];
958        [fRPCWhitelistTable reloadData];
959       
960        [fDefaults setObject: fRPCWhitelistArray forKey: @"RPCWhitelist"];
961        [self updateRPCWhitelist];
962    }
963    else
964    {
965        [fRPCWhitelistArray addObject: @""];
966        [fRPCWhitelistTable reloadData];
967       
968        int row = [fRPCWhitelistArray count] - 1;
969        [fRPCWhitelistTable selectRow: row byExtendingSelection: NO];
970        [fRPCWhitelistTable editColumn: 0 row: row withEvent: nil select: YES];
971    }
972}
973
974- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
975{
976    return [fRPCWhitelistArray count];
977}
978
979- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
980{
981    return [fRPCWhitelistArray objectAtIndex: row];
982}
983
984- (void) tableView: (NSTableView *) tableView setObjectValue: (id) object forTableColumn: (NSTableColumn *) tableColumn
985    row: (NSInteger) row
986{
987    NSArray * components = [object componentsSeparatedByString: @"."];
988    NSMutableArray * newComponents = [NSMutableArray arrayWithCapacity: 4];
989       
990    //create better-formatted ip string
991    BOOL valid = false;
992    if ([components count] == 4)
993    {
994        valid = true;
995        for (NSString * component in components)
996        {
997            if ([component isEqualToString: @"*"])
998                [newComponents addObject: component];
999            else
1000            {
1001                int num = [component intValue];
1002                if (num >= 0 && num < 256)
1003                    [newComponents addObject: [[NSNumber numberWithInt: num] stringValue]];
1004                else
1005                {
1006                    valid = false;
1007                    break;
1008                }
1009            }
1010        }
1011    }
1012   
1013    NSString * newIP;
1014    if (valid)
1015    {
1016        newIP = [newComponents componentsJoinedByString: @"."];
1017       
1018        //don't allow the same ip address
1019        if ([fRPCWhitelistArray containsObject: newIP] && ![[fRPCWhitelistArray objectAtIndex: row] isEqualToString: newIP])
1020            valid = false;
1021    }
1022   
1023    if (valid)
1024    {
1025        [fRPCWhitelistArray replaceObjectAtIndex: row withObject: newIP];
1026        [fRPCWhitelistArray sortUsingSelector: @selector(compareNumeric:)];
1027    }
1028    else
1029    {
1030        NSBeep();
1031        if ([[fRPCWhitelistArray objectAtIndex: row] isEqualToString: @""])
1032            [fRPCWhitelistArray removeObjectAtIndex: row];
1033    }
1034       
1035    [fRPCWhitelistTable deselectAll: self];
1036    [fRPCWhitelistTable reloadData];
1037   
1038    [fDefaults setObject: fRPCWhitelistArray forKey: @"RPCWhitelist"];
1039    [self updateRPCWhitelist];
1040}
1041
1042- (void) tableViewSelectionDidChange: (NSNotification *) notification
1043{
1044    [fRPCAddRemoveControl setEnabled: [fRPCWhitelistTable numberOfSelectedRows] > 0 forSegment: RPC_IP_REMOVE_TAG];
1045}
1046
1047- (void) helpForPeers: (id) sender
1048{
1049    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PeersPrefs"
1050        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
1051}
1052
1053- (void) helpForNetwork: (id) sender
1054{
1055    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"NetworkPrefs"
1056        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
1057}
1058
1059- (void) helpForRemote: (id) sender
1060{
1061    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"RemotePrefs"
1062        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
1063}
1064
1065- (void) rpcUpdatePrefs
1066{
1067    //encryption
1068    const tr_encryption_mode encryptionMode = tr_sessionGetEncryption(fHandle);
1069    [fDefaults setBool: encryptionMode != TR_CLEAR_PREFERRED forKey: @"EncryptionPrefer"];
1070    [fDefaults setBool: encryptionMode == TR_ENCRYPTION_REQUIRED forKey: @"EncryptionRequire"];
1071   
1072    //download directory
1073    NSString * downloadLocation = [[NSString stringWithUTF8String: tr_sessionGetDownloadDir(fHandle)] stringByStandardizingPath];
1074    [fDefaults setObject: downloadLocation forKey: @"DownloadFolder"];
1075   
1076    //peers
1077    const uint16_t peersTotal = tr_sessionGetPeerLimit(fHandle);
1078    [fDefaults setInteger: peersTotal forKey: @"PeersTotal"];
1079   
1080    const uint16_t peersTorrent = tr_sessionGetPeerLimitPerTorrent(fHandle);
1081    [fDefaults setInteger: peersTotal forKey: @"PeersTorrent"];
1082   
1083    //pex
1084    const BOOL pex = tr_sessionIsPexEnabled(fHandle);
1085    [fDefaults setBool: pex forKey: @"PEXGlobal"];
1086   
1087    //port
1088    const tr_port port = tr_sessionGetPeerPort(fHandle);
1089    [fDefaults setInteger: port forKey: @"BindPort"];
1090   
1091    const BOOL nat = tr_sessionIsPortForwardingEnabled(fHandle);
1092    [fDefaults setBool: nat forKey: @"NatTraversal"];
1093   
1094    fPeerPort = -1;
1095    fNatStatus = -1;
1096    [self updatePortStatus];
1097   
1098    const BOOL randomPort = tr_sessionGetPeerPortRandomOnStart(fHandle);
1099    [fDefaults setBool: randomPort forKey: @"RandomPort"];
1100   
1101    //speed limit - down
1102    const BOOL downLimitEnabled = tr_sessionIsSpeedLimited(fHandle, TR_DOWN);
1103    [fDefaults setBool: downLimitEnabled forKey: @"CheckDownload"];
1104   
1105    const int downLimit = tr_sessionGetSpeedLimit(fHandle, TR_DOWN);
1106    [fDefaults setInteger: downLimit forKey: @"DownloadLimit"];
1107   
1108    //speed limit - up
1109    const BOOL upLimitEnabled = tr_sessionIsSpeedLimited(fHandle, TR_UP);
1110    [fDefaults setBool: upLimitEnabled forKey: @"CheckUpload"];
1111   
1112    const int upLimit = tr_sessionGetSpeedLimit(fHandle, TR_UP);
1113    [fDefaults setInteger: upLimit forKey: @"UploadLimit"];
1114   
1115    //alt speed limit enabled
1116    const BOOL useAltSpeed = tr_sessionUsesAltSpeed(fHandle);
1117    [fDefaults setBool: useAltSpeed forKey: @"SpeedLimit"];
1118   
1119    //alt speed limit - down
1120    const int downLimitAlt = tr_sessionGetAltSpeed(fHandle, TR_DOWN);
1121    [fDefaults setInteger: downLimit forKey: @"SpeedLimitDownloadLimit"];
1122   
1123    //alt speed limit - up
1124    const int upLimitAlt = tr_sessionGetAltSpeed(fHandle, TR_UP);
1125    [fDefaults setInteger: upLimitAlt forKey: @"SpeedLimitDownloadLimit"];
1126   
1127    //alt speed limit schedule
1128    const BOOL useAltSpeedSched = tr_sessionUsesAltSpeedTime(fHandle);
1129    [fDefaults setBool: useAltSpeedSched forKey: @"SpeedLimitAuto"];
1130   
1131    NSDate * limitStartDate = [PrefsController timeSumToDate: tr_sessionGetAltSpeedBegin(fHandle)];
1132    [fDefaults setObject: limitStartDate forKey: @"SpeedLimitAutoOnDate"];
1133   
1134    NSDate * limitEndDate = [PrefsController timeSumToDate: tr_sessionGetAltSpeedEnd(fHandle)];
1135    [fDefaults setObject: limitEndDate forKey: @"SpeedLimitAutoOffDate"];
1136   
1137    const int limitDay = tr_sessionGetAltSpeedDay(fHandle);
1138    [fDefaults setInteger: limitDay forKey: @"SpeedLimitAutoDay"];
1139   
1140    //blocklist
1141    const BOOL blocklist = tr_blocklistIsEnabled(fHandle);
1142    [fDefaults setBool: blocklist forKey: @"Blocklist"];
1143   
1144    //seed ratio
1145    const BOOL ratioLimited = tr_sessionIsRatioLimited(fHandle);
1146    [fDefaults setBool: ratioLimited forKey: @"RatioCheck"];
1147   
1148    const float ratioLimit = tr_sessionGetRatioLimit(fHandle);
1149    [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
1150   
1151    //update gui if loaded
1152    if (fHasLoaded)
1153    {
1154        //encryption handled by bindings
1155       
1156        //download directory handled by bindings
1157       
1158        [fPeersGlobalField setIntValue: peersTotal];
1159        [fPeersTorrentField setIntValue: peersTorrent];
1160       
1161        //pex handled by bindings
1162       
1163        [fPortField setIntValue: port];
1164        //port forwarding (nat) handled by bindings
1165        //random port handled by bindings
1166       
1167        //limit check handled by bindings
1168        [fDownloadField setIntValue: downLimit];
1169       
1170        //limit check handled by bindings
1171        [fUploadField setIntValue: upLimit];
1172       
1173        [fSpeedLimitDownloadField setIntValue: downLimitAlt];
1174       
1175        [fSpeedLimitUploadField setIntValue: upLimitAlt];
1176       
1177        //speed limit schedule handled by bindings
1178       
1179        //speed limit schedule times and day handled by bindings
1180       
1181        [self updateBlocklistFields];
1182       
1183        //ratio limit enabled handled by bindings
1184        [fRatioStopField setFloatValue: ratioLimit];
1185    }
1186   
1187    [[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
1188}
1189
1190@end
1191
1192@implementation PrefsController (Private)
1193
1194- (void) setPrefView: (id) sender
1195{
1196    NSString * identifier;
1197    if (sender)
1198    {
1199        identifier = [sender itemIdentifier];
1200        [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"SelectedPrefView"];
1201    }
1202    else
1203        identifier = [[NSUserDefaults standardUserDefaults] stringForKey: @"SelectedPrefView"];
1204   
1205    NSView * view;
1206    if ([identifier isEqualToString: TOOLBAR_TRANSFERS])
1207        view = fTransfersView;
1208    else if ([identifier isEqualToString: TOOLBAR_GROUPS])
1209        view = fGroupsView;
1210    else if ([identifier isEqualToString: TOOLBAR_BANDWIDTH])
1211        view = fBandwidthView;
1212    else if ([identifier isEqualToString: TOOLBAR_PEERS])
1213        view = fPeersView;
1214    else if ([identifier isEqualToString: TOOLBAR_NETWORK])
1215        view = fNetworkView;
1216    else if ([identifier isEqualToString: TOOLBAR_REMOTE])
1217        view = fRemoteView;
1218    else
1219    {
1220        identifier = TOOLBAR_GENERAL; //general view is the default selected
1221        view = fGeneralView;
1222    }
1223   
1224    [[[self window] toolbar] setSelectedItemIdentifier: identifier];
1225   
1226    NSWindow * window = [self window];
1227    if ([window contentView] == view)
1228        return;
1229   
1230    NSRect windowRect = [window frame];
1231    float difference = ([view frame].size.height - [[window contentView] frame].size.height) * [window userSpaceScaleFactor];
1232    windowRect.origin.y -= difference;
1233    windowRect.size.height += difference;
1234   
1235    [view setHidden: YES];
1236    [window setContentView: view];
1237    [window setFrame: windowRect display: YES animate: YES];
1238    [view setHidden: NO];
1239   
1240    //set title label
1241    if (sender)
1242        [window setTitle: [sender label]];
1243    else
1244    {
1245        NSToolbar * toolbar = [window toolbar];
1246        NSString * itemIdentifier = [toolbar selectedItemIdentifier];
1247        for (NSToolbarItem * item in [toolbar items])
1248            if ([[item itemIdentifier] isEqualToString: itemIdentifier])
1249            {
1250                [window setTitle: [item label]];
1251                break;
1252            }
1253    }
1254}
1255
1256- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
1257{
1258    if (code == NSOKButton)
1259    {
1260        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
1261       
1262        NSString * folder = [[openPanel filenames] objectAtIndex: 0];
1263        [fDefaults setObject: folder forKey: @"DownloadFolder"];
1264        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
1265       
1266        tr_sessionSetDownloadDir(fHandle, [folder UTF8String]);
1267    }
1268    else
1269    {
1270        //reset if cancelled
1271        [fFolderPopUp selectItemAtIndex: [fDefaults boolForKey: @"DownloadLocationConstant"] ? DOWNLOAD_FOLDER : DOWNLOAD_TORRENT];
1272    }
1273}
1274
1275- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
1276{
1277    if (code == NSOKButton)
1278        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"IncompleteDownloadFolder"];
1279    [fIncompleteFolderPopUp selectItemAtIndex: 0];
1280}
1281
1282- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
1283{
1284    NSString * path = [fDefaults stringForKey: @"AutoImportDirectory"];
1285    if (code == NSOKButton)
1286    {
1287        UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
1288        if (path)
1289            [sharedQueue removePathFromQueue: [path stringByExpandingTildeInPath]];
1290       
1291        path = [[openPanel filenames] objectAtIndex: 0];
1292        [fDefaults setObject: path forKey: @"AutoImportDirectory"];
1293        [sharedQueue addPath: [path stringByExpandingTildeInPath]];
1294       
1295        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
1296    }
1297    else if (!path)
1298        [fDefaults setBool: NO forKey: @"AutoImport"];
1299   
1300    [fImportFolderPopUp selectItemAtIndex: 0];
1301}
1302
1303- (void) setKeychainPassword: (const char *) password forService: (const char *) service username: (const char *) username
1304{
1305    SecKeychainItemRef item = NULL;
1306    NSUInteger passwordLength = strlen(password);
1307   
1308    OSStatus result = SecKeychainFindGenericPassword(NULL, strlen(service), service, strlen(username), username, NULL, NULL, &item);
1309    if (result == noErr && item)
1310    {
1311        if (passwordLength > 0) //found, so update
1312        {
1313            result = SecKeychainItemModifyAttributesAndData(item, NULL, passwordLength, (const void *)password);
1314            if (result != noErr)
1315                NSLog(@"Problem updating Keychain item: %s", GetMacOSStatusErrorString(result));
1316        }
1317        else //remove the item
1318        {
1319            result = SecKeychainItemDelete(item);
1320            if (result != noErr)
1321                NSLog(@"Problem removing Keychain item: %s", GetMacOSStatusErrorString(result));
1322        }
1323    }
1324    else if (result == errSecItemNotFound) //not found, so add
1325    {
1326        if (passwordLength > 0)
1327        {
1328            result = SecKeychainAddGenericPassword(NULL, strlen(service), service, strlen(username), username,
1329                        passwordLength, (const void *)password, NULL);
1330            if (result != noErr)
1331                NSLog(@"Problem adding Keychain item: %s", GetMacOSStatusErrorString(result));
1332        }
1333    }
1334    else
1335        NSLog(@"Problem accessing Keychain: %s", GetMacOSStatusErrorString(result));
1336}
1337
1338@end
Note: See TracBrowser for help on using the repository browser.