source: trunk/macosx/PrefsController.m @ 9020

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

on 10.6 use localizedStringFromDate:dateStyle:timeStyle: for formatting the blocklist update date

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